Add txt and pdf fangle files
[newfangle.git] / fangle.module
blobd1bb991ca47c31b0809813e66d866e6665453a18
1 #\DeclareLyXModule{Fangle Literate Listings}
2 #DescriptionBegin
3 #  Fangle literate listings allow one to write
4 #   literate programs after the fashion of noweb, but without having
5 #   to use noweave to generate the documentation. Instead the listings
6 #   package is extended in conjunction with the noweb package to implement
7 #   to code formating directly as latex.
8 #  The fangle awk script
9 #DescriptionEnd
11 #fangle - fully featured notangle replacement in awk
13 # Copyright (C) 2009 Sam Liddicott <sam@liddicott.com>
15 # This program is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
30 Format 11
32 AddToPreamble
33 \usepackage{listings}%
34 \usepackage{noweb}%
35 \usepackage{xargs}%
36 \renewcommand{\code}[1]{\texttt{#1}}%
37 \lstnewenvironment{Chunk}{\relax}{\relax}%
38 \lstset{numbers=left, stepnumber=5, numbersep=5pt,
39         breaklines=false,basicstyle=\ttfamily,
40         numberstyle=\tiny, language=C}%
41 \lstset{escapeinside={=<}{>}}%
42 %\makeatletter
43 %somehow re-defining maketitle gives us a left-aligned title
44 %which is extactly what our specially formatted title needs!
45 \global\let\fangle@lst@maketitle\lst@maketitle%
46 \global\def\lst@maketitle{}%
47 \def\Chunk#1{%
48   \lstset{title={\fanglecaption},name=#1}%
49   \global\edef\lst@chunkname{\lst@intname}%
51 \def\lst@chunkname{\empty}%
52 \lst@Key{params}\relax{\def\fangle@chunk@params{#1}}%
53 \lst@Key{append}\relax{\def\fangle@chunk@append{#1}}%
54 \def\fanglecaption{\protect\fangle@caption}%
55 \newcounter{fangle@chunkcounter}%
56 \def\fangle@caption{%
57   \edef\chunkcount{lst-chunk-\lst@intname}%
58   \@ifundefined{\chunkcount}{%
59     \expandafter\gdef\csname \chunkcount\endcsname{0}%
60     \setcounter{fangle@chunkcounter}{\csname \chunkcount\endcsname}%
61     \let\prevchunkname\relax%
62   }{%
63     \setcounter{fangle@chunkcounter}{\csname \chunkcount\endcsname}%
64     \edef\prevchunkname{\lst@intname-\arabic{fangle@chunkcounter}}%
65   }%
66   \addtocounter{fangle@chunkcounter}{1}%
67   \global\expandafter\edef\csname \chunkcount\endcsname{\arabic{fangle@chunkcounter}}%
68   \edef\chunkname{\lst@intname-\arabic{fangle@chunkcounter}}%
69   \edef\firstchunkname{\lst@intname-1}%
70   \addtocounter{fangle@chunkcounter}{1}%
71   \edef\nextchunkname{\lst@intname-\arabic{fangle@chunkcounter}}%
72   \@ifundefined{r@label-\nextchunkname}{\let\nextchunkname\relax}{}%
73   \sublabel{\chunkname}%
74 % define this label for every chunk instance, so we
75 % can tell when we are the last chunk of this name
76   \label{label-\chunkname}%
77   \addcontentsline{lol}{lstlisting}{\lst@name~[\protect\subpageref{\chunkname}]}%
78   \nwmargintag{%
79     {%
80       \nwtagstyle{}%
81       \subpageref{\chunkname}%
82     }%
83   }%
85   \moddef{%
86     {\lst@name}%
87     {%
88       \nwtagstyle{}\/%
89       \@ifundefined{fangle@chunk@params}{}{%
90         (\fangle@chunk@params)%
91       }%
92       [\csname \chunkcount\endcsname]~%
93       \subpageref{\firstchunkname}%
94     }%
95     \@ifundefined{fangle@chunk@append}{}{%
96     \ifx{}\fangle@chunk@append{x}\else%
97         ,~add~to~\fangle@chunk@append%
98     \fi%
99     }%
100 \global\def\fangle@chunk@append{}%
101 \lstset{append=x}%
102   }%
104   \ifx\relax\prevchunkname\endmoddef\else\plusendmoddef\fi%
105 %  \nwstartdeflinemarkup%
106   \nwprevnextdefs{\prevchunkname}{\nextchunkname}%
107 %  \nwenddeflinemarkup%
109 %\lst@BeginAspect{fangle}
110 %\lst@Key{fangle}{true}[t]{\lstKV@SetIf{#1}{true}}
111 \lst@AddToHookExe{PreSet}{\global\let\lst@intname\lst@chunkname}
112 \lst@AddToHook{Init}{}%\fangle@caption}
113 %\lst@EndAspect
114 \def\chunkref@args#1,{%
115   \def\arg{#1}%
116   \lst@ReplaceIn\arg\lst@filenamerpl%
117   \arg%
118   \@ifnextchar){\relax}{, \chunkref@args}%
120 \newcommand\chunkref[2][0]{%
121   \@ifnextchar({\chunkref@i{#1}{#2}}{\chunkref@i{#1}{#2}()}%
123 \def\chunkref@i#1#2(#3){%
124   \def\zero{0}%
125   \def\chunk{#2}%
126   \def\chunkno{#1}%
127   \def\chunkargs{#3}%
128   \ifx\chunkno\zero%
129     \def\chunkname{#2-1}%
130   \else%
131     \def\chunkname{#2-\chunkno}%
132   \fi%
133   \let\lst@arg\chunk%
134   \lst@ReplaceIn\chunk\lst@filenamerpl%
135   \LA{%\moddef{%
136     {\chunk}%
137     {%
138       \nwtagstyle{}\/%
139       \ifx\chunkno\zero%
140       \else%
141       [\chunkno]%
142       \fi%
143       \ifx\chunkargs\empty%
144       \else%
145         (\chunkref@args #3,)%
146       \fi%
147       ~\subpageref{\chunkname}%
148     }%
149   }%
150   \RA%\endmoddef%
153 %\makeatother
154 EndPreamble
156 Style Chunk
157   LatexType             Command
158   LatexName             Chunk
159   Margin                First_Dynamic
160   LeftMargin            Chunk:xxx
161   LabelSep              xx
162   LabelType             Static
163   LabelString           "Chunk:"
164   Align                 Left
165   PassThru              1
167   LabelFont
168     Family              Sans
169     Size                Large
170     Series              Bold
171     Shape               Italic
172     Color               red
173   EndFont
176 InsetLayout Chunkref
177   LyxType               charstyle
178   LatexType             Command
179   LatexName             chunkref
180   PassThru              1
181   LabelFont             
182     Shape               Italic
183     Color               red
184   EndFont