diff options
author | Noam Postavsky <npostavs@gmail.com> | 2017-04-23 22:21:42 -0400 |
---|---|---|
committer | Noam Postavsky <npostavs@gmail.com> | 2017-05-19 18:16:15 -0400 |
commit | 267be4bdc28564a99f45da29e84eb98838117b50 (patch) | |
tree | 3816b74e945cb1a17a1f4c8ad5e25e4abb0e8206 /lisp/simple.el | |
parent | c1c8b67246c4314b302cca2ac43f13a0baba4c16 (diff) | |
download | emacs-267be4bdc28564a99f45da29e84eb98838117b50.tar.gz |
Refactor lisp eval result printing
* lisp/simple.el (eval-expression-print-format): Don't check
`standard-output' or `current-prefix-arg'.
(eval-expression-get-print-arguments): New function, centralizes
decision about how to print results of `eval-expression' and
`eval-last-sexp'.
(eval-expression):
* lisp/progmodes/elisp-mode.el (elisp--eval-last-sexp-print-value):
Use it.
Diffstat (limited to 'lisp/simple.el')
-rw-r--r-- | lisp/simple.el | 58 |
1 files changed, 30 insertions, 28 deletions
diff --git a/lisp/simple.el b/lisp/simple.el index 7f13df5006d..3af62657dbf 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -1456,16 +1456,14 @@ This string will typically look like \" (#o1, #x1, ?\\C-a)\". If VALUE is not an integer, nil is returned. This function is used by functions like `prin1' that display the result of expression evaluation." - (if (and (integerp value) - (or (eq standard-output t) - (zerop (prefix-numeric-value current-prefix-arg)))) - (let ((char-string - (if (and (characterp value) - (char-displayable-p value)) - (prin1-char value)))) - (if char-string - (format " (#o%o, #x%x, %s)" value value char-string) - (format " (#o%o, #x%x)" value value))))) + (when (integerp value) + (let ((char-string + (and (characterp value) + (char-displayable-p value) + (prin1-char value)))) + (if char-string + (format " (#o%o, #x%x, %s)" value value char-string) + (format " (#o%o, #x%x)" value value))))) (defvar eval-expression-minibuffer-setup-hook nil "Hook run by `eval-expression' when entering the minibuffer.") @@ -1484,9 +1482,21 @@ result of expression evaluation." read-expression-map t 'read-expression-history)))) +(defun eval-expression-get-print-arguments (prefix-argument) + "Get arguments for commands that print an expression result. +Returns a list (INSERT-VALUE NO-TRUNCATE CHAR-PRINT) +based on PREFIX-ARG. This function determines the interpretation +of the prefix argument for `eval-expression' and +`eval-last-sexp'." + (let ((num (prefix-numeric-value prefix-argument))) + (list (not (memq prefix-argument '(nil))) + (= num 0) + (cond ((not (memq prefix-argument '(0 nil))) nil) + (t t))))) + ;; We define this, rather than making `eval' interactive, ;; for the sake of completion of names like eval-region, eval-buffer. -(defun eval-expression (exp &optional insert-value) +(defun eval-expression (exp &optional insert-value no-truncate char-print) "Evaluate EXP and print value in the echo area. When called interactively, read an Emacs Lisp expression and evaluate it. Value is also consed on to front of the variable `values'. @@ -1507,8 +1517,8 @@ minibuffer. If `eval-expression-debug-on-error' is non-nil, which is the default, this command arranges for all errors to enter the debugger." (interactive - (list (read--expression "Eval: ") - current-prefix-arg)) + (cons (read--expression "Eval: ") + (eval-expression-get-print-arguments current-prefix-arg))) (if (null eval-expression-debug-on-error) (push (eval exp lexical-binding) values) @@ -1523,23 +1533,15 @@ this command arranges for all errors to enter the debugger." (unless (eq old-value new-value) (setq debug-on-error new-value)))) - (let ((print-length (and (not (zerop (prefix-numeric-value insert-value))) - eval-expression-print-length)) - (print-level (and (not (zerop (prefix-numeric-value insert-value))) - eval-expression-print-level)) + (let ((print-length (unless no-truncate eval-expression-print-length)) + (print-level (unless no-truncate eval-expression-print-level)) (deactivate-mark)) - (if insert-value - (with-no-warnings - (let ((standard-output (current-buffer))) - (prog1 - (prin1 (car values)) - (when (zerop (prefix-numeric-value insert-value)) - (let ((str (eval-expression-print-format (car values)))) - (if str (princ str))))))) + (let ((out (if insert-value (current-buffer) t))) (prog1 - (prin1 (car values) t) - (let ((str (eval-expression-print-format (car values)))) - (if str (princ str t))))))) + (prin1 (car values) out) + (let ((str (and char-print + (eval-expression-print-format (car values))))) + (when str (princ str out))))))) (defun edit-and-eval-command (prompt command) "Prompting with PROMPT, let user edit COMMAND and eval result. |