1 ;;; pp.el --- pretty printer for Emacs Lisp
3 ;; Copyright (C) 1989, 1993, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Randal Schwartz <merlyn@stonehenge.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (defvar font-lock-verbose
)
30 "Pretty printer for Emacs Lisp."
34 (defcustom pp-escape-newlines t
35 "Value of `print-escape-newlines' used by pp-* functions."
40 (defun pp-to-string (object)
41 "Return a string containing the pretty-printed representation of OBJECT.
42 OBJECT can be any Lisp object. Quoting characters are used as needed
43 to make output that `read' can handle, whenever this is possible."
45 (lisp-mode-variables nil
)
46 (set-syntax-table emacs-lisp-mode-syntax-table
)
47 (let ((print-escape-newlines pp-escape-newlines
)
49 (prin1 object
(current-buffer)))
55 "Prettify the current buffer with printed representation of a Lisp object."
56 (goto-char (point-min))
58 ;; (message "%06d" (- (point-max) (point)))
60 ((ignore-errors (down-list 1) t
)
63 (skip-chars-backward "'`#^")
64 (when (and (not (bobp)) (memq (char-before) '(?\s ?
\t ?
\n)))
67 (progn (skip-chars-backward " \t\n") (point)))
69 ((ignore-errors (up-list 1) t
)
70 (while (looking-at-p "\\s)")
74 (progn (skip-chars-forward " \t\n") (point)))
76 (t (goto-char (point-max)))))
77 (goto-char (point-min))
81 (defun pp (object &optional stream
)
82 "Output the pretty-printed representation of OBJECT, any Lisp object.
83 Quoting characters are printed as needed to make output that `read'
84 can handle, whenever this is possible.
85 Output stream is STREAM, or value of `standard-output' (which see)."
86 (princ (pp-to-string object
) (or stream standard-output
)))
88 (defun pp-display-expression (expression out-buffer-name
)
89 "Prettify and display EXPRESSION in an appropriate way, depending on length.
90 If a temporary buffer is needed for representation, it will be named
91 after OUT-BUFFER-NAME."
92 (let* ((old-show-function temp-buffer-show-function
)
93 ;; Use this function to display the buffer.
94 ;; This function either decides not to display it at all
95 ;; or displays it in the usual way.
96 (temp-buffer-show-function
99 (with-current-buffer buf
100 (goto-char (point-min))
102 (if (or (< (1+ (point)) (point-max))
103 (>= (- (point) (point-min)) (frame-width)))
104 (let ((temp-buffer-show-function old-show-function
)
105 (old-selected (selected-window))
106 (window (display-buffer buf
)))
107 (goto-char (point-min)) ; expected by some hooks ...
108 (make-frame-visible (window-frame window
))
111 (select-window window
)
112 (run-hooks 'temp-buffer-show-hook
))
113 (when (window-live-p old-selected
)
114 (select-window old-selected
))
115 (message "See buffer %s." out-buffer-name
)))
116 (message "%s" (buffer-substring (point-min) (point)))
118 (with-output-to-temp-buffer out-buffer-name
120 (with-current-buffer standard-output
122 (setq buffer-read-only nil
)
123 (set (make-local-variable 'font-lock-verbose
) nil
)))))
126 (defun pp-eval-expression (expression)
127 "Evaluate EXPRESSION and pretty-print its value.
128 Also add the value to the front of the list in the variable `values'."
130 (list (read-from-minibuffer "Eval: " nil read-expression-map t
131 'read-expression-history
)))
132 (message "Evaluating...")
133 (setq values
(cons (eval expression
) values
))
134 (pp-display-expression (car values
) "*Pp Eval Output*"))
137 (defun pp-macroexpand-expression (expression)
138 "Macroexpand EXPRESSION and pretty-print its value."
140 (list (read-from-minibuffer "Macroexpand: " nil read-expression-map t
141 'read-expression-history
)))
142 (pp-display-expression (macroexpand expression
) "*Pp Macroexpand Output*"))
144 (defun pp-last-sexp ()
145 "Read sexp before point. Ignores leading comment characters."
146 (let ((stab (syntax-table)) (pt (point)) start exp
)
147 (set-syntax-table emacs-lisp-mode-syntax-table
)
150 ;; If first line is commented, ignore all leading comments:
151 (if (save-excursion (beginning-of-line) (looking-at-p "[ \t]*;"))
153 (setq exp
(buffer-substring (point) pt
))
154 (while (string-match "\n[ \t]*;+" exp start
)
155 (setq start
(1+ (match-beginning 0))
156 exp
(concat (substring exp
0 start
)
157 (substring exp
(match-end 0)))))
158 (setq exp
(read exp
)))
159 (setq exp
(read (current-buffer)))))
160 (set-syntax-table stab
)
164 (defun pp-eval-last-sexp (arg)
165 "Run `pp-eval-expression' on sexp before point.
166 With argument, pretty-print output into current buffer.
167 Ignores leading comment characters."
170 (insert (pp-to-string (eval (pp-last-sexp))))
171 (pp-eval-expression (pp-last-sexp))))
174 (defun pp-macroexpand-last-sexp (arg)
175 "Run `pp-macroexpand-expression' on sexp before point.
176 With argument, pretty-print output into current buffer.
177 Ignores leading comment characters."
180 (insert (pp-to-string (macroexpand (pp-last-sexp))))
181 (pp-macroexpand-expression (pp-last-sexp))))
183 ;;; Test cases for quote
184 ;; (pp-eval-expression ''(quote quote))
185 ;; (pp-eval-expression ''((quote a) (quote b)))
186 ;; (pp-eval-expression ''('a 'b)) ; same as above
187 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
188 ;; These do not satisfy the quote test.
189 ;; (pp-eval-expression ''quote)
190 ;; (pp-eval-expression ''(quote))
191 ;; (pp-eval-expression ''(quote . quote))
192 ;; (pp-eval-expression ''(quote a b))
193 ;; (pp-eval-expression ''(quotefoo))
194 ;; (pp-eval-expression ''(a b))
196 (provide 'pp
) ; so (require 'pp) works