Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / emacs-lisp / pp.el
CommitLineData
be010748 1;;; pp.el --- pretty printer for Emacs Lisp
b578f267 2
73b0cd50 3;; Copyright (C) 1989, 1993, 2001-2011 Free Software Foundation, Inc.
258544f8 4
df072942 5;; Author: Randal Schwartz <merlyn@stonehenge.com>
284b3043 6;; Keywords: lisp
258544f8
RS
7
8;; This file is part of GNU Emacs.
9
d6cba7ae 10;; GNU Emacs is free software: you can redistribute it and/or modify
258544f8 11;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
258544f8
RS
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
d6cba7ae 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
258544f8 22
e8af40ee
PJ
23;;; Commentary:
24
258544f8 25;;; Code:
e8af40ee 26
f96b52a3
JB
27(defvar font-lock-verbose)
28
666b9413
SE
29(defgroup pp nil
30 "Pretty printer for Emacs Lisp."
31 :prefix "pp-"
32 :group 'lisp)
33
a1506d29 34(defcustom pp-escape-newlines t
cb711556 35 "Value of `print-escape-newlines' used by pp-* functions."
666b9413
SE
36 :type 'boolean
37 :group 'pp)
258544f8 38
0e520006 39;;;###autoload
258544f8 40(defun pp-to-string (object)
e189df50
KH
41 "Return a string containing the pretty-printed representation of OBJECT.
42OBJECT can be any Lisp object. Quoting characters are used as needed
43to make output that `read' can handle, whenever this is possible."
9a529312 44 (with-current-buffer (generate-new-buffer " pp-to-string")
258544f8
RS
45 (unwind-protect
46 (progn
15b605ae
RS
47 (lisp-mode-variables nil)
48 (set-syntax-table emacs-lisp-mode-syntax-table)
3d98a374
AS
49 (let ((print-escape-newlines pp-escape-newlines)
50 (print-quoted t))
258544f8 51 (prin1 object (current-buffer)))
575c3bca 52 (pp-buffer)
258544f8
RS
53 (buffer-string))
54 (kill-buffer (current-buffer)))))
55
c47e847f 56;;;###autoload
575c3bca
JL
57(defun pp-buffer ()
58 "Prettify the current buffer with printed representation of a Lisp object."
59 (goto-char (point-min))
60 (while (not (eobp))
61 ;; (message "%06d" (- (point-max) (point)))
62 (cond
63 ((condition-case err-var
64 (prog1 t (down-list 1))
65 (error nil))
66 (save-excursion
67 (backward-char 1)
68 (skip-chars-backward "'`#^")
1bcb666b 69 (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n)))
575c3bca
JL
70 (delete-region
71 (point)
72 (progn (skip-chars-backward " \t\n") (point)))
73 (insert "\n"))))
74 ((condition-case err-var
75 (prog1 t (up-list 1))
76 (error nil))
77 (while (looking-at "\\s)")
78 (forward-char 1))
79 (delete-region
80 (point)
81 (progn (skip-chars-forward " \t\n") (point)))
82 (insert ?\n))
83 (t (goto-char (point-max)))))
84 (goto-char (point-min))
85 (indent-sexp))
86
ccba0a20 87;;;###autoload
258544f8
RS
88(defun pp (object &optional stream)
89 "Output the pretty-printed representation of OBJECT, any Lisp object.
e189df50 90Quoting characters are printed as needed to make output that `read'
258544f8
RS
91can handle, whenever this is possible.
92Output stream is STREAM, or value of `standard-output' (which see)."
93 (princ (pp-to-string object) (or stream standard-output)))
94
497a66ae
GM
95(defun pp-display-expression (expression out-buffer-name)
96 "Prettify and display EXPRESSION in an appropriate way, depending on length.
97If a temporary buffer is needed for representation, it will be named
98after OUT-BUFFER-NAME."
bf464fad
RS
99 (let* ((old-show-function temp-buffer-show-function)
100 ;; Use this function to display the buffer.
101 ;; This function either decides not to display it at all
102 ;; or displays it in the usual way.
103 (temp-buffer-show-function
258544f8
RS
104 (function
105 (lambda (buf)
9a529312 106 (with-current-buffer buf
258544f8
RS
107 (goto-char (point-min))
108 (end-of-line 1)
109 (if (or (< (1+ (point)) (point-max))
06115f9d 110 (>= (- (point) (point-min)) (frame-width)))
bf464fad
RS
111 (let ((temp-buffer-show-function old-show-function)
112 (old-selected (selected-window))
113 (window (display-buffer buf)))
258544f8 114 (goto-char (point-min)) ; expected by some hooks ...
bf464fad
RS
115 (make-frame-visible (window-frame window))
116 (unwind-protect
117 (progn
118 (select-window window)
119 (run-hooks 'temp-buffer-show-hook))
52f55ab0 120 (select-window old-selected)
497a66ae 121 (message "See buffer %s." out-buffer-name)))
258544f8 122 (message "%s" (buffer-substring (point-min) (point)))
bf464fad 123 ))))))
497a66ae
GM
124 (with-output-to-temp-buffer out-buffer-name
125 (pp expression)
eaf33a17
SM
126 (with-current-buffer standard-output
127 (emacs-lisp-mode)
52f55ab0 128 (setq buffer-read-only nil)
eaf33a17 129 (set (make-local-variable 'font-lock-verbose) nil)))))
258544f8 130
ccba0a20 131;;;###autoload
497a66ae
GM
132(defun pp-eval-expression (expression)
133 "Evaluate EXPRESSION and pretty-print its value.
134Also add the value to the front of the list in the variable `values'."
135 (interactive
136 (list (read-from-minibuffer "Eval: " nil read-expression-map t
137 'read-expression-history)))
138 (message "Evaluating...")
139 (setq values (cons (eval expression) values))
140 (pp-display-expression (car values) "*Pp Eval Output*"))
141
142;;;###autoload
143(defun pp-macroexpand-expression (expression)
144 "Macroexpand EXPRESSION and pretty-print its value."
145 (interactive
146 (list (read-from-minibuffer "Macroexpand: " nil read-expression-map t
147 'read-expression-history)))
148 (pp-display-expression (macroexpand expression) "*Pp Macroexpand Output*"))
149
150(defun pp-last-sexp ()
151 "Read sexp before point. Ignores leading comment characters."
258544f8
RS
152 (let ((stab (syntax-table)) (pt (point)) start exp)
153 (set-syntax-table emacs-lisp-mode-syntax-table)
154 (save-excursion
155 (forward-sexp -1)
156 ;; If first line is commented, ignore all leading comments:
157 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
158 (progn
159 (setq exp (buffer-substring (point) pt))
160 (while (string-match "\n[ \t]*;+" exp start)
161 (setq start (1+ (match-beginning 0))
162 exp (concat (substring exp 0 start)
163 (substring exp (match-end 0)))))
164 (setq exp (read exp)))
165 (setq exp (read (current-buffer)))))
166 (set-syntax-table stab)
497a66ae
GM
167 exp))
168
169;;;###autoload
170(defun pp-eval-last-sexp (arg)
171 "Run `pp-eval-expression' on sexp before point.
172With argument, pretty-print output into current buffer.
173Ignores leading comment characters."
174 (interactive "P")
175 (if arg
176 (insert (pp-to-string (eval (pp-last-sexp))))
177 (pp-eval-expression (pp-last-sexp))))
178
179;;;###autoload
180(defun pp-macroexpand-last-sexp (arg)
181 "Run `pp-macroexpand-expression' on sexp before point.
182With argument, pretty-print output into current buffer.
183Ignores leading comment characters."
184 (interactive "P")
185 (if arg
186 (insert (pp-to-string (macroexpand (pp-last-sexp))))
187 (pp-macroexpand-expression (pp-last-sexp))))
258544f8
RS
188
189;;; Test cases for quote
190;; (pp-eval-expression ''(quote quote))
191;; (pp-eval-expression ''((quote a) (quote b)))
192;; (pp-eval-expression ''('a 'b)) ; same as above
193;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
194;; These do not satisfy the quote test.
195;; (pp-eval-expression ''quote)
196;; (pp-eval-expression ''(quote))
197;; (pp-eval-expression ''(quote . quote))
198;; (pp-eval-expression ''(quote a b))
199;; (pp-eval-expression ''(quotefoo))
200;; (pp-eval-expression ''(a b))
201
202(provide 'pp) ; so (require 'pp) works
203
e8af40ee 204;;; pp.el ends here