1 ;;; pp.el --- pretty printer for Emacs Lisp
3 ;; Copyright (C) 1989, 1993 Free Software Foundation, Inc.
5 ;; Author: Randal Schwartz <merlyn@stonehenge.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 "Pretty printer for Emacs Lisp."
30 (defcustom pp-escape-newlines t
31 "*Value of `print-escape-newlines' used by pp-* functions."
35 (defun pp-to-string (object)
36 "Return a string containing the pretty-printed representation of OBJECT.
37 OBJECT can be any Lisp object. Quoting characters are used as needed
38 to make output that `read' can handle, whenever this is possible."
40 (set-buffer (generate-new-buffer " pp-to-string"))
43 (lisp-mode-variables nil
)
44 (set-syntax-table emacs-lisp-mode-syntax-table
)
45 (let ((print-escape-newlines pp-escape-newlines
)
47 (prin1 object
(current-buffer)))
48 (goto-char (point-min))
50 ;; (message "%06d" (- (point-max) (point)))
52 ((condition-case err-var
53 (prog1 t
(down-list 1))
57 (skip-chars-backward "'`#^")
58 (when (and (not (bobp)) (= ?\
(char-before)))
61 ((condition-case err-var
64 (while (looking-at "\\s)")
68 (progn (skip-chars-forward " \t") (point)))
70 (t (goto-char (point-max)))))
71 (goto-char (point-min))
74 (kill-buffer (current-buffer)))))
77 (defun pp (object &optional stream
)
78 "Output the pretty-printed representation of OBJECT, any Lisp object.
79 Quoting characters are printed as needed to make output that `read'
80 can handle, whenever this is possible.
81 Output stream is STREAM, or value of `standard-output' (which see)."
82 (princ (pp-to-string object
) (or stream standard-output
)))
85 (defun pp-eval-expression (expression)
86 "Evaluate EXPRESSION and pretty-print value into a new display buffer.
87 If the pretty-printed value fits on one line, the message line is used
88 instead. The value is also consed onto the front of the list
89 in the variable `values'."
90 (interactive "xPp-eval: ")
91 (setq values
(cons (eval expression
) values
))
92 (let* ((old-show-function temp-buffer-show-function
)
93 ;; Use this function to display the buffer.
94 ;; This function either decides not to display it at all
95 ;; or displays it in the usual way.
96 (temp-buffer-show-function
101 (goto-char (point-min))
103 (if (or (< (1+ (point)) (point-max))
104 (>= (- (point) (point-min)) (frame-width)))
105 (let ((temp-buffer-show-function old-show-function
)
106 (old-selected (selected-window))
107 (window (display-buffer buf
)))
108 (goto-char (point-min)) ; expected by some hooks ...
109 (make-frame-visible (window-frame window
))
112 (select-window window
)
113 (run-hooks 'temp-buffer-show-hook
))
114 (select-window old-selected
)))
115 (message "%s" (buffer-substring (point-min) (point)))
117 (with-output-to-temp-buffer "*Pp Eval Output*"
120 (set-buffer "*Pp Eval Output*")
122 (make-local-variable 'font-lock-verbose
)
123 (setq font-lock-verbose nil
))))
126 (defun pp-eval-last-sexp (arg)
127 "Run `pp-eval-expression' on sexp before point (which see).
128 With argument, pretty-print output into current buffer.
129 Ignores leading comment characters."
131 (let ((stab (syntax-table)) (pt (point)) start exp
)
132 (set-syntax-table emacs-lisp-mode-syntax-table
)
135 ;; If first line is commented, ignore all leading comments:
136 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
138 (setq exp
(buffer-substring (point) pt
))
139 (while (string-match "\n[ \t]*;+" exp start
)
140 (setq start
(1+ (match-beginning 0))
141 exp
(concat (substring exp
0 start
)
142 (substring exp
(match-end 0)))))
143 (setq exp
(read exp
)))
144 (setq exp
(read (current-buffer)))))
145 (set-syntax-table stab
)
147 (insert (pp-to-string (eval exp
)))
148 (pp-eval-expression exp
))))
150 ;;; Test cases for quote
151 ;; (pp-eval-expression ''(quote quote))
152 ;; (pp-eval-expression ''((quote a) (quote b)))
153 ;; (pp-eval-expression ''('a 'b)) ; same as above
154 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
155 ;; These do not satisfy the quote test.
156 ;; (pp-eval-expression ''quote)
157 ;; (pp-eval-expression ''(quote))
158 ;; (pp-eval-expression ''(quote . quote))
159 ;; (pp-eval-expression ''(quote a b))
160 ;; (pp-eval-expression ''(quotefoo))
161 ;; (pp-eval-expression ''(a b))
163 (provide 'pp
) ; so (require 'pp) works