annotate lisp/utils/pp.el @ 126:1370575f1259 xemacs-20-1p1

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