(tramp-handle-verify-visited-file-modtime): `visited-file-modtime' now
[emacs.git] / lisp / emacs-lisp / pp.el
blob93e30fb0f556415629f22a3289ec370abef79deb
1 ;;; pp.el --- pretty printer for Emacs Lisp
3 ;; Copyright (C) 1989, 1993, 2001, 2004 Free Software Foundation, Inc.
5 ;; Author: Randal Schwartz <merlyn@stonehenge.com>
6 ;; Keywords: lisp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;;; Code:
29 (defgroup pp nil
30 "Pretty printer for Emacs Lisp."
31 :prefix "pp-"
32 :group 'lisp)
34 (defcustom pp-escape-newlines t
35 "*Value of `print-escape-newlines' used by pp-* functions."
36 :type 'boolean
37 :group 'pp)
39 ;;;###autoload
40 (defun pp-to-string (object)
41 "Return a string containing the pretty-printed representation of OBJECT.
42 OBJECT can be any Lisp object. Quoting characters are used as needed
43 to make output that `read' can handle, whenever this is possible."
44 (save-excursion
45 (set-buffer (generate-new-buffer " pp-to-string"))
46 (unwind-protect
47 (progn
48 (lisp-mode-variables nil)
49 (set-syntax-table emacs-lisp-mode-syntax-table)
50 (let ((print-escape-newlines pp-escape-newlines)
51 (print-quoted t))
52 (prin1 object (current-buffer)))
53 (pp-buffer)
54 (buffer-string))
55 (kill-buffer (current-buffer)))))
57 ;;;###autoload
58 (defun pp-buffer ()
59 "Prettify the current buffer with printed representation of a Lisp object."
60 (goto-char (point-min))
61 (while (not (eobp))
62 ;; (message "%06d" (- (point-max) (point)))
63 (cond
64 ((condition-case err-var
65 (prog1 t (down-list 1))
66 (error nil))
67 (save-excursion
68 (backward-char 1)
69 (skip-chars-backward "'`#^")
70 (when (and (not (bobp)) (memq (char-before) '(?\ ?\t ?\n)))
71 (delete-region
72 (point)
73 (progn (skip-chars-backward " \t\n") (point)))
74 (insert "\n"))))
75 ((condition-case err-var
76 (prog1 t (up-list 1))
77 (error nil))
78 (while (looking-at "\\s)")
79 (forward-char 1))
80 (delete-region
81 (point)
82 (progn (skip-chars-forward " \t\n") (point)))
83 (insert ?\n))
84 (t (goto-char (point-max)))))
85 (goto-char (point-min))
86 (indent-sexp))
88 ;;;###autoload
89 (defun pp (object &optional stream)
90 "Output the pretty-printed representation of OBJECT, any Lisp object.
91 Quoting characters are printed as needed to make output that `read'
92 can handle, whenever this is possible.
93 Output stream is STREAM, or value of `standard-output' (which see)."
94 (princ (pp-to-string object) (or stream standard-output)))
96 ;;;###autoload
97 (defun pp-eval-expression (expression)
98 "Evaluate EXPRESSION and pretty-print value into a new display buffer.
99 If the pretty-printed value fits on one line, the message line is used
100 instead. The value is also consed onto the front of the list
101 in the variable `values'."
102 (interactive "xPp-eval: ")
103 (setq values (cons (eval expression) values))
104 (let* ((old-show-function temp-buffer-show-function)
105 ;; Use this function to display the buffer.
106 ;; This function either decides not to display it at all
107 ;; or displays it in the usual way.
108 (temp-buffer-show-function
109 (function
110 (lambda (buf)
111 (save-excursion
112 (set-buffer buf)
113 (goto-char (point-min))
114 (end-of-line 1)
115 (if (or (< (1+ (point)) (point-max))
116 (>= (- (point) (point-min)) (frame-width)))
117 (let ((temp-buffer-show-function old-show-function)
118 (old-selected (selected-window))
119 (window (display-buffer buf)))
120 (goto-char (point-min)) ; expected by some hooks ...
121 (make-frame-visible (window-frame window))
122 (unwind-protect
123 (progn
124 (select-window window)
125 (run-hooks 'temp-buffer-show-hook))
126 (select-window old-selected)))
127 (message "%s" (buffer-substring (point-min) (point)))
128 ))))))
129 (with-output-to-temp-buffer "*Pp Eval Output*"
130 (pp (car values))
131 (with-current-buffer standard-output
132 (emacs-lisp-mode)
133 (set (make-local-variable 'font-lock-verbose) nil)))))
135 ;;;###autoload
136 (defun pp-eval-last-sexp (arg)
137 "Run `pp-eval-expression' on sexp before point (which see).
138 With argument, pretty-print output into current buffer.
139 Ignores leading comment characters."
140 (interactive "P")
141 (let ((stab (syntax-table)) (pt (point)) start exp)
142 (set-syntax-table emacs-lisp-mode-syntax-table)
143 (save-excursion
144 (forward-sexp -1)
145 ;; If first line is commented, ignore all leading comments:
146 (if (save-excursion (beginning-of-line) (looking-at "[ \t]*;"))
147 (progn
148 (setq exp (buffer-substring (point) pt))
149 (while (string-match "\n[ \t]*;+" exp start)
150 (setq start (1+ (match-beginning 0))
151 exp (concat (substring exp 0 start)
152 (substring exp (match-end 0)))))
153 (setq exp (read exp)))
154 (setq exp (read (current-buffer)))))
155 (set-syntax-table stab)
156 (if arg
157 (insert (pp-to-string (eval exp)))
158 (pp-eval-expression exp))))
160 ;;; Test cases for quote
161 ;; (pp-eval-expression ''(quote quote))
162 ;; (pp-eval-expression ''((quote a) (quote b)))
163 ;; (pp-eval-expression ''('a 'b)) ; same as above
164 ;; (pp-eval-expression ''((quote (quote quote)) (quote quote)))
165 ;; These do not satisfy the quote test.
166 ;; (pp-eval-expression ''quote)
167 ;; (pp-eval-expression ''(quote))
168 ;; (pp-eval-expression ''(quote . quote))
169 ;; (pp-eval-expression ''(quote a b))
170 ;; (pp-eval-expression ''(quotefoo))
171 ;; (pp-eval-expression ''(a b))
173 (provide 'pp) ; so (require 'pp) works
175 ;;; arch-tag: b0f7c65b-02c7-42bb-9ee3-508a59b8fbb9
176 ;;; pp.el ends here