22.2.2 Examples of using the Pretty Printer

As an example of the interaction of logical blocks, conditional newlines,
and indentation, consider the function simple-pprint-defun below. This
function prints out lists whose cars are defun in the
standard way assuming that the list has exactly length 4.

If the line width available is greater than or equal to 26, then all of the
output appears on one line. If the line width available is reduced to 25,
a line break is inserted at the
linear-style conditional newline
before the
expression(* x y), producing the output shown. The
(pprint-indent :block 1) causes (* x y) to be printed at a relative
indentation of 1 in the logical block.

(DEFUN PROD (X Y)
(* X Y))

If the line width available is 15, a line break is also inserted at the
fill style conditional newline before the argument list. The call on
(pprint-indent :current 0) causes the argument list to line up under the
function name.

(DEFUN PROD
(X Y)
(* X Y))

If *print-miser-width* were greater than or equal to 14, the example
output above would have been as follows, because all indentation changes
are ignored in miser mode and line breaks are inserted at
miser-style conditional newlines.

(DEFUN
PROD
(X Y)
(* X Y))

As an example of a per-line prefix, consider that evaluating the following
produces the output shown with a line width of 20 and
*print-miser-width* of nil.

As a more complex (and realistic) example, consider the function pprint-let
below. This specifies how to print a letform in the traditional
style. It is more complex than the example above, because it has to deal with
nested structure. Also, unlike the example above it contains complete code to
readably print any possible list that begins with the symbollet.
The outermost pprint-logical-blockform handles the printing of
the input list as a whole and specifies that parentheses should be printed in the
output. The second pprint-logical-blockform handles the list
of binding pairs. Each pair in the list is itself printed by the innermost
pprint-logical-block. (A loopform is used instead of
merely decomposing the pair into two objects so that readable output will
be produced no matter whether the list corresponding to the pair has one element,
two elements, or (being malformed) has more than two elements.)
A space and a
fill-style conditional newline
are placed after
each pair except the last. The loop at the end of the topmost
pprint-logical-blockform prints out the forms in the body
of the letform separated by spaces and
linear-style conditional newlines.

If the line length is greater than or equal to 77, the output produced
appears on one line. However, if the line length is 76, line breaks are
inserted at the linear-style conditional newlines separating the forms in
the body and the output below is produced. Note that, the degenerate
binding pair x is printed readably even though it fails to be a list; a
depth abbreviation marker is printed in place of (g 3); the binding pair
(z . 2) is printed readably even though it is not a proper list; and
appropriate circularity markers are printed.

Suppose that the line length is further reduced to 22 and *print-length* is
set to 3. In this situation, line breaks are inserted after both the first
and second binding pairs. In addition, the second binding pair is itself
broken across two lines. Clause (b) of the description of fill-style
conditional newlines (see the functionpprint-newline)
prevents the binding pair (z . 2) from being printed
at the end of the third line. Note that the length abbreviation hides the
circularity from view and therefore the printing of circularity markers
disappears.

As examples of the convenience of specifying pretty printing with
format strings, consider that the functions simple-pprint-defun
and pprint-let used as examples above can be compactly defined as follows.
(The function pprint-vector cannot be defined using format
because the data structure it traverses is not a list.)

In the following example, the first form restores
*print-pprint-dispatch* to the equivalent of its initial value.
The next two forms then set up a special way to pretty print ratios.
Note that the more specific type specifier has to be associated
with a higher priority.

The following two forms illustrate the definition of
pretty printing functions for types of code. The first
form illustrates how to specify the traditional method
for printing quoted objects using single-quote. Note
the care taken to ensure that data lists that happen to begin
with quote will be printed readably. The second form
specifies that lists beginning with the symbol my-let
should print the same way that lists beginning with let
print when the initial pprint dispatch table is in effect.

The next example specifies a default method for printing lists that do not
correspond to function calls. Note that the functions pprint-linear,
pprint-fill, and pprint-tabular are all defined with
optional colon-p and at-sign-p arguments so that they can
be used as pprint dispatch functions as well as ~/.../
functions.

The pretty printing function for the structure family specifies how to
adjust the layout of the output so that it can fit aesthetically into
a variety of line widths. In addition, it obeys
the printer control variables *print-level*,
*print-length*, *print-lines*,
*print-circle*
and *print-escape*,
and can tolerate several different kinds of malformity in the data structure.
The output below shows what is printed out with a right margin of 25,
*print-pretty* being true, *print-escape* being false,
and a malformed kids list.