Ok, I tested it with success.

I apply the patch on top of the following commits:
- Emacs: 4ece0e2e46b
- Org-mode: ff33d9ec2

And I have followed your instruction.


Ihor Radchenko <yanta...@posteo.net> writes:

> Sébastien Gendre <s...@k-7.ch> writes:
>
>> Can someone regroup the patch I need to apply ?
>>
>> I'm a bit lost between them.
>
> I am attaching two files here:
> 1. display-indirect-buffer.diff
> 2. 0001-org-agenda-goto-Reuse-visible-indirect-buffer-window.patch
>
> You should
> 1. Download Emacs git repo
> 2. Apply display-indirect-buffer.diff on top
> 3. Re-compile Emacs from the repo
> 4. Download Org git repo
> 5. Apply 0001-org-agenda-goto-Reuse-visible-indirect-buffer-window.patch
>    on top
> 6. Run Emacs from git repo and load Org from the Org git repo to test
>    things with your setup.
>
> Hope the steps are not too confusing.
>
> diff --git a/lisp/window.el b/lisp/window.el
> index e9d57652ec6..5178e302238 100644
> --- a/lisp/window.el
> +++ b/lisp/window.el
> @@ -2615,7 +2615,36 @@ get-largest-window
>         (setq best-window window))))
>      best-window))
>  
> -(defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
> +(defun window-indirect-buffer-p (&optional window buffer-or-name)
> +  "Return non-nil if specified WINDOW is indirectly related to 
> BUFFER-OR-NAME.
> +WINDOW must be a live window and defaults to the selected window.
> +BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
> +defaults to the current buffer.
> +
> +WINODW is indirectly related to BUFFER-OR-NAME if one of the following
> +conditions hold:
> +
> +- BUFFER-OR-NAME specifies an indirect buffer and WINDOW's buffer is its
> +  base buffer.
> +
> +- WINDOW's buffer is an indirect buffer whose base buffer is the buffer
> +  specified by BUFFER-OR-NAME.
> +
> +- Both, WINDOW's buffer and the buffer specified by BUFFER-OR-NAME, are
> +  indirect buffer's sharing the same base buffer.
> +
> +Return nil if none of the above holds."
> +  (let* ((window (window-normalize-window window t))
> +      (window-buffer (window-buffer window))
> +      (window-base-buffer (buffer-base-buffer window-buffer))
> +      (buffer (window-normalize-buffer buffer-or-name))
> +      (buffer-base-buffer (buffer-base-buffer buffer)))
> +    (or (eq buffer-base-buffer window-buffer)
> +     (eq window-base-buffer buffer)
> +     (and buffer-base-buffer
> +          (eq buffer-base-buffer window-base-buffer)))))
> +
> +(defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames 
> indirect)
>    "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
>  BUFFER-OR-NAME may be a buffer or the name of an existing buffer
>  and defaults to the current buffer.  If the selected window displays
> @@ -2644,12 +2673,23 @@ get-buffer-window-list
>  - A frame means consider all windows on that frame only.
>  
>  Anything else means consider all windows on the selected frame
> -and no others."
> +and no others.
> +
> +INDIRECT non-nil means to append to the list of windows showing
> +BUFFER-OR-NAME a list of all windows that are indirectly related to
> +BUFFER-OR-NAME, that is, windows for which `window-indirect-buffer-p'
> +with the window and the buffer specified by BUFFER-OR-NAME as arguments
> +returns non-nil."
>    (let ((buffer (window-normalize-buffer buffer-or-name))
> +     (window-list (window-list-1 (selected-window) minibuf all-frames))
>       windows)
> -    (dolist (window (window-list-1 (selected-window) minibuf all-frames))
> +    (dolist (window window-list)
>        (when (eq (window-buffer window) buffer)
>       (setq windows (cons window windows))))
> +    (when indirect
> +      (dolist (window window-list)
> +     (when (window-indirect-buffer-p window buffer)
> +       (setq windows (cons window windows)))))
>      (nreverse windows)))
>  
>  (defun minibuffer-window-active-p (window)
> @@ -8235,35 +8275,57 @@ display-buffer-reuse-window
>  event that a window on another frame is chosen, avoid raising
>  that frame.
>  
> +If ALIST has a non-nil `reuse-indirect' entry and no window showing
> +BUFFER has been found, try to find a window that is indirectly related
> +to BUFFER and return that window.  This would be a window for which
> +`window-indirect-buffer-p' with the window and BUFFER as arguments
> +returns non-nil.  If a suitable window has been found and the cdr of the
> +entry equals the symbol `buffer', do not replace the buffer of the
> +chosen window with BUFFER but return the window with its old buffer in
> +place.  Otherwise, put BUFFER into the chosen window and return the
> +window.
> +
>  This is an action function for buffer display, see Info
>  node `(elisp) Buffer Display Action Functions'.  It should be
>  called only by `display-buffer' or a function directly or
>  indirectly called by the latter."
> -  (let* ((alist-entry (assq 'reusable-frames alist))
> -      (frames (cond (alist-entry (cdr alist-entry))
> +  (let* ((reusable-frames (assq 'reusable-frames alist))
> +      (reuse-indirect (assq 'reuse-indirect alist))
> +      (frames (cond (reusable-frames (cdr reusable-frames))
>                      ((window--pop-up-frames alist)
>                       0)
>                      (display-buffer-reuse-frames 0)
>                      (t (last-nonminibuffer-frame))))
> -      (window (if (and (eq buffer (window-buffer))
> -                       (not (cdr (assq 'inhibit-same-window alist))))
> -                  (selected-window)
> -                   ;; Preferably use a window on the selected frame,
> -                   ;; if such a window exists (Bug#36680).
> -                   (let* ((windows (delq (selected-window)
> -                                         (get-buffer-window-list
> -                                          buffer 'nomini frames)))
> -                          (first (car windows))
> -                          (this-frame (selected-frame)))
> -                     (cond
> -                      ((eq (window-frame first) this-frame)
> -                       first)
> -                      ((catch 'found
> -                         (dolist (next (cdr windows))
> -                           (when (eq (window-frame next) this-frame)
> -                             (throw 'found next)))))
> -                      (t first))))))
> +      (inhibit-same (cdr (assq 'inhibit-same-window alist)))
> +      (window
> +       ;; Avoid calling 'get-buffer-window-list' if the selected
> +       ;; window already shows BUFFER and can be used.
> +       (if (and (eq buffer (window-buffer)) (not inhibit-same))
> +           (selected-window)
> +            ;; Preferably use a window on the selected frame,
> +            ;; if such a window exists (Bug#36680).
> +            (let* ((windows-raw
> +                 (get-buffer-window-list
> +                     buffer 'nomini frames reuse-indirect))
> +                (windows (if inhibit-same
> +                             (delq (selected-window) windows-raw)
> +                           windows-raw))
> +                   (first (car windows))
> +                   (this-frame (selected-frame)))
> +              (cond
> +               ((eq (window-frame first) this-frame)
> +                first)
> +               ((catch 'found
> +                  (dolist (next (cdr windows))
> +                    (when (eq (window-frame next) this-frame)
> +                      (throw 'found next)))))
> +               (t first))))))
>      (when (window-live-p window)
> +      (when (and (eq (cdr reuse-indirect) 'buffer)
> +              (not (eq (window-buffer window) buffer)))
> +     ;; Pretend we were asking for a window showing the buffer of
> +     ;; that window.
> +     (setq buffer (window-buffer window)))
>        (prog1 (window--display-buffer buffer window 'reuse alist)
>       (unless (cdr (assq 'inhibit-switch-frame alist))
>         (window--maybe-raise-frame (window-frame window)))))))
>
>>From df57e6e25f843c94102ceba7c992d6bf775acb8d Mon Sep 17 00:00:00 2001
> Message-ID: 
> <df57e6e25f843c94102ceba7c992d6bf775acb8d.1739628844.git.yanta...@posteo.net>
> From: Ihor Radchenko <yanta...@posteo.net>
> Date: Sat, 15 Feb 2025 15:10:51 +0100
> Subject: [PATCH] org-agenda-goto: Reuse visible indirect buffer window; try
>  keeping narrowing
> MIME-Version: 1.0
> Content-Type: text/plain; charset=UTF-8
> Content-Transfer-Encoding: 8bit
>
> * lisp/org-agenda.el (org-agenda-goto): When jumping to agenda item,
> the item's indirect buffer is displayed, reuse it.  If the item's
> buffer is narrowed and the item position is within the narrowing, do
> not widen.
>
> Reusing indirect buffer requires Emacs support for reuse-indirect
> option in the buffer alist (see the link).
>
> Reported-by: Sébastien Gendre <s...@k-7.ch>
> Link: https://yhetil.org/emacs-devel/87msgx7yo5.fsf@localhost/
> ---
>  lisp/org-agenda.el | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
> index 8143fd97a7..9872382c4d 100644
> --- a/lisp/org-agenda.el
> +++ b/lisp/org-agenda.el
> @@ -9258,8 +9258,12 @@ (defun org-agenda-goto (&optional highlight)
>                    (org-agenda-error)))
>        (buffer (marker-buffer marker))
>        (pos (marker-position marker)))
> -    (switch-to-buffer-other-window buffer)
> -    (widen)
> +    (pop-to-buffer
> +     buffer
> +     `((display-buffer-reuse-window display-buffer-pop-up-window)
> +       (reuse-indirect . buffer)))
> +    (unless (<= (point-min) pos (point-max))
> +      (widen))
>      (push-mark)
>      (goto-char pos)
>      (when (derived-mode-p 'org-mode)
> -- 
> 2.47.1

Attachment: signature.asc
Description: PGP signature

Reply via email to