Add top-level tunnelling so a makefile included in a makefile isn't quoted, etc
[newfangle.git] / fangle.module
blob766455ada3026b114548c4cd8d30bbddcf5029b1
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-2010 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/>.
29 Format 11
31 AddToPreamble
32 \usepackage{listings}%
33 \usepackage{noweb}%
34 \usepackage{xargs}%
35 \renewcommand{\code}[1]{\texttt{#1}}%
36 \lstnewenvironment{Chunk}{\relax}{\relax}%
37 \lstset{numbers=left, stepnumber=5, numbersep=5pt,
38         breaklines=false,basicstyle=\ttfamily,
39         numberstyle=\tiny, language=C}%
40 \lstset{escapeinside={=<}{>}}%
41 %\makeatletter
42 %somehow re-defining maketitle gives us a left-aligned title
43 %which is extactly what our specially formatted title needs!
44 \global\let\fangle@lst@maketitle\lst@maketitle%
45 \global\def\lst@maketitle{}%
46 \def\Chunk#1{%
47   \lstset{title={\fanglecaption},name=#1}%
48   \global\edef\lst@chunkname{\lst@intname}%
50 \def\lst@chunkname{\empty}%
51 \lst@Key{params}\relax{\def\fangle@chunk@params{#1}}%
52 \lst@Key{append}\relax{\def\fangle@chunk@append{#1}}%
53 \def\fanglecaption{\protect\fangle@caption}%
54 \newcounter{fangle@chunkcounter}%
55 \def\fangle@caption{%
56   \edef\chunkcount{lst-chunk-\lst@intname}%
57   \@ifundefined{\chunkcount}{%
58     \expandafter\gdef\csname \chunkcount\endcsname{0}%
59     \setcounter{fangle@chunkcounter}{\csname \chunkcount\endcsname}%
60     \let\prevchunkname\relax%
61   }{%
62     \setcounter{fangle@chunkcounter}{\csname \chunkcount\endcsname}%
63     \edef\prevchunkname{\lst@intname-\arabic{fangle@chunkcounter}}%
64   }%
65   \addtocounter{fangle@chunkcounter}{1}%
66   \global\expandafter\edef\csname \chunkcount\endcsname{\arabic{fangle@chunkcounter}}%
67   \edef\chunkname{\lst@intname-\arabic{fangle@chunkcounter}}%
68   \edef\firstchunkname{\lst@intname-1}%
69   \addtocounter{fangle@chunkcounter}{1}%
70   \edef\nextchunkname{\lst@intname-\arabic{fangle@chunkcounter}}%
71   \@ifundefined{r@label-\nextchunkname}{\let\nextchunkname\relax}{}%
72   \sublabel{\chunkname}%
73 % define this label for every chunk instance, so we
74 % can tell when we are the last chunk of this name
75   \label{label-\chunkname}%
76   \addcontentsline{lol}{lstlisting}{\lst@name~[\protect\subpageref{\chunkname}]}%
77   \nwmargintag{%
78     {%
79       \nwtagstyle{}%
80       \subpageref{\chunkname}%
81     }%
82   }%
84   \moddef{%
85     {\lst@name}%
86     {%
87       \nwtagstyle{}\/%
88       \@ifundefined{fangle@chunk@params}{}{%
89         (\fangle@chunk@params)%
90       }%
91       [\csname \chunkcount\endcsname]~%
92       \subpageref{\firstchunkname}%
93     }%
94     \@ifundefined{fangle@chunk@append}{}{%
95     \ifx{}\fangle@chunk@append{x}\else%
96         ,~add~to~\fangle@chunk@append%
97     \fi%
98     }%
99 \global\def\fangle@chunk@append{}%
100 \lstset{append=x}%
101   }%
103   \ifx\relax\prevchunkname\endmoddef\else\plusendmoddef\fi%
104 %  \nwstartdeflinemarkup%
105   \nwprevnextdefs{\prevchunkname}{\nextchunkname}%
106 %  \nwenddeflinemarkup%
108 %\lst@BeginAspect{fangle}
109 %\lst@Key{fangle}{true}[t]{\lstKV@SetIf{#1}{true}}
110 \lst@AddToHookExe{PreSet}{\global\let\lst@intname\lst@chunkname}
111 \lst@AddToHook{Init}{}%\fangle@caption}
112 %\lst@EndAspect
113 \def\chunkref@args#1,{%
114   \def\arg{#1}%
115   \lst@ReplaceIn\arg\lst@filenamerpl%
116   \arg%
117   \@ifnextchar){\relax}{, \chunkref@args}%
119 \newcommand\chunkref[2][0]{%
120   \@ifnextchar({\chunkref@i{#1}{#2}}{\chunkref@i{#1}{#2}()}%
122 \def\chunkref@i#1#2(#3){%
123   \def\zero{0}%
124   \def\chunk{#2}%
125   \def\chunkno{#1}%
126   \def\chunkargs{#3}%
127   \ifx\chunkno\zero%
128     \def\chunkname{#2-1}%
129   \else%
130     \def\chunkname{#2-\chunkno}%
131   \fi%
132   \let\lst@arg\chunk%
133   \lst@ReplaceIn\chunk\lst@filenamerpl%
134   \LA{%\moddef{%
135     {\chunk}%
136     {%
137       \nwtagstyle{}\/%
138       \ifx\chunkno\zero%
139       \else%
140       [\chunkno]%
141       \fi%
142       \ifx\chunkargs\empty%
143       \else%
144         (\chunkref@args #3,)%
145       \fi%
146       ~\subpageref{\chunkname}%
147     }%
148   }%
149   \RA%\endmoddef%
152 %\makeatother
153 EndPreamble
155 Style Chunk
156   LatexType             Command
157   LatexName             Chunk
158   Margin                First_Dynamic
159   LeftMargin            Chunk:xxx
160   LabelSep              xx
161   LabelType             Static
162   LabelString           "Chunk:"
163   Align                 Left
164   PassThru              1
166   LabelFont
167     Family              Sans
168     Size                Large
169     Series              Bold
170     Shape               Italic
171     Color               red
172   EndFont
175 InsetLayout Chunkref
176   LyxType               charstyle
177   LatexType             Command
178   LatexName             chunkref
179   PassThru              1
180   LabelFont             
181     Shape               Italic
182     Color               red
183   EndFont