Mercurial > emacs
annotate lisp/emacs-lisp/pp.el @ 92291:d2de7d81aab9
Update commentary.
Remove leading `*' from defcustom doc-strings.
(Info-menu, Info-goto-node): Remove declarations.
(report-emacs-bug-info): Use info rather than Info-goto-node.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Thu, 28 Feb 2008 03:43:47 +0000 |
parents | 107ccd98fa12 |
children | 606f2d163a64 1e3a407766b9 |
rev | line source |
---|---|
13337 | 1 ;;; pp.el --- pretty printer for Emacs Lisp |
14169 | 2 |
64751
5b1a238fcbb4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1989, 1993, 2001, 2002, 2003, 2004, |
79704 | 4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
8544 | 5 |
14224 | 6 ;; Author: Randal Schwartz <merlyn@stonehenge.com> |
39117 | 7 ;; Keywords: lisp |
8544 | 8 |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
78217
935157c0b596
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75473
diff
changeset
|
13 ;; the Free Software Foundation; either version 3, or (at your option) |
8544 | 14 ;; any later version. |
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 | |
14169 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64085 | 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 ;; Boston, MA 02110-1301, USA. | |
8544 | 25 |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
23244
diff
changeset
|
26 ;;; Commentary: |
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
23244
diff
changeset
|
27 |
8544 | 28 ;;; Code: |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
23244
diff
changeset
|
29 |
65193
228d206af8c8
(font-lock-verbose): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents:
64751
diff
changeset
|
30 (defvar font-lock-verbose) |
228d206af8c8
(font-lock-verbose): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents:
64751
diff
changeset
|
31 |
21365 | 32 (defgroup pp nil |
33 "Pretty printer for Emacs Lisp." | |
34 :prefix "pp-" | |
35 :group 'lisp) | |
8544 | 36 |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
43295
diff
changeset
|
37 (defcustom pp-escape-newlines t |
21365 | 38 "*Value of `print-escape-newlines' used by pp-* functions." |
39 :type 'boolean | |
40 :group 'pp) | |
8544 | 41 |
43295
ce2590f06ba0
2002-02-14 Per Abrahamsen <abraham@dina.kvl.dk>
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
39117
diff
changeset
|
42 ;;;###autoload |
8544 | 43 (defun pp-to-string (object) |
23244 | 44 "Return a string containing the pretty-printed representation of OBJECT. |
45 OBJECT can be any Lisp object. Quoting characters are used as needed | |
46 to make output that `read' can handle, whenever this is possible." | |
8544 | 47 (save-excursion |
48 (set-buffer (generate-new-buffer " pp-to-string")) | |
49 (unwind-protect | |
50 (progn | |
19147
ec57f19bc39c
(pp-to-string): Use emacs-lisp-mode-sytax-table.
Richard M. Stallman <rms@gnu.org>
parents:
16171
diff
changeset
|
51 (lisp-mode-variables nil) |
ec57f19bc39c
(pp-to-string): Use emacs-lisp-mode-sytax-table.
Richard M. Stallman <rms@gnu.org>
parents:
16171
diff
changeset
|
52 (set-syntax-table emacs-lisp-mode-syntax-table) |
20598
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
53 (let ((print-escape-newlines pp-escape-newlines) |
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
54 (print-quoted t)) |
8544 | 55 (prin1 object (current-buffer))) |
55802
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
56 (pp-buffer) |
8544 | 57 (buffer-string)) |
58 (kill-buffer (current-buffer))))) | |
59 | |
56274 | 60 ;;;###autoload |
55802
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
61 (defun pp-buffer () |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
62 "Prettify the current buffer with printed representation of a Lisp object." |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
63 (goto-char (point-min)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
64 (while (not (eobp)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
65 ;; (message "%06d" (- (point-max) (point))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
66 (cond |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
67 ((condition-case err-var |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
68 (prog1 t (down-list 1)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
69 (error nil)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
70 (save-excursion |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
71 (backward-char 1) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
72 (skip-chars-backward "'`#^") |
63852
dd7fb3231397
(pp-buffer): Change space constants followed by a sexp to "?\s ".
Juanma Barranquero <lekktu@gmail.com>
parents:
56274
diff
changeset
|
73 (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n))) |
55802
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
74 (delete-region |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
75 (point) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
76 (progn (skip-chars-backward " \t\n") (point))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
77 (insert "\n")))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
78 ((condition-case err-var |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
79 (prog1 t (up-list 1)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
80 (error nil)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
81 (while (looking-at "\\s)") |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
82 (forward-char 1)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
83 (delete-region |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
84 (point) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
85 (progn (skip-chars-forward " \t\n") (point))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
86 (insert ?\n)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
87 (t (goto-char (point-max))))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
88 (goto-char (point-min)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
89 (indent-sexp)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
90 |
10329 | 91 ;;;###autoload |
8544 | 92 (defun pp (object &optional stream) |
93 "Output the pretty-printed representation of OBJECT, any Lisp object. | |
23244 | 94 Quoting characters are printed as needed to make output that `read' |
8544 | 95 can handle, whenever this is possible. |
96 Output stream is STREAM, or value of `standard-output' (which see)." | |
97 (princ (pp-to-string object) (or stream standard-output))) | |
98 | |
10329 | 99 ;;;###autoload |
75173
4e312c5cf360
(pp-eval-expression): Once again eval the argument, but read it as `X' does.
Richard M. Stallman <rms@gnu.org>
parents:
73632
diff
changeset
|
100 (defun pp-eval-expression (expression) |
4e312c5cf360
(pp-eval-expression): Once again eval the argument, but read it as `X' does.
Richard M. Stallman <rms@gnu.org>
parents:
73632
diff
changeset
|
101 "Evaluate EXPRESSION and pretty-print its value. |
4e312c5cf360
(pp-eval-expression): Once again eval the argument, but read it as `X' does.
Richard M. Stallman <rms@gnu.org>
parents:
73632
diff
changeset
|
102 Also add the value to the front of the list in the variable `values'." |
4e312c5cf360
(pp-eval-expression): Once again eval the argument, but read it as `X' does.
Richard M. Stallman <rms@gnu.org>
parents:
73632
diff
changeset
|
103 (interactive |
4e312c5cf360
(pp-eval-expression): Once again eval the argument, but read it as `X' does.
Richard M. Stallman <rms@gnu.org>
parents:
73632
diff
changeset
|
104 (list (read-from-minibuffer "Eval: " nil read-expression-map t |
4e312c5cf360
(pp-eval-expression): Once again eval the argument, but read it as `X' does.
Richard M. Stallman <rms@gnu.org>
parents:
73632
diff
changeset
|
105 'read-expression-history))) |
87649 | 106 (message "Evaluating...") |
75173
4e312c5cf360
(pp-eval-expression): Once again eval the argument, but read it as `X' does.
Richard M. Stallman <rms@gnu.org>
parents:
73632
diff
changeset
|
107 (setq values (cons (eval expression) values)) |
11730
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
108 (let* ((old-show-function temp-buffer-show-function) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
109 ;; Use this function to display the buffer. |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
110 ;; This function either decides not to display it at all |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
111 ;; or displays it in the usual way. |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
112 (temp-buffer-show-function |
8544 | 113 (function |
114 (lambda (buf) | |
115 (save-excursion | |
116 (set-buffer buf) | |
117 (goto-char (point-min)) | |
118 (end-of-line 1) | |
119 (if (or (< (1+ (point)) (point-max)) | |
14225
ec298b8bee29
(pp-eval-expression): Use `frame-width' instead of `screen-width'.
Erik Naggum <erik@naggum.no>
parents:
14224
diff
changeset
|
120 (>= (- (point) (point-min)) (frame-width))) |
11730
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
121 (let ((temp-buffer-show-function old-show-function) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
122 (old-selected (selected-window)) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
123 (window (display-buffer buf))) |
8544 | 124 (goto-char (point-min)) ; expected by some hooks ... |
11730
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
125 (make-frame-visible (window-frame window)) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
126 (unwind-protect |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
127 (progn |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
128 (select-window window) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
129 (run-hooks 'temp-buffer-show-hook)) |
87649 | 130 (select-window old-selected) |
131 (message "Evaluating...done. \ | |
132 See buffer *Pp Eval Output*."))) | |
8544 | 133 (message "%s" (buffer-substring (point-min) (point))) |
11730
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
134 )))))) |
8544 | 135 (with-output-to-temp-buffer "*Pp Eval Output*" |
54502
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
136 (pp (car values)) |
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
137 (with-current-buffer standard-output |
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
138 (emacs-lisp-mode) |
87649 | 139 (setq buffer-read-only nil) |
54502
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
140 (set (make-local-variable 'font-lock-verbose) nil))))) |
8544 | 141 |
10329 | 142 ;;;###autoload |
8544 | 143 (defun pp-eval-last-sexp (arg) |
144 "Run `pp-eval-expression' on sexp before point (which see). | |
145 With argument, pretty-print output into current buffer. | |
146 Ignores leading comment characters." | |
147 (interactive "P") | |
148 (let ((stab (syntax-table)) (pt (point)) start exp) | |
149 (set-syntax-table emacs-lisp-mode-syntax-table) | |
150 (save-excursion | |
151 (forward-sexp -1) | |
152 ;; If first line is commented, ignore all leading comments: | |
153 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;")) | |
154 (progn | |
155 (setq exp (buffer-substring (point) pt)) | |
156 (while (string-match "\n[ \t]*;+" exp start) | |
157 (setq start (1+ (match-beginning 0)) | |
158 exp (concat (substring exp 0 start) | |
159 (substring exp (match-end 0))))) | |
160 (setq exp (read exp))) | |
161 (setq exp (read (current-buffer))))) | |
162 (set-syntax-table stab) | |
163 (if arg | |
164 (insert (pp-to-string (eval exp))) | |
75473
869eaab96a64
(pp-eval-last-sexp): Don't eval here.
Richard M. Stallman <rms@gnu.org>
parents:
75346
diff
changeset
|
165 (pp-eval-expression exp)))) |
8544 | 166 |
167 ;;; Test cases for quote | |
168 ;; (pp-eval-expression ''(quote quote)) | |
169 ;; (pp-eval-expression ''((quote a) (quote b))) | |
170 ;; (pp-eval-expression ''('a 'b)) ; same as above | |
171 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote))) | |
172 ;; These do not satisfy the quote test. | |
173 ;; (pp-eval-expression ''quote) | |
174 ;; (pp-eval-expression ''(quote)) | |
175 ;; (pp-eval-expression ''(quote . quote)) | |
176 ;; (pp-eval-expression ''(quote a b)) | |
177 ;; (pp-eval-expression ''(quotefoo)) | |
178 ;; (pp-eval-expression ''(a b)) | |
179 | |
180 (provide 'pp) ; so (require 'pp) works | |
181 | |
52401 | 182 ;;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9 |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
23244
diff
changeset
|
183 ;;; pp.el ends here |