3 @chapter Pretty Printing
5 @c FIXME::martin: Review me!
7 @cindex pretty printing
8 The module @code{(ice-9 pretty-print)} provides the procedure
9 @code{pretty-print}, which provides nicely formatted output of Scheme
10 objects. This is especially useful for deeply nested or complex data
11 structures, such as lists and vectors.
13 The module is loaded by simply saying.
16 (use-modules (ice-9 pretty-print))
19 This makes the procedure @code{pretty-print} available. As an example
20 how @code{pretty-print} will format the output, see the following:
23 (pretty-print '(define (foo) (lambda (x)
24 (cond ((zero? x) #t) ((negative? x) -x) (else (if (= x 1) 2 (* x x x)))))))
30 (else (if (= x 1) 2 (* x x x))))))
33 @deffn {Scheme Procedure} pretty-print obj [port]
34 Print the textual representation of the Scheme object @var{obj} to
35 @var{port}. @var{port} defaults to the current output port, if not
39 Beware: Since @code{pretty-print} uses it's own write procedure, it's
40 output will not be the same as for example the output of @code{write}.
41 Consider the following example.
44 (write (lambda (x) x))
48 (pretty-print (lambda (x) x))
53 The reason is that @code{pretty-print} does not know as much about
54 Guile's object types as the builtin procedures. This is particularly
55 important for smobs, for which a write procedure can be defined and be
56 used by @code{write}, but not by @code{pretty-print}.
59 @node Formatted Output
60 @chapter Formatted Output
62 @c FIXME::martin: Review me!
65 @cindex formatted output
66 Outputting messages or other texts which are composed of literal
67 strings, variable contents, newlines and other formatting can be
68 cumbersome, when only the standard procedures like @code{display},
69 @code{write} and @code{newline} are available. Additionally, one
70 often wants to collect the output in strings. With the standard
71 routines, the user is required to set up a string port, add this port
72 as a parameter to the output procedure calls and then retrieve the
73 resulting string from the string port.
75 The @code{format} procedure, to be found in module @code{(ice-9
76 format)}, can do all this, and even more. If you are a C programmer,
77 you can think of this procedure as Guile's @code{fprintf}.
79 @deffn {Scheme Procedure} format destination format-string args @dots{}
80 The first parameter is the @var{destination}, it determines where the
81 output of @code{format} will go.
85 Send the formatted output to the current output port and return
89 Return the formatted output as a string.
91 @item Any number value
92 Send the formatted output to the current error port and return
95 @item A valid output port
96 Send the formatted output to the port @var{destination} and return
100 The second parameter is the format string. It has a similar function
101 to the format string in calls to @code{printf} or @code{fprintf} in C.
102 It is output to the specified destination, but all escape sequences
103 are replaced by the results of formatting the corresponding sequence.
105 Note that escape sequences are marked with the character @code{~}
106 (tilde), and not with a @code{%} (percent sign), as in C.
108 The escape sequences in the following table are supported. When there
109 appears ``corresponding @var{arg}', that means any of the additional
110 arguments, after dropping all arguments which have been used up by
111 escape sequences which have been processed earlier. Some of the
112 format characters (the characters following the tilde) can be prefixed
113 by @code{:}, @code{@@}, or @code{:@@}, to modify the behaviour of the
114 format character. How the modified behaviour differs from the default
115 behaviour is described for every character in the table where
120 Output a single @code{~} (tilde) character.
123 Output a newline character, thus advancing to the next output line.
126 Start a new line, that is, output a newline character if not already
127 at the start of a line.
130 Output a single space character.
133 Output a single tabulator character.
136 Output a page separator (formfeed) character.
139 Advance to the next tabulator position.
142 Pretty-print the correspinding @var{arg}.
145 Output the corresponding @var{arg} like @code{display}.
148 Output the corresponding @var{arg} like @code{write}.
151 Output the corresponding @var{arg} as a decimal number.
154 Output the corresponding @var{arg} as a hexadecimal number.
157 Output the corresponding @var{arg} as an octal number.
160 Output the corresponding @var{arg} as a binary number.
163 Output the corresponding @var{arg} as a number word, e.g. 10 prints as
164 @code{ten}. If prefixed with @code{:}, @code{tenth} is printed, if
165 prefixed with @code{:@@}, roman numbers are printed.
168 Output the corresponding @var{arg} as a fixed format floating point
169 number, such as @code{1.34}.
172 Output the corresponding @var{arg} in exponential notation, such as
176 This works either like @code{~f} or like @code{~e}, whichever produces
177 less characters to be written.
180 Like @code{~f}, but only with two digits after the decimal point.
183 Output the corresponding @var{arg} as a complex number.
186 Output the corresponding @var{arg} as a character. If prefixed with
187 @code{@@}, it is printed like with @code{write}. If prefixed with
188 @code{:}, control characters are treated specially, for example
189 @code{#\newline} will be printed as @code{^J}.
192 ``Plural''. If the corresponding @var{arg} is 1, nothing is printed
193 (or @code{y} if prefixed with @code{@@} or @code{:@@}), otherwise
194 @code{s} is printed (or @code{ies} if prefixed with @code{@@} or
198 Take the corresponding argument as a format string, and the following
199 argument as a list of values. Then format the values with respect to
203 Flush the output to the output port.
205 @item ~#\newline (tilde-newline)
206 @c FIXME::martin: I don't understand this from the source.
210 Argument jumping. Navigate in the argument list as specified by the
211 corresponding argument. If prefixed with @code{:}, jump backwards in
212 the argument list, if prefixed by @code{:@@}, jump to the parameter
213 with the absolute index, otherwise jump forward in the argument list.
216 Case conversion begin. If prefixed by @code{:}, the following output
217 string will be capitalized, if prefixed by @code{@@}, the first
218 character will be capitalized, if prefixed by @code{:@@} it will be
219 upcased and otherwise it will be downcased. Conversion stops when the
220 ``Case conversion end'' @code{~)}sequence is encountered.
223 Case conversion end. Stop any case conversion currently in effect.
226 @c FIXME::martin: I don't understand this from the source.
230 @c FIXME::martin: I don't understand this from the source.
231 Conditional separator.
234 @c FIXME::martin: I don't understand this from the source.
238 @c FIXME::martin: I don't understand this from the source.
242 @c FIXME::martin: I don't understand this from the source.
246 @c FIXME::martin: I don't understand this from the source.
250 @c FIXME::martin: I don't understand this from the source.
253 @item ~0 @dots{} ~9, ~-, ~+
254 @c FIXME::martin: I don't understand this from the source.
258 @c FIXME::martin: I don't understand this from the source.
259 Variable parameter from next argument.
262 Parameter is number of remaining args. The number of the remaining
263 arguments is prepended to the list of unprocessed arguments.
266 @c FIXME::martin: I don't understand this from the source.
267 Parameter separators.
270 Inquiry message. Insert a copyright message into the output.
273 If any type conversions should fail (for example when using an escape
274 sequence for number output, but the argument is a string), an error
278 You may have noticed that Guile contains a @code{format} procedure
279 even when the module @code{(ice-9 format)} is not loaded. The default
280 @code{format} procedure does not support all escape sequences
281 documented in this chapter, and will signal an error if you try to use
282 one of them. The reason for providing two versions of @code{format}
283 is that the full-featured module is fairly large and requires some
284 time to get loaded. So the Guile maintainers decided not to load the
285 large version of @code{format} by default, so that the start-up time
286 of the interpreter is not unnecessarily increased.
290 @c TeX-master: "guile.texi"