add an apropos-hook to ice-9 session
[bpt/guile.git] / doc / ref / misc-modules.texi
CommitLineData
2da09c3f
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
9fd01bce 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2009, 2010, 2011
2da09c3f
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
a0e07ba4 7@node Pretty Printing
3229f68b 8@section Pretty Printing
a0e07ba4
NJ
9
10@c FIXME::martin: Review me!
11
12@cindex pretty printing
13The module @code{(ice-9 pretty-print)} provides the procedure
14@code{pretty-print}, which provides nicely formatted output of Scheme
15objects. This is especially useful for deeply nested or complex data
16structures, such as lists and vectors.
17
b8596c08 18The module is loaded by entering the following:
a0e07ba4
NJ
19
20@lisp
21(use-modules (ice-9 pretty-print))
22@end lisp
23
24This makes the procedure @code{pretty-print} available. As an example
25how @code{pretty-print} will format the output, see the following:
26
27@lisp
28(pretty-print '(define (foo) (lambda (x)
385dbc8b
KR
29(cond ((zero? x) #t) ((negative? x) -x) (else
30(if (= x 1) 2 (* x x x)))))))
a0e07ba4
NJ
31@print{}
32(define (foo)
33 (lambda (x)
34 (cond ((zero? x) #t)
35 ((negative? x) -x)
36 (else (if (= x 1) 2 (* x x x))))))
37@end lisp
38
385dbc8b 39@deffn {Scheme Procedure} pretty-print obj [port] [keyword-options]
a0e07ba4
NJ
40Print the textual representation of the Scheme object @var{obj} to
41@var{port}. @var{port} defaults to the current output port, if not
42given.
385dbc8b
KR
43
44The further @var{keyword-options} are keywords and parameters as
45follows,
46
47@table @asis
48@item @nicode{#:display?} @var{flag}
49If @var{flag} is true then print using @code{display}. The default is
50@code{#f} which means use @code{write} style. (@pxref{Writing})
51
52@item @nicode{#:per-line-prefix} @var{string}
53Print the given @var{string} as a prefix on each line. The default is
54no prefix.
55
56@item @nicode{#:width} @var{columns}
57Print within the given @var{columns}. The default is 79.
58@end table
a0e07ba4
NJ
59@end deffn
60
2a946b44 61
b8596c08
AW
62@cindex truncated printing
63Also exported by the @code{(ice-9 pretty-print)} module is
64@code{truncated-print}, a procedure to print Scheme datums, truncating
65the output to a certain number of characters. This is useful when you
66need to present an arbitrary datum to the user, but you only have one
67line in which to do so.
68
69@lisp
70(define exp '(a b #(c d e) f . g))
71(truncated-print exp #:width 10) (newline)
72@print{} (a b . #)
73(truncated-print exp #:width 15) (newline)
74@print{} (a b # f . g)
75(truncated-print exp #:width 18) (newline)
76@print{} (a b #(c ...) . #)
77(truncated-print exp #:width 20) (newline)
78@print{} (a b #(c d e) f . g)
87a6a236 79(truncated-print "The quick brown fox" #:width 20) (newline)
b8596c08
AW
80@print{} "The quick brown..."
81(truncated-print (current-module) #:width 20) (newline)
82@print{} #<directory (gui...>
83@end lisp
84
c5e05a1c
LC
85@code{truncated-print} will not output a trailing newline. If an expression does
86not fit in the given width, it will be truncated -- possibly
87ellipsized@footnote{On Unicode-capable ports, the ellipsis is represented by
88character `HORIZONTAL ELLIPSIS' (U+2026), otherwise it is represented by three
89dots.}, or in the worst case, displayed as @nicode{#}.
b8596c08
AW
90
91@deffn {Scheme Procedure} truncated-print obj [port] [keyword-options]
92Print @var{obj}, truncating the output, if necessary, to make it fit
93into @var{width} characters. By default, @var{x} will be printed using
ecb87335 94@code{write}, though that behavior can be overridden via the
b8596c08
AW
95@var{display?} keyword argument.
96
97The default behaviour is to print depth-first, meaning that the entire
ecb87335 98remaining width will be available to each sub-expression of @var{x} --
b8596c08
AW
99e.g., if @var{x} is a vector, each member of @var{x}. One can attempt to
100``ration'' the available width, trying to allocate it equally to each
101sub-expression, via the @var{breadth-first?} keyword argument.
102
103The further @var{keyword-options} are keywords and parameters as
104follows,
105
106@table @asis
107@item @nicode{#:display?} @var{flag}
108If @var{flag} is true then print using @code{display}. The default is
109@code{#f} which means use @code{write} style. (@pxref{Writing})
110
111@item @nicode{#:width} @var{columns}
112Print within the given @var{columns}. The default is 79.
113
114@item @nicode{#:breadth-first?} @var{flag}
115If @var{flag} is true, then allocate the available width breadth-first
116among elements of a compound data structure (list, vector, pair,
117etc.). The default is @code{#f} which means that any element is
118allowed to consume all of the available width.
119@end table
120@end deffn
121
122
a0e07ba4 123@node Formatted Output
3229f68b 124@section Formatted Output
a0e07ba4 125@cindex formatted output
a0e07ba4 126
76d3f3d4
KR
127@c For reference, in this section escapes like ~a are given in
128@c @nicode, to give code font in TeX etc, but leave them unadorned in
129@c Info.
130@c
131@c The idea is to reduce clutter around what's shown, and avoid any
132@c possible confusion over whether the ` ' quotes are part of what
133@c should be entered. (In particular for instance of course ' is
134@c meaningful in a format string, introducing a char parameter).
135
136The @code{format} function is a powerful way to print numbers, strings
137and other objects together with literal text under the control of a
138format string. This function is available from
139
140@example
141(use-modules (ice-9 format))
142@end example
143
144A format string is generally more compact and easier than using just
145the standard procedures like @code{display}, @code{write} and
146@code{newline}. Parameters in the output string allow various output
147styles, and parameters can be taken from the arguments for runtime
148flexibility.
149
150@code{format} is similar to the Common Lisp procedure of the same
151name, but it's not identical and doesn't have quite all the features
152found in Common Lisp.
153
154C programmers will note the similarity between @code{format} and
155@code{printf}, though escape sequences are marked with @nicode{~}
156instead of @nicode{%}, and are more powerful.
157
158@sp 1
159@deffn {Scheme Procedure} format dest fmt [args@dots{}]
160Write output specified by the @var{fmt} string to @var{dest}.
161@var{dest} can be an output port, @code{#t} for
c89920a7
AW
162@code{current-output-port} (@pxref{Default Ports}), or @code{#f} to
163return the output as a string.
76d3f3d4
KR
164
165@var{fmt} can contain literal text to be output, and @nicode{~}
166escapes. Each escape has the form
167
168@example
169~ [param [, param@dots{}] [:] [@@] code
170@end example
171
172@nicode{code} is a character determining the escape sequence. The
173@nicode{:} and @nicode{@@} characters are optional modifiers, one or
174both of which change the way various codes operate. Optional
175parameters are accepted by some codes too. Parameters have the
176following forms,
a0e07ba4 177
76d3f3d4 178@table @asis
80a894c9 179@item @nicode{[+/-]number}
76d3f3d4
KR
180An integer, with optional @nicode{+} or @nicode{-}.
181@item @nicode{'} (apostrophe)
182The following character in the format string, for instance @nicode{'z}
183for @nicode{z}.
184@item @nicode{v}
185The next function argument as the parameter. @nicode{v} stands for
186``variable'', a parameter can be calculated at runtime and included in
187the arguments. Upper case @nicode{V} can be used too.
188@item @nicode{#}
189The number of arguments remaining. (See @nicode{~*} below for some
190usages.)
a0e07ba4
NJ
191@end table
192
76d3f3d4
KR
193Parameters are separated by commas (@nicode{,}). A parameter can be
194left empty to keep its default value when supplying later parameters.
a0e07ba4 195
76d3f3d4
KR
196@sp 1
197The following escapes are available. The code letters are not
198case-sensitive, upper and lower case are the same.
a0e07ba4 199
76d3f3d4
KR
200@table @asis
201@item @nicode{~a}
202@itemx @nicode{~s}
203Object output. Parameters: @var{minwidth}, @var{padinc},
204@var{minpad}, @var{padchar}.
205
206@nicode{~a} outputs an argument like @code{display}, @nicode{~s}
207outputs an argument like @code{write} (@pxref{Writing}).
208
209@example
210(format #t "~a" "foo") @print{} foo
211(format #t "~s" "foo") @print{} "foo"
212@end example
213
80a894c9
KR
214@nicode{~:a} and @nicode{~:s} put objects that don't have an external
215representation in quotes like a string.
76d3f3d4
KR
216
217@example
218(format #t "~:a" car) @print{} "#<primitive-procedure car>"
219@end example
220
221If the output is less than @var{minwidth} characters (default 0), it's
80a894c9
KR
222padded on the right with @var{padchar} (default space). @nicode{~@@a}
223and @nicode{~@@s} put the padding on the left instead.
76d3f3d4
KR
224
225@example
226(format #f "~5a" 'abc) @result{} "abc "
227(format #f "~5,,,'-@@a" 'abc) @result{} "--abc"
228@end example
229
230@var{minpad} is a minimum for the padding then plus a multiple of
231@var{padinc}. Ie.@: the padding is @math{@var{minpad} + @var{N} *
232@var{padinc}}, where @var{n} is the smallest integer making the total
233object plus padding greater than or equal to @var{minwidth}. The
234default @var{minpad} is 0 and the default @var{padinc} is 1 (imposing
235no minimum or multiple).
236
237@example
238(format #f "~5,1,4a" 'abc) @result{} "abc "
239@end example
240
241@item @nicode{~c}
242Character. Parameter: @var{charnum}.
243
244Output a character. The default is to simply output, as per
80a894c9
KR
245@code{write-char} (@pxref{Writing}). @nicode{~@@c} prints in
246@code{write} style. @nicode{~:c} prints control characters (ASCII 0
247to 31) in @nicode{^X} form.
76d3f3d4
KR
248
249@example
250(format #t "~c" #\z) @print{} z
251(format #t "~@@c" #\z) @print{} #\z
252(format #t "~:c" #\newline) @print{} ^J
253@end example
254
255If the @var{charnum} parameter is given then an argument is not taken
256but instead the character is @code{(integer->char @var{charnum})}
257(@pxref{Characters}). This can be used for instance to output
258characters given by their ASCII code.
259
260@example
261(format #t "~65c") @print{} A
262@end example
263
264@item @nicode{~d}
265@itemx @nicode{~x}
266@itemx @nicode{~o}
267@itemx @nicode{~b}
268Integer. Parameters: @var{minwidth}, @var{padchar}, @var{commachar},
269@var{commawidth}.
270
271Output an integer argument as a decimal, hexadecimal, octal or binary
272integer (respectively).
273
274@example
275(format #t "~d" 123) @print{} 123
276@end example
277
80a894c9 278@nicode{~@@d} etc shows a @nicode{+} sign is shown on positive
76d3f3d4
KR
279numbers.
280
281@c FIXME: "+" is not shown on zero, unlike in Common Lisp. Should
282@c that be changed in the code, or is it too late and should just be
283@c documented that way?
284
285@example
286(format #t "~@@b" 12) @print{} +1100
287@end example
288
289If the output is less than the @var{minwidth} parameter (default no
290minimum), it's padded on the left with the @var{padchar} parameter
291(default space).
292
293@example
294(format #t "~5,'*d" 12) @print{} ***12
295(format #t "~5,'0d" 12) @print{} 00012
296(format #t "~3d" 1234) @print{} 1234
297@end example
298
80a894c9
KR
299@nicode{~:d} adds commas (or the @var{commachar} parameter) every
300three digits (or the @var{commawidth} parameter many).
76d3f3d4
KR
301
302@example
303(format #t "~:d" 1234567) @print{} 1,234,567
304(format #t "~10,'*,'/,2:d" 12345) @print{} ***1/23/45
305@end example
306
307Hexadecimal @nicode{~x} output is in lower case, but the @nicode{~(}
308and @nicode{~)} case conversion directives described below can be used
309to get upper case.
310
311@example
312(format #t "~x" 65261) @print{} feed
313(format #t "~:@@(~x~)" 65261) @print{} FEED
314@end example
315
316@item @nicode{~r}
317Integer in words, roman numerals, or a specified radix. Parameters:
318@var{radix}, @var{minwidth}, @var{padchar}, @var{commachar},
319@var{commawidth}.
320
321With no parameters output is in words as a cardinal like ``ten'', or
80a894c9 322@nicode{~:r} prints an ordinal like ``tenth''.
76d3f3d4
KR
323
324@example
325(format #t "~r" 9) @print{} nine ;; cardinal
326(format #t "~r" -9) @print{} minus nine ;; cardinal
327(format #t "~:r" 9) @print{} ninth ;; ordinal
328@end example
329
80a894c9 330And also with no parameters, @nicode{~@@r} gives roman numerals and
40296bab 331@nicode{~:@@r} gives old roman numerals. In old roman numerals
80a894c9
KR
332there's no ``subtraction'', so 9 is @nicode{VIIII} instead of
333@nicode{IX}. In both cases only positive numbers can be output.
76d3f3d4
KR
334
335@example
336(format #t "~@@r" 89) @print{} LXXXIX ;; roman
40296bab 337(format #t "~:@@r" 89) @print{} LXXXVIIII ;; old roman
76d3f3d4
KR
338@end example
339
340When a parameter is given it means numeric output in the specified
341@var{radix}. The modifiers and parameters following the radix are the
342same as described for @nicode{~d} etc above.
343
344@example
345(format #f "~3r" 27) @result{} "1000" ;; base 3
346(format #f "~3,5r" 26) @result{} " 222" ;; base 3 width 5
347@end example
348
349@item @nicode{~f}
350Fixed-point float. Parameters: @var{width}, @var{decimals},
351@var{scale}, @var{overflowchar}, @var{padchar}.
352
353Output a number or number string in fixed-point format, ie.@: with a
354decimal point.
355
356@example
357(format #t "~f" 5) @print{} 5.0
358(format #t "~f" "123") @print{} 123.0
359(format #t "~f" "1e-1") @print{} 0.1
360@end example
361
80a894c9
KR
362@nicode{~@@f} prints a @nicode{+} sign on positive numbers (including
363zero).
76d3f3d4
KR
364
365@example
366(format #t "~@@f" 0) @print{} +0.0
367@end example
368
369If the output is less than @var{width} characters it's padded on the
370left with @var{padchar} (space by default). If the output equals or
371exceeds @var{width} then there's no padding. The default for
372@var{width} is no padding.
373
374@example
375(format #f "~6f" -1.5) @result{} " -1.5"
376(format #f "~6,,,,'*f" 23) @result{} "**23.0"
377(format #f "~6f" 1234567.0) @result{} "1234567.0"
378@end example
379
380@var{decimals} is how many digits to print after the decimal point,
381with the value rounded or padded with zeros as necessary. (The
382default is to output as many decimals as required.)
383
384@example
385(format #t "~1,2f" 3.125) @print{} 3.13
386(format #t "~1,2f" 1.5) @print{} 1.50
387@end example
388
389@var{scale} is a power of 10 applied to the value, moving the decimal
390point that many places. A positive @var{scale} increases the value
391shown, a negative decreases it.
392
393@example
394(format #t "~,,2f" 1234) @print{} 123400.0
395(format #t "~,,-2f" 1234) @print{} 12.34
396@end example
397
398If @var{overflowchar} and @var{width} are both given and if the output
399would exceed @var{width}, then that many @var{overflowchar}s are
400printed instead of the value.
401
402@example
9fd01bce
AW
403(format #t "~6,,,'xf" 12345) @print{} 12345.
404(format #t "~5,,,'xf" 12345) @print{} xxxxx
76d3f3d4
KR
405@end example
406
407@item @nicode{~e}
408Exponential float. Parameters: @var{width}, @var{mantdigits},
409@var{expdigits}, @var{intdigits}, @var{overflowchar}, @var{padchar},
410@var{expchar}.
411
412Output a number or number string in exponential notation.
413
414@example
415(format #t "~e" 5000.25) @print{} 5.00025E+3
416(format #t "~e" "123.4") @print{} 1.234E+2
417(format #t "~e" "1e4") @print{} 1.0E+4
418@end example
419
80a894c9
KR
420@nicode{~@@e} prints a @nicode{+} sign on positive numbers (including
421zero). (This is for the mantissa, a @nicode{+} or @nicode{-} sign is
422always shown on the exponent.)
76d3f3d4
KR
423
424@example
425(format #t "~@@e" 5000.0) @print{} +5.0E+3
426@end example
427
428If the output is less than @var{width} characters it's padded on the
429left with @var{padchar} (space by default). The default for
430@var{width} is to output with no padding.
431
432@example
433(format #f "~10e" 1234.0) @result{} " 1.234E+3"
434(format #f "~10,,,,,'*e" 0.5) @result{} "****5.0E-1"
435@end example
436
437@c FIXME: Describe what happens when the number is bigger than WIDTH.
438@c There seems to be a bit of dodginess about this, or some deviation
439@c from Common Lisp.
440
441@var{mantdigits} is the number of digits shown in the mantissa after
442the decimal point. The value is rounded or trailing zeros are added
443as necessary. The default @var{mantdigits} is to show as much as
444needed by the value.
445
446@example
447(format #f "~,3e" 11111.0) @result{} "1.111E+4"
448(format #f "~,8e" 123.0) @result{} "1.23000000E+2"
449@end example
450
451@var{expdigits} is the minimum number of digits shown for the
452exponent, with leading zeros added if necessary. The default for
453@var{expdigits} is to show only as many digits as required. At least
4541 digit is always shown.
455
456@example
457(format #f "~,,1e" 1.0e99) @result{} "1.0E+99"
458(format #f "~,,6e" 1.0e99) @result{} "1.0E+000099"
459@end example
460
461@var{intdigits} (default 1) is the number of digits to show before the
462decimal point in the mantissa. @var{intdigits} can be zero, in which
463case the integer part is a single @nicode{0}, or it can be negative,
464in which case leading zeros are shown after the decimal point.
465
466@c FIXME: When INTDIGITS is 0, Common Lisp format apparently only
467@c shows the single 0 digit if it fits in WIDTH. format.scm seems to
468@c show it always. Is it meant to?
469
470@example
471(format #t "~,,,3e" 12345.0) @print{} 123.45E+2
472(format #t "~,,,0e" 12345.0) @print{} 0.12345E+5
473(format #t "~,,,-3e" 12345.0) @print{} 0.00012345E+8
474@end example
475
476@c FIXME: MANTDIGITS with negative INTDIGITS doesn't match CL spec,
477@c believe the spec says it ought to still show mantdigits+1 sig
679cceed 478@c figures, i.e. leading zeros don't count towards MANTDIGITS, but it
76d3f3d4
KR
479@c seems to just treat MANTDIGITS as how many digits after the
480@c decimal point.
481
482If @var{overflowchar} is given then @var{width} is a hard limit. If
483the output would exceed @var{width} then instead that many
484@var{overflowchar}s are printed.
485
486@example
487(format #f "~6,,,,'xe" 100.0) @result{} "1.0E+2"
488(format #f "~3,,,,'xe" 100.0) @result{} "xxx"
489@end example
490
491@var{expchar} is the exponent marker character (default @nicode{E}).
492
493@example
494(format #t "~,,,,,,'ee" 100.0) @print{} 1.0e+2
495@end example
496
497@item @nicode{~g}
498General float. Parameters: @var{width}, @var{mantdigits},
499@var{expdigits}, @var{intdigits}, @var{overflowchar}, @var{padchar},
500@var{expchar}.
501
502Output a number or number string in either exponential format the same
503as @nicode{~e}, or fixed-point format like @nicode{~f} but aligned
504where the mantissa would have been and followed by padding where the
505exponent would have been.
506
507@c FIXME: The default MANTDIGITS is apparently max(needed,min(n,7))
508@c where 10^(n-1)<=abs(x)<=10^n. But the Common Lisp spec seems to
509@c ask for "needed" to be without leading or trailing zeros, whereas
510@c format.scm seems to include trailing zeros, ending up with it
511@c using fixed format for bigger values than it should.
512
513Fixed-point is used when the absolute value is 0.1 or more and it
514takes no more space than the mantissa in exponential format, ie.@:
515basically up to @var{mantdigits} digits.
516
517@example
518(format #f "~12,4,2g" 999.0) @result{} " 999.0 "
519(format #f "~12,4,2g" "100000") @result{} " 1.0000E+05"
520@end example
521
522The parameters are interpreted as per @nicode{~e} above. When
523fixed-point is used, the @var{decimals} parameter to @nicode{~f} is
524established from @var{mantdigits}, so as to give a total
525@math{@var{mantdigits}+1} figures.
526
527@item @nicode{~$}
528Monetary style fixed-point float. Parameters: @var{decimals},
529@var{intdigits}, @var{width}, @var{padchar}.
530
531@c For reference, fmtdoc.txi from past versions of slib showed the
532@c INTDIGITS parameter as SCALE. That looks like a typo, in the code
533@c and in the Common Lisp spec it's a minimum digits for the integer
534@c part, it isn't a power of 10 like in ~f.
535
536Output a number or number string in fixed-point format, ie.@: with a
537decimal point. @var{decimals} is the number of decimal places to
538show, default 2.
539
540@example
541(format #t "~$" 5) @print{} 5.00
542(format #t "~4$" "2.25") @print{} 2.2500
543(format #t "~4$" "1e-2") @print{} 0.0100
544@end example
545
80a894c9
KR
546@nicode{~@@$} prints a @nicode{+} sign on positive numbers (including
547zero).
76d3f3d4
KR
548
549@example
550(format #t "~@@$" 0) @print{} +0.00
551@end example
552
553@var{intdigits} is a minimum number of digits to show in the integer
554part of the value (default 1).
555
556@example
557(format #t "~,3$" 9.5) @print{} 009.50
558(format #t "~,0$" 0.125) @print{} .13
559@end example
560
561If the output is less than @var{width} characters (default 0), it's
80a894c9
KR
562padded on the left with @var{padchar} (default space). @nicode{~:$}
563puts the padding after the sign.
76d3f3d4
KR
564
565@example
566(format #f "~,,8$" -1.5) @result{} " -1.50"
567(format #f "~,,8:$" -1.5) @result{} "- 1.50"
40296bab 568(format #f "~,,8,'.:@@$" 3) @result{} "+...3.00"
76d3f3d4
KR
569@end example
570
571Note that floating point for dollar amounts is generally not a good
572idea, because a cent @math{0.01} cannot be represented exactly in the
573binary floating point Guile uses, which leads to slowly accumulating
574rounding errors. Keeping values as cents (or fractions of a cent) in
575integers then printing with the scale option in @nicode{~f} may be a
576better approach.
577
578@c For reference, fractions don't work with ~$ (or any of the float
579@c conversions) currently. If they did work then we could perhaps
580@c suggest keeping dollar amounts as rationals, which would of course
581@c give exact cents. An integer as cents is probably still a better
582@c recommendation though, since it forces one to think about where
583@c and when rounding can or should occur.
584
585@item @nicode{~i}
586Complex fixed-point float. Parameters: @var{width}, @var{decimals},
587@var{scale}, @var{overflowchar}, @var{padchar}.
588
589@c For reference, in Common Lisp ~i is an indent, but slib fmtdoc.txi
590@c described it as complex number output, so we keep that.
591
592Output the argument as a complex number, with both real and imaginary
593part shown (even if one or both are zero).
594
595The parameters and modifiers are the same as for fixed-point
596@nicode{~f} described above. The real and imaginary parts are both
597output with the same given parameters and modifiers, except that for
598the imaginary part the @nicode{@@} modifier is always enabled, so as
599to print a @nicode{+} sign between the real and imaginary parts.
600
601@example
602(format #t "~i" 1) @print{} 1.0+0.0i
603@end example
604
605@item @nicode{~p}
606Plural. No parameters.
607
608Output nothing if the argument is 1, or @samp{s} for any other
609value.
610
611@example
612(format #t "enter name~p" 1) @print{} enter name
613(format #t "enter name~p" 2) @print{} enter names
614@end example
615
80a894c9 616@nicode{~@@p} prints @samp{y} for 1 or @samp{ies} otherwise.
76d3f3d4
KR
617
618@example
619(format #t "pupp~@@p" 1) @print{} puppy
620(format #t "pupp~@@p" 2) @print{} puppies
621@end example
622
80a894c9
KR
623@nicode{~:p} re-uses the preceding argument instead of taking a new
624one, which can be convenient when printing some sort of count.
76d3f3d4
KR
625
626@example
80a894c9 627(format #t "~d cat~:p" 9) @print{} 9 cats
40296bab 628(format #t "~d pupp~:@@p" 5) @print{} 5 puppies
76d3f3d4
KR
629@end example
630
80a894c9
KR
631@nicode{~p} is designed for English plurals and there's no attempt to
632support other languages. @nicode{~[} conditionals (below) may be able
633to help. When using @code{gettext} to translate messages
634@code{ngettext} is probably best though
635(@pxref{Internationalization}).
636
76d3f3d4 637@item @nicode{~y}
b8596c08
AW
638Structured printing. Parameters: @var{width}.
639
640@nicode{~y} outputs an argument using @code{pretty-print}
641(@pxref{Pretty Printing}). The result will be formatted to fit within
642@var{width} columns (79 by default), consuming multiple lines if
643necessary.
76d3f3d4 644
b8596c08
AW
645@nicode{~@@y} outputs an argument using @code{truncated-print}
646(@pxref{Pretty Printing}). The resulting code will be formatted to fit
647within @var{width} columns (79 by default), on a single line. The
648output will be truncated if necessary.
76d3f3d4 649
9274c3dd
AW
650@nicode{~:@@y} is like @nicode{~@@y}, except the @var{width} parameter
651is interpreted to be the maximum column to which to output. That is to
652say, if you are at column 10, and @nicode{~60:@@y} is seen, the datum
653will be truncated to 50 columns.
76d3f3d4
KR
654
655@item @nicode{~?}
656@itemx @nicode{~k}
657Sub-format. No parameters.
658
659Take a format string argument and a second argument which is a list of
80a894c9
KR
660arguments for that string, and output the result.
661
662@example
663(format #t "~?" "~d ~d" '(1 2)) @print{} 1 2
664@end example
665
666@nicode{~@@?} takes arguments for the sub-format directly rather than
667in a list.
76d3f3d4
KR
668
669@example
76d3f3d4
KR
670(format #t "~@@? ~s" "~d ~d" 1 2 "foo") @print{} 1 2 "foo"
671@end example
672
673@nicode{~?} and @nicode{~k} are the same, @nicode{~k} is provided for
674T-Scheme compatibility.
675
676@item @nicode{~*}
677Argument jumping. Parameter: @var{N}.
678
80a894c9
KR
679Move forward @var{N} arguments (default 1) in the argument list.
680@nicode{~:*} moves backwards. (@var{N} cannot be negative.)
a0e07ba4 681
76d3f3d4 682@example
471d2c6d
KR
683(format #f "~d ~2*~d" 1 2 3 4) @result{} "1 4"
684(format #f "~d ~:*~d" 6) @result{} "6 6"
76d3f3d4 685@end example
a0e07ba4 686
80a894c9
KR
687@nicode{~@@*} moves to argument number @var{N}. The first argument is
688number 0 (and that's the default for @var{N}).
a0e07ba4 689
76d3f3d4
KR
690@example
691(format #f "~d~d again ~@@*~d~d" 1 2) @result{} "12 again 12"
692(format #f "~d~d~d ~1@@*~d~d" 1 2 3) @result{} "123 23"
693@end example
a0e07ba4 694
471d2c6d
KR
695A @nicode{#} move to the end followed by a @nicode{:} modifier move
696back can be used for an absolute position relative to the end of the
697argument list, a reverse of what the @nicode{@@} modifier does.
a0e07ba4 698
76d3f3d4 699@example
471d2c6d 700(format #t "~#*~2:*~a" 'a 'b 'c 'd) @print{} c
76d3f3d4 701@end example
a0e07ba4 702
72b3aa56 703At the end of the format string the current argument position doesn't
471d2c6d 704matter, any further arguments are ignored.
a0e07ba4 705
76d3f3d4
KR
706@item @nicode{~t}
707Advance to a column position. Parameters: @var{colnum}, @var{colinc},
708@var{padchar}.
a0e07ba4 709
76d3f3d4
KR
710Output @var{padchar} (space by default) to move to the given
711@var{colnum} column. The start of the line is column 0, the default
712for @var{colnum} is 1.
a0e07ba4 713
76d3f3d4
KR
714@example
715(format #f "~tX") @result{} " X"
716(format #f "~3tX") @result{} " X"
717@end example
a0e07ba4 718
76d3f3d4
KR
719If the current column is already past @var{colnum}, then the move is
720to there plus a multiple of @var{colinc}, ie.@: column
721@math{@var{colnum} + @var{N} * @var{colinc}} for the smallest @var{N}
722which makes that value greater than or equal to the current column.
723The default @var{colinc} is 1 (which means no further move).
a0e07ba4 724
76d3f3d4
KR
725@example
726(format #f "abcd~2,5,'.tx") @result{} "abcd...x"
727@end example
a0e07ba4 728
80a894c9
KR
729@nicode{~@@t} takes @var{colnum} as an offset from the current column.
730@var{colnum} many pad characters are output, then further padding to
731make the current column a multiple of @var{colinc}, if it isn't
732already so.
76d3f3d4
KR
733
734@example
735(format #f "a~3,5'*@@tx") @result{} "a****x"
736@end example
737
471d2c6d
KR
738@nicode{~t} is implemented using @code{port-column} (@pxref{Reading}),
739so it works even there has been other output before @code{format}.
740
76d3f3d4
KR
741@item @nicode{~~}
742Tilde character. Parameter: @var{n}.
743
744Output a tilde character @nicode{~}, or @var{n} many if a parameter is
745given. Normally @nicode{~} introduces an escape sequence, @nicode{~~}
746is the way to output a literal tilde.
747
748@item @nicode{~%}
749Newline. Parameter: @var{n}.
750
751Output a newline character, or @var{n} many if a parameter is given.
752A newline (or a few newlines) can of course be output just by
753including them in the format string.
754
755@item @nicode{~&}
756Start a new line. Parameter: @var{n}.
757
758Output a newline if not already at the start of a line. With a
759parameter, output that many newlines, but with the first only if not
760already at the start of a line. So for instance 3 would be a newline
761if not already at the start of a line, and 2 further newlines.
762
763@item @nicode{~_}
764Space character. Parameter: @var{n}.
765
766@c For reference, in Common Lisp ~_ is a conditional newline, but
767@c slib fmtdoc.txi described it as a space, so we keep that.
768
769Output a space character, or @var{n} many if a parameter is given.
770
771With a variable parameter this is one way to insert runtime calculated
772padding (@nicode{~t} or the various field widths can do similar
773things).
774
775@example
776(format #f "~v_foo" 4) @result{} " foo"
777@end example
778
779@item @nicode{~/}
780Tab character. Parameter: @var{n}.
781
782Output a tab character, or @var{n} many if a parameter is given.
783
784@item @nicode{~|}
785Formfeed character. Parameter: @var{n}.
786
787Output a formfeed character, or @var{n} many if a parameter is given.
788
789@item @nicode{~!}
790Force output. No parameters.
791
792At the end of output, call @code{force-output} to flush any buffers on
793the destination (@pxref{Writing}). @nicode{~!} can occur anywhere in
794the format string, but the force is done at the end of output.
795
796When output is to a string (destination @code{#f}), @nicode{~!} does
797nothing.
798
799@item @nicode{~newline} (ie.@: newline character)
800Continuation line. No parameters.
801
802Skip this newline and any following whitespace in the format string,
80a894c9
KR
803ie.@: don't send it to the output. This can be used to break up a
804long format string for readability, but not print the extra
76d3f3d4
KR
805whitespace.
806
76d3f3d4
KR
807@example
808(format #f "abc~
809 ~d def~
810 ~d" 1 2) @result{} "abc1 def2"
811@end example
812
80a894c9
KR
813@nicode{~:newline} skips the newline but leaves any further whitespace
814to be printed normally.
815
816@nicode{~@@newline} prints the newline then skips following
817whitespace.
818
76d3f3d4 819@item @nicode{~(} @nicode{~)}
f0a9ab4d
KR
820Case conversion. No parameters.
821
822Between @nicode{~(} and @nicode{~)} the case of all output is changed.
823The modifiers on @nicode{~(} control the conversion.
76d3f3d4 824
f47029a1 825@itemize @w{}
76d3f3d4 826@item
80a894c9 827@nicode{~(} --- lower case.
76d3f3d4
KR
828@c
829@c FIXME: The : and @ modifiers are not yet documented because the
830@c code applies string-capitalize and string-capitalize-first to each
831@c separate format:out-str call, which has various subtly doubtful
832@c effects. And worse they're applied to individual characters,
833@c including literal characters in the format string, which has the
834@c silly effect of being always an upcase.
835@c
836@c The Common Lisp spec is apparently for the capitalization to be
837@c applied in one hit to the whole of the output between ~( and ~).
838@c (This can no doubt be implemented without accumulating all that
839@c text, just by keeping a state or the previous char to tell whether
840@c within a word.)
841@c
842@c @item
843@c @nicode{:} --- first letter of each word upper case, the rest lower
844@c case, as per the @code{string-capitalize} function (@pxref{Alphabetic
845@c Case Mapping}).
846@c @item
847@c @nicode{@@} --- first letter of just the first word upper case, the
848@c rest lower case.
849@c
850@item
40296bab 851@nicode{~:@@(} --- upper case.
76d3f3d4
KR
852@end itemize
853
854For example,
855
856@example
857(format #t "~(Hello~)") @print{} hello
40296bab 858(format #t "~:@@(Hello~)") @print{} HELLO
76d3f3d4
KR
859@end example
860
861In the future it's intended the modifiers @nicode{:} and @nicode{@@}
862alone will capitalize the first letters of words, as per Common Lisp
863@code{format}, but the current implementation of this is flawed and
864not recommended for use.
865
866Case conversions do not nest, currently. This might change in the
867future, but if it does then it will be to Common Lisp style where the
868outermost conversion has priority, overriding inner ones (making those
869fairly pointless).
870
871@item @nicode{~@{} @nicode{~@}}
872Iteration. Parameter: @var{maxreps} (for @nicode{~@{}).
873
874The format between @nicode{~@{} and @nicode{~@}} is iterated. The
875modifiers to @nicode{~@{} determine how arguments are taken. The
876default is a list argument with each iteration successively consuming
877elements from it. This is a convenient way to output a whole list.
878
879@example
880(format #t "~@{~d~@}" '(1 2 3)) @print{} 123
881(format #t "~@{~s=~d ~@}" '("x" 1 "y" 2)) @print{} "x"=1 "y"=2
882@end example
883
80a894c9
KR
884@nicode{~:@{} takes a single argument which is a list of lists, each
885of those contained lists gives the arguments for the iterated format.
76d3f3d4 886
40296bab 887@c @print{} on a new line here to avoid overflowing page width in DVI
76d3f3d4 888@example
40296bab
KR
889(format #t "~:@{~dx~d ~@}" '((1 2) (3 4) (5 6)))
890@print{} 1x2 3x4 5x6
76d3f3d4
KR
891@end example
892
80a894c9
KR
893@nicode{~@@@{} takes arguments directly, with each iteration
894successively consuming arguments.
76d3f3d4
KR
895
896@example
897(format #t "~@@@{~d~@}" 1 2 3) @print{} 123
898(format #t "~@@@{~s=~d ~@}" "x" 1 "y" 2) @print{} "x"=1 "y"=2
899@end example
900
40296bab 901@nicode{~:@@@{} takes list arguments, one argument for each iteration,
80a894c9 902using that list for the format.
76d3f3d4 903
40296bab 904@c @print{} on a new line here to avoid overflowing page width in DVI
76d3f3d4 905@example
40296bab
KR
906(format #t "~:@@@{~dx~d ~@}" '(1 2) '(3 4) '(5 6))
907@print{} 1x2 3x4 5x6
76d3f3d4
KR
908@end example
909
910Iterating stops when there are no more arguments or when the
911@var{maxreps} parameter to @nicode{~@{} is reached (default no
912maximum).
913
914@example
915(format #t "~2@{~d~@}" '(1 2 3 4)) @print{} 12
916@end example
917
918If the format between @nicode{~@{} and @nicode{~@}} is empty, then a
919format string argument is taken (before iteration argument(s)) and
920used instead. This allows a sub-format (like @nicode{~?} above) to be
921iterated.
922
923@example
924(format #t "~@{~@}" "~d" '(1 2 3)) @print{} 123
925@end example
926
927@c FIXME: What is the @nicode{:} modifier to ~} meant to do? The
928@c Common Lisp spec says it's a minimum of 1 iteration, but the
929@c format.scm code seems to merely make it have MAXREPS default to 1.
930
931Iterations can be nested, an inner iteration operates in the same way
932as described, but of course on the arguments the outer iteration
933provides it. This can be used to work into nested list structures.
934For example in the following the inner @nicode{~@{~d~@}x} is applied
935to @code{(1 2)} then @code{(3 4 5)} etc.
936
937@example
938(format #t "~@{~@{~d~@}x~@}" '((1 2) (3 4 5))) @print{} 12x345x
939@end example
940
80a894c9
KR
941See also @nicode{~^} below for escaping from iteration.
942
76d3f3d4
KR
943@item @nicode{~[} @nicode{~;} @nicode{~]}
944Conditional. Parameter: @var{selector}.
945
946A conditional block is delimited by @nicode{~[} and @nicode{~]}, and
947@nicode{~;} separates clauses within the block. @nicode{~[} takes an
948integer argument and that number clause is used. The first clause is
949number 0.
950
951@example
952(format #f "~[peach~;banana~;mango~]" 1) @result{} "banana"
953@end example
954
955The @var{selector} parameter can be used for the clause number,
956instead of taking an argument.
957
958@example
959(format #f "~2[peach~;banana~;mango~]") @result{} "mango"
960@end example
961
962If the clause number is out of range then nothing is output. Or the
80a894c9 963last clause can be @nicode{~:;} to use that for a number out of range.
76d3f3d4
KR
964
965@example
966(format #f "~[banana~;mango~]" 99) @result{} ""
967(format #f "~[banana~;mango~:;fruit~]" 99) @result{} "fruit"
968@end example
969
80a894c9
KR
970@nicode{~:[} treats the argument as a flag, and expects two clauses.
971The first is used if the argument is @code{#f} or the second
972otherwise.
76d3f3d4
KR
973
974@example
975(format #f "~:[false~;not false~]" #f) @result{} "false"
976(format #f "~:[false~;not false~]" 'abc) @result{} "not false"
977
978(let ((n 3))
979 (format #t "~d gnu~:[s are~; is~] here" n (= 1 n)))
980@print{} 3 gnus are here
981@end example
982
80a894c9
KR
983@nicode{~@@[} also treats the argument as a flag, and expects one
984clause. If the argument is @code{#f} then no output is produced and
985the argument is consumed, otherwise the clause is used and the
986argument is not consumed, it's left for the clause. This can be used
987for instance to suppress output if @code{#f} means something not
988available.
76d3f3d4
KR
989
990@example
991(format #f "~@@[temperature=~d~]" 27) @result{} "temperature=27"
992(format #f "~@@[temperature=~d~]" #f) @result{} ""
993@end example
994
995@item @nicode{~^}
996Escape. Parameters: @var{val1}, @var{val2}, @var{val3}.
997
998Stop formatting if there are no more arguments. This can be used for
80a894c9 999instance to have a format string adapt to a variable number of
76d3f3d4
KR
1000arguments.
1001
1002@example
1003(format #t "~d~^ ~d" 1) @print{} 1
1004(format #t "~d~^ ~d" 1 2) @print{} 1 2
1005@end example
1006
1007Within a @nicode{~@{} @nicode{~@}} iteration, @nicode{~^} stops the
1008current iteration step if there are no more arguments to that step,
80a894c9
KR
1009but continuing with possible further steps and the rest of the format.
1010This can be used for instance to avoid a separator on the last
1011iteration, or to adapt to variable length argument lists.
76d3f3d4
KR
1012
1013@example
1014(format #f "~@{~d~^/~@} go" '(1 2 3)) @result{} "1/2/3 go"
1015(format #f "~:@{ ~d~^~d~@} go" '((1) (2 3))) @result{} " 1 23 go"
1016@end example
1017
1018@c For reference, format.scm doesn't implement that Common Lisp ~:^
1019@c modifier which stops the entire iterating of ~:{ or ~@:{.
1020
1021@c FIXME: Believe the Common Lisp spec is for ~^ within ~[ ~]
1022@c conditional to terminate the whole format (or iteration step if in
1023@c an iteration). But format.scm seems to terminate just the
1024@c conditional form.
1025@c
1026@c (format #f "~[abc~^def~;ghi~] blah" 0)
1027@c @result{} "abc blah" ;; looks wrong
1028
1029@c FIXME: Believe the Common Lisp spec is for ~^ within ~( ~) to end
1030@c that case conversion and then also terminate the whole format (or
1031@c iteration step if in an iteration). But format.scm doesn't seem
1032@c to do that quite right.
1033@c
1034@c (format #f "~d ~^ ~d" 1) @result{} "1 "
1035@c (format #f "~(~d ~^ ~d~)" 1) @result{} ERROR
1036
1037Within a @nicode{~?} sub-format, @nicode{~^} operates just on that
1038sub-format. If it terminates the sub-format then the originating
1039format will still continue.
1040
1041@example
1042(format #t "~? items" "~d~^ ~d" '(1)) @print{} 1 items
1043(format #t "~? items" "~d~^ ~d" '(1 2)) @print{} 1 2 items
1044@end example
1045
1046The parameters to @nicode{~^} (which are numbers) change the condition
1047used to terminate. For a single parameter, termination is when that
1048value is zero (notice this makes plain @nicode{~^} equivalent to
1049@nicode{~#^}). For two parameters, termination is when those two are
1050equal. For three parameters, termination is when @math{@var{val1}
1051@le{} @var{val2}} and @math{@var{val2} @le{} @var{val3}}.
1052
1053@c FIXME: Good examples of these?
1054
1055@item @nicode{~q}
80a894c9
KR
1056Inquiry message. Insert a copyright message into the output.
1057
1058@nicode{~:q} inserts the format implementation version.
a0e07ba4
NJ
1059@end table
1060
76d3f3d4 1061@sp 1
471d2c6d
KR
1062It's an error if there are not enough arguments for the escapes in the
1063format string, but any excess arguments are ignored.
76d3f3d4
KR
1064
1065Iterations @nicode{~@{} @nicode{~@}} and conditionals @nicode{~[}
1066@nicode{~;} @nicode{~]} can be nested, but must be properly nested,
1067meaning the inner form must be entirely within the outer form. So
1068it's not possible, for instance, to try to conditionalize the endpoint
1069of an iteration.
1070
1071@example
1072(format #t "~@{ ~[ ... ~] ~@}" ...) ;; good
1073(format #t "~@{ ~[ ... ~@} ... ~]" ...) ;; bad
1074@end example
1075
1076The same applies to case conversions @nicode{~(} @nicode{~)}, they
1077must properly nest with respect to iterations and conditionals (though
1078currently a case conversion cannot nest within another case
1079conversion).
1080
1081When a sub-format (@nicode{~?}) is used, that sub-format string must
1082be self-contained. It cannot for instance give a @nicode{~@{} to
1083begin an iteration form and have the @nicode{~@}} up in the
1084originating format, or similar.
a0e07ba4
NJ
1085@end deffn
1086
76d3f3d4
KR
1087@sp 1
1088Guile contains a @code{format} procedure even when the module
1089@code{(ice-9 format)} is not loaded. The default @code{format} is
1090@code{simple-format} (@pxref{Writing}), it doesn't support all escape
1091sequences documented in this section, and will signal an error if you
1092try to use one of them. The reason for two versions is that the full
1093@code{format} is fairly large and requires some time to load.
1094@code{simple-format} is often adequate too.
a0e07ba4
NJ
1095
1096
6da1534c 1097@node File Tree Walk
3229f68b 1098@section File Tree Walk
6da1534c
KR
1099@cindex file tree walk
1100
1101The functions in this section traverse a tree of files and
1102directories, in a fashion similar to the C @code{ftw} and @code{nftw}
1103routines (@pxref{Working with Directory Trees,,, libc, GNU C Library
1104Reference Manual}).
1105
1106@example
1107(use-modules (ice-9 ftw))
1108@end example
1109@sp 1
1110
1111@defun ftw startname proc ['hash-size n]
c4e84357 1112Walk the file system tree descending from @var{startname}, calling
6da1534c
KR
1113@var{proc} for each file and directory.
1114
1115Hard links and symbolic links are followed. A file or directory is
1116reported to @var{proc} only once, and skipped if seen again in another
1117place. One consequence of this is that @code{ftw} is safe against
1118circularly linked directory structures.
1119
1120Each @var{proc} call is @code{(@var{proc} filename statinfo flag)} and
1121it should return @code{#t} to continue, or any other value to stop.
1122
1123@var{filename} is the item visited, being @var{startname} plus a
1124further path and the name of the item. @var{statinfo} is the return
1125from @code{stat} (@pxref{File System}) on @var{filename}. @var{flag}
1126is one of the following symbols,
1127
1128@table @code
1129@item regular
1130@var{filename} is a file, this includes special files like devices,
1131named pipes, etc.
1132
1133@item directory
1134@var{filename} is a directory.
1135
1136@item invalid-stat
1137An error occurred when calling @code{stat}, so nothing is known.
1138@var{statinfo} is @code{#f} in this case.
1139
1140@item directory-not-readable
1141@var{filename} is a directory, but one which cannot be read and hence
1142won't be recursed into.
1143
1144@item symlink
1145@var{filename} is a dangling symbolic link. Symbolic links are
1146normally followed and their target reported, the link itself is
1147reported if the target does not exist.
1148@end table
1149
1150The return value from @code{ftw} is @code{#t} if it ran to completion,
1151or otherwise the non-@code{#t} value from @var{proc} which caused the
1152stop.
1153
1154Optional argument symbol @code{hash-size} and an integer can be given
1155to set the size of the hash table used to track items already visited.
1156(@pxref{Hash Table Reference})
1157
1158@c Actually, it's probably safe to escape from ftw, just need to
1159@c check it.
1160@c
1161In the current implementation, returning non-@code{#t} from @var{proc}
1162is the only valid way to terminate @code{ftw}. @var{proc} must not
1163use @code{throw} or similar to escape.
1164@end defun
1165
1166
1167@defun nftw startname proc ['chdir] ['depth] ['hash-size n] ['mount] ['physical]
c4e84357 1168Walk the file system tree starting at @var{startname}, calling
6da1534c
KR
1169@var{proc} for each file and directory. @code{nftw} has extra
1170features over the basic @code{ftw} described above.
1171
40296bab
KR
1172Like @code{ftw}, hard links and symbolic links are followed. A file
1173or directory is reported to @var{proc} only once, and skipped if seen
1174again in another place. One consequence of this is that @code{nftw}
1175is safe against circular linked directory structures.
6da1534c
KR
1176
1177Each @var{proc} call is @code{(@var{proc} filename statinfo flag
40296bab 1178base level)} and it should return @code{#t} to continue, or any
6da1534c
KR
1179other value to stop.
1180
1181@var{filename} is the item visited, being @var{startname} plus a
1182further path and the name of the item. @var{statinfo} is the return
40296bab
KR
1183from @code{stat} on @var{filename} (@pxref{File System}). @var{base}
1184is an integer offset into @var{filename} which is where the basename
1185for this item begins. @var{level} is an integer giving the directory
1186nesting level, starting from 0 for the contents of @var{startname} (or
1187that item itself if it's a file). @var{flag} is one of the following
1188symbols,
6da1534c
KR
1189
1190@table @code
1191@item regular
40296bab
KR
1192@var{filename} is a file, including special files like devices, named
1193pipes, etc.
6da1534c
KR
1194
1195@item directory
1196@var{filename} is a directory.
1197
1198@item directory-processed
1199@var{filename} is a directory, and its contents have all been visited.
1200This flag is given instead of @code{directory} when the @code{depth}
1201option below is used.
1202
1203@item invalid-stat
1204An error occurred when applying @code{stat} to @var{filename}, so
1205nothing is known about it. @var{statinfo} is @code{#f} in this case.
1206
1207@item directory-not-readable
1208@var{filename} is a directory, but one which cannot be read and hence
1209won't be recursed into.
1210
6da1534c 1211@item stale-symlink
40296bab
KR
1212@var{filename} is a dangling symbolic link. Links are normally
1213followed and their target reported, the link itself is reported if its
1214target does not exist.
1215
1216@item symlink
1217When the @code{physical} option described below is used, this
1218indicates @var{filename} is a symbolic link whose target exists (and
1219is not being followed).
6da1534c
KR
1220@end table
1221
1222The following optional arguments can be given to modify the way
1223@code{nftw} works. Each is passed as a symbol (and @code{hash-size}
1224takes a following integer value).
1225
1226@table @asis
1227@item @code{chdir}
1228Change to the directory containing the item before calling @var{proc}.
1229When @code{nftw} returns the original current directory is restored.
1230
40296bab
KR
1231Under this option, generally the @var{base} parameter to each
1232@var{proc} call should be used to pick out the base part of the
1233@var{filename}. The @var{filename} is still a path but with a changed
1234directory it won't be valid (unless the @var{startname} directory was
1235absolute).
6da1534c
KR
1236
1237@item @code{depth}
1238Visit files ``depth first'', meaning @var{proc} is called for the
1239contents of each directory before it's called for the directory
1240itself. Normally a directory is reported first, then its contents.
1241
1242Under this option, the @var{flag} to @var{proc} for a directory is
1243@code{directory-processed} instead of @code{directory}.
1244
1245@item @code{hash-size @var{n}}
1246Set the size of the hash table used to track items already visited.
1247(@pxref{Hash Table Reference})
1248
1249@item @code{mount}
1250Don't cross a mount point, meaning only visit items on the same
c4e84357 1251file system as @var{startname} (ie.@: the same @code{stat:dev}).
6da1534c
KR
1252
1253@item @code{physical}
1254Don't follow symbolic links, instead report them to @var{proc} as
40296bab
KR
1255@code{symlink}. Dangling links (those whose target doesn't exist) are
1256still reported as @code{stale-symlink}.
6da1534c
KR
1257@end table
1258
1259The return value from @code{nftw} is @code{#t} if it ran to
1260completion, or otherwise the non-@code{#t} value from @var{proc} which
1261caused the stop.
1262
ecb87335 1263@c For reference, one reason not to escape is that the current
6da1534c
KR
1264@c directory is not saved and restored with dynamic-wind. Maybe
1265@c changing that would be enough to allow escaping.
1266@c
1267In the current implementation, returning non-@code{#t} from @var{proc}
1268is the only valid way to terminate @code{ftw}. @var{proc} must not
1269use @code{throw} or similar to escape.
1270@end defun
1271
1272
2370f809 1273@node Queues
3229f68b 1274@section Queues
d10196fc 1275@cindex queues
2370f809
KR
1276@tindex Queues
1277
1278@noindent
1279The functions in this section are provided by
1280
1281@example
1282(use-modules (ice-9 q))
1283@end example
1284
1285This module implements queues holding arbitrary scheme objects and
1286designed for efficient first-in / first-out operations.
1287
1288@code{make-q} creates a queue, and objects are entered and removed
23f2b9a3 1289with @code{enq!} and @code{deq!}. @code{q-push!} and @code{q-pop!}
2370f809
KR
1290can be used too, treating the front of the queue like a stack.
1291
1292@sp 1
1293
1294@deffn {Scheme Procedure} make-q
1295Return a new queue.
1296@end deffn
1297
1298@deffn {Scheme Procedure} q? obj
1299Return @code{#t} if @var{obj} is a queue, or @code{#f} if not.
1300
1301Note that queues are not a distinct class of objects but are
1302implemented with cons cells. For that reason certain list structures
1303can get @code{#t} from @code{q?}.
1304@end deffn
1305
1306@deffn {Scheme Procedure} enq! q obj
1307Add @var{obj} to the rear of @var{q}, and return @var{q}.
1308@end deffn
1309
1310@deffn {Scheme Procedure} deq! q
1311@deffnx {Scheme Procedure} q-pop! q
1312Remove and return the front element from @var{q}. If @var{q} is
1313empty, a @code{q-empty} exception is thrown.
1314
1315@code{deq!} and @code{q-pop!} are the same operation, the two names
1316just let an application match @code{enq!} with @code{deq!}, or
1317@code{q-push!} with @code{q-pop!}.
1318@end deffn
1319
1320@deffn {Scheme Procedure} q-push! q obj
1321Add @var{obj} to the front of @var{q}, and return @var{q}.
1322@end deffn
1323
1324@deffn {Scheme Procedure} q-length q
1325Return the number of elements in @var{q}.
1326@end deffn
1327
1328@deffn {Scheme Procedure} q-empty? q
1329Return true if @var{q} is empty.
1330@end deffn
1331
1332@deffn {Scheme Procedure} q-empty-check q
1333Throw a @code{q-empty} exception if @var{q} is empty.
1334@end deffn
1335
1336@deffn {Scheme Procedure} q-front q
1337Return the first element of @var{q} (without removing it). If @var{q}
1338is empty, a @code{q-empty} exception is thrown.
1339@end deffn
1340
1341@deffn {Scheme Procedure} q-rear q
1342Return the last element of @var{q} (without removing it). If @var{q}
1343is empty, a @code{q-empty} exception is thrown.
1344@end deffn
1345
1346@deffn {Scheme Procedure} q-remove! q obj
72b3aa56 1347Remove all occurrences of @var{obj} from @var{q}, and return @var{q}.
2370f809
KR
1348@var{obj} is compared to queue elements using @code{eq?}.
1349@end deffn
1350
1351@sp 1
1352@cindex @code{q-empty}
1353The @code{q-empty} exceptions described above are thrown just as
1354@code{(throw 'q-empty)}, there's no message etc like an error throw.
1355
1356A queue is implemented as a cons cell, the @code{car} containing a
1357list of queued elements, and the @code{cdr} being the last cell in
1358that list (for ease of enqueuing).
1359
1360@example
1361(@var{list} . @var{last-cell})
1362@end example
1363
1364@noindent
1365If the queue is empty, @var{list} is the empty list and
1366@var{last-cell} is @code{#f}.
1367
1368An application can directly access the queue list if desired, for
1369instance to search the elements or to insert at a specific point.
1370
1371@deffn {Scheme Procedure} sync-q! q
1372Recompute the @var{last-cell} field in @var{q}.
1373
1374All the operations above maintain @var{last-cell} as described, so
1375normally there's no need for @code{sync-q!}. But if an application
1376modifies the queue @var{list} then it must either maintain
1377@var{last-cell} similarly, or call @code{sync-q!} to recompute it.
1378@end deffn
1379
1380
458dd501
KR
1381@node Streams
1382@section Streams
1383@cindex streams
1384
1385A stream represents a sequence of values, each of which is calculated
1386only when required. This allows large or even infinite sequences to
1387be represented and manipulated with familiar operations like ``car'',
1388``cdr'', ``map'' or ``fold''. In such manipulations only as much as
1389needed is actually held in memory at any one time. The functions in
1390this section are available from
1391
1392@example
1393(use-modules (ice-9 streams))
1394@end example
1395
1396Streams are implemented using promises (@pxref{Delayed Evaluation}),
1397which is how the underlying calculation of values is made only when
1398needed, and the values then retained so the calculation is not
1399repeated.
1400
1401@noindent
1402Here is a simple example producing a stream of all odd numbers,
1403
1404@example
1405(define odds (make-stream (lambda (state)
1406 (cons state (+ state 2)))
1407 1))
1408(stream-car odds) @result{} 1
1409(stream-car (stream-cdr odds)) @result{} 3
1410@end example
1411
1412@noindent
1413@code{stream-map} could be used to derive a stream of odd squares,
1414
1415@example
1416(define (square n) (* n n))
1417(define oddsquares (stream-map square odds))
1418@end example
1419
1420These are infinite sequences, so it's not possible to convert them to
1421a list, but they could be printed (infinitely) with for example
1422
1423@example
1424(stream-for-each (lambda (n sq)
1425 (format #t "~a squared is ~a\n" n sq))
1426 odds oddsquares)
1427@print{}
14281 squared is 1
14293 squared is 9
14305 squared is 25
14317 squared is 49
1432@dots{}
1433@end example
1434
1435@sp 1
1436@defun make-stream proc initial-state
1437Return a new stream, formed by calling @var{proc} successively.
1438
1439Each call is @code{(@var{proc} @var{state})}, it should return a pair,
1440the @code{car} being the value for the stream, and the @code{cdr}
1441being the new @var{state} for the next call. For the first call
1442@var{state} is the given @var{initial-state}. At the end of the
1443stream, @var{proc} should return some non-pair object.
1444@end defun
1445
1446@defun stream-car stream
1447Return the first element from @var{stream}. @var{stream} must not be
1448empty.
1449@end defun
1450
1451@defun stream-cdr stream
1452Return a stream which is the second and subsequent elements of
1453@var{stream}. @var{stream} must not be empty.
1454@end defun
1455
1456@defun stream-null? stream
1457Return true if @var{stream} is empty.
1458@end defun
1459
1460@defun list->stream list
1461@defunx vector->stream vector
1462Return a stream with the contents of @var{list} or @var{vector}.
1463
1464@var{list} or @var{vector} should not be modified subsequently, since
1465it's unspecified whether changes there will be reflected in the stream
1466returned.
1467@end defun
1468
1469@defun port->stream port readproc
1470Return a stream which is the values obtained by reading from
1471@var{port} using @var{readproc}. Each read call is
1472@code{(@var{readproc} @var{port})}, and it should return an EOF object
1473(@pxref{Reading}) at the end of input.
1474
1475For example a stream of characters from a file,
1476
1477@example
1478(port->stream (open-input-file "/foo/bar.txt") read-char)
1479@end example
1480@end defun
1481
1482@defun stream->list stream
1483Return a list which is the entire contents of @var{stream}.
1484@end defun
1485
1486@defun stream->reversed-list stream
1487Return a list which is the entire contents of @var{stream}, but in
1488reverse order.
1489@end defun
1490
1491@defun stream->list&length stream
5179b0e2
KR
1492Return two values (@pxref{Multiple Values}), being firstly a list
1493which is the entire contents of @var{stream}, and secondly the number
1494of elements in that list.
458dd501
KR
1495@end defun
1496
1497@defun stream->reversed-list&length stream
5179b0e2
KR
1498Return two values (@pxref{Multiple Values}) being firstly a list which
1499is the entire contents of @var{stream}, but in reverse order, and
1500secondly the number of elements in that list.
458dd501
KR
1501@end defun
1502
1503@defun stream->vector stream
1504Return a vector which is the entire contents of @var{stream}.
1505@end defun
1506
1507@defun stream-fold proc init stream0 @dots{} streamN
1508Apply @var{proc} successively over the elements of the given streams,
1509from first to last until the end of the shortest stream is reached.
1510Return the result from the last @var{proc} call.
1511
1512Each call is @code{(@var{proc} elem0 @dots{} elemN prev)}, where each
1513@var{elem} is from the corresponding @var{stream}. @var{prev} is the
1514return from the previous @var{proc} call, or the given @var{init} for
1515the first call.
1516@end defun
1517
1518@defun stream-for-each proc stream0 @dots{} streamN
1519Call @var{proc} on the elements from the given @var{stream}s. The
1520return value is unspecified.
1521
1522Each call is @code{(@var{proc} elem0 @dots{} elemN)}, where each
1523@var{elem} is from the corresponding @var{stream}.
1524@code{stream-for-each} stops when it reaches the end of the shortest
1525@var{stream}.
1526@end defun
1527
1528@defun stream-map proc stream0 @dots{} streamN
1529Return a new stream which is the results of applying @var{proc} to the
1530elements of the given @var{stream}s.
1531
1532Each call is @code{(@var{proc} elem0 @dots{} elemN)}, where each
1533@var{elem} is from the corresponding @var{stream}. The new stream
5179b0e2 1534ends when the end of the shortest given @var{stream} is reached.
458dd501
KR
1535@end defun
1536
1537
40296bab
KR
1538@node Buffered Input
1539@section Buffered Input
1540@cindex Buffered input
1541@cindex Line continuation
1542
1543The following functions are provided by
1544
1545@example
1546(use-modules (ice-9 buffered-input))
1547@end example
1548
1549A buffered input port allows a reader function to return chunks of
1550characters which are to be handed out on reading the port. A notion
1551of further input for an application level logical expression is
1552maintained too, and passed through to the reader.
1553
1554@defun make-buffered-input-port reader
1555Create an input port which returns characters obtained from the given
1556@var{reader} function. @var{reader} is called (@var{reader} cont),
1557and should return a string or an EOF object.
1558
1559The new port gives precisely the characters returned by @var{reader},
1560nothing is added, so if any newline characters or other separators are
1561desired they must come from the reader function.
1562
1563The @var{cont} parameter to @var{reader} is @code{#f} for initial
1564input, or @code{#t} when continuing an expression. This is an
1565application level notion, set with
1566@code{set-buffered-input-continuation?!} below. If the user has
1567entered a partial expression then it allows @var{reader} for instance
1568to give a different prompt to show more is required.
1569@end defun
1570
1571@defun make-line-buffered-input-port reader
1572@cindex Line buffered input
1573Create an input port which returns characters obtained from the
1574specified @var{reader} function, similar to
1575@code{make-buffered-input-port} above, but where @var{reader} is
1576expected to be a line-oriented.
1577
1578@var{reader} is called (@var{reader} cont), and should return a string
1579or an EOF object as above. Each string is a line of input without a
1580newline character, the port code inserts a newline after each string.
1581@end defun
1582
1583@defun set-buffered-input-continuation?! port cont
1584Set the input continuation flag for a given buffered input
1585@var{port}.
1586
1587An application uses this by calling with a @var{cont} flag of
1588@code{#f} when beginning to read a new logical expression. For
1589example with the Scheme @code{read} function (@pxref{Scheme Read}),
1590
1591@example
1592(define my-port (make-buffered-input-port my-reader))
1593
1594(set-buffered-input-continuation?! my-port #f)
1595(let ((obj (read my-port)))
1596 ...
1597@end example
1598@end defun
1599
1600
a0e07ba4
NJ
1601@c Local Variables:
1602@c TeX-master: "guile.texi"
1603@c End: