elisp-mode.el: Use handler-bind instead of debug-on-error

* lisp/progmodes/elisp-mode.el (elisp-enable-lexical-binding):
Don't get fooled by a global binding of `lexical-binding` to t.
(elisp--eval-last-sexp-fake-value): Delete var.
(elisp--eval-defun): Don't let-bind `debug-on-error` since it's
already arranged by the only caller.
(eval-last-sexp, eval-defun): Use `handler-bind` instead of
`debug-on-error`.
This commit is contained in:
Stefan Monnier 2024-02-23 16:46:01 -05:00
parent c0d7f7fc54
commit 84f72f19e5

View file

@ -309,7 +309,7 @@ Comments in the form will be lost."
INTERACTIVE non-nil means ask the user for confirmation; this
happens in interactive invocations."
(interactive "p")
(if lexical-binding
(if (and (local-variable-p 'lexical-binding) lexical-binding)
(when interactive
(message "lexical-binding already enabled!")
(ding))
@ -371,6 +371,12 @@ be used instead.
;; Font-locking support.
(defun elisp--font-lock-shorthand (_limit)
;; Add faces on shorthands between point and LIMIT.
;; ...
;; Return nil to tell font-lock, that there's nothing left to do.
nil)
(defun elisp--font-lock-flush-elisp-buffers (&optional file)
;; We're only ever called from after-load-functions, load-in-progress can
;; still be t in case of nested loads.
@ -1582,9 +1588,6 @@ character)."
(buffer-substring-no-properties beg end))
))))
(defvar elisp--eval-last-sexp-fake-value (make-symbol "t"))
(defun eval-sexp-add-defvars (exp &optional pos)
"Prepend EXP with all the `defvar's that precede it in the buffer.
POS specifies the starting position where EXP was found and defaults to point."
@ -1626,16 +1629,9 @@ integer value is also printed as a character of that codepoint.
If `eval-expression-debug-on-error' is non-nil, which is the default,
this command arranges for all errors to enter the debugger."
(interactive "P")
(if (null eval-expression-debug-on-error)
(values--store-value
(elisp--eval-last-sexp eval-last-sexp-arg-internal))
(let ((value
(let ((debug-on-error elisp--eval-last-sexp-fake-value))
(cons (elisp--eval-last-sexp eval-last-sexp-arg-internal)
debug-on-error))))
(unless (eq (cdr value) elisp--eval-last-sexp-fake-value)
(setq debug-on-error (cdr value)))
(car value))))
(values--store-value
(handler-bind ((error (if #'eval-expression--debug #'ignore)))
(elisp--eval-last-sexp eval-last-sexp-arg-internal))))
(defun elisp--eval-defun-1 (form)
"Treat some expressions in FORM specially.
@ -1694,8 +1690,7 @@ Return the result of evaluation."
;; FIXME: the print-length/level bindings should only be applied while
;; printing, not while evaluating.
(defvar elisp--eval-defun-result)
(let ((debug-on-error eval-expression-debug-on-error)
(edebugging edebug-all-defs)
(let ((edebugging edebug-all-defs)
elisp--eval-defun-result)
(save-excursion
;; Arrange for eval-region to "read" the (possibly) altered form.
@ -1774,15 +1769,8 @@ which see."
(defvar edebug-all-defs)
(eval-defun (not edebug-all-defs)))
(t
(if (null eval-expression-debug-on-error)
(elisp--eval-defun)
(let (new-value value)
(let ((debug-on-error elisp--eval-last-sexp-fake-value))
(setq value (elisp--eval-defun))
(setq new-value debug-on-error))
(unless (eq elisp--eval-last-sexp-fake-value new-value)
(setq debug-on-error new-value))
value)))))
(handler-bind ((error (if #'eval-expression--debug #'ignore)))
(elisp--eval-defun)))))
;;; ElDoc Support