1 ;;; pprint.el --- A flexible Elisp pretty-printer
3 ;; Copyright (C) 2002, 2003, 2004 David Ponce
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 06 Mar 2002
9 ;; X-RCS: $Id: pprint.el,v 1.1 2007-11-26 15:06:42 michaels Exp $
11 ;; This file is not part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; pprint library provides a simple and flexible pretty-printer for
33 ;; The core function `pprint-to-string' uses `prin1' to write into a
34 ;; temporary buffer a "flat" representation of a given expression.
35 ;; Then it walks over that linear form and inserts line breaks and
36 ;; indentations to produce a pretty-printed representation of the
39 ;; The implementation uses a lot of inline functions to improve
40 ;; performance. And, when byte-compiled, pprint is reasonably fast.
42 ;; pprint can handle specific expressions using adapted "printers". A
43 ;; "printer" is a function that will be called to process a form that
44 ;; match a regular expression. A number of "standard printers" are
45 ;; predefined to pretty print common Elisp statements like `defun',
46 ;; `defvar', `lambda', `let', `progn', `cond', `if', etc..
48 ;; More "printers" should be provided in future versions ;-)
57 ;;;; Printers management
60 (defvar pprint-standard-printers nil
61 "The standard printers.")
63 (defvar pprint-printers nil
64 "The current printers.
65 This is an alist which maps printers (functions) to
68 (defsubst pprint-clear-printers ()
69 "Clear the current defined printers."
70 (setq pprint-printers nil))
72 (defun pprint-push-printer (printer matcher)
73 "Push a new PRINTER on top of defined printers.
74 MATCHER is a regexp matching expressions passed to PRINTER."
75 (add-to-list 'pprint-printers (cons printer matcher)))
76 (put 'pprint-push-printer 'lisp-indent-function 1)
78 (defmacro pprint-with-printers (table &rest body)
79 "Set up a copy of the TABLE of printers and evaluate BODY.
80 The current table of printers is saved, BODY is evaluated, and the
81 saved table is restored, even in case of an abnormal exit. Value is
83 (let ((old-table (make-symbol "old-table")))
84 `(let ((,old-table pprint-printers))
87 (setq pprint-printers (copy-sequence ,table))
89 (setq pprint-printers ,old-table)))))
90 (put 'pprint-with-printers 'lisp-indent-function 1)
96 (defvar pprint-min-width 20
97 "Minimum width required to prettify an expression.
98 If current width is greater than this value, the pretty printer does
101 (defvar pprint-width)
102 (defvar pprint-no-break)
104 (defmacro pprint-no-break-p (&rest motions)
105 "Return non-nil if executing MOTIONS don't break line."
109 (and (<= (current-column) pprint-width)
110 (= (progn (beginning-of-line) (point))
111 (progn (goto-char p) (beginning-of-line) (point)))))))
113 (defsubst pprint-maybe-newline-and-indent ()
114 "Insert a newline, then indent.
115 Does nothing if point is before a close parenthesis character or
116 already at the beginning of a line."
117 (or (looking-at "\\s)")
118 (save-excursion (skip-syntax-backward "-") (bolp))
119 (newline-and-indent)))
121 (defsubst pprint-search-printer (table)
122 "Search in TABLE for a printer to process expression at point.
123 Return the first one that match expression at point or nil if not
125 (while (and table (not (looking-at (cdar table))))
126 (setq table (cdr table)))
129 (defsubst pprint-dispatch-printer ()
130 "Dispatch a printer to print current expression.
131 Return non-nil if a printer was found."
132 (let ((printer (pprint-search-printer pprint-printers)))
137 (defsubst pprint-sexp-try (room)
138 "Try to pretty print current expression.
139 Return nil if the width needed to pretty print current expression goes
140 beyond specified ROOM."
142 (narrow-to-region (point) (progn (forward-sexp) (point)))
143 (let* ((old-sexp (buffer-string))
146 (goto-char (point-min))
148 (goto-char (point-min))
150 (while (and (setq nobreak (<= (current-column) room))
153 (delete-region (point-min) (point-max))
157 (defsubst pprint-close-list ()
158 "Built-in printer to process close parenthesis characters."
161 (defsubst pprint-nil-as-list ()
162 "If next s-expression is the nil symbol print it as ().
163 Return non-nil if nil has been found and printed."
164 (skip-syntax-forward "-'")
165 (when (looking-at "\\<nil\\>")
166 (delete-region (point) (save-excursion (forward-sexp) (point)))
170 (defsubst pprint-list ()
171 "Built-in list printer."
173 (pprint-sexp t) ;; never break after an open paren
174 (let* ((room (- pprint-width (current-column)))
175 (nobreak (>= room pprint-min-width)))
177 (while (and nobreak (not (looking-at "\\s)")))
178 (setq nobreak (pprint-sexp-try room))))
179 (or nobreak (pprint-maybe-newline-and-indent))
180 (while (not (looking-at "\\s)"))
181 (pprint-sexp nobreak)
185 (defsubst pprint-sequence ()
186 "Built-in printer of a sequence of expressions.
187 Insert a line break before each expression."
188 (while (not (looking-at "\\s)"))
189 (pprint-maybe-newline-and-indent)
192 (defun pprint-sexp (&optional pprint-no-break)
193 "Pretty print S-expression at point.
194 If optional argument PPRINT-NO-BREAK is non-nil the pretty-printed
195 representation will not start on a new line."
196 (if (or (> pprint-min-width pprint-width)
197 (pprint-no-break-p (forward-sexp)))
199 (or pprint-no-break (pprint-maybe-newline-and-indent))
200 (let ((old-mark (copy-marker (mark-marker))))
202 (mark-marker) (save-excursion (forward-sexp) (point)))
203 (while (< (point) (marker-position (mark-marker)))
204 (skip-syntax-forward "-'")
206 ((pprint-no-break-p (forward-sexp))
208 ((pprint-dispatch-printer))
216 (set-marker (mark-marker) (marker-position old-mark))
217 (set-marker old-mark nil))))
220 ;;;; Standard printers
223 (defun pprint-lambda ()
224 "Standard printer for `lambda' like forms."
227 ;; Print empty args as () instead of nil
228 (or (pprint-nil-as-list)
233 (defun pprint-defun ()
234 "Standard printer for `defun' like forms."
235 (pprint-maybe-newline-and-indent)
239 ;; Print empty args as () instead of nil
240 (or (pprint-nil-as-list)
245 (defun pprint-defvar ()
246 "Standard printer for `defvar' like forms."
247 (pprint-maybe-newline-and-indent)
250 (unless (looking-at "\\s)")
252 (unless (looking-at "\\s)")
258 "Standard printer for `let' like forms."
261 (skip-syntax-forward "-'")
262 (if (looking-at "\\s(")
265 (skip-syntax-forward "-'")
266 (unless (looking-at "\\s)")
270 ;; Print empty let binding as () instead of nil
271 (or (pprint-nil-as-list)
273 (pprint-maybe-newline-and-indent)
278 "Standard printer for `if' like forms."
282 (pprint-maybe-newline-and-indent)
287 (defun pprint-while ()
288 "Standard printer for `while' like forms."
295 (defun pprint-progn ()
296 "Standard printer for `progn' like forms."
302 (defun pprint-setq ()
303 "Standard printer for `setq' like forms."
306 (forward-sexp) ;; 1rst VAR
307 (pprint-sexp t) ;; 1rst VAL
308 (while (not (looking-at "\\s)"))
309 (pprint-maybe-newline-and-indent)
310 (forward-sexp) ;; VAR
311 (pprint-sexp t)) ;; VAL
314 (defun pprint-cond ()
315 "Standard printer for `cond' like forms."
318 (while (not (looking-at "\\s)"))
319 (pprint-maybe-newline-and-indent)
326 (defun pprint-with ()
327 "Standard printer for `with-' like forms."
328 (let* ((withfun (intern-soft (match-string 1)))
329 (nobreak (or (get withfun 'lisp-indent-function) 0)))
334 (setq nobreak (1- nobreak)))
336 (pprint-close-list)))
338 (defun pprint-setup-standard-printers ()
339 "Setup standard printers."
340 (pprint-clear-printers)
341 ;; Printers are searched in sequence from last to first pushed one.
342 ;; So it could be important to push the most generic printers first!
343 (pprint-push-printer 'pprint-with
344 (format "(%s\\>" "\\(with[-]\\(\\sw\\|\\s_\\)+\\)"
346 (pprint-push-printer 'pprint-defun
350 "defun" "defmacro" "defsubst"
352 (pprint-push-printer 'pprint-lambda
358 (pprint-push-printer 'pprint-defvar
364 (pprint-push-printer 'pprint-let
370 (pprint-push-printer 'pprint-if
376 (pprint-push-printer 'pprint-while
380 "while" "when" "unless"
381 "condition-case" "catch"
384 (pprint-push-printer 'pprint-cond
390 (pprint-push-printer 'pprint-progn
395 "save-excursion" "save-restriction"
398 (pprint-push-printer 'pprint-setq
404 (setq pprint-standard-printers pprint-printers))
406 (pprint-setup-standard-printers)
409 ;;;; User's functions & commands
413 (defun pprint-to-string (object &optional width)
414 "Return a string containing the pretty-printed representation of OBJECT.
415 OBJECT can be any Lisp object. Quoting characters are used as needed
416 to make output that `read' can handle, whenever this is possible. The
417 pretty printer try as much as possible to limit the length of lines to
418 given WIDTH. WIDTH value defaults to `fill-column'."
420 (lisp-mode-variables nil)
421 (set-syntax-table emacs-lisp-mode-syntax-table)
422 (let ((print-escape-newlines nil)
424 (prin1 object (current-buffer)))
425 (goto-char (point-min))
426 ;; Escape "(" at beginning of line. Can only occur in strings.
427 (when (looking-at "\\s(")
429 (while (re-search-forward "^\\s(" nil t)
430 (goto-char (match-beginning 0))
432 (goto-char (point-min))
433 (let* ((pprint-width (or width fill-column))
434 (zmacs-regions nil) ;; XEmacs
435 (inhibit-modification-hooks t)) ;; Emacs
440 (defun pprint (object &optional stream width)
441 "Output the pretty-printed representation of OBJECT, any Lisp object.
442 Quoting characters are printed as needed to make output that `read'
443 can handle, whenever this is possible. Output stream is STREAM, or
444 value of `standard-output' (which see). The pretty printer try as
445 much as possible to limit the length of lines to given WIDTH. WIDTH
446 value defaults to `fill-column'."
447 (princ (pprint-to-string object width)
448 (or stream standard-output)))
451 (defun pprint-function (function-name)
452 "See a pretty-printed representation of FUNCTION-NAME."
453 (interactive "aPretty print function: ")
454 (let ((code (symbol-function function-name)))
455 (if (byte-code-function-p code)
456 (error "Can't pretty-print a byte compiled function"))
458 (get-buffer-create (format "*pprint-function %s*"
462 (pprint code (current-buffer))
463 (goto-char (point-min))
464 (pop-to-buffer (current-buffer)))))
468 ;;; pprint.el ends here