]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/pp.el
(pp-eval-expression): Use `X' to read value.
[gnu-emacs] / lisp / emacs-lisp / pp.el
1 ;;; pp.el --- pretty printer for Emacs Lisp
2
3 ;; Copyright (C) 1989, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Randal Schwartz <merlyn@stonehenge.com>
7 ;; Keywords: lisp
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
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (defvar font-lock-verbose)
31
32 (defgroup pp nil
33 "Pretty printer for Emacs Lisp."
34 :prefix "pp-"
35 :group 'lisp)
36
37 (defcustom pp-escape-newlines t
38 "*Value of `print-escape-newlines' used by pp-* functions."
39 :type 'boolean
40 :group 'pp)
41
42 ;;;###autoload
43 (defun pp-to-string (object)
44 "Return a string containing the pretty-printed representation of OBJECT.
45 OBJECT can be any Lisp object. Quoting characters are used as needed
46 to make output that `read' can handle, whenever this is possible."
47 (save-excursion
48 (set-buffer (generate-new-buffer " pp-to-string"))
49 (unwind-protect
50 (progn
51 (lisp-mode-variables nil)
52 (set-syntax-table emacs-lisp-mode-syntax-table)
53 (let ((print-escape-newlines pp-escape-newlines)
54 (print-quoted t))
55 (prin1 object (current-buffer)))
56 (pp-buffer)
57 (buffer-string))
58 (kill-buffer (current-buffer)))))
59
60 ;;;###autoload
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 "'`#^")
73 (when (and (not (bobp)) (memq (char-before) '(?\s ?\t ?\n)))
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
91 ;;;###autoload
92 (defun pp (object &optional stream)
93 "Output the pretty-printed representation of OBJECT, any Lisp object.
94 Quoting characters are printed as needed to make output that `read'
95 can handle, whenever this is possible.
96 Output stream is STREAM, or value of `standard-output' (which see)."
97 (princ (pp-to-string object) (or stream standard-output)))
98
99 ;;;###autoload
100 (defun pp-eval-expression (expval)
101 "Evaluate an expression, then pretty-print value EXPVAL into a new buffer.
102 If pretty-printed EXPVAL fits on one line, display it in the echo
103 area instead. Also add EXPVAL to the front of the list
104 in the variable `values'.
105
106 Non-interactively, the argument is the value, EXPVAL, not the expression
107 to evaluate."
108 (interactive "XPp-eval: ")
109 (setq values (cons expval values))
110 (let* ((old-show-function temp-buffer-show-function)
111 ;; Use this function to display the buffer.
112 ;; This function either decides not to display it at all
113 ;; or displays it in the usual way.
114 (temp-buffer-show-function
115 (function
116 (lambda (buf)
117 (save-excursion
118 (set-buffer buf)
119 (goto-char (point-min))
120 (end-of-line 1)
121 (if (or (< (1+ (point)) (point-max))
122 (>= (- (point) (point-min)) (frame-width)))
123 (let ((temp-buffer-show-function old-show-function)
124 (old-selected (selected-window))
125 (window (display-buffer buf)))
126 (goto-char (point-min)) ; expected by some hooks ...
127 (make-frame-visible (window-frame window))
128 (unwind-protect
129 (progn
130 (select-window window)
131 (run-hooks 'temp-buffer-show-hook))
132 (select-window old-selected)))
133 (message "%s" (buffer-substring (point-min) (point)))
134 ))))))
135 (with-output-to-temp-buffer "*Pp Eval Output*"
136 (pp (car values))
137 (with-current-buffer standard-output
138 (emacs-lisp-mode)
139 (set (make-local-variable 'font-lock-verbose) nil)))))
140
141 ;;;###autoload
142 (defun pp-eval-last-sexp (arg)
143 "Run `pp-eval-expression' on sexp before point (which see).
144 With argument, pretty-print output into current buffer.
145 Ignores leading comment characters."
146 (interactive "P")
147 (let ((stab (syntax-table)) (pt (point)) start exp)
148 (set-syntax-table emacs-lisp-mode-syntax-table)
149 (save-excursion
150 (forward-sexp -1)
151 ;; If first line is commented, ignore all leading comments:
152 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
153 (progn
154 (setq exp (buffer-substring (point) pt))
155 (while (string-match "\n[ \t]*;+" exp start)
156 (setq start (1+ (match-beginning 0))
157 exp (concat (substring exp 0 start)
158 (substring exp (match-end 0)))))
159 (setq exp (read exp)))
160 (setq exp (read (current-buffer)))))
161 (set-syntax-table stab)
162 (if arg
163 (insert (pp-to-string (eval exp)))
164 (pp-eval-expression exp))))
165
166 ;;; Test cases for quote
167 ;; (pp-eval-expression ''(quote quote))
168 ;; (pp-eval-expression ''((quote a) (quote b)))
169 ;; (pp-eval-expression ''('a 'b)) ; same as above
170 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
171 ;; These do not satisfy the quote test.
172 ;; (pp-eval-expression ''quote)
173 ;; (pp-eval-expression ''(quote))
174 ;; (pp-eval-expression ''(quote . quote))
175 ;; (pp-eval-expression ''(quote a b))
176 ;; (pp-eval-expression ''(quotefoo))
177 ;; (pp-eval-expression ''(a b))
178
179 (provide 'pp) ; so (require 'pp) works
180
181 ;;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9
182 ;;; pp.el ends here