From: Eli Zaretskii Date: Thu, 24 Apr 2025 12:56:23 +0000 (+0300) Subject: Allow to disable 'lexical-binding' X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=b6d0808afcebcfa08b81a6059777dba1550d52b3;p=emacs.git Allow to disable 'lexical-binding' * lisp/progmodes/elisp-mode.el (elisp-enable-lexical-binding): Optionally, allow to disable 'lexical-binding'. (cherry picked from commit eb6531ba942042a426390835ebddc83b7936d60f) --- diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el index 8965e5d6587..a9794579fe8 100644 --- a/lisp/progmodes/elisp-mode.el +++ b/lisp/progmodes/elisp-mode.el @@ -312,18 +312,36 @@ Comments in the form will be lost." (defun elisp-enable-lexical-binding (&optional interactive) "Make the current buffer use `lexical-binding'. +With a prefix argument \\[universal-argument], make the buffer use +dynamic binding instead. +In addition to setting the value of `lexical-binding' in the buffer, +this function adds the lexbind cookie to the first line of the buffer, +if it is not already there, so that saving the buffer to its file +will cause Emacs to use the specified value of `lexical-binding' +when the file is loaded henceforth. INTERACTIVE non-nil means ask the user for confirmation; this -happens in interactive invocations." +happens in interactive invocations. +When calling from Lisp, use nil or a positive number as the value +of INTERACTIVE to enable `lexical-binding', a negative number to +disable it." (interactive "p") - (if (and (local-variable-p 'lexical-binding) lexical-binding) - (when interactive - (message "lexical-binding already enabled!") - (ding)) - (when (or (not interactive) - (y-or-n-p (format "Enable lexical-binding in this %s? " - (if buffer-file-name "file" "buffer")))) - (setq-local lexical-binding t) - (add-file-local-variable-prop-line 'lexical-binding t interactive)))) + (let* ((disable-lexbind (or (and (numberp interactive) + (< interactive 0)) + (if current-prefix-arg t))) + (required-value (not disable-lexbind))) + (if (and (local-variable-p 'lexical-binding) + (null (xor required-value lexical-binding))) + (when interactive + (message "lexical-binding already %s!" + (if disable-lexbind "disabled" "enabled")) + (ding)) + (when (or (not interactive) + (y-or-n-p (format "%s lexical-binding in this %s? " + (if disable-lexbind "Disable" "Enable") + (if buffer-file-name "file" "buffer")))) + (setq-local lexical-binding required-value) + (add-file-local-variable-prop-line 'lexical-binding required-value + interactive))))) (defvar-keymap elisp--dynlex-modeline-map " " #'elisp-enable-lexical-binding)