Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; pp.el --- pretty printer for Emacs Lisp |
b578f267 | 2 | |
3731a850 | 3 | ;; Copyright (C) 1989, 1993, 2001, 2002, 2003, 2004, |
114f9c96 | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
258544f8 | 5 | |
df072942 | 6 | ;; Author: Randal Schwartz <merlyn@stonehenge.com> |
284b3043 | 7 | ;; Keywords: lisp |
258544f8 RS |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
d6cba7ae | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
258544f8 | 12 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
258544f8 RS |
15 | |
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
258544f8 | 23 | |
e8af40ee PJ |
24 | ;;; Commentary: |
25 | ||
258544f8 | 26 | ;;; Code: |
e8af40ee | 27 | |
f96b52a3 JB |
28 | (defvar font-lock-verbose) |
29 | ||
666b9413 SE |
30 | (defgroup pp nil |
31 | "Pretty printer for Emacs Lisp." | |
32 | :prefix "pp-" | |
33 | :group 'lisp) | |
34 | ||
a1506d29 | 35 | (defcustom pp-escape-newlines t |
cb711556 | 36 | "Value of `print-escape-newlines' used by pp-* functions." |
666b9413 SE |
37 | :type 'boolean |
38 | :group 'pp) | |
258544f8 | 39 | |
0e520006 | 40 | ;;;###autoload |
258544f8 | 41 | (defun pp-to-string (object) |
e189df50 KH |
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." | |
9a529312 | 45 | (with-current-buffer (generate-new-buffer " pp-to-string") |
258544f8 RS |
46 | (unwind-protect |
47 | (progn | |
15b605ae RS |
48 | (lisp-mode-variables nil) |
49 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
3d98a374 AS |
50 | (let ((print-escape-newlines pp-escape-newlines) |
51 | (print-quoted t)) | |
258544f8 | 52 | (prin1 object (current-buffer))) |
575c3bca | 53 | (pp-buffer) |
258544f8 RS |
54 | (buffer-string)) |
55 | (kill-buffer (current-buffer))))) | |
56 | ||
c47e847f | 57 | ;;;###autoload |
575c3bca JL |
58 | (defun pp-buffer () |
59 | "Prettify the current buffer with printed representation of a Lisp object." | |
60 | (goto-char (point-min)) | |
61 | (while (not (eobp)) | |
62 | ;; (message "%06d" (- (point-max) (point))) | |
63 | (cond | |
64 | ((condition-case err-var | |
65 | (prog1 t (down-list 1)) | |
66 | (error nil)) | |
67 | (save-excursion | |
68 | (backward-char 1) | |
69 | (skip-chars-backward "'`#^") | |
1bcb666b | 70 | (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n))) |
575c3bca JL |
71 | (delete-region |
72 | (point) | |
73 | (progn (skip-chars-backward " \t\n") (point))) | |
74 | (insert "\n")))) | |
75 | ((condition-case err-var | |
76 | (prog1 t (up-list 1)) | |
77 | (error nil)) | |
78 | (while (looking-at "\\s)") | |
79 | (forward-char 1)) | |
80 | (delete-region | |
81 | (point) | |
82 | (progn (skip-chars-forward " \t\n") (point))) | |
83 | (insert ?\n)) | |
84 | (t (goto-char (point-max))))) | |
85 | (goto-char (point-min)) | |
86 | (indent-sexp)) | |
87 | ||
ccba0a20 | 88 | ;;;###autoload |
258544f8 RS |
89 | (defun pp (object &optional stream) |
90 | "Output the pretty-printed representation of OBJECT, any Lisp object. | |
e189df50 | 91 | Quoting characters are printed as needed to make output that `read' |
258544f8 RS |
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))) | |
95 | ||
497a66ae GM |
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." | |
bf464fad RS |
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 | |
258544f8 RS |
105 | (function |
106 | (lambda (buf) | |
9a529312 | 107 | (with-current-buffer buf |
258544f8 RS |
108 | (goto-char (point-min)) |
109 | (end-of-line 1) | |
110 | (if (or (< (1+ (point)) (point-max)) | |
06115f9d | 111 | (>= (- (point) (point-min)) (frame-width))) |
bf464fad RS |
112 | (let ((temp-buffer-show-function old-show-function) |
113 | (old-selected (selected-window)) | |
114 | (window (display-buffer buf))) | |
258544f8 | 115 | (goto-char (point-min)) ; expected by some hooks ... |
bf464fad RS |
116 | (make-frame-visible (window-frame window)) |
117 | (unwind-protect | |
118 | (progn | |
119 | (select-window window) | |
120 | (run-hooks 'temp-buffer-show-hook)) | |
52f55ab0 | 121 | (select-window old-selected) |
497a66ae | 122 | (message "See buffer %s." out-buffer-name))) |
258544f8 | 123 | (message "%s" (buffer-substring (point-min) (point))) |
bf464fad | 124 | )))))) |
497a66ae GM |
125 | (with-output-to-temp-buffer out-buffer-name |
126 | (pp expression) | |
eaf33a17 SM |
127 | (with-current-buffer standard-output |
128 | (emacs-lisp-mode) | |
52f55ab0 | 129 | (setq buffer-read-only nil) |
eaf33a17 | 130 | (set (make-local-variable 'font-lock-verbose) nil))))) |
258544f8 | 131 | |
ccba0a20 | 132 | ;;;###autoload |
497a66ae GM |
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'." | |
136 | (interactive | |
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*")) | |
142 | ||
143 | ;;;###autoload | |
144 | (defun pp-macroexpand-expression (expression) | |
145 | "Macroexpand EXPRESSION and pretty-print its value." | |
146 | (interactive | |
147 | (list (read-from-minibuffer "Macroexpand: " nil read-expression-map t | |
148 | 'read-expression-history))) | |
149 | (pp-display-expression (macroexpand expression) "*Pp Macroexpand Output*")) | |
150 | ||
151 | (defun pp-last-sexp () | |
152 | "Read sexp before point. Ignores leading comment characters." | |
258544f8 RS |
153 | (let ((stab (syntax-table)) (pt (point)) start exp) |
154 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
155 | (save-excursion | |
156 | (forward-sexp -1) | |
157 | ;; If first line is commented, ignore all leading comments: | |
158 | (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;")) | |
159 | (progn | |
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) | |
497a66ae GM |
168 | exp)) |
169 | ||
170 | ;;;###autoload | |
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." | |
175 | (interactive "P") | |
176 | (if arg | |
177 | (insert (pp-to-string (eval (pp-last-sexp)))) | |
178 | (pp-eval-expression (pp-last-sexp)))) | |
179 | ||
180 | ;;;###autoload | |
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." | |
185 | (interactive "P") | |
186 | (if arg | |
187 | (insert (pp-to-string (macroexpand (pp-last-sexp)))) | |
188 | (pp-macroexpand-expression (pp-last-sexp)))) | |
258544f8 RS |
189 | |
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)) | |
202 | ||
203 | (provide 'pp) ; so (require 'pp) works | |
204 | ||
cbee283d | 205 | ;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9 |
e8af40ee | 206 | ;;; pp.el ends here |