Subject: bug#69259: 30.0.50; (get-pos-property 9483 'cursor-intangible) gives args-out-of-range error in folded magit buffer

From: StrawberryTea <[email-masked]>
Date: Wed, 21 Feb 2024 17:29:54 -0600

Thanks for the explanation Stefan and Eli. I ended up making a PR to magit. And
I think Tarsius is going to merge it. He just asked me to test it after he made
a small change.

Sincerely,
StrawberryTea

modified   lisp/magit-section.el
@@ -1635,10 +1635,14 @@ evaluated its BODY.  Admittedly that’s a bit of a hack.“
   (setq magit-section-pre-command-section (magit-current-section)))

(defun magit-section-post-command-hook ()
• (cursor-sensor-move-to-tangible (selected-window))
• (when (or magit–context-menu-buffer
• magit–context-menu-section)
• (magit-menu-highlight-point-section))
⁃ (let ((window (selected-window)))
⁃ ;; The command may have used `set-window-buffer’ to change
⁃ ;; the window’s buffer without changing the current buffer.
⁃ (when (eq (current-buffer) (window-buffer window))
⁃ (cursor-sensor-move-to-tangible window)
⁃ (when (or magit–context-menu-buffer
⁃ magit–context-menu-section)
⁃ (magit-menu-highlight-point-section))))
  (unless (memq this-command ’(magit-refresh magit-refresh-all))
    (magit-section-update-highlight)))
Stefan Monnier <[email-masked]> writes:

> Eli wrote:
>> I think cursor-sensor is written under the assumption that the
>> selected window’s buffer is also the current buffer, and if so,
>> magit-post-command-hook should abide by that protocol.  Stefan, am I
>> right?
>
> Not `cursor-sensor.el` in its entirety, but
> `cursor-sensor-move-to-tangible` presumes that it is called with the
> current-buffer already set to (window-buffer window).
>
> StrawberryTea wrote:
>> Why exactly are the window-buffer and the current-buffer different?
>
> Good question.  AFAIK the code that runs `post-command-hook` normally
> tries to avoid such situations.
>
>> I think this has to do with persp-mode.  My guess is that when it sets
>> the new window configuration, the current-buffer is not updated to the
>> new window’s buffer immediately.
>
> Could be a “bad citizen” on `post-command-hook`, indeed, which messes up
> subsequent functions on the hook.  Maybe the code that runs
> `post-command-hook` should be more careful to (re)set current-buffer
> after each function, but it seems easier to fix the rare functions which
> mess it up.
>
>         Stefan