Add stateful non-recursive modes tracker ready for output formatting
[newfangle.git] / newfangle.module
blob098ea9950dcc5407b461c3b6219d1a3d1a118083
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 \lst@Key{append}\relax{\def\newfangle@chunk@append{#1}}%
35 \def\newfanglecaption{\protect\newfangle@caption}%
36 \newcounter{newfangle@chunkcounter}%
37 \def\newfangle@caption{%
38   \edef\chunkcount{lst-chunk-\lst@intname}%
39   \@ifundefined{\chunkcount}{%
40     \expandafter\gdef\csname \chunkcount\endcsname{0}%
41     \setcounter{newfangle@chunkcounter}{\csname \chunkcount\endcsname}%
42     \let\prevchunkname\relax%
43   }{%
44     \setcounter{newfangle@chunkcounter}{\csname \chunkcount\endcsname}%
45     \edef\prevchunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
46   }%
47   \addtocounter{newfangle@chunkcounter}{1}%
48   \global\expandafter\edef\csname \chunkcount\endcsname{\arabic{newfangle@chunkcounter}}%
49   \edef\chunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
50   \edef\firstchunkname{\lst@intname-1}%
51   \addtocounter{newfangle@chunkcounter}{1}%
52   \edef\nextchunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
53   \@ifundefined{r@label-\nextchunkname}{\let\nextchunkname\relax}{}%
54   \sublabel{\chunkname}%
55 % define this label for every chunk instance, so we
56 % can tell when we are the last chunk of this name
57   \label{label-\chunkname}%
58   \addcontentsline{lol}{lstlisting}{\lst@name~[\protect\subpageref{\chunkname}]}%
59   \nwmargintag{%
60     {%
61       \nwtagstyle{}%
62       \subpageref{\chunkname}%
63     }%
64   }%
66   \moddef{%
67     {\lst@name}%
68     {%
69       \nwtagstyle{}\/%
70       \@ifundefined{newfangle@chunk@params}{}{%
71         (\newfangle@chunk@params)%
72       }%
73       [\csname \chunkcount\endcsname]~%
74       \subpageref{\firstchunkname}%
75     }%
76     \@ifundefined{newfangle@chunk@append}{}{%
77     \ifx{}\newfangle@chunk@append{x}\else%
78         ,~add~to~\newfangle@chunk@append%
79     \fi%
80     }%
81 \global\def\newfangle@chunk@append{}%
82 \lstset{append=x}%
83   }%
85   \ifx\relax\prevchunkname\endmoddef\else\plusendmoddef\fi%
86 %  \nwstartdeflinemarkup%
87   \nwprevnextdefs{\prevchunkname}{\nextchunkname}%
88 %  \nwenddeflinemarkup%
90 %\lst@BeginAspect{newfangle}
91 %\lst@Key{newfangle}{true}[t]{\lstKV@SetIf{#1}{true}}
92 \lst@AddToHookExe{PreSet}{\global\let\lst@intname\lst@chunkname}
93 \lst@AddToHook{Init}{}%\newfangle@caption}
94 %\lst@EndAspect
95 \def\chunkref@args#1,{%
96   \def\arg{#1}%
97   \lst@ReplaceIn\arg\lst@filenamerpl%
98   \arg%
99   \@ifnextchar){\relax}{, \chunkref@args}%
101 \newcommand\chunkref[2][0]{%
102   \@ifnextchar({\chunkref@i{#1}{#2}}{\chunkref@i{#1}{#2}()}%
104 \def\chunkref@i#1#2(#3){%
105   \def\zero{0}%
106   \def\chunk{#2}%
107   \def\chunkno{#1}%
108   \def\chunkargs{#3}%
109   \ifx\chunkno\zero%
110     \def\chunkname{#2-1}%
111   \else%
112     \def\chunkname{#2-\chunkno}%
113   \fi%
114   \let\lst@arg\chunk%
115   \lst@ReplaceIn\chunk\lst@filenamerpl%
116   \LA{%\moddef{%
117     {\chunk}%
118     {%
119       \nwtagstyle{}\/%
120       \ifx\chunkno\zero%
121       \else%
122       [\chunkno]%
123       \fi%
124       \ifx\chunkargs\empty%
125       \else%
126         (\chunkref@args #3,)%
127       \fi%
128       ~\subpageref{\chunkname}%
129     }%
130   }%
131   \RA%\endmoddef%
134 %\makeatother
135 EndPreamble
137 Style Chunk
138   LatexType             Command
139   LatexName             Chunk
140   Margin                First_Dynamic
141   LeftMargin            Chunk:xxx
142   LabelSep              xx
143   LabelType             Static
144   LabelString           "Chunk:"
145   Align                 Left
146   PassThru              1
148   LabelFont
149     Family              Sans
150     Size                Large
151     Series              Bold
152     Shape               Italic
153     Color               red
154   EndFont
157 InsetLayout Chunkref
158   LyxType               charstyle
159   LatexType             Command
160   LatexName             chunkref
161   PassThru              1
162   LabelFont             
163     Shape               Italic
164     Color               red
165   EndFont