Skip to main content

*print-pprint-dispatch*

print-pprint-dispatch∗ Variable

Value Type:

a pprint dispatch table.

Initial Value:

implementation-dependent, but the initial entries all use a special class of priorities that have the property that they are less than every priority that can be specified using set-pprint-dispatch, so that the initial contents of any entry can be overridden.

Description:

The pprint dispatch table which currently controls the pretty printer .

See Also:

*print-pretty*, Section 22.2.1.4 (Pretty Print Dispatch Tables)

Notes:

The intent is that the initial value of this variable should cause ‘traditional’ pretty printing of code. In general, however, you can put a value in *print-pprint-dispatch* that makes pretty-printed output look exactly like non-pretty-printed output. Setting *print-pretty* to true just causes the functions contained in the current pprint dispatch table to have priority over normal print-object methods; it has no magic way of enforcing that those functions actually produce pretty output. For details, see Section 22.2.1.4 (Pretty Print Dispatch Tables).

Expanded Reference: *print-pprint-dispatch*

What It Holds

*print-pprint-dispatch* holds the current pprint dispatch table, which controls how objects are pretty-printed. The initial value contains entries for standard Lisp forms.

(type-of *print-pprint-dispatch*)
;; => SB-PRETTY:PPRINT-DISPATCH-TABLE ; implementation-dependent

Using a Custom Dispatch Table

You can bind *print-pprint-dispatch* to a custom table to change pretty-printing behavior locally.

;; Create a copy and add a custom printer for integers
(let ((*print-pprint-dispatch* (copy-pprint-dispatch))
(*print-pretty* t))
(set-pprint-dispatch 'integer
(lambda (stream obj)
(format stream "[~D]" obj)))
(write-to-string '(+ 1 2 3)))
;; => "(+ [1] [2] [3])"

Restoring Default Behavior

Passing nil to copy-pprint-dispatch returns a copy of the initial (standard) dispatch table.

;; Reset to default pretty-printing
(let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
(*print-pretty* t))
(write-to-string '(defun foo (x) (+ x 1))))
=> "(DEFUN FOO (X) (+ X 1))"

The Dispatch Table and print-pretty

The dispatch table is only consulted when *print-pretty* is true. When false, the standard print-object methods are used directly.

(let ((*print-pretty* nil))
;; dispatch table entries are ignored
(write-to-string '(a b c)))
=> "(A B C)"