From: Xah Lee on 11 Aug 2010 09:39 ⢠Emacs Lisp's print, princ, prin1, format, message http://xahlee.org/emacs/elisp_printing.html plain text version follows. -------------------------------------------------- This is a short tutorial on printing in emacs lisp. If you don't know elisp, first take a look at Emacs Lisp Basics. Simple Printing with âmessageâ The most basic printing function is âmessageâ. Here's a example: ; printing (message "hi") ; printing variable values (message "Her age is: %d " 16) ; %d is for number (message "Her name is: %s " "Vicky") ; %s is for string (message "Her mid init is: %c " 86) ; %c is for character in ascii code Note: You can see all past output from âmessageâ in the buffer named â*Messages*â. You can switch to it by âAlt+x switch-to-bufferâ. The âmessageâ function prints to the special buffer â*Messages*â. That buffer is special, because it is the general output destination for any messages from emacs, designed to be read by human. For example, it automatically truncate the top entries when the buffer reaches some size. Also, when a message is repeated many times, it automatically condense the repeated lines. And if the a message is a huge line, the line is truncated automatically. ;; print a line many times (setq xx 1) (while (< xx 20) (message "yay") (setq xx (1+ xx)) ) (switch-to-buffer "*Messages*") In the above example, you'll just see: âyay [19 times]â. (info "(elisp)Displaying Messages") Print to Your Own Buffer When writing a elisp script that does batch processing, it's best to print to your own buffer. For example, suppose you have a elisp batch script that do find and replace on all files in a dir. For each file visited, it prints out the file path. If you use â(message ...)â, it prints to the â*Messages*â buffer, which automatically roll off the top if you have more than a hundred lines. Also, it may intermix your script's output with output from other emacs activities. Here's a example of printing to your own buffer: (with-output-to-temp-buffer "*my output*" (mapc 'my-process-file (find-lisp-find-files inputDir "\\.html$")) (princ "Done.\n") (switch-to-buffer "*my output*") ) âprintâ function Elisp provides the âprintâ function. The basic syntax is this: (print OBJECT) The âOBJECTâ is any elisp object you want to print. It can be any lisp datatype, such as string, number, list, ... âformatâ function How a lisp object is converted to string for printing is done by the âformatâ function. Use describe-function to lookup its docs.. For example, if you want better control on how your numbers are formatted, you can do: (print (format ...)) âprincâ and âprin1â Elisp provide several other convenient functions to control printing. Here's a summary of their differences: Function Name Purpose Comment print print with newline. output can be read back by âreadâ.. prin1 like print, but does not add newline. output can be read back by âreadâ. princ print without newline nor delimiters. For human reading. Output can not be read back (info "(elisp) Output Functions") Xah â http://xahlee.org/ â
|
Pages: 1 Prev: GET PAID FOR WORKING ONLINE $100 ON YOUR FIRST DAY Next: Stack Overflow for Stream in SICP |