Gauche Devlog

< Segmented completion | Real numerical functions >

2023/09/28

Pretty print indentation

Yet another small thing good to have. You can now specify base indentation to the pretty printer (ref:pprint). It is applied to the second line and after.

gosh> (pprint (make-list 100 'abc) :indent 20)
(abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc)
#<undef>

To say more precisely, when the pretty printer spills data to another line, it inserts "a newline + whitespace * indent", plus any indent computed by the pretty printer.

The benefit easiest to see is when the pretty printer is used inside format. When a pretty printing triggered by the ~:w directive, it sets the base indentation at the column it starts printing. Hence the entire pretty print is indented to align nicely:

gosh> (format #t "Long list: ~:w\n" (make-list 100 'abc))
Long list: (abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
            abc abc)
#<undef>

Since pretty printing is built-in to the core printer (pprint is just a simple interface to use), other output routines such as write can also use base indentation. You can set indent slot of a write-controls.

gosh> (write (make-list 100 'abc) (make-write-controls :pretty #t :indent 20 :width 79))
(abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc abc abc abc abc abc abc abc abc abc abc abc abc
                     abc abc)#<undef>

Tags: 0.9.13, pretty-printing