Mercurial > emacs
annotate lisp/emacs-lisp/pp.el @ 12309:c78f7af6cee9
(quit_throw_to_read_char): Turn off the error check
testing poll_suppress_count.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 18 Jun 1995 22:28:53 +0000 |
parents | 084c36d46615 |
children | 84acc3adcd63 |
rev | line source |
---|---|
8544 | 1 ;; pp.el --- pretty printer for Emacs Lisp |
2 ;; Copyright (C) 1989, 1993 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Randal Schwartz <merlyn@ora.com> | |
5 | |
6 ;; This file is part of GNU Emacs. | |
7 | |
8 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 ;; it under the terms of the GNU General Public License as published by | |
10 ;; the Free Software Foundation; either version 2, or (at your option) | |
11 ;; any later version. | |
12 | |
13 ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 ;; GNU General Public License for more details. | |
17 | |
18 ;; You should have received a copy of the GNU General Public License | |
19 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | |
22 ;;; Code: | |
23 | |
24 (defvar pp-escape-newlines t | |
25 "*Value of print-escape-newlines used by pp-* functions.") | |
26 | |
27 (defun pp-to-string (object) | |
28 "Return a string containing the pretty-printed representation of OBJECT, | |
29 any Lisp object. Quoting characters are used when needed to make output | |
30 that `read' can handle, whenever this is possible." | |
31 (save-excursion | |
32 (set-buffer (generate-new-buffer " pp-to-string")) | |
33 (unwind-protect | |
34 (progn | |
10732
bd193969988d
(pp-to-string): Fix previous change.
Richard M. Stallman <rms@gnu.org>
parents:
10724
diff
changeset
|
35 (lisp-mode-variables t) |
8544 | 36 (let ((print-escape-newlines pp-escape-newlines)) |
37 (prin1 object (current-buffer))) | |
38 (goto-char (point-min)) | |
39 (while (not (eobp)) | |
40 ;; (message "%06d" (- (point-max) (point))) | |
41 (cond | |
42 ((looking-at "\\s\(") | |
43 (while (looking-at "\\s(") | |
44 (forward-char 1))) | |
45 ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)") | |
46 (> (match-beginning 1) 1) | |
47 (= ?\( (char-after (1- (match-beginning 1)))) | |
48 ;; Make sure this is a two-element list. | |
49 (save-excursion | |
50 (goto-char (match-beginning 2)) | |
51 (forward-sexp) | |
52 ;; (looking-at "[ \t]*\)") | |
53 ;; Avoid mucking with match-data; does this test work? | |
54 (char-equal ?\) (char-after (point))))) | |
55 ;; -1 gets the paren preceding the quote as well. | |
56 (delete-region (1- (match-beginning 1)) (match-end 1)) | |
57 (insert "'") | |
58 (forward-sexp 1) | |
59 (if (looking-at "[ \t]*\)") | |
60 (delete-region (match-beginning 0) (match-end 0)) | |
61 (error "Malformed quote")) | |
62 (backward-sexp 1)) | |
63 ((condition-case err-var | |
64 (prog1 t (down-list 1)) | |
65 (error nil)) | |
66 (backward-char 1) | |
67 (skip-chars-backward " \t") | |
68 (delete-region | |
69 (point) | |
70 (progn (skip-chars-forward " \t") (point))) | |
71 (if (not (char-equal ?' (char-after (1- (point))))) | |
72 (insert ?\n))) | |
73 ((condition-case err-var | |
74 (prog1 t (up-list 1)) | |
75 (error nil)) | |
76 (while (looking-at "\\s)") | |
77 (forward-char 1)) | |
78 (skip-chars-backward " \t") | |
79 (delete-region | |
80 (point) | |
81 (progn (skip-chars-forward " \t") (point))) | |
82 (if (not (char-equal ?' (char-after (1- (point))))) | |
83 (insert ?\n))) | |
84 (t (goto-char (point-max))))) | |
85 (goto-char (point-min)) | |
86 (indent-sexp) | |
87 (buffer-string)) | |
88 (kill-buffer (current-buffer))))) | |
89 | |
10329 | 90 ;;;###autoload |
8544 | 91 (defun pp (object &optional stream) |
92 "Output the pretty-printed representation of OBJECT, any Lisp object. | |
93 Quoting characters are printed when needed to make output that `read' | |
94 can handle, whenever this is possible. | |
95 Output stream is STREAM, or value of `standard-output' (which see)." | |
96 (princ (pp-to-string object) (or stream standard-output))) | |
97 | |
10329 | 98 ;;;###autoload |
8544 | 99 (defun pp-eval-expression (expression) |
100 "Evaluate EXPRESSION and pretty-print value into a new display buffer. | |
101 If the pretty-printed value fits on one line, the message line is used | |
102 instead. Value is also consed on to front of variable values 's | |
103 value." | |
104 (interactive "xPp-eval: ") | |
105 (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
|
106 (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
|
107 ;; 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
|
108 ;; 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
|
109 ;; 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
|
110 (temp-buffer-show-function |
8544 | 111 (function |
112 (lambda (buf) | |
113 (save-excursion | |
114 (set-buffer buf) | |
115 (goto-char (point-min)) | |
116 (end-of-line 1) | |
117 (if (or (< (1+ (point)) (point-max)) | |
118 (>= (- (point) (point-min)) (screen-width))) | |
11730
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
119 (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
|
120 (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
|
121 (window (display-buffer buf))) |
8544 | 122 (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
|
123 (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
|
124 (unwind-protect |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
125 (progn |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
126 (select-window window) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
127 (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
|
128 (select-window old-selected))) |
8544 | 129 (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
|
130 )))))) |
8544 | 131 (with-output-to-temp-buffer "*Pp Eval Output*" |
132 (pp (car values))) | |
133 (save-excursion | |
134 (set-buffer "*Pp Eval Output*") | |
135 (emacs-lisp-mode)))) | |
136 | |
10329 | 137 ;;;###autoload |
8544 | 138 (defun pp-eval-last-sexp (arg) |
139 "Run `pp-eval-expression' on sexp before point (which see). | |
140 With argument, pretty-print output into current buffer. | |
141 Ignores leading comment characters." | |
142 (interactive "P") | |
143 (let ((stab (syntax-table)) (pt (point)) start exp) | |
144 (set-syntax-table emacs-lisp-mode-syntax-table) | |
145 (save-excursion | |
146 (forward-sexp -1) | |
147 ;; If first line is commented, ignore all leading comments: | |
148 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;")) | |
149 (progn | |
150 (setq exp (buffer-substring (point) pt)) | |
151 (while (string-match "\n[ \t]*;+" exp start) | |
152 (setq start (1+ (match-beginning 0)) | |
153 exp (concat (substring exp 0 start) | |
154 (substring exp (match-end 0))))) | |
155 (setq exp (read exp))) | |
156 (setq exp (read (current-buffer))))) | |
157 (set-syntax-table stab) | |
158 (if arg | |
159 (insert (pp-to-string (eval exp))) | |
160 (pp-eval-expression exp)))) | |
161 | |
162 ;;; Test cases for quote | |
163 ;; (pp-eval-expression ''(quote quote)) | |
164 ;; (pp-eval-expression ''((quote a) (quote b))) | |
165 ;; (pp-eval-expression ''('a 'b)) ; same as above | |
166 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote))) | |
167 ;; These do not satisfy the quote test. | |
168 ;; (pp-eval-expression ''quote) | |
169 ;; (pp-eval-expression ''(quote)) | |
170 ;; (pp-eval-expression ''(quote . quote)) | |
171 ;; (pp-eval-expression ''(quote a b)) | |
172 ;; (pp-eval-expression ''(quotefoo)) | |
173 ;; (pp-eval-expression ''(a b)) | |
174 | |
175 (provide 'pp) ; so (require 'pp) works | |
176 | |
177 ;;; pp.el ends here. |