format.texi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. @menu
  2. * Format Interface::
  3. * Format Specification::
  4. @end menu
  5. @node Format Interface, Format Specification, Format, Format
  6. @subsection Format Interface
  7. @defun format destination format-string . arguments
  8. An almost complete implementation of Common LISP format description
  9. according to the CL reference book @cite{Common LISP} from Guy L.
  10. Steele, Digital Press. Backward compatible to most of the available
  11. Scheme format implementations.
  12. Returns @code{#t}, @code{#f} or a string; has side effect of printing
  13. according to @var{format-string}. If @var{destination} is @code{#t},
  14. the output is to the current output port and @code{#t} is returned. If
  15. @var{destination} is @code{#f}, a formatted string is returned as the
  16. result of the call. NEW: If @var{destination} is a string,
  17. @var{destination} is regarded as the format string; @var{format-string} is
  18. then the first argument and the output is returned as a string. If
  19. @var{destination} is a number, the output is to the current error port
  20. if available by the implementation. Otherwise @var{destination} must be
  21. an output port and @code{#t} is returned.@refill
  22. @var{format-string} must be a string. In case of a formatting error
  23. format returns @code{#f} and prints a message on the current output or
  24. error port. Characters are output as if the string were output by the
  25. @code{display} function with the exception of those prefixed by a tilde
  26. (~). For a detailed description of the @var{format-string} syntax
  27. please consult a Common LISP format reference manual. For a test suite
  28. to verify this format implementation load @file{formatst.scm}. Please
  29. send bug reports to @code{lutzeb@@cs.tu-berlin.de}.
  30. Note: @code{format} is not reentrant, i.e. only one @code{format}-call
  31. may be executed at a time.
  32. @end defun
  33. @node Format Specification, , Format Interface, Format
  34. @subsection Format Specification (Format version 3.0)
  35. Please consult a Common LISP format reference manual for a detailed
  36. description of the format string syntax. For a demonstration of the
  37. implemented directives see @file{formatst.scm}.@refill
  38. This implementation supports directive parameters and modifiers
  39. (@code{:} and @code{@@} characters). Multiple parameters must be
  40. separated by a comma (@code{,}). Parameters can be numerical parameters
  41. (positive or negative), character parameters (prefixed by a quote
  42. character (@code{'}), variable parameters (@code{v}), number of rest
  43. arguments parameter (@code{#}), empty and default parameters. Directive
  44. characters are case independent. The general form of a directive
  45. is:@refill
  46. @noindent
  47. @var{directive} ::= ~@{@var{directive-parameter},@}[:][@@]@var{directive-character}
  48. @noindent
  49. @var{directive-parameter} ::= [ [-|+]@{0-9@}+ | '@var{character} | v | # ]
  50. @subsubsection Implemented CL Format Control Directives
  51. Documentation syntax: Uppercase characters represent the corresponding
  52. control directive characters. Lowercase characters represent control
  53. directive parameter descriptions.
  54. @table @asis
  55. @item @code{~A}
  56. Any (print as @code{display} does).
  57. @table @asis
  58. @item @code{~@@A}
  59. left pad.
  60. @item @code{~@var{mincol},@var{colinc},@var{minpad},@var{padchar}A}
  61. full padding.
  62. @end table
  63. @item @code{~S}
  64. S-expression (print as @code{write} does).
  65. @table @asis
  66. @item @code{~@@S}
  67. left pad.
  68. @item @code{~@var{mincol},@var{colinc},@var{minpad},@var{padchar}S}
  69. full padding.
  70. @end table
  71. @item @code{~D}
  72. Decimal.
  73. @table @asis
  74. @item @code{~@@D}
  75. print number sign always.
  76. @item @code{~:D}
  77. print comma separated.
  78. @item @code{~@var{mincol},@var{padchar},@var{commachar}D}
  79. padding.
  80. @end table
  81. @item @code{~X}
  82. Hexadecimal.
  83. @table @asis
  84. @item @code{~@@X}
  85. print number sign always.
  86. @item @code{~:X}
  87. print comma separated.
  88. @item @code{~@var{mincol},@var{padchar},@var{commachar}X}
  89. padding.
  90. @end table
  91. @item @code{~O}
  92. Octal.
  93. @table @asis
  94. @item @code{~@@O}
  95. print number sign always.
  96. @item @code{~:O}
  97. print comma separated.
  98. @item @code{~@var{mincol},@var{padchar},@var{commachar}O}
  99. padding.
  100. @end table
  101. @item @code{~B}
  102. Binary.
  103. @table @asis
  104. @item @code{~@@B}
  105. print number sign always.
  106. @item @code{~:B}
  107. print comma separated.
  108. @item @code{~@var{mincol},@var{padchar},@var{commachar}B}
  109. padding.
  110. @end table
  111. @item @code{~@var{n}R}
  112. Radix @var{n}.
  113. @table @asis
  114. @item @code{~@var{n},@var{mincol},@var{padchar},@var{commachar}R}
  115. padding.
  116. @end table
  117. @item @code{~@@R}
  118. print a number as a Roman numeral.
  119. @item @code{~:@@R}
  120. print a number as an ``old fashioned'' Roman numeral.
  121. @item @code{~:R}
  122. print a number as an ordinal English number.
  123. @item @code{~:@@R}
  124. print a number as a cardinal English number.
  125. @item @code{~P}
  126. Plural.
  127. @table @asis
  128. @item @code{~@@P}
  129. prints @code{y} and @code{ies}.
  130. @item @code{~:P}
  131. as @code{~P but jumps 1 argument backward.}
  132. @item @code{~:@@P}
  133. as @code{~@@P but jumps 1 argument backward.}
  134. @end table
  135. @item @code{~C}
  136. Character.
  137. @table @asis
  138. @item @code{~@@C}
  139. prints a character as the reader can understand it (i.e. @code{#\} prefixing).
  140. @item @code{~:C}
  141. prints a character as emacs does (eg. @code{^C} for ASCII 03).
  142. @end table
  143. @item @code{~F}
  144. Fixed-format floating-point (prints a flonum like @var{mmm.nnn}).
  145. @table @asis
  146. @item @code{~@var{width},@var{digits},@var{scale},@var{overflowchar},@var{padchar}F}
  147. @item @code{~@@F}
  148. If the number is positive a plus sign is printed.
  149. @end table
  150. @item @code{~E}
  151. Exponential floating-point (prints a flonum like @var{mmm.nnn}@code{E}@var{ee}).
  152. @table @asis
  153. @item @code{~@var{width},@var{digits},@var{exponentdigits},@var{scale},@var{overflowchar},@var{padchar},@var{exponentchar}E}
  154. @item @code{~@@E}
  155. If the number is positive a plus sign is printed.
  156. @end table
  157. @item @code{~G}
  158. General floating-point (prints a flonum either fixed or exponential).
  159. @table @asis
  160. @item @code{~@var{width},@var{digits},@var{exponentdigits},@var{scale},@var{overflowchar},@var{padchar},@var{exponentchar}G}
  161. @item @code{~@@G}
  162. If the number is positive a plus sign is printed.
  163. @end table
  164. @item @code{~$}
  165. Dollars floating-point (prints a flonum in fixed with signs separated).
  166. @table @asis
  167. @item @code{~@var{digits},@var{scale},@var{width},@var{padchar}$}
  168. @item @code{~@@$}
  169. If the number is positive a plus sign is printed.
  170. @item @code{~:@@$}
  171. A sign is always printed and appears before the padding.
  172. @item @code{~:$}
  173. The sign appears before the padding.
  174. @end table
  175. @item @code{~%}
  176. Newline.
  177. @table @asis
  178. @item @code{~@var{n}%}
  179. print @var{n} newlines.
  180. @end table
  181. @item @code{~&}
  182. print newline if not at the beginning of the output line.
  183. @table @asis
  184. @item @code{~@var{n}&}
  185. prints @code{~&} and then @var{n-1} newlines.
  186. @end table
  187. @item @code{~|}
  188. Page Separator.
  189. @table @asis
  190. @item @code{~@var{n}|}
  191. print @var{n} page separators.
  192. @end table
  193. @item @code{~~}
  194. Tilde.
  195. @table @asis
  196. @item @code{~@var{n}~}
  197. print @var{n} tildes.
  198. @end table
  199. @item @code{~}<newline>
  200. Continuation Line.
  201. @table @asis
  202. @item @code{~:}<newline>
  203. newline is ignored, white space left.
  204. @item @code{~@@}<newline>
  205. newline is left, white space ignored.
  206. @end table
  207. @item @code{~T}
  208. Tabulation.
  209. @table @asis
  210. @item @code{~@@T}
  211. relative tabulation.
  212. @item @code{~@var{colnum,colinc}T}
  213. full tabulation.
  214. @end table
  215. @item @code{~?}
  216. Indirection (expects indirect arguments as a list).
  217. @table @asis
  218. @item @code{~@@?}
  219. extracts indirect arguments from format arguments.
  220. @end table
  221. @item @code{~(@var{str}~)}
  222. Case conversion (converts by @code{string-downcase}).
  223. @table @asis
  224. @item @code{~:(@var{str}~)}
  225. converts by @code{string-capitalize}.
  226. @item @code{~@@(@var{str}~)}
  227. converts by @code{string-capitalize-first}.
  228. @item @code{~:@@(@var{str}~)}
  229. converts by @code{string-upcase}.
  230. @end table
  231. @item @code{~*}
  232. Argument Jumping (jumps 1 argument forward).
  233. @table @asis
  234. @item @code{~@var{n}*}
  235. jumps @var{n} arguments forward.
  236. @item @code{~:*}
  237. jumps 1 argument backward.
  238. @item @code{~@var{n}:*}
  239. jumps @var{n} arguments backward.
  240. @item @code{~@@*}
  241. jumps to the 0th argument.
  242. @item @code{~@var{n}@@*}
  243. jumps to the @var{n}th argument (beginning from 0)
  244. @end table
  245. @item @code{~[@var{str0}~;@var{str1}~;...~;@var{strn}~]}
  246. Conditional Expression (numerical clause conditional).
  247. @table @asis
  248. @item @code{~@var{n}[}
  249. take argument from @var{n}.
  250. @item @code{~@@[}
  251. true test conditional.
  252. @item @code{~:[}
  253. if-else-then conditional.
  254. @item @code{~;}
  255. clause separator.
  256. @item @code{~:;}
  257. default clause follows.
  258. @end table
  259. @item @code{~@{@var{str}~@}}
  260. Iteration (args come from the next argument (a list)).
  261. @table @asis
  262. @item @code{~@var{n}@{}
  263. at most @var{n} iterations.
  264. @item @code{~:@{}
  265. args from next arg (a list of lists).
  266. @item @code{~@@@{}
  267. args from the rest of arguments.
  268. @item @code{~:@@@{}
  269. args from the rest args (lists).
  270. @end table
  271. @item @code{~^}
  272. Up and out.
  273. @table @asis
  274. @item @code{~@var{n}^}
  275. aborts if @var{n} = 0
  276. @item @code{~@var{n},@var{m}^}
  277. aborts if @var{n} = @var{m}
  278. @item @code{~@var{n},@var{m},@var{k}^}
  279. aborts if @var{n} <= @var{m} <= @var{k}
  280. @end table
  281. @end table
  282. @subsubsection Not Implemented CL Format Control Directives
  283. @table @asis
  284. @item @code{~:A}
  285. print @code{#f} as an empty list (see below).
  286. @item @code{~:S}
  287. print @code{#f} as an empty list (see below).
  288. @item @code{~<~>}
  289. Justification.
  290. @item @code{~:^}
  291. (sorry I don't understand its semantics completely)
  292. @end table
  293. @subsubsection Extended, Replaced and Additional Control Directives
  294. @table @asis
  295. @item @code{~@var{mincol},@var{padchar},@var{commachar},@var{commawidth}D}
  296. @item @code{~@var{mincol},@var{padchar},@var{commachar},@var{commawidth}X}
  297. @item @code{~@var{mincol},@var{padchar},@var{commachar},@var{commawidth}O}
  298. @item @code{~@var{mincol},@var{padchar},@var{commachar},@var{commawidth}B}
  299. @item @code{~@var{n},@var{mincol},@var{padchar},@var{commachar},@var{commawidth}R}
  300. @var{commawidth} is the number of characters between two comma characters.
  301. @end table
  302. @table @asis
  303. @item @code{~I}
  304. print an R5RS complex number as @code{~F~@@Fi} with passed parameters for
  305. @code{~F}.
  306. @item @code{~Y}
  307. Pretty print formatting of an argument for scheme code lists.
  308. @item @code{~K}
  309. Same as @code{~?.}
  310. @item @code{~!}
  311. Flushes the output if format @var{destination} is a port.
  312. @item @code{~_}
  313. Print a @code{#\space} character
  314. @table @asis
  315. @item @code{~@var{n}_}
  316. print @var{n} @code{#\space} characters.
  317. @end table
  318. @item @code{~/}
  319. Print a @code{#\tab} character
  320. @table @asis
  321. @item @code{~@var{n}/}
  322. print @var{n} @code{#\tab} characters.
  323. @end table
  324. @item @code{~@var{n}C}
  325. Takes @var{n} as an integer representation for a character. No arguments
  326. are consumed. @var{n} is converted to a character by
  327. @code{integer->char}. @var{n} must be a positive decimal number.@refill
  328. @item @code{~:S}
  329. Print out readproof. Prints out internal objects represented as
  330. @code{#<...>} as strings @code{"#<...>"} so that the format output can always
  331. be processed by @code{read}.
  332. @refill
  333. @item @code{~:A}
  334. Print out readproof. Prints out internal objects represented as
  335. @code{#<...>} as strings @code{"#<...>"} so that the format output can always
  336. be processed by @code{read}.
  337. @item @code{~Q}
  338. Prints information and a copyright notice on the format implementation.
  339. @table @asis
  340. @item @code{~:Q}
  341. prints format version.
  342. @end table
  343. @refill
  344. @item @code{~F, ~E, ~G, ~$}
  345. may also print number strings, i.e. passing a number as a string and
  346. format it accordingly.
  347. @end table
  348. @subsubsection Configuration Variables
  349. Format has some configuration variables at the beginning of
  350. @file{format.scm} to suit the systems and users needs. There should be
  351. no modification necessary for the configuration that comes with SLIB.
  352. If modification is desired the variable should be set after the format
  353. code is loaded. Format detects automatically if the running scheme
  354. system implements floating point numbers and complex numbers.
  355. @table @asis
  356. @item @var{format:symbol-case-conv}
  357. Symbols are converted by @code{symbol->string} so the case type of the
  358. printed symbols is implementation dependent.
  359. @code{format:symbol-case-conv} is a one arg closure which is either
  360. @code{#f} (no conversion), @code{string-upcase}, @code{string-downcase}
  361. or @code{string-capitalize}. (default @code{#f})
  362. @item @var{format:iobj-case-conv}
  363. As @var{format:symbol-case-conv} but applies for the representation of
  364. implementation internal objects. (default @code{#f})
  365. @item @var{format:expch}
  366. The character prefixing the exponent value in @code{~E} printing. (default
  367. @code{#\E})
  368. @end table
  369. @subsubsection Compatibility With Other Format Implementations
  370. @table @asis
  371. @item SLIB format 2.x:
  372. See @file{format.doc}.
  373. @item SLIB format 1.4:
  374. Downward compatible except for padding support and @code{~A}, @code{~S},
  375. @code{~P}, @code{~X} uppercase printing. SLIB format 1.4 uses C-style
  376. @code{printf} padding support which is completely replaced by the CL
  377. @code{format} padding style.
  378. @item MIT C-Scheme 7.1:
  379. Downward compatible except for @code{~}, which is not documented
  380. (ignores all characters inside the format string up to a newline
  381. character). (7.1 implements @code{~a}, @code{~s},
  382. ~@var{newline}, @code{~~}, @code{~%}, numerical and variable
  383. parameters and @code{:/@@} modifiers in the CL sense).@refill
  384. @item Elk 1.5/2.0:
  385. Downward compatible except for @code{~A} and @code{~S} which print in
  386. uppercase. (Elk implements @code{~a}, @code{~s}, @code{~~}, and
  387. @code{~%} (no directive parameters or modifiers)).@refill
  388. @item Scheme->C 01nov91:
  389. Downward compatible except for an optional destination parameter: S2C
  390. accepts a format call without a destination which returns a formatted
  391. string. This is equivalent to a #f destination in S2C. (S2C implements
  392. @code{~a}, @code{~s}, @code{~c}, @code{~%}, and @code{~~} (no directive
  393. parameters or modifiers)).@refill
  394. @end table
  395. This implementation of format is solely useful in the SLIB context
  396. because it requires other components provided by SLIB.@refill