Show file names in lexbind warnings when loading themes

* lisp/international/mule.el (load-with-code-conversion):
* lisp/custom.el (load-theme): Set 'files--name-of-loading-file'
buffer-locally to record the name of the file being loaded.
* lisp/files.el (files--name-of-loading-file): New buffer-local
variable.
(internal--get-default-lexical-binding): Use it to show the name
of the file from which we load or evaluate Lisp code.  (Bug#81686)
This commit is contained in:
Eli Zaretskii 2026-08-31 14:53:29 +03:00
parent d3caf5fbd1
commit f3d4c95f0a
3 changed files with 20 additions and 8 deletions

View file

@ -1335,6 +1335,8 @@ This variable cannot be set in a Custom theme."
:risky t
:version "24.1")
(defvar files--name-of-loading-file)
(defun load-theme (theme &optional no-confirm no-enable)
"Load Custom theme named THEME from its file and possibly enable it.
The theme file is named THEME-theme.el, in one of the directories
@ -1397,6 +1399,7 @@ Return t if THEME was successfully loaded, nil otherwise."
(load (file-name-sans-extension file) nil t nil t))
((with-temp-buffer
(insert-file-contents file)
(setq files--name-of-loading-file file)
(let ((hash (secure-hash 'sha256 (current-buffer))))
(when (or (member hash custom-safe-themes)
(custom-theme-load-confirm hash))

View file

@ -4323,19 +4323,22 @@ all the specified local variables, but ignores any settings of \"mode:\"."
(push elem file-local-variables-alist)))
(hack-local-variables-apply))))))
(defvar-local files--name-of-loading-file nil
"File name of the file loaded/evaluated in the buffer.")
(defun internal--get-default-lexical-binding (from)
(let ((mib (lambda (node) (buttonize node (lambda (_) (info node))
nil "mouse-2: Jump to Info node"))))
(or (and (bufferp from) (zerop (buffer-size from)))
(and (stringp from)
(eql 0 (file-attribute-size (file-attributes from))))
(let ((source
(if (not (and (bufferp from)
(string-match-p "\\` \\*load\\*\\(-[0-9]+\\)?\\'"
(buffer-name from))
load-file-name))
from
(abbreviate-file-name load-file-name))))
(let* ((fname
(if (bufferp from)
(buffer-local-value 'files--name-of-loading-file from)))
(source
(if fname
(abbreviate-file-name fname)
from)))
(condition-case nil
(display-warning
`(files missing-lexbind-cookie

View file

@ -291,6 +291,8 @@ attribute."
(defvar hack-read-symbol-shorthands-function nil
"Holds function to compute `read-symbol-shorthands'.")
(defvar files--name-of-loading-file)
(defun load-with-code-conversion (fullname file &optional noerror nomessage
eval-function)
"Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
@ -350,7 +352,11 @@ Return t if file exists."
'raw-text)))
(set-buffer-multibyte nil))
;; Make `kill-buffer' quiet.
(set-buffer-modified-p nil))
(set-buffer-modified-p nil)
;; Record the name of the file in a buffer-local variable
;; that 'internal--get-default-lexical-binding' can use if
;; it needs to show a lexical-binding cookie warning.
(setq files--name-of-loading-file fullname))
;; Have the original buffer current while we eval,
;; but consider shorthands of the eval'ed one.
(let ((read-symbol-shorthands shorthands))