Mercurial > emacs
annotate lisp/emacs-lisp/pp.el @ 55857:a713acca9ca9
(find-file): Doc fix.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 30 May 2004 13:34:19 +0000 |
parents | 7bb617df33db |
children | 25c3950468a9 4c90ffeb71c5 |
rev | line source |
---|---|
13337 | 1 ;;; pp.el --- pretty printer for Emacs Lisp |
14169 | 2 |
54502
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
3 ;; Copyright (C) 1989, 1993, 2001, 2004 Free Software Foundation, Inc. |
8544 | 4 |
14224 | 5 ;; Author: Randal Schwartz <merlyn@stonehenge.com> |
39117 | 6 ;; Keywords: lisp |
8544 | 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 | |
14169 | 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. | |
8544 | 24 |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
23244
diff
changeset
|
25 ;;; Commentary: |
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
23244
diff
changeset
|
26 |
8544 | 27 ;;; Code: |
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
23244
diff
changeset
|
28 |
21365 | 29 (defgroup pp nil |
30 "Pretty printer for Emacs Lisp." | |
31 :prefix "pp-" | |
32 :group 'lisp) | |
8544 | 33 |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
43295
diff
changeset
|
34 (defcustom pp-escape-newlines t |
21365 | 35 "*Value of `print-escape-newlines' used by pp-* functions." |
36 :type 'boolean | |
37 :group 'pp) | |
8544 | 38 |
43295
ce2590f06ba0
2002-02-14 Per Abrahamsen <abraham@dina.kvl.dk>
Per Abrahamsen <abraham@dina.kvl.dk>
parents:
39117
diff
changeset
|
39 ;;;###autoload |
8544 | 40 (defun pp-to-string (object) |
23244 | 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." | |
8544 | 44 (save-excursion |
45 (set-buffer (generate-new-buffer " pp-to-string")) | |
46 (unwind-protect | |
47 (progn | |
19147
ec57f19bc39c
(pp-to-string): Use emacs-lisp-mode-sytax-table.
Richard M. Stallman <rms@gnu.org>
parents:
16171
diff
changeset
|
48 (lisp-mode-variables nil) |
ec57f19bc39c
(pp-to-string): Use emacs-lisp-mode-sytax-table.
Richard M. Stallman <rms@gnu.org>
parents:
16171
diff
changeset
|
49 (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
|
50 (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
|
51 (print-quoted t)) |
8544 | 52 (prin1 object (current-buffer))) |
55802
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
53 (pp-buffer) |
8544 | 54 (buffer-string)) |
55 (kill-buffer (current-buffer))))) | |
56 | |
55802
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
57 (defun pp-buffer () |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
58 "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
|
59 (goto-char (point-min)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
60 (while (not (eobp)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
61 ;; (message "%06d" (- (point-max) (point))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
62 (cond |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
63 ((condition-case err-var |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
64 (prog1 t (down-list 1)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
65 (error nil)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
66 (save-excursion |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
67 (backward-char 1) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
68 (skip-chars-backward "'`#^") |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
69 (when (and (not (bobp)) (memq (char-before) '(?\ ?\t ?\n))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
70 (delete-region |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
71 (point) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
72 (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
|
73 (insert "\n")))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
74 ((condition-case err-var |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
75 (prog1 t (up-list 1)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
76 (error nil)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
77 (while (looking-at "\\s)") |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
78 (forward-char 1)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
79 (delete-region |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
80 (point) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
81 (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
|
82 (insert ?\n)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
83 (t (goto-char (point-max))))) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
84 (goto-char (point-min)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
85 (indent-sexp)) |
7bb617df33db
(pp-buffer): New fun created from the code in
Juri Linkov <juri@jurta.org>
parents:
54502
diff
changeset
|
86 |
10329 | 87 ;;;###autoload |
8544 | 88 (defun pp (object &optional stream) |
89 "Output the pretty-printed representation of OBJECT, any Lisp object. | |
23244 | 90 Quoting characters are printed as needed to make output that `read' |
8544 | 91 can handle, whenever this is possible. |
92 Output stream is STREAM, or value of `standard-output' (which see)." | |
93 (princ (pp-to-string object) (or stream standard-output))) | |
94 | |
10329 | 95 ;;;###autoload |
8544 | 96 (defun pp-eval-expression (expression) |
97 "Evaluate EXPRESSION and pretty-print value into a new display buffer. | |
98 If the pretty-printed value fits on one line, the message line is used | |
23244 | 99 instead. The value is also consed onto the front of the list |
100 in the variable `values'." | |
8544 | 101 (interactive "xPp-eval: ") |
102 (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
|
103 (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
|
104 ;; 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
|
105 ;; 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
|
106 ;; 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
|
107 (temp-buffer-show-function |
8544 | 108 (function |
109 (lambda (buf) | |
110 (save-excursion | |
111 (set-buffer buf) | |
112 (goto-char (point-min)) | |
113 (end-of-line 1) | |
114 (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
|
115 (>= (- (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
|
116 (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
|
117 (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
|
118 (window (display-buffer buf))) |
8544 | 119 (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
|
120 (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
|
121 (unwind-protect |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
122 (progn |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
123 (select-window window) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
124 (run-hooks 'temp-buffer-show-hook)) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
125 (select-window old-selected))) |
8544 | 126 (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
|
127 )))))) |
8544 | 128 (with-output-to-temp-buffer "*Pp Eval Output*" |
54502
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
129 (pp (car values)) |
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
130 (with-current-buffer standard-output |
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
131 (emacs-lisp-mode) |
a8550ca246eb
(pp-eval-expression): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
132 (set (make-local-variable 'font-lock-verbose) nil))))) |
8544 | 133 |
10329 | 134 ;;;###autoload |
8544 | 135 (defun pp-eval-last-sexp (arg) |
136 "Run `pp-eval-expression' on sexp before point (which see). | |
137 With argument, pretty-print output into current buffer. | |
138 Ignores leading comment characters." | |
139 (interactive "P") | |
140 (let ((stab (syntax-table)) (pt (point)) start exp) | |
141 (set-syntax-table emacs-lisp-mode-syntax-table) | |
142 (save-excursion | |
143 (forward-sexp -1) | |
144 ;; If first line is commented, ignore all leading comments: | |
145 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;")) | |
146 (progn | |
147 (setq exp (buffer-substring (point) pt)) | |
148 (while (string-match "\n[ \t]*;+" exp start) | |
149 (setq start (1+ (match-beginning 0)) | |
150 exp (concat (substring exp 0 start) | |
151 (substring exp (match-end 0))))) | |
152 (setq exp (read exp))) | |
153 (setq exp (read (current-buffer))))) | |
154 (set-syntax-table stab) | |
155 (if arg | |
156 (insert (pp-to-string (eval exp))) | |
157 (pp-eval-expression exp)))) | |
158 | |
159 ;;; Test cases for quote | |
160 ;; (pp-eval-expression ''(quote quote)) | |
161 ;; (pp-eval-expression ''((quote a) (quote b))) | |
162 ;; (pp-eval-expression ''('a 'b)) ; same as above | |
163 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote))) | |
164 ;; These do not satisfy the quote test. | |
165 ;; (pp-eval-expression ''quote) | |
166 ;; (pp-eval-expression ''(quote)) | |
167 ;; (pp-eval-expression ''(quote . quote)) | |
168 ;; (pp-eval-expression ''(quote a b)) | |
169 ;; (pp-eval-expression ''(quotefoo)) | |
170 ;; (pp-eval-expression ''(a b)) | |
171 | |
172 (provide 'pp) ; so (require 'pp) works | |
173 | |
52401 | 174 ;;; 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
|
175 ;;; pp.el ends here |