Next: , Previous: , Up: iklib printing   [Index]


6.39.2 Pretty printing Scheme objects

Function: pretty-print datum
Function: pretty-print datum output-port

Printi Scheme data, typically Scheme programs, in a format close to how a Scheme programmer would write it. Unlike write, which writes its input all in one line, pretty-print inserts spaces and new lines in order to produce more pleasant output.

(define fact-code
  '(letrec ([fact (lambda (n)
                    (if (zero? n)
                        1
                      (* n (fact (- n 1)))))])
     (fact 5)))

> (pretty-print fact-code)
(letrec ((fact
          (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))
  (fact 5))

The second argument to pretty-print, if supplied, must be an open textual output port. If not supplied, the current-output-port is used.

Function: pretty-print* datum output-port start-column ending-newline?

Like pretty-print but accepts the additional arguments:

start-column

A non–negative fixnum. The zero–based column offset at which the first line is displayed. When printing a multiline symbolic expression, the column offset of the first line depends upon what has been already printed on such line; this argument allows us to correctly align the second and subsequent lines. For example the following program:

#!r6rs
(import (vicare))

(pretty-width 10)
(set-port-buffer-mode! (current-output-port)
                       (buffer-mode none))

(display "01234")
(pretty-print  '("56789" "8 7 6 5"))

(newline)

(display "01234")
(pretty-print* '("56789" "8 7 6 5")
               (current-output-port) 5 #t)

prints:

01234("56789"
  "8 7 6 5")

01234("56789"
       "8 7 6 5")
ending-newline?

Interpreted as boolean value. If true: a newline character is output after the symbolic expression.

Parameter: pretty-width
Parameter: pretty-width n

The parameter pretty-width controls the number of characters after which the pretty-print starts breaking long lines into multiple lines. The initial value of pretty-width is set to 60 characters, which is suitable for most terminals and printed material.

> (parameterize ([pretty-width 40])
     (pretty-print fact-code))
(letrec ((fact
          (lambda (n)
            (if (zero? n)
                1
                (* n (fact (- n 1)))))))
  (fact 5))

Note that pretty-width does not guarantee that the output will not extend beyond the specified number. Very long symbols, for examples, cannot be split into multiple lines and may force the printer to go beyond the value of pretty-width.

Function: pretty-format obj

Still undocumented.

Parameter: print-unicode
Parameter: print-unicode #t
Parameter: print-unicode #f

If set to true: print Scheme Unicode characters outside of the printable ASCII range in hex format, else print them using the encoding of the output port. This parameter is initialised to #f.

Parameter: printer-integer-radix

The radix used to print exact integers (fixnums and bignums) with write, display and similar functions. Initialised to 10, it must hold a value usable as second argument to string->number.

Parameter: print-gensym
Parameter: print-gensym #t
Parameter: print-gensym #f
Parameter: print-gensym 'pretty

Control how gensyms are printed by the various writers.

This parameter is initialised to #t. Examples:

> (parametrise ((print-gensym #f))
    (pretty-print (list (gensym) (gensym))))
(g0 g1)

> (parametrise ((print-gensym #t))
    (pretty-print (list (gensym) (gensym))))
(#{g2 |KR1M2&CTt1<B0n/m|} #{g3 |FBAb&7NC6&=c82!O|})

> (parametrise ((print-gensym 'pretty))
    (pretty-print (list (gensym) (gensym))))
(#:g4 #:g5)
Parameter: gensym-prefix
Parameter: gensym-prefix string

Specify the string to be used as the prefix to generated pretty names. The initialisation value is the string ‘g’, which causes generated strings to have pretty names in the sequence ‘g0’, ‘g1’, ‘g2’, etc.

> (parametrise ((gensym-prefix "var")
                (print-gensym  #f))
    (pretty-print (list (gensym) (gensym) (gensym))))
(var0 var1 var2)

This parameter controls how pretty names are generated, and has nothing to do with how gensym constructs a new gensym; in particular, knowing that it is pretty-print that generates the pretty names, notice the difference between the output in the first example with the output of the examples below:

> (pretty-print
    (parametrise ((gensym-prefix "var")
                  (print-gensym  #f))
      (list (gensym) (gensym) (gensym))))
(g3 g4 g5)

> (let ((ls (list (gensym) (gensym) (gensym))))
    (parametrise ((gensym-prefix "var")
                  (print-gensym  #f))
      (pretty-print ls)))
(var5 var6 var7)
Parameter: gensym-count
Parameter: gensym-count n

Determine the number which is attached to the gensym-prefix when gensyms’ pretty names are generated. The initialisation value is ‘0’ and it is incremented every time a pretty name is generated. It might be set to any non–negative integer value.

> (let ((x (gensym)))
    (parametrise ((gensym-count 100)
                  (print-gensym #f))
      (pretty-print (list (gensym) x (gensym)))))
(g100 g101 g102)

Notice from all the examples so far that pretty names are generated in the order at which the gensyms are printed, not in the order in which gensyms were created.


Next: , Previous: , Up: iklib printing   [Index]