Use \chunkref, use pdf hyperrefs
[newfangle.git] / newfangle.module
bloba8ce657e868f31bac88bdf483e35338db675ecac
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 \renewcommand{\code}[1]{\texttt{#1}}
17 \lstset{numbers=left, stepnumber=5, numbersep=5pt,
18         breaklines=false,basicstyle=\ttfamily,
19         numberstyle=\tiny, language=C}
20 \lstset{escapeinside={=<}{>}}
21 %\makeatletter
22 %somehow re-defining maketitle gives us a left-aligned title
23 %which is extactly what our specially formatted title needs!
24 \global\let\newfangle@lst@maketitle\lst@maketitle%
25 \global\def\lst@maketitle{}%
26 \def\Chunk#1{%
27   \lstset{title={\newfanglecaption},name=#1}%
28   \global\edef\lst@chunkname{\lst@intname}%
30 \def\lst@chunkname{\empty}%
31 \lst@Key{params}\relax{\def\newfangle@chunk@params{#1}}%
32 \def\newfanglecaption{\protect\newfangle@caption}%
33 \newcounter{newfangle@chunkcounter}%
34 \def\newfangle@caption{%
35   \edef\chunkcount{lst-chunk-\lst@intname}%
36   \@ifundefined{\chunkcount}{%
37     \expandafter\gdef\csname \chunkcount\endcsname{0}%
38     \setcounter{newfangle@chunkcounter}{\csname \chunkcount\endcsname}%
39     \let\prevchunkname\relax%
40   }{%
41     \setcounter{newfangle@chunkcounter}{\csname \chunkcount\endcsname}%
42     \edef\prevchunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
43   }%
44   \addtocounter{newfangle@chunkcounter}{1}%
45   \global\expandafter\edef\csname \chunkcount\endcsname{\arabic{newfangle@chunkcounter}}%
46   \edef\chunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
47   \edef\firstchunkname{\lst@intname-1}%
48   \addtocounter{newfangle@chunkcounter}{1}%
49   \edef\nextchunkname{\lst@intname-\arabic{newfangle@chunkcounter}}%
50   \@ifundefined{r@label-\nextchunkname}{\let\nextchunkname\relax}{}%
51   \sublabel{\chunkname}%
52 % define this label for every chunk instance, so we
53 % can tell when we are the last chunk of this name
54   \label{label-\chunkname}%
55   \addcontentsline{lol}{lstlisting}{\lst@name~[\protect\subpageref{\chunkname}]}%
56   \nwmargintag{%
57     {%
58       \nwtagstyle{}%
59       \subpageref{\chunkname}%
60     }%
61   }%
63   \moddef{%
64     {\lst@name}%
65     {%
66       \nwtagstyle{}\/[\csname \chunkcount\endcsname]~%%
67       \subpageref{\firstchunkname}%
68     }%
69   }%
71   \ifx\relax\prevchunkname\endmoddef\else\plusendmoddef\fi%
72 %  \nwstartdeflinemarkup%
73   \nwprevnextdefs{\prevchunkname}{\nextchunkname}%
74 %  \nwenddeflinemarkup%
76 %\lst@BeginAspect{newfangle}
77 %\lst@Key{newfangle}{true}[t]{\lstKV@SetIf{#1}{true}}
78 \lst@AddToHookExe{PreSet}{\global\let\lst@intname\lst@chunkname}
79 \lst@AddToHook{Init}{}%\newfangle@caption}
80 %\lst@EndAspect
81 \newcommand\chunkref[2][1]{%
82   \def\chunk{#2}%
83   \def\chunkname{#2-#1}%
84   \let\lst@arg\chunk%
85   \lst@ReplaceIn\chunk\lst@filenamerpl%
86   \LA{%\moddef{%
87     {\chunk}%
88     {%
89       \nwtagstyle{}\/[#1]~%
90       \subpageref{\chunkname}%
91     }%
92   }%
93   \RA%\endmoddef%
96 %\makeatother
97 EndPreamble
99 Style Chunk
100   LatexType             Command
101   LatexName             Chunk
102   Margin                First_Dynamic
103   LeftMargin            Chunk:xxx
104   LabelSep              xx
105   LabelType             Static
106   LabelString           "Chunk:"
107   Align                 Left
108   PassThru              1
110   LabelFont
111     Family              Sans
112     Size                Large
113     Series              Bold
114     Shape               Italic
115     Color               red
116   EndFont
119 InsetLayout Chunkref
120   LyxType               charstyle
121   LatexType             Command
122   LatexName             chunkref
123   PassThru              1
124   LabelFont             
125     Shape               Italic
126     Color               red
127   EndFont