Renamed and updated to latest fangle
[newfangle.git] / fangle.html
blob4d9c349de7a412cca88b69f31ad21f1fd04993d6
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN">
3 <html xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns="http://www.w3.org/1999/xhtml" xmlns:x="http://www.texmacs.org/2002/extensions">
4 <head>
5 <title>fangle</title>
6 <meta content="TeXmacs 1.0.7.10" name="generator"></meta>
7 <style type="text/css">
8 body { text-align: justify } h5 { display: inline; padding-right: 1em }
9 h6 { display: inline; padding-right: 1em } table { border-collapse:
10 collapse } td { padding: 0.2em; vertical-align: baseline } .subsup {
11 display: inline; vertical-align: -0.2em } .subsup td { padding: 0px;
12 text-align: left} .fraction { display: inline; vertical-align: -0.8em }
13 .fraction td { padding: 0px; text-align: center } .wide { position:
14 relative; margin-left: -0.4em } .accent { position: relative;
15 margin-left: -0.4em; top: -0.1em } .title-block { width: 100%;
16 text-align: center } .title-block p { margin: 0px } .compact-block p {
17 margin-top: 0px; margin-bottom: 0px } .left-tab { text-align: left }
18 .center-tab { text-align: center } .right-tab { float: right; position:
19 relative; top: -1em }
20 </style>
21 </head>
22 <body>
23 <p>
25 </p>
26 <p>
27 <p>
29 </p>
30 <table class="title-block">
31 <tr>
32 <td><font size="+2"><p>
33 <table class="title-block">
34 <tr>
35 <td><b><font size="+5">fangle</font></b></td>
36 </tr>
37 </table>
38 <p style="margin-top: 10%; margin-bottom: 10%">
39 <div class="compact-block">
40 <table class="title-block">
41 <tr>
42 <td><p>
43 <span style="margin-left: 0pt"></span>
44 <table style="display: inline; vertical-align: -0.55em">
45 <tbody><tr>
46 <td style="text-align: center; padding-left: 0em; padding-right: 0em; padding-bottom: 0em; padding-top: 0em; width: 100%"><center>
47 <p>
48 <p>
49 <span style="margin-left: 0pt"></span>
50 <table style="display: inline; vertical-align: -0.55em">
51 <tbody><tr>
52 <td style="text-align: center; padding-left: 0em; padding-right: 0em; padding-bottom: 0em; padding-top: 0em; width: 100%"><center>
53 <p>
54 <class style="font-variant: small-caps"><font size="+2"> Sam
55 Liddicott</font></class>
56 </p>
57 </center></td>
58 </tr></tbody>
59 </table>
60 </p>
61 <p style="margin-top: 2em; margin-bottom: 2em">
62 <span style="margin-left: 0pt"></span>
63 <table style="display: inline; vertical-align: -0.55em">
64 <tbody><tr>
65 <td style="text-align: center; padding-left: 0em; padding-right: 0em; padding-bottom: 0em; padding-top: 0em; width: 100%"><center>
66 <p>
67 sam@liddicott.com
68 </p>
69 </center></td>
70 </tr></tbody>
71 </table>
72 </p>
73 </p>
74 </center></td>
75 </tr></tbody>
76 </table>
77 </p></td>
78 </tr>
79 </table>
80 </div>
81 </p>
82 <p>
83 <span style="margin-left: 0pt"></span>
84 <table style="display: inline; vertical-align: -0.55em">
85 <tbody><tr>
86 <td style="text-align: center; padding-left: 0em; padding-right: 0em; padding-bottom: 0em; padding-top: 0em; width: 100%"><center>
87 <p>
88 <i>August 2009</i>
89 </p>
90 </center></td>
91 </tr></tbody>
92 </table>
93 </p>
94 </p></font></td>
95 </tr>
96 </table>
97 <p>
99 </p>
102 </p>
103 <div class="right-tab">
104 <span style="margin-left: 0em"></span>
105 </div>
106 </p>
107 <h2 id="auto-1">Introduction</h2>
109 <class style="font-variant: small-caps">Fangle</class> is a tool for fangled literate programming.
110 Newfangled is defined as <em>New and often needlessly novel</em> by
111 <class style="font-variant: small-caps">TheFreeDictionary.com</class>.
112 </p>
114 In this case, fangled means yet another not-so-new
116 <font size="-1"><div align="justify">
117 <div style="margin-left: 0px">
118 <div style="margin-right: 0px">
120 . but improved.
121 </p>
122 </div>
123 </div>
124 </div></font>
125 </p>
126 <span style="margin-left: 0em"></span>
127 <a id="footnr-1"></a>
128 <sup><a href="#footnote-1">1</a></sup>
129 method for literate programming.
130 </p>
132 <class style="font-variant: small-caps">Literate Programming</class> has a long history starting
133 with the great <class style="font-variant: small-caps">Donald Knuth</class> himself, whose
134 literate programming tools seem to make use of as many escape sequences
135 for semantic markup as TeX (also by <class style="font-variant: small-caps">Donald Knuth</class>).
136 </p>
138 <class style="font-variant: small-caps">Norman Ramsey</class> wrote the <class style="font-variant: small-caps">Noweb</class>
139 set of tools (<tt class="verbatim">notangle</tt>, <tt class="verbatim">noweave</tt> and <tt
140 class="verbatim">noroots</tt>) and helpfully reduced the amount of magic character
141 sequences to pretty much just <tt class="verbatim">&lt;&lt;</tt>, <tt class="verbatim">&gt;&gt;</tt>
142 and <tt class="verbatim">@</tt>, and in doing so brought the wonders of literate
143 programming within my reach.
144 </p>
146 While using the L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X editor for
147 LaTeX editing I had various troubles with the noweb tools, some of which
148 were my fault, some of which were noweb's fault and some of which were
149 L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X's fault.
150 </p>
152 <class style="font-variant: small-caps">Noweb</class> generally brought literate programming to
153 the masses through removing some of the complexity of the original
154 literate programming, but this would be of no advantage to me if the
155 L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X / LaTeX combination brought
156 more complications in their place.
157 </p>
159 <class style="font-variant: small-caps">Fangle</class> was thus born (originally called <class
160 style="font-variant: small-caps">Newfangle</class>) as an awk replacement for notangle, adding
161 some important features, like better integration with L<span style="margin-left: -0.1667em"></span>Y<span
162 style="margin-left: -0.125em"></span>X and LaTeX (and later TeXmacs), multiple output format
163 conversions, and fixing notangle bugs like indentation when using -L for
164 line numbers.
165 </p>
167 Significantly, fangle is just one program which replaces various
168 programs in <class style="font-variant: small-caps">Noweb</class>. Noweave is done away with and
169 implemented directly as LaTeX macros, and noroots is implemented as a
170 function of the untangler fangle.
171 </p>
173 Fangle is written in awk for portability reasons, awk being available
174 for most platforms. A Python version
176 <font size="-1"><div align="justify">
177 <div style="margin-left: 0px">
178 <div style="margin-right: 0px">
180 . hasn't anyone implemented awk in python yet?
181 </p>
182 </div>
183 </div>
184 </div></font>
185 </p>
186 <span style="margin-left: 0em"></span>
187 <a id="footnr-2"></a>
188 <sup><a href="#footnote-2">2</a></sup>
189 was considered for the benefit of L
190 <span style="margin-left: -0.1667em"></span>
192 <span style="margin-left: -0.125em"></span>
193 X but a scheme version for TeXmacs will probably materialise first; as
194 TeXmacs macro capabilities help make edit-time and format-time rendering
195 of fangle chunks simple enough for my weak brain.
196 </p>
198 As an extension to many literate-programming styles, Fangle permits code
199 chunks to take parameters and thus operate somewhat like C pre-processor
200 macros, or like C++ templates. Name parameters (or even local
201 <em>variables</em> in the callers scope) are anticipated, as
202 parameterized chunks <class style="font-family: Times New Roman">&mdash;</class> useful though they
203 are <class style="font-family: Times New Roman">&mdash;</class> are hard to comprehend in the literate
204 document.
205 </p>
206 <h2 id="auto-2"><a id="License"></a>License</h2>
208 Fangle is licensed under the GPL 3 (or later).
209 </p>
211 This doesn't mean that sources generated by fangle must be licensed
212 under the GPL 3.
213 </p>
215 This doesn't mean that you can't use or distribute fangle with sources
216 of an incompatible license, but it means you must make the source of
217 fangle available too.
218 </p>
220 As fangle is currently written in awk, an interpreted language, this
221 should not be too hard.
222 </p>
224 <font size="-1"><p>
225 <a id="code-label-gpl3-copyright-1"></a>
226 <table style="width: 100%" id="code-ref-gpl3-copyright-1">
227 <tbody><tr>
228 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="gpl3-copyright"></a><font size="-1"><font color="blue">gpl3-copyright</font>[1](),
229 lang=<font color="blue">text</font> &equiv;</font></td>
230 </tr></tbody>
231 </table>
232 </p><p>
234 <tt class="verbatim"><div class="compact-block">
235 <tt>1 </tt># fangle - fully featured notangle replacement in awk
236 </div></tt>
237 </p>
238 <pre class="verbatim" xml:space="preserve">
239 <div class="compact-block"><tt>2 </tt>#
240 <tt>3 </tt># Copyright (C) 2009-2010 Sam Liddicott &lt;sam@liddicott.com&gt;
241 <tt>4 </tt>#
242 <tt>5 </tt># This program is free software: you can redistribute it and/or modify
243 <tt>6 </tt># it under the terms of the GNU General Public License as published by
244 <tt>7 </tt># the Free Software Foundation, either version 3 of the License, or
245 <tt>8 </tt># (at your option) any later version.
246 <tt>9 </tt>#
247 <tt>10 </tt># This program is distributed in the hope that it will be useful,
248 <tt>11 </tt># but WITHOUT ANY WARRANTY; without even the implied warranty of
249 <tt>12 </tt># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
250 <tt>13 </tt># GNU General Public License for more details.
251 <tt>14 </tt>#
252 <tt>15 </tt># You should have received a copy of the GNU General Public License</div></pre>
254 <tt class="verbatim"><div class="compact-block">
255 <tt>16 </tt># along with this program. If not, see
256 &lt;http://www.gnu.org/licenses/&gt;.
257 </div></tt>
258 </p>
259 </p><table style="width: 100%" id="code-end-gpl3-copyright-1">
260 <tbody><tr>
261 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
262 </tr></tbody>
263 </table></font>
264 </p>
265 <h1>Table of contents</h1>
266 <p style="margin-top: 25%; margin-bottom: 10%">
267 <div style="text-indent: 0em">
268 <div class="compact-block">
270 Introduction <span style="margin-left: 5mm"></span> <a href="#auto-1">3</a>
271 </p>
273 License <span style="margin-left: 5mm"></span> <a href="#auto-2">4</a>
274 </p>
275 <p style="margin-top: 2em; margin-bottom: 1em">
276 <b><font size="+1">I<span style="margin-left: 1em"></span>Using Fangle</font></b> <span
277 style="margin-left: 5mm"></span> <a href="#auto-3">9</a>
278 </p>
279 <p style="margin-top: 1em; margin-bottom: 0.5em">
280 <b>1<span style="margin-left: 1em"></span>Introduction to Literate Programming</b>
281 <span style="margin-left: 5mm"></span> <a href="#auto-4">11</a>
282 </p>
283 <p style="margin-top: 1em; margin-bottom: 0.5em">
284 <b>2<span style="margin-left: 1em"></span>Running Fangle</b> <span style="margin-left: 5mm"></span> <a
285 href="#auto-5">13</a>
286 </p>
288 2.1<span style="margin-left: 1em"></span>Listing roots <span style="margin-left: 5mm"></span> <a href="#auto-6">13</a>
289 </p>
291 2.2<span style="margin-left: 1em"></span>Extracting roots <span style="margin-left: 5mm"></span> <a
292 href="#auto-7">13</a>
293 </p>
295 2.3<span style="margin-left: 1em"></span>Formatting the document <span style="margin-left: 5mm"></span>
296 <a href="#auto-8">13</a>
297 </p>
298 <p style="margin-top: 1em; margin-bottom: 0.5em">
299 <b>3<span style="margin-left: 1em"></span>Using Fangle with L<sup><span style="margin-left: -0.4em"></span>A</sup><span
300 style="margin-left: -0.1em"></span>T<sub><span style="margin-left: -0.2em"></span>E</sub><span style="margin-left: -0.2em"></span>X</b>
301 <span style="margin-left: 5mm"></span> <a href="#auto-9">15</a>
302 </p>
303 <p style="margin-top: 1em; margin-bottom: 0.5em">
304 <b>4<span style="margin-left: 1em"></span>Using Fangle with L<span style="margin-left: -0.1667em"></span>Y<span
305 style="margin-left: -0.125em"></span>X</b> <span style="margin-left: 5mm"></span> <a href="#auto-10">17</a>
306 </p>
308 4.1<span style="margin-left: 1em"></span>Installing the L<span style="margin-left: -0.1667em"></span>Y<span
309 style="margin-left: -0.125em"></span>X module <span style="margin-left: 5mm"></span> <a href="#auto-11">17</a>
310 </p>
312 4.2<span style="margin-left: 1em"></span>Obtaining a decent mono font <span style="margin-left: 5mm"></span>
313 <a href="#auto-12">17</a>
314 </p>
315 <div style="margin-left: 24px">
317 4.2.1<span style="margin-left: 1em"></span>txfonts <span style="margin-left: 5mm"></span> <a href="#auto-13">17</a>
318 </p>
319 </div>
320 <div style="margin-left: 24px">
322 4.2.2<span style="margin-left: 1em"></span>ams pmb <span style="margin-left: 5mm"></span> <a href="#auto-14">17</a>
323 </p>
324 </div>
325 <div style="margin-left: 24px">
327 4.2.3<span style="margin-left: 1em"></span>Luximono <span style="margin-left: 5mm"></span> <a href="#auto-15">17</a>
328 </p>
329 </div>
331 4.3<span style="margin-left: 1em"></span>Formatting your Lyx document <span style="margin-left: 5mm"></span>
332 <a href="#auto-16">18</a>
333 </p>
334 <div style="margin-left: 24px">
336 4.3.1<span style="margin-left: 1em"></span>Customising the listing appearance
337 <span style="margin-left: 5mm"></span> <a href="#auto-17">18</a>
338 </p>
339 </div>
340 <div style="margin-left: 24px">
342 4.3.2<span style="margin-left: 1em"></span>Global customisations <span style="margin-left: 5mm"></span>
343 <a href="#auto-18">18</a>
344 </p>
345 </div>
347 4.4<span style="margin-left: 1em"></span>Configuring the build script <span style="margin-left: 5mm"></span>
348 <a href="#auto-19">19</a>
349 </p>
350 <div style="margin-left: 24px">
352 4.4.1<span style="margin-left: 1em"></span>... <span style="margin-left: 5mm"></span> <a href="#auto-20">19</a>
353 </p>
354 </div>
355 <p style="margin-top: 1em; margin-bottom: 0.5em">
356 <b>5<span style="margin-left: 1em"></span>Using Fangle with T<sub><span style="margin-left: -0.2em"></span>E</sub><span
357 style="margin-left: -0.2em"></span>X<sub><span style="margin-left: -0.2em"></span>M<span style="margin-left: -0.1em"></span>A<span
358 style="margin-left: -0.2em"></span>CS</sub></b> <span style="margin-left: 5mm"></span> <a href="#auto-21">21</a>
359 </p>
360 <p style="margin-top: 1em; margin-bottom: 0.5em">
361 <b>6<span style="margin-left: 1em"></span>Fangle with Makefiles</b> <span style="margin-left: 5mm"></span>
362 <a href="#auto-22">23</a>
363 </p>
365 6.1<span style="margin-left: 1em"></span>A word about makefiles formats <span style="margin-left: 5mm"></span>
366 <a href="#auto-23">23</a>
367 </p>
369 6.2<span style="margin-left: 1em"></span>Extracting Sources <span style="margin-left: 5mm"></span> <a
370 href="#auto-24">23</a>
371 </p>
372 <div style="margin-left: 24px">
374 6.2.1<span style="margin-left: 1em"></span>Converting from L<span style="margin-left: -0.1667em"></span>Y<span
375 style="margin-left: -0.125em"></span>X to L<sup><span style="margin-left: -0.4em"></span>A</sup><span style="margin-left: -0.1em"></span>T<sub><span
376 style="margin-left: -0.2em"></span>E</sub><span style="margin-left: -0.2em"></span>X <span style="margin-left: 5mm"></span>
377 <a href="#auto-25">24</a>
378 </p>
379 </div>
380 <div style="margin-left: 24px">
382 6.2.2<span style="margin-left: 1em"></span>Converting from T<sub><span style="margin-left: -0.2em"></span>E</sub><span
383 style="margin-left: -0.2em"></span>X<sub><span style="margin-left: -0.2em"></span>M<span style="margin-left: -0.1em"></span>A<span
384 style="margin-left: -0.2em"></span>CS</sub> <span style="margin-left: 5mm"></span> <a href="#auto-26">24</a>
385 </p>
386 </div>
388 6.3<span style="margin-left: 1em"></span>Extracting Program Source <span style="margin-left: 5mm"></span>
389 <a href="#auto-27">25</a>
390 </p>
392 6.4<span style="margin-left: 1em"></span>Extracting Source Files <span style="margin-left: 5mm"></span>
393 <a href="#auto-28">25</a>
394 </p>
396 6.5<span style="margin-left: 1em"></span>Extracting Documentation <span style="margin-left: 5mm"></span>
397 <a href="#auto-29">27</a>
398 </p>
399 <div style="margin-left: 24px">
401 6.5.1<span style="margin-left: 1em"></span>Formatting T<sub><span style="margin-left: -0.2em"></span>E</sub><span
402 style="margin-left: -0.2em"></span>X <span style="margin-left: 5mm"></span> <a href="#auto-30">27</a>
403 </p>
404 </div>
405 <div style="margin-left: 48px">
407 6.5.1.1<span style="margin-left: 1em"></span>Running pdflatex <span style="margin-left: 5mm"></span>
408 <a href="#auto-31">27</a>
409 </p>
410 </div>
411 <div style="margin-left: 24px">
413 6.5.2<span style="margin-left: 1em"></span>Formatting T<sub><span style="margin-left: -0.2em"></span>E</sub><span
414 style="margin-left: -0.2em"></span>X<sub><span style="margin-left: -0.2em"></span>M<span style="margin-left: -0.1em"></span>A<span
415 style="margin-left: -0.2em"></span>CS</sub> <span style="margin-left: 5mm"></span> <a href="#auto-32">28</a>
416 </p>
417 </div>
418 <div style="margin-left: 24px">
420 6.5.3<span style="margin-left: 1em"></span>Building the Documentation as a Whole
421 <span style="margin-left: 5mm"></span> <a href="#auto-33">28</a>
422 </p>
423 </div>
425 6.6<span style="margin-left: 1em"></span>Other helpers <span style="margin-left: 5mm"></span> <a href="#auto-34">29</a>
426 </p>
428 6.7<span style="margin-left: 1em"></span>Boot-strapping the extraction <span style="margin-left: 5mm"></span>
429 <a href="#auto-35">29</a>
430 </p>
432 6.8<span style="margin-left: 1em"></span>Incorporating Makefile.inc into existing
433 projects <span style="margin-left: 5mm"></span> <a href="#auto-36">30</a>
434 </p>
435 <div style="margin-left: 96px">
437 Example <span style="margin-left: 5mm"></span> <a href="#auto-37">30</a>
438 </p>
439 </div>
440 <p style="margin-top: 2em; margin-bottom: 1em">
441 <b><font size="+1">II<span style="margin-left: 1em"></span>Source Code</font></b> <span
442 style="margin-left: 5mm"></span> <a href="#auto-38">31</a>
443 </p>
444 <p style="margin-top: 1em; margin-bottom: 0.5em">
445 <b>7<span style="margin-left: 1em"></span>Fangle awk source code</b> <span style="margin-left: 5mm"></span>
446 <a href="#auto-39">33</a>
447 </p>
449 7.1<span style="margin-left: 1em"></span>AWK tricks <span style="margin-left: 5mm"></span> <a href="#auto-40">33</a>
450 </p>
452 7.2<span style="margin-left: 1em"></span>Catching errors <span style="margin-left: 5mm"></span> <a
453 href="#auto-41">34</a>
454 </p>
455 <p style="margin-top: 1em; margin-bottom: 0.5em">
456 <b>8<span style="margin-left: 1em"></span>L<sup><span style="margin-left: -0.4em"></span>A</sup><span
457 style="margin-left: -0.1em"></span>T<sub><span style="margin-left: -0.2em"></span>E</sub><span style="margin-left: -0.2em"></span>X
458 and lstlistings</b> <span style="margin-left: 5mm"></span> <a href="#auto-42">35</a>
459 </p>
461 8.1<span style="margin-left: 1em"></span>Additional lstlstings parameters <span
462 style="margin-left: 5mm"></span> <a href="#auto-43">35</a>
463 </p>
465 8.2<span style="margin-left: 1em"></span>Parsing chunk arguments <span style="margin-left: 5mm"></span>
466 <a href="#auto-44">37</a>
467 </p>
469 8.3<span style="margin-left: 1em"></span>Expanding parameters in the text <span
470 style="margin-left: 5mm"></span> <a href="#auto-45">38</a>
471 </p>
472 <p style="margin-top: 1em; margin-bottom: 0.5em">
473 <b>9<span style="margin-left: 1em"></span>Language Modes &amp; Quoting</b> <span
474 style="margin-left: 5mm"></span> <a href="#auto-46">41</a>
475 </p>
477 9.1<span style="margin-left: 1em"></span>Modes <span style="margin-left: 5mm"></span> <a href="#auto-47">41</a>
478 </p>
479 <div style="margin-left: 24px">
481 9.1.1<span style="margin-left: 1em"></span>Modes to keep code together <span
482 style="margin-left: 5mm"></span> <a href="#auto-48">41</a>
483 </p>
484 </div>
485 <div style="margin-left: 24px">
487 9.1.2<span style="margin-left: 1em"></span>Modes affect included chunks <span
488 style="margin-left: 5mm"></span> <a href="#auto-49">41</a>
489 </p>
490 </div>
492 9.2<span style="margin-left: 1em"></span>Language Mode Definitions <span style="margin-left: 5mm"></span>
493 <a href="#auto-50">42</a>
494 </p>
495 <div style="margin-left: 24px">
497 9.2.1<span style="margin-left: 1em"></span>Backslash <span style="margin-left: 5mm"></span> <a href="#auto-51">43</a>
498 </p>
499 </div>
500 <div style="margin-left: 24px">
502 9.2.2<span style="margin-left: 1em"></span>Strings <span style="margin-left: 5mm"></span> <a href="#auto-52">44</a>
503 </p>
504 </div>
505 <div style="margin-left: 24px">
507 9.2.3<span style="margin-left: 1em"></span>Parentheses, Braces and Brackets <span
508 style="margin-left: 5mm"></span> <a href="#auto-53">45</a>
509 </p>
510 </div>
511 <div style="margin-left: 24px">
513 9.2.4<span style="margin-left: 1em"></span>Customizing Standard Modes <span style="margin-left: 5mm"></span>
514 <a href="#auto-54">45</a>
515 </p>
516 </div>
517 <div style="margin-left: 24px">
519 9.2.5<span style="margin-left: 1em"></span>Comments <span style="margin-left: 5mm"></span> <a href="#auto-55">45</a>
520 </p>
521 </div>
522 <div style="margin-left: 24px">
524 9.2.6<span style="margin-left: 1em"></span>Regex <span style="margin-left: 5mm"></span> <a href="#auto-56">46</a>
525 </p>
526 </div>
527 <div style="margin-left: 24px">
529 9.2.7<span style="margin-left: 1em"></span>Perl <span style="margin-left: 5mm"></span> <a href="#auto-57">47</a>
530 </p>
531 </div>
532 <div style="margin-left: 24px">
534 9.2.8<span style="margin-left: 1em"></span>sh <span style="margin-left: 5mm"></span> <a href="#auto-58">47</a>
535 </p>
536 </div>
538 9.3<span style="margin-left: 1em"></span>Some tests <span style="margin-left: 5mm"></span> <a href="#auto-59">47</a>
539 </p>
541 9.4<span style="margin-left: 1em"></span>A non-recursive mode tracker <span style="margin-left: 5mm"></span>
542 <a href="#auto-60">48</a>
543 </p>
544 <div style="margin-left: 24px">
546 9.4.1<span style="margin-left: 1em"></span>Constructor <span style="margin-left: 5mm"></span> <a
547 href="#auto-61">48</a>
548 </p>
549 </div>
550 <div style="margin-left: 24px">
552 9.4.2<span style="margin-left: 1em"></span>Management <span style="margin-left: 5mm"></span> <a
553 href="#auto-62">48</a>
554 </p>
555 </div>
556 <div style="margin-left: 24px">
558 9.4.3<span style="margin-left: 1em"></span>Tracker <span style="margin-left: 5mm"></span> <a href="#auto-63">49</a>
559 </p>
560 </div>
561 <div style="margin-left: 48px">
563 9.4.3.1<span style="margin-left: 1em"></span>One happy chunk <span style="margin-left: 5mm"></span>
564 <a href="#auto-64">52</a>
565 </p>
566 </div>
567 <div style="margin-left: 48px">
569 9.4.3.2<span style="margin-left: 1em"></span>Tests <span style="margin-left: 5mm"></span> <a href="#auto-65">52</a>
570 </p>
571 </div>
573 9.5<span style="margin-left: 1em"></span>Escaping and Quoting <span style="margin-left: 5mm"></span>
574 <a href="#auto-66">52</a>
575 </p>
576 <p style="margin-top: 1em; margin-bottom: 0.5em">
577 <b>10<span style="margin-left: 1em"></span>Recognizing Chunks</b> <span style="margin-left: 5mm"></span>
578 <a href="#auto-67">55</a>
579 </p>
581 10.1<span style="margin-left: 1em"></span>Chunk start <span style="margin-left: 5mm"></span> <a href="#auto-68">55</a>
582 </p>
583 <div style="margin-left: 24px">
585 10.1.1<span style="margin-left: 1em"></span>T<sub><span style="margin-left: -0.2em"></span>E</sub><span
586 style="margin-left: -0.2em"></span>X<sub><span style="margin-left: -0.2em"></span>M<span style="margin-left: -0.1em"></span>A<span
587 style="margin-left: -0.2em"></span>CS</sub> hackery <span style="margin-left: 5mm"></span> <a href="#auto-69">55</a>
588 </p>
589 </div>
590 <div style="margin-left: 24px">
592 10.1.2<span style="margin-left: 1em"></span>lstlistings <span style="margin-left: 5mm"></span> <a
593 href="#auto-70">56</a>
594 </p>
595 </div>
596 <div style="margin-left: 24px">
598 10.1.3<span style="margin-left: 1em"></span>T<sub><span style="margin-left: -0.2em"></span>E</sub><span
599 style="margin-left: -0.2em"></span>X<sub><span style="margin-left: -0.2em"></span>M<span style="margin-left: -0.1em"></span>A<span
600 style="margin-left: -0.2em"></span>CS</sub> <span style="margin-left: 5mm"></span> <a href="#auto-71">56</a>
601 </p>
602 </div>
603 <div style="margin-left: 24px">
605 10.1.4<span style="margin-left: 1em"></span>Noweb <span style="margin-left: 5mm"></span> <a href="#auto-72">57</a>
606 </p>
607 </div>
609 10.2<span style="margin-left: 1em"></span>Chunk end <span style="margin-left: 5mm"></span> <a href="#auto-73">57</a>
610 </p>
611 <div style="margin-left: 24px">
613 10.2.1<span style="margin-left: 1em"></span>lstlistings <span style="margin-left: 5mm"></span> <a
614 href="#auto-74">58</a>
615 </p>
616 </div>
617 <div style="margin-left: 24px">
619 10.2.2<span style="margin-left: 1em"></span>noweb <span style="margin-left: 5mm"></span> <a href="#auto-75">58</a>
620 </p>
621 </div>
623 10.3<span style="margin-left: 1em"></span>Chunk contents <span style="margin-left: 5mm"></span> <a
624 href="#auto-76">58</a>
625 </p>
626 <div style="margin-left: 24px">
628 10.3.1<span style="margin-left: 1em"></span>lstlistings <span style="margin-left: 5mm"></span> <a
629 href="#auto-77">59</a>
630 </p>
631 </div>
632 <p style="margin-top: 1em; margin-bottom: 0.5em">
633 <b>11<span style="margin-left: 1em"></span>Processing Options</b> <span style="margin-left: 5mm"></span>
634 <a href="#auto-78">61</a>
635 </p>
636 <p style="margin-top: 1em; margin-bottom: 0.5em">
637 <b>12<span style="margin-left: 1em"></span>Generating the Output</b> <span style="margin-left: 5mm"></span>
638 <a href="#auto-79">63</a>
639 </p>
641 12.1<span style="margin-left: 1em"></span>Assembling the Chunks <span style="margin-left: 5mm"></span>
642 <a href="#auto-80">64</a>
643 </p>
644 <div style="margin-left: 24px">
646 12.1.1<span style="margin-left: 1em"></span>Chunk Parts <span style="margin-left: 5mm"></span> <a
647 href="#auto-81">64</a>
648 </p>
649 </div>
650 <p style="margin-top: 1em; margin-bottom: 0.5em">
651 <b>13<span style="margin-left: 1em"></span>Storing Chunks</b> <span style="margin-left: 5mm"></span>
652 <a href="#auto-82">69</a>
653 </p>
654 <p style="margin-top: 1em; margin-bottom: 0.5em">
655 <b>14<span style="margin-left: 1em"></span>getopt</b> <span style="margin-left: 5mm"></span> <a href="#auto-83">71</a>
656 </p>
657 <p style="margin-top: 1em; margin-bottom: 0.5em">
658 <b>15<span style="margin-left: 1em"></span>Fangle LaTeX source code</b> <span style="margin-left: 5mm"></span>
659 <a href="#auto-84">75</a>
660 </p>
662 15.1<span style="margin-left: 1em"></span>fangle module <span style="margin-left: 5mm"></span> <a
663 href="#auto-85">75</a>
664 </p>
665 <div style="margin-left: 24px">
667 15.1.1<span style="margin-left: 1em"></span>The Chunk style <span style="margin-left: 5mm"></span>
668 <a href="#auto-86">75</a>
669 </p>
670 </div>
671 <div style="margin-left: 24px">
673 15.1.2<span style="margin-left: 1em"></span>The chunkref style <span style="margin-left: 5mm"></span>
674 <a href="#auto-87">76</a>
675 </p>
676 </div>
678 15.2<span style="margin-left: 1em"></span>Latex Macros <span style="margin-left: 5mm"></span> <a href="#auto-88">76</a>
679 </p>
680 <div style="margin-left: 24px">
682 15.2.1<span style="margin-left: 1em"></span>The chunk command <span style="margin-left: 5mm"></span>
683 <a href="#auto-89">77</a>
684 </p>
685 </div>
686 <div style="margin-left: 48px">
688 15.2.1.1<span style="margin-left: 1em"></span>Chunk parameters <span style="margin-left: 5mm"></span>
689 <a href="#auto-90">78</a>
690 </p>
691 </div>
692 <div style="margin-left: 24px">
694 15.2.2<span style="margin-left: 1em"></span>The noweb styled caption <span style="margin-left: 5mm"></span>
695 <a href="#auto-91">78</a>
696 </p>
697 </div>
698 <div style="margin-left: 24px">
700 15.2.3<span style="margin-left: 1em"></span>The chunk counter <span style="margin-left: 5mm"></span>
701 <a href="#auto-93">78</a>
702 </p>
703 </div>
704 <div style="margin-left: 24px">
706 15.2.4<span style="margin-left: 1em"></span>Cross references <span style="margin-left: 5mm"></span>
707 <a href="#auto-94">81</a>
708 </p>
709 </div>
710 <div style="margin-left: 24px">
712 15.2.5<span style="margin-left: 1em"></span>The end <span style="margin-left: 5mm"></span> <a href="#auto-95">82</a>
713 </p>
714 </div>
715 <p style="margin-top: 1em; margin-bottom: 0.5em">
716 <b>16<span style="margin-left: 1em"></span>Extracting fangle</b> <span style="margin-left: 5mm"></span>
717 <a href="#auto-96">83</a>
718 </p>
720 16.1<span style="margin-left: 1em"></span>Extracting from Lyx <span style="margin-left: 5mm"></span>
721 <a href="#auto-97">83</a>
722 </p>
724 16.2<span style="margin-left: 1em"></span>Extracting documentation <span style="margin-left: 5mm"></span>
725 <a href="#auto-98">83</a>
726 </p>
728 16.3<span style="margin-left: 1em"></span>Extracting from the command line <span
729 style="margin-left: 5mm"></span> <a href="#auto-99">84</a>
730 </p>
732 16.4<span style="margin-left: 1em"></span>Testing <span style="margin-left: 5mm"></span> <a href="#auto-100">84</a>
733 </p>
734 <p style="margin-top: 2em; margin-bottom: 1em">
735 <b><font size="+1">III<span style="margin-left: 1em"></span>Tests</font></b> <span
736 style="margin-left: 5mm"></span> <a href="#auto-101">85</a>
737 </p>
738 <p style="margin-top: 1em; margin-bottom: 0.5em">
739 <b>17<span style="margin-left: 1em"></span>Chunk Parameters</b> <span style="margin-left: 5mm"></span>
740 <a href="#auto-102">87</a>
741 </p>
742 <p style="margin-top: 1em; margin-bottom: 0.5em">
743 <b>18<span style="margin-left: 1em"></span>Compile-log-lyx</b> <span style="margin-left: 5mm"></span>
744 <a href="#auto-103">89</a>
745 </p>
746 </div>
747 </div>
748 <a id="auto-3"></a>
749 <br></br>
750 <b><font size="+5"><div class="center-tab">
751 Part I
752 </div><div class="right-tab">
753 <br></br>Using Fangle
754 </div></font></b>
755 </p>
756 <h1 id="auto-4">Chapter 1<br></br>Introduction to Literate Programming</h1>
758 Todo: Should really follow on from a part-0 explanation of what literate
759 programming is.
760 </p>
761 <h1 id="auto-5">Chapter 2<br></br>Running Fangle</h1>
763 Fangle is a replacement for <class style="font-variant: small-caps">noweb</class>, which consists
764 of <tt class="verbatim">notangle</tt>, <tt class="verbatim">noroots</tt> and <tt class="verbatim">noweave</tt>.
765 </p>
767 Like <tt class="verbatim">notangle</tt> and <tt class="verbatim">noroots</tt>, <tt class="verbatim">fangle</tt>
768 can read multiple named files, or from stdin.
769 </p>
770 <h2 id="auto-6">2.1<span style="margin-left: 1em"></span>Listing roots</h2>
772 The -r option causes fangle to behave like noroots.
773 </p>
775 <code>fangle -r filename.tex</code>
776 </p>
778 will print out the fangle roots of a tex file.
779 </p>
781 Unlike the <tt class="verbatim">noroots</tt> command, the printed roots are not
782 enclosed in angle brackets e.g. <tt class="verbatim">&lt;&lt;name&gt;&gt;</tt>,
783 unless at least one of the roots is defined using the <tt class="verbatim">notangle</tt>
784 notation <tt class="verbatim">&lt;&lt;name&gt;&gt;=</tt>.
785 </p>
787 Also, unlike noroots, it prints out all roots &ndash;- not just those
788 that are not used elsewhere. I find that a root not being used doesn't
789 make it particularly top level <class style="font-family: Times New Roman">&mdash;</class> and
790 so-called top level roots could also be included in another root as
791 well.
792 </p>
794 My convention is that top level roots to be extracted begin with <tt
795 class="verbatim">./</tt> and have the form of a filename.
796 </p>
798 Makefile.inc, discussed in <a href="#makefile.inc">6</a>, can automatically extract all
799 such sources prefixed with <tt class="verbatim">./</tt>
800 </p>
801 <h2 id="auto-7">2.2<span style="margin-left: 1em"></span>Extracting roots</h2>
803 notangle's <tt class="verbatim">-R</tt> and <tt class="verbatim">-L</tt> options are
804 supported.
805 </p>
807 If you are using L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X or LaTeX,
808 the standard way to extract a file would be:
809 </p>
810 <pre class="verbatim" xml:space="preserve">
811 fangle -R./Makefile.inc fangle.tex &gt; ./Makefile.inc</pre>
813 If you are using TeXmacs, the standard way to extract a file would
814 similarly be:
815 </p>
816 <pre class="verbatim" xml:space="preserve">
817 fangle -R./Makefile.inc fangle.txt &gt; ./Makefile.inc</pre>
819 TeXmacs users would obtain the text file with a <em>verbatim</em> export
820 from TeXmacs which can be done on the command line with <tt class="verbatim">texmacs
821 -s -c fangle.tm fangle.txt -q</tt>
822 </p>
824 Unlike the <tt class="verbatim">noroots</tt> command, the <tt class="verbatim"><tt class="verbatim">-L</tt></tt>
825 option to generate C pre-preocessor <tt class="verbatim">#file</tt> style
826 line-number directives,does not break indenting of the generated file..
827 </p>
829 Also, thanks to mode tracking (described in <a href="#modes">9</a>) the <tt
830 class="verbatim">-L</tt> option does not interrupt (and break) multi-line C macros
831 either.
832 </p>
834 This does mean that sometimes the compiler might calculate the source
835 line wrongly when generating error messages in such cases, but there
836 isn't any other way around if multi-line macros include other chunks.
837 </p>
839 Future releases will include a mapping file so that line/character
840 references from the C compiler can be converted to the correct part of
841 the source document.
842 </p>
843 <h2 id="auto-8">2.3<span style="margin-left: 1em"></span>Formatting the document</h2>
845 The noweave replacement built into the editing and formatting
846 environment for TeXmacs, L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X
847 (which uses LaTeX), and even for raw LaTeX.
848 </p>
850 Use of fangle with TeXmacs, L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X
851 and LaTeX are explained the the next few chapters.
852 </p>
853 <h1 id="auto-9">Chapter 3<br></br>Using Fangle with LaTeX</h1>
855 Because the noweave replacement is impemented in LaTeX, there is no
856 processing stage required before running the LaTeX command. Of course,
857 LaTeX may need running two or more times, so that the code chunk
858 references can be fully calculated.
859 </p>
861 The formatting is managed by a set of macros shown in <a href="#latex-source">15</a>,
862 and can be included with:
863 </p>
864 <pre class="verbatim" xml:space="preserve">
865 \usepackage{fangle.sty}</pre>
867 Norman Ramsay's origial <tt class="verbatim">noweb.sty</tt> package is currently
868 required as it is used for formatting the code chunk captions.
869 </p>
871 The <tt class="verbatim">listings.sty</tt> package is required, and is used for
872 formatting the code chunks and syntax highlighting.
873 </p>
875 The <tt class="verbatim">xargs.sty</tt> package is also required, and makes
876 writing LaTeX macro so much more pleasant.
877 </p>
878 <table style="display: inline; vertical-align: -0.55em">
879 <tbody><tr>
880 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: Add examples of use of Macros</td>
881 </tr></tbody>
882 </table>
883 <h1 id="auto-10">Chapter 4<br></br>Using Fangle with L<span style="margin-left: -0.1667em"></span>Y<span
884 style="margin-left: -0.125em"></span>X</h1>
886 L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X uses the same LaTeX macros
887 shown in <a href="#latex-source">15</a> as part of a L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X
888 module file <tt class="verbatim">fangle.module</tt>, which automatically includes
889 the macros in the document pre-amble provided that the fangle L<span
890 style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X module is used in the document.
891 </p>
892 <h2 id="auto-11">4.1<span style="margin-left: 1em"></span>Installing the L<span style="margin-left: -0.1667em"></span>Y<span
893 style="margin-left: -0.125em"></span>X module</h2>
895 Copy <tt class="verbatim">fangle.module</tt> to your L<span style="margin-left: -0.1667em"></span>Y<span
896 style="margin-left: -0.125em"></span>X layouts directory, which for unix users will be <tt
897 class="verbatim">~/.lyx/layouts</tt>
898 </p>
900 In order to make the new literate styles availalble, you will need to
901 reconfigure L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X by clicking
902 Tools-&gt;Reconfigure, and then re-start L<span style="margin-left: -0.1667em"></span>Y<span
903 style="margin-left: -0.125em"></span>X.
904 </p>
905 <h2 id="auto-12">4.2<span style="margin-left: 1em"></span>Obtaining a decent mono font</h2>
907 The syntax high-lighting features of <class style="font-variant: small-caps">lstlistings</class>
908 makes use of bold; however a mono-space tt font is used to typeset the
909 listings. Obtaining a <tt><strong>bold</strong> tt font</tt> can be
910 impossibly difficult and amazingly easy. I spent many hours at it,
911 following complicated instructions from those who had spend many hours
912 over it, and was finally delivered the simple solution on the lyx
913 mailing list.
914 </p>
915 <h3 id="auto-13">4.2.1<span style="margin-left: 1em"></span>txfonts</h3>
917 The simple way was to add this to my preamble:
918 </p>
919 <pre class="verbatim" xml:space="preserve">
920 \usepackage{txfonts}
921 \renewcommand{\ttdefault}{txtt}</pre>
924 </p>
925 <h3 id="auto-14">4.2.2<span style="margin-left: 1em"></span>ams pmb</h3>
927 The next simplest way was to use ams poor-mans-bold, by adding this to
928 the pre-amble:
929 </p>
930 <pre class="verbatim" xml:space="preserve">
931 \usepackage{amsbsy}
932 %\renewcommand{\ttdefault}{txtt}
933 %somehow make \pmb be the command for bold, forgot how, sorry, above line not work</pre>
935 It works, but looks wretched on the dvi viewer.
936 </p>
937 <h3 id="auto-15">4.2.3<span style="margin-left: 1em"></span>Luximono</h3>
939 The lstlistings documention suggests using Luximono.
940 </p>
942 Luximono was installed according to the instructions in Ubuntu Forums
943 thread 1159181
945 <font size="-1"><div align="justify">
946 <div style="margin-left: 0px">
947 <div style="margin-right: 0px">
949 . http://ubuntuforums.org/showthread.php?t=1159181
950 </p>
951 </div>
952 </div>
953 </div></font>
954 </p>
955 <span style="margin-left: 0em"></span>
956 <a id="footnr-1"></a>
957 <sup><a href="#footnote-1">1</a></sup>
958 with tips from miknight
960 <font size="-1"><div align="justify">
961 <div style="margin-left: 0px">
962 <div style="margin-right: 0px">
965 http://miknight.blogspot.com/2005/11/how-to-install-luxi-mono-font-in.html
966 </p>
967 </div>
968 </div>
969 </div></font>
970 </p>
971 <span style="margin-left: 0em"></span>
972 <a id="footnr-2"></a>
973 <sup><a href="#footnote-2">2</a></sup>
974 stating that
975 <tt class="verbatim">sudo updmap &ndash;enable MixedMap ul9.map</tt>
976 is required. It looks fine in PDF and PS view but still looks rotten in
977 dvi view.
978 </p>
979 <h2 id="auto-16">4.3<span style="margin-left: 1em"></span>Formatting your Lyx document</h2>
981 It is not necessary to base your literate document on any of the
982 original L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X literate classes;
983 so select a regular class for your document type.
984 </p>
986 Add the new module <em>Fangle Literate Listings</em> and also
987 <em>Logical Markup</em> which is very useful.
988 </p>
990 In the drop-down style listbox you should notice a new style defined,
991 called <em>Chunk</em>.
992 </p>
994 When you wish to insert a literate chunk, you enter it's plain name in
995 the Chunk style, instead of the old <class style="font-variant: small-caps">noweb</class> method
996 that uses <tt class="verbatim">&lt;&lt;name&gt;&gt;=</tt> type tags. In the line
997 (or paragraph) following the chunk name, you insert a listing with:
998 Insert-&gt;Program Listing.
999 </p>
1001 Inside the white listing box you can type (or paste using
1002 <kbd>shift+ctrl+V</kbd>) your listing. There is no need to use
1003 <kbd>ctrl+enter</kbd> at the end of lines as with some older L<span
1004 style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X literate techniques &ndash;- just
1005 press enter as normal.
1006 </p>
1007 <h3 id="auto-17">4.3.1<span style="margin-left: 1em"></span>Customising the listing appearance</h3>
1009 The code is formatted using the <class style="font-variant: small-caps">lstlistings</class>
1010 package. The chunk style doesn't just define the chunk name, but can
1011 also define any other chunk options supported by the lstlistings package
1012 <tt class="verbatim">\lstset</tt> command. In fact, what you type in the chunk
1013 style is raw latex. If you want to set the chunk language without having
1014 to right-click the listing, just add <tt class="verbatim">,lanuage=C</tt> after
1015 the chunk name. (Currently the language will affect all subsequent
1016 listings, so you may need to specify <tt class="verbatim">,language=</tt> quite a
1017 lot).
1018 </p>
1019 <table style="display: inline; vertical-align: -0.55em">
1020 <tbody><tr>
1021 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: so fix the bug</td>
1022 </tr></tbody>
1023 </table>
1025 Of course you can do this by editing the listings box advanced
1026 properties by right-clicking on the listings box, but that takes longer,
1027 and you can't see at-a-glance what the advanced settings are while
1028 editing the document; also advanced settings apply only to that box
1029 &ndash;- the chunk settings apply through the rest of the document
1031 <font size="-1"><div align="justify">
1032 <div style="margin-left: 0px">
1033 <div style="margin-right: 0px">
1035 . It ought to apply only to subsequent chunks of the same
1036 name. I'll fix that later
1037 </p>
1038 </div>
1039 </div>
1040 </div></font>
1041 </p>
1042 <span style="margin-left: 0em"></span>
1043 <a id="footnr-3"></a>
1044 <sup><a href="#footnote-3">3</a></sup>
1046 </p>
1047 <table style="display: inline; vertical-align: -0.55em">
1048 <tbody><tr>
1049 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: So make sure they only apply to chunks of
1050 that name</td>
1051 </tr></tbody>
1052 </table>
1053 <h3 id="auto-18">4.3.2<span style="margin-left: 1em"></span>Global customisations</h3>
1055 As lstlistings is used to set the code chunks, it's <tt class="verbatim">\lstset</tt>
1056 command can be used in the pre-amble to set some document wide settings.
1057 </p>
1059 If your source has many words with long sequences of capital letters,
1060 then <tt class="verbatim">columns=fullflexible</tt> may be a good idea, or the
1061 capital letters will get crowded. (I think lstlistings ought to use a
1062 slightly smaller font for captial letters so that they still fit).
1063 </p>
1065 The font family <tt class="verbatim">\ttfamily</tt> looks more normal for code,
1066 but has no bold (an alternate typewriter font is used).
1067 </p>
1069 With <tt class="verbatim">\ttfamily</tt>, I must also specify <tt class="verbatim">columns=fullflexible</tt>
1070 or the wrong letter spacing is used.
1071 </p>
1073 In my LaTeX pre-amble I usually specialise my code format with:
1074 </p>
1076 <font size="-1"><p>
1077 <a id="code-label-document-preamble-1"></a>
1078 <table style="width: 100%" id="code-ref-document-preamble-1">
1079 <tbody><tr>
1080 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="document-preamble"></a><font size="-1"><font color="blue">document-preamble</font>[1](),
1081 lang=<font color="blue">tex</font> &equiv;</font></td>
1082 </tr></tbody>
1083 </table>
1084 </p><p>
1086 <tt class="verbatim"><div class="compact-block">
1087 <tt>1 </tt>\lstset{
1088 </div></tt>
1089 </p>
1090 <pre class="verbatim" xml:space="preserve">
1091 <div class="compact-block"><tt>2 </tt>numbers=left, stepnumber=1, numbersep=5pt,
1092 <tt>3 </tt>breaklines=false,
1093 <tt>4 </tt>basicstyle=\footnotesize\ttfamily,
1094 <tt>5 </tt>numberstyle=\tiny,
1095 <tt>6 </tt>language=C,
1096 <tt>7 </tt>columns=fullflexible,
1097 <tt>8 </tt>numberfirstline=true</div></pre>
1099 <tt class="verbatim"><div class="compact-block">
1100 <tt>9 </tt>}
1101 </div></tt>
1102 </p>
1103 </p><table style="width: 100%" id="code-end-document-preamble-1">
1104 <tbody><tr>
1105 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
1106 </tr></tbody>
1107 </table></font>
1108 </p>
1111 </p>
1112 <h2 id="auto-19">4.4<span style="margin-left: 1em"></span>Configuring the build script</h2>
1114 You can invoke code extraction and building from the L<span style="margin-left: -0.1667em"></span>Y<span
1115 style="margin-left: -0.125em"></span>X menu option Document-&gt;Build Program.
1116 </p>
1118 First, make sure you don't have a conversion defined for Lyx-&gt;Program
1119 </p>
1121 From the menu Tools-&gt;Preferences, add a conversion from
1122 Latex(Plain)-&gt;Program as:
1123 </p>
1124 <pre class="verbatim" xml:space="preserve">
1125 set -x ; fangle -Rlyx-build $$i |
1126 env LYX_b=$$b LYX_i=$$i LYX_o=$$o LYX_p=$$p LYX_r=$$r bash</pre>
1128 (But don't cut-n-paste it from this document or you may be be pasting a
1129 multi-line string which will break your lyx preferences file).
1130 </p>
1132 I hope that one day, L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X will
1133 set these into the environment when calling the build script.
1134 </p>
1136 You may also want to consider adding options to this conversion...
1137 </p>
1138 <pre class="verbatim" xml:space="preserve">
1139 parselog=/usr/share/lyx/scripts/listerrors</pre>
1141 ...but if you do you will lose your stderr
1143 <font size="-1"><div align="justify">
1144 <div style="margin-left: 0px">
1145 <div style="margin-right: 0px">
1147 . There is some bash plumbing to get a copy of stderr but this
1148 footnote is too small
1149 </p>
1150 </div>
1151 </div>
1152 </div></font>
1153 </p>
1154 <span style="margin-left: 0em"></span>
1155 <a id="footnr-4"></a>
1156 <sup><a href="#footnote-4">4</a></sup>
1158 </p>
1160 Now, a shell script chunk called <tt class="verbatim">lyx-build</tt> will be
1161 extracted and run whenever you choose the Document-&gt;Build Program
1162 menu item.
1163 </p>
1165 This document was originally managed using L<span style="margin-left: -0.1667em"></span>Y<span
1166 style="margin-left: -0.125em"></span>X and lyx-build script for this document is shown here for
1167 historical reference.
1168 </p>
1169 <pre class="verbatim" xml:space="preserve">
1170 lyx -e latex fangle.lyx &amp;&amp; \
1171 fangle fangle.lyx &gt; ./autoboot</pre>
1173 This looks simple enough, but as mentioned, fangle has to be had from
1174 somewhere before it can be extracted.
1175 </p>
1176 <h3 id="auto-20">4.4.1<span style="margin-left: 1em"></span>...</h3>
1178 When the lyx-build chunk is executed, the current directory will be a
1179 temporary directory, and <tt class="verbatim">LYX_SOURCE</tt> will refer to the
1180 tex file in this temporary directory. This is unfortunate as our
1181 makefile wants to run from the project directory where the Lyx file is
1182 kept.
1183 </p>
1185 We can extract the project directory from <tt class="verbatim">$$r</tt>, and
1186 derive the probable Lyx filename from the noweb file that Lyx generated.
1187 </p>
1189 <font size="-1"><p>
1190 <a id="code-label-lyx-build-helper-1"></a>
1191 <table style="width: 100%" id="code-ref-lyx-build-helper-1">
1192 <tbody><tr>
1193 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="lyx-build-helper"></a><font size="-1"><div class="left-tab">
1194 <font color="blue">lyx-build-helper</font>[1](), lang=<font color="blue">sh</font>
1195 &equiv;
1196 </div><div class="right-tab">
1197 <a href="#code-ref-lyx-build-helper-2">83c</a>&#x22B3;
1198 </div></font></td>
1199 </tr></tbody>
1200 </table>
1201 </p><p>
1203 <tt class="verbatim"><div class="compact-block">
1204 <tt>1 </tt>PROJECT_DIR=&quot;$LYX_r&quot;
1205 </div></tt>
1206 </p>
1207 <pre class="verbatim" xml:space="preserve">
1208 <div class="compact-block"><tt>2 </tt>LYX_SRC=&quot;$PROJECT_DIR/${LYX_i%.tex}.lyx&quot;
1209 <tt>3 </tt>TEX_DIR=&quot;$LYX_p&quot;</div></pre>
1211 <tt class="verbatim"><div class="compact-block">
1212 <tt>4 </tt>TEX_SRC=&quot;$TEX_DIR/$LYX_i&quot;
1213 </div></tt>
1214 </p>
1215 </p><p>
1216 <a id="code-end-lyx-build-helper-1"></a>
1217 </p></font>
1218 </p>
1220 And then we can define a lyx-build fragment similar to the autoboot
1221 fragment
1222 </p>
1224 <font size="-1"><p>
1225 <a id="code-label-lyx-build-1"></a>
1226 <table style="width: 100%" id="code-ref-lyx-build-1">
1227 <tbody><tr>
1228 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="lyx-build"></a><font size="-1"><div class="left-tab">
1229 <font color="blue">lyx-build</font>[1](), lang=<font color="blue">sh</font>
1230 &equiv;
1231 </div><div class="right-tab">
1232 <a href="#code-ref-lyx-build-2">83a</a>&#x22B3;
1233 </div></font></td>
1234 </tr></tbody>
1235 </table>
1236 </p><p>
1238 <tt class="verbatim"><div class="compact-block">
1239 <tt>1 </tt>#! /bin/sh
1240 </div></tt>
1241 </p>
1242 <pre class="verbatim" xml:space="preserve">
1243 <div class="compact-block"><tt>2 </tt>=&lt;\chunkref{lyx-build-helper}&gt;
1244 <tt>3 </tt>cd $PROJECT_DIR || exit 1
1245 <tt>4 </tt>
1246 <tt>5 </tt>#/usr/bin/fangle -filter ./notanglefix-filter \
1247 <tt>6 </tt># -R./Makefile.inc &quot;../../noweb-lyx/noweb-lyx3.lyx&quot; \
1248 <tt>7 </tt># | sed '/NOWEB_SOURCE=/s/=.*/=samba4-dfs.lyx/' \
1249 <tt>8 </tt># &gt; ./Makefile.inc
1250 <tt>9 </tt>#</div></pre>
1252 <tt class="verbatim"><div class="compact-block">
1253 <tt>10 </tt>#make -f ./Makefile.inc fangle_sources
1254 </div></tt>
1255 </p>
1256 </p><p>
1257 <a id="code-end-lyx-build-1"></a>
1258 </p></font>
1259 </p>
1262 </p>
1263 <h1 id="auto-21">Chapter 5<br></br>Using Fangle with TeXmacs</h1>
1264 <table style="display: inline; vertical-align: -0.55em">
1265 <tbody><tr>
1266 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: Write this chapter</td>
1267 </tr></tbody>
1268 </table>
1269 <h1 id="auto-22"><a id="makefile.inc"></a>Chapter 6<br></br>Fangle with Makefiles</h1>
1271 Here we describe a <tt class="verbatim">Makefile.inc</tt> that you can include in
1272 your own Makefiles, or glue as a recursive make to other projects.
1273 </p>
1275 <tt class="verbatim">Makefile.inc</tt> will cope with extracting all the other
1276 source files from this or any specified literate document and keeping
1277 them up to date.
1278 </p>
1280 It may also be included by a <tt class="verbatim">Makefile</tt> or <tt class="verbatim">Makefile.am</tt>
1281 defined in a literate document to automatically deal with the extraction
1282 of source files and documents during normal builds.
1283 </p>
1285 Thus, if <tt class="verbatim">Makefile.inc</tt> is included into a main project
1286 makefile it add rules for the source files, capable of extracting the
1287 source files from the literate document.
1288 </p>
1289 <h2 id="auto-23">6.1<span style="margin-left: 1em"></span>A word about makefiles formats</h2>
1291 Whitespace formatting is very important in a Makefile. The first
1292 character of each action line must be a TAB.
1293 </p>
1294 <pre class="verbatim" xml:space="preserve">
1295 target: pre-requisite
1296 &#x21A6;action
1297 &#x21A6;action</pre>
1299 This requires that the literate programming environment have the ability
1300 to represent a TAB character in a way that fangle will generate an
1301 actual TAB character.
1302 </p>
1304 We also adopt a convention that code chunks whose names beginning with
1305 <tt class="verbatim">./</tt> should always be automatically extracted from the
1306 document. Code chunks whose names do not begin with <tt class="verbatim">./</tt>
1307 are for internal reference. Such chunks may be extracted directly, but
1308 will not be automatically extracted by this Makefile.
1309 </p>
1310 <h2 id="auto-24">6.2<span style="margin-left: 1em"></span>Extracting Sources</h2>
1312 Our makefile has two parts; variables must be defined before the targets
1313 that use them.
1314 </p>
1316 As we progress through this chapter, explaining concepts, we will be
1317 adding lines to Makefile.inc-vars <a href="#code-ref-Makefile.inc-vars-1">23b</a> and
1318 Makefile.inc-targets <a href="#code-ref-Makefile.inc-targets-1">24b</a> which are included in
1319 ./Makefile.inc <a href="#code-ref-./Makefile.inc-1">23a</a> below.
1320 </p>
1322 <font size="-1"><p>
1323 <a id="code-label-./Makefile.inc-1"></a>
1324 <table style="width: 100%" id="code-ref-./Makefile.inc-1">
1325 <tbody><tr>
1326 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./Makefile.inc"></a><font size="-1"><font color="blue">./Makefile.inc</font>[1](),
1327 lang=<font color="blue">make</font> &equiv;</font></td>
1328 </tr></tbody>
1329 </table>
1330 </p><p>
1332 <tt class="verbatim"><div class="compact-block">
1333 <tt>1 </tt>Makefile.inc-vars <a href="#code-ref-Makefile.inc-vars-1">23b</a>
1334 </div></tt>
1335 </p>
1336 <pre class="verbatim" xml:space="preserve">
1337 <div class="compact-block"></div></pre>
1339 <tt class="verbatim"><div class="compact-block">
1340 <tt>2 </tt>Makefile.inc-targets <a href="#code-ref-Makefile.inc-targets-1">24b</a>
1341 </div></tt>
1342 </p>
1343 </p><table style="width: 100%" id="code-end-./Makefile.inc-1">
1344 <tbody><tr>
1345 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
1346 </tr></tbody>
1347 </table></font>
1348 </p>
1350 We first define a placeholder for <tt class="verbatim">LITERATE_SOURCE</tt> to
1351 hold the name of this document. This will normally be passed on the
1352 command line.
1353 </p>
1355 <font size="-1"><p>
1356 <a id="code-label-Makefile.inc-vars-1"></a>
1357 <table style="width: 100%" id="code-ref-Makefile.inc-vars-1">
1358 <tbody><tr>
1359 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1360 <font color="blue">Makefile.inc-vars</font>[1](), lang=<font color="blue"></font>
1361 &equiv;
1362 </div><div class="right-tab">
1363 <a href="#code-ref-Makefile.inc-vars-2">24a</a>&#x22B3;
1364 </div></font></td>
1365 </tr></tbody>
1366 </table>
1367 </p><p>
1368 <tt class="verbatim"><div class="compact-block">
1369 <tt>1 </tt>LITERATE_SOURCE=
1370 </div></tt>
1371 </p><p>
1372 <a id="code-end-Makefile.inc-vars-1"></a>
1373 </p></font>
1374 </p>
1376 Fangle cannot process L
1377 <span style="margin-left: -0.1667em"></span>
1379 <span style="margin-left: -0.125em"></span>
1380 X or TeXmacs documents directly, so the first stage is to convert these
1381 to more suitable text based formats
1383 <font size="-1"><div align="justify">
1384 <div style="margin-left: 0px">
1385 <div style="margin-right: 0px">
1387 . L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X and TeXmacs
1388 formats are text-based, but not suitable for fangle
1389 </p>
1390 </div>
1391 </div>
1392 </div></font>
1393 </p>
1394 <span style="margin-left: 0em"></span>
1395 <a id="footnr-1"></a>
1396 <sup><a href="#footnote-1">1</a></sup>
1398 </p>
1399 <h3 id="auto-25"><a id="Converting-from-Lyx"></a>6.2.1<span style="margin-left: 1em"></span>Converting from L<span style="margin-left: -0.1667em"></span>Y<span
1400 style="margin-left: -0.125em"></span>X to LaTeX</h3>
1402 The first stage will always be to convert the L
1403 <span style="margin-left: -0.1667em"></span>
1405 <span style="margin-left: -0.125em"></span>
1406 X file to a LaTeX file. Fangle must run on a TeX file because the L
1407 <span style="margin-left: -0.1667em"></span>
1409 <span style="margin-left: -0.125em"></span>
1410 X command
1411 <tt class="verbatim">server-goto-file-line</tt>
1413 <font size="-1"><div align="justify">
1414 <div style="margin-left: 0px">
1415 <div style="margin-right: 0px">
1417 . The Lyx command <tt class="verbatim">server-goto-file-line</tt> is
1418 used to position the Lyx cursor at the compiler errors.
1419 </p>
1420 </div>
1421 </div>
1422 </div></font>
1423 </p>
1424 <span style="margin-left: 0em"></span>
1425 <a id="footnr-2"></a>
1426 <sup><a href="#footnote-2">2</a></sup>
1427 requries that the line number provided be a line of the TeX file and
1428 always maps this the line in the L
1429 <span style="margin-left: -0.1667em"></span>
1431 <span style="margin-left: -0.125em"></span>
1432 X docment. We use
1433 <tt class="verbatim">server-goto-file-line</tt>
1434 when moving the cursor to error lines during compile failures.
1435 </p>
1437 The command <tt class="verbatim">lyx -e literate fangle.lyx</tt> will produce <tt
1438 class="verbatim">fangle.tex</tt>, a TeX file; so we define a make target to be the
1439 same as the L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X file but with
1440 the <tt class="verbatim">.tex</tt> extension.
1441 </p>
1443 The <tt class="verbatim">EXTRA_DIST</tt> is for automake support so that the TeX
1444 files will automaticaly be distributed with the source, to help those
1445 who don't have L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X installed.
1446 </p>
1448 <font size="-1"><p>
1449 <a id="code-label-Makefile.inc-vars-2"></a>
1450 <table style="width: 100%" id="code-ref-Makefile.inc-vars-2">
1451 <tbody><tr>
1452 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1453 <font color="blue">Makefile.inc-vars</font>[2]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
1454 lang=<font color="blue"></font> + &equiv;
1455 </div><div class="right-tab">
1456 &#x22B2;<a href="#code-ref-Makefile.inc-vars-1">23b</a> <a href="#code-ref-Makefile.inc-vars-3">24c</a>&#x25BF;
1457 </div></font></td>
1458 </tr></tbody>
1459 </table>
1460 </p><p>
1462 <tt class="verbatim"><div class="compact-block">
1463 <tt>2 </tt>TEX_SOURCE=$(LYX_SOURCE:.lyx=.tex)
1464 </div></tt>
1465 </p>
1466 <pre class="verbatim" xml:space="preserve">
1467 <div class="compact-block"></div></pre>
1469 <tt class="verbatim"><div class="compact-block">
1470 <tt>3 </tt>EXTRA_DIST+=$(TEX_SOURCE)
1471 </div></tt>
1472 </p>
1473 </p><p>
1474 <a id="code-end-Makefile.inc-vars-2"></a>
1475 </p></font>
1476 </p>
1478 We then specify that the TeX source is to be generated from the L<span
1479 style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X source.
1480 </p>
1482 <font size="-1"><p>
1483 <a id="code-label-Makefile.inc-targets-1"></a>
1484 <table style="width: 100%" id="code-ref-Makefile.inc-targets-1">
1485 <tbody><tr>
1486 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
1487 <font color="blue">Makefile.inc-targets</font>[1](), lang=<font color="blue"></font>
1488 &equiv;
1489 </div><div class="right-tab">
1490 <a href="#code-ref-Makefile.inc-targets-2">24d</a>&#x25BF;
1491 </div></font></td>
1492 </tr></tbody>
1493 </table>
1494 </p><p>
1496 <tt class="verbatim"><div class="compact-block">
1497 <tt>1 </tt>$(TEX_SOURCE): $(LYX_SOURCE)
1498 </div></tt>
1499 </p>
1500 <pre class="verbatim" xml:space="preserve">
1501 <div class="compact-block"><tt>2 </tt>&#x21A6;lyx -e latex $&lt;
1502 <tt>3 </tt>clean_tex:
1503 <tt>4 </tt>&#x21A6;rm -f -- $(TEX_SOURCE)</div></pre>
1505 <tt class="verbatim"><div class="compact-block">
1506 <tt>5 </tt>clean: clean_tex
1507 </div></tt>
1508 </p>
1509 </p><p>
1510 <a id="code-end-Makefile.inc-targets-1"></a>
1511 </p></font>
1512 </p>
1513 <h3 id="auto-26"><a id="Converting-from-Lyx"></a>6.2.2<span style="margin-left: 1em"></span>Converting from TeXmacs</h3>
1515 Fangle cannot process TeXmacs files directly
1517 <font size="-1"><div align="justify">
1518 <div style="margin-left: 0px">
1519 <div style="margin-right: 0px">
1521 . but this is planned when TeXmacs uses xml as it's native
1522 format
1523 </p>
1524 </div>
1525 </div>
1526 </div></font>
1527 </p>
1528 <span style="margin-left: 0em"></span>
1529 <a id="footnr-3"></a>
1530 <sup><a href="#footnote-3">3</a></sup>
1531 , but must first convert them to text files.
1532 </p>
1534 The command <tt class="verbatim">texmacs -c fangle.tm fangle.txt -q</tt> will
1535 produce <tt class="verbatim">fangle.txt</tt>, a text file; so we define a make
1536 target to be the same as the TeXmacs file but with the <tt class="verbatim">.txt</tt>
1537 extension.
1538 </p>
1540 The <tt class="verbatim">EXTRA_DIST</tt> is for automake support so that the TeX
1541 files will automaticaly be distributed with the source, to help those
1542 who don't have L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X installed.
1543 </p>
1545 <font size="-1"><p>
1546 <a id="code-label-Makefile.inc-vars-3"></a>
1547 <table style="width: 100%" id="code-ref-Makefile.inc-vars-3">
1548 <tbody><tr>
1549 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1550 <font color="blue">Makefile.inc-vars</font>[3]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
1551 lang=<font color="blue"></font> + &equiv;
1552 </div><div class="right-tab">
1553 &#x25B5;<a href="#code-ref-Makefile.inc-vars-2">24a</a> <a href="#code-ref-Makefile.inc-vars-4">25a</a>&#x22B3;
1554 </div></font></td>
1555 </tr></tbody>
1556 </table>
1557 </p><p>
1559 <tt class="verbatim"><div class="compact-block">
1560 <tt>4 </tt>TXT_SOURCE=$(LITERATE_SOURCE:.tm=.txt)
1561 </div></tt>
1562 </p>
1563 <pre class="verbatim" xml:space="preserve">
1564 <div class="compact-block"></div></pre>
1566 <tt class="verbatim"><div class="compact-block">
1567 <tt>5 </tt>EXTRA_DIST+=$(TXT_SOURCE)
1568 </div></tt>
1569 </p>
1570 </p><p>
1571 <a id="code-end-Makefile.inc-vars-3"></a>
1572 </p></font>
1573 </p>
1574 <table style="display: inline; vertical-align: -0.55em">
1575 <tbody><tr>
1576 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: Add loop around each $&lt; so multiple
1577 targets can be specified</td>
1578 </tr></tbody>
1579 </table>
1581 <font size="-1"><p>
1582 <a id="code-label-Makefile.inc-targets-2"></a>
1583 <table style="width: 100%" id="code-ref-Makefile.inc-targets-2">
1584 <tbody><tr>
1585 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
1586 <font color="blue">Makefile.inc-targets</font>[2]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
1587 lang=<font color="blue"></font> + &equiv;
1588 </div><div class="right-tab">
1589 &#x25B5;<a href="#code-ref-Makefile.inc-targets-1">24b</a> <a href="#code-ref-Makefile.inc-targets-3">25c</a>&#x22B3;
1590 </div></font></td>
1591 </tr></tbody>
1592 </table>
1593 </p><p>
1595 <tt class="verbatim"><div class="compact-block">
1596 <tt>6 </tt>$(TXT_SOURCE): $(LITERATE_SOURCE)
1597 </div></tt>
1598 </p>
1599 <pre class="verbatim" xml:space="preserve">
1600 <div class="compact-block"><tt>7 </tt>&#x21A6;texmacs -c $&lt; $(TXT_SOURCE) -q
1601 <tt>8 </tt>clean_txt:
1602 <tt>9 </tt>&#x21A6;rm -f -- $(TXT_SOURCE)</div></pre>
1604 <tt class="verbatim"><div class="compact-block">
1605 <tt>10 </tt>clean: clean_txt
1606 </div></tt>
1607 </p>
1608 </p><p>
1609 <a id="code-end-Makefile.inc-targets-2"></a>
1610 </p></font>
1611 </p>
1612 <h2 id="auto-27">6.3<span style="margin-left: 1em"></span>Extracting Program Source</h2>
1614 The program source is extracted using fangle, which is designed to
1615 operate on text or a LaTeX documents
1617 <font size="-1"><div align="justify">
1618 <div style="margin-left: 0px">
1619 <div style="margin-right: 0px">
1621 . LaTeX documents are just slightly special text documents
1622 </p>
1623 </div>
1624 </div>
1625 </div></font>
1626 </p>
1627 <span style="margin-left: 0em"></span>
1628 <a id="footnr-4"></a>
1629 <sup><a href="#footnote-4">4</a></sup>
1631 </p>
1633 <font size="-1"><p>
1634 <a id="code-label-Makefile.inc-vars-4"></a>
1635 <table style="width: 100%" id="code-ref-Makefile.inc-vars-4">
1636 <tbody><tr>
1637 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1638 <font color="blue">Makefile.inc-vars</font>[4]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
1639 lang=<font color="blue"></font> + &equiv;
1640 </div><div class="right-tab">
1641 &#x22B2;<a href="#code-ref-Makefile.inc-vars-3">24c</a> <a href="#code-ref-Makefile.inc-vars-5">25b</a>&#x25BF;
1642 </div></font></td>
1643 </tr></tbody>
1644 </table>
1645 </p><p>
1646 <tt class="verbatim"><div class="compact-block">
1647 <tt>6 </tt>FANGLE_SOURCE=$(TEX_SOURCE) $(TXT_SOURCE)
1648 </div></tt>
1649 </p><p>
1650 <a id="code-end-Makefile.inc-vars-4"></a>
1651 </p></font>
1652 </p>
1654 The literate document can result in any number of source files, but not
1655 all of these will be changed each time the document is updated. We
1656 certainly don't want to update the timestamps of these files and cause
1657 the whole source tree to be recompiled just because the literate
1658 explanation was revised. We use <tt class="verbatim">CPIF</tt> from the
1659 <em>Noweb</em> tools to avoid updating the file if the content has not
1660 changed, but should probably write our own.
1661 </p>
1663 However, if a source file is not updated, then the fangle file will
1664 always have a newer time-stamp and the makefile would always re-attempt
1665 to extact a newer source file which would be a waste of time.
1666 </p>
1668 Because of this, we use a stamp file which is always updated each time
1669 the sources are fully extracted from the LaTeX document. If the stamp
1670 file is newer than the document, then we can avoid an attempt to
1671 re-extract any of the sources. Because this stamp file is only updated
1672 when extraction is complete, it is safe for the user to interrupt the
1673 build-process mid-extraction.
1674 </p>
1676 We use <tt class="verbatim">echo</tt> rather than <tt class="verbatim">touch</tt> to update
1677 the stamp file beause the <tt class="verbatim">touch</tt> command does not work
1678 very well over an <tt class="verbatim">sshfs</tt>mount that I was using.
1679 </p>
1681 <font size="-1"><p>
1682 <a id="code-label-Makefile.inc-vars-5"></a>
1683 <table style="width: 100%" id="code-ref-Makefile.inc-vars-5">
1684 <tbody><tr>
1685 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1686 <font color="blue">Makefile.inc-vars</font>[5]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
1687 lang=<font color="blue"></font> + &equiv;
1688 </div><div class="right-tab">
1689 &#x25B5;<a href="#code-ref-Makefile.inc-vars-4">25a</a> <a href="#code-ref-Makefile.inc-vars-6">26a</a>&#x22B3;
1690 </div></font></td>
1691 </tr></tbody>
1692 </table>
1693 </p><p>
1694 <tt class="verbatim"><div class="compact-block">
1695 <tt>7 </tt>FANGLE_SOURCE_STAMP=$(FANGLE_SOURCE).stamp
1696 </div></tt>
1697 </p><p>
1698 <a id="code-end-Makefile.inc-vars-5"></a>
1699 </p></font>
1700 </p>
1702 <font size="-1"><p>
1703 <a id="code-label-Makefile.inc-targets-3"></a>
1704 <table style="width: 100%" id="code-ref-Makefile.inc-targets-3">
1705 <tbody><tr>
1706 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
1707 <font color="blue">Makefile.inc-targets</font>[3]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
1708 lang=<font color="blue"></font> + &equiv;
1709 </div><div class="right-tab">
1710 &#x22B2;<a href="#code-ref-Makefile.inc-targets-2">24d</a> <a href="#code-ref-Makefile.inc-targets-4">26b</a>&#x22B3;
1711 </div></font></td>
1712 </tr></tbody>
1713 </table>
1714 </p><p>
1716 <tt class="verbatim"><div class="compact-block">
1717 <tt>11 </tt>$(FANGLE_SOURCE_STAMP): $(FANGLE_SOURCE) \
1718 </div></tt>
1719 </p>
1720 <pre class="verbatim" xml:space="preserve">
1721 <div class="compact-block"><tt>12 </tt>&#x21A6; $(FANGLE_SOURCES) ; \
1722 <tt>13 </tt>&#x21A6;echo -n &gt; $(FANGLE_SOURCE_STAMP)
1723 <tt>14 </tt>clean_stamp:
1724 <tt>15 </tt>&#x21A6;rm -f $(FANGLE_SOURCE_STAMP)</div></pre>
1726 <tt class="verbatim"><div class="compact-block">
1727 <tt>16 </tt>clean: clean_stamp
1728 </div></tt>
1729 </p>
1730 </p><p>
1731 <a id="code-end-Makefile.inc-targets-3"></a>
1732 </p></font>
1733 </p>
1734 <h2 id="auto-28">6.4<span style="margin-left: 1em"></span>Extracting Source Files</h2>
1736 We compute <tt class="verbatim">FANGLE_SOURCES</tt> to hold the names of all the
1737 source files defined in the document. We compute this only once, by
1738 means of <tt class="verbatim">:=</tt> in assignent. The sed deletes the any <tt
1739 class="verbatim">&lt;&lt;</tt> and <tt class="verbatim">&gt;&gt;</tt> which may surround the
1740 roots names (for compatibility with Noweb's noroots command).
1741 </p>
1743 As we use chunk names beginning with <tt class="verbatim">./</tt> to denote top
1744 level fragments that should be extracted, we filter out all fragments
1745 that do not begin with <tt class="verbatim">./</tt>
1746 </p>
1747 <p style="margin-top: 1em; margin-bottom: 1em">
1748 <b>Note <class style="font-style: normal">1</class>. </b><tt class="verbatim">FANGLE_PREFIX</tt> is
1749 set to <tt class="verbatim">./</tt> by default, but whatever it may be overridden
1750 to, the prefix is replaced by a literal <tt class="verbatim">./</tt> before
1751 extraction so that files will be extracted in the current directory
1752 whatever the prefix. This helps namespace or sub-project prefixes like
1753 <tt class="verbatim">documents:</tt> for chunks like <tt class="verbatim">documents:docbook/intro.xml</tt>
1754 </p>
1755 <table style="display: inline; vertical-align: -0.55em">
1756 <tbody><tr>
1757 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: This doesn't work though, because it loses
1758 the full name and doesn't know what to extact!</td>
1759 </tr></tbody>
1760 </table>
1762 <font size="-1"><p>
1763 <a id="code-label-Makefile.inc-vars-6"></a>
1764 <table style="width: 100%" id="code-ref-Makefile.inc-vars-6">
1765 <tbody><tr>
1766 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1767 <font color="blue">Makefile.inc-vars</font>[6]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
1768 lang=<font color="blue"></font> + &equiv;
1769 </div><div class="right-tab">
1770 &#x22B2;<a href="#code-ref-Makefile.inc-vars-5">25b</a> <a href="#code-ref-Makefile.inc-vars-7">26e</a>&#x25BF;
1771 </div></font></td>
1772 </tr></tbody>
1773 </table>
1774 </p><p>
1776 <tt class="verbatim"><div class="compact-block">
1777 <tt>8 </tt>FANGLE_PREFIX:=\.\/
1778 </div></tt>
1779 </p>
1780 <pre class="verbatim" xml:space="preserve">
1781 <div class="compact-block"><tt>9 </tt>FANGLE_SOURCES:=$(shell \
1782 <tt>10 </tt> fangle -r $(FANGLE_SOURCE) |\
1783 <tt>11 </tt> sed -e 's/^[&lt;][&lt;]//;s/[&gt;][&gt;]$$//;/^$(FANGLE_PREFIX)/!d' \</div></pre>
1785 <tt class="verbatim"><div class="compact-block">
1786 <tt>12 </tt> -e 's/^$(FANGLE_PREFIX)/\.\//' )
1787 </div></tt>
1788 </p>
1789 </p><p>
1790 <a id="code-end-Makefile.inc-vars-6"></a>
1791 </p></font>
1792 </p>
1794 The target below, <tt class="verbatim">echo_fangle_sources</tt> is a helpful
1795 debugging target and shows the names of the files that would be
1796 extracted.
1797 </p>
1799 <font size="-1"><p>
1800 <a id="code-label-Makefile.inc-targets-4"></a>
1801 <table style="width: 100%" id="code-ref-Makefile.inc-targets-4">
1802 <tbody><tr>
1803 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
1804 <font color="blue">Makefile.inc-targets</font>[4]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
1805 lang=<font color="blue"></font> + &equiv;
1806 </div><div class="right-tab">
1807 &#x22B2;<a href="#code-ref-Makefile.inc-targets-3">25c</a> <a href="#code-ref-Makefile.inc-targets-5">26c</a>&#x25BF;
1808 </div></font></td>
1809 </tr></tbody>
1810 </table>
1811 </p><p>
1813 <tt class="verbatim"><div class="compact-block">
1814 <tt>17 </tt>.PHONY: echo_fangle_sources
1815 </div></tt>
1816 </p>
1817 <pre class="verbatim" xml:space="preserve">
1818 <div class="compact-block"></div></pre>
1820 <tt class="verbatim"><div class="compact-block">
1821 <tt>18 </tt>echo_fangle_sources: ; @echo $(FANGLE_SOURCES)
1822 </div></tt>
1823 </p>
1824 </p><p>
1825 <a id="code-end-Makefile.inc-targets-4"></a>
1826 </p></font>
1827 </p>
1829 We define a convenient target called <tt class="verbatim">fangle_sources</tt> so
1830 that <tt class="verbatim">make -f fangle_sources</tt> will re-extract the source
1831 if the literate document has been updated.
1832 </p>
1834 <font size="-1"><p>
1835 <a id="code-label-Makefile.inc-targets-5"></a>
1836 <table style="width: 100%" id="code-ref-Makefile.inc-targets-5">
1837 <tbody><tr>
1838 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
1839 <font color="blue">Makefile.inc-targets</font>[5]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
1840 lang=<font color="blue"></font> + &equiv;
1841 </div><div class="right-tab">
1842 &#x25B5;<a href="#code-ref-Makefile.inc-targets-4">26b</a> <a href="#code-ref-Makefile.inc-targets-6">26d</a>&#x25BF;
1843 </div></font></td>
1844 </tr></tbody>
1845 </table>
1846 </p><p>
1848 <tt class="verbatim"><div class="compact-block">
1849 <tt>19 </tt>.PHONY: fangle_sources
1850 </div></tt>
1851 </p>
1852 <pre class="verbatim" xml:space="preserve">
1853 <div class="compact-block"></div></pre>
1855 <tt class="verbatim"><div class="compact-block">
1856 <tt>20 </tt>fangle_sources: $(FANGLE_SOURCE_STAMP)
1857 </div></tt>
1858 </p>
1859 </p><p>
1860 <a id="code-end-Makefile.inc-targets-5"></a>
1861 </p></font>
1862 </p>
1864 And also a convenient target to remove extracted sources.
1865 </p>
1867 <font size="-1"><p>
1868 <a id="code-label-Makefile.inc-targets-6"></a>
1869 <table style="width: 100%" id="code-ref-Makefile.inc-targets-6">
1870 <tbody><tr>
1871 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
1872 <font color="blue">Makefile.inc-targets</font>[6]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
1873 lang=<font color="blue"></font> + &equiv;
1874 </div><div class="right-tab">
1875 &#x25B5;<a href="#code-ref-Makefile.inc-targets-5">26c</a> <a href="#code-ref-Makefile.inc-targets-7">27d</a>&#x22B3;
1876 </div></font></td>
1877 </tr></tbody>
1878 </table>
1879 </p><p>
1881 <tt class="verbatim"><div class="compact-block">
1882 <tt>21 </tt>.PHONY: clean_fangle_sources
1883 </div></tt>
1884 </p>
1885 <pre class="verbatim" xml:space="preserve">
1886 <div class="compact-block"><tt>22 </tt>clean_fangle_sources: ; \</div></pre>
1888 <tt class="verbatim"><div class="compact-block">
1889 <tt>23 </tt> rm -f &ndash; $(FANGLE_SOURCE_STAMP)
1890 $(FANGLE_SOURCES)
1891 </div></tt>
1892 </p>
1893 </p><p>
1894 <a id="code-end-Makefile.inc-targets-6"></a>
1895 </p></font>
1896 </p>
1898 We now look at the extraction of the source files.
1899 </p>
1901 This makefile macro <tt class="verbatim">if_extension</tt> takes 4 arguments: the
1902 filename <tt class="verbatim">$(1)</tt>, some extensions to match <tt class="verbatim">$(2)</tt>
1903 and a shell command to return if the filename does match the exensions
1904 <tt class="verbatim">$(3)</tt>, and a shell command to return if it does not match
1905 the extensions <tt class="verbatim">$(4)</tt>.
1906 </p>
1908 <font size="-1"><p>
1909 <a id="code-label-Makefile.inc-vars-7"></a>
1910 <table style="width: 100%" id="code-ref-Makefile.inc-vars-7">
1911 <tbody><tr>
1912 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1913 <font color="blue">Makefile.inc-vars</font>[7]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
1914 lang=<font color="blue"></font> + &equiv;
1915 </div><div class="right-tab">
1916 &#x25B5;<a href="#code-ref-Makefile.inc-vars-6">26a</a> <a href="#code-ref-Makefile.inc-vars-8">26f</a>&#x25BF;
1917 </div></font></td>
1918 </tr></tbody>
1919 </table>
1920 </p><p>
1921 <tt class="verbatim"><div class="compact-block">
1922 <tt>13 </tt>if_extension=$(if $(findstring $(suffix
1923 $(1)),$(2)),$(3),$(4))
1924 </div></tt>
1925 </p><p>
1926 <a id="code-end-Makefile.inc-vars-7"></a>
1927 </p></font>
1928 </p>
1930 For some source files like C files, we want to output the line number
1931 and filename of the original LaTeX document from which the source came
1933 <font size="-1"><div align="justify">
1934 <div style="margin-left: 0px">
1935 <div style="margin-right: 0px">
1937 . I plan to replace this option with a separate mapping file
1938 so as not to pollute the generated source, and also to allow a
1939 code pretty-printing reformatter like <tt class="verbatim">indent</tt>
1940 be able to re-format the file and adjust for changes through
1941 comparing the character streams.
1942 </p>
1943 </div>
1944 </div>
1945 </div></font>
1946 </p>
1947 <span style="margin-left: 0em"></span>
1948 <a id="footnr-5"></a>
1949 <sup><a href="#footnote-5">5</a></sup>
1951 </p>
1953 To make this easier we define the file extensions for which we want to
1954 do this.
1955 </p>
1957 <font size="-1"><p>
1958 <a id="code-label-Makefile.inc-vars-8"></a>
1959 <table style="width: 100%" id="code-ref-Makefile.inc-vars-8">
1960 <tbody><tr>
1961 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1962 <font color="blue">Makefile.inc-vars</font>[8]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
1963 lang=<font color="blue"></font> + &equiv;
1964 </div><div class="right-tab">
1965 &#x25B5;<a href="#code-ref-Makefile.inc-vars-7">26e</a> <a href="#code-ref-Makefile.inc-vars-9">26g</a>&#x25BF;
1966 </div></font></td>
1967 </tr></tbody>
1968 </table>
1969 </p><p>
1970 <tt class="verbatim"><div class="compact-block">
1971 <tt>14 </tt>C_EXTENSIONS=.c .h
1972 </div></tt>
1973 </p><p>
1974 <a id="code-end-Makefile.inc-vars-8"></a>
1975 </p></font>
1976 </p>
1978 We can then use the <tt class="verbatim">if_extensions</tt> macro to define a
1979 macro which expands out to the <tt class="verbatim">-L</tt> option if fangle is
1980 being invoked in a C source file, so that C compile errors will refer to
1981 the line number in the TeX document.
1982 </p>
1984 <font size="-1"><p>
1985 <a id="code-label-Makefile.inc-vars-9"></a>
1986 <table style="width: 100%" id="code-ref-Makefile.inc-vars-9">
1987 <tbody><tr>
1988 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
1989 <font color="blue">Makefile.inc-vars</font>[9]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
1990 lang=<font color="blue"></font> + &equiv;
1991 </div><div class="right-tab">
1992 &#x25B5;<a href="#code-ref-Makefile.inc-vars-8">26f</a> <a href="#code-ref-Makefile.inc-vars-10">27a</a>&#x22B3;
1993 </div></font></td>
1994 </tr></tbody>
1995 </table>
1996 </p><p>
1998 <tt class="verbatim"><div class="compact-block">
1999 <tt>15 </tt>TABS=8
2000 </div></tt>
2001 </p>
2002 <pre class="verbatim" xml:space="preserve">
2003 <div class="compact-block"><tt>16 </tt>nf_line=-L -T$(TABS)</div></pre>
2005 <tt class="verbatim"><div class="compact-block">
2006 <tt>17 </tt>fangle=fangle $(call
2007 if_extension,$(2),$(C_EXTENSIONS),$(nf_line)) -R&quot;$(2)&quot;
2008 $(1)
2009 </div></tt>
2010 </p>
2011 </p><p>
2012 <a id="code-end-Makefile.inc-vars-9"></a>
2013 </p></font>
2014 </p>
2016 We can use a similar trick to define an indent macro which takes just
2017 the filename as an argument and can return a pipeline stage calling the
2018 indent command. Indent can be turned off with <tt class="verbatim">make
2019 fangle_sources indent=</tt>
2020 </p>
2022 <font size="-1"><p>
2023 <a id="code-label-Makefile.inc-vars-10"></a>
2024 <table style="width: 100%" id="code-ref-Makefile.inc-vars-10">
2025 <tbody><tr>
2026 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
2027 <font color="blue">Makefile.inc-vars</font>[10]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
2028 lang=<font color="blue"></font> + &equiv;
2029 </div><div class="right-tab">
2030 &#x22B2;<a href="#code-ref-Makefile.inc-vars-9">26g</a> <a href="#code-ref-Makefile.inc-vars-11">27b</a>&#x25BF;
2031 </div></font></td>
2032 </tr></tbody>
2033 </table>
2034 </p><p>
2036 <tt class="verbatim"><div class="compact-block">
2037 <tt>18 </tt>indent_options=-npro -kr -i8 -ts8 -sob -l80 -ss -ncs
2038 </div></tt>
2039 </p>
2040 <pre class="verbatim" xml:space="preserve">
2041 <div class="compact-block"></div></pre>
2043 <tt class="verbatim"><div class="compact-block">
2044 <tt>19 </tt>indent=$(call if_extension,$(1),$(C_EXTENSIONS), |
2045 indent $(indent_options))
2046 </div></tt>
2047 </p>
2048 </p><p>
2049 <a id="code-end-Makefile.inc-vars-10"></a>
2050 </p></font>
2051 </p>
2053 We now define the pattern for extracting a file. The files are written
2054 using noweb's <tt class="verbatim">cpif</tt> so that the file timestamp will not
2055 be touched if the contents haven't changed. This avoids the need to
2056 rebuild the entire project because of a typographical change in the
2057 documentation, or if none or a few C source files have changed.
2058 </p>
2060 <font size="-1"><p>
2061 <a id="code-label-Makefile.inc-vars-11"></a>
2062 <table style="width: 100%" id="code-ref-Makefile.inc-vars-11">
2063 <tbody><tr>
2064 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
2065 <font color="blue">Makefile.inc-vars</font>[11]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
2066 lang=<font color="blue"></font> + &equiv;
2067 </div><div class="right-tab">
2068 &#x25B5;<a href="#code-ref-Makefile.inc-vars-10">27a</a> <a href="#code-ref-Makefile.inc-vars-12">27c</a>&#x25BF;
2069 </div></font></td>
2070 </tr></tbody>
2071 </table>
2072 </p><p>
2074 <tt class="verbatim"><div class="compact-block">
2075 <tt>20 </tt>fangle_extract=@mkdir -p $(dir $(1)) &amp;&amp; \
2076 </div></tt>
2077 </p>
2078 <pre class="verbatim" xml:space="preserve">
2079 <div class="compact-block"><tt>21 </tt> $(call fangle,$(2),$(1)) &gt; &quot;$(1).tmp&quot; &amp;&amp; \
2080 <tt>22 </tt> cat &quot;$(1).tmp&quot; $(indent) | cpif &quot;$(1)&quot; \
2081 <tt>23 </tt> &amp;&amp; rm -- &quot;$(1).tmp&quot; || \</div></pre>
2083 <tt class="verbatim"><div class="compact-block">
2084 <tt>24 </tt> (echo error newfangling $(1) from $(2) ; exit 1)
2085 </div></tt>
2086 </p>
2087 </p><p>
2088 <a id="code-end-Makefile.inc-vars-11"></a>
2089 </p></font>
2090 </p>
2092 We define a target which will extract or update all sources. To do this
2093 we first defined a makefile template that can do this for any source
2094 file in the LaTeX document.
2095 </p>
2097 <font size="-1"><p>
2098 <a id="code-label-Makefile.inc-vars-12"></a>
2099 <table style="width: 100%" id="code-ref-Makefile.inc-vars-12">
2100 <tbody><tr>
2101 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
2102 <font color="blue">Makefile.inc-vars</font>[12]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
2103 lang=<font color="blue"></font> + &equiv;
2104 </div><div class="right-tab">
2105 &#x25B5;<a href="#code-ref-Makefile.inc-vars-11">27b</a> <a href="#code-ref-Makefile.inc-vars-13">28a</a>&#x22B3;
2106 </div></font></td>
2107 </tr></tbody>
2108 </table>
2109 </p><p>
2111 <tt class="verbatim"><div class="compact-block">
2112 <tt>25 </tt>define FANGLE_template
2113 </div></tt>
2114 </p>
2115 <pre class="verbatim" xml:space="preserve">
2116 <div class="compact-block"><tt>26 </tt> $(1): $(2)
2117 <tt>27 </tt>&#x21A6;$$(call fangle_extract,$(1),$(2))
2118 <tt>28 </tt> FANGLE_TARGETS+=$(1)</div></pre>
2120 <tt class="verbatim"><div class="compact-block">
2121 <tt>29 </tt>endef
2122 </div></tt>
2123 </p>
2124 </p><p>
2125 <a id="code-end-Makefile.inc-vars-12"></a>
2126 </p></font>
2127 </p>
2129 We then enumerate the discovered <tt class="verbatim">FANGLE_SOURCES</tt> to
2130 generate a makefile rule for each one using the makefile template we
2131 defined above.
2132 </p>
2134 <font size="-1"><p>
2135 <a id="code-label-Makefile.inc-targets-7"></a>
2136 <table style="width: 100%" id="code-ref-Makefile.inc-targets-7">
2137 <tbody><tr>
2138 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
2139 <font color="blue">Makefile.inc-targets</font>[7]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
2140 lang=<font color="blue"></font> + &equiv;
2141 </div><div class="right-tab">
2142 &#x22B2;<a href="#code-ref-Makefile.inc-targets-6">26d</a> <a href="#code-ref-Makefile.inc-targets-8">27e</a>&#x25BF;
2143 </div></font></td>
2144 </tr></tbody>
2145 </table>
2146 </p><p>
2148 <tt class="verbatim"><div class="compact-block">
2149 <tt>24 </tt>$(foreach source,$(FANGLE_SOURCES),\
2150 </div></tt>
2151 </p>
2152 <pre class="verbatim" xml:space="preserve">
2153 <div class="compact-block"><tt>25 </tt> $(eval $(call FANGLE_template,$(source),$(FANGLE_SOURCE))) \</div></pre>
2155 <tt class="verbatim"><div class="compact-block">
2156 <tt>26 </tt>)
2157 </div></tt>
2158 </p>
2159 </p><p>
2160 <a id="code-end-Makefile.inc-targets-7"></a>
2161 </p></font>
2162 </p>
2164 These will all be built with <tt class="verbatim">FANGLE_SOURCE_STAMP</tt>.
2165 </p>
2167 We also remove the generated sources on a make distclean.
2168 </p>
2170 <font size="-1"><p>
2171 <a id="code-label-Makefile.inc-targets-8"></a>
2172 <table style="width: 100%" id="code-ref-Makefile.inc-targets-8">
2173 <tbody><tr>
2174 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
2175 <font color="blue">Makefile.inc-targets</font>[8]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
2176 lang=<font color="blue"></font> + &equiv;
2177 </div><div class="right-tab">
2178 &#x25B5;<a href="#code-ref-Makefile.inc-targets-7">27d</a> <a href="#code-ref-Makefile.inc-targets-9">28b</a>&#x22B3;
2179 </div></font></td>
2180 </tr></tbody>
2181 </table>
2182 </p><p>
2183 <tt class="verbatim"><div class="compact-block">
2184 <tt>27 </tt>_distclean: clean_fangle_sources
2185 </div></tt>
2186 </p><p>
2187 <a id="code-end-Makefile.inc-targets-8"></a>
2188 </p></font>
2189 </p>
2190 <h2 id="auto-29">6.5<span style="margin-left: 1em"></span>Extracting Documentation</h2>
2192 We then identify the intermediate stages of the documentation and their
2193 build and clean targets.
2194 </p>
2195 <h3 id="auto-30">6.5.1<span style="margin-left: 1em"></span>Formatting TeX</h3>
2196 <h4 id="auto-31">6.5.1.1<span style="margin-left: 1em"></span>Running pdflatex</h4>
2198 We produce a pdf file from the tex file.
2199 </p>
2201 <font size="-1"><p>
2202 <a id="code-label-Makefile.inc-vars-13"></a>
2203 <table style="width: 100%" id="code-ref-Makefile.inc-vars-13">
2204 <tbody><tr>
2205 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
2206 <font color="blue">Makefile.inc-vars</font>[13]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
2207 lang=<font color="blue"></font> + &equiv;
2208 </div><div class="right-tab">
2209 &#x22B2;<a href="#code-ref-Makefile.inc-vars-12">27c</a> <a href="#code-ref-Makefile.inc-vars-14">28c</a>&#x25BF;
2210 </div></font></td>
2211 </tr></tbody>
2212 </table>
2213 </p><p>
2214 <tt class="verbatim"><div class="compact-block">
2215 <tt>30 </tt>FANGLE_PDF=$(TEX_SOURCE:.tex=.pdf)
2216 </div></tt>
2217 </p><p>
2218 <a id="code-end-Makefile.inc-vars-13"></a>
2219 </p></font>
2220 </p>
2222 We run pdflatex twice to be sure that the contents and aux files are up
2223 to date. We certainly are <em>required</em> to run pdflatex at least
2224 twice if these files do not exist.
2225 </p>
2227 <font size="-1"><p>
2228 <a id="code-label-Makefile.inc-targets-9"></a>
2229 <table style="width: 100%" id="code-ref-Makefile.inc-targets-9">
2230 <tbody><tr>
2231 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
2232 <font color="blue">Makefile.inc-targets</font>[9]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
2233 lang=<font color="blue"></font> + &equiv;
2234 </div><div class="right-tab">
2235 &#x22B2;<a href="#code-ref-Makefile.inc-targets-8">27e</a> <a href="#code-ref-Makefile.inc-targets-10">28d</a>&#x25BF;
2236 </div></font></td>
2237 </tr></tbody>
2238 </table>
2239 </p><p>
2241 <tt class="verbatim"><div class="compact-block">
2242 <tt>28 </tt>$(FANGLE_PDF): $(TEX_SOURCE)
2243 </div></tt>
2244 </p>
2245 <pre class="verbatim" xml:space="preserve">
2246 <div class="compact-block"><tt>29 </tt>&#x21A6;pdflatex $&lt; &amp;&amp; pdflatex $&lt;
2247 <tt>30 </tt>
2248 <tt>31 </tt>clean_pdf:
2249 <tt>32 </tt>&#x21A6;rm -f -- $(FANGLE_PDF) $(TEX_SOURCE:.tex=.toc) \</div></pre>
2251 <tt class="verbatim"><div class="compact-block">
2252 <tt>33 </tt>&#x21A6; $(TEX_SOURCE:.tex=.log)
2253 $(TEX_SOURCE:.tex=.aux)
2254 </div></tt>
2255 </p>
2256 </p><p>
2257 <a id="code-end-Makefile.inc-targets-9"></a>
2258 </p></font>
2259 </p>
2260 <h3 id="auto-32">6.5.2<span style="margin-left: 1em"></span>Formatting TeXmacs</h3>
2262 TeXmacs can produce a PDF file directly.
2263 </p>
2265 <font size="-1"><p>
2266 <a id="code-label-Makefile.inc-vars-14"></a>
2267 <table style="width: 100%" id="code-ref-Makefile.inc-vars-14">
2268 <tbody><tr>
2269 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
2270 <font color="blue">Makefile.inc-vars</font>[14]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
2271 lang=<font color="blue"></font> + &equiv;
2272 </div><div class="right-tab">
2273 &#x25B5;<a href="#code-ref-Makefile.inc-vars-13">28a</a> <a href="#code-ref-Makefile.inc-vars-15">28e</a>&#x25BF;
2274 </div></font></td>
2275 </tr></tbody>
2276 </table>
2277 </p><p>
2278 <tt class="verbatim"><div class="compact-block">
2279 <tt>31 </tt>FANGLE_PDF=$(TEX_SOURCE:.tm=.pdf)
2280 </div></tt>
2281 </p><p>
2282 <a id="code-end-Makefile.inc-vars-14"></a>
2283 </p></font>
2284 </p>
2285 <table style="display: inline; vertical-align: -0.55em">
2286 <tbody><tr>
2287 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: <p>
2288 Outputting the PDF may not be enough to update the links and page
2289 references. I think
2290 </p><p>
2291 we need to update twice, generate a pdf, update twice mode and
2292 generate a new PDF.
2293 </p><p>
2294 Basically the PDF export of TeXmacs is pretty rotten and doesn't
2295 work properly from the CLI
2296 </p></td>
2297 </tr></tbody>
2298 </table>
2300 <font size="-1"><p>
2301 <a id="code-label-Makefile.inc-targets-10"></a>
2302 <table style="width: 100%" id="code-ref-Makefile.inc-targets-10">
2303 <tbody><tr>
2304 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
2305 <font color="blue">Makefile.inc-targets</font>[10]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
2306 lang=<font color="blue"></font> + &equiv;
2307 </div><div class="right-tab">
2308 &#x25B5;<a href="#code-ref-Makefile.inc-targets-9">28b</a> <a href="#code-ref-Makefile.inc-targets-11">28f</a>&#x25BF;
2309 </div></font></td>
2310 </tr></tbody>
2311 </table>
2312 </p><p>
2314 <tt class="verbatim"><div class="compact-block">
2315 <tt>34 </tt>$(FANGLE_PDF): $(TEXMACS_SOURCE)
2316 </div></tt>
2317 </p>
2318 <pre class="verbatim" xml:space="preserve">
2319 <div class="compact-block"><tt>35 </tt>&#x21A6;texmacs -c $(TEXMACS_SOURCE) $&lt; -q
2320 <tt>36 </tt>
2321 <tt>37 </tt>clean_pdf:</div></pre>
2323 <tt class="verbatim"><div class="compact-block">
2324 <tt>38 </tt>&#x21A6;rm -f &ndash; $(FANGLE_PDF)
2325 </div></tt>
2326 </p>
2327 </p><p>
2328 <a id="code-end-Makefile.inc-targets-10"></a>
2329 </p></font>
2330 </p>
2331 <h3 id="auto-33">6.5.3<span style="margin-left: 1em"></span>Building the Documentation as a
2332 Whole</h3>
2334 Currently we only build pdf as a final format, but <tt class="verbatim">FANGLE_DOCS</tt>
2335 may later hold other output formats.
2336 </p>
2338 <font size="-1"><p>
2339 <a id="code-label-Makefile.inc-vars-15"></a>
2340 <table style="width: 100%" id="code-ref-Makefile.inc-vars-15">
2341 <tbody><tr>
2342 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-vars"></a><font size="-1"><div class="left-tab">
2343 <font color="blue">Makefile.inc-vars</font>[15]() &uArr;<a href="#code-ref-Makefile.inc-vars-1">23b</a>,
2344 lang=<font color="blue"></font> + &equiv;
2345 </div><div class="right-tab">
2346 &#x25B5;<a href="#code-ref-Makefile.inc-vars-14">28c</a>
2347 </div></font></td>
2348 </tr></tbody>
2349 </table>
2350 </p><p>
2351 <tt class="verbatim"><div class="compact-block">
2352 <tt>32 </tt>FANGLE_DOCS=$(FANGLE_PDF)
2353 </div></tt>
2354 </p><table style="width: 100%" id="code-end-Makefile.inc-vars-15">
2355 <tbody><tr>
2356 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
2357 </tr></tbody>
2358 </table></font>
2359 </p>
2361 We also define <tt class="verbatim">fangle_docs</tt> as a convenient phony target.
2362 </p>
2364 <font size="-1"><p>
2365 <a id="code-label-Makefile.inc-targets-11"></a>
2366 <table style="width: 100%" id="code-ref-Makefile.inc-targets-11">
2367 <tbody><tr>
2368 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
2369 <font color="blue">Makefile.inc-targets</font>[11]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
2370 lang=<font color="blue"></font> + &equiv;
2371 </div><div class="right-tab">
2372 &#x25B5;<a href="#code-ref-Makefile.inc-targets-10">28d</a> <a href="#code-ref-Makefile.inc-targets-12">28g</a>&#x25BF;
2373 </div></font></td>
2374 </tr></tbody>
2375 </table>
2376 </p><p>
2378 <tt class="verbatim"><div class="compact-block">
2379 <tt>39 </tt>.PHONY: fangle_docs
2380 </div></tt>
2381 </p>
2382 <pre class="verbatim" xml:space="preserve">
2383 <div class="compact-block"><tt>40 </tt>fangle_docs: $(FANGLE_DOCS)</div></pre>
2385 <tt class="verbatim"><div class="compact-block">
2386 <tt>41 </tt>docs: fangle_docs
2387 </div></tt>
2388 </p>
2389 </p><p>
2390 <a id="code-end-Makefile.inc-targets-11"></a>
2391 </p></font>
2392 </p>
2394 And define a convenient <tt class="verbatim">clean_fangle_docs</tt> which we add
2395 to the regular clean target
2396 </p>
2398 <font size="-1"><p>
2399 <a id="code-label-Makefile.inc-targets-12"></a>
2400 <table style="width: 100%" id="code-ref-Makefile.inc-targets-12">
2401 <tbody><tr>
2402 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Makefile.inc-targets"></a><font size="-1"><div class="left-tab">
2403 <font color="blue">Makefile.inc-targets</font>[12]() &uArr;<a href="#code-ref-Makefile.inc-targets-1">24b</a>,
2404 lang=<font color="blue"></font> + &equiv;
2405 </div><div class="right-tab">
2406 &#x25B5;<a href="#code-ref-Makefile.inc-targets-11">28f</a>
2407 </div></font></td>
2408 </tr></tbody>
2409 </table>
2410 </p><p>
2412 <tt class="verbatim"><div class="compact-block">
2413 <tt>42 </tt>.PHONEY: clean_fangle_docs
2414 </div></tt>
2415 </p>
2416 <pre class="verbatim" xml:space="preserve">
2417 <div class="compact-block"><tt>43 </tt>clean_fangle_docs: clean_tex clean_pdf
2418 <tt>44 </tt>clean: clean_fangle_docs
2419 <tt>45 </tt>
2420 <tt>46 </tt>distclean_fangle_docs: clean_tex clean_fangle_docs</div></pre>
2422 <tt class="verbatim"><div class="compact-block">
2423 <tt>47 </tt>distclean: clean distclean_fangle_docs
2424 </div></tt>
2425 </p>
2426 </p><table style="width: 100%" id="code-end-Makefile.inc-targets-12">
2427 <tbody><tr>
2428 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
2429 </tr></tbody>
2430 </table></font>
2431 </p>
2432 <h2 id="auto-34">6.6<span style="margin-left: 1em"></span>Other helpers</h2>
2434 If <tt class="verbatim">Makefile.inc</tt> is included into <tt class="verbatim">Makefile</tt>,
2435 then extracted files can be updated with this command:
2436 </p>
2437 <pre class="verbatim" xml:space="preserve">
2438 make fangle_sources</pre>
2440 otherwise, with:
2441 </p>
2442 <pre class="verbatim" xml:space="preserve">
2443 make -f Makefile.inc fangle_sources</pre>
2444 <h2 id="auto-35">6.7<span style="margin-left: 1em"></span>Boot-strapping the extraction</h2>
2446 As well as having the makefile extract or update the source files as
2447 part of it's operation, it also seems convenient to have the makefile
2448 re-extracted itself from <em>this</em> document.
2449 </p>
2451 It would also be convenient to have the code that extracts the makefile
2452 from this document to also be part of this document, however we have to
2453 start somewhere and this unfortunately requires us to type at least a
2454 few words by hand to start things off.
2455 </p>
2457 Therefore we will have a minimal root fragment, which, when extracted,
2458 can cope with extracting the rest of the source. This shell script
2459 fragment can do that. It's name is <tt class="verbatim">*</tt> <class style="font-family: Times New Roman">&mdash;</class>
2460 out of regard for <class style="font-variant: small-caps">Noweb</class>, but when extracted might
2461 better be called <tt class="verbatim">autoupdate</tt>.
2462 </p>
2463 <table style="display: inline; vertical-align: -0.55em">
2464 <tbody><tr>
2465 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: De-lyxify</td>
2466 </tr></tbody>
2467 </table>
2469 <font size="-1"><p>
2470 <a id="code-label-*-1"></a>
2471 <table style="width: 100%" id="code-ref-*-1">
2472 <tbody><tr>
2473 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="*"></a><font size="-1"><font color="blue">*</font>[1](),
2474 lang=<font color="blue">sh</font> &equiv;</font></td>
2475 </tr></tbody>
2476 </table>
2477 </p><p>
2479 <tt class="verbatim"><div class="compact-block">
2480 <tt>1 </tt>#! /bin/sh
2481 </div></tt>
2482 </p>
2483 <pre class="verbatim" xml:space="preserve">
2484 <div class="compact-block"><tt>2 </tt>
2485 <tt>3 </tt>MAKE_SRC=&quot;${1:-${NW_LYX:-../../noweb-lyx/noweb-lyx3.lyx}}&quot;
2486 <tt>4 </tt>MAKE_SRC=&lsquo;dirname &quot;$MAKE_SRC&quot;&lsquo;/&lsquo;basename &quot;$MAKE_SRC&quot; .lyx&lsquo;
2487 <tt>5 </tt>NOWEB_SRC=&quot;${2:-${NOWEB_SRC:-$MAKE_SRC.lyx}}&quot;
2488 <tt>6 </tt>lyx -e latex $MAKE_SRC
2489 <tt>7 </tt>
2490 <tt>8 </tt>fangle -R./Makefile.inc ${MAKE_SRC}.tex \
2491 <tt>9 </tt> | sed &quot;/FANGLE_SOURCE=/s/^/#/;T;aNOWEB_SOURCE=$FANGLE_SRC&quot; \
2492 <tt>10 </tt> | cpif ./Makefile.inc
2493 <tt>11 </tt></div></pre>
2495 <tt class="verbatim"><div class="compact-block">
2496 <tt>12 </tt>make -f ./Makefile.inc fangle_sources
2497 </div></tt>
2498 </p>
2499 </p><table style="width: 100%" id="code-end-*-1">
2500 <tbody><tr>
2501 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
2502 </tr></tbody>
2503 </table></font>
2504 </p>
2506 The general Makefile can be invoked with <tt class="verbatim">./autoboot</tt> and
2507 can also be included into any automake file to automatically re-generate
2508 the source files.
2509 </p>
2511 The <em>autoboot</em> can be extracted with this command:
2512 </p>
2513 <pre class="verbatim" xml:space="preserve">
2514 lyx -e latex fangle.lyx &amp;&amp; \
2515 fangle fangle.lyx &gt; ./autoboot</pre>
2517 This looks simple enough, but as mentioned, fangle has to be had from
2518 somewhere before it can be extracted.
2519 </p>
2521 On a unix system this will extract <tt class="verbatim">fangle.module</tt> and the
2522 <tt class="verbatim">fangle</tt> awk script, and run some basic tests.
2523 </p>
2524 <table style="display: inline; vertical-align: -0.55em">
2525 <tbody><tr>
2526 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: cross-ref to test chapter when it is a
2527 chapter all on its own</td>
2528 </tr></tbody>
2529 </table>
2530 <h2 id="auto-36">6.8<span style="margin-left: 1em"></span>Incorporating Makefile.inc into existing
2531 projects</h2>
2533 If you are writing a literate module of an existing non-literate program
2534 you may find it easier to use a slight recursive make instead of
2535 directly including <tt class="verbatim">Makefile.inc</tt> in the projects
2536 makefile.
2537 </p>
2539 This way there is less chance of definitions in <tt class="verbatim">Makefile.inc</tt>
2540 interfering with definitions in the main makefile, or with definitions
2541 in other <tt class="verbatim">Makefile.inc</tt> from other literate modules of the
2542 same project.
2543 </p>
2545 To do this we add some <em>glue</em> to the project makefile that
2546 invokes Makefile.inc in the right way. The glue works by adding a <tt
2547 class="verbatim">.PHONY</tt> target to call the recursive make, and adding this
2548 target as an additional pre-requisite to the existing targets.
2549 </p>
2551 <a id="auto-37"></a><h5>Example</h5>Sub-module of existing system
2552 </p>
2554 In this example, we are building <tt class="verbatim">module.so</tt> as a literate
2555 module of a larger project.
2556 </p>
2558 We will show the sort glue that can be inserted into the projects
2559 Makefile <class style="font-family: Times New Roman">&mdash;</class> or more likely <class style="font-family: Times New Roman">&mdash;</class>
2560 a regular Makefile included in or invoked by the projects Makefile.
2561 </p>
2563 <font size="-1"><p>
2564 <a id="code-label-makefile-glue-1"></a>
2565 <table style="width: 100%" id="code-ref-makefile-glue-1">
2566 <tbody><tr>
2567 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="makefile-glue"></a><font size="-1"><div class="left-tab">
2568 <font color="blue">makefile-glue</font>[1](), lang=<font color="blue"></font>
2569 &equiv;
2570 </div><div class="right-tab">
2571 <a href="#code-ref-makefile-glue-2">30b</a>&#x25BF;
2572 </div></font></td>
2573 </tr></tbody>
2574 </table>
2575 </p><p>
2577 <tt class="verbatim"><div class="compact-block">
2578 <tt>1 </tt>module_srcdir=modules/module
2579 </div></tt>
2580 </p>
2581 <pre class="verbatim" xml:space="preserve">
2582 <div class="compact-block"><tt>2 </tt>MODULE_SOURCE=module.tm</div></pre>
2584 <tt class="verbatim"><div class="compact-block">
2585 <tt>3 </tt>MODULE_STAMP=$(MODULE_SOURCE).stamp
2586 </div></tt>
2587 </p>
2588 </p><p>
2589 <a id="code-end-makefile-glue-1"></a>
2590 </p></font>
2591 </p>
2593 The existing build system may already have a build target for
2594 <tt class="verbatim">module.o</tt>
2595 , but we just add another pre-requisite to that. In this case we use
2596 <tt class="verbatim">module.tm.stamp</tt>
2597 as a pre-requisite, the stamp file's modified time indicating when all
2598 sources were extracted
2600 <font size="-1"><div align="justify">
2601 <div style="margin-left: 0px">
2602 <div style="margin-right: 0px">
2604 . If the projects build system does not know how to build the
2605 module from the extracted sources, then just add build actions
2606 here as normal.
2607 </p>
2608 </div>
2609 </div>
2610 </div></font>
2611 </p>
2612 <span style="margin-left: 0em"></span>
2613 <a id="footnr-6"></a>
2614 <sup><a href="#footnote-6">6</a></sup>
2616 </p>
2618 <font size="-1"><p>
2619 <a id="code-label-makefile-glue-2"></a>
2620 <table style="width: 100%" id="code-ref-makefile-glue-2">
2621 <tbody><tr>
2622 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="makefile-glue"></a><font size="-1"><div class="left-tab">
2623 <font color="blue">makefile-glue</font>[2]() &uArr;<a href="#code-ref-makefile-glue-1">30a</a>,
2624 lang=<font color="blue">make</font> + &equiv;
2625 </div><div class="right-tab">
2626 &#x25B5;<a href="#code-ref-makefile-glue-1">30a</a> <a href="#code-ref-makefile-glue-3">30c</a>&#x25BF;
2627 </div></font></td>
2628 </tr></tbody>
2629 </table>
2630 </p><p>
2631 <tt class="verbatim"><div class="compact-block">
2632 <tt>4 </tt>$(module_srcdir)/module.o:
2633 $(module_srcdir)/$(MODULE_STAMP)
2634 </div></tt>
2635 </p><p>
2636 <a id="code-end-makefile-glue-2"></a>
2637 </p></font>
2638 </p>
2640 The target for this new pre-requisite will be generated by a recursive
2641 make using <tt class="verbatim">Makefile.inc</tt> which will make sure that the
2642 source is up to date, before it is built by the main projects makefile.
2643 </p>
2645 <font size="-1"><p>
2646 <a id="code-label-makefile-glue-3"></a>
2647 <table style="width: 100%" id="code-ref-makefile-glue-3">
2648 <tbody><tr>
2649 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="makefile-glue"></a><font size="-1"><div class="left-tab">
2650 <font color="blue">makefile-glue</font>[3]() &uArr;<a href="#code-ref-makefile-glue-1">30a</a>,
2651 lang=<font color="blue"></font> + &equiv;
2652 </div><div class="right-tab">
2653 &#x25B5;<a href="#code-ref-makefile-glue-2">30b</a> <a href="#code-ref-makefile-glue-4">30d</a>&#x25BF;
2654 </div></font></td>
2655 </tr></tbody>
2656 </table>
2657 </p><p>
2659 <tt class="verbatim"><div class="compact-block">
2660 <tt>5 </tt>$(module_srcdir)/$(MODULE_STAMP):
2661 $(module_srcdir)/$(MODULE_SOURCE)
2662 </div></tt>
2663 </p>
2664 <pre class="verbatim" xml:space="preserve">
2665 <div class="compact-block"></div></pre>
2667 <tt class="verbatim"><div class="compact-block">
2668 <tt>6 </tt>&#x21A6;$(MAKE) -C $(module_srcdir) -f Makefile.inc
2669 fangle_sources LITERATE_SOURCE=$(MODULE_SOURCE)
2670 </div></tt>
2671 </p>
2672 </p><p>
2673 <a id="code-end-makefile-glue-3"></a>
2674 </p></font>
2675 </p>
2677 We can do similar glue for the docs, clean and distclean targets. In
2678 this example the main prject was using a double colon for these targets,
2679 so we must use the same in our glue.
2680 </p>
2682 <font size="-1"><p>
2683 <a id="code-label-makefile-glue-4"></a>
2684 <table style="width: 100%" id="code-ref-makefile-glue-4">
2685 <tbody><tr>
2686 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="makefile-glue"></a><font size="-1"><div class="left-tab">
2687 <font color="blue">makefile-glue</font>[4]() &uArr;<a href="#code-ref-makefile-glue-1">30a</a>,
2688 lang=<font color="blue"></font> + &equiv;
2689 </div><div class="right-tab">
2690 &#x25B5;<a href="#code-ref-makefile-glue-3">30c</a>
2691 </div></font></td>
2692 </tr></tbody>
2693 </table>
2694 </p><p>
2696 <tt class="verbatim"><div class="compact-block">
2697 <tt>7 </tt>docs:: docs_module
2698 </div></tt>
2699 </p>
2700 <pre class="verbatim" xml:space="preserve">
2701 <div class="compact-block"><tt>8 </tt>.PHONY: docs_module
2702 <tt>9 </tt>docs_module:
2703 <tt>10 </tt>&#x21A6;$(MAKE) -C $(module_srcdir) -f Makefile.inc docs LITERATE_SOURCE=$(MODULE_SOURCE)
2704 <tt>11 </tt>
2705 <tt>12 </tt>clean:: clean_module
2706 <tt>13 </tt>.PHONEY: clean_module
2707 <tt>14 </tt>clean_module:
2708 <tt>15 </tt>&#x21A6;$(MAKE) -C $(module_srcdir) -f Makefile.inc clean LITERATE_SOURCE=$(MODULE_SOURCE)
2709 <tt>16 </tt>
2710 <tt>17 </tt>distclean:: distclean_module
2711 <tt>18 </tt>.PHONY: distclean_module
2712 <tt>19 </tt>distclean_module:</div></pre>
2714 <tt class="verbatim"><div class="compact-block">
2715 <tt>20 </tt>&#x21A6;$(MAKE) -C $(module_srcdir) -f Makefile.inc
2716 distclean LITERATE_SOURCE=$(MODULE_SOURCE)
2717 </div></tt>
2718 </p>
2719 </p><table style="width: 100%" id="code-end-makefile-glue-4">
2720 <tbody><tr>
2721 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
2722 </tr></tbody>
2723 </table></font>
2724 </p>
2726 We could do similarly for install targets to install the generated docs.
2727 </p>
2728 <p style="margin-top: 25%; margin-bottom: 10%">
2729 <a id="auto-38"></a><br></br><b><font size="+5"><div class="center-tab">
2730 Part II
2731 </div><div class="right-tab">
2732 <br></br>Source Code
2733 </div></font></b>
2734 </p>
2735 <h1 id="auto-39">Chapter 7<br></br>Fangle awk source code</h1>
2737 We use the copyright notice from chapter <a href="#License">2</a>.
2738 </p>
2740 <font size="-1"><p>
2741 <a id="code-label-./fangle-1"></a>
2742 <table style="width: 100%" id="code-ref-./fangle-1">
2743 <tbody><tr>
2744 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle"></a><font size="-1"><div class="left-tab">
2745 <font color="blue">./fangle</font>[1](), lang=<font color="blue">awk</font>
2746 &equiv;
2747 </div><div class="right-tab">
2748 <a href="#code-ref-./fangle-2">33b</a>&#x25BF;
2749 </div></font></td>
2750 </tr></tbody>
2751 </table>
2752 </p><p>
2754 <tt class="verbatim"><div class="compact-block">
2755 <tt>1 </tt>#! /usr/bin/awk -f
2756 </div></tt>
2757 </p>
2758 <pre class="verbatim" xml:space="preserve">
2759 <div class="compact-block"></div></pre>
2761 <tt class="verbatim"><div class="compact-block">
2762 <tt>2 </tt># gpl3-copyright <a href="#code-ref-gpl3-copyright-1">4a</a>
2763 </div></tt>
2764 </p>
2765 </p><p>
2766 <a id="code-end-./fangle-1"></a>
2767 </p></font>
2768 </p>
2770 We also use code from <class style="font-variant: small-caps">Arnold Robbins</class> public domain
2771 getopt (1993 revision) defined in <a href="#getopt">73a</a>, and naturally want
2772 to attribute this appropriately.
2773 </p>
2775 <font size="-1"><p>
2776 <a id="code-label-./fangle-2"></a>
2777 <table style="width: 100%" id="code-ref-./fangle-2">
2778 <tbody><tr>
2779 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle"></a><font size="-1"><div class="left-tab">
2780 <font color="blue">./fangle</font>[2]() &uArr;<a href="#code-ref-./fangle-1">33a</a>,
2781 lang=<font color="blue"></font> + &equiv;
2782 </div><div class="right-tab">
2783 &#x25B5;<a href="#code-ref-./fangle-1">33a</a> <a href="#code-ref-./fangle-3">33c</a>&#x25BF;
2784 </div></font></td>
2785 </tr></tbody>
2786 </table>
2787 </p><p>
2789 <tt class="verbatim"><div class="compact-block">
2790 <tt>3 </tt># NOTE: Arnold Robbins public domain getopt for awk is
2791 also used:
2792 </div></tt>
2793 </p>
2794 <pre class="verbatim" xml:space="preserve">
2795 <div class="compact-block"><tt>4 </tt>getopt.awk-header <a href="#code-ref-getopt.awk-header-1">71a</a>
2796 <tt>5 </tt>getopt.awk-getopt() <a href="#code-ref-getopt.awk-getopt()-1">71c</a></div></pre>
2798 <tt class="verbatim"><div class="compact-block">
2799 <tt>6 </tt>
2800 </div></tt>
2801 </p>
2802 </p><p>
2803 <a id="code-end-./fangle-2"></a>
2804 </p></font>
2805 </p>
2807 And include the following chunks (which are explained further on) to
2808 make up the program:
2809 </p>
2811 <font size="-1"><p>
2812 <a id="code-label-./fangle-3"></a>
2813 <table style="width: 100%" id="code-ref-./fangle-3">
2814 <tbody><tr>
2815 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle"></a><font size="-1"><div class="left-tab">
2816 <font color="blue">./fangle</font>[3]() &uArr;<a href="#code-ref-./fangle-1">33a</a>,
2817 lang=<font color="blue"></font> + &equiv;
2818 </div><div class="right-tab">
2819 &#x25B5;<a href="#code-ref-./fangle-2">33b</a> <a href="#code-ref-./fangle-4">36a</a>&#x22B3;
2820 </div></font></td>
2821 </tr></tbody>
2822 </table>
2823 </p><p>
2825 <tt class="verbatim"><div class="compact-block">
2826 <tt>7 </tt>helper-functions <a href="#code-ref-helper-functions-1">34d</a>
2827 </div></tt>
2828 </p>
2829 <pre class="verbatim" xml:space="preserve">
2830 <div class="compact-block"><tt>8 </tt>mode-tracker <a href="#code-ref-mode-tracker-1">52a</a>
2831 <tt>9 </tt>parse_chunk_args <a href="#code-ref-parse_chunk_args-1">38a</a>
2832 <tt>10 </tt>chunk-storage-functions <a href="#code-ref-chunk-storage-functions-1">69b</a>
2833 <tt>11 </tt>output_chunk_names() <a href="#code-ref-output_chunk_names()-1">63d</a>
2834 <tt>12 </tt>output_chunks() <a href="#code-ref-output_chunks()-1">63e</a>
2835 <tt>13 </tt>write_chunk() <a href="#code-ref-write_chunk()-1">64a</a>
2836 <tt>14 </tt>expand_chunk_args() <a
2837 href="#code-ref-expand_chunk_args()-1">38b</a>
2838 <tt>15 </tt>
2839 <tt>16 </tt>begin <a href="#code-ref-begin-1">61d</a>
2840 <tt>17 </tt>recognize-chunk <a href="#code-ref-recognize-chunk-1">55a</a></div></pre>
2842 <tt class="verbatim"><div class="compact-block">
2843 <tt>18 </tt>end <a href="#code-ref-end-1">63c</a>
2844 </div></tt>
2845 </p>
2846 </p><p>
2847 <a id="code-end-./fangle-3"></a>
2848 </p></font>
2849 </p>
2850 <h2 id="auto-40">7.1<span style="margin-left: 1em"></span>AWK tricks</h2>
2852 The portable way to erase an array in awk is to split the empty string,
2853 so we define a fangle macro that can split an array, like this:
2854 </p>
2856 <font size="-1"><p>
2857 <a id="code-label-awk-delete-array-1"></a>
2858 <table style="width: 100%" id="code-ref-awk-delete-array-1">
2859 <tbody><tr>
2860 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="awk-delete-array"></a><font size="-1"><font color="blue">awk-delete-array</font>[1](<font
2861 color="blue">ARRAY</font>), lang=<font color="blue">awk</font>
2862 &equiv;</font></td>
2863 </tr></tbody>
2864 </table>
2865 </p><p>
2866 <tt class="verbatim"><div class="compact-block">
2867 <tt>1 </tt>split(&quot;&quot;, <font color="#008000">ARRAY</font>);
2868 </div></tt>
2869 </p><table style="width: 100%" id="code-end-awk-delete-array-1">
2870 <tbody><tr>
2871 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
2872 </tr></tbody>
2873 </table></font>
2874 </p>
2876 For debugging it is sometimes convenient to be able to dump the contents
2877 of an array to <tt class="verbatim">stderr</tt>, and so this macro is also useful.
2878 </p>
2880 <font size="-1"><p>
2881 <a id="code-label-dump-array-1"></a>
2882 <table style="width: 100%" id="code-ref-dump-array-1">
2883 <tbody><tr>
2884 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="dump-array"></a><font size="-1"><font color="blue">dump-array</font>[1](<font
2885 color="blue">ARRAY</font>), lang=<font color="blue">awk</font>
2886 &equiv;</font></td>
2887 </tr></tbody>
2888 </table>
2889 </p><p>
2891 <tt class="verbatim"><div class="compact-block">
2892 <tt>1 </tt>print &quot;\nDump: <font color="#008000">ARRAY</font>\n&ndash;&ndash;&ndash;&ndash;\n&quot;
2893 &gt; &quot;/dev/stderr&quot;;
2894 </div></tt>
2895 </p>
2896 <pre class="verbatim" xml:space="preserve">
2897 <div class="compact-block"><tt>2 </tt>for (_x in <font color="#008000">ARRAY</font>) {
2898 <tt>3 </tt> print _x &quot;=&quot; <font color="#008000">ARRAY</font>[_x] &quot;\n&quot; &gt; &quot;/dev/stderr&quot;;
2899 <tt>4 </tt>}</div></pre>
2901 <tt class="verbatim"><div class="compact-block">
2902 <tt>5 </tt>print &quot;========\n&quot; &gt;
2903 &quot;/dev/stderr&quot;;
2904 </div></tt>
2905 </p>
2906 </p><table style="width: 100%" id="code-end-dump-array-1">
2907 <tbody><tr>
2908 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
2909 </tr></tbody>
2910 </table></font>
2911 </p>
2912 <h2 id="auto-41">7.2<span style="margin-left: 1em"></span>Catching errors</h2>
2914 Fatal errors are issued with the error function:
2915 </p>
2917 <font size="-1"><p>
2918 <a id="code-label-error()-1"></a>
2919 <table style="width: 100%" id="code-ref-error()-1">
2920 <tbody><tr>
2921 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="error()"></a><font size="-1"><div class="left-tab">
2922 <font color="blue">error()</font>[1](), lang=<font color="blue">awk</font>
2923 &equiv;
2924 </div><div class="right-tab">
2925 <a href="#code-ref-error()-2">34b</a>&#x25BF;
2926 </div></font></td>
2927 </tr></tbody>
2928 </table>
2929 </p><p>
2931 <tt class="verbatim"><div class="compact-block">
2932 <tt>1 </tt>function error(message)
2933 </div></tt>
2934 </p>
2935 <pre class="verbatim" xml:space="preserve">
2936 <div class="compact-block"><tt>2 </tt>{
2937 <tt>3 </tt> print &quot;ERROR: &quot; FILENAME &quot;:&quot; FNR &quot; &quot; message &gt; &quot;/dev/stderr&quot;;
2938 <tt>4 </tt> exit 1;</div></pre>
2940 <tt class="verbatim"><div class="compact-block">
2941 <tt>5 </tt>}
2942 </div></tt>
2943 </p>
2944 </p><p>
2945 <a id="code-end-error()-1"></a>
2946 </p></font>
2947 </p>
2949 and likewise for non-fatal warnings:
2950 </p>
2952 <font size="-1"><p>
2953 <a id="code-label-error()-2"></a>
2954 <table style="width: 100%" id="code-ref-error()-2">
2955 <tbody><tr>
2956 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="error()"></a><font size="-1"><div class="left-tab">
2957 <font color="blue">error()</font>[2]() &uArr;<a href="#code-ref-error()-1">34a</a>,
2958 lang=<font color="blue">awk</font> + &equiv;
2959 </div><div class="right-tab">
2960 &#x25B5;<a href="#code-ref-error()-1">34a</a> <a href="#code-ref-error()-3">34c</a>&#x25BF;
2961 </div></font></td>
2962 </tr></tbody>
2963 </table>
2964 </p><p>
2966 <tt class="verbatim"><div class="compact-block">
2967 <tt>6 </tt>function warning(message)
2968 </div></tt>
2969 </p>
2970 <pre class="verbatim" xml:space="preserve">
2971 <div class="compact-block"><tt>7 </tt>{
2972 <tt>8 </tt> print &quot;WARNING: &quot; FILENAME &quot;:&quot; FNR &quot; &quot; message &gt; &quot;/dev/stderr&quot;;
2973 <tt>9 </tt> warnings++;</div></pre>
2975 <tt class="verbatim"><div class="compact-block">
2976 <tt>10 </tt>}
2977 </div></tt>
2978 </p>
2979 </p><p>
2980 <a id="code-end-error()-2"></a>
2981 </p></font>
2982 </p>
2984 and debug output too:
2985 </p>
2987 <font size="-1"><p>
2988 <a id="code-label-error()-3"></a>
2989 <table style="width: 100%" id="code-ref-error()-3">
2990 <tbody><tr>
2991 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="error()"></a><font size="-1"><div class="left-tab">
2992 <font color="blue">error()</font>[3]() &uArr;<a href="#code-ref-error()-1">34a</a>,
2993 lang=<font color="blue">awk</font> + &equiv;
2994 </div><div class="right-tab">
2995 &#x25B5;<a href="#code-ref-error()-2">34b</a>
2996 </div></font></td>
2997 </tr></tbody>
2998 </table>
2999 </p><p>
3001 <tt class="verbatim"><div class="compact-block">
3002 <tt>11 </tt>function debug_log(message)
3003 </div></tt>
3004 </p>
3005 <pre class="verbatim" xml:space="preserve">
3006 <div class="compact-block"><tt>12 </tt>{
3007 <tt>13 </tt> print &quot;DEBUG: &quot; FILENAME &quot;:&quot; FNR &quot; &quot; message &gt; &quot;/dev/stderr&quot;;</div></pre>
3009 <tt class="verbatim"><div class="compact-block">
3010 <tt>14 </tt>}
3011 </div></tt>
3012 </p>
3013 </p><table style="width: 100%" id="code-end-error()-3">
3014 <tbody><tr>
3015 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3016 </tr></tbody>
3017 </table></font>
3018 </p>
3019 <table style="display: inline; vertical-align: -0.55em">
3020 <tbody><tr>
3021 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: append=helper-functions</td>
3022 </tr></tbody>
3023 </table>
3025 <font size="-1"><p>
3026 <a id="code-label-helper-functions-1"></a>
3027 <table style="width: 100%" id="code-ref-helper-functions-1">
3028 <tbody><tr>
3029 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="helper-functions"></a><font size="-1"><font color="blue">helper-functions</font>[1](),
3030 lang=<font color="blue"></font> &equiv;</font></td>
3031 </tr></tbody>
3032 </table>
3033 </p><p>
3034 <tt class="verbatim"><div class="compact-block">
3035 <tt>1 </tt>error() <a href="#code-ref-error()-1">34a</a>
3036 </div></tt>
3037 </p><table style="width: 100%" id="code-end-helper-functions-1">
3038 <tbody><tr>
3039 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3040 </tr></tbody>
3041 </table></font>
3042 </p>
3043 <h1 id="auto-42">Chapter 8<br></br>LaTeX and lstlistings</h1>
3044 <table style="display: inline; vertical-align: -0.55em">
3045 <tbody><tr>
3046 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: Split LyX and TeXmacs parts</td>
3047 </tr></tbody>
3048 </table>
3050 For L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X and LaTeX, the <tt
3051 class="verbatim">lstlistings</tt> package is used to format the lines of code
3052 chunks. You may recal from chapter XXX that arguments to a chunk
3053 definition are pure LaTeX code. This means that fangle needs to be able
3054 to parse LaTeX a little.
3055 </p>
3057 LaTeX arguments to <tt class="verbatim">lstlistings</tt> macros are a comma
3058 seperated list of key-value pairs, and values containing commas are
3059 enclosed in <tt class="verbatim">{</tt> braces <tt class="verbatim">}</tt> (which is to be
3060 expected for LaTeX).
3061 </p>
3063 A sample expressions is:
3064 </p>
3065 <pre class="verbatim" xml:space="preserve">
3066 name=thomas, params={a, b}, something, something-else</pre>
3068 but we see that this is just a simpler form of this expression:
3069 </p>
3070 <pre class="verbatim" xml:space="preserve">
3071 name=freddie, foo={bar=baz, quux={quirk, a=fleeg}}, etc</pre>
3073 We may consider that we need a function that can parse such LaTeX
3074 expressions and assign the values to an AWK associated array, perhaps
3075 using a recursive parser into a multi-dimensional hash
3077 <font size="-1"><div align="justify">
3078 <div style="margin-left: 0px">
3079 <div style="margin-right: 0px">
3081 . as AWK doesn't have nested-hash support
3082 </p>
3083 </div>
3084 </div>
3085 </div></font>
3086 </p>
3087 <span style="margin-left: 0em"></span>
3088 <a id="footnr-1"></a>
3089 <sup><a href="#footnote-1">1</a></sup>
3090 , resulting in:
3091 </p>
3092 <table style="display: inline; vertical-align: -3.3em">
3093 <tbody><tr>
3094 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">key</td>
3095 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">value</td>
3096 </tr><tr>
3097 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">a[name]</td>
3098 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">freddie</td>
3099 </tr><tr>
3100 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">a[foo, bar]</td>
3101 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">baz</td>
3102 </tr><tr>
3103 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">a[foo, quux, quirk]</td>
3104 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid"></td>
3105 </tr><tr>
3106 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">a[foo, quux, a]</td>
3107 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">fleeg</td>
3108 </tr><tr>
3109 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">a[etc]</td>
3110 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid"></td>
3111 </tr></tbody>
3112 </table>
3114 Yet, also, on reflection it seems that sometimes such nesting is not
3115 desirable, as the braces are also used to delimit values that contain
3116 commas &ndash;- we may consider that
3117 </p>
3118 <pre class="verbatim" xml:space="preserve">
3119 name={williamson, freddie}</pre>
3121 should assign <tt class="verbatim">williamson, freddie</tt> to <tt class="verbatim">name</tt>.
3122 </p>
3124 In fact we are not so interested in the detail so as to be bothered by
3125 this, which turns out to be a good thing for two reasons. Firstly TeX
3126 has a malleable parser with no strict syntax, and secondly whether or
3127 not <tt class="verbatim">williamson</tt> and <tt class="verbatim">freddie</tt> should count
3128 as two items will be context dependant anyway.
3129 </p>
3131 We need to parse this latex for only one reason; which is that we are
3132 extending lstlistings to add some additional arguments which will be
3133 used to express chunk parameters and other chunk options.
3134 </p>
3135 <h2 id="auto-43">8.1<span style="margin-left: 1em"></span>Additional lstlstings parameters</h2>
3137 Further on we define a <tt class="verbatim">\Chunk</tt> LaTeX macro whose
3138 arguments will consist of a the chunk name, optionally followed by a
3139 comma and then a comma separated list of arguments. In fact we will just
3140 need to prefix <tt class="verbatim">name=</tt> to the arguments to in order to
3141 create valid lstlistings arguments.
3142 </p>
3144 There will be other arguments supported too;
3145 </p>
3146 <dl>
3147 <dt>
3148 params
3149 </dt>
3150 <dd>
3152 As an extension to many literate-programming styles, fangle permits
3153 code chunks to take parameters and thus operate somewhat like C
3154 pre-processor macros, or like C++ templates. Chunk parameters are
3155 declared with a chunk argument called params, which holds a
3156 semi-colon separated list of parameters, like this:
3157 </p>
3158 <pre class="verbatim" xml:space="preserve">
3159 achunk,language=C,params=name;address</pre>
3160 </dd>
3161 <dt>
3162 addto
3163 </dt>
3164 <dd>
3165 a named chunk that this chunk is to be included into. This saves the
3166 effort of having to declare another listing of the named chunk merely
3167 to include this one.
3168 </dd>
3169 </dl>
3171 Function get_chunk_args() will accept two paramters, text being the text
3172 to parse, and values being an array to receive the parsed values as
3173 described above. The optional parameter path is used during recursion to
3174 build up the multi-dimensional array path.
3175 </p>
3177 <font size="-1"><p>
3178 <a id="code-label-./fangle-4"></a>
3179 <table style="width: 100%" id="code-ref-./fangle-4">
3180 <tbody><tr>
3181 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle"></a><font size="-1"><div class="left-tab">
3182 <font color="blue">./fangle</font>[4]() &uArr;<a href="#code-ref-./fangle-1">33a</a>,
3183 lang=<font color="blue"></font> + &equiv;
3184 </div><div class="right-tab">
3185 &#x22B2;<a href="#code-ref-./fangle-3">33c</a>
3186 </div></font></td>
3187 </tr></tbody>
3188 </table>
3189 </p><p>
3190 <tt class="verbatim"><div class="compact-block">
3191 <tt>19 </tt>=&lt;\chunkref{get_chunk_args()}&gt;
3192 </div></tt>
3193 </p><table style="width: 100%" id="code-end-./fangle-4">
3194 <tbody><tr>
3195 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3196 </tr></tbody>
3197 </table></font>
3198 </p>
3200 <font size="-1"><p>
3201 <a id="code-label-get_chunk_args()-1"></a>
3202 <table style="width: 100%" id="code-ref-get_chunk_args()-1">
3203 <tbody><tr>
3204 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="get_chunk_args()"></a><font size="-1"><div class="left-tab">
3205 <font color="blue">get_chunk_args()</font>[1](), lang=<font color="blue"></font>
3206 &equiv;
3207 </div><div class="right-tab">
3208 <a href="#code-ref-get_chunk_args()-2">36c</a>&#x25BF;
3209 </div></font></td>
3210 </tr></tbody>
3211 </table>
3212 </p><p>
3214 <tt class="verbatim"><div class="compact-block">
3215 <tt>1 </tt>function get_chunk_args(text, values,
3216 </div></tt>
3217 </p>
3218 <pre class="verbatim" xml:space="preserve">
3219 <div class="compact-block"><tt>2 </tt> # optional parameters
3220 <tt>3 </tt> path, # hierarchical precursors
3221 <tt>4 </tt> # local vars</div></pre>
3223 <tt class="verbatim"><div class="compact-block">
3224 <tt>5 </tt> a, name)
3225 </div></tt>
3226 </p>
3227 </p><p>
3228 <a id="code-end-get_chunk_args()-1"></a>
3229 </p></font>
3230 </p>
3232 The strategy is to parse the name, and then look for a value. If the
3233 value begins with a brace <tt class="verbatim">{</tt>, then we recurse and consume
3234 as much of the text as necessary, returning the remaining text when we
3235 encounter a leading close-brace <tt class="verbatim">}</tt>. This being the
3236 strategy &ndash;- and executed in a loop &ndash;- we realise that we
3237 must first look for the closing brace (perhaps preceded by white space)
3238 in order to terminate the recursion, and returning remaining text.
3239 </p>
3241 <font size="-1"><p>
3242 <a id="code-label-get_chunk_args()-2"></a>
3243 <table style="width: 100%" id="code-ref-get_chunk_args()-2">
3244 <tbody><tr>
3245 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="get_chunk_args()"></a><font size="-1"><div class="left-tab">
3246 <font color="blue">get_chunk_args()</font>[2]() &uArr;<a href="#code-ref-get_chunk_args()-1">36b</a>,
3247 lang=<font color="blue"></font> + &equiv;
3248 </div><div class="right-tab">
3249 &#x25B5;<a href="#code-ref-get_chunk_args()-1">36b</a>
3250 </div></font></td>
3251 </tr></tbody>
3252 </table>
3253 </p><p>
3255 <tt class="verbatim"><div class="compact-block">
3256 <tt>6 </tt>{
3257 </div></tt>
3258 </p>
3259 <pre class="verbatim" xml:space="preserve">
3260 <div class="compact-block"><tt>7 </tt> split(&quot;&quot;, next_chunk_args);
3261 <tt>8 </tt> while(length(text)) {
3262 <tt>9 </tt> if (match(text, &quot;^ *}(.*)&quot;, a)) {
3263 <tt>10 </tt> return a[1];
3264 <tt>11 </tt> }
3265 <tt>12 </tt> =&lt;\chunkref{parse-chunk-args}&gt;
3266 <tt>13 </tt> }
3267 <tt>14 </tt> return text;</div></pre>
3269 <tt class="verbatim"><div class="compact-block">
3270 <tt>15 </tt>}
3271 </div></tt>
3272 </p>
3273 </p><table style="width: 100%" id="code-end-get_chunk_args()-2">
3274 <tbody><tr>
3275 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3276 </tr></tbody>
3277 </table></font>
3278 </p>
3280 We can see that the text could be inspected with this regex:
3281 </p>
3283 <font size="-1"><p>
3284 <a id="code-label-parse-chunk-args-1"></a>
3285 <table style="width: 100%" id="code-ref-parse-chunk-args-1">
3286 <tbody><tr>
3287 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="parse-chunk-args"></a><font size="-1"><div class="left-tab">
3288 <font color="blue">parse-chunk-args</font>[1](), lang=<font color="blue"></font>
3289 &equiv;
3290 </div><div class="right-tab">
3291 <a href="#code-ref-parse-chunk-args-2">37a</a>&#x22B3;
3292 </div></font></td>
3293 </tr></tbody>
3294 </table>
3295 </p><p>
3297 <tt class="verbatim"><div class="compact-block">
3298 <tt>1 </tt>if (! match(text, &quot; *([^,=]*[^,= ]) *(([,=])
3299 *(([^,}]*) *,* *(.*))|)$&quot;, a)) {
3300 </div></tt>
3301 </p>
3302 <pre class="verbatim" xml:space="preserve">
3303 <div class="compact-block"><tt>2 </tt> return text;</div></pre>
3305 <tt class="verbatim"><div class="compact-block">
3306 <tt>3 </tt>}
3307 </div></tt>
3308 </p>
3309 </p><p>
3310 <a id="code-end-parse-chunk-args-1"></a>
3311 </p></font>
3312 </p>
3314 and that <tt class="verbatim">a</tt> will have the following values:
3315 </p>
3316 <table style="display: inline; vertical-align: -3.85em">
3317 <tbody><tr>
3318 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">a[n]</td>
3319 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">assigned text</td>
3320 </tr><tr>
3321 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">1</td>
3322 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">freddie</td>
3323 </tr><tr>
3324 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">2</td>
3325 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">=freddie, foo={bar=baz, quux={quirk, a=fleeg}}, etc</td>
3326 </tr><tr>
3327 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">3</td>
3328 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">=</td>
3329 </tr><tr>
3330 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">4</td>
3331 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">freddie, foo={bar=baz, quux={quirk, a=fleeg}}, etc</td>
3332 </tr><tr>
3333 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">5</td>
3334 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">freddie</td>
3335 </tr><tr>
3336 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">6</td>
3337 <td style="border-left: 0.5pt solid; border-right: 0.5pt solid; border-bottom: 0.5pt solid; border-top: 0.5pt solid">, foo={bar=baz, quux={quirk, a=fleeg}}, etc</td>
3338 </tr></tbody>
3339 </table>
3341 <tt class="verbatim">a[3]</tt> will be either <tt class="verbatim">=</tt> or <tt class="verbatim">,</tt>
3342 and signify whether the option named in <tt class="verbatim">a[1]</tt> has a value
3343 or not (respectively).
3344 </p>
3346 If the option does have a value, then if the expression <tt class="verbatim">substr(a[4],1,1)</tt>
3347 returns a brace <tt class="verbatim">{</tt> it will signify that we need to
3348 recurse:
3349 </p>
3351 <font size="-1"><p>
3352 <a id="code-label-parse-chunk-args-2"></a>
3353 <table style="width: 100%" id="code-ref-parse-chunk-args-2">
3354 <tbody><tr>
3355 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="parse-chunk-args"></a><font size="-1"><div class="left-tab">
3356 <font color="blue">parse-chunk-args</font>[2]() &uArr;<a href="#code-ref-parse-chunk-args-1">36d</a>,
3357 lang=<font color="blue"></font> + &equiv;
3358 </div><div class="right-tab">
3359 &#x22B2;<a href="#code-ref-parse-chunk-args-1">36d</a>
3360 </div></font></td>
3361 </tr></tbody>
3362 </table>
3363 </p><p>
3365 <tt class="verbatim"><div class="compact-block">
3366 <tt>4 </tt>name=a[1];
3367 </div></tt>
3368 </p>
3369 <pre class="verbatim" xml:space="preserve">
3370 <div class="compact-block"><tt>5 </tt>if (a[3] == &quot;=&quot;) {
3371 <tt>6 </tt> if (substr(a[4],1,1) == &quot;{&quot;) {
3372 <tt>7 </tt> text = get_chunk_args(substr(a[4],2), values, path name SUBSEP);
3373 <tt>8 </tt> } else {
3374 <tt>9 </tt> values[path name]=a[5];
3375 <tt>10 </tt> text = a[6];
3376 <tt>11 </tt> }
3377 <tt>12 </tt>} else {
3378 <tt>13 </tt> values[path name]=&quot;&quot;;
3379 <tt>14 </tt> text = a[2];</div></pre>
3381 <tt class="verbatim"><div class="compact-block">
3382 <tt>15 </tt>}
3383 </div></tt>
3384 </p>
3385 </p><table style="width: 100%" id="code-end-parse-chunk-args-2">
3386 <tbody><tr>
3387 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3388 </tr></tbody>
3389 </table></font>
3390 </p>
3392 We can test this function like this:
3393 </p>
3395 <font size="-1"><p>
3396 <a id="code-label-gca-test.awk-1"></a>
3397 <table style="width: 100%" id="code-ref-gca-test.awk-1">
3398 <tbody><tr>
3399 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="gca-test.awk"></a><font size="-1"><font color="blue">gca-test.awk</font>[1](),
3400 lang=<font color="blue"></font> &equiv;</font></td>
3401 </tr></tbody>
3402 </table>
3403 </p><p>
3405 <tt class="verbatim"><div class="compact-block">
3406 <tt>1 </tt>=&lt;\chunkref{get_chunk_args()}&gt;
3407 </div></tt>
3408 </p>
3409 <pre class="verbatim" xml:space="preserve">
3410 <div class="compact-block"><tt>2 </tt>BEGIN {
3411 <tt>3 </tt> SUBSEP=&quot;.&quot;;
3412 <tt>4 </tt>
3413 <tt>5 </tt> print get_chunk_args(&quot;name=freddie, foo={bar=baz, quux={quirk, a=fleeg}}, etc&quot;, a);
3414 <tt>6 </tt> for (b in a) {
3415 <tt>7 </tt> print &quot;a[&quot; b &quot;] =&gt; &quot; a[b];
3416 <tt>8 </tt> }</div></pre>
3418 <tt class="verbatim"><div class="compact-block">
3419 <tt>9 </tt>}
3420 </div></tt>
3421 </p>
3422 </p><table style="width: 100%" id="code-end-gca-test.awk-1">
3423 <tbody><tr>
3424 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3425 </tr></tbody>
3426 </table></font>
3427 </p>
3429 which should give this output:
3430 </p>
3432 <font size="-1"><p>
3433 <a id="code-label-gca-test.awk-results-1"></a>
3434 <table style="width: 100%" id="code-ref-gca-test.awk-results-1">
3435 <tbody><tr>
3436 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="gca-test.awk-results"></a><font size="-1"><font color="blue">gca-test.awk-results</font>[1](),
3437 lang=<font color="blue"></font> &equiv;</font></td>
3438 </tr></tbody>
3439 </table>
3440 </p><p>
3442 <tt class="verbatim"><div class="compact-block">
3443 <tt>1 </tt>a[foo.quux.quirk] =&gt;
3444 </div></tt>
3445 </p>
3446 <pre class="verbatim" xml:space="preserve">
3447 <div class="compact-block"><tt>2 </tt>a[foo.quux.a] =&gt; fleeg
3448 <tt>3 </tt>a[foo.bar] =&gt; baz
3449 <tt>4 </tt>a[etc] =&gt; </div></pre>
3451 <tt class="verbatim"><div class="compact-block">
3452 <tt>5 </tt>a[name] =&gt; freddie
3453 </div></tt>
3454 </p>
3455 </p><table style="width: 100%" id="code-end-gca-test.awk-results-1">
3456 <tbody><tr>
3457 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3458 </tr></tbody>
3459 </table></font>
3460 </p>
3461 <h2 id="auto-44"><a id="Chunk Arguments"></a>8.2<span style="margin-left: 1em"></span>Parsing chunk arguments</h2>
3463 Arguments to paramterized chunks are expressed in round brackets as a
3464 comma separated list of optional arguments. For example, a chunk that is
3465 defined with:
3466 </p>
3467 <pre class="verbatim" xml:space="preserve">
3468 \Chunk{achunk, params=name ; address}</pre>
3470 could be invoked as:
3471 </p>
3472 <pre class="verbatim" xml:space="preserve">
3473 \chunkref{achunk}(John Jones, jones@example.com)</pre>
3475 An argument list may be as simple as in <tt class="verbatim">\chunkref{pull}(thing,
3476 otherthing)</tt> or as complex as:
3477 </p>
3478 <pre class="verbatim" xml:space="preserve">
3479 \chunkref{pull}(things[x, y], get_other_things(a, &quot;(all)&quot;))</pre>
3481 &ndash;- which for all it's commas and quotes and parenthesis represents
3482 only two parameters: <tt class="verbatim">things[x, y]</tt> and <tt class="verbatim">get_other_things(a,
3483 &quot;(all)&quot;)</tt>.
3484 </p>
3486 If we simply split parameter list on commas, then the comma in <tt
3487 class="verbatim">things[x,y]</tt> would split into two seperate arguments: <tt
3488 class="verbatim">things[x</tt> and <tt class="verbatim">y]</tt>&ndash;- neither of which
3489 make sense on their own.
3490 </p>
3492 One way to prevent this would be by refusing to split text between
3493 matching delimiters, such as
3494 <tt class="verbatim">[</tt>
3496 <tt class="verbatim">]</tt>
3498 <tt class="verbatim">(</tt>
3500 <tt class="verbatim">)</tt>
3502 <tt class="verbatim">{</tt>
3504 <tt class="verbatim">}</tt>
3505 and most likely also
3506 <tt class="verbatim">&quot;</tt>
3508 <tt class="verbatim">&quot;</tt>
3510 <tt class="verbatim">'</tt>
3512 <tt class="verbatim">'</tt>
3513 . Of course this also makes it impossible to pass such mis-matched code
3514 fragments as parameters, but I think that it would be hard for readers
3515 to cope with authors who would pass such code unbalanced fragments as
3516 chunk parameters
3518 <font size="-1"><div align="justify">
3519 <div style="margin-left: 0px">
3520 <div style="margin-right: 0px">
3522 . I know that I couldn't cope with users doing such things,
3523 and although the GPL3 license prevents me from actually
3524 forbidding anyone from trying, if they want it to work they'll
3525 have to write the code themselves and not expect any support
3526 from me.
3527 </p>
3528 </div>
3529 </div>
3530 </div></font>
3531 </p>
3532 <span style="margin-left: 0em"></span>
3533 <a id="footnr-2"></a>
3534 <sup><a href="#footnote-2">2</a></sup>
3536 </p>
3538 Unfortunately, the full set of matching delimiters may vary from
3539 language to language. In certain C++ template contexts, <tt class="verbatim">&lt;</tt>
3540 and <tt class="verbatim">&gt;</tt> would count as delimiters, and yet in other
3541 contexts they would not.
3542 </p>
3544 This puts me in the unfortunate position of having to parse-somewhat all
3545 programming languages without knowing what they are!
3546 </p>
3548 However, if this universal mode-tracking is possible, then parsing the
3549 arguments would be trivial. Such a mode tracker is described in chapter
3550 <a href="#modes">9</a> and used here with simplicity.
3551 </p>
3553 <font size="-1"><p>
3554 <a id="code-label-parse_chunk_args-1"></a>
3555 <table style="width: 100%" id="code-ref-parse_chunk_args-1">
3556 <tbody><tr>
3557 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="parse_chunk_args"></a><font size="-1"><font color="blue">parse_chunk_args</font>[1](),
3558 lang=<font color="blue"></font> &equiv;</font></td>
3559 </tr></tbody>
3560 </table>
3561 </p><p>
3563 <tt class="verbatim"><div class="compact-block">
3564 <tt>1 </tt>function parse_chunk_args(language, text, values,
3565 mode,
3566 </div></tt>
3567 </p>
3568 <pre class="verbatim" xml:space="preserve">
3569 <div class="compact-block"><tt>2 </tt> # local vars
3570 <tt>3 </tt> c, context, rest)
3571 <tt>4 </tt>{
3572 <tt>5 </tt> =&lt;\chunkref{new-mode-tracker}(context, language, mode)&gt;
3573 <tt>6 </tt> rest = mode_tracker(context, text, values);
3574 <tt>7 </tt> # extract values
3575 <tt>8 </tt> for(c=1; c &lt;= context[0, &quot;values&quot;]; c++) {
3576 <tt>9 </tt> values[c] = context[0, &quot;values&quot;, c];
3577 <tt>10 </tt> }
3578 <tt>11 </tt> return rest;</div></pre>
3580 <tt class="verbatim"><div class="compact-block">
3581 <tt>12 </tt>}
3582 </div></tt>
3583 </p>
3584 </p><table style="width: 100%" id="code-end-parse_chunk_args-1">
3585 <tbody><tr>
3586 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3587 </tr></tbody>
3588 </table></font>
3589 </p>
3590 <h2 id="auto-45">8.3<span style="margin-left: 1em"></span>Expanding parameters in the text</h2>
3592 Within the body of the chunk, the parameters are referred to with: <tt
3593 class="verbatim">${name}</tt> and <tt class="verbatim">${address}</tt>. There is a strong
3594 case that a LaTeX style notation should be used, like <tt class="verbatim">\param{name}</tt>
3595 which would be expressed in the listing as <tt class="verbatim">=&lt;\param{name}&gt;</tt>
3596 and be rendered as <tt class="verbatim"><font color="#008000">name</font></tt>. Such
3597 notation would make me go blind, but I do intend to adopt it.
3598 </p>
3600 We therefore need a function <tt class="verbatim">expand_chunk_args</tt> which
3601 will take a block of text, a list of permitted parameters, and the
3602 arguments which must substitute for the parameters.
3603 </p>
3605 Here we split the text on <tt class="verbatim">${</tt> which means that all parts
3606 except the first will begin with a parameter name which will be
3607 terminated by <tt class="verbatim">}</tt>. The split function will consume the
3608 literal <tt class="verbatim">${</tt> in each case.
3609 </p>
3611 <font size="-1"><p>
3612 <a id="code-label-expand_chunk_args()-1"></a>
3613 <table style="width: 100%" id="code-ref-expand_chunk_args()-1">
3614 <tbody><tr>
3615 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="expand_chunk_args()"></a><font size="-1"><font color="blue">expand_chunk_args()</font>[1](),
3616 lang=<font color="blue"></font> &equiv;</font></td>
3617 </tr></tbody>
3618 </table>
3619 </p><p>
3621 <tt class="verbatim"><div class="compact-block">
3622 <tt>1 </tt>function expand_chunk_args(text, params, args,
3623 </div></tt>
3624 </p>
3625 <pre class="verbatim" xml:space="preserve">
3626 <div class="compact-block"><tt>2 </tt> p, text_array, next_text, v, t, l)
3627 <tt>3 </tt>{
3628 <tt>4 </tt> if (split(text, text_array, &quot;\\${&quot;)) {
3629 <tt>5 </tt> substitute-chunk-args <a href="#code-ref-substitute-chunk-args-1">39a</a>
3630 <tt>6 </tt> }
3631 <tt>7 </tt>
3632 <tt>8 </tt> return text;</div></pre>
3634 <tt class="verbatim"><div class="compact-block">
3635 <tt>9 </tt>}
3636 </div></tt>
3637 </p>
3638 </p><table style="width: 100%" id="code-end-expand_chunk_args()-1">
3639 <tbody><tr>
3640 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3641 </tr></tbody>
3642 </table></font>
3643 </p>
3645 First, we produce an associative array of substitution values indexed by
3646 parameter names. This will serve as a cache, allowing us to look up the
3647 replacement values as we extract each name.
3648 </p>
3650 <font size="-1"><p>
3651 <a id="code-label-substitute-chunk-args-1"></a>
3652 <table style="width: 100%" id="code-ref-substitute-chunk-args-1">
3653 <tbody><tr>
3654 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="substitute-chunk-args"></a><font size="-1"><div class="left-tab">
3655 <font color="blue">substitute-chunk-args</font>[1](), lang=<font
3656 color="blue"></font> &equiv;
3657 </div><div class="right-tab">
3658 <a href="#code-ref-substitute-chunk-args-2">39b</a>&#x25BF;
3659 </div></font></td>
3660 </tr></tbody>
3661 </table>
3662 </p><p>
3664 <tt class="verbatim"><div class="compact-block">
3665 <tt>1 </tt>for(p in params) {
3666 </div></tt>
3667 </p>
3668 <pre class="verbatim" xml:space="preserve">
3669 <div class="compact-block"><tt>2 </tt> v[params[p]]=args[p];</div></pre>
3671 <tt class="verbatim"><div class="compact-block">
3672 <tt>3 </tt>}
3673 </div></tt>
3674 </p>
3675 </p><p>
3676 <a id="code-end-substitute-chunk-args-1"></a>
3677 </p></font>
3678 </p>
3680 We accumulate substituted text in the variable text. As the first part
3681 of the split function is the part before the delimiter &ndash;- which is
3682 <tt class="verbatim">${</tt> in our case &ndash;- this part will never contain a
3683 parameter reference, so we assign this directly to the result kept in
3684 <tt class="verbatim">$text</tt>.
3685 </p>
3687 <font size="-1"><p>
3688 <a id="code-label-substitute-chunk-args-2"></a>
3689 <table style="width: 100%" id="code-ref-substitute-chunk-args-2">
3690 <tbody><tr>
3691 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="substitute-chunk-args"></a><font size="-1"><div class="left-tab">
3692 <font color="blue">substitute-chunk-args</font>[2]() &uArr;<a href="#code-ref-substitute-chunk-args-1">39a</a>,
3693 lang=<font color="blue"></font> + &equiv;
3694 </div><div class="right-tab">
3695 &#x25B5;<a href="#code-ref-substitute-chunk-args-1">39a</a> <a href="#code-ref-substitute-chunk-args-3">39c</a>&#x25BF;
3696 </div></font></td>
3697 </tr></tbody>
3698 </table>
3699 </p><p>
3700 <tt class="verbatim"><div class="compact-block">
3701 <tt>4 </tt>text=text_array[1];
3702 </div></tt>
3703 </p><p>
3704 <a id="code-end-substitute-chunk-args-2"></a>
3705 </p></font>
3706 </p>
3708 We then iterate over the remaining values in the array
3710 <font size="-1"><div align="justify">
3711 <div style="margin-left: 0px">
3712 <div style="margin-right: 0px">
3714 . I don't know why I think that it will enumerate the array in
3715 order, but it seems to work
3716 </p>
3717 </div>
3718 </div>
3719 </div></font>
3720 </p>
3721 <span style="margin-left: 0em"></span>
3722 <a id="footnr-3"></a>
3723 <sup><a href="#footnote-3">3</a></sup>
3724 <table style="display: inline; vertical-align: -0.55em">
3725 <tbody><tr>
3726 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: fix or prove it</td>
3727 </tr></tbody>
3728 </table>
3729 , and substitute each reference for it's argument.
3730 </p>
3732 <font size="-1"><p>
3733 <a id="code-label-substitute-chunk-args-3"></a>
3734 <table style="width: 100%" id="code-ref-substitute-chunk-args-3">
3735 <tbody><tr>
3736 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="substitute-chunk-args"></a><font size="-1"><div class="left-tab">
3737 <font color="blue">substitute-chunk-args</font>[3]() &uArr;<a href="#code-ref-substitute-chunk-args-1">39a</a>,
3738 lang=<font color="blue"></font> + &equiv;
3739 </div><div class="right-tab">
3740 &#x25B5;<a href="#code-ref-substitute-chunk-args-2">39b</a>
3741 </div></font></td>
3742 </tr></tbody>
3743 </table>
3744 </p><p>
3746 <tt class="verbatim"><div class="compact-block">
3747 <tt>5 </tt>for(t=2; t in text_array; t++) {
3748 </div></tt>
3749 </p>
3750 <pre class="verbatim" xml:space="preserve">
3751 <div class="compact-block"><tt>6 </tt> =&lt;\chunkref{substitute-chunk-arg}&gt;</div></pre>
3753 <tt class="verbatim"><div class="compact-block">
3754 <tt>7 </tt>}
3755 </div></tt>
3756 </p>
3757 </p><table style="width: 100%" id="code-end-substitute-chunk-args-3">
3758 <tbody><tr>
3759 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3760 </tr></tbody>
3761 </table></font>
3762 </p>
3764 After the split on <tt class="verbatim">${</tt> a valid parameter reference will
3765 consist of valid parameter name terminated by a close-brace <tt class="verbatim">}</tt>.
3766 A valid character name begins with the underscore or a letter, and may
3767 contain letters, digits or underscores.
3768 </p>
3770 A valid looking reference that is not actually the name of a parameter
3771 will be and not substituted. This is good because there is nothing to
3772 substitute anyway, and it avoids clashes when writing code for languages
3773 where <tt class="verbatim">${...}</tt> is a valid construct &ndash;- such
3774 constructs will not be interfered with unless the parameter name also
3775 matches.
3776 </p>
3778 <font size="-1"><p>
3779 <a id="code-label-substitute-chunk-arg-1"></a>
3780 <table style="width: 100%" id="code-ref-substitute-chunk-arg-1">
3781 <tbody><tr>
3782 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="substitute-chunk-arg"></a><font size="-1"><font color="blue">substitute-chunk-arg</font>[1](),
3783 lang=<font color="blue"></font> &equiv;</font></td>
3784 </tr></tbody>
3785 </table>
3786 </p><p>
3788 <tt class="verbatim"><div class="compact-block">
3789 <tt>1 </tt>if (match(text_array[t],
3790 &quot;^([a-zA-Z_][a-zA-Z0-9_]*)}&quot;, l) &amp;&amp;
3791 </div></tt>
3792 </p>
3793 <pre class="verbatim" xml:space="preserve">
3794 <div class="compact-block"><tt>2 </tt> l[1] in v)
3795 <tt>3 </tt>{
3796 <tt>4 </tt> text = text v[l[1]] substr(text_array[t], length(l[1])+2);
3797 <tt>5 </tt>} else {
3798 <tt>6 </tt> text = text &quot;${&quot; text_array[t];</div></pre>
3800 <tt class="verbatim"><div class="compact-block">
3801 <tt>7 </tt>}
3802 </div></tt>
3803 </p>
3804 </p><table style="width: 100%" id="code-end-substitute-chunk-arg-1">
3805 <tbody><tr>
3806 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3807 </tr></tbody>
3808 </table></font>
3809 </p>
3810 <h1 id="auto-46"><a id="modes"></a>Chapter 9<br></br>Language Modes &amp; Quoting</h1>
3811 <h2 id="auto-47">9.1<span style="margin-left: 1em"></span>Modes</h2>
3813 <tt class="verbatim">lstlistings</tt> and <tt class="verbatim">fangle</tt> both recognize
3814 source languages, and perform some basic parsing. <tt class="verbatim">lstlistings</tt>
3815 can detect strings and comments within a language definition and perform
3816 suitable rendering, such as italics for comments, and visible-spaces
3817 within strings.
3818 </p>
3820 Fangle similarly can recognize strings, and comments, etc, within a
3821 language, so that any chunks included with <tt class="verbatim">\chunkref</tt> can
3822 be suitably escape or quoted.
3823 </p>
3824 <h3 id="auto-48">9.1.1<span style="margin-left: 1em"></span>Modes to keep code together</h3>
3826 As an example, in the C language there are a few parse modes, affecting
3827 the interpretation of characters.
3828 </p>
3830 One parse mode is the strings mode. The string mode is commenced by an
3831 un-escaped quotation mark <tt class="verbatim">&quot;</tt> and terminated by the
3832 same. Within the string mode, only one additional mode can be commenced,
3833 it is the backslash mode <tt class="verbatim">\</tt>, which is always terminated
3834 after the folloing character.
3835 </p>
3837 Another mode is <tt class="verbatim">[</tt> which is terminated by a <tt class="verbatim">]</tt>
3838 (unless it occurs in a string).
3839 </p>
3841 Consider this fragment of C code:
3842 </p>
3845 </p>
3847 things([<var>x</var>,
3848 <var>y</var>])<sup>&amp;wide-overbrace;</sup><sup>1. [ mode</sup>,
3849 get_other_things((<var>a</var>, <sub>3. "
3850 mode</sub>))<sup>&amp;wide-overbrace;</sup><sup>2. ( mode</sup>
3851 </p>
3854 </p>
3856 Mode nesting prevents the close parenthesis in the quoted string (part
3857 3) from terminating the parenthesis mode (part 2).
3858 </p>
3860 Each language has a set of modes, the default mode being the null mode.
3861 Each mode can lead to other modes.
3862 </p>
3863 <h3 id="auto-49">9.1.2<span style="margin-left: 1em"></span>Modes affect included chunks</h3>
3865 For instance, consider this chunk with language=perl:
3866 </p>
3868 <font size="-1"><p>
3869 <a id="code-label-example-perl-1"></a>
3870 <table style="width: 100%" id="code-ref-example-perl-1">
3871 <tbody><tr>
3872 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="example-perl"></a><font size="-1"><font color="blue">example-perl</font>[1](),
3873 lang=<font color="blue">perl</font> &equiv;</font></td>
3874 </tr></tbody>
3875 </table>
3876 </p><p>
3877 <tt class="verbatim"><div class="compact-block">
3878 print &quot;hello world $0\n&quot;;
3879 </div></tt>
3880 </p><table style="width: 100%" id="code-end-example-perl-1">
3881 <tbody><tr>
3882 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3883 </tr></tbody>
3884 </table></font>
3885 </p>
3887 If it were included in a chunk with <tt class="verbatim">language=sh</tt>, like
3888 this:
3889 </p>
3891 <font size="-1"><p>
3892 <a id="code-label-example-sh-1"></a>
3893 <table style="width: 100%" id="code-ref-example-sh-1">
3894 <tbody><tr>
3895 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="example-sh"></a><font size="-1"><font color="blue">example-sh</font>[1](),
3896 lang=<font color="blue">sh</font> &equiv;</font></td>
3897 </tr></tbody>
3898 </table>
3899 </p><p>
3900 <tt class="verbatim"><div class="compact-block">
3901 perl -e &quot;=&lt;\chunkref{example-perl}&gt;&quot;
3902 </div></tt>
3903 </p><table style="width: 100%" id="code-end-example-sh-1">
3904 <tbody><tr>
3905 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3906 </tr></tbody>
3907 </table></font>
3908 </p>
3910 fangle would <em>want</em> to generate output like this:
3911 </p>
3912 <pre class="verbatim" xml:space="preserve">
3913 perl -e &quot;print \&quot;hello world \$0\\n\&quot;;&quot; </pre>
3915 See that the double quote <tt class="verbatim">&quot;</tt>, back-slash <tt class="verbatim">\</tt>
3916 and <tt class="verbatim">$</tt> have been quoted with a back-slash to protect them
3917 from shell interpretation.
3918 </p>
3920 If that were then included in a chunk with language=make, like this:
3921 </p>
3923 <font size="-1"><p>
3924 <a id="code-label-example-makefile-1"></a>
3925 <table style="width: 100%" id="code-ref-example-makefile-1">
3926 <tbody><tr>
3927 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="example-makefile"></a><font size="-1"><font color="blue">example-makefile</font>[1](),
3928 lang=<font color="blue">make</font> &equiv;</font></td>
3929 </tr></tbody>
3930 </table>
3931 </p><p>
3933 <tt class="verbatim"><div class="compact-block">
3934 <tt>1 </tt>target: pre-req
3935 </div></tt>
3936 </p>
3937 <pre class="verbatim" xml:space="preserve">
3938 <div class="compact-block"></div></pre>
3940 <tt class="verbatim"><div class="compact-block">
3941 <div class="left-tab">
3942 <tt>2 </tt>
3943 </div>
3944 <div class="right-tab">
3945 =&lt;\chunkref{example-sh}&gt;
3946 </div>
3947 </div></tt>
3948 </p>
3949 </p><table style="width: 100%" id="code-end-example-makefile-1">
3950 <tbody><tr>
3951 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
3952 </tr></tbody>
3953 </table></font>
3954 </p>
3956 We would need the output to look like this &ndash;- note the <tt class="verbatim">$$</tt>:
3957 </p>
3958 <pre class="verbatim" xml:space="preserve">
3959 target: pre-req
3960 perl -e &quot;print \&quot;hello world \$$0\\n\&quot;;&quot;</pre>
3962 In order to make this work, we need to define a mode-tracker supporting
3963 each language, that can detect the various quoting modes, and provide a
3964 transformation that must be applied to any included text so that
3965 included text will be interpreted correctly after any interpolation that
3966 it may be subject to at run-time.
3967 </p>
3969 For example, the sed transformation for text to be inserted into shell
3970 double-quoted strings would be something like:
3971 </p>
3972 <pre class="verbatim" xml:space="preserve">
3973 s/\\/\\\\/g;s/$/\\$/g;s/&quot;/\\&quot;/g;</pre>
3975 which protects <tt class="verbatim">\ $ &quot;</tt>.
3976 </p>
3978 <table style="display: inline; vertical-align: -0.55em">
3979 <tbody><tr>
3980 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: I don't think this example is true</td>
3981 </tr></tbody>
3982 </table>
3983 The mode tracker must also track nested mode-changes, as in this sh
3984 example.
3985 </p>
3986 <pre class="verbatim" xml:space="preserve">
3987 echo &quot;hello &lsquo;id ...&lsquo;&quot;</pre>
3989 &uarr;
3990 </p>
3992 Any characters inserted at the point marked &uarr; would need to be
3993 escaped, including <tt class="verbatim">&lsquo;</tt> <tt class="verbatim">|</tt> <tt class="verbatim">*</tt>
3994 among others. First it would need escaping for the back-ticks <tt class="verbatim">&lsquo;</tt>,
3995 and then for the double-quotes <tt class="verbatim">&quot;</tt>.
3996 </p>
3998 <table style="display: inline; vertical-align: -0.55em">
3999 <tbody><tr>
4000 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: MAYBE</td>
4001 </tr></tbody>
4002 </table>
4003 Escaping need not occur if the format and mode of the included chunk
4004 matches that of the including chunk.
4005 </p>
4007 As each chunk is output a new mode tracker for that language is
4008 initialized in it's normal state. As text is output for that chunk the
4009 output mode is tracked. When a new chunk is included, a transformation
4010 appropriate to that mode is selected and pushed onto a stack of
4011 transformations. Any text to be output is first passed through this
4012 stack of transformations.
4013 </p>
4015 It remains to consider if the chunk-include function should return it's
4016 generated text so that the caller can apply any transformations (and
4017 formatting), or if it should apply the stack of transformations itself.
4018 </p>
4020 Note that the transformed text should have the property of not being
4021 able to change the mode in the current chunk.
4022 </p>
4023 <table style="display: inline; vertical-align: -0.55em">
4024 <tbody><tr>
4025 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: Note chunk parameters should probably also
4026 be transformed</td>
4027 </tr></tbody>
4028 </table>
4029 <h2 id="auto-50">9.2<span style="margin-left: 1em"></span>Language Mode Definitions</h2>
4031 All modes are stored in a single multi-dimensional hash. The first index
4032 is the language, and the second index is the mode-identifier. The third
4033 indexes are terminators, and optionally, submodes, and delimiters.
4034 </p>
4036 A useful set of mode definitions for a nameless general C-type language
4037 is shown here. (Don't be confused by the double backslash escaping
4038 needed in awk. One set of escaping is for the string, and the second set
4039 of escaping is for the regex).
4040 </p>
4041 <table style="display: inline; vertical-align: -0.55em">
4042 <tbody><tr>
4043 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: <p>
4044 TODO: Add =&lt;\mode{}&gt; command which will allow us to signify
4045 that a string is
4046 </p><p>
4047 regex and thus fangle will quote it for us.
4048 </p></td>
4049 </tr></tbody>
4050 </table>
4052 Submodes are entered by the characters <tt class="verbatim">&quot;</tt> <tt
4053 class="verbatim">'</tt> <tt class="verbatim">{</tt> <tt class="verbatim">(</tt> <tt class="verbatim">[</tt> <tt
4054 class="verbatim">/*</tt>
4055 </p>
4057 <font size="-1"><p>
4058 <a id="code-label-common-mode-definitions-1"></a>
4059 <table style="width: 100%" id="code-ref-common-mode-definitions-1">
4060 <tbody><tr>
4061 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="common-mode-definitions"></a><font size="-1"><div class="left-tab">
4062 <font color="blue">common-mode-definitions</font>[1](<font color="blue">language</font>),
4063 lang=<font color="blue"></font> &equiv;
4064 </div><div class="right-tab">
4065 <a href="#code-ref-common-mode-definitions-2">43b</a>&#x25BF;
4066 </div></font></td>
4067 </tr></tbody>
4068 </table>
4069 </p><p>
4070 <tt class="verbatim"><div class="compact-block">
4071 <tt>1 </tt>modes[${language}, &quot;&quot;,
4072 &quot;submodes&quot;]=&quot;\\\\|\&quot;|'|{|\\(|\\[&quot;;
4073 </div></tt>
4074 </p><p>
4075 <a id="code-end-common-mode-definitions-1"></a>
4076 </p></font>
4077 </p>
4079 In the default mode, a comma surrounded by un-important white space is a
4080 delimiter of language items
4082 <font size="-1"><div align="justify">
4083 <div style="margin-left: 0px">
4084 <div style="margin-right: 0px">
4086 . whatever a <em>language item</em> might be
4087 </p>
4088 </div>
4089 </div>
4090 </div></font>
4091 </p>
4092 <span style="margin-left: 0em"></span>
4093 <a id="footnr-1"></a>
4094 <sup><a href="#footnote-1">1</a></sup>
4096 </p>
4098 <font size="-1"><p>
4099 <a id="code-label-common-mode-definitions-2"></a>
4100 <table style="width: 100%" id="code-ref-common-mode-definitions-2">
4101 <tbody><tr>
4102 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="common-mode-definitions"></a><font size="-1"><div class="left-tab">
4103 <font color="blue">common-mode-definitions</font>[2](language)
4104 &uArr;<a href="#code-ref-common-mode-definitions-1">43a</a>, lang=<font color="blue"></font> + &equiv;
4105 </div><div class="right-tab">
4106 &#x25B5;<a href="#code-ref-common-mode-definitions-1">43a</a> <a href="#code-ref-common-mode-definitions-3">43d</a>&#x25BF;
4107 </div></font></td>
4108 </tr></tbody>
4109 </table>
4110 </p><p>
4111 <tt class="verbatim"><div class="compact-block">
4112 <tt>2 </tt>modes[${language}, &quot;&quot;,
4113 &quot;delimiters&quot;]=&quot; *, *&quot;;
4114 </div></tt>
4115 </p><p>
4116 <a id="code-end-common-mode-definitions-2"></a>
4117 </p></font>
4118 </p>
4120 and should pass this test:
4121 <table style="display: inline; vertical-align: -0.55em">
4122 <tbody><tr>
4123 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: Why do the tests run in ?(? mode and not
4124 ?? mode</td>
4125 </tr></tbody>
4126 </table>
4127 </p>
4129 <font size="-1"><p>
4130 <a id="code-label-test:mode-definitions-1"></a>
4131 <table style="width: 100%" id="code-ref-test:mode-definitions-1">
4132 <tbody><tr>
4133 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:mode-definitions"></a><font size="-1"><div class="left-tab">
4134 <font color="blue">test:mode-definitions</font>[1](), lang=<font
4135 color="blue"></font> &equiv;
4136 </div><div class="right-tab">
4137 <a href="#code-ref-test:mode-definitions-2">44g</a>&#x22B3;
4138 </div></font></td>
4139 </tr></tbody>
4140 </table>
4141 </p><p>
4143 <tt class="verbatim"><div class="compact-block">
4144 <tt>1 </tt>parse_chunk_args(&quot;c-like&quot;,
4145 &quot;1,2,3&quot;, a, &quot;&quot;);
4146 </div></tt>
4147 </p>
4148 <pre class="verbatim" xml:space="preserve">
4149 <div class="compact-block"><tt>2 </tt>if (a[1] != &quot;1&quot;) e++;
4150 <tt>3 </tt>if (a[2] != &quot;2&quot;) e++;
4151 <tt>4 </tt>if (a[3] != &quot;3&quot;) e++;
4152 <tt>5 </tt>if (length(a) != 3) e++;
4153 <tt>6 </tt>=&lt;\chunkref{pca-test.awk:summary}&gt;
4154 <tt>7 </tt>
4155 <tt>8 </tt>parse_chunk_args(&quot;c-like&quot;, &quot;joe, red&quot;, a, &quot;&quot;);
4156 <tt>9 </tt>if (a[1] != &quot;joe&quot;) e++;
4157 <tt>10 </tt>if (a[2] != &quot;red&quot;) e++;
4158 <tt>11 </tt>if (length(a) != 2) e++;
4159 <tt>12 </tt>=&lt;\chunkref{pca-test.awk:summary}&gt;
4160 <tt>13 </tt>
4161 <tt>14 </tt>parse_chunk_args(&quot;c-like&quot;, &quot;${colour}&quot;, a, &quot;&quot;);
4162 <tt>15 </tt>if (a[1] != &quot;${colour}&quot;) e++;
4163 <tt>16 </tt>if (length(a) != 1) e++;</div></pre>
4165 <tt class="verbatim"><div class="compact-block">
4166 <tt>17 </tt>=&lt;\chunkref{pca-test.awk:summary}&gt;
4167 </div></tt>
4168 </p>
4169 </p><p>
4170 <a id="code-end-test:mode-definitions-1"></a>
4171 </p></font>
4172 </p>
4174 Nested modes are identified by a backslash, a double or single quote,
4175 various bracket styles or a <tt class="verbatim">/*</tt> comment.
4176 </p>
4178 For each of these sub-modes modes we must also identify at a mode
4179 terminator, and any sub-modes or delimiters that may be entered
4181 <font size="-1"><div align="justify">
4182 <div style="margin-left: 0px">
4183 <div style="margin-right: 0px">
4185 . Because we are using the sub-mode characters as the mode
4186 identifier it means we can't currently have a mode character
4187 dependant on it's context; i.e. <tt class="verbatim">{</tt> can't behave
4188 differently when it is inside <tt class="verbatim">[</tt>.
4189 </p>
4190 </div>
4191 </div>
4192 </div></font>
4193 </p>
4194 <span style="margin-left: 0em"></span>
4195 <a id="footnr-2"></a>
4196 <sup><a href="#footnote-2">2</a></sup>
4198 </p>
4199 <h3 id="auto-51">9.2.1<span style="margin-left: 1em"></span>Backslash</h3>
4201 The backslash mode has no submodes or delimiters, and is terminated by
4202 any character. Note that we are not so much interested in evaluating or
4203 interpolating content as we are in delineating content. It is no matter
4204 that a double backslash (<tt class="verbatim">\\</tt>) may represent a single
4205 backslash while a backslash-newline may represent white space, but it
4206 does matter that the newline in a backslash newline should not be able
4207 to terminate a C pre-processor statement; and so the newline will be
4208 consumed by the backslash however it is to be interpreted.
4209 </p>
4211 <font size="-1"><p>
4212 <a id="code-label-common-mode-definitions-3"></a>
4213 <table style="width: 100%" id="code-ref-common-mode-definitions-3">
4214 <tbody><tr>
4215 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="common-mode-definitions"></a><font size="-1"><div class="left-tab">
4216 <font color="blue">common-mode-definitions</font>[3](language)
4217 &uArr;<a href="#code-ref-common-mode-definitions-1">43a</a>, lang=<font color="blue"></font> + &equiv;
4218 </div><div class="right-tab">
4219 &#x25B5;<a href="#code-ref-common-mode-definitions-2">43b</a> <a href="#code-ref-common-mode-definitions-4">44f</a>&#x22B3;
4220 </div></font></td>
4221 </tr></tbody>
4222 </table>
4223 </p><p>
4224 <tt class="verbatim"><div class="compact-block">
4225 <tt>3 </tt>modes[${language}, &quot;\\&quot;,
4226 &quot;terminators&quot;]=&quot;.&quot;;
4227 </div></tt>
4228 </p><p>
4229 <a id="code-end-common-mode-definitions-3"></a>
4230 </p></font>
4231 </p>
4232 <h3 id="auto-52">9.2.2<span style="margin-left: 1em"></span>Strings</h3>
4234 Common languages support two kinds of strings quoting, double quotes and
4235 single quotes.
4236 </p>
4238 In a string we have one special mode, which is the backslash. This may
4239 escape an embedded quote and prevent us thinking that it should
4240 terminate the string.
4241 </p>
4243 <font size="-1"><p>
4244 <a id="code-label-mode:common-string-1"></a>
4245 <table style="width: 100%" id="code-ref-mode:common-string-1">
4246 <tbody><tr>
4247 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:common-string"></a><font size="-1"><div class="left-tab">
4248 <font color="blue">mode:common-string</font>[1](<font color="blue">language</font>,
4249 <font color="blue">quote</font>), lang=<font color="blue"></font> &equiv;
4250 </div><div class="right-tab">
4251 <a href="#code-ref-mode:common-string-2">44b</a>&#x25BF;
4252 </div></font></td>
4253 </tr></tbody>
4254 </table>
4255 </p><p>
4256 <tt class="verbatim"><div class="compact-block">
4257 <tt>1 </tt>modes[${language}, ${quote},
4258 &quot;submodes&quot;]=&quot;\\\\&quot;;
4259 </div></tt>
4260 </p><p>
4261 <a id="code-end-mode:common-string-1"></a>
4262 </p></font>
4263 </p>
4265 Otherwise, the string will be terminated by the same character that
4266 commenced it.
4267 </p>
4269 <font size="-1"><p>
4270 <a id="code-label-mode:common-string-2"></a>
4271 <table style="width: 100%" id="code-ref-mode:common-string-2">
4272 <tbody><tr>
4273 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:common-string"></a><font size="-1"><div class="left-tab">
4274 <font color="blue">mode:common-string</font>[2](language, quote)
4275 &uArr;<a href="#code-ref-mode:common-string-1">44a</a>, lang=<font color="blue"></font> + &equiv;
4276 </div><div class="right-tab">
4277 &#x25B5;<a href="#code-ref-mode:common-string-1">44a</a> <a href="#code-ref-mode:common-string-3">44c</a>&#x25BF;
4278 </div></font></td>
4279 </tr></tbody>
4280 </table>
4281 </p><p>
4282 <tt class="verbatim"><div class="compact-block">
4283 <tt>2 </tt>modes[${language}, ${quote},
4284 &quot;terminators&quot;]=${quote};
4285 </div></tt>
4286 </p><p>
4287 <a id="code-end-mode:common-string-2"></a>
4288 </p></font>
4289 </p>
4291 In C type languages, certain escape sequences exist in strings. We need
4292 to define mechanism to enclode any chunks included in this mode using
4293 those escape sequences. These are expressed in two parts, s meaning
4294 search, and r meaning replace.
4295 </p>
4297 The first substitution is to replace a backslash with a double
4298 backslash. We do this first as other substitutions may introduce a
4299 backslash which we would not then want to escape again here.
4300 </p>
4302 Note: Backslashes need double-escaping in the search pattern but not in
4303 the replacement string, hence we are replacing a literal <tt class="verbatim">\</tt>
4304 with a literal <tt class="verbatim">\\</tt>.
4305 </p>
4307 <font size="-1"><p>
4308 <a id="code-label-mode:common-string-3"></a>
4309 <table style="width: 100%" id="code-ref-mode:common-string-3">
4310 <tbody><tr>
4311 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:common-string"></a><font size="-1"><div class="left-tab">
4312 <font color="blue">mode:common-string</font>[3](language, quote)
4313 &uArr;<a href="#code-ref-mode:common-string-1">44a</a>, lang=<font color="blue"></font> + &equiv;
4314 </div><div class="right-tab">
4315 &#x25B5;<a href="#code-ref-mode:common-string-2">44b</a> <a href="#code-ref-mode:common-string-4">44d</a>&#x25BF;
4316 </div></font></td>
4317 </tr></tbody>
4318 </table>
4319 </p><p>
4321 <tt class="verbatim"><div class="compact-block">
4322 <tt>3 </tt>escapes[${language}, ${quote}, ++escapes[${language},
4323 ${quote}], &quot;s&quot;]=&quot;\\\\&quot;;
4324 </div></tt>
4325 </p>
4326 <pre class="verbatim" xml:space="preserve">
4327 <div class="compact-block"></div></pre>
4329 <tt class="verbatim"><div class="compact-block">
4330 <tt>4 </tt>escapes[${language}, ${quote}, escapes[${language},
4331 ${quote}], &quot;r&quot;]=&quot;\\\\&quot;;
4332 </div></tt>
4333 </p>
4334 </p><p>
4335 <a id="code-end-mode:common-string-3"></a>
4336 </p></font>
4337 </p>
4339 If the quote character occurs in the text, it should be preceded by a
4340 backslash, otherwise it would terminate the string unexpectedly.
4341 </p>
4343 <font size="-1"><p>
4344 <a id="code-label-mode:common-string-4"></a>
4345 <table style="width: 100%" id="code-ref-mode:common-string-4">
4346 <tbody><tr>
4347 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:common-string"></a><font size="-1"><div class="left-tab">
4348 <font color="blue">mode:common-string</font>[4](language, quote)
4349 &uArr;<a href="#code-ref-mode:common-string-1">44a</a>, lang=<font color="blue"></font> + &equiv;
4350 </div><div class="right-tab">
4351 &#x25B5;<a href="#code-ref-mode:common-string-3">44c</a> <a href="#code-ref-mode:common-string-5">44e</a>&#x25BF;
4352 </div></font></td>
4353 </tr></tbody>
4354 </table>
4355 </p><p>
4357 <tt class="verbatim"><div class="compact-block">
4358 <tt>5 </tt>escapes[${language}, ${quote}, ++escapes[${language},
4359 ${quote}], &quot;s&quot;]=${quote};
4360 </div></tt>
4361 </p>
4362 <pre class="verbatim" xml:space="preserve">
4363 <div class="compact-block"></div></pre>
4365 <tt class="verbatim"><div class="compact-block">
4366 <tt>6 </tt>escapes[${language}, ${quote}, escapes[${language},
4367 ${quote}], &quot;r&quot;]=&quot;\\&quot; ${quote};
4368 </div></tt>
4369 </p>
4370 </p><p>
4371 <a id="code-end-mode:common-string-4"></a>
4372 </p></font>
4373 </p>
4375 Any newlines in the string, must be replaced by <tt class="verbatim">\n</tt>.
4376 </p>
4378 <font size="-1"><p>
4379 <a id="code-label-mode:common-string-5"></a>
4380 <table style="width: 100%" id="code-ref-mode:common-string-5">
4381 <tbody><tr>
4382 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:common-string"></a><font size="-1"><div class="left-tab">
4383 <font color="blue">mode:common-string</font>[5](language, quote)
4384 &uArr;<a href="#code-ref-mode:common-string-1">44a</a>, lang=<font color="blue"></font> + &equiv;
4385 </div><div class="right-tab">
4386 &#x25B5;<a href="#code-ref-mode:common-string-4">44d</a>
4387 </div></font></td>
4388 </tr></tbody>
4389 </table>
4390 </p><p>
4392 <tt class="verbatim"><div class="compact-block">
4393 <tt>7 </tt>escapes[${language}, ${quote}, ++escapes[${language},
4394 ${quote}], &quot;s&quot;]=&quot;\n&quot;;
4395 </div></tt>
4396 </p>
4397 <pre class="verbatim" xml:space="preserve">
4398 <div class="compact-block"></div></pre>
4400 <tt class="verbatim"><div class="compact-block">
4401 <tt>8 </tt>escapes[${language}, ${quote}, escapes[${language},
4402 ${quote}], &quot;r&quot;]=&quot;\\n&quot;;
4403 </div></tt>
4404 </p>
4405 </p><table style="width: 100%" id="code-end-mode:common-string-5">
4406 <tbody><tr>
4407 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4408 </tr></tbody>
4409 </table></font>
4410 </p>
4412 For the common modes, we define this string handling for double and
4413 single quotes.
4414 </p>
4416 <font size="-1"><p>
4417 <a id="code-label-common-mode-definitions-4"></a>
4418 <table style="width: 100%" id="code-ref-common-mode-definitions-4">
4419 <tbody><tr>
4420 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="common-mode-definitions"></a><font size="-1"><div class="left-tab">
4421 <font color="blue">common-mode-definitions</font>[4](language)
4422 &uArr;<a href="#code-ref-common-mode-definitions-1">43a</a>, lang=<font color="blue"></font> + &equiv;
4423 </div><div class="right-tab">
4424 &#x22B2;<a href="#code-ref-common-mode-definitions-3">43d</a> <a href="#code-ref-common-mode-definitions-5">45b</a>&#x22B3;
4425 </div></font></td>
4426 </tr></tbody>
4427 </table>
4428 </p><p>
4430 <tt class="verbatim"><div class="compact-block">
4431 <tt>4 </tt>=&lt;\chunkref{mode:common-string}(${language},
4432 &quot;\textbackslash{}&quot;&quot;)&gt;
4433 </div></tt>
4434 </p>
4435 <pre class="verbatim" xml:space="preserve">
4436 <div class="compact-block"></div></pre>
4438 <tt class="verbatim"><div class="compact-block">
4439 <tt>5 </tt>=&lt;\chunkref{mode:common-string}(${language},
4440 &quot;'&quot;)&gt;
4441 </div></tt>
4442 </p>
4443 </p><p>
4444 <a id="code-end-common-mode-definitions-4"></a>
4445 </p></font>
4446 </p>
4448 Working strings should pass this test:
4449 </p>
4451 <font size="-1"><p>
4452 <a id="code-label-test:mode-definitions-2"></a>
4453 <table style="width: 100%" id="code-ref-test:mode-definitions-2">
4454 <tbody><tr>
4455 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:mode-definitions"></a><font size="-1"><div class="left-tab">
4456 <font color="blue">test:mode-definitions</font>[2]() &uArr;<a href="#code-ref-test:mode-definitions-1">43c</a>,
4457 lang=<font color="blue"></font> + &equiv;
4458 </div><div class="right-tab">
4459 &#x22B2;<a href="#code-ref-test:mode-definitions-1">43c</a> <a href="#code-ref-test:mode-definitions-3">47d</a>&#x22B3;
4460 </div></font></td>
4461 </tr></tbody>
4462 </table>
4463 </p><p>
4465 <tt class="verbatim"><div class="compact-block">
4466 <tt>18 </tt>parse_chunk_args(&quot;c-like&quot;, &quot;say
4467 \&quot;I said, \\\&quot;Hello, how are you\\\&quot;.\&quot;, for
4468 me&quot;, a, &quot;&quot;);
4469 </div></tt>
4470 </p>
4471 <pre class="verbatim" xml:space="preserve">
4472 <div class="compact-block"><tt>19 </tt>if (a[1] != &quot;say \&quot;I said, \\\&quot;Hello, how are you\\\&quot;.\&quot;&quot;) e++;
4473 <tt>20 </tt>if (a[2] != &quot;for me&quot;) e++;
4474 <tt>21 </tt>if (length(a) != 2) e++;</div></pre>
4476 <tt class="verbatim"><div class="compact-block">
4477 <tt>22 </tt>=&lt;\chunkref{pca-test.awk:summary}&gt;
4478 </div></tt>
4479 </p>
4480 </p><p>
4481 <a id="code-end-test:mode-definitions-2"></a>
4482 </p></font>
4483 </p>
4484 <h3 id="auto-53">9.2.3<span style="margin-left: 1em"></span>Parentheses, Braces and Brackets</h3>
4486 Where quotes are closed by the same character, parentheses, brackets and
4487 braces are closed by an alternate character.
4488 </p>
4490 <font size="-1"><p>
4491 <a id="code-label-mode:common-brackets-1"></a>
4492 <table style="width: 100%" id="code-ref-mode:common-brackets-1">
4493 <tbody><tr>
4494 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:common-brackets"></a><font size="-1"><font color="blue">mode:common-brackets</font>[1](<font
4495 color="blue">language</font>, <font color="blue">open</font>, <font color="blue">close</font>),
4496 lang=<font color="blue"></font> &equiv;</font></td>
4497 </tr></tbody>
4498 </table>
4499 </p><p>
4501 <tt class="verbatim"><div class="compact-block">
4502 <tt>1 </tt>modes[<font color="#008000">language</font>, <font color="#008000">open</font>,
4503 &quot;submodes&quot; ]=&quot;\\\\|\&quot;|{|\\(|\\[|'|/\\*&quot;;
4504 </div></tt>
4505 </p>
4506 <pre class="verbatim" xml:space="preserve">
4507 <div class="compact-block"><tt>2 </tt>modes[<font color="#008000">language</font>, <font color="#008000">open</font>, &quot;delimiters&quot;]=&quot; *, *&quot;;</div></pre>
4509 <tt class="verbatim"><div class="compact-block">
4510 <tt>3 </tt>modes[<font color="#008000">language</font>, <font color="#008000">open</font>,
4511 &quot;terminators&quot;]=<font color="#008000">close</font>;
4512 </div></tt>
4513 </p>
4514 </p><table style="width: 100%" id="code-end-mode:common-brackets-1">
4515 <tbody><tr>
4516 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4517 </tr></tbody>
4518 </table></font>
4519 </p>
4521 Note that the open is NOT a regex but the close token IS.
4522 <table style="display: inline; vertical-align: -0.55em">
4523 <tbody><tr>
4524 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: When we can quote regex we won't have to
4525 put the slashes in here</td>
4526 </tr></tbody>
4527 </table>
4528 </p>
4530 <font size="-1"><p>
4531 <a id="code-label-common-mode-definitions-5"></a>
4532 <table style="width: 100%" id="code-ref-common-mode-definitions-5">
4533 <tbody><tr>
4534 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="common-mode-definitions"></a><font size="-1"><div class="left-tab">
4535 <font color="blue">common-mode-definitions</font>[5](language)
4536 &uArr;<a href="#code-ref-common-mode-definitions-1">43a</a>, lang=<font color="blue"></font> + &equiv;
4537 </div><div class="right-tab">
4538 &#x22B2;<a href="#code-ref-common-mode-definitions-4">44f</a>
4539 </div></font></td>
4540 </tr></tbody>
4541 </table>
4542 </p><p>
4544 <tt class="verbatim"><div class="compact-block">
4545 <tt>6 </tt>=&lt;\chunkref{mode:common-brackets}(${language},
4546 &quot;{&quot;, &quot;}&quot;)&gt;
4547 </div></tt>
4548 </p>
4549 <pre class="verbatim" xml:space="preserve">
4550 <div class="compact-block"><tt>7 </tt>=&lt;\chunkref{mode:common-brackets}(${language}, &quot;[&quot;, &quot;\textbackslash{}\textbackslash{}]&quot;)&gt;</div></pre>
4552 <tt class="verbatim"><div class="compact-block">
4553 <tt>8 </tt>=&lt;\chunkref{mode:common-brackets}(${language},
4554 &quot;(&quot;, &quot;\textbackslash{}\textbackslash{})&quot;)&gt;
4555 </div></tt>
4556 </p>
4557 </p><table style="width: 100%" id="code-end-common-mode-definitions-5">
4558 <tbody><tr>
4559 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4560 </tr></tbody>
4561 </table></font>
4562 </p>
4563 <h3 id="auto-54">9.2.4<span style="margin-left: 1em"></span>Customizing Standard Modes</h3>
4565 <font size="-1"><p>
4566 <a id="code-label-mode:add-submode-1"></a>
4567 <table style="width: 100%" id="code-ref-mode:add-submode-1">
4568 <tbody><tr>
4569 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:add-submode"></a><font size="-1"><font color="blue">mode:add-submode</font>[1](<font
4570 color="blue">language</font>, <font color="blue">mode</font>, <font color="blue">submode</font>),
4571 lang=<font color="blue"></font> &equiv;</font></td>
4572 </tr></tbody>
4573 </table>
4574 </p><p>
4575 <tt class="verbatim"><div class="compact-block">
4576 <tt>1 </tt>modes[${language}, ${mode}, &quot;submodes&quot;] =
4577 modes[${language}, ${mode}, &quot;submodes&quot;] &quot;|&quot;
4578 ${submode};
4579 </div></tt>
4580 </p><table style="width: 100%" id="code-end-mode:add-submode-1">
4581 <tbody><tr>
4582 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4583 </tr></tbody>
4584 </table></font>
4585 </p>
4587 <font size="-1"><p>
4588 <a id="code-label-mode:add-escapes-1"></a>
4589 <table style="width: 100%" id="code-ref-mode:add-escapes-1">
4590 <tbody><tr>
4591 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:add-escapes"></a><font size="-1"><font color="blue">mode:add-escapes</font>[1](<font
4592 color="blue">language</font>, <font color="blue">mode</font>, <font color="blue">search</font>,
4593 <font color="blue">replace</font>), lang=<font color="blue"></font>
4594 &equiv;</font></td>
4595 </tr></tbody>
4596 </table>
4597 </p><p>
4599 <tt class="verbatim"><div class="compact-block">
4600 <tt>1 </tt>escapes[${language}, ${mode}, ++escapes[${language},
4601 ${mode}], &quot;s&quot;]=${search};
4602 </div></tt>
4603 </p>
4604 <pre class="verbatim" xml:space="preserve">
4605 <div class="compact-block"></div></pre>
4607 <tt class="verbatim"><div class="compact-block">
4608 <tt>2 </tt>escapes[${language}, ${mode}, escapes[${language},
4609 ${mode}], &quot;r&quot;]=${replace};
4610 </div></tt>
4611 </p>
4612 </p><table style="width: 100%" id="code-end-mode:add-escapes-1">
4613 <tbody><tr>
4614 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4615 </tr></tbody>
4616 </table></font>
4617 </p>
4620 </p>
4621 <h3 id="auto-55">9.2.5<span style="margin-left: 1em"></span>Comments</h3>
4623 We can define <tt class="verbatim">/* comment */</tt> style comments and <tt
4624 class="verbatim">//comment</tt> style comments to be added to any language:
4625 </p>
4627 <font size="-1"><p>
4628 <a id="code-label-mode:multi-line-comments-1"></a>
4629 <table style="width: 100%" id="code-ref-mode:multi-line-comments-1">
4630 <tbody><tr>
4631 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:multi-line-comments"></a><font size="-1"><font color="blue">mode:multi-line-comments</font>[1](<font
4632 color="blue">language</font>), lang=<font color="blue"></font>
4633 &equiv;</font></td>
4634 </tr></tbody>
4635 </table>
4636 </p><p>
4638 <tt class="verbatim"><div class="compact-block">
4639 <tt>1 </tt>=&lt;\chunkref{mode:add-submode}(${language},
4640 &quot;&quot;, &quot;/\textbackslash{}\textbackslash{}*&quot;)&gt;
4641 </div></tt>
4642 </p>
4643 <pre class="verbatim" xml:space="preserve">
4644 <div class="compact-block"></div></pre>
4646 <tt class="verbatim"><div class="compact-block">
4647 <tt>2 </tt>modes[${language}, &quot;/*&quot;,
4648 &quot;terminators&quot;]=&quot;\\*/&quot;;
4649 </div></tt>
4650 </p>
4651 </p><table style="width: 100%" id="code-end-mode:multi-line-comments-1">
4652 <tbody><tr>
4653 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4654 </tr></tbody>
4655 </table></font>
4656 </p>
4658 <font size="-1"><p>
4659 <a id="code-label-mode:single-line-slash-comments-1"></a>
4660 <table style="width: 100%" id="code-ref-mode:single-line-slash-comments-1">
4661 <tbody><tr>
4662 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:single-line-slash-comments"></a><font size="-1"><font color="blue">mode:single-line-slash-comments</font>[1](language),
4663 lang=<font color="blue"></font> &equiv;</font></td>
4664 </tr></tbody>
4665 </table>
4666 </p><p>
4668 <tt class="verbatim"><div class="compact-block">
4669 <tt>1 </tt>=&lt;\chunkref{mode:add-submode}(${language},
4670 &quot;&quot;, &quot;//&quot;)&gt;
4671 </div></tt>
4672 </p>
4673 <pre class="verbatim" xml:space="preserve">
4674 <div class="compact-block"><tt>2 </tt>modes[${language}, &quot;//&quot;, &quot;terminators&quot;]=&quot;\n&quot;;</div></pre>
4676 <tt class="verbatim"><div class="compact-block">
4677 <tt>3 </tt>=&lt;\chunkref{mode:add-escapes}(${language},
4678 &quot;//&quot;, &quot;\textbackslash{}n&quot;,
4679 &quot;\textbackslash{}n//&quot;)&gt;
4680 </div></tt>
4681 </p>
4682 </p><table style="width: 100%" id="code-end-mode:single-line-slash-comments-1">
4683 <tbody><tr>
4684 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4685 </tr></tbody>
4686 </table></font>
4687 </p>
4689 We can also define <tt class="verbatim"># comment</tt> style comments (as used in
4690 awk and shell scripts) in a similar manner.
4691 </p>
4692 <table style="display: inline; vertical-align: -0.55em">
4693 <tbody><tr>
4694 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: I'm having to use # for hash and
4695 &macr;extbackslash{} for and have hacky work-arounds in the parser
4696 for now</td>
4697 </tr></tbody>
4698 </table>
4700 <font size="-1"><p>
4701 <a id="code-label-mode:add-hash-comments-1"></a>
4702 <table style="width: 100%" id="code-ref-mode:add-hash-comments-1">
4703 <tbody><tr>
4704 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:add-hash-comments"></a><font size="-1"><font color="blue">mode:add-hash-comments</font>[1](<font
4705 color="blue">language</font>), lang=<font color="blue"></font>
4706 &equiv;</font></td>
4707 </tr></tbody>
4708 </table>
4709 </p><p>
4711 <tt class="verbatim"><div class="compact-block">
4712 <tt>1 </tt>=&lt;\chunkref{mode:add-submode}(${language},
4713 &quot;&quot;, &quot;\#&quot;)&gt;
4714 </div></tt>
4715 </p>
4716 <pre class="verbatim" xml:space="preserve">
4717 <div class="compact-block"><tt>2 </tt>modes[${language}, &quot;#&quot;, &quot;terminators&quot;]=&quot;\n&quot;;</div></pre>
4719 <tt class="verbatim"><div class="compact-block">
4720 <tt>3 </tt>=&lt;\chunkref{mode:add-escapes}(${language},
4721 &quot;\#&quot;, &quot;\textbackslash{}n&quot;,
4722 &quot;\textbackslash{}n\#&quot;)&gt;
4723 </div></tt>
4724 </p>
4725 </p><table style="width: 100%" id="code-end-mode:add-hash-comments-1">
4726 <tbody><tr>
4727 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4728 </tr></tbody>
4729 </table></font>
4730 </p>
4732 In C, the <tt class="verbatim">#</tt> denotes pre-processor directives which can
4733 be multi-line
4734 </p>
4736 <font size="-1"><p>
4737 <a id="code-label-mode:add-hash-defines-1"></a>
4738 <table style="width: 100%" id="code-ref-mode:add-hash-defines-1">
4739 <tbody><tr>
4740 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:add-hash-defines"></a><font size="-1"><font color="blue">mode:add-hash-defines</font>[1](<font
4741 color="blue">language</font>), lang=<font color="blue"></font>
4742 &equiv;</font></td>
4743 </tr></tbody>
4744 </table>
4745 </p><p>
4747 <tt class="verbatim"><div class="compact-block">
4748 <tt>1 </tt>=&lt;\chunkref{mode:add-submode}(${language},
4749 &quot;&quot;, &quot;\#&quot;)&gt;
4750 </div></tt>
4751 </p>
4752 <pre class="verbatim" xml:space="preserve">
4753 <div class="compact-block"><tt>2 </tt>modes[${language}, &quot;#&quot;, &quot;submodes&quot; ]=&quot;\\\\&quot;;
4754 <tt>3 </tt>modes[${language}, &quot;#&quot;, &quot;terminators&quot;]=&quot;\n&quot;;</div></pre>
4756 <tt class="verbatim"><div class="compact-block">
4757 <tt>4 </tt>=&lt;\chunkref{mode:add-escapes}(${language},
4758 &quot;\#&quot;, &quot;\textbackslash{}n&quot;,
4759 &quot;\textbackslash{}\textbackslash{}\textbackslash{}\textbackslash{}\textbackslash{}n&quot;)&gt;
4760 </div></tt>
4761 </p>
4762 </p><table style="width: 100%" id="code-end-mode:add-hash-defines-1">
4763 <tbody><tr>
4764 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4765 </tr></tbody>
4766 </table></font>
4767 </p>
4769 <font size="-1"><p>
4770 <a id="code-label-mode:quote-dollar-escape-1"></a>
4771 <table style="width: 100%" id="code-ref-mode:quote-dollar-escape-1">
4772 <tbody><tr>
4773 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:quote-dollar-escape"></a><font size="-1"><font color="blue">mode:quote-dollar-escape</font>[1](<font
4774 color="blue">language</font>, <font color="blue">quote</font>), lang=<font
4775 color="blue"></font> &equiv;</font></td>
4776 </tr></tbody>
4777 </table>
4778 </p><p>
4780 <tt class="verbatim"><div class="compact-block">
4781 <tt>1 </tt>escapes[${language}, ${quote}, ++escapes[${language},
4782 ${quote}], &quot;s&quot;]=&quot;\\$&quot;;
4783 </div></tt>
4784 </p>
4785 <pre class="verbatim" xml:space="preserve">
4786 <div class="compact-block"></div></pre>
4788 <tt class="verbatim"><div class="compact-block">
4789 <tt>2 </tt>escapes[${language}, ${quote}, escapes[${language},
4790 ${quote}], &quot;r&quot;]=&quot;\\$&quot;;
4791 </div></tt>
4792 </p>
4793 </p><table style="width: 100%" id="code-end-mode:quote-dollar-escape-1">
4794 <tbody><tr>
4795 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4796 </tr></tbody>
4797 </table></font>
4798 </p>
4800 We can add these definitions to various languages
4801 </p>
4803 <font size="-1"><p>
4804 <a id="code-label-mode-definitions-1"></a>
4805 <table style="width: 100%" id="code-ref-mode-definitions-1">
4806 <tbody><tr>
4807 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode-definitions"></a><font size="-1"><div class="left-tab">
4808 <font color="blue">mode-definitions</font>[1](), lang=<font color="blue"></font>
4809 &equiv;
4810 </div><div class="right-tab">
4811 <a href="#code-ref-mode-definitions-2">47b</a>&#x22B3;
4812 </div></font></td>
4813 </tr></tbody>
4814 </table>
4815 </p><p>
4817 <tt class="verbatim"><div class="compact-block">
4818 <tt>1 </tt>common-mode-definitions(<font color="blue">&quot;c-like&quot;</font>)
4819 <a href="#code-ref-common-mode-definitions-1">43a</a>
4820 </div></tt>
4821 </p>
4822 <pre class="verbatim" xml:space="preserve">
4823 <div class="compact-block"><tt>2 </tt>
4824 <tt>3 </tt>common-mode-definitions(<font color="blue">&quot;c&quot;</font>) <a href="#code-ref-common-mode-definitions-1">43a</a>
4825 <tt>4 </tt>=&lt;\chunkref{mode:multi-line-comments}(&quot;c&quot;)&gt;
4826 <tt>5 </tt>=&lt;\chunkref{mode:single-line-slash-comments}(&quot;c&quot;)&gt;
4827 <tt>6 </tt>=&lt;\chunkref{mode:add-hash-defines}(&quot;c&quot;)&gt;
4828 <tt>7 </tt>
4829 <tt>8 </tt>=&lt;\chunkref{common-mode-definitions}(&quot;awk&quot;)&gt;
4830 <tt>9 </tt>=&lt;\chunkref{mode:add-hash-comments}(&quot;awk&quot;)&gt;</div></pre>
4832 <tt class="verbatim"><div class="compact-block">
4833 <tt>10
4834 </tt>=&lt;\chunkref{mode:add-naked-regex}(&quot;awk&quot;)&gt;
4835 </div></tt>
4836 </p>
4837 </p><p>
4838 <a id="code-end-mode-definitions-1"></a>
4839 </p></font>
4840 </p>
4842 The awk definitions should allow a comment block like this:
4843 </p>
4845 <font size="-1"><p>
4846 <a id="code-label-test:comment-quote-1"></a>
4847 <table style="width: 100%" id="code-ref-test:comment-quote-1">
4848 <tbody><tr>
4849 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:comment-quote"></a><font size="-1"><font color="blue">test:comment-quote</font>[1](),
4850 lang=<font color="blue">awk</font> &equiv;</font></td>
4851 </tr></tbody>
4852 </table>
4853 </p><p>
4854 <tt class="verbatim"><div class="compact-block">
4855 <tt>1 </tt># Comment: =&lt;\chunkref{test:comment-text}&gt;
4856 </div></tt>
4857 </p><table style="width: 100%" id="code-end-test:comment-quote-1">
4858 <tbody><tr>
4859 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4860 </tr></tbody>
4861 </table></font>
4862 </p>
4864 <font size="-1"><p>
4865 <a id="code-label-test:comment-text-1"></a>
4866 <table style="width: 100%" id="code-ref-test:comment-text-1">
4867 <tbody><tr>
4868 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:comment-text"></a><font size="-1"><font color="blue">test:comment-text</font>[1](),
4869 lang=<font color="blue"></font> &equiv;</font></td>
4870 </tr></tbody>
4871 </table>
4872 </p><p>
4874 <tt class="verbatim"><div class="compact-block">
4875 <tt>1 </tt>Now is the time for
4876 </div></tt>
4877 </p>
4878 <pre class="verbatim" xml:space="preserve">
4879 <div class="compact-block"><tt>2 </tt>the quick brown fox to bring lemonade</div></pre>
4881 <tt class="verbatim"><div class="compact-block">
4882 <tt>3 </tt>to the party
4883 </div></tt>
4884 </p>
4885 </p><table style="width: 100%" id="code-end-test:comment-text-1">
4886 <tbody><tr>
4887 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4888 </tr></tbody>
4889 </table></font>
4890 </p>
4892 to come out like this:
4893 </p>
4895 <font size="-1"><p>
4896 <a id="code-label-test:comment-quote:result-1"></a>
4897 <table style="width: 100%" id="code-ref-test:comment-quote:result-1">
4898 <tbody><tr>
4899 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:comment-quote:result"></a><font size="-1"><font color="blue">test:comment-quote:result</font>[1](),
4900 lang=<font color="blue"></font> &equiv;</font></td>
4901 </tr></tbody>
4902 </table>
4903 </p><p>
4905 <tt class="verbatim"><div class="compact-block">
4906 <tt>1 </tt># Comment: Now is the time for
4907 </div></tt>
4908 </p>
4909 <pre class="verbatim" xml:space="preserve">
4910 <div class="compact-block"><tt>2 </tt>#the quick brown fox to bring lemonade</div></pre>
4912 <tt class="verbatim"><div class="compact-block">
4913 <tt>3 </tt>#to the party
4914 </div></tt>
4915 </p>
4916 </p><table style="width: 100%" id="code-end-test:comment-quote:result-1">
4917 <tbody><tr>
4918 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4919 </tr></tbody>
4920 </table></font>
4921 </p>
4923 The C definition for such a block should have it come out like this:
4924 </p>
4926 <font size="-1"><p>
4927 <a id="code-label-test:comment-quote:C-result-1"></a>
4928 <table style="width: 100%" id="code-ref-test:comment-quote:C-result-1">
4929 <tbody><tr>
4930 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:comment-quote:C-result"></a><font size="-1"><font color="blue">test:comment-quote:C-result</font>[1](),
4931 lang=<font color="blue"></font> &equiv;</font></td>
4932 </tr></tbody>
4933 </table>
4934 </p><p>
4936 <tt class="verbatim"><div class="compact-block">
4937 <tt>1 </tt># Comment: Now is the time for\
4938 </div></tt>
4939 </p>
4940 <pre class="verbatim" xml:space="preserve">
4941 <div class="compact-block"><tt>2 </tt>the quick brown fox to bring lemonade\</div></pre>
4943 <tt class="verbatim"><div class="compact-block">
4944 <tt>3 </tt>to the party
4945 </div></tt>
4946 </p>
4947 </p><table style="width: 100%" id="code-end-test:comment-quote:C-result-1">
4948 <tbody><tr>
4949 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4950 </tr></tbody>
4951 </table></font>
4952 </p>
4953 <h3 id="auto-56">9.2.6<span style="margin-left: 1em"></span>Regex</h3>
4955 This pattern is incomplete, but meant to detect naked regular
4956 expressions in awk and perl; e.g. <tt class="verbatim">/.*$/</tt>, however
4957 required capabilities are not present.
4958 </p>
4960 Current it only detects regexes anchored with ^ as used in fangle.
4961 </p>
4963 For full regex support, modes need to be named not after their starting
4964 character, but some other more fully qualified name.
4965 </p>
4967 <font size="-1"><p>
4968 <a id="code-label-mode:add-naked-regex-1"></a>
4969 <table style="width: 100%" id="code-ref-mode:add-naked-regex-1">
4970 <tbody><tr>
4971 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode:add-naked-regex"></a><font size="-1"><font color="blue">mode:add-naked-regex</font>[1](<font
4972 color="blue">language</font>), lang=<font color="blue"></font>
4973 &equiv;</font></td>
4974 </tr></tbody>
4975 </table>
4976 </p><p>
4978 <tt class="verbatim"><div class="compact-block">
4979 <tt>1 </tt>=&lt;\chunkref{mode:add-submode}(${language},
4980 &quot;&quot;, &quot;/\textbackslash{}\textbackslash{}\^&quot;)&gt;
4981 </div></tt>
4982 </p>
4983 <pre class="verbatim" xml:space="preserve">
4984 <div class="compact-block"></div></pre>
4986 <tt class="verbatim"><div class="compact-block">
4987 <tt>2 </tt>modes[${language}, &quot;/^&quot;,
4988 &quot;terminators&quot;]=&quot;/&quot;;
4989 </div></tt>
4990 </p>
4991 </p><table style="width: 100%" id="code-end-mode:add-naked-regex-1">
4992 <tbody><tr>
4993 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
4994 </tr></tbody>
4995 </table></font>
4996 </p>
4997 <h3 id="auto-57">9.2.7<span style="margin-left: 1em"></span>Perl</h3>
4999 <font size="-1"><p>
5000 <a id="code-label-mode-definitions-2"></a>
5001 <table style="width: 100%" id="code-ref-mode-definitions-2">
5002 <tbody><tr>
5003 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode-definitions"></a><font size="-1"><div class="left-tab">
5004 <font color="blue">mode-definitions</font>[2]() &uArr;<a href="#code-ref-mode-definitions-1">46c</a>,
5005 lang=<font color="blue"></font> + &equiv;
5006 </div><div class="right-tab">
5007 &#x22B2;<a href="#code-ref-mode-definitions-1">46c</a> <a href="#code-ref-mode-definitions-3">47c</a>&#x25BF;
5008 </div></font></td>
5009 </tr></tbody>
5010 </table>
5011 </p><p>
5013 <tt class="verbatim"><div class="compact-block">
5014 <tt>11
5015 </tt>=&lt;\chunkref{common-mode-definitions}(&quot;perl&quot;)&gt;
5016 </div></tt>
5017 </p>
5018 <pre class="verbatim" xml:space="preserve">
5019 <div class="compact-block"><tt>12 </tt>=&lt;\chunkref{mode:multi-line-comments}(&quot;perl&quot;)&gt;</div></pre>
5021 <tt class="verbatim"><div class="compact-block">
5022 <tt>13
5023 </tt>=&lt;\chunkref{mode:add-hash-comments}(&quot;perl&quot;)&gt;
5024 </div></tt>
5025 </p>
5026 </p><p>
5027 <a id="code-end-mode-definitions-2"></a>
5028 </p></font>
5029 </p>
5031 Still need to add add <tt class="verbatim">s/</tt>, submode <tt class="verbatim">/</tt>,
5032 terminate both with <tt class="verbatim">//</tt>. This is likely to be impossible
5033 as perl regexes can contain perl.
5034 </p>
5035 <h3 id="auto-58">9.2.8<span style="margin-left: 1em"></span>sh</h3>
5037 <font size="-1"><p>
5038 <a id="code-label-mode-definitions-3"></a>
5039 <table style="width: 100%" id="code-ref-mode-definitions-3">
5040 <tbody><tr>
5041 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode-definitions"></a><font size="-1"><div class="left-tab">
5042 <font color="blue">mode-definitions</font>[3]() &uArr;<a href="#code-ref-mode-definitions-1">46c</a>,
5043 lang=<font color="blue"></font> + &equiv;
5044 </div><div class="right-tab">
5045 &#x25B5;<a href="#code-ref-mode-definitions-2">47b</a>
5046 </div></font></td>
5047 </tr></tbody>
5048 </table>
5049 </p><p>
5051 <tt class="verbatim"><div class="compact-block">
5052 <tt>14
5053 </tt>=&lt;\chunkref{common-mode-definitions}(&quot;sh&quot;)&gt;
5054 </div></tt>
5055 </p>
5056 <pre class="verbatim" xml:space="preserve">
5057 <div class="compact-block"><tt>15 </tt>#&lt;\chunkref{mode:common-string}(&quot;sh&quot;, &quot;\textbackslash{}&quot;&quot;)&gt;
5058 <tt>16 </tt>#&lt;\chunkref{mode:common-string}(&quot;sh&quot;, &quot;'&quot;)&gt;
5059 <tt>17 </tt>=&lt;\chunkref{mode:add-hash-comments}(&quot;sh&quot;)&gt;</div></pre>
5061 <tt class="verbatim"><div class="compact-block">
5062 <tt>18
5063 </tt>=&lt;\chunkref{mode:quote-dollar-escape}(&quot;sh&quot;,
5064 &quot;\&quot;&quot;)&gt;
5065 </div></tt>
5066 </p>
5067 </p><table style="width: 100%" id="code-end-mode-definitions-3">
5068 <tbody><tr>
5069 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5070 </tr></tbody>
5071 </table></font>
5072 </p>
5073 <h2 id="auto-59">9.3<span style="margin-left: 1em"></span>Some tests</h2>
5075 Also, the parser must return any spare text at the end that has not been
5076 processed due to a mode terminator being found.
5077 </p>
5079 <font size="-1"><p>
5080 <a id="code-label-test:mode-definitions-3"></a>
5081 <table style="width: 100%" id="code-ref-test:mode-definitions-3">
5082 <tbody><tr>
5083 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:mode-definitions"></a><font size="-1"><div class="left-tab">
5084 <font color="blue">test:mode-definitions</font>[3]() &uArr;<a href="#code-ref-test:mode-definitions-1">43c</a>,
5085 lang=<font color="blue"></font> + &equiv;
5086 </div><div class="right-tab">
5087 &#x22B2;<a href="#code-ref-test:mode-definitions-2">44g</a> <a href="#code-ref-test:mode-definitions-4">47e</a>&#x25BF;
5088 </div></font></td>
5089 </tr></tbody>
5090 </table>
5091 </p><p>
5093 <tt class="verbatim"><div class="compact-block">
5094 <tt>23 </tt>rest = parse_chunk_args(&quot;c-like&quot;, &quot;1,
5095 2, 3) spare&quot;, a, &quot;(&quot;);
5096 </div></tt>
5097 </p>
5098 <pre class="verbatim" xml:space="preserve">
5099 <div class="compact-block"><tt>24 </tt>if (a[1] != 1) e++;
5100 <tt>25 </tt>if (a[2] != 2) e++;
5101 <tt>26 </tt>if (a[3] != 3) e++;
5102 <tt>27 </tt>if (length(a) != 3) e++;
5103 <tt>28 </tt>if (rest != &quot; spare&quot;) e++;</div></pre>
5105 <tt class="verbatim"><div class="compact-block">
5106 <tt>29 </tt>=&lt;\chunkref{pca-test.awk:summary}&gt;
5107 </div></tt>
5108 </p>
5109 </p><p>
5110 <a id="code-end-test:mode-definitions-3"></a>
5111 </p></font>
5112 </p>
5114 We must also be able to parse the example given earlier.
5115 </p>
5117 <font size="-1"><p>
5118 <a id="code-label-test:mode-definitions-4"></a>
5119 <table style="width: 100%" id="code-ref-test:mode-definitions-4">
5120 <tbody><tr>
5121 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:mode-definitions"></a><font size="-1"><div class="left-tab">
5122 <font color="blue">test:mode-definitions</font>[4]() &uArr;<a href="#code-ref-test:mode-definitions-1">43c</a>,
5123 lang=<font color="blue"></font> + &equiv;
5124 </div><div class="right-tab">
5125 &#x25B5;<a href="#code-ref-test:mode-definitions-3">47d</a>
5126 </div></font></td>
5127 </tr></tbody>
5128 </table>
5129 </p><p>
5131 <tt class="verbatim"><div class="compact-block">
5132 <tt>30 </tt>parse_chunk_args(&quot;c-like&quot;, &quot;things[x,
5133 y], get_other_things(a, \&quot;(all)\&quot;), 99&quot;, a,
5134 &quot;(&quot;);
5135 </div></tt>
5136 </p>
5137 <pre class="verbatim" xml:space="preserve">
5138 <div class="compact-block"><tt>31 </tt>if (a[1] != &quot;things[x, y]&quot;) e++;
5139 <tt>32 </tt>if (a[2] != &quot;get_other_things(a, \&quot;(all)\&quot;)&quot;) e++;
5140 <tt>33 </tt>if (a[3] != &quot;99&quot;) e++;
5141 <tt>34 </tt>if (length(a) != 3) e++;</div></pre>
5143 <tt class="verbatim"><div class="compact-block">
5144 <tt>35 </tt>=&lt;\chunkref{pca-test.awk:summary}&gt;
5145 </div></tt>
5146 </p>
5147 </p><table style="width: 100%" id="code-end-test:mode-definitions-4">
5148 <tbody><tr>
5149 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5150 </tr></tbody>
5151 </table></font>
5152 </p>
5153 <h2 id="auto-60">9.4<span style="margin-left: 1em"></span>A non-recursive mode tracker</h2>
5154 <h3 id="auto-61">9.4.1<span style="margin-left: 1em"></span>Constructor</h3>
5156 The mode tracker holds its state in a stack based on a numerically
5157 indexed hash. This function, when passed an empty hash, will intialize
5159 </p>
5161 <font size="-1"><p>
5162 <a id="code-label-new_mode_tracker()-1"></a>
5163 <table style="width: 100%" id="code-ref-new_mode_tracker()-1">
5164 <tbody><tr>
5165 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="new_mode_tracker()"></a><font size="-1"><font color="blue">new_mode_tracker()</font>[1](),
5166 lang=<font color="blue"></font> &equiv;</font></td>
5167 </tr></tbody>
5168 </table>
5169 </p><p>
5171 <tt class="verbatim"><div class="compact-block">
5172 <tt>1 </tt>function new_mode_tracker(context, language, mode) {
5173 </div></tt>
5174 </p>
5175 <pre class="verbatim" xml:space="preserve">
5176 <div class="compact-block"><tt>2 </tt> context[&quot;&quot;] = 0;
5177 <tt>3 </tt> context[0, &quot;language&quot;] = language;
5178 <tt>4 </tt> context[0, &quot;mode&quot;] = mode;</div></pre>
5180 <tt class="verbatim"><div class="compact-block">
5181 <tt>5 </tt>}
5182 </div></tt>
5183 </p>
5184 </p><table style="width: 100%" id="code-end-new_mode_tracker()-1">
5185 <tbody><tr>
5186 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5187 </tr></tbody>
5188 </table></font>
5189 </p>
5191 Because awk functions cannot return an array, we must create the array
5192 first and pass it in, so we have a fangle macro to do this:
5193 </p>
5195 <font size="-1"><p>
5196 <a id="code-label-new-mode-tracker-1"></a>
5197 <table style="width: 100%" id="code-ref-new-mode-tracker-1">
5198 <tbody><tr>
5199 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="new-mode-tracker"></a><font size="-1"><font color="blue">new-mode-tracker</font>[1](<font
5200 color="blue">context</font>, <font color="blue">language</font>, <font color="blue">mode</font>),
5201 lang=<font color="blue">awk</font> &equiv;</font></td>
5202 </tr></tbody>
5203 </table>
5204 </p><p>
5206 <tt class="verbatim"><div class="compact-block">
5207 <tt>1 </tt>awk-delete-array(<font color="blue">context</font>) <a href="#code-ref-awk-delete-array-1">33d</a>
5208 </div></tt>
5209 </p>
5210 <pre class="verbatim" xml:space="preserve">
5211 <div class="compact-block"></div></pre>
5213 <tt class="verbatim"><div class="compact-block">
5214 <tt>2 </tt>new_mode_tracker(${context}, ${language}, ${mode});
5215 </div></tt>
5216 </p>
5217 </p><table style="width: 100%" id="code-end-new-mode-tracker-1">
5218 <tbody><tr>
5219 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5220 </tr></tbody>
5221 </table></font>
5222 </p>
5223 <h3 id="auto-62">9.4.2<span style="margin-left: 1em"></span>Management</h3>
5225 And for tracking modes, we dispatch to a mode-tracker action based on
5226 the current language
5227 </p>
5229 <font size="-1"><p>
5230 <a id="code-label-mode_tracker-1"></a>
5231 <table style="width: 100%" id="code-ref-mode_tracker-1">
5232 <tbody><tr>
5233 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker"></a><font size="-1"><div class="left-tab">
5234 <font color="blue">mode_tracker</font>[1](), lang=<font color="blue">awk</font>
5235 &equiv;
5236 </div><div class="right-tab">
5237 <a href="#code-ref-mode_tracker-2">48d</a>&#x25BF;
5238 </div></font></td>
5239 </tr></tbody>
5240 </table>
5241 </p><p>
5243 <tt class="verbatim"><div class="compact-block">
5244 <tt>1 </tt>function push_mode_tracker(context, language, mode,
5245 </div></tt>
5246 </p>
5247 <pre class="verbatim" xml:space="preserve">
5248 <div class="compact-block"><tt>2 </tt> # local vars
5249 <tt>3 </tt> top)
5250 <tt>4 </tt>{
5251 <tt>5 </tt> if (! (&quot;&quot; in context)) {
5252 <tt>6 </tt> new-mode-tracker(<font color="blue">context</font>, <font color="blue">language</font>, <font color="blue">mode</font>) <a href="#code-ref-new-mode-tracker-1">48b</a>
5253 <tt>7 </tt> } else {
5254 <tt>8 </tt> top = context[&quot;&quot;];
5255 <tt>9 </tt> if (context[top, &quot;language&quot;] == language &amp;&amp; mode==&quot;&quot;) mode = context[top, &quot;mode&quot;];
5256 <tt>10 </tt> top++;
5257 <tt>11 </tt> context[top, &quot;language&quot;] = language;
5258 <tt>12 </tt> context[top, &quot;mode&quot;] = mode;
5259 <tt>13 </tt> context[&quot;&quot;] = top;
5260 <tt>14 </tt> }</div></pre>
5262 <tt class="verbatim"><div class="compact-block">
5263 <tt>15 </tt>}
5264 </div></tt>
5265 </p>
5266 </p><p>
5267 <a id="code-end-mode_tracker-1"></a>
5268 </p></font>
5269 </p>
5271 <font size="-1"><p>
5272 <a id="code-label-mode_tracker-2"></a>
5273 <table style="width: 100%" id="code-ref-mode_tracker-2">
5274 <tbody><tr>
5275 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker"></a><font size="-1"><div class="left-tab">
5276 <font color="blue">mode_tracker</font>[2]() &uArr;<a href="#code-ref-mode_tracker-1">48c</a>,
5277 lang=<font color="blue"></font> + &equiv;
5278 </div><div class="right-tab">
5279 &#x25B5;<a href="#code-ref-mode_tracker-1">48c</a> <a href="#code-ref-mode_tracker-3">48e</a>&#x25BF;
5280 </div></font></td>
5281 </tr></tbody>
5282 </table>
5283 </p><p>
5285 <tt class="verbatim"><div class="compact-block">
5286 <tt>16 </tt>function dump_mode_tracker(context,
5287 </div></tt>
5288 </p>
5289 <pre class="verbatim" xml:space="preserve">
5290 <div class="compact-block"><tt>17 </tt> c, d)
5291 <tt>18 </tt>{
5292 <tt>19 </tt> for(c=0; c &lt;= context[&quot;&quot;]; c++) {
5293 <tt>20 </tt> printf(&quot; %2d %s:%s\n&quot;, c, context[c, &quot;language&quot;], context[c, &quot;mode&quot;]) &gt; &quot;/dev/stderr&quot;;
5294 <tt>21 </tt> for(d=1; ( (c, &quot;values&quot;, d) in context); d++) {
5295 <tt>22 </tt> printf(&quot; %2d %s\n&quot;, d, context[c, &quot;values&quot;, d]) &gt; &quot;/dev/stderr&quot;;
5296 <tt>23 </tt> }
5297 <tt>24 </tt> }</div></pre>
5299 <tt class="verbatim"><div class="compact-block">
5300 <tt>25 </tt>}
5301 </div></tt>
5302 </p>
5303 </p><p>
5304 <a id="code-end-mode_tracker-2"></a>
5305 </p></font>
5306 </p>
5308 <font size="-1"><p>
5309 <a id="code-label-mode_tracker-3"></a>
5310 <table style="width: 100%" id="code-ref-mode_tracker-3">
5311 <tbody><tr>
5312 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker"></a><font size="-1"><div class="left-tab">
5313 <font color="blue">mode_tracker</font>[3]() &uArr;<a href="#code-ref-mode_tracker-1">48c</a>,
5314 lang=<font color="blue"></font> + &equiv;
5315 </div><div class="right-tab">
5316 &#x25B5;<a href="#code-ref-mode_tracker-2">48d</a> <a href="#code-ref-mode_tracker-4">53a</a>&#x22B3;
5317 </div></font></td>
5318 </tr></tbody>
5319 </table>
5320 </p><p>
5322 <tt class="verbatim"><div class="compact-block">
5323 <tt>26 </tt>function finalize_mode_tracker(context)
5324 </div></tt>
5325 </p>
5326 <pre class="verbatim" xml:space="preserve">
5327 <div class="compact-block"><tt>27 </tt>{
5328 <tt>28 </tt> if ( (&quot;&quot; in context) &amp;&amp; context[&quot;&quot;] != 0) return 0;
5329 <tt>29 </tt> return 1;</div></pre>
5331 <tt class="verbatim"><div class="compact-block">
5332 <tt>30 </tt>}
5333 </div></tt>
5334 </p>
5335 </p><p>
5336 <a id="code-end-mode_tracker-3"></a>
5337 </p></font>
5338 </p>
5340 This implies that any chunk must be syntactically whole; for instance,
5341 this is fine:
5342 </p>
5344 <font size="-1"><p>
5345 <a id="code-label-test:whole-chunk-1"></a>
5346 <table style="width: 100%" id="code-ref-test:whole-chunk-1">
5347 <tbody><tr>
5348 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:whole-chunk"></a><font size="-1"><font color="blue">test:whole-chunk</font>[1](),
5349 lang=<font color="blue"></font> &equiv;</font></td>
5350 </tr></tbody>
5351 </table>
5352 </p><p>
5354 <tt class="verbatim"><div class="compact-block">
5355 <tt>1 </tt>if (1) {
5356 </div></tt>
5357 </p>
5358 <pre class="verbatim" xml:space="preserve">
5359 <div class="compact-block"><tt>2 </tt> =&lt;\chunkref{test:say-hello}&gt;</div></pre>
5361 <tt class="verbatim"><div class="compact-block">
5362 <tt>3 </tt>}
5363 </div></tt>
5364 </p>
5365 </p><table style="width: 100%" id="code-end-test:whole-chunk-1">
5366 <tbody><tr>
5367 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5368 </tr></tbody>
5369 </table></font>
5370 </p>
5372 <font size="-1"><p>
5373 <a id="code-label-test:say-hello-1"></a>
5374 <table style="width: 100%" id="code-ref-test:say-hello-1">
5375 <tbody><tr>
5376 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:say-hello"></a><font size="-1"><font color="blue">test:say-hello</font>[1](),
5377 lang=<font color="blue"></font> &equiv;</font></td>
5378 </tr></tbody>
5379 </table>
5380 </p><p>
5381 <tt class="verbatim"><div class="compact-block">
5382 <tt>1 </tt>print &quot;hello&quot;;
5383 </div></tt>
5384 </p><table style="width: 100%" id="code-end-test:say-hello-1">
5385 <tbody><tr>
5386 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5387 </tr></tbody>
5388 </table></font>
5389 </p>
5391 But this is not fine; the chunk test:hidden-else <a href="#code-ref-test:hidden-else-1">49d</a> is not
5392 properly cromulent.
5393 </p>
5395 <font size="-1"><p>
5396 <a id="code-label-test:partial-chunk-1"></a>
5397 <table style="width: 100%" id="code-ref-test:partial-chunk-1">
5398 <tbody><tr>
5399 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:partial-chunk"></a><font size="-1"><font color="blue">test:partial-chunk</font>[1](),
5400 lang=<font color="blue"></font> &equiv;</font></td>
5401 </tr></tbody>
5402 </table>
5403 </p><p>
5405 <tt class="verbatim"><div class="compact-block">
5406 <tt>1 </tt>if (1) {
5407 </div></tt>
5408 </p>
5409 <pre class="verbatim" xml:space="preserve">
5410 <div class="compact-block"><tt>2 </tt> =&lt;\chunkref{test:hidden-else}&gt;</div></pre>
5412 <tt class="verbatim"><div class="compact-block">
5413 <tt>3 </tt>}
5414 </div></tt>
5415 </p>
5416 </p><table style="width: 100%" id="code-end-test:partial-chunk-1">
5417 <tbody><tr>
5418 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5419 </tr></tbody>
5420 </table></font>
5421 </p>
5423 <font size="-1"><p>
5424 <a id="code-label-test:hidden-else-1"></a>
5425 <table style="width: 100%" id="code-ref-test:hidden-else-1">
5426 <tbody><tr>
5427 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:hidden-else"></a><font size="-1"><font color="blue">test:hidden-else</font>[1](),
5428 lang=<font color="blue"></font> &equiv;</font></td>
5429 </tr></tbody>
5430 </table>
5431 </p><p>
5433 <tt class="verbatim"><div class="compact-block">
5434 <tt>1 </tt> print &quot;I'm fine&quot;;
5435 </div></tt>
5436 </p>
5437 <pre class="verbatim" xml:space="preserve">
5438 <div class="compact-block"><tt>2 </tt>} else {</div></pre>
5440 <tt class="verbatim"><div class="compact-block">
5441 <tt>3 </tt> print &quot;I'm not&quot;;
5442 </div></tt>
5443 </p>
5444 </p><table style="width: 100%" id="code-end-test:hidden-else-1">
5445 <tbody><tr>
5446 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5447 </tr></tbody>
5448 </table></font>
5449 </p>
5451 These tests will check for correct behaviour:
5452 </p>
5454 <font size="-1"><p>
5455 <a id="code-label-test:cromulence-1"></a>
5456 <table style="width: 100%" id="code-ref-test:cromulence-1">
5457 <tbody><tr>
5458 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:cromulence"></a><font size="-1"><font color="blue">test:cromulence</font>[1](),
5459 lang=<font color="blue"></font> &equiv;</font></td>
5460 </tr></tbody>
5461 </table>
5462 </p><p>
5464 <tt class="verbatim"><div class="compact-block">
5465 <tt>1 </tt>echo Cromulence test
5466 </div></tt>
5467 </p>
5468 <pre class="verbatim" xml:space="preserve">
5469 <div class="compact-block"><tt>2 </tt>passtest $FANGLE -Rtest:whole-chunk $TEX_SRC &amp;&gt;/dev/null || ( echo &quot;Whole chunk failed&quot; &amp;&amp; exit 1 )</div></pre>
5471 <tt class="verbatim"><div class="compact-block">
5472 <tt>3 </tt>failtest $FANGLE -Rtest:partial-chunk $TEX_SRC
5473 &amp;&gt;/dev/null || ( echo &quot;Partial chunk failed&quot;
5474 &amp;&amp; exit 1 )
5475 </div></tt>
5476 </p>
5477 </p><table style="width: 100%" id="code-end-test:cromulence-1">
5478 <tbody><tr>
5479 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5480 </tr></tbody>
5481 </table></font>
5482 </p>
5483 <h3 id="auto-63">9.4.3<span style="margin-left: 1em"></span>Tracker</h3>
5485 We must avoid recursion as a language construct because we intend to
5486 employ mode-tracking to track language mode of emitted code, and the
5487 code is emitted from a function which is itself recursive, so instead we
5488 implement psuedo-recursion using our own stack based on a hash.
5489 </p>
5491 <font size="-1"><p>
5492 <a id="code-label-mode_tracker()-1"></a>
5493 <table style="width: 100%" id="code-ref-mode_tracker()-1">
5494 <tbody><tr>
5495 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5496 <font color="blue">mode_tracker()</font>[1](), lang=<font color="blue">awk</font>
5497 &equiv;
5498 </div><div class="right-tab">
5499 <a href="#code-ref-mode_tracker()-2">49g</a>&#x25BF;
5500 </div></font></td>
5501 </tr></tbody>
5502 </table>
5503 </p><p>
5505 <tt class="verbatim"><div class="compact-block">
5506 <tt>1 </tt>function mode_tracker(context, text, values,
5507 </div></tt>
5508 </p>
5509 <pre class="verbatim" xml:space="preserve">
5510 <div class="compact-block"><tt>2 </tt> # optional parameters
5511 <tt>3 </tt> # local vars
5512 <tt>4 </tt> mode, submodes, language,
5513 <tt>5 </tt> cindex, c, a, part, item, name, result, new_values, new_mode,
5514 <tt>6 </tt> delimiters, terminators)</div></pre>
5516 <tt class="verbatim"><div class="compact-block">
5517 <tt>7 </tt>{
5518 </div></tt>
5519 </p>
5520 </p><p>
5521 <a id="code-end-mode_tracker()-1"></a>
5522 </p></font>
5523 </p>
5525 We could be re-commencing with a valid context, so we need to setup the
5526 state according to the last context.
5527 </p>
5529 <font size="-1"><p>
5530 <a id="code-label-mode_tracker()-2"></a>
5531 <table style="width: 100%" id="code-ref-mode_tracker()-2">
5532 <tbody><tr>
5533 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5534 <font color="blue">mode_tracker()</font>[2]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5535 lang=<font color="blue"></font> + &equiv;
5536 </div><div class="right-tab">
5537 &#x25B5;<a href="#code-ref-mode_tracker()-1">49f</a> <a href="#code-ref-mode_tracker()-3">50c</a>&#x22B3;
5538 </div></font></td>
5539 </tr></tbody>
5540 </table>
5541 </p><p>
5543 <tt class="verbatim"><div class="compact-block">
5544 <tt>8 </tt> cindex = context[&quot;&quot;] + 0;
5545 </div></tt>
5546 </p>
5547 <pre class="verbatim" xml:space="preserve">
5548 <div class="compact-block"><tt>9 </tt> mode = context[cindex, &quot;mode&quot;];</div></pre>
5550 <tt class="verbatim"><div class="compact-block">
5551 <tt>10 </tt> language = context[cindex, &quot;language&quot; ];
5552 </div></tt>
5553 </p>
5554 </p><p>
5555 <a id="code-end-mode_tracker()-2"></a>
5556 </p></font>
5557 </p>
5559 First we construct a single large regex combining the possible sub-modes
5560 for the current mode along with the terminators for the current mode.
5561 </p>
5563 <font size="-1"><p>
5564 <a id="code-label-parse_chunk_args-reset-modes-1"></a>
5565 <table style="width: 100%" id="code-ref-parse_chunk_args-reset-modes-1">
5566 <tbody><tr>
5567 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="parse_chunk_args-reset-modes"></a><font size="-1"><div class="left-tab">
5568 <font color="blue">parse_chunk_args-reset-modes</font>[1](),
5569 lang=<font color="blue"></font> &equiv;
5570 </div><div class="right-tab">
5571 <a href="#code-ref-parse_chunk_args-reset-modes-2">50b</a>&#x25BF;
5572 </div></font></td>
5573 </tr></tbody>
5574 </table>
5575 </p><p>
5577 <tt class="verbatim"><div class="compact-block">
5578 <tt>1 </tt> submodes=modes[language, mode,
5579 &quot;submodes&quot;];
5580 </div></tt>
5581 </p>
5582 <pre class="verbatim" xml:space="preserve">
5583 <div class="compact-block"><tt>2 </tt>
5584 <tt>3 </tt> if ((language, mode, &quot;delimiters&quot;) in modes) {
5585 <tt>4 </tt> delimiters = modes[language, mode, &quot;delimiters&quot;];
5586 <tt>5 </tt> if (length(submodes)&gt;0) submodes = submodes &quot;|&quot;;
5587 <tt>6 </tt> submodes=submodes delimiters;
5588 <tt>7 </tt> } else delimiters=&quot;&quot;;
5589 <tt>8 </tt> if ((language, mode, &quot;terminators&quot;) in modes) {
5590 <tt>9 </tt> terminators = modes[language, mode, &quot;terminators&quot;];
5591 <tt>10 </tt> if (length(submodes)&gt;0) submodes = submodes &quot;|&quot;;
5592 <tt>11 </tt> submodes=submodes terminators;</div></pre>
5594 <tt class="verbatim"><div class="compact-block">
5595 <tt>12 </tt> } else terminators=&quot;&quot;;
5596 </div></tt>
5597 </p>
5598 </p><p>
5599 <a id="code-end-parse_chunk_args-reset-modes-1"></a>
5600 </p></font>
5601 </p>
5603 If we don't find anything to match on &ndash;- probably because the
5604 language is not supported &ndash;- then we return the entire text
5605 without matching anything.
5606 </p>
5608 <font size="-1"><p>
5609 <a id="code-label-parse_chunk_args-reset-modes-2"></a>
5610 <table style="width: 100%" id="code-ref-parse_chunk_args-reset-modes-2">
5611 <tbody><tr>
5612 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="parse_chunk_args-reset-modes"></a><font size="-1"><div class="left-tab">
5613 <font color="blue">parse_chunk_args-reset-modes</font>[2]() &uArr;<a
5614 href="#code-ref-parse_chunk_args-reset-modes-1">50a</a>, lang=<font color="blue"></font> + &equiv;
5615 </div><div class="right-tab">
5616 &#x25B5;<a href="#code-ref-parse_chunk_args-reset-modes-1">50a</a>
5617 </div></font></td>
5618 </tr></tbody>
5619 </table>
5620 </p><p>
5621 <tt class="verbatim"><div class="compact-block">
5622 <tt>13 </tt> if (! length(submodes)) return text;
5623 </div></tt>
5624 </p><table style="width: 100%" id="code-end-parse_chunk_args-reset-modes-2">
5625 <tbody><tr>
5626 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5627 </tr></tbody>
5628 </table></font>
5629 </p>
5631 <font size="-1"><p>
5632 <a id="code-label-mode_tracker()-3"></a>
5633 <table style="width: 100%" id="code-ref-mode_tracker()-3">
5634 <tbody><tr>
5635 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5636 <font color="blue">mode_tracker()</font>[3]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5637 lang=<font color="blue"></font> + &equiv;
5638 </div><div class="right-tab">
5639 &#x22B2;<a href="#code-ref-mode_tracker()-2">49g</a> <a href="#code-ref-mode_tracker()-4">50d</a>&#x25BF;
5640 </div></font></td>
5641 </tr></tbody>
5642 </table>
5643 </p><p>
5644 <tt class="verbatim"><div class="compact-block">
5645 <tt>11 </tt>=&lt;\chunkref{parse_chunk_args-reset-modes}&gt;
5646 </div></tt>
5647 </p><p>
5648 <a id="code-end-mode_tracker()-3"></a>
5649 </p></font>
5650 </p>
5652 We then iterate the text (until there is none left) looking for
5653 sub-modes or terminators in the regex.
5654 </p>
5656 <font size="-1"><p>
5657 <a id="code-label-mode_tracker()-4"></a>
5658 <table style="width: 100%" id="code-ref-mode_tracker()-4">
5659 <tbody><tr>
5660 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5661 <font color="blue">mode_tracker()</font>[4]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5662 lang=<font color="blue"></font> + &equiv;
5663 </div><div class="right-tab">
5664 &#x25B5;<a href="#code-ref-mode_tracker()-3">50c</a> <a href="#code-ref-mode_tracker()-5">50e</a>&#x25BF;
5665 </div></font></td>
5666 </tr></tbody>
5667 </table>
5668 </p><p>
5670 <tt class="verbatim"><div class="compact-block">
5671 <tt>12 </tt> while((cindex &gt;= 0) &amp;&amp; length(text)) {
5672 </div></tt>
5673 </p>
5674 <pre class="verbatim" xml:space="preserve">
5675 <div class="compact-block"></div></pre>
5677 <tt class="verbatim"><div class="compact-block">
5678 <tt>13 </tt> if (match(text, &quot;(&quot; submodes
5679 &quot;)&quot;, a)) {
5680 </div></tt>
5681 </p>
5682 </p><p>
5683 <a id="code-end-mode_tracker()-4"></a>
5684 </p></font>
5685 </p>
5687 A bug that creeps in regularly during development is bad regexes of zero
5688 length which result in an infinite loop (as no text is consumed), so I
5689 catch that right away with this test.
5690 </p>
5692 <font size="-1"><p>
5693 <a id="code-label-mode_tracker()-5"></a>
5694 <table style="width: 100%" id="code-ref-mode_tracker()-5">
5695 <tbody><tr>
5696 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5697 <font color="blue">mode_tracker()</font>[5]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5698 lang=<font color="blue"></font> + &equiv;
5699 </div><div class="right-tab">
5700 &#x25B5;<a href="#code-ref-mode_tracker()-4">50d</a> <a href="#code-ref-mode_tracker()-6">50f</a>&#x25BF;
5701 </div></font></td>
5702 </tr></tbody>
5703 </table>
5704 </p><p>
5706 <tt class="verbatim"><div class="compact-block">
5707 <tt>14 </tt> if (RLENGTH&lt;1) {
5708 </div></tt>
5709 </p>
5710 <pre class="verbatim" xml:space="preserve">
5711 <div class="compact-block"><tt>15 </tt> error(sprintf(&quot;Internal error, matched zero length submode, should be impossible - likely regex computation error\n&quot; \
5712 <tt>16 </tt> &quot;Language=%s\nmode=%s\nmatch=%s\n&quot;, language, mode, submodes));</div></pre>
5714 <tt class="verbatim"><div class="compact-block">
5715 <tt>17 </tt> }
5716 </div></tt>
5717 </p>
5718 </p><p>
5719 <a id="code-end-mode_tracker()-5"></a>
5720 </p></font>
5721 </p>
5723 part is defined as the text up to the sub-mode or terminator, and this
5724 is appended to item &ndash;- which is the current text being gathered.
5725 If a mode has a delimiter, then item is reset each time a delimiter is
5726 found.
5727 </p>
5729 (<tt class="verbatim">&quot;</tt><sub>item</sub>, <sub>item</sub><tt class="verbatim">&quot;</tt>)<sup>&amp;wide-overbrace;</sup><sup>item</sup>,
5730 he said.<sup class="wide">&amp;wide-overbrace;</sup><sup>item</sup>
5731 </p>
5733 <font size="-1"><p>
5734 <a id="code-label-mode_tracker()-6"></a>
5735 <table style="width: 100%" id="code-ref-mode_tracker()-6">
5736 <tbody><tr>
5737 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5738 <font color="blue">mode_tracker()</font>[6]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5739 lang=<font color="blue"></font> + &equiv;
5740 </div><div class="right-tab">
5741 &#x25B5;<a href="#code-ref-mode_tracker()-5">50e</a> <a href="#code-ref-mode_tracker()-7">50g</a>&#x25BF;
5742 </div></font></td>
5743 </tr></tbody>
5744 </table>
5745 </p><p>
5747 <tt class="verbatim"><div class="compact-block">
5748 <tt>18 </tt> part = substr(text, 1, RSTART -1);
5749 </div></tt>
5750 </p>
5751 <pre class="verbatim" xml:space="preserve">
5752 <div class="compact-block"></div></pre>
5754 <tt class="verbatim"><div class="compact-block">
5755 <tt>19 </tt> item = item part;
5756 </div></tt>
5757 </p>
5758 </p><p>
5759 <a id="code-end-mode_tracker()-6"></a>
5760 </p></font>
5761 </p>
5763 We must now determine what was matched. If it was a terminator, then we
5764 must restore the previous mode.
5765 </p>
5767 <font size="-1"><p>
5768 <a id="code-label-mode_tracker()-7"></a>
5769 <table style="width: 100%" id="code-ref-mode_tracker()-7">
5770 <tbody><tr>
5771 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5772 <font color="blue">mode_tracker()</font>[7]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5773 lang=<font color="blue"></font> + &equiv;
5774 </div><div class="right-tab">
5775 &#x25B5;<a href="#code-ref-mode_tracker()-6">50f</a> <a href="#code-ref-mode_tracker()-8">51a</a>&#x22B3;
5776 </div></font></td>
5777 </tr></tbody>
5778 </table>
5779 </p><p>
5781 <tt class="verbatim"><div class="compact-block">
5782 <tt>20 </tt> if (match(a[1], &quot;^&quot; terminators
5783 &quot;$&quot;)) {
5784 </div></tt>
5785 </p>
5786 <pre class="verbatim" xml:space="preserve">
5787 <div class="compact-block"><tt>21 </tt>#printf(&quot;%2d EXIT MODE [%s] by [%s] [%s]\n&quot;, cindex, mode, a[1], text) &gt; &quot;/dev/stderr&quot;
5788 <tt>22 </tt> context[cindex, &quot;values&quot;, ++context[cindex, &quot;values&quot;]] = item;
5789 <tt>23 </tt> delete context[cindex];
5790 <tt>24 </tt> context[&quot;&quot;] = --cindex;
5791 <tt>25 </tt> if (cindex&gt;=0) {
5792 <tt>26 </tt> mode = context[cindex, &quot;mode&quot;];
5793 <tt>27 </tt> language = context[cindex, &quot;language&quot;];
5794 <tt>28 </tt> =&lt;\chunkref{parse_chunk_args-reset-modes}&gt;
5795 <tt>29 </tt> }
5796 <tt>30 </tt> item = item a[1];
5797 <tt>31 </tt> text = substr(text, 1 + length(part) + length(a[1]));</div></pre>
5799 <tt class="verbatim"><div class="compact-block">
5800 <tt>32 </tt> }
5801 </div></tt>
5802 </p>
5803 </p><p>
5804 <a id="code-end-mode_tracker()-7"></a>
5805 </p></font>
5806 </p>
5808 If a delimiter was matched, then we must store the current item in the
5809 parsed values array, and reset the item.
5810 </p>
5812 <font size="-1"><p>
5813 <a id="code-label-mode_tracker()-8"></a>
5814 <table style="width: 100%" id="code-ref-mode_tracker()-8">
5815 <tbody><tr>
5816 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5817 <font color="blue">mode_tracker()</font>[8]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5818 lang=<font color="blue"></font> + &equiv;
5819 </div><div class="right-tab">
5820 &#x22B2;<a href="#code-ref-mode_tracker()-7">50g</a> <a href="#code-ref-mode_tracker()-9">51b</a>&#x25BF;
5821 </div></font></td>
5822 </tr></tbody>
5823 </table>
5824 </p><p>
5826 <tt class="verbatim"><div class="compact-block">
5827 <tt>33 </tt> else if (match(a[1], &quot;^&quot; delimiters
5828 &quot;$&quot;)) {
5829 </div></tt>
5830 </p>
5831 <pre class="verbatim" xml:space="preserve">
5832 <div class="compact-block"><tt>34 </tt> if (cindex==0) {
5833 <tt>35 </tt> context[cindex, &quot;values&quot;, ++context[cindex, &quot;values&quot;]] = item;
5834 <tt>36 </tt> item = &quot;&quot;;
5835 <tt>37 </tt> } else {
5836 <tt>38 </tt> item = item a[1];
5837 <tt>39 </tt> }
5838 <tt>40 </tt> text = substr(text, 1 + length(part) + length(a[1]));</div></pre>
5840 <tt class="verbatim"><div class="compact-block">
5841 <tt>41 </tt> }
5842 </div></tt>
5843 </p>
5844 </p><p>
5845 <a id="code-end-mode_tracker()-8"></a>
5846 </p></font>
5847 </p>
5849 otherwise, if a new submode is detected (all submodes have terminators),
5850 we must create a nested parse context until we find the terminator for
5851 this mode.
5852 </p>
5854 <font size="-1"><p>
5855 <a id="code-label-mode_tracker()-9"></a>
5856 <table style="width: 100%" id="code-ref-mode_tracker()-9">
5857 <tbody><tr>
5858 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5859 <font color="blue">mode_tracker()</font>[9]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5860 lang=<font color="blue"></font> + &equiv;
5861 </div><div class="right-tab">
5862 &#x25B5;<a href="#code-ref-mode_tracker()-8">51a</a> <a href="#code-ref-mode_tracker()-10">51c</a>&#x25BF;
5863 </div></font></td>
5864 </tr></tbody>
5865 </table>
5866 </p><p>
5868 <tt class="verbatim"><div class="compact-block">
5869 <tt>42 </tt> else if ((language, a[1], &quot;terminators&quot;)
5870 in modes) {
5871 </div></tt>
5872 </p>
5873 <pre class="verbatim" xml:space="preserve">
5874 <div class="compact-block"><tt>43 </tt> #check if new_mode is defined
5875 <tt>44 </tt> item = item a[1];
5876 <tt>45 </tt>#printf(&quot;%2d ENTER MODE [%s] in [%s]\n&quot;, cindex, a[1], text) &gt; &quot;/dev/stderr&quot;
5877 <tt>46 </tt> text = substr(text, 1 + length(part) + length(a[1]));
5878 <tt>47 </tt> context[&quot;&quot;] = ++cindex;
5879 <tt>48 </tt> context[cindex, &quot;mode&quot;] = a[1];
5880 <tt>49 </tt> context[cindex, &quot;language&quot;] = language;
5881 <tt>50 </tt> mode = a[1];
5882 <tt>51 </tt> =&lt;\chunkref{parse_chunk_args-reset-modes}&gt;
5883 <tt>52 </tt> } else {
5884 <tt>53 </tt> error(sprintf(&quot;Submode '%s' set unknown mode in text: %s\nLanguage %s Mode %s\n&quot;, a[1], text, language, mode));
5885 <tt>54 </tt> text = substr(text, 1 + length(part) + length(a[1]));
5886 <tt>55 </tt> }</div></pre>
5888 <tt class="verbatim"><div class="compact-block">
5889 <tt>56 </tt> }
5890 </div></tt>
5891 </p>
5892 </p><p>
5893 <a id="code-end-mode_tracker()-9"></a>
5894 </p></font>
5895 </p>
5897 In the final case, we parsed to the end of the string. If the string was
5898 entire, then we should have no nested mode context, but if the string
5899 was just a fragment we may have a mode context which must be preserved
5900 for the next fragment. Todo: Consideration ought to be given if sub-mode
5901 strings are split over two fragments.
5902 </p>
5904 <font size="-1"><p>
5905 <a id="code-label-mode_tracker()-10"></a>
5906 <table style="width: 100%" id="code-ref-mode_tracker()-10">
5907 <tbody><tr>
5908 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker()"></a><font size="-1"><div class="left-tab">
5909 <font color="blue">mode_tracker()</font>[10]() &uArr;<a href="#code-ref-mode_tracker()-1">49f</a>,
5910 lang=<font color="blue"></font> + &equiv;
5911 </div><div class="right-tab">
5912 &#x25B5;<a href="#code-ref-mode_tracker()-9">51b</a>
5913 </div></font></td>
5914 </tr></tbody>
5915 </table>
5916 </p><p>
5918 <tt class="verbatim"><div class="compact-block">
5919 <tt>57 </tt>else {
5920 </div></tt>
5921 </p>
5922 <pre class="verbatim" xml:space="preserve">
5923 <div class="compact-block"><tt>58 </tt> context[cindex, &quot;values&quot;, ++context[cindex, &quot;values&quot;]] = item text;
5924 <tt>59 </tt> text = &quot;&quot;;
5925 <tt>60 </tt> item = &quot;&quot;;
5926 <tt>61 </tt> }
5927 <tt>62 </tt> }
5928 <tt>63 </tt>
5929 <tt>64 </tt> context[&quot;item&quot;] = item;
5930 <tt>65 </tt>
5931 <tt>66 </tt> if (length(item)) context[cindex, &quot;values&quot;, ++context[cindex, &quot;values&quot;]] = item;
5932 <tt>67 </tt> return text;</div></pre>
5934 <tt class="verbatim"><div class="compact-block">
5935 <tt>68 </tt>}
5936 </div></tt>
5937 </p>
5938 </p><table style="width: 100%" id="code-end-mode_tracker()-10">
5939 <tbody><tr>
5940 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5941 </tr></tbody>
5942 </table></font>
5943 </p>
5944 <h4 id="auto-64">9.4.3.1<span style="margin-left: 1em"></span>One happy chunk</h4>
5946 All the mode tracker chunks are referred to here:
5947 </p>
5949 <font size="-1"><p>
5950 <a id="code-label-mode-tracker-1"></a>
5951 <table style="width: 100%" id="code-ref-mode-tracker-1">
5952 <tbody><tr>
5953 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode-tracker"></a><font size="-1"><font color="blue">mode-tracker</font>[1](),
5954 lang=<font color="blue"></font> &equiv;</font></td>
5955 </tr></tbody>
5956 </table>
5957 </p><p>
5959 <tt class="verbatim"><div class="compact-block">
5960 <tt>1 </tt>new_mode_tracker() <a href="#code-ref-new_mode_tracker()-1">48a</a>
5961 </div></tt>
5962 </p>
5963 <pre class="verbatim" xml:space="preserve">
5964 <div class="compact-block"></div></pre>
5966 <tt class="verbatim"><div class="compact-block">
5967 <tt>2 </tt>mode_tracker() <a href="#code-ref-mode_tracker()-1">49f</a>
5968 </div></tt>
5969 </p>
5970 </p><table style="width: 100%" id="code-end-mode-tracker-1">
5971 <tbody><tr>
5972 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
5973 </tr></tbody>
5974 </table></font>
5975 </p>
5976 <h4 id="auto-65">9.4.3.2<span style="margin-left: 1em"></span>Tests</h4>
5978 We can test this function like this:
5979 </p>
5981 <font size="-1"><p>
5982 <a id="code-label-pca-test.awk-1"></a>
5983 <table style="width: 100%" id="code-ref-pca-test.awk-1">
5984 <tbody><tr>
5985 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="pca-test.awk"></a><font size="-1"><font color="blue">pca-test.awk</font>[1](),
5986 lang=<font color="blue">awk</font> &equiv;</font></td>
5987 </tr></tbody>
5988 </table>
5989 </p><p>
5991 <tt class="verbatim"><div class="compact-block">
5992 <tt>1 </tt>=&lt;\chunkref{error()}&gt;
5993 </div></tt>
5994 </p>
5995 <pre class="verbatim" xml:space="preserve">
5996 <div class="compact-block"><tt>2 </tt>=&lt;\chunkref{mode-tracker}&gt;
5997 <tt>3 </tt>=&lt;\chunkref{parse_chunk_args()}&gt;
5998 <tt>4 </tt>BEGIN {
5999 <tt>5 </tt> SUBSEP=&quot;.&quot;;
6000 <tt>6 </tt> =&lt;\chunkref{mode-definitions}&gt;
6001 <tt>7 </tt>
6002 <tt>8 </tt> =&lt;\chunkref{test:mode-definitions}&gt;</div></pre>
6004 <tt class="verbatim"><div class="compact-block">
6005 <tt>9 </tt>}
6006 </div></tt>
6007 </p>
6008 </p><table style="width: 100%" id="code-end-pca-test.awk-1">
6009 <tbody><tr>
6010 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6011 </tr></tbody>
6012 </table></font>
6013 </p>
6015 <font size="-1"><p>
6016 <a id="code-label-pca-test.awk:summary-1"></a>
6017 <table style="width: 100%" id="code-ref-pca-test.awk:summary-1">
6018 <tbody><tr>
6019 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="pca-test.awk:summary"></a><font size="-1"><font color="blue">pca-test.awk:summary</font>[1](),
6020 lang=<font color="blue">awk</font> &equiv;</font></td>
6021 </tr></tbody>
6022 </table>
6023 </p><p>
6025 <tt class="verbatim"><div class="compact-block">
6026 <tt>1 </tt>if (e) {
6027 </div></tt>
6028 </p>
6029 <pre class="verbatim" xml:space="preserve">
6030 <div class="compact-block"><tt>2 </tt> printf &quot;Failed &quot; e
6031 <tt>3 </tt> for (b in a) {
6032 <tt>4 </tt> print &quot;a[&quot; b &quot;] =&gt; &quot; a[b];
6033 <tt>5 </tt> }
6034 <tt>6 </tt>} else {
6035 <tt>7 </tt> print &quot;Passed&quot;
6036 <tt>8 </tt>}
6037 <tt>9 </tt>split(&quot;&quot;, a);</div></pre>
6039 <tt class="verbatim"><div class="compact-block">
6040 <tt>10 </tt>e=0;
6041 </div></tt>
6042 </p>
6043 </p><table style="width: 100%" id="code-end-pca-test.awk:summary-1">
6044 <tbody><tr>
6045 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6046 </tr></tbody>
6047 </table></font>
6048 </p>
6050 which should give this output:
6051 </p>
6053 <font size="-1"><p>
6054 <a id="code-label-pca-test.awk-results-1"></a>
6055 <table style="width: 100%" id="code-ref-pca-test.awk-results-1">
6056 <tbody><tr>
6057 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="pca-test.awk-results"></a><font size="-1"><font color="blue">pca-test.awk-results</font>[1](),
6058 lang=<font color="blue"></font> &equiv;</font></td>
6059 </tr></tbody>
6060 </table>
6061 </p><p>
6063 <tt class="verbatim"><div class="compact-block">
6064 <tt>1 </tt>a[foo.quux.quirk] =&gt;
6065 </div></tt>
6066 </p>
6067 <pre class="verbatim" xml:space="preserve">
6068 <div class="compact-block"><tt>2 </tt>a[foo.quux.a] =&gt; fleeg
6069 <tt>3 </tt>a[foo.bar] =&gt; baz
6070 <tt>4 </tt>a[etc] =&gt; </div></pre>
6072 <tt class="verbatim"><div class="compact-block">
6073 <tt>5 </tt>a[name] =&gt; freddie
6074 </div></tt>
6075 </p>
6076 </p><table style="width: 100%" id="code-end-pca-test.awk-results-1">
6077 <tbody><tr>
6078 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6079 </tr></tbody>
6080 </table></font>
6081 </p>
6082 <h2 id="auto-66">9.5<span style="margin-left: 1em"></span>Escaping and Quoting</h2>
6084 For the time being and to get around TeXmacs inability to export a
6085 <kbd>TAB</kbd> character, the right arrow whose UTF-8 sequence is ...
6086 </p>
6087 <table style="display: inline; vertical-align: -0.55em">
6088 <tbody><tr>
6089 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: complete</td>
6090 </tr></tbody>
6091 </table>
6093 Another special character is used, the left-arrow with UTF-8 sequence
6094 0xE2 0x86 0xA4 is used to strip any preceding white space as a way of
6095 un-tabbing and removing indent that has been applied <class style="font-family: Times New Roman">&mdash;</class>
6096 this is important for bash here documents, and the like. It's a filthy
6097 hack.
6098 </p>
6099 <table style="display: inline; vertical-align: -0.55em">
6100 <tbody><tr>
6101 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: remove the hack</td>
6102 </tr></tbody>
6103 </table>
6105 <font size="-1"><p>
6106 <a id="code-label-mode_tracker-4"></a>
6107 <table style="width: 100%" id="code-ref-mode_tracker-4">
6108 <tbody><tr>
6109 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker"></a><font size="-1"><div class="left-tab">
6110 <font color="blue">mode_tracker</font>[4]() &uArr;<a href="#code-ref-mode_tracker-1">48c</a>,
6111 lang=<font color="blue"></font> + &equiv;
6112 </div><div class="right-tab">
6113 &#x22B2;<a href="#code-ref-mode_tracker-3">48e</a> <a href="#code-ref-mode_tracker-5">53b</a>&#x25BF;
6114 </div></font></td>
6115 </tr></tbody>
6116 </table>
6117 </p><p>
6119 <tt class="verbatim"><div class="compact-block">
6121 </div></tt>
6122 </p>
6123 <pre class="verbatim" xml:space="preserve">
6124 <div class="compact-block"><tt>31 </tt>function untab(text) {
6125 <tt>32 </tt> gsub(&quot;[[:space:]]*\xE2\x86\xA4&quot;,&quot;&quot;, text);
6126 <tt>33 </tt> return text;</div></pre>
6128 <tt class="verbatim"><div class="compact-block">
6129 <tt>34 </tt>}
6130 </div></tt>
6131 </p>
6132 </p><p>
6133 <a id="code-end-mode_tracker-4"></a>
6134 </p></font>
6135 </p>
6137 Each nested mode can optionally define a set of transforms to be applied
6138 to any text that is included from another language.
6139 </p>
6141 This code can perform transforms
6142 </p>
6144 <font size="-1"><p>
6145 <a id="code-label-mode_tracker-5"></a>
6146 <table style="width: 100%" id="code-ref-mode_tracker-5">
6147 <tbody><tr>
6148 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker"></a><font size="-1"><div class="left-tab">
6149 <font color="blue">mode_tracker</font>[5]() &uArr;<a href="#code-ref-mode_tracker-1">48c</a>,
6150 lang=<font color="blue">awk</font> + &equiv;
6151 </div><div class="right-tab">
6152 &#x25B5;<a href="#code-ref-mode_tracker-4">53a</a> <a href="#code-ref-mode_tracker-6">53c</a>&#x25BF;
6153 </div></font></td>
6154 </tr></tbody>
6155 </table>
6156 </p><p>
6158 <tt class="verbatim"><div class="compact-block">
6159 <tt>35 </tt>function transform_escape(s, r, text,
6160 </div></tt>
6161 </p>
6162 <pre class="verbatim" xml:space="preserve">
6163 <div class="compact-block"><tt>36 </tt> # optional
6164 <tt>37 </tt> max,
6165 <tt>38 </tt> # local vars
6166 <tt>39 </tt> c)
6167 <tt>40 </tt>{
6168 <tt>41 </tt> for(c=1; c &lt;= max &amp;&amp; (c in s); c++) {
6169 <tt>42 </tt> gsub(s[c], r[c], text);
6170 <tt>43 </tt> }
6171 <tt>44 </tt> return text;</div></pre>
6173 <tt class="verbatim"><div class="compact-block">
6174 <tt>45 </tt>}
6175 </div></tt>
6176 </p>
6177 </p><p>
6178 <a id="code-end-mode_tracker-5"></a>
6179 </p></font>
6180 </p>
6182 This function must append from index c onwards, and escape transforms
6183 from the supplied context, and return c + number of new transforms.
6184 </p>
6186 <font size="-1"><p>
6187 <a id="code-label-mode_tracker-6"></a>
6188 <table style="width: 100%" id="code-ref-mode_tracker-6">
6189 <tbody><tr>
6190 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="mode_tracker"></a><font size="-1"><div class="left-tab">
6191 <font color="blue">mode_tracker</font>[6]() &uArr;<a href="#code-ref-mode_tracker-1">48c</a>,
6192 lang=<font color="blue">awk</font> + &equiv;
6193 </div><div class="right-tab">
6194 &#x25B5;<a href="#code-ref-mode_tracker-5">53b</a>
6195 </div></font></td>
6196 </tr></tbody>
6197 </table>
6198 </p><p>
6200 <tt class="verbatim"><div class="compact-block">
6201 <tt>46 </tt>function mode_escaper(context, s, r, src,
6202 </div></tt>
6203 </p>
6204 <pre class="verbatim" xml:space="preserve">
6205 <div class="compact-block"><tt>47 </tt> c, cp, cpl)
6206 <tt>48 </tt>{
6207 <tt>49 </tt> for(c = context[&quot;&quot;]; c &gt;= 0; c--) {
6208 <tt>50 </tt> if ( (context[c, &quot;language&quot;], context[c, &quot;mode&quot;]) in escapes) {
6209 <tt>51 </tt> cpl = escapes[context[c, &quot;language&quot;], context[c, &quot;mode&quot;]];
6210 <tt>52 </tt> for (cp = 1; cp &lt;= cpl; cp ++) {
6211 <tt>53 </tt> ++src;
6212 <tt>54 </tt> s[src] = escapes[context[c, &quot;language&quot;], context[c, &quot;mode&quot;], cp, &quot;s&quot;];
6213 <tt>55 </tt> r[src] = escapes[context[c, &quot;language&quot;], context[c, &quot;mode&quot;], cp, &quot;r&quot;];
6214 <tt>56 </tt> }
6215 <tt>57 </tt> }
6216 <tt>58 </tt> }
6217 <tt>59 </tt> return src;
6218 <tt>60 </tt>}
6219 <tt>61 </tt>function dump_escaper(c, s, r, cc) {
6220 <tt>62 </tt> for(cc=1; cc&lt;=c; cc++) {
6221 <tt>63 </tt> printf(&quot;%2d s[%s] r[%s]\n&quot;, cc, s[cc], r[cc]) &gt; &quot;/dev/stderr&quot;
6222 <tt>64 </tt> }</div></pre>
6224 <tt class="verbatim"><div class="compact-block">
6225 <tt>65 </tt>}
6226 </div></tt>
6227 </p>
6228 </p><table style="width: 100%" id="code-end-mode_tracker-6">
6229 <tbody><tr>
6230 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6231 </tr></tbody>
6232 </table></font>
6233 </p>
6235 <font size="-1"><p>
6236 <a id="code-label-test:escapes-1"></a>
6237 <table style="width: 100%" id="code-ref-test:escapes-1">
6238 <tbody><tr>
6239 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:escapes"></a><font size="-1"><font color="blue">test:escapes</font>[1](),
6240 lang=<font color="blue">sh</font> &equiv;</font></td>
6241 </tr></tbody>
6242 </table>
6243 </p><p>
6245 <tt class="verbatim"><div class="compact-block">
6246 <tt>1 </tt>echo escapes test
6247 </div></tt>
6248 </p>
6249 <pre class="verbatim" xml:space="preserve">
6250 <div class="compact-block"></div></pre>
6252 <tt class="verbatim"><div class="compact-block">
6253 <tt>2 </tt>passtest $FANGLE -Rtest:comment-quote $TEX_SRC
6254 &amp;&gt;/dev/null || ( echo &quot;Comment-quote failed&quot;
6255 &amp;&amp; exit 1 )
6256 </div></tt>
6257 </p>
6258 </p><table style="width: 100%" id="code-end-test:escapes-1">
6259 <tbody><tr>
6260 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6261 </tr></tbody>
6262 </table></font>
6263 </p>
6264 <h1 id="auto-67">Chapter 10<br></br>Recognizing Chunks</h1>
6266 Fangle recognizes noweb chunks, but as we also want better LaTeX
6267 integration we will recognize any of these:
6268 </p>
6269 <ul>
6270 <li>
6271 notangle chunks matching the pattern <tt class="verbatim">^&lt;&lt;.*?&gt;&gt;=</tt>
6272 </li>
6273 <li>
6274 chunks beginning with <tt class="verbatim">\begin{lstlistings}</tt>, possibly
6275 with <tt class="verbatim">\Chunk{...}</tt> on the previous line
6276 </li>
6277 <li>
6278 an older form I have used, beginning with
6279 <tt class="verbatim">\begin{Chunk}[options]</tt>
6280 &ndash;- also more suitable for plain LaTeX users
6282 <font size="-1"><div align="justify">
6283 <div style="margin-left: 0px">
6284 <div style="margin-right: 0px">
6286 . Is there such a thing as plain LaTeX?
6287 </p>
6288 </div>
6289 </div>
6290 </div></font>
6291 </p>
6292 <span style="margin-left: 0em"></span>
6293 <a id="footnr-1"></a>
6294 <sup><a href="#footnote-1">1</a></sup>
6296 </li>
6297 </ul>
6298 <h2 id="auto-68">10.1<span style="margin-left: 1em"></span>Chunk start</h2>
6300 The variable chunking is used to signify that we are processing a code
6301 chunk and not document. In such a state, input lines will be assigned to
6302 the current chunk; otherwise they are ignored.
6303 </p>
6304 <h3 id="auto-69">10.1.1<span style="margin-left: 1em"></span>TeXmacs hackery</h3>
6306 We don't handle TeXmacs files natively but instead emit unicode
6307 character sequences to mark up the text-export file which we work on.
6308 </p>
6310 These hacks detect such sequences and retro-fit in the old TeX parsing.
6311 </p>
6313 <font size="-1"><p>
6314 <a id="code-label-recognize-chunk-1"></a>
6315 <table style="width: 100%" id="code-ref-recognize-chunk-1">
6316 <tbody><tr>
6317 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6318 <font color="blue">recognize-chunk</font>[1](), lang=<font color="blue"></font>
6319 &equiv;
6320 </div><div class="right-tab">
6321 <a href="#code-ref-recognize-chunk-2">56a</a>&#x22B3;
6322 </div></font></td>
6323 </tr></tbody>
6324 </table>
6325 </p><p>
6327 <tt class="verbatim"><div class="compact-block">
6329 </div></tt>
6330 </p>
6331 <pre class="verbatim" xml:space="preserve">
6332 <div class="compact-block"><tt>1 </tt>#/\n/ {
6333 <tt>2 </tt># gsub(&quot;\n*$&quot;,&quot;&quot;);
6334 <tt>3 </tt># gsub(&quot;\n&quot;, &quot; &quot;);
6335 <tt>4 </tt>#}
6336 <tt>5 </tt>#===
6337 <tt>6 </tt>/\xE2\x86\xA6/ {
6338 <tt>7 </tt> gsub(&quot;\\xE2\\x86\\xA6&quot;, &quot;\x09&quot;);
6339 <tt>8 </tt>}
6340 <tt>9 </tt>
6341 <tt>10 </tt>/\xE2\x80\x98/ {
6342 <tt>11 </tt> gsub(&quot;\\xE2\\x80\\x98&quot;, &quot;&lsquo;&quot;);
6343 <tt>12 </tt>}
6344 <tt>13 </tt>
6345 <tt>14 </tt>/\xE2\x89\xA1/ {
6346 <tt>15 </tt> if (match($0, &quot;^ *([^[ ]* |)&lt;([^[ ]*)\\[[0-9]*\\][(](.*)[)].*, lang=([^ ]*)&quot;, line)) {
6347 <tt>16 </tt> next_chunk_name=line[2];
6348 <tt>17 </tt> gsub(&quot;,&quot;,&quot;;&quot;,line[3]);
6349 <tt>18 </tt> params=&quot;params=&quot; line[3];
6350 <tt>19 </tt> if ((line[4])) {
6351 <tt>20 </tt> params = params &quot;,language=&quot; line[4]
6352 <tt>21 </tt> }
6353 <tt>22 </tt> get_chunk_args(params, next_chunk_args);
6354 <tt>23 </tt> new_chunk(next_chunk_name, next_chunk_args);
6355 <tt>24 </tt> texmacs_chunking = 1;
6356 <tt>25 </tt> } else {
6357 <tt>26 </tt>#print &quot;Unexpected
6358 <tt>27 </tt>#print
6359 <tt>28 </tt>#exit 1
6360 <tt>29 </tt> }
6361 <tt>30 </tt> next;
6362 <tt>31 </tt>}</div></pre>
6364 <tt class="verbatim"><div class="compact-block">
6365 <tt>32 </tt>#===
6366 </div></tt>
6367 </p>
6368 </p><p>
6369 <a id="code-end-recognize-chunk-1"></a>
6370 </p></font>
6371 </p>
6372 <h3 id="auto-70">10.1.2<span style="margin-left: 1em"></span>lstlistings</h3>
6374 Our current scheme is to recognize the new lstlisting chunks, but these
6375 may be preceded by a <tt class="verbatim">\Chunk</tt> command which in L<span
6376 style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X is a more convenient way to pass
6377 the chunk name to the <tt class="verbatim">\begin{lstlistings}</tt> command, and a
6378 more visible way to specify other <tt class="verbatim">lstset</tt> settings.
6379 </p>
6381 The arguments to the <tt class="verbatim">\Chunk</tt> command are a name, and then
6382 a comma-seperated list of key-value pairs after the manner of <tt class="verbatim">\lstset</tt>.
6383 (In fact within the LaTeX <tt class="verbatim">\Chunk</tt> macro (section <a href="#sub:The-chunk-command">15.2.1</a>)
6384 the text <tt class="verbatim">name=</tt> is prefixed to the argument which is then
6385 literally passed to <tt class="verbatim">\lstset</tt>).
6386 </p>
6388 <font size="-1"><p>
6389 <a id="code-label-recognize-chunk-2"></a>
6390 <table style="width: 100%" id="code-ref-recognize-chunk-2">
6391 <tbody><tr>
6392 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6393 <font color="blue">recognize-chunk</font>[2]() &uArr;<a href="#code-ref-recognize-chunk-1">55a</a>,
6394 lang=<font color="blue">awk</font> + &equiv;
6395 </div><div class="right-tab">
6396 &#x22B2;<a href="#code-ref-recognize-chunk-1">55a</a> <a href="#code-ref-recognize-chunk-3">56b</a>&#x25BF;
6397 </div></font></td>
6398 </tr></tbody>
6399 </table>
6400 </p><p>
6402 <tt class="verbatim"><div class="compact-block">
6403 <tt>33 </tt>/^\\Chunk{/ {
6404 </div></tt>
6405 </p>
6406 <pre class="verbatim" xml:space="preserve">
6407 <div class="compact-block"><tt>34 </tt> if (match($0, &quot;^\\\\Chunk{ *([^ ,}]*),?(.*)}&quot;, line)) {
6408 <tt>35 </tt> next_chunk_name = line[1];
6409 <tt>36 </tt> get_chunk_args(line[2], next_chunk_args);
6410 <tt>37 </tt> }
6411 <tt>38 </tt> next;</div></pre>
6413 <tt class="verbatim"><div class="compact-block">
6414 <tt>39 </tt>}
6415 </div></tt>
6416 </p>
6417 </p><p>
6418 <a id="code-end-recognize-chunk-2"></a>
6419 </p></font>
6420 </p>
6422 We also make a basic attempt to parse the name out of the
6423 <tt class="verbatim">\lstlistings[name=chunk-name]</tt>
6424 text, otherwise we fall back to the name found in the previous chunk
6425 command. This attempt is very basic and doesn't support commas or spaces
6426 or square brackets as part of the chunkname. We also recognize
6427 <tt class="verbatim">\begin{Chunk}</tt>
6428 which is convenient for some users
6430 <font size="-1"><div align="justify">
6431 <div style="margin-left: 0px">
6432 <div style="margin-right: 0px">
6434 . but not yet supported in the LaTeX macros
6435 </p>
6436 </div>
6437 </div>
6438 </div></font>
6439 </p>
6440 <span style="margin-left: 0em"></span>
6441 <a id="footnr-2"></a>
6442 <sup><a href="#footnote-2">2</a></sup>
6444 </p>
6446 <font size="-1"><p>
6447 <a id="code-label-recognize-chunk-3"></a>
6448 <table style="width: 100%" id="code-ref-recognize-chunk-3">
6449 <tbody><tr>
6450 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6451 <font color="blue">recognize-chunk</font>[3]() &uArr;<a href="#code-ref-recognize-chunk-1">55a</a>,
6452 lang=<font color="blue"></font> + &equiv;
6453 </div><div class="right-tab">
6454 &#x25B5;<a href="#code-ref-recognize-chunk-2">56a</a> <a href="#code-ref-recognize-chunk-4">56c</a>&#x25BF;
6455 </div></font></td>
6456 </tr></tbody>
6457 </table>
6458 </p><p>
6460 <tt class="verbatim"><div class="compact-block">
6461 <tt>40 </tt>/^\\begin{lstlisting}|^\\begin{Chunk}/ {
6462 </div></tt>
6463 </p>
6464 <pre class="verbatim" xml:space="preserve">
6465 <div class="compact-block"><tt>41 </tt> if (match($0, &quot;}.*[[,] *name= *{? *([^], }]*)&quot;, line)) {
6466 <tt>42 </tt> new_chunk(line[1]);
6467 <tt>43 </tt> } else {
6468 <tt>44 </tt> new_chunk(next_chunk_name, next_chunk_args);
6469 <tt>45 </tt> }
6470 <tt>46 </tt> chunking=1;
6471 <tt>47 </tt> next;</div></pre>
6473 <tt class="verbatim"><div class="compact-block">
6474 <tt>48 </tt>}
6475 </div></tt>
6476 </p>
6477 </p><p>
6478 <a id="code-end-recognize-chunk-3"></a>
6479 </p></font>
6480 </p>
6481 <h3 id="auto-71">10.1.3<span style="margin-left: 1em"></span>TeXmacs</h3>
6484 </p>
6486 <font size="-1"><p>
6487 <a id="code-label-recognize-chunk-4"></a>
6488 <table style="width: 100%" id="code-ref-recognize-chunk-4">
6489 <tbody><tr>
6490 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6491 <font color="blue">recognize-chunk</font>[4]() &uArr;<a href="#code-ref-recognize-chunk-1">55a</a>,
6492 lang=<font color="blue"></font> + &equiv;
6493 </div><div class="right-tab">
6494 &#x25B5;<a href="#code-ref-recognize-chunk-3">56b</a> <a href="#code-ref-recognize-chunk-5">57a</a>&#x22B3;
6495 </div></font></td>
6496 </tr></tbody>
6497 </table>
6498 </p><p>
6500 <tt class="verbatim"><div class="compact-block">
6501 <tt>49 </tt>#===
6502 </div></tt>
6503 </p>
6504 <pre class="verbatim" xml:space="preserve">
6505 <div class="compact-block"><tt>50 </tt>/^ *\|____________*/ &amp;&amp; texmacs_chunking {
6506 <tt>51 </tt> active_chunk=&quot;&quot;;
6507 <tt>52 </tt> texmacs_chunking=0;
6508 <tt>53 </tt> chunking=0;
6509 <tt>54 </tt>}
6510 <tt>55 </tt>/^ *\|\/\\/ &amp;&amp; texmacs_chunking {
6511 <tt>56 </tt> texmacs_chunking=0;
6512 <tt>57 </tt> chunking=0;
6513 <tt>58 </tt> active_chunk=&quot;&quot;;
6514 <tt>59 </tt>}
6515 <tt>60 </tt>texmacs_chunk=0;
6516 <tt>61 </tt>/^ *[1-9][0-9]* *\| / {
6517 <tt>62 </tt> if (texmacs_chunking) {
6518 <tt>63 </tt> chunking=1;
6519 <tt>64 </tt> texmacs_chunk=1;
6520 <tt>65 </tt> gsub(&quot;^ *[1-9][0-9]* *\\| &quot;, &quot;&quot;)
6521 <tt>66 </tt> }
6522 <tt>67 </tt>}
6523 <tt>68 </tt>/^ *\.\/\\/ &amp;&amp; texmacs_chunking {
6524 <tt>69 </tt> next;
6525 <tt>70 </tt>}
6526 <tt>71 </tt>/^ *__*$/ &amp;&amp; texmacs_chunking {
6527 <tt>72 </tt> next;
6528 <tt>73 </tt>}
6529 <tt>74 </tt>
6530 <tt>75 </tt>texmacs_chunking {
6531 <tt>76 </tt> if (! texmacs_chunk) {
6532 <tt>77 </tt> # must be a texmacs continued line
6533 <tt>78 </tt> chunking=1;
6534 <tt>79 </tt> texmacs_chunk=1;
6535 <tt>80 </tt> }
6536 <tt>81 </tt>}
6537 <tt>82 </tt>! texmacs_chunk {
6538 <tt>83 </tt># texmacs_chunking=0;
6539 <tt>84 </tt> chunking=0;
6540 <tt>85 </tt>}
6541 <tt>86 </tt></div></pre>
6543 <tt class="verbatim"><div class="compact-block">
6544 <tt>87 </tt>#===
6545 </div></tt>
6546 </p>
6547 </p><p>
6548 <a id="code-end-recognize-chunk-4"></a>
6549 </p></font>
6550 </p>
6551 <h3 id="auto-72">10.1.4<span style="margin-left: 1em"></span>Noweb</h3>
6553 We recognize notangle style chunks too:
6554 </p>
6556 <font size="-1"><p>
6557 <a id="code-label-recognize-chunk-5"></a>
6558 <table style="width: 100%" id="code-ref-recognize-chunk-5">
6559 <tbody><tr>
6560 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6561 <font color="blue">recognize-chunk</font>[5]() &uArr;<a href="#code-ref-recognize-chunk-1">55a</a>,
6562 lang=<font color="blue">awk</font> + &equiv;
6563 </div><div class="right-tab">
6564 &#x22B2;<a href="#code-ref-recognize-chunk-4">56c</a> <a href="#code-ref-recognize-chunk-6">58a</a>&#x22B3;
6565 </div></font></td>
6566 </tr></tbody>
6567 </table>
6568 </p><p>
6570 <tt class="verbatim"><div class="compact-block">
6571 <tt>88 </tt>/^[&lt;]&lt;.*[&gt;]&gt;=/ {
6572 </div></tt>
6573 </p>
6574 <pre class="verbatim" xml:space="preserve">
6575 <div class="compact-block"><tt>89 </tt> if (match($0, &quot;^[&lt;]&lt;(.*)[&gt;]&gt;= *$&quot;, line)) {
6576 <tt>90 </tt> chunking=1;
6577 <tt>91 </tt> notangle_mode=1;
6578 <tt>92 </tt> new_chunk(line[1]);
6579 <tt>93 </tt> next;
6580 <tt>94 </tt> }</div></pre>
6582 <tt class="verbatim"><div class="compact-block">
6583 <tt>95 </tt>}
6584 </div></tt>
6585 </p>
6586 </p><p>
6587 <a id="code-end-recognize-chunk-5"></a>
6588 </p></font>
6589 </p>
6590 <h2 id="auto-73">10.2<span style="margin-left: 1em"></span>Chunk end</h2>
6592 Likewise, we need to recognize when a chunk ends.
6593 </p>
6594 <h3 id="auto-74">10.2.1<span style="margin-left: 1em"></span>lstlistings</h3>
6597 <tt class="verbatim">e</tt>
6599 <tt class="verbatim">[e]nd{lislisting}</tt>
6600 is surrounded by square brackets so that when this document is
6601 processed, this chunk doesn't terminate early when the lstlistings
6602 package recognizes it's own end-string!
6604 <font size="-1"><div align="justify">
6605 <div style="margin-left: 0px">
6606 <div style="margin-right: 0px">
6608 . This doesn't make sense as the regex is anchored with ^,
6609 which this line does not begin with!
6610 </p>
6611 </div>
6612 </div>
6613 </div></font>
6614 </p>
6615 <span style="margin-left: 0em"></span>
6616 <a id="footnr-3"></a>
6617 <sup><a href="#footnote-3">3</a></sup>
6618 </p>
6620 <font size="-1"><p>
6621 <a id="code-label-recognize-chunk-6"></a>
6622 <table style="width: 100%" id="code-ref-recognize-chunk-6">
6623 <tbody><tr>
6624 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6625 <font color="blue">recognize-chunk</font>[6]() &uArr;<a href="#code-ref-recognize-chunk-1">55a</a>,
6626 lang=<font color="blue"></font> + &equiv;
6627 </div><div class="right-tab">
6628 &#x22B2;<a href="#code-ref-recognize-chunk-5">57a</a> <a href="#code-ref-recognize-chunk-7">58b</a>&#x25BF;
6629 </div></font></td>
6630 </tr></tbody>
6631 </table>
6632 </p><p>
6634 <tt class="verbatim"><div class="compact-block">
6635 <tt>96 </tt>/^\\[e]nd{lstlisting}|^\\[e]nd{Chunk}/ {
6636 </div></tt>
6637 </p>
6638 <pre class="verbatim" xml:space="preserve">
6639 <div class="compact-block"><tt>97 </tt> chunking=0;
6640 <tt>98 </tt> active_chunk=&quot;&quot;;
6641 <tt>99 </tt> next;</div></pre>
6643 <tt class="verbatim"><div class="compact-block">
6644 <tt>100 </tt>}
6645 </div></tt>
6646 </p>
6647 </p><p>
6648 <a id="code-end-recognize-chunk-6"></a>
6649 </p></font>
6650 </p>
6651 <h3 id="auto-75">10.2.2<span style="margin-left: 1em"></span>noweb</h3>
6653 <font size="-1"><p>
6654 <a id="code-label-recognize-chunk-7"></a>
6655 <table style="width: 100%" id="code-ref-recognize-chunk-7">
6656 <tbody><tr>
6657 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6658 <font color="blue">recognize-chunk</font>[7]() &uArr;<a href="#code-ref-recognize-chunk-1">55a</a>,
6659 lang=<font color="blue"></font> + &equiv;
6660 </div><div class="right-tab">
6661 &#x25B5;<a href="#code-ref-recognize-chunk-6">58a</a> <a href="#code-ref-recognize-chunk-8">58c</a>&#x25BF;
6662 </div></font></td>
6663 </tr></tbody>
6664 </table>
6665 </p><p>
6667 <tt class="verbatim"><div class="compact-block">
6668 <tt>101 </tt>/^@ *$/ {
6669 </div></tt>
6670 </p>
6671 <pre class="verbatim" xml:space="preserve">
6672 <div class="compact-block"><tt>102 </tt> chunking=0;
6673 <tt>103 </tt> active_chunk=&quot;&quot;;</div></pre>
6675 <tt class="verbatim"><div class="compact-block">
6676 <tt>104 </tt>}
6677 </div></tt>
6678 </p>
6679 </p><p>
6680 <a id="code-end-recognize-chunk-7"></a>
6681 </p></font>
6682 </p>
6684 All other recognizers are only of effect if we are chunking; there's no
6685 point in looking at lines if they aren't part of a chunk, so we just
6686 ignore them as efficiently as we can.
6687 </p>
6689 <font size="-1"><p>
6690 <a id="code-label-recognize-chunk-8"></a>
6691 <table style="width: 100%" id="code-ref-recognize-chunk-8">
6692 <tbody><tr>
6693 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6694 <font color="blue">recognize-chunk</font>[8]() &uArr;<a href="#code-ref-recognize-chunk-1">55a</a>,
6695 lang=<font color="blue"></font> + &equiv;
6696 </div><div class="right-tab">
6697 &#x25B5;<a href="#code-ref-recognize-chunk-7">58b</a> <a href="#code-ref-recognize-chunk-9">58d</a>&#x25BF;
6698 </div></font></td>
6699 </tr></tbody>
6700 </table>
6701 </p><p>
6702 <tt class="verbatim"><div class="compact-block">
6703 <tt>105 </tt>! chunking { next; }
6704 </div></tt>
6705 </p><p>
6706 <a id="code-end-recognize-chunk-8"></a>
6707 </p></font>
6708 </p>
6709 <h2 id="auto-76">10.3<span style="margin-left: 1em"></span>Chunk contents</h2>
6711 Chunk contents are any lines read while <tt class="verbatim">chunking</tt> is
6712 true. Some chunk contents are special in that they refer to other
6713 chunks, and will be replaced by the contents of these chunks when the
6714 file is generated.
6715 </p>
6717 <a id="sub:ORS-chunk-text"></a>
6718 We add the output record separator
6719 <tt class="verbatim">ORS</tt>
6720 to the line now, because we will set
6721 <tt class="verbatim">ORS</tt>
6722 to the empty string when we generate the output
6724 <font size="-1"><div align="justify">
6725 <div style="margin-left: 0px">
6726 <div style="margin-right: 0px">
6728 . So that we can partial print lines using
6729 <tt class="verbatim">print</tt>
6730 instead of
6731 <tt class="verbatim">printf</tt>
6733 <table style="display: inline; vertical-align: -0.55em">
6734 <tbody><tr>
6735 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: This does't make sense</td>
6736 </tr></tbody>
6737 </table>
6738 </p>
6739 </div>
6740 </div>
6741 </div></font>
6742 </p>
6743 <span style="margin-left: 0em"></span>
6744 <a id="footnr-4"></a>
6745 <sup><a href="#footnote-4">4</a></sup>
6747 </p>
6749 <font size="-1"><p>
6750 <a id="code-label-recognize-chunk-9"></a>
6751 <table style="width: 100%" id="code-ref-recognize-chunk-9">
6752 <tbody><tr>
6753 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="recognize-chunk"></a><font size="-1"><div class="left-tab">
6754 <font color="blue">recognize-chunk</font>[9]() &uArr;<a href="#code-ref-recognize-chunk-1">55a</a>,
6755 lang=<font color="blue"></font> + &equiv;
6756 </div><div class="right-tab">
6757 &#x25B5;<a href="#code-ref-recognize-chunk-8">58c</a>
6758 </div></font></td>
6759 </tr></tbody>
6760 </table>
6761 </p><p>
6763 <tt class="verbatim"><div class="compact-block">
6764 <tt>106 </tt>length(active_chunk) {
6765 </div></tt>
6766 </p>
6767 <pre class="verbatim" xml:space="preserve">
6768 <div class="compact-block"><tt>107 </tt> =&lt;\chunkref{process-chunk-tabs}&gt;
6769 <tt>108 </tt> =&lt;\chunkref{process-chunk}&gt;</div></pre>
6771 <tt class="verbatim"><div class="compact-block">
6772 <tt>109 </tt>}
6773 </div></tt>
6774 </p>
6775 </p><table style="width: 100%" id="code-end-recognize-chunk-9">
6776 <tbody><tr>
6777 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6778 </tr></tbody>
6779 </table></font>
6780 </p>
6782 If a chunk just consisted of plain text, we could handle the chunk like
6783 this:
6784 </p>
6786 <font size="-1"><p>
6787 <a id="code-label-process-chunk-simple-1"></a>
6788 <table style="width: 100%" id="code-ref-process-chunk-simple-1">
6789 <tbody><tr>
6790 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="process-chunk-simple"></a><font size="-1"><font color="blue">process-chunk-simple</font>[1](),
6791 lang=<font color="blue"></font> &equiv;</font></td>
6792 </tr></tbody>
6793 </table>
6794 </p><p>
6795 <tt class="verbatim"><div class="compact-block">
6796 <tt>1 </tt>chunk_line(active_chunk, $0 ORS);
6797 </div></tt>
6798 </p><table style="width: 100%" id="code-end-process-chunk-simple-1">
6799 <tbody><tr>
6800 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6801 </tr></tbody>
6802 </table></font>
6803 </p>
6805 but in fact a chunk can include references to other chunks. Chunk
6806 includes are traditionally written as <tt class="verbatim">&lt;&lt;chunk-name&gt;&gt;</tt>
6807 but we support other variations, some of which are more suitable for
6808 particular editing systems.
6809 </p>
6811 However, we also process tabs at this point, a tab at input can be
6812 replaced by a number of spaces defined by the <tt class="verbatim">tabs</tt>
6813 variable, set by the <tt class="verbatim">-T</tt> option. Of course this is poor
6814 tab behaviour, we should probably have the option to use proper counted
6815 tab-stops and process this on output.
6816 </p>
6818 <font size="-1"><p>
6819 <a id="code-label-process-chunk-tabs-1"></a>
6820 <table style="width: 100%" id="code-ref-process-chunk-tabs-1">
6821 <tbody><tr>
6822 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="process-chunk-tabs"></a><font size="-1"><font color="blue">process-chunk-tabs</font>[1](),
6823 lang=<font color="blue"></font> &equiv;</font></td>
6824 </tr></tbody>
6825 </table>
6826 </p><p>
6828 <tt class="verbatim"><div class="compact-block">
6829 <tt>1 </tt>if (length(tabs)) {
6830 </div></tt>
6831 </p>
6832 <pre class="verbatim" xml:space="preserve">
6833 <div class="compact-block"><tt>2 </tt> gsub(&quot;\t&quot;, tabs);</div></pre>
6835 <tt class="verbatim"><div class="compact-block">
6836 <tt>3 </tt>}
6837 </div></tt>
6838 </p>
6839 </p><table style="width: 100%" id="code-end-process-chunk-tabs-1">
6840 <tbody><tr>
6841 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6842 </tr></tbody>
6843 </table></font>
6844 </p>
6845 <h3 id="auto-77"><a id="sub:lst-listings-includes"></a>10.3.1<span style="margin-left: 1em"></span>lstlistings</h3>
6847 If <tt class="verbatim">\lstset{escapeinside={=&lt;}{&gt;}}</tt> is set, then we
6848 can use <tt class="verbatim">=&lt;\chunkref{chunk-name}&gt;</tt> in listings. The
6849 sequence <tt class="verbatim">=&lt;</tt> was chosen because:
6850 </p>
6851 <ol>
6852 <li>
6853 it is a better mnemonic than <tt class="verbatim">&lt;&lt;chunk-name&gt;&gt;</tt>
6854 in that the <tt class="verbatim">=</tt> sign signifies equivalence or
6855 substitutability.
6856 </li>
6857 <li>
6858 and because <tt class="verbatim">=&lt;</tt> is not valid in C or any language I
6859 can think of.
6860 </li>
6861 <li>
6862 and also because lstlistings doesn't like <tt class="verbatim">&gt;&gt;</tt> as
6863 an end delimiter for the <em>texcl</em> escape, so we must make do
6864 with a single <tt class="verbatim">&gt;</tt> which is better complemented by <tt
6865 class="verbatim">=&lt;</tt> than by <tt class="verbatim">&lt;&lt;</tt>.
6866 </li>
6867 </ol>
6869 Unfortunately the <tt class="verbatim">=&lt;...&gt;</tt> that we use re-enters a
6870 LaTeX parsing mode in which some characters are special, e.g. <tt class="verbatim">#
6871 \</tt> and so these cause trouble if used in arguments to <tt class="verbatim">\chunkref</tt>.
6872 At some point I must fix the LaTeX command <tt class="verbatim">\chunkref</tt> so
6873 that it can accept these literally, but until then, when writing
6874 chunkref argumemts that need these characters, I must use the forms <tt
6875 class="verbatim">\textbackslash{}</tt> and <tt class="verbatim">\#</tt>; so I also define a
6876 hacky chunk <tt class="verbatim">delatex</tt> to be used further on whose purpose
6877 it is to remove these from any arguments parsed by fangle.
6878 </p>
6880 <font size="-1"><p>
6881 <a id="code-label-delatex-1"></a>
6882 <table style="width: 100%" id="code-ref-delatex-1">
6883 <tbody><tr>
6884 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="delatex"></a><font size="-1"><font color="blue">delatex</font>[1](<font
6885 color="blue">text</font>), lang=<font color="blue"></font> &equiv;</font></td>
6886 </tr></tbody>
6887 </table>
6888 </p><p>
6890 <tt class="verbatim"><div class="compact-block">
6891 <tt>1 </tt># FILTHY HACK
6892 </div></tt>
6893 </p>
6894 <pre class="verbatim" xml:space="preserve">
6895 <div class="compact-block"><tt>2 </tt>gsub(&quot;\\\\#&quot;, &quot;#&quot;, ${text});
6896 <tt>3 </tt>gsub(&quot;\\\\textbackslash{}&quot;, &quot;\\&quot;, ${text});</div></pre>
6898 <tt class="verbatim"><div class="compact-block">
6899 <tt>4 </tt>gsub(&quot;\\\\\\^&quot;, &quot;^&quot;, ${text});
6900 </div></tt>
6901 </p>
6902 </p><table style="width: 100%" id="code-end-delatex-1">
6903 <tbody><tr>
6904 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
6905 </tr></tbody>
6906 </table></font>
6907 </p>
6909 As each chunk line may contain more than one chunk include, we will
6910 split out chunk includes in an iterative fashion
6912 <font size="-1"><div align="justify">
6913 <div style="margin-left: 0px">
6914 <div style="margin-right: 0px">
6916 . Contrary to our use of split when substituting parameters in
6917 chapter <a href="#Here-we-split">?</a>
6918 </p>
6919 </div>
6920 </div>
6921 </div></font>
6922 </p>
6923 <span style="margin-left: 0em"></span>
6924 <a id="footnr-5"></a>
6925 <sup><a href="#footnote-5">5</a></sup>
6927 </p>
6929 First, as long as the chunk contains a <tt class="verbatim">\chunkref</tt> command
6930 we take as much as we can up to the first <tt class="verbatim">\chunkref</tt>
6931 command.
6932 </p>
6934 <font size="-1"><p>
6935 <a id="code-label-process-chunk-1"></a>
6936 <table style="width: 100%" id="code-ref-process-chunk-1">
6937 <tbody><tr>
6938 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="process-chunk"></a><font size="-1"><div class="left-tab">
6939 <font color="blue">process-chunk</font>[1](), lang=<font color="blue"></font>
6940 &equiv;
6941 </div><div class="right-tab">
6942 <a href="#code-ref-process-chunk-2">60a</a>&#x22B3;
6943 </div></font></td>
6944 </tr></tbody>
6945 </table>
6946 </p><p>
6948 <tt class="verbatim"><div class="compact-block">
6949 <tt>1 </tt>chunk = $0;
6950 </div></tt>
6951 </p>
6952 <pre class="verbatim" xml:space="preserve">
6953 <div class="compact-block"><tt>2 </tt>indent = 0;
6954 <tt>3 </tt>while(match(chunk,&quot;(\xC2\xAB)([^\xC2]*) [^\xC2]*\xC2\xBB&quot;, line) ||
6955 <tt>4 </tt> match(chunk,
6956 <tt>5 </tt> &quot;([=]&lt;\\\\chunkref{([^}&gt;]*)}(\\(.*\\)|)&gt;|&lt;&lt;([a-zA-Z_][-a-zA-Z0-9_]*)&gt;&gt;)&quot;,
6957 <tt>6 </tt> line)\
6958 <tt>7 </tt>) {</div></pre>
6960 <tt class="verbatim"><div class="compact-block">
6961 <tt>8 </tt> chunklet = substr(chunk, 1, RSTART - 1);
6962 </div></tt>
6963 </p>
6964 </p><p>
6965 <a id="code-end-process-chunk-1"></a>
6966 </p></font>
6967 </p>
6969 We keep track of the indent count, by counting the number of literal
6970 characters found. We can then preserve this indent on each output line
6971 when multi-line chunks are expanded.
6972 </p>
6974 We then process this first part literal text, and set the chunk which is
6975 still to be processed to be the text after the <tt class="verbatim">\chunkref</tt>
6976 command, which we will process next as we continue around the loop.
6977 </p>
6979 <font size="-1"><p>
6980 <a id="code-label-process-chunk-2"></a>
6981 <table style="width: 100%" id="code-ref-process-chunk-2">
6982 <tbody><tr>
6983 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="process-chunk"></a><font size="-1"><div class="left-tab">
6984 <font color="blue">process-chunk</font>[2]() &uArr;<a href="#code-ref-process-chunk-1">59c</a>,
6985 lang=<font color="blue"></font> + &equiv;
6986 </div><div class="right-tab">
6987 &#x22B2;<a href="#code-ref-process-chunk-1">59c</a> <a href="#code-ref-process-chunk-3">60b</a>&#x25BF;
6988 </div></font></td>
6989 </tr></tbody>
6990 </table>
6991 </p><p>
6993 <tt class="verbatim"><div class="compact-block">
6994 <tt>9 </tt> indent += length(chunklet);
6995 </div></tt>
6996 </p>
6997 <pre class="verbatim" xml:space="preserve">
6998 <div class="compact-block"><tt>10 </tt> chunk_line(active_chunk, chunklet);</div></pre>
7000 <tt class="verbatim"><div class="compact-block">
7001 <tt>11 </tt> chunk = substr(chunk, RSTART + RLENGTH);
7002 </div></tt>
7003 </p>
7004 </p><p>
7005 <a id="code-end-process-chunk-2"></a>
7006 </p></font>
7007 </p>
7009 We then consider the type of chunk command we have found, whether it is
7010 the fangle style command beginning with <tt class="verbatim">=&lt;</tt> the older
7011 notangle style beginning with <tt class="verbatim">&lt;&lt;</tt>.
7012 </p>
7014 Fangle chunks may have parameters contained within square brackets.
7015 These will be matched in <tt class="verbatim">line[3]</tt> and are considered at
7016 this stage of processing to be part of the name of the chunk to be
7017 included.
7018 </p>
7020 <font size="-1"><p>
7021 <a id="code-label-process-chunk-3"></a>
7022 <table style="width: 100%" id="code-ref-process-chunk-3">
7023 <tbody><tr>
7024 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="process-chunk"></a><font size="-1"><div class="left-tab">
7025 <font color="blue">process-chunk</font>[3]() &uArr;<a href="#code-ref-process-chunk-1">59c</a>,
7026 lang=<font color="blue"><p>
7028 </p></font> + &equiv;
7029 </div><div class="right-tab">
7030 &#x25B5;<a href="#code-ref-process-chunk-2">60a</a> <a href="#code-ref-process-chunk-4">60c</a>&#x25BF;
7031 </div></font></td>
7032 </tr></tbody>
7033 </table>
7034 </p><p>
7036 <tt class="verbatim"><div class="compact-block">
7037 <tt>12 </tt> if (substr(line[1], 1, 1) == &quot;=&quot;) {
7038 </div></tt>
7039 </p>
7040 <pre class="verbatim" xml:space="preserve">
7041 <div class="compact-block"><tt>13 </tt> # chunk name up to }
7042 <tt>14 </tt> =&lt;\chunkref{delatex}(line[3])&gt;
7043 <tt>15 </tt> chunk_include(active_chunk, line[2] line[3], indent);
7044 <tt>16 </tt> } else if (substr(line[1], 1, 1) == &quot;&lt;&quot;) {
7045 <tt>17 </tt> chunk_include(active_chunk, line[4], indent);
7046 <tt>18 </tt> } else if (line[1] == &quot;\xC2\xAB&quot;) {
7047 <tt>19 </tt> chunk_include(active_chunk, line[2], indent);
7048 <tt>20 </tt> } else {
7049 <tt>21 </tt> error(&quot;Unknown chunk fragment: &quot; line[1]);</div></pre>
7051 <tt class="verbatim"><div class="compact-block">
7052 <tt>22 </tt> }
7053 </div></tt>
7054 </p>
7055 </p><p>
7056 <a id="code-end-process-chunk-3"></a>
7057 </p></font>
7058 </p>
7060 The loop will continue until there are no more chunkref statements in
7061 the text, at which point we process the final part of the chunk.
7062 </p>
7064 <font size="-1"><p>
7065 <a id="code-label-process-chunk-4"></a>
7066 <table style="width: 100%" id="code-ref-process-chunk-4">
7067 <tbody><tr>
7068 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="process-chunk"></a><font size="-1"><div class="left-tab">
7069 <font color="blue">process-chunk</font>[4]() &uArr;<a href="#code-ref-process-chunk-1">59c</a>,
7070 lang=<font color="blue"></font> + &equiv;
7071 </div><div class="right-tab">
7072 &#x25B5;<a href="#code-ref-process-chunk-3">60b</a> <a href="#code-ref-process-chunk-5">60d</a>&#x25BF;
7073 </div></font></td>
7074 </tr></tbody>
7075 </table>
7076 </p><p>
7078 <tt class="verbatim"><div class="compact-block">
7079 <tt>23 </tt>}
7080 </div></tt>
7081 </p>
7082 <pre class="verbatim" xml:space="preserve">
7083 <div class="compact-block"></div></pre>
7085 <tt class="verbatim"><div class="compact-block">
7086 <tt>24 </tt>chunk_line(active_chunk, chunk);
7087 </div></tt>
7088 </p>
7089 </p><p>
7090 <a id="code-end-process-chunk-4"></a>
7091 </p></font>
7092 </p>
7094 <a id="lone-newline"></a>We add the newline character as a chunklet on it's own, to
7095 make it easier to detect new lines and thus manage indentation when
7096 processing the output.
7097 </p>
7099 <font size="-1"><p>
7100 <a id="code-label-process-chunk-5"></a>
7101 <table style="width: 100%" id="code-ref-process-chunk-5">
7102 <tbody><tr>
7103 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="process-chunk"></a><font size="-1"><div class="left-tab">
7104 <font color="blue">process-chunk</font>[5]() &uArr;<a href="#code-ref-process-chunk-1">59c</a>,
7105 lang=<font color="blue"><p>
7107 </p></font> + &equiv;
7108 </div><div class="right-tab">
7109 &#x25B5;<a href="#code-ref-process-chunk-4">60c</a>
7110 </div></font></td>
7111 </tr></tbody>
7112 </table>
7113 </p><p>
7114 <tt class="verbatim"><div class="compact-block">
7115 <tt>25 </tt>chunk_line(active_chunk, &quot;\n&quot;);
7116 </div></tt>
7117 </p><table style="width: 100%" id="code-end-process-chunk-5">
7118 <tbody><tr>
7119 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7120 </tr></tbody>
7121 </table></font>
7122 </p>
7124 We will also permit a chunk-part number to follow in square brackets, so
7125 that <tt class="verbatim">=&lt;\chunkref{chunk-name[1]}&gt;</tt> will refer to the
7126 first part only. This can make it easy to include a C function prototype
7127 in a header file, if the first part of the chunk is just the function
7128 prototype without the trailing semi-colon. The header file would include
7129 the prototype with the trailing semi-colon, like this:
7130 </p>
7131 <pre class="verbatim" xml:space="preserve">
7132 =&lt;\chunkref{chunk-name[1]}&gt;</pre>
7134 This is handled in section <a href="#sub:Chunk-parts">12.1.1</a>
7135 </p>
7137 We should perhaps introduce a notion of language specific chunk options;
7138 so that perhaps we could specify:
7139 </p>
7140 <pre class="verbatim" xml:space="preserve">
7141 =&lt;\chunkref{chunk-name[function-declaration]}</pre>
7143 which applies a transform
7144 <tt class="verbatim">function-declaration</tt>
7145 to the chunk &ndash;- which in this case would extract a function
7146 prototype from a function.
7147 <table style="display: inline; vertical-align: -0.55em">
7148 <tbody><tr>
7149 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid; border-left: 1px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid" bgcolor="#ffdfdf">To do: Do it</td>
7150 </tr></tbody>
7151 </table>
7152 </p>
7153 <h1 id="auto-78">Chapter 11<br></br>Processing Options</h1>
7155 At the start, first we set the default options.
7156 </p>
7158 <font size="-1"><p>
7159 <a id="code-label-default-options-1"></a>
7160 <table style="width: 100%" id="code-ref-default-options-1">
7161 <tbody><tr>
7162 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="default-options"></a><font size="-1"><font color="blue">default-options</font>[1](),
7163 lang=<font color="blue"></font> &equiv;</font></td>
7164 </tr></tbody>
7165 </table>
7166 </p><p>
7168 <tt class="verbatim"><div class="compact-block">
7169 <tt>1 </tt>debug=0;
7170 </div></tt>
7171 </p>
7172 <pre class="verbatim" xml:space="preserve">
7173 <div class="compact-block"><tt>2 </tt>linenos=0;
7174 <tt>3 </tt>notangle_mode=0;
7175 <tt>4 </tt>root=&quot;*&quot;;</div></pre>
7177 <tt class="verbatim"><div class="compact-block">
7178 <tt>5 </tt>tabs = &quot;&quot;;
7179 </div></tt>
7180 </p>
7181 </p><table style="width: 100%" id="code-end-default-options-1">
7182 <tbody><tr>
7183 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7184 </tr></tbody>
7185 </table></font>
7186 </p>
7188 Then we use getopt the standard way, and null out ARGV afterwards in the
7189 normal AWK fashion.
7190 </p>
7192 <font size="-1"><p>
7193 <a id="code-label-read-options-1"></a>
7194 <table style="width: 100%" id="code-ref-read-options-1">
7195 <tbody><tr>
7196 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="read-options"></a><font size="-1"><font color="blue">read-options</font>[1](),
7197 lang=<font color="blue"></font> &equiv;</font></td>
7198 </tr></tbody>
7199 </table>
7200 </p><p>
7202 <tt class="verbatim"><div class="compact-block">
7203 <tt>1 </tt>Optind = 1 # skip ARGV[0]
7204 </div></tt>
7205 </p>
7206 <pre class="verbatim" xml:space="preserve">
7207 <div class="compact-block"><tt>2 </tt>while(getopt(ARGC, ARGV, &quot;R:LdT:hr&quot;)!=-1) {
7208 <tt>3 </tt> =&lt;\chunkref{handle-options}&gt;
7209 <tt>4 </tt>}</div></pre>
7211 <tt class="verbatim"><div class="compact-block">
7212 <tt>5 </tt>for (i=1; i&lt;Optind; i++) { ARGV[i]=&quot;&quot;; }
7213 </div></tt>
7214 </p>
7215 </p><table style="width: 100%" id="code-end-read-options-1">
7216 <tbody><tr>
7217 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7218 </tr></tbody>
7219 </table></font>
7220 </p>
7222 This is how we handle our options:
7223 </p>
7225 <font size="-1"><p>
7226 <a id="code-label-handle-options-1"></a>
7227 <table style="width: 100%" id="code-ref-handle-options-1">
7228 <tbody><tr>
7229 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="handle-options"></a><font size="-1"><font color="blue">handle-options</font>[1](),
7230 lang=<font color="blue"></font> &equiv;</font></td>
7231 </tr></tbody>
7232 </table>
7233 </p><p>
7235 <tt class="verbatim"><div class="compact-block">
7236 <tt>1 </tt>if (Optopt == &quot;R&quot;) root = Optarg;
7237 </div></tt>
7238 </p>
7239 <pre class="verbatim" xml:space="preserve">
7240 <div class="compact-block"><tt>2 </tt>else if (Optopt == &quot;r&quot;) root=&quot;&quot;;
7241 <tt>3 </tt>else if (Optopt == &quot;L&quot;) linenos = 1;
7242 <tt>4 </tt>else if (Optopt == &quot;d&quot;) debug = 1;
7243 <tt>5 </tt>else if (Optopt == &quot;T&quot;) tabs = indent_string(Optarg+0);
7244 <tt>6 </tt>else if (Optopt == &quot;h&quot;) help();</div></pre>
7246 <tt class="verbatim"><div class="compact-block">
7247 <tt>7 </tt>else if (Optopt == &quot;?&quot;) help();
7248 </div></tt>
7249 </p>
7250 </p><table style="width: 100%" id="code-end-handle-options-1">
7251 <tbody><tr>
7252 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7253 </tr></tbody>
7254 </table></font>
7255 </p>
7257 We do all of this at the beginning of the program
7258 </p>
7260 <font size="-1"><p>
7261 <a id="code-label-begin-1"></a>
7262 <table style="width: 100%" id="code-ref-begin-1">
7263 <tbody><tr>
7264 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="begin"></a><font size="-1"><font color="blue">begin</font>[1](),
7265 lang=<font color="blue"></font> &equiv;</font></td>
7266 </tr></tbody>
7267 </table>
7268 </p><p>
7270 <tt class="verbatim"><div class="compact-block">
7271 <tt>1 </tt>BEGIN {
7272 </div></tt>
7273 </p>
7274 <pre class="verbatim" xml:space="preserve">
7275 <div class="compact-block"><tt>2 </tt> =&lt;\chunkref{constants}&gt;
7276 <tt>3 </tt> =&lt;\chunkref{mode-definitions}&gt;
7277 <tt>4 </tt> =&lt;\chunkref{default-options}&gt;
7278 <tt>5 </tt>
7279 <tt>6 </tt> =&lt;\chunkref{read-options}&gt;</div></pre>
7281 <tt class="verbatim"><div class="compact-block">
7282 <tt>7 </tt>}
7283 </div></tt>
7284 </p>
7285 </p><table style="width: 100%" id="code-end-begin-1">
7286 <tbody><tr>
7287 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7288 </tr></tbody>
7289 </table></font>
7290 </p>
7292 And have a simple help function
7293 </p>
7295 <font size="-1"><p>
7296 <a id="code-label-help()-1"></a>
7297 <table style="width: 100%" id="code-ref-help()-1">
7298 <tbody><tr>
7299 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="help()"></a><font size="-1"><font color="blue">help()</font>[1](),
7300 lang=<font color="blue"></font> &equiv;</font></td>
7301 </tr></tbody>
7302 </table>
7303 </p><p>
7305 <tt class="verbatim"><div class="compact-block">
7306 <tt>1 </tt>function help() {
7307 </div></tt>
7308 </p>
7309 <pre class="verbatim" xml:space="preserve">
7310 <div class="compact-block"><tt>2 </tt> print &quot;Usage:&quot;
7311 <tt>3 </tt> print &quot; fangle [-L] -R&lt;rootname&gt; [source.tex ...]&quot;
7312 <tt>4 </tt> print &quot; fangle -r [source.tex ...]&quot;
7313 <tt>5 </tt> print &quot; If the filename, source.tex is not specified then stdin is used&quot;
7314 <tt>6 </tt> print
7315 <tt>7 </tt> print &quot;-L causes the C statement: #line &lt;lineno&gt; \&quot;filename\&quot;&quot; to be issued&quot;
7316 <tt>8 </tt> print &quot;-R causes the named root to be written to stdout&quot;
7317 <tt>9 </tt> print &quot;-r lists all roots in the file (even those used elsewhere)&quot;
7318 <tt>10 </tt> exit 1;</div></pre>
7320 <tt class="verbatim"><div class="compact-block">
7321 <tt>11 </tt>}
7322 </div></tt>
7323 </p>
7324 </p><table style="width: 100%" id="code-end-help()-1">
7325 <tbody><tr>
7326 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7327 </tr></tbody>
7328 </table></font>
7329 </p>
7330 <h1 id="auto-79">Chapter 12<br></br>Generating the Output</h1>
7332 We generate output by calling output_chunk, or listing the chunk names.
7333 </p>
7335 <font size="-1"><p>
7336 <a id="code-label-generate-output-1"></a>
7337 <table style="width: 100%" id="code-ref-generate-output-1">
7338 <tbody><tr>
7339 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="generate-output"></a><font size="-1"><font color="blue">generate-output</font>[1](),
7340 lang=<font color="blue"></font> &equiv;</font></td>
7341 </tr></tbody>
7342 </table>
7343 </p><p>
7345 <tt class="verbatim"><div class="compact-block">
7346 <tt>1 </tt>if (length(root)) output_chunk(root);
7347 </div></tt>
7348 </p>
7349 <pre class="verbatim" xml:space="preserve">
7350 <div class="compact-block"></div></pre>
7352 <tt class="verbatim"><div class="compact-block">
7353 <tt>2 </tt>else output_chunk_names();
7354 </div></tt>
7355 </p>
7356 </p><table style="width: 100%" id="code-end-generate-output-1">
7357 <tbody><tr>
7358 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7359 </tr></tbody>
7360 </table></font>
7361 </p>
7363 We also have some other output debugging:
7364 </p>
7366 <font size="-1"><p>
7367 <a id="code-label-debug-output-1"></a>
7368 <table style="width: 100%" id="code-ref-debug-output-1">
7369 <tbody><tr>
7370 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="debug-output"></a><font size="-1"><font color="blue">debug-output</font>[1](),
7371 lang=<font color="blue"></font> &equiv;</font></td>
7372 </tr></tbody>
7373 </table>
7374 </p><p>
7376 <tt class="verbatim"><div class="compact-block">
7377 <tt>1 </tt>if (debug) {
7378 </div></tt>
7379 </p>
7380 <pre class="verbatim" xml:space="preserve">
7381 <div class="compact-block"><tt>2 </tt> print &quot;------ chunk names &quot;
7382 <tt>3 </tt> output_chunk_names();
7383 <tt>4 </tt> print &quot;====== chunks&quot;
7384 <tt>5 </tt> output_chunks();
7385 <tt>6 </tt> print &quot;++++++ debug&quot;
7386 <tt>7 </tt> for (a in chunks) {
7387 <tt>8 </tt> print a &quot;=&quot; chunks[a];
7388 <tt>9 </tt> }</div></pre>
7390 <tt class="verbatim"><div class="compact-block">
7391 <tt>10 </tt>}
7392 </div></tt>
7393 </p>
7394 </p><table style="width: 100%" id="code-end-debug-output-1">
7395 <tbody><tr>
7396 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7397 </tr></tbody>
7398 </table></font>
7399 </p>
7401 We do both of these at the end. We also set <tt class="verbatim">ORS=&quot;&quot;</tt>
7402 because each chunklet is not necessarily a complete line, and we already
7403 added <tt class="verbatim">ORS</tt> to each input line in section <a href="#sub:ORS-chunk-text">10.3</a>.
7404 </p>
7406 <font size="-1"><p>
7407 <a id="code-label-end-1"></a>
7408 <table style="width: 100%" id="code-ref-end-1">
7409 <tbody><tr>
7410 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="end"></a><font size="-1"><font color="blue">end</font>[1](),
7411 lang=<font color="blue"></font> &equiv;</font></td>
7412 </tr></tbody>
7413 </table>
7414 </p><p>
7416 <tt class="verbatim"><div class="compact-block">
7417 <tt>1 </tt>END {
7418 </div></tt>
7419 </p>
7420 <pre class="verbatim" xml:space="preserve">
7421 <div class="compact-block"><tt>2 </tt> =&lt;\chunkref{debug-output}&gt;
7422 <tt>3 </tt> ORS=&quot;&quot;;
7423 <tt>4 </tt> =&lt;\chunkref{generate-output}&gt;</div></pre>
7425 <tt class="verbatim"><div class="compact-block">
7426 <tt>5 </tt>}
7427 </div></tt>
7428 </p>
7429 </p><table style="width: 100%" id="code-end-end-1">
7430 <tbody><tr>
7431 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7432 </tr></tbody>
7433 </table></font>
7434 </p>
7436 We write chunk names like this. If we seem to be running in notangle
7437 compatibility mode, then we enclose the name like this <tt class="verbatim">&lt;&lt;name&gt;&gt;</tt>
7438 the same way notangle does:
7439 </p>
7441 <font size="-1"><p>
7442 <a id="code-label-output_chunk_names()-1"></a>
7443 <table style="width: 100%" id="code-ref-output_chunk_names()-1">
7444 <tbody><tr>
7445 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="output_chunk_names()"></a><font size="-1"><font color="blue">output_chunk_names()</font>[1](),
7446 lang=<font color="blue"></font> &equiv;</font></td>
7447 </tr></tbody>
7448 </table>
7449 </p><p>
7451 <tt class="verbatim"><div class="compact-block">
7452 <tt>1 </tt>function output_chunk_names( c, prefix, suffix)
7453 </div></tt>
7454 </p>
7455 <pre class="verbatim" xml:space="preserve">
7456 <div class="compact-block"><tt>2 </tt>{
7457 <tt>3 </tt> if (notangle_mode) {
7458 <tt>4 </tt> prefix=&quot;&lt;&lt;&quot;;
7459 <tt>5 </tt> suffix=&quot;&gt;&gt;&quot;;
7460 <tt>6 </tt> }
7461 <tt>7 </tt> for (c in chunk_names) {
7462 <tt>8 </tt> print prefix c suffix &quot;\n&quot;;
7463 <tt>9 </tt> }</div></pre>
7465 <tt class="verbatim"><div class="compact-block">
7466 <tt>10 </tt>}
7467 </div></tt>
7468 </p>
7469 </p><table style="width: 100%" id="code-end-output_chunk_names()-1">
7470 <tbody><tr>
7471 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7472 </tr></tbody>
7473 </table></font>
7474 </p>
7476 This function would write out all chunks
7477 </p>
7479 <font size="-1"><p>
7480 <a id="code-label-output_chunks()-1"></a>
7481 <table style="width: 100%" id="code-ref-output_chunks()-1">
7482 <tbody><tr>
7483 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="output_chunks()"></a><font size="-1"><font color="blue">output_chunks()</font>[1](),
7484 lang=<font color="blue"></font> &equiv;</font></td>
7485 </tr></tbody>
7486 </table>
7487 </p><p>
7489 <tt class="verbatim"><div class="compact-block">
7490 <tt>1 </tt>function output_chunks( a)
7491 </div></tt>
7492 </p>
7493 <pre class="verbatim" xml:space="preserve">
7494 <div class="compact-block"><tt>2 </tt>{
7495 <tt>3 </tt> for (a in chunk_names) {
7496 <tt>4 </tt> output_chunk(a);
7497 <tt>5 </tt> }
7498 <tt>6 </tt>}
7499 <tt>7 </tt>
7500 <tt>8 </tt>function output_chunk(chunk) {
7501 <tt>9 </tt> newline = 1;
7502 <tt>10 </tt> lineno_needed = linenos;
7503 <tt>11 </tt>
7504 <tt>12 </tt> write_chunk(chunk);
7505 <tt>13 </tt>}</div></pre>
7507 <tt class="verbatim"><div class="compact-block">
7508 <tt>14 </tt>
7509 </div></tt>
7510 </p>
7511 </p><table style="width: 100%" id="code-end-output_chunks()-1">
7512 <tbody><tr>
7513 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7514 </tr></tbody>
7515 </table></font>
7516 </p>
7517 <h2 id="auto-80">12.1<span style="margin-left: 1em"></span>Assembling the Chunks</h2>
7519 <tt class="verbatim">chunk_path</tt> holds a string consisting of the names of all
7520 the chunks that resulted in this chunk being output. It should probably
7521 also contain the source line numbers at which each inclusion also
7522 occured.
7523 </p>
7525 We first initialize the mode tracker for this chunk.
7526 </p>
7528 <font size="-1"><p>
7529 <a id="code-label-write_chunk()-1"></a>
7530 <table style="width: 100%" id="code-ref-write_chunk()-1">
7531 <tbody><tr>
7532 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write_chunk()"></a><font size="-1"><div class="left-tab">
7533 <font color="blue">write_chunk()</font>[1](), lang=<font color="blue"></font>
7534 &equiv;
7535 </div><div class="right-tab">
7536 <a href="#code-ref-write_chunk()-2">64b</a>&#x25BF;
7537 </div></font></td>
7538 </tr></tbody>
7539 </table>
7540 </p><p>
7542 <tt class="verbatim"><div class="compact-block">
7543 <tt>1 </tt>function write_chunk(chunk_name) {
7544 </div></tt>
7545 </p>
7546 <pre class="verbatim" xml:space="preserve">
7547 <div class="compact-block"><tt>2 </tt> =&lt;\chunkref{awk-delete-array}(context)&gt;
7548 <tt>3 </tt> return write_chunk_r(chunk_name, context);
7549 <tt>4 </tt>}
7550 <tt>5 </tt>
7551 <tt>6 </tt>function write_chunk_r(chunk_name, context, indent, tail,
7552 <tt>7 </tt> # optional vars
7553 <tt>8 </tt> <i>chunk_path</i>, chunk_args,
7554 <tt>9 </tt> s, r, src, new_src,
7555 <tt>10 </tt> # local vars
7556 <tt>11 </tt> chunk_params, part, max_part, part_line, frag, max_frag, text,
7557 <tt>12 </tt> chunklet, only_part, call_chunk_args, new_context)
7558 <tt>13 </tt>{</div></pre>
7560 <tt class="verbatim"><div class="compact-block">
7561 <tt>14 </tt> if (debug) debug_log(&quot;write_chunk_r(&quot;,
7562 chunk_name, &quot;)&quot;);
7563 </div></tt>
7564 </p>
7565 </p><p>
7566 <a id="code-end-write_chunk()-1"></a>
7567 </p></font>
7568 </p>
7569 <h3 id="auto-81"><a id="sub:Chunk-parts"></a>12.1.1<span style="margin-left: 1em"></span>Chunk Parts</h3>
7571 As mentioned in section <a href="#sub:lstlistings-includes">?</a>, a chunk name may contain a part
7572 specifier in square brackets, limiting the parts that should be emitted.
7573 </p>
7575 <font size="-1"><p>
7576 <a id="code-label-write_chunk()-2"></a>
7577 <table style="width: 100%" id="code-ref-write_chunk()-2">
7578 <tbody><tr>
7579 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write_chunk()"></a><font size="-1"><div class="left-tab">
7580 <font color="blue">write_chunk()</font>[2]() &uArr;<a href="#code-ref-write_chunk()-1">64a</a>,
7581 lang=<font color="blue"></font> + &equiv;
7582 </div><div class="right-tab">
7583 &#x25B5;<a href="#code-ref-write_chunk()-1">64a</a> <a href="#code-ref-write_chunk()-3">64c</a>&#x25BF;
7584 </div></font></td>
7585 </tr></tbody>
7586 </table>
7587 </p><p>
7589 <tt class="verbatim"><div class="compact-block">
7590 <tt>15 </tt> if (match(chunk_name,
7591 &quot;^(.*)\\[([0-9]*)\\]$&quot;, chunk_name_parts)) {
7592 </div></tt>
7593 </p>
7594 <pre class="verbatim" xml:space="preserve">
7595 <div class="compact-block"><tt>16 </tt> chunk_name = chunk_name_parts[1];
7596 <tt>17 </tt> only_part = chunk_name_parts[2];</div></pre>
7598 <tt class="verbatim"><div class="compact-block">
7599 <tt>18 </tt> }
7600 </div></tt>
7601 </p>
7602 </p><p>
7603 <a id="code-end-write_chunk()-2"></a>
7604 </p></font>
7605 </p>
7607 We then create a mode tracker
7608 </p>
7610 <font size="-1"><p>
7611 <a id="code-label-write_chunk()-3"></a>
7612 <table style="width: 100%" id="code-ref-write_chunk()-3">
7613 <tbody><tr>
7614 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write_chunk()"></a><font size="-1"><div class="left-tab">
7615 <font color="blue">write_chunk()</font>[3]() &uArr;<a href="#code-ref-write_chunk()-1">64a</a>,
7616 lang=<font color="blue"></font> + &equiv;
7617 </div><div class="right-tab">
7618 &#x25B5;<a href="#code-ref-write_chunk()-2">64b</a> <a href="#code-ref-write_chunk()-4">65a</a>&#x22B3;
7619 </div></font></td>
7620 </tr></tbody>
7621 </table>
7622 </p><p>
7623 <tt class="verbatim"><div class="compact-block">
7624 <tt>19 </tt> =&lt;\chunkref{new-mode-tracker}(context,
7625 chunks[chunk_name, &quot;language&quot;], &quot;&quot;)&gt;
7626 </div></tt>
7627 </p><p>
7628 <a id="code-end-write_chunk()-3"></a>
7629 </p></font>
7630 </p>
7632 We extract into <tt class="verbatim">chunk_params</tt> the names of the parameters
7633 that this chunk accepts, whose values were (optionally) passed in <tt
7634 class="verbatim">chunk_args</tt>.
7635 </p>
7637 <font size="-1"><p>
7638 <a id="code-label-write_chunk()-4"></a>
7639 <table style="width: 100%" id="code-ref-write_chunk()-4">
7640 <tbody><tr>
7641 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write_chunk()"></a><font size="-1"><div class="left-tab">
7642 <font color="blue">write_chunk()</font>[4]() &uArr;<a href="#code-ref-write_chunk()-1">64a</a>,
7643 lang=<font color="blue"></font> + &equiv;
7644 </div><div class="right-tab">
7645 &#x22B2;<a href="#code-ref-write_chunk()-3">64c</a> <a href="#code-ref-write_chunk()-5">65b</a>&#x25BF;
7646 </div></font></td>
7647 </tr></tbody>
7648 </table>
7649 </p><p>
7650 <tt class="verbatim"><div class="compact-block">
7651 <tt>20 </tt> split(chunks[chunk_name, &quot;params&quot;],
7652 chunk_params, &quot; *; *&quot;);
7653 </div></tt>
7654 </p><p>
7655 <a id="code-end-write_chunk()-4"></a>
7656 </p></font>
7657 </p>
7659 To assemble a chunk, we write out each part.
7660 </p>
7662 <font size="-1"><p>
7663 <a id="code-label-write_chunk()-5"></a>
7664 <table style="width: 100%" id="code-ref-write_chunk()-5">
7665 <tbody><tr>
7666 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write_chunk()"></a><font size="-1"><div class="left-tab">
7667 <font color="blue">write_chunk()</font>[5]() &uArr;<a href="#code-ref-write_chunk()-1">64a</a>,
7668 lang=<font color="blue"></font> + &equiv;
7669 </div><div class="right-tab">
7670 &#x25B5;<a href="#code-ref-write_chunk()-4">65a</a>
7671 </div></font></td>
7672 </tr></tbody>
7673 </table>
7674 </p><p>
7676 <tt class="verbatim"><div class="compact-block">
7677 <tt>21 </tt> if (! (chunk_name in chunk_names)) {
7678 </div></tt>
7679 </p>
7680 <pre class="verbatim" xml:space="preserve">
7681 <div class="compact-block"><tt>22 </tt> error(sprintf(_&quot;The root module &lt;&lt;%s&gt;&gt; was not defined.\nUsed by: %s&quot;,\
7682 <tt>23 </tt> chunk_name, chunk_path));
7683 <tt>24 </tt> }
7684 <tt>25 </tt>
7685 <tt>26 </tt> max_part = chunks[chunk_name, &quot;part&quot;];
7686 <tt>27 </tt> for(part = 1; part &lt;= max_part; part++) {
7687 <tt>28 </tt> if (! only_part || part == only_part) {
7688 <tt>29 </tt> =&lt;\chunkref{write-part}&gt;
7689 <tt>30 </tt> }
7690 <tt>31 </tt> }
7691 <tt>32 </tt> if (! finalize_mode_tracker(context)) {
7692 <tt>33 </tt> dump_mode_tracker(context);
7693 <tt>34 </tt> error(sprintf(_&quot;Module %s did not close context properly.\nUsed by: %s\n&quot;, chunk_name, chunk_path));
7694 <tt>35 </tt> }</div></pre>
7696 <tt class="verbatim"><div class="compact-block">
7697 <tt>36 </tt>}
7698 </div></tt>
7699 </p>
7700 </p><table style="width: 100%" id="code-end-write_chunk()-5">
7701 <tbody><tr>
7702 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7703 </tr></tbody>
7704 </table></font>
7705 </p>
7707 A part can either be a chunklet of lines, or an include of another
7708 chunk.
7709 </p>
7711 Chunks may also have parameters, specified in LaTeX style with braces
7712 after the chunk name &ndash;- looking like this in the document:
7713 chunkname{param1, param2}. Arguments are passed in square brackets: <tt
7714 class="verbatim">\chunkref{chunkname}[arg1, arg2]</tt>.
7715 </p>
7717 Before we process each part, we check that the source position hasn't
7718 changed unexpectedly, so that we can know if we need to output a new
7719 file-line directive.
7720 </p>
7722 <font size="-1"><p>
7723 <a id="code-label-write-part-1"></a>
7724 <table style="width: 100%" id="code-ref-write-part-1">
7725 <tbody><tr>
7726 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-part"></a><font size="-1"><font color="blue">write-part</font>[1](),
7727 lang=<font color="blue"></font> &equiv;</font></td>
7728 </tr></tbody>
7729 </table>
7730 </p><p>
7732 <tt class="verbatim"><div class="compact-block">
7733 <tt>1 </tt>=&lt;\chunkref{check-source-jump}&gt;
7734 </div></tt>
7735 </p>
7736 <pre class="verbatim" xml:space="preserve">
7737 <div class="compact-block"><tt>2 </tt>
7738 <tt>3 </tt>chunklet = chunks[chunk_name, &quot;part&quot;, part];
7739 <tt>4 </tt>if (chunks[chunk_name, &quot;part&quot;, part, &quot;type&quot;] == part_type_chunk) {
7740 <tt>5 </tt> =&lt;\chunkref{write-included-chunk}&gt;
7741 <tt>6 </tt>} else if (chunklet SUBSEP &quot;line&quot; in chunks) {
7742 <tt>7 </tt> =&lt;\chunkref{write-chunklets}&gt;
7743 <tt>8 </tt>} else {
7744 <tt>9 </tt> # empty last chunklet</div></pre>
7746 <tt class="verbatim"><div class="compact-block">
7747 <tt>10 </tt>}
7748 </div></tt>
7749 </p>
7750 </p><table style="width: 100%" id="code-end-write-part-1">
7751 <tbody><tr>
7752 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7753 </tr></tbody>
7754 </table></font>
7755 </p>
7757 To write an included chunk, we must detect any optional chunk arguments
7758 in parenthesis. Then we recurse calling <tt class="verbatim">write_chunk()</tt>.
7759 </p>
7761 <font size="-1"><p>
7762 <a id="code-label-write-included-chunk-1"></a>
7763 <table style="width: 100%" id="code-ref-write-included-chunk-1">
7764 <tbody><tr>
7765 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-included-chunk"></a><font size="-1"><font color="blue">write-included-chunk</font>[1](),
7766 lang=<font color="blue"></font> &equiv;</font></td>
7767 </tr></tbody>
7768 </table>
7769 </p><p>
7771 <tt class="verbatim"><div class="compact-block">
7772 <tt>1 </tt>if (match(chunklet,
7773 &quot;^([^\\[\\(]*)\\((.*)\\)$&quot;, chunklet_parts)) {
7774 </div></tt>
7775 </p>
7776 <pre class="verbatim" xml:space="preserve">
7777 <div class="compact-block"><tt>2 </tt> chunklet = chunklet_parts[1];
7778 <tt>3 </tt> parse_chunk_args(&quot;c-like&quot;, chunklet_parts[2], call_chunk_args, &quot;(&quot;);
7779 <tt>4 </tt> for (c in call_chunk_args) {
7780 <tt>5 </tt> call_chunk_args[c] = expand_chunk_args(call_chunk_args[c], chunk_params, chunk_args);
7781 <tt>6 </tt> }
7782 <tt>7 </tt>} else {
7783 <tt>8 </tt> split(&quot;&quot;, call_chunk_args);
7784 <tt>9 </tt>}
7785 <tt>10 </tt># update the transforms arrays
7786 <tt>11 </tt>new_src = mode_escaper(context, s, r, src);
7787 <tt>12 </tt>=&lt;\chunkref{awk-delete-array}(new_context)&gt;
7788 <tt>13 </tt>write_chunk_r(chunklet, new_context,
7789 <tt>14 </tt> chunks[chunk_name, &quot;part&quot;, part, &quot;indent&quot;] indent,
7790 <tt>15 </tt> chunks[chunk_name, &quot;part&quot;, part, &quot;tail&quot;],
7791 <tt>16 </tt> chunk_path &quot;\n &quot; chunk_name,
7792 <tt>17 </tt> call_chunk_args,</div></pre>
7794 <tt class="verbatim"><div class="compact-block">
7795 <tt>18 </tt> s, r, new_src);
7796 </div></tt>
7797 </p>
7798 </p><table style="width: 100%" id="code-end-write-included-chunk-1">
7799 <tbody><tr>
7800 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
7801 </tr></tbody>
7802 </table></font>
7803 </p>
7805 Before we output a chunklet of lines, we first emit the file and line
7806 number if we have one, and if it is safe to do so.
7807 </p>
7809 Chunklets are generally broken up by includes, so the start of a
7810 chunklet is a good place to do this. Then we output each line of the
7811 chunklet.
7812 </p>
7814 When it is not safe, such as in the middle of a multi-line macro
7815 definition, <tt class="verbatim">lineno_suppressed</tt> is set to true, and in
7816 such a case we note that we want to emit the line statement when it is
7817 next safe.
7818 </p>
7820 <font size="-1"><p>
7821 <a id="code-label-write-chunklets-1"></a>
7822 <table style="width: 100%" id="code-ref-write-chunklets-1">
7823 <tbody><tr>
7824 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-chunklets"></a><font size="-1"><div class="left-tab">
7825 <font color="blue">write-chunklets</font>[1](), lang=<font color="blue"></font>
7826 &equiv;
7827 </div><div class="right-tab">
7828 <a href="#code-ref-write-chunklets-2">66b</a>&#x25BF;
7829 </div></font></td>
7830 </tr></tbody>
7831 </table>
7832 </p><p>
7834 <tt class="verbatim"><div class="compact-block">
7835 <tt>1 </tt>max_frag = chunks[chunklet, &quot;line&quot;];
7836 </div></tt>
7837 </p>
7838 <pre class="verbatim" xml:space="preserve">
7839 <div class="compact-block"><tt>2 </tt>for(frag = 1; frag &lt;= max_frag; frag++) {</div></pre>
7841 <tt class="verbatim"><div class="compact-block">
7842 <tt>3 </tt> =&lt;\chunkref{write-file-line}&gt;
7843 </div></tt>
7844 </p>
7845 </p><p>
7846 <a id="code-end-write-chunklets-1"></a>
7847 </p></font>
7848 </p>
7850 We then extract the chunklet text and expand any arguments.
7851 </p>
7853 <font size="-1"><p>
7854 <a id="code-label-write-chunklets-2"></a>
7855 <table style="width: 100%" id="code-ref-write-chunklets-2">
7856 <tbody><tr>
7857 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-chunklets"></a><font size="-1"><div class="left-tab">
7858 <font color="blue">write-chunklets</font>[2]() &uArr;<a href="#code-ref-write-chunklets-1">66a</a>,
7859 lang=<font color="blue"></font> + &equiv;
7860 </div><div class="right-tab">
7861 &#x25B5;<a href="#code-ref-write-chunklets-1">66a</a> <a href="#code-ref-write-chunklets-3">66c</a>&#x25BF;
7862 </div></font></td>
7863 </tr></tbody>
7864 </table>
7865 </p><p>
7867 <tt class="verbatim"><div class="compact-block">
7868 <tt>4 </tt>
7869 </div></tt>
7870 </p>
7871 <pre class="verbatim" xml:space="preserve">
7872 <div class="compact-block"><tt>5 </tt> text = chunks[chunklet, frag];
7873 <tt>6 </tt>
7874 <tt>7 </tt> /* check params */</div></pre>
7876 <tt class="verbatim"><div class="compact-block">
7877 <tt>8 </tt> text = expand_chunk_args(text, chunk_params,
7878 chunk_args);
7879 </div></tt>
7880 </p>
7881 </p><p>
7882 <a id="code-end-write-chunklets-2"></a>
7883 </p></font>
7884 </p>
7886 If the text is a single newline (which we keep separate - see <a href="#lone-newline">5</a>)
7887 then we increment the line number. In the case where this is the last
7888 line of a chunk and it is not a top-level chunk we replace the newline
7889 with an empty string &ndash;- because the chunk that included this chunk
7890 will have the newline at the end of the line that included this chunk.
7891 </p>
7893 We also note by <tt class="verbatim">newline = 1</tt> that we have started a new
7894 line, so that indentation can be managed with the following piece of
7895 text.
7896 </p>
7898 <font size="-1"><p>
7899 <a id="code-label-write-chunklets-3"></a>
7900 <table style="width: 100%" id="code-ref-write-chunklets-3">
7901 <tbody><tr>
7902 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-chunklets"></a><font size="-1"><div class="left-tab">
7903 <font color="blue">write-chunklets</font>[3]() &uArr;<a href="#code-ref-write-chunklets-1">66a</a>,
7904 lang=<font color="blue"></font> + &equiv;
7905 </div><div class="right-tab">
7906 &#x25B5;<a href="#code-ref-write-chunklets-2">66b</a> <a href="#code-ref-write-chunklets-4">66d</a>&#x25BF;
7907 </div></font></td>
7908 </tr></tbody>
7909 </table>
7910 </p><p>
7912 <tt class="verbatim"><div class="compact-block">
7913 <tt>9 </tt>
7914 </div></tt>
7915 </p>
7916 <pre class="verbatim" xml:space="preserve">
7917 <div class="compact-block"><tt>10 </tt> if (text == &quot;\n&quot;) {
7918 <tt>11 </tt> lineno++;
7919 <tt>12 </tt> if (part == max_part &amp;&amp; frag == max_frag &amp;&amp; length(chunk_path)) {
7920 <tt>13 </tt> text = &quot;&quot;;
7921 <tt>14 </tt> break;
7922 <tt>15 </tt> } else {
7923 <tt>16 </tt> newline = 1;</div></pre>
7925 <tt class="verbatim"><div class="compact-block">
7926 <tt>17 </tt> }
7927 </div></tt>
7928 </p>
7929 </p><p>
7930 <a id="code-end-write-chunklets-3"></a>
7931 </p></font>
7932 </p>
7934 If this text does not represent a newline, but we see that we are the
7935 first piece of text on a newline, then we prefix our text with the
7936 current indent.
7937 </p>
7938 <p style="margin-top: 1em; margin-bottom: 1em">
7939 <b>Note <class style="font-style: normal">1</class>. </b><tt class="verbatim">newline</tt> is a global
7940 output-state variable, but the <tt class="verbatim">indent</tt> is not.
7941 </p>
7943 <font size="-1"><p>
7944 <a id="code-label-write-chunklets-4"></a>
7945 <table style="width: 100%" id="code-ref-write-chunklets-4">
7946 <tbody><tr>
7947 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-chunklets"></a><font size="-1"><div class="left-tab">
7948 <font color="blue">write-chunklets</font>[4]() &uArr;<a href="#code-ref-write-chunklets-1">66a</a>,
7949 lang=<font color="blue"></font> + &equiv;
7950 </div><div class="right-tab">
7951 &#x25B5;<a href="#code-ref-write-chunklets-3">66c</a> <a href="#code-ref-write-chunklets-5">67a</a>&#x22B3;
7952 </div></font></td>
7953 </tr></tbody>
7954 </table>
7955 </p><p>
7957 <tt class="verbatim"><div class="compact-block">
7958 <tt>18 </tt> } else if (length(text) || length(tail)) {
7959 </div></tt>
7960 </p>
7961 <pre class="verbatim" xml:space="preserve">
7962 <div class="compact-block"><tt>19 </tt> if (newline) text = indent text;
7963 <tt>20 </tt> newline = 0;
7964 <tt>21 </tt> }</div></pre>
7966 <tt class="verbatim"><div class="compact-block">
7967 <tt>22 </tt>
7968 </div></tt>
7969 </p>
7970 </p><p>
7971 <a id="code-end-write-chunklets-4"></a>
7972 </p></font>
7973 </p>
7975 Tail will soon no longer be relevant once mode-detection is in place.
7976 </p>
7978 <font size="-1"><p>
7979 <a id="code-label-write-chunklets-5"></a>
7980 <table style="width: 100%" id="code-ref-write-chunklets-5">
7981 <tbody><tr>
7982 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-chunklets"></a><font size="-1"><div class="left-tab">
7983 <font color="blue">write-chunklets</font>[5]() &uArr;<a href="#code-ref-write-chunklets-1">66a</a>,
7984 lang=<font color="blue"></font> + &equiv;
7985 </div><div class="right-tab">
7986 &#x22B2;<a href="#code-ref-write-chunklets-4">66d</a> <a href="#code-ref-write-chunklets-6">67b</a>&#x25BF;
7987 </div></font></td>
7988 </tr></tbody>
7989 </table>
7990 </p><p>
7992 <tt class="verbatim"><div class="compact-block">
7993 <tt>23 </tt> text = text tail;
7994 </div></tt>
7995 </p>
7996 <pre class="verbatim" xml:space="preserve">
7997 <div class="compact-block"><tt>24 </tt> mode_tracker(context, text);</div></pre>
7999 <tt class="verbatim"><div class="compact-block">
8000 <tt>25 </tt> print untab(transform_escape(s, r, text, src));
8001 </div></tt>
8002 </p>
8003 </p><p>
8004 <a id="code-end-write-chunklets-5"></a>
8005 </p></font>
8006 </p>
8008 If a line ends in a backslash &ndash;- suggesting continuation &ndash;-
8009 then we supress outputting file-line as it would probably break the
8010 continued lines.
8011 </p>
8013 <font size="-1"><p>
8014 <a id="code-label-write-chunklets-6"></a>
8015 <table style="width: 100%" id="code-ref-write-chunklets-6">
8016 <tbody><tr>
8017 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-chunklets"></a><font size="-1"><div class="left-tab">
8018 <font color="blue">write-chunklets</font>[6]() &uArr;<a href="#code-ref-write-chunklets-1">66a</a>,
8019 lang=<font color="blue"></font> + &equiv;
8020 </div><div class="right-tab">
8021 &#x25B5;<a href="#code-ref-write-chunklets-5">67a</a>
8022 </div></font></td>
8023 </tr></tbody>
8024 </table>
8025 </p><p>
8027 <tt class="verbatim"><div class="compact-block">
8028 <tt>26 </tt> if (linenos) {
8029 </div></tt>
8030 </p>
8031 <pre class="verbatim" xml:space="preserve">
8032 <div class="compact-block"><tt>27 </tt> lineno_suppressed = substr(lastline, length(lastline)) == &quot;\\&quot;;
8033 <tt>28 </tt> }</div></pre>
8035 <tt class="verbatim"><div class="compact-block">
8036 <tt>29 </tt>}
8037 </div></tt>
8038 </p>
8039 </p><table style="width: 100%" id="code-end-write-chunklets-6">
8040 <tbody><tr>
8041 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8042 </tr></tbody>
8043 </table></font>
8044 </p>
8046 Of course there is no point in actually outputting the source filename
8047 and line number (file-line) if they don't say anything new! We only need
8048 to emit them if they aren't what is expected, or if we we not able to
8049 emit one when they had changed.
8050 </p>
8052 <font size="-1"><p>
8053 <a id="code-label-write-file-line-1"></a>
8054 <table style="width: 100%" id="code-ref-write-file-line-1">
8055 <tbody><tr>
8056 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="write-file-line"></a><font size="-1"><font color="blue">write-file-line</font>[1](),
8057 lang=<font color="blue"></font> &equiv;</font></td>
8058 </tr></tbody>
8059 </table>
8060 </p><p>
8062 <tt class="verbatim"><div class="compact-block">
8063 <tt>1 </tt>if (newline &amp;&amp; lineno_needed &amp;&amp; !
8064 lineno_suppressed) {
8065 </div></tt>
8066 </p>
8067 <pre class="verbatim" xml:space="preserve">
8068 <div class="compact-block"><tt>2 </tt> filename = a_filename;
8069 <tt>3 </tt> lineno = a_lineno;
8070 <tt>4 </tt> print &quot;#line &quot; lineno &quot; \&quot;&quot; filename &quot;\&quot;\n&quot;
8071 <tt>5 </tt> lineno_needed = 0;</div></pre>
8073 <tt class="verbatim"><div class="compact-block">
8074 <tt>6 </tt>}
8075 </div></tt>
8076 </p>
8077 </p><table style="width: 100%" id="code-end-write-file-line-1">
8078 <tbody><tr>
8079 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8080 </tr></tbody>
8081 </table></font>
8082 </p>
8084 We check if a new file-line is needed by checking if the source line
8085 matches what we (or a compiler) would expect.
8086 </p>
8088 <font size="-1"><p>
8089 <a id="code-label-check-source-jump-1"></a>
8090 <table style="width: 100%" id="code-ref-check-source-jump-1">
8091 <tbody><tr>
8092 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="check-source-jump"></a><font size="-1"><font color="blue">check-source-jump</font>[1](),
8093 lang=<font color="blue"></font> &equiv;</font></td>
8094 </tr></tbody>
8095 </table>
8096 </p><p>
8098 <tt class="verbatim"><div class="compact-block">
8099 <tt>1 </tt>if (linenos &amp;&amp; (chunk_name SUBSEP
8100 &quot;part&quot; SUBSEP part SUBSEP &quot;FILENAME&quot; in
8101 chunks)) {
8102 </div></tt>
8103 </p>
8104 <pre class="verbatim" xml:space="preserve">
8105 <div class="compact-block"><tt>2 </tt> a_filename = chunks[chunk_name, &quot;part&quot;, part, &quot;FILENAME&quot;];
8106 <tt>3 </tt> a_lineno = chunks[chunk_name, &quot;part&quot;, part, &quot;LINENO&quot;];
8107 <tt>4 </tt> if (a_filename != filename || a_lineno != lineno) {
8108 <tt>5 </tt> lineno_needed++;
8109 <tt>6 </tt> }</div></pre>
8111 <tt class="verbatim"><div class="compact-block">
8112 <tt>7 </tt>}
8113 </div></tt>
8114 </p>
8115 </p><table style="width: 100%" id="code-end-check-source-jump-1">
8116 <tbody><tr>
8117 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8118 </tr></tbody>
8119 </table></font>
8120 </p>
8121 <h1 id="auto-82">Chapter 13<br></br>Storing Chunks</h1>
8123 Awk has pretty limited data structures, so we will use two main hashes.
8124 Uninterrupted sequences of a chunk will be stored in chunklets and the
8125 chunklets used in a chunk will be stored in <tt class="verbatim">chunks</tt>.
8126 </p>
8128 <font size="-1"><p>
8129 <a id="code-label-constants-1"></a>
8130 <table style="width: 100%" id="code-ref-constants-1">
8131 <tbody><tr>
8132 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="constants"></a><font size="-1"><font color="blue">constants</font>[1](),
8133 lang=<font color="blue"></font> &equiv;</font></td>
8134 </tr></tbody>
8135 </table>
8136 </p><p>
8138 <tt class="verbatim"><div class="compact-block">
8139 <tt>1 </tt>part_type_chunk=1;
8140 </div></tt>
8141 </p>
8142 <pre class="verbatim" xml:space="preserve">
8143 <div class="compact-block"></div></pre>
8145 <tt class="verbatim"><div class="compact-block">
8146 <tt>2 </tt>SUBSEP=&quot;,&quot;;
8147 </div></tt>
8148 </p>
8149 </p><table style="width: 100%" id="code-end-constants-1">
8150 <tbody><tr>
8151 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8152 </tr></tbody>
8153 </table></font>
8154 </p>
8156 The params mentioned are not chunk parameters for parameterized chunks,
8157 as mentioned in
8158 <a href="#Chunk Arguments">8.2</a>
8159 , but the lstlistings style parameters used in the
8160 <tt class="verbatim">\Chunk</tt>
8161 command
8163 <font size="-1"><div align="justify">
8164 <div style="margin-left: 0px">
8165 <div style="margin-right: 0px">
8167 . The <tt class="verbatim">params</tt> parameter is used to hold the
8168 parameters for parameterized chunks
8169 </p>
8170 </div>
8171 </div>
8172 </div></font>
8173 </p>
8174 <span style="margin-left: 0em"></span>
8175 <a id="footnr-1"></a>
8176 <sup><a href="#footnote-1">1</a></sup>
8178 </p>
8180 <font size="-1"><p>
8181 <a id="code-label-chunk-storage-functions-1"></a>
8182 <table style="width: 100%" id="code-ref-chunk-storage-functions-1">
8183 <tbody><tr>
8184 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="chunk-storage-functions"></a><font size="-1"><div class="left-tab">
8185 <font color="blue">chunk-storage-functions</font>[1](), lang=<font
8186 color="blue"></font> &equiv;
8187 </div><div class="right-tab">
8188 <a href="#code-ref-chunk-storage-functions-2">69c</a>&#x25BF;
8189 </div></font></td>
8190 </tr></tbody>
8191 </table>
8192 </p><p>
8194 <tt class="verbatim"><div class="compact-block">
8195 <tt>1 </tt>function new_chunk(chunk_name, params,
8196 </div></tt>
8197 </p>
8198 <pre class="verbatim" xml:space="preserve">
8199 <div class="compact-block"><tt>2 </tt> # local vars
8200 <tt>3 </tt> p, append )
8201 <tt>4 </tt>{
8202 <tt>5 </tt> # HACK WHILE WE CHANGE TO ( ) for PARAM CHUNKS
8203 <tt>6 </tt> gsub(&quot;\\(\\)$&quot;, &quot;&quot;, chunk_name);
8204 <tt>7 </tt> if (! (chunk_name in chunk_names)) {
8205 <tt>8 </tt> if (debug) print &quot;New chunk &quot; chunk_name;
8206 <tt>9 </tt> chunk_names[chunk_name];
8207 <tt>10 </tt> for (p in params) {
8208 <tt>11 </tt> chunks[chunk_name, p] = params[p];
8209 <tt>12 </tt> if (debug) print &quot;chunks[&quot; chunk_name &quot;,&quot; p &quot;] = &quot; params[p];
8210 <tt>13 </tt> }
8211 <tt>14 </tt> if (&quot;append&quot; in params) {
8212 <tt>15 </tt> append=params[&quot;append&quot;];
8213 <tt>16 </tt> if (! (append in chunk_names)) {
8214 <tt>17 </tt> warning(&quot;Chunk &quot; chunk_name &quot; is appended to chunk &quot; append &quot; which is not defined yet&quot;);
8215 <tt>18 </tt> new_chunk(append);
8216 <tt>19 </tt> }
8217 <tt>20 </tt> chunk_include(append, chunk_name);
8218 <tt>21 </tt> chunk_line(append, ORS);
8219 <tt>22 </tt> }
8220 <tt>23 </tt> }
8221 <tt>24 </tt> active_chunk = chunk_name;
8222 <tt>25 </tt> prime_chunk(chunk_name);</div></pre>
8224 <tt class="verbatim"><div class="compact-block">
8225 <tt>26 </tt>}
8226 </div></tt>
8227 </p>
8228 </p><p>
8229 <a id="code-end-chunk-storage-functions-1"></a>
8230 </p></font>
8231 </p>
8233 <font size="-1"><p>
8234 <a id="code-label-chunk-storage-functions-2"></a>
8235 <table style="width: 100%" id="code-ref-chunk-storage-functions-2">
8236 <tbody><tr>
8237 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="chunk-storage-functions"></a><font size="-1"><div class="left-tab">
8238 <font color="blue">chunk-storage-functions</font>[2]() &uArr;<a href="#code-ref-chunk-storage-functions-1">69b</a>,
8239 lang=<font color="blue"></font> + &equiv;
8240 </div><div class="right-tab">
8241 &#x25B5;<a href="#code-ref-chunk-storage-functions-1">69b</a> <a href="#code-ref-chunk-storage-functions-3">70a</a>&#x22B3;
8242 </div></font></td>
8243 </tr></tbody>
8244 </table>
8245 </p><p>
8247 <tt class="verbatim"><div class="compact-block">
8248 <tt>27 </tt>
8249 </div></tt>
8250 </p>
8251 <pre class="verbatim" xml:space="preserve">
8252 <div class="compact-block"><tt>28 </tt>function prime_chunk(chunk_name)
8253 <tt>29 </tt>{
8254 <tt>30 </tt> chunks[chunk_name, &quot;part&quot;, ++chunks[chunk_name, &quot;part&quot;] ] = \
8255 <tt>31 </tt> chunk_name SUBSEP &quot;chunklet&quot; SUBSEP &quot;&quot; ++chunks[chunk_name, &quot;chunklet&quot;];
8256 <tt>32 </tt> chunks[chunk_name, &quot;part&quot;, chunks[chunk_name, &quot;part&quot;], &quot;FILENAME&quot;] = FILENAME;
8257 <tt>33 </tt> chunks[chunk_name, &quot;part&quot;, chunks[chunk_name, &quot;part&quot;], &quot;LINENO&quot;] = FNR + 1;
8258 <tt>34 </tt>}
8259 <tt>35 </tt>
8260 <tt>36 </tt>function chunk_line(chunk_name, line){
8261 <tt>37 </tt> chunks[chunk_name, &quot;chunklet&quot;, chunks[chunk_name, &quot;chunklet&quot;],
8262 <tt>38 </tt> ++chunks[chunk_name, &quot;chunklet&quot;, chunks[chunk_name, &quot;chunklet&quot;], &quot;line&quot;] ] = line;
8263 <tt>39 </tt>}</div></pre>
8265 <tt class="verbatim"><div class="compact-block">
8266 <tt>40 </tt>
8267 </div></tt>
8268 </p>
8269 </p><p>
8270 <a id="code-end-chunk-storage-functions-2"></a>
8271 </p></font>
8272 </p>
8274 Chunk include represents a <em>chunkref</em> statement, and stores the
8275 requirement to include another chunk. The parameter indent represents
8276 the quanity of literal text characters that preceded this
8277 <em>chunkref</em> statement and therefore by how much additional lines
8278 of the included chunk should be indented.
8279 </p>
8281 <font size="-1"><p>
8282 <a id="code-label-chunk-storage-functions-3"></a>
8283 <table style="width: 100%" id="code-ref-chunk-storage-functions-3">
8284 <tbody><tr>
8285 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="chunk-storage-functions"></a><font size="-1"><div class="left-tab">
8286 <font color="blue">chunk-storage-functions</font>[3]() &uArr;<a href="#code-ref-chunk-storage-functions-1">69b</a>,
8287 lang=<font color="blue"></font> + &equiv;
8288 </div><div class="right-tab">
8289 &#x22B2;<a href="#code-ref-chunk-storage-functions-2">69c</a> <a href="#code-ref-chunk-storage-functions-4">70b</a>&#x25BF;
8290 </div></font></td>
8291 </tr></tbody>
8292 </table>
8293 </p><p>
8295 <tt class="verbatim"><div class="compact-block">
8296 <tt>41 </tt>function chunk_include(chunk_name, chunk_ref, indent,
8297 tail)
8298 </div></tt>
8299 </p>
8300 <pre class="verbatim" xml:space="preserve">
8301 <div class="compact-block"><tt>42 </tt>{
8302 <tt>43 </tt> chunks[chunk_name, &quot;part&quot;, ++chunks[chunk_name, &quot;part&quot;] ] = chunk_ref;
8303 <tt>44 </tt> chunks[chunk_name, &quot;part&quot;, chunks[chunk_name, &quot;part&quot;], &quot;type&quot; ] = part_type_chunk;
8304 <tt>45 </tt> chunks[chunk_name, &quot;part&quot;, chunks[chunk_name, &quot;part&quot;], &quot;indent&quot; ] = indent_string(indent);
8305 <tt>46 </tt> chunks[chunk_name, &quot;part&quot;, chunks[chunk_name, &quot;part&quot;], &quot;tail&quot; ] = tail;
8306 <tt>47 </tt> prime_chunk(chunk_name);
8307 <tt>48 </tt>}</div></pre>
8309 <tt class="verbatim"><div class="compact-block">
8310 <tt>49 </tt>
8311 </div></tt>
8312 </p>
8313 </p><p>
8314 <a id="code-end-chunk-storage-functions-3"></a>
8315 </p></font>
8316 </p>
8318 The indent is calculated by indent_string, which may in future convert
8319 some spaces into tab characters. This function works by generating a
8320 printf padded format string, like <tt class="verbatim">%22s</tt> for an indent of
8321 22, and then printing an empty string using that format.
8322 </p>
8324 <font size="-1"><p>
8325 <a id="code-label-chunk-storage-functions-4"></a>
8326 <table style="width: 100%" id="code-ref-chunk-storage-functions-4">
8327 <tbody><tr>
8328 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="chunk-storage-functions"></a><font size="-1"><div class="left-tab">
8329 <font color="blue">chunk-storage-functions</font>[4]() &uArr;<a href="#code-ref-chunk-storage-functions-1">69b</a>,
8330 lang=<font color="blue"></font> + &equiv;
8331 </div><div class="right-tab">
8332 &#x25B5;<a href="#code-ref-chunk-storage-functions-3">70a</a>
8333 </div></font></td>
8334 </tr></tbody>
8335 </table>
8336 </p><p>
8338 <tt class="verbatim"><div class="compact-block">
8339 <tt>50 </tt>function indent_string(indent) {
8340 </div></tt>
8341 </p>
8342 <pre class="verbatim" xml:space="preserve">
8343 <div class="compact-block"><tt>51 </tt> return sprintf(&quot;%&quot; indent &quot;s&quot;, &quot;&quot;);</div></pre>
8345 <tt class="verbatim"><div class="compact-block">
8346 <tt>52 </tt>}
8347 </div></tt>
8348 </p>
8349 </p><table style="width: 100%" id="code-end-chunk-storage-functions-4">
8350 <tbody><tr>
8351 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8352 </tr></tbody>
8353 </table></font>
8354 </p>
8355 <h1 id="auto-83"><a id="cha:getopt"></a>Chapter 14<br></br>getopt</h1>
8357 I use Arnold Robbins public domain getopt (1993 revision). This is
8358 probably the same one that is covered in chapter 12 of
8359 &acirc;&#x0102;&#x0132;Edition 3 of GAWK: Effective AWK Programming: A
8360 User's Guide for GNU Awk&acirc;&#x0102;&#x0130; but as that is licensed
8361 under the GNU Free Documentation License, Version 1.3, which conflicts
8362 with the GPL3, I can't use it from there (or it's accompanying
8363 explanations), so I do my best to explain how it works here.
8364 </p>
8366 The getopt.awk header is:
8367 </p>
8369 <font size="-1"><p>
8370 <a id="code-label-getopt.awk-header-1"></a>
8371 <table style="width: 100%" id="code-ref-getopt.awk-header-1">
8372 <tbody><tr>
8373 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="getopt.awk-header"></a><font size="-1"><font color="blue">getopt.awk-header</font>[1](),
8374 lang=<font color="blue"></font> &equiv;</font></td>
8375 </tr></tbody>
8376 </table>
8377 </p><p>
8379 <tt class="verbatim"><div class="compact-block">
8380 <tt>1 </tt># getopt.awk &ndash;- do C library getopt(3) function
8381 in awk
8382 </div></tt>
8383 </p>
8384 <pre class="verbatim" xml:space="preserve">
8385 <div class="compact-block"><tt>2 </tt>#
8386 <tt>3 </tt># Arnold Robbins, arnold@skeeve.com, Public Domain
8387 <tt>4 </tt>#
8388 <tt>5 </tt># Initial version: March, 1991
8389 <tt>6 </tt># Revised: May, 1993</div></pre>
8391 <tt class="verbatim"><div class="compact-block">
8392 <tt>7 </tt>
8393 </div></tt>
8394 </p>
8395 </p><table style="width: 100%" id="code-end-getopt.awk-header-1">
8396 <tbody><tr>
8397 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8398 </tr></tbody>
8399 </table></font>
8400 </p>
8402 The provided explanation is:
8403 </p>
8405 <font size="-1"><p>
8406 <a id="code-label-getopt.awk-notes-1"></a>
8407 <table style="width: 100%" id="code-ref-getopt.awk-notes-1">
8408 <tbody><tr>
8409 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="getopt.awk-notes"></a><font size="-1"><font color="blue">getopt.awk-notes</font>[1](),
8410 lang=<font color="blue"></font> &equiv;</font></td>
8411 </tr></tbody>
8412 </table>
8413 </p><p>
8415 <tt class="verbatim"><div class="compact-block">
8416 <tt>1 </tt># External variables:
8417 </div></tt>
8418 </p>
8419 <pre class="verbatim" xml:space="preserve">
8420 <div class="compact-block"><tt>2 </tt># Optind -- index in ARGV of first nonoption argument
8421 <tt>3 </tt># Optarg -- string value of argument to current option
8422 <tt>4 </tt># Opterr -- if nonzero, print our own diagnostic
8423 <tt>5 </tt># Optopt -- current option letter
8424 <tt>6 </tt>
8425 <tt>7 </tt># Returns:
8426 <tt>8 </tt># -1 at end of options
8427 <tt>9 </tt># ? for unrecognized option
8428 <tt>10 </tt># &lt;c&gt; a character representing the current option
8429 <tt>11 </tt>
8430 <tt>12 </tt># Private Data:
8431 <tt>13 </tt># _opti -- index in multi-flag option, e.g., -abc</div></pre>
8433 <tt class="verbatim"><div class="compact-block">
8434 <tt>14 </tt>
8435 </div></tt>
8436 </p>
8437 </p><table style="width: 100%" id="code-end-getopt.awk-notes-1">
8438 <tbody><tr>
8439 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8440 </tr></tbody>
8441 </table></font>
8442 </p>
8444 The function follows. The final two parameters, <tt class="verbatim">thisopt</tt>
8445 and <tt class="verbatim">i</tt> are local variables and not parameters &ndash;- as
8446 indicated by the multiple spaces preceding them. Awk doesn't care, the
8447 multiple spaces are a convention to help us humans.
8448 </p>
8450 <font size="-1"><p>
8451 <a id="code-label-getopt.awk-getopt()-1"></a>
8452 <table style="width: 100%" id="code-ref-getopt.awk-getopt()-1">
8453 <tbody><tr>
8454 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="getopt.awk-getopt()"></a><font size="-1"><div class="left-tab">
8455 <font color="blue">getopt.awk-getopt()</font>[1](), lang=<font color="blue"></font>
8456 &equiv;
8457 </div><div class="right-tab">
8458 <a href="#code-ref-getopt.awk-getopt()-2">72a</a>&#x22B3;
8459 </div></font></td>
8460 </tr></tbody>
8461 </table>
8462 </p><p>
8464 <tt class="verbatim"><div class="compact-block">
8465 <tt>1 </tt>function getopt(argc, argv, options, thisopt, i)
8466 </div></tt>
8467 </p>
8468 <pre class="verbatim" xml:space="preserve">
8469 <div class="compact-block"><tt>2 </tt>{
8470 <tt>3 </tt> if (length(options) == 0) # no options given
8471 <tt>4 </tt> return -1
8472 <tt>5 </tt> if (argv[Optind] == &quot;--&quot;) { # all done
8473 <tt>6 </tt> Optind++
8474 <tt>7 </tt> _opti = 0
8475 <tt>8 </tt> return -1
8476 <tt>9 </tt> } else if (argv[Optind] !~ /^-[^: \t\n\f\r\v\b]/) {
8477 <tt>10 </tt> _opti = 0
8478 <tt>11 </tt> return -1
8479 <tt>12 </tt> }
8480 <tt>13 </tt> if (_opti == 0)
8481 <tt>14 </tt> _opti = 2
8482 <tt>15 </tt> thisopt = substr(argv[Optind], _opti, 1)
8483 <tt>16 </tt> Optopt = thisopt
8484 <tt>17 </tt> i = index(options, thisopt)
8485 <tt>18 </tt> if (i == 0) {
8486 <tt>19 </tt> if (Opterr)
8487 <tt>20 </tt> printf(&quot;%c -- invalid option\n&quot;,
8488 <tt>21 </tt> thisopt) &gt; &quot;/dev/stderr&quot;
8489 <tt>22 </tt> if (_opti &gt;= length(argv[Optind])) {
8490 <tt>23 </tt> Optind++
8491 <tt>24 </tt> _opti = 0
8492 <tt>25 </tt> } else
8493 <tt>26 </tt> _opti++
8494 <tt>27 </tt> return &quot;?&quot;</div></pre>
8496 <tt class="verbatim"><div class="compact-block">
8497 <tt>28 </tt> }
8498 </div></tt>
8499 </p>
8500 </p><p>
8501 <a id="code-end-getopt.awk-getopt()-1"></a>
8502 </p></font>
8503 </p>
8505 At this point, the option has been found and we need to know if it takes
8506 any arguments.
8507 </p>
8509 <font size="-1"><p>
8510 <a id="code-label-getopt.awk-getopt()-2"></a>
8511 <table style="width: 100%" id="code-ref-getopt.awk-getopt()-2">
8512 <tbody><tr>
8513 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="getopt.awk-getopt()"></a><font size="-1"><div class="left-tab">
8514 <font color="blue">getopt.awk-getopt()</font>[2]() &uArr;<a href="#code-ref-getopt.awk-getopt()-1">71c</a>,
8515 lang=<font color="blue"></font> + &equiv;
8516 </div><div class="right-tab">
8517 &#x22B2;<a href="#code-ref-getopt.awk-getopt()-1">71c</a>
8518 </div></font></td>
8519 </tr></tbody>
8520 </table>
8521 </p><p>
8523 <tt class="verbatim"><div class="compact-block">
8524 <tt>29 </tt> if (substr(options, i + 1, 1) == &quot;:&quot;) {
8525 </div></tt>
8526 </p>
8527 <pre class="verbatim" xml:space="preserve">
8528 <div class="compact-block"><tt>30 </tt> # get option argument
8529 <tt>31 </tt> if (length(substr(argv[Optind], _opti + 1)) &gt; 0)
8530 <tt>32 </tt> Optarg = substr(argv[Optind], _opti + 1)
8531 <tt>33 </tt> else
8532 <tt>34 </tt> Optarg = argv[++Optind]
8533 <tt>35 </tt> _opti = 0
8534 <tt>36 </tt> } else
8535 <tt>37 </tt> Optarg = &quot;&quot;
8536 <tt>38 </tt> if (_opti == 0 || _opti &gt;= length(argv[Optind])) {
8537 <tt>39 </tt> Optind++
8538 <tt>40 </tt> _opti = 0
8539 <tt>41 </tt> } else
8540 <tt>42 </tt> _opti++
8541 <tt>43 </tt> return thisopt</div></pre>
8543 <tt class="verbatim"><div class="compact-block">
8544 <tt>44 </tt>}
8545 </div></tt>
8546 </p>
8547 </p><table style="width: 100%" id="code-end-getopt.awk-getopt()-2">
8548 <tbody><tr>
8549 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8550 </tr></tbody>
8551 </table></font>
8552 </p>
8554 A test program is built in, too
8555 </p>
8557 <font size="-1"><p>
8558 <a id="code-label-getopt.awk-begin-1"></a>
8559 <table style="width: 100%" id="code-ref-getopt.awk-begin-1">
8560 <tbody><tr>
8561 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="getopt.awk-begin"></a><font size="-1"><font color="blue">getopt.awk-begin</font>[1](),
8562 lang=<font color="blue"></font> &equiv;</font></td>
8563 </tr></tbody>
8564 </table>
8565 </p><p>
8567 <tt class="verbatim"><div class="compact-block">
8568 <tt>1 </tt>BEGIN {
8569 </div></tt>
8570 </p>
8571 <pre class="verbatim" xml:space="preserve">
8572 <div class="compact-block"><tt>2 </tt> Opterr = 1 # default is to diagnose
8573 <tt>3 </tt> Optind = 1 # skip ARGV[0]
8574 <tt>4 </tt> # test program
8575 <tt>5 </tt> if (_getopt_test) {
8576 <tt>6 </tt> while ((_go_c = getopt(ARGC, ARGV, &quot;ab:cd&quot;)) != -1)
8577 <tt>7 </tt> printf(&quot;c = &lt;%c&gt;, optarg = &lt;%s&gt;\n&quot;,
8578 <tt>8 </tt> _go_c, Optarg)
8579 <tt>9 </tt> printf(&quot;non-option arguments:\n&quot;)
8580 <tt>10 </tt> for (; Optind &lt; ARGC; Optind++)
8581 <tt>11 </tt> printf(&quot;\tARGV[%d] = &lt;%s&gt;\n&quot;,
8582 <tt>12 </tt> Optind, ARGV[Optind])
8583 <tt>13 </tt> }</div></pre>
8585 <tt class="verbatim"><div class="compact-block">
8586 <tt>14 </tt>}
8587 </div></tt>
8588 </p>
8589 </p><table style="width: 100%" id="code-end-getopt.awk-begin-1">
8590 <tbody><tr>
8591 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8592 </tr></tbody>
8593 </table></font>
8594 </p>
8596 The entire getopt.awk is made out of these chunks in order
8597 </p>
8599 <font size="-1"><p>
8600 <a id="code-label-getopt.awk-1"></a>
8601 <table style="width: 100%" id="code-ref-getopt.awk-1">
8602 <tbody><tr>
8603 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="getopt.awk"></a><font size="-1"><font color="blue">getopt.awk</font>[1](),
8604 lang=<font color="blue"></font> &equiv;</font></td>
8605 </tr></tbody>
8606 </table>
8607 </p><p>
8609 <tt class="verbatim"><div class="compact-block">
8610 <tt>1 </tt>=&lt;\chunkref{getopt.awk-header}&gt;
8611 </div></tt>
8612 </p>
8613 <pre class="verbatim" xml:space="preserve">
8614 <div class="compact-block"><tt>2 </tt>
8615 <tt>3 </tt>=&lt;\chunkref{getopt.awk-notes}&gt;
8616 <tt>4 </tt>=&lt;\chunkref{getopt.awk-getopt()}&gt;</div></pre>
8618 <tt class="verbatim"><div class="compact-block">
8619 <tt>5 </tt>=&lt;\chunkref{getopt.awk-begin}&gt;
8620 </div></tt>
8621 </p>
8622 </p><table style="width: 100%" id="code-end-getopt.awk-1">
8623 <tbody><tr>
8624 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8625 </tr></tbody>
8626 </table></font>
8627 </p>
8629 Although we only want the header and function:
8630 </p>
8632 <font size="-1"><p>
8633 <a id="code-label-getopt-1"></a>
8634 <table style="width: 100%" id="code-ref-getopt-1">
8635 <tbody><tr>
8636 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="getopt"></a><font size="-1"><font color="blue">getopt</font>[1](),
8637 lang=<font color="blue"></font> &equiv;</font></td>
8638 </tr></tbody>
8639 </table>
8640 </p><p>
8642 <tt class="verbatim"><div class="compact-block">
8643 <tt>1 </tt># try: locate getopt.awk for the full original file
8644 </div></tt>
8645 </p>
8646 <pre class="verbatim" xml:space="preserve">
8647 <div class="compact-block"><tt>2 </tt># as part of your standard awk installation
8648 <tt>3 </tt>=&lt;\chunkref{getopt.awk-header}&gt;
8649 <tt>4 </tt></div></pre>
8651 <tt class="verbatim"><div class="compact-block">
8652 <tt>5 </tt>=&lt;\chunkref{getopt.awk-getopt()}&gt;
8653 </div></tt>
8654 </p>
8655 </p><table style="width: 100%" id="code-end-getopt-1">
8656 <tbody><tr>
8657 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8658 </tr></tbody>
8659 </table></font>
8660 </p>
8661 <h1 id="auto-84"><a id="latex-source"></a>Chapter 15<br></br>Fangle LaTeX source code</h1>
8662 <h2 id="auto-85">15.1<span style="margin-left: 1em"></span>fangle module</h2>
8664 Here we define a L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X <tt class="verbatim">.module</tt>
8665 file that makes it convenient to use L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X
8666 for writing such literate programs.
8667 </p>
8669 This file <tt class="verbatim">./fangle.module</tt> can be installed in your
8670 personal <tt class="verbatim">.lyx/layouts</tt> folder. You will need to Tools
8671 Reconfigure so that L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X notices
8672 it. It adds a new format Chunk, which should precede every listing and
8673 contain the chunk name.
8674 </p>
8676 <font size="-1"><p>
8677 <a id="code-label-./fangle.module-1"></a>
8678 <table style="width: 100%" id="code-ref-./fangle.module-1">
8679 <tbody><tr>
8680 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.module"></a><font size="-1"><font color="blue">./fangle.module</font>[1](),
8681 lang=<font color="blue">lyx-module</font> &equiv;</font></td>
8682 </tr></tbody>
8683 </table>
8684 </p><p>
8686 <tt class="verbatim"><div class="compact-block">
8687 <tt>1 </tt>#\DeclareLyXModule{Fangle Literate Listings}
8688 </div></tt>
8689 </p>
8690 <pre class="verbatim" xml:space="preserve">
8691 <div class="compact-block"><tt>2 </tt>#DescriptionBegin
8692 <tt>3 </tt># Fangle literate listings allow one to write
8693 <tt>4 </tt># literate programs after the fashion of noweb, but without having
8694 <tt>5 </tt># to use noweave to generate the documentation. Instead the listings
8695 <tt>6 </tt># package is extended in conjunction with the noweb package to implement
8696 <tt>7 </tt># to code formating directly as latex.
8697 <tt>8 </tt># The fangle awk script
8698 <tt>9 </tt>#DescriptionEnd
8699 <tt>10 </tt>
8700 <tt>11 </tt>=&lt;\chunkref{gpl3-copyright.hashed}&gt;
8701 <tt>12 </tt>
8702 <tt>13 </tt>Format 11
8703 <tt>14 </tt>
8704 <tt>15 </tt>AddToPreamble
8705 <tt>16 </tt>=&lt;\chunkref{./fangle.sty}&gt;
8706 <tt>17 </tt>EndPreamble
8707 <tt>18 </tt>
8708 <tt>19 </tt>=&lt;\chunkref{chunkstyle}&gt;
8709 <tt>20 </tt></div></pre>
8711 <tt class="verbatim"><div class="compact-block">
8712 <tt>21 </tt>=&lt;\chunkref{chunkref}&gt;
8713 </div></tt>
8714 </p>
8715 </p><table style="width: 100%" id="code-end-./fangle.module-1">
8716 <tbody><tr>
8717 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8718 </tr></tbody>
8719 </table></font>
8720 </p>
8722 Because L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X modules are not yet
8723 a language supported by fangle or lstlistings, we resort to this fake
8724 awk chunk below in order to have each line of the GPL3 license commence
8725 with a #
8726 </p>
8728 <font size="-1"><p>
8729 <a id="code-label-gpl3-copyright.hashed-1"></a>
8730 <table style="width: 100%" id="code-ref-gpl3-copyright.hashed-1">
8731 <tbody><tr>
8732 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="gpl3-copyright.hashed"></a><font size="-1"><font color="blue">gpl3-copyright.hashed</font>[1](),
8733 lang=<font color="blue">awk</font> &equiv;</font></td>
8734 </tr></tbody>
8735 </table>
8736 </p><p>
8738 <tt class="verbatim"><div class="compact-block">
8739 <tt>1 </tt>#=&lt;\chunkref{gpl3-copyright}&gt;
8740 </div></tt>
8741 </p>
8742 <pre class="verbatim" xml:space="preserve">
8743 <div class="compact-block"></div></pre>
8745 <tt class="verbatim"><div class="compact-block">
8746 <tt>2 </tt>
8747 </div></tt>
8748 </p>
8749 </p><table style="width: 100%" id="code-end-gpl3-copyright.hashed-1">
8750 <tbody><tr>
8751 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8752 </tr></tbody>
8753 </table></font>
8754 </p>
8755 <h3 id="auto-86">15.1.1<span style="margin-left: 1em"></span>The Chunk style</h3>
8757 The purpose of the <class style="font-variant: small-caps">chunk</class> style is to make it
8758 easier for L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X users to provide
8759 the name to <tt class="verbatim">lstlistings</tt>. Normally this requires
8760 right-clicking on the listing, choosing settings, advanced, and then
8761 typing <tt class="verbatim">name=chunk-name</tt>. This has the further
8762 disadvantage that the name (and other options) are not generally visible
8763 during document editing.
8764 </p>
8766 The chunk style is defined as a LaTeX command, so that all text on the
8767 same line is passed to the <tt class="verbatim">LaTeX</tt> command <tt class="verbatim">Chunk</tt>.
8768 This makes it easy to parse using <tt class="verbatim">fangle</tt>, and easy to
8769 pass these options on to the listings package. The first word in a chunk
8770 section should be the chunk name, and will have <tt class="verbatim">name=</tt>
8771 prepended to it. Any other words are accepted arguments to <tt class="verbatim">lstset</tt>.
8772 </p>
8774 We set PassThru to 1 because the user is actually entering raw latex.
8775 </p>
8777 <font size="-1"><p>
8778 <a id="code-label-chunkstyle-1"></a>
8779 <table style="width: 100%" id="code-ref-chunkstyle-1">
8780 <tbody><tr>
8781 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="chunkstyle"></a><font size="-1"><div class="left-tab">
8782 <font color="blue">chunkstyle</font>[1](), lang=<font color="blue"></font>
8783 &equiv;
8784 </div><div class="right-tab">
8785 <a href="#code-ref-chunkstyle-2">76b</a>&#x25BF;
8786 </div></font></td>
8787 </tr></tbody>
8788 </table>
8789 </p><p>
8791 <tt class="verbatim"><div class="compact-block">
8792 <tt>1 </tt>Style Chunk
8793 </div></tt>
8794 </p>
8795 <pre class="verbatim" xml:space="preserve">
8796 <div class="compact-block"><tt>2 </tt> LatexType Command
8797 <tt>3 </tt> LatexName Chunk
8798 <tt>4 </tt> Margin First_Dynamic
8799 <tt>5 </tt> LeftMargin Chunk:xxx
8800 <tt>6 </tt> LabelSep xx
8801 <tt>7 </tt> LabelType Static
8802 <tt>8 </tt> LabelString &quot;Chunk:&quot;
8803 <tt>9 </tt> Align Left
8804 <tt>10 </tt> PassThru 1</div></pre>
8806 <tt class="verbatim"><div class="compact-block">
8807 <tt>11 </tt>
8808 </div></tt>
8809 </p>
8810 </p><p>
8811 <a id="code-end-chunkstyle-1"></a>
8812 </p></font>
8813 </p>
8815 To make the label very visible we choose a larger font coloured red.
8816 </p>
8818 <font size="-1"><p>
8819 <a id="code-label-chunkstyle-2"></a>
8820 <table style="width: 100%" id="code-ref-chunkstyle-2">
8821 <tbody><tr>
8822 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="chunkstyle"></a><font size="-1"><div class="left-tab">
8823 <font color="blue">chunkstyle</font>[2]() &uArr;<a href="#code-ref-chunkstyle-1">76a</a>,
8824 lang=<font color="blue"></font> + &equiv;
8825 </div><div class="right-tab">
8826 &#x25B5;<a href="#code-ref-chunkstyle-1">76a</a>
8827 </div></font></td>
8828 </tr></tbody>
8829 </table>
8830 </p><p>
8832 <tt class="verbatim"><div class="compact-block">
8833 <tt>12 </tt> LabelFont
8834 </div></tt>
8835 </p>
8836 <pre class="verbatim" xml:space="preserve">
8837 <div class="compact-block"><tt>13 </tt> Family Sans
8838 <tt>14 </tt> Size Large
8839 <tt>15 </tt> Series Bold
8840 <tt>16 </tt> Shape Italic
8841 <tt>17 </tt> Color red
8842 <tt>18 </tt> EndFont</div></pre>
8844 <tt class="verbatim"><div class="compact-block">
8845 <tt>19 </tt>End
8846 </div></tt>
8847 </p>
8848 </p><table style="width: 100%" id="code-end-chunkstyle-2">
8849 <tbody><tr>
8850 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8851 </tr></tbody>
8852 </table></font>
8853 </p>
8854 <h3 id="auto-87">15.1.2<span style="margin-left: 1em"></span>The chunkref style</h3>
8856 We also define the Chunkref style which can be used to express cross
8857 references to chunks.
8858 </p>
8860 <font size="-1"><p>
8861 <a id="code-label-chunkref-1"></a>
8862 <table style="width: 100%" id="code-ref-chunkref-1">
8863 <tbody><tr>
8864 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="chunkref"></a><font size="-1"><font color="blue">chunkref</font>[1](),
8865 lang=<font color="blue"></font> &equiv;</font></td>
8866 </tr></tbody>
8867 </table>
8868 </p><p>
8870 <tt class="verbatim"><div class="compact-block">
8871 <tt>1 </tt>InsetLayout Chunkref
8872 </div></tt>
8873 </p>
8874 <pre class="verbatim" xml:space="preserve">
8875 <div class="compact-block"><tt>2 </tt> LyxType charstyle
8876 <tt>3 </tt> LatexType Command
8877 <tt>4 </tt> LatexName chunkref
8878 <tt>5 </tt> PassThru 1
8879 <tt>6 </tt> LabelFont
8880 <tt>7 </tt> Shape Italic
8881 <tt>8 </tt> Color red
8882 <tt>9 </tt> EndFont</div></pre>
8884 <tt class="verbatim"><div class="compact-block">
8885 <tt>10 </tt>End
8886 </div></tt>
8887 </p>
8888 </p><table style="width: 100%" id="code-end-chunkref-1">
8889 <tbody><tr>
8890 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
8891 </tr></tbody>
8892 </table></font>
8893 </p>
8894 <h2 id="auto-88"><a id="sec:Latex-Macros"></a>15.2<span style="margin-left: 1em"></span>Latex Macros</h2>
8896 We require the listings, noweb and xargs packages. As noweb defines it's
8897 own <tt class="verbatim">\code</tt> environment, we re-define the one that L<span
8898 style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X logical markup module expects here.
8899 </p>
8901 <font size="-1"><p>
8902 <a id="code-label-./fangle.sty-1"></a>
8903 <table style="width: 100%" id="code-ref-./fangle.sty-1">
8904 <tbody><tr>
8905 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
8906 <font color="blue">./fangle.sty</font>[1](), lang=<font color="blue">tex</font>
8907 &equiv;
8908 </div><div class="right-tab">
8909 <a href="#code-ref-./fangle.sty-2">77a</a>&#x22B3;
8910 </div></font></td>
8911 </tr></tbody>
8912 </table>
8913 </p><p>
8915 <tt class="verbatim"><div class="compact-block">
8916 <tt>1 </tt>\usepackage{listings}%
8917 </div></tt>
8918 </p>
8919 <pre class="verbatim" xml:space="preserve">
8920 <div class="compact-block"><tt>2 </tt>\usepackage{noweb}%
8921 <tt>3 </tt>\usepackage{xargs}%</div></pre>
8923 <tt class="verbatim"><div class="compact-block">
8924 <tt>4 </tt>\renewcommand{\code}[1]{\texttt{#1}}%
8925 </div></tt>
8926 </p>
8927 </p><p>
8928 <a id="code-end-./fangle.sty-1"></a>
8929 </p></font>
8930 </p>
8932 We also define a <tt class="verbatim">CChunk</tt> macro, for use as: <tt class="verbatim">\begin{CChunk}</tt>
8933 which will need renaming to <tt class="verbatim">\begin{Chunk}</tt> when I can do
8934 this without clashing with <tt class="verbatim">\Chunk</tt>.
8935 </p>
8937 <font size="-1"><p>
8938 <a id="code-label-./fangle.sty-2"></a>
8939 <table style="width: 100%" id="code-ref-./fangle.sty-2">
8940 <tbody><tr>
8941 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
8942 <font color="blue">./fangle.sty</font>[2]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
8943 lang=<font color="blue"></font> + &equiv;
8944 </div><div class="right-tab">
8945 &#x22B2;<a href="#code-ref-./fangle.sty-1">76d</a> <a href="#code-ref-./fangle.sty-3">77b</a>&#x25BF;
8946 </div></font></td>
8947 </tr></tbody>
8948 </table>
8949 </p><p>
8950 <tt class="verbatim"><div class="compact-block">
8951 <tt>5 </tt>\lstnewenvironment{Chunk}{\relax}{\relax}%
8952 </div></tt>
8953 </p><p>
8954 <a id="code-end-./fangle.sty-2"></a>
8955 </p></font>
8956 </p>
8958 We also define a suitable <tt class="verbatim">\lstset</tt> of parameters that
8959 suit the literate programming style after the fashion of <class style="font-variant: small-caps">noweave</class>.
8960 </p>
8962 <font size="-1"><p>
8963 <a id="code-label-./fangle.sty-3"></a>
8964 <table style="width: 100%" id="code-ref-./fangle.sty-3">
8965 <tbody><tr>
8966 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
8967 <font color="blue">./fangle.sty</font>[3]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
8968 lang=<font color="blue"></font> + &equiv;
8969 </div><div class="right-tab">
8970 &#x25B5;<a href="#code-ref-./fangle.sty-2">77a</a> <a href="#code-ref-./fangle.sty-4">77c</a>&#x25BF;
8971 </div></font></td>
8972 </tr></tbody>
8973 </table>
8974 </p><p>
8976 <tt class="verbatim"><div class="compact-block">
8977 <tt>6 </tt>\lstset{numbers=left, stepnumber=5, numbersep=5pt,
8978 </div></tt>
8979 </p>
8980 <pre class="verbatim" xml:space="preserve">
8981 <div class="compact-block"><tt>7 </tt> breaklines=false,basicstyle=\ttfamily,</div></pre>
8983 <tt class="verbatim"><div class="compact-block">
8984 <tt>8 </tt> numberstyle=\tiny, language=C}%
8985 </div></tt>
8986 </p>
8987 </p><p>
8988 <a id="code-end-./fangle.sty-3"></a>
8989 </p></font>
8990 </p>
8992 We also define a notangle-like mechanism for escaping to LaTeX from the
8993 listing, and by which we can refer to other listings. We declare the <tt
8994 class="verbatim">=&lt;...&gt;</tt> sequence to contain LaTeX code, and include
8995 another like this chunk: <tt class="verbatim">=&lt;\chunkref{chunkname}&gt;</tt>.
8996 However, because <tt class="verbatim">=&lt;...&gt;</tt> is already defined to
8997 contain LaTeX code for this document &ndash;- this is a fangle document
8998 after all &ndash;- the code fragment below effectively contains the
8999 LaTeX code: <tt class="verbatim">}{</tt>. To avoid problems with document
9000 generation, I had to declare an lstlistings property: <tt class="verbatim">escapeinside={}</tt>
9001 for this listing only; which in L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X
9002 was done by right-clicking the listings inset, choosing
9003 settings-&gt;advanced. Therefore <tt class="verbatim">=&lt;</tt> isn't interpreted
9004 literally here, in a listing when the escape sequence is already defined
9005 as shown... we need to somehow escape this representation...
9006 </p>
9008 <font size="-1"><p>
9009 <a id="code-label-./fangle.sty-4"></a>
9010 <table style="width: 100%" id="code-ref-./fangle.sty-4">
9011 <tbody><tr>
9012 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9013 <font color="blue">./fangle.sty</font>[4]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9014 lang=<font color="blue"></font> + &equiv;
9015 </div><div class="right-tab">
9016 &#x25B5;<a href="#code-ref-./fangle.sty-3">77b</a> <a href="#code-ref-./fangle.sty-5">77d</a>&#x25BF;
9017 </div></font></td>
9018 </tr></tbody>
9019 </table>
9020 </p><p>
9021 <tt class="verbatim"><div class="compact-block">
9022 <tt>9 </tt>\lstset{escapeinside={=&lt;}{&gt;}}%
9023 </div></tt>
9024 </p><p>
9025 <a id="code-end-./fangle.sty-4"></a>
9026 </p></font>
9027 </p>
9029 Although our macros will contain the <tt class="verbatim">@</tt> symbol, they will
9030 be included in a <tt class="verbatim">\makeatletter</tt> section by L<span style="margin-left: -0.1667em"></span>Y<span
9031 style="margin-left: -0.125em"></span>X; however we keep the commented out <tt class="verbatim">\makeatletter</tt>
9032 as a reminder. The listings package likes to centre the titles, but
9033 noweb titles are specially formatted and must be left aligned. The
9034 simplest way to do this turned out to be by removing the definition of
9035 <tt class="verbatim">\lst@maketitle</tt>. This may interact badly if other
9036 listings want a regular title or caption. We remember the old maketitle
9037 in case we need it.
9038 </p>
9040 <font size="-1"><p>
9041 <a id="code-label-./fangle.sty-5"></a>
9042 <table style="width: 100%" id="code-ref-./fangle.sty-5">
9043 <tbody><tr>
9044 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9045 <font color="blue">./fangle.sty</font>[5]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9046 lang=<font color="blue"></font> + &equiv;
9047 </div><div class="right-tab">
9048 &#x25B5;<a href="#code-ref-./fangle.sty-4">77c</a> <a href="#code-ref-./fangle.sty-6">77e</a>&#x25BF;
9049 </div></font></td>
9050 </tr></tbody>
9051 </table>
9052 </p><p>
9054 <tt class="verbatim"><div class="compact-block">
9055 <tt>10 </tt>%\makeatletter
9056 </div></tt>
9057 </p>
9058 <pre class="verbatim" xml:space="preserve">
9059 <div class="compact-block"><tt>11 </tt>%somehow re-defining maketitle gives us a left-aligned title
9060 <tt>12 </tt>%which is extactly what our specially formatted title needs!
9061 <tt>13 </tt>\global\let\fangle@lst@maketitle\lst@maketitle%</div></pre>
9063 <tt class="verbatim"><div class="compact-block">
9064 <tt>14 </tt>\global\def\lst@maketitle{}%
9065 </div></tt>
9066 </p>
9067 </p><p>
9068 <a id="code-end-./fangle.sty-5"></a>
9069 </p></font>
9070 </p>
9071 <h3 id="auto-89"><a id="sub:The-chunk-command"></a>15.2.1<span style="margin-left: 1em"></span>The chunk command</h3>
9073 Our chunk command accepts one argument, and calls <tt class="verbatim">\ltset</tt>.
9074 Although <tt class="verbatim">\ltset</tt> will note the name, this is erased when
9075 the next <tt class="verbatim">\lstlisting</tt> starts, so we make a note of this
9076 in <tt class="verbatim">\lst@chunkname</tt> and restore in in lstlistings Init
9077 hook.
9078 </p>
9080 <font size="-1"><p>
9081 <a id="code-label-./fangle.sty-6"></a>
9082 <table style="width: 100%" id="code-ref-./fangle.sty-6">
9083 <tbody><tr>
9084 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9085 <font color="blue">./fangle.sty</font>[6]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9086 lang=<font color="blue"></font> + &equiv;
9087 </div><div class="right-tab">
9088 &#x25B5;<a href="#code-ref-./fangle.sty-5">77d</a> <a href="#code-ref-./fangle.sty-7">78a</a>&#x22B3;
9089 </div></font></td>
9090 </tr></tbody>
9091 </table>
9092 </p><p>
9094 <tt class="verbatim"><div class="compact-block">
9095 <tt>15 </tt>\def\Chunk#1{%
9096 </div></tt>
9097 </p>
9098 <pre class="verbatim" xml:space="preserve">
9099 <div class="compact-block"><tt>16 </tt> \lstset{title={\fanglecaption},name=#1}%
9100 <tt>17 </tt> \global\edef\lst@chunkname{\lst@intname}%
9101 <tt>18 </tt>}%</div></pre>
9103 <tt class="verbatim"><div class="compact-block">
9104 <tt>19 </tt>\def\lst@chunkname{\empty}%
9105 </div></tt>
9106 </p>
9107 </p><p>
9108 <a id="code-end-./fangle.sty-6"></a>
9109 </p></font>
9110 </p>
9111 <h4 id="auto-90">15.2.1.1<span style="margin-left: 1em"></span>Chunk parameters</h4>
9113 Fangle permits parameterized chunks, and requires the paramters to be
9114 specified as listings options. The fangle script uses this, and although
9115 we don't do anything with these in the LaTeX code right now, we need to
9116 stop the listings package complaining.
9117 </p>
9119 <font size="-1"><p>
9120 <a id="code-label-./fangle.sty-7"></a>
9121 <table style="width: 100%" id="code-ref-./fangle.sty-7">
9122 <tbody><tr>
9123 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9124 <font color="blue">./fangle.sty</font>[7]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9125 lang=<font color="blue"></font> + &equiv;
9126 </div><div class="right-tab">
9127 &#x22B2;<a href="#code-ref-./fangle.sty-6">77e</a> <a href="#code-ref-./fangle.sty-8">78b</a>&#x25BF;
9128 </div></font></td>
9129 </tr></tbody>
9130 </table>
9131 </p><p>
9132 <tt class="verbatim"><div class="compact-block">
9133 <tt>20 </tt>\lst@Key{params}\relax{\def\fangle@chunk@params{#1}}%
9134 </div></tt>
9135 </p><p>
9136 <a id="code-end-./fangle.sty-7"></a>
9137 </p></font>
9138 </p>
9140 As it is common to define a chunk which then needs appending to another
9141 chunk, and annoying to have to declare a single line chunk to manage the
9142 include, we support an append= option.
9143 </p>
9145 <font size="-1"><p>
9146 <a id="code-label-./fangle.sty-8"></a>
9147 <table style="width: 100%" id="code-ref-./fangle.sty-8">
9148 <tbody><tr>
9149 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9150 <font color="blue">./fangle.sty</font>[8]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9151 lang=<font color="blue"></font> + &equiv;
9152 </div><div class="right-tab">
9153 &#x25B5;<a href="#code-ref-./fangle.sty-7">78a</a> <a href="#code-ref-./fangle.sty-9">78c</a>&#x25BF;
9154 </div></font></td>
9155 </tr></tbody>
9156 </table>
9157 </p><p>
9158 <tt class="verbatim"><div class="compact-block">
9159 <tt>21 </tt>\lst@Key{append}\relax{\def\fangle@chunk@append{#1}}%
9160 </div></tt>
9161 </p><p>
9162 <a id="code-end-./fangle.sty-8"></a>
9163 </p></font>
9164 </p>
9165 <h3 id="auto-91">15.2.2<span style="margin-left: 1em"></span>The noweb styled caption</h3>
9167 We define a public macro <tt class="verbatim">\fanglecaption</tt> which can be set
9168 as a regular title. By means of <tt class="verbatim">\protect</tt>, It expands to
9169 <tt class="verbatim">\fangle@caption</tt> at the appopriate time when the caption
9170 is emitted.
9171 </p>
9173 <font size="-1"><p>
9174 <a id="code-label-./fangle.sty-9"></a>
9175 <table style="width: 100%" id="code-ref-./fangle.sty-9">
9176 <tbody><tr>
9177 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9178 <font color="blue">./fangle.sty</font>[9]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9179 lang=<font color="blue"></font> + &equiv;
9180 </div><div class="right-tab">
9181 &#x25B5;<a href="#code-ref-./fangle.sty-8">78b</a> <a href="#code-ref-./fangle.sty-10">78d</a>&#x25BF;
9182 </div></font></td>
9183 </tr></tbody>
9184 </table>
9185 </p><p>
9186 <tt class="verbatim"><div class="compact-block">
9187 \def\fanglecaption{\protect\fangle@caption}%
9188 </div></tt>
9189 </p><p>
9190 <a id="code-end-./fangle.sty-9"></a>
9191 </p></font>
9192 </p>
9193 <table style="width: 100%">
9194 <tbody><tr>
9195 <td style="text-align: center; padding-left: 0em; padding-right: 0em"><p>
9196 22c &#x27E8;some-chunk 19b&#x27E9;&equiv;+ &#x22B2;22b 24d&#x22B3;
9197 </p><p>
9199 </p><p>
9200 In this example, the current chunk is 22c, and therefore the third
9201 chunk on page 22.
9202 </p><p>
9203 It's name is some-chunk.
9204 </p><p>
9205 The first chunk with this name (19b) occurs as the second chunk on
9206 page 19.
9207 </p><p>
9208 The previous chunk (22d) with the same name is the second chunk on
9209 page 22.
9210 </p><p>
9211 The next chunk (24d) is the fourth chunk on page 24.
9212 </p></td>
9213 </tr><tr>
9214 <td style="text-align: center; padding-left: 0em; padding-right: 0em; height: 0.5em"></td>
9215 </tr><tr>
9216 <td style="text-align: center; padding-left: 0em; padding-right: 0em; padding-left: 1.5em; padding-right: 1.5em"><p>
9217 <font size="-1"><p>
9218 <b>Figure 1. </b><a id="auto-92"></a>Noweb Heading<a id="noweb heading"></a>
9219 </p></font>
9220 </p></td>
9221 </tr></tbody>
9222 </table>
9224 The general noweb output format compactly identifies the current chunk,
9225 and references to the first chunk, and the previous and next chunks that
9226 have the same name.
9227 </p>
9229 This means that we need to keep a counter for each chunk-name, that we
9230 use to count chunks of the same name.
9231 </p>
9232 <h3 id="auto-93">15.2.3<span style="margin-left: 1em"></span>The chunk counter</h3>
9234 It would be natural to have a counter for each chunk name, but TeX would
9235 soon run out of counters
9237 <font size="-1"><div align="justify">
9238 <div style="margin-left: 0px">
9239 <div style="margin-right: 0px">
9241 . ...soon did run out of counters and so I had to re-write the
9242 LaTeX macros to share a counter as described here.
9243 </p>
9244 </div>
9245 </div>
9246 </div></font>
9247 </p>
9248 <span style="margin-left: 0em"></span>
9249 <a id="footnr-1"></a>
9250 <sup><a href="#footnote-1">1</a></sup>
9251 , so we have one counter which we save at the end of a chunk and restore
9252 at the beginning of a chunk.
9253 </p>
9255 <font size="-1"><p>
9256 <a id="code-label-./fangle.sty-10"></a>
9257 <table style="width: 100%" id="code-ref-./fangle.sty-10">
9258 <tbody><tr>
9259 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9260 <font color="blue">./fangle.sty</font>[10]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9261 lang=<font color="blue"></font> + &equiv;
9262 </div><div class="right-tab">
9263 &#x25B5;<a href="#code-ref-./fangle.sty-9">78c</a> <a href="#code-ref-./fangle.sty-11">79c</a>&#x22B3;
9264 </div></font></td>
9265 </tr></tbody>
9266 </table>
9267 </p><p>
9268 <tt class="verbatim"><div class="compact-block">
9269 <tt>22 </tt>\newcounter{fangle@chunkcounter}%
9270 </div></tt>
9271 </p><p>
9272 <a id="code-end-./fangle.sty-10"></a>
9273 </p></font>
9274 </p>
9276 We construct the name of this variable to store the counter to be the
9277 text <tt class="verbatim">lst-chunk-</tt> prefixed onto the chunks own name, and
9278 store it in <tt class="verbatim">\chunkcount</tt>.
9279 </p>
9281 We save the counter like this:
9282 </p>
9284 <font size="-1"><p>
9285 <a id="code-label-save-counter-1"></a>
9286 <table style="width: 100%" id="code-ref-save-counter-1">
9287 <tbody><tr>
9288 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="save-counter"></a><font size="-1"><font color="blue">save-counter</font>[1](),
9289 lang=<font color="blue"></font> &equiv;</font></td>
9290 </tr></tbody>
9291 </table>
9292 </p><p>
9293 <tt class="verbatim"><div class="compact-block">
9294 \global\expandafter\edef\csname
9295 \chunkcount\endcsname{\arabic{fangle@chunkcounter}}%
9296 </div></tt>
9297 </p><table style="width: 100%" id="code-end-save-counter-1">
9298 <tbody><tr>
9299 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9300 </tr></tbody>
9301 </table></font>
9302 </p>
9304 and restore the counter like this:
9305 </p>
9307 <font size="-1"><p>
9308 <a id="code-label-restore-counter-1"></a>
9309 <table style="width: 100%" id="code-ref-restore-counter-1">
9310 <tbody><tr>
9311 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="restore-counter"></a><font size="-1"><font color="blue">restore-counter</font>[1](),
9312 lang=<font color="blue"></font> &equiv;</font></td>
9313 </tr></tbody>
9314 </table>
9315 </p><p>
9316 <tt class="verbatim"><div class="compact-block">
9317 \setcounter{fangle@chunkcounter}{\csname \chunkcount\endcsname}%
9318 </div></tt>
9319 </p><table style="width: 100%" id="code-end-restore-counter-1">
9320 <tbody><tr>
9321 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9322 </tr></tbody>
9323 </table></font>
9324 </p>
9326 If there does not already exist a variable whose name is stored in <tt
9327 class="verbatim">\chunkcount</tt>, then we know we are the first chunk with this
9328 name, and then define a counter.
9329 </p>
9331 Although chunks of the same name share a common counter, they must still
9332 be distinguished. We use is the internal name of the listing, suffixed
9333 by the counter value. So the first chunk might be <tt class="verbatim">something-1</tt>
9334 and the second chunk be <tt class="verbatim">something-2</tt>, etc.
9335 </p>
9337 We also calculate the name of the previous chunk if we can (before we
9338 increment the chunk counter). If this is the first chunk of that name,
9339 then <tt class="verbatim">\prevchunkname</tt> is set to <tt class="verbatim">\relax</tt>
9340 which the noweb package will interpret as not existing.
9341 </p>
9343 <font size="-1"><p>
9344 <a id="code-label-./fangle.sty-11"></a>
9345 <table style="width: 100%" id="code-ref-./fangle.sty-11">
9346 <tbody><tr>
9347 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9348 <font color="blue">./fangle.sty</font>[11]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9349 lang=<font color="blue"></font> + &equiv;
9350 </div><div class="right-tab">
9351 &#x22B2;<a href="#code-ref-./fangle.sty-10">78d</a> <a href="#code-ref-./fangle.sty-12">79d</a>&#x25BF;
9352 </div></font></td>
9353 </tr></tbody>
9354 </table>
9355 </p><p>
9357 <tt class="verbatim"><div class="compact-block">
9358 <tt>23 </tt>\def\fangle@caption{%
9359 </div></tt>
9360 </p>
9361 <pre class="verbatim" xml:space="preserve">
9362 <div class="compact-block"><tt>24 </tt> \edef\chunkcount{lst-chunk-\lst@intname}%
9363 <tt>25 </tt> \@ifundefined{\chunkcount}{%
9364 <tt>26 </tt> \expandafter\gdef\csname \chunkcount\endcsname{0}%
9365 <tt>27 </tt> \setcounter{fangle@chunkcounter}{\csname \chunkcount\endcsname}%
9366 <tt>28 </tt> \let\prevchunkname\relax%
9367 <tt>29 </tt> }{%
9368 <tt>30 </tt> \setcounter{fangle@chunkcounter}{\csname \chunkcount\endcsname}%
9369 <tt>31 </tt> \edef\prevchunkname{\lst@intname-\arabic{fangle@chunkcounter}}%</div></pre>
9371 <tt class="verbatim"><div class="compact-block">
9372 <tt>32 </tt> }%
9373 </div></tt>
9374 </p>
9375 </p><p>
9376 <a id="code-end-./fangle.sty-11"></a>
9377 </p></font>
9378 </p>
9380 After incrementing the chunk counter, we then define the name of this
9381 chunk, as well as the name of the first chunk.
9382 </p>
9384 <font size="-1"><p>
9385 <a id="code-label-./fangle.sty-12"></a>
9386 <table style="width: 100%" id="code-ref-./fangle.sty-12">
9387 <tbody><tr>
9388 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9389 <font color="blue">./fangle.sty</font>[12]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9390 lang=<font color="blue"></font> + &equiv;
9391 </div><div class="right-tab">
9392 &#x25B5;<a href="#code-ref-./fangle.sty-11">79c</a> <a href="#code-ref-./fangle.sty-13">79e</a>&#x25BF;
9393 </div></font></td>
9394 </tr></tbody>
9395 </table>
9396 </p><p>
9398 <tt class="verbatim"><div class="compact-block">
9399 <tt>33 </tt> \addtocounter{fangle@chunkcounter}{1}%
9400 </div></tt>
9401 </p>
9402 <pre class="verbatim" xml:space="preserve">
9403 <div class="compact-block"><tt>34 </tt> \global\expandafter\edef\csname \chunkcount\endcsname{\arabic{fangle@chunkcounter}}%
9404 <tt>35 </tt> \edef\chunkname{\lst@intname-\arabic{fangle@chunkcounter}}%</div></pre>
9406 <tt class="verbatim"><div class="compact-block">
9407 <tt>36 </tt> \edef\firstchunkname{\lst@intname-1}%
9408 </div></tt>
9409 </p>
9410 </p><p>
9411 <a id="code-end-./fangle.sty-12"></a>
9412 </p></font>
9413 </p>
9415 We now need to calculate the name of the next chunk. We do this by
9416 temporarily skipping the counter on by one; however there may not
9417 actually be another chunk with this name! We detect this by also
9418 defining a label for each chunk based on the chunkname. If there is a
9419 next chunkname then it will define a label with that name. As labels are
9420 persistent, we can at least tell the second time LaTeX is run. If we
9421 don't find such a defined label then we define <tt class="verbatim">\nextchunkname</tt>
9422 to <tt class="verbatim">\relax</tt>.
9423 </p>
9425 <font size="-1"><p>
9426 <a id="code-label-./fangle.sty-13"></a>
9427 <table style="width: 100%" id="code-ref-./fangle.sty-13">
9428 <tbody><tr>
9429 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9430 <font color="blue">./fangle.sty</font>[13]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9431 lang=<font color="blue"></font> + &equiv;
9432 </div><div class="right-tab">
9433 &#x25B5;<a href="#code-ref-./fangle.sty-12">79d</a> <a href="#code-ref-./fangle.sty-14">80a</a>&#x22B3;
9434 </div></font></td>
9435 </tr></tbody>
9436 </table>
9437 </p><p>
9439 <tt class="verbatim"><div class="compact-block">
9440 <tt>37 </tt> \addtocounter{fangle@chunkcounter}{1}%
9441 </div></tt>
9442 </p>
9443 <pre class="verbatim" xml:space="preserve">
9444 <div class="compact-block"><tt>38 </tt> \edef\nextchunkname{\lst@intname-\arabic{fangle@chunkcounter}}%</div></pre>
9446 <tt class="verbatim"><div class="compact-block">
9447 <tt>39 </tt>
9448 \@ifundefined{r@label-\nextchunkname}{\let\nextchunkname\relax}{}%
9449 </div></tt>
9450 </p>
9451 </p><p>
9452 <a id="code-end-./fangle.sty-13"></a>
9453 </p></font>
9454 </p>
9456 The noweb package requires that we define a <tt class="verbatim">\sublabel</tt>
9457 for every chunk, with a unique name, which is then used to print out
9458 it's navigation hints.
9459 </p>
9461 We also define a regular label for this chunk, as was mentioned above
9462 when we calculated <tt class="verbatim">\nextchunkname</tt>. This requires LaTeX
9463 to be run at least twice after new chunk sections are added &ndash;- but
9464 noweb requried that anyway.
9465 </p>
9467 <font size="-1"><p>
9468 <a id="code-label-./fangle.sty-14"></a>
9469 <table style="width: 100%" id="code-ref-./fangle.sty-14">
9470 <tbody><tr>
9471 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9472 <font color="blue">./fangle.sty</font>[14]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9473 lang=<font color="blue"></font> + &equiv;
9474 </div><div class="right-tab">
9475 &#x22B2;<a href="#code-ref-./fangle.sty-13">79e</a> <a href="#code-ref-./fangle.sty-15">80b</a>&#x25BF;
9476 </div></font></td>
9477 </tr></tbody>
9478 </table>
9479 </p><p>
9481 <tt class="verbatim"><div class="compact-block">
9482 <tt>40 </tt> \sublabel{\chunkname}%
9483 </div></tt>
9484 </p>
9485 <pre class="verbatim" xml:space="preserve">
9486 <div class="compact-block"><tt>41 </tt>% define this label for every chunk instance, so we
9487 <tt>42 </tt>% can tell when we are the last chunk of this name</div></pre>
9489 <tt class="verbatim"><div class="compact-block">
9490 <tt>43 </tt> \label{label-\chunkname}%
9491 </div></tt>
9492 </p>
9493 </p><p>
9494 <a id="code-end-./fangle.sty-14"></a>
9495 </p></font>
9496 </p>
9498 We also try and add the chunk to the list of listings, but I'm afraid we
9499 don't do very well. We want each chunk name listing once, with all of
9500 it's references.
9501 </p>
9503 <font size="-1"><p>
9504 <a id="code-label-./fangle.sty-15"></a>
9505 <table style="width: 100%" id="code-ref-./fangle.sty-15">
9506 <tbody><tr>
9507 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9508 <font color="blue">./fangle.sty</font>[15]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9509 lang=<font color="blue"></font> + &equiv;
9510 </div><div class="right-tab">
9511 &#x25B5;<a href="#code-ref-./fangle.sty-14">80a</a> <a href="#code-ref-./fangle.sty-16">80c</a>&#x25BF;
9512 </div></font></td>
9513 </tr></tbody>
9514 </table>
9515 </p><p>
9516 <tt class="verbatim"><div class="compact-block">
9517 <tt>44 </tt>
9518 \addcontentsline{lol}{lstlisting}{\lst@name~[\protect\subpageref{\chunkname}]}%
9519 </div></tt>
9520 </p><p>
9521 <a id="code-end-./fangle.sty-15"></a>
9522 </p></font>
9523 </p>
9525 We then call the noweb output macros in the same way that noweave
9526 generates them, except that we don't need to call <tt class="verbatim">\nwstartdeflinemarkup</tt>
9527 or <tt class="verbatim">\nwenddeflinemarkup</tt> <class style="font-family: Times New Roman">&mdash;</class> and
9528 if we do, it messes up the output somewhat.
9529 </p>
9531 <font size="-1"><p>
9532 <a id="code-label-./fangle.sty-16"></a>
9533 <table style="width: 100%" id="code-ref-./fangle.sty-16">
9534 <tbody><tr>
9535 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9536 <font color="blue">./fangle.sty</font>[16]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9537 lang=<font color="blue"></font> + &equiv;
9538 </div><div class="right-tab">
9539 &#x25B5;<a href="#code-ref-./fangle.sty-15">80b</a> <a href="#code-ref-./fangle.sty-17">80d</a>&#x25BF;
9540 </div></font></td>
9541 </tr></tbody>
9542 </table>
9543 </p><p>
9545 <tt class="verbatim"><div class="compact-block">
9546 <tt>45 </tt> \nwmargintag{%
9547 </div></tt>
9548 </p>
9549 <pre class="verbatim" xml:space="preserve">
9550 <div class="compact-block"><tt>46 </tt> {%
9551 <tt>47 </tt> \nwtagstyle{}%
9552 <tt>48 </tt> \subpageref{\chunkname}%
9553 <tt>49 </tt> }%
9554 <tt>50 </tt> }%
9555 <tt>51 </tt>%
9556 <tt>52 </tt> \moddef{%
9557 <tt>53 </tt> {\lst@name}%
9558 <tt>54 </tt> {%
9559 <tt>55 </tt> \nwtagstyle{}\/%
9560 <tt>56 </tt> \@ifundefined{fangle@chunk@params}{}{%
9561 <tt>57 </tt> (\fangle@chunk@params)%
9562 <tt>58 </tt> }%
9563 <tt>59 </tt> [\csname \chunkcount\endcsname]~%
9564 <tt>60 </tt> \subpageref{\firstchunkname}%
9565 <tt>61 </tt> }%
9566 <tt>62 </tt> \@ifundefined{fangle@chunk@append}{}{%
9567 <tt>63 </tt> \ifx{}\fangle@chunk@append{x}\else%
9568 <tt>64 </tt> ,~add~to~\fangle@chunk@append%
9569 <tt>65 </tt> \fi%
9570 <tt>66 </tt> }%
9571 <tt>67 </tt>\global\def\fangle@chunk@append{}%
9572 <tt>68 </tt>\lstset{append=x}%
9573 <tt>69 </tt> }%
9574 <tt>70 </tt>%
9575 <tt>71 </tt> \ifx\relax\prevchunkname\endmoddef\else\plusendmoddef\fi%
9576 <tt>72 </tt>% \nwstartdeflinemarkup%
9577 <tt>73 </tt> \nwprevnextdefs{\prevchunkname}{\nextchunkname}%
9578 <tt>74 </tt>% \nwenddeflinemarkup%</div></pre>
9580 <tt class="verbatim"><div class="compact-block">
9581 <tt>75 </tt>}%
9582 </div></tt>
9583 </p>
9584 </p><p>
9585 <a id="code-end-./fangle.sty-16"></a>
9586 </p></font>
9587 </p>
9589 Originally this was developed as a <tt class="verbatim">listings</tt> aspect, in
9590 the Init hook, but it was found easier to affect the title without using
9591 a hook <class style="font-family: Times New Roman">&mdash;</class> <tt class="verbatim">\lst@AddToHookExe{PreSet}</tt>
9592 is still required to set the listings name to the name passed to the <tt
9593 class="verbatim">\Chunk</tt> command, though.
9594 </p>
9596 <font size="-1"><p>
9597 <a id="code-label-./fangle.sty-17"></a>
9598 <table style="width: 100%" id="code-ref-./fangle.sty-17">
9599 <tbody><tr>
9600 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9601 <font color="blue">./fangle.sty</font>[17]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9602 lang=<font color="blue"></font> + &equiv;
9603 </div><div class="right-tab">
9604 &#x25B5;<a href="#code-ref-./fangle.sty-16">80c</a> <a href="#code-ref-./fangle.sty-18">81a</a>&#x22B3;
9605 </div></font></td>
9606 </tr></tbody>
9607 </table>
9608 </p><p>
9610 <tt class="verbatim"><div class="compact-block">
9611 <tt>76 </tt>%\lst@BeginAspect{fangle}
9612 </div></tt>
9613 </p>
9614 <pre class="verbatim" xml:space="preserve">
9615 <div class="compact-block"><tt>77 </tt>%\lst@Key{fangle}{true}[t]{\lstKV@SetIf{#1}{true}}
9616 <tt>78 </tt>\lst@AddToHookExe{PreSet}{\global\let\lst@intname\lst@chunkname}
9617 <tt>79 </tt>\lst@AddToHook{Init}{}%\fangle@caption}</div></pre>
9619 <tt class="verbatim"><div class="compact-block">
9620 <tt>80 </tt>%\lst@EndAspect
9621 </div></tt>
9622 </p>
9623 </p><p>
9624 <a id="code-end-./fangle.sty-17"></a>
9625 </p></font>
9626 </p>
9627 <h3 id="auto-94">15.2.4<span style="margin-left: 1em"></span>Cross references</h3>
9629 We define the <tt class="verbatim">\chunkref</tt> command which makes it easy to
9630 generate visual references to different code chunks, e.g.
9631 </p>
9632 <table style="display: inline; vertical-align: -2.2em">
9633 <tbody><tr>
9634 <td style="border-right: 1px solid; border-bottom: 1px solid; border-left: 1px solid; border-top: 1px solid">Macro</td>
9635 <td style="border-right: 1px solid; border-bottom: 1px solid; border-top: 1px solid">Appearance</td>
9636 </tr><tr>
9637 <td style="border-right: 1px solid; border-bottom: 1px solid; border-left: 1px solid"><tt class="verbatim">\chunkref{preamble}</tt></td>
9638 <td style="border-right: 1px solid; border-bottom: 1px solid"></td>
9639 </tr><tr>
9640 <td style="border-right: 1px solid; border-bottom: 1px solid; border-left: 1px solid"><tt class="verbatim">\chunkref[3]{preamble}</tt></td>
9641 <td style="border-right: 1px solid; border-bottom: 1px solid"></td>
9642 </tr><tr>
9643 <td style="border-right: 1px solid; border-bottom: 1px solid; border-left: 1px solid"><tt class="verbatim">\chunkref{preamble}[arg1, arg2]</tt></td>
9644 <td style="border-right: 1px solid; border-bottom: 1px solid"></td>
9645 </tr></tbody>
9646 </table>
9648 Chunkref can also be used within a code chunk to include another code
9649 chunk. The third optional parameter to chunkref is a comma sepatarated
9650 list of arguments, which will replace defined parameters in the
9651 chunkref.
9652 </p>
9653 <p style="margin-top: 1em; margin-bottom: 1em">
9654 <b>Note <class style="font-style: normal">1</class>. </b>Darn it, if I have: <tt class="verbatim">=&lt;\chunkref{new-mode-tracker}[{chunks[chunk_name,
9655 &quot;language&quot;]},{mode}]&gt;</tt> the inner braces (inside [ ])
9656 cause _ to signify subscript even though we have <tt class="verbatim">lst@ReplaceIn</tt>
9657 </p>
9659 <font size="-1"><p>
9660 <a id="code-label-./fangle.sty-18"></a>
9661 <table style="width: 100%" id="code-ref-./fangle.sty-18">
9662 <tbody><tr>
9663 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9664 <font color="blue">./fangle.sty</font>[18]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9665 lang=<font color="blue"></font> + &equiv;
9666 </div><div class="right-tab">
9667 &#x22B2;<a href="#code-ref-./fangle.sty-17">80d</a> <a href="#code-ref-./fangle.sty-19">82a</a>&#x22B3;
9668 </div></font></td>
9669 </tr></tbody>
9670 </table>
9671 </p><p>
9673 <tt class="verbatim"><div class="compact-block">
9674 <tt>81 </tt>\def\chunkref@args#1,{%
9675 </div></tt>
9676 </p>
9677 <pre class="verbatim" xml:space="preserve">
9678 <div class="compact-block"><tt>82 </tt> \def\arg{#1}%
9679 <tt>83 </tt> \lst@ReplaceIn\arg\lst@filenamerpl%
9680 <tt>84 </tt> \arg%
9681 <tt>85 </tt> \@ifnextchar){\relax}{, \chunkref@args}%
9682 <tt>86 </tt>}%
9683 <tt>87 </tt>\newcommand\chunkref[2][0]{%
9684 <tt>88 </tt> \@ifnextchar({\chunkref@i{#1}{#2}}{\chunkref@i{#1}{#2}()}%
9685 <tt>89 </tt>}%
9686 <tt>90 </tt>\def\chunkref@i#1#2(#3){%
9687 <tt>91 </tt> \def\zero{0}%
9688 <tt>92 </tt> \def\chunk{#2}%
9689 <tt>93 </tt> \def\chunkno{#1}%
9690 <tt>94 </tt> \def\chunkargs{#3}%
9691 <tt>95 </tt> \ifx\chunkno\zero%
9692 <tt>96 </tt> \def\chunkname{#2-1}%
9693 <tt>97 </tt> \else%
9694 <tt>98 </tt> \def\chunkname{#2-\chunkno}%
9695 <tt>99 </tt> \fi%
9696 <tt>100 </tt> \let\lst@arg\chunk%
9697 <tt>101 </tt> \lst@ReplaceIn\chunk\lst@filenamerpl%
9698 <tt>102 </tt> \LA{%\moddef{%
9699 <tt>103 </tt> {\chunk}%
9700 <tt>104 </tt> {%
9701 <tt>105 </tt> \nwtagstyle{}\/%
9702 <tt>106 </tt> \ifx\chunkno\zero%
9703 <tt>107 </tt> \else%
9704 <tt>108 </tt> [\chunkno]%
9705 <tt>109 </tt> \fi%
9706 <tt>110 </tt> \ifx\chunkargs\empty%
9707 <tt>111 </tt> \else%
9708 <tt>112 </tt> (\chunkref@args #3,)%
9709 <tt>113 </tt> \fi%
9710 <tt>114 </tt> ~\subpageref{\chunkname}%
9711 <tt>115 </tt> }%
9712 <tt>116 </tt> }%
9713 <tt>117 </tt> \RA%\endmoddef%</div></pre>
9715 <tt class="verbatim"><div class="compact-block">
9716 <tt>118 </tt>}%
9717 </div></tt>
9718 </p>
9719 </p><p>
9720 <a id="code-end-./fangle.sty-18"></a>
9721 </p></font>
9722 </p>
9723 <h3 id="auto-95">15.2.5<span style="margin-left: 1em"></span>The end</h3>
9725 <font size="-1"><p>
9726 <a id="code-label-./fangle.sty-19"></a>
9727 <table style="width: 100%" id="code-ref-./fangle.sty-19">
9728 <tbody><tr>
9729 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./fangle.sty"></a><font size="-1"><div class="left-tab">
9730 <font color="blue">./fangle.sty</font>[19]() &uArr;<a href="#code-ref-./fangle.sty-1">76d</a>,
9731 lang=<font color="blue"></font> + &equiv;
9732 </div><div class="right-tab">
9733 &#x22B2;<a href="#code-ref-./fangle.sty-18">81a</a>
9734 </div></font></td>
9735 </tr></tbody>
9736 </table>
9737 </p><p>
9739 <tt class="verbatim"><div class="compact-block">
9740 <tt>119 </tt>%
9741 </div></tt>
9742 </p>
9743 <pre class="verbatim" xml:space="preserve">
9744 <div class="compact-block"></div></pre>
9746 <tt class="verbatim"><div class="compact-block">
9747 <tt>120 </tt>%\makeatother
9748 </div></tt>
9749 </p>
9750 </p><table style="width: 100%" id="code-end-./fangle.sty-19">
9751 <tbody><tr>
9752 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9753 </tr></tbody>
9754 </table></font>
9755 </p>
9756 <h1 id="auto-96">Chapter 16<br></br>Extracting fangle</h1>
9757 <h2 id="auto-97">16.1<span style="margin-left: 1em"></span>Extracting from Lyx</h2>
9759 To extract from L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X, you will
9760 need to configure L<span style="margin-left: -0.1667em"></span>Y<span style="margin-left: -0.125em"></span>X as
9761 explained in section <a href="#Configuring-the-build">?</a>.
9762 </p>
9764 <a id="lyx-build-script"></a>And this lyx-build scrap will extract fangle for me.
9765 </p>
9767 <font size="-1"><p>
9768 <a id="code-label-lyx-build-2"></a>
9769 <table style="width: 100%" id="code-ref-lyx-build-2">
9770 <tbody><tr>
9771 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="lyx-build"></a><font size="-1"><div class="left-tab">
9772 <font color="blue">lyx-build</font>[2]() &uArr;<a href="#code-ref-lyx-build-1">20a</a>,
9773 lang=<font color="blue">sh</font> + &equiv;
9774 </div><div class="right-tab">
9775 &#x22B2;<a href="#code-ref-lyx-build-1">20a</a>
9776 </div></font></td>
9777 </tr></tbody>
9778 </table>
9779 </p><p>
9781 <tt class="verbatim"><div class="compact-block">
9782 <tt>11 </tt>#! /bin/sh
9783 </div></tt>
9784 </p>
9785 <pre class="verbatim" xml:space="preserve">
9786 <div class="compact-block"><tt>12 </tt>set -x
9787 <tt>13 </tt>
9788 <tt>14 </tt>=&lt;\chunkref{lyx-build-helper}&gt;
9789 <tt>15 </tt>cd $PROJECT_DIR || exit 1
9790 <tt>16 </tt>
9791 <tt>17 </tt>/usr/local/bin/fangle -R./fangle $TEX_SRC &gt; ./fangle
9792 <tt>18 </tt>/usr/local/bin/fangle -R./fangle.module $TEX_SRC &gt; ./fangle.module
9793 <tt>19 </tt>
9794 <tt>20 </tt>=&lt;\chunkref{test:helpers}&gt;
9795 <tt>21 </tt>export FANGLE=./fangle
9796 <tt>22 </tt>export TMP=${TMP:-/tmp}
9797 <tt>23 </tt>=&lt;\chunkref{test:run-tests}&gt;
9798 <tt>24 </tt># Now check that we can extract a fangle that also passes the tests!
9799 <tt>25 </tt>$FANGLE -R./fangle $TEX_SRC &gt; ./new-fangle
9800 <tt>26 </tt>export FANGLE=./new-fangle</div></pre>
9802 <tt class="verbatim"><div class="compact-block">
9803 <tt>27 </tt>=&lt;\chunkref{test:run-tests}&gt;
9804 </div></tt>
9805 </p>
9806 </p><table style="width: 100%" id="code-end-lyx-build-2">
9807 <tbody><tr>
9808 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9809 </tr></tbody>
9810 </table></font>
9811 </p>
9813 <font size="-1"><p>
9814 <a id="code-label-test:run-tests-1"></a>
9815 <table style="width: 100%" id="code-ref-test:run-tests-1">
9816 <tbody><tr>
9817 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:run-tests"></a><font size="-1"><font color="blue">test:run-tests</font>[1](),
9818 lang=<font color="blue">sh</font> &equiv;</font></td>
9819 </tr></tbody>
9820 </table>
9821 </p><p>
9823 <tt class="verbatim"><div class="compact-block">
9824 <tt>1 </tt># run tests
9825 </div></tt>
9826 </p>
9827 <pre class="verbatim" xml:space="preserve">
9828 <div class="compact-block"><tt>2 </tt>$FANGLE -Rpca-test.awk $TEX_SRC | awk -f - || exit 1
9829 <tt>3 </tt>=&lt;\chunkref{test:cromulence}&gt;
9830 <tt>4 </tt>=&lt;\chunkref{test:escapes}&gt;</div></pre>
9832 <tt class="verbatim"><div class="compact-block">
9833 <tt>5 </tt>=&lt;\chunkref{test:chunk-params}&gt;
9834 </div></tt>
9835 </p>
9836 </p><table style="width: 100%" id="code-end-test:run-tests-1">
9837 <tbody><tr>
9838 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9839 </tr></tbody>
9840 </table></font>
9841 </p>
9843 With a lyx-build-helper
9844 </p>
9846 <font size="-1"><p>
9847 <a id="code-label-lyx-build-helper-2"></a>
9848 <table style="width: 100%" id="code-ref-lyx-build-helper-2">
9849 <tbody><tr>
9850 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0px solid; padding-top: 0pt; padding-bottom: 1px"><a id="lyx-build-helper"></a><font size="-1"><div class="left-tab">
9851 <font color="blue">lyx-build-helper</font>[2]() &uArr;<a href="#code-ref-lyx-build-helper-1">19b</a>,
9852 lang=<font color="blue">sh</font> + &equiv;
9853 </div><div class="right-tab">
9854 &#x22B2;<a href="#code-ref-lyx-build-helper-1">19b</a>
9855 </div></font></td>
9856 </tr></tbody>
9857 </table>
9858 </p><p>
9860 <tt class="verbatim"><div class="compact-block">
9861 <tt>5 </tt>PROJECT_DIR=&quot;$LYX_r&quot;
9862 </div></tt>
9863 </p>
9864 <pre class="verbatim" xml:space="preserve">
9865 <div class="compact-block"><tt>6 </tt>LYX_SRC=&quot;$PROJECT_DIR/${LYX_i%.tex}.lyx&quot;
9866 <tt>7 </tt>TEX_DIR=&quot;$LYX_p&quot;</div></pre>
9868 <tt class="verbatim"><div class="compact-block">
9869 <tt>8 </tt>TEX_SRC=&quot;$TEX_DIR/$LYX_i&quot;
9870 </div></tt>
9871 </p>
9872 </p><table style="width: 100%" id="code-end-lyx-build-helper-2">
9873 <tbody><tr>
9874 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9875 </tr></tbody>
9876 </table></font>
9877 </p>
9878 <h2 id="auto-98">16.2<span style="margin-left: 1em"></span>Extracting documentation</h2>
9880 <font size="-1"><p>
9881 <a id="code-label-./gen-www-1"></a>
9882 <table style="width: 100%" id="code-ref-./gen-www-1">
9883 <tbody><tr>
9884 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="./gen-www"></a><font size="-1"><font color="blue">./gen-www</font>[1](),
9885 lang=<font color="blue"></font> &equiv;</font></td>
9886 </tr></tbody>
9887 </table>
9888 </p><p>
9890 <tt class="verbatim"><div class="compact-block">
9891 <tt>1 </tt>#python -m elyxer &ndash;css lyx.css $LYX_SRC | \
9892 </div></tt>
9893 </p>
9894 <pre class="verbatim" xml:space="preserve">
9895 <div class="compact-block"><tt>2 </tt># iconv -c -f utf-8 -t ISO-8859-1//TRANSLIT | \
9896 <tt>3 </tt># sed 's/UTF-8&quot;\(.\)&gt;/ISO-8859-1&quot;\1&gt;/' &gt; www/docs/fangle.html
9897 <tt>4 </tt>
9898 <tt>5 </tt>python -m elyxer --css lyx.css --iso885915 --html --destdirectory www/docs/fangle.e \
9899 <tt>6 </tt> fangle.lyx &gt; www/docs/fangle.e/fangle.html
9900 <tt>7 </tt>
9901 <tt>8 </tt>( mkdir -p www/docs/fangle &amp;&amp; cd www/docs/fangle &amp;&amp; \
9902 <tt>9 </tt> lyx -e latex ../../../fangle.lyx &amp;&amp; \
9903 <tt>10 </tt> htlatex ../../../fangle.tex &quot;xhtml,fn-in&quot; &amp;&amp; \
9904 <tt>11 </tt> sed -i -e 's/&lt;!--l\. [0-9][0-9]* *--&gt;//g' fangle.html
9905 <tt>12 </tt>)
9906 <tt>13 </tt>
9907 <tt>14 </tt>( mkdir -p www/docs/literate &amp;&amp; cd www/docs/literate &amp;&amp; \
9908 <tt>15 </tt> lyx -e latex ../../../literate.lyx &amp;&amp; \
9909 <tt>16 </tt> htlatex ../../../literate.tex &quot;xhtml,fn-in&quot; &amp;&amp; \
9910 <tt>17 </tt> sed -i -e 's/&lt;!--l\. [0-9][0-9]* *--&gt;$//g' literate.html</div></pre>
9912 <tt class="verbatim"><div class="compact-block">
9913 <tt>18 </tt>)
9914 </div></tt>
9915 </p>
9916 </p><table style="width: 100%" id="code-end-./gen-www-1">
9917 <tbody><tr>
9918 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9919 </tr></tbody>
9920 </table></font>
9921 </p>
9922 <h2 id="auto-99">16.3<span style="margin-left: 1em"></span>Extracting from the command line</h2>
9924 First you will need the tex output, then you can extract:
9925 </p>
9927 <font size="-1"><p>
9928 <a id="code-label-lyx-build-manual-1"></a>
9929 <table style="width: 100%" id="code-ref-lyx-build-manual-1">
9930 <tbody><tr>
9931 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="lyx-build-manual"></a><font size="-1"><font color="blue">lyx-build-manual</font>[1](),
9932 lang=<font color="blue">sh</font> &equiv;</font></td>
9933 </tr></tbody>
9934 </table>
9935 </p><p>
9937 <tt class="verbatim"><div class="compact-block">
9938 <tt>1 </tt>lyx -e latex fangle.lyx
9939 </div></tt>
9940 </p>
9941 <pre class="verbatim" xml:space="preserve">
9942 <div class="compact-block"><tt>2 </tt>fangle -R./fangle fangle.tex &gt; ./fangle</div></pre>
9944 <tt class="verbatim"><div class="compact-block">
9945 <tt>3 </tt>fangle -R./fangle.module fangle.tex &gt;
9946 ./fangle.module
9947 </div></tt>
9948 </p>
9949 </p><table style="width: 100%" id="code-end-lyx-build-manual-1">
9950 <tbody><tr>
9951 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9952 </tr></tbody>
9953 </table></font>
9954 </p>
9955 <h2 id="auto-100">16.4<span style="margin-left: 1em"></span>Testing</h2>
9957 <font size="-1"><p>
9958 <a id="code-label-test:helpers-1"></a>
9959 <table style="width: 100%" id="code-ref-test:helpers-1">
9960 <tbody><tr>
9961 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:helpers"></a><font size="-1"><font color="blue">test:helpers</font>[1](),
9962 lang=<font color="blue"></font> &equiv;</font></td>
9963 </tr></tbody>
9964 </table>
9965 </p><p>
9967 <tt class="verbatim"><div class="compact-block">
9968 <tt>1 </tt>passtest() {
9969 </div></tt>
9970 </p>
9971 <pre class="verbatim" xml:space="preserve">
9972 <div class="compact-block"><tt>2 </tt> if &quot;$@&quot;
9973 <tt>3 </tt> then echo &quot;Passed&quot;
9974 <tt>4 </tt> else echo &quot;Failed&quot;
9975 <tt>5 </tt> return 1
9976 <tt>6 </tt> fi
9977 <tt>7 </tt>}
9978 <tt>8 </tt>
9979 <tt>9 </tt>failtest() {
9980 <tt>10 </tt> if ! &quot;$@&quot;
9981 <tt>11 </tt> then echo &quot;Passed&quot;
9982 <tt>12 </tt> else echo &quot;Failed&quot;
9983 <tt>13 </tt> return 1
9984 <tt>14 </tt> fi</div></pre>
9986 <tt class="verbatim"><div class="compact-block">
9987 <tt>15 </tt>}
9988 </div></tt>
9989 </p>
9990 </p><table style="width: 100%" id="code-end-test:helpers-1">
9991 <tbody><tr>
9992 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
9993 </tr></tbody>
9994 </table></font>
9995 </p>
9996 <p style="margin-top: 25%; margin-bottom: 10%">
9997 <a id="auto-101"></a><br></br><b><font size="+5"><div class="center-tab">
9998 Part III
9999 </div><div class="right-tab">
10000 <br></br>Tests
10001 </div></font></b>
10002 </p>
10003 <h1 id="auto-102">Chapter 17<br></br>Chunk Parameters</h1>
10005 <font size="-1"><p>
10006 <a id="code-label-test:chunk-params:sub-1"></a>
10007 <table style="width: 100%" id="code-ref-test:chunk-params:sub-1">
10008 <tbody><tr>
10009 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:chunk-params:sub"></a><font size="-1"><font color="blue">test:chunk-params:sub</font>[1](<font
10010 color="blue">THING</font>, <font color="blue">colour</font>), lang=<font
10011 color="blue"></font> &equiv;</font></td>
10012 </tr></tbody>
10013 </table>
10014 </p><p>
10016 <tt class="verbatim"><div class="compact-block">
10017 <tt>1 </tt>I see a ${THING},
10018 </div></tt>
10019 </p>
10020 <pre class="verbatim" xml:space="preserve">
10021 <div class="compact-block"><tt>2 </tt>a ${THING} of colour ${colour}, </div></pre>
10023 <tt class="verbatim"><div class="compact-block">
10024 <tt>3 </tt>and looking closer
10025 =&lt;\chunkref{test:chunk-params:sub:sub}(${colour})&gt;
10026 </div></tt>
10027 </p>
10028 </p><table style="width: 100%" id="code-end-test:chunk-params:sub-1">
10029 <tbody><tr>
10030 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
10031 </tr></tbody>
10032 </table></font>
10033 </p>
10035 <font size="-1"><p>
10036 <a id="code-label-test:chunk-params:sub:sub-1"></a>
10037 <table style="width: 100%" id="code-ref-test:chunk-params:sub:sub-1">
10038 <tbody><tr>
10039 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:chunk-params:sub:sub"></a><font size="-1"><font color="blue">test:chunk-params:sub:sub</font>[1](<font
10040 color="blue">colour</font>), lang=<font color="blue"></font>
10041 &equiv;</font></td>
10042 </tr></tbody>
10043 </table>
10044 </p><p>
10045 <tt class="verbatim"><div class="compact-block">
10046 <tt>1 </tt>a funny shade of ${colour}
10047 </div></tt>
10048 </p><table style="width: 100%" id="code-end-test:chunk-params:sub:sub-1">
10049 <tbody><tr>
10050 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
10051 </tr></tbody>
10052 </table></font>
10053 </p>
10055 <font size="-1"><p>
10056 <a id="code-label-test:chunk-params:text-1"></a>
10057 <table style="width: 100%" id="code-ref-test:chunk-params:text-1">
10058 <tbody><tr>
10059 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:chunk-params:text"></a><font size="-1"><font color="blue">test:chunk-params:text</font>[1](),
10060 lang=<font color="blue"></font> &equiv;</font></td>
10061 </tr></tbody>
10062 </table>
10063 </p><p>
10065 <tt class="verbatim"><div class="compact-block">
10066 <tt>1 </tt>What do you see?
10067 &quot;=&lt;\chunkref{test:chunk-params:sub}(joe, red)&gt;&quot;
10068 </div></tt>
10069 </p>
10070 <pre class="verbatim" xml:space="preserve">
10071 <div class="compact-block"></div></pre>
10073 <tt class="verbatim"><div class="compact-block">
10074 <tt>2 </tt>Well, fancy!
10075 </div></tt>
10076 </p>
10077 </p><table style="width: 100%" id="code-end-test:chunk-params:text-1">
10078 <tbody><tr>
10079 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
10080 </tr></tbody>
10081 </table></font>
10082 </p>
10084 Should generate output:
10085 </p>
10087 <font size="-1"><p>
10088 <a id="code-label-test:chunk-params:result-1"></a>
10089 <table style="width: 100%" id="code-ref-test:chunk-params:result-1">
10090 <tbody><tr>
10091 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:chunk-params:result"></a><font size="-1"><font color="blue">test:chunk-params:result</font>[1](),
10092 lang=<font color="blue"></font> &equiv;</font></td>
10093 </tr></tbody>
10094 </table>
10095 </p><p>
10097 <tt class="verbatim"><div class="compact-block">
10098 <tt>1 </tt>What do you see? &quot;I see a joe,
10099 </div></tt>
10100 </p>
10101 <pre class="verbatim" xml:space="preserve">
10102 <div class="compact-block"><tt>2 </tt> a joe of colour red,
10103 <tt>3 </tt> and looking closer a funny shade of red&quot;</div></pre>
10105 <tt class="verbatim"><div class="compact-block">
10106 <tt>4 </tt>Well, fancy!
10107 </div></tt>
10108 </p>
10109 </p><table style="width: 100%" id="code-end-test:chunk-params:result-1">
10110 <tbody><tr>
10111 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
10112 </tr></tbody>
10113 </table></font>
10114 </p>
10116 And this chunk will perform the test:
10117 </p>
10119 <font size="-1"><p>
10120 <a id="code-label-test:chunk-params-1"></a>
10121 <table style="width: 100%" id="code-ref-test:chunk-params-1">
10122 <tbody><tr>
10123 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="test:chunk-params"></a><font size="-1"><font color="blue">test:chunk-params</font>[1](),
10124 lang=<font color="blue"></font> &equiv;</font></td>
10125 </tr></tbody>
10126 </table>
10127 </p><p>
10129 <tt class="verbatim"><div class="compact-block">
10130 <tt>1 </tt>$FANGLE -Rtest:chunk-params:result $TEX_SRC &gt;
10131 $TMP/answer || exit 1
10132 </div></tt>
10133 </p>
10134 <pre class="verbatim" xml:space="preserve">
10135 <div class="compact-block"><tt>2 </tt>$FANGLE -Rtest:chunk-params:text $TEX_SRC &gt; $TMP/result || exit 1</div></pre>
10137 <tt class="verbatim"><div class="compact-block">
10138 <tt>3 </tt>passtest diff $TMP/answer $TMP/result || (echo
10139 test:chunk-params:text failed ; exit 1)
10140 </div></tt>
10141 </p>
10142 </p><table style="width: 100%" id="code-end-test:chunk-params-1">
10143 <tbody><tr>
10144 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
10145 </tr></tbody>
10146 </table></font>
10147 </p>
10148 <h1 id="auto-103"><a id="Compile-log-lyx"></a>Chapter 18<br></br>Compile-log-lyx</h1>
10150 <font size="-1"><p>
10151 <a id="code-label-Chunk:./compile-log-lyx-1"></a>
10152 <table style="width: 100%" id="code-ref-Chunk:./compile-log-lyx-1">
10153 <tbody><tr>
10154 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0pt solid; border-bottom: 0.5px solid; padding-top: 0pt; padding-bottom: 1px"><a id="Chunk:./compile-log-lyx"></a><font size="-1"><font color="blue">Chunk:./compile-log-lyx</font>[1](),
10155 lang=<font color="blue">sh</font> &equiv;</font></td>
10156 </tr></tbody>
10157 </table>
10158 </p><p>
10160 <tt class="verbatim"><div class="compact-block">
10161 <tt>1 </tt>#! /bin/sh
10162 </div></tt>
10163 </p>
10164 <pre class="verbatim" xml:space="preserve">
10165 <div class="compact-block"><tt>2 </tt># can't use gtkdialog -i, cos it uses the &quot;source&quot; command which ubuntu sh doesn't have
10166 <tt>3 </tt>
10167 <tt>4 </tt>main() {
10168 <tt>5 </tt> errors=&quot;/tmp/compile.log.$$&quot;
10169 <tt>6 </tt># if grep '^[^ ]*:\( In \|[0-9][0-9]*: [^ ]*:\)' &gt; $errors
10170 <tt>7 </tt>if grep '^[^ ]*(\([0-9][0-9]*\)) *: *\(error\|warning\)' &gt; $errors
10171 <tt>8 </tt> then
10172 <tt>9 </tt> sed -i -e 's/^[^ ]*[/\\]\([^/\\]*\)(\([ 0-9][ 0-9]*\)) *: */\1:\2|\2|/' $errors
10173 <tt>10 </tt> COMPILE_DIALOG='
10174 <tt>11 </tt> &lt;vbox&gt;
10175 <tt>12 </tt> &lt;text&gt;
10176 <tt>13 </tt> &lt;label&gt;Compiler errors:&lt;/label&gt;
10177 <tt>14 </tt> &lt;/text&gt;
10178 <tt>15 </tt> &lt;tree exported_column=&quot;0&quot;&gt;
10179 <tt>16 </tt> &lt;variable&gt;LINE&lt;/variable&gt;
10180 <tt>17 </tt> &lt;height&gt;400&lt;/height&gt;&lt;width&gt;800&lt;/width&gt;
10181 <tt>18 </tt> &lt;label&gt;File | Line | Message&lt;/label&gt;
10182 <tt>19 </tt> &lt;action&gt;'&quot;. $SELF ; &quot;'lyxgoto $LINE&lt;/action&gt;
10183 <tt>20 </tt> &lt;input&gt;'&quot;cat $errors&quot;'&lt;/input&gt;
10184 <tt>21 </tt> &lt;/tree&gt;
10185 <tt>22 </tt> &lt;hbox&gt;
10186 <tt>23 </tt> &lt;button&gt;&lt;label&gt;Build&lt;/label&gt;
10187 <tt>24 </tt> &lt;action&gt;lyxclient -c &quot;LYXCMD:build-program&quot; &amp;&lt;/action&gt;
10188 <tt>25 </tt> &lt;/button&gt;
10189 <tt>26 </tt> &lt;button ok&gt;&lt;/button&gt;
10190 <tt>27 </tt> &lt;/hbox&gt;
10191 <tt>28 </tt> &lt;/vbox&gt;
10192 <tt>29 </tt>'
10193 <tt>30 </tt> export COMPILE_DIALOG
10194 <tt>31 </tt> ( gtkdialog --program=COMPILE_DIALOG ; rm $errors ) &amp;
10195 <tt>32 </tt> else
10196 <tt>33 </tt> rm $errors
10197 <tt>34 </tt> fi
10198 <tt>35 </tt>}
10199 <tt>36 </tt>
10200 <tt>37 </tt>lyxgoto() {
10201 <tt>38 </tt> file=&quot;${LINE%:*}&quot;
10202 <tt>39 </tt> line=&quot;${LINE##*:}&quot;
10203 <tt>40 </tt> extraline=&lsquo;cat $file | head -n $line | tac | sed '/^\\\\begin{lstlisting}/q' | wc -l&lsquo;
10204 <tt>41 </tt> extraline=&lsquo;expr $extraline - 1&lsquo;
10205 <tt>42 </tt> lyxclient -c &quot;LYXCMD:command-sequence server-goto-file-row $file $line ; char-forward ; repeat $extraline paragraph-down ; paragraph-up-select&quot;
10206 <tt>43 </tt>}
10207 <tt>44 </tt>
10208 <tt>45 </tt>SELF=&quot;$0&quot;
10209 <tt>46 </tt>if test -z &quot;$COMPILE_DIALOG&quot;
10210 <tt>47 </tt>then main &quot;$@&quot; </div></pre>
10212 <tt class="verbatim"><div class="compact-block">
10213 <tt>48 </tt>fi
10214 </div></tt>
10215 </p>
10216 </p><table style="width: 100%" id="code-end-Chunk:./compile-log-lyx-1">
10217 <tbody><tr>
10218 <td style="width: 100%; padding-left: 0pt; padding-right: 0pt; border-top: 0.5px solid; border-bottom: 0px solid; padding-top: 1px; padding-bottom: 0px"></td>
10219 </tr></tbody>
10220 </table></font>
10221 </p>
10224 </p>
10225 </body>
10226 </html>