Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; pp.el --- pretty printer for Emacs Lisp |
b578f267 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1989, 1993, 2001-2014 Free Software Foundation, Inc. |
258544f8 | 4 | |
df072942 | 5 | ;; Author: Randal Schwartz <merlyn@stonehenge.com> |
284b3043 | 6 | ;; Keywords: lisp |
258544f8 RS |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
d6cba7ae | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
258544f8 | 11 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
258544f8 RS |
14 | |
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. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
258544f8 | 22 | |
e8af40ee PJ |
23 | ;;; Commentary: |
24 | ||
258544f8 | 25 | ;;; Code: |
e8af40ee | 26 | |
f96b52a3 JB |
27 | (defvar font-lock-verbose) |
28 | ||
666b9413 SE |
29 | (defgroup pp nil |
30 | "Pretty printer for Emacs Lisp." | |
31 | :prefix "pp-" | |
32 | :group 'lisp) | |
33 | ||
a1506d29 | 34 | (defcustom pp-escape-newlines t |
cb711556 | 35 | "Value of `print-escape-newlines' used by pp-* functions." |
666b9413 SE |
36 | :type 'boolean |
37 | :group 'pp) | |
258544f8 | 38 | |
0e520006 | 39 | ;;;###autoload |
258544f8 | 40 | (defun pp-to-string (object) |
e189df50 KH |
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." | |
0696d255 JB |
44 | (with-temp-buffer |
45 | (lisp-mode-variables nil) | |
46 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
47 | (let ((print-escape-newlines pp-escape-newlines) | |
48 | (print-quoted t)) | |
49 | (prin1 object (current-buffer))) | |
50 | (pp-buffer) | |
51 | (buffer-string))) | |
258544f8 | 52 | |
c47e847f | 53 | ;;;###autoload |
575c3bca JL |
54 | (defun pp-buffer () |
55 | "Prettify the current buffer with printed representation of a Lisp object." | |
56 | (goto-char (point-min)) | |
57 | (while (not (eobp)) | |
58 | ;; (message "%06d" (- (point-max) (point))) | |
59 | (cond | |
0696d255 | 60 | ((ignore-errors (down-list 1) t) |
575c3bca JL |
61 | (save-excursion |
62 | (backward-char 1) | |
63 | (skip-chars-backward "'`#^") | |
1bcb666b | 64 | (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n))) |
575c3bca JL |
65 | (delete-region |
66 | (point) | |
67 | (progn (skip-chars-backward " \t\n") (point))) | |
68 | (insert "\n")))) | |
0696d255 JB |
69 | ((ignore-errors (up-list 1) t) |
70 | (while (looking-at-p "\\s)") | |
575c3bca JL |
71 | (forward-char 1)) |
72 | (delete-region | |
73 | (point) | |
74 | (progn (skip-chars-forward " \t\n") (point))) | |
75 | (insert ?\n)) | |
76 | (t (goto-char (point-max))))) | |
77 | (goto-char (point-min)) | |
78 | (indent-sexp)) | |
79 | ||
ccba0a20 | 80 | ;;;###autoload |
258544f8 RS |
81 | (defun pp (object &optional stream) |
82 | "Output the pretty-printed representation of OBJECT, any Lisp object. | |
e189df50 | 83 | Quoting characters are printed as needed to make output that `read' |
258544f8 RS |
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))) | |
87 | ||
497a66ae GM |
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." | |
bf464fad RS |
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 | |
258544f8 RS |
97 | (function |
98 | (lambda (buf) | |
9a529312 | 99 | (with-current-buffer buf |
258544f8 RS |
100 | (goto-char (point-min)) |
101 | (end-of-line 1) | |
102 | (if (or (< (1+ (point)) (point-max)) | |
06115f9d | 103 | (>= (- (point) (point-min)) (frame-width))) |
bf464fad RS |
104 | (let ((temp-buffer-show-function old-show-function) |
105 | (old-selected (selected-window)) | |
106 | (window (display-buffer buf))) | |
258544f8 | 107 | (goto-char (point-min)) ; expected by some hooks ... |
bf464fad RS |
108 | (make-frame-visible (window-frame window)) |
109 | (unwind-protect | |
110 | (progn | |
111 | (select-window window) | |
112 | (run-hooks 'temp-buffer-show-hook)) | |
f08088e3 CS |
113 | (when (window-live-p old-selected) |
114 | (select-window old-selected)) | |
497a66ae | 115 | (message "See buffer %s." out-buffer-name))) |
258544f8 | 116 | (message "%s" (buffer-substring (point-min) (point))) |
bf464fad | 117 | )))))) |
497a66ae GM |
118 | (with-output-to-temp-buffer out-buffer-name |
119 | (pp expression) | |
eaf33a17 SM |
120 | (with-current-buffer standard-output |
121 | (emacs-lisp-mode) | |
52f55ab0 | 122 | (setq buffer-read-only nil) |
eaf33a17 | 123 | (set (make-local-variable 'font-lock-verbose) nil))))) |
258544f8 | 124 | |
ccba0a20 | 125 | ;;;###autoload |
497a66ae GM |
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'." | |
129 | (interactive | |
9f7b1925 | 130 | (list (read--expression "Eval: "))) |
497a66ae GM |
131 | (message "Evaluating...") |
132 | (setq values (cons (eval expression) values)) | |
133 | (pp-display-expression (car values) "*Pp Eval Output*")) | |
134 | ||
135 | ;;;###autoload | |
136 | (defun pp-macroexpand-expression (expression) | |
137 | "Macroexpand EXPRESSION and pretty-print its value." | |
138 | (interactive | |
9f7b1925 | 139 | (list (read--expression "Macroexpand: "))) |
497a66ae GM |
140 | (pp-display-expression (macroexpand expression) "*Pp Macroexpand Output*")) |
141 | ||
142 | (defun pp-last-sexp () | |
143 | "Read sexp before point. Ignores leading comment characters." | |
258544f8 RS |
144 | (let ((stab (syntax-table)) (pt (point)) start exp) |
145 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
146 | (save-excursion | |
147 | (forward-sexp -1) | |
148 | ;; If first line is commented, ignore all leading comments: | |
0696d255 | 149 | (if (save-excursion (beginning-of-line) (looking-at-p "[ \t]*;")) |
258544f8 RS |
150 | (progn |
151 | (setq exp (buffer-substring (point) pt)) | |
152 | (while (string-match "\n[ \t]*;+" exp start) | |
153 | (setq start (1+ (match-beginning 0)) | |
154 | exp (concat (substring exp 0 start) | |
155 | (substring exp (match-end 0))))) | |
156 | (setq exp (read exp))) | |
157 | (setq exp (read (current-buffer))))) | |
158 | (set-syntax-table stab) | |
497a66ae GM |
159 | exp)) |
160 | ||
161 | ;;;###autoload | |
162 | (defun pp-eval-last-sexp (arg) | |
163 | "Run `pp-eval-expression' on sexp before point. | |
164 | With argument, pretty-print output into current buffer. | |
165 | Ignores leading comment characters." | |
166 | (interactive "P") | |
167 | (if arg | |
168 | (insert (pp-to-string (eval (pp-last-sexp)))) | |
169 | (pp-eval-expression (pp-last-sexp)))) | |
170 | ||
171 | ;;;###autoload | |
172 | (defun pp-macroexpand-last-sexp (arg) | |
173 | "Run `pp-macroexpand-expression' on sexp before point. | |
174 | With argument, pretty-print output into current buffer. | |
175 | Ignores leading comment characters." | |
176 | (interactive "P") | |
177 | (if arg | |
178 | (insert (pp-to-string (macroexpand (pp-last-sexp)))) | |
179 | (pp-macroexpand-expression (pp-last-sexp)))) | |
258544f8 RS |
180 | |
181 | ;;; Test cases for quote | |
182 | ;; (pp-eval-expression ''(quote quote)) | |
183 | ;; (pp-eval-expression ''((quote a) (quote b))) | |
184 | ;; (pp-eval-expression ''('a 'b)) ; same as above | |
185 | ;; (pp-eval-expression ''((quote (quote quote)) (quote quote))) | |
186 | ;; These do not satisfy the quote test. | |
187 | ;; (pp-eval-expression ''quote) | |
188 | ;; (pp-eval-expression ''(quote)) | |
189 | ;; (pp-eval-expression ''(quote . quote)) | |
190 | ;; (pp-eval-expression ''(quote a b)) | |
191 | ;; (pp-eval-expression ''(quotefoo)) | |
192 | ;; (pp-eval-expression ''(a b)) | |
193 | ||
194 | (provide 'pp) ; so (require 'pp) works | |
195 | ||
e8af40ee | 196 | ;;; pp.el ends here |