@node Printer, Reader, Streams, Top @chapter Printer @menu * The Lisp Printer:: * The Lisp Pretty Printer:: * Formatted Output:: * Printer Dictionary:: @end menu @node The Lisp Printer, The Lisp Pretty Printer, Printer, Printer @section The Lisp Printer @c including concept-print @menu * Overview of The Lisp Printer:: * Printer Dispatching:: * Default Print-Object Methods:: * Examples of Printer Behavior:: @end menu @node Overview of The Lisp Printer, Printer Dispatching, The Lisp Printer, The Lisp Printer @subsection Overview of The Lisp Printer @r{Common Lisp} provides a representation of most @i{objects} in the form of printed text called the printed representation. Functions such as @b{print} take an @i{object} and send the characters of its printed representation to a @i{stream}. The collection of routines that does this is known as the (@r{Common Lisp}) printer. Reading a printed representation typically produces an @i{object} that is @b{equal} to the originally printed @i{object}. @menu * Multiple Possible Textual Representations:: * Printer Escaping:: @end menu @node Multiple Possible Textual Representations, Printer Escaping, Overview of The Lisp Printer, Overview of The Lisp Printer @subsubsection Multiple Possible Textual Representations Most @i{objects} have more than one possible textual representation. For example, the positive @i{integer} with a magnitude of twenty-seven can be textually expressed in any of these ways: @example 27 27. #o33 #x1B #b11011 #.(* 3 3 3) 81/3 @end example A list containing the two symbols @t{A} and @t{B} can also be textually expressed in a variety of ways: @example (A B) (a b) ( a b ) (\A |B|) (|\A| B ) @end example In general, from the point of view of the @i{Lisp reader}, wherever @i{whitespace} is permissible in a textual representation, any number of @i{spaces} and @i{newlines} can appear in @i{standard syntax}. When a function such as @b{print} produces a printed representation, it must choose from among many possible textual representations. In most cases, it chooses a program readable representation, but in certain cases it might use a more compact notation that is not program-readable. A number of option variables, called @i{printer control variables} @IGindex{printer control variable} , are provided to permit control of individual aspects of the printed representation of @i{objects}. Figure 22--1 shows the @i{standardized} @i{printer control variables}; there might also be @i{implementation-defined} @i{printer control variables}. @group @noindent @w{ *print-array* *print-gensym* *print-pprint-dispatch* } @w{ *print-base* *print-length* *print-pretty* } @w{ *print-case* *print-level* *print-radix* } @w{ *print-circle* *print-lines* *print-readably* } @w{ *print-escape* *print-miser-width* *print-right-margin* } @noindent @w{ Figure 22--1: Standardized Printer Control Variables } @end group In addition to the @i{printer control variables}, the following additional @i{defined names} relate to or affect the behavior of the @i{Lisp printer}: @group @noindent @w{ *package* *read-eval* readtable-case } @w{ *read-default-float-format* *readtable* } @noindent @w{ Figure 22--2: Additional Influences on the Lisp printer. } @end group @node Printer Escaping, , Multiple Possible Textual Representations, Overview of The Lisp Printer @subsubsection Printer Escaping The @i{variable} @b{*print-escape*} controls whether the @i{Lisp printer} tries to produce notations such as escape characters and package prefixes. The @i{variable} @b{*print-readably*} can be used to override many of the individual aspects controlled by the other @i{printer control variables} when program-readable output is especially important. One of the many effects of making the @i{value} of @b{*print-readably*} be @i{true} is that the @i{Lisp printer} behaves as if @b{*print-escape*} were also @i{true}. For notational convenience, we say that if the value of either @b{*print-readably*} or @b{*print-escape*} is @i{true}, then @i{printer escaping} @IGindex{printer escaping} is ``enabled''; and we say that if the values of both @b{*print-readably*} and @b{*print-escape*} are @i{false}, then @i{printer escaping} is ``disabled''. @node Printer Dispatching, Default Print-Object Methods, Overview of The Lisp Printer, The Lisp Printer @subsection Printer Dispatching The @i{Lisp printer} makes its determination of how to print an @i{object} as follows: If the @i{value} of @b{*print-pretty*} is @i{true}, printing is controlled by the @i{current pprint dispatch table}; see @ref{Pretty Print Dispatch Tables}. Otherwise (if the @i{value} of @b{*print-pretty*} is @i{false}), the object's @b{print-object} method is used; see @ref{Default Print-Object Methods}. @node Default Print-Object Methods, Examples of Printer Behavior, Printer Dispatching, The Lisp Printer @subsection Default Print-Object Methods This section describes the default behavior of @b{print-object} methods for the @i{standardized} @i{types}. @menu * Printing Numbers:: * Printing Integers:: * Printing Ratios:: * Printing Floats:: * Printing Complexes:: * Note about Printing Numbers:: * Printing Characters:: * Printing Symbols:: * Package Prefixes for Symbols:: * Effect of Readtable Case on the Lisp Printer:: * Examples of Effect of Readtable Case on the Lisp Printer:: * Printing Strings:: * Printing Lists and Conses:: * Printing Bit Vectors:: * Printing Other Vectors:: * Printing Other Arrays:: * Examples of Printing Arrays:: * Printing Random States:: * Printing Pathnames:: * Printing Structures:: * Printing Other Objects:: @end menu @node Printing Numbers, Printing Integers, Default Print-Object Methods, Default Print-Object Methods @subsubsection Printing Numbers @node Printing Integers, Printing Ratios, Printing Numbers, Default Print-Object Methods @subsubsection Printing Integers @i{Integers} are printed in the radix specified by the @i{current output base} in positional notation, most significant digit first. If appropriate, a radix specifier can be printed; see @b{*print-radix*}. If an @i{integer} is negative, a minus sign is printed and then the absolute value of the @i{integer} is printed. The @i{integer} zero is represented by the single digit @t{0} and never has a sign. A decimal point might be printed, depending on the @i{value} of @b{*print-radix*}. For related information about the syntax of an @i{integer}, see @ref{Syntax of an Integer}. @node Printing Ratios, Printing Floats, Printing Integers, Default Print-Object Methods @subsubsection Printing Ratios @IRindex{ratio} @i{Ratios} are printed as follows: the absolute value of the numerator is printed, as for an @i{integer}; then a @t{/}; then the denominator. The numerator and denominator are both printed in the radix specified by the @i{current output base}; they are obtained as if by @b{numerator} and @b{denominator}, and so @i{ratios} are printed in reduced form (lowest terms). If appropriate, a radix specifier can be printed; see @b{*print-radix*}. If the ratio is negative, a minus sign is printed before the numerator. For related information about the syntax of a @i{ratio}, see @ref{Syntax of a Ratio}. @node Printing Floats, Printing Complexes, Printing Ratios, Default Print-Object Methods @subsubsection Printing Floats @IRindex{float} If the magnitude of the @i{float} is either zero or between 10^{-3} (inclusive) and 10^7 (exclusive), it is printed as the integer part of the number, then a decimal point, followed by the fractional part of the number; there is always at least one digit on each side of the decimal point. If the sign of the number (as determined by @b{float-sign}) is negative, then a minus sign is printed before the number. If the format of the number does not match that specified by @b{*read-default-float-format*}, then the @i{exponent marker} for that format and the digit @t{0} are also printed. For example, the base of the natural logarithms as a @i{short float} might be printed as @t{2.71828S0}. For non-zero magnitudes outside of the range 10^{-3} to 10^7, a @i{float} is printed in computerized scientific notation. The representation of the number is scaled to be between 1 (inclusive) and 10 (exclusive) and then printed, with one digit before the decimal point and at least one digit after the decimal point. Next the @i{exponent marker} for the format is printed, except that if the format of the number matches that specified by @b{*read-default-float-format*}, then the @i{exponent marker} @t{E} is used. Finally, the power of ten by which the fraction must be multiplied to equal the original number is printed as a decimal integer. For example, Avogadro's number as a @i{short float} is printed as @t{6.02S23}. For related information about the syntax of a @i{float}, see @ref{Syntax of a Float}. @node Printing Complexes, Note about Printing Numbers, Printing Floats, Default Print-Object Methods @subsubsection Printing Complexes @IRindex{complex} A @i{complex} is printed as @t{#C}, an open parenthesis, the printed representation of its real part, a space, the printed representation of its imaginary part, and finally a close parenthesis. For related information about the syntax of a @i{complex}, see @ref{Syntax of a Complex} and @ref{Sharpsign C}. @node Note about Printing Numbers, Printing Characters, Printing Complexes, Default Print-Object Methods @subsubsection Note about Printing Numbers The printed representation of a number must not contain @i{escape} @i{characters}; see @ref{Escape Characters and Potential Numbers}. @node Printing Characters, Printing Symbols, Note about Printing Numbers, Default Print-Object Methods @subsubsection Printing Characters When @i{printer escaping} is disabled, a @i{character} prints as itself; it is sent directly to the output @i{stream}. When @i{printer escaping} is enabled, then @t{#\} syntax is used. When the printer types out the name of a @i{character}, it uses the same table as the @t{#\} @i{reader macro} would use; therefore any @i{character} name that is typed out is acceptable as input (in that @i{implementation}). If a @i{non-graphic} @i{character} has a @i{standardized} @i{name}_5, that @i{name} is preferred over non-standard @i{names} for printing in @t{#\} notation. For the @i{graphic} @i{standard characters}, the @i{character} itself is always used for printing in @t{#\} notation---even if the @i{character} also has a @i{name}_5. For details about the @t{#\} @i{reader macro}, see @ref{Sharpsign Backslash}. @node Printing Symbols, Package Prefixes for Symbols, Printing Characters, Default Print-Object Methods @subsubsection Printing Symbols When @i{printer escaping} is disabled, only the characters of the @i{symbol}'s @i{name} are output (but the case in which to print characters in the @i{name} is controlled by @b{*print-case*}; see @ref{Effect of Readtable Case on the Lisp Printer}). The remainder of this section applies only when @i{printer escaping} is enabled. When printing a @i{symbol}, the printer inserts enough @i{single escape} and/or @i{multiple escape} characters (@i{backslashes} and/or @i{vertical-bars}) so that if @b{read} were called with the same @b{*readtable*} and with @b{*read-base*} bound to the @i{current output base}, it would return the same @i{symbol} (if it is not @i{apparently uninterned}) or an @i{uninterned} @i{symbol} with the same @i{print name} (otherwise). For example, if the @i{value} of @b{*print-base*} were @t{16} when printing the symbol @t{face}, it would have to be printed as @t{\FACE} or @t{\Face} or @t{|FACE|}, because the token @t{face} would be read as a hexadecimal number (decimal value 64206) if the @i{value} of @b{*read-base*} were @t{16}. For additional restrictions concerning characters with nonstandard @i{syntax types} in the @i{current readtable}, see the @i{variable} @b{*print-readably*} For information about how the @i{Lisp reader} parses @i{symbols}, see @ref{Symbols as Tokens} and @ref{Sharpsign Colon}. @b{nil} might be printed as @t{()} when @b{*print-pretty*} is @i{true} and @i{printer escaping} is enabled. @node Package Prefixes for Symbols, Effect of Readtable Case on the Lisp Printer, Printing Symbols, Default Print-Object Methods @subsubsection Package Prefixes for Symbols @i{Package prefixes} are printed if necessary. The rules for @i{package prefixes} are as follows. When the @i{symbol} is printed, if it is in the @t{KEYWORD} @i{package}, then it is printed with a preceding @i{colon}; otherwise, if it is @i{accessible} in the @i{current package}, it is printed without any @i{package prefix}; otherwise, it is printed with a @i{package prefix}. A @i{symbol} that is @i{apparently uninterned} is printed preceded by ``@t{#:}'' if @b{*print-gensym*} is @i{true} and @i{printer escaping} is enabled; if @b{*print-gensym*} is @i{false} or @i{printer escaping} is disabled, then the @i{symbol} is printed without a prefix, as if it were in the @i{current package}. Because the @t{#:} syntax does not intern the following symbol, it is necessary to use circular-list syntax if @b{*print-circle*} is @i{true} and the same uninterned symbol appears several times in an expression to be printed. For example, the result of @example (let ((x (make-symbol "FOO"))) (list x x)) @end example would be printed as @t{(#:foo #:foo)} if @b{*print-circle*} were @i{false}, but as @t{(#1=#:foo #1#)} if @b{*print-circle*} were @i{true}. A summary of the preceding package prefix rules follows: @table @asis @item @t{foo:bar} @t{foo:bar} is printed when @i{symbol} @t{bar} is external in its @i{home package} @t{foo} and is not @i{accessible} in the @i{current package}. @item @t{foo::bar} @t{foo::bar} is printed when @t{bar} is internal in its @i{home package} @t{foo} and is not @i{accessible} in the @i{current package}. @item @t{:bar} @t{:bar} is printed when the home package of @t{bar} is the @t{KEYWORD} @i{package}. @item #:bar @t{#:bar} is printed when @t{bar} is @i{apparently uninterned}, even in the pathological case that @t{bar} has no @i{home package} but is nevertheless somehow @i{accessible} in the @i{current package}. @end table @node Effect of Readtable Case on the Lisp Printer, Examples of Effect of Readtable Case on the Lisp Printer, Package Prefixes for Symbols, Default Print-Object Methods @subsubsection Effect of Readtable Case on the Lisp Printer When @i{printer escaping} is disabled, or the characters under consideration are not already quoted specifically by @i{single escape} or @i{multiple escape} syntax, the @i{readtable case} of the @i{current readtable} affects the way the @i{Lisp printer} writes @i{symbols} in the following ways: @table @asis @item @t{:upcase} When the @i{readtable case} is @t{:upcase}, @i{uppercase} @i{characters} are printed in the case specified by @b{*print-case*}, and @i{lowercase} @i{characters} are printed in their own case. @item @t{:downcase} When the @i{readtable case} is @t{:downcase}, @i{uppercase} @i{characters} are printed in their own case, and @i{lowercase} @i{characters} are printed in the case specified by @b{*print-case*}. @item @t{:preserve} When the @i{readtable case} is @t{:preserve}, all @i{alphabetic} @i{characters} are printed in their own case. @item @t{:invert} When the @i{readtable case} is @t{:invert}, the case of all @i{alphabetic} @i{characters} in single case symbol names is inverted. Mixed-case symbol names are printed as is. @end table The rules for escaping @i{alphabetic} @i{characters} in symbol names are affected by the @b{readtable-case} if @i{printer escaping} is enabled. @i{Alphabetic} @i{characters} are escaped as follows: @table @asis @item @t{:upcase} When the @i{readtable case} is @t{:upcase}, all @i{lowercase} @i{characters} must be escaped. @item @t{:downcase} When the @i{readtable case} is @t{:downcase}, all @i{uppercase} @i{characters} must be escaped. @item @t{:preserve} When the @i{readtable case} is @t{:preserve}, no @i{alphabetic} @i{characters} need be escaped. @item @t{:invert} When the @i{readtable case} is @t{:invert}, no @i{alphabetic} @i{characters} need be escaped. @end table @node Examples of Effect of Readtable Case on the Lisp Printer, Printing Strings, Effect of Readtable Case on the Lisp Printer, Default Print-Object Methods @subsubsection Examples of Effect of Readtable Case on the Lisp Printer @example (defun test-readtable-case-printing () (let ((*readtable* (copy-readtable nil)) (*print-case* *print-case*)) (format t "READTABLE-CASE *PRINT-CASE* Symbol-name Output~ ~ ~ (dolist (readtable-case '(:upcase :downcase :preserve :invert)) (setf (readtable-case *readtable*) readtable-case) (dolist (print-case '(:upcase :downcase :capitalize)) (dolist (symbol '(|ZEBRA| |Zebra| |zebra|)) (setq *print-case* print-case) (format t "~&:~A~15T:~A~29T~A~42T~A" (string-upcase readtable-case) (string-upcase print-case) (symbol-name symbol) (prin1-to-string symbol))))))) @end example The output from @t{(test-readtable-case-printing)} should be as follows: @example READTABLE-CASE *PRINT-CASE* Symbol-name Output -------------------------------------------------- :UPCASE :UPCASE ZEBRA ZEBRA :UPCASE :UPCASE Zebra |Zebra| :UPCASE :UPCASE zebra |zebra| :UPCASE :DOWNCASE ZEBRA zebra :UPCASE :DOWNCASE Zebra |Zebra| :UPCASE :DOWNCASE zebra |zebra| :UPCASE :CAPITALIZE ZEBRA Zebra :UPCASE :CAPITALIZE Zebra |Zebra| :UPCASE :CAPITALIZE zebra |zebra| :DOWNCASE :UPCASE ZEBRA |ZEBRA| :DOWNCASE :UPCASE Zebra |Zebra| :DOWNCASE :UPCASE zebra ZEBRA :DOWNCASE :DOWNCASE ZEBRA |ZEBRA| :DOWNCASE :DOWNCASE Zebra |Zebra| :DOWNCASE :DOWNCASE zebra zebra :DOWNCASE :CAPITALIZE ZEBRA |ZEBRA| :DOWNCASE :CAPITALIZE Zebra |Zebra| :DOWNCASE :CAPITALIZE zebra Zebra :PRESERVE :UPCASE ZEBRA ZEBRA :PRESERVE :UPCASE Zebra Zebra :PRESERVE :UPCASE zebra zebra :PRESERVE :DOWNCASE ZEBRA ZEBRA :PRESERVE :DOWNCASE Zebra Zebra :PRESERVE :DOWNCASE zebra zebra :PRESERVE :CAPITALIZE ZEBRA ZEBRA :PRESERVE :CAPITALIZE Zebra Zebra :PRESERVE :CAPITALIZE zebra zebra :INVERT :UPCASE ZEBRA zebra :INVERT :UPCASE Zebra Zebra :INVERT :UPCASE zebra ZEBRA :INVERT :DOWNCASE ZEBRA zebra :INVERT :DOWNCASE Zebra Zebra :INVERT :DOWNCASE zebra ZEBRA :INVERT :CAPITALIZE ZEBRA zebra :INVERT :CAPITALIZE Zebra Zebra :INVERT :CAPITALIZE zebra ZEBRA @end example @node Printing Strings, Printing Lists and Conses, Examples of Effect of Readtable Case on the Lisp Printer, Default Print-Object Methods @subsubsection Printing Strings The characters of the @i{string} are output in order. If @i{printer escaping} is enabled, a @i{double-quote} is output before and after, and all @i{double-quotes} and @i{single escapes} are preceded by @i{backslash}. The printing of @i{strings} is not affected by @b{*print-array*}. Only the @i{active} @i{elements} of the @i{string} are printed. For information on how the @i{Lisp reader} parses @i{strings}, see @ref{Double-Quote}. @node Printing Lists and Conses, Printing Bit Vectors, Printing Strings, Default Print-Object Methods @subsubsection Printing Lists and Conses Wherever possible, list notation is preferred over dot notation. Therefore the following algorithm is used to print a @i{cons} x: @table @asis @item 1. A @i{left-parenthesis} is printed. @item 2. The @i{car} of x is printed. @item 3. If the @i{cdr} of x is itself a @i{cons}, it is made to be the current @i{cons} (@i{i.e.}, x becomes that @i{cons}), a @i{space} is printed, and step 2 is re-entered. @item 4. If the @i{cdr} of x is not @i{null}, a @i{space}, a @i{dot}, a @i{space}, and the @i{cdr} of x are printed. @item 5. A @i{right-parenthesis} is printed. @end table Actually, the above algorithm is only used when @b{*print-pretty*} is @i{false}. When @b{*print-pretty*} is @i{true} (or when @b{pprint} is used), additional @i{whitespace}_1 may replace the use of a single @i{space}, and a more elaborate algorithm with similar goals but more presentational flexibility is used; see @ref{Printer Dispatching}. Although the two expressions below are equivalent, and the reader accepts either one and produces the same @i{cons}, the printer always prints such a @i{cons} in the second form. @example (a . (b . ((c . (d . nil)) . (e . nil)))) (a b (c d) e) @end example The printing of @i{conses} is affected by @b{*print-level*}, @b{*print-length*}, and @b{*print-circle*}. Following are examples of printed representations of @i{lists}: @example (a . b) ;A dotted pair of a and b (a.b) ;A list of one element, the symbol named a.b (a. b) ;A list of two elements a. and b (a .b) ;A list of two elements a and .b (a b . c) ;A dotted list of a and b with c at the end; two conses .iot ;The symbol whose name is .iot (. b) ;Invalid -- an error is signaled if an attempt is made to read ;this syntax. (a .) ;Invalid -- an error is signaled. (a .. b) ;Invalid -- an error is signaled. (a . . b) ;Invalid -- an error is signaled. (a b c ...) ;Invalid -- an error is signaled. (a \. b) ;A list of three elements a, ., and b (a |.| b) ;A list of three elements a, ., and b (a \... b) ;A list of three elements a, ..., and b (a |...| b) ;A list of three elements a, ..., and b @end example For information on how the @i{Lisp reader} parses @i{lists} and @i{conses}, see @ref{Left-Parenthesis}. @node Printing Bit Vectors, Printing Other Vectors, Printing Lists and Conses, Default Print-Object Methods @subsubsection Printing Bit Vectors A @i{bit vector} is printed as @t{#*} followed by the bits of the @i{bit vector} in order. If @b{*print-array*} is @i{false}, then the @i{bit vector} is printed in a format (using @t{#<}) that is concise but not readable. Only the @i{active} @i{elements} of the @i{bit vector} are printed. [Reviewer Note by Barrett: Need to provide for @t{#5*0} as an alternate notation for @t{#*00000}.] For information on @i{Lisp reader} parsing of @i{bit vectors}, see @ref{Sharpsign Asterisk}. @node Printing Other Vectors, Printing Other Arrays, Printing Bit Vectors, Default Print-Object Methods @subsubsection Printing Other Vectors If @b{*print-array*} is @i{true} and @b{*print-readably*} is @i{false}, any @i{vector} other than a @i{string} or @i{bit vector} is printed using general-vector syntax; this means that information about specialized vector representations does not appear. The printed representation of a zero-length @i{vector} is @t{#()}. The printed representation of a non-zero-length @i{vector} begins with @t{#(}. Following that, the first element of the @i{vector} is printed. If there are any other elements, they are printed in turn, with each such additional element preceded by a @i{space} if @b{*print-pretty*} is @i{false}, or @i{whitespace}_1 if @b{*print-pretty*} is @i{true}. A @i{right-parenthesis} after the last element terminates the printed representation of the @i{vector}. The printing of @i{vectors} is affected by @b{*print-level*} and @b{*print-length*}. If the @i{vector} has a @i{fill pointer}, then only those elements below the @i{fill pointer} are printed. If both @b{*print-array*} and @b{*print-readably*} are @i{false}, the @i{vector} is not printed as described above, but in a format (using @t{#<}) that is concise but not readable. If @b{*print-readably*} is @i{true}, the @i{vector} prints in an @i{implementation-defined} manner; see the @i{variable} @b{*print-readably*}. For information on how the @i{Lisp reader} parses these ``other @i{vectors},'' see @ref{Sharpsign Left-Parenthesis}. @node Printing Other Arrays, Examples of Printing Arrays, Printing Other Vectors, Default Print-Object Methods @subsubsection Printing Other Arrays If @b{*print-array*} is @i{true} and @b{*print-readably*} is @i{false}, any @i{array} other than a @i{vector} is printed using @t{#}@t{n}@t{A} format. Let @t{n} be the @i{rank} of the @i{array}. Then @t{#} is printed, then @t{n} as a decimal integer, then @t{A}, then @t{n} open parentheses. Next the @i{elements} are scanned in row-major order, using @b{write} on each @i{element}, and separating @i{elements} from each other with @i{whitespace}_1. The array's dimensions are numbered 0 to @t{n}-1 from left to right, and are enumerated with the rightmost index changing fastest. Every time the index for dimension @t{j} is incremented, the following actions are taken: @table @asis @item @t{*} If @t{j} < @t{n}-1, then a close parenthesis is printed. @item @t{*} If incrementing the index for dimension @t{j} caused it to equal dimension @t{j}, that index is reset to zero and the index for dimension @t{j}-1 is incremented (thereby performing these three steps recursively), unless @t{j}=0, in which case the entire algorithm is terminated. If incrementing the index for dimension @t{j} did not cause it to equal dimension @t{j}, then a space is printed. @item @t{*} If @t{j} < @t{n}-1, then an open parenthesis is printed. @end table This causes the contents to be printed in a format suitable for @t{:initial-contents} to @b{make-array}. The lists effectively printed by this procedure are subject to truncation by @b{*print-level*} and @b{*print-length*}. If the @i{array} is of a specialized @i{type}, containing bits or characters, then the innermost lists generated by the algorithm given above can instead be printed using bit-vector or string syntax, provided that these innermost lists would not be subject to truncation by @b{*print-length*}. If both @b{*print-array*} and @b{*print-readably*} are @i{false}, then the @i{array} is printed in a format (using @t{#<}) that is concise but not readable. If @b{*print-readably*} is @i{true}, the @i{array} prints in an @i{implementation-defined} manner; see the @i{variable} @b{*print-readably*}. In particular, this may be important for arrays having some dimension @t{0}. For information on how the @i{Lisp reader} parses these ``other @i{arrays},'' see @ref{Sharpsign A}. @node Examples of Printing Arrays, Printing Random States, Printing Other Arrays, Default Print-Object Methods @subsubsection Examples of Printing Arrays @example (let ((a (make-array '(3 3))) (*print-pretty* t) (*print-array* t)) (dotimes (i 3) (dotimes (j 3) (setf (aref a i j) (format nil "<~D,~D>" i j)))) (print a) (print (make-array 9 :displaced-to a))) @t{ |> } #2A(("<0,0>" "<0,1>" "<0,2>") @t{ |> } ("<1,0>" "<1,1>" "<1,2>") @t{ |> } ("<2,0>" "<2,1>" "<2,2>")) @t{ |> } #("<0,0>" "<0,1>" "<0,2>" "<1,0>" "<1,1>" "<1,2>" "<2,0>" "<2,1>" "<2,2>") @result{} # @end example @node Printing Random States, Printing Pathnames, Examples of Printing Arrays, Default Print-Object Methods @subsubsection Printing Random States A specific syntax for printing @i{objects} of @i{type} @b{random-state} is not specified. However, every @i{implementation} must arrange to print a @i{random state} @i{object} in such a way that, within the same implementation, @b{read} can construct from the printed representation a copy of the @i{random state} object as if the copy had been made by @b{make-random-state}. If the type @i{random state} is effectively implemented by using the machinery for @b{defstruct}, the usual structure syntax can then be used for printing @i{random state} objects; one might look something like @example #S(RANDOM-STATE :DATA #(14 49 98436589 786345 8734658324 ... )) @end example where the components are @i{implementation-dependent}. @node Printing Pathnames, Printing Structures, Printing Random States, Default Print-Object Methods @subsubsection Printing Pathnames When @i{printer escaping} is enabled, the syntax @t{#P"..."} is how a @i{pathname} is printed by @b{write} and the other functions herein described. The @t{"..."} is the namestring representation of the pathname. When @i{printer escaping} is disabled, @b{write} writes a @i{pathname} @i{P} by writing @t{(namestring @i{P})} instead. For information on how the @i{Lisp reader} parses @i{pathnames}, see @ref{Sharpsign P}. @node Printing Structures, Printing Other Objects, Printing Pathnames, Default Print-Object Methods @subsubsection Printing Structures By default, a @i{structure} of type S is printed using @t{#S} syntax. This behavior can be customized by specifying a @t{:print-function} or @t{:print-object} option to the @b{defstruct} @i{form} that defines S, or by writing a @b{print-object} @i{method} that is @i{specialized} for @i{objects} of type S. Different structures might print out in different ways; the default notation for structures is: @example #S(@i{structure-name} @{@i{slot-key} @i{slot-value}@}{*}) @end example where @t{#S} indicates structure syntax, @i{structure-name} is a @i{structure name}, each @i{slot-key} is an initialization argument @i{name} for a @i{slot} in the @i{structure}, and each corresponding @i{slot-value} is a representation of the @i{object} in that @i{slot}. For information on how the @i{Lisp reader} parses @i{structures}, see @ref{Sharpsign S}. @node Printing Other Objects, , Printing Structures, Default Print-Object Methods @subsubsection Printing Other Objects Other @i{objects} are printed in an @i{implementation-dependent} manner. It is not required that an @i{implementation} print those @i{objects} @i{readably}. For example, @i{hash tables}, @i{readtables}, @i{packages}, @i{streams}, and @i{functions} might not print @i{readably}. A common notation to use in this circumstance is @t{#<...>}. Since @t{#<} is not readable by the @i{Lisp reader}, the precise format of the text which follows is not important, but a common format to use is that provided by the @b{print-unreadable-object} @i{macro}. For information on how the @i{Lisp reader} treats this notation, see @ref{Sharpsign Less-Than-Sign}. For information on how to notate @i{objects} that cannot be printed @i{readably}, see @ref{Sharpsign Dot}. @node Examples of Printer Behavior, , Default Print-Object Methods, The Lisp Printer @subsection Examples of Printer Behavior @example (let ((*print-escape* t)) (fresh-line) (write #\a)) @t{ |> } #\a @result{} #\a (let ((*print-escape* nil) (*print-readably* nil)) (fresh-line) (write #\a)) @t{ |> } a @result{} #\a (progn (fresh-line) (prin1 #\a)) @t{ |> } #\a @result{} #\a (progn (fresh-line) (print #\a)) @t{ |> } @t{ |> } #\a @result{} #\a (progn (fresh-line) (princ #\a)) @t{ |> } a @result{} #\a (dolist (val '(t nil)) (let ((*print-escape* val) (*print-readably* val)) (print '#\a) (prin1 #\a) (write-char #\Space) (princ #\a) (write-char #\Space) (write #\a))) @t{ |> } #\a #\a a #\a @t{ |> } #\a #\a a a @result{} NIL (progn (fresh-line) (write '(let ((a 1) (b 2)) (+ a b)))) @t{ |> } (LET ((A 1) (B 2)) (+ A B)) @result{} (LET ((A 1) (B 2)) (+ A B)) (progn (fresh-line) (pprint '(let ((a 1) (b 2)) (+ a b)))) @t{ |> } (LET ((A 1) @t{ |> } (B 2)) @t{ |> } (+ A B)) @result{} (LET ((A 1) (B 2)) (+ A B)) (progn (fresh-line) (write '(let ((a 1) (b 2)) (+ a b)) :pretty t)) @t{ |> } (LET ((A 1) @t{ |> } (B 2)) @t{ |> } (+ A B)) @result{} (LET ((A 1) (B 2)) (+ A B)) (with-output-to-string (s) (write 'write :stream s) (prin1 'prin1 s)) @result{} "WRITEPRIN1" @end example @c end of including concept-print @node The Lisp Pretty Printer, Formatted Output, The Lisp Printer, Printer @section The Lisp Pretty Printer @c including concept-pprint @menu * Pretty Printer Concepts:: * Examples of using the Pretty Printer:: * Notes about the Pretty Printer's Background:: @end menu @node Pretty Printer Concepts, Examples of using the Pretty Printer, The Lisp Pretty Printer, The Lisp Pretty Printer @subsection Pretty Printer Concepts The facilities provided by the @i{pretty printer} @IGindex{pretty printer} permit @i{programs} to redefine the way in which @i{code} is displayed, and allow the full power of @i{pretty printing} to be applied to complex combinations of data structures. Whether any given style of output is in fact ``pretty'' is inherently a somewhat subjective issue. However, since the effect of the @i{pretty printer} can be customized by @i{conforming programs}, the necessary flexibility is provided for individual @i{programs} to achieve an arbitrary degree of aesthetic control. By providing direct access to the mechanisms within the pretty printer that make dynamic decisions about layout, the macros and functions @b{pprint-logical-block}, @b{pprint-newline}, and @b{pprint-indent} make it possible to specify pretty printing layout rules as a part of any function that produces output. They also make it very easy for the detection of circularity and sharing, and abbreviation based on length and nesting depth to be supported by the function. The @i{pretty printer} is driven entirely by dispatch based on the @i{value} of @b{*print-pprint-dispatch*}. The @i{function} @b{set-pprint-dispatch} makes it possible for @i{conforming programs} to associate new pretty printing functions with a @i{type}. @menu * Dynamic Control of the Arrangement of Output:: * Format Directive Interface:: * Compiling Format Strings:: * Pretty Print Dispatch Tables:: * Pretty Printer Margins:: @end menu @node Dynamic Control of the Arrangement of Output, Format Directive Interface, Pretty Printer Concepts, Pretty Printer Concepts @subsubsection Dynamic Control of the Arrangement of Output The actions of the @i{pretty printer} when a piece of output is too large to fit in the space available can be precisely controlled. Three concepts underlie the way these operations work---@i{logical blocks} @IGindex{logical blocks} , @i{conditional newlines} @IGindex{conditional newlines} , and @i{sections} @IGindex{sections} . Before proceeding further, it is important to define these terms. The first line of Figure 22--3 shows a schematic piece of output. Each of the characters in the output is represented by ``@t{-}''. The positions of conditional newlines are indicated by digits. The beginnings and ends of logical blocks are indicated by ``@t{<}'' and ``@t{>}'' respectively. The output as a whole is a logical block and the outermost section. This section is indicated by the @t{0}'s on the second line of Figure 1. Logical blocks nested within the output are specified by the macro @b{pprint-logical-block}. Conditional newline positions are specified by calls to @b{pprint-newline}. Each conditional newline defines two sections (one before it and one after it) and is associated with a third (the section immediately containing it). The section after a conditional newline consists of: all the output up to, but not including, (a) the next conditional newline immediately contained in the same logical block; or if (a) is not applicable, (b) the next newline that is at a lesser level of nesting in logical blocks; or if (b) is not applicable, (c) the end of the output. The section before a conditional newline consists of: all the output back to, but not including, (a) the previous conditional newline that is immediately contained in the same logical block; or if (a) is not applicable, (b) the beginning of the immediately containing logical block. The last four lines in Figure 1 indicate the sections before and after the four conditional newlines. The section immediately containing a conditional newline is the shortest section that contains the conditional newline in question. In Figure 22--3, the first conditional newline is immediately contained in the section marked with @t{0}'s, the second and third conditional newlines are immediately contained in the section before the fourth conditional newline, and the fourth conditional newline is immediately contained in the section after the first conditional newline. @example <-1---<--<--2---3->--4-->-> 000000000000000000000000000 11 111111111111111111111111 22 222 333 3333 44444444444444 44444 @end example @w{ Figure 22--2: Example of Logical Blocks, Conditional Newlines, and Sections} Whenever possible, the pretty printer displays the entire contents of a section on a single line. However, if the section is too long to fit in the space available, line breaks are inserted at conditional newline positions within the section. @node Format Directive Interface, Compiling Format Strings, Dynamic Control of the Arrangement of Output, Pretty Printer Concepts @subsubsection Format Directive Interface The primary interface to operations for dynamically determining the arrangement of output is provided through the functions and macros of the pretty printer. Figure 22--3 shows the defined names related to @i{pretty printing}. @group @noindent @w{ *print-lines* pprint-dispatch pprint-pop } @w{ *print-miser-width* pprint-exit-if-list-exhausted pprint-tab } @w{ *print-pprint-dispatch* pprint-fill pprint-tabular } @w{ *print-right-margin* pprint-indent set-pprint-dispatch } @w{ copy-pprint-dispatch pprint-linear write } @w{ format pprint-logical-block } @w{ formatter pprint-newline } @noindent @w{ Figure 22--3: Defined names related to pretty printing. } @end group Figure 22--4 identifies a set of @i{format directives} which serve as an alternate interface to the same pretty printing operations in a more textually compact form. @group @noindent @w{ @t{~I} @t{~W} @t{~<...~:>} } @w{ @t{~:T} @t{~/.../} @t{~_} } @noindent @w{ Figure 22--4: Format directives related to Pretty Printing} @end group @node Compiling Format Strings, Pretty Print Dispatch Tables, Format Directive Interface, Pretty Printer Concepts @subsubsection Compiling Format Strings A @i{format string} is essentially a program in a special-purpose language that performs printing, and that is interpreted by the @i{function} @b{format}. The @b{formatter} @i{macro} provides the efficiency of using a @i{compiled function} to do that same printing but without losing the textual compactness of @i{format strings}. A @i{format control} @IGindex{format control} is either a @i{format string} or a @i{function} that was returned by the the @b{formatter} @i{macro}. @node Pretty Print Dispatch Tables, Pretty Printer Margins, Compiling Format Strings, Pretty Printer Concepts @subsubsection Pretty Print Dispatch Tables A @i{pprint dispatch table} @IGindex{pprint dispatch table} is a mapping from keys to pairs of values. Each key is a @i{type specifier}. The values associated with a key are a ``function'' (specifically, a @i{function designator} or @b{nil}) and a ``numerical priority'' (specifically, a @i{real}). Basic insertion and retrieval is done based on the keys with the equality of keys being tested by @b{equal}. When @b{*print-pretty*} is @i{true}, the @i{current pprint dispatch table} @IGindex{current pprint dispatch table} (in @b{*print-pprint-dispatch*}) controls how @i{objects} are printed. The information in this table takes precedence over all other mechanisms for specifying how to print @i{objects}. In particular, it has priority over user-defined @b{print-object} @i{methods} because the @i{current pprint dispatch table} is consulted first. The function is chosen from the @i{current pprint dispatch table} by finding the highest priority function that is associated with a @i{type specifier} that matches the @i{object}; if there is more than one such function, it is @i{implementation-dependent} which is used. However, if there is no information in the table about how to @i{pretty print} a particular kind of @i{object}, a @i{function} is invoked which uses @b{print-object} to print the @i{object}. The value of @b{*print-pretty*} is still @i{true} when this function is @i{called}, and individual methods for @b{print-object} might still elect to produce output in a special format conditional on the @i{value} of @b{*print-pretty*}. @node Pretty Printer Margins, , Pretty Print Dispatch Tables, Pretty Printer Concepts @subsubsection Pretty Printer Margins A primary goal of pretty printing is to keep the output between a pair of margins. The column where the output begins is taken as the left margin. If the current column cannot be determined at the time output begins, the left margin is assumed to be zero. The right margin is controlled by @b{*print-right-margin*}. @node Examples of using the Pretty Printer, Notes about the Pretty Printer's Background, Pretty Printer Concepts, The Lisp Pretty Printer @subsection Examples of using the Pretty Printer As an example of the interaction of logical blocks, conditional newlines, and indentation, consider the function @t{simple-pprint-defun} below. This function prints out lists whose @i{cars} are @b{defun} in the standard way assuming that the list has exactly length @t{4}. @example (defun simple-pprint-defun (*standard-output* list) (pprint-logical-block (*standard-output* list :prefix "(" :suffix ")") (write (first list)) (write-char #\Space) (pprint-newline :miser) (pprint-indent :current 0) (write (second list)) (write-char #\Space) (pprint-newline :fill) (write (third list)) (pprint-indent :block 1) (write-char #\Space) (pprint-newline :linear) (write (fourth list)))) @end example Suppose that one evaluates the following: @example (simple-pprint-defun *standard-output* '(defun prod (x y) (* x y))) @end example If the line width available is greater than or equal to @t{26}, then all of the output appears on one line. If the line width available is reduced to @t{25}, a line break is inserted at the linear-style conditional newline @ITindex{linear-style conditional newline} before the @i{expression} @t{(* x y)}, producing the output shown. The @t{(pprint-indent :block 1)} causes @t{(* x y)} to be printed at a relative indentation of @t{1} in the logical block. @example (DEFUN PROD (X Y) (* X Y)) @end example If the line width available is @t{15}, a line break is also inserted at the fill style conditional newline before the argument list. The call on @t{(pprint-indent :current 0)} causes the argument list to line up under the function name. @example (DEFUN PROD (X Y) (* X Y)) @end example If @b{*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. @ITindex{miser-style conditional newline} @example (DEFUN PROD (X Y) (* X Y)) @end example As an example of a per-line prefix, consider that evaluating the following produces the output shown with a line width of @t{20} and @b{*print-miser-width*} of @b{nil}. @example (pprint-logical-block (*standard-output* nil :per-line-prefix ";;; ") (simple-pprint-defun *standard-output* '(defun prod (x y) (* x y)))) ;;; (DEFUN PROD ;;; (X Y) ;;; (* X Y)) @end example As a more complex (and realistic) example, consider the function @t{pprint-let} below. This specifies how to print a @b{let} @i{form} 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 @i{symbol} @b{let}. The outermost @b{pprint-logical-block} @i{form} handles the printing of the input list as a whole and specifies that parentheses should be printed in the output. The second @b{pprint-logical-block} @i{form} handles the list of binding pairs. Each pair in the list is itself printed by the innermost @b{pprint-logical-block}. (A @b{loop} @i{form} is used instead of merely decomposing the pair into two @i{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 @ITindex{fill-style conditional newline} are placed after each pair except the last. The loop at the end of the topmost @b{pprint-logical-block} @i{form} prints out the forms in the body of the @b{let} @i{form} separated by spaces and linear-style conditional newlines. @example (defun pprint-let (*standard-output* list) (pprint-logical-block (nil list :prefix "(" :suffix ")") (write (pprint-pop)) (pprint-exit-if-list-exhausted) (write-char #\Space) (pprint-logical-block (nil (pprint-pop) :prefix "(" :suffix ")") (pprint-exit-if-list-exhausted) (loop (pprint-logical-block (nil (pprint-pop) :prefix "(" :suffix ")") (pprint-exit-if-list-exhausted) (loop (write (pprint-pop)) (pprint-exit-if-list-exhausted) (write-char #\Space) (pprint-newline :linear))) (pprint-exit-if-list-exhausted) (write-char #\Space) (pprint-newline :fill))) (pprint-indent :block 1) (loop (pprint-exit-if-list-exhausted) (write-char #\Space) (pprint-newline :linear) (write (pprint-pop))))) @end example Suppose that one evaluates the following with @b{*print-level*} being 4, and @b{*print-circle*} being @i{true}. @example (pprint-let *standard-output* '#1=(let (x (*print-length* (f (g 3))) (z . 2) (k (car y))) (setq x (sqrt z)) #1#)) @end example If the line length is greater than or equal to @t{77}, the output produced appears on one line. However, if the line length is @t{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 @t{x} is printed readably even though it fails to be a list; a depth abbreviation marker is printed in place of @t{(g 3)}; the binding pair @t{(z . 2)} is printed readably even though it is not a proper list; and appropriate circularity markers are printed. @example #1=(LET (X (*PRINT-LENGTH* (F #)) (Z . 2) (K (CAR Y))) (SETQ X (SQRT Z)) #1#) @end example If the line length is reduced to @t{35}, a line break is inserted at one of the fill-style conditional newlines separating the binding pairs. @example #1=(LET (X (*PRINT-PRETTY* (F #)) (Z . 2) (K (CAR Y))) (SETQ X (SQRT Z)) #1#) @end example Suppose that the line length is further reduced to @t{22} and @b{*print-length*} is set to @t{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 @i{function} @b{pprint-newline}) prevents the binding pair @t{(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. @example (LET (X (*PRINT-LENGTH* (F #)) (Z . 2) ...) (SETQ X (SQRT Z)) ...) @end example The next function prints a vector using ``@t{#(...)}'' notation. @example (defun pprint-vector (*standard-output* v) (pprint-logical-block (nil nil :prefix "#(" :suffix ")") (let ((end (length v)) (i 0)) (when (plusp end) (loop (pprint-pop) (write (aref v i)) (if (= (incf i) end) (return nil)) (write-char #\Space) (pprint-newline :fill)))))) @end example Evaluating the following with a line length of 15 produces the output shown. @example (pprint-vector *standard-output* '#(12 34 567 8 9012 34 567 89 0 1 23)) #(12 34 567 8 9012 34 567 89 0 1 23) @end example As examples of the convenience of specifying pretty printing with @i{format strings}, consider that the functions @t{simple-pprint-defun} and @t{pprint-let} used as examples above can be compactly defined as follows. (The function @t{pprint-vector} cannot be defined using @b{format} because the data structure it traverses is not a list.) @example (defun simple-pprint-defun (*standard-output* list) (format T "~:<~W ~@@_~:I~W ~:_~W~1I ~_~W~:>" list)) (defun pprint-let (*standard-output* list) (format T "~:<~W~{@t{^}}~:<~@@@{~:<~@@@{~W~{@t{^}}~_~@}~:>~{@t{^}}~:_~@}~:>~1I~@@@{~{@t{^}}~_~W~@}~:>" list)) @end example In the following example, the first @i{form} restores @b{*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 @i{type specifier} has to be associated with a higher priority. @example (setq *print-pprint-dispatch* (copy-pprint-dispatch nil)) (set-pprint-dispatch 'ratio #'(lambda (s obj) (format s "#.(/ ~W ~W)" (numerator obj) (denominator obj)))) (set-pprint-dispatch '(and ratio (satisfies minusp)) #'(lambda (s obj) (format s "#.(- (/ ~W ~W))" (- (numerator obj)) (denominator obj))) 5) (pprint '(1/3 -2/3)) (#.(/ 1 3) #.(- (/ 2 3))) @end example The following two @i{forms} illustrate the definition of pretty printing functions for types of @i{code}. The first @i{form} illustrates how to specify the traditional method for printing quoted objects using @i{single-quote}. Note the care taken to ensure that data lists that happen to begin with @b{quote} will be printed readably. The second form specifies that lists beginning with the symbol @t{my-let} should print the same way that lists beginning with @b{let} print when the initial @i{pprint dispatch table} is in effect. @example (set-pprint-dispatch '(cons (member quote)) () #'(lambda (s list) (if (and (consp (cdr list)) (null (cddr list))) (funcall (formatter "'~W") s (cadr list)) (pprint-fill s list)))) (set-pprint-dispatch '(cons (member my-let)) (pprint-dispatch '(let) nil)) @end example The next example specifies a default method for printing lists that do not correspond to function calls. Note that the functions @b{pprint-linear}, @b{pprint-fill}, and @b{pprint-tabular} are all defined with optional @i{colon-p} and @i{at-sign-p} arguments so that they can be used as @b{pprint dispatch functions} as well as @t{~/.../} functions. @example (set-pprint-dispatch '(cons (not (and symbol (satisfies fboundp)))) #'pprint-fill -5) ;; Assume a line length of 9 (pprint '(0 b c d e f g h i j k)) (0 b c d e f g h i j k) @end example This final example shows how to define a pretty printing function for a user defined data structure. @example (defstruct family mom kids) (set-pprint-dispatch 'family #'(lambda (s f) (funcall (formatter "~@@<#<~;~W and ~2I~_~/pprint-fill/~;>~:>") s (family-mom f) (family-kids f)))) @end example The pretty printing function for the structure @t{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 @b{*print-level*}, @b{*print-length*}, @b{*print-lines*}, @b{*print-circle*} and @b{*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 @t{25}, @b{*print-pretty*} being @i{true}, @b{*print-escape*} being @i{false}, and a malformed @t{kids} list. @example (write (list 'principal-family (make-family :mom "Lucy" :kids '("Mark" "Bob" . "Dan"))) :right-margin 25 :pretty T :escape nil :miser-width nil) (PRINCIPAL-FAMILY #) @end example Note that a pretty printing function for a structure is different from the structure's @b{print-object} @i{method}. While @b{print-object} @i{methods} are permanently associated with a structure, pretty printing functions are stored in @i{pprint dispatch tables} and can be rapidly changed to reflect different printing needs. If there is no pretty printing function for a structure in the current @i{pprint dispatch table}, its @b{print-object} @i{method} is used instead. @node Notes about the Pretty Printer's Background, , Examples of using the Pretty Printer, The Lisp Pretty Printer @subsection Notes about the Pretty Printer's Background For a background reference to the abstract concepts detailed in this section, see @i{XP: A Common Lisp Pretty Printing System}. The details of that paper are not binding on this document, but may be helpful in establishing a conceptual basis for understanding this material. @c end of including concept-pprint @node Formatted Output, Printer Dictionary, The Lisp Pretty Printer, Printer @section Formatted Output @c including concept-format [Editorial Note by KMP: This is transplanted from FORMAT and will need a bit of work before it looks good standing alone. Bear with me.] @b{format} is useful for producing nicely formatted text, producing good-looking messages, and so on. @b{format} can generate and return a @i{string} or output to @i{destination}. The @i{control-string} argument to @b{format} is actually a @i{format control}. That is, it can be either a @i{format string} or a @i{function}, for example a @i{function} returned by the @b{formatter} @i{macro}. If it is a @i{function}, the @i{function} is called with the appropriate output stream as its first argument and the data arguments to @b{format} as its remaining arguments. The function should perform whatever output is necessary and return the unused tail of the arguments (if any). The compilation process performed by @b{formatter} produces a @i{function} that would do with its @i{arguments} as the @b{format} interpreter would do with those @i{arguments}. The remainder of this section describes what happens if the @i{control-string} is a @i{format string}. @i{Control-string} is composed of simple text (@i{characters}) and embedded directives. @b{format} writes the simple text as is; each embedded directive specifies further text output that is to appear at the corresponding point within the simple text. Most directives use one or more elements of @i{args} to create their output. A directive consists of a @i{tilde}, optional prefix parameters separated by commas, optional @i{colon} and @i{at-sign} modifiers, and a single character indicating what kind of directive this is. There is no required ordering between the @i{at-sign} and @i{colon} modifier. The @i{case} of the directive character is ignored. Prefix parameters are notated as signed (sign is optional) decimal numbers, or as a @i{single-quote} followed by a character. For example, @t{~5,'0d} can be used to print an @i{integer} in decimal radix in five columns with leading zeros, or @t{~5,'*d} to get leading asterisks. In place of a prefix parameter to a directive, @t{V} (or @t{v}) can be used. In this case, @b{format} takes an argument from @i{args} as a parameter to the directive. The argument should be an @i{integer} or @i{character}. If the @i{arg} used by a @t{V} parameter is @b{nil}, the effect is as if the parameter had been omitted. @t{#} can be used in place of a prefix parameter; it represents the number of @i{args} remaining to be processed. When used within a recursive format, in the context of @t{~?} or @t{~@{}, the @t{#} prefix parameter represents the number of @i{format arguments} remaining within the recursive call. Examples of @i{format strings}: @group @noindent @w{ @t{"~S"} ;This is an S directive with no parameters or modifiers. } @w{ @t{"~3,-4:@@s"} ;This is an S directive with two parameters, @t{3} and @t{-4}, } @w{ ; and both the @i{colon} and @i{at-sign} flags. } @w{ @t{"~,+4S"} ;Here the first prefix parameter is omitted and takes } @w{ ; on its default value, while the second parameter is @t{4}. } @noindent @w{ Figure 22--5: Examples of format control strings } @end group @b{format} sends the output to @i{destination}. If @i{destination} is @b{nil}, @b{format} creates and returns a @i{string} containing the output from @i{control-string}. If @i{destination} is @i{non-nil}, it must be a @i{string} with a @i{fill pointer}, a @i{stream}, or the symbol @b{t}. If @i{destination} is a @i{string} with a @i{fill pointer}, the output is added to the end of the @i{string}. If @i{destination} is a @i{stream}, the output is sent to that @i{stream}. If @i{destination} is @b{t}, the output is sent to @i{standard output}. In the description of the directives that follows, the term @i{arg} in general refers to the next item of the set of @i{args} to be processed. The word or phrase at the beginning of each description is a mnemonic for the directive. @b{format} directives do not bind any of the printer control variables (@b{*print-...*}) except as specified in the following descriptions. Implementations may specify the binding of new, implementation-specific printer control variables for each @b{format} directive, but they may neither bind any standard printer control variables not specified in description of a @b{format} directive nor fail to bind any standard printer control variables as specified in the description. @menu * FORMAT Basic Output:: * FORMAT Radix Control:: * FORMAT Floating-Point Printers:: * FORMAT Printer Operations:: * FORMAT Pretty Printer Operations:: * FORMAT Layout Control:: * FORMAT Control-Flow Operations:: * FORMAT Miscellaneous Operations:: * FORMAT Miscellaneous Pseudo-Operations:: * Additional Information about FORMAT Operations:: * Examples of FORMAT:: * Notes about FORMAT:: @end menu @node FORMAT Basic Output, FORMAT Radix Control, Formatted Output, Formatted Output @subsection FORMAT Basic Output @menu * Tilde C-> Character:: * Tilde Percent-> Newline:: * Tilde Ampersand-> Fresh-Line:: * Tilde Vertical-Bar-> Page:: * Tilde Tilde-> Tilde:: @end menu @node Tilde C-> Character, Tilde Percent-> Newline, FORMAT Basic Output, FORMAT Basic Output @subsubsection Tilde C: Character The next @i{arg} should be a @i{character}; it is printed according to the modifier flags. @t{~C} prints the @i{character} as if by using @b{write-char} if it is a @i{simple character}. @i{Characters} that are not @i{simple} are not necessarily printed as if by @b{write-char}, but are displayed in an @i{implementation-defined}, abbreviated format. For example, @example (format nil "~C" #\A) @result{} "A" (format nil "~C" #\Space) @result{} " " @end example @t{~:C} is the same as @t{~C} for @i{printing} @i{characters}, but other @i{characters} are ``spelled out.'' The intent is that this is a ``pretty'' format for printing characters. For @i{simple} @i{characters} that are not @i{printing}, what is spelled out is the @i{name} of the @i{character} (see @b{char-name}). For @i{characters} that are not @i{simple} and not @i{printing}, what is spelled out is @i{implementation-defined}. For example, @example (format nil "~:C" #\A) @result{} "A" (format nil "~:C" #\Space) @result{} "Space" ;; This next example assumes an implementation-defined "Control" attribute. (format nil "~:C" #\Control-Space) @result{} "Control-Space" @i{OR}@result{} "c-Space" @end example @t{~:@@C} prints what @t{~:C} would, and then if the @i{character} requires unusual shift keys on the keyboard to type it, this fact is mentioned. For example, @example (format nil "~:@@C" #\Control-Partial) @result{} "Control-{\partial} (Top-F)" @end example This is the format used for telling the user about a key he is expected to type, in prompts, for instance. The precise output may depend not only on the implementation, but on the particular I/O devices in use. @t{~@@C} prints the @i{character} in a way that the @i{Lisp reader} can understand, using @t{#\} syntax. @t{~@@C} binds @b{*print-escape*} to @b{t}. @node Tilde Percent-> Newline, Tilde Ampersand-> Fresh-Line, Tilde C-> Character, FORMAT Basic Output @subsubsection Tilde Percent: Newline This outputs a @t{#\Newline} character, thereby terminating the current output line and beginning a new one. @t{~@i{n}%} outputs @i{n} newlines. No @i{arg} is used. @node Tilde Ampersand-> Fresh-Line, Tilde Vertical-Bar-> Page, Tilde Percent-> Newline, FORMAT Basic Output @subsubsection Tilde Ampersand: Fresh-Line Unless it can be determined that the output stream is already at the beginning of a line, this outputs a newline. @t{~@i{n}&} calls @b{fresh-line} and then outputs @i{n}- 1 newlines. @t{~0&} does nothing. @node Tilde Vertical-Bar-> Page, Tilde Tilde-> Tilde, Tilde Ampersand-> Fresh-Line, FORMAT Basic Output @subsubsection Tilde Vertical-Bar: Page This outputs a page separator character, if possible. @t{~@i{n}|} does this @i{n} times. @node Tilde Tilde-> Tilde, , Tilde Vertical-Bar-> Page, FORMAT Basic Output @subsubsection Tilde Tilde: Tilde This outputs a @i{tilde}. @t{~@i{n}~} outputs @i{n} tildes. @node FORMAT Radix Control, FORMAT Floating-Point Printers, FORMAT Basic Output, Formatted Output @subsection FORMAT Radix Control @menu * Tilde R-> Radix:: * Tilde D-> Decimal:: * Tilde B-> Binary:: * Tilde O-> Octal:: * Tilde X-> Hexadecimal:: @end menu @node Tilde R-> Radix, Tilde D-> Decimal, FORMAT Radix Control, FORMAT Radix Control @subsubsection Tilde R: Radix @t{~@i{n}R} prints @i{arg} in radix @i{n}. The modifier flags and any remaining parameters are used as for the @t{~D} directive. @t{~D} is the same as @t{~10R}. The full form is @t{~@i{radix},@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}R}. If no prefix parameters are given to @t{~R}, then a different interpretation is given. The argument should be an @i{integer}. For example, if @i{arg} is 4: @table @asis @item @t{*} @t{~R} prints @i{arg} as a cardinal English number: @t{four}. @item @t{*} @t{~:R} prints @i{arg} as an ordinal English number: @t{fourth}. @item @t{*} @t{~@@R} prints @i{arg} as a Roman numeral: @t{IV}. @item @t{*} @t{~:@@R} prints @i{arg} as an old Roman numeral: @t{IIII}. @end table For example: @example (format nil "~,,' ,4:B" 13) @result{} "1101" (format nil "~,,' ,4:B" 17) @result{} "1 0001" (format nil "~19,0,' ,4:B" 3333) @result{} "0000 1101 0000 0101" (format nil "~3,,,' ,2:R" 17) @result{} "1 22" (format nil "~,,'|,2:D" #xFFFF) @result{} "6|55|35" @end example If and only if the first parameter, @i{n}, is supplied, @t{~R} binds @b{*print-escape*} to @i{false}, @b{*print-radix*} to @i{false}, @b{*print-base*} to @i{n}, and @b{*print-readably*} to @i{false}. If and only if no parameters are supplied, @t{~R} binds @b{*print-base*} to @t{10}. @node Tilde D-> Decimal, Tilde B-> Binary, Tilde R-> Radix, FORMAT Radix Control @subsubsection Tilde D: Decimal An @i{arg}, which should be an @i{integer}, is printed in decimal radix. @t{~D} will never put a decimal point after the number. @t{~@i{mincol}D} uses a column width of @i{mincol}; spaces are inserted on the left if the number requires fewer than @i{mincol} columns for its digits and sign. If the number doesn't fit in @i{mincol} columns, additional columns are used as needed. @t{~@i{mincol},@i{padchar}D} uses @i{padchar} as the pad character instead of space. If @i{arg} is not an @i{integer}, it is printed in @t{~A} format and decimal base. The @t{@@} modifier causes the number's sign to be printed always; the default is to print it only if the number is negative. The @t{:} modifier causes commas to be printed between groups of digits; @i{commachar} may be used to change the character used as the comma. @i{comma-interval} must be an @i{integer} and defaults to 3. When the @t{:} modifier is given to any of these directives, the @i{commachar} is printed between groups of @i{comma-interval} digits. Thus the most general form of @t{~D} is @t{~@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}D}. @t{~D} binds @b{*print-escape*} to @i{false}, @b{*print-radix*} to @i{false}, @b{*print-base*} to @t{10}, and @b{*print-readably*} to @i{false}. @node Tilde B-> Binary, Tilde O-> Octal, Tilde D-> Decimal, FORMAT Radix Control @subsubsection Tilde B: Binary This is just like @t{~D} but prints in binary radix (radix 2) instead of decimal. The full form is therefore @t{~@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}B}. @t{~B} binds @b{*print-escape*} to @i{false}, @b{*print-radix*} to @i{false}, @b{*print-base*} to @t{2}, and @b{*print-readably*} to @i{false}. @node Tilde O-> Octal, Tilde X-> Hexadecimal, Tilde B-> Binary, FORMAT Radix Control @subsubsection Tilde O: Octal This is just like @t{~D} but prints in octal radix (radix 8) instead of decimal. The full form is therefore @t{~@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}O}. @t{~O} binds @b{*print-escape*} to @i{false}, @b{*print-radix*} to @i{false}, @b{*print-base*} to @t{8}, and @b{*print-readably*} to @i{false}. @node Tilde X-> Hexadecimal, , Tilde O-> Octal, FORMAT Radix Control @subsubsection Tilde X: Hexadecimal This is just like @t{~D} but prints in hexadecimal radix (radix 16) instead of decimal. The full form is therefore @t{~@i{mincol},@i{padchar},@i{commachar},@i{comma-interval}X}. @t{~X} binds @b{*print-escape*} to @i{false}, @b{*print-radix*} to @i{false}, @b{*print-base*} to @t{16}, and @b{*print-readably*} to @i{false}. @node FORMAT Floating-Point Printers, FORMAT Printer Operations, FORMAT Radix Control, Formatted Output @subsection FORMAT Floating-Point Printers @menu * Tilde F-> Fixed-Format Floating-Point:: * Tilde E-> Exponential Floating-Point:: * Tilde G-> General Floating-Point:: * Tilde Dollarsign-> Monetary Floating-Point:: @end menu @node Tilde F-> Fixed-Format Floating-Point, Tilde E-> Exponential Floating-Point, FORMAT Floating-Point Printers, FORMAT Floating-Point Printers @subsubsection Tilde F: Fixed-Format Floating-Point The next @i{arg} is printed as a @i{float}. The full form is @t{~@i{w},@i{d},@i{k},@i{overflowchar},@i{padchar}F}. The parameter @i{w} is the width of the field to be printed; @i{d} is the number of digits to print after the decimal point; @i{k} is a scale factor that defaults to zero. Exactly @i{w} characters will be output. First, leading copies of the character @i{padchar} (which defaults to a space) are printed, if necessary, to pad the field on the left. If the @i{arg} is negative, then a minus sign is printed; if the @i{arg} is not negative, then a plus sign is printed if and only if the @t{@@} modifier was supplied. Then a sequence of digits, containing a single embedded decimal point, is printed; this represents the magnitude of the value of @i{arg} times 10^@i{k}, rounded to @i{d} fractional digits. When rounding up and rounding down would produce printed values equidistant from the scaled value of @i{arg}, then the implementation is free to use either one. For example, printing the argument @t{6.375} using the format @t{~4,2F} may correctly produce either @t{6.37} or @t{6.38}. Leading zeros are not permitted, except that a single zero digit is output before the decimal point if the printed value is less than one, and this single zero digit is not output at all if @i{w}=@i{d}+1. If it is impossible to print the value in the required format in a field of width @i{w}, then one of two actions is taken. If the parameter @i{overflowchar} is supplied, then @i{w} copies of that parameter are printed instead of the scaled value of @i{arg}. If the @i{overflowchar} parameter is omitted, then the scaled value is printed using more than @i{w} characters, as many more as may be needed. If the @i{w} parameter is omitted, then the field is of variable width. In effect, a value is chosen for @i{w} in such a way that no leading pad characters need to be printed and exactly @i{d} characters will follow the decimal point. For example, the directive @t{~,2F} will print exactly two digits after the decimal point and as many as necessary before the decimal point. If the parameter @i{d} is omitted, then there is no constraint on the number of digits to appear after the decimal point. A value is chosen for @i{d} in such a way that as many digits as possible may be printed subject to the width constraint imposed by the parameter @i{w} and the constraint that no trailing zero digits may appear in the fraction, except that if the fraction to be printed is zero, then a single zero digit should appear after the decimal point if permitted by the width constraint. If both @i{w} and @i{d} are omitted, then the effect is to print the value using ordinary free-format output; @b{prin1} uses this format for any number whose magnitude is either zero or between 10^{-3} (inclusive) and 10^7 (exclusive). If @i{w} is omitted, then if the magnitude of @i{arg} is so large (or, if @i{d} is also omitted, so small) that more than 100 digits would have to be printed, then an implementation is free, at its discretion, to print the number using exponential notation instead, as if by the directive @t{~E} (with all parameters to @t{~E} defaulted, not taking their values from the @t{~F} directive). If @i{arg} is a @i{rational} number, then it is coerced to be a @i{single float} and then printed. Alternatively, an implementation is permitted to process a @i{rational} number by any other method that has essentially the same behavior but avoids loss of precision or overflow because of the coercion. If @i{w} and @i{d} are not supplied and the number has no exact decimal representation, for example @t{1/3}, some precision cutoff must be chosen by the implementation since only a finite number of digits may be printed. If @i{arg} is a @i{complex} number or some non-numeric @i{object}, then it is printed using the format directive @t{~@i{w}D}, thereby printing it in decimal radix and a minimum field width of @i{w}. @t{~F} binds @b{*print-escape*} to @i{false} and @b{*print-readably*} to @i{false}. @node Tilde E-> Exponential Floating-Point, Tilde G-> General Floating-Point, Tilde F-> Fixed-Format Floating-Point, FORMAT Floating-Point Printers @subsubsection Tilde E: Exponential Floating-Point The next @i{arg} is printed as a @i{float} in exponential notation. The full form is @t{~@i{w},@i{d},@i{e},@i{k},@i{overflowchar},@i{padchar},@i{exponentchar}E}. The parameter @i{w} is the width of the field to be printed; @i{d} is the number of digits to print after the decimal point; @i{e} is the number of digits to use when printing the exponent; @i{k} is a scale factor that defaults to one (not zero). Exactly @i{w} characters will be output. First, leading copies of the character @i{padchar} (which defaults to a space) are printed, if necessary, to pad the field on the left. If the @i{arg} is negative, then a minus sign is printed; if the @i{arg} is not negative, then a plus sign is printed if and only if the @t{@@} modifier was supplied. Then a sequence of digits containing a single embedded decimal point is printed. The form of this sequence of digits depends on the scale factor @i{k}. If @i{k} is zero, then @i{d} digits are printed after the decimal point, and a single zero digit appears before the decimal point if the total field width will permit it. If @i{k} is positive, then it must be strictly less than @i{d}+2; @i{k} significant digits are printed before the decimal point, and @i{d}- @i{k}+1 digits are printed after the decimal point. If @i{k} is negative, then it must be strictly greater than - @i{d}; a single zero digit appears before the decimal point if the total field width will permit it, and after the decimal point are printed first - @i{k} zeros and then @i{d}+@i{k} significant digits. The printed fraction must be properly rounded. When rounding up and rounding down would produce printed values equidistant from the scaled value of @i{arg}, then the implementation is free to use either one. For example, printing the argument @t{637.5} using the format @t{~8,2E} may correctly produce either @t{6.37E+2} or @t{6.38E+2}. Following the digit sequence, the exponent is printed. First the character parameter @i{exponentchar} is printed; if this parameter is omitted, then the @i{exponent marker} that @b{prin1} would use is printed, as determined from the type of the @i{float} and the current value of @b{*read-default-float-format*}. Next, either a plus sign or a minus sign is printed, followed by @i{e} digits representing the power of ten by which the printed fraction must be multiplied to properly represent the rounded value of @i{arg}. If it is impossible to print the value in the required format in a field of width @i{w}, possibly because @i{k} is too large or too small or because the exponent cannot be printed in @i{e} character positions, then one of two actions is taken. If the parameter @i{overflowchar} is supplied, then @i{w} copies of that parameter are printed instead of the scaled value of @i{arg}. If the @i{overflowchar} parameter is omitted, then the scaled value is printed using more than @i{w} characters, as many more as may be needed; if the problem is that @i{d} is too small for the supplied @i{k} or that @i{e} is too small, then a larger value is used for @i{d} or @i{e} as may be needed. If the @i{w} parameter is omitted, then the field is of variable width. In effect a value is chosen for @i{w} in such a way that no leading pad characters need to be printed. If the parameter @i{d} is omitted, then there is no constraint on the number of digits to appear. A value is chosen for @i{d} in such a way that as many digits as possible may be printed subject to the width constraint imposed by the parameter @i{w}, the constraint of the scale factor @i{k}, and the constraint that no trailing zero digits may appear in the fraction, except that if the fraction to be printed is zero then a single zero digit should appear after the decimal point. If the parameter @i{e} is omitted, then the exponent is printed using the smallest number of digits necessary to represent its value. If all of @i{w}, @i{d}, and @i{e} are omitted, then the effect is to print the value using ordinary free-format exponential-notation output; @b{prin1} uses a similar format for any non-zero number whose magnitude is less than 10^{-3} or greater than or equal to 10^7. The only difference is that the @t{~E} directive always prints a plus or minus sign in front of the exponent, while @b{prin1} omits the plus sign if the exponent is non-negative. If @i{arg} is a @i{rational} number, then it is coerced to be a @i{single float} and then printed. Alternatively, an implementation is permitted to process a @i{rational} number by any other method that has essentially the same behavior but avoids loss of precision or overflow because of the coercion. If @i{w} and @i{d} are unsupplied and the number has no exact decimal representation, for example @t{1/3}, some precision cutoff must be chosen by the implementation since only a finite number of digits may be printed. If @i{arg} is a @i{complex} number or some non-numeric @i{object}, then it is printed using the format directive @t{~@i{w}D}, thereby printing it in decimal radix and a minimum field width of @i{w}. @t{~E} binds @b{*print-escape*} to @i{false} and @b{*print-readably*} to @i{false}. @node Tilde G-> General Floating-Point, Tilde Dollarsign-> Monetary Floating-Point, Tilde E-> Exponential Floating-Point, FORMAT Floating-Point Printers @subsubsection Tilde G: General Floating-Point The next @i{arg} is printed as a @i{float} in either fixed-format or exponential notation as appropriate. The full form is @t{~@i{w},@i{d},@i{e},@i{k},@i{overflowchar},@i{padchar},@i{exponentchar}G}. The format in which to print @i{arg} depends on the magnitude (absolute value) of the @i{arg}. Let @i{n} be an integer such that 10^{{n}-1} \le |@i{arg}| < 10^@i{n}. Let @i{ee} equal @i{e}+2, or 4 if @i{e} is omitted. Let @i{ww} equal @i{w}- @i{ee}, or @b{nil} if @i{w} is omitted. If @i{d} is omitted, first let @i{q} be the number of digits needed to print @i{arg} with no loss of information and without leading or trailing zeros; then let @i{d} equal @t{(max @i{q} (min @i{n} 7))}. Let @i{dd} equal @i{d}- @i{n}. If 0 \le @i{dd} \le @i{d}, then @i{arg} is printed as if by the format directives @t{~@i{ww},@i{dd},,@i{overflowchar},@i{padchar}F~@i{ee}@@T} Note that the scale factor @i{k} is not passed to the @t{~F} directive. For all other values of @i{dd}, @i{arg} is printed as if by the format directive @t{~@i{w},@i{d},@i{e},@i{k},@i{overflowchar},@i{padchar},@i{exponentchar}E} In either case, an @t{@@} modifier is supplied to the @t{~F} or @t{~E} directive if and only if one was supplied to the @t{~G} directive. @t{~G} binds @b{*print-escape*} to @i{false} and @b{*print-readably*} to @i{false}. @node Tilde Dollarsign-> Monetary Floating-Point, , Tilde G-> General Floating-Point, FORMAT Floating-Point Printers @subsubsection Tilde Dollarsign: Monetary Floating-Point The next @i{arg} is printed as a @i{float} in fixed-format notation. The full form is @t{~@i{d},@i{n},@i{w},@i{padchar}$}. The parameter @i{d} is the number of digits to print after the decimal point (default value 2); @i{n} is the minimum number of digits to print before the decimal point (default value 1); @i{w} is the minimum total width of the field to be printed (default value 0). First padding and the sign are output. If the @i{arg} is negative, then a minus sign is printed; if the @i{arg} is not negative, then a plus sign is printed if and only if the @t{@@} modifier was supplied. If the @t{:} modifier is used, the sign appears before any padding, and otherwise after the padding. If @i{w} is supplied and the number of other characters to be output is less than @i{w}, then copies of @i{padchar} (which defaults to a space) are output to make the total field width equal @i{w}. Then @i{n} digits are printed for the integer part of @i{arg}, with leading zeros if necessary; then a decimal point; then @i{d} digits of fraction, properly rounded. If the magnitude of @i{arg} is so large that more than @i{m} digits would have to be printed, where @i{m} is the larger of @i{w} and 100, then an implementation is free, at its discretion, to print the number using exponential notation instead, as if by the directive @t{~@i{w},@i{q},,,,@i{padchar}E}, where @i{w} and @i{padchar} are present or omitted according to whether they were present or omitted in the @t{~$} directive, and where @i{q}=@i{d}+@i{n}- 1, where @i{d} and @i{n} are the (possibly default) values given to the @t{~$} directive. If @i{arg} is a @i{rational} number, then it is coerced to be a @i{single float} and then printed. Alternatively, an implementation is permitted to process a @i{rational} number by any other method that has essentially the same behavior but avoids loss of precision or overflow because of the coercion. If @i{arg} is a @i{complex} number or some non-numeric @i{object}, then it is printed using the format directive @t{~@i{w}D}, thereby printing it in decimal radix and a minimum field width of @i{w}. @t{~$} binds @b{*print-escape*} to @i{false} and @b{*print-readably*} to @i{false}. @node FORMAT Printer Operations, FORMAT Pretty Printer Operations, FORMAT Floating-Point Printers, Formatted Output @subsection FORMAT Printer Operations @menu * Tilde A-> Aesthetic:: * Tilde S-> Standard:: * Tilde W-> Write:: @end menu @node Tilde A-> Aesthetic, Tilde S-> Standard, FORMAT Printer Operations, FORMAT Printer Operations @subsubsection Tilde A: Aesthetic An @i{arg}, any @i{object}, is printed without escape characters (as by @b{princ}). If @i{arg} is a @i{string}, its @i{characters} will be output verbatim. If @i{arg} is @b{nil} it will be printed as @b{nil}; the @i{colon} modifier (@t{~:A}) will cause an @i{arg} of @b{nil} to be printed as @t{()}, but if @i{arg} is a composite structure, such as a @i{list} or @i{vector}, any contained occurrences of @b{nil} will still be printed as @b{nil}. @t{~@i{mincol}A} inserts spaces on the right, if necessary, to make the width at least @i{mincol} columns. The @t{@@} modifier causes the spaces to be inserted on the left rather than the right. @t{~@i{mincol},@i{colinc},@i{minpad},@i{padchar}A} is the full form of @t{~A}, which allows control of the padding. The @i{string} is padded on the right (or on the left if the @t{@@} modifier is used) with at least @i{minpad} copies of @i{padchar}; padding characters are then inserted @i{colinc} characters at a time until the total width is at least @i{mincol}. The defaults are @t{0} for @i{mincol} and @i{minpad}, @t{1} for @i{colinc}, and the space character for @i{padchar}. @t{~A} binds @b{*print-escape*} to @i{false}, and @b{*print-readably*} to @i{false}. @node Tilde S-> Standard, Tilde W-> Write, Tilde A-> Aesthetic, FORMAT Printer Operations @subsubsection Tilde S: Standard This is just like @t{~A}, but @i{arg} is printed with escape characters (as by @b{prin1} rather than @t{princ}). The output is therefore suitable for input to @b{read}. @t{~S} accepts all the arguments and modifiers that @t{~A} does. @t{~S} binds @b{*print-escape*} to @b{t}. @node Tilde W-> Write, , Tilde S-> Standard, FORMAT Printer Operations @subsubsection Tilde W: Write An argument, any @i{object}, is printed obeying every printer control variable (as by @b{write}). In addition, @t{~W} interacts correctly with depth abbreviation, by not resetting the depth counter to zero. @t{~W} does not accept parameters. If given the @i{colon} modifier, @t{~W} binds @b{*print-pretty*} to @i{true}. If given the @i{at-sign} modifier, @t{~W} binds @b{*print-level*} and @b{*print-length*} to @b{nil}. @t{~W} provides automatic support for the detection of circularity and sharing. If the @i{value} of @b{*print-circle*} is not @b{nil} and @t{~W} is applied to an argument that is a circular (or shared) reference, an appropriate @t{#@i{n}#} marker is inserted in the output instead of printing the argument. @node FORMAT Pretty Printer Operations, FORMAT Layout Control, FORMAT Printer Operations, Formatted Output @subsection FORMAT Pretty Printer Operations The following constructs provide access to the @i{pretty printer}: @menu * Tilde Underscore-> Conditional Newline:: * Tilde Less-Than-Sign-> Logical Block:: * Tilde I-> Indent:: * Tilde Slash-> Call Function:: @end menu @node Tilde Underscore-> Conditional Newline, Tilde Less-Than-Sign-> Logical Block, FORMAT Pretty Printer Operations, FORMAT Pretty Printer Operations @subsubsection Tilde Underscore: Conditional Newline Without any modifiers, @t{~_} is the same as @t{(pprint-newline :linear)}. @t{~@@_} is the same as @t{(pprint-newline :miser)}. @t{~:_} is the same as @t{(pprint-newline :fill)}. @t{~:@@_} is the same as @t{(pprint-newline :mandatory)}. @node Tilde Less-Than-Sign-> Logical Block, Tilde I-> Indent, Tilde Underscore-> Conditional Newline, FORMAT Pretty Printer Operations @subsubsection Tilde Less-Than-Sign: Logical Block @t{~<...~:>} If @t{~:>} is used to terminate a @t{~<...~>}, the directive is equivalent to a call to @b{pprint-logical-block}. The argument corresponding to the @t{~<...~:>} directive is treated in the same way as the @i{list} argument to @b{pprint-logical-block}, thereby providing automatic support for non-@i{list} arguments and the detection of circularity, sharing, and depth abbreviation. The portion of the @i{control-string} nested within the @t{~<...~:>} specifies the @t{:prefix} (or @t{:per-line-prefix}), @t{:suffix}, and body of the @b{pprint-logical-block}. The @i{control-string} portion enclosed by @t{~<...~:>} can be divided into segments @t{~<@i{prefix}~;@i{body}~;@i{suffix}~:>} by @t{~;} directives. If the first section is terminated by @t{~@@;}, it specifies a per-line prefix rather than a simple prefix. The @i{prefix} and @i{suffix} cannot contain format directives. An error is signaled if either the prefix or suffix fails to be a constant string or if the enclosed portion is divided into more than three segments. If the enclosed portion is divided into only two segments, the @i{suffix} defaults to the null string. If the enclosed portion consists of only a single segment, both the @i{prefix} and the @i{suffix} default to the null string. If the @i{colon} modifier is used (@i{i.e.}, @t{~:<...~:>}), the @i{prefix} and @i{suffix} default to @t{"("} and @t{")"} (respectively) instead of the null string. The body segment can be any arbitrary @i{format string}. This @i{format string} is applied to the elements of the list corresponding to the @t{~<...~:>} directive as a whole. Elements are extracted from this list using @b{pprint-pop}, thereby providing automatic support for malformed lists, and the detection of circularity, sharing, and length abbreviation. Within the body segment, @t{~{@t{^}}} acts like @b{pprint-exit-if-list-exhausted}. @t{~<...~:>} supports a feature not supported by @b{pprint-logical-block}. If @t{~:@@>} is used to terminate the directive (@i{i.e.}, @t{~<...~:@@>}), then a fill-style conditional newline is automatically inserted after each group of blanks immediately contained in the body (except for blanks after a ~<@i{Newline}> directive). This makes it easy to achieve the equivalent of paragraph filling. If the @i{at-sign} modifier is used with @t{~<...~:>}, the entire remaining argument list is passed to the directive as its argument. All of the remaining arguments are always consumed by @t{~@@<...~:>}, even if they are not all used by the @i{format string} nested in the directive. Other than the difference in its argument, @t{~@@<...~:>} is exactly the same as @t{~<...~:>} except that circularity detection is not applied if @t{~@@<...~:>} is encountered at top level in a @i{format string}. This ensures that circularity detection is applied only to data lists, not to @i{format argument} @i{lists}. @t{" . #@i{n}#"} is printed if circularity or sharing has to be indicated for its argument as a whole. To a considerable extent, the basic form of the directive @t{~<...~>} is incompatible with the dynamic control of the arrangement of output by @t{~W}, @t{~_}, @t{~<...~:>}, @t{~I}, and @t{~:T}. As a result, an error is signaled if any of these directives is nested within @t{~<...~>}. Beyond this, an error is also signaled if the @t{~<...~:;...~>} form of @t{~<...~>} is used in the same @i{format string} with @t{~W}, @t{~_}, @t{~<...~:>}, @t{~I}, or @t{~:T}. See also @ref{Tilde Less-Than-Sign-> Justification}. @node Tilde I-> Indent, Tilde Slash-> Call Function, Tilde Less-Than-Sign-> Logical Block, FORMAT Pretty Printer Operations @subsubsection Tilde I: Indent @t{~@i{n}I} is the same as @t{(pprint-indent :block n)}. @t{~@i{n}:I} is the same as @t{(pprint-indent :current n)}. In both cases, @i{n} defaults to zero, if it is omitted. @node Tilde Slash-> Call Function, , Tilde I-> Indent, FORMAT Pretty Printer Operations @subsubsection Tilde Slash: Call Function @t{~/@i{name}/} User defined functions can be called from within a format string by using the directive @t{~/@i{name}/}. The @i{colon} modifier, the @i{at-sign} modifier, and arbitrarily many parameters can be specified with the @t{~/@i{name}/} directive. @i{name} can be any arbitrary string that does not contain a "/". All of the characters in @i{name} are treated as if they were upper case. If @i{name} contains a single @i{colon} (@t{:}) or double @i{colon} (@t{::}), then everything up to but not including the first @t{":"} or @t{"::"} is taken to be a @i{string} that names a @i{package}. Everything after the first @t{":"} or @t{"::"} (if any) is taken to be a @i{string} that names a @t{symbol}. The function corresponding to a @t{~/name/} directive is obtained by looking up the @i{symbol} that has the indicated name in the indicated @i{package}. If @i{name} does not contain a @t{":"} or @t{"::"}, then the whole @i{name} string is looked up in the @t{COMMON-LISP-USER} @i{package}. When a @t{~/name/} directive is encountered, the indicated function is called with four or more arguments. The first four arguments are: the output stream, the @i{format argument} corresponding to the directive, a @i{generalized boolean} that is @i{true} if the @i{colon} modifier was used, and a @i{generalized boolean} that is @i{true} if the @i{at-sign} modifier was used. The remaining arguments consist of any parameters specified with the directive. The function should print the argument appropriately. Any values returned by the function are ignored. The three @i{functions} @b{pprint-linear}, @b{pprint-fill}, and @b{pprint-tabular} are specifically designed so that they can be called by @t{~/.../} (@i{i.e.}, @t{~/pprint-linear/}, @t{~/pprint-fill/}, and @t{~/pprint-tabular/}). In particular they take @i{colon} and @i{at-sign} arguments. @node FORMAT Layout Control, FORMAT Control-Flow Operations, FORMAT Pretty Printer Operations, Formatted Output @subsection FORMAT Layout Control @menu * Tilde T-> Tabulate:: * Tilde Less-Than-Sign-> Justification:: * Tilde Greater-Than-Sign-> End of Justification:: @end menu @node Tilde T-> Tabulate, Tilde Less-Than-Sign-> Justification, FORMAT Layout Control, FORMAT Layout Control @subsubsection Tilde T: Tabulate This spaces over to a given column. @t{~@i{colnum},@i{colinc}T} will output sufficient spaces to move the cursor to column @i{colnum}. If the cursor is already at or beyond column @i{colnum}, it will output spaces to move it to column @i{colnum}+@i{k}*@i{colinc} for the smallest positive integer @i{k} possible, unless @i{colinc} is zero, in which case no spaces are output if the cursor is already at or beyond column @i{colnum}. @i{colnum} and @i{colinc} default to @t{1}. If for some reason the current absolute column position cannot be determined by direct inquiry, @b{format} may be able to deduce the current column position by noting that certain directives (such as @t{~%}, or @t{~&}, or @t{~A} with the argument being a string containing a newline) cause the column position to be reset to zero, and counting the number of characters emitted since that point. If that fails, @b{format} may attempt a similar deduction on the riskier assumption that the destination was at column zero when @b{format} was invoked. If even this heuristic fails or is implementationally inconvenient, at worst the @t{~T} operation will simply output two spaces. @t{~@@T} performs relative tabulation. @t{~@i{colrel},@i{colinc}@@T} outputs @i{colrel} spaces and then outputs the smallest non-negative number of additional spaces necessary to move the cursor to a column that is a multiple of @i{colinc}. For example, the directive @t{~3,8@@T} outputs three spaces and then moves the cursor to a ``standard multiple-of-eight tab stop'' if not at one already. If the current output column cannot be determined, however, then @i{colinc} is ignored, and exactly @i{colrel} spaces are output. If the @i{colon} modifier is used with the @t{~T} directive, the tabbing computation is done relative to the horizontal position where the section immediately containing the directive begins, rather than with respect to a horizontal position of zero. The numerical parameters are both interpreted as being in units of @i{ems} and both default to @t{1}. @t{~@i{n},@i{m}:T} is the same as @t{(pprint-tab :section @i{n} @i{m})}. @t{~@i{n},@i{m}:@@T} is the same as @t{(pprint-tab :section-relative @i{n} @i{m})}. @node Tilde Less-Than-Sign-> Justification, Tilde Greater-Than-Sign-> End of Justification, Tilde T-> Tabulate, FORMAT Layout Control @subsubsection Tilde Less-Than-Sign: Justification @t{~@i{mincol},@i{colinc},@i{minpad},@i{padchar}<@i{str}~>} This justifies the text produced by processing @i{str} within a field at least @i{mincol} columns wide. @i{str} may be divided up into segments with @t{~;}, in which case the spacing is evenly divided between the text segments. With no modifiers, the leftmost text segment is left justified in the field, and the rightmost text segment is right justified. If there is only one text element, as a special case, it is right justified. The @t{:} modifier causes spacing to be introduced before the first text segment; the @t{@@} modifier causes spacing to be added after the last. The @i{minpad} parameter (default @t{0}) is the minimum number of padding characters to be output between each segment. The padding character is supplied by @i{padchar}, which defaults to the space character. If the total width needed to satisfy these constraints is greater than @i{mincol}, then the width used is @i{mincol}+@i{k}*@i{colinc} for the smallest possible non-negative integer value @i{k}. @i{colinc} defaults to @t{1}, and @i{mincol} defaults to @t{0}. Note that @i{str} may include @b{format} directives. All the clauses in @i{str} are processed in order; it is the resulting pieces of text that are justified. The @t{~@t{^} } directive may be used to terminate processing of the clauses prematurely, in which case only the completely processed clauses are justified. If the first clause of a @t{~<} is terminated with @t{~:;} instead of @t{~;}, then it is used in a special way. All of the clauses are processed (subject to @t{~@t{^} }, of course), but the first one is not used in performing the spacing and padding. When the padded result has been determined, then if it will fit on the current line of output, it is output, and the text for the first clause is discarded. If, however, the padded text will not fit on the current line, then the text segment for the first clause is output before the padded text. The first clause ought to contain a newline (such as a @t{~%} directive). The first clause is always processed, and so any arguments it refers to will be used; the decision is whether to use the resulting segment of text, not whether to process the first clause. If the @t{~:;} has a prefix parameter @i{n}, then the padded text must fit on the current line with @i{n} character positions to spare to avoid outputting the first clause's text. For example, the control string @example "~ @end example can be used to print a list of items separated by commas without breaking items over line boundaries, beginning each line with @t{;; }. The prefix parameter @t{1} in @t{~1:;} accounts for the width of the comma that will follow the justified item if it is not the last element in the list, or the period if it is. If @t{~:;} has a second prefix parameter, then it is used as the width of the line, thus overriding the natural line width of the output stream. To make the preceding example use a line width of 50, one would write @example "~ @end example If the second argument is not supplied, then @b{format} uses the line width of the @i{destination} output stream. If this cannot be determined (for example, when producing a @i{string} result), then @b{format} uses @t{72} as the line length. See also @ref{Tilde Less-Than-Sign-> Logical Block}. @node Tilde Greater-Than-Sign-> End of Justification, , Tilde Less-Than-Sign-> Justification, FORMAT Layout Control @subsubsection Tilde Greater-Than-Sign: End of Justification @t{~>} terminates a @t{~<}. The consequences of using it elsewhere are undefined. @node FORMAT Control-Flow Operations, FORMAT Miscellaneous Operations, FORMAT Layout Control, Formatted Output @subsection FORMAT Control-Flow Operations @menu * Tilde Asterisk-> Go-To:: * Tilde Left-Bracket-> Conditional Expression:: * Tilde Right-Bracket-> End of Conditional Expression:: * Tilde Left-Brace-> Iteration:: * Tilde Right-Brace-> End of Iteration:: * Tilde Question-Mark-> Recursive Processing:: @end menu @node Tilde Asterisk-> Go-To, Tilde Left-Bracket-> Conditional Expression, FORMAT Control-Flow Operations, FORMAT Control-Flow Operations @subsubsection Tilde Asterisk: Go-To The next @i{arg} is ignored. @t{~@i{n}*} ignores the next @i{n} arguments. @t{~:*} backs up in the list of arguments so that the argument last processed will be processed again. @t{~@i{n}:*} backs up @i{n} arguments. When within a @t{~@{} construct (see below), the ignoring (in either direction) is relative to the list of arguments being processed by the iteration. @t{~@i{n}@@*} goes to the @i{n}th @i{arg}, where 0 means the first one; @i{n} defaults to 0, so @t{~@@*} goes back to the first @i{arg}. Directives after a @t{~@i{n}@@*} will take arguments in sequence beginning with the one gone to. When within a @t{~@{} construct, the ``goto'' is relative to the list of arguments being processed by the iteration. @node Tilde Left-Bracket-> Conditional Expression, Tilde Right-Bracket-> End of Conditional Expression, Tilde Asterisk-> Go-To, FORMAT Control-Flow Operations @subsubsection Tilde Left-Bracket: Conditional Expression @t{~[@i{str0}~;@i{str1}~;@i{...}~;@i{strn}~]} This is a set of control strings, called @i{clauses}, one of which is chosen and used. The clauses are separated by @t{~;} and the construct is terminated by @t{~]}. For example, @t{"~[Siamese~;Manx~;Persian~] Cat"} The @i{arg}th clause is selected, where the first clause is number 0. If a prefix parameter is given (as @t{~@i{n}[}), then the parameter is used instead of an argument. If @i{arg} is out of range then no clause is selected and no error is signaled. After the selected alternative has been processed, the control string continues after the @t{~]}. @t{~[@i{str0}~;@i{str1}~;@i{...}~;@i{strn}~:;@i{default}~]} has a default case. If the @i{last} @t{~;} used to separate clauses is @t{~:;} instead, then the last clause is an else clause that is performed if no other clause is selected. For example: @t{"~[Siamese~;Manx~;Persian~:;Alley~] Cat"} @t{~:[@i{alternative}~;@i{consequent}~]} selects the @i{alternative} control string if @i{arg} is @i{false}, and selects the @i{consequent} control string otherwise. @t{~@@[@i{consequent}~]} tests the argument. If it is @i{true}, then the argument is not used up by the @t{~[} command but remains as the next one to be processed, and the one clause @i{consequent} is processed. If the @i{arg} is @i{false}, then the argument is used up, and the clause is not processed. The clause therefore should normally use exactly one argument, and may expect it to be @i{non-nil}. For example: @example (setq *print-level* nil *print-length* 5) (format nil "~@@[ print level = ~D~]~@@[ print length = ~D~]" *print-level* *print-length*) @result{} " print length = 5" @end example Note also that @example (format @i{stream} "...~@@[@i{str}~]..." ...) @equiv{} (format @i{stream} "...~:[~;~:*@i{str}~]..." ...) @end example The combination of @t{~[} and @t{#} is useful, for example, for dealing with English conventions for printing lists: @example (setq foo "Items:~#[ none~; ~S~; ~S and ~S~ ~:;~@@@{~#[~; and~] ~S~@t{^} ,~@}~].") (format nil foo) @result{} "Items: none." (format nil foo 'foo) @result{} "Items: FOO." (format nil foo 'foo 'bar) @result{} "Items: FOO and BAR." (format nil foo 'foo 'bar 'baz) @result{} "Items: FOO, BAR, and BAZ." (format nil foo 'foo 'bar 'baz 'quux) @result{} "Items: FOO, BAR, BAZ, and QUUX." @end example @node Tilde Right-Bracket-> End of Conditional Expression, Tilde Left-Brace-> Iteration, Tilde Left-Bracket-> Conditional Expression, FORMAT Control-Flow Operations @subsubsection Tilde Right-Bracket: End of Conditional Expression @t{~]} terminates a @t{~[}. The consequences of using it elsewhere are undefined. @node Tilde Left-Brace-> Iteration, Tilde Right-Brace-> End of Iteration, Tilde Right-Bracket-> End of Conditional Expression, FORMAT Control-Flow Operations @subsubsection Tilde Left-Brace: Iteration @t{~@{{@i{str}}~@}} This is an iteration construct. The argument should be a @i{list}, which is used as a set of arguments as if for a recursive call to @b{format}. The @i{string} @i{str} is used repeatedly as the control string. Each iteration can absorb as many elements of the @i{list} as it likes as arguments; if @i{str} uses up two arguments by itself, then two elements of the @i{list} will get used up each time around the loop. If before any iteration step the @i{list} is empty, then the iteration is terminated. Also, if a prefix parameter @i{n} is given, then there will be at most @i{n} repetitions of processing of @i{str}. Finally, the @t{~@t{^} } directive can be used to terminate the iteration prematurely. For example: @example (format nil "The winners are:~@{ ~S~@}." '(fred harry jill)) @result{} "The winners are: FRED HARRY JILL." (format nil "Pairs:~@{ <~S,~S>~@}." '(a 1 b 2 c 3)) @result{} "Pairs: ." @end example @t{~:@{ @i{str}~@} } is similar, but the argument should be a @i{list} of sublists. At each repetition step, one sublist is used as the set of arguments for processing @i{str}; on the next repetition, a new sublist is used, whether or not all of the last sublist had been processed. For example: @example (format nil "Pairs:~:@{ <~S,~S>~@} ." '((a 1) (b 2) (c 3))) @result{} "Pairs: ." @end example @t{~@@@{ @i{str}~@} } is similar to @t{~@{ @i{str}~@} }, but instead of using one argument that is a list, all the remaining arguments are used as the list of arguments for the iteration. Example: @example (format nil "Pairs:~@@@{ <~S,~S>~@} ." 'a 1 'b 2 'c 3) @result{} "Pairs: ." @end example If the iteration is terminated before all the remaining arguments are consumed, then any arguments not processed by the iteration remain to be processed by any directives following the iteration construct. @t{~:@@@{ @i{str}~@} } combines the features of @t{~:@{ @i{str}~@} } and @t{~@@@{ @i{str}~@} }. All the remaining arguments are used, and each one must be a @i{list}. On each iteration, the next argument is used as a @i{list} of arguments to @i{str}. Example: @example (format nil "Pairs:~:@@@{ <~S,~S>~@} ." '(a 1) '(b 2) '(c 3)) @result{} "Pairs: ." @end example Terminating the repetition construct with @t{~:@} } instead of @t{~@} } forces @i{str} to be processed at least once, even if the initial list of arguments is null. However, this will not override an explicit prefix parameter of zero. If @i{str} is empty, then an argument is used as @i{str}. It must be a @i{format control} and precede any arguments processed by the iteration. As an example, the following are equivalent: @example (apply #'format stream string arguments) @equiv{} (format stream "~1@{~:@}" string arguments) @end example This will use @t{string} as a formatting string. The @t{~1@{ } says it will be processed at most once, and the @t{~:@} } says it will be processed at least once. Therefore it is processed exactly once, using @t{arguments} as the arguments. This case may be handled more clearly by the @t{~?} directive, but this general feature of @t{~@{ } is more powerful than @t{~?}. @node Tilde Right-Brace-> End of Iteration, Tilde Question-Mark-> Recursive Processing, Tilde Left-Brace-> Iteration, FORMAT Control-Flow Operations @subsubsection Tilde Right-Brace: End of Iteration @t{~@}} terminates a @t{~@{}. The consequences of using it elsewhere are undefined. @node Tilde Question-Mark-> Recursive Processing, , Tilde Right-Brace-> End of Iteration, FORMAT Control-Flow Operations @subsubsection Tilde Question-Mark: Recursive Processing The next @i{arg} must be a @i{format control}, and the one after it a @i{list}; both are consumed by the @t{~?} directive. The two are processed as a @i{control-string}, with the elements of the @i{list} as the arguments. Once the recursive processing has been finished, the processing of the control string containing the @t{~?} directive is resumed. Example: @example (format nil "~? ~D" "<~A ~D>" '("Foo" 5) 7) @result{} " 7" (format nil "~? ~D" "<~A ~D>" '("Foo" 5 14) 7) @result{} " 7" @end example Note that in the second example three arguments are supplied to the @i{format string} @t{"<~A ~D>"}, but only two are processed and the third is therefore ignored. With the @t{@@} modifier, only one @i{arg} is directly consumed. The @i{arg} must be a @i{string}; it is processed as part of the control string as if it had appeared in place of the @t{~@@?} construct, and any directives in the recursively processed control string may consume arguments of the control string containing the @t{~@@?} directive. Example: @example (format nil "~@@? ~D" "<~A ~D>" "Foo" 5 7) @result{} " 7" (format nil "~@@? ~D" "<~A ~D>" "Foo" 5 14 7) @result{} " 14" @end example @node FORMAT Miscellaneous Operations, FORMAT Miscellaneous Pseudo-Operations, FORMAT Control-Flow Operations, Formatted Output @subsection FORMAT Miscellaneous Operations @menu * Tilde Left-Paren-> Case Conversion:: * Tilde Right-Paren-> End of Case Conversion:: * Tilde P-> Plural:: @end menu @node Tilde Left-Paren-> Case Conversion, Tilde Right-Paren-> End of Case Conversion, FORMAT Miscellaneous Operations, FORMAT Miscellaneous Operations @subsubsection Tilde Left-Paren: Case Conversion @t{~(@i{str}~)} The contained control string @i{str} is processed, and what it produces is subject to case conversion. With no flags, every @i{uppercase} @i{character} is converted to the corresponding @i{lowercase} @i{character}. @t{~:(} capitalizes all words, as if by @b{string-capitalize}. @t{~@@(} capitalizes just the first word and forces the rest to lower case. @t{~:@@(} converts every lowercase character to the corresponding uppercase character. In this example @t{~@@(} is used to cause the first word produced by @t{~@@R} to be capitalized: @example (format nil "~@@R ~(~@@R~)" 14 14) @result{} "XIV xiv" (defun f (n) (format nil "~@@(~R~) error~:P detected." n)) @result{} F (f 0) @result{} "Zero errors detected." (f 1) @result{} "One error detected." (f 23) @result{} "Twenty-three errors detected." @end example When case conversions appear nested, the outer conversion dominates, as illustrated in the following example: @example (format nil "~@@(how is ~:(BOB SMITH~)?~)") @result{} "How is bob smith?" @i{NOT}@result{} "How is Bob Smith?" @end example @node Tilde Right-Paren-> End of Case Conversion, Tilde P-> Plural, Tilde Left-Paren-> Case Conversion, FORMAT Miscellaneous Operations @subsubsection Tilde Right-Paren: End of Case Conversion @t{~)} terminates a @t{~(}. The consequences of using it elsewhere are undefined. @node Tilde P-> Plural, , Tilde Right-Paren-> End of Case Conversion, FORMAT Miscellaneous Operations @subsubsection Tilde P: Plural If @i{arg} is not @b{eql} to the integer @t{1}, a lowercase @t{s} is printed; if @i{arg} is @b{eql} to @t{1}, nothing is printed. If @i{arg} is a floating-point @t{1.0}, the @t{s} is printed. @t{~:P} does the same thing, after doing a @t{~:*} to back up one argument; that is, it prints a lowercase @t{s} if the previous argument was not @t{1}. @t{~@@P} prints @t{y} if the argument is @t{1}, or @t{ies} if it is not. @t{~:@@P} does the same thing, but backs up first. @example (format nil "~D tr~:@@P/~D win~:P" 7 1) @result{} "7 tries/1 win" (format nil "~D tr~:@@P/~D win~:P" 1 0) @result{} "1 try/0 wins" (format nil "~D tr~:@@P/~D win~:P" 1 3) @result{} "1 try/3 wins" @end example @node FORMAT Miscellaneous Pseudo-Operations, Additional Information about FORMAT Operations, FORMAT Miscellaneous Operations, Formatted Output @subsection FORMAT Miscellaneous Pseudo-Operations @menu * Tilde Semicolon-> Clause Separator:: * Tilde Circumflex-> Escape Upward:: * Tilde Newline-> Ignored Newline:: @end menu @node Tilde Semicolon-> Clause Separator, Tilde Circumflex-> Escape Upward, FORMAT Miscellaneous Pseudo-Operations, FORMAT Miscellaneous Pseudo-Operations @subsubsection Tilde Semicolon: Clause Separator This separates clauses in @t{~[} and @t{~<} constructs. The consequences of using it elsewhere are undefined. @node Tilde Circumflex-> Escape Upward, Tilde Newline-> Ignored Newline, Tilde Semicolon-> Clause Separator, FORMAT Miscellaneous Pseudo-Operations @subsubsection Tilde Circumflex: Escape Upward {@t{~@t{^} }} This is an escape construct. If there are no more arguments remaining to be processed, then the immediately enclosing @t{~@{ } or @t{~<} construct is terminated. If there is no such enclosing construct, then the entire formatting operation is terminated. In the @t{~<} case, the formatting is performed, but no more segments are processed before doing the justification. @t{~@t{^} } may appear anywhere in a @t{~@{ } construct. @example (setq donestr "Done.~{@t{^}} ~D warning~:P.~{@t{^}} ~D error~:P.") @result{} "Done.~{@t{^}} ~D warning~:P.~{@t{^}} ~D error~:P." (format nil donestr) @result{} "Done." (format nil donestr 3) @result{} "Done. 3 warnings." (format nil donestr 1 5) @result{} "Done. 1 warning. 5 errors." @end example If a prefix parameter is given, then termination occurs if the parameter is zero. (Hence @t{~{@t{^}}} is equivalent to @t{~#{@t{^}}}.) If two parameters are given, termination occurs if they are equal. [Reviewer Note by Barmar: Which equality predicate?] If three parameters are given, termination occurs if the first is less than or equal to the second and the second is less than or equal to the third. Of course, this is useless if all the prefix parameters are constants; at least one of them should be a @t{#} or a @t{V} parameter. If @t{~{@t{^}}} is used within a @t{~:@{ } construct, then it terminates the current iteration step because in the standard case it tests for remaining arguments of the current step only; the next iteration step commences immediately. @t{~:{@t{^}}} is used to terminate the iteration process. @t{~:{@t{^}}} may be used only if the command it would terminate is @t{~:@{ } or @t{~:@@@{ }. The entire iteration process is terminated if and only if the sublist that is supplying the arguments for the current iteration step is the last sublist in the case of @t{~:@{ }, or the last @b{format} argument in the case of @t{~:@@@{ }. @t{~:{@t{^}}} is not equivalent to @t{~#:{@t{^}}}; the latter terminates the entire iteration if and only if no arguments remain for the current iteration step. For example: @example (format nil "~:@{ ~@@?~:@t{^} ...~@} " '(("a") ("b"))) @result{} "a...b" @end example If @t{~{@t{^}}} appears within a control string being processed under the control of a @t{~?} directive, but not within any @t{~@{ } or @t{~<} construct within that string, then the string being processed will be terminated, thereby ending processing of the @t{~?} directive. Processing then continues within the string containing the @t{~?} directive at the point following that directive. If @t{~{@t{^}}} appears within a @t{~[} or @t{~(} construct, then all the commands up to the @t{~{@t{^}}} are properly selected or case-converted, the @t{~[} or @t{~(} processing is terminated, and the outward search continues for a @t{~@{ } or @t{~<} construct to be terminated. For example: @example (setq tellstr "~@@(~@@[~R~]~{@t{^}} ~A!~)") @result{} "~@@(~@@[~R~]~{@t{^}} ~A!~)" (format nil tellstr 23) @result{} "Twenty-three!" (format nil tellstr nil "losers") @result{} " Losers!" (format nil tellstr 23 "losers") @result{} "Twenty-three losers!" @end example Following are examples of the use of @t{~{@t{^}}} within a @t{~<} construct. @example (format nil "~15<~S~;~{@t{^}}~S~;~{@t{^}}~S~>" 'foo) @result{} " FOO" (format nil "~15<~S~;~{@t{^}}~S~;~{@t{^}}~S~>" 'foo 'bar) @result{} "FOO BAR" (format nil "~15<~S~;~{@t{^}}~S~;~{@t{^}}~S~>" 'foo 'bar 'baz) @result{} "FOO BAR BAZ" @end example @node Tilde Newline-> Ignored Newline, , Tilde Circumflex-> Escape Upward, FORMAT Miscellaneous Pseudo-Operations @subsubsection Tilde Newline: Ignored Newline @i{Tilde} immediately followed by a @i{newline} ignores the @i{newline} and any following non-newline @i{whitespace}_1 characters. With a @t{:}, the @i{newline} is ignored, but any following @i{whitespace}_1 is left in place. With an @t{@@}, the @i{newline} is left in place, but any following @i{whitespace}_1 is ignored. For example: @example (defun type-clash-error (fn nargs argnum right-type wrong-type) (format *error-output* "~&~S requires its ~:[~:R~;~*~]~ argument to be of type ~S,~ with an argument of type ~S.~ fn (eql nargs 1) argnum right-type wrong-type)) (type-clash-error 'aref nil 2 'integer 'vector) prints: AREF requires its second argument to be of type INTEGER, but it was called with an argument of type VECTOR. NIL (type-clash-error 'car 1 1 'list 'short-float) prints: CAR requires its argument to be of type LIST, but it was called with an argument of type SHORT-FLOAT. NIL @end example Note that in this example newlines appear in the output only as specified by the @t{~&} and @t{~%} directives; the actual newline characters in the control string are suppressed because each is preceded by a tilde. @node Additional Information about FORMAT Operations, Examples of FORMAT, FORMAT Miscellaneous Pseudo-Operations, Formatted Output @subsection Additional Information about FORMAT Operations @menu * Nesting of FORMAT Operations:: * Missing and Additional FORMAT Arguments:: * Additional FORMAT Parameters:: * Undefined FORMAT Modifier Combinations:: @end menu @node Nesting of FORMAT Operations, Missing and Additional FORMAT Arguments, Additional Information about FORMAT Operations, Additional Information about FORMAT Operations @subsubsection Nesting of FORMAT Operations The case-conversion, conditional, iteration, and justification constructs can contain other formatting constructs by bracketing them. These constructs must nest properly with respect to each other. For example, it is not legitimate to put the start of a case-conversion construct in each arm of a conditional and the end of the case-conversion construct outside the conditional: @example (format nil "~:[abc~:@@(def~;ghi~ :@@(jkl~]mno~)" x) ;Invalid! @end example This notation is invalid because the @t{~[...~;...~]} and @t{~(...~)} constructs are not properly nested. The processing indirection caused by the @t{~?} directive is also a kind of nesting for the purposes of this rule of proper nesting. It is not permitted to start a bracketing construct within a string processed under control of a @t{~?} directive and end the construct at some point after the @t{~?} construct in the string containing that construct, or vice versa. For example, this situation is invalid: @example (format nil "~@@?ghi~)" "abc~@@(def") ;Invalid! @end example This notation is invalid because the @t{~?} and @t{~(...~)} constructs are not properly nested. @node Missing and Additional FORMAT Arguments, Additional FORMAT Parameters, Nesting of FORMAT Operations, Additional Information about FORMAT Operations @subsubsection Missing and Additional FORMAT Arguments The consequences are undefined if no @i{arg} remains for a directive requiring an argument. However, it is permissible for one or more @i{args} to remain unprocessed by a directive; such @i{args} are ignored. @node Additional FORMAT Parameters, Undefined FORMAT Modifier Combinations, Missing and Additional FORMAT Arguments, Additional Information about FORMAT Operations @subsubsection Additional FORMAT Parameters The consequences are undefined if a format directive is given more parameters than it is described here as accepting. @node Undefined FORMAT Modifier Combinations, , Additional FORMAT Parameters, Additional Information about FORMAT Operations @subsubsection Undefined FORMAT Modifier Combinations The consequences are undefined if @i{colon} or @i{at-sign} modifiers are given to a directive in a combination not specifically described here as being meaningful. @node Examples of FORMAT, Notes about FORMAT, Additional Information about FORMAT Operations, Formatted Output @subsection Examples of FORMAT @example (format nil "foo") @result{} "foo" (setq x 5) @result{} 5 (format nil "The answer is ~D." x) @result{} "The answer is 5." (format nil "The answer is ~3D." x) @result{} "The answer is 5." (format nil "The answer is ~3,'0D." x) @result{} "The answer is 005." (format nil "The answer is ~:D." (expt 47 x)) @result{} "The answer is 229,345,007." (setq y "elephant") @result{} "elephant" (format nil "Look at the ~A!" y) @result{} "Look at the elephant!" (setq n 3) @result{} 3 (format nil "~D item~:P found." n) @result{} "3 items found." (format nil "~R dog~:[s are~; is~] here." n (= n 1)) @result{} "three dogs are here." (format nil "~R dog~:*~[s are~; is~:;s are~] here." n) @result{} "three dogs are here." (format nil "Here ~[are~;is~:;are~] ~:*~R pupp~:@@P." n) @result{} "Here are three puppies." @end example @example (defun foo (x) (format nil "~6,2F|~6,2,1,'*F|~6,2,,'?F|~6F|~,2F|~F" x x x x x x)) @result{} FOO (foo 3.14159) @result{} " 3.14| 31.42| 3.14|3.1416|3.14|3.14159" (foo -3.14159) @result{} " -3.14|-31.42| -3.14|-3.142|-3.14|-3.14159" (foo 100.0) @result{} "100.00|******|100.00| 100.0|100.00|100.0" (foo 1234.0) @result{} "1234.00|******|??????|1234.0|1234.00|1234.0" (foo 0.006) @result{} " 0.01| 0.06| 0.01| 0.006|0.01|0.006" @end example @example (defun foo (x) (format nil "~9,2,1,,'*E|~10,3,2,2,'?,,'$E|~ ~9,3,2,-2,' x x x x)) (foo 3.14159) @result{} " 3.14E+0| 31.42$-01|+.003E+03| 3.14E+0" (foo -3.14159) @result{} " -3.14E+0|-31.42$-01|-.003E+03| -3.14E+0" (foo 1100.0) @result{} " 1.10E+3| 11.00$+02|+.001E+06| 1.10E+3" (foo 1100.0L0) @result{} " 1.10L+3| 11.00$+02|+.001L+06| 1.10L+3" (foo 1.1E13) @result{} "*********| 11.00$+12|+.001E+16| 1.10E+13" (foo 1.1L120) @result{} "*********|??????????| (foo 1.1L1200) @result{} "*********|??????????| @end example As an example of the effects of varying the scale factor, the code @example (dotimes (k 13) (format t "~ (- k 5) (- k 5) 3.14159)) @end example produces the following output: @example Scale factor -5: | 0.000003E+06| Scale factor -4: | 0.000031E+05| Scale factor -3: | 0.000314E+04| Scale factor -2: | 0.003142E+03| Scale factor -1: | 0.031416E+02| Scale factor 0: | 0.314159E+01| Scale factor 1: | 3.141590E+00| Scale factor 2: | 31.41590E-01| Scale factor 3: | 314.1590E-02| Scale factor 4: | 3141.590E-03| Scale factor 5: | 31415.90E-04| Scale factor 6: | 314159.0E-05| Scale factor 7: | 3141590.E-06| @end example @example (defun foo (x) (format nil "~9,2,1,,'*G|~9,3,2,3,'?,,'$G|~9,3,2,0,' x x x x)) (foo 0.0314159) @result{} " 3.14E-2|314.2$-04|0.314E-01| 3.14E-2" (foo 0.314159) @result{} " 0.31 |0.314 |0.314 | 0.31 " (foo 3.14159) @result{} " 3.1 | 3.14 | 3.14 | 3.1 " (foo 31.4159) @result{} " 31. | 31.4 | 31.4 | 31. " (foo 314.159) @result{} " 3.14E+2| 314. | 314. | 3.14E+2" (foo 3141.59) @result{} " 3.14E+3|314.2$+01|0.314E+04| 3.14E+3" (foo 3141.59L0) @result{} " 3.14L+3|314.2$+01|0.314L+04| 3.14L+3" (foo 3.14E12) @result{} "*********|314.0$+10|0.314E+13| 3.14E+12" (foo 3.14L120) @result{} "*********|?????????| (foo 3.14L1200) @result{} "*********|?????????| @end example @example (format nil "~10") @result{} "foo bar" (format nil "~10:") @result{} " foo bar" (format nil "~10") @result{} " foobar" (format nil "~10:") @result{} " foobar" (format nil "~10:@@") @result{} " foo bar " (format nil "~10@@") @result{} "foobar " (format nil "~10:@@") @result{} " foobar " @end example @example (FORMAT NIL "Written to ~A." #P"foo.bin") @result{} "Written to foo.bin." @end example @node Notes about FORMAT, , Examples of FORMAT, Formatted Output @subsection Notes about FORMAT Formatted output is performed not only by @b{format}, but by certain other functions that accept a @i{format control} the way @b{format} does. For example, error-signaling functions such as @b{cerror} accept @i{format controls}. Note that the meaning of @b{nil} and @b{t} as destinations to @b{format} are different than those of @b{nil} and @b{t} as @i{stream designators}. The @t{~{@t{^}}} should appear only at the beginning of a @t{~<} clause, because it aborts the entire clause in which it appears (as well as all following clauses). @c end of including concept-format @node Printer Dictionary, , Formatted Output, Printer @section Printer Dictionary @c including dict-printer @menu * copy-pprint-dispatch:: * formatter:: * pprint-dispatch:: * pprint-exit-if-list-exhausted:: * pprint-fill:: * pprint-indent:: * pprint-logical-block:: * pprint-newline:: * pprint-pop:: * pprint-tab:: * print-object:: * print-unreadable-object:: * set-pprint-dispatch:: * write:: * write-to-string:: * *print-array*:: * *print-base*:: * *print-case*:: * *print-circle*:: * *print-escape*:: * *print-gensym*:: * *print-level*:: * *print-lines*:: * *print-miser-width*:: * *print-pprint-dispatch*:: * *print-pretty*:: * *print-readably*:: * *print-right-margin*:: * print-not-readable:: * print-not-readable-object:: * format:: @end menu @node copy-pprint-dispatch, formatter, Printer Dictionary, Printer Dictionary @subsection copy-pprint-dispatch [Function] @code{copy-pprint-dispatch} @i{{&optional} table} @result{} @i{new-table} @subsubheading Arguments and Values:: @i{table}---a @i{pprint dispatch table}, or @b{nil}. @i{new-table}---a @i{fresh} @i{pprint dispatch table}. @subsubheading Description:: Creates and returns a copy of the specified @i{table}, or of the @i{value} of @b{*print-pprint-dispatch*} if no @i{table} is specified, or of the initial @i{value} of @b{*print-pprint-dispatch*} if @b{nil} is specified. @subsubheading Exceptional Situations:: Should signal an error of @i{type} @b{type-error} if @i{table} is not a @i{pprint dispatch table}. @node formatter, pprint-dispatch, copy-pprint-dispatch, Printer Dictionary @subsection formatter [Macro] @code{formatter} @i{control-string} @result{} @i{function} @subsubheading Arguments and Values:: @i{control-string}---a @i{format string}; not evaluated. @i{function}---a @i{function}. @subsubheading Description:: Returns a @i{function} which has behavior equivalent to: @example #'(lambda (*standard-output* &rest arguments) (apply #'format t @i{control-string} arguments) @i{arguments-tail}) @end example where @i{arguments-tail} is either the tail of @i{arguments} which has as its @i{car} the argument that would be processed next if there were more format directives in the @i{control-string}, or else @b{nil} if no more @i{arguments} follow the most recently processed argument. @subsubheading Examples:: @example (funcall (formatter "~&~A~A") *standard-output* 'a 'b 'c) @t{ |> } AB @result{} (C) (format t (formatter "~&~A~A") 'a 'b 'c) @t{ |> } AB @result{} NIL @end example @subsubheading Exceptional Situations:: Might signal an error (at macro expansion time or at run time) if the argument is not a valid @i{format string}. @subsubheading See Also:: @ref{format} @node pprint-dispatch, pprint-exit-if-list-exhausted, formatter, Printer Dictionary @subsection pprint-dispatch [Function] @code{pprint-dispatch} @i{object {&optional} table} @result{} @i{function, found-p} @subsubheading Arguments and Values:: @i{object}---an @i{object}. @i{table}---a @i{pprint dispatch table}, or @b{nil}. The default is the @i{value} of @b{*print-pprint-dispatch*}. @i{function}---a @i{function designator}. @i{found-p}---a @i{generalized boolean}. @subsubheading Description:: Retrieves the highest priority function in @i{table} that is associated with a @i{type specifier} that matches @i{object}. The function is chosen by finding all of the @i{type specifiers} in @i{table} that match the @i{object} and selecting the highest priority function associated with any of these @i{type specifiers}. If there is more than one highest priority function, an arbitrary choice is made. If no @i{type specifiers} match the @i{object}, a function is returned that prints @i{object} using @b{print-object}. The @i{secondary value}, @i{found-p}, is @i{true} if a matching @i{type specifier} was found in @i{table}, or @i{false} otherwise. If @i{table} is @b{nil}, retrieval is done in the @i{initial pprint dispatch table}. @subsubheading Affected By:: The state of the @i{table}. @subsubheading Exceptional Situations:: Should signal an error of @i{type} @b{type-error} if @i{table} is neither a @i{pprint-dispatch-table} nor @b{nil}. @subsubheading Notes:: @example (let ((*print-pretty* t)) (write object :stream s)) @equiv{} (funcall (pprint-dispatch object) s object) @end example @node pprint-exit-if-list-exhausted, pprint-fill, pprint-dispatch, Printer Dictionary @subsection pprint-exit-if-list-exhausted [Local Macro] @subsubheading Syntax:: @code{pprint-exit-if-list-exhausted} @i{<@i{no @i{arguments}}>} @result{} @i{@b{nil}} @subsubheading Description:: Tests whether or not the @i{list} passed to the @i{lexically current logical block} has been exhausted; see @ref{Dynamic Control of the Arrangement of Output}. If this @i{list} has been reduced to @b{nil}, @b{pprint-exit-if-list-exhausted} terminates the execution of the @i{lexically current logical block} except for the printing of the suffix. Otherwise @b{pprint-exit-if-list-exhausted} returns @b{nil}. Whether or not @b{pprint-exit-if-list-exhausted} is @i{fbound} in the @i{global environment} is @i{implementation-dependent}; however, the restrictions on redefinition and @i{shadowing} of @b{pprint-exit-if-list-exhausted} are the same as for @i{symbols} in the @t{COMMON-LISP} @i{package} which are @i{fbound} in the @i{global environment}. The consequences of attempting to use @b{pprint-exit-if-list-exhausted} outside of @b{pprint-logical-block} are undefined. @subsubheading Exceptional Situations:: An error is signaled (at macro expansion time or at run time) if @b{pprint-exit-if-list-exhausted} is used anywhere other than lexically within a call on @b{pprint-logical-block}. Also, the consequences of executing @b{pprint-if-list-exhausted} outside of the dynamic extent of the @b{pprint-logical-block} which lexically contains it are undefined. @subsubheading See Also:: @ref{pprint-logical-block} , @ref{pprint-pop} . @node pprint-fill, pprint-indent, pprint-exit-if-list-exhausted, Printer Dictionary @subsection pprint-fill, pprint-linear, pprint-tabular [Function] @code{pprint-fill} @i{stream object {&optional} colon-p at-sign-p} @result{} @i{@b{nil}} @code{pprint-linear} @i{stream object {&optional} colon-p at-sign-p} @result{} @i{@b{nil}} @code{pprint-tabular} @i{stream object {&optional} colon-p at-sign-p tabsize} @result{} @i{@b{nil}} @subsubheading Arguments and Values:: @i{stream}---an @i{output} @i{stream designator}. @i{object}---an @i{object}. @i{colon-p}---a @i{generalized boolean}. The default is @i{true}. @i{at-sign-p}---a @i{generalized boolean}. The default is @i{implementation-dependent}. @i{tabsize}---a non-negative @i{integer}. The default is @t{16}. @subsubheading Description:: The functions @b{pprint-fill}, @b{pprint-linear}, and @b{pprint-tabular} specify particular ways of @i{pretty printing} a @i{list} to @i{stream}. Each function prints parentheses around the output if and only if @i{colon-p} is @i{true}. Each function ignores its @i{at-sign-p} argument. (Both arguments are included even though only one is needed so that these functions can be used via @t{~/.../} and as @b{set-pprint-dispatch} functions, as well as directly.) Each function handles abbreviation and the detection of circularity and sharing correctly, and uses @b{write} to print @i{object} when it is a @i{non-list}. If @i{object} is a @i{list} and if the @i{value} of @b{*print-pretty*} is @i{false}, each of these functions prints @i{object} using a minimum of @i{whitespace}, as described in @ref{Printing Lists and Conses}. Otherwise (if @i{object} is a @i{list} and if the @i{value} of @b{*print-pretty*} is @i{true}): @table @asis @item @t{*} The @i{function} @b{pprint-linear} prints a @i{list} either all on one line, or with each @i{element} on a separate line. @item @t{*} The @i{function} @b{pprint-fill} prints a @i{list} with as many @i{elements} as possible on each line. @item @t{*} The @i{function} @b{pprint-tabular} is the same as @b{pprint-fill} except that it prints the @i{elements} so that they line up in columns. The @i{tabsize} specifies the column spacing in @i{ems}, which is the total spacing from the leading edge of one column to the leading edge of the next. @end table @subsubheading Examples:: Evaluating the following with a line length of @t{25} produces the output shown. @example (progn (princ "Roads ") (pprint-tabular *standard-output* '(elm main maple center) nil nil 8)) Roads ELM MAIN MAPLE CENTER @end example @subsubheading Side Effects:: Performs output to the indicated @i{stream}. @subsubheading Affected By:: The cursor position on the indicated @i{stream}, if it can be determined. @subsubheading Notes:: The @i{function} @b{pprint-tabular} could be defined as follows: @example (defun pprint-tabular (s list &optional (colon-p t) at-sign-p (tabsize nil)) (declare (ignore at-sign-p)) (when (null tabsize) (setq tabsize 16)) (pprint-logical-block (s list :prefix (if colon-p "(" "") :suffix (if colon-p ")" "")) (pprint-exit-if-list-exhausted) (loop (write (pprint-pop) :stream s) (pprint-exit-if-list-exhausted) (write-char #\Space s) (pprint-tab :section-relative 0 tabsize s) (pprint-newline :fill s)))) @end example Note that it would have been inconvenient to specify this function using @b{format}, because of the need to pass its @i{tabsize} argument through to a @t{~:T} format directive nested within an iteration over a list. @node pprint-indent, pprint-logical-block, pprint-fill, Printer Dictionary @subsection pprint-indent [Function] @code{pprint-indent} @i{relative-to n {&optional} stream} @result{} @i{@b{nil}} @subsubheading Arguments and Values:: @i{relative-to}---either @t{:block} or @t{:current}. @i{n}---a @i{real}. @i{stream}---an @i{output} @i{stream designator}. The default is @i{standard output}. @subsubheading Description:: @b{pprint-indent} specifies the indentation to use in a logical block on @i{stream}. If @i{stream} is a @i{pretty printing stream} and the @i{value} of @b{*print-pretty*} is @i{true}, @b{pprint-indent} sets the indentation in the innermost dynamically enclosing logical block; otherwise, @b{pprint-indent} has no effect. @i{N} specifies the indentation in @i{ems}. If @i{relative-to} is @t{:block}, the indentation is set to the horizontal position of the first character in the @i{dynamically current logical block} plus @i{n} @i{ems}. If @i{relative-to} is @t{:current}, the indentation is set to the current output position plus @i{n} @i{ems}. (For robustness in the face of variable-width fonts, it is advisable to use @t{:current} with an @i{n} of zero whenever possible.) @i{N} can be negative; however, the total indentation cannot be moved left of the beginning of the line or left of the end of the rightmost per-line prefix---an attempt to move beyond one of these limits is treated the same as an attempt to move to that limit. Changes in indentation caused by @i{pprint-indent} do not take effect until after the next line break. In addition, in miser mode all calls to @b{pprint-indent} are ignored, forcing the lines corresponding to the logical block to line up under the first character in the block. @subsubheading Exceptional Situations:: An error is signaled if @i{relative-to} is any @i{object} other than @t{:block} or @t{:current}. @subsubheading See Also:: @ref{Tilde I-> Indent} @node pprint-logical-block, pprint-newline, pprint-indent, Printer Dictionary @subsection pprint-logical-block [Macro] @code{pprint-logical-block} @i{@r{(}stream-symbol object {&key} prefix per-line-prefix suffix@r{)} @{@i{declaration}@}{*} @{@i{form}@}{*}}@* @result{} @i{@b{nil}} @subsubheading Arguments and Values:: @i{stream-symbol}---a @i{stream variable designator}. @i{object}---an @i{object}; evaluated. @t{:prefix}---a @i{string}; evaluated. Complicated defaulting behavior; see below. @t{:per-line-prefix}---a @i{string}; evaluated. Complicated defaulting behavior; see below. @t{:suffix}---a @i{string}; evaluated. The default is the @i{null} @i{string}. @i{declaration}---a @b{declare} @i{expression}; not evaluated. @i{forms}---an @i{implicit progn}. @subsubheading Description:: Causes printing to be grouped into a logical block. The logical block is printed to the @i{stream} that is the @i{value} of the @i{variable} denoted by @i{stream-symbol}. During the execution of the @i{forms}, that @i{variable} is @i{bound} to a @i{pretty printing stream} that supports decisions about the arrangement of output and then forwards the output to the destination stream. All the standard printing functions (@i{e.g.}, @b{write}, @b{princ}, and @b{terpri}) can be used to print output to the @i{pretty printing stream}. All and only the output sent to this @i{pretty printing stream} is treated as being in the logical block. The @i{prefix} specifies a prefix to be printed before the beginning of the logical block. The @i{per-line-prefix} specifies a prefix that is printed before the block and at the beginning of each new line in the block. The @t{:prefix} and @t{:pre-line-prefix} @i{arguments} are mutually exclusive. If neither @t{:prefix} nor @t{:per-line-prefix} is specified, a @i{prefix} of the @i{null} @i{string} is assumed. The @i{suffix} specifies a suffix that is printed just after the logical block. The @i{object} is normally a @i{list} that the body @i{forms} are responsible for printing. If @i{object} is not a @i{list}, it is printed using @b{write}. (This makes it easier to write printing functions that are robust in the face of malformed arguments.) If @b{*print-circle*} is @i{non-nil} and @i{object} is a circular (or shared) reference to a @i{cons}, then an appropriate ``@t{#@i{n}#}'' marker is printed. (This makes it easy to write printing functions that provide full support for circularity and sharing abbreviation.) If @b{*print-level*} is not @b{nil} and the logical block is at a dynamic nesting depth of greater than @b{*print-level*} in logical blocks, ``@t{#}'' is printed. (This makes easy to write printing functions that provide full support for depth abbreviation.) If either of the three conditions above occurs, the indicated output is printed on @i{stream-symbol} and the body @i{forms} are skipped along with the printing of the @t{:prefix} and @t{:suffix}. (If the body @i{forms} are not to be responsible for printing a list, then the first two tests above can be turned off by supplying @b{nil} for the @i{object} argument.) In addition to the @i{object} argument of @b{pprint-logical-block}, the arguments of the standard printing functions (such as @b{write}, @b{print}, @b{prin1}, and @b{pprint}, as well as the arguments of the standard @i{format directives} such as @t{~A}, @t{~S}, (and @t{~W}) are all checked (when necessary) for circularity and sharing. However, such checking is not applied to the arguments of the functions @b{write-line}, @b{write-string}, and @b{write-char} or to the literal text output by @b{format}. A consequence of this is that you must use one of the latter functions if you want to print some literal text in the output that is not supposed to be checked for circularity or sharing. The body @i{forms} of a @b{pprint-logical-block} @i{form} must not perform any side-effects on the surrounding environment; for example, no @i{variables} must be assigned which have not been @i{bound} within its scope. The @b{pprint-logical-block} @i{macro} may be used regardless of the @i{value} of @b{*print-pretty*}. @subsubheading Affected By:: @b{*print-circle*}, @b{*print-level*}. @subsubheading Exceptional Situations:: An error of @i{type} @b{type-error} is signaled if any of the @t{:suffix}, @t{:prefix}, or @t{:per-line-prefix} is supplied but does not evaluate to a @i{string}. An error is signaled if @t{:prefix} and @t{:pre-line-prefix} are both used. @b{pprint-logical-block} and the @i{pretty printing stream} it creates have @i{dynamic extent}. The consequences are undefined if, outside of this extent, output is attempted to the @i{pretty printing stream} it creates. It is also unspecified what happens if, within this extent, any output is sent directly to the underlying destination stream. @subsubheading See Also:: @ref{pprint-pop} , @ref{pprint-exit-if-list-exhausted} , @ref{Tilde Less-Than-Sign-> Logical Block} @subsubheading Notes:: One reason for using the @b{pprint-logical-block} @i{macro} when the @i{value} of @b{*print-pretty*} is @b{nil} would be to allow it to perform checking for @i{dotted lists}, as well as (in conjunction with @b{pprint-pop}) checking for @b{*print-level*} or @b{*print-length*} being exceeded. Detection of circularity and sharing is supported by the @i{pretty printer} by in essence performing requested output twice. On the first pass, circularities and sharing are detected and the actual outputting of characters is suppressed. On the second pass, the appropriate ``@t{#@i{n}=}'' and ``@t{#@i{n}#}'' markers are inserted and characters are output. This is why the restriction on side-effects is necessary. Obeying this restriction is facilitated by using @b{pprint-pop}, instead of an ordinary @b{pop} when traversing a list being printed by the body @i{forms} of the @b{pprint-logical-block} @i{form}.) @node pprint-newline, pprint-pop, pprint-logical-block, Printer Dictionary @subsection pprint-newline [Function] @code{pprint-newline} @i{kind {&optional} stream} @result{} @i{@b{nil}} @subsubheading Arguments and Values:: @i{kind}---one of @t{:linear}, @t{:fill}, @t{:miser}, or @t{:mandatory}. @i{stream}---a @i{stream designator}. The default is @i{standard output}. @subsubheading Description:: If @i{stream} is a @i{pretty printing stream} and the @i{value} of @b{*print-pretty*} is @i{true}, a line break is inserted in the output when the appropriate condition below is satisfied; otherwise, @b{pprint-newline} has no effect. @i{Kind} specifies the style of conditional newline. This @i{parameter} is treated as follows: @table @asis @item @t{:linear} This specifies a ``linear-style'' @i{conditional newline}. @ITindex{linear-style conditional newline} A line break is inserted if and only if the immediately containing @i{section} cannot be printed on one line. The effect of this is that line breaks are either inserted at every linear-style conditional newline in a logical block or at none of them. @item @t{:miser} This specifies a ``miser-style'' @i{conditional newline}. @ITindex{miser-style conditional newline} A line break is inserted if and only if the immediately containing @i{section} cannot be printed on one line and miser style is in effect in the immediately containing logical block. The effect of this is that miser-style conditional newlines act like linear-style conditional newlines, but only when miser style is in effect. Miser style is in effect for a logical block if and only if the starting position of the logical block is less than or equal to @b{*print-miser-width*} @i{ems} from the right margin. @item @t{:fill} This specifies a ``fill-style'' @i{conditional newline}. @ITindex{fill-style conditional newline} A line break is inserted if and only if either (a) the following @i{section} cannot be printed on the end of the current line, (b) the preceding @i{section} was not printed on a single line, or (c) the immediately containing @i{section} cannot be printed on one line and miser style is in effect in the immediately containing logical block. If a logical block is broken up into a number of subsections by fill-style conditional newlines, the basic effect is that the logical block is printed with as many subsections as possible on each line. However, if miser style is in effect, fill-style conditional newlines act like linear-style conditional newlines. @item @t{:mandatory} This specifies a ``mandatory-style'' @i{conditional newline}. @ITindex{mandatory-style conditional newline} A line break is always inserted. This implies that none of the containing @i{sections} can be printed on a single line and will therefore trigger the insertion of line breaks at linear-style conditional newlines in these @i{sections}. @end table When a line break is inserted by any type of conditional newline, any blanks that immediately precede the conditional newline are omitted from the output and indentation is introduced at the beginning of the next line. By default, the indentation causes the following line to begin in the same horizontal position as the first character in the immediately containing logical block. (The indentation can be changed via @b{pprint-indent}.) There are a variety of ways unconditional newlines can be introduced into the output (@i{i.e.}, via @b{terpri} or by printing a string containing a newline character). As with mandatory conditional newlines, this prevents any of the containing @i{sections} from being printed on one line. In general, when an unconditional newline is encountered, it is printed out without suppression of the preceding blanks and without any indentation following it. However, if a per-line prefix has been specified (see @b{pprint-logical-block}), this prefix will always be printed no matter how a newline originates. @subsubheading Examples:: See @ref{Examples of using the Pretty Printer}. @subsubheading Side Effects:: Output to @i{stream}. @subsubheading Affected By:: @b{*print-pretty*}, @b{*print-miser*}. The presence of containing logical blocks. The placement of newlines and conditional newlines. @subsubheading Exceptional Situations:: An error of @i{type} @b{type-error} is signaled if @i{kind} is not one of @t{:linear}, @t{:fill}, @t{:miser}, or @t{:mandatory}. @subsubheading See Also:: @ref{Tilde Underscore-> Conditional Newline}, @ref{Examples of using the Pretty Printer} @node pprint-pop, pprint-tab, pprint-newline, Printer Dictionary @subsection pprint-pop [Local Macro] @subsubheading Syntax:: @code{pprint-pop} @i{<@i{no @i{arguments}}>} @result{} @i{object} @subsubheading Arguments and Values:: @i{object}---an @i{element} of the @i{list} being printed in the @i{lexically current logical block}, or @b{nil}. @subsubheading Description:: Pops one @i{element} from the @i{list} being printed in the @i{lexically current logical block}, obeying @b{*print-length*} and @b{*print-circle*} as described below. Each time @b{pprint-pop} is called, it pops the next value off the @i{list} passed to the @i{lexically current logical block} and returns it. However, before doing this, it performs three tests: @table @asis @item @t{*} If the remaining `list' is not a @i{list}, ``@t{. }'' is printed followed by the remaining `list.' (This makes it easier to write printing functions that are robust in the face of malformed arguments.) @item @t{*} If @b{*print-length*} is @i{non-nil}, and @b{pprint-pop} has already been called @b{*print-length*} times within the immediately containing logical block, ``@t{...}'' is printed. (This makes it easy to write printing functions that properly handle @b{*print-length*}.) @item @t{*} If @b{*print-circle*} is @i{non-nil}, and the remaining list is a circular (or shared) reference, then ``@t{. }'' is printed followed by an appropriate ``@t{#@i{n}#}'' marker. (This catches instances of @i{cdr} circularity and sharing in lists.) @end table If either of the three conditions above occurs, the indicated output is printed on the @i{pretty printing stream} created by the immediately containing @b{pprint-logical-block} and the execution of the immediately containing @b{pprint-logical-block} is terminated except for the printing of the suffix. If @b{pprint-logical-block} is given a `list' argument of @b{nil}---because it is not processing a list---@b{pprint-pop} can still be used to obtain support for @b{*print-length*}. In this situation, the first and third tests above are disabled and @b{pprint-pop} always returns @b{nil}. See @ref{Examples of using the Pretty Printer}---specifically, the @b{pprint-vector} example. Whether or not @b{pprint-pop} is @i{fbound} in the @i{global environment} is @i{implementation-dependent}; however, the restrictions on redefinition and @i{shadowing} of @b{pprint-pop} are the same as for @i{symbols} in the @t{COMMON-LISP} @i{package} which are @i{fbound} in the @i{global environment}. The consequences of attempting to use @b{pprint-pop} outside of @b{pprint-logical-block} are undefined. @subsubheading Side Effects:: Might cause output to the @i{pretty printing stream} associated with the lexically current logical block. @subsubheading Affected By:: @b{*print-length*}, @b{*print-circle*}. @subsubheading Exceptional Situations:: An error is signaled (either at macro expansion time or at run time) if a usage of @b{pprint-pop} occurs where there is no lexically containing @b{pprint-logical-block} @i{form}. The consequences are undefined if @b{pprint-pop} is executed outside of the @i{dynamic extent} of this @b{pprint-logical-block}. @subsubheading See Also:: @ref{pprint-exit-if-list-exhausted} , @ref{pprint-logical-block} . @subsubheading Notes:: It is frequently a good idea to call @b{pprint-exit-if-list-exhausted} before calling @b{pprint-pop}. @node pprint-tab, print-object, pprint-pop, Printer Dictionary @subsection pprint-tab [Function] @code{pprint-tab} @i{kind colnum colinc {&optional} stream} @result{} @i{@b{nil}} @subsubheading Arguments and Values:: @i{kind}---one of @t{:line}, @t{:section}, @t{:line-relative}, or @t{:section-relative}. @i{colnum}---a non-negative @i{integer}. @i{colinc}---a non-negative @i{integer}. @i{stream}---an @i{output} @i{stream designator}. @subsubheading Description:: Specifies tabbing to @i{stream} as performed by the standard @t{~T} format directive. If @i{stream} is a @i{pretty printing stream} and the @i{value} of @b{*print-pretty*} is @i{true}, tabbing is performed; otherwise, @b{pprint-tab} has no effect. The arguments @i{colnum} and @i{colinc} correspond to the two @i{parameters} to @t{~T} and are in terms of @i{ems}. The @i{kind} argument specifies the style of tabbing. It must be one of @t{:line} (tab as by @t{~T}), @t{:section} (tab as by @t{~:T}, but measuring horizontal positions relative to the start of the dynamically enclosing section), @t{:line-relative} (tab as by @t{~@@T}), or @t{:section-relative} (tab as by @t{~:@@T}, but measuring horizontal positions relative to the start of the dynamically enclosing section). @subsubheading Exceptional Situations:: An error is signaled if @i{kind} is not one of @t{:line}, @t{:section}, @t{:line-relative}, or @t{:section-relative}. @subsubheading See Also:: @ref{pprint-logical-block} @node print-object, print-unreadable-object, pprint-tab, Printer Dictionary @subsection print-object [Standard Generic Function] @subsubheading Syntax:: @code{print-object} @i{object stream} @result{} @i{object} @subsubheading Method Signatures:: @code{print-object} @i{@r{(}@i{object} standard-object@r{)} @i{stream}} @code{print-object} @i{@r{(}@i{object} structure-object@r{)} @i{stream}} @subsubheading Arguments and Values:: @i{object}---an @i{object}. @i{stream}---a @i{stream}. @subsubheading Description:: The @i{generic function} @b{print-object} writes the printed representation of @i{object} to @i{stream}. The @i{function} @b{print-object} is called by the @i{Lisp printer}; it should not be called by the user. Each implementation is required to provide a @i{method} on the @i{class} @b{standard-object} and on the @i{class} @b{structure-object}. In addition, each @i{implementation} must provide @i{methods} on enough other @i{classes} so as to ensure that there is always an applicable @i{method}. Implementations are free to add @i{methods} for other @i{classes}. Users may write @i{methods} for @b{print-object} for their own @i{classes} if they do not wish to inherit an @i{implementation-dependent} @i{method}. The @i{method} on the @i{class} @b{structure-object} prints the object in the default @t{#S} notation; see @ref{Printing Structures}. @i{Methods} on @b{print-object} are responsible for implementing their part of the semantics of the @i{printer control variables}, as follows: @table @asis @item @b{*print-readably*} All methods for @b{print-object} must obey @b{*print-readably*}. This includes both user-defined methods and @i{implementation-defined} methods. Readable printing of @i{structures} and @i{standard objects} is controlled by their @b{print-object} method, not by their @b{make-load-form} @i{method}. @i{Similarity} for these @i{objects} is application dependent and hence is defined to be whatever these @i{methods} do; see @ref{Similarity of Literal Objects}. @item @b{*print-escape*} Each @i{method} must implement @b{*print-escape*}. @item @b{*print-pretty*} The @i{method} may wish to perform specialized line breaking or other output conditional on the @i{value} of @b{*print-pretty*}. For further information, see (for example) the @i{macro} @b{pprint-fill}. See also @ref{Pretty Print Dispatch Tables} and @ref{Examples of using the Pretty Printer}. @item @b{*print-length*} @i{Methods} that produce output of indefinite length must obey @b{*print-length*}. For further information, see (for example) the @i{macros} @b{pprint-logical-block} and @b{pprint-pop}. See also @ref{Pretty Print Dispatch Tables} and @ref{Examples of using the Pretty Printer}. @item @b{*print-level*} The printer takes care of @b{*print-level*} automatically, provided that each @i{method} handles exactly one level of structure and calls @b{write} (or an equivalent @i{function}) recursively if there are more structural levels. The printer's decision of whether an @i{object} has components (and therefore should not be printed when the printing depth is not less than @b{*print-level*}) is @i{implementation-dependent}. In some implementations its @b{print-object} @i{method} is not called; in others the @i{method} is called, and the determination that the @i{object} has components is based on what it tries to write to the @i{stream}. @item @b{*print-circle*} When the @i{value} of @b{*print-circle*} is @i{true}, a user-defined @b{print-object} @i{method} can print @i{objects} to the supplied @i{stream} using @b{write}, @b{prin1}, @b{princ}, or @b{format} and expect circularities to be detected and printed using the @t{#@i{n}#} syntax. If a user-defined @b{print-object} @i{method} prints to a @i{stream} other than the one that was supplied, then circularity detection starts over for that @i{stream}. See @b{*print-circle*}. @item @b{*print-base*}, @b{*print-radix*}, @b{*print-case*}, @b{*print-gensym*}, and @b{*print-array*} These @i{printer control variables} apply to specific types of @i{objects} and are handled by the @i{methods} for those @i{objects}. @end table If these rules are not obeyed, the results are undefined. In general, the printer and the @b{print-object} methods should not rebind the print control variables as they operate recursively through the structure, but this is @i{implementation-dependent}. In some implementations the @i{stream} argument passed to a @b{print-object} @i{method} is not the original @i{stream}, but is an intermediate @i{stream} that implements part of the printer. @i{methods} should therefore not depend on the identity of this @i{stream}. @subsubheading See Also:: @ref{pprint-fill; pprint-linear; pprint-tabular} , @ref{pprint-logical-block} , @ref{pprint-pop} , @ref{write; prin1; print; pprint; princ} , @b{*print-readably*}, @b{*print-escape*}, @b{*print-pretty*}, @b{*print-length*}, @ref{Default Print-Object Methods}, @ref{Printing Structures}, @ref{Pretty Print Dispatch Tables}, @ref{Examples of using the Pretty Printer} @node print-unreadable-object, set-pprint-dispatch, print-object, Printer Dictionary @subsection print-unreadable-object [Macro] @code{print-unreadable-object} @i{@r{(}object stream {&key} type identity@r{)} @{@i{form}@}{*}} @result{} @i{@b{nil}} @subsubheading Arguments and Values:: @i{object}---an @i{object}; evaluated. @i{stream}--- a @i{stream designator}; evaluated. @i{type}---a @i{generalized boolean}; evaluated. @i{identity}---a @i{generalized boolean}; evaluated. @i{forms}---an @i{implicit progn}. @subsubheading Description:: Outputs a printed representation of @i{object} on @i{stream}, beginning with ``@t{#<}'' and ending with ``@t{>}''. Everything output to @i{stream} by the body @i{forms} is enclosed in the the angle brackets. If @i{type} is @i{true}, the output from @i{forms} is preceded by a brief description of the @i{object}'s @i{type} and a space character. If @i{identity} is @i{true}, the output from @i{forms} is followed by a space character and a representation of the @i{object}'s identity, typically a storage address. If either @i{type} or @i{identity} is not supplied, its value is @i{false}. It is valid to omit the body @i{forms}. If @i{type} and @i{identity} are both true and there are no body @i{forms}, only one space character separates the type and the identity. @subsubheading Examples:: ;; Note that in this example, the precise form of the output ;; is @i{implementation-dependent}. @example (defmethod print-object ((obj airplane) stream) (print-unreadable-object (obj stream :type t :identity t) (princ (tail-number obj) stream))) (prin1-to-string my-airplane) @result{} "#" @i{OR}@result{} "#" @end example @subsubheading Exceptional Situations:: If @b{*print-readably*} is @i{true}, @b{print-unreadable-object} signals an error of @i{type} @b{print-not-readable} without printing anything. @node set-pprint-dispatch, write, print-unreadable-object, Printer Dictionary @subsection set-pprint-dispatch [Function] @code{set-pprint-dispatch} @i{type-specifier function {&optional} priority table} @result{} @i{@b{nil}} @subsubheading Arguments and Values:: @i{type-specifier}---a @i{type specifier}. @i{function}---a @i{function}, a @i{function name}, or @b{nil}. @i{priority}---a @i{real}. The default is @t{0}. @i{table}---a @i{pprint dispatch table}. The default is the @i{value} of @b{*print-pprint-dispatch*}. @subsubheading Description:: Installs an entry into the @i{pprint dispatch table} which is @i{table}. @i{Type-specifier} is the @i{key} of the entry. The first action of @b{set-pprint-dispatch} is to remove any pre-existing entry associated with @i{type-specifier}. This guarantees that there will never be two entries associated with the same @i{type specifier} in a given @i{pprint dispatch table}. Equality of @i{type specifiers} is tested by @b{equal}. Two values are associated with each @i{type specifier} in a @i{pprint dispatch table}: a @i{function} and a @i{priority}. The @i{function} must accept two arguments: the @i{stream} to which output is sent and the @i{object} to be printed. The @i{function} should @i{pretty print} the @i{object} to the @i{stream}. The @i{function} can assume that object satisfies the @i{type} given by @i{type-specifier}. The @i{function} must obey @b{*print-readably*}. Any values returned by the @i{function} are ignored. @i{Priority} is a priority to resolve conflicts when an object matches more than one entry. It is permissible for @i{function} to be @b{nil}. In this situation, there will be no @i{type-specifier} entry in @i{table} after @b{set-pprint-dispatch} returns. @subsubheading Exceptional Situations:: An error is signaled if @i{priority} is not a @i{real}. @subsubheading Notes:: Since @i{pprint dispatch tables} are often used to control the pretty printing of Lisp code, it is common for the @i{type-specifier} to be an @i{expression} of the form @example (cons @i{car-type} @i{cdr-type}) @end example This signifies that the corresponding object must be a cons cell whose @i{car} matches the @i{type specifier} @i{car-type} and whose @i{cdr} matches the @i{type specifier} @i{cdr-type}. The @i{cdr-type} can be omitted in which case it defaults to @b{t}. @node write, write-to-string, set-pprint-dispatch, Printer Dictionary @subsection write, prin1, print, pprint, princ [Function] @code{write} @i{@i{object} {&key} \writekeys{stream}}@* @result{} @i{object} @code{prin} @i{1} @result{} @i{object {&optional} output-stream} {object} @code{princ} @i{object {&optional} output-stream} @result{} @i{object} @code{print} @i{object {&optional} output-stream} @result{} @i{object} @code{pprint} @i{object {&optional} output-stream} @result{} @i{<@i{no @i{values}}>} @subsubheading Arguments and Values:: @i{object}---an @i{object}. @i{output-stream}---an @i{output} @i{stream designator}. The default is @i{standard output}. \writekeydescriptions{@i{stream}---an @i{output} @i{stream designator}. The default is @i{standard output}.} @subsubheading Description:: @b{write}, @b{prin1}, @b{princ}, @b{print}, and @b{pprint} write the printed representation of @i{object} to @i{output-stream}. @b{write} is the general entry point to the @i{Lisp printer}. For each explicitly supplied @i{keyword parameter} named in Figure 22--6, the corresponding @i{printer control variable} is dynamically bound to its @i{value} while printing goes on; for each @i{keyword parameter} in Figure 22--6 that is not explicitly supplied, the value of the corresponding @i{printer control variable} is the same as it was at the time @b{write} was invoked. Once the appropriate @i{bindings} are @i{established}, the @i{object} is output by the @i{Lisp printer}. @group @noindent @w{ Parameter Corresponding Dynamic Variable } @w{ @i{array} @b{*print-array*} } @w{ @i{base} @b{*print-base*} } @w{ @i{case} @b{*print-case*} } @w{ @i{circle} @b{*print-circle*} } @w{ @i{escape} @b{*print-escape*} } @w{ @i{gensym} @b{*print-gensym*} } @w{ @i{length} @b{*print-length*} } @w{ @i{level} @b{*print-level*} } @w{ @i{lines} @b{*print-lines*} } @w{ @i{miser-width} @b{*print-miser-width*} } @w{ @i{pprint-dispatch} @b{*print-pprint-dispatch*} } @w{ @i{pretty} @b{*print-pretty*} } @w{ @i{radix} @b{*print-radix*} } @w{ @i{readably} @b{*print-readably*} } @w{ @i{right-margin} @b{*print-right-margin*} } @noindent @w{ Figure 22--6: Argument correspondences for the WRITE function.} @end group @b{prin1}, @b{princ}, @b{print}, and @b{pprint} implicitly @i{bind} certain print parameters to particular values. The remaining parameter values are taken from @b{*print-array*}, @b{*print-base*}, @b{*print-case*}, @b{*print-circle*}, @b{*print-escape*}, @b{*print-gensym*}, @b{*print-length*}, @b{*print-level*}, @b{*print-lines*}, @b{*print-miser-width*}, @b{*print-pprint-dispatch*}, @b{*print-pretty*}, @b{*print-radix*}, and @b{*print-right-margin*}. @b{prin1} produces output suitable for input to @b{read}. It binds @b{*print-escape*} to @i{true}. @b{princ} is just like @b{prin1} except that the output has no @i{escape} @i{characters}. It binds @b{*print-escape*} to @i{false} and @b{*print-readably*} to @i{false}. The general rule is that output from @b{princ} is intended to look good to people, while output from @b{prin1} is intended to be acceptable to @b{read}. @b{print} is just like @b{prin1} except that the printed representation of @i{object} is preceded by a newline and followed by a space. @b{pprint} is just like @b{print} except that the trailing space is omitted and @i{object} is printed with the @b{*print-pretty*} flag @i{non-nil} to produce pretty output. @i{Output-stream} specifies the @i{stream} to which output is to be sent. @subsubheading Affected By:: @b{*standard-output*}, @b{*terminal-io*}, @b{*print-escape*}, @b{*print-radix*}, @b{*print-base*}, @b{*print-circle*}, @b{*print-pretty*}, @b{*print-level*}, @b{*print-length*}, @b{*print-case*}, @b{*print-gensym*}, @b{*print-array*}, @b{*read-default-float-format*}. @subsubheading See Also:: @ref{readtable-case} , @ref{FORMAT Printer Operations} @subsubheading Notes:: The @i{functions} @b{prin1} and @b{print} do not bind @b{*print-readably*}. @example (prin1 object output-stream) @equiv{} (write object :stream output-stream :escape t) @end example @example (princ object output-stream) @equiv{} (write object stream output-stream :escape nil :readably nil) @end example @example (print object output-stream) @equiv{} (progn (terpri output-stream) (write object :stream output-stream :escape t) (write-char #\space output-stream)) @end example @example (pprint object output-stream) @equiv{} (write object :stream output-stream :escape t :pretty t) @end example @node write-to-string, *print-array*, write, Printer Dictionary @subsection write-to-string, prin1-to-string, princ-to-string [Function] @code{write-to-string} @i{object {&key} \writekeys{}}@* @result{} @i{string} @code{prin} @i{1} @result{} @i{-to-string} {object} {string} @code{princ-to-string} @i{object} @result{} @i{string} @subsubheading Arguments and Values:: @i{object}---an @i{object}. \writekeydescriptions{} @i{string}---a @i{string}. @subsubheading Description:: @b{write-to-string}, @b{prin1-to-string}, and @b{princ-to-string} are used to create a @i{string} consisting of the printed representation of @i{object}. @i{Object} is effectively printed as if by @b{write}, @b{prin1}, or @b{princ}, respectively, and the @i{characters} that would be output are made into a @i{string}. @b{write-to-string} is the general output function. It has the ability to specify all the parameters applicable to the printing of @i{object}. @b{prin1-to-string} acts like @b{write-to-string} with @t{:escape t}, that is, escape characters are written where appropriate. @b{princ-to-string} acts like @b{write-to-string} with @t{:escape nil :readably nil}. Thus no @i{escape} @i{characters} are written. All other keywords that would be specified to @b{write-to-string} are default values when @b{prin1-to-string} or @b{princ-to-string} is invoked. The meanings and defaults for the keyword arguments to @b{write-to-string} are the same as those for @b{write}. @subsubheading Examples:: @example (prin1-to-string "abc") @result{} "\"abc\"" (princ-to-string "abc") @result{} "abc" @end example @subsubheading Affected By:: @b{*print-escape*}, @b{*print-radix*}, @b{*print-base*}, @b{*print-circle*}, @b{*print-pretty*}, @b{*print-level*}, @b{*print-length*}, @b{*print-case*}, @b{*print-gensym*}, @b{*print-array*}, @b{*read-default-float-format*}. @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} @subsubheading Notes:: @example (write-to-string @i{object} @{@i{key} @i{argument}@}{*}) @equiv{} (with-output-to-string (#1=#:string-stream) (write object :stream #1# @{@i{key} @i{argument}@}{*})) (princ-to-string @i{object}) @equiv{} (with-output-to-string (string-stream) (princ @i{object} string-stream)) (prin1-to-string @i{object}) @equiv{} (with-output-to-string (string-stream) (prin1 @i{object} string-stream)) @end example @node *print-array*, *print-base*, write-to-string, Printer Dictionary @subsection *print-array* [Variable] @subsubheading Value Type:: a @i{generalized boolean}. @subsubheading Initial Value:: @i{implementation-dependent}. @subsubheading Description:: Controls the format in which @i{arrays} are printed. If it is @i{false}, the contents of @i{arrays} other than @i{strings} are never printed. Instead, @i{arrays} are printed in a concise form using @t{#<} that gives enough information for the user to be able to identify the @i{array}, but does not include the entire @i{array} contents. If it is @i{true}, non-@i{string} @i{arrays} are printed using @t{#(...)}, @t{#*}, or @t{#nA} syntax. @subsubheading Affected By:: The @i{implementation}. @subsubheading See Also:: @ref{Sharpsign Left-Parenthesis}, @ref{Sharpsign Less-Than-Sign} @node *print-base*, *print-case*, *print-array*, Printer Dictionary @subsection *print-base*, *print-radix* [Variable] @subsubheading Value Type:: @b{*print-base*}---a @i{radix}. @b{*print-radix*}---a @i{generalized boolean}. @subsubheading Initial Value:: The initial @i{value} of @b{*print-base*} is @t{10}. The initial @i{value} of @b{*print-radix*} is @i{false}. @subsubheading Description:: @b{*print-base*} and @b{*print-radix*} control the printing of @i{rationals}. The @i{value} of @b{*print-base*} is called the @i{current output base} @IGindex{current output base} . The @i{value} of @b{*print-base*} is the @i{radix} in which the printer will print @i{rationals}. For radices above @t{10}, letters of the alphabet are used to represent digits above @t{9}. If the @i{value} of @b{*print-radix*} is @i{true}, the printer will print a radix specifier to indicate the @i{radix} in which it is printing a @i{rational} number. The radix specifier is always printed using lowercase letters. If @b{*print-base*} is @t{2}, @t{8}, or @t{16}, then the radix specifier used is @t{#b}, @t{#o}, or @t{#x}, respectively. For @i{integers}, base ten is indicated by a trailing decimal point instead of a leading radix specifier; for @i{ratios}, @t{#10r} is used. @subsubheading Examples:: @example (let ((*print-base* 24.) (*print-radix* t)) (print 23.)) @t{ |> } #24rN @result{} 23 (setq *print-base* 10) @result{} 10 (setq *print-radix* nil) @result{} NIL (dotimes (i 35) (let ((*print-base* (+ i 2))) ;print the decimal number 40 (write 40) ;in each base from 2 to 36 (if (zerop (mod i 10)) (terpri) (format t " ")))) @t{ |> } 101000 @t{ |> } 1111 220 130 104 55 50 44 40 37 34 @t{ |> } 31 2C 2A 28 26 24 22 20 1J 1I @t{ |> } 1H 1G 1F 1E 1D 1C 1B 1A 19 18 @t{ |> } 17 16 15 14 @result{} NIL (dolist (pb '(2 3 8 10 16)) (let ((*print-radix* t) ;print the integer 10 and (*print-base* pb)) ;the ratio 1/10 in bases 2, (format t "~&~S ~S~ @t{ |> } #b1010 #b1/1010 @t{ |> } #3r101 #3r1/101 @t{ |> } #o12 #o1/12 @t{ |> } 10. #10r1/10 @t{ |> } #xA #x1/A @result{} NIL @end example @subsubheading Affected By:: Might be @i{bound} by @b{format}, and @b{write}, @b{write-to-string}. @subsubheading See Also:: @ref{format} , @ref{write; prin1; print; pprint; princ} , @ref{write-to-string; prin1-to-string; princ-to-string} @node *print-case*, *print-circle*, *print-base*, Printer Dictionary @subsection *print-case* [Variable] @subsubheading Value Type:: One of the @i{symbols} @t{:upcase}, @t{:downcase}, or @t{:capitalize}. @subsubheading Initial Value:: The @i{symbol} @t{:upcase}. @subsubheading Description:: The @i{value} of @b{*print-case*} controls the case (upper, lower, or mixed) in which to print any uppercase characters in the names of @i{symbols} when vertical-bar syntax is not used. @b{*print-case*} has an effect at all times when the @i{value} of @b{*print-escape*} is @i{false}. @b{*print-case*} also has an effect when the @i{value} of @b{*print-escape*} is @i{true} unless inside an escape context (@i{i.e.}, unless between @i{vertical-bars} or after a @i{slash}). @subsubheading Examples:: @example (defun test-print-case () (dolist (*print-case* '(:upcase :downcase :capitalize)) (format t "~&~S ~S~ @result{} TEST-PC ;; Although the choice of which characters to escape is specified by ;; *PRINT-CASE*, the choice of how to escape those characters ;; (i.e., whether single escapes or multiple escapes are used) ;; is implementation-dependent. The examples here show two of the ;; many valid ways in which escaping might appear. (test-print-case) ;Implementation A @t{ |> } THIS-AND-THAT |And-something-elSE| @t{ |> } this-and-that a\n\d-\s\o\m\e\t\h\i\n\g-\e\lse @t{ |> } This-And-That A\n\d-\s\o\m\e\t\h\i\n\g-\e\lse @result{} NIL (test-print-case) ;Implementation B @t{ |> } THIS-AND-THAT |And-something-elSE| @t{ |> } this-and-that a|nd-something-el|se @t{ |> } This-And-That A|nd-something-el|se @result{} NIL @end example @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} @subsubheading Notes:: @b{read} normally converts lowercase characters appearing in @i{symbols} to corresponding uppercase characters, so that internally print names normally contain only uppercase characters. If @b{*print-escape*} is @i{true}, lowercase characters in the @i{name} of a @i{symbol} are always printed in lowercase, and are preceded by a single escape character or enclosed by multiple escape characters; uppercase characters in the @i{name} of a @i{symbol} are printed in upper case, in lower case, or in mixed case so as to capitalize words, according to the value of @b{*print-case*}. The convention for what constitutes a ``word'' is the same as for @b{string-capitalize}. @node *print-circle*, *print-escape*, *print-case*, Printer Dictionary @subsection *print-circle* [Variable] @subsubheading Value Type:: a @i{generalized boolean}. @subsubheading Initial Value:: @i{false}. @subsubheading Description:: Controls the attempt to detect circularity and sharing in an @i{object} being printed. If @i{false}, the printing process merely proceeds by recursive descent without attempting to detect circularity and sharing. If @i{true}, the printer will endeavor to detect cycles and sharing in the structure to be printed, and to use @t{#@i{n}=} and @t{#@i{n}#} syntax to indicate the circularities or shared components. If @i{true}, a user-defined @b{print-object} @i{method} can print @i{objects} to the supplied @i{stream} using @b{write}, @b{prin1}, @b{princ}, or @b{format} and expect circularities and sharing to be detected and printed using the @t{#@i{n}#} syntax. If a user-defined @b{print-object} @i{method} prints to a @i{stream} other than the one that was supplied, then circularity detection starts over for that @i{stream}. Note that implementations should not use @t{#@i{n}#} notation when the @i{Lisp reader} would automatically assure sharing without it (@i{e.g.}, as happens with @i{interned} @i{symbols}). @subsubheading Examples:: @example (let ((a (list 1 2 3))) (setf (cdddr a) a) (let ((*print-circle* t)) (write a) :done)) @t{ |> } #1=(1 2 3 . #1#) @result{} :DONE @end example @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} @subsubheading Notes:: An attempt to print a circular structure with @b{*print-circle*} set to @b{nil} may lead to looping behavior and failure to terminate. @node *print-escape*, *print-gensym*, *print-circle*, Printer Dictionary @subsection *print-escape* [Variable] @subsubheading Value Type:: a @i{generalized boolean}. @subsubheading Initial Value:: @i{true}. @subsubheading Description:: If @i{false}, escape characters and @i{package prefixes} are not output when an expression is printed. If @i{true}, an attempt is made to print an @i{expression} in such a way that it can be read again to produce an @b{equal} @i{expression}. (This is only a guideline; not a requirement. See @b{*print-readably*}.) For more specific details of how the @i{value} of @b{*print-escape*} affects the printing of certain @i{types}, see @ref{Default Print-Object Methods}. @subsubheading Examples:: @example (let ((*print-escape* t)) (write #\a)) @t{ |> } #\a @result{} #\a (let ((*print-escape* nil)) (write #\a)) @t{ |> } a @result{} #\a @end example @subsubheading Affected By:: @b{princ}, @b{prin1}, @b{format} @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} , @ref{readtable-case} @subsubheading Notes:: @b{princ} effectively binds @b{*print-escape*} to @i{false}. @b{prin1} effectively binds @b{*print-escape*} to @i{true}. @node *print-gensym*, *print-level*, *print-escape*, Printer Dictionary @subsection *print-gensym* [Variable] @subsubheading Value Type:: a @i{generalized boolean}. @subsubheading Initial Value:: @i{true}. @subsubheading Description:: Controls whether the prefix ``@t{#:}'' is printed before @i{apparently uninterned} @i{symbols}. The prefix is printed before such @i{symbols} if and only if the @i{value} of @b{*print-gensym*} is @i{true}. @subsubheading Examples:: @example (let ((*print-gensym* nil)) (print (gensym))) @t{ |> } G6040 @result{} #:G6040 @end example @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} , @b{*print-escape*} @node *print-level*, *print-lines*, *print-gensym*, Printer Dictionary @subsection *print-level*, *print-length* [Variable] @subsubheading Value Type:: a non-negative @i{integer}, or @b{nil}. @subsubheading Initial Value:: @b{nil}. @subsubheading Description:: @b{*print-level*} controls how many levels deep a nested @i{object} will print. If it is @i{false}, then no control is exercised. Otherwise, it is an @i{integer} indicating the maximum level to be printed. An @i{object} to be printed is at level @t{0}; its components (as of a @i{list} or @i{vector}) are at level @t{1}; and so on. If an @i{object} to be recursively printed has components and is at a level equal to or greater than the @i{value} of @b{*print-level*}, then the @i{object} is printed as ``@t{#}''. @b{*print-length*} controls how many elements at a given level are printed. If it is @i{false}, there is no limit to the number of components printed. Otherwise, it is an @i{integer} indicating the maximum number of @i{elements} of an @i{object} to be printed. If exceeded, the printer will print ``@t{...}'' in place of the other @i{elements}. In the case of a @i{dotted list}, if the @i{list} contains exactly as many @i{elements} as the @i{value} of @b{*print-length*}, the terminating @i{atom} is printed rather than printing ``@t{...}'' @b{*print-level*} and @b{*print-length*} affect the printing of an any @i{object} printed with a list-like syntax. They do not affect the printing of @i{symbols}, @i{strings}, and @i{bit vectors}. @subsubheading Examples:: @example (setq a '(1 (2 (3 (4 (5 (6))))))) @result{} (1 (2 (3 (4 (5 (6)))))) (dotimes (i 8) (let ((*print-level* i)) (format t "~&~D -- ~S~ @t{ |> } 0 -- # @t{ |> } 1 -- (1 #) @t{ |> } 2 -- (1 (2 #)) @t{ |> } 3 -- (1 (2 (3 #))) @t{ |> } 4 -- (1 (2 (3 (4 #)))) @t{ |> } 5 -- (1 (2 (3 (4 (5 #))))) @t{ |> } 6 -- (1 (2 (3 (4 (5 (6)))))) @t{ |> } 7 -- (1 (2 (3 (4 (5 (6)))))) @result{} NIL (setq a '(1 2 3 4 5 6)) @result{} (1 2 3 4 5 6) (dotimes (i 7) (let ((*print-length* i)) (format t "~&~D -- ~S~ @t{ |> } 0 -- (...) @t{ |> } 1 -- (1 ...) @t{ |> } 2 -- (1 2 ...) @t{ |> } 3 -- (1 2 3 ...) @t{ |> } 4 -- (1 2 3 4 ...) @t{ |> } 5 -- (1 2 3 4 5 6) @t{ |> } 6 -- (1 2 3 4 5 6) @result{} NIL (dolist (level-length '((0 1) (1 1) (1 2) (1 3) (1 4) (2 1) (2 2) (2 3) (3 2) (3 3) (3 4))) (let ((*print-level* (first level-length)) (*print-length* (second level-length))) (format t "~&~D ~D -- ~S~ *print-level* *print-length* '(if (member x y) (+ (car x) 3) '(foo . #(a b c d "Baz")))))) @t{ |> } 0 1 -- # @t{ |> } 1 1 -- (IF ...) @t{ |> } 1 2 -- (IF # ...) @t{ |> } 1 3 -- (IF # # ...) @t{ |> } 1 4 -- (IF # # #) @t{ |> } 2 1 -- (IF ...) @t{ |> } 2 2 -- (IF (MEMBER X ...) ...) @t{ |> } 2 3 -- (IF (MEMBER X Y) (+ # 3) ...) @t{ |> } 3 2 -- (IF (MEMBER X ...) ...) @t{ |> } 3 3 -- (IF (MEMBER X Y) (+ (CAR X) 3) ...) @t{ |> } 3 4 -- (IF (MEMBER X Y) (+ (CAR X) 3) '(FOO . #(A B C D ...))) @result{} NIL @end example @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} @node *print-lines*, *print-miser-width*, *print-level*, Printer Dictionary @subsection *print-lines* [Variable] @subsubheading Value Type:: a non-negative @i{integer}, or @b{nil}. @subsubheading Initial Value:: @b{nil}. @subsubheading Description:: When the @i{value} of @b{*print-lines*} is other than @b{nil}, it is a limit on the number of output lines produced when something is pretty printed. If an attempt is made to go beyond that many lines, ``@t{..}'' is printed at the end of the last line followed by all of the suffixes (closing delimiters) that are pending to be printed. @subsubheading Examples:: @example (let ((*print-right-margin* 25) (*print-lines* 3)) (pprint '(progn (setq a 1 b 2 c 3 d 4)))) @t{ |> } (PROGN (SETQ A 1 @t{ |> } B 2 @t{ |> } C 3 ..)) @result{} <@i{no @i{values}}> @end example @subsubheading Notes:: The ``@t{..}'' notation is intentionally different than the ``@t{...}'' notation used for level abbreviation, so that the two different situations can be visually distinguished. This notation is used to increase the likelihood that the @i{Lisp reader} will signal an error if an attempt is later made to read the abbreviated output. Note however that if the truncation occurs in a @i{string}, as in @t{"This string has been trunc.."}, the problem situation cannot be detected later and no such error will be signaled. @node *print-miser-width*, *print-pprint-dispatch*, *print-lines*, Printer Dictionary @subsection *print-miser-width* [Variable] @subsubheading Value Type:: a non-negative @i{integer}, or @b{nil}. @subsubheading Initial Value:: @i{implementation-dependent} @subsubheading Description:: If it is not @b{nil}, the @i{pretty printer} switches to a compact style of output (called miser style) whenever the width available for printing a substructure is less than or equal to this many @i{ems}. @node *print-pprint-dispatch*, *print-pretty*, *print-miser-width*, Printer Dictionary @subsection *print-pprint-dispatch* [Variable] @subsubheading Value Type:: a @i{pprint dispatch table}. @subsubheading Initial Value:: @i{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 @b{set-pprint-dispatch}, so that the initial contents of any entry can be overridden. @subsubheading Description:: The @i{pprint dispatch table} which currently controls the @i{pretty printer}. @subsubheading See Also:: @b{*print-pretty*}, @ref{Pretty Print Dispatch Tables} @subsubheading Notes:: The intent is that the initial @i{value} of this @i{variable} should cause `traditional' @i{pretty printing} of @i{code}. In general, however, you can put a value in @b{*print-pprint-dispatch*} that makes pretty-printed output look exactly like non-pretty-printed output. Setting @b{*print-pretty*} to @i{true} just causes the functions contained in the @i{current pprint dispatch table} to have priority over normal @b{print-object} methods; it has no magic way of enforcing that those functions actually produce pretty output. For details, see @ref{Pretty Print Dispatch Tables}. @node *print-pretty*, *print-readably*, *print-pprint-dispatch*, Printer Dictionary @subsection *print-pretty* [Variable] @subsubheading Value Type:: a @i{generalized boolean}. @subsubheading Initial Value:: @i{implementation-dependent}. @subsubheading Description:: Controls whether the @i{Lisp printer} calls the @i{pretty printer}. If it is @i{false}, the @i{pretty printer} is not used and a minimum of @i{whitespace}_1 is output when printing an expression. If it is @i{true}, the @i{pretty printer} is used, and the @i{Lisp printer} will endeavor to insert extra @i{whitespace}_1 where appropriate to make @i{expressions} more readable. @b{*print-pretty*} has an effect even when the @i{value} of @b{*print-escape*} is @i{false}. @subsubheading Examples:: @example (setq *print-pretty* 'nil) @result{} NIL (progn (write '(let ((a 1) (b 2) (c 3)) (+ a b c))) nil) @t{ |> } (LET ((A 1) (B 2) (C 3)) (+ A B C)) @result{} NIL (let ((*print-pretty* t)) (progn (write '(let ((a 1) (b 2) (c 3)) (+ a b c))) nil)) @t{ |> } (LET ((A 1) @t{ |> } (B 2) @t{ |> } (C 3)) @t{ |> } (+ A B C)) @result{} NIL ;; Note that the first two expressions printed by this next form ;; differ from the second two only in whether escape characters are printed. ;; In all four cases, extra whitespace is inserted by the pretty printer. (flet ((test (x) (let ((*print-pretty* t)) (print x) (format t "~ (terpri) (princ x) (princ " ") (format t "~ (test '#'(lambda () (list "a" #@b{'c} #'d)))) @t{ |> } #'(LAMBDA () @t{ |> } (LIST "a" #@b{'C} #'D)) @t{ |> } #'(LAMBDA () @t{ |> } (LIST "a" #@b{'C} #'D)) @t{ |> } #'(LAMBDA () @t{ |> } (LIST a b 'C #'D)) @t{ |> } #'(LAMBDA () @t{ |> } (LIST a b 'C #'D)) @result{} NIL @end example @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} @node *print-readably*, *print-right-margin*, *print-pretty*, Printer Dictionary @subsection *print-readably* [Variable] @subsubheading Value Type:: a @i{generalized boolean}. @subsubheading Initial Value:: @i{false}. @subsubheading Description:: If @b{*print-readably*} is @i{true}, some special rules for printing @i{objects} go into effect. Specifically, printing any @i{object} O_1 produces a printed representation that, when seen by the @i{Lisp reader} while the @i{standard readtable} is in effect, will produce an @i{object} O_2 that is @i{similar} to O_1. The printed representation produced might or might not be the same as the printed representation produced when @b{*print-readably*} is @i{false}. If printing an @i{object} @i{readably} is not possible, an error of @i{type} @b{print-not-readable} is signaled rather than using a syntax (@i{e.g.}, the ``@t{#<}'' syntax) that would not be readable by the same @i{implementation}. If the @i{value} of some other @i{printer control variable} is such that these requirements would be violated, the @i{value} of that other @i{variable} is ignored. Specifically, if @b{*print-readably*} is @i{true}, printing proceeds as if @b{*print-escape*}, @b{*print-array*}, and @b{*print-gensym*} were also @i{true}, and as if @b{*print-length*}, @b{*print-level*}, and @b{*print-lines*} were @i{false}. If @b{*print-readably*} is @i{false}, the normal rules for printing and the normal interpretations of other @i{printer control variables} are in effect. Individual @i{methods} for @b{print-object}, including user-defined @i{methods}, are responsible for implementing these requirements. If @b{*read-eval*} is @i{false} and @b{*print-readably*} is @i{true}, any such method that would output a reference to the ``@t{#.}'' @i{reader macro} will either output something else or will signal an error (as described above). @subsubheading Examples:: @example (let ((x (list "a" '\a (gensym) '((a (b (c))) d e f g))) (*print-escape* nil) (*print-gensym* nil) (*print-level* 3) (*print-length* 3)) (write x) (let ((*print-readably* t)) (terpri) (write x) :done)) @t{ |> } (a a G4581 ((A #) D E ...)) @t{ |> } ("a" |a| #:G4581 ((A (B (C))) D E F G)) @result{} :DONE ;; This is setup code is shared between the examples ;; of three hypothetical implementations which follow. (setq table (make-hash-table)) @result{} # (setf (gethash table 1) 'one) @result{} ONE (setf (gethash table 2) 'two) @result{} TWO ;; Implementation A (let ((*print-readably* t)) (print table)) Error: Can't print # readably. ;; Implementation B ;; No standardized #S notation for hash tables is defined, ;; but there might be an implementation-defined notation. (let ((*print-readably* t)) (print table)) @t{ |> } #S(HASH-TABLE :TEST EQL :SIZE 120 :CONTENTS (1 ONE 2 TWO)) @result{} # ;; Implementation C ;; Note that #. notation can only be used if *READ-EVAL* is true. ;; If *READ-EVAL* were false, this same implementation might have to ;; signal an error unless it had yet another printing strategy to fall ;; back on. (let ((*print-readably* t)) (print table)) @t{ |> } #.(LET ((HASH-TABLE (MAKE-HASH-TABLE))) @t{ |> } (SETF (GETHASH 1 HASH-TABLE) ONE) @t{ |> } (SETF (GETHASH 2 HASH-TABLE) TWO) @t{ |> } HASH-TABLE) @result{} # @end example @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} , @ref{print-unreadable-object} @subsubheading Notes:: The rules for ``@i{similarity}'' imply that @t{#A} or @t{#(} syntax cannot be used for @i{arrays} of @i{element type} other than @b{t}. An implementation will have to use another syntax or signal an error of @i{type} @b{print-not-readable}. @node *print-right-margin*, print-not-readable, *print-readably*, Printer Dictionary @subsection *print-right-margin* [Variable] @subsubheading Value Type:: a non-negative @i{integer}, or @b{nil}. @subsubheading Initial Value:: @b{nil}. @subsubheading Description:: If it is @i{non-nil}, it specifies the right margin (as @i{integer} number of @i{ems}) to use when the @i{pretty printer} is making layout decisions. If it is @b{nil}, the right margin is taken to be the maximum line length such that output can be displayed without wraparound or truncation. If this cannot be determined, an @i{implementation-dependent} value is used. @subsubheading Notes:: This measure is in units of @i{ems} in order to be compatible with @i{implementation-defined} variable-width fonts while still not requiring the language to provide support for fonts. @node print-not-readable, print-not-readable-object, *print-right-margin*, Printer Dictionary @subsection print-not-readable [Condition Type] @subsubheading Class Precedence List:: @b{print-not-readable}, @b{error}, @b{serious-condition}, @b{condition}, @b{t} @subsubheading Description:: The @i{type} @b{print-not-readable} consists of error conditions that occur during output while @b{*print-readably*} is @i{true}, as a result of attempting to write a printed representation with the @i{Lisp printer} that would not be correctly read back with the @i{Lisp reader}. The object which could not be printed is initialized by the @t{:object} initialization argument to @b{make-condition}, and is @i{accessed} by the @i{function} @b{print-not-readable-object}. @subsubheading See Also:: @ref{print-not-readable-object} @node print-not-readable-object, format, print-not-readable, Printer Dictionary @subsection print-not-readable-object [Function] @code{print-not-readable-object} @i{condition} @result{} @i{object} @subsubheading Arguments and Values:: @i{condition}---a @i{condition} of @i{type} @b{print-not-readable}. @i{object}---an @i{object}. @subsubheading Description:: Returns the @i{object} that could not be printed readably in the situation represented by @i{condition}. @subsubheading See Also:: @b{print-not-readable}, @ref{Conditions} @node format, , print-not-readable-object, Printer Dictionary @subsection format [Function] @code{format} @i{destination control-string {&rest} args} @result{} @i{result} @subsubheading Arguments and Values:: @i{destination}---@b{nil}, @b{t}, a @i{stream}, or a @i{string} with a @i{fill pointer}. @i{control-string}---a @i{format control}. @i{args}---@i{format arguments} for @i{control-string}. @i{result}---if @i{destination} is @i{non-nil}, then @b{nil}; otherwise, a @i{string}. @subsubheading Description:: @b{format} produces formatted output by outputting the characters of @i{control-string} and observing that a @i{tilde} introduces a directive. The character after the tilde, possibly preceded by prefix parameters and modifiers, specifies what kind of formatting is desired. Most directives use one or more elements of @i{args} to create their output. If @i{destination} is a @i{string}, a @i{stream}, or @b{t}, then the @i{result} is @b{nil}. Otherwise, the @i{result} is a @i{string} containing the `output.' @b{format} is useful for producing nicely formatted text, producing good-looking messages, and so on. @b{format} can generate and return a @i{string} or output to @i{destination}. For details on how the @i{control-string} is interpreted, see @ref{Formatted Output}. @subsubheading Affected By:: @b{*standard-output*}, @b{*print-escape*}, @b{*print-radix*}, @b{*print-base*}, @b{*print-circle*}, @b{*print-pretty*}, @b{*print-level*}, @b{*print-length*}, @b{*print-case*}, @b{*print-gensym*}, @b{*print-array*}. @subsubheading Exceptional Situations:: If @i{destination} is a @i{string} with a @i{fill pointer}, the consequences are undefined if destructive modifications are performed directly on the @i{string} during the @i{dynamic extent} of the call. @subsubheading See Also:: @ref{write; prin1; print; pprint; princ} , @ref{Documentation of Implementation-Defined Scripts} @c end of including dict-printer @c %**end of chapter