Pretty-printing.xhtml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Pretty-printing</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Format.xhtml" title="Formatted Output (Common-Lisp-style)"/>
  10. <link rel="next" href="Resources.xhtml" title="Resources"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Pretty-printing" epub:type="subchapter" id="Pretty-printing">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Pretty-printing</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>Pretty-printing is displaying a data structure as text,
  23. by adding line-breaks and indenttaion so that the visual
  24. structure of the output corresponds to the logical structure of
  25. data structure. This makes it easier to read and understand.
  26. Pretty-printing takes into account the column width of the output
  27. so as to avoid using more lines than needed.
  28. </p>
  29. <p>Pretty-printing of standard sequences types such as lists and
  30. vectors is done by default. For example:
  31. </p>
  32. <pre class="screen"><span class="prompt">#|kawa:11|# </span><strong class="userinput"><code>(set! *print-right-margin* 50)</code></strong>
  33. <span class="prompt">#|kawa:12|# </span><strong class="userinput"><code>'(ABCDEF (aa bb cc dd) (x123456789</code></strong>
  34. <span class="prompt">#|.....13|# </span><strong class="userinput"><code>y123456789 z123456789) ABCDEFG HIJKL)</code></strong>
  35. (ABCDEF (aa bb cc dd)
  36. (x123456789 y123456789 z123456789) ABCDEFG HIJK)
  37. </pre>
  38. <p>Setting <code class="literal">*print-right-margin*</code> to 50
  39. causes output to be limited to 50 columns.
  40. Notice the top-level list has to be split,
  41. but sub-lists <code class="literal">(aa bb cc dd)</code>
  42. and <code class="literal">(x123456789 y123456789 z123456789)</code>
  43. don’t need to be split.
  44. </p>
  45. <p>When outputting to a DomTerm REPL,
  46. then <code class="literal">*print-right-margin*</code> is ignored,
  47. and the line-breaking is actually handled by DomTerm.
  48. If you change the window width, DomTerm will dynamically
  49. re-calculate the line-breaks of previous pretten output.
  50. This works even in the case of a session saved to an HTML
  51. file, as long as JavaScript is enabled.
  52. </p>
  53. <p>The concepts and terminology are
  54. based on those of <a class="ulink" href="https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node253.html" target="_top">Common Lisp</a>.
  55. </p>
  56. <section class="sect2" title="Pretty-printing Scheme forms" epub:type="division" id="idm139667872102416">
  57. <div class="titlepage">
  58. <div>
  59. <div>
  60. <h3 class="title">Pretty-printing Scheme forms</h3>
  61. </div>
  62. </div>
  63. </div>
  64. <p>Scheme and Lisp code is traditionally pretty-printed
  65. slightly differently than plain lists.
  66. The <code class="literal">pprint</code> procedure assumes the argument
  67. is a Scheme form, and prints its accordingly.
  68. For example the special form <code class="literal">(let ...)</code> is printed
  69. differently from a regular function call <code class="literal">(list ...)</code>.
  70. </p>
  71. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872099392" class="indexterm"/> <code class="function">pprint</code> <em class="replaceable"><code>obj</code></em> [<em class="replaceable"><code>out</code></em>]</p>
  72. <div class="blockquote">
  73. <blockquote class="blockquote">
  74. <p>Assume <em class="replaceable"><code>obj</code></em> is a Scheme form, and pretty-print it
  75. in traditional Scheme format. For example:
  76. </p>
  77. <pre class="screen"><span class="prompt">#|kawa:1|# </span><strong class="userinput"><code>(import (kawa pprint))</code></strong>
  78. <span class="prompt">#|kawa:2|# </span><strong class="userinput"><code>(define fib-form</code></strong>
  79. <span class="prompt">#|.....3|# </span><strong class="userinput"><code> '(define (fibonacci n)</code></strong>
  80. <span class="prompt">#|.....4|# </span><strong class="userinput"><code> (let loop ((i0 0) (i1 1) (n n))</code></strong>
  81. <span class="prompt">#|.....5|# </span><strong class="userinput"><code> (if (&lt;= n 0) i0</code></strong>
  82. <span class="prompt">#|.....6|# </span><strong class="userinput"><code> (loop i1 (+ i0 i1) (- n 1))))))</code></strong>
  83. <span class="prompt">#|kawa:7|# </span><strong class="userinput"><code>(set! *print-right-margin* 80)</code></strong>
  84. <span class="prompt">#|kawa:8|# </span><strong class="userinput"><code>(pprint fib-form)</code></strong>
  85. (define (fibonacci n)
  86. (let loop ((i0 0) (i1 1) (n n)) (if (&lt;= n 0) i0 (loop i1 (+ i0 i1) (- n 1)))))
  87. <span class="prompt">#|kawa:9|# </span><strong class="userinput"><code>(set! *print-right-margin* 40)</code></strong>
  88. <span class="prompt">#|kawa:10|# </span><strong class="userinput"><code>(pprint fib-form)</code></strong>
  89. (define (fibonacci n)
  90. (let loop ((i0 0) (i1 1) (n n))
  91. (if (&lt;= n 0)
  92. i0
  93. (loop i1 (+ i0 i1) (- n 1)))))
  94. </pre>
  95. <p>The <code class="literal">pprint</code> special-cases forms that start with
  96. <code class="literal">define</code>, <code class="literal">if</code>, <code class="literal">lambda</code>, <code class="literal">let</code>,
  97. and a few more, and formats them with “traditional” indentation.
  98. However, it is not as complete or polished as it should be.
  99. (It should also use a programmable dispatch table,
  100. rather than having these special cases hard-wired.
  101. That is an improvemet for another day.)
  102. </p>
  103. </blockquote>
  104. </div>
  105. </section>
  106. <section class="sect2" title="Generic pretty-printing functions" epub:type="division" id="idm139667872084384">
  107. <div class="titlepage">
  108. <div>
  109. <div>
  110. <h3 class="title">Generic pretty-printing functions</h3>
  111. </div>
  112. </div>
  113. </div>
  114. <p>The following procedures are used to indicate logical blocks,
  115. and optional newlines.
  116. </p>
  117. <p>To access them do:
  118. </p>
  119. <pre class="screen">(import (kawa pprint))
  120. </pre>
  121. <p>In the following, <em class="replaceable"><code>out</code></em> is the output port, which
  122. defaults to <code class="literal">(current-output-port)</code>.
  123. </p>
  124. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667872080848" class="indexterm"/> <code class="function">pprint-logical-block</code> <em class="replaceable"><code><em class="replaceable"><code>options</code></em></code></em> <em class="replaceable"><code><a class="link" href="Bodies.xhtml#meta-statement"><em class="replaceable"><code>statement</code></em></a></code></em><em class="replaceable"><code><sup>*</sup></code></em></p>
  125. <div class="blockquote">
  126. <blockquote class="blockquote">
  127. <p>Evaluate the <em class="replaceable"><code>statement</code></em>s within the context of a new “logical block”.
  128. </p>
  129. <p>The <em class="replaceable"><code>options</code></em> are one or more of the following:
  130. </p>
  131. <div class="variablelist" epub:type="list">
  132. <dl class="variablelist">
  133. <dt class="term"><code class="literal"><span class="bold"><strong>prefix:</strong></span></code> <em class="replaceable"><code>prefix</code></em>
  134. </dt>
  135. <dt class="term"><code class="literal"><span class="bold"><strong>per-line:</strong></span></code> <em class="replaceable"><code>per-line-prefix</code></em>
  136. </dt>
  137. <dd>
  138. <p>Emit <em class="replaceable"><code>prefix</code></em> or <em class="replaceable"><code>per-line-prefix</code></em> (only one of them can be specified) before the start of the logical block.
  139. If <em class="replaceable"><code>per-line-prefix</code></em> is provided, it is also print for each
  140. line within the logical block, indented the same.
  141. These are strings and default to <code class="literal">""</code>.
  142. </p>
  143. </dd>
  144. <dt class="term"><code class="literal"><span class="bold"><strong>suffix:</strong></span></code> <em class="replaceable"><code>suffix</code></em>
  145. </dt>
  146. <dd>
  147. <p>Emit <em class="replaceable"><code>suffix</code></em> after the end of the logical block.
  148. </p>
  149. </dd>
  150. <dt class="term"><code class="literal"><span class="bold"><strong>out:</strong></span></code> <em class="replaceable"><code>out</code></em>
  151. </dt>
  152. <dd>
  153. <p>The output file.
  154. </p>
  155. </dd>
  156. </dl>
  157. </div>
  158. <p>For example to print a list you might do:
  159. </p>
  160. <pre class="screen">(pprint-logical-block prefix: "(" suffix: ")"
  161. <span class="emphasis"><em>print contents of list</em></span>)
  162. </pre>
  163. <p>This macro is equivalent to:
  164. </p>
  165. <pre class="screen">(pprint-start-logical-block <em class="replaceable"><code>prefix</code></em> <em class="replaceable"><code>is-per-line</code></em> <em class="replaceable"><code>suffix</code></em> <em class="replaceable"><code>out</code></em>)
  166. (try-finally
  167. (begin <em class="replaceable"><code>statement</code></em><sup>*</sup>)
  168. (pprint-end-logical-block <em class="replaceable"><code>suffix</code></em> <em class="replaceable"><code>out</code></em>))
  169. </pre>
  170. </blockquote>
  171. </div>
  172. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872059808" class="indexterm"/> <code class="function">pprint-start-logical-block</code> <em class="replaceable"><code>prefix</code></em> <em class="replaceable"><code>is-per-line</code></em> <em class="replaceable"><code>suffix</code></em> <em class="replaceable"><code>out</code></em></p>
  173. <div class="blockquote">
  174. <blockquote class="blockquote">
  175. <p>Start a logical block.
  176. The <em class="replaceable"><code>is-per-line</code></em> argument is a boolean to specifiy
  177. of <em class="replaceable"><code>prefix</code></em> is a per-line-prefix or a plain prefix.
  178. </p>
  179. </blockquote>
  180. </div>
  181. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872054352" class="indexterm"/> <code class="function">pprint-end-logical-block</code> <em class="replaceable"><code>suffix</code></em> <em class="replaceable"><code>out</code></em></p>
  182. <div class="blockquote">
  183. <blockquote class="blockquote">
  184. <p>End a logical block.
  185. </p>
  186. </blockquote>
  187. </div>
  188. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872050560" class="indexterm"/> <code class="function">pprint-newline</code> <em class="replaceable"><code>kind</code></em> [<em class="replaceable"><code>out</code></em>]</p>
  189. <div class="blockquote">
  190. <blockquote class="blockquote">
  191. <p>Print a conditional newline, where <em class="replaceable"><code>kind</code></em> is one of the
  192. symbols <code class="literal">'fill</code>, <code class="literal">'linear</code>, <code class="literal">'mandatory</code>,
  193. or <code class="literal">'miser</code>.
  194. Usually follows printing of a space, as nothing is printed
  195. if the line is not broken here.
  196. </p>
  197. </blockquote>
  198. </div>
  199. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872044400" class="indexterm"/> <code class="function">pprint-ident</code> <em class="replaceable"><code>mode</code></em> <em class="replaceable"><code>amount</code></em> [<em class="replaceable"><code>out</code></em>]</p>
  200. <div class="blockquote">
  201. <blockquote class="blockquote">
  202. <p>Change how much following lines are indented
  203. (with the current logical block).
  204. The <em class="replaceable"><code>amount</code></em> is the size of the indentation, in characters.
  205. The <em class="replaceable"><code>mode</code></em> is either <code class="literal">'current</code> (if the
  206. <em class="replaceable"><code>amount</code></em> is relative to the current position),
  207. or <code class="literal">'block</code> (if the <em class="replaceable"><code>amount</code></em> is relative to the
  208. start (after any <em class="replaceable"><code>prefix</code></em>) of the current logical block).
  209. </p>
  210. </blockquote>
  211. </div>
  212. </section>
  213. </section>
  214. <footer>
  215. <div class="navfooter">
  216. <ul>
  217. <li>
  218. <b class="toc">
  219. <a href="Pretty-printing.xhtml#idm139667872102416">Pretty-printing Scheme forms</a>
  220. </b>
  221. </li>
  222. <li>
  223. <b class="toc">
  224. <a href="Pretty-printing.xhtml#idm139667872084384">Generic pretty-printing functions</a>
  225. </b>
  226. </li>
  227. </ul>
  228. <p>
  229. Up: <a accesskey="u" href="Input-Output.xhtml">Input, output, and file handling</a></p>
  230. <p>
  231. Previous: <a accesskey="p" href="Format.xhtml">Formatted Output (Common-Lisp-style)</a></p>
  232. <p>
  233. Next: <a accesskey="n" href="Resources.xhtml">Resources</a></p>
  234. </div>
  235. </footer>
  236. </body>
  237. </html>