Various docstring and commentary fixes, including
[emacs.git] / lisp / emacs-lisp / pp.el
blobbdc884ab50b85ed44ef711c35ca4b6d2ce95fc7c
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)
12 ;; any later version.
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.
24 ;;; Code:
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."
33 (save-excursion
34 (set-buffer (generate-new-buffer " pp-to-string"))
35 (unwind-protect
36 (progn
37 (lisp-mode-variables nil)
38 (set-syntax-table emacs-lisp-mode-syntax-table)
39 (let ((print-escape-newlines pp-escape-newlines)
40 (print-quoted t))
41 (prin1 object (current-buffer)))
42 (goto-char (point-min))
43 (while (not (eobp))
44 ;; (message "%06d" (- (point-max) (point)))
45 (cond
46 ((condition-case err-var
47 (prog1 t (down-list 1))
48 (error nil))
49 (save-excursion
50 (backward-char 1)
51 (skip-chars-backward "'`#^")
52 (when (and (not (bobp)) (= ?\ (char-before)))
53 (delete-char -1)
54 (insert "\n"))))
55 ((condition-case err-var
56 (prog1 t (up-list 1))
57 (error nil))
58 (while (looking-at "\\s)")
59 (forward-char 1))
60 (delete-region
61 (point)
62 (progn (skip-chars-forward " \t") (point)))
63 (insert ?\n))
64 (t (goto-char (point-max)))))
65 (goto-char (point-min))
66 (indent-sexp)
67 (buffer-string))
68 (kill-buffer (current-buffer)))))
70 ;;;###autoload
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)))
78 ;;;###autoload
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
83 value."
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
91 (function
92 (lambda (buf)
93 (save-excursion
94 (set-buffer buf)
95 (goto-char (point-min))
96 (end-of-line 1)
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))
104 (unwind-protect
105 (progn
106 (select-window window)
107 (run-hooks 'temp-buffer-show-hook))
108 (select-window old-selected)))
109 (message "%s" (buffer-substring (point-min) (point)))
110 ))))))
111 (with-output-to-temp-buffer "*Pp Eval Output*"
112 (pp (car values)))
113 (save-excursion
114 (set-buffer "*Pp Eval Output*")
115 (emacs-lisp-mode)
116 (make-local-variable 'font-lock-verbose)
117 (setq font-lock-verbose nil))))
119 ;;;###autoload
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."
124 (interactive "P")
125 (let ((stab (syntax-table)) (pt (point)) start exp)
126 (set-syntax-table emacs-lisp-mode-syntax-table)
127 (save-excursion
128 (forward-sexp -1)
129 ;; If first line is commented, ignore all leading comments:
130 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
131 (progn
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)
140 (if arg
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
159 ;;; pp.el ends here.