3 <style|<tuple|article|env-program|fangle>>
7 <assign|LyX|<macro|L<space|-0.1667em><move|Y|0fn|-0.25em><space|-0.125em>X>>
10 <doc-data|<doc-title|Sample Literate Programming>|<doc-author-data|<author-name|Sam
11 Liddicott>|<\author-address>
12 Using <TeXmacs> and Fangle
16 Literate Programming will not be explained here but it will be shown as a
17 way narrate program development showing the progression of ideas a is
18 natural for humans to understand, leaving the generation of programs from
19 those ideas to the machine.
21 <TeXmacs> is used as the editor and <name|Fangle><\footnote>
22 http://new.fangled.org
23 </footnote> as the extraction tool.
26 Here we will explain the popular <em|hello world> written in <name|C>.
28 <subsubsection|Message>
30 Here is the message that we wish to give to world:
32 <nf-chunk|message|Hello World!|txt|>
34 Because we did not press enter in the literate programming environment,
35 this line is not prefixed in the document tree with an
36 <verbatim|\<less\>item\<gtr\>> tag and so has no line number displayed and
37 is not a line but a line-fragment.
40 The header contains the chunk name <em|message> as well as the language
41 of the listing which is <em|txt>. The beginning of the header gives the
42 id to this <em|chunk> of code <emdash> <chunk-reference|message|1>
43 <emdash> which suggests to the reader that it is the first chunk on page
44 <chunk-page-reference|message|1>.<\footnote>
45 The label and hyperlink <chunk-reference|message|1> were generated with
46 the <TeXmacs> document tag <verbatim|\<less\>chunk-reference\|message\|1\<gtr\>>
47 to generate a link to the first chunklet of the chunk named
52 <subsubsection|Displaying the message>
54 We have a few choices available. The message can be output with
55 <verbatim|printf>, like this:
57 <\nf-chunk|message-printf>
58 <item>printf("<nf-ref|message|>\\n");
61 or even with <verbatim|puts>:
63 <\nf-chunk|message-puts>
64 <item>puts("<nf-ref|message|>\\n");
69 But, we prefer <verbatim|printf> which is more traditional.
72 These last two chunks have a different letter for the chunk reference
73 even though they may be on the same page.
77 They also contain a line number, because they are intended to be a full
81 <subsubsection|The main function>
84 If a code chunk is split across two pages, as this next code chunk is,
85 the code chunk header is repeated in the page header of the last page.
86 The technique used is to put a <TeXmacs> <em|label> at the begining of
87 the chunk and also at the end of the chunk. We take the <em|pagerefpage>
88 of each label, and if they are not on the same page, then at the end of
89 the chunk we set the page header to be the same as the chunk header.
92 We have to enclose this line in the standard C <em|main> function, like
96 <item>int main(int argc, char** argv) {
98 <item> \ <label|printfs><nf-ref|message-printf|>
105 The statement <verbatim|return 0;> lets the operating system know that the
106 function completed successfully <emdash> which is a bit of a presumption as
107 we don't check if <verbatim|printf> is successful.
110 Note the <verbatim|printf> statement defined in
111 <chunk-reference|message-printf|1> has been included in
116 The line numbering starts counting again from 1 for each named chunk.
119 <subsubsection|Composing the file>
121 By convention, with <TeXmacs> and <name|Fangle>, a chunk whose name begins
122 with a <verbatim|./> is considered to be a file suitable for automatic
123 extraction; so we will define out main chunk to be
124 <verbatim|./hello-world.c>
126 <paragraph|Header files>
128 On my system, both <verbatim|printf> and <verbatim|puts> both require the
129 header <verbatim|stdio.h>; so this line becomes the first line of our file.
131 <\nf-chunk|./hello-world.c>
132 <item>#include stdout.h
133 </nf-chunk|cpp|<tuple|>>
136 The header contains a link to chunk <chunk-reference|./hello-world.c|2>
137 which is the next chunklet of this named chunk.
140 <subparagraph|Main Body>
142 The include statement is followed by our main function that we defined in
143 <chunk-reference|main|1>.
145 <\nf-chunk|./hello-world.c>
150 This time the header also contains a link to chunk
151 <chunk-reference|hello-world.c|1> which is the previous chunklet of this
155 <subparagraph|Closing remarks>
157 And a final good-bye comment.
159 <\nf-chunk|./hello-world.c>
160 <item>/* thats the end, folks */
164 Like the first chunks we looked at, there are no further chunklets with
165 the same name and so there is no link to the next chunklet shown,
166 although there is a link to the previous chunklet.
169 <subsubsection|Updating the Document>
171 Because of the amount of page-referencing going on to calculate the
172 chunklet references, it can sometimes require the document to be updated
173 three or more times to fix the links and references.
176 <item>The first time, a chunk has to discover which page it is one.
178 <item>The second time all the chunks can be given the correct id based on
181 <item>The third time, all references to a chunk can be updated to the
184 <item>A forth and additional times may be required if the page-breaking
185 was changed as a result of a change in label size <emdash> although this
186 is only likely to occur if a reference occured in the text of the
187 document for the references in the header have enough space so as not to
191 <subsubsection|Extracting the source>
193 The file <verbatim|./hello-world.c> can be extracted directly with:
196 texmacs -s -c hello-world.tm hello-world.txt -q &&
198 fangle -R./hello-world.c hello-world.txt \<gtr\> hello-world.c
201 Although this is a little verbose, it shows how things work. There is a
202 Makefile.inc project that allows you to do all that sort of thing
205 <\the-index*|nf-chunk|Code Index>
206 <index-1|./hello-world.c|<reference|code-ref-./hello-world.c-1>,
207 <reference|code-ref-./hello-world.c-2>,
208 <reference|code-ref-./hello-world.c-3>>
210 <index-1|main|<reference|code-ref-main-1>>
212 <index-2|Used by|<reference|code-ref-./hello-world.c-1>>
214 <index-1|message|<reference|code-ref-message-1>>
216 <index-2|Used by|<reference|code-ref-message-printf-1>,
217 <reference|code-ref-message-puts-1>>
219 <index-1|message-printf|<reference|code-ref-message-printf-1>>
221 <index-2|Used by|<reference|code-ref-main-1>>
223 <index-1|message-puts|<reference|code-ref-message-puts-1>>
231 <associate|info-flag|detailed>
232 <associate|page-medium|paper>
233 <associate|page-screen-margin|false>
234 <associate|page-show-hf|true>
235 <associate|preamble|false>
236 <associate|src-style|angular>