Mercurial > emacs
annotate lisp/emacs-lisp/pp.el @ 19765:adacf35e99eb libc-970906 libc-970907 libc-970908
* config.sub: Recognize v850-elf.
author | Jeff Law <law@redhat.com> |
---|---|
date | Fri, 05 Sep 1997 18:08:34 +0000 |
parents | ec57f19bc39c |
children | a61e2395961f |
rev | line source |
---|---|
13337 | 1 ;;; pp.el --- pretty printer for Emacs Lisp |
14169 | 2 |
8544 | 3 ;; Copyright (C) 1989, 1993 Free Software Foundation, Inc. |
4 | |
14224 | 5 ;; Author: Randal Schwartz <merlyn@stonehenge.com> |
8544 | 6 |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
14169 | 20 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
8544 | 23 |
24 ;;; Code: | |
25 | |
26 (defvar pp-escape-newlines t | |
27 "*Value of print-escape-newlines used by pp-* functions.") | |
28 | |
29 (defun pp-to-string (object) | |
30 "Return a string containing the pretty-printed representation of OBJECT, | |
31 any Lisp object. Quoting characters are used when needed to make output | |
32 that `read' can handle, whenever this is possible." | |
33 (save-excursion | |
34 (set-buffer (generate-new-buffer " pp-to-string")) | |
35 (unwind-protect | |
36 (progn | |
19147
ec57f19bc39c
(pp-to-string): Use emacs-lisp-mode-sytax-table.
Richard M. Stallman <rms@gnu.org>
parents:
16171
diff
changeset
|
37 (lisp-mode-variables nil) |
ec57f19bc39c
(pp-to-string): Use emacs-lisp-mode-sytax-table.
Richard M. Stallman <rms@gnu.org>
parents:
16171
diff
changeset
|
38 (set-syntax-table emacs-lisp-mode-syntax-table) |
8544 | 39 (let ((print-escape-newlines pp-escape-newlines)) |
40 (prin1 object (current-buffer))) | |
41 (goto-char (point-min)) | |
42 (while (not (eobp)) | |
43 ;; (message "%06d" (- (point-max) (point))) | |
44 (cond | |
15446
13e4d0daa8c1
(pp-to-string): Treat #( like (.
Richard M. Stallman <rms@gnu.org>
parents:
14225
diff
changeset
|
45 ((looking-at "\\s(\\|#\\s(") |
13e4d0daa8c1
(pp-to-string): Treat #( like (.
Richard M. Stallman <rms@gnu.org>
parents:
14225
diff
changeset
|
46 (while (looking-at "\\s(\\|#\\s(") |
8544 | 47 (forward-char 1))) |
48 ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)") | |
49 (> (match-beginning 1) 1) | |
50 (= ?\( (char-after (1- (match-beginning 1)))) | |
51 ;; Make sure this is a two-element list. | |
52 (save-excursion | |
53 (goto-char (match-beginning 2)) | |
54 (forward-sexp) | |
55 ;; (looking-at "[ \t]*\)") | |
56 ;; Avoid mucking with match-data; does this test work? | |
57 (char-equal ?\) (char-after (point))))) | |
58 ;; -1 gets the paren preceding the quote as well. | |
59 (delete-region (1- (match-beginning 1)) (match-end 1)) | |
60 (insert "'") | |
61 (forward-sexp 1) | |
62 (if (looking-at "[ \t]*\)") | |
63 (delete-region (match-beginning 0) (match-end 0)) | |
64 (error "Malformed quote")) | |
65 (backward-sexp 1)) | |
66 ((condition-case err-var | |
67 (prog1 t (down-list 1)) | |
68 (error nil)) | |
69 (backward-char 1) | |
70 (skip-chars-backward " \t") | |
71 (delete-region | |
72 (point) | |
73 (progn (skip-chars-forward " \t") (point))) | |
74 (if (not (char-equal ?' (char-after (1- (point))))) | |
75 (insert ?\n))) | |
76 ((condition-case err-var | |
77 (prog1 t (up-list 1)) | |
78 (error nil)) | |
79 (while (looking-at "\\s)") | |
80 (forward-char 1)) | |
81 (skip-chars-backward " \t") | |
82 (delete-region | |
83 (point) | |
84 (progn (skip-chars-forward " \t") (point))) | |
85 (if (not (char-equal ?' (char-after (1- (point))))) | |
86 (insert ?\n))) | |
87 (t (goto-char (point-max))))) | |
88 (goto-char (point-min)) | |
89 (indent-sexp) | |
90 (buffer-string)) | |
91 (kill-buffer (current-buffer))))) | |
92 | |
10329 | 93 ;;;###autoload |
8544 | 94 (defun pp (object &optional stream) |
95 "Output the pretty-printed representation of OBJECT, any Lisp object. | |
96 Quoting characters are printed when needed to make output that `read' | |
97 can handle, whenever this is possible. | |
98 Output stream is STREAM, or value of `standard-output' (which see)." | |
99 (princ (pp-to-string object) (or stream standard-output))) | |
100 | |
10329 | 101 ;;;###autoload |
8544 | 102 (defun pp-eval-expression (expression) |
103 "Evaluate EXPRESSION and pretty-print value into a new display buffer. | |
104 If the pretty-printed value fits on one line, the message line is used | |
105 instead. Value is also consed on to front of variable values 's | |
106 value." | |
107 (interactive "xPp-eval: ") | |
108 (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
|
109 (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
|
110 ;; 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
|
111 ;; 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
|
112 ;; 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
|
113 (temp-buffer-show-function |
8544 | 114 (function |
115 (lambda (buf) | |
116 (save-excursion | |
117 (set-buffer buf) | |
118 (goto-char (point-min)) | |
119 (end-of-line 1) | |
120 (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
|
121 (>= (- (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
|
122 (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
|
123 (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
|
124 (window (display-buffer buf))) |
8544 | 125 (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
|
126 (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
|
127 (unwind-protect |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
128 (progn |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
129 (select-window window) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
130 (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
|
131 (select-window old-selected))) |
8544 | 132 (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
|
133 )))))) |
8544 | 134 (with-output-to-temp-buffer "*Pp Eval Output*" |
135 (pp (car values))) | |
136 (save-excursion | |
137 (set-buffer "*Pp Eval Output*") | |
16171
919ed1778c0b
(pp-eval-expression): Set font-lock-default locally to nil.
Richard M. Stallman <rms@gnu.org>
parents:
15446
diff
changeset
|
138 (emacs-lisp-mode) |
919ed1778c0b
(pp-eval-expression): Set font-lock-default locally to nil.
Richard M. Stallman <rms@gnu.org>
parents:
15446
diff
changeset
|
139 (make-local-variable 'font-lock-verbose) |
919ed1778c0b
(pp-eval-expression): Set font-lock-default locally to nil.
Richard M. Stallman <rms@gnu.org>
parents:
15446
diff
changeset
|
140 (setq 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))) | |
165 (pp-eval-expression exp)))) | |
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 | |
182 ;;; pp.el ends here. |