From e5130bdecca00f8277640c10292036f99abee59d Mon Sep 17 00:00:00 2001 From: Pip Cet Date: Fri, 7 Nov 2025 09:12:55 +0000 Subject: [PATCH] The --script option now enforces lexical binding (bug#79760) * etc/NEWS: Document the change. * lisp/startup.el (command-line--load-script): Set 'lexical-binding'; if we delete the first line, insert a new line to preserve line numbers. (command-line--eval-script): Set 'lexical-binding'. --- etc/NEWS | 10 ++++++++++ lisp/startup.el | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index e490115a23f..d5a0c847142 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -3168,6 +3168,16 @@ and blocks Emacs, or does not provide ways to limit how often it runs. * Incompatible Lisp Changes in Emacs 31.1 +** Files loaded from '-x' and '--script' now use lexical binding. +If you don't have time to adapt your script's code to the lexical +binding dialect (see (info "(elisp)Converting to Lexical Binding")), +you can wrap your code in: + + #!/usr/bin/env -S emacs --batch --script + (eval + '(progn + YOUR CODE HERE)) + +++ ** String mutation has been restricted further. 'aset' on unibyte strings now requires the new character to be a single diff --git a/lisp/startup.el b/lisp/startup.el index aab8fcfe9f2..cdbfcc8acb7 100644 --- a/lisp/startup.el +++ b/lisp/startup.el @@ -2961,12 +2961,14 @@ nil default-directory" name) file file nil t (lambda (buffer file) (with-current-buffer buffer + (setq-local lexical-binding t) (goto-char (point-min)) ;; Removing the #! and then calling `eval-buffer' will make the ;; reader not signal an error if it then turns out that the ;; buffer is empty. (when (looking-at "#!") - (delete-line)) + (delete-line) + (insert ";; -*- lexical-binding: t -*-\n")) (eval-buffer buffer nil file nil t))))) (defun command-line--eval-script (file) @@ -2975,6 +2977,7 @@ nil default-directory" name) (lambda (buffer _) (with-current-buffer buffer (goto-char (point-min)) + (setq-local lexical-binding t) (when (looking-at "#!") (forward-line)) (let (value form)