Mercurial > emacs
annotate lisp/emacs-lisp/pp.el @ 20947:a66eb4a41765
(rmail-decode-babyl-format): Message modified.
(rmail): Comment for the binding of rmail-enable-multibyte added.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Fri, 20 Feb 1998 11:15:28 +0000 |
parents | a61e2395961f |
children | db005054f15d |
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) |
20598
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
39 (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
|
40 (print-quoted t)) |
8544 | 41 (prin1 object (current-buffer))) |
42 (goto-char (point-min)) | |
43 (while (not (eobp)) | |
44 ;; (message "%06d" (- (point-max) (point))) | |
45 (cond | |
46 ((condition-case err-var | |
47 (prog1 t (down-list 1)) | |
48 (error nil)) | |
20598
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
49 (save-excursion |
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
50 (backward-char 1) |
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
51 (skip-chars-backward "'`#^") |
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
52 (when (and (not (bobp)) (= ?\ (char-before))) |
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
53 (delete-char -1) |
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
54 (insert "\n")))) |
8544 | 55 ((condition-case err-var |
56 (prog1 t (up-list 1)) | |
57 (error nil)) | |
58 (while (looking-at "\\s)") | |
59 (forward-char 1)) | |
60 (delete-region | |
61 (point) | |
62 (progn (skip-chars-forward " \t") (point))) | |
20598
a61e2395961f
(pp-to-string): Greatly simplify by letting the
Andreas Schwab <schwab@suse.de>
parents:
19147
diff
changeset
|
63 (insert ?\n)) |
8544 | 64 (t (goto-char (point-max))))) |
65 (goto-char (point-min)) | |
66 (indent-sexp) | |
67 (buffer-string)) | |
68 (kill-buffer (current-buffer))))) | |
69 | |
10329 | 70 ;;;###autoload |
8544 | 71 (defun pp (object &optional stream) |
72 "Output the pretty-printed representation of OBJECT, any Lisp object. | |
73 Quoting characters are printed when needed to make output that `read' | |
74 can handle, whenever this is possible. | |
75 Output stream is STREAM, or value of `standard-output' (which see)." | |
76 (princ (pp-to-string object) (or stream standard-output))) | |
77 | |
10329 | 78 ;;;###autoload |
8544 | 79 (defun pp-eval-expression (expression) |
80 "Evaluate EXPRESSION and pretty-print value into a new display buffer. | |
81 If the pretty-printed value fits on one line, the message line is used | |
82 instead. Value is also consed on to front of variable values 's | |
83 value." | |
84 (interactive "xPp-eval: ") | |
85 (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
|
86 (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
|
87 ;; 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
|
88 ;; 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
|
89 ;; 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
|
90 (temp-buffer-show-function |
8544 | 91 (function |
92 (lambda (buf) | |
93 (save-excursion | |
94 (set-buffer buf) | |
95 (goto-char (point-min)) | |
96 (end-of-line 1) | |
97 (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
|
98 (>= (- (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
|
99 (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
|
100 (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
|
101 (window (display-buffer buf))) |
8544 | 102 (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
|
103 (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
|
104 (unwind-protect |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
105 (progn |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
106 (select-window window) |
084c36d46615
(pp-eval-expression): Update use of temp-buffer-show-function.
Richard M. Stallman <rms@gnu.org>
parents:
10732
diff
changeset
|
107 (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
|
108 (select-window old-selected))) |
8544 | 109 (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
|
110 )))))) |
8544 | 111 (with-output-to-temp-buffer "*Pp Eval Output*" |
112 (pp (car values))) | |
113 (save-excursion | |
114 (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
|
115 (emacs-lisp-mode) |
919ed1778c0b
(pp-eval-expression): Set font-lock-default locally to nil.
Richard M. Stallman <rms@gnu.org>
parents:
15446
diff
changeset
|
116 (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
|
117 (setq font-lock-verbose nil)))) |
8544 | 118 |
10329 | 119 ;;;###autoload |
8544 | 120 (defun pp-eval-last-sexp (arg) |
121 "Run `pp-eval-expression' on sexp before point (which see). | |
122 With argument, pretty-print output into current buffer. | |
123 Ignores leading comment characters." | |
124 (interactive "P") | |
125 (let ((stab (syntax-table)) (pt (point)) start exp) | |
126 (set-syntax-table emacs-lisp-mode-syntax-table) | |
127 (save-excursion | |
128 (forward-sexp -1) | |
129 ;; If first line is commented, ignore all leading comments: | |
130 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;")) | |
131 (progn | |
132 (setq exp (buffer-substring (point) pt)) | |
133 (while (string-match "\n[ \t]*;+" exp start) | |
134 (setq start (1+ (match-beginning 0)) | |
135 exp (concat (substring exp 0 start) | |
136 (substring exp (match-end 0))))) | |
137 (setq exp (read exp))) | |
138 (setq exp (read (current-buffer))))) | |
139 (set-syntax-table stab) | |
140 (if arg | |
141 (insert (pp-to-string (eval exp))) | |
142 (pp-eval-expression exp)))) | |
143 | |
144 ;;; Test cases for quote | |
145 ;; (pp-eval-expression ''(quote quote)) | |
146 ;; (pp-eval-expression ''((quote a) (quote b))) | |
147 ;; (pp-eval-expression ''('a 'b)) ; same as above | |
148 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote))) | |
149 ;; These do not satisfy the quote test. | |
150 ;; (pp-eval-expression ''quote) | |
151 ;; (pp-eval-expression ''(quote)) | |
152 ;; (pp-eval-expression ''(quote . quote)) | |
153 ;; (pp-eval-expression ''(quote a b)) | |
154 ;; (pp-eval-expression ''(quotefoo)) | |
155 ;; (pp-eval-expression ''(a b)) | |
156 | |
157 (provide 'pp) ; so (require 'pp) works | |
158 | |
159 ;;; pp.el ends here. |