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 (defvar pp-escape-newlines t
27 "*Value of print-escape-newlines used by pp-* functions.")
29 (defun pp-to-string (object)
30 "Return a string containing the pretty-printed representation of OBJECT,
31 any Lisp object. Quoting characters are used when needed to make output
32 that `read' can handle, whenever this is possible."
34 (set-buffer (generate-new-buffer " pp-to-string"))
37 (lisp-mode-variables nil
)
38 (set-syntax-table emacs-lisp-mode-syntax-table
)
39 (let ((print-escape-newlines pp-escape-newlines
)
41 (prin1 object
(current-buffer)))
42 (goto-char (point-min))
44 ;; (message "%06d" (- (point-max) (point)))
46 ((condition-case err-var
47 (prog1 t
(down-list 1))
51 (skip-chars-backward "'`#^")
52 (when (and (not (bobp)) (= ?\
(char-before)))
55 ((condition-case err-var
58 (while (looking-at "\\s)")
62 (progn (skip-chars-forward " \t") (point)))
64 (t (goto-char (point-max)))))
65 (goto-char (point-min))
68 (kill-buffer (current-buffer)))))
71 (defun pp (object &optional stream
)
72 "Output the pretty-printed representation of OBJECT, any Lisp object.
73 Quoting characters are printed when needed to make output that `read'
74 can handle, whenever this is possible.
75 Output stream is STREAM, or value of `standard-output' (which see)."
76 (princ (pp-to-string object
) (or stream standard-output
)))
79 (defun pp-eval-expression (expression)
80 "Evaluate EXPRESSION and pretty-print value into a new display buffer.
81 If the pretty-printed value fits on one line, the message line is used
82 instead. Value is also consed on to front of variable values 's
84 (interactive "xPp-eval: ")
85 (setq values
(cons (eval expression
) values
))
86 (let* ((old-show-function temp-buffer-show-function
)
87 ;; Use this function to display the buffer.
88 ;; This function either decides not to display it at all
89 ;; or displays it in the usual way.
90 (temp-buffer-show-function
95 (goto-char (point-min))
97 (if (or (< (1+ (point)) (point-max))
98 (>= (- (point) (point-min)) (frame-width)))
99 (let ((temp-buffer-show-function old-show-function
)
100 (old-selected (selected-window))
101 (window (display-buffer buf
)))
102 (goto-char (point-min)) ; expected by some hooks ...
103 (make-frame-visible (window-frame window
))
106 (select-window window
)
107 (run-hooks 'temp-buffer-show-hook
))
108 (select-window old-selected
)))
109 (message "%s" (buffer-substring (point-min) (point)))
111 (with-output-to-temp-buffer "*Pp Eval Output*"
114 (set-buffer "*Pp Eval Output*")
116 (make-local-variable 'font-lock-verbose
)
117 (setq font-lock-verbose nil
))))
120 (defun pp-eval-last-sexp (arg)
121 "Run `pp-eval-expression' on sexp before point (which see).
122 With argument, pretty-print output into current buffer.
123 Ignores leading comment characters."
125 (let ((stab (syntax-table)) (pt (point)) start exp
)
126 (set-syntax-table emacs-lisp-mode-syntax-table
)
129 ;; If first line is commented, ignore all leading comments:
130 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
132 (setq exp
(buffer-substring (point) pt
))
133 (while (string-match "\n[ \t]*;+" exp start
)
134 (setq start
(1+ (match-beginning 0))
135 exp
(concat (substring exp
0 start
)
136 (substring exp
(match-end 0)))))
137 (setq exp
(read exp
)))
138 (setq exp
(read (current-buffer)))))
139 (set-syntax-table stab
)
141 (insert (pp-to-string (eval exp
)))
142 (pp-eval-expression exp
))))
144 ;;; Test cases for quote
145 ;; (pp-eval-expression ''(quote quote))
146 ;; (pp-eval-expression ''((quote a) (quote b)))
147 ;; (pp-eval-expression ''('a 'b)) ; same as above
148 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
149 ;; These do not satisfy the quote test.
150 ;; (pp-eval-expression ''quote)
151 ;; (pp-eval-expression ''(quote))
152 ;; (pp-eval-expression ''(quote . quote))
153 ;; (pp-eval-expression ''(quote a b))
154 ;; (pp-eval-expression ''(quotefoo))
155 ;; (pp-eval-expression ''(a b))
157 (provide 'pp
) ; so (require 'pp) works