Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; pp.el --- pretty printer for Emacs Lisp |
b578f267 | 2 | |
3731a850 | 3 | ;; Copyright (C) 1989, 1993, 2001, 2002, 2003, 2004, |
8b72699e | 4 | ;; 2005, 2006, 2007, 2008 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 |
666b9413 SE |
36 | "*Value of `print-escape-newlines' used by pp-* functions." |
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." | |
258544f8 RS |
45 | (save-excursion |
46 | (set-buffer (generate-new-buffer " pp-to-string")) | |
47 | (unwind-protect | |
48 | (progn | |
15b605ae RS |
49 | (lisp-mode-variables nil) |
50 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
3d98a374 AS |
51 | (let ((print-escape-newlines pp-escape-newlines) |
52 | (print-quoted t)) | |
258544f8 | 53 | (prin1 object (current-buffer))) |
575c3bca | 54 | (pp-buffer) |
258544f8 RS |
55 | (buffer-string)) |
56 | (kill-buffer (current-buffer))))) | |
57 | ||
c47e847f | 58 | ;;;###autoload |
575c3bca JL |
59 | (defun pp-buffer () |
60 | "Prettify the current buffer with printed representation of a Lisp object." | |
61 | (goto-char (point-min)) | |
62 | (while (not (eobp)) | |
63 | ;; (message "%06d" (- (point-max) (point))) | |
64 | (cond | |
65 | ((condition-case err-var | |
66 | (prog1 t (down-list 1)) | |
67 | (error nil)) | |
68 | (save-excursion | |
69 | (backward-char 1) | |
70 | (skip-chars-backward "'`#^") | |
1bcb666b | 71 | (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n))) |
575c3bca JL |
72 | (delete-region |
73 | (point) | |
74 | (progn (skip-chars-backward " \t\n") (point))) | |
75 | (insert "\n")))) | |
76 | ((condition-case err-var | |
77 | (prog1 t (up-list 1)) | |
78 | (error nil)) | |
79 | (while (looking-at "\\s)") | |
80 | (forward-char 1)) | |
81 | (delete-region | |
82 | (point) | |
83 | (progn (skip-chars-forward " \t\n") (point))) | |
84 | (insert ?\n)) | |
85 | (t (goto-char (point-max))))) | |
86 | (goto-char (point-min)) | |
87 | (indent-sexp)) | |
88 | ||
ccba0a20 | 89 | ;;;###autoload |
258544f8 RS |
90 | (defun pp (object &optional stream) |
91 | "Output the pretty-printed representation of OBJECT, any Lisp object. | |
e189df50 | 92 | Quoting characters are printed as needed to make output that `read' |
258544f8 RS |
93 | can handle, whenever this is possible. |
94 | Output stream is STREAM, or value of `standard-output' (which see)." | |
95 | (princ (pp-to-string object) (or stream standard-output))) | |
96 | ||
497a66ae GM |
97 | (defun pp-display-expression (expression out-buffer-name) |
98 | "Prettify and display EXPRESSION in an appropriate way, depending on length. | |
99 | If a temporary buffer is needed for representation, it will be named | |
100 | after OUT-BUFFER-NAME." | |
bf464fad RS |
101 | (let* ((old-show-function temp-buffer-show-function) |
102 | ;; Use this function to display the buffer. | |
103 | ;; This function either decides not to display it at all | |
104 | ;; or displays it in the usual way. | |
105 | (temp-buffer-show-function | |
258544f8 RS |
106 | (function |
107 | (lambda (buf) | |
108 | (save-excursion | |
109 | (set-buffer buf) | |
110 | (goto-char (point-min)) | |
111 | (end-of-line 1) | |
112 | (if (or (< (1+ (point)) (point-max)) | |
06115f9d | 113 | (>= (- (point) (point-min)) (frame-width))) |
bf464fad RS |
114 | (let ((temp-buffer-show-function old-show-function) |
115 | (old-selected (selected-window)) | |
116 | (window (display-buffer buf))) | |
258544f8 | 117 | (goto-char (point-min)) ; expected by some hooks ... |
bf464fad RS |
118 | (make-frame-visible (window-frame window)) |
119 | (unwind-protect | |
120 | (progn | |
121 | (select-window window) | |
122 | (run-hooks 'temp-buffer-show-hook)) | |
52f55ab0 | 123 | (select-window old-selected) |
497a66ae | 124 | (message "See buffer %s." out-buffer-name))) |
258544f8 | 125 | (message "%s" (buffer-substring (point-min) (point))) |
bf464fad | 126 | )))))) |
497a66ae GM |
127 | (with-output-to-temp-buffer out-buffer-name |
128 | (pp expression) | |
eaf33a17 SM |
129 | (with-current-buffer standard-output |
130 | (emacs-lisp-mode) | |
52f55ab0 | 131 | (setq buffer-read-only nil) |
eaf33a17 | 132 | (set (make-local-variable 'font-lock-verbose) nil))))) |
258544f8 | 133 | |
ccba0a20 | 134 | ;;;###autoload |
497a66ae GM |
135 | (defun pp-eval-expression (expression) |
136 | "Evaluate EXPRESSION and pretty-print its value. | |
137 | Also add the value to the front of the list in the variable `values'." | |
138 | (interactive | |
139 | (list (read-from-minibuffer "Eval: " nil read-expression-map t | |
140 | 'read-expression-history))) | |
141 | (message "Evaluating...") | |
142 | (setq values (cons (eval expression) values)) | |
143 | (pp-display-expression (car values) "*Pp Eval Output*")) | |
144 | ||
145 | ;;;###autoload | |
146 | (defun pp-macroexpand-expression (expression) | |
147 | "Macroexpand EXPRESSION and pretty-print its value." | |
148 | (interactive | |
149 | (list (read-from-minibuffer "Macroexpand: " nil read-expression-map t | |
150 | 'read-expression-history))) | |
151 | (pp-display-expression (macroexpand expression) "*Pp Macroexpand Output*")) | |
152 | ||
153 | (defun pp-last-sexp () | |
154 | "Read sexp before point. Ignores leading comment characters." | |
258544f8 RS |
155 | (let ((stab (syntax-table)) (pt (point)) start exp) |
156 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
157 | (save-excursion | |
158 | (forward-sexp -1) | |
159 | ;; If first line is commented, ignore all leading comments: | |
160 | (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;")) | |
161 | (progn | |
162 | (setq exp (buffer-substring (point) pt)) | |
163 | (while (string-match "\n[ \t]*;+" exp start) | |
164 | (setq start (1+ (match-beginning 0)) | |
165 | exp (concat (substring exp 0 start) | |
166 | (substring exp (match-end 0))))) | |
167 | (setq exp (read exp))) | |
168 | (setq exp (read (current-buffer))))) | |
169 | (set-syntax-table stab) | |
497a66ae GM |
170 | exp)) |
171 | ||
172 | ;;;###autoload | |
173 | (defun pp-eval-last-sexp (arg) | |
174 | "Run `pp-eval-expression' on sexp before point. | |
175 | With argument, pretty-print output into current buffer. | |
176 | Ignores leading comment characters." | |
177 | (interactive "P") | |
178 | (if arg | |
179 | (insert (pp-to-string (eval (pp-last-sexp)))) | |
180 | (pp-eval-expression (pp-last-sexp)))) | |
181 | ||
182 | ;;;###autoload | |
183 | (defun pp-macroexpand-last-sexp (arg) | |
184 | "Run `pp-macroexpand-expression' on sexp before point. | |
185 | With argument, pretty-print output into current buffer. | |
186 | Ignores leading comment characters." | |
187 | (interactive "P") | |
188 | (if arg | |
189 | (insert (pp-to-string (macroexpand (pp-last-sexp)))) | |
190 | (pp-macroexpand-expression (pp-last-sexp)))) | |
258544f8 RS |
191 | |
192 | ;;; Test cases for quote | |
193 | ;; (pp-eval-expression ''(quote quote)) | |
194 | ;; (pp-eval-expression ''((quote a) (quote b))) | |
195 | ;; (pp-eval-expression ''('a 'b)) ; same as above | |
196 | ;; (pp-eval-expression ''((quote (quote quote)) (quote quote))) | |
197 | ;; These do not satisfy the quote test. | |
198 | ;; (pp-eval-expression ''quote) | |
199 | ;; (pp-eval-expression ''(quote)) | |
200 | ;; (pp-eval-expression ''(quote . quote)) | |
201 | ;; (pp-eval-expression ''(quote a b)) | |
202 | ;; (pp-eval-expression ''(quotefoo)) | |
203 | ;; (pp-eval-expression ''(a b)) | |
204 | ||
205 | (provide 'pp) ; so (require 'pp) works | |
206 | ||
cbee283d | 207 | ;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9 |
e8af40ee | 208 | ;;; pp.el ends here |