Subject: bug#12985: eval-last-sexp looks broken when executed twice

From: Juri Linkov <[email-masked]>
Date: Thu, 09 May 2013 00:19:09 +0300

>> It seems a better design would be to always include the string, even
>> the first time, when displaying in the echo area, and never include
>> the string when printing into the buffer, regardless of the invoking
>> command name. This is simpler, and it's how eval-expression already works.
>
> Thank you for suggesting a new design.  I agree it is better
> than the current design.  The patch below implements it and
> removes the feature of behaving differently when called twice.

Since this feature will use a better design now,
more commands could benefit from this feature:
`C-M-x' (`eval-defun' and `edebug-eval-defun')
and `ielm-send-input'.

=== modified file 'lisp/emacs-lisp/lisp-mode.el'
--- lisp/emacs-lisp/lisp-mode.el	2013-05-06 20:23:40 +0000
+++ lisp/emacs-lisp/lisp-mode.el	2013-05-08 21:12:52 +0000
@@ -928,6 +928,8 @@ (defun eval-defun-2 ()
 		 ;; will make eval-region return.
 		 (goto-char ,end)
 		 ',form))))))
+  (let ((str (eval-expression-print-format (car values))))
+    (if str (princ str)))
   ;; The result of evaluation has been put onto VALUES.  So return it.
   (car values))

=== modified file 'lisp/emacs-lisp/edebug.el'
--- lisp/emacs-lisp/edebug.el	2013-04-08 02:21:59 +0000
+++ lisp/emacs-lisp/edebug.el	2013-05-08 21:17:06 +0000
@@ -528,7 +528,10 @@ (defun edebug-eval-defun (edebug-it)
 			(put (nth 1 form) 'saved-face nil)))))
     (setq edebug-result (eval (eval-sexp-add-defvars form) lexical-binding))
     (if (not edebugging)
-	(princ edebug-result)
+	(prog1
+	    (princ edebug-result)
+	  (let ((str (eval-expression-print-format edebug-result)))
+	    (if str (princ str))))
       edebug-result)))

=== modified file 'lisp/ielm.el'
--- lisp/ielm.el	2013-01-01 09:11:05 +0000
+++ lisp/ielm.el	2013-05-08 21:10:23 +0000
@@ -408,7 +408,8 @@ (defun ielm-eval-input (input-string)
 	(condition-case nil
 	    ;; Self-referential objects cause loops in the printer, so
 	    ;; trap quits here. May as well do errors, too
-	    (setq ielm-output (concat ielm-output (pp-to-string ielm-result)))
+	    (setq ielm-output (concat ielm-output (pp-to-string ielm-result)
+				      (eval-expression-print-format ielm-result)))
 	  (error (setq ielm-error-type "IELM Error")
 		 (setq ielm-result "Error during pretty-printing (bug in pp)"))
 	  (quit  (setq ielm-error-type "IELM Error")