Skip to main content

pprint-indent

pprint-indent Function

Syntax:

pprint-indent relative-to n &optional stream → nil

Arguments and Values:

relative-to—either :block or :current.

n—a real.

stream—an output stream designator . The default is standard output.

Description:

pprint-indent specifies the indentation to use in a logical block on stream. If stream is a pretty printing stream and the value of *print-pretty* is true, pprint-indent sets the indentation in the innermost dynamically enclosing logical block; otherwise, pprint-indent has no effect.

N specifies the indentation in ems. If relative-to is :block, the indentation is set to the horizontal position of the first character in the dynamically current logical block plus n ems. If relative-to is :current, the indentation is set to the current output position plus n ems. (For robustness in the face of variable-width fonts, it is advisable to use :current with an n of zero whenever possible.)

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 pprint-indent do not take effect until after the next line break. In addition, in miser mode all calls to pprint-indent are ignored, forcing the lines corresponding to the logical block to line up under the first character in the block.

Exceptional Situations:

An error is signaled if relative-to is any object other than :block or :current.

See Also:

Section 22.3.5.3 (Tilde I: Indent)

Expanded Reference: pprint-indent

:block Indentation

With :block, the indentation is set relative to the start of the enclosing logical block.

(let ((*print-pretty* t)
(*print-right-margin* 25))
(with-output-to-string (s)
(pprint-logical-block (s '(let ((a 1) (b 2)) body1 body2)
:prefix "(" :suffix ")")
(write (pprint-pop) :stream s) ; LET
(write-char #\Space s)
(write (pprint-pop) :stream s) ; bindings
(pprint-indent :block 2 s) ; indent body by 2 from block start
(pprint-newline :mandatory s)
(write (pprint-pop) :stream s) ; body1
(pprint-newline :mandatory s)
(write (pprint-pop) :stream s)))) ; body2
=> "(LET ((A 1) (B 2))
BODY1
BODY2)"

:current Indentation

With :current, the indentation is set relative to the current output position.

(let ((*print-pretty* t)
(*print-right-margin* 10))
(with-output-to-string (s)
(pprint-logical-block (s '(begin a b c d) :prefix "(" :suffix ")")
(write (pprint-pop) :stream s) ; BEGIN
(write-char #\Space s)
(pprint-indent :current 0 s) ; align subsequent with current position
(loop (pprint-exit-if-list-exhausted)
(write (pprint-pop) :stream s)
(pprint-exit-if-list-exhausted)
(write-char #\Space s)
(pprint-newline :linear s)))))
=> "(BEGIN A
B
C
D)"

Indentation Takes Effect After Next Line Break

Changes from pprint-indent do not affect the current line; they take effect on subsequent lines after a line break.

(let ((*print-pretty* t)
(*print-right-margin* 30))
(with-output-to-string (s)
(pprint-logical-block (s nil :prefix "[" :suffix "]")
(write-string "HEADER" s)
(pprint-indent :block 4 s)
(pprint-newline :mandatory s)
(write-string "indented body" s))))
=> "[HEADER
indented body]"