Subject: Re: bug#56210: 29.0.50; Keyboard macros do not trigger after-change-functions

From: Eli Zaretskii <[email-masked]>
Date: Sun, 26 Jun 2022 11:10:57 +0300

> Date: Sat, 25 Jun 2022 16:34:11 -0400
> Cc: [email-masked], [email-masked]
> From: Richard Hansen <[email-masked]>
> 
> On 2022-06-25 14:56, Eli Zaretskii wrote:
> > Maybe we should step back and ask what do you think the below do?
> > 
> >    (execute-kbd-macro (kbd "x"))
> > 
> > What is, in your mental model, the effect of the above in Emacs?
> 
> I would expect that to be roughly equivalent to typing "x" on the physical keyboard, which would insert an event into some sort of event queue.  (I'm not familiar enough with Emacs to know whether that queue is per-buffer, per-window, per-frame, per-terminal, per-process, or something else.)  Under normal circumstances, the event would be dispatched to the buffer associated with the `selected-window' of the `selected-frame', and insert an "x".

That is correct.

> I think my understanding of "current buffer" is incorrect.  My current mental model of `with-current-buffer' is that it behaves as if the window's associated buffer is briefly switched to the specified buffer (like "C-x b" does) except it doesn't actually go to the expense of rendering the other buffer in the window (or update the toolbar, etc.).  Looking at the documentation of `set-window-buffer', that mental model appears to be wrong.

Yes, with-temp-buffer doesn't associate the temporary buffer with the
selected window.  And execute-kbd-macro starts a command loop, which
always makes sure the selected-window's buffer is current.

> But if my mental model is wrong, why does my original code insert "x" into the temporary buffer? It is not due to something specific to `ert-with-test-buffer', because if I evaluate the following in *scratch* then I see the same behavior that Lars saw with `with-temp-buffer' (the "x" is inserted into *scratch*):
> 
>      (ert-with-test-buffer () (execute-kbd-macro (kbd "x")))
> 
> Maybe `ert-run-tests-batch-and-exit' is setting it up to more closely simulate a user interacting with Emacs?  Or maybe the behavior is caused by batch mode?

Could be, you will have to investigate what ERT does in those cases.
Its macro complexity is above my pay grade.

> Regardless, given that the temporary buffer is modified in my original example, shouldn't `after-change-functions' run?

Is the temporary buffer indeed modified, and is the hook set in that
buffer?