Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; pp.el --- pretty printer for Emacs Lisp |
b578f267 | 2 | |
284b3043 | 3 | ;; Copyright (C) 1989, 1993, 2001 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 | ||
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 2, or (at your option) | |
13 | ;; any later version. | |
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 | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
258544f8 | 24 | |
e8af40ee PJ |
25 | ;;; Commentary: |
26 | ||
258544f8 | 27 | ;;; Code: |
e8af40ee | 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 |
666b9413 SE |
35 | "*Value of `print-escape-newlines' used by pp-* functions." |
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." | |
258544f8 RS |
44 | (save-excursion |
45 | (set-buffer (generate-new-buffer " pp-to-string")) | |
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 RS |
52 | (prin1 object (current-buffer))) |
53 | (goto-char (point-min)) | |
54 | (while (not (eobp)) | |
55 | ;; (message "%06d" (- (point-max) (point))) | |
56 | (cond | |
258544f8 RS |
57 | ((condition-case err-var |
58 | (prog1 t (down-list 1)) | |
59 | (error nil)) | |
3d98a374 AS |
60 | (save-excursion |
61 | (backward-char 1) | |
62 | (skip-chars-backward "'`#^") | |
63 | (when (and (not (bobp)) (= ?\ (char-before))) | |
64 | (delete-char -1) | |
65 | (insert "\n")))) | |
258544f8 RS |
66 | ((condition-case err-var |
67 | (prog1 t (up-list 1)) | |
68 | (error nil)) | |
69 | (while (looking-at "\\s)") | |
70 | (forward-char 1)) | |
258544f8 RS |
71 | (delete-region |
72 | (point) | |
73 | (progn (skip-chars-forward " \t") (point))) | |
3d98a374 | 74 | (insert ?\n)) |
258544f8 RS |
75 | (t (goto-char (point-max))))) |
76 | (goto-char (point-min)) | |
77 | (indent-sexp) | |
78 | (buffer-string)) | |
79 | (kill-buffer (current-buffer))))) | |
80 | ||
ccba0a20 | 81 | ;;;###autoload |
258544f8 RS |
82 | (defun pp (object &optional stream) |
83 | "Output the pretty-printed representation of OBJECT, any Lisp object. | |
e189df50 | 84 | Quoting characters are printed as needed to make output that `read' |
258544f8 RS |
85 | can handle, whenever this is possible. |
86 | Output stream is STREAM, or value of `standard-output' (which see)." | |
87 | (princ (pp-to-string object) (or stream standard-output))) | |
88 | ||
ccba0a20 | 89 | ;;;###autoload |
258544f8 RS |
90 | (defun pp-eval-expression (expression) |
91 | "Evaluate EXPRESSION and pretty-print value into a new display buffer. | |
92 | If the pretty-printed value fits on one line, the message line is used | |
e189df50 KH |
93 | instead. The value is also consed onto the front of the list |
94 | in the variable `values'." | |
258544f8 RS |
95 | (interactive "xPp-eval: ") |
96 | (setq values (cons (eval expression) values)) | |
bf464fad RS |
97 | (let* ((old-show-function temp-buffer-show-function) |
98 | ;; Use this function to display the buffer. | |
99 | ;; This function either decides not to display it at all | |
100 | ;; or displays it in the usual way. | |
101 | (temp-buffer-show-function | |
258544f8 RS |
102 | (function |
103 | (lambda (buf) | |
104 | (save-excursion | |
105 | (set-buffer buf) | |
106 | (goto-char (point-min)) | |
107 | (end-of-line 1) | |
108 | (if (or (< (1+ (point)) (point-max)) | |
06115f9d | 109 | (>= (- (point) (point-min)) (frame-width))) |
bf464fad RS |
110 | (let ((temp-buffer-show-function old-show-function) |
111 | (old-selected (selected-window)) | |
112 | (window (display-buffer buf))) | |
258544f8 | 113 | (goto-char (point-min)) ; expected by some hooks ... |
bf464fad RS |
114 | (make-frame-visible (window-frame window)) |
115 | (unwind-protect | |
116 | (progn | |
117 | (select-window window) | |
118 | (run-hooks 'temp-buffer-show-hook)) | |
119 | (select-window old-selected))) | |
258544f8 | 120 | (message "%s" (buffer-substring (point-min) (point))) |
bf464fad | 121 | )))))) |
258544f8 RS |
122 | (with-output-to-temp-buffer "*Pp Eval Output*" |
123 | (pp (car values))) | |
124 | (save-excursion | |
125 | (set-buffer "*Pp Eval Output*") | |
9614b740 RS |
126 | (emacs-lisp-mode) |
127 | (make-local-variable 'font-lock-verbose) | |
128 | (setq font-lock-verbose nil)))) | |
258544f8 | 129 | |
ccba0a20 | 130 | ;;;###autoload |
258544f8 RS |
131 | (defun pp-eval-last-sexp (arg) |
132 | "Run `pp-eval-expression' on sexp before point (which see). | |
133 | With argument, pretty-print output into current buffer. | |
134 | Ignores leading comment characters." | |
135 | (interactive "P") | |
136 | (let ((stab (syntax-table)) (pt (point)) start exp) | |
137 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
138 | (save-excursion | |
139 | (forward-sexp -1) | |
140 | ;; If first line is commented, ignore all leading comments: | |
141 | (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;")) | |
142 | (progn | |
143 | (setq exp (buffer-substring (point) pt)) | |
144 | (while (string-match "\n[ \t]*;+" exp start) | |
145 | (setq start (1+ (match-beginning 0)) | |
146 | exp (concat (substring exp 0 start) | |
147 | (substring exp (match-end 0))))) | |
148 | (setq exp (read exp))) | |
149 | (setq exp (read (current-buffer))))) | |
150 | (set-syntax-table stab) | |
151 | (if arg | |
152 | (insert (pp-to-string (eval exp))) | |
153 | (pp-eval-expression exp)))) | |
154 | ||
155 | ;;; Test cases for quote | |
156 | ;; (pp-eval-expression ''(quote quote)) | |
157 | ;; (pp-eval-expression ''((quote a) (quote b))) | |
158 | ;; (pp-eval-expression ''('a 'b)) ; same as above | |
159 | ;; (pp-eval-expression ''((quote (quote quote)) (quote quote))) | |
160 | ;; These do not satisfy the quote test. | |
161 | ;; (pp-eval-expression ''quote) | |
162 | ;; (pp-eval-expression ''(quote)) | |
163 | ;; (pp-eval-expression ''(quote . quote)) | |
164 | ;; (pp-eval-expression ''(quote a b)) | |
165 | ;; (pp-eval-expression ''(quotefoo)) | |
166 | ;; (pp-eval-expression ''(a b)) | |
167 | ||
168 | (provide 'pp) ; so (require 'pp) works | |
169 | ||
ab5796a9 | 170 | ;;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9 |
e8af40ee | 171 | ;;; pp.el ends here |