Merge from emacs--rel--22
[bpt/emacs.git] / lisp / emacs-lisp / pp.el
CommitLineData
be010748 1;;; pp.el --- pretty printer for Emacs Lisp
b578f267 2
3731a850 3;; Copyright (C) 1989, 1993, 2001, 2002, 2003, 2004,
f0fa15c5 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
258544f8 5
df072942 6;; Author: Randal Schwartz <merlyn@stonehenge.com>
284b3043 7;; Keywords: lisp
258544f8
RS
8
9;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
258544f8 25
e8af40ee
PJ
26;;; Commentary:
27
258544f8 28;;; Code:
e8af40ee 29
f96b52a3
JB
30(defvar font-lock-verbose)
31
666b9413
SE
32(defgroup pp nil
33 "Pretty printer for Emacs Lisp."
34 :prefix "pp-"
35 :group 'lisp)
36
a1506d29 37(defcustom pp-escape-newlines t
666b9413
SE
38 "*Value of `print-escape-newlines' used by pp-* functions."
39 :type 'boolean
40 :group 'pp)
258544f8 41
0e520006 42;;;###autoload
258544f8 43(defun pp-to-string (object)
e189df50
KH
44 "Return a string containing the pretty-printed representation of OBJECT.
45OBJECT can be any Lisp object. Quoting characters are used as needed
46to make output that `read' can handle, whenever this is possible."
258544f8
RS
47 (save-excursion
48 (set-buffer (generate-new-buffer " pp-to-string"))
49 (unwind-protect
50 (progn
15b605ae
RS
51 (lisp-mode-variables nil)
52 (set-syntax-table emacs-lisp-mode-syntax-table)
3d98a374
AS
53 (let ((print-escape-newlines pp-escape-newlines)
54 (print-quoted t))
258544f8 55 (prin1 object (current-buffer)))
575c3bca 56 (pp-buffer)
258544f8
RS
57 (buffer-string))
58 (kill-buffer (current-buffer)))))
59
c47e847f 60;;;###autoload
575c3bca
JL
61(defun pp-buffer ()
62 "Prettify the current buffer with printed representation of a Lisp object."
63 (goto-char (point-min))
64 (while (not (eobp))
65 ;; (message "%06d" (- (point-max) (point)))
66 (cond
67 ((condition-case err-var
68 (prog1 t (down-list 1))
69 (error nil))
70 (save-excursion
71 (backward-char 1)
72 (skip-chars-backward "'`#^")
1bcb666b 73 (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n)))
575c3bca
JL
74 (delete-region
75 (point)
76 (progn (skip-chars-backward " \t\n") (point)))
77 (insert "\n"))))
78 ((condition-case err-var
79 (prog1 t (up-list 1))
80 (error nil))
81 (while (looking-at "\\s)")
82 (forward-char 1))
83 (delete-region
84 (point)
85 (progn (skip-chars-forward " \t\n") (point)))
86 (insert ?\n))
87 (t (goto-char (point-max)))))
88 (goto-char (point-min))
89 (indent-sexp))
90
ccba0a20 91;;;###autoload
258544f8
RS
92(defun pp (object &optional stream)
93 "Output the pretty-printed representation of OBJECT, any Lisp object.
e189df50 94Quoting characters are printed as needed to make output that `read'
258544f8
RS
95can handle, whenever this is possible.
96Output stream is STREAM, or value of `standard-output' (which see)."
97 (princ (pp-to-string object) (or stream standard-output)))
98
ccba0a20 99;;;###autoload
247bc272
RS
100(defun pp-eval-expression (expression)
101 "Evaluate EXPRESSION and pretty-print its value.
102Also add the value to the front of the list in the variable `values'."
103 (interactive
104 (list (read-from-minibuffer "Eval: " nil read-expression-map t
105 'read-expression-history)))
106 (setq values (cons (eval expression) values))
bf464fad
RS
107 (let* ((old-show-function temp-buffer-show-function)
108 ;; Use this function to display the buffer.
109 ;; This function either decides not to display it at all
110 ;; or displays it in the usual way.
111 (temp-buffer-show-function
258544f8
RS
112 (function
113 (lambda (buf)
114 (save-excursion
115 (set-buffer buf)
116 (goto-char (point-min))
117 (end-of-line 1)
118 (if (or (< (1+ (point)) (point-max))
06115f9d 119 (>= (- (point) (point-min)) (frame-width)))
bf464fad
RS
120 (let ((temp-buffer-show-function old-show-function)
121 (old-selected (selected-window))
122 (window (display-buffer buf)))
258544f8 123 (goto-char (point-min)) ; expected by some hooks ...
bf464fad
RS
124 (make-frame-visible (window-frame window))
125 (unwind-protect
126 (progn
127 (select-window window)
128 (run-hooks 'temp-buffer-show-hook))
129 (select-window old-selected)))
258544f8 130 (message "%s" (buffer-substring (point-min) (point)))
bf464fad 131 ))))))
258544f8 132 (with-output-to-temp-buffer "*Pp Eval Output*"
eaf33a17
SM
133 (pp (car values))
134 (with-current-buffer standard-output
135 (emacs-lisp-mode)
136 (set (make-local-variable 'font-lock-verbose) nil)))))
258544f8 137
ccba0a20 138;;;###autoload
258544f8
RS
139(defun pp-eval-last-sexp (arg)
140 "Run `pp-eval-expression' on sexp before point (which see).
141With argument, pretty-print output into current buffer.
142Ignores leading comment characters."
143 (interactive "P")
144 (let ((stab (syntax-table)) (pt (point)) start exp)
145 (set-syntax-table emacs-lisp-mode-syntax-table)
146 (save-excursion
147 (forward-sexp -1)
148 ;; If first line is commented, ignore all leading comments:
149 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
150 (progn
151 (setq exp (buffer-substring (point) pt))
152 (while (string-match "\n[ \t]*;+" exp start)
153 (setq start (1+ (match-beginning 0))
154 exp (concat (substring exp 0 start)
155 (substring exp (match-end 0)))))
156 (setq exp (read exp)))
157 (setq exp (read (current-buffer)))))
158 (set-syntax-table stab)
159 (if arg
160 (insert (pp-to-string (eval exp)))
f61eed1d 161 (pp-eval-expression exp))))
258544f8
RS
162
163;;; Test cases for quote
164;; (pp-eval-expression ''(quote quote))
165;; (pp-eval-expression ''((quote a) (quote b)))
166;; (pp-eval-expression ''('a 'b)) ; same as above
167;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
168;; These do not satisfy the quote test.
169;; (pp-eval-expression ''quote)
170;; (pp-eval-expression ''(quote))
171;; (pp-eval-expression ''(quote . quote))
172;; (pp-eval-expression ''(quote a b))
173;; (pp-eval-expression ''(quotefoo))
174;; (pp-eval-expression ''(a b))
175
176(provide 'pp) ; so (require 'pp) works
177
ab5796a9 178;;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9
e8af40ee 179;;; pp.el ends here