Subject: bug#70931: 29.3; scratch

From: Eli Zaretskii <[email-masked]>
Date: Tue, 14 May 2024 13:46:17 +0300

> Date: Tue, 14 May 2024 04:52:59 -0500
> From: Devon Sean McCullough <[email-masked]>
> 
> $ Open -n -a /Applications/Emacs.app --args -Q
> 
> (find-file "/tmp/x") ; then Control-J
> 
> ;; A buffer appears named x
> ;; containing the text #<buffer x>
> ;; but that text should instead be in the buffer named *scratch*

Thanks.  This was caused by a too-eager simplification made back when
Emacs 26 was being developed.

Does the patch below give good results?

CC'ing Stefan in case he has comments.

diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el
index 84814c9..d96fbbd 100644
--- a/lisp/progmodes/elisp-mode.el
+++ b/lisp/progmodes/elisp-mode.el
@@ -1557,13 +1557,16 @@ elisp--eval-last-sexp
   (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 that
+    ;; 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)