[racket] Any way to force line breaks when printing s-exprs?

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Sun Dec 12 01:25:38 EST 2010

Todd O'Bryan wrote:
> Is there some way, short of rewriting pretty-print myself, to signal
> where I want line-breaks when printing an s-expr?
> 
> For example, suppose I have '(a b c d), and I'd like it displayed as:
> 
> (a b
>   c
>   d)
> 
> Is there a clever way to do that, or am I in for some work?

If there's already something else that prints that way, you're in luck: 
you can update the 'pretty-print-current-style-table' to treat the 
symbol as that other thing. The example you have above looks like the 
indentation of 'lambda', so you could write:

   (parameterize ((pretty-print-current-style-table
                   (pretty-print-extend-style-table
                    (pretty-print-current-style-table)
                    (list 'a)
                    (list 'lambda))))
     (pretty-write '(a b c d)))

Of course, it'll still print on one line if it can.

There are other hooks you might look at. You might be able to intercept 
all lists starting with 'a, for example, and handle the printing of 
those yourself. See 'pretty-print-size-hook' and 
'pretty-print-print-hook', and maybe others.

Ryan


Posted on the users mailing list.