Subject: Re: bug#61925: 30.0.50; normal-mode does not set up local variables in temporary buffers (scratch, with-temp-buffer)

From: Wolfgang Scherer <[email-masked]>
Date: Sun, 5 Mar 2023 06:03:53 +0100

> And AFAICT, it _is_ documented in the ELisp Reference manual:
>
>   -- Command: normal-mode &optional find-file
>       This function establishes the proper major mode and buffer-local
>       variable bindings for the current buffer.  It calls ‘set-auto-mode’
>       (see below).  As of Emacs 26.1, it no longer runs
>       ‘hack-local-variables’, this now being done in ‘run-mode-hooks’ at
>       the initialization of major modes (*note Mode Hooks::).
>
> And the documentation of run-mode-hooks says:
>
>
>   -- Function: run-mode-hooks &rest hookvars
>       Major modes should run their mode hook using this function.  It is
>       similar to ‘run-hooks’ (*note Hooks::), but it also runs
>       ‘change-major-mode-after-body-hook’, ‘hack-local-variables’ (when
>       the buffer is visiting a file) (*note File Local Variables::)  ^^^^
>       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Just for your information, the machinery in `normal-mode' and
`run-mode-hooks' is -- at least in some edge cases -- out of
sync.

Here is a case, where normal-mode in Emacs > 26.1 still calls
`hack-local-variables', which causes a discrepancy, depending on
`delay-mode-hooks', when `after-change-major-mode-hook' alters
the value of a local variable, which is also set in the local
variables list. The reason is, that in `normal-mode'
`after-change-major-mode-hook' is not called the same way as it
is in `run-mode-hooks'.

;; for all cases: `after-change-major-mode-hook' establishes local
;; variable and sets the value to "hello"

;; case 1:  buffer with `buffer-file-name' == nil, `delay-mode-hooks' case 1.1: nil, case 1.2: t

(check-normal-mode-in-temp-buffer nil nil nil t  ) ::go:: ;; => hello
(check-normal-mode-in-temp-buffer t   nil nil t  ) ::go:: ;; => good-bye

;; case 2: buffer with `buffer-file-name' not nil, `delay-mode-hooks' case 2.1: nil, case 2.2: t
(check-normal-mode-in-temp-buffer nil t   nil t  ) ::go:: ;; => hello
(check-normal-mode-in-temp-buffer t   t   nil t  ) ::go:: ;; => good-bye

The following is the complete test setup that I used for testing:

(defun check-normal-mode (&optional set-delay-mode-hooks set-buffer-file-name find-file)
   (interactive)
   (let ((buffer-file-name (or buffer-file-name (and set-buffer-file-name (or null-device "/dev/null"))))
         (delay-mode-hooks (if set-delay-mode-hooks t)))
     (normal-mode find-file)))

(defun check-normal-mode-in-temp-buffer (set-delay-mode-hooks set-buffer-file-name change-hook after-hook)
   (let ((hook-function #'(lambda () (make-local-variable 'wsx-here-i-am) (setq wsx-here-i-am "hello"))))
     (if change-hook
         (add-hook 'change-major-mode-after-body-hook hook-function)
       (remove-hook 'change-major-mode-after-body-hook hook-function))
     (if after-hook
         (add-hook 'after-change-major-mode-hook hook-function)
       (remove-hook 'after-change-major-mode-hook hook-function))
     (put 'wsx-here-i-am 'safe-local-variable 'string-or-null-p)
     (with-temp-buffer
       (insert ";; -*- mode: lisp-interaction; truncate-lines: t; wsx-here-i-am: \"good bye\"; -*-\n")
       (normal-mode)
       (check-normal-mode set-delay-mode-hooks set-buffer-file-name)
       (describe-variable 'wsx-here-i-am)
       )
     (remove-hook 'change-major-mode-after-body-hook hook-function)
     (remove-hook 'after-change-major-mode-hook hook-function)
     (put 'wsx-here-i-am 'safe-local-variable nil)))

;; Emacs >= 26.1

(check-normal-mode-in-temp-buffer nil nil nil nil) ::go:: ;; => void
(check-normal-mode-in-temp-buffer t   nil nil nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer nil t   nil nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer t   t   nil nil) ::go:: ;; => good-bye

(check-normal-mode-in-temp-buffer nil nil t   nil) ::go:: ;; => hello
(check-normal-mode-in-temp-buffer t   nil t   nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer nil t   t   nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer t   t   t   nil) ::go:: ;; => good-bye

(check-normal-mode-in-temp-buffer nil nil nil t  ) ::go:: ;; => hello
(check-normal-mode-in-temp-buffer t   nil nil t  ) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer nil t   nil t  ) ::go:: ;; => hello
(check-normal-mode-in-temp-buffer t   t   nil t  ) ::go:: ;; => good-bye

;; Emacs < 26.1

(check-normal-mode-in-temp-buffer nil nil nil nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer t   nil nil nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer nil t   nil nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer t   t   nil nil) ::go:: ;; => good-bye

(check-normal-mode-in-temp-buffer nil nil t   nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer t   nil t   nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer nil t   t   nil) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer t   t   t   nil) ::go:: ;; => good-bye

(check-normal-mode-in-temp-buffer nil nil nil t  ) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer t   nil nil t  ) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer nil t   nil t  ) ::go:: ;; => good-bye
(check-normal-mode-in-temp-buffer t   t   nil t  ) ::go:: ;; => good-bye