Search code examples
common-lispostreamformat-string

“~&” Knows Whether the Ostream is At Line Head?


CL-USER> (progn
           (format t "abc~%")
           (format t "~&abc"))
abc
abc
NIL
CL-USER> 

My guess is: An ostream descriptor always stores the latest char sent to it.
Say, after ostream receiving an #\a, FORMAT can determine that the ostream is not at the beginning because the latest char sent to it is #\a.

But I'm not sure that's really the case.


Solution

  • ~& just calls fresh-line, so the question is in fact how does fresh-line know this, and the answer is

    • it is not required to know it at all: see TERPRI, FRESH-LINE;
    • and how it knows it is entirely up to the implementation.

    If you look at various implementations for which source is available you will probably find various ways they do this. For instance SBCL has an fd-stream object (in src/code/fd-stream.lisp) which keeps track of the output-column. But how implementations do this, and whether they do it at all, is up to them.