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">
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:
30 <table class=
"title-block">
32 <td><font size=
"+2"><p>
33 <table class=
"title-block">
35 <td><b><font size=
"+5">fangle
</font></b></td>
38 <p style=
"margin-top: 10%; margin-bottom: 10%">
39 <div class=
"compact-block">
40 <table class=
"title-block">
43 <span style=
"margin-left: 0pt"></span>
44 <table style=
"display: inline; vertical-align: -0.55em">
46 <td style=
"text-align: center; padding-left: 0em; padding-right: 0em; padding-bottom: 0em; padding-top: 0em; width: 100%"><center>
49 <span style=
"margin-left: 0pt"></span>
50 <table style=
"display: inline; vertical-align: -0.55em">
52 <td style=
"text-align: center; padding-left: 0em; padding-right: 0em; padding-bottom: 0em; padding-top: 0em; width: 100%"><center>
54 <class style=
"font-variant: small-caps"><font size=
"+2"> Sam
55 Liddicott
</font></class>
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">
65 <td style=
"text-align: center; padding-left: 0em; padding-right: 0em; padding-bottom: 0em; padding-top: 0em; width: 100%"><center>
83 <span style=
"margin-left: 0pt"></span>
84 <table style=
"display: inline; vertical-align: -0.55em">
86 <td style=
"text-align: center; padding-left: 0em; padding-right: 0em; padding-bottom: 0em; padding-top: 0em; width: 100%"><center>
103 <div class=
"right-tab">
104 <span style=
"margin-left: 0em"></span>
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>.
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">
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.
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>).
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"><<</tt>,
<tt class=
"verbatim">>></tt>
142 and
<tt class=
"verbatim">@
</tt>, and in doing so brought the wonders of literate
143 programming within my reach.
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.
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.
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
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.
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?
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.
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">—</class> useful though they
203 are
<class style=
"font-family: Times New Roman">—</class> are hard to comprehend in the literate
206 <h2 id=
"auto-2"><a id=
"License"></a>License
</h2>
208 Fangle is licensed under the GPL
3 (or later).
211 This doesn't mean that sources generated by fangle must be licensed
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.
220 As fangle is currently written in awk, an interpreted language, this
221 should not be too hard.
225 <a id=
"code-label-gpl3-copyright-1"></a>
226 <table style=
"width: 100%" id=
"code-ref-gpl3-copyright-1">
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> ≡</font></td>
234 <tt class=
"verbatim"><div class=
"compact-block">
235 <tt>1 </tt># fangle - fully featured notangle replacement in awk
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
<sam@liddicott.com
>
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.
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.
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 <http://www.gnu.org/licenses/
>.
259 </p><table style=
"width: 100%" id=
"code-end-gpl3-copyright-1">
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>
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>
273 License
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-2">4</a>
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>
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>
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>
288 2.1<span style=
"margin-left: 1em"></span>Listing roots
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-6">13</a>
291 2.2<span style=
"margin-left: 1em"></span>Extracting roots
<span style=
"margin-left: 5mm"></span> <a
292 href=
"#auto-7">13</a>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
369 6.2<span style=
"margin-left: 1em"></span>Extracting Sources
<span style=
"margin-left: 5mm"></span> <a
370 href=
"#auto-24">23</a>
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>
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>
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>
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>
396 6.5<span style=
"margin-left: 1em"></span>Extracting Documentation
<span style=
"margin-left: 5mm"></span>
397 <a href=
"#auto-29">27</a>
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>
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>
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>
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>
425 6.6<span style=
"margin-left: 1em"></span>Other helpers
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-34">29</a>
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>
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>
435 <div style=
"margin-left: 96px">
437 Example
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-37">30</a>
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>
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>
449 7.1<span style=
"margin-left: 1em"></span>AWK tricks
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-40">33</a>
452 7.2<span style=
"margin-left: 1em"></span>Catching errors
<span style=
"margin-left: 5mm"></span> <a
453 href=
"#auto-41">34</a>
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>
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>
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>
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>
472 <p style=
"margin-top: 1em; margin-bottom: 0.5em">
473 <b>9<span style=
"margin-left: 1em"></span>Language Modes
& Quoting
</b> <span
474 style=
"margin-left: 5mm"></span> <a href=
"#auto-46">41</a>
477 9.1<span style=
"margin-left: 1em"></span>Modes
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-47">41</a>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
538 9.3<span style=
"margin-left: 1em"></span>Some tests
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-59">47</a>
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>
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>
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>
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>
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>
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>
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>
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>
581 10.1<span style=
"margin-left: 1em"></span>Chunk start
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-68">55</a>
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>
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>
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>
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>
609 10.2<span style=
"margin-left: 1em"></span>Chunk end
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-73">57</a>
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>
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>
623 10.3<span style=
"margin-left: 1em"></span>Chunk contents
<span style=
"margin-left: 5mm"></span> <a
624 href=
"#auto-76">58</a>
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>
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>
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>
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>
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>
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>
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>
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>
662 15.1<span style=
"margin-left: 1em"></span>fangle module
<span style=
"margin-left: 5mm"></span> <a
663 href=
"#auto-85">75</a>
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>
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>
678 15.2<span style=
"margin-left: 1em"></span>Latex Macros
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-88">76</a>
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>
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>
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>
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>
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>
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>
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>
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>
724 16.2<span style=
"margin-left: 1em"></span>Extracting documentation
<span style=
"margin-left: 5mm"></span>
725 <a href=
"#auto-98">83</a>
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>
732 16.4<span style=
"margin-left: 1em"></span>Testing
<span style=
"margin-left: 5mm"></span> <a href=
"#auto-100">84</a>
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>
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>
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>
750 <b><font size=
"+5"><div class=
"center-tab">
752 </div><div class=
"right-tab">
753 <br></br>Using Fangle
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
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>.
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.
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.
775 <code>fangle -r filename.tex
</code>
778 will print out the fangle roots of a tex file.
781 Unlike the
<tt class=
"verbatim">noroots
</tt> command, the printed roots are not
782 enclosed in angle brackets e.g.
<tt class=
"verbatim"><<name
>></tt>,
783 unless at least one of the roots is defined using the
<tt class=
"verbatim">notangle
</tt>
784 notation
<tt class=
"verbatim"><<name
>>=
</tt>.
787 Also, unlike noroots, it prints out all roots
–- 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">—</class> and
790 so-called top level roots could also be included in another root as
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.
798 Makefile.inc, discussed in
<a href=
"#makefile.inc">6</a>, can automatically extract all
799 such sources prefixed with
<tt class=
"verbatim">./
</tt>
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
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:
810 <pre class=
"verbatim" xml:
space=
"preserve">
811 fangle -R./Makefile.inc fangle.tex
> ./Makefile.inc
</pre>
813 If you are using TeXmacs, the standard way to extract a file would
816 <pre class=
"verbatim" xml:
space=
"preserve">
817 fangle -R./Makefile.inc fangle.txt
> ./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>
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..
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
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.
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
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.
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.
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.
861 The formatting is managed by a set of macros shown in
<a href=
"#latex-source">15</a>,
862 and can be included with:
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.
871 The
<tt class=
"verbatim">listings.sty
</tt> package is required, and is used for
872 formatting the code chunks and syntax highlighting.
875 The
<tt class=
"verbatim">xargs.sty
</tt> package is also required, and makes
876 writing LaTeX macro so much more pleasant.
878 <table style=
"display: inline; vertical-align: -0.55em">
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>
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.
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>
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-
>Reconfigure, and then re-start L
<span style=
"margin-left: -0.1667em"></span>Y
<span
903 style=
"margin-left: -0.125em"></span>X.
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
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:
919 <pre class=
"verbatim" xml:
space=
"preserve">
921 \renewcommand{\ttdefault}{txtt}
</pre>
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
930 <pre class=
"verbatim" xml:
space=
"preserve">
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.
937 <h3 id=
"auto-15">4.2.3<span style=
"margin-left: 1em"></span>Luximono
</h3>
939 The lstlistings documention suggests using Luximono.
942 Luximono was installed according to the instructions in Ubuntu Forums
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
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
971 <span style=
"margin-left: 0em"></span>
972 <a id=
"footnr-2"></a>
973 <sup><a href=
"#footnote-2">2</a></sup>
975 <tt class=
"verbatim">sudo updmap
–enable MixedMap ul9.map
</tt>
976 is required. It looks fine in PDF and PS view but still looks rotten in
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.
986 Add the new module
<em>Fangle Literate Listings
</em> and also
987 <em>Logical Markup
</em> which is very useful.
990 In the drop-down style listbox you should notice a new style defined,
991 called
<em>Chunk
</em>.
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"><<name
>>=
</tt> type tags. In the line
997 (or paragraph) following the chunk name, you insert a listing with:
998 Insert-
>Program Listing.
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
–- just
1005 press enter as normal.
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
1019 <table style=
"display: inline; vertical-align: -0.55em">
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>
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 –- 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
1042 <span style=
"margin-left: 0em"></span>
1043 <a id=
"footnr-3"></a>
1044 <sup><a href=
"#footnote-3">3</a></sup>
1047 <table style=
"display: inline; vertical-align: -0.55em">
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
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.
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).
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).
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.
1073 In my LaTeX pre-amble I usually specialise my code format with:
1077 <a id=
"code-label-document-preamble-1"></a>
1078 <table style=
"width: 100%" id=
"code-ref-document-preamble-1">
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> ≡</font></td>
1086 <tt class=
"verbatim"><div class=
"compact-block">
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">
1103 </p><table style=
"width: 100%" id=
"code-end-document-preamble-1">
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>
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-
>Build Program.
1118 First, make sure you don't have a conversion defined for Lyx-
>Program
1121 From the menu Tools-
>Preferences, add a conversion from
1122 Latex(Plain)-
>Program as:
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).
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.
1136 You may also want to consider adding options to this conversion...
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
1154 <span style=
"margin-left: 0em"></span>
1155 <a id=
"footnr-4"></a>
1156 <sup><a href=
"#footnote-4">4</a></sup>
1160 Now, a shell script chunk called
<tt class=
"verbatim">lyx-build
</tt> will be
1161 extracted and run whenever you choose the Document-
>Build Program
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.
1169 <pre class=
"verbatim" xml:
space=
"preserve">
1170 lyx -e latex fangle.lyx
&& \
1171 fangle fangle.lyx
> ./autoboot
</pre>
1173 This looks simple enough, but as mentioned, fangle has to be had from
1174 somewhere before it can be extracted.
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
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.
1190 <a id=
"code-label-lyx-build-helper-1"></a>
1191 <table style=
"width: 100%" id=
"code-ref-lyx-build-helper-1">
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>
1196 </div><div class=
"right-tab">
1197 <a href=
"#code-ref-lyx-build-helper-2">83c
</a>⊳
1203 <tt class=
"verbatim"><div class=
"compact-block">
1204 <tt>1 </tt>PROJECT_DIR=
"$LYX_r
"
1207 <pre class=
"verbatim" xml:
space=
"preserve">
1208 <div class=
"compact-block"><tt>2 </tt>LYX_SRC=
"$PROJECT_DIR/${LYX_i%.tex}.lyx
"
1209 <tt>3 </tt>TEX_DIR=
"$LYX_p
"</div></pre>
1211 <tt class=
"verbatim"><div class=
"compact-block">
1212 <tt>4 </tt>TEX_SRC=
"$TEX_DIR/$LYX_i
"
1216 <a id=
"code-end-lyx-build-helper-1"></a>
1220 And then we can define a lyx-build fragment similar to the autoboot
1225 <a id=
"code-label-lyx-build-1"></a>
1226 <table style=
"width: 100%" id=
"code-ref-lyx-build-1">
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>
1231 </div><div class=
"right-tab">
1232 <a href=
"#code-ref-lyx-build-2">83a
</a>⊳
1238 <tt class=
"verbatim"><div class=
"compact-block">
1239 <tt>1 </tt>#! /bin/sh
1242 <pre class=
"verbatim" xml:
space=
"preserve">
1243 <div class=
"compact-block"><tt>2 </tt>=
<\chunkref{lyx-build-helper}
>
1244 <tt>3 </tt>cd $PROJECT_DIR || exit
1
1246 <tt>5 </tt>#/usr/bin/fangle -filter ./notanglefix-filter \
1247 <tt>6 </tt># -R./Makefile.inc
"../../noweb-lyx/noweb-lyx3.lyx
" \
1248 <tt>7 </tt># | sed '/NOWEB_SOURCE=/s/=.*/=samba4-dfs.lyx/' \
1249 <tt>8 </tt>#
> ./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
1257 <a id=
"code-end-lyx-build-1"></a>
1263 <h1 id=
"auto-21">Chapter
5<br></br>Using Fangle with TeXmacs
</h1>
1264 <table style=
"display: inline; vertical-align: -0.55em">
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>
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.
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
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.
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.
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.
1294 <pre class=
"verbatim" xml:
space=
"preserve">
1295 target: pre-requisite
1297 ↦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.
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.
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
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.
1323 <a id=
"code-label-./Makefile.inc-1"></a>
1324 <table style=
"width: 100%" id=
"code-ref-./Makefile.inc-1">
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> ≡</font></td>
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>
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>
1343 </p><table style=
"width: 100%" id=
"code-end-./Makefile.inc-1">
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>
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
1356 <a id=
"code-label-Makefile.inc-vars-1"></a>
1357 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-1">
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>
1362 </div><div class=
"right-tab">
1363 <a href=
"#code-ref-Makefile.inc-vars-2">24a
</a>⊳
1368 <tt class=
"verbatim"><div class=
"compact-block">
1369 <tt>1 </tt>LITERATE_SOURCE=
1372 <a id=
"code-end-Makefile.inc-vars-1"></a>
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
1394 <span style=
"margin-left: 0em"></span>
1395 <a id=
"footnr-1"></a>
1396 <sup><a href=
"#footnote-1">1</a></sup>
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>
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.
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>
1433 <tt class=
"verbatim">server-goto-file-line
</tt>
1434 when moving the cursor to error lines during compile failures.
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.
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.
1449 <a id=
"code-label-Makefile.inc-vars-2"></a>
1450 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-2">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
1454 lang=
<font color=
"blue"></font> +
≡
1455 </div><div class=
"right-tab">
1456 ⊲<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a> <a href=
"#code-ref-Makefile.inc-vars-3">24c
</a>▿
1462 <tt class=
"verbatim"><div class=
"compact-block">
1463 <tt>2 </tt>TEX_SOURCE=$(LYX_SOURCE:.lyx=.tex)
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)
1474 <a id=
"code-end-Makefile.inc-vars-2"></a>
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.
1483 <a id=
"code-label-Makefile.inc-targets-1"></a>
1484 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-1">
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>
1489 </div><div class=
"right-tab">
1490 <a href=
"#code-ref-Makefile.inc-targets-2">24d
</a>▿
1496 <tt class=
"verbatim"><div class=
"compact-block">
1497 <tt>1 </tt>$(TEX_SOURCE): $(LYX_SOURCE)
1500 <pre class=
"verbatim" xml:
space=
"preserve">
1501 <div class=
"compact-block"><tt>2 </tt>↦lyx -e latex $
<
1502 <tt>3 </tt>clean_tex:
1503 <tt>4 </tt>↦rm -f -- $(TEX_SOURCE)
</div></pre>
1505 <tt class=
"verbatim"><div class=
"compact-block">
1506 <tt>5 </tt>clean: clean_tex
1510 <a id=
"code-end-Makefile.inc-targets-1"></a>
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
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.
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>
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.
1546 <a id=
"code-label-Makefile.inc-vars-3"></a>
1547 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-3">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
1551 lang=
<font color=
"blue"></font> +
≡
1552 </div><div class=
"right-tab">
1553 ▵<a href=
"#code-ref-Makefile.inc-vars-2">24a
</a> <a href=
"#code-ref-Makefile.inc-vars-4">25a
</a>⊳
1559 <tt class=
"verbatim"><div class=
"compact-block">
1560 <tt>4 </tt>TXT_SOURCE=$(LITERATE_SOURCE:.tm=.txt)
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)
1571 <a id=
"code-end-Makefile.inc-vars-3"></a>
1574 <table style=
"display: inline; vertical-align: -0.55em">
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 $
< so multiple
1577 targets can be specified
</td>
1582 <a id=
"code-label-Makefile.inc-targets-2"></a>
1583 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-2">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
1587 lang=
<font color=
"blue"></font> +
≡
1588 </div><div class=
"right-tab">
1589 ▵<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a> <a href=
"#code-ref-Makefile.inc-targets-3">25c
</a>⊳
1595 <tt class=
"verbatim"><div class=
"compact-block">
1596 <tt>6 </tt>$(TXT_SOURCE): $(LITERATE_SOURCE)
1599 <pre class=
"verbatim" xml:
space=
"preserve">
1600 <div class=
"compact-block"><tt>7 </tt>↦texmacs -c $
< $(TXT_SOURCE) -q
1601 <tt>8 </tt>clean_txt:
1602 <tt>9 </tt>↦rm -f -- $(TXT_SOURCE)
</div></pre>
1604 <tt class=
"verbatim"><div class=
"compact-block">
1605 <tt>10 </tt>clean: clean_txt
1609 <a id=
"code-end-Makefile.inc-targets-2"></a>
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
1627 <span style=
"margin-left: 0em"></span>
1628 <a id=
"footnr-4"></a>
1629 <sup><a href=
"#footnote-4">4</a></sup>
1634 <a id=
"code-label-Makefile.inc-vars-4"></a>
1635 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-4">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
1639 lang=
<font color=
"blue"></font> +
≡
1640 </div><div class=
"right-tab">
1641 ⊲<a href=
"#code-ref-Makefile.inc-vars-3">24c
</a> <a href=
"#code-ref-Makefile.inc-vars-5">25b
</a>▿
1646 <tt class=
"verbatim"><div class=
"compact-block">
1647 <tt>6 </tt>FANGLE_SOURCE=$(TEX_SOURCE) $(TXT_SOURCE)
1650 <a id=
"code-end-Makefile.inc-vars-4"></a>
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.
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.
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.
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.
1682 <a id=
"code-label-Makefile.inc-vars-5"></a>
1683 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-5">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
1687 lang=
<font color=
"blue"></font> +
≡
1688 </div><div class=
"right-tab">
1689 ▵<a href=
"#code-ref-Makefile.inc-vars-4">25a
</a> <a href=
"#code-ref-Makefile.inc-vars-6">26a
</a>⊳
1694 <tt class=
"verbatim"><div class=
"compact-block">
1695 <tt>7 </tt>FANGLE_SOURCE_STAMP=$(FANGLE_SOURCE).stamp
1698 <a id=
"code-end-Makefile.inc-vars-5"></a>
1703 <a id=
"code-label-Makefile.inc-targets-3"></a>
1704 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-3">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
1708 lang=
<font color=
"blue"></font> +
≡
1709 </div><div class=
"right-tab">
1710 ⊲<a href=
"#code-ref-Makefile.inc-targets-2">24d
</a> <a href=
"#code-ref-Makefile.inc-targets-4">26b
</a>⊳
1716 <tt class=
"verbatim"><div class=
"compact-block">
1717 <tt>11 </tt>$(FANGLE_SOURCE_STAMP): $(FANGLE_SOURCE) \
1720 <pre class=
"verbatim" xml:
space=
"preserve">
1721 <div class=
"compact-block"><tt>12 </tt>↦ $(FANGLE_SOURCES) ; \
1722 <tt>13 </tt>↦echo -n
> $(FANGLE_SOURCE_STAMP)
1723 <tt>14 </tt>clean_stamp:
1724 <tt>15 </tt>↦rm -f $(FANGLE_SOURCE_STAMP)
</div></pre>
1726 <tt class=
"verbatim"><div class=
"compact-block">
1727 <tt>16 </tt>clean: clean_stamp
1731 <a id=
"code-end-Makefile.inc-targets-3"></a>
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"><<</tt> and
<tt class=
"verbatim">>></tt> which may surround the
1740 roots names (for compatibility with Noweb's noroots command).
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>
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>
1755 <table style=
"display: inline; vertical-align: -0.55em">
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>
1763 <a id=
"code-label-Makefile.inc-vars-6"></a>
1764 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-6">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
1768 lang=
<font color=
"blue"></font> +
≡
1769 </div><div class=
"right-tab">
1770 ⊲<a href=
"#code-ref-Makefile.inc-vars-5">25b
</a> <a href=
"#code-ref-Makefile.inc-vars-7">26e
</a>▿
1776 <tt class=
"verbatim"><div class=
"compact-block">
1777 <tt>8 </tt>FANGLE_PREFIX:=\.\/
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/^[
<][
<]//;s/[
>][
>]$$//;/^$(FANGLE_PREFIX)/!d' \
</div></pre>
1785 <tt class=
"verbatim"><div class=
"compact-block">
1786 <tt>12 </tt> -e 's/^$(FANGLE_PREFIX)/\.\//' )
1790 <a id=
"code-end-Makefile.inc-vars-6"></a>
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
1800 <a id=
"code-label-Makefile.inc-targets-4"></a>
1801 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-4">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
1805 lang=
<font color=
"blue"></font> +
≡
1806 </div><div class=
"right-tab">
1807 ⊲<a href=
"#code-ref-Makefile.inc-targets-3">25c
</a> <a href=
"#code-ref-Makefile.inc-targets-5">26c
</a>▿
1813 <tt class=
"verbatim"><div class=
"compact-block">
1814 <tt>17 </tt>.PHONY: echo_fangle_sources
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)
1825 <a id=
"code-end-Makefile.inc-targets-4"></a>
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.
1835 <a id=
"code-label-Makefile.inc-targets-5"></a>
1836 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-5">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
1840 lang=
<font color=
"blue"></font> +
≡
1841 </div><div class=
"right-tab">
1842 ▵<a href=
"#code-ref-Makefile.inc-targets-4">26b
</a> <a href=
"#code-ref-Makefile.inc-targets-6">26d
</a>▿
1848 <tt class=
"verbatim"><div class=
"compact-block">
1849 <tt>19 </tt>.PHONY: fangle_sources
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)
1860 <a id=
"code-end-Makefile.inc-targets-5"></a>
1864 And also a convenient target to remove extracted sources.
1868 <a id=
"code-label-Makefile.inc-targets-6"></a>
1869 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-6">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
1873 lang=
<font color=
"blue"></font> +
≡
1874 </div><div class=
"right-tab">
1875 ▵<a href=
"#code-ref-Makefile.inc-targets-5">26c
</a> <a href=
"#code-ref-Makefile.inc-targets-7">27d
</a>⊳
1881 <tt class=
"verbatim"><div class=
"compact-block">
1882 <tt>21 </tt>.PHONY: clean_fangle_sources
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
– $(FANGLE_SOURCE_STAMP)
1894 <a id=
"code-end-Makefile.inc-targets-6"></a>
1898 We now look at the extraction of the source files.
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>.
1909 <a id=
"code-label-Makefile.inc-vars-7"></a>
1910 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-7">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
1914 lang=
<font color=
"blue"></font> +
≡
1915 </div><div class=
"right-tab">
1916 ▵<a href=
"#code-ref-Makefile.inc-vars-6">26a
</a> <a href=
"#code-ref-Makefile.inc-vars-8">26f
</a>▿
1921 <tt class=
"verbatim"><div class=
"compact-block">
1922 <tt>13 </tt>if_extension=$(if $(findstring $(suffix
1923 $(
1)),$(
2)),$(
3),$(
4))
1926 <a id=
"code-end-Makefile.inc-vars-7"></a>
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.
1947 <span style=
"margin-left: 0em"></span>
1948 <a id=
"footnr-5"></a>
1949 <sup><a href=
"#footnote-5">5</a></sup>
1953 To make this easier we define the file extensions for which we want to
1958 <a id=
"code-label-Makefile.inc-vars-8"></a>
1959 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-8">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
1963 lang=
<font color=
"blue"></font> +
≡
1964 </div><div class=
"right-tab">
1965 ▵<a href=
"#code-ref-Makefile.inc-vars-7">26e
</a> <a href=
"#code-ref-Makefile.inc-vars-9">26g
</a>▿
1970 <tt class=
"verbatim"><div class=
"compact-block">
1971 <tt>14 </tt>C_EXTENSIONS=.c .h
1974 <a id=
"code-end-Makefile.inc-vars-8"></a>
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.
1985 <a id=
"code-label-Makefile.inc-vars-9"></a>
1986 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-9">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
1990 lang=
<font color=
"blue"></font> +
≡
1991 </div><div class=
"right-tab">
1992 ▵<a href=
"#code-ref-Makefile.inc-vars-8">26f
</a> <a href=
"#code-ref-Makefile.inc-vars-10">27a
</a>⊳
1998 <tt class=
"verbatim"><div class=
"compact-block">
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
"$(
2)
"
2012 <a id=
"code-end-Makefile.inc-vars-9"></a>
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>
2023 <a id=
"code-label-Makefile.inc-vars-10"></a>
2024 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-10">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
2028 lang=
<font color=
"blue"></font> +
≡
2029 </div><div class=
"right-tab">
2030 ⊲<a href=
"#code-ref-Makefile.inc-vars-9">26g
</a> <a href=
"#code-ref-Makefile.inc-vars-11">27b
</a>▿
2036 <tt class=
"verbatim"><div class=
"compact-block">
2037 <tt>18 </tt>indent_options=-npro -kr -i8 -ts8 -sob -l80 -ss -ncs
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))
2049 <a id=
"code-end-Makefile.inc-vars-10"></a>
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.
2061 <a id=
"code-label-Makefile.inc-vars-11"></a>
2062 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-11">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
2066 lang=
<font color=
"blue"></font> +
≡
2067 </div><div class=
"right-tab">
2068 ▵<a href=
"#code-ref-Makefile.inc-vars-10">27a
</a> <a href=
"#code-ref-Makefile.inc-vars-12">27c
</a>▿
2074 <tt class=
"verbatim"><div class=
"compact-block">
2075 <tt>20 </tt>fangle_extract=@mkdir -p $(dir $(
1))
&& \
2078 <pre class=
"verbatim" xml:
space=
"preserve">
2079 <div class=
"compact-block"><tt>21 </tt> $(call fangle,$(
2),$(
1))
> "$(
1).tmp
" && \
2080 <tt>22 </tt> cat
"$(
1).tmp
" $(indent) | cpif
"$(
1)
" \
2081 <tt>23 </tt> && rm --
"$(
1).tmp
" || \
</div></pre>
2083 <tt class=
"verbatim"><div class=
"compact-block">
2084 <tt>24 </tt> (echo error newfangling $(
1) from $(
2) ; exit
1)
2088 <a id=
"code-end-Makefile.inc-vars-11"></a>
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.
2098 <a id=
"code-label-Makefile.inc-vars-12"></a>
2099 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-12">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
2103 lang=
<font color=
"blue"></font> +
≡
2104 </div><div class=
"right-tab">
2105 ▵<a href=
"#code-ref-Makefile.inc-vars-11">27b
</a> <a href=
"#code-ref-Makefile.inc-vars-13">28a
</a>⊳
2111 <tt class=
"verbatim"><div class=
"compact-block">
2112 <tt>25 </tt>define FANGLE_template
2115 <pre class=
"verbatim" xml:
space=
"preserve">
2116 <div class=
"compact-block"><tt>26 </tt> $(
1): $(
2)
2117 <tt>27 </tt>↦$$(call fangle_extract,$(
1),$(
2))
2118 <tt>28 </tt> FANGLE_TARGETS+=$(
1)
</div></pre>
2120 <tt class=
"verbatim"><div class=
"compact-block">
2125 <a id=
"code-end-Makefile.inc-vars-12"></a>
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
2135 <a id=
"code-label-Makefile.inc-targets-7"></a>
2136 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-7">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
2140 lang=
<font color=
"blue"></font> +
≡
2141 </div><div class=
"right-tab">
2142 ⊲<a href=
"#code-ref-Makefile.inc-targets-6">26d
</a> <a href=
"#code-ref-Makefile.inc-targets-8">27e
</a>▿
2148 <tt class=
"verbatim"><div class=
"compact-block">
2149 <tt>24 </tt>$(foreach source,$(FANGLE_SOURCES),\
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">
2160 <a id=
"code-end-Makefile.inc-targets-7"></a>
2164 These will all be built with
<tt class=
"verbatim">FANGLE_SOURCE_STAMP
</tt>.
2167 We also remove the generated sources on a make distclean.
2171 <a id=
"code-label-Makefile.inc-targets-8"></a>
2172 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-8">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
2176 lang=
<font color=
"blue"></font> +
≡
2177 </div><div class=
"right-tab">
2178 ▵<a href=
"#code-ref-Makefile.inc-targets-7">27d
</a> <a href=
"#code-ref-Makefile.inc-targets-9">28b
</a>⊳
2183 <tt class=
"verbatim"><div class=
"compact-block">
2184 <tt>27 </tt>_distclean: clean_fangle_sources
2187 <a id=
"code-end-Makefile.inc-targets-8"></a>
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.
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.
2202 <a id=
"code-label-Makefile.inc-vars-13"></a>
2203 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-13">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
2207 lang=
<font color=
"blue"></font> +
≡
2208 </div><div class=
"right-tab">
2209 ⊲<a href=
"#code-ref-Makefile.inc-vars-12">27c
</a> <a href=
"#code-ref-Makefile.inc-vars-14">28c
</a>▿
2214 <tt class=
"verbatim"><div class=
"compact-block">
2215 <tt>30 </tt>FANGLE_PDF=$(TEX_SOURCE:.tex=.pdf)
2218 <a id=
"code-end-Makefile.inc-vars-13"></a>
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.
2228 <a id=
"code-label-Makefile.inc-targets-9"></a>
2229 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-9">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
2233 lang=
<font color=
"blue"></font> +
≡
2234 </div><div class=
"right-tab">
2235 ⊲<a href=
"#code-ref-Makefile.inc-targets-8">27e
</a> <a href=
"#code-ref-Makefile.inc-targets-10">28d
</a>▿
2241 <tt class=
"verbatim"><div class=
"compact-block">
2242 <tt>28 </tt>$(FANGLE_PDF): $(TEX_SOURCE)
2245 <pre class=
"verbatim" xml:
space=
"preserve">
2246 <div class=
"compact-block"><tt>29 </tt>↦pdflatex $
< && pdflatex $
<
2248 <tt>31 </tt>clean_pdf:
2249 <tt>32 </tt>↦rm -f -- $(FANGLE_PDF) $(TEX_SOURCE:.tex=.toc) \
</div></pre>
2251 <tt class=
"verbatim"><div class=
"compact-block">
2252 <tt>33 </tt>↦ $(TEX_SOURCE:.tex=.log)
2253 $(TEX_SOURCE:.tex=.aux)
2257 <a id=
"code-end-Makefile.inc-targets-9"></a>
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.
2266 <a id=
"code-label-Makefile.inc-vars-14"></a>
2267 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-14">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
2271 lang=
<font color=
"blue"></font> +
≡
2272 </div><div class=
"right-tab">
2273 ▵<a href=
"#code-ref-Makefile.inc-vars-13">28a
</a> <a href=
"#code-ref-Makefile.inc-vars-15">28e
</a>▿
2278 <tt class=
"verbatim"><div class=
"compact-block">
2279 <tt>31 </tt>FANGLE_PDF=$(TEX_SOURCE:.tm=.pdf)
2282 <a id=
"code-end-Makefile.inc-vars-14"></a>
2285 <table style=
"display: inline; vertical-align: -0.55em">
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
2291 we need to update twice, generate a pdf, update twice mode and
2294 Basically the PDF export of TeXmacs is pretty rotten and doesn't
2295 work properly from the CLI
2301 <a id=
"code-label-Makefile.inc-targets-10"></a>
2302 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-10">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
2306 lang=
<font color=
"blue"></font> +
≡
2307 </div><div class=
"right-tab">
2308 ▵<a href=
"#code-ref-Makefile.inc-targets-9">28b
</a> <a href=
"#code-ref-Makefile.inc-targets-11">28f
</a>▿
2314 <tt class=
"verbatim"><div class=
"compact-block">
2315 <tt>34 </tt>$(FANGLE_PDF): $(TEXMACS_SOURCE)
2318 <pre class=
"verbatim" xml:
space=
"preserve">
2319 <div class=
"compact-block"><tt>35 </tt>↦texmacs -c $(TEXMACS_SOURCE) $
< -q
2321 <tt>37 </tt>clean_pdf:
</div></pre>
2323 <tt class=
"verbatim"><div class=
"compact-block">
2324 <tt>38 </tt>↦rm -f
– $(FANGLE_PDF)
2328 <a id=
"code-end-Makefile.inc-targets-10"></a>
2331 <h3 id=
"auto-33">6.5.3<span style=
"margin-left: 1em"></span>Building the Documentation as a
2334 Currently we only build pdf as a final format, but
<tt class=
"verbatim">FANGLE_DOCS
</tt>
2335 may later hold other output formats.
2339 <a id=
"code-label-Makefile.inc-vars-15"></a>
2340 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-vars-15">
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]()
⇑<a href=
"#code-ref-Makefile.inc-vars-1">23b
</a>,
2344 lang=
<font color=
"blue"></font> +
≡
2345 </div><div class=
"right-tab">
2346 ▵<a href=
"#code-ref-Makefile.inc-vars-14">28c
</a>
2351 <tt class=
"verbatim"><div class=
"compact-block">
2352 <tt>32 </tt>FANGLE_DOCS=$(FANGLE_PDF)
2354 </p><table style=
"width: 100%" id=
"code-end-Makefile.inc-vars-15">
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>
2361 We also define
<tt class=
"verbatim">fangle_docs
</tt> as a convenient phony target.
2365 <a id=
"code-label-Makefile.inc-targets-11"></a>
2366 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-11">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
2370 lang=
<font color=
"blue"></font> +
≡
2371 </div><div class=
"right-tab">
2372 ▵<a href=
"#code-ref-Makefile.inc-targets-10">28d
</a> <a href=
"#code-ref-Makefile.inc-targets-12">28g
</a>▿
2378 <tt class=
"verbatim"><div class=
"compact-block">
2379 <tt>39 </tt>.PHONY: fangle_docs
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
2390 <a id=
"code-end-Makefile.inc-targets-11"></a>
2394 And define a convenient
<tt class=
"verbatim">clean_fangle_docs
</tt> which we add
2395 to the regular clean target
2399 <a id=
"code-label-Makefile.inc-targets-12"></a>
2400 <table style=
"width: 100%" id=
"code-ref-Makefile.inc-targets-12">
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]()
⇑<a href=
"#code-ref-Makefile.inc-targets-1">24b
</a>,
2404 lang=
<font color=
"blue"></font> +
≡
2405 </div><div class=
"right-tab">
2406 ▵<a href=
"#code-ref-Makefile.inc-targets-11">28f
</a>
2412 <tt class=
"verbatim"><div class=
"compact-block">
2413 <tt>42 </tt>.PHONEY: clean_fangle_docs
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
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
2426 </p><table style=
"width: 100%" id=
"code-end-Makefile.inc-targets-12">
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>
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:
2437 <pre class=
"verbatim" xml:
space=
"preserve">
2438 make fangle_sources
</pre>
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.
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.
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">—</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>.
2463 <table style=
"display: inline; vertical-align: -0.55em">
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>
2470 <a id=
"code-label-*-1"></a>
2471 <table style=
"width: 100%" id=
"code-ref-*-1">
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> ≡</font></td>
2479 <tt class=
"verbatim"><div class=
"compact-block">
2480 <tt>1 </tt>#! /bin/sh
2483 <pre class=
"verbatim" xml:
space=
"preserve">
2484 <div class=
"compact-block"><tt>2 </tt>
2485 <tt>3 </tt>MAKE_SRC=
"${
1:-${NW_LYX:-../../noweb-lyx/noweb-lyx3.lyx}}
"
2486 <tt>4 </tt>MAKE_SRC=
‘dirname
"$MAKE_SRC
"‘/
‘basename
"$MAKE_SRC
" .lyx
‘
2487 <tt>5 </tt>NOWEB_SRC=
"${
2:-${NOWEB_SRC:-$MAKE_SRC.lyx}}
"
2488 <tt>6 </tt>lyx -e latex $MAKE_SRC
2490 <tt>8 </tt>fangle -R./Makefile.inc ${MAKE_SRC}.tex \
2491 <tt>9 </tt> | sed
"/FANGLE_SOURCE=/s/^/#/;T;aNOWEB_SOURCE=$FANGLE_SRC
" \
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
2499 </p><table style=
"width: 100%" id=
"code-end-*-1">
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>
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
2511 The
<em>autoboot
</em> can be extracted with this command:
2513 <pre class=
"verbatim" xml:
space=
"preserve">
2514 lyx -e latex fangle.lyx
&& \
2515 fangle fangle.lyx
> ./autoboot
</pre>
2517 This looks simple enough, but as mentioned, fangle has to be had from
2518 somewhere before it can be extracted.
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.
2524 <table style=
"display: inline; vertical-align: -0.55em">
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>
2530 <h2 id=
"auto-36">6.8<span style=
"margin-left: 1em"></span>Incorporating Makefile.inc into existing
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
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
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.
2551 <a id=
"auto-37"></a><h5>Example
</h5>Sub-module of existing system
2554 In this example, we are building
<tt class=
"verbatim">module.so
</tt> as a literate
2555 module of a larger project.
2558 We will show the sort glue that can be inserted into the projects
2559 Makefile
<class style=
"font-family: Times New Roman">—</class> or more likely
<class style=
"font-family: Times New Roman">—</class>
2560 a regular Makefile included in or invoked by the projects Makefile.
2564 <a id=
"code-label-makefile-glue-1"></a>
2565 <table style=
"width: 100%" id=
"code-ref-makefile-glue-1">
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>
2570 </div><div class=
"right-tab">
2571 <a href=
"#code-ref-makefile-glue-2">30b
</a>▿
2577 <tt class=
"verbatim"><div class=
"compact-block">
2578 <tt>1 </tt>module_srcdir=modules/module
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
2589 <a id=
"code-end-makefile-glue-1"></a>
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
2612 <span style=
"margin-left: 0em"></span>
2613 <a id=
"footnr-6"></a>
2614 <sup><a href=
"#footnote-6">6</a></sup>
2619 <a id=
"code-label-makefile-glue-2"></a>
2620 <table style=
"width: 100%" id=
"code-ref-makefile-glue-2">
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]()
⇑<a href=
"#code-ref-makefile-glue-1">30a
</a>,
2624 lang=
<font color=
"blue">make
</font> +
≡
2625 </div><div class=
"right-tab">
2626 ▵<a href=
"#code-ref-makefile-glue-1">30a
</a> <a href=
"#code-ref-makefile-glue-3">30c
</a>▿
2631 <tt class=
"verbatim"><div class=
"compact-block">
2632 <tt>4 </tt>$(module_srcdir)/module.o:
2633 $(module_srcdir)/$(MODULE_STAMP)
2636 <a id=
"code-end-makefile-glue-2"></a>
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.
2646 <a id=
"code-label-makefile-glue-3"></a>
2647 <table style=
"width: 100%" id=
"code-ref-makefile-glue-3">
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]()
⇑<a href=
"#code-ref-makefile-glue-1">30a
</a>,
2651 lang=
<font color=
"blue"></font> +
≡
2652 </div><div class=
"right-tab">
2653 ▵<a href=
"#code-ref-makefile-glue-2">30b
</a> <a href=
"#code-ref-makefile-glue-4">30d
</a>▿
2659 <tt class=
"verbatim"><div class=
"compact-block">
2660 <tt>5 </tt>$(module_srcdir)/$(MODULE_STAMP):
2661 $(module_srcdir)/$(MODULE_SOURCE)
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>↦$(MAKE) -C $(module_srcdir) -f Makefile.inc
2669 fangle_sources LITERATE_SOURCE=$(MODULE_SOURCE)
2673 <a id=
"code-end-makefile-glue-3"></a>
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.
2683 <a id=
"code-label-makefile-glue-4"></a>
2684 <table style=
"width: 100%" id=
"code-ref-makefile-glue-4">
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]()
⇑<a href=
"#code-ref-makefile-glue-1">30a
</a>,
2688 lang=
<font color=
"blue"></font> +
≡
2689 </div><div class=
"right-tab">
2690 ▵<a href=
"#code-ref-makefile-glue-3">30c
</a>
2696 <tt class=
"verbatim"><div class=
"compact-block">
2697 <tt>7 </tt>docs:: docs_module
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>↦$(MAKE) -C $(module_srcdir) -f Makefile.inc docs LITERATE_SOURCE=$(MODULE_SOURCE)
2705 <tt>12 </tt>clean:: clean_module
2706 <tt>13 </tt>.PHONEY: clean_module
2707 <tt>14 </tt>clean_module:
2708 <tt>15 </tt>↦$(MAKE) -C $(module_srcdir) -f Makefile.inc clean LITERATE_SOURCE=$(MODULE_SOURCE)
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>↦$(MAKE) -C $(module_srcdir) -f Makefile.inc
2716 distclean LITERATE_SOURCE=$(MODULE_SOURCE)
2719 </p><table style=
"width: 100%" id=
"code-end-makefile-glue-4">
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>
2726 We could do similarly for install targets to install the generated docs.
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">
2731 </div><div class=
"right-tab">
2732 <br></br>Source Code
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>.
2741 <a id=
"code-label-./fangle-1"></a>
2742 <table style=
"width: 100%" id=
"code-ref-./fangle-1">
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>
2747 </div><div class=
"right-tab">
2748 <a href=
"#code-ref-./fangle-2">33b
</a>▿
2754 <tt class=
"verbatim"><div class=
"compact-block">
2755 <tt>1 </tt>#! /usr/bin/awk -f
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>
2766 <a id=
"code-end-./fangle-1"></a>
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.
2776 <a id=
"code-label-./fangle-2"></a>
2777 <table style=
"width: 100%" id=
"code-ref-./fangle-2">
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]()
⇑<a href=
"#code-ref-./fangle-1">33a
</a>,
2781 lang=
<font color=
"blue"></font> +
≡
2782 </div><div class=
"right-tab">
2783 ▵<a href=
"#code-ref-./fangle-1">33a
</a> <a href=
"#code-ref-./fangle-3">33c
</a>▿
2789 <tt class=
"verbatim"><div class=
"compact-block">
2790 <tt>3 </tt># NOTE: Arnold Robbins public domain getopt for awk is
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">
2803 <a id=
"code-end-./fangle-2"></a>
2807 And include the following chunks (which are explained further on) to
2808 make up the program:
2812 <a id=
"code-label-./fangle-3"></a>
2813 <table style=
"width: 100%" id=
"code-ref-./fangle-3">
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]()
⇑<a href=
"#code-ref-./fangle-1">33a
</a>,
2817 lang=
<font color=
"blue"></font> +
≡
2818 </div><div class=
"right-tab">
2819 ▵<a href=
"#code-ref-./fangle-2">33b
</a> <a href=
"#code-ref-./fangle-4">36a
</a>⊳
2825 <tt class=
"verbatim"><div class=
"compact-block">
2826 <tt>7 </tt>helper-functions
<a href=
"#code-ref-helper-functions-1">34d
</a>
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>
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>
2847 <a id=
"code-end-./fangle-3"></a>
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:
2857 <a id=
"code-label-awk-delete-array-1"></a>
2858 <table style=
"width: 100%" id=
"code-ref-awk-delete-array-1">
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>
2866 <tt class=
"verbatim"><div class=
"compact-block">
2867 <tt>1 </tt>split(
"",
<font color=
"#008000">ARRAY
</font>);
2869 </p><table style=
"width: 100%" id=
"code-end-awk-delete-array-1">
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>
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.
2881 <a id=
"code-label-dump-array-1"></a>
2882 <table style=
"width: 100%" id=
"code-ref-dump-array-1">
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>
2891 <tt class=
"verbatim"><div class=
"compact-block">
2892 <tt>1 </tt>print
"\nDump:
<font color=
"#008000">ARRAY
</font>\n
––––\n
"
2893 > "/dev/stderr
";
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
"=
" <font color=
"#008000">ARRAY
</font>[_x]
"\n
" > "/dev/stderr
";
2899 <tt>4 </tt>}
</div></pre>
2901 <tt class=
"verbatim"><div class=
"compact-block">
2902 <tt>5 </tt>print
"========\n
" >
2903 "/dev/stderr
";
2906 </p><table style=
"width: 100%" id=
"code-end-dump-array-1">
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>
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:
2918 <a id=
"code-label-error()-1"></a>
2919 <table style=
"width: 100%" id=
"code-ref-error()-1">
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>
2924 </div><div class=
"right-tab">
2925 <a href=
"#code-ref-error()-2">34b
</a>▿
2931 <tt class=
"verbatim"><div class=
"compact-block">
2932 <tt>1 </tt>function error(message)
2935 <pre class=
"verbatim" xml:
space=
"preserve">
2936 <div class=
"compact-block"><tt>2 </tt>{
2937 <tt>3 </tt> print
"ERROR:
" FILENAME
":
" FNR
" " message
> "/dev/stderr
";
2938 <tt>4 </tt> exit
1;
</div></pre>
2940 <tt class=
"verbatim"><div class=
"compact-block">
2945 <a id=
"code-end-error()-1"></a>
2949 and likewise for non-fatal warnings:
2953 <a id=
"code-label-error()-2"></a>
2954 <table style=
"width: 100%" id=
"code-ref-error()-2">
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]()
⇑<a href=
"#code-ref-error()-1">34a
</a>,
2958 lang=
<font color=
"blue">awk
</font> +
≡
2959 </div><div class=
"right-tab">
2960 ▵<a href=
"#code-ref-error()-1">34a
</a> <a href=
"#code-ref-error()-3">34c
</a>▿
2966 <tt class=
"verbatim"><div class=
"compact-block">
2967 <tt>6 </tt>function warning(message)
2970 <pre class=
"verbatim" xml:
space=
"preserve">
2971 <div class=
"compact-block"><tt>7 </tt>{
2972 <tt>8 </tt> print
"WARNING:
" FILENAME
":
" FNR
" " message
> "/dev/stderr
";
2973 <tt>9 </tt> warnings++;
</div></pre>
2975 <tt class=
"verbatim"><div class=
"compact-block">
2980 <a id=
"code-end-error()-2"></a>
2984 and debug output too:
2988 <a id=
"code-label-error()-3"></a>
2989 <table style=
"width: 100%" id=
"code-ref-error()-3">
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]()
⇑<a href=
"#code-ref-error()-1">34a
</a>,
2993 lang=
<font color=
"blue">awk
</font> +
≡
2994 </div><div class=
"right-tab">
2995 ▵<a href=
"#code-ref-error()-2">34b
</a>
3001 <tt class=
"verbatim"><div class=
"compact-block">
3002 <tt>11 </tt>function debug_log(message)
3005 <pre class=
"verbatim" xml:
space=
"preserve">
3006 <div class=
"compact-block"><tt>12 </tt>{
3007 <tt>13 </tt> print
"DEBUG:
" FILENAME
":
" FNR
" " message
> "/dev/stderr
";
</div></pre>
3009 <tt class=
"verbatim"><div class=
"compact-block">
3013 </p><table style=
"width: 100%" id=
"code-end-error()-3">
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>
3019 <table style=
"display: inline; vertical-align: -0.55em">
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>
3026 <a id=
"code-label-helper-functions-1"></a>
3027 <table style=
"width: 100%" id=
"code-ref-helper-functions-1">
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> ≡</font></td>
3034 <tt class=
"verbatim"><div class=
"compact-block">
3035 <tt>1 </tt>error()
<a href=
"#code-ref-error()-1">34a
</a>
3037 </p><table style=
"width: 100%" id=
"code-end-helper-functions-1">
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>
3043 <h1 id=
"auto-42">Chapter
8<br></br>LaTeX and lstlistings
</h1>
3044 <table style=
"display: inline; vertical-align: -0.55em">
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>
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.
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).
3063 A sample expressions is:
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:
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
3087 <span style=
"margin-left: 0em"></span>
3088 <a id=
"footnr-1"></a>
3089 <sup><a href=
"#footnote-1">1</a></sup>
3092 <table style=
"display: inline; vertical-align: -3.3em">
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>
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>
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>
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>
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>
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>
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
–- we may consider that
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>.
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.
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.
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.
3144 There will be other arguments supported too;
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:
3158 <pre class=
"verbatim" xml:
space=
"preserve">
3159 achunk,language=C,params=name;address
</pre>
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.
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.
3178 <a id=
"code-label-./fangle-4"></a>
3179 <table style=
"width: 100%" id=
"code-ref-./fangle-4">
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]()
⇑<a href=
"#code-ref-./fangle-1">33a
</a>,
3183 lang=
<font color=
"blue"></font> +
≡
3184 </div><div class=
"right-tab">
3185 ⊲<a href=
"#code-ref-./fangle-3">33c
</a>
3190 <tt class=
"verbatim"><div class=
"compact-block">
3191 <tt>19 </tt>=
<\chunkref{get_chunk_args()}
>
3193 </p><table style=
"width: 100%" id=
"code-end-./fangle-4">
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>
3201 <a id=
"code-label-get_chunk_args()-1"></a>
3202 <table style=
"width: 100%" id=
"code-ref-get_chunk_args()-1">
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>
3207 </div><div class=
"right-tab">
3208 <a href=
"#code-ref-get_chunk_args()-2">36c
</a>▿
3214 <tt class=
"verbatim"><div class=
"compact-block">
3215 <tt>1 </tt>function get_chunk_args(text, values,
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)
3228 <a id=
"code-end-get_chunk_args()-1"></a>
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
–- and executed in a loop
–- 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.
3242 <a id=
"code-label-get_chunk_args()-2"></a>
3243 <table style=
"width: 100%" id=
"code-ref-get_chunk_args()-2">
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]()
⇑<a href=
"#code-ref-get_chunk_args()-1">36b
</a>,
3247 lang=
<font color=
"blue"></font> +
≡
3248 </div><div class=
"right-tab">
3249 ▵<a href=
"#code-ref-get_chunk_args()-1">36b
</a>
3255 <tt class=
"verbatim"><div class=
"compact-block">
3259 <pre class=
"verbatim" xml:
space=
"preserve">
3260 <div class=
"compact-block"><tt>7 </tt> split(
"", next_chunk_args);
3261 <tt>8 </tt> while(length(text)) {
3262 <tt>9 </tt> if (match(text,
"^ *}(.*)
", a)) {
3263 <tt>10 </tt> return a[
1];
3265 <tt>12 </tt> =
<\chunkref{parse-chunk-args}
>
3267 <tt>14 </tt> return text;
</div></pre>
3269 <tt class=
"verbatim"><div class=
"compact-block">
3273 </p><table style=
"width: 100%" id=
"code-end-get_chunk_args()-2">
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>
3280 We can see that the text could be inspected with this regex:
3284 <a id=
"code-label-parse-chunk-args-1"></a>
3285 <table style=
"width: 100%" id=
"code-ref-parse-chunk-args-1">
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>
3290 </div><div class=
"right-tab">
3291 <a href=
"#code-ref-parse-chunk-args-2">37a
</a>⊳
3297 <tt class=
"verbatim"><div class=
"compact-block">
3298 <tt>1 </tt>if (! match(text,
" *([^,=]*[^,= ]) *(([,=])
3299 *(([^,}]*) *,* *(.*))|)$
", a)) {
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">
3310 <a id=
"code-end-parse-chunk-args-1"></a>
3314 and that
<tt class=
"verbatim">a
</tt> will have the following values:
3316 <table style=
"display: inline; vertical-align: -3.85em">
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>
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>
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>
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>
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>
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>
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>
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).
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
3352 <a id=
"code-label-parse-chunk-args-2"></a>
3353 <table style=
"width: 100%" id=
"code-ref-parse-chunk-args-2">
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]()
⇑<a href=
"#code-ref-parse-chunk-args-1">36d
</a>,
3357 lang=
<font color=
"blue"></font> +
≡
3358 </div><div class=
"right-tab">
3359 ⊲<a href=
"#code-ref-parse-chunk-args-1">36d
</a>
3365 <tt class=
"verbatim"><div class=
"compact-block">
3366 <tt>4 </tt>name=a[
1];
3369 <pre class=
"verbatim" xml:
space=
"preserve">
3370 <div class=
"compact-block"><tt>5 </tt>if (a[
3] ==
"=
") {
3371 <tt>6 </tt> if (substr(a[
4],
1,
1) ==
"{
") {
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];
3377 <tt>12 </tt>} else {
3378 <tt>13 </tt> values[path name]=
"";
3379 <tt>14 </tt> text = a[
2];
</div></pre>
3381 <tt class=
"verbatim"><div class=
"compact-block">
3385 </p><table style=
"width: 100%" id=
"code-end-parse-chunk-args-2">
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>
3392 We can test this function like this:
3396 <a id=
"code-label-gca-test.awk-1"></a>
3397 <table style=
"width: 100%" id=
"code-ref-gca-test.awk-1">
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> ≡</font></td>
3405 <tt class=
"verbatim"><div class=
"compact-block">
3406 <tt>1 </tt>=
<\chunkref{get_chunk_args()}
>
3409 <pre class=
"verbatim" xml:
space=
"preserve">
3410 <div class=
"compact-block"><tt>2 </tt>BEGIN {
3411 <tt>3 </tt> SUBSEP=
".
";
3413 <tt>5 </tt> print get_chunk_args(
"name=freddie, foo={bar=baz, quux={quirk, a=fleeg}}, etc
", a);
3414 <tt>6 </tt> for (b in a) {
3415 <tt>7 </tt> print
"a[
" b
"] =
> " a[b];
3416 <tt>8 </tt> }
</div></pre>
3418 <tt class=
"verbatim"><div class=
"compact-block">
3422 </p><table style=
"width: 100%" id=
"code-end-gca-test.awk-1">
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>
3429 which should give this output:
3433 <a id=
"code-label-gca-test.awk-results-1"></a>
3434 <table style=
"width: 100%" id=
"code-ref-gca-test.awk-results-1">
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> ≡</font></td>
3442 <tt class=
"verbatim"><div class=
"compact-block">
3443 <tt>1 </tt>a[foo.quux.quirk] =
>
3446 <pre class=
"verbatim" xml:
space=
"preserve">
3447 <div class=
"compact-block"><tt>2 </tt>a[foo.quux.a] =
> fleeg
3448 <tt>3 </tt>a[foo.bar] =
> baz
3449 <tt>4 </tt>a[etc] =
> </div></pre>
3451 <tt class=
"verbatim"><div class=
"compact-block">
3452 <tt>5 </tt>a[name] =
> freddie
3455 </p><table style=
"width: 100%" id=
"code-end-gca-test.awk-results-1">
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>
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
3467 <pre class=
"verbatim" xml:
space=
"preserve">
3468 \Chunk{achunk, params=name ; address}
</pre>
3470 could be invoked as:
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:
3478 <pre class=
"verbatim" xml:
space=
"preserve">
3479 \chunkref{pull}(things[x, y], get_other_things(a,
"(all)
"))
</pre>
3481 –- 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 "(all)
")
</tt>.
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>–- neither of which
3489 make sense on their own.
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">"</tt>
3508 <tt class=
"verbatim">"</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
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
3532 <span style=
"margin-left: 0em"></span>
3533 <a id=
"footnr-2"></a>
3534 <sup><a href=
"#footnote-2">2</a></sup>
3538 Unfortunately, the full set of matching delimiters may vary from
3539 language to language. In certain C++ template contexts,
<tt class=
"verbatim"><</tt>
3540 and
<tt class=
"verbatim">></tt> would count as delimiters, and yet in other
3541 contexts they would not.
3544 This puts me in the unfortunate position of having to parse-somewhat all
3545 programming languages without knowing what they are!
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.
3554 <a id=
"code-label-parse_chunk_args-1"></a>
3555 <table style=
"width: 100%" id=
"code-ref-parse_chunk_args-1">
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> ≡</font></td>
3563 <tt class=
"verbatim"><div class=
"compact-block">
3564 <tt>1 </tt>function parse_chunk_args(language, text, values,
3568 <pre class=
"verbatim" xml:
space=
"preserve">
3569 <div class=
"compact-block"><tt>2 </tt> # local vars
3570 <tt>3 </tt> c, context, rest)
3572 <tt>5 </tt> =
<\chunkref{new-mode-tracker}(context, language, mode)
>
3573 <tt>6 </tt> rest = mode_tracker(context, text, values);
3574 <tt>7 </tt> # extract values
3575 <tt>8 </tt> for(c=
1; c
<= context[
0,
"values
"]; c++) {
3576 <tt>9 </tt> values[c] = context[
0,
"values
", c];
3578 <tt>11 </tt> return rest;
</div></pre>
3580 <tt class=
"verbatim"><div class=
"compact-block">
3584 </p><table style=
"width: 100%" id=
"code-end-parse_chunk_args-1">
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>
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">=
<\param{name}
></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.
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.
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.
3612 <a id=
"code-label-expand_chunk_args()-1"></a>
3613 <table style=
"width: 100%" id=
"code-ref-expand_chunk_args()-1">
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> ≡</font></td>
3621 <tt class=
"verbatim"><div class=
"compact-block">
3622 <tt>1 </tt>function expand_chunk_args(text, params, args,
3625 <pre class=
"verbatim" xml:
space=
"preserve">
3626 <div class=
"compact-block"><tt>2 </tt> p, text_array, next_text, v, t, l)
3628 <tt>4 </tt> if (split(text, text_array,
"\\${
")) {
3629 <tt>5 </tt> substitute-chunk-args
<a href=
"#code-ref-substitute-chunk-args-1">39a
</a>
3632 <tt>8 </tt> return text;
</div></pre>
3634 <tt class=
"verbatim"><div class=
"compact-block">
3638 </p><table style=
"width: 100%" id=
"code-end-expand_chunk_args()-1">
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>
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.
3651 <a id=
"code-label-substitute-chunk-args-1"></a>
3652 <table style=
"width: 100%" id=
"code-ref-substitute-chunk-args-1">
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> ≡
3657 </div><div class=
"right-tab">
3658 <a href=
"#code-ref-substitute-chunk-args-2">39b
</a>▿
3664 <tt class=
"verbatim"><div class=
"compact-block">
3665 <tt>1 </tt>for(p in params) {
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">
3676 <a id=
"code-end-substitute-chunk-args-1"></a>
3680 We accumulate substituted text in the variable text. As the first part
3681 of the split function is the part before the delimiter
–- which is
3682 <tt class=
"verbatim">${
</tt> in our case
–- 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>.
3688 <a id=
"code-label-substitute-chunk-args-2"></a>
3689 <table style=
"width: 100%" id=
"code-ref-substitute-chunk-args-2">
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]()
⇑<a href=
"#code-ref-substitute-chunk-args-1">39a
</a>,
3693 lang=
<font color=
"blue"></font> +
≡
3694 </div><div class=
"right-tab">
3695 ▵<a href=
"#code-ref-substitute-chunk-args-1">39a
</a> <a href=
"#code-ref-substitute-chunk-args-3">39c
</a>▿
3700 <tt class=
"verbatim"><div class=
"compact-block">
3701 <tt>4 </tt>text=text_array[
1];
3704 <a id=
"code-end-substitute-chunk-args-2"></a>
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
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">
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>
3729 , and substitute each reference for it's argument.
3733 <a id=
"code-label-substitute-chunk-args-3"></a>
3734 <table style=
"width: 100%" id=
"code-ref-substitute-chunk-args-3">
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]()
⇑<a href=
"#code-ref-substitute-chunk-args-1">39a
</a>,
3738 lang=
<font color=
"blue"></font> +
≡
3739 </div><div class=
"right-tab">
3740 ▵<a href=
"#code-ref-substitute-chunk-args-2">39b
</a>
3746 <tt class=
"verbatim"><div class=
"compact-block">
3747 <tt>5 </tt>for(t=
2; t in text_array; t++) {
3750 <pre class=
"verbatim" xml:
space=
"preserve">
3751 <div class=
"compact-block"><tt>6 </tt> =
<\chunkref{substitute-chunk-arg}
></div></pre>
3753 <tt class=
"verbatim"><div class=
"compact-block">
3757 </p><table style=
"width: 100%" id=
"code-end-substitute-chunk-args-3">
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>
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.
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
–- such
3774 constructs will not be interfered with unless the parameter name also
3779 <a id=
"code-label-substitute-chunk-arg-1"></a>
3780 <table style=
"width: 100%" id=
"code-ref-substitute-chunk-arg-1">
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> ≡</font></td>
3788 <tt class=
"verbatim"><div class=
"compact-block">
3789 <tt>1 </tt>if (match(text_array[t],
3790 "^([a-zA-Z_][a-zA-Z0-
9_]*)}
", l)
&&
3793 <pre class=
"verbatim" xml:
space=
"preserve">
3794 <div class=
"compact-block"><tt>2 </tt> l[
1] in v)
3796 <tt>4 </tt> text = text v[l[
1]] substr(text_array[t], length(l[
1])+
2);
3798 <tt>6 </tt> text = text
"${
" text_array[t];
</div></pre>
3800 <tt class=
"verbatim"><div class=
"compact-block">
3804 </p><table style=
"width: 100%" id=
"code-end-substitute-chunk-arg-1">
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>
3810 <h1 id=
"auto-46"><a id=
"modes"></a>Chapter
9<br></br>Language Modes
& 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
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.
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.
3830 One parse mode is the strings mode. The string mode is commenced by an
3831 un-escaped quotation mark
<tt class=
"verbatim">"</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.
3837 Another mode is
<tt class=
"verbatim">[
</tt> which is terminated by a
<tt class=
"verbatim">]
</tt>
3838 (unless it occurs in a string).
3841 Consider this fragment of C code:
3847 things([
<var>x
</var>,
3848 <var>y
</var>])
<sup>&wide-overbrace;
</sup><sup>1. [ mode
</sup>,
3849 get_other_things((
<var>a
</var>,
<sub>3.
"
3850 mode</sub>))<sup>&wide-overbrace;</sup><sup>2. ( mode</sup>
3856 Mode nesting prevents the close parenthesis in the quoted string (part
3857 3) from terminating the parenthesis mode (part 2).
3860 Each language has a set of modes, the default mode being the null mode.
3861 Each mode can lead to other modes.
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:
3869 <a id="code-label-example-perl-
1"></a>
3870 <table style="width:
100%
" id="code-ref-example-perl-
1">
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> ≡</font></td>
3877 <tt class="verbatim
"><div class="compact-block
">
3878 print "hello world $0\n";
3880 </p><table style="width:
100%
" id="code-end-example-perl-
1">
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>
3887 If it were included in a chunk with <tt class="verbatim
">language=sh</tt>, like
3892 <a id="code-label-example-sh-
1"></a>
3893 <table style="width:
100%
" id="code-ref-example-sh-
1">
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> ≡</font></td>
3900 <tt class="verbatim
"><div class="compact-block
">
3901 perl -e "=<\chunkref{example-perl}>"
3903 </p><table style="width:
100%
" id="code-end-example-sh-
1">
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>
3910 fangle would <em>want</em> to generate output like this:
3912 <pre class="verbatim
" xml:space="preserve
">
3913 perl -e "print \"hello world \$0\\n\";" </pre>
3915 See that the double quote <tt class="verbatim
">"</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.
3920 If that were then included in a chunk with language=make, like this:
3924 <a id="code-label-example-makefile-
1"></a>
3925 <table style="width:
100%
" id="code-ref-example-makefile-
1">
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> ≡</font></td>
3933 <tt class="verbatim
"><div class="compact-block
">
3934 <tt>1 </tt>target: pre-req
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
">
3944 <div class="right-tab
">
3945 =<\chunkref{example-sh}>
3949 </p><table style="width:
100%
" id="code-end-example-makefile-
1">
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>
3956 We would need the output to look like this –- note the <tt class="verbatim
">$$</tt>:
3958 <pre class="verbatim
" xml:space="preserve
">
3960 perl -e "print \"hello world \$$0\\n\";"</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.
3969 For example, the sed transformation for text to be inserted into shell
3970 double-quoted strings would be something like:
3972 <pre class="verbatim
" xml:space="preserve
">
3973 s/\\/\\\\/g;s/$/\\$/g;s/"/\\"/g;</pre>
3975 which protects <tt class="verbatim
">\ $ "</tt>.
3978 <table style="display: inline; vertical-align: -
0.55em
">
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>
3983 The mode tracker must also track nested mode-changes, as in this sh
3986 <pre class="verbatim
" xml:space="preserve
">
3987 echo "hello ‘id ...‘"</pre>
3992 Any characters inserted at the point marked ↑ would need to be
3993 escaped, including <tt class="verbatim
">‘</tt> <tt class="verbatim
">|</tt> <tt class="verbatim
">*</tt>
3994 among others. First it would need escaping for the back-ticks <tt class="verbatim
">‘</tt>,
3995 and then for the double-quotes <tt class="verbatim
">"</tt>.
3998 <table style="display: inline; vertical-align: -
0.55em
">
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>
4003 Escaping need not occur if the format and mode of the included chunk
4004 matches that of the including chunk.
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.
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.
4020 Note that the transformed text should have the property of not being
4021 able to change the mode in the current chunk.
4023 <table style="display: inline; vertical-align: -
0.55em
">
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
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.
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).
4041 <table style="display: inline; vertical-align: -
0.55em
">
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 =<\mode{}> command which will allow us to signify
4047 regex and thus fangle will quote it for us.
4052 Submodes are entered by the characters <tt class="verbatim
">"</tt> <tt
4053 class="verbatim
">'</tt> <tt class="verbatim
">{</tt> <tt class="verbatim
">(</tt> <tt class="verbatim
">[</tt> <tt
4054 class="verbatim
">/*</tt>
4058 <a id="code-label-common-mode-definitions-
1"></a>
4059 <table style="width:
100%
" id="code-ref-common-mode-definitions-
1">
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> ≡
4064 </div><div class="right-tab
">
4065 <a href="#code-ref-common-mode-definitions-
2">43b</a>▿
4070 <tt class="verbatim
"><div class="compact-block
">
4071 <tt>1 </tt>modes[${language}, "",
4072 "submodes"]="\\\\|\"|'|{|\\(|\\[";
4075 <a id="code-end-common-mode-definitions-
1"></a>
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
4092 <span style="margin-left:
0em
"></span>
4093 <a id="footnr-
1"></a>
4094 <sup><a href="#footnote-
1">1</a></sup>
4099 <a id="code-label-common-mode-definitions-
2"></a>
4100 <table style="width:
100%
" id="code-ref-common-mode-definitions-
2">
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 ⇑<a href="#code-ref-common-mode-definitions-
1">43a</a>, lang=<font color="blue
"></font> + ≡
4105 </div><div class="right-tab
">
4106 ▵<a href="#code-ref-common-mode-definitions-
1">43a</a> <a href="#code-ref-common-mode-definitions-
3">43d</a>▿
4111 <tt class="verbatim
"><div class="compact-block
">
4112 <tt>2 </tt>modes[${language}, "",
4113 "delimiters"]=" *, *";
4116 <a id="code-end-common-mode-definitions-
2"></a>
4120 and should pass this test:
4121 <table style="display: inline; vertical-align: -
0.55em
">
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
4130 <a id="code-label-test:mode-definitions-
1"></a>
4131 <table style="width:
100%
" id="code-ref-test:mode-definitions-
1">
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> ≡
4136 </div><div class="right-tab
">
4137 <a href="#code-ref-test:mode-definitions-
2">44g</a>⊳
4143 <tt class="verbatim
"><div class="compact-block
">
4144 <tt>1 </tt>parse_chunk_args("c-like",
4145 "1,2,3", a, "");
4148 <pre class="verbatim
" xml:space="preserve
">
4149 <div class="compact-block
"><tt>2 </tt>if (a[1] != "1") e++;
4150 <tt>3 </tt>if (a[2] != "2") e++;
4151 <tt>4 </tt>if (a[3] != "3") e++;
4152 <tt>5 </tt>if (length(a) != 3) e++;
4153 <tt>6 </tt>=<\chunkref{pca-test.awk:summary}>
4155 <tt>8 </tt>parse_chunk_args("c-like", "joe, red", a, "");
4156 <tt>9 </tt>if (a[1] != "joe") e++;
4157 <tt>10 </tt>if (a[2] != "red") e++;
4158 <tt>11 </tt>if (length(a) != 2) e++;
4159 <tt>12 </tt>=<\chunkref{pca-test.awk:summary}>
4161 <tt>14 </tt>parse_chunk_args("c-like", "${colour}", a, "");
4162 <tt>15 </tt>if (a[1] != "${colour}") e++;
4163 <tt>16 </tt>if (length(a) != 1) e++;</div></pre>
4165 <tt class="verbatim
"><div class="compact-block
">
4166 <tt>17 </tt>=<\chunkref{pca-test.awk:summary}>
4170 <a id="code-end-test:mode-definitions-
1"></a>
4174 Nested modes are identified by a backslash, a double or single quote,
4175 various bracket styles or a <tt class="verbatim
">/*</tt> comment.
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>.
4194 <span style="margin-left:
0em
"></span>
4195 <a id="footnr-
2"></a>
4196 <sup><a href="#footnote-
2">2</a></sup>
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.
4212 <a id="code-label-common-mode-definitions-
3"></a>
4213 <table style="width:
100%
" id="code-ref-common-mode-definitions-
3">
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 ⇑<a href="#code-ref-common-mode-definitions-
1">43a</a>, lang=<font color="blue
"></font> + ≡
4218 </div><div class="right-tab
">
4219 ▵<a href="#code-ref-common-mode-definitions-
2">43b</a> <a href="#code-ref-common-mode-definitions-
4">44f</a>⊳
4224 <tt class="verbatim
"><div class="compact-block
">
4225 <tt>3 </tt>modes[${language}, "\\",
4226 "terminators"]=".";
4229 <a id="code-end-common-mode-definitions-
3"></a>
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
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.
4244 <a id="code-label-mode:common-string-
1"></a>
4245 <table style="width:
100%
" id="code-ref-mode:common-string-
1">
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> ≡
4250 </div><div class="right-tab
">
4251 <a href="#code-ref-mode:common-string-
2">44b</a>▿
4256 <tt class="verbatim
"><div class="compact-block
">
4257 <tt>1 </tt>modes[${language}, ${quote},
4258 "submodes"]="\\\\";
4261 <a id="code-end-mode:common-string-
1"></a>
4265 Otherwise, the string will be terminated by the same character that
4270 <a id="code-label-mode:common-string-
2"></a>
4271 <table style="width:
100%
" id="code-ref-mode:common-string-
2">
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 ⇑<a href="#code-ref-mode:common-string-
1">44a</a>, lang=<font color="blue
"></font> + ≡
4276 </div><div class="right-tab
">
4277 ▵<a href="#code-ref-mode:common-string-
1">44a</a> <a href="#code-ref-mode:common-string-
3">44c</a>▿
4282 <tt class="verbatim
"><div class="compact-block
">
4283 <tt>2 </tt>modes[${language}, ${quote},
4284 "terminators"]=${quote};
4287 <a id="code-end-mode:common-string-
2"></a>
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.
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.
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>.
4308 <a id="code-label-mode:common-string-
3"></a>
4309 <table style="width:
100%
" id="code-ref-mode:common-string-
3">
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 ⇑<a href="#code-ref-mode:common-string-
1">44a</a>, lang=<font color="blue
"></font> + ≡
4314 </div><div class="right-tab
">
4315 ▵<a href="#code-ref-mode:common-string-
2">44b</a> <a href="#code-ref-mode:common-string-
4">44d</a>▿
4321 <tt class="verbatim
"><div class="compact-block
">
4322 <tt>3 </tt>escapes[${language}, ${quote}, ++escapes[${language},
4323 ${quote}], "s"]="\\\\";
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}], "r"]="\\\\";
4335 <a id="code-end-mode:common-string-
3"></a>
4339 If the quote character occurs in the text, it should be preceded by a
4340 backslash, otherwise it would terminate the string unexpectedly.
4344 <a id="code-label-mode:common-string-
4"></a>
4345 <table style="width:
100%
" id="code-ref-mode:common-string-
4">
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 ⇑<a href="#code-ref-mode:common-string-
1">44a</a>, lang=<font color="blue
"></font> + ≡
4350 </div><div class="right-tab
">
4351 ▵<a href="#code-ref-mode:common-string-
3">44c</a> <a href="#code-ref-mode:common-string-
5">44e</a>▿
4357 <tt class="verbatim
"><div class="compact-block
">
4358 <tt>5 </tt>escapes[${language}, ${quote}, ++escapes[${language},
4359 ${quote}], "s"]=${quote};
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}], "r"]="\\" ${quote};
4371 <a id="code-end-mode:common-string-
4"></a>
4375 Any newlines in the string, must be replaced by <tt class="verbatim
">\n</tt>.
4379 <a id="code-label-mode:common-string-
5"></a>
4380 <table style="width:
100%
" id="code-ref-mode:common-string-
5">
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 ⇑<a href="#code-ref-mode:common-string-
1">44a</a>, lang=<font color="blue
"></font> + ≡
4385 </div><div class="right-tab
">
4386 ▵<a href="#code-ref-mode:common-string-
4">44d</a>
4392 <tt class="verbatim
"><div class="compact-block
">
4393 <tt>7 </tt>escapes[${language}, ${quote}, ++escapes[${language},
4394 ${quote}], "s"]="\n";
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}], "r"]="\\n";
4405 </p><table style="width:
100%
" id="code-end-mode:common-string-
5">
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>
4412 For the common modes, we define this string handling for double and
4417 <a id="code-label-common-mode-definitions-
4"></a>
4418 <table style="width:
100%
" id="code-ref-common-mode-definitions-
4">
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 ⇑<a href="#code-ref-common-mode-definitions-
1">43a</a>, lang=<font color="blue
"></font> + ≡
4423 </div><div class="right-tab
">
4424 ⊲<a href="#code-ref-common-mode-definitions-
3">43d</a> <a href="#code-ref-common-mode-definitions-
5">45b</a>⊳
4430 <tt class="verbatim
"><div class="compact-block
">
4431 <tt>4 </tt>=<\chunkref{mode:common-string}(${language},
4432 "\textbackslash{}"")>
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>=<\chunkref{mode:common-string}(${language},
4444 <a id="code-end-common-mode-definitions-
4"></a>
4448 Working strings should pass this test:
4452 <a id="code-label-test:mode-definitions-
2"></a>
4453 <table style="width:
100%
" id="code-ref-test:mode-definitions-
2">
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]() ⇑<a href="#code-ref-test:mode-definitions-
1">43c</a>,
4457 lang=<font color="blue
"></font> + ≡
4458 </div><div class="right-tab
">
4459 ⊲<a href="#code-ref-test:mode-definitions-
1">43c</a> <a href="#code-ref-test:mode-definitions-
3">47d</a>⊳
4465 <tt class="verbatim
"><div class="compact-block
">
4466 <tt>18 </tt>parse_chunk_args("c-like", "say
4467 \"I said, \\\"Hello, how are you\\\".\", for
4468 me", a, "");
4471 <pre class="verbatim
" xml:space="preserve
">
4472 <div class="compact-block
"><tt>19 </tt>if (a[1] != "say \"I said, \\\"Hello, how are you\\\".\"") e++;
4473 <tt>20 </tt>if (a[2] != "for me") e++;
4474 <tt>21 </tt>if (length(a) != 2) e++;</div></pre>
4476 <tt class="verbatim
"><div class="compact-block
">
4477 <tt>22 </tt>=<\chunkref{pca-test.awk:summary}>
4481 <a id="code-end-test:mode-definitions-
2"></a>
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.
4491 <a id="code-label-mode:common-brackets-
1"></a>
4492 <table style="width:
100%
" id="code-ref-mode:common-brackets-
1">
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> ≡</font></td>
4501 <tt class="verbatim
"><div class="compact-block
">
4502 <tt>1 </tt>modes[<font color="#
008000">language</font>, <font color="#
008000">open</font>,
4503 "submodes" ]="\\\\|\"|{|\\(|\\[|'|/\\*";
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>, "delimiters"]=" *, *";</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 "terminators"]=<font color="#
008000">close</font>;
4514 </p><table style="width:
100%
" id="code-end-mode:common-brackets-
1">
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>
4521 Note that the open is NOT a regex but the close token IS.
4522 <table style="display: inline; vertical-align: -
0.55em
">
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>
4531 <a id="code-label-common-mode-definitions-
5"></a>
4532 <table style="width:
100%
" id="code-ref-common-mode-definitions-
5">
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 ⇑<a href="#code-ref-common-mode-definitions-
1">43a</a>, lang=<font color="blue
"></font> + ≡
4537 </div><div class="right-tab
">
4538 ⊲<a href="#code-ref-common-mode-definitions-
4">44f</a>
4544 <tt class="verbatim
"><div class="compact-block
">
4545 <tt>6 </tt>=<\chunkref{mode:common-brackets}(${language},
4546 "{", "}")>
4549 <pre class="verbatim
" xml:space="preserve
">
4550 <div class="compact-block
"><tt>7 </tt>=<\chunkref{mode:common-brackets}(${language}, "[", "\textbackslash{}\textbackslash{}]")></div></pre>
4552 <tt class="verbatim
"><div class="compact-block
">
4553 <tt>8 </tt>=<\chunkref{mode:common-brackets}(${language},
4554 "(", "\textbackslash{}\textbackslash{})")>
4557 </p><table style="width:
100%
" id="code-end-common-mode-definitions-
5">
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>
4563 <h3 id="auto-
54">9.2.4<span style="margin-left:
1em
"></span>Customizing Standard Modes</h3>
4566 <a id="code-label-mode:add-submode-
1"></a>
4567 <table style="width:
100%
" id="code-ref-mode:add-submode-
1">
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> ≡</font></td>
4575 <tt class="verbatim
"><div class="compact-block
">
4576 <tt>1 </tt>modes[${language}, ${mode}, "submodes"] =
4577 modes[${language}, ${mode}, "submodes"] "|"
4580 </p><table style="width:
100%
" id="code-end-mode:add-submode-
1">
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>
4588 <a id="code-label-mode:add-escapes-
1"></a>
4589 <table style="width:
100%
" id="code-ref-mode:add-escapes-
1">
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>
4599 <tt class="verbatim
"><div class="compact-block
">
4600 <tt>1 </tt>escapes[${language}, ${mode}, ++escapes[${language},
4601 ${mode}], "s"]=${search};
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}], "r"]=${replace};
4612 </p><table style="width:
100%
" id="code-end-mode:add-escapes-
1">
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>
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:
4628 <a id="code-label-mode:multi-line-comments-
1"></a>
4629 <table style="width:
100%
" id="code-ref-mode:multi-line-comments-
1">
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>
4638 <tt class="verbatim
"><div class="compact-block
">
4639 <tt>1 </tt>=<\chunkref{mode:add-submode}(${language},
4640 "", "/\textbackslash{}\textbackslash{}*")>
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}, "/*",
4648 "terminators"]="\\*/";
4651 </p><table style="width:
100%
" id="code-end-mode:multi-line-comments-
1">
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>
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">
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> ≡</font></td>
4668 <tt class="verbatim
"><div class="compact-block
">
4669 <tt>1 </tt>=<\chunkref{mode:add-submode}(${language},
4670 "", "//")>
4673 <pre class="verbatim
" xml:space="preserve
">
4674 <div class="compact-block
"><tt>2 </tt>modes[${language}, "//", "terminators"]="\n";</div></pre>
4676 <tt class="verbatim
"><div class="compact-block
">
4677 <tt>3 </tt>=<\chunkref{mode:add-escapes}(${language},
4678 "//", "\textbackslash{}n",
4679 "\textbackslash{}n//")>
4682 </p><table style="width:
100%
" id="code-end-mode:single-line-slash-comments-
1">
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>
4689 We can also define <tt class="verbatim
"># comment</tt> style comments (as used in
4690 awk and shell scripts) in a similar manner.
4692 <table style="display: inline; vertical-align: -
0.55em
">
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 ¯extbackslash{} for and have hacky work-arounds in the parser
4701 <a id="code-label-mode:add-hash-comments-
1"></a>
4702 <table style="width:
100%
" id="code-ref-mode:add-hash-comments-
1">
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>
4711 <tt class="verbatim
"><div class="compact-block
">
4712 <tt>1 </tt>=<\chunkref{mode:add-submode}(${language},
4713 "", "\#")>
4716 <pre class="verbatim
" xml:space="preserve
">
4717 <div class="compact-block
"><tt>2 </tt>modes[${language}, "#", "terminators"]="\n";</div></pre>
4719 <tt class="verbatim
"><div class="compact-block
">
4720 <tt>3 </tt>=<\chunkref{mode:add-escapes}(${language},
4721 "\#", "\textbackslash{}n",
4722 "\textbackslash{}n\#")>
4725 </p><table style="width:
100%
" id="code-end-mode:add-hash-comments-
1">
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>
4732 In C, the <tt class="verbatim
">#</tt> denotes pre-processor directives which can
4737 <a id="code-label-mode:add-hash-defines-
1"></a>
4738 <table style="width:
100%
" id="code-ref-mode:add-hash-defines-
1">
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>
4747 <tt class="verbatim
"><div class="compact-block
">
4748 <tt>1 </tt>=<\chunkref{mode:add-submode}(${language},
4749 "", "\#")>
4752 <pre class="verbatim
" xml:space="preserve
">
4753 <div class="compact-block
"><tt>2 </tt>modes[${language}, "#", "submodes" ]="\\\\";
4754 <tt>3 </tt>modes[${language}, "#", "terminators"]="\n";</div></pre>
4756 <tt class="verbatim
"><div class="compact-block
">
4757 <tt>4 </tt>=<\chunkref{mode:add-escapes}(${language},
4758 "\#", "\textbackslash{}n",
4759 "\textbackslash{}\textbackslash{}\textbackslash{}\textbackslash{}\textbackslash{}n")>
4762 </p><table style="width:
100%
" id="code-end-mode:add-hash-defines-
1">
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>
4770 <a id="code-label-mode:quote-dollar-escape-
1"></a>
4771 <table style="width:
100%
" id="code-ref-mode:quote-dollar-escape-
1">
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> ≡</font></td>
4780 <tt class="verbatim
"><div class="compact-block
">
4781 <tt>1 </tt>escapes[${language}, ${quote}, ++escapes[${language},
4782 ${quote}], "s"]="\\$";
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}], "r"]="\\$";
4793 </p><table style="width:
100%
" id="code-end-mode:quote-dollar-escape-
1">
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>
4800 We can add these definitions to various languages
4804 <a id="code-label-mode-definitions-
1"></a>
4805 <table style="width:
100%
" id="code-ref-mode-definitions-
1">
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>
4810 </div><div class="right-tab
">
4811 <a href="#code-ref-mode-definitions-
2">47b</a>⊳
4817 <tt class="verbatim
"><div class="compact-block
">
4818 <tt>1 </tt>common-mode-definitions(<font color="blue
">"c-like"</font>)
4819 <a href="#code-ref-common-mode-definitions-
1">43a</a>
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
">"c"</font>) <a href="#code-ref-common-mode-definitions-
1">43a</a>
4825 <tt>4 </tt>=<\chunkref{mode:multi-line-comments}("c")>
4826 <tt>5 </tt>=<\chunkref{mode:single-line-slash-comments}("c")>
4827 <tt>6 </tt>=<\chunkref{mode:add-hash-defines}("c")>
4829 <tt>8 </tt>=<\chunkref{common-mode-definitions}("awk")>
4830 <tt>9 </tt>=<\chunkref{mode:add-hash-comments}("awk")></div></pre>
4832 <tt class="verbatim
"><div class="compact-block
">
4834 </tt>=<\chunkref{mode:add-naked-regex}("awk")>
4838 <a id="code-end-mode-definitions-
1"></a>
4842 The awk definitions should allow a comment block like this:
4846 <a id="code-label-test:comment-quote-
1"></a>
4847 <table style="width:
100%
" id="code-ref-test:comment-quote-
1">
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> ≡</font></td>
4854 <tt class="verbatim
"><div class="compact-block
">
4855 <tt>1 </tt># Comment: =<\chunkref{test:comment-text}>
4857 </p><table style="width:
100%
" id="code-end-test:comment-quote-
1">
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>
4865 <a id="code-label-test:comment-text-
1"></a>
4866 <table style="width:
100%
" id="code-ref-test:comment-text-
1">
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> ≡</font></td>
4874 <tt class="verbatim
"><div class="compact-block
">
4875 <tt>1 </tt>Now is the time for
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
4885 </p><table style="width:
100%
" id="code-end-test:comment-text-
1">
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>
4892 to come out like this:
4896 <a id="code-label-test:comment-quote:result-
1"></a>
4897 <table style="width:
100%
" id="code-ref-test:comment-quote:result-
1">
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> ≡</font></td>
4905 <tt class="verbatim
"><div class="compact-block
">
4906 <tt>1 </tt># Comment: Now is the time for
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
4916 </p><table style="width:
100%
" id="code-end-test:comment-quote:result-
1">
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>
4923 The C definition for such a block should have it come out like this:
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">
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> ≡</font></td>
4936 <tt class="verbatim
"><div class="compact-block
">
4937 <tt>1 </tt># Comment: Now is the time for\
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
4947 </p><table style="width:
100%
" id="code-end-test:comment-quote:C-result-
1">
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>
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.
4960 Current it only detects regexes anchored with ^ as used in fangle.
4963 For full regex support, modes need to be named not after their starting
4964 character, but some other more fully qualified name.
4968 <a id="code-label-mode:add-naked-regex-
1"></a>
4969 <table style="width:
100%
" id="code-ref-mode:add-naked-regex-
1">
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>
4978 <tt class="verbatim
"><div class="compact-block
">
4979 <tt>1 </tt>=<\chunkref{mode:add-submode}(${language},
4980 "", "/\textbackslash{}\textbackslash{}\^")>
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}, "/^",
4988 "terminators"]="/";
4991 </p><table style="width:
100%
" id="code-end-mode:add-naked-regex-
1">
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>
4997 <h3 id="auto-
57">9.2.7<span style="margin-left:
1em
"></span>Perl</h3>
5000 <a id="code-label-mode-definitions-
2"></a>
5001 <table style="width:
100%
" id="code-ref-mode-definitions-
2">
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]() ⇑<a href="#code-ref-mode-definitions-
1">46c</a>,
5005 lang=<font color="blue
"></font> + ≡
5006 </div><div class="right-tab
">
5007 ⊲<a href="#code-ref-mode-definitions-
1">46c</a> <a href="#code-ref-mode-definitions-
3">47c</a>▿
5013 <tt class="verbatim
"><div class="compact-block
">
5015 </tt>=<\chunkref{common-mode-definitions}("perl")>
5018 <pre class="verbatim
" xml:space="preserve
">
5019 <div class="compact-block
"><tt>12 </tt>=<\chunkref{mode:multi-line-comments}("perl")></div></pre>
5021 <tt class="verbatim
"><div class="compact-block
">
5023 </tt>=<\chunkref{mode:add-hash-comments}("perl")>
5027 <a id="code-end-mode-definitions-
2"></a>
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.
5035 <h3 id="auto-
58">9.2.8<span style="margin-left:
1em
"></span>sh</h3>
5038 <a id="code-label-mode-definitions-
3"></a>
5039 <table style="width:
100%
" id="code-ref-mode-definitions-
3">
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]() ⇑<a href="#code-ref-mode-definitions-
1">46c</a>,
5043 lang=<font color="blue
"></font> + ≡
5044 </div><div class="right-tab
">
5045 ▵<a href="#code-ref-mode-definitions-
2">47b</a>
5051 <tt class="verbatim
"><div class="compact-block
">
5053 </tt>=<\chunkref{common-mode-definitions}("sh")>
5056 <pre class="verbatim
" xml:space="preserve
">
5057 <div class="compact-block
"><tt>15 </tt>#<\chunkref{mode:common-string}("sh", "\textbackslash{}"")>
5058 <tt>16 </tt>#<\chunkref{mode:common-string}("sh", "'")>
5059 <tt>17 </tt>=<\chunkref{mode:add-hash-comments}("sh")></div></pre>
5061 <tt class="verbatim
"><div class="compact-block
">
5063 </tt>=<\chunkref{mode:quote-dollar-escape}("sh",
5064 "\"")>
5067 </p><table style="width:
100%
" id="code-end-mode-definitions-
3">
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>
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.
5080 <a id="code-label-test:mode-definitions-
3"></a>
5081 <table style="width:
100%
" id="code-ref-test:mode-definitions-
3">
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]() ⇑<a href="#code-ref-test:mode-definitions-
1">43c</a>,
5085 lang=<font color="blue
"></font> + ≡
5086 </div><div class="right-tab
">
5087 ⊲<a href="#code-ref-test:mode-definitions-
2">44g</a> <a href="#code-ref-test:mode-definitions-
4">47e</a>▿
5093 <tt class="verbatim
"><div class="compact-block
">
5094 <tt>23 </tt>rest = parse_chunk_args("c-like", "1,
5095 2, 3) spare", a, "(");
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 != " spare") e++;</div></pre>
5105 <tt class="verbatim
"><div class="compact-block
">
5106 <tt>29 </tt>=<\chunkref{pca-test.awk:summary}>
5110 <a id="code-end-test:mode-definitions-
3"></a>
5114 We must also be able to parse the example given earlier.
5118 <a id="code-label-test:mode-definitions-
4"></a>
5119 <table style="width:
100%
" id="code-ref-test:mode-definitions-
4">
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]() ⇑<a href="#code-ref-test:mode-definitions-
1">43c</a>,
5123 lang=<font color="blue
"></font> + ≡
5124 </div><div class="right-tab
">
5125 ▵<a href="#code-ref-test:mode-definitions-
3">47d</a>
5131 <tt class="verbatim
"><div class="compact-block
">
5132 <tt>30 </tt>parse_chunk_args("c-like", "things[x,
5133 y], get_other_things(a, \"(all)\"), 99", a,
5137 <pre class="verbatim
" xml:space="preserve
">
5138 <div class="compact-block
"><tt>31 </tt>if (a[1] != "things[x, y]") e++;
5139 <tt>32 </tt>if (a[2] != "get_other_things(a, \"(all)\")") e++;
5140 <tt>33 </tt>if (a[3] != "99") e++;
5141 <tt>34 </tt>if (length(a) != 3) e++;</div></pre>
5143 <tt class="verbatim
"><div class="compact-block
">
5144 <tt>35 </tt>=<\chunkref{pca-test.awk:summary}>
5147 </p><table style="width:
100%
" id="code-end-test:mode-definitions-
4">
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>
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
5162 <a id="code-label-new_mode_tracker()-
1"></a>
5163 <table style="width:
100%
" id="code-ref-new_mode_tracker()-
1">
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> ≡</font></td>
5171 <tt class="verbatim
"><div class="compact-block
">
5172 <tt>1 </tt>function new_mode_tracker(context, language, mode) {
5175 <pre class="verbatim
" xml:space="preserve
">
5176 <div class="compact-block
"><tt>2 </tt> context[""] = 0;
5177 <tt>3 </tt> context[0, "language"] = language;
5178 <tt>4 </tt> context[0, "mode"] = mode;</div></pre>
5180 <tt class="verbatim
"><div class="compact-block
">
5184 </p><table style="width:
100%
" id="code-end-new_mode_tracker()-
1">
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>
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:
5196 <a id="code-label-new-mode-tracker-
1"></a>
5197 <table style="width:
100%
" id="code-ref-new-mode-tracker-
1">
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> ≡</font></td>
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>
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});
5217 </p><table style="width:
100%
" id="code-end-new-mode-tracker-
1">
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>
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
5230 <a id="code-label-mode_tracker-
1"></a>
5231 <table style="width:
100%
" id="code-ref-mode_tracker-
1">
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>
5236 </div><div class="right-tab
">
5237 <a href="#code-ref-mode_tracker-
2">48d</a>▿
5243 <tt class="verbatim
"><div class="compact-block
">
5244 <tt>1 </tt>function push_mode_tracker(context, language, mode,
5247 <pre class="verbatim
" xml:space="preserve
">
5248 <div class="compact-block
"><tt>2 </tt> # local vars
5251 <tt>5 </tt> if (! ("" 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[""];
5255 <tt>9 </tt> if (context[top, "language"] == language && mode=="") mode = context[top, "mode"];
5257 <tt>11 </tt> context[top, "language"] = language;
5258 <tt>12 </tt> context[top, "mode"] = mode;
5259 <tt>13 </tt> context[""] = top;
5260 <tt>14 </tt> }</div></pre>
5262 <tt class="verbatim
"><div class="compact-block
">
5267 <a id="code-end-mode_tracker-
1"></a>
5272 <a id="code-label-mode_tracker-
2"></a>
5273 <table style="width:
100%
" id="code-ref-mode_tracker-
2">
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]() ⇑<a href="#code-ref-mode_tracker-
1">48c</a>,
5277 lang=<font color="blue
"></font> + ≡
5278 </div><div class="right-tab
">
5279 ▵<a href="#code-ref-mode_tracker-
1">48c</a> <a href="#code-ref-mode_tracker-
3">48e</a>▿
5285 <tt class="verbatim
"><div class="compact-block
">
5286 <tt>16 </tt>function dump_mode_tracker(context,
5289 <pre class="verbatim
" xml:space="preserve
">
5290 <div class="compact-block
"><tt>17 </tt> c, d)
5292 <tt>19 </tt> for(c=0; c <= context[""]; c++) {
5293 <tt>20 </tt> printf(" %2d %s:%s\n", c, context[c, "language"], context[c, "mode"]) > "/dev/stderr";
5294 <tt>21 </tt> for(d=1; ( (c, "values", d) in context); d++) {
5295 <tt>22 </tt> printf(" %2d %s\n", d, context[c, "values", d]) > "/dev/stderr";
5297 <tt>24 </tt> }</div></pre>
5299 <tt class="verbatim
"><div class="compact-block
">
5304 <a id="code-end-mode_tracker-
2"></a>
5309 <a id="code-label-mode_tracker-
3"></a>
5310 <table style="width:
100%
" id="code-ref-mode_tracker-
3">
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]() ⇑<a href="#code-ref-mode_tracker-
1">48c</a>,
5314 lang=<font color="blue
"></font> + ≡
5315 </div><div class="right-tab
">
5316 ▵<a href="#code-ref-mode_tracker-
2">48d</a> <a href="#code-ref-mode_tracker-
4">53a</a>⊳
5322 <tt class="verbatim
"><div class="compact-block
">
5323 <tt>26 </tt>function finalize_mode_tracker(context)
5326 <pre class="verbatim
" xml:space="preserve
">
5327 <div class="compact-block
"><tt>27 </tt>{
5328 <tt>28 </tt> if ( ("" in context) && context[""] != 0) return 0;
5329 <tt>29 </tt> return 1;</div></pre>
5331 <tt class="verbatim
"><div class="compact-block
">
5336 <a id="code-end-mode_tracker-
3"></a>
5340 This implies that any chunk must be syntactically whole; for instance,
5345 <a id="code-label-test:whole-chunk-
1"></a>
5346 <table style="width:
100%
" id="code-ref-test:whole-chunk-
1">
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> ≡</font></td>
5354 <tt class="verbatim
"><div class="compact-block
">
5358 <pre class="verbatim
" xml:space="preserve
">
5359 <div class="compact-block
"><tt>2 </tt> =<\chunkref{test:say-hello}></div></pre>
5361 <tt class="verbatim
"><div class="compact-block
">
5365 </p><table style="width:
100%
" id="code-end-test:whole-chunk-
1">
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>
5373 <a id="code-label-test:say-hello-
1"></a>
5374 <table style="width:
100%
" id="code-ref-test:say-hello-
1">
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> ≡</font></td>
5381 <tt class="verbatim
"><div class="compact-block
">
5382 <tt>1 </tt>print "hello";
5384 </p><table style="width:
100%
" id="code-end-test:say-hello-
1">
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>
5391 But this is not fine; the chunk test:hidden-else <a href="#code-ref-test:hidden-else-
1">49d</a> is not
5396 <a id="code-label-test:partial-chunk-
1"></a>
5397 <table style="width:
100%
" id="code-ref-test:partial-chunk-
1">
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> ≡</font></td>
5405 <tt class="verbatim
"><div class="compact-block
">
5409 <pre class="verbatim
" xml:space="preserve
">
5410 <div class="compact-block
"><tt>2 </tt> =<\chunkref{test:hidden-else}></div></pre>
5412 <tt class="verbatim
"><div class="compact-block
">
5416 </p><table style="width:
100%
" id="code-end-test:partial-chunk-
1">
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>
5424 <a id="code-label-test:hidden-else-
1"></a>
5425 <table style="width:
100%
" id="code-ref-test:hidden-else-
1">
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> ≡</font></td>
5433 <tt class="verbatim
"><div class="compact-block
">
5434 <tt>1 </tt> print "I'm fine";
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 "I'm not";
5444 </p><table style="width:
100%
" id="code-end-test:hidden-else-
1">
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>
5451 These tests will check for correct behaviour:
5455 <a id="code-label-test:cromulence-
1"></a>
5456 <table style="width:
100%
" id="code-ref-test:cromulence-
1">
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> ≡</font></td>
5464 <tt class="verbatim
"><div class="compact-block
">
5465 <tt>1 </tt>echo Cromulence test
5468 <pre class="verbatim
" xml:space="preserve
">
5469 <div class="compact-block
"><tt>2 </tt>passtest $FANGLE -Rtest:whole-chunk $TEX_SRC &>/dev/null || ( echo "Whole chunk failed" && exit 1 )</div></pre>
5471 <tt class="verbatim
"><div class="compact-block
">
5472 <tt>3 </tt>failtest $FANGLE -Rtest:partial-chunk $TEX_SRC
5473 &>/dev/null || ( echo "Partial chunk failed"
5477 </p><table style="width:
100%
" id="code-end-test:cromulence-
1">
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>
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.
5492 <a id="code-label-mode_tracker()-
1"></a>
5493 <table style="width:
100%
" id="code-ref-mode_tracker()-
1">
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>
5498 </div><div class="right-tab
">
5499 <a href="#code-ref-mode_tracker()-
2">49g</a>▿
5505 <tt class="verbatim
"><div class="compact-block
">
5506 <tt>1 </tt>function mode_tracker(context, text, values,
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
">
5521 <a id="code-end-mode_tracker()-
1"></a>
5525 We could be re-commencing with a valid context, so we need to setup the
5526 state according to the last context.
5530 <a id="code-label-mode_tracker()-
2"></a>
5531 <table style="width:
100%
" id="code-ref-mode_tracker()-
2">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5535 lang=<font color="blue
"></font> + ≡
5536 </div><div class="right-tab
">
5537 ▵<a href="#code-ref-mode_tracker()-
1">49f</a> <a href="#code-ref-mode_tracker()-
3">50c</a>⊳
5543 <tt class="verbatim
"><div class="compact-block
">
5544 <tt>8 </tt> cindex = context[""] + 0;
5547 <pre class="verbatim
" xml:space="preserve
">
5548 <div class="compact-block
"><tt>9 </tt> mode = context[cindex, "mode"];</div></pre>
5550 <tt class="verbatim
"><div class="compact-block
">
5551 <tt>10 </tt> language = context[cindex, "language" ];
5555 <a id="code-end-mode_tracker()-
2"></a>
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.
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">
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> ≡
5570 </div><div class="right-tab
">
5571 <a href="#code-ref-parse_chunk_args-reset-modes-
2">50b</a>▿
5577 <tt class="verbatim
"><div class="compact-block
">
5578 <tt>1 </tt> submodes=modes[language, mode,
5579 "submodes"];
5582 <pre class="verbatim
" xml:space="preserve
">
5583 <div class="compact-block
"><tt>2 </tt>
5584 <tt>3 </tt> if ((language, mode, "delimiters") in modes) {
5585 <tt>4 </tt> delimiters = modes[language, mode, "delimiters"];
5586 <tt>5 </tt> if (length(submodes)>0) submodes = submodes "|";
5587 <tt>6 </tt> submodes=submodes delimiters;
5588 <tt>7 </tt> } else delimiters="";
5589 <tt>8 </tt> if ((language, mode, "terminators") in modes) {
5590 <tt>9 </tt> terminators = modes[language, mode, "terminators"];
5591 <tt>10 </tt> if (length(submodes)>0) submodes = submodes "|";
5592 <tt>11 </tt> submodes=submodes terminators;</div></pre>
5594 <tt class="verbatim
"><div class="compact-block
">
5595 <tt>12 </tt> } else terminators="";
5599 <a id="code-end-parse_chunk_args-reset-modes-
1"></a>
5603 If we don't find anything to match on –- probably because the
5604 language is not supported –- then we return the entire text
5605 without matching anything.
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">
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]() ⇑<a
5614 href="#code-ref-parse_chunk_args-reset-modes-
1">50a</a>, lang=<font color="blue
"></font> + ≡
5615 </div><div class="right-tab
">
5616 ▵<a href="#code-ref-parse_chunk_args-reset-modes-
1">50a</a>
5621 <tt class="verbatim
"><div class="compact-block
">
5622 <tt>13 </tt> if (! length(submodes)) return text;
5624 </p><table style="width:
100%
" id="code-end-parse_chunk_args-reset-modes-
2">
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>
5632 <a id="code-label-mode_tracker()-
3"></a>
5633 <table style="width:
100%
" id="code-ref-mode_tracker()-
3">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5637 lang=<font color="blue
"></font> + ≡
5638 </div><div class="right-tab
">
5639 ⊲<a href="#code-ref-mode_tracker()-
2">49g</a> <a href="#code-ref-mode_tracker()-
4">50d</a>▿
5644 <tt class="verbatim
"><div class="compact-block
">
5645 <tt>11 </tt>=<\chunkref{parse_chunk_args-reset-modes}>
5648 <a id="code-end-mode_tracker()-
3"></a>
5652 We then iterate the text (until there is none left) looking for
5653 sub-modes or terminators in the regex.
5657 <a id="code-label-mode_tracker()-
4"></a>
5658 <table style="width:
100%
" id="code-ref-mode_tracker()-
4">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5662 lang=<font color="blue
"></font> + ≡
5663 </div><div class="right-tab
">
5664 ▵<a href="#code-ref-mode_tracker()-
3">50c</a> <a href="#code-ref-mode_tracker()-
5">50e</a>▿
5670 <tt class="verbatim
"><div class="compact-block
">
5671 <tt>12 </tt> while((cindex >= 0) && length(text)) {
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, "(" submodes
5679 ")", a)) {
5683 <a id="code-end-mode_tracker()-
4"></a>
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.
5693 <a id="code-label-mode_tracker()-
5"></a>
5694 <table style="width:
100%
" id="code-ref-mode_tracker()-
5">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5698 lang=<font color="blue
"></font> + ≡
5699 </div><div class="right-tab
">
5700 ▵<a href="#code-ref-mode_tracker()-
4">50d</a> <a href="#code-ref-mode_tracker()-
6">50f</a>▿
5706 <tt class="verbatim
"><div class="compact-block
">
5707 <tt>14 </tt> if (RLENGTH<1) {
5710 <pre class="verbatim
" xml:space="preserve
">
5711 <div class="compact-block
"><tt>15 </tt> error(sprintf("Internal error, matched zero length submode, should be impossible - likely regex computation error\n" \
5712 <tt>16 </tt> "Language=%s\nmode=%s\nmatch=%s\n", language, mode, submodes));</div></pre>
5714 <tt class="verbatim
"><div class="compact-block
">
5719 <a id="code-end-mode_tracker()-
5"></a>
5723 part is defined as the text up to the sub-mode or terminator, and this
5724 is appended to item –- which is the current text being gathered.
5725 If a mode has a delimiter, then item is reset each time a delimiter is
5729 (<tt class="verbatim
">"</tt><sub>item</sub>, <sub>item</sub><tt class="verbatim
">"</tt>)<sup>&wide-overbrace;</sup><sup>item</sup>,
5730 he said.<sup class="wide
">&wide-overbrace;</sup><sup>item</sup>
5734 <a id="code-label-mode_tracker()-
6"></a>
5735 <table style="width:
100%
" id="code-ref-mode_tracker()-
6">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5739 lang=<font color="blue
"></font> + ≡
5740 </div><div class="right-tab
">
5741 ▵<a href="#code-ref-mode_tracker()-
5">50e</a> <a href="#code-ref-mode_tracker()-
7">50g</a>▿
5747 <tt class="verbatim
"><div class="compact-block
">
5748 <tt>18 </tt> part = substr(text, 1, RSTART -1);
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;
5759 <a id="code-end-mode_tracker()-
6"></a>
5763 We must now determine what was matched. If it was a terminator, then we
5764 must restore the previous mode.
5768 <a id="code-label-mode_tracker()-
7"></a>
5769 <table style="width:
100%
" id="code-ref-mode_tracker()-
7">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5773 lang=<font color="blue
"></font> + ≡
5774 </div><div class="right-tab
">
5775 ▵<a href="#code-ref-mode_tracker()-
6">50f</a> <a href="#code-ref-mode_tracker()-
8">51a</a>⊳
5781 <tt class="verbatim
"><div class="compact-block
">
5782 <tt>20 </tt> if (match(a[1], "^" terminators
5786 <pre class="verbatim
" xml:space="preserve
">
5787 <div class="compact-block
"><tt>21 </tt>#printf("%2d EXIT MODE [%s] by [%s] [%s]\n", cindex, mode, a[1], text) > "/dev/stderr"
5788 <tt>22 </tt> context[cindex, "values", ++context[cindex, "values"]] = item;
5789 <tt>23 </tt> delete context[cindex];
5790 <tt>24 </tt> context[""] = --cindex;
5791 <tt>25 </tt> if (cindex>=0) {
5792 <tt>26 </tt> mode = context[cindex, "mode"];
5793 <tt>27 </tt> language = context[cindex, "language"];
5794 <tt>28 </tt> =<\chunkref{parse_chunk_args-reset-modes}>
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
">
5804 <a id="code-end-mode_tracker()-
7"></a>
5808 If a delimiter was matched, then we must store the current item in the
5809 parsed values array, and reset the item.
5813 <a id="code-label-mode_tracker()-
8"></a>
5814 <table style="width:
100%
" id="code-ref-mode_tracker()-
8">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5818 lang=<font color="blue
"></font> + ≡
5819 </div><div class="right-tab
">
5820 ⊲<a href="#code-ref-mode_tracker()-
7">50g</a> <a href="#code-ref-mode_tracker()-
9">51b</a>▿
5826 <tt class="verbatim
"><div class="compact-block
">
5827 <tt>33 </tt> else if (match(a[1], "^" delimiters
5831 <pre class="verbatim
" xml:space="preserve
">
5832 <div class="compact-block
"><tt>34 </tt> if (cindex==0) {
5833 <tt>35 </tt> context[cindex, "values", ++context[cindex, "values"]] = item;
5834 <tt>36 </tt> item = "";
5835 <tt>37 </tt> } else {
5836 <tt>38 </tt> item = item a[1];
5838 <tt>40 </tt> text = substr(text, 1 + length(part) + length(a[1]));</div></pre>
5840 <tt class="verbatim
"><div class="compact-block
">
5845 <a id="code-end-mode_tracker()-
8"></a>
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
5855 <a id="code-label-mode_tracker()-
9"></a>
5856 <table style="width:
100%
" id="code-ref-mode_tracker()-
9">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5860 lang=<font color="blue
"></font> + ≡
5861 </div><div class="right-tab
">
5862 ▵<a href="#code-ref-mode_tracker()-
8">51a</a> <a href="#code-ref-mode_tracker()-
10">51c</a>▿
5868 <tt class="verbatim
"><div class="compact-block
">
5869 <tt>42 </tt> else if ((language, a[1], "terminators")
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("%2d ENTER MODE [%s] in [%s]\n", cindex, a[1], text) > "/dev/stderr"
5877 <tt>46 </tt> text = substr(text, 1 + length(part) + length(a[1]));
5878 <tt>47 </tt> context[""] = ++cindex;
5879 <tt>48 </tt> context[cindex, "mode"] = a[1];
5880 <tt>49 </tt> context[cindex, "language"] = language;
5881 <tt>50 </tt> mode = a[1];
5882 <tt>51 </tt> =<\chunkref{parse_chunk_args-reset-modes}>
5883 <tt>52 </tt> } else {
5884 <tt>53 </tt> error(sprintf("Submode '%s' set unknown mode in text: %s\nLanguage %s Mode %s\n", 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
">
5893 <a id="code-end-mode_tracker()-
9"></a>
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.
5905 <a id="code-label-mode_tracker()-
10"></a>
5906 <table style="width:
100%
" id="code-ref-mode_tracker()-
10">
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]() ⇑<a href="#code-ref-mode_tracker()-
1">49f</a>,
5910 lang=<font color="blue
"></font> + ≡
5911 </div><div class="right-tab
">
5912 ▵<a href="#code-ref-mode_tracker()-
9">51b</a>
5918 <tt class="verbatim
"><div class="compact-block
">
5922 <pre class="verbatim
" xml:space="preserve
">
5923 <div class="compact-block
"><tt>58 </tt> context[cindex, "values", ++context[cindex, "values"]] = item text;
5924 <tt>59 </tt> text = "";
5925 <tt>60 </tt> item = "";
5929 <tt>64 </tt> context["item"] = item;
5931 <tt>66 </tt> if (length(item)) context[cindex, "values", ++context[cindex, "values"]] = item;
5932 <tt>67 </tt> return text;</div></pre>
5934 <tt class="verbatim
"><div class="compact-block
">
5938 </p><table style="width:
100%
" id="code-end-mode_tracker()-
10">
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>
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:
5950 <a id="code-label-mode-tracker-
1"></a>
5951 <table style="width:
100%
" id="code-ref-mode-tracker-
1">
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> ≡</font></td>
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>
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>
5970 </p><table style="width:
100%
" id="code-end-mode-tracker-
1">
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>
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:
5982 <a id="code-label-pca-test.awk-
1"></a>
5983 <table style="width:
100%
" id="code-ref-pca-test.awk-
1">
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> ≡</font></td>
5991 <tt class="verbatim
"><div class="compact-block
">
5992 <tt>1 </tt>=<\chunkref{error()}>
5995 <pre class="verbatim
" xml:space="preserve
">
5996 <div class="compact-block
"><tt>2 </tt>=<\chunkref{mode-tracker}>
5997 <tt>3 </tt>=<\chunkref{parse_chunk_args()}>
5999 <tt>5 </tt> SUBSEP=".";
6000 <tt>6 </tt> =<\chunkref{mode-definitions}>
6002 <tt>8 </tt> =<\chunkref{test:mode-definitions}></div></pre>
6004 <tt class="verbatim
"><div class="compact-block
">
6008 </p><table style="width:
100%
" id="code-end-pca-test.awk-
1">
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>
6016 <a id="code-label-pca-test.awk:summary-
1"></a>
6017 <table style="width:
100%
" id="code-ref-pca-test.awk:summary-
1">
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> ≡</font></td>
6025 <tt class="verbatim
"><div class="compact-block
">
6029 <pre class="verbatim
" xml:space="preserve
">
6030 <div class="compact-block
"><tt>2 </tt> printf "Failed " e
6031 <tt>3 </tt> for (b in a) {
6032 <tt>4 </tt> print "a[" b "] => " a[b];
6035 <tt>7 </tt> print "Passed"
6037 <tt>9 </tt>split("", a);</div></pre>
6039 <tt class="verbatim
"><div class="compact-block
">
6043 </p><table style="width:
100%
" id="code-end-pca-test.awk:summary-
1">
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>
6050 which should give this output:
6054 <a id="code-label-pca-test.awk-results-
1"></a>
6055 <table style="width:
100%
" id="code-ref-pca-test.awk-results-
1">
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> ≡</font></td>
6063 <tt class="verbatim
"><div class="compact-block
">
6064 <tt>1 </tt>a[foo.quux.quirk] =>
6067 <pre class="verbatim
" xml:space="preserve
">
6068 <div class="compact-block
"><tt>2 </tt>a[foo.quux.a] => fleeg
6069 <tt>3 </tt>a[foo.bar] => baz
6070 <tt>4 </tt>a[etc] => </div></pre>
6072 <tt class="verbatim
"><div class="compact-block
">
6073 <tt>5 </tt>a[name] => freddie
6076 </p><table style="width:
100%
" id="code-end-pca-test.awk-results-
1">
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>
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 ...
6087 <table style="display: inline; vertical-align: -
0.55em
">
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>
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
">—</class>
6096 this is important for bash here documents, and the like. It's a filthy
6099 <table style="display: inline; vertical-align: -
0.55em
">
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>
6106 <a id="code-label-mode_tracker-
4"></a>
6107 <table style="width:
100%
" id="code-ref-mode_tracker-
4">
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]() ⇑<a href="#code-ref-mode_tracker-
1">48c</a>,
6111 lang=<font color="blue
"></font> + ≡
6112 </div><div class="right-tab
">
6113 ⊲<a href="#code-ref-mode_tracker-
3">48e</a> <a href="#code-ref-mode_tracker-
5">53b</a>▿
6119 <tt class="verbatim
"><div class="compact-block
">
6123 <pre class="verbatim
" xml:space="preserve
">
6124 <div class="compact-block
"><tt>31 </tt>function untab(text) {
6125 <tt>32 </tt> gsub("[[:space:]]*\xE2\x86\xA4","", text);
6126 <tt>33 </tt> return text;</div></pre>
6128 <tt class="verbatim
"><div class="compact-block
">
6133 <a id="code-end-mode_tracker-
4"></a>
6137 Each nested mode can optionally define a set of transforms to be applied
6138 to any text that is included from another language.
6141 This code can perform transforms
6145 <a id="code-label-mode_tracker-
5"></a>
6146 <table style="width:
100%
" id="code-ref-mode_tracker-
5">
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]() ⇑<a href="#code-ref-mode_tracker-
1">48c</a>,
6150 lang=<font color="blue
">awk</font> + ≡
6151 </div><div class="right-tab
">
6152 ▵<a href="#code-ref-mode_tracker-
4">53a</a> <a href="#code-ref-mode_tracker-
6">53c</a>▿
6158 <tt class="verbatim
"><div class="compact-block
">
6159 <tt>35 </tt>function transform_escape(s, r, text,
6162 <pre class="verbatim
" xml:space="preserve
">
6163 <div class="compact-block
"><tt>36 </tt> # optional
6165 <tt>38 </tt> # local vars
6168 <tt>41 </tt> for(c=1; c <= max && (c in s); c++) {
6169 <tt>42 </tt> gsub(s[c], r[c], text);
6171 <tt>44 </tt> return text;</div></pre>
6173 <tt class="verbatim
"><div class="compact-block
">
6178 <a id="code-end-mode_tracker-
5"></a>
6182 This function must append from index c onwards, and escape transforms
6183 from the supplied context, and return c + number of new transforms.
6187 <a id="code-label-mode_tracker-
6"></a>
6188 <table style="width:
100%
" id="code-ref-mode_tracker-
6">
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]() ⇑<a href="#code-ref-mode_tracker-
1">48c</a>,
6192 lang=<font color="blue
">awk</font> + ≡
6193 </div><div class="right-tab
">
6194 ▵<a href="#code-ref-mode_tracker-
5">53b</a>
6200 <tt class="verbatim
"><div class="compact-block
">
6201 <tt>46 </tt>function mode_escaper(context, s, r, src,
6204 <pre class="verbatim
" xml:space="preserve
">
6205 <div class="compact-block
"><tt>47 </tt> c, cp, cpl)
6207 <tt>49 </tt> for(c = context[""]; c >= 0; c--) {
6208 <tt>50 </tt> if ( (context[c, "language"], context[c, "mode"]) in escapes) {
6209 <tt>51 </tt> cpl = escapes[context[c, "language"], context[c, "mode"]];
6210 <tt>52 </tt> for (cp = 1; cp <= cpl; cp ++) {
6212 <tt>54 </tt> s[src] = escapes[context[c, "language"], context[c, "mode"], cp, "s"];
6213 <tt>55 </tt> r[src] = escapes[context[c, "language"], context[c, "mode"], cp, "r"];
6217 <tt>59 </tt> return src;
6219 <tt>61 </tt>function dump_escaper(c, s, r, cc) {
6220 <tt>62 </tt> for(cc=1; cc<=c; cc++) {
6221 <tt>63 </tt> printf("%2d s[%s] r[%s]\n", cc, s[cc], r[cc]) > "/dev/stderr"
6222 <tt>64 </tt> }</div></pre>
6224 <tt class="verbatim
"><div class="compact-block
">
6228 </p><table style="width:
100%
" id="code-end-mode_tracker-
6">
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>
6236 <a id="code-label-test:escapes-
1"></a>
6237 <table style="width:
100%
" id="code-ref-test:escapes-
1">
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> ≡</font></td>
6245 <tt class="verbatim
"><div class="compact-block
">
6246 <tt>1 </tt>echo escapes test
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 &>/dev/null || ( echo "Comment-quote failed"
6258 </p><table style="width:
100%
" id="code-end-test:escapes-
1">
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>
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:
6271 notangle chunks matching the pattern <tt class="verbatim
">^<<.*?>>=</tt>
6274 chunks beginning with <tt class="verbatim
">\begin{lstlistings}</tt>, possibly
6275 with <tt class="verbatim
">\Chunk{...}</tt> on the previous line
6278 an older form I have used, beginning with
6279 <tt class="verbatim
">\begin{Chunk}[options]</tt>
6280 –- 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?
6292 <span style="margin-left:
0em
"></span>
6293 <a id="footnr-
1"></a>
6294 <sup><a href="#footnote-
1">1</a></sup>
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.
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.
6310 These hacks detect such sequences and retro-fit in the old TeX parsing.
6314 <a id="code-label-recognize-chunk-
1"></a>
6315 <table style="width:
100%
" id="code-ref-recognize-chunk-
1">
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>
6320 </div><div class="right-tab
">
6321 <a href="#code-ref-recognize-chunk-
2">56a</a>⊳
6327 <tt class="verbatim
"><div class="compact-block
">
6331 <pre class="verbatim
" xml:space="preserve
">
6332 <div class="compact-block
"><tt>1 </tt>#/\n/ {
6333 <tt>2 </tt># gsub("\n*$","");
6334 <tt>3 </tt># gsub("\n", " ");
6337 <tt>6 </tt>/\xE2\x86\xA6/ {
6338 <tt>7 </tt> gsub("\\xE2\\x86\\xA6", "\x09");
6341 <tt>10 </tt>/\xE2\x80\x98/ {
6342 <tt>11 </tt> gsub("\\xE2\\x80\\x98", "‘");
6345 <tt>14 </tt>/\xE2\x89\xA1/ {
6346 <tt>15 </tt> if (match($0, "^ *([^[ ]* |)<([^[ ]*)\\[[0-9]*\\][(](.*)[)].*, lang=([^ ]*)", line)) {
6347 <tt>16 </tt> next_chunk_name=line[2];
6348 <tt>17 </tt> gsub(",",";",line[3]);
6349 <tt>18 </tt> params="params=" line[3];
6350 <tt>19 </tt> if ((line[4])) {
6351 <tt>20 </tt> params = params ",language=" line[4]
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 "Unexpected
6362 <tt>31 </tt>}</div></pre>
6364 <tt class="verbatim
"><div class="compact-block
">
6369 <a id="code-end-recognize-chunk-
1"></a>
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.
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>).
6389 <a id="code-label-recognize-chunk-
2"></a>
6390 <table style="width:
100%
" id="code-ref-recognize-chunk-
2">
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]() ⇑<a href="#code-ref-recognize-chunk-
1">55a</a>,
6394 lang=<font color="blue
">awk</font> + ≡
6395 </div><div class="right-tab
">
6396 ⊲<a href="#code-ref-recognize-chunk-
1">55a</a> <a href="#code-ref-recognize-chunk-
3">56b</a>▿
6402 <tt class="verbatim
"><div class="compact-block
">
6403 <tt>33 </tt>/^\\Chunk{/ {
6406 <pre class="verbatim
" xml:space="preserve
">
6407 <div class="compact-block
"><tt>34 </tt> if (match($0, "^\\\\Chunk{ *([^ ,}]*),?(.*)}", line)) {
6408 <tt>35 </tt> next_chunk_name = line[1];
6409 <tt>36 </tt> get_chunk_args(line[2], next_chunk_args);
6411 <tt>38 </tt> next;</div></pre>
6413 <tt class="verbatim
"><div class="compact-block
">
6418 <a id="code-end-recognize-chunk-
2"></a>
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
6440 <span style="margin-left:
0em
"></span>
6441 <a id="footnr-
2"></a>
6442 <sup><a href="#footnote-
2">2</a></sup>
6447 <a id="code-label-recognize-chunk-
3"></a>
6448 <table style="width:
100%
" id="code-ref-recognize-chunk-
3">
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]() ⇑<a href="#code-ref-recognize-chunk-
1">55a</a>,
6452 lang=<font color="blue
"></font> + ≡
6453 </div><div class="right-tab
">
6454 ▵<a href="#code-ref-recognize-chunk-
2">56a</a> <a href="#code-ref-recognize-chunk-
4">56c</a>▿
6460 <tt class="verbatim
"><div class="compact-block
">
6461 <tt>40 </tt>/^\\begin{lstlisting}|^\\begin{Chunk}/ {
6464 <pre class="verbatim
" xml:space="preserve
">
6465 <div class="compact-block
"><tt>41 </tt> if (match($0, "}.*[[,] *name= *{? *([^], }]*)", 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);
6470 <tt>46 </tt> chunking=1;
6471 <tt>47 </tt> next;</div></pre>
6473 <tt class="verbatim
"><div class="compact-block
">
6478 <a id="code-end-recognize-chunk-
3"></a>
6481 <h3 id="auto-
71">10.1.3<span style="margin-left:
1em
"></span>TeXmacs</h3>
6487 <a id="code-label-recognize-chunk-
4"></a>
6488 <table style="width:
100%
" id="code-ref-recognize-chunk-
4">
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]() ⇑<a href="#code-ref-recognize-chunk-
1">55a</a>,
6492 lang=<font color="blue
"></font> + ≡
6493 </div><div class="right-tab
">
6494 ▵<a href="#code-ref-recognize-chunk-
3">56b</a> <a href="#code-ref-recognize-chunk-
5">57a</a>⊳
6500 <tt class="verbatim
"><div class="compact-block
">
6504 <pre class="verbatim
" xml:space="preserve
">
6505 <div class="compact-block
"><tt>50 </tt>/^ *\|____________*/ && texmacs_chunking {
6506 <tt>51 </tt> active_chunk="";
6507 <tt>52 </tt> texmacs_chunking=0;
6508 <tt>53 </tt> chunking=0;
6510 <tt>55 </tt>/^ *\|\/\\/ && texmacs_chunking {
6511 <tt>56 </tt> texmacs_chunking=0;
6512 <tt>57 </tt> chunking=0;
6513 <tt>58 </tt> active_chunk="";
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("^ *[1-9][0-9]* *\\| ", "")
6523 <tt>68 </tt>/^ *\.\/\\/ && texmacs_chunking {
6526 <tt>71 </tt>/^ *__*$/ && texmacs_chunking {
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;
6537 <tt>82 </tt>! texmacs_chunk {
6538 <tt>83 </tt># texmacs_chunking=0;
6539 <tt>84 </tt> chunking=0;
6541 <tt>86 </tt></div></pre>
6543 <tt class="verbatim
"><div class="compact-block
">
6548 <a id="code-end-recognize-chunk-
4"></a>
6551 <h3 id="auto-
72">10.1.4<span style="margin-left:
1em
"></span>Noweb</h3>
6553 We recognize notangle style chunks too:
6557 <a id="code-label-recognize-chunk-
5"></a>
6558 <table style="width:
100%
" id="code-ref-recognize-chunk-
5">
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]() ⇑<a href="#code-ref-recognize-chunk-
1">55a</a>,
6562 lang=<font color="blue
">awk</font> + ≡
6563 </div><div class="right-tab
">
6564 ⊲<a href="#code-ref-recognize-chunk-
4">56c</a> <a href="#code-ref-recognize-chunk-
6">58a</a>⊳
6570 <tt class="verbatim
"><div class="compact-block
">
6571 <tt>88 </tt>/^[<]<.*[>]>=/ {
6574 <pre class="verbatim
" xml:space="preserve
">
6575 <div class="compact-block
"><tt>89 </tt> if (match($0, "^[<]<(.*)[>]>= *$", line)) {
6576 <tt>90 </tt> chunking=1;
6577 <tt>91 </tt> notangle_mode=1;
6578 <tt>92 </tt> new_chunk(line[1]);
6580 <tt>94 </tt> }</div></pre>
6582 <tt class="verbatim
"><div class="compact-block
">
6587 <a id="code-end-recognize-chunk-
5"></a>
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.
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!
6615 <span style="margin-left:
0em
"></span>
6616 <a id="footnr-
3"></a>
6617 <sup><a href="#footnote-
3">3</a></sup>
6621 <a id="code-label-recognize-chunk-
6"></a>
6622 <table style="width:
100%
" id="code-ref-recognize-chunk-
6">
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]() ⇑<a href="#code-ref-recognize-chunk-
1">55a</a>,
6626 lang=<font color="blue
"></font> + ≡
6627 </div><div class="right-tab
">
6628 ⊲<a href="#code-ref-recognize-chunk-
5">57a</a> <a href="#code-ref-recognize-chunk-
7">58b</a>▿
6634 <tt class="verbatim
"><div class="compact-block
">
6635 <tt>96 </tt>/^\\[e]nd{lstlisting}|^\\[e]nd{Chunk}/ {
6638 <pre class="verbatim
" xml:space="preserve
">
6639 <div class="compact-block
"><tt>97 </tt> chunking=0;
6640 <tt>98 </tt> active_chunk="";
6641 <tt>99 </tt> next;</div></pre>
6643 <tt class="verbatim
"><div class="compact-block
">
6648 <a id="code-end-recognize-chunk-
6"></a>
6651 <h3 id="auto-
75">10.2.2<span style="margin-left:
1em
"></span>noweb</h3>
6654 <a id="code-label-recognize-chunk-
7"></a>
6655 <table style="width:
100%
" id="code-ref-recognize-chunk-
7">
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]() ⇑<a href="#code-ref-recognize-chunk-
1">55a</a>,
6659 lang=<font color="blue
"></font> + ≡
6660 </div><div class="right-tab
">
6661 ▵<a href="#code-ref-recognize-chunk-
6">58a</a> <a href="#code-ref-recognize-chunk-
8">58c</a>▿
6667 <tt class="verbatim
"><div class="compact-block
">
6668 <tt>101 </tt>/^@ *$/ {
6671 <pre class="verbatim
" xml:space="preserve
">
6672 <div class="compact-block
"><tt>102 </tt> chunking=0;
6673 <tt>103 </tt> active_chunk="";</div></pre>
6675 <tt class="verbatim
"><div class="compact-block
">
6680 <a id="code-end-recognize-chunk-
7"></a>
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.
6690 <a id="code-label-recognize-chunk-
8"></a>
6691 <table style="width:
100%
" id="code-ref-recognize-chunk-
8">
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]() ⇑<a href="#code-ref-recognize-chunk-
1">55a</a>,
6695 lang=<font color="blue
"></font> + ≡
6696 </div><div class="right-tab
">
6697 ▵<a href="#code-ref-recognize-chunk-
7">58b</a> <a href="#code-ref-recognize-chunk-
9">58d</a>▿
6702 <tt class="verbatim
"><div class="compact-block
">
6703 <tt>105 </tt>! chunking { next; }
6706 <a id="code-end-recognize-chunk-
8"></a>
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
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>
6731 <tt class="verbatim
">printf</tt>
6733 <table style="display: inline; vertical-align: -
0.55em
">
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>
6743 <span style="margin-left:
0em
"></span>
6744 <a id="footnr-
4"></a>
6745 <sup><a href="#footnote-
4">4</a></sup>
6750 <a id="code-label-recognize-chunk-
9"></a>
6751 <table style="width:
100%
" id="code-ref-recognize-chunk-
9">
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]() ⇑<a href="#code-ref-recognize-chunk-
1">55a</a>,
6755 lang=<font color="blue
"></font> + ≡
6756 </div><div class="right-tab
">
6757 ▵<a href="#code-ref-recognize-chunk-
8">58c</a>
6763 <tt class="verbatim
"><div class="compact-block
">
6764 <tt>106 </tt>length(active_chunk) {
6767 <pre class="verbatim
" xml:space="preserve
">
6768 <div class="compact-block
"><tt>107 </tt> =<\chunkref{process-chunk-tabs}>
6769 <tt>108 </tt> =<\chunkref{process-chunk}></div></pre>
6771 <tt class="verbatim
"><div class="compact-block
">
6775 </p><table style="width:
100%
" id="code-end-recognize-chunk-
9">
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>
6782 If a chunk just consisted of plain text, we could handle the chunk like
6787 <a id="code-label-process-chunk-simple-
1"></a>
6788 <table style="width:
100%
" id="code-ref-process-chunk-simple-
1">
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> ≡</font></td>
6795 <tt class="verbatim
"><div class="compact-block
">
6796 <tt>1 </tt>chunk_line(active_chunk, $0 ORS);
6798 </p><table style="width:
100%
" id="code-end-process-chunk-simple-
1">
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>
6805 but in fact a chunk can include references to other chunks. Chunk
6806 includes are traditionally written as <tt class="verbatim
"><<chunk-name>></tt>
6807 but we support other variations, some of which are more suitable for
6808 particular editing systems.
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.
6819 <a id="code-label-process-chunk-tabs-
1"></a>
6820 <table style="width:
100%
" id="code-ref-process-chunk-tabs-
1">
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> ≡</font></td>
6828 <tt class="verbatim
"><div class="compact-block
">
6829 <tt>1 </tt>if (length(tabs)) {
6832 <pre class="verbatim
" xml:space="preserve
">
6833 <div class="compact-block
"><tt>2 </tt> gsub("\t", tabs);</div></pre>
6835 <tt class="verbatim
"><div class="compact-block
">
6839 </p><table style="width:
100%
" id="code-end-process-chunk-tabs-
1">
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>
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={=<}{>}}</tt> is set, then we
6848 can use <tt class="verbatim
">=<\chunkref{chunk-name}></tt> in listings. The
6849 sequence <tt class="verbatim
">=<</tt> was chosen because:
6853 it is a better mnemonic than <tt class="verbatim
"><<chunk-name>></tt>
6854 in that the <tt class="verbatim
">=</tt> sign signifies equivalence or
6858 and because <tt class="verbatim
">=<</tt> is not valid in C or any language I
6862 and also because lstlistings doesn't like <tt class="verbatim
">>></tt> as
6863 an end delimiter for the <em>texcl</em> escape, so we must make do
6864 with a single <tt class="verbatim
">></tt> which is better complemented by <tt
6865 class="verbatim
">=<</tt> than by <tt class="verbatim
"><<</tt>.
6869 Unfortunately the <tt class="verbatim
">=<...></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.
6881 <a id="code-label-delatex-
1"></a>
6882 <table style="width:
100%
" id="code-ref-delatex-
1">
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> ≡</font></td>
6890 <tt class="verbatim
"><div class="compact-block
">
6891 <tt>1 </tt># FILTHY HACK
6894 <pre class="verbatim
" xml:space="preserve
">
6895 <div class="compact-block
"><tt>2 </tt>gsub("\\\\#", "#", ${text});
6896 <tt>3 </tt>gsub("\\\\textbackslash{}", "\\", ${text});</div></pre>
6898 <tt class="verbatim
"><div class="compact-block
">
6899 <tt>4 </tt>gsub("\\\\\\^", "^", ${text});
6902 </p><table style="width:
100%
" id="code-end-delatex-
1">
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>
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>
6923 <span style="margin-left:
0em
"></span>
6924 <a id="footnr-
5"></a>
6925 <sup><a href="#footnote-
5">5</a></sup>
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>
6935 <a id="code-label-process-chunk-
1"></a>
6936 <table style="width:
100%
" id="code-ref-process-chunk-
1">
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>
6941 </div><div class="right-tab
">
6942 <a href="#code-ref-process-chunk-
2">60a</a>⊳
6948 <tt class="verbatim
"><div class="compact-block
">
6949 <tt>1 </tt>chunk = $0;
6952 <pre class="verbatim
" xml:space="preserve
">
6953 <div class="compact-block
"><tt>2 </tt>indent = 0;
6954 <tt>3 </tt>while(match(chunk,"(\xC2\xAB)([^\xC2]*) [^\xC2]*\xC2\xBB", line) ||
6955 <tt>4 </tt> match(chunk,
6956 <tt>5 </tt> "([=]<\\\\chunkref{([^}>]*)}(\\(.*\\)|)>|<<([a-zA-Z_][-a-zA-Z0-9_]*)>>)",
6958 <tt>7 </tt>) {</div></pre>
6960 <tt class="verbatim
"><div class="compact-block
">
6961 <tt>8 </tt> chunklet = substr(chunk, 1, RSTART - 1);
6965 <a id="code-end-process-chunk-
1"></a>
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.
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.
6980 <a id="code-label-process-chunk-
2"></a>
6981 <table style="width:
100%
" id="code-ref-process-chunk-
2">
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]() ⇑<a href="#code-ref-process-chunk-
1">59c</a>,
6985 lang=<font color="blue
"></font> + ≡
6986 </div><div class="right-tab
">
6987 ⊲<a href="#code-ref-process-chunk-
1">59c</a> <a href="#code-ref-process-chunk-
3">60b</a>▿
6993 <tt class="verbatim
"><div class="compact-block
">
6994 <tt>9 </tt> indent += length(chunklet);
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);
7005 <a id="code-end-process-chunk-
2"></a>
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
">=<</tt> the older
7011 notangle style beginning with <tt class="verbatim
"><<</tt>.
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
7021 <a id="code-label-process-chunk-
3"></a>
7022 <table style="width:
100%
" id="code-ref-process-chunk-
3">
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]() ⇑<a href="#code-ref-process-chunk-
1">59c</a>,
7026 lang=<font color="blue
"><p>
7028 </p></font> + ≡
7029 </div><div class="right-tab
">
7030 ▵<a href="#code-ref-process-chunk-
2">60a</a> <a href="#code-ref-process-chunk-
4">60c</a>▿
7036 <tt class="verbatim
"><div class="compact-block
">
7037 <tt>12 </tt> if (substr(line[1], 1, 1) == "=") {
7040 <pre class="verbatim
" xml:space="preserve
">
7041 <div class="compact-block
"><tt>13 </tt> # chunk name up to }
7042 <tt>14 </tt> =<\chunkref{delatex}(line[3])>
7043 <tt>15 </tt> chunk_include(active_chunk, line[2] line[3], indent);
7044 <tt>16 </tt> } else if (substr(line[1], 1, 1) == "<") {
7045 <tt>17 </tt> chunk_include(active_chunk, line[4], indent);
7046 <tt>18 </tt> } else if (line[1] == "\xC2\xAB") {
7047 <tt>19 </tt> chunk_include(active_chunk, line[2], indent);
7048 <tt>20 </tt> } else {
7049 <tt>21 </tt> error("Unknown chunk fragment: " line[1]);</div></pre>
7051 <tt class="verbatim
"><div class="compact-block
">
7056 <a id="code-end-process-chunk-
3"></a>
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.
7065 <a id="code-label-process-chunk-
4"></a>
7066 <table style="width:
100%
" id="code-ref-process-chunk-
4">
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]() ⇑<a href="#code-ref-process-chunk-
1">59c</a>,
7070 lang=<font color="blue
"></font> + ≡
7071 </div><div class="right-tab
">
7072 ▵<a href="#code-ref-process-chunk-
3">60b</a> <a href="#code-ref-process-chunk-
5">60d</a>▿
7078 <tt class="verbatim
"><div class="compact-block
">
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);
7090 <a id="code-end-process-chunk-
4"></a>
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.
7100 <a id="code-label-process-chunk-
5"></a>
7101 <table style="width:
100%
" id="code-ref-process-chunk-
5">
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]() ⇑<a href="#code-ref-process-chunk-
1">59c</a>,
7105 lang=<font color="blue
"><p>
7107 </p></font> + ≡
7108 </div><div class="right-tab
">
7109 ▵<a href="#code-ref-process-chunk-
4">60c</a>
7114 <tt class="verbatim
"><div class="compact-block
">
7115 <tt>25 </tt>chunk_line(active_chunk, "\n");
7117 </p><table style="width:
100%
" id="code-end-process-chunk-
5">
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>
7124 We will also permit a chunk-part number to follow in square brackets, so
7125 that <tt class="verbatim
">=<\chunkref{chunk-name[1]}></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:
7131 <pre class="verbatim
" xml:space="preserve
">
7132 =<\chunkref{chunk-name[1]}></pre>
7134 This is handled in section <a href="#sub:Chunk-parts
">12.1.1</a>
7137 We should perhaps introduce a notion of language specific chunk options;
7138 so that perhaps we could specify:
7140 <pre class="verbatim
" xml:space="preserve
">
7141 =<\chunkref{chunk-name[function-declaration]}</pre>
7143 which applies a transform
7144 <tt class="verbatim
">function-declaration</tt>
7145 to the chunk –- which in this case would extract a function
7146 prototype from a function.
7147 <table style="display: inline; vertical-align: -
0.55em
">
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>
7153 <h1 id="auto-
78">Chapter 11<br></br>Processing Options</h1>
7155 At the start, first we set the default options.
7159 <a id="code-label-default-options-
1"></a>
7160 <table style="width:
100%
" id="code-ref-default-options-
1">
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> ≡</font></td>
7168 <tt class="verbatim
"><div class="compact-block
">
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="*";</div></pre>
7177 <tt class="verbatim
"><div class="compact-block
">
7178 <tt>5 </tt>tabs = "";
7181 </p><table style="width:
100%
" id="code-end-default-options-
1">
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>
7188 Then we use getopt the standard way, and null out ARGV afterwards in the
7193 <a id="code-label-read-options-
1"></a>
7194 <table style="width:
100%
" id="code-ref-read-options-
1">
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> ≡</font></td>
7202 <tt class="verbatim
"><div class="compact-block
">
7203 <tt>1 </tt>Optind = 1 # skip ARGV[0]
7206 <pre class="verbatim
" xml:space="preserve
">
7207 <div class="compact-block
"><tt>2 </tt>while(getopt(ARGC, ARGV, "R:LdT:hr")!=-1) {
7208 <tt>3 </tt> =<\chunkref{handle-options}>
7209 <tt>4 </tt>}</div></pre>
7211 <tt class="verbatim
"><div class="compact-block
">
7212 <tt>5 </tt>for (i=1; i<Optind; i++) { ARGV[i]=""; }
7215 </p><table style="width:
100%
" id="code-end-read-options-
1">
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>
7222 This is how we handle our options:
7226 <a id="code-label-handle-options-
1"></a>
7227 <table style="width:
100%
" id="code-ref-handle-options-
1">
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> ≡</font></td>
7235 <tt class="verbatim
"><div class="compact-block
">
7236 <tt>1 </tt>if (Optopt == "R") root = Optarg;
7239 <pre class="verbatim
" xml:space="preserve
">
7240 <div class="compact-block
"><tt>2 </tt>else if (Optopt == "r") root="";
7241 <tt>3 </tt>else if (Optopt == "L") linenos = 1;
7242 <tt>4 </tt>else if (Optopt == "d") debug = 1;
7243 <tt>5 </tt>else if (Optopt == "T") tabs = indent_string(Optarg+0);
7244 <tt>6 </tt>else if (Optopt == "h") help();</div></pre>
7246 <tt class="verbatim
"><div class="compact-block
">
7247 <tt>7 </tt>else if (Optopt == "?") help();
7250 </p><table style="width:
100%
" id="code-end-handle-options-
1">
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>
7257 We do all of this at the beginning of the program
7261 <a id="code-label-begin-
1"></a>
7262 <table style="width:
100%
" id="code-ref-begin-
1">
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> ≡</font></td>
7270 <tt class="verbatim
"><div class="compact-block
">
7274 <pre class="verbatim
" xml:space="preserve
">
7275 <div class="compact-block
"><tt>2 </tt> =<\chunkref{constants}>
7276 <tt>3 </tt> =<\chunkref{mode-definitions}>
7277 <tt>4 </tt> =<\chunkref{default-options}>
7279 <tt>6 </tt> =<\chunkref{read-options}></div></pre>
7281 <tt class="verbatim
"><div class="compact-block
">
7285 </p><table style="width:
100%
" id="code-end-begin-
1">
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>
7292 And have a simple help function
7296 <a id="code-label-help()-
1"></a>
7297 <table style="width:
100%
" id="code-ref-help()-
1">
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> ≡</font></td>
7305 <tt class="verbatim
"><div class="compact-block
">
7306 <tt>1 </tt>function help() {
7309 <pre class="verbatim
" xml:space="preserve
">
7310 <div class="compact-block
"><tt>2 </tt> print "Usage:"
7311 <tt>3 </tt> print " fangle [-L] -R<rootname> [source.tex ...]"
7312 <tt>4 </tt> print " fangle -r [source.tex ...]"
7313 <tt>5 </tt> print " If the filename, source.tex is not specified then stdin is used"
7315 <tt>7 </tt> print "-L causes the C statement: #line <lineno> \"filename\"" to be issued"
7316 <tt>8 </tt> print "-R causes the named root to be written to stdout"
7317 <tt>9 </tt> print "-r lists all roots in the file (even those used elsewhere)"
7318 <tt>10 </tt> exit 1;</div></pre>
7320 <tt class="verbatim
"><div class="compact-block
">
7324 </p><table style="width:
100%
" id="code-end-help()-
1">
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>
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.
7336 <a id="code-label-generate-output-
1"></a>
7337 <table style="width:
100%
" id="code-ref-generate-output-
1">
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> ≡</font></td>
7345 <tt class="verbatim
"><div class="compact-block
">
7346 <tt>1 </tt>if (length(root)) output_chunk(root);
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();
7356 </p><table style="width:
100%
" id="code-end-generate-output-
1">
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>
7363 We also have some other output debugging:
7367 <a id="code-label-debug-output-
1"></a>
7368 <table style="width:
100%
" id="code-ref-debug-output-
1">
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> ≡</font></td>
7376 <tt class="verbatim
"><div class="compact-block
">
7377 <tt>1 </tt>if (debug) {
7380 <pre class="verbatim
" xml:space="preserve
">
7381 <div class="compact-block
"><tt>2 </tt> print "------ chunk names "
7382 <tt>3 </tt> output_chunk_names();
7383 <tt>4 </tt> print "====== chunks"
7384 <tt>5 </tt> output_chunks();
7385 <tt>6 </tt> print "++++++ debug"
7386 <tt>7 </tt> for (a in chunks) {
7387 <tt>8 </tt> print a "=" chunks[a];
7388 <tt>9 </tt> }</div></pre>
7390 <tt class="verbatim
"><div class="compact-block
">
7394 </p><table style="width:
100%
" id="code-end-debug-output-
1">
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>
7401 We do both of these at the end. We also set <tt class="verbatim
">ORS=""</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>.
7407 <a id="code-label-end-
1"></a>
7408 <table style="width:
100%
" id="code-ref-end-
1">
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> ≡</font></td>
7416 <tt class="verbatim
"><div class="compact-block
">
7420 <pre class="verbatim
" xml:space="preserve
">
7421 <div class="compact-block
"><tt>2 </tt> =<\chunkref{debug-output}>
7422 <tt>3 </tt> ORS="";
7423 <tt>4 </tt> =<\chunkref{generate-output}></div></pre>
7425 <tt class="verbatim
"><div class="compact-block
">
7429 </p><table style="width:
100%
" id="code-end-end-
1">
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>
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
"><<name>></tt>
7438 the same way notangle does:
7442 <a id="code-label-output_chunk_names()-
1"></a>
7443 <table style="width:
100%
" id="code-ref-output_chunk_names()-
1">
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> ≡</font></td>
7451 <tt class="verbatim
"><div class="compact-block
">
7452 <tt>1 </tt>function output_chunk_names( c, prefix, suffix)
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="<<";
7459 <tt>5 </tt> suffix=">>";
7461 <tt>7 </tt> for (c in chunk_names) {
7462 <tt>8 </tt> print prefix c suffix "\n";
7463 <tt>9 </tt> }</div></pre>
7465 <tt class="verbatim
"><div class="compact-block
">
7469 </p><table style="width:
100%
" id="code-end-output_chunk_names()-
1">
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>
7476 This function would write out all chunks
7480 <a id="code-label-output_chunks()-
1"></a>
7481 <table style="width:
100%
" id="code-ref-output_chunks()-
1">
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> ≡</font></td>
7489 <tt class="verbatim
"><div class="compact-block
">
7490 <tt>1 </tt>function output_chunks( a)
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);
7500 <tt>8 </tt>function output_chunk(chunk) {
7501 <tt>9 </tt> newline = 1;
7502 <tt>10 </tt> lineno_needed = linenos;
7504 <tt>12 </tt> write_chunk(chunk);
7505 <tt>13 </tt>}</div></pre>
7507 <tt class="verbatim
"><div class="compact-block
">
7511 </p><table style="width:
100%
" id="code-end-output_chunks()-
1">
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>
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
7525 We first initialize the mode tracker for this chunk.
7529 <a id="code-label-write_chunk()-
1"></a>
7530 <table style="width:
100%
" id="code-ref-write_chunk()-
1">
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>
7535 </div><div class="right-tab
">
7536 <a href="#code-ref-write_chunk()-
2">64b</a>▿
7542 <tt class="verbatim
"><div class="compact-block
">
7543 <tt>1 </tt>function write_chunk(chunk_name) {
7546 <pre class="verbatim
" xml:space="preserve
">
7547 <div class="compact-block
"><tt>2 </tt> =<\chunkref{awk-delete-array}(context)>
7548 <tt>3 </tt> return write_chunk_r(chunk_name, context);
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("write_chunk_r(",
7562 chunk_name, ")");
7566 <a id="code-end-write_chunk()-
1"></a>
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.
7576 <a id="code-label-write_chunk()-
2"></a>
7577 <table style="width:
100%
" id="code-ref-write_chunk()-
2">
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]() ⇑<a href="#code-ref-write_chunk()-
1">64a</a>,
7581 lang=<font color="blue
"></font> + ≡
7582 </div><div class="right-tab
">
7583 ▵<a href="#code-ref-write_chunk()-
1">64a</a> <a href="#code-ref-write_chunk()-
3">64c</a>▿
7589 <tt class="verbatim
"><div class="compact-block
">
7590 <tt>15 </tt> if (match(chunk_name,
7591 "^(.*)\\[([0-9]*)\\]$", chunk_name_parts)) {
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
">
7603 <a id="code-end-write_chunk()-
2"></a>
7607 We then create a mode tracker
7611 <a id="code-label-write_chunk()-
3"></a>
7612 <table style="width:
100%
" id="code-ref-write_chunk()-
3">
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]() ⇑<a href="#code-ref-write_chunk()-
1">64a</a>,
7616 lang=<font color="blue
"></font> + ≡
7617 </div><div class="right-tab
">
7618 ▵<a href="#code-ref-write_chunk()-
2">64b</a> <a href="#code-ref-write_chunk()-
4">65a</a>⊳
7623 <tt class="verbatim
"><div class="compact-block
">
7624 <tt>19 </tt> =<\chunkref{new-mode-tracker}(context,
7625 chunks[chunk_name, "language"], "")>
7628 <a id="code-end-write_chunk()-
3"></a>
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>.
7638 <a id="code-label-write_chunk()-
4"></a>
7639 <table style="width:
100%
" id="code-ref-write_chunk()-
4">
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]() ⇑<a href="#code-ref-write_chunk()-
1">64a</a>,
7643 lang=<font color="blue
"></font> + ≡
7644 </div><div class="right-tab
">
7645 ⊲<a href="#code-ref-write_chunk()-
3">64c</a> <a href="#code-ref-write_chunk()-
5">65b</a>▿
7650 <tt class="verbatim
"><div class="compact-block
">
7651 <tt>20 </tt> split(chunks[chunk_name, "params"],
7652 chunk_params, " *; *");
7655 <a id="code-end-write_chunk()-
4"></a>
7659 To assemble a chunk, we write out each part.
7663 <a id="code-label-write_chunk()-
5"></a>
7664 <table style="width:
100%
" id="code-ref-write_chunk()-
5">
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]() ⇑<a href="#code-ref-write_chunk()-
1">64a</a>,
7668 lang=<font color="blue
"></font> + ≡
7669 </div><div class="right-tab
">
7670 ▵<a href="#code-ref-write_chunk()-
4">65a</a>
7676 <tt class="verbatim
"><div class="compact-block
">
7677 <tt>21 </tt> if (! (chunk_name in chunk_names)) {
7680 <pre class="verbatim
" xml:space="preserve
">
7681 <div class="compact-block
"><tt>22 </tt> error(sprintf(_"The root module <<%s>> was not defined.\nUsed by: %s",\
7682 <tt>23 </tt> chunk_name, chunk_path));
7685 <tt>26 </tt> max_part = chunks[chunk_name, "part"];
7686 <tt>27 </tt> for(part = 1; part <= max_part; part++) {
7687 <tt>28 </tt> if (! only_part || part == only_part) {
7688 <tt>29 </tt> =<\chunkref{write-part}>
7691 <tt>32 </tt> if (! finalize_mode_tracker(context)) {
7692 <tt>33 </tt> dump_mode_tracker(context);
7693 <tt>34 </tt> error(sprintf(_"Module %s did not close context properly.\nUsed by: %s\n", chunk_name, chunk_path));
7694 <tt>35 </tt> }</div></pre>
7696 <tt class="verbatim
"><div class="compact-block
">
7700 </p><table style="width:
100%
" id="code-end-write_chunk()-
5">
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>
7707 A part can either be a chunklet of lines, or an include of another
7711 Chunks may also have parameters, specified in LaTeX style with braces
7712 after the chunk name –- 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>.
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.
7723 <a id="code-label-write-part-
1"></a>
7724 <table style="width:
100%
" id="code-ref-write-part-
1">
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> ≡</font></td>
7732 <tt class="verbatim
"><div class="compact-block
">
7733 <tt>1 </tt>=<\chunkref{check-source-jump}>
7736 <pre class="verbatim
" xml:space="preserve
">
7737 <div class="compact-block
"><tt>2 </tt>
7738 <tt>3 </tt>chunklet = chunks[chunk_name, "part", part];
7739 <tt>4 </tt>if (chunks[chunk_name, "part", part, "type"] == part_type_chunk) {
7740 <tt>5 </tt> =<\chunkref{write-included-chunk}>
7741 <tt>6 </tt>} else if (chunklet SUBSEP "line" in chunks) {
7742 <tt>7 </tt> =<\chunkref{write-chunklets}>
7744 <tt>9 </tt> # empty last chunklet</div></pre>
7746 <tt class="verbatim
"><div class="compact-block
">
7750 </p><table style="width:
100%
" id="code-end-write-part-
1">
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>
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>.
7762 <a id="code-label-write-included-chunk-
1"></a>
7763 <table style="width:
100%
" id="code-ref-write-included-chunk-
1">
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> ≡</font></td>
7771 <tt class="verbatim
"><div class="compact-block
">
7772 <tt>1 </tt>if (match(chunklet,
7773 "^([^\\[\\(]*)\\((.*)\\)$", chunklet_parts)) {
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("c-like", chunklet_parts[2], call_chunk_args, "(");
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);
7783 <tt>8 </tt> split("", call_chunk_args);
7785 <tt>10 </tt># update the transforms arrays
7786 <tt>11 </tt>new_src = mode_escaper(context, s, r, src);
7787 <tt>12 </tt>=<\chunkref{awk-delete-array}(new_context)>
7788 <tt>13 </tt>write_chunk_r(chunklet, new_context,
7789 <tt>14 </tt> chunks[chunk_name, "part", part, "indent"] indent,
7790 <tt>15 </tt> chunks[chunk_name, "part", part, "tail"],
7791 <tt>16 </tt> chunk_path "\n " 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);
7798 </p><table style="width:
100%
" id="code-end-write-included-chunk-
1">
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>
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.
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
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
7821 <a id="code-label-write-chunklets-
1"></a>
7822 <table style="width:
100%
" id="code-ref-write-chunklets-
1">
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>
7827 </div><div class="right-tab
">
7828 <a href="#code-ref-write-chunklets-
2">66b</a>▿
7834 <tt class="verbatim
"><div class="compact-block
">
7835 <tt>1 </tt>max_frag = chunks[chunklet, "line"];
7838 <pre class="verbatim
" xml:space="preserve
">
7839 <div class="compact-block
"><tt>2 </tt>for(frag = 1; frag <= max_frag; frag++) {</div></pre>
7841 <tt class="verbatim
"><div class="compact-block
">
7842 <tt>3 </tt> =<\chunkref{write-file-line}>
7846 <a id="code-end-write-chunklets-
1"></a>
7850 We then extract the chunklet text and expand any arguments.
7854 <a id="code-label-write-chunklets-
2"></a>
7855 <table style="width:
100%
" id="code-ref-write-chunklets-
2">
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]() ⇑<a href="#code-ref-write-chunklets-
1">66a</a>,
7859 lang=<font color="blue
"></font> + ≡
7860 </div><div class="right-tab
">
7861 ▵<a href="#code-ref-write-chunklets-
1">66a</a> <a href="#code-ref-write-chunklets-
3">66c</a>▿
7867 <tt class="verbatim
"><div class="compact-block
">
7871 <pre class="verbatim
" xml:space="preserve
">
7872 <div class="compact-block
"><tt>5 </tt> text = chunks[chunklet, frag];
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,
7882 <a id="code-end-write-chunklets-
2"></a>
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 –- because the chunk that included this chunk
7890 will have the newline at the end of the line that included this chunk.
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
7899 <a id="code-label-write-chunklets-
3"></a>
7900 <table style="width:
100%
" id="code-ref-write-chunklets-
3">
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]() ⇑<a href="#code-ref-write-chunklets-
1">66a</a>,
7904 lang=<font color="blue
"></font> + ≡
7905 </div><div class="right-tab
">
7906 ▵<a href="#code-ref-write-chunklets-
2">66b</a> <a href="#code-ref-write-chunklets-
4">66d</a>▿
7912 <tt class="verbatim
"><div class="compact-block
">
7916 <pre class="verbatim
" xml:space="preserve
">
7917 <div class="compact-block
"><tt>10 </tt> if (text == "\n") {
7918 <tt>11 </tt> lineno++;
7919 <tt>12 </tt> if (part == max_part && frag == max_frag && length(chunk_path)) {
7920 <tt>13 </tt> text = "";
7922 <tt>15 </tt> } else {
7923 <tt>16 </tt> newline = 1;</div></pre>
7925 <tt class="verbatim
"><div class="compact-block
">
7930 <a id="code-end-write-chunklets-
3"></a>
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
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.
7944 <a id="code-label-write-chunklets-
4"></a>
7945 <table style="width:
100%
" id="code-ref-write-chunklets-
4">
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]() ⇑<a href="#code-ref-write-chunklets-
1">66a</a>,
7949 lang=<font color="blue
"></font> + ≡
7950 </div><div class="right-tab
">
7951 ▵<a href="#code-ref-write-chunklets-
3">66c</a> <a href="#code-ref-write-chunklets-
5">67a</a>⊳
7957 <tt class="verbatim
"><div class="compact-block
">
7958 <tt>18 </tt> } else if (length(text) || length(tail)) {
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
">
7971 <a id="code-end-write-chunklets-
4"></a>
7975 Tail will soon no longer be relevant once mode-detection is in place.
7979 <a id="code-label-write-chunklets-
5"></a>
7980 <table style="width:
100%
" id="code-ref-write-chunklets-
5">
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]() ⇑<a href="#code-ref-write-chunklets-
1">66a</a>,
7984 lang=<font color="blue
"></font> + ≡
7985 </div><div class="right-tab
">
7986 ⊲<a href="#code-ref-write-chunklets-
4">66d</a> <a href="#code-ref-write-chunklets-
6">67b</a>▿
7992 <tt class="verbatim
"><div class="compact-block
">
7993 <tt>23 </tt> text = text tail;
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));
8004 <a id="code-end-write-chunklets-
5"></a>
8008 If a line ends in a backslash –- suggesting continuation –-
8009 then we supress outputting file-line as it would probably break the
8014 <a id="code-label-write-chunklets-
6"></a>
8015 <table style="width:
100%
" id="code-ref-write-chunklets-
6">
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]() ⇑<a href="#code-ref-write-chunklets-
1">66a</a>,
8019 lang=<font color="blue
"></font> + ≡
8020 </div><div class="right-tab
">
8021 ▵<a href="#code-ref-write-chunklets-
5">67a</a>
8027 <tt class="verbatim
"><div class="compact-block
">
8028 <tt>26 </tt> if (linenos) {
8031 <pre class="verbatim
" xml:space="preserve
">
8032 <div class="compact-block
"><tt>27 </tt> lineno_suppressed = substr(lastline, length(lastline)) == "\\";
8033 <tt>28 </tt> }</div></pre>
8035 <tt class="verbatim
"><div class="compact-block
">
8039 </p><table style="width:
100%
" id="code-end-write-chunklets-
6">
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>
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.
8053 <a id="code-label-write-file-line-
1"></a>
8054 <table style="width:
100%
" id="code-ref-write-file-line-
1">
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> ≡</font></td>
8062 <tt class="verbatim
"><div class="compact-block
">
8063 <tt>1 </tt>if (newline && lineno_needed && !
8064 lineno_suppressed) {
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 "#line " lineno " \"" filename "\"\n"
8071 <tt>5 </tt> lineno_needed = 0;</div></pre>
8073 <tt class="verbatim
"><div class="compact-block
">
8077 </p><table style="width:
100%
" id="code-end-write-file-line-
1">
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>
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.
8089 <a id="code-label-check-source-jump-
1"></a>
8090 <table style="width:
100%
" id="code-ref-check-source-jump-
1">
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> ≡</font></td>
8098 <tt class="verbatim
"><div class="compact-block
">
8099 <tt>1 </tt>if (linenos && (chunk_name SUBSEP
8100 "part" SUBSEP part SUBSEP "FILENAME" in
8104 <pre class="verbatim
" xml:space="preserve
">
8105 <div class="compact-block
"><tt>2 </tt> a_filename = chunks[chunk_name, "part", part, "FILENAME"];
8106 <tt>3 </tt> a_lineno = chunks[chunk_name, "part", part, "LINENO"];
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
">
8115 </p><table style="width:
100%
" id="code-end-check-source-jump-
1">
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>
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>.
8129 <a id="code-label-constants-
1"></a>
8130 <table style="width:
100%
" id="code-ref-constants-
1">
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> ≡</font></td>
8138 <tt class="verbatim
"><div class="compact-block
">
8139 <tt>1 </tt>part_type_chunk=1;
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=",";
8149 </p><table style="width:
100%
" id="code-end-constants-
1">
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>
8156 The params mentioned are not chunk parameters for parameterized chunks,
8158 <a href="#Chunk Arguments
">8.2</a>
8159 , but the lstlistings style parameters used in the
8160 <tt class="verbatim
">\Chunk</tt>
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
8174 <span style="margin-left:
0em
"></span>
8175 <a id="footnr-
1"></a>
8176 <sup><a href="#footnote-
1">1</a></sup>
8181 <a id="code-label-chunk-storage-functions-
1"></a>
8182 <table style="width:
100%
" id="code-ref-chunk-storage-functions-
1">
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> ≡
8187 </div><div class="right-tab
">
8188 <a href="#code-ref-chunk-storage-functions-
2">69c</a>▿
8194 <tt class="verbatim
"><div class="compact-block
">
8195 <tt>1 </tt>function new_chunk(chunk_name, params,
8198 <pre class="verbatim
" xml:space="preserve
">
8199 <div class="compact-block
"><tt>2 </tt> # local vars
8200 <tt>3 </tt> p, append )
8202 <tt>5 </tt> # HACK WHILE WE CHANGE TO ( ) for PARAM CHUNKS
8203 <tt>6 </tt> gsub("\\(\\)$", "", chunk_name);
8204 <tt>7 </tt> if (! (chunk_name in chunk_names)) {
8205 <tt>8 </tt> if (debug) print "New chunk " 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 "chunks[" chunk_name "," p "] = " params[p];
8211 <tt>14 </tt> if ("append" in params) {
8212 <tt>15 </tt> append=params["append"];
8213 <tt>16 </tt> if (! (append in chunk_names)) {
8214 <tt>17 </tt> warning("Chunk " chunk_name " is appended to chunk " append " which is not defined yet");
8215 <tt>18 </tt> new_chunk(append);
8217 <tt>20 </tt> chunk_include(append, chunk_name);
8218 <tt>21 </tt> chunk_line(append, ORS);
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
">
8229 <a id="code-end-chunk-storage-functions-
1"></a>
8234 <a id="code-label-chunk-storage-functions-
2"></a>
8235 <table style="width:
100%
" id="code-ref-chunk-storage-functions-
2">
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]() ⇑<a href="#code-ref-chunk-storage-functions-
1">69b</a>,
8239 lang=<font color="blue
"></font> + ≡
8240 </div><div class="right-tab
">
8241 ▵<a href="#code-ref-chunk-storage-functions-
1">69b</a> <a href="#code-ref-chunk-storage-functions-
3">70a</a>⊳
8247 <tt class="verbatim
"><div class="compact-block
">
8251 <pre class="verbatim
" xml:space="preserve
">
8252 <div class="compact-block
"><tt>28 </tt>function prime_chunk(chunk_name)
8254 <tt>30 </tt> chunks[chunk_name, "part", ++chunks[chunk_name, "part"] ] = \
8255 <tt>31 </tt> chunk_name SUBSEP "chunklet" SUBSEP "" ++chunks[chunk_name, "chunklet"];
8256 <tt>32 </tt> chunks[chunk_name, "part", chunks[chunk_name, "part"], "FILENAME"] = FILENAME;
8257 <tt>33 </tt> chunks[chunk_name, "part", chunks[chunk_name, "part"], "LINENO"] = FNR + 1;
8260 <tt>36 </tt>function chunk_line(chunk_name, line){
8261 <tt>37 </tt> chunks[chunk_name, "chunklet", chunks[chunk_name, "chunklet"],
8262 <tt>38 </tt> ++chunks[chunk_name, "chunklet", chunks[chunk_name, "chunklet"], "line"] ] = line;
8263 <tt>39 </tt>}</div></pre>
8265 <tt class="verbatim
"><div class="compact-block
">
8270 <a id="code-end-chunk-storage-functions-
2"></a>
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.
8282 <a id="code-label-chunk-storage-functions-
3"></a>
8283 <table style="width:
100%
" id="code-ref-chunk-storage-functions-
3">
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]() ⇑<a href="#code-ref-chunk-storage-functions-
1">69b</a>,
8287 lang=<font color="blue
"></font> + ≡
8288 </div><div class="right-tab
">
8289 ⊲<a href="#code-ref-chunk-storage-functions-
2">69c</a> <a href="#code-ref-chunk-storage-functions-
4">70b</a>▿
8295 <tt class="verbatim
"><div class="compact-block
">
8296 <tt>41 </tt>function chunk_include(chunk_name, chunk_ref, indent,
8300 <pre class="verbatim
" xml:space="preserve
">
8301 <div class="compact-block
"><tt>42 </tt>{
8302 <tt>43 </tt> chunks[chunk_name, "part", ++chunks[chunk_name, "part"] ] = chunk_ref;
8303 <tt>44 </tt> chunks[chunk_name, "part", chunks[chunk_name, "part"], "type" ] = part_type_chunk;
8304 <tt>45 </tt> chunks[chunk_name, "part", chunks[chunk_name, "part"], "indent" ] = indent_string(indent);
8305 <tt>46 </tt> chunks[chunk_name, "part", chunks[chunk_name, "part"], "tail" ] = tail;
8306 <tt>47 </tt> prime_chunk(chunk_name);
8307 <tt>48 </tt>}</div></pre>
8309 <tt class="verbatim
"><div class="compact-block
">
8314 <a id="code-end-chunk-storage-functions-
3"></a>
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.
8325 <a id="code-label-chunk-storage-functions-
4"></a>
8326 <table style="width:
100%
" id="code-ref-chunk-storage-functions-
4">
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]() ⇑<a href="#code-ref-chunk-storage-functions-
1">69b</a>,
8330 lang=<font color="blue
"></font> + ≡
8331 </div><div class="right-tab
">
8332 ▵<a href="#code-ref-chunk-storage-functions-
3">70a</a>
8338 <tt class="verbatim
"><div class="compact-block
">
8339 <tt>50 </tt>function indent_string(indent) {
8342 <pre class="verbatim
" xml:space="preserve
">
8343 <div class="compact-block
"><tt>51 </tt> return sprintf("%" indent "s", "");</div></pre>
8345 <tt class="verbatim
"><div class="compact-block
">
8349 </p><table style="width:
100%
" id="code-end-chunk-storage-functions-
4">
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>
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 âĂIJEdition 3 of GAWK: Effective AWK Programming: A
8360 User's Guide for GNU AwkâĂİ 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.
8366 The getopt.awk header is:
8370 <a id="code-label-getopt.awk-header-
1"></a>
8371 <table style="width:
100%
" id="code-ref-getopt.awk-header-
1">
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> ≡</font></td>
8379 <tt class="verbatim
"><div class="compact-block
">
8380 <tt>1 </tt># getopt.awk –- do C library getopt(3) function
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
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
">
8395 </p><table style="width:
100%
" id="code-end-getopt.awk-header-
1">
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>
8402 The provided explanation is:
8406 <a id="code-label-getopt.awk-notes-
1"></a>
8407 <table style="width:
100%
" id="code-ref-getopt.awk-notes-
1">
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> ≡</font></td>
8415 <tt class="verbatim
"><div class="compact-block
">
8416 <tt>1 </tt># External variables:
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
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># <c> a character representing the current option
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
">
8437 </p><table style="width:
100%
" id="code-end-getopt.awk-notes-
1">
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>
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 –- as
8446 indicated by the multiple spaces preceding them. Awk doesn't care, the
8447 multiple spaces are a convention to help us humans.
8451 <a id="code-label-getopt.awk-getopt()-
1"></a>
8452 <table style="width:
100%
" id="code-ref-getopt.awk-getopt()-
1">
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>
8457 </div><div class="right-tab
">
8458 <a href="#code-ref-getopt.awk-getopt()-
2">72a</a>⊳
8464 <tt class="verbatim
"><div class="compact-block
">
8465 <tt>1 </tt>function getopt(argc, argv, options, thisopt, i)
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] == "--") { # 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
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("%c -- invalid option\n",
8488 <tt>21 </tt> thisopt) > "/dev/stderr"
8489 <tt>22 </tt> if (_opti >= length(argv[Optind])) {
8490 <tt>23 </tt> Optind++
8491 <tt>24 </tt> _opti = 0
8493 <tt>26 </tt> _opti++
8494 <tt>27 </tt> return "?"</div></pre>
8496 <tt class="verbatim
"><div class="compact-block
">
8501 <a id="code-end-getopt.awk-getopt()-
1"></a>
8505 At this point, the option has been found and we need to know if it takes
8510 <a id="code-label-getopt.awk-getopt()-
2"></a>
8511 <table style="width:
100%
" id="code-ref-getopt.awk-getopt()-
2">
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]() ⇑<a href="#code-ref-getopt.awk-getopt()-
1">71c</a>,
8515 lang=<font color="blue
"></font> + ≡
8516 </div><div class="right-tab
">
8517 ⊲<a href="#code-ref-getopt.awk-getopt()-
1">71c</a>
8523 <tt class="verbatim
"><div class="compact-block
">
8524 <tt>29 </tt> if (substr(options, i + 1, 1) == ":") {
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)) > 0)
8530 <tt>32 </tt> Optarg = substr(argv[Optind], _opti + 1)
8532 <tt>34 </tt> Optarg = argv[++Optind]
8533 <tt>35 </tt> _opti = 0
8535 <tt>37 </tt> Optarg = ""
8536 <tt>38 </tt> if (_opti == 0 || _opti >= length(argv[Optind])) {
8537 <tt>39 </tt> Optind++
8538 <tt>40 </tt> _opti = 0
8540 <tt>42 </tt> _opti++
8541 <tt>43 </tt> return thisopt</div></pre>
8543 <tt class="verbatim
"><div class="compact-block
">
8547 </p><table style="width:
100%
" id="code-end-getopt.awk-getopt()-
2">
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>
8554 A test program is built in, too
8558 <a id="code-label-getopt.awk-begin-
1"></a>
8559 <table style="width:
100%
" id="code-ref-getopt.awk-begin-
1">
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> ≡</font></td>
8567 <tt class="verbatim
"><div class="compact-block
">
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, "ab:cd")) != -1)
8577 <tt>7 </tt> printf("c = <%c>, optarg = <%s>\n",
8578 <tt>8 </tt> _go_c, Optarg)
8579 <tt>9 </tt> printf("non-option arguments:\n")
8580 <tt>10 </tt> for (; Optind < ARGC; Optind++)
8581 <tt>11 </tt> printf("\tARGV[%d] = <%s>\n",
8582 <tt>12 </tt> Optind, ARGV[Optind])
8583 <tt>13 </tt> }</div></pre>
8585 <tt class="verbatim
"><div class="compact-block
">
8589 </p><table style="width:
100%
" id="code-end-getopt.awk-begin-
1">
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>
8596 The entire getopt.awk is made out of these chunks in order
8600 <a id="code-label-getopt.awk-
1"></a>
8601 <table style="width:
100%
" id="code-ref-getopt.awk-
1">
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> ≡</font></td>
8609 <tt class="verbatim
"><div class="compact-block
">
8610 <tt>1 </tt>=<\chunkref{getopt.awk-header}>
8613 <pre class="verbatim
" xml:space="preserve
">
8614 <div class="compact-block
"><tt>2 </tt>
8615 <tt>3 </tt>=<\chunkref{getopt.awk-notes}>
8616 <tt>4 </tt>=<\chunkref{getopt.awk-getopt()}></div></pre>
8618 <tt class="verbatim
"><div class="compact-block
">
8619 <tt>5 </tt>=<\chunkref{getopt.awk-begin}>
8622 </p><table style="width:
100%
" id="code-end-getopt.awk-
1">
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>
8629 Although we only want the header and function:
8633 <a id="code-label-getopt-
1"></a>
8634 <table style="width:
100%
" id="code-ref-getopt-
1">
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> ≡</font></td>
8642 <tt class="verbatim
"><div class="compact-block
">
8643 <tt>1 </tt># try: locate getopt.awk for the full original file
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>=<\chunkref{getopt.awk-header}>
8649 <tt>4 </tt></div></pre>
8651 <tt class="verbatim
"><div class="compact-block
">
8652 <tt>5 </tt>=<\chunkref{getopt.awk-getopt()}>
8655 </p><table style="width:
100%
" id="code-end-getopt-
1">
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>
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.
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.
8677 <a id="code-label-./fangle.module-
1"></a>
8678 <table style="width:
100%
" id="code-ref-./fangle.module-
1">
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> ≡</font></td>
8686 <tt class="verbatim
"><div class="compact-block
">
8687 <tt>1 </tt>#\DeclareLyXModule{Fangle Literate Listings}
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
8700 <tt>11 </tt>=<\chunkref{gpl3-copyright.hashed}>
8702 <tt>13 </tt>Format 11
8704 <tt>15 </tt>AddToPreamble
8705 <tt>16 </tt>=<\chunkref{./fangle.sty}>
8706 <tt>17 </tt>EndPreamble
8708 <tt>19 </tt>=<\chunkref{chunkstyle}>
8709 <tt>20 </tt></div></pre>
8711 <tt class="verbatim
"><div class="compact-block
">
8712 <tt>21 </tt>=<\chunkref{chunkref}>
8715 </p><table style="width:
100%
" id="code-end-./fangle.module-
1">
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>
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
8729 <a id="code-label-gpl3-copyright.hashed-
1"></a>
8730 <table style="width:
100%
" id="code-ref-gpl3-copyright.hashed-
1">
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> ≡</font></td>
8738 <tt class="verbatim
"><div class="compact-block
">
8739 <tt>1 </tt>#=<\chunkref{gpl3-copyright}>
8742 <pre class="verbatim
" xml:space="preserve
">
8743 <div class="compact-block
"></div></pre>
8745 <tt class="verbatim
"><div class="compact-block
">
8749 </p><table style="width:
100%
" id="code-end-gpl3-copyright.hashed-
1">
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>
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.
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>.
8774 We set PassThru to 1 because the user is actually entering raw latex.
8778 <a id="code-label-chunkstyle-
1"></a>
8779 <table style="width:
100%
" id="code-ref-chunkstyle-
1">
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>
8784 </div><div class="right-tab
">
8785 <a href="#code-ref-chunkstyle-
2">76b</a>▿
8791 <tt class="verbatim
"><div class="compact-block
">
8792 <tt>1 </tt>Style Chunk
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 "Chunk:"
8803 <tt>9 </tt> Align Left
8804 <tt>10 </tt> PassThru 1</div></pre>
8806 <tt class="verbatim
"><div class="compact-block
">
8811 <a id="code-end-chunkstyle-
1"></a>
8815 To make the label very visible we choose a larger font coloured red.
8819 <a id="code-label-chunkstyle-
2"></a>
8820 <table style="width:
100%
" id="code-ref-chunkstyle-
2">
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]() ⇑<a href="#code-ref-chunkstyle-
1">76a</a>,
8824 lang=<font color="blue
"></font> + ≡
8825 </div><div class="right-tab
">
8826 ▵<a href="#code-ref-chunkstyle-
1">76a</a>
8832 <tt class="verbatim
"><div class="compact-block
">
8833 <tt>12 </tt> LabelFont
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
">
8848 </p><table style="width:
100%
" id="code-end-chunkstyle-
2">
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>
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.
8861 <a id="code-label-chunkref-
1"></a>
8862 <table style="width:
100%
" id="code-ref-chunkref-
1">
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> ≡</font></td>
8870 <tt class="verbatim
"><div class="compact-block
">
8871 <tt>1 </tt>InsetLayout Chunkref
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
">
8888 </p><table style="width:
100%
" id="code-end-chunkref-
1">
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>
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.
8902 <a id="code-label-./fangle.sty-
1"></a>
8903 <table style="width:
100%
" id="code-ref-./fangle.sty-
1">
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>
8908 </div><div class="right-tab
">
8909 <a href="#code-ref-./fangle.sty-
2">77a</a>⊳
8915 <tt class="verbatim
"><div class="compact-block
">
8916 <tt>1 </tt>\usepackage{listings}%
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}}%
8928 <a id="code-end-./fangle.sty-
1"></a>
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>.
8938 <a id="code-label-./fangle.sty-
2"></a>
8939 <table style="width:
100%
" id="code-ref-./fangle.sty-
2">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
8943 lang=<font color="blue
"></font> + ≡
8944 </div><div class="right-tab
">
8945 ⊲<a href="#code-ref-./fangle.sty-
1">76d</a> <a href="#code-ref-./fangle.sty-
3">77b</a>▿
8950 <tt class="verbatim
"><div class="compact-block
">
8951 <tt>5 </tt>\lstnewenvironment{Chunk}{\relax}{\relax}%
8954 <a id="code-end-./fangle.sty-
2"></a>
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>.
8963 <a id="code-label-./fangle.sty-
3"></a>
8964 <table style="width:
100%
" id="code-ref-./fangle.sty-
3">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
8968 lang=<font color="blue
"></font> + ≡
8969 </div><div class="right-tab
">
8970 ▵<a href="#code-ref-./fangle.sty-
2">77a</a> <a href="#code-ref-./fangle.sty-
4">77c</a>▿
8976 <tt class="verbatim
"><div class="compact-block
">
8977 <tt>6 </tt>\lstset{numbers=left, stepnumber=5, numbersep=5pt,
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}%
8988 <a id="code-end-./fangle.sty-
3"></a>
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
">=<...></tt> sequence to contain LaTeX code, and include
8995 another like this chunk: <tt class="verbatim
">=<\chunkref{chunkname}></tt>.
8996 However, because <tt class="verbatim
">=<...></tt> is already defined to
8997 contain LaTeX code for this document –- this is a fangle document
8998 after all –- 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->advanced. Therefore <tt class="verbatim
">=<</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...
9009 <a id="code-label-./fangle.sty-
4"></a>
9010 <table style="width:
100%
" id="code-ref-./fangle.sty-
4">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9014 lang=<font color="blue
"></font> + ≡
9015 </div><div class="right-tab
">
9016 ▵<a href="#code-ref-./fangle.sty-
3">77b</a> <a href="#code-ref-./fangle.sty-
5">77d</a>▿
9021 <tt class="verbatim
"><div class="compact-block
">
9022 <tt>9 </tt>\lstset{escapeinside={=<}{>}}%
9025 <a id="code-end-./fangle.sty-
4"></a>
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
9041 <a id="code-label-./fangle.sty-
5"></a>
9042 <table style="width:
100%
" id="code-ref-./fangle.sty-
5">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9046 lang=<font color="blue
"></font> + ≡
9047 </div><div class="right-tab
">
9048 ▵<a href="#code-ref-./fangle.sty-
4">77c</a> <a href="#code-ref-./fangle.sty-
6">77e</a>▿
9054 <tt class="verbatim
"><div class="compact-block
">
9055 <tt>10 </tt>%\makeatletter
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{}%
9068 <a id="code-end-./fangle.sty-
5"></a>
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
9081 <a id="code-label-./fangle.sty-
6"></a>
9082 <table style="width:
100%
" id="code-ref-./fangle.sty-
6">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9086 lang=<font color="blue
"></font> + ≡
9087 </div><div class="right-tab
">
9088 ▵<a href="#code-ref-./fangle.sty-
5">77d</a> <a href="#code-ref-./fangle.sty-
7">78a</a>⊳
9094 <tt class="verbatim
"><div class="compact-block
">
9095 <tt>15 </tt>\def\Chunk#1{%
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}%
9108 <a id="code-end-./fangle.sty-
6"></a>
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.
9120 <a id="code-label-./fangle.sty-
7"></a>
9121 <table style="width:
100%
" id="code-ref-./fangle.sty-
7">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9125 lang=<font color="blue
"></font> + ≡
9126 </div><div class="right-tab
">
9127 ⊲<a href="#code-ref-./fangle.sty-
6">77e</a> <a href="#code-ref-./fangle.sty-
8">78b</a>▿
9132 <tt class="verbatim
"><div class="compact-block
">
9133 <tt>20 </tt>\lst@Key{params}\relax{\def\fangle@chunk@params{#1}}%
9136 <a id="code-end-./fangle.sty-
7"></a>
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.
9146 <a id="code-label-./fangle.sty-
8"></a>
9147 <table style="width:
100%
" id="code-ref-./fangle.sty-
8">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9151 lang=<font color="blue
"></font> + ≡
9152 </div><div class="right-tab
">
9153 ▵<a href="#code-ref-./fangle.sty-
7">78a</a> <a href="#code-ref-./fangle.sty-
9">78c</a>▿
9158 <tt class="verbatim
"><div class="compact-block
">
9159 <tt>21 </tt>\lst@Key{append}\relax{\def\fangle@chunk@append{#1}}%
9162 <a id="code-end-./fangle.sty-
8"></a>
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
9174 <a id="code-label-./fangle.sty-
9"></a>
9175 <table style="width:
100%
" id="code-ref-./fangle.sty-
9">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9179 lang=<font color="blue
"></font> + ≡
9180 </div><div class="right-tab
">
9181 ▵<a href="#code-ref-./fangle.sty-
8">78b</a> <a href="#code-ref-./fangle.sty-
10">78d</a>▿
9186 <tt class="verbatim
"><div class="compact-block
">
9187 \def\fanglecaption{\protect\fangle@caption}%
9190 <a id="code-end-./fangle.sty-
9"></a>
9193 <table style="width:
100%
">
9195 <td style="text-align: center; padding-left:
0em; padding-right:
0em
"><p>
9196 22c ⟨some-chunk 19b⟩≡+ ⊲22b 24d⊳
9200 In this example, the current chunk is 22c, and therefore the third
9203 It's name is some-chunk.
9205 The first chunk with this name (19b) occurs as the second chunk on
9208 The previous chunk (22d) with the same name is the second chunk on
9211 The next chunk (24d) is the fourth chunk on page 24.
9214 <td style="text-align: center; padding-left:
0em; padding-right:
0em; height:
0.5em
"></td>
9216 <td style="text-align: center; padding-left:
0em; padding-right:
0em; padding-left:
1.5em; padding-right:
1.5em
"><p>
9218 <b>Figure 1. </b><a id="auto-
92"></a>Noweb Heading<a id="noweb heading
"></a>
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
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.
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.
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.
9256 <a id="code-label-./fangle.sty-
10"></a>
9257 <table style="width:
100%
" id="code-ref-./fangle.sty-
10">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9261 lang=<font color="blue
"></font> + ≡
9262 </div><div class="right-tab
">
9263 ▵<a href="#code-ref-./fangle.sty-
9">78c</a> <a href="#code-ref-./fangle.sty-
11">79c</a>⊳
9268 <tt class="verbatim
"><div class="compact-block
">
9269 <tt>22 </tt>\newcounter{fangle@chunkcounter}%
9272 <a id="code-end-./fangle.sty-
10"></a>
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>.
9281 We save the counter like this:
9285 <a id="code-label-save-counter-
1"></a>
9286 <table style="width:
100%
" id="code-ref-save-counter-
1">
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> ≡</font></td>
9293 <tt class="verbatim
"><div class="compact-block
">
9294 \global\expandafter\edef\csname
9295 \chunkcount\endcsname{\arabic{fangle@chunkcounter}}%
9297 </p><table style="width:
100%
" id="code-end-save-counter-
1">
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>
9304 and restore the counter like this:
9308 <a id="code-label-restore-counter-
1"></a>
9309 <table style="width:
100%
" id="code-ref-restore-counter-
1">
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> ≡</font></td>
9316 <tt class="verbatim
"><div class="compact-block
">
9317 \setcounter{fangle@chunkcounter}{\csname \chunkcount\endcsname}%
9319 </p><table style="width:
100%
" id="code-end-restore-counter-
1">
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>
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.
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.
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.
9344 <a id="code-label-./fangle.sty-
11"></a>
9345 <table style="width:
100%
" id="code-ref-./fangle.sty-
11">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9349 lang=<font color="blue
"></font> + ≡
9350 </div><div class="right-tab
">
9351 ⊲<a href="#code-ref-./fangle.sty-
10">78d</a> <a href="#code-ref-./fangle.sty-
12">79d</a>▿
9357 <tt class="verbatim
"><div class="compact-block
">
9358 <tt>23 </tt>\def\fangle@caption{%
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%
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
">
9376 <a id="code-end-./fangle.sty-
11"></a>
9380 After incrementing the chunk counter, we then define the name of this
9381 chunk, as well as the name of the first chunk.
9385 <a id="code-label-./fangle.sty-
12"></a>
9386 <table style="width:
100%
" id="code-ref-./fangle.sty-
12">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9390 lang=<font color="blue
"></font> + ≡
9391 </div><div class="right-tab
">
9392 ▵<a href="#code-ref-./fangle.sty-
11">79c</a> <a href="#code-ref-./fangle.sty-
13">79e</a>▿
9398 <tt class="verbatim
"><div class="compact-block
">
9399 <tt>33 </tt> \addtocounter{fangle@chunkcounter}{1}%
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}%
9411 <a id="code-end-./fangle.sty-
12"></a>
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>.
9426 <a id="code-label-./fangle.sty-
13"></a>
9427 <table style="width:
100%
" id="code-ref-./fangle.sty-
13">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9431 lang=<font color="blue
"></font> + ≡
9432 </div><div class="right-tab
">
9433 ▵<a href="#code-ref-./fangle.sty-
12">79d</a> <a href="#code-ref-./fangle.sty-
14">80a</a>⊳
9439 <tt class="verbatim
"><div class="compact-block
">
9440 <tt>37 </tt> \addtocounter{fangle@chunkcounter}{1}%
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
">
9448 \@ifundefined{r@label-\nextchunkname}{\let\nextchunkname\relax}{}%
9452 <a id="code-end-./fangle.sty-
13"></a>
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.
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 –- but
9464 noweb requried that anyway.
9468 <a id="code-label-./fangle.sty-
14"></a>
9469 <table style="width:
100%
" id="code-ref-./fangle.sty-
14">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9473 lang=<font color="blue
"></font> + ≡
9474 </div><div class="right-tab
">
9475 ⊲<a href="#code-ref-./fangle.sty-
13">79e</a> <a href="#code-ref-./fangle.sty-
15">80b</a>▿
9481 <tt class="verbatim
"><div class="compact-block
">
9482 <tt>40 </tt> \sublabel{\chunkname}%
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}%
9494 <a id="code-end-./fangle.sty-
14"></a>
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
9504 <a id="code-label-./fangle.sty-
15"></a>
9505 <table style="width:
100%
" id="code-ref-./fangle.sty-
15">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9509 lang=<font color="blue
"></font> + ≡
9510 </div><div class="right-tab
">
9511 ▵<a href="#code-ref-./fangle.sty-
14">80a</a> <a href="#code-ref-./fangle.sty-
16">80c</a>▿
9516 <tt class="verbatim
"><div class="compact-block
">
9518 \addcontentsline{lol}{lstlisting}{\lst@name~[\protect\subpageref{\chunkname}]}%
9521 <a id="code-end-./fangle.sty-
15"></a>
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
">—</class> and
9528 if we do, it messes up the output somewhat.
9532 <a id="code-label-./fangle.sty-
16"></a>
9533 <table style="width:
100%
" id="code-ref-./fangle.sty-
16">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9537 lang=<font color="blue
"></font> + ≡
9538 </div><div class="right-tab
">
9539 ▵<a href="#code-ref-./fangle.sty-
15">80b</a> <a href="#code-ref-./fangle.sty-
17">80d</a>▿
9545 <tt class="verbatim
"><div class="compact-block
">
9546 <tt>45 </tt> \nwmargintag{%
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}%
9556 <tt>52 </tt> \moddef{%
9557 <tt>53 </tt> {\lst@name}%
9559 <tt>55 </tt> \nwtagstyle{}\/%
9560 <tt>56 </tt> \@ifundefined{fangle@chunk@params}{}{%
9561 <tt>57 </tt> (\fangle@chunk@params)%
9563 <tt>59 </tt> [\csname \chunkcount\endcsname]~%
9564 <tt>60 </tt> \subpageref{\firstchunkname}%
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%
9571 <tt>67 </tt>\global\def\fangle@chunk@append{}%
9572 <tt>68 </tt>\lstset{append=x}%
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
">
9585 <a id="code-end-./fangle.sty-
16"></a>
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
">—</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.
9597 <a id="code-label-./fangle.sty-
17"></a>
9598 <table style="width:
100%
" id="code-ref-./fangle.sty-
17">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9602 lang=<font color="blue
"></font> + ≡
9603 </div><div class="right-tab
">
9604 ▵<a href="#code-ref-./fangle.sty-
16">80c</a> <a href="#code-ref-./fangle.sty-
18">81a</a>⊳
9610 <tt class="verbatim
"><div class="compact-block
">
9611 <tt>76 </tt>%\lst@BeginAspect{fangle}
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
9624 <a id="code-end-./fangle.sty-
17"></a>
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.
9632 <table style="display: inline; vertical-align: -
2.2em
">
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>
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>
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>
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>
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
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
">=<\chunkref{new-mode-tracker}[{chunks[chunk_name,
9655 "language"]},{mode}]></tt> the inner braces (inside [ ])
9656 cause _ to signify subscript even though we have <tt class="verbatim
">lst@ReplaceIn</tt>
9660 <a id="code-label-./fangle.sty-
18"></a>
9661 <table style="width:
100%
" id="code-ref-./fangle.sty-
18">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9665 lang=<font color="blue
"></font> + ≡
9666 </div><div class="right-tab
">
9667 ⊲<a href="#code-ref-./fangle.sty-
17">80d</a> <a href="#code-ref-./fangle.sty-
19">82a</a>⊳
9673 <tt class="verbatim
"><div class="compact-block
">
9674 <tt>81 </tt>\def\chunkref@args#1,{%
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%
9681 <tt>85 </tt> \@ifnextchar){\relax}{, \chunkref@args}%
9683 <tt>87 </tt>\newcommand\chunkref[2][0]{%
9684 <tt>88 </tt> \@ifnextchar({\chunkref@i{#1}{#2}}{\chunkref@i{#1}{#2}()}%
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}%
9694 <tt>98 </tt> \def\chunkname{#2-\chunkno}%
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}%
9701 <tt>105 </tt> \nwtagstyle{}\/%
9702 <tt>106 </tt> \ifx\chunkno\zero%
9703 <tt>107 </tt> \else%
9704 <tt>108 </tt> [\chunkno]%
9706 <tt>110 </tt> \ifx\chunkargs\empty%
9707 <tt>111 </tt> \else%
9708 <tt>112 </tt> (\chunkref@args #3,)%
9710 <tt>114 </tt> ~\subpageref{\chunkname}%
9713 <tt>117 </tt> \RA%\endmoddef%</div></pre>
9715 <tt class="verbatim
"><div class="compact-block
">
9720 <a id="code-end-./fangle.sty-
18"></a>
9723 <h3 id="auto-
95">15.2.5<span style="margin-left:
1em
"></span>The end</h3>
9726 <a id="code-label-./fangle.sty-
19"></a>
9727 <table style="width:
100%
" id="code-ref-./fangle.sty-
19">
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]() ⇑<a href="#code-ref-./fangle.sty-
1">76d</a>,
9731 lang=<font color="blue
"></font> + ≡
9732 </div><div class="right-tab
">
9733 ⊲<a href="#code-ref-./fangle.sty-
18">81a</a>
9739 <tt class="verbatim
"><div class="compact-block
">
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
9750 </p><table style="width:
100%
" id="code-end-./fangle.sty-
19">
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>
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>.
9764 <a id="lyx-build-script
"></a>And this lyx-build scrap will extract fangle for me.
9768 <a id="code-label-lyx-build-
2"></a>
9769 <table style="width:
100%
" id="code-ref-lyx-build-
2">
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]() ⇑<a href="#code-ref-lyx-build-
1">20a</a>,
9773 lang=<font color="blue
">sh</font> + ≡
9774 </div><div class="right-tab
">
9775 ⊲<a href="#code-ref-lyx-build-
1">20a</a>
9781 <tt class="verbatim
"><div class="compact-block
">
9782 <tt>11 </tt>#! /bin/sh
9785 <pre class="verbatim
" xml:space="preserve
">
9786 <div class="compact-block
"><tt>12 </tt>set -x
9788 <tt>14 </tt>=<\chunkref{lyx-build-helper}>
9789 <tt>15 </tt>cd $PROJECT_DIR || exit 1
9791 <tt>17 </tt>/usr/local/bin/fangle -R./fangle $TEX_SRC > ./fangle
9792 <tt>18 </tt>/usr/local/bin/fangle -R./fangle.module $TEX_SRC > ./fangle.module
9794 <tt>20 </tt>=<\chunkref{test:helpers}>
9795 <tt>21 </tt>export FANGLE=./fangle
9796 <tt>22 </tt>export TMP=${TMP:-/tmp}
9797 <tt>23 </tt>=<\chunkref{test:run-tests}>
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 > ./new-fangle
9800 <tt>26 </tt>export FANGLE=./new-fangle</div></pre>
9802 <tt class="verbatim
"><div class="compact-block
">
9803 <tt>27 </tt>=<\chunkref{test:run-tests}>
9806 </p><table style="width:
100%
" id="code-end-lyx-build-
2">
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>
9814 <a id="code-label-test:run-tests-
1"></a>
9815 <table style="width:
100%
" id="code-ref-test:run-tests-
1">
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> ≡</font></td>
9823 <tt class="verbatim
"><div class="compact-block
">
9824 <tt>1 </tt># run tests
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>=<\chunkref{test:cromulence}>
9830 <tt>4 </tt>=<\chunkref{test:escapes}></div></pre>
9832 <tt class="verbatim
"><div class="compact-block
">
9833 <tt>5 </tt>=<\chunkref{test:chunk-params}>
9836 </p><table style="width:
100%
" id="code-end-test:run-tests-
1">
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>
9843 With a lyx-build-helper
9847 <a id="code-label-lyx-build-helper-
2"></a>
9848 <table style="width:
100%
" id="code-ref-lyx-build-helper-
2">
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]() ⇑<a href="#code-ref-lyx-build-helper-
1">19b</a>,
9852 lang=<font color="blue
">sh</font> + ≡
9853 </div><div class="right-tab
">
9854 ⊲<a href="#code-ref-lyx-build-helper-
1">19b</a>
9860 <tt class="verbatim
"><div class="compact-block
">
9861 <tt>5 </tt>PROJECT_DIR="$LYX_r"
9864 <pre class="verbatim
" xml:space="preserve
">
9865 <div class="compact-block
"><tt>6 </tt>LYX_SRC="$PROJECT_DIR/${LYX_i%.tex}.lyx"
9866 <tt>7 </tt>TEX_DIR="$LYX_p"</div></pre>
9868 <tt class="verbatim
"><div class="compact-block
">
9869 <tt>8 </tt>TEX_SRC="$TEX_DIR/$LYX_i"
9872 </p><table style="width:
100%
" id="code-end-lyx-build-helper-
2">
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>
9878 <h2 id="auto-
98">16.2<span style="margin-left:
1em
"></span>Extracting documentation</h2>
9881 <a id="code-label-./gen-www-
1"></a>
9882 <table style="width:
100%
" id="code-ref-./gen-www-
1">
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> ≡</font></td>
9890 <tt class="verbatim
"><div class="compact-block
">
9891 <tt>1 </tt>#python -m elyxer –css lyx.css $LYX_SRC | \
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"\(.\)>/ISO-8859-1"\1>/' > www/docs/fangle.html
9898 <tt>5 </tt>python -m elyxer --css lyx.css --iso885915 --html --destdirectory www/docs/fangle.e \
9899 <tt>6 </tt> fangle.lyx > www/docs/fangle.e/fangle.html
9901 <tt>8 </tt>( mkdir -p www/docs/fangle && cd www/docs/fangle && \
9902 <tt>9 </tt> lyx -e latex ../../../fangle.lyx && \
9903 <tt>10 </tt> htlatex ../../../fangle.tex "xhtml,fn-in" && \
9904 <tt>11 </tt> sed -i -e 's/<!--l\. [0-9][0-9]* *-->//g' fangle.html
9907 <tt>14 </tt>( mkdir -p www/docs/literate && cd www/docs/literate && \
9908 <tt>15 </tt> lyx -e latex ../../../literate.lyx && \
9909 <tt>16 </tt> htlatex ../../../literate.tex "xhtml,fn-in" && \
9910 <tt>17 </tt> sed -i -e 's/<!--l\. [0-9][0-9]* *-->$//g' literate.html</div></pre>
9912 <tt class="verbatim
"><div class="compact-block
">
9916 </p><table style="width:
100%
" id="code-end-./gen-www-
1">
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>
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:
9928 <a id="code-label-lyx-build-manual-
1"></a>
9929 <table style="width:
100%
" id="code-ref-lyx-build-manual-
1">
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> ≡</font></td>
9937 <tt class="verbatim
"><div class="compact-block
">
9938 <tt>1 </tt>lyx -e latex fangle.lyx
9941 <pre class="verbatim
" xml:space="preserve
">
9942 <div class="compact-block
"><tt>2 </tt>fangle -R./fangle fangle.tex > ./fangle</div></pre>
9944 <tt class="verbatim
"><div class="compact-block
">
9945 <tt>3 </tt>fangle -R./fangle.module fangle.tex >
9949 </p><table style="width:
100%
" id="code-end-lyx-build-manual-
1">
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>
9955 <h2 id="auto-
100">16.4<span style="margin-left:
1em
"></span>Testing</h2>
9958 <a id="code-label-test:helpers-
1"></a>
9959 <table style="width:
100%
" id="code-ref-test:helpers-
1">
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> ≡</font></td>
9967 <tt class="verbatim
"><div class="compact-block
">
9968 <tt>1 </tt>passtest() {
9971 <pre class="verbatim
" xml:space="preserve
">
9972 <div class="compact-block
"><tt>2 </tt> if "$@"
9973 <tt>3 </tt> then echo "Passed"
9974 <tt>4 </tt> else echo "Failed"
9975 <tt>5 </tt> return 1
9979 <tt>9 </tt>failtest() {
9980 <tt>10 </tt> if ! "$@"
9981 <tt>11 </tt> then echo "Passed"
9982 <tt>12 </tt> else echo "Failed"
9983 <tt>13 </tt> return 1
9984 <tt>14 </tt> fi</div></pre>
9986 <tt class="verbatim
"><div class="compact-block
">
9990 </p><table style="width:
100%
" id="code-end-test:helpers-
1">
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>
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
">
9999 </div><div class="right-tab
">
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">
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> ≡</font></td>
10016 <tt class="verbatim
"><div class="compact-block
">
10017 <tt>1 </tt>I see a ${THING},
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 =<\chunkref{test:chunk-params:sub:sub}(${colour})>
10028 </p><table style="width:
100%
" id="code-end-test:chunk-params:sub-
1">
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>
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">
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 ≡</font></td>
10045 <tt class="verbatim
"><div class="compact-block
">
10046 <tt>1 </tt>a funny shade of ${colour}
10048 </p><table style="width:
100%
" id="code-end-test:chunk-params:sub:sub-
1">
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>
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">
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> ≡</font></td>
10065 <tt class="verbatim
"><div class="compact-block
">
10066 <tt>1 </tt>What do you see?
10067 "=<\chunkref{test:chunk-params:sub}(joe, red)>"
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!
10077 </p><table style="width:
100%
" id="code-end-test:chunk-params:text-
1">
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>
10084 Should generate output:
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">
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> ≡</font></td>
10097 <tt class="verbatim
"><div class="compact-block
">
10098 <tt>1 </tt>What do you see? "I see a joe,
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"</div></pre>
10105 <tt class="verbatim
"><div class="compact-block
">
10106 <tt>4 </tt>Well, fancy!
10109 </p><table style="width:
100%
" id="code-end-test:chunk-params:result-
1">
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>
10116 And this chunk will perform the test:
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">
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> ≡</font></td>
10129 <tt class="verbatim
"><div class="compact-block
">
10130 <tt>1 </tt>$FANGLE -Rtest:chunk-params:result $TEX_SRC >
10131 $TMP/answer || exit 1
10134 <pre class="verbatim
" xml:space="preserve
">
10135 <div class="compact-block
"><tt>2 </tt>$FANGLE -Rtest:chunk-params:text $TEX_SRC > $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)
10142 </p><table style="width:
100%
" id="code-end-test:chunk-params-
1">
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>
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">
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> ≡</font></td>
10160 <tt class="verbatim
"><div class="compact-block
">
10161 <tt>1 </tt>#! /bin/sh
10164 <pre class="verbatim
" xml:space="preserve
">
10165 <div class="compact-block
"><tt>2 </tt># can't use gtkdialog -i, cos it uses the "source" command which ubuntu sh doesn't have
10167 <tt>4 </tt>main() {
10168 <tt>5 </tt> errors="/tmp/compile.log.$$"
10169 <tt>6 </tt># if grep '^[^ ]*:\( In \|[0-9][0-9]*: [^ ]*:\)' > $errors
10170 <tt>7 </tt>if grep '^[^ ]*(\([0-9][0-9]*\)) *: *\(error\|warning\)' > $errors
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> <vbox>
10175 <tt>12 </tt> <text>
10176 <tt>13 </tt> <label>Compiler errors:</label>
10177 <tt>14 </tt> </text>
10178 <tt>15 </tt> <tree exported_column="0">
10179 <tt>16 </tt> <variable>LINE</variable>
10180 <tt>17 </tt> <height>400</height><width>800</width>
10181 <tt>18 </tt> <label>File | Line | Message</label>
10182 <tt>19 </tt> <action>'". $SELF ; "'lyxgoto $LINE</action>
10183 <tt>20 </tt> <input>'"cat $errors"'</input>
10184 <tt>21 </tt> </tree>
10185 <tt>22 </tt> <hbox>
10186 <tt>23 </tt> <button><label>Build</label>
10187 <tt>24 </tt> <action>lyxclient -c "LYXCMD:build-program" &</action>
10188 <tt>25 </tt> </button>
10189 <tt>26 </tt> <button ok></button>
10190 <tt>27 </tt> </hbox>
10191 <tt>28 </tt> </vbox>
10193 <tt>30 </tt> export COMPILE_DIALOG
10194 <tt>31 </tt> ( gtkdialog --program=COMPILE_DIALOG ; rm $errors ) &
10196 <tt>33 </tt> rm $errors
10200 <tt>37 </tt>lyxgoto() {
10201 <tt>38 </tt> file="${LINE%:*}"
10202 <tt>39 </tt> line="${LINE##*:}"
10203 <tt>40 </tt> extraline=‘cat $file | head -n $line | tac | sed '/^\\\\begin{lstlisting}/q' | wc -l‘
10204 <tt>41 </tt> extraline=‘expr $extraline - 1‘
10205 <tt>42 </tt> lyxclient -c "LYXCMD:command-sequence server-goto-file-row $file $line ; char-forward ; repeat $extraline paragraph-down ; paragraph-up-select"
10208 <tt>45 </tt>SELF="$0"
10209 <tt>46 </tt>if test -z "$COMPILE_DIALOG"
10210 <tt>47 </tt>then main "$@" </div></pre>
10212 <tt class="verbatim
"><div class="compact-block
">
10216 </p><table style="width:
100%
" id="code-end-Chunk:./compile-log-lyx-
1">
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>