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