Emacs Lisp: Print, Output
Emacs lisp has several ways to print. You can print to the Messages Buffer, or insert output directly into a buffer.
Insert Text to Current Buffer
(insert &rest ARGS)
Instert string to current buffer, at cursor position.
[see Emacs Lisp: Text Editing Functions]
Print to Messages Buffer
(message FORMAT_STRING &rest ARGS)
Print a Format String to the Messages Buffer .
(message "Name is: %s" "Joe")
Print to a Buffer
(print OBJECT &optional TARGET)
- Print lisp object in lisp syntax.
- Print a newline at end.
- Optional arg for a target buffer, or other functions.
formatif you need to print multiple items in one shot. [see Emacs Lisp: Format String]
When writing a elisp script that does batch processing, it's best to print to your own buffer, because the Messages Buffer scrolls off.
(setq xbuff (generate-new-buffer "*my output*")) (print "something" xbuff) (switch-to-buffer xbuff )
[see Buffer Functions]
(prin1 OBJECT &optional TARGET)
(princ OBJECT &optional TARGET)
- Print in more human-readable format. (e.g. no string delimiters.)
- No newline at end.
(warn FORMAT_STRING &rest ARGS)
Print a warning of Format String .
(warn "Name is: %s" "Joe")
Temporarily Specify a Buffer for Output
(with-output-to-temp-buffer BUFNAME &rest BODY)
Bind standard-output to buffer BUFNAME, eval BODY, then show that buffer.
- buffer is cleared before printing to it.
- print functions default to print to the buffer.
(let ((xbuff (generate-new-buffer "*my output*"))) (with-output-to-temp-buffer xbuff ;; this is printed to xbuff (print "abc") ;; ))