Hi all!

 I wanted to write a command which could modify an Org entry from the
 corresponding agenda item, and for that I tried to use
 `(org-with-point-at (org-get-at-bol 'org-hd-marker))` (ChatGPT's
 suggestion). But I started noticing that sometimes it did not work and
 it tried to edit instead the entry that was originally at point in the
 Org buffer. After some debugging I confirmed that this happened if and
 only if the original Org buffer was in a non-selected window (e.g., if
 the agenda had been invoked from that buffer). I tried to use
 `org-agenda-with-point-at-orig-entry` and exactly the same happen.

 After some debugging investigation I identified the issue: those
 macros only set the current buffer and the buffer point, but not
 the window point. If the provided body has an implicit redisplay (and
 mine did because it used `completing-read`), Emacs can decide that the
 buffer point and window point are inconsistent and overwrite the
 buffer point with the window point, so the `goto-char` in those
 macros is effectively undone.

 Coming up with a minimal reproduction example that manifest this
 it's hard, because redisplay logic it's complex and doesn't always
 re-syncs the buffer point. With my usual configuration for example,
 I could always reproduce the issue just with

 ```
  (org-with-point-at my-marker
    (redisplay t)
    (message "Point: %s. Original marker: %s" (point) my-marker))
 ```

 But when I tried with emacs -Q, I no longer could. Eventually, I
 narrowed down the configuration that made it happen and it turns
 out to be flycheck-mode, which is enabled by default in all buffers
 (even if Flycheck doesn't have a checker for Org buffers in my
 configuration). If Flycheck is enabled, the issue happens, and if it's not
 enabled, the issue doesn't happen. I guess Flycheck touches some
 buffer state in the background which makes Emacs think the
 reconciliation is needed. I tried to come up with some simpler
 function to add before the redisplay which forced the points to be
 reconciled later without flycheck mode enabled, but I couldn't.

 I don't really know enough of Emacs and Org Mode internals to
 determine whether this is a bug or just some necessary limitation
 (I just learned yesterday about the difference between buffer and
 window point after all, because of this), but I think it would be
 nice to protect against that scenario in those macros. I tried
 using `(with-current-buffer (clone-indirect-buffer nil nil)` inside
 them and that fixes it, with no undesired side effect to the best
 of my knowledge. If you agree, I can submit a patch, with that
 workaround or any other someone more knowledgeable proposes. Also,
 if there is concern we could start patching only
 `org-agenda-with-point-at-orig-entry`, which is not used that much.

 Let me know what you think

 Best regards!

 ------------------------------------------------------------------------


 Emacs  : GNU Emacs 29.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.41, 
cairo version 1.18.0)
 of 2025-10-14
 Package: Org mode version 9.6.6 (release_9.6.6 @ 
/usr/local/share/emacs/29.1/lisp/org/)

Reply via email to