misc-modules.texi 61 KB

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