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

From: Richard Hansen <[email-masked]>
Date: Wed, 29 Jun 2022 19:17:29 -0400

On 2022-06-27 12:54, Eli Zaretskii wrote:
>> I changed the test to switch to the temporary buffer in the 
>> selected window and it now reliably inserts "x" into the temporary 
>> buffer.  It still doesn't run the `after-change-functions' hook, 
>> however.  Updated test code:
>>
>> ;; -*- lexical-binding: t; -*-
>> (require 'ert)
>> (require 'ert-x)
>> (defvar-local acf 0)
>> (defun acf (&rest _) (setq-local acf (1+ acf)))
>> (ert-deftest test ()
>>     (ert-with-test-buffer ()
>>       (let ((b (current-buffer)))
>>         (save-window-excursion
>>           (with-current-buffer-window b
>>               `(display-buffer-below-selected
>>                 (body-function
>>                  . ,(lambda (window)
>>                       (select-window window t)
>>                       (should (eq (current-buffer) b))
>>                       (setq-local acf 0)
>>                       ;; Note that LOCAL is t:
>>                       (add-hook 'after-change-functions #'acf nil t)
>>                       (should (memq #'acf after-change-functions))
>>                       (execute-kbd-macro (kbd "x"))
>>                       (should (equal (buffer-string) "x"))
>>                       ;; The above checks pass, this check fails:
>>                       (should (equal acf 1)))))
>>               nil)))))
> 
> Your test calls with-current-buffer-window, which calls 
> temp-buffer-window-setup, which inhibits modification hooks:

I looked into this a bit more.  It turns out that `inhibit-modification-hooks' is actually nil in the body of `with-current-buffer-window', as expected.  It is t in the above test code because of `display-buffer-below-selected', which calls `window--display-buffer', which does the following:

     (when (functionp (cdr (assq 'body-function alist)))
       (let ((inhibit-read-only t)
             (inhibit-modification-hooks t))
         (funcall (cdr (assq 'body-function alist)) window)))

The `body-function' action alist entry was added in Emacs 28.1 by commit 3273e2ace78 [1] for bug#39822 [2].  Presumably the modification hooks are inhibited during the execution of `body-function' because `body-function' was added to replace the (now-deprecated) `with-displayed-buffer-window' macro, which inhibits them.  The `with-displayed-buffer-window' macro has inhibited them ever since the macro was first added to Emacs 25.1 by commit f0f70ec0bc5 [3] for bug#17809 [4].

I'm guessing that `body-function' is only intended for initial buffer set-up, thus modification hooks are inhibited.  However, the body of `with-current-buffer-window' is also only intended for initial buffer set-up (it actually runs before `body-function' and before the buffer is displayed), so the inconsistency is unexpected and awkward.  Either both should inhibit the modification hooks, or neither should.

[1] https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=3273e2ace788a58bef77cef936021d151815ea94
[2] https://debbugs.gnu.org/cgi/bugreport.cgi?bug=39822
[3] https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=f0f70ec0bc55e452ea29b5cf3f532740966b0192
[4] https://debbugs.gnu.org/cgi/bugreport.cgi?bug=17809