Spell check.
[bpt/guile.git] / doc / ref / misc-modules.texi
CommitLineData
a0e07ba4
NJ
1@page
2@node Pretty Printing
3@chapter Pretty Printing
4
5@c FIXME::martin: Review me!
6
7@cindex pretty printing
8The module @code{(ice-9 pretty-print)} provides the procedure
9@code{pretty-print}, which provides nicely formatted output of Scheme
10objects. This is especially useful for deeply nested or complex data
11structures, such as lists and vectors.
12
13The module is loaded by simply saying.
14
15@lisp
16(use-modules (ice-9 pretty-print))
17@end lisp
18
19This makes the procedure @code{pretty-print} available. As an example
20how @code{pretty-print} will format the output, see the following:
21
22@lisp
23(pretty-print '(define (foo) (lambda (x)
24(cond ((zero? x) #t) ((negative? x) -x) (else (if (= x 1) 2 (* x x x)))))))
25@print{}
26(define (foo)
27 (lambda (x)
28 (cond ((zero? x) #t)
29 ((negative? x) -x)
30 (else (if (= x 1) 2 (* x x x))))))
31@end lisp
32
8f85c0c6 33@deffn {Scheme Procedure} pretty-print obj [port]
a0e07ba4
NJ
34Print the textual representation of the Scheme object @var{obj} to
35@var{port}. @var{port} defaults to the current output port, if not
36given.
37@end deffn
38
39Beware: Since @code{pretty-print} uses it's own write procedure, it's
40output will not be the same as for example the output of @code{write}.
41Consider the following example.
42
43@lisp
44(write (lambda (x) x))
45@print{}
46#<procedure #f (x)>
47
48(pretty-print (lambda (x) x))
49@print{}
50#[procedure]
51@end lisp
52
53The reason is that @code{pretty-print} does not know as much about
54Guile's object types as the builtin procedures. This is particularly
55important for smobs, for which a write procedure can be defined and be
56used by @code{write}, but not by @code{pretty-print}.
57
2a946b44 58
a0e07ba4
NJ
59@page
60@node Formatted Output
61@chapter Formatted Output
62
63@c FIXME::martin: Review me!
64
65@cindex format
66@cindex formatted output
67Outputting messages or other texts which are composed of literal
68strings, variable contents, newlines and other formatting can be
69cumbersome, when only the standard procedures like @code{display},
70@code{write} and @code{newline} are available. Additionally, one
71often wants to collect the output in strings. With the standard
72routines, the user is required to set up a string port, add this port
73as a parameter to the output procedure calls and then retrieve the
74resulting string from the string port.
75
76The @code{format} procedure, to be found in module @code{(ice-9
77format)}, can do all this, and even more. If you are a C programmer,
78you can think of this procedure as Guile's @code{fprintf}.
79
8f85c0c6 80@deffn {Scheme Procedure} format destination format-string args @dots{}
a0e07ba4
NJ
81The first parameter is the @var{destination}, it determines where the
82output of @code{format} will go.
83
84@table @asis
85@item @code{#t}
86Send the formatted output to the current output port and return
87@code{#t}.
88
89@item @code{#f}
90Return the formatted output as a string.
91
92@item Any number value
93Send the formatted output to the current error port and return
94@code{#t}.
95
96@item A valid output port
97Send the formatted output to the port @var{destination} and return
98@code{#t}.
99@end table
100
101The second parameter is the format string. It has a similar function
102to the format string in calls to @code{printf} or @code{fprintf} in C.
103It is output to the specified destination, but all escape sequences
104are replaced by the results of formatting the corresponding sequence.
105
106Note that escape sequences are marked with the character @code{~}
107(tilde), and not with a @code{%} (percent sign), as in C.
108
109The escape sequences in the following table are supported. When there
110appears ``corresponding @var{arg}', that means any of the additional
111arguments, after dropping all arguments which have been used up by
112escape sequences which have been processed earlier. Some of the
113format characters (the characters following the tilde) can be prefixed
114by @code{:}, @code{@@}, or @code{:@@}, to modify the behaviour of the
115format character. How the modified behaviour differs from the default
116behaviour is described for every character in the table where
117appropriate.
118
119@table @code
120@item ~~
121Output a single @code{~} (tilde) character.
122
123@item ~%
124Output a newline character, thus advancing to the next output line.
125
126@item ~&
127Start a new line, that is, output a newline character if not already
40f316d0 128at the start of a line.
a0e07ba4
NJ
129
130@item ~_
131Output a single space character.
132
133@item ~/
134Output a single tabulator character.
135
136@item ~|
137Output a page separator (formfeed) character.
138
139@item ~t
140Advance to the next tabulator position.
141
142@item ~y
85a9b4ed 143Pretty-print the corresponding @var{arg}.
a0e07ba4
NJ
144
145@item ~a
146Output the corresponding @var{arg} like @code{display}.
147
148@item ~s
149Output the corresponding @var{arg} like @code{write}.
150
151@item ~d
152Output the corresponding @var{arg} as a decimal number.
153
154@item ~x
155Output the corresponding @var{arg} as a hexadecimal number.
156
157@item ~o
158Output the corresponding @var{arg} as an octal number.
159
160@item ~b
161Output the corresponding @var{arg} as a binary number.
162
163@item ~r
164Output the corresponding @var{arg} as a number word, e.g. 10 prints as
165@code{ten}. If prefixed with @code{:}, @code{tenth} is printed, if
85a9b4ed 166prefixed with @code{:@@}, Roman numbers are printed.
a0e07ba4
NJ
167
168@item ~f
169Output the corresponding @var{arg} as a fixed format floating point
170number, such as @code{1.34}.
171
172@item ~e
173Output the corresponding @var{arg} in exponential notation, such as
174@code{1.34E+0}.
175
176@item ~g
40f316d0
MG
177This works either like @code{~f} or like @code{~e}, whichever produces
178less characters to be written.
a0e07ba4
NJ
179
180@item ~$
181Like @code{~f}, but only with two digits after the decimal point.
182
183@item ~i
184Output the corresponding @var{arg} as a complex number.
185
186@item ~c
187Output the corresponding @var{arg} as a character. If prefixed with
188@code{@@}, it is printed like with @code{write}. If prefixed with
189@code{:}, control characters are treated specially, for example
190@code{#\newline} will be printed as @code{^J}.
191
192@item ~p
193``Plural''. If the corresponding @var{arg} is 1, nothing is printed
194(or @code{y} if prefixed with @code{@@} or @code{:@@}), otherwise
195@code{s} is printed (or @code{ies} if prefixed with @code{@@} or
196@code{:@@}).
197
198@item ~?, ~k
199Take the corresponding argument as a format string, and the following
200argument as a list of values. Then format the values with respect to
201the format string.
202
203@item ~!
204Flush the output to the output port.
205
206@item ~#\newline (tilde-newline)
207@c FIXME::martin: I don't understand this from the source.
208Continuation lines.
209
210@item ~*
211Argument jumping. Navigate in the argument list as specified by the
212corresponding argument. If prefixed with @code{:}, jump backwards in
213the argument list, if prefixed by @code{:@@}, jump to the parameter
214with the absolute index, otherwise jump forward in the argument list.
215
216@item ~(
217Case conversion begin. If prefixed by @code{:}, the following output
218string will be capitalized, if prefixed by @code{@@}, the first
219character will be capitalized, if prefixed by @code{:@@} it will be
220upcased and otherwise it will be downcased. Conversion stops when the
221``Case conversion end'' @code{~)}sequence is encountered.
222
223@item ~)
224Case conversion end. Stop any case conversion currently in effect.
225
226@item ~[
227@c FIXME::martin: I don't understand this from the source.
228Conditional begin.
229
230@item ~;
231@c FIXME::martin: I don't understand this from the source.
232Conditional separator.
233
234@item ~]
235@c FIXME::martin: I don't understand this from the source.
236Conditional end.
237
238@item ~@{
239@c FIXME::martin: I don't understand this from the source.
240Iteration begin.
241
242@item ~@}
243@c FIXME::martin: I don't understand this from the source.
244Iteration end.
245
246@item ~^
247@c FIXME::martin: I don't understand this from the source.
248Up and out.
249
250@item ~'
251@c FIXME::martin: I don't understand this from the source.
252Character parameter.
253
254@item ~0 @dots{} ~9, ~-, ~+
255@c FIXME::martin: I don't understand this from the source.
256Numeric parameter.
257
258@item ~v
259@c FIXME::martin: I don't understand this from the source.
260Variable parameter from next argument.
261
262@item ~#
263Parameter is number of remaining args. The number of the remaining
264arguments is prepended to the list of unprocessed arguments.
265
266@item ~,
267@c FIXME::martin: I don't understand this from the source.
268Parameter separators.
269
270@item ~q
271Inquiry message. Insert a copyright message into the output.
272@end table
273
274If any type conversions should fail (for example when using an escape
275sequence for number output, but the argument is a string), an error
276will be signalled.
277@end deffn
278
279You may have noticed that Guile contains a @code{format} procedure
280even when the module @code{(ice-9 format)} is not loaded. The default
281@code{format} procedure does not support all escape sequences
282documented in this chapter, and will signal an error if you try to use
283one of them. The reason for providing two versions of @code{format}
284is that the full-featured module is fairly large and requires some
285time to get loaded. So the Guile maintainers decided not to load the
286large version of @code{format} by default, so that the start-up time
287of the interpreter is not unnecessarily increased.
288
289
2a946b44
NJ
290@page
291@node Rx Regexps
292@chapter The Rx Regular Expression Library
293
294[FIXME: this is taken from Gary and Mark's quick summaries and should be
295reviewed and expanded. Rx is pretty stable, so could already be done!]
296
297@cindex rx
298@cindex finite automaton
299
300The @file{guile-lang-allover} package provides an interface to Tom
301Lord's Rx library (currently only to POSIX regular expressions). Use of
302the library requires a two step process: compile a regular expression
303into an efficient structure, then use the structure in any number of
304string comparisons.
305
306For example, given the regular expression @samp{abc.} (which matches any
307string containing @samp{abc} followed by any single character):
308
309@smalllisp
310guile> @kbd{(define r (regcomp "abc."))}
311guile> @kbd{r}
312#<rgx abc.>
313guile> @kbd{(regexec r "abc")}
314#f
315guile> @kbd{(regexec r "abcd")}
316#((0 . 4))
317guile>
318@end smalllisp
319
320The definitions of @code{regcomp} and @code{regexec} are as follows:
321
322@deffn {Scheme Procedure} regcomp pattern [flags]
323Compile the regular expression pattern using POSIX rules. Flags is
324optional and should be specified using symbolic names:
325@defvar REG_EXTENDED
326use extended POSIX syntax
327@end defvar
328@defvar REG_ICASE
329use case-insensitive matching
330@end defvar
331@defvar REG_NEWLINE
332allow anchors to match after newline characters in the
333string and prevents @code{.} or @code{[^...]} from matching newlines.
334@end defvar
335
336The @code{logior} procedure can be used to combine multiple flags.
337The default is to use
338POSIX basic syntax, which makes @code{+} and @code{?} literals and @code{\+}
339and @code{\?}
340operators. Backslashes in @var{pattern} must be escaped if specified in a
341literal string e.g., @code{"\\(a\\)\\?"}.
342@end deffn
343
344@deffn {Scheme Procedure} regexec regex string [match-pick] [flags]
345Match @var{string} against the compiled POSIX regular expression
346@var{regex}.
347@var{match-pick} and @var{flags} are optional. Possible flags (which can be
348combined using the logior procedure) are:
349
350@defvar REG_NOTBOL
351The beginning of line operator won't match the beginning of
352@var{string} (presumably because it's not the beginning of a line)
353@end defvar
354
355@defvar REG_NOTEOL
356Similar to REG_NOTBOL, but prevents the end of line operator
357from matching the end of @var{string}.
358@end defvar
359
360If no match is possible, regexec returns #f. Otherwise @var{match-pick}
361determines the return value:
362
363@code{#t} or unspecified: a newly-allocated vector is returned,
364containing pairs with the indices of the matched part of @var{string} and any
365substrings.
366
367@code{""}: a list is returned: the first element contains a nested list
368with the matched part of @var{string} surrounded by the the unmatched parts.
369Remaining elements are matched substrings (if any). All returned
370substrings share memory with @var{string}.
371
372@code{#f}: regexec returns #t if a match is made, otherwise #f.
373
374vector: the supplied vector is returned, with the first element replaced
375by a pair containing the indices of the matched portion of @var{string} and
376further elements replaced by pairs containing the indices of matched
377substrings (if any).
378
379list: a list will be returned, with each member of the list
380specified by a code in the corresponding position of the supplied list:
381
382a number: the numbered matching substring (0 for the entire match).
383
384@code{#\<}: the beginning of @var{string} to the beginning of the part matched
385by regex.
386
387@code{#\>}: the end of the matched part of @var{string} to the end of
388@var{string}.
389
390@code{#\c}: the "final tag", which seems to be associated with the "cut
391operator", which doesn't seem to be available through the posix
392interface.
393
394e.g., @code{(list #\< 0 1 #\>)}. The returned substrings share memory with
395@var{string}.
396@end deffn
397
398Here are some other procedures that might be used when using regular
399expressions:
400
401@deffn {Scheme Procedure} compiled-regexp? obj
402Test whether obj is a compiled regular expression.
403@end deffn
404
405@deffn {Scheme Procedure} regexp->dfa regex [flags]
406@end deffn
407
408@deffn {Scheme Procedure} dfa-fork dfa
409@end deffn
410
411@deffn {Scheme Procedure} reset-dfa! dfa
412@end deffn
413
414@deffn {Scheme Procedure} dfa-final-tag dfa
415@end deffn
416
417@deffn {Scheme Procedure} dfa-continuable? dfa
418@end deffn
419
420@deffn {Scheme Procedure} advance-dfa! dfa string
421@end deffn
422
423
a0e07ba4
NJ
424@c Local Variables:
425@c TeX-master: "guile.texi"
426@c End: