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

From: Kelly Dean <[email-masked]>
Date: Tue, 27 Nov 2012 04:41:44 -0800 (PST)

> It's not a bug, it's a feature of
> eval-expression-print-format.
Sorry for the mistaken bug report. I should have read the source code, and reported this bug as a documentation deficiency instead, since the help pages for eval-last-sexp and eval-print-last-sexp say nothing about eval-expression-print-format or about behaving differently when called twice.
I propose adding to the doc strings for eval-last-sexp and eval-print-last-sexp, "If the value is an integer, and this command is called twice in succession, then in addition to printing the decimal representation, also print the octal and hex representations and char interpretation of the value."
BTW the doc string for eval-last-sexp says "print value in minibuffer." which should be "print value into echo area."

Also, eval-expression-print-format omits the char if the invoking command name is not eval-last-sexp or eval-print-last-sexp. Why? For example, if I do
M-x eval-expression [ret] 5 [ret]
I only get
5 (#o5, #x5)
I don't see why not simplify it to:

(defun eval-expression-print-format (value)
  "Format VALUE as a result of evaluated expression if invoked twice, invoked as a name other than eval-last-sexp or eval-print-last-sexp, or if in debug mode. Return a formatted string that is displayed in the echo area in addition to the value printed by prin1 in functions which display the result of expression evaluation."
  (if (and (integerp value)
           (or (not (memq this-command '(eval-last-sexp eval-print-last-sexp)))
               (eq this-command last-command)
               (if (boundp 'edebug-active) edebug-active)))
      (let ((char-string (prin1-char value)))
        (if char-string
            (format " (#o%o, #x%x, %s)" value value char-string)
          (format " (#o%o, #x%x)" value value)))))

Even then the design seems wrong. It's designed to avoid producing the string the first time eval-last-sexp or eval-print-last-sexp is called, regardless of whether the result will be displayed in the echo area (in which case I don't see any reason to avoid it) or printed into the buffer (in which case avoiding it is good since the user probably wants just the decimal representation).
And it makes one of my custom functions fail to work the same as eval-last-sexp when intended, which is what brought all this to my attention in the first place:

(defun eval-region-or-last-sexp () "Eval region if active; otherwise, eval last sexp and print value into echo area or, with prefix argument, into current buffer. See `eval-region' and `eval-last-sexp' for details."
 (interactive)
 (if mark-active
  (call-interactively 'eval-region)
  (call-interactively 'eval-last-sexp)))

I can fix my function using (setq this-command 'eval-last-sexp), but that's a kludge.
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.

Regarding lack of hard word wrap in the doc strings above, it's because I propose
(add-hook 'help-mode-hook (lambda () (toggle-word-wrap 1)))