From: Eli Zaretskii Date: Sat, 18 May 2024 09:55:34 +0000 (+0300) Subject: Fix printing the result by 'eval-print-last-sexp' X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=d49e6212cfc8953067bfd79753ffc9a6e4e68892;p=emacs.git Fix printing the result by 'eval-print-last-sexp' * lisp/progmodes/elisp-mode.el (elisp--eval-last-sexp): Record the desired output stream before evaluating the expression. (Bug#70931) (cherry picked from commit 82a31e27e6ceed17a116286d2f0f9810be2e1772) --- diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el index 44dc4a5d5ee..9e727bd4543 100644 --- a/lisp/progmodes/elisp-mode.el +++ b/lisp/progmodes/elisp-mode.el @@ -1556,13 +1556,16 @@ character)." (pcase-let* ((`(,insert-value ,no-truncate ,char-print-limit) (eval-expression-get-print-arguments eval-last-sexp-arg-internal))) - ;; Setup the lexical environment if lexical-binding is enabled. - (elisp--eval-last-sexp-print-value - (eval (macroexpand-all - (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))) + ;; The expression might change to a different buffer, so record the + ;; desired output stream now. + (let ((output (if insert-value (current-buffer) t))) + ;; Setup the lexical environment if lexical-binding is enabled. + (elisp--eval-last-sexp-print-value + (eval (macroexpand-all + (eval-sexp-add-defvars + (elisp--eval-defun-1 (macroexpand (elisp--preceding-sexp))))) + lexical-binding) + output no-truncate char-print-limit)))) (defun elisp--eval-last-sexp-print-value (value output &optional no-truncate char-print-limit)