unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Wolfgang Scherer <Wolfgang.Scherer@gmx.de>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 61925@debbugs.gnu.org
Subject: bug#61925: 30.0.50; normal-mode does not set up local variables in temporary buffers (scratch, with-temp-buffer)
Date: Sun, 5 Mar 2023 06:03:53 +0100	[thread overview]
Message-ID: <b7102821-2282-063c-d321-33cc0459dce1@gmx.de> (raw)
In-Reply-To: <834jr0byz0.fsf@gnu.org>


> 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







  parent reply	other threads:[~2023-03-05  5:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-02 23:11 bug#61925: 30.0.50; normal-mode does not set up local variables in temporary buffers (scratch, with-temp-buffer) Wolfgang Scherer
2023-03-04 10:19 ` Eli Zaretskii
2023-03-05  4:58   ` Wolfgang Scherer
2023-03-05  5:03   ` Wolfgang Scherer [this message]
2023-03-06  2:07   ` Wolfgang Scherer
2024-01-10 11:08   ` Stefan Kangas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b7102821-2282-063c-d321-33cc0459dce1@gmx.de \
    --to=wolfgang.scherer@gmx.de \
    --cc=61925@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).