Regenerate configure.
[emacs.git] / lisp / emacs-lisp / pp.el
blobbf53c9e6153f060086ede8e23dba9e589e9ff11b
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 (save-excursion
46 (set-buffer (generate-new-buffer " pp-to-string"))
47 (unwind-protect
48 (progn
49 (lisp-mode-variables nil)
50 (set-syntax-table emacs-lisp-mode-syntax-table)
51 (let ((print-escape-newlines pp-escape-newlines)
52 (print-quoted t))
53 (prin1 object (current-buffer)))
54 (pp-buffer)
55 (buffer-string))
56 (kill-buffer (current-buffer)))))
58 ;;;###autoload
59 (defun pp-buffer ()
60 "Prettify the current buffer with printed representation of a Lisp object."
61 (goto-char (point-min))
62 (while (not (eobp))
63 ;; (message "%06d" (- (point-max) (point)))
64 (cond
65 ((condition-case err-var
66 (prog1 t (down-list 1))
67 (error nil))
68 (save-excursion
69 (backward-char 1)
70 (skip-chars-backward "'`#^")
71 (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n)))
72 (delete-region
73 (point)
74 (progn (skip-chars-backward " \t\n") (point)))
75 (insert "\n"))))
76 ((condition-case err-var
77 (prog1 t (up-list 1))
78 (error nil))
79 (while (looking-at "\\s)")
80 (forward-char 1))
81 (delete-region
82 (point)
83 (progn (skip-chars-forward " \t\n") (point)))
84 (insert ?\n))
85 (t (goto-char (point-max)))))
86 (goto-char (point-min))
87 (indent-sexp))
89 ;;;###autoload
90 (defun pp (object &optional stream)
91 "Output the pretty-printed representation of OBJECT, any Lisp object.
92 Quoting characters are printed as needed to make output that `read'
93 can handle, whenever this is possible.
94 Output stream is STREAM, or value of `standard-output' (which see)."
95 (princ (pp-to-string object) (or stream standard-output)))
97 (defun pp-display-expression (expression out-buffer-name)
98 "Prettify and display EXPRESSION in an appropriate way, depending on length.
99 If a temporary buffer is needed for representation, it will be named
100 after OUT-BUFFER-NAME."
101 (let* ((old-show-function temp-buffer-show-function)
102 ;; Use this function to display the buffer.
103 ;; This function either decides not to display it at all
104 ;; or displays it in the usual way.
105 (temp-buffer-show-function
106 (function
107 (lambda (buf)
108 (save-excursion
109 (set-buffer buf)
110 (goto-char (point-min))
111 (end-of-line 1)
112 (if (or (< (1+ (point)) (point-max))
113 (>= (- (point) (point-min)) (frame-width)))
114 (let ((temp-buffer-show-function old-show-function)
115 (old-selected (selected-window))
116 (window (display-buffer buf)))
117 (goto-char (point-min)) ; expected by some hooks ...
118 (make-frame-visible (window-frame window))
119 (unwind-protect
120 (progn
121 (select-window window)
122 (run-hooks 'temp-buffer-show-hook))
123 (select-window old-selected)
124 (message "See buffer %s." out-buffer-name)))
125 (message "%s" (buffer-substring (point-min) (point)))
126 ))))))
127 (with-output-to-temp-buffer out-buffer-name
128 (pp expression)
129 (with-current-buffer standard-output
130 (emacs-lisp-mode)
131 (setq buffer-read-only nil)
132 (set (make-local-variable 'font-lock-verbose) nil)))))
134 ;;;###autoload
135 (defun pp-eval-expression (expression)
136 "Evaluate EXPRESSION and pretty-print its value.
137 Also add the value to the front of the list in the variable `values'."
138 (interactive
139 (list (read-from-minibuffer "Eval: " nil read-expression-map t
140 'read-expression-history)))
141 (message "Evaluating...")
142 (setq values (cons (eval expression) values))
143 (pp-display-expression (car values) "*Pp Eval Output*"))
145 ;;;###autoload
146 (defun pp-macroexpand-expression (expression)
147 "Macroexpand EXPRESSION and pretty-print its value."
148 (interactive
149 (list (read-from-minibuffer "Macroexpand: " nil read-expression-map t
150 'read-expression-history)))
151 (pp-display-expression (macroexpand expression) "*Pp Macroexpand Output*"))
153 (defun pp-last-sexp ()
154 "Read sexp before point. Ignores leading comment characters."
155 (let ((stab (syntax-table)) (pt (point)) start exp)
156 (set-syntax-table emacs-lisp-mode-syntax-table)
157 (save-excursion
158 (forward-sexp -1)
159 ;; If first line is commented, ignore all leading comments:
160 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
161 (progn
162 (setq exp (buffer-substring (point) pt))
163 (while (string-match "\n[ \t]*;+" exp start)
164 (setq start (1+ (match-beginning 0))
165 exp (concat (substring exp 0 start)
166 (substring exp (match-end 0)))))
167 (setq exp (read exp)))
168 (setq exp (read (current-buffer)))))
169 (set-syntax-table stab)
170 exp))
172 ;;;###autoload
173 (defun pp-eval-last-sexp (arg)
174 "Run `pp-eval-expression' on sexp before point.
175 With argument, pretty-print output into current buffer.
176 Ignores leading comment characters."
177 (interactive "P")
178 (if arg
179 (insert (pp-to-string (eval (pp-last-sexp))))
180 (pp-eval-expression (pp-last-sexp))))
182 ;;;###autoload
183 (defun pp-macroexpand-last-sexp (arg)
184 "Run `pp-macroexpand-expression' on sexp before point.
185 With argument, pretty-print output into current buffer.
186 Ignores leading comment characters."
187 (interactive "P")
188 (if arg
189 (insert (pp-to-string (macroexpand (pp-last-sexp))))
190 (pp-macroexpand-expression (pp-last-sexp))))
192 ;;; Test cases for quote
193 ;; (pp-eval-expression ''(quote quote))
194 ;; (pp-eval-expression ''((quote a) (quote b)))
195 ;; (pp-eval-expression ''('a 'b)) ; same as above
196 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
197 ;; These do not satisfy the quote test.
198 ;; (pp-eval-expression ''quote)
199 ;; (pp-eval-expression ''(quote))
200 ;; (pp-eval-expression ''(quote . quote))
201 ;; (pp-eval-expression ''(quote a b))
202 ;; (pp-eval-expression ''(quotefoo))
203 ;; (pp-eval-expression ''(a b))
205 (provide 'pp) ; so (require 'pp) works
207 ;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9
208 ;;; pp.el ends here