Interactive command for inserting the string returned by a function
When evaluating elisp symbolic expressions with (eval-last-sexp), bound to C-x C-e, I can just type C-u before that command to insert the result of the expression into the current buffer.
Is there some equivalent to that when calling functions interactively? For example, if I want to insert the string returned by (emacs-version) into the current-buffer, how would I do that? M-x emacs-version only shows the string in the minibuffer and typing C-u before M-x emacs-version won't work either.
If there is no such equivalent, what would be the easiest way to insert a string returned by a function without first having to type the function down before evaluating it?
UPDATE:
C-u M-x emacs-version does act开发者_运维知识库ually work. My mistake. But it does not work for emacs-uptime. How come it doesn't work for that one?
(defun my-insert-command-value (command)
"Insert the return value of the command."
(interactive "*C(insert) M-x ")
(print (call-interactively command) (current-buffer)))
emacs-uptime
was implemented to output the result only to minibuffer
(defun emacs-uptime (&optional format)
(interactive)
(let ((str
(format-seconds (or format "%Y, %D, %H, %M, %z%S")
(float-time
(time-subtract (current-time) before-init-time)))))
(if (called-interactively-p 'interactive)
(message "%s" str)
str)))
emacs-version
has the following code which prints output if called with C-u
(if here
(insert version-string)
(if (called-interactively-p 'interactive)
(message "%s" version-string)
version-string))
If you want to print the result of a particular command (e.g. emacs-uptime
) you can wrap it to insert
result into the current buffer (similar to emacs-version
).
However, I don't know a generic solution - how to output the result of any Emacs command.
C-u M-x pp-eval-expression RET (emacs-uptime) RET
"Evaluate Emacs-Lisp sexp EXPRESSION, and pretty-print its value. With a prefix arg, insert the value into the current buffer at point. With a negative prefix arg, if the value is a string, then insert it into the buffer without double-quotes (`"')."
See pp+.el.
精彩评论