From: Lars Ingebrigtsen Date: Thu, 15 Oct 2020 14:26:40 +0000 (+0200) Subject: Make `C-x C-e' work more like `C-M-x' on defvar etc X-Git-Tag: emacs-28.0.90~5623 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c6ecf6428e8d4848a633782ed0605ddbacfcebee;p=emacs.git Make `C-x C-e' work more like `C-M-x' on defvar etc * doc/emacs/building.texi (Lisp Eval): Document it. * lisp/emacs-lisp/pp.el (pp-eval-last-sexp): Ditto. * lisp/progmodes/elisp-mode.el (elisp--eval-last-sexp): Work more like `eval-defun': Re-evaluate defvar/defcustom/defface forms. --- diff --git a/doc/emacs/building.texi b/doc/emacs/building.texi index 573b7ad71a2..3e09f243226 100644 --- a/doc/emacs/building.texi +++ b/doc/emacs/building.texi @@ -1672,21 +1672,26 @@ abbreviation of the output according to the variables argument of @code{-1} overrides the effect of @code{eval-expression-print-length}. + @kbd{C-x C-e} (@code{eval-last-sexp}) treats @code{defvar} +expressions specially. Normally, evaluating a @code{defvar} +expression does nothing if the variable it defines already has a +value. But this command unconditionally resets the variable to the +initial value specified by the @code{defvar}; this is convenient for +debugging Emacs Lisp programs. @code{defcustom} and @code{defface} +expressions are treated similarly. Note the other commands documented +in this section, except @code{eval-defun}, do not have this special +feature. + @kindex C-M-x @r{(Emacs Lisp mode)} @findex eval-defun The @code{eval-defun} command is bound to @kbd{C-M-x} in Emacs Lisp mode. It evaluates the top-level Lisp expression containing or following point, and prints the value in the echo area. In this context, a top-level expression is referred to as a ``defun'', but it -need not be an actual @code{defun} (function definition). In -particular, this command treats @code{defvar} expressions specially. -Normally, evaluating a @code{defvar} expression does nothing if the -variable it defines already has a value. But this command -unconditionally resets the variable to the initial value specified by -the @code{defvar}; this is convenient for debugging Emacs Lisp -programs. @code{defcustom} and @code{defface} expressions are treated -similarly. Note that the other commands documented in this section do -not have this special feature. +need not be an actual @code{defun} (function definition). + + This command handles @code{defvar}/@code{defcustom}/@code{defface} +forms the same way that @code{eval-last-sexp} does. With a prefix argument, @kbd{C-M-x} instruments the function definition for Edebug, the Emacs Lisp Debugger. @xref{Instrumenting, diff --git a/etc/NEWS b/etc/NEWS index 6b9105f577a..97e2e6f1d35 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -180,6 +180,12 @@ file during parsing" and dropping out of the minibuffer. The user would have to type 'M-: M-p' to edit and redo the expression. Now Emacs will echo the message and allow the user to continue editing. ++++ +** 'eval-last-sexp' now handles 'defvar'/'defcustom'/'defface' specially. +This command would previously not redefine values defined by these +forms, but this command has now been changed to work more like +'eval-defun', and reset the values as specified. + +++ ** New command 'undo-redo'. It undoes previous undo commands, but doesn't record itself as an diff --git a/lisp/emacs-lisp/pp.el b/lisp/emacs-lisp/pp.el index 3df7b0e368e..eb2ee94be3b 100644 --- a/lisp/emacs-lisp/pp.el +++ b/lisp/emacs-lisp/pp.el @@ -164,8 +164,11 @@ With argument, pretty-print output into current buffer. Ignores leading comment characters." (interactive "P") (if arg - (insert (pp-to-string (eval (pp-last-sexp) lexical-binding))) - (pp-eval-expression (pp-last-sexp)))) + (insert (pp-to-string (eval (elisp--eval-defun-1 + (macroexpand (pp-last-sexp))) + lexical-binding))) + (pp-eval-expression (elisp--eval-defun-1 + (macroexpand (pp-last-sexp)))))) ;;;###autoload (defun pp-macroexpand-last-sexp (arg) diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el index b4803687b5a..dbbb1274faa 100644 --- a/lisp/progmodes/elisp-mode.el +++ b/lisp/progmodes/elisp-mode.el @@ -1190,7 +1190,8 @@ character)." ;; Setup the lexical environment if lexical-binding is enabled. (elisp--eval-last-sexp-print-value (eval (macroexpand-all - (eval-sexp-add-defvars (elisp--preceding-sexp))) + (eval-sexp-add-defvars + (elisp--eval-defun-1 (macroexpand (elisp--preceding-sexp))))) lexical-binding) (if insert-value (current-buffer) t) no-truncate char-print-limit))) @@ -1246,6 +1247,10 @@ POS specifies the starting position where EXP was found and defaults to point." Interactively, with a non `-' prefix argument, print output into current buffer. +This commands handles `defvar', `defcustom' and `defface' the +same way that `eval-defun' does. See the doc string of that +function for details. + Normally, this function truncates long output according to the value of the variables `eval-expression-print-length' and `eval-expression-print-level'. With a prefix argument of zero,