Lewis Baker <lewissba...@gmail.com> writes: > In org-capture--fill-template there is a section that creates a new buffer > named "*Capture*" and then activates this buffer so that it can insert the > template and perform various regexp search/replace operations on the > placeholders. However, the activation of this temporary buffer is performed > using (switch-to-buffer-other-window (get-buffer-create "*Capture*")) which > internally ends up dispatching to (display-buffer). > > The problem with this is that it can modify the window layout, which can > cause existing windows to be resized, even if only temporarily. When a > window is resized, this can in turn cause (point) in the displayed buffer > to be changed so that (point) remains on-screen. > ...
Sounds plausible. > I managed to fix the behaviour locally by applying the following change to > org-capture--fill-template: > > --- a/lisp/org-capture.el > +++ b/lisp/org-capture.el > @@ -1696,7 +1696,7 @@ Expansion occurs in a temporary Org mode buffer." > (message "no template") (ding) > (sit-for 1)) > (save-window-excursion > - (switch-to-buffer-other-window (get-buffer-create "*Capture*")) > + (set-buffer (get-buffer-create "*Capture*")) > (erase-buffer) > (setq buffer-file-name nil) > (setq mark-active nil) > > This activates the buffer without changing the window layout. > Does this change/fix seem reasonable? > > I suspect that the (save-window-excursion) should probably also be changed > to a (save-excursion), but was unable to convince myself so far that there > weren't other window-affecting operations in there. AFAIK, `switch-to-buffer-other-window' there is intentional. In particular, it is useful when the template expansion involves interactive prompt where we want the user to see the current state of *Capture* buffer and also the original window where the capture command was called. I think that the best we can do here on Org side is to document this potential pitfall. Tentative patch attached.
>From b4e21345770183f7263ba523178fd86e77db8081 Mon Sep 17 00:00:00 2001 Message-ID: <b4e21345770183f7263ba523178fd86e77db8081.1753012415.git.yanta...@posteo.net> From: Ihor Radchenko <yanta...@posteo.net> Date: Sun, 20 Jul 2025 13:52:25 +0200 Subject: [PATCH] org-capture-fill-template: Clarify that point may move as a side effect * lisp/org-capture.el (org-capture-fill-template): Update the docstring, warning that calling the function might lead to point moving in displayed buffers. Reported-by: Lewis Baker <lewissba...@gmail.com> Link: https://orgmode.org/list/calqfjj6gmrqkgxksv7bk4h6yrwwilmsv2-e1zz+gd6y-_oa...@mail.gmail.com --- lisp/org-capture.el | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lisp/org-capture.el b/lisp/org-capture.el index 02a1ea833..580bf5f19 100644 --- a/lisp/org-capture.el +++ b/lisp/org-capture.el @@ -1714,7 +1714,10 @@ (defun org-capture-fill-template (&optional template initial annotation) INITIAL content and/or ANNOTATION may be specified, but will be overridden by their respective `org-store-link-plist' properties if present. -Expansion occurs in a temporary Org mode buffer." +Expansion occurs in a temporary Org mode buffer that will be displayed +if the template expansion triggers user prompt. Beware that displaying +the temporary buffer may alter point position in the already displayed +buffers." (let* ((template (or template (org-capture-get :template))) (buffer (org-capture-get :buffer)) (file (buffer-file-name (or (buffer-base-buffer buffer) buffer))) -- 2.50.1
-- Ihor Radchenko // yantar92, Org mode maintainer, Learn more about Org mode at <https://orgmode.org/>. Support Org development at <https://liberapay.com/org-mode>, or support my work at <https://liberapay.com/yantar92>