1 ;;; pp.el --- pretty printer for Emacs Lisp
3 ;; Copyright (C) 1989, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Randal Schwartz <merlyn@stonehenge.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (defvar font-lock-verbose
)
31 "Pretty printer for Emacs Lisp."
35 (defcustom pp-escape-newlines t
36 "Value of `print-escape-newlines' used by pp-* functions."
41 (defun pp-to-string (object)
42 "Return a string containing the pretty-printed representation of OBJECT.
43 OBJECT can be any Lisp object. Quoting characters are used as needed
44 to make output that `read' can handle, whenever this is possible."
45 (with-current-buffer (generate-new-buffer " pp-to-string")
48 (lisp-mode-variables nil
)
49 (set-syntax-table emacs-lisp-mode-syntax-table
)
50 (let ((print-escape-newlines pp-escape-newlines
)
52 (prin1 object
(current-buffer)))
55 (kill-buffer (current-buffer)))))
59 "Prettify the current buffer with printed representation of a Lisp object."
60 (goto-char (point-min))
62 ;; (message "%06d" (- (point-max) (point)))
64 ((condition-case err-var
65 (prog1 t
(down-list 1))
69 (skip-chars-backward "'`#^")
70 (when (and (not (bobp)) (memq (char-before) '(?\s ?
\t ?
\n)))
73 (progn (skip-chars-backward " \t\n") (point)))
75 ((condition-case err-var
78 (while (looking-at "\\s)")
82 (progn (skip-chars-forward " \t\n") (point)))
84 (t (goto-char (point-max)))))
85 (goto-char (point-min))
89 (defun pp (object &optional stream
)
90 "Output the pretty-printed representation of OBJECT, any Lisp object.
91 Quoting characters are printed as needed to make output that `read'
92 can handle, whenever this is possible.
93 Output stream is STREAM, or value of `standard-output' (which see)."
94 (princ (pp-to-string object
) (or stream standard-output
)))
96 (defun pp-display-expression (expression out-buffer-name
)
97 "Prettify and display EXPRESSION in an appropriate way, depending on length.
98 If a temporary buffer is needed for representation, it will be named
99 after OUT-BUFFER-NAME."
100 (let* ((old-show-function temp-buffer-show-function
)
101 ;; Use this function to display the buffer.
102 ;; This function either decides not to display it at all
103 ;; or displays it in the usual way.
104 (temp-buffer-show-function
107 (with-current-buffer buf
108 (goto-char (point-min))
110 (if (or (< (1+ (point)) (point-max))
111 (>= (- (point) (point-min)) (frame-width)))
112 (let ((temp-buffer-show-function old-show-function
)
113 (old-selected (selected-window))
114 (window (display-buffer buf
)))
115 (goto-char (point-min)) ; expected by some hooks ...
116 (make-frame-visible (window-frame window
))
119 (select-window window
)
120 (run-hooks 'temp-buffer-show-hook
))
121 (select-window old-selected
)
122 (message "See buffer %s." out-buffer-name
)))
123 (message "%s" (buffer-substring (point-min) (point)))
125 (with-output-to-temp-buffer out-buffer-name
127 (with-current-buffer standard-output
129 (setq buffer-read-only nil
)
130 (set (make-local-variable 'font-lock-verbose
) nil
)))))
133 (defun pp-eval-expression (expression)
134 "Evaluate EXPRESSION and pretty-print its value.
135 Also add the value to the front of the list in the variable `values'."
137 (list (read-from-minibuffer "Eval: " nil read-expression-map t
138 'read-expression-history
)))
139 (message "Evaluating...")
140 (setq values
(cons (eval expression
) values
))
141 (pp-display-expression (car values
) "*Pp Eval Output*"))
144 (defun pp-macroexpand-expression (expression)
145 "Macroexpand EXPRESSION and pretty-print its value."
147 (list (read-from-minibuffer "Macroexpand: " nil read-expression-map t
148 'read-expression-history
)))
149 (pp-display-expression (macroexpand expression
) "*Pp Macroexpand Output*"))
151 (defun pp-last-sexp ()
152 "Read sexp before point. Ignores leading comment characters."
153 (let ((stab (syntax-table)) (pt (point)) start exp
)
154 (set-syntax-table emacs-lisp-mode-syntax-table
)
157 ;; If first line is commented, ignore all leading comments:
158 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
160 (setq exp
(buffer-substring (point) pt
))
161 (while (string-match "\n[ \t]*;+" exp start
)
162 (setq start
(1+ (match-beginning 0))
163 exp
(concat (substring exp
0 start
)
164 (substring exp
(match-end 0)))))
165 (setq exp
(read exp
)))
166 (setq exp
(read (current-buffer)))))
167 (set-syntax-table stab
)
171 (defun pp-eval-last-sexp (arg)
172 "Run `pp-eval-expression' on sexp before point.
173 With argument, pretty-print output into current buffer.
174 Ignores leading comment characters."
177 (insert (pp-to-string (eval (pp-last-sexp))))
178 (pp-eval-expression (pp-last-sexp))))
181 (defun pp-macroexpand-last-sexp (arg)
182 "Run `pp-macroexpand-expression' on sexp before point.
183 With argument, pretty-print output into current buffer.
184 Ignores leading comment characters."
187 (insert (pp-to-string (macroexpand (pp-last-sexp))))
188 (pp-macroexpand-expression (pp-last-sexp))))
190 ;;; Test cases for quote
191 ;; (pp-eval-expression ''(quote quote))
192 ;; (pp-eval-expression ''((quote a) (quote b)))
193 ;; (pp-eval-expression ''('a 'b)) ; same as above
194 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
195 ;; These do not satisfy the quote test.
196 ;; (pp-eval-expression ''quote)
197 ;; (pp-eval-expression ''(quote))
198 ;; (pp-eval-expression ''(quote . quote))
199 ;; (pp-eval-expression ''(quote a b))
200 ;; (pp-eval-expression ''(quotefoo))
201 ;; (pp-eval-expression ''(a b))
203 (provide 'pp
) ; so (require 'pp) works
205 ;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9