Dynamic Pretty-printing in DomTerm
The goal of pretty-printing is to split a text into lines with appropriate indentation, in a way that conforms to the logical structure of the text.
For example if we need to print the following list:
((alpha-1 alpha-2 alpha-3) (beta-1 beta-2 beta-3 beta-4))in a window 40 characters wide, we want:
((alpha-1 alpha-2 alpha-3) (beta-1 beta-2 beta-3 beta-4))but not:
((alpha-1 alpha-2 alpha-3) (beta-1 beta-2 beta-3 beta-4))
Pretty-printing is common in Lisp environments to display complicated nested data structures. Traditionally, it is done by the programming-language runtime, based on a given line width. However, the line width of a console can change if the user resizes the window or changes font size. In that case, previously-emitted pretty-printed lines will quickly become ugly: If the line-width decreases, the breaks will be all wrong and the text hard to read; if the line-width increases we may be using more lines than necessary.
Modern terminal emulators do
dumb line-breaking: Splitting a long
lines into screen lines, but regardless of structure or even word boundaries.
Some emulators remember for each line whether an overflow happened, or
hard newline was printed.
Some terminal emulators (for example Gnome Terminal) will use this
to re-do the splitting when a window is re-sized.
However, that does not help with pretty-printer output.
Below is a screenshot from Kawa running in DomTerm at 80 colutions.
We reduce the window size to 50 columns.
The user input (yellow background) is raw text, so its line is split
but the output (white background) gets
(Note the window size indicator in the lower-right.)
We reduce the window size to 35 columns:
It also works with saved pages
DomTerm allows you to save the current session as
a static HTML page.
hlib directory, then dynamic line-breaking
happens even for saved
(The lazy way is to create
hlib as a symbolic link
hlib directory of the DomTerm distribution.)
Try it yourself on a saved session.
--debug-print-expr flag causes Kawa to print out each command
before it is compiled and evaluated. The result (shown in red because it
is sent to the standard error stream) is pretty-printed dynamically.
This is how it works.
When an application pretty-prints a structure, it calls
procedures to mark which parts of the output logically belong
and where line-breaks and indentation may be inserted.
In Kawa the default print formatting for lists and vectors automatically calls
these procedures when the output is a
The pretty-printing library calculates where to put line-breaks and indentation,
based on these commands and the specified line length.
However, when the output stream is a DomTerm terminal, Kawa's pretty-printing library does not actually calculate the line-breaks. Instead it encodes the above-mentioned procedure calls as special escape sequences that get sent to DomTerm.
When DomTerm receives these escape sequences, it builds a nested DOM structure that corresponds to the orginal procedure calls. DomTerm calculates how to layout that structure using a variant of the Common Lisp pretty-printing algorithm, inserting soft left-breaks and indentation as needed.
When DomTerm detects that its window has been re-sized or zoomed, it first removes old soft line-breaks and identation. It does re-runs the layout algorithm.
Hyphenation and word-breakingDomTerm supports general soft (optional) line breaks: You can specify separate texts (optionally with styling) for each of the following: The text used when the line is not broken at that point; the text to use before the line-break, when broken (commonly a hyphen); the text to use after the line-break (following indentation). One use for this words that change their spelling when hyphenated, as may happen in German. For example the word
bak-ken. You can handle this using
as the non-break text;
as the pre-break text; and
as the post-break text.