annotate lisp/utils/pp.el @ 0:376386a54a3c r19-14

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