newfangle-texmacs is for use with the texmacs stylesheet.
[newfangle.git] / newfangle.ts
blobff4513dd849277a9c34182d4c2c8d44eedb5f8ea
1 <TeXmacs|1.0.7.4>
3 <style|source>
5 <\body>
6   <\active*>
7     <\src-title>
8       <src-style-file|newfangle-literate-programming|0.2>
10       <\src-purpose>
11         Style to be used in literate programs.
12       </src-purpose>
14       <\src-copyright|2009-2010>
15         Sam Liddicott
16       </src-copyright>
18       <\src-license>
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.
24       </src-license>
25     </src-title>
26   </active*>
28   <use-package|std|env|title-generic|header-generic|section-generic|tmdoc-markup>
30   <\active*>
31     <\src-comment>
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 ?
41     </src-comment>
42   </active*>
44   \;
46   <assign|ispageref?|<macro|x|<and|<not|<equal|<uninit>|<pagerefpage|<arg|x>>>>|<not|<equal|?|<pagerefpage|<arg|x>>>>>>>
48   <\active*>
49     <\src-comment>
50       Joris tells me:
52       <\verbatim>
53         Page references are links, defined by the following macro:
55         \ \ \ (macro "x"
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")))
64       </verbatim>
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.
70     </src-comment>
71   </active*>
73   <assign|pagerefpage|<macro|x|<get-binding|<arg|x>|1>>>
75   <\active*>
76     <\src-comment>
77       These next macros control how a code chunk is rendered on the screen,
78       and could ligitimately be re-defined in the document.
79     </src-comment>
80   </active*>
82   <\active*>
83     <\src-comment>
84       nf-framed-table defines the format of the single cell that holds the
85       listing. I prefer a top and left border
86     </src-comment>
87   </active*>
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>>>>>
92   <\active*>
93     <\src-comment>
94       nf-framed-fragment defines the listing to be rendered in a table with a
95       single cell.
96     </src-comment>
97   </active*>
99   <assign|nf-framed-fragment|<macro|x|<nf-framed-table|<tformat|<table|<row|<\cell>
100     <with|color|black|<arg|x>>
101   </cell>>>>>>>
103   <\active*>
104     <\src-comment>
105       prog-f renders a body in prog mode with specified language and without
106       a ghastly par-par-sep
107     </src-comment>
108   </active*>
110   <assign|prog-f|<macro|lang|body|<with|mode|prog|prog-language|<arg|lang>|<with|par-par-sep|0fn|<arg|body>>>>>
112   <\active*>
113     <\src-comment>
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.
116     </src-comment>
117   </active*>
119   <assign|prog-fragment|<\macro|lang|x>
120     <indent|<nf-framed-fragment|<compound|prog-f|<arg|lang>|<arg|x>>>>
121   </macro>>
123   <\active*>
124     <\src-comment>
125       Numbering the listing
126     </src-comment>
127   </active*>
129   <\active*>
130     <\src-comment>
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
135       going wrong.
136     </src-comment>
137   </active*>
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>>>>>>>
142   <\active*>
143     <\src-comment>
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.
152     </src-comment>
153   </active*>
155   <assign|itemly|<macro|x|<item><assign|the-label|<value|the-chunk-label>
156   <localize|line> <the-item>><arg|x>>>
158   <\active*>
159     <\src-comment>
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.\ 
169     </src-comment>
170   </active*>
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>>
176   </macro>>
178   <\active*>
179     <\src-comment>
180       Forgot what nf-chunk is for
181     </src-comment>
182   </active*>
184   <new-counter|nf-chunk>
186   <\active*>
187     <\src-comment>
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.
196     </src-comment>
197   </active*>
199   <assign|nf-page|0>
201   <\active*>
202     <\src-comment>
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
205     </src-comment>
206   </active*>
208   <assign|xxxnext-chunk-code-counter|<macro|>>
210   <\active*>
211     <\src-comment>
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
214     </src-comment>
215   </active*>
217   <assign|xxxnext-chunk-line-counter|<macro|>>
219   <\active*>
220     <\src-comment>
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
223       chunk.
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
227       number.
228     </src-comment>
229   </active*>
231   <assign|nf-chunk-id|<macro|name|tag|n|<merge|code-|<arg|tag>|-|<unquote|<arg|name>>|-|<arg|n>>>>
233   <\active*>
234     <\src-comment>
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)
237     </src-comment>
238   </active*>
240   <assign|nf-this-chunk-id|<macro|name|tag|<nf-chunk-id|<arg|name>|<arg|tag>|<compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>>>>
242   <\active*>
243     <\src-comment>
244       Simple way to discover if we are processing the first chunklet of a
245       named chunk - by checjing it's chunklet counter.
246     </src-comment>
247   </active*>
249   <assign|nf-first-chunklet?|<macro|name|<equal|<compound|<merge|the-code-chunk-|<unquote|<arg|name>>>>|1>>>
251   <\active*>
252     <\src-comment>
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.
257     </src-comment>
258   </active*>
260   <assign|nf-chunklet-exists?|<macro|id|<ispageref?|<arg|id>>>>
262   <\active*>
263     <\src-comment>
264       nf-nav shows renders the links to the previous and next chunklets of
265       this chunk (if they exist).
266     </src-comment>
267   </active*>
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\>>>>>>
272   <\active*>
273     <\src-comment>
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.
279     </src-comment>
280   </active*>
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>>>>
287   <\active*>
288     <\src-comment>
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.
296     </src-comment>
297   </active*>
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>>>>>>
301   <\active*>
302     <\src-comment>
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
305       counter
306     </src-comment>
307   </active*>
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>>>>>>
311   <\active*>
312     <\src-comment>
313       nf-chunk-new is called by nf-chunk-init for the additional chunklets of
314       a given chunk name.\ 
316       It increments the chunklet counter
317     </src-comment>
318   </active*>
320   <assign|nf-chunk-next|<macro|name|<compound|<merge|inc-code-chunk-|<unquote|<arg|name>>>>>>
322   <\active*>
323     <\src-comment>
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.
329     </src-comment>
330   </active*>
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>>>>
334   <\active*>
335     <\src-comment>
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
339       etc.
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.
349     </src-comment>
350   </active*>
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>>>>
354   <\active*>
355     <\src-comment>
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
360       exports :-(
361     </src-comment>
362   </active*>
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|>>>>>
367   <\active*>
368     <\src-comment>
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
372       by nf-chunk-ref.
373     </src-comment>
374   </active*>
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>>>>
379   <\active*>
380     <\src-comment>
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
386       change it now.
387     </src-comment>
388   </active*>
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>>>>
392   <\active*>
393     <\src-comment>
394       nf-chunk is the first public macro. It shows a named listing with all
395       the trimmings
396     </src-comment>
397   </active*>
399   <assign|nf-chunk|<\macro|name|x|lang>
400     \;
402     <\with|the-label|>
403       <nf-chunk-init|<arg|name>><nf-gather|<arg|name>|<arg|x>|<arg|lang>><no-page-break><small|<nf-header|<arg|name>|<arg|lang>>>
405       <\small>
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>>>>
407       </small>
409       <vspace|0.5fn>
410     </with>
411   </macro>>
413   <\active*>
414     <\src-comment>
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
423       extracted.
424     </src-comment>
425   </active*>
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>>>>>>
429   <\active*>
430     <\src-comment>
431       chunk-reference takes the chunks name and chunklet number and returns a
432       regular reference - like \<less\>reference\|\<gtr\> but calculates the
433       id for you.
434     </src-comment>
435   </active*>
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>>>>>
443   <\active*>
444     <\src-comment>
445       Trash Bin - contains half-thought out or no-longer-needed code that I
446       don't want to delete yet.
447     </src-comment>
448   </active*>
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>>>
459 </body>
461 <\initial>
462   <\collection>
463     <associate|page-medium|automatic>
464     <associate|page-screen-height|614400tmpt>
465     <associate|page-screen-width|798720tmpt>
466     <associate|preamble|true>
467   </collection>
468 </initial>