1 #\DeclareLyXModule{Newfangle Literate Listings}
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
10 #newfangle - fully featured notangle replacement in awk
12 # Copyright (C) 2009 Sam Liddicott <sam@liddicott.com>
14 # This program is free software: you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation, either version 3 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
32 \usepackage{listings}%
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={=<}{>}}%
42 %somehow re-defining maketitle gives us a left-aligned title
43 %which is extactly what our specially formatted title needs!
44 \global\let\newfangle@lst@maketitle\lst@maketitle%
45 \global\def\lst@maketitle{}%
47 \lstset{title={\newfanglecaption},name=#1}%
48 \global\edef\lst@chunkname{\lst@intname}%
50 \def\lst@chunkname{\empty}%
51 \lst@Key{params}\relax{\def\newfangle@chunk@params{#1}}%
52 \lst@Key{append}\relax{\def\newfangle@chunk@append{#1}}%
53 \def\newfanglecaption{\protect\newfangle@caption}%
54 \newcounter{newfangle@chunkcounter}%
55 \def\newfangle@caption{%
56 \edef\chunkcount{lst-chunk-\lst@intname}%
57 \@ifundefined{\chunkcount}{%
58 \expandafter\gdef\csname \chunkcount\endcsname{0}%
59 \setcounter{newfangle@chunkcounter}{\csname \chunkcount\endcsname}%
60 \let\prevchunkname\relax%
62 \setcounter{newfangle@chunkcounter}{\csname \chunkcount\endcsname}%
63 \edef\prevchunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
65 \addtocounter{newfangle@chunkcounter}{1}%
66 \global\expandafter\edef\csname \chunkcount\endcsname{\arabic{newfangle@chunkcounter}}%
67 \edef\chunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
68 \edef\firstchunkname{\lst@intname-1}%
69 \addtocounter{newfangle@chunkcounter}{1}%
70 \edef\nextchunkname{\lst@intname-\arabic{newfangle@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}]}%
80 \subpageref{\chunkname}%
88 \@ifundefined{newfangle@chunk@params}{}{%
89 (\newfangle@chunk@params)%
91 [\csname \chunkcount\endcsname]~%
92 \subpageref{\firstchunkname}%
94 \@ifundefined{newfangle@chunk@append}{}{%
95 \ifx{}\newfangle@chunk@append{x}\else%
96 ,~add~to~\newfangle@chunk@append%
99 \global\def\newfangle@chunk@append{}%
103 \ifx\relax\prevchunkname\endmoddef\else\plusendmoddef\fi%
104 % \nwstartdeflinemarkup%
105 \nwprevnextdefs{\prevchunkname}{\nextchunkname}%
106 % \nwenddeflinemarkup%
108 %\lst@BeginAspect{newfangle}
109 %\lst@Key{newfangle}{true}[t]{\lstKV@SetIf{#1}{true}}
110 \lst@AddToHookExe{PreSet}{\global\let\lst@intname\lst@chunkname}
111 \lst@AddToHook{Init}{}%\newfangle@caption}
113 \def\chunkref@args#1,{%
115 \lst@ReplaceIn\arg\lst@filenamerpl%
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){%
128 \def\chunkname{#2-1}%
130 \def\chunkname{#2-\chunkno}%
133 \lst@ReplaceIn\chunk\lst@filenamerpl%
142 \ifx\chunkargs\empty%
144 (\chunkref@args #3,)%
146 ~\subpageref{\chunkname}%