Commit | Line | Data |
---|---|---|
a0e07ba4 NJ |
1 | @page |
2 | @node Pretty Printing | |
3 | @chapter Pretty Printing | |
4 | ||
5 | @c FIXME::martin: Review me! | |
6 | ||
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. | |
12 | ||
13 | The module is loaded by simply saying. | |
14 | ||
15 | @lisp | |
16 | (use-modules (ice-9 pretty-print)) | |
17 | @end lisp | |
18 | ||
19 | This makes the procedure @code{pretty-print} available. As an example | |
20 | how @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 |
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 | |
36 | given. | |
37 | @end deffn | |
38 | ||
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. | |
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 | ||
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}. | |
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 | |
67 | Outputting messages or other texts which are composed of literal | |
68 | strings, variable contents, newlines and other formatting can be | |
69 | cumbersome, when only the standard procedures like @code{display}, | |
70 | @code{write} and @code{newline} are available. Additionally, one | |
71 | often wants to collect the output in strings. With the standard | |
72 | routines, the user is required to set up a string port, add this port | |
73 | as a parameter to the output procedure calls and then retrieve the | |
74 | resulting string from the string port. | |
75 | ||
76 | The @code{format} procedure, to be found in module @code{(ice-9 | |
77 | format)}, can do all this, and even more. If you are a C programmer, | |
78 | you can think of this procedure as Guile's @code{fprintf}. | |
79 | ||
8f85c0c6 | 80 | @deffn {Scheme Procedure} format destination format-string args @dots{} |
a0e07ba4 NJ |
81 | The first parameter is the @var{destination}, it determines where the |
82 | output of @code{format} will go. | |
83 | ||
84 | @table @asis | |
85 | @item @code{#t} | |
86 | Send the formatted output to the current output port and return | |
87 | @code{#t}. | |
88 | ||
89 | @item @code{#f} | |
90 | Return the formatted output as a string. | |
91 | ||
92 | @item Any number value | |
93 | Send the formatted output to the current error port and return | |
94 | @code{#t}. | |
95 | ||
96 | @item A valid output port | |
97 | Send the formatted output to the port @var{destination} and return | |
98 | @code{#t}. | |
99 | @end table | |
100 | ||
101 | The second parameter is the format string. It has a similar function | |
102 | to the format string in calls to @code{printf} or @code{fprintf} in C. | |
103 | It is output to the specified destination, but all escape sequences | |
104 | are replaced by the results of formatting the corresponding sequence. | |
105 | ||
106 | Note that escape sequences are marked with the character @code{~} | |
107 | (tilde), and not with a @code{%} (percent sign), as in C. | |
108 | ||
109 | The escape sequences in the following table are supported. When there | |
110 | appears ``corresponding @var{arg}', that means any of the additional | |
111 | arguments, after dropping all arguments which have been used up by | |
112 | escape sequences which have been processed earlier. Some of the | |
113 | format characters (the characters following the tilde) can be prefixed | |
114 | by @code{:}, @code{@@}, or @code{:@@}, to modify the behaviour of the | |
115 | format character. How the modified behaviour differs from the default | |
116 | behaviour is described for every character in the table where | |
117 | appropriate. | |
118 | ||
119 | @table @code | |
120 | @item ~~ | |
121 | Output a single @code{~} (tilde) character. | |
122 | ||
123 | @item ~% | |
124 | Output a newline character, thus advancing to the next output line. | |
125 | ||
126 | @item ~& | |
127 | Start a new line, that is, output a newline character if not already | |
40f316d0 | 128 | at the start of a line. |
a0e07ba4 NJ |
129 | |
130 | @item ~_ | |
131 | Output a single space character. | |
132 | ||
133 | @item ~/ | |
134 | Output a single tabulator character. | |
135 | ||
136 | @item ~| | |
137 | Output a page separator (formfeed) character. | |
138 | ||
139 | @item ~t | |
140 | Advance to the next tabulator position. | |
141 | ||
142 | @item ~y | |
85a9b4ed | 143 | Pretty-print the corresponding @var{arg}. |
a0e07ba4 NJ |
144 | |
145 | @item ~a | |
146 | Output the corresponding @var{arg} like @code{display}. | |
147 | ||
148 | @item ~s | |
149 | Output the corresponding @var{arg} like @code{write}. | |
150 | ||
151 | @item ~d | |
152 | Output the corresponding @var{arg} as a decimal number. | |
153 | ||
154 | @item ~x | |
155 | Output the corresponding @var{arg} as a hexadecimal number. | |
156 | ||
157 | @item ~o | |
158 | Output the corresponding @var{arg} as an octal number. | |
159 | ||
160 | @item ~b | |
161 | Output the corresponding @var{arg} as a binary number. | |
162 | ||
163 | @item ~r | |
164 | Output 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 | 166 | prefixed with @code{:@@}, Roman numbers are printed. |
a0e07ba4 NJ |
167 | |
168 | @item ~f | |
169 | Output the corresponding @var{arg} as a fixed format floating point | |
170 | number, such as @code{1.34}. | |
171 | ||
172 | @item ~e | |
173 | Output the corresponding @var{arg} in exponential notation, such as | |
174 | @code{1.34E+0}. | |
175 | ||
176 | @item ~g | |
40f316d0 MG |
177 | This works either like @code{~f} or like @code{~e}, whichever produces |
178 | less characters to be written. | |
a0e07ba4 NJ |
179 | |
180 | @item ~$ | |
181 | Like @code{~f}, but only with two digits after the decimal point. | |
182 | ||
183 | @item ~i | |
184 | Output the corresponding @var{arg} as a complex number. | |
185 | ||
186 | @item ~c | |
187 | Output 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 | |
199 | Take the corresponding argument as a format string, and the following | |
200 | argument as a list of values. Then format the values with respect to | |
201 | the format string. | |
202 | ||
203 | @item ~! | |
204 | Flush the output to the output port. | |
205 | ||
206 | @item ~#\newline (tilde-newline) | |
207 | @c FIXME::martin: I don't understand this from the source. | |
208 | Continuation lines. | |
209 | ||
210 | @item ~* | |
211 | Argument jumping. Navigate in the argument list as specified by the | |
212 | corresponding argument. If prefixed with @code{:}, jump backwards in | |
213 | the argument list, if prefixed by @code{:@@}, jump to the parameter | |
214 | with the absolute index, otherwise jump forward in the argument list. | |
215 | ||
216 | @item ~( | |
217 | Case conversion begin. If prefixed by @code{:}, the following output | |
218 | string will be capitalized, if prefixed by @code{@@}, the first | |
219 | character will be capitalized, if prefixed by @code{:@@} it will be | |
220 | upcased and otherwise it will be downcased. Conversion stops when the | |
221 | ``Case conversion end'' @code{~)}sequence is encountered. | |
222 | ||
223 | @item ~) | |
224 | Case conversion end. Stop any case conversion currently in effect. | |
225 | ||
226 | @item ~[ | |
227 | @c FIXME::martin: I don't understand this from the source. | |
228 | Conditional begin. | |
229 | ||
230 | @item ~; | |
231 | @c FIXME::martin: I don't understand this from the source. | |
232 | Conditional separator. | |
233 | ||
234 | @item ~] | |
235 | @c FIXME::martin: I don't understand this from the source. | |
236 | Conditional end. | |
237 | ||
238 | @item ~@{ | |
239 | @c FIXME::martin: I don't understand this from the source. | |
240 | Iteration begin. | |
241 | ||
242 | @item ~@} | |
243 | @c FIXME::martin: I don't understand this from the source. | |
244 | Iteration end. | |
245 | ||
246 | @item ~^ | |
247 | @c FIXME::martin: I don't understand this from the source. | |
248 | Up and out. | |
249 | ||
250 | @item ~' | |
251 | @c FIXME::martin: I don't understand this from the source. | |
252 | Character parameter. | |
253 | ||
254 | @item ~0 @dots{} ~9, ~-, ~+ | |
255 | @c FIXME::martin: I don't understand this from the source. | |
256 | Numeric parameter. | |
257 | ||
258 | @item ~v | |
259 | @c FIXME::martin: I don't understand this from the source. | |
260 | Variable parameter from next argument. | |
261 | ||
262 | @item ~# | |
263 | Parameter is number of remaining args. The number of the remaining | |
264 | arguments is prepended to the list of unprocessed arguments. | |
265 | ||
266 | @item ~, | |
267 | @c FIXME::martin: I don't understand this from the source. | |
268 | Parameter separators. | |
269 | ||
270 | @item ~q | |
271 | Inquiry message. Insert a copyright message into the output. | |
272 | @end table | |
273 | ||
274 | If any type conversions should fail (for example when using an escape | |
275 | sequence for number output, but the argument is a string), an error | |
276 | will be signalled. | |
277 | @end deffn | |
278 | ||
279 | You may have noticed that Guile contains a @code{format} procedure | |
280 | even when the module @code{(ice-9 format)} is not loaded. The default | |
281 | @code{format} procedure does not support all escape sequences | |
282 | documented in this chapter, and will signal an error if you try to use | |
283 | one of them. The reason for providing two versions of @code{format} | |
284 | is that the full-featured module is fairly large and requires some | |
285 | time to get loaded. So the Guile maintainers decided not to load the | |
286 | large version of @code{format} by default, so that the start-up time | |
287 | of 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 | |
295 | reviewed and expanded. Rx is pretty stable, so could already be done!] | |
296 | ||
297 | @cindex rx | |
298 | @cindex finite automaton | |
299 | ||
300 | The @file{guile-lang-allover} package provides an interface to Tom | |
301 | Lord's Rx library (currently only to POSIX regular expressions). Use of | |
302 | the library requires a two step process: compile a regular expression | |
303 | into an efficient structure, then use the structure in any number of | |
304 | string comparisons. | |
305 | ||
306 | For example, given the regular expression @samp{abc.} (which matches any | |
307 | string containing @samp{abc} followed by any single character): | |
308 | ||
309 | @smalllisp | |
310 | guile> @kbd{(define r (regcomp "abc."))} | |
311 | guile> @kbd{r} | |
312 | #<rgx abc.> | |
313 | guile> @kbd{(regexec r "abc")} | |
314 | #f | |
315 | guile> @kbd{(regexec r "abcd")} | |
316 | #((0 . 4)) | |
317 | guile> | |
318 | @end smalllisp | |
319 | ||
320 | The definitions of @code{regcomp} and @code{regexec} are as follows: | |
321 | ||
322 | @deffn {Scheme Procedure} regcomp pattern [flags] | |
323 | Compile the regular expression pattern using POSIX rules. Flags is | |
324 | optional and should be specified using symbolic names: | |
325 | @defvar REG_EXTENDED | |
326 | use extended POSIX syntax | |
327 | @end defvar | |
328 | @defvar REG_ICASE | |
329 | use case-insensitive matching | |
330 | @end defvar | |
331 | @defvar REG_NEWLINE | |
332 | allow anchors to match after newline characters in the | |
333 | string and prevents @code{.} or @code{[^...]} from matching newlines. | |
334 | @end defvar | |
335 | ||
336 | The @code{logior} procedure can be used to combine multiple flags. | |
337 | The default is to use | |
338 | POSIX basic syntax, which makes @code{+} and @code{?} literals and @code{\+} | |
339 | and @code{\?} | |
340 | operators. Backslashes in @var{pattern} must be escaped if specified in a | |
341 | literal string e.g., @code{"\\(a\\)\\?"}. | |
342 | @end deffn | |
343 | ||
344 | @deffn {Scheme Procedure} regexec regex string [match-pick] [flags] | |
345 | Match @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 | |
348 | combined using the logior procedure) are: | |
349 | ||
350 | @defvar REG_NOTBOL | |
351 | The 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 | |
356 | Similar to REG_NOTBOL, but prevents the end of line operator | |
357 | from matching the end of @var{string}. | |
358 | @end defvar | |
359 | ||
360 | If no match is possible, regexec returns #f. Otherwise @var{match-pick} | |
361 | determines the return value: | |
362 | ||
363 | @code{#t} or unspecified: a newly-allocated vector is returned, | |
364 | containing pairs with the indices of the matched part of @var{string} and any | |
365 | substrings. | |
366 | ||
367 | @code{""}: a list is returned: the first element contains a nested list | |
368 | with the matched part of @var{string} surrounded by the the unmatched parts. | |
369 | Remaining elements are matched substrings (if any). All returned | |
370 | substrings share memory with @var{string}. | |
371 | ||
372 | @code{#f}: regexec returns #t if a match is made, otherwise #f. | |
373 | ||
374 | vector: the supplied vector is returned, with the first element replaced | |
375 | by a pair containing the indices of the matched portion of @var{string} and | |
376 | further elements replaced by pairs containing the indices of matched | |
377 | substrings (if any). | |
378 | ||
379 | list: a list will be returned, with each member of the list | |
380 | specified by a code in the corresponding position of the supplied list: | |
381 | ||
382 | a 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 | |
385 | by 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 | |
391 | operator", which doesn't seem to be available through the posix | |
392 | interface. | |
393 | ||
394 | e.g., @code{(list #\< 0 1 #\>)}. The returned substrings share memory with | |
395 | @var{string}. | |
396 | @end deffn | |
397 | ||
398 | Here are some other procedures that might be used when using regular | |
399 | expressions: | |
400 | ||
401 | @deffn {Scheme Procedure} compiled-regexp? obj | |
402 | Test 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: |