Stop expressing newfangle so much in terms on notangle
[newfangle.git] / newfangle.module
blob52f7bd87b7980b3c755f44b0b4b83f6a868aecd4
1 #\DeclareLyXModule{Newfangle Literate Listings}
2 #DescriptionBegin
3 #  Newfangle 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 newfangle awk script
9 #DescriptionEnd
11 Format 11
13 AddToPreamble
14 \usepackage{listings}%
15 \usepackage{noweb}%
16 \usepackage{xargs}%
17 \renewcommand{\code}[1]{\texttt{#1}}%
18 \lstnewenvironment{Chunk}{\relax}{\relax}%
19 \lstset{numbers=left, stepnumber=5, numbersep=5pt,
20         breaklines=false,basicstyle=\ttfamily,
21         numberstyle=\tiny, language=C}%
22 \lstset{escapeinside={=<}{>}}%
23 %\makeatletter
24 %somehow re-defining maketitle gives us a left-aligned title
25 %which is extactly what our specially formatted title needs!
26 \global\let\newfangle@lst@maketitle\lst@maketitle%
27 \global\def\lst@maketitle{}%
28 \def\Chunk#1{%
29   \lstset{title={\newfanglecaption},name=#1}%
30   \global\edef\lst@chunkname{\lst@intname}%
32 \def\lst@chunkname{\empty}%
33 \lst@Key{params}\relax{\def\newfangle@chunk@params{#1}}%
34 \def\newfanglecaption{\protect\newfangle@caption}%
35 \newcounter{newfangle@chunkcounter}%
36 \def\newfangle@caption{%
37   \edef\chunkcount{lst-chunk-\lst@intname}%
38   \@ifundefined{\chunkcount}{%
39     \expandafter\gdef\csname \chunkcount\endcsname{0}%
40     \setcounter{newfangle@chunkcounter}{\csname \chunkcount\endcsname}%
41     \let\prevchunkname\relax%
42   }{%
43     \setcounter{newfangle@chunkcounter}{\csname \chunkcount\endcsname}%
44     \edef\prevchunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
45   }%
46   \addtocounter{newfangle@chunkcounter}{1}%
47   \global\expandafter\edef\csname \chunkcount\endcsname{\arabic{newfangle@chunkcounter}}%
48   \edef\chunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
49   \edef\firstchunkname{\lst@intname-1}%
50   \addtocounter{newfangle@chunkcounter}{1}%
51   \edef\nextchunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
52   \@ifundefined{r@label-\nextchunkname}{\let\nextchunkname\relax}{}%
53   \sublabel{\chunkname}%
54 % define this label for every chunk instance, so we
55 % can tell when we are the last chunk of this name
56   \label{label-\chunkname}%
57   \addcontentsline{lol}{lstlisting}{\lst@name~[\protect\subpageref{\chunkname}]}%
58   \nwmargintag{%
59     {%
60       \nwtagstyle{}%
61       \subpageref{\chunkname}%
62     }%
63   }%
65   \moddef{%
66     {\lst@name}%
67     {%
68       \nwtagstyle{}\/%
69       \@ifundefined{newfangle@chunk@params}{}{%
70         (\newfangle@chunk@params)%
71       }%
72       [\csname \chunkcount\endcsname]~%%
73       \subpageref{\firstchunkname}%
74     }%
75   }%
77   \ifx\relax\prevchunkname\endmoddef\else\plusendmoddef\fi%
78 %  \nwstartdeflinemarkup%
79   \nwprevnextdefs{\prevchunkname}{\nextchunkname}%
80 %  \nwenddeflinemarkup%
82 %\lst@BeginAspect{newfangle}
83 %\lst@Key{newfangle}{true}[t]{\lstKV@SetIf{#1}{true}}
84 \lst@AddToHookExe{PreSet}{\global\let\lst@intname\lst@chunkname}
85 \lst@AddToHook{Init}{}%\newfangle@caption}
86 %\lst@EndAspect
87 \def\chunkref@args#1,{%
88   \def\arg{#1}%
89   \lst@ReplaceIn\arg\lst@filenamerpl%
90   \arg%
91   \@ifnextchar){\relax}{, \chunkref@args}%
93 \newcommand\chunkref[2][0]{%
94   \@ifnextchar({\chunkref@i{#1}{#2}}{\chunkref@i{#1}{#2}()}%
96 \def\chunkref@i#1#2(#3){%
97   \def\zero{0}%
98   \def\chunk{#2}%
99   \def\chunkno{#1}%
100   \def\chunkargs{#3}%
101   \ifx\chunkno\zero%
102     \def\chunkname{#2-1}%
103   \else%
104     \def\chunkname{#2-\chunkno}%
105   \fi%
106   \let\lst@arg\chunk%
107   \lst@ReplaceIn\chunk\lst@filenamerpl%
108   \LA{%\moddef{%
109     {\chunk}%
110     {%
111       \nwtagstyle{}\/%
112       \ifx\chunkno\zero%
113       \else%
114       [\chunkno]%
115       \fi%
116       \ifx\chunkargs\empty%
117       \else%
118         (\chunkref@args #3,)%
119       \fi%
120       ~\subpageref{\chunkname}%
121     }%
122   }%
123   \RA%\endmoddef%
126 %\makeatother
127 EndPreamble
129 Style Chunk
130   LatexType             Command
131   LatexName             Chunk
132   Margin                First_Dynamic
133   LeftMargin            Chunk:xxx
134   LabelSep              xx
135   LabelType             Static
136   LabelString           "Chunk:"
137   Align                 Left
138   PassThru              1
140   LabelFont
141     Family              Sans
142     Size                Large
143     Series              Bold
144     Shape               Italic
145     Color               red
146   EndFont
149 InsetLayout Chunkref
150   LyxType               charstyle
151   LatexType             Command
152   LatexName             chunkref
153   PassThru              1
154   LabelFont             
155     Shape               Italic
156     Color               red
157   EndFont