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