8 <src-style-file|newfangle-literate-programming|0.2>
11 Style to be used in literate programs.
14 <\src-copyright|2009-2010>
19 This <TeXmacs> style file falls under the <hlink|GNU general public
20 license|$TEXMACS_PATH/LICENSE> and comes WITHOUT ANY WARRANTY
21 WHATSOEVER. If you do not have a copy of the license, then write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
28 <use-package|std|env|title-generic|header-generic|section-generic|tmdoc-markup>
32 First, some extensions to standard <TeXmacs> functionality - the
33 get-binding trick to extract a page number is kindly given by Joris who
34 gave us so much already.
36 I would rather ispageref? just made the first test comparing to uninit
37 (the red question mark) so that real but un-placed page refs would
38 still be recognized as a pageref - however I have found documents with
39 stale labels - that no longer exist in the document but which are still
40 stored in the document references section with a target page of ?
46 <assign|ispageref?|<macro|x|<and|<not|<equal|<uninit>|<pagerefpage|<arg|x>>>>|<not|<equal|?|<pagerefpage|<arg|x>>>>>>>
53 Page references are links, defined by the following macro:
57 \ \ \ \ \ (locus (id (hard-id (arg "x")))
59 \ \ \ \ \ \ \ \ \ \ \ \ (hyperlink (id (hard-id (arg "x")))
61 \ \ \ \ \ (url (merge "#" (arg "x"))))
63 (get-binding (arg "x") "1")))
66 which leads to this definition if pagerefpage whose job it is to give
67 the page number for a label (as it was last calculated to be). This is
68 useful when constructing our own labels that consist of the page number
69 and a sub-page counter of the target.
73 <assign|pagerefpage|<macro|x|<get-binding|<arg|x>|1>>>
77 These next macros control how a code chunk is rendered on the screen,
78 and could ligitimately be re-defined in the document.
84 nf-framed-table defines the format of the single cell that holds the
85 listing. I prefer a top and left border
89 <assign|nf-framed-table|<macro|x|<with|color|dark
90 grey|<tformat|<twith|table-width|1par>|<cwith|1|-1|1|-1|cell-hyphen|t>|<cwith|1|-1|1|-1|cell-bsep|1spc>|<cwith|1|-1|1|-1|cell-tsep|1spc>|<cwith|1|-1|1|-1|cell-background|white>|<cwith|1|-1|1|-1|cell-lborder|0.5ln>|<cwith|1|-1|1|-1|cell-rborder|0.0ln>|<cwith|1|-1|1|-1|cell-bborder|0.0ln>|<cwith|1|-1|1|-1|cell-tborder|0.5ln>|<arg|x>>>>>
94 nf-framed-fragment defines the listing to be rendered in a table with a
99 <assign|nf-framed-fragment|<macro|x|<nf-framed-table|<tformat|<table|<row|<\cell>
100 <with|color|black|<arg|x>>
105 prog-f renders a body in prog mode with specified language and without
106 a ghastly par-par-sep
110 <assign|prog-f|<macro|lang|body|<with|mode|prog|prog-language|<arg|lang>|<with|par-par-sep|0fn|<arg|body>>>>>
114 prog-fragment is the top level macro used for rendering a listing
115 (without line numbers). It renders prog-f within a framed fragment.
119 <assign|prog-fragment|<\macro|lang|x>
120 <indent|<nf-framed-fragment|<compound|prog-f|<arg|lang>|<arg|x>>>>
125 Numbering the listing
131 numbered-prog-fragment isn't used because it doesn't work when called
132 from nf-chunk, and I don't know why. The same definition is instead
133 used directly within nf-chunk. numbered-prog-fragment as shown here
134 represents a desire to re-factor the code when I understand what is
139 <assign|numbered-prog-fragment|<style-with|src-compact|none|<macro|name|lang|x|<prog-fragment|<arg|lang>|<list|<macro|x|<arg|x>
140 >|<macro|x|<smaller|<arg|x>>>|<assign|item-nr|<value|<merge|code-line-|<unquote|<arg|name>>|-nr>>><if|<equal|<get-label|<arg|x>>|document>|<map-args|itemly|document|x>|<arg|x>><assign|<merge|code-line-|<unquote|<arg|name>>|-nr>|<value|item-nr>>>>>>>
144 itemly is what is used to add line numbers to the listing. It is
145 invoked within an enumerate environment.\
147 If the listing is a document node then it is invoked on map-args of the
148 listing - which means on each line. We insert an \<less\>item\<gtr\>
149 tag at the start which will emit the line number (as it is within an
150 enumerate environment). We also define the label text to refer to the
151 line number within the listing.
155 <assign|itemly|<macro|x|<item><assign|the-label|<value|the-chunk-label>
156 <localize|line> <the-item>><arg|x>>>
160 itemly-c and itemly-d are not used right now, but are attempts to
161 recursively navigate a listings tree that may include in-place
162 expansions of other listings - so that numbering will be correct.\
164 The strategy is to recurse through the tree and each time a document
165 node is found, all it's children are handled by itemly which must also
166 recurse in the same way.
168 It doesn't work yet.\
172 <assign|itemly-c|<macro|x|<case|<equal|<get-label|<arg|x>>|document>|CCC<get-arity|<arg|x>><get-label|<arg|x>><item><map-args|itemly-d|document|<arg|x>>|<arg|x>>>>
174 <assign|itemly-d|<\macro|x>
175 <item><case|<equal|<get-label|<arg|x>>|concat>|DDD<get-arity|<arg|x>><get-label|<arg|x>><map|itemly-c|<arg|x>>|<arg|x>>
180 Forgot what nf-chunk is for
184 <new-counter|nf-chunk>
188 The noweb format that we follow names listings after the page number
189 followed by an alpha-counter that is reset on each page; e.g. 12a then
190 12b, 12c and then on page 13 back to 13a
192 As there is no trigger called on a new page that can help us, we
193 instead store the page number as we process each chunk so that we can
194 tell when a chunk is on a different page to the last chunk in order to
195 reset the alpha-counter.
203 The next-code-chunk-counter macro used before the code lines to set the
204 current label to something more useful that the line number
208 <assign|xxxnext-chunk-code-counter|<macro|>>
212 The next-code-line-counter macro used in the code lines to set the
213 current label to the chunk reference and the line number
217 <assign|xxxnext-chunk-line-counter|<macro|>>
221 Chunks have a name which is provided by the user, but more than one
222 chunk can have the same name. These are called chunklets of the same
225 Although chunklets have the same name, they have a different id, which
226 is the name with a prefix, and a suffix which is the chunklet sequence
231 <assign|nf-chunk-id|<macro|name|tag|n|<merge|code-|<arg|tag>|-|<unquote|<arg|name>>|-|<arg|n>>>>
235 nf-this-chunk-id is a convenience that uses the counter for the current
236 chunk. (Each chunk has it's own counter to count it's chunklets)
240 <assign|nf-this-chunk-id|<macro|name|tag|<nf-chunk-id|<arg|name>|<arg|tag>|<compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>>>>
244 Simple way to discover if we are processing the first chunklet of a
245 named chunk - by checjing it's chunklet counter.
249 <assign|nf-first-chunklet?|<macro|name|<equal|<compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>|1>>>
253 Simple way to discover if a chunklet (probably) exists - which is
254 needed to know if we should show a forward reference to the next
255 chunklet. As each chunklet defines a label, we just need to check if
256 there is a pageref to that label.
260 <assign|nf-chunklet-exists?|<macro|id|<ispageref?|<arg|id>>>>
264 nf-nav shows renders the links to the previous and next chunklets of
265 this chunk (if they exist).
269 <assign|nf-nav|<style-with|src-compact|none|<macro|name|<style-with|src-compact|all|<if|<not|<nf-first-chunklet?|<arg|name>>>|<math|\<vartriangleleft\>><reference|<nf-chunk-id|<arg|name>|ref|<minus|<compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>|1>>>>><if|<compound|nf-chunklet-exists?|<nf-chunk-id|<arg|name>|ref|<plus|<compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>|1>>>|
270 <reference|<nf-chunk-id|<arg|name>|ref|<plus|<compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>|1>>><math|\<vartriangleright\>>>>>>
274 nf-header renders the single line header before the listing. It lists
275 the reference of this chunk which is the-label (already estiablished by
276 nf-chunk-init) along with the chunk name and language, the chunklet
277 number and a reference to the first chunklet with the same name, and
278 also the chunklet navigation.
282 <assign|nf-header|<macro|name|lang|<with|par-first|-1fn|<yes-indent>><resize|<value|the-label>
283 |r-1.5fn||r+0.5fn|><with|mode|math|<left|langle>><label|<arg|name>><with|color|blue|<arg|name>>[<compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>]<if|<not|<compound|nf-first-chunklet?|<arg|name>>>|
284 <math|\<Uparrow\>><reference|<nf-chunk-id|<arg|name>|ref|1>>>,
285 lang=<with|color|blue|<arg|lang>><with|mode|math|<right|rangle>><math|<if|<not|<nf-first-chunklet?|<arg|name>>>|+>\<equiv\>><htab|0pt><nf-nav|<arg|name>>>>
289 nf-gather is an attempt to remember each named chunk so that it can
290 optionally be expanded in-place in other chunks. It's a poor attempt
291 and only works for forward uses. Such expansion should be compined with
292 over-brace or side-brace decoration of the included chunk, and also
293 requires that the inline continuation after the expanded chunk continue
294 from the baseline of the expanded chunk instead of the baseline before
295 the expanded chunk was included.
299 <assign|nf-gather|<macro|name|x|lang|<if|<provides|<arg|name>>|<assign|<unquote|<arg|name>>|<value|<arg|name>><unquote|<arg|x>>>|<assign|<unquote|<arg|name>>|<unquote|<arg|x>>>>>>
303 nf-chunk-new is called by nf-chunk-init for the first chunklet of a
304 given chunk name - it defines a chunklet counter and a line number
309 <assign|nf-chunk-new|<macro|name|<new-counter|<merge|code-chunk-|<unquote|<arg|name>>>><new-counter|<merge|code-line-|<unquote|<arg|name>>>><compound|<merge|inc-code-chunk-|<unquote|<arg|name>>>>>>
313 nf-chunk-new is called by nf-chunk-init for the additional chunklets of
316 It increments the chunklet counter
320 <assign|nf-chunk-next|<macro|name|<compound|<merge|inc-code-chunk-|<unquote|<arg|name>>>>>>
324 nf-check-page uses the label defined in nf-chunk-label to get the
325 number of the page containing this chunk to compare to nf-page (the
326 number of the page containing the previous chunk) to see if this chunk
327 is the first chunk on a page. If so, then the nf-chunk alpha counter is
328 reset, otherwise it is increased.
332 <assign|nf-check-page|<macro|name|<if|<not|<equal|<value|nf-page>|<pagerefpage|<nf-this-chunk-id|<arg|name>|label>>>>|<reset-nf-chunk><inc-nf-chunk><assign|nf-page|<pagerefpage|<nf-this-chunk-id|<arg|name>|label>>>|<inc-nf-chunk>>>>
336 nf-chunk-ref defines labels and label text so that we can generate
337 references to chunklets in the form
338 \<less\>PAGENO\<gtr\>\<less\>the-nf-chunk\<gtr\> \ e.g. 17a then 17b
341 The label text is stored in the-chunk-label which is used by XXX. It
342 then creates a label (with the tag ref) of the form code-ref-NAME-NO
343 which is a bit long winded to use with \<less\>reference\|...\<gtr\>
344 and so \<less\>chunk-reference\|...\<gtr\> is provided to help.
346 The ref tagged label is to be used in preference to the label-tagged
347 label created in nf-chunk-label whose only purpose is to find out what
348 page a chunk starts on so that the chunk ref label can be created.
352 <assign|nf-chunk-ref|<macro|name|<nf-check-page|<arg|name>><assign|the-chunk-label|<pagerefpage|<nf-this-chunk-id|<arg|name>|label>><number|<the-nf-chunk>|alpha>><assign|the-label|<value|the-chunk-label>><label|<nf-this-chunk-id|<arg|name>|ref>>>>
356 nf-render-line-no is responsible for rendering the listing line numbers
357 outside of the table cell. It adds a padding and a pipe separator for
358 text output to make text listings more readable. Only it does nothing!
359 There seems to be no \<less\>specific\|...\<gtr\> tag for verbatim
364 <assign|nf-render-line-no|<macro|x|<with|mode|text|<with|font-family|tt|<resize|<with|font-base-size|7|<arg|x><specific|verbatim|\|
365 >>|r+2.2fn||r+0.7fn|>>>>>
369 A chunklabel is of the form [chunkaname chunkno] e.g. [main.c 1] but
370 it's main purpose is not decorative or visual but as a means to
371 discover the page number containing this chunk so that it can be used
376 <assign|nf-chunk-label|<macro|name|<assign|the-label|[<arg|name>
377 <compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>]><label|<nf-this-chunk-id|<arg|name>|label>>>>
381 nf-chunk-init is called at the start of nf-chunk to create all
382 neccessary counters and labels. The
383 \<less\>provides\|\<less\>merge\|code-chunk-\|\<less\>unquote\|name\<gtr\>\|-nr\<gtr\>\<gtr\>
384 test could be replaced by some more canonical way to discover if a
385 chunk or chunk counter is defined. It's old code and I don't want to
390 <assign|nf-chunk-init|<macro|name|<if|<not|<provides|<merge|code-chunk-|<unquote|<arg|name>>|-nr>>>|<nf-chunk-new|<arg|name>>|<nf-chunk-next|<arg|name>>><nf-chunk-label|<arg|name>><nf-chunk-ref|<arg|name>>>>
394 nf-chunk is the first public macro. It shows a named listing with all
399 <assign|nf-chunk|<\macro|name|x|lang>
403 <nf-chunk-init|<arg|name>><nf-gather|<arg|name>|<arg|x>|<arg|lang>><no-page-break><small|<nf-header|<arg|name>|<arg|lang>>>
406 <prog-fragment|<arg|lang>|<list|<macro|x|<nf-render-line-no|<arg|x>>>|<macro|x|<smaller|<arg|x>>>|<assign|item-nr|<value|<merge|code-line-|<unquote|<arg|name>>|-nr>>><if|<equal|<get-label|<arg|x>>|document>|<map-args|itemly|document|x>|<arg|x>><assign|<merge|code-line-|<unquote|<arg|name>>|-nr>|<value|item-nr>>>>
415 nf-ref is the second public macro. It may be inserted into a chunk
416 listing to signify that another listing should be included at that
417 point. As \\ doesn't work in listings you'll have to type it in a
418 regular program block and cut-n-paste into the listing until I work out
419 how to assign a short-cut key to do it for you.
421 This will become an xmacro or something so that it can take optional
422 arguments (like C macros) which will be expanded when the programs are
427 <assign|nf-ref|<style-with|src-compact|none|<macro|name|\S<hlink|<arg|name>|<merge|#|<arg|name>>>\T<flag|<arg|name>|red><if|<equal|0|1>|<compound|<quote-arg|name>>>>>>
431 chunk-reference takes the chunks name and chunklet number and returns a
432 regular reference - like \<less\>reference\|\<gtr\> but calculates the
437 <assign|chunk-reference|<macro|name|n|<reference|<nf-chunk-id|<arg|name>|ref|<arg|n>>>>>
439 <assign|chunk-page-reference|<macro|name|n|<pageref|<nf-chunk-id|<arg|name>|ref|<arg|n>>>>>
441 <assign|chunk-page-reference-page|<macro|name|n|<pagerefpage|<nf-chunk-id|<arg|name>|ref|<arg|n>>>>>
445 Trash Bin - contains half-thought out or no-longer-needed code that I
446 don't want to delete yet.
450 <assign|nf-parse-look-ahead|<macro|<with|nf-chunks-index|<look-up|<look-up|<look-up|<value|nf-chunks-index>|0>|1>|4>|<with|i|0|<while|<less|<value|i>|<get-arity|<value|nf-chunks-index>>>|<assign|<merge|nfla-|<look-up|<look-up|<value|nf-chunks-index>|<value|i>>|0>>|<look-up|<value|nf-chunks-index>|<value|i>>><assign|i|<plus|<value|i>|1>>>>>>>
452 <assign|juggles|<macro|m|<tree|<arg|m>=<get-label|<value|<arg|m>>>(<get-arity|<value|<arg|m>>>)|<look-up|<value|<arg|m>>|0>|<look-up|<value|<arg|m>>|1>|<look-up|<value|<arg|m>>|2>|<look-up|<value|<arg|m>>|3>|<look-up|<value|<arg|m>>|4>|<look-up|<value|<arg|m>>|5>>>>
454 <assign|old-nf-chunklet-exists?|<macro|name|<is-tuple|<value|<merge|nfla-|<arg|name>>>>>>
456 <assign|global-put|<macro|key|value|<with|the-label|<arg|value>|<label|<arg|key>>>>>
458 <assign|global-get|<macro|key|<get-binding|<arg|key>|0>>>
463 <associate|page-medium|automatic>
464 <associate|page-screen-height|614400tmpt>
465 <associate|page-screen-width|798720tmpt>
466 <associate|preamble|true>