### 4.12 Printing Scheme ¶

#### 4.12.1 Generic-Write ¶

`(require 'generic-write)`

`generic-write` is a procedure that transforms a Scheme data value (or Scheme program expression) into its textual representation and prints it. The interface to the procedure is sufficiently general to easily implement other useful formatting procedures such as pretty printing, output to a string and truncated output.

Procedure: generic-write obj display? width output ¶
  obj

    Scheme data value to transform.

  display?

    Boolean, controls whether characters and strings are quoted.

  width

    Extended boolean, selects format:

    #f

      single line format

    integer > 0

      pretty-print (value = max nb of chars per line)

  output

    Procedure of 1 argument of string type, called repeatedly with successive substrings of the textual representation. This procedure can return `#f` to stop the transformation.

  The value returned by `generic-write` is undefined.

  Examples:

      (write obj) ≡ (generic-write obj #f #f display-string)
      (display obj) ≡ (generic-write obj #t #f display-string)

  where

      display-string ≡
      (lambda (s) (for-each write-char (string->list s)) #t)

#### 4.12.2 Object-To-String ¶

`(require 'object->string)`

Function: object->string obj ¶

  Returns the textual representation of obj as a string.

Function: object->limited-string obj limit ¶

  Returns the textual representation of obj as a string of length at most limit.

#### 4.12.3 Pretty-Print ¶

`(require 'pretty-print)`

Procedure: pretty-print obj ¶
Procedure: pretty-print obj port ¶

  `pretty-print`s obj on port. If port is not specified, `current-output-port` is used.

  Example:

      (pretty-print '((1 2 3 4 5) (6 7 8 9 10) (11 12 13 14 15)
                      (16 17 18 19 20) (21 22 23 24 25)))
         -| ((1 2 3 4 5)
         -|  (6 7 8 9 10)
         -|  (11 12 13 14 15)
         -|  (16 17 18 19 20)
         -|  (21 22 23 24 25))

Procedure: pretty-print->string obj ¶
Procedure: pretty-print->string obj width ¶

  Returns the string of obj `pretty-print`ed in width columns. If width is not specified, `(output-port-width)` is used.

  Example:

      (pretty-print->string '((1 2 3 4 5) (6 7 8 9 10) (11 12 13 14 15)
                              (16 17 18 19 20) (21 22 23 24 25)))
      ⇒
      "((1 2 3 4 5)
       (6 7 8 9 10)
       (11 12 13 14 15)
       (16 17 18 19 20)
       (21 22 23 24 25))
      "

      (pretty-print->string '((1 2 3 4 5) (6 7 8 9 10) (11 12 13 14 15)
                              (16 17 18 19 20) (21 22 23 24 25))
                            16)
      ⇒
      "((1 2 3 4 5)
       (6 7 8 9 10)
       (11
        12
        13
        14
        15)
       (16
        17
        18
        19
        20)
       (21
        22
        23
        24
        25))
      "

`(require 'pprint-file)`

Procedure: pprint-file infile ¶
Procedure: pprint-file infile outfile ¶

  Pretty-prints all the code in infile. If outfile is specified, the output goes to outfile, otherwise it goes to `(current-output-port)`.

Function: pprint-filter-file infile proc outfile ¶
Function: pprint-filter-file infile proc ¶

  infile is a port or a string naming an existing file. Scheme source code expressions and definitions are read from the port (or file) and proc is applied to them sequentially.

  outfile is a port or a string. If no outfile is specified then `current-output-port` is assumed. These expanded expressions are then `pretty-print`ed to this port.

  Whitepsace and comments (introduced by `;`) which are not part of scheme expressions are reproduced in the output. This procedure does not affect the values returned by `current-input-port`, `current-error-port`, and `current-output-port`.

`pprint-filter-file` can be used to pre-compile macro-expansion and thus can reduce loading time. The following will write into `exp-code.scm` the result of expanding all defmacros in `code.scm`.

    (require 'pprint-file)
    (require 'defmacroexpand)
    (defmacro:load "my-macros.scm")
    (pprint-filter-file "code.scm" defmacro:expand* "exp-code.scm")