Skip to main content

terpri, fresh-line

terpri, fresh-line Function

Syntax:

terpri &optional output-stream → nil

fresh-line &optional output-stream → generalized-boolean

Arguments and Values:

output-stream – an output stream designator . The default is standard output.

generalized-boolean—a generalized boolean.

Description:

terpri outputs a newline to output-stream.

fresh-line is similar to terpri but outputs a newline only if the output-stream is not already at the start of a line. If for some reason this cannot be determined, then a newline is output anyway. fresh-line returns true if it outputs a newline; otherwise it returns false.

Examples:

(with-output-to-string (s) 
(write-string "some text" s)
(terpri s)
(terpri s)
(write-string "more text" s))
"some text
more text"
(with-output-to-string (s)
(write-string "some text" s)
(fresh-line s)
(fresh-line s)
(write-string "more text" s))
"some text
more text"

Side Effects:

The output-stream is modified.

Affected By:

*standard-output*, *terminal-io*.

Exceptional Situations:

None.

Notes:

terpri is identical in effect to

(write-char #\Newline output-stream)

Expanded Reference: terpri, fresh-line

terpri: Unconditional Newline

terpri always outputs a newline to the stream, regardless of the current position. It returns NIL.

(with-output-to-string (s)
(write-string "line1" s)
(terpri s)
(write-string "line2" s))
=> "line1
line2"

terpri Outputs Extra Blank Lines

Since terpri always writes a newline, consecutive calls produce blank lines.

(with-output-to-string (s)
(write-string "above" s)
(terpri s)
(terpri s)
(write-string "below" s))
=> "above

below"

fresh-line: Conditional Newline

fresh-line outputs a newline only if the stream is not already at the start of a line. It returns T if a newline was output, NIL otherwise.

(with-output-to-string (s)
(write-string "text" s)
(fresh-line s) ; outputs newline (returns T)
(fresh-line s) ; already at start of line (returns NIL)
(write-string "more" s))
=> "text
more"

Comparing terpri and fresh-line

;; terpri always adds a newline
(with-output-to-string (s)
(write-string "some text" s)
(terpri s)
(terpri s)
(write-string "more text" s))
=> "some text

more text"

;; fresh-line avoids double newlines
(with-output-to-string (s)
(write-string "some text" s)
(fresh-line s)
(fresh-line s)
(write-string "more text" s))
=> "some text
more text"