summaryrefslogtreecommitdiff
path: root/support/splint/tex/noweb.sty
blob: a0f2c7cf437d1a1e39a35ea2b61fc0b2d44480c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
% Copyright 2012-2020, Alexander Shibakov
% This file is part of SPLinT
%
% SPLinT is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% SPLinT is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with SPLinT.  If not, see <http://www.gnu.org/licenses/>.

\def\stseclap{\rightskip=0pt % get out of C mode (cf. \B)
    \sfcode`;=1500 \pretolerance 200 \hyphenpenalty 50 \exhyphenpenalty 50
    \noindent{\let\*=\empty\llap{\tentitle\setsafesecno{\secno}\quad}}% push it to the margins
    \putxref{\MNM}{}{}.% generate a location reference for noweb cross referencing style
    \ifpdftex\smash{\raise\baselineskip\hbox to0pt{%
        \let\*=\empty\ifmakepdf\pdfdest num \secstar fith\fi}}% this space is a bug in the original cwebmac.tex; AS
    \else
        \ifpdf
            \smash{\raise\baselineskip
                \hbox to0pt{%
                    \let\*=\empty\special{%
                    pdf: dest (\romannumeral\secstar) [ @thispage /FitH @ypos ]%
                    }%
                }%
            }%
        \fi
    \fi
}

\def\stsecchap#1#2{\rightskip=0pt % get out of C mode (cf. \B)
    \sfcode`;=1500 \pretolerance 200 \hyphenpenalty 50 \exhyphenpenalty 50
%
    \ifnum#1>\z@ % the `level' if this section is below that of a chapter
        \noindent{\let\*=\empty\llap{\tentitle
            \setsafesecno{\secno}\quad}}{\ttl #2}% push it to the margins
        \putxref{\MNM}{}{}.% generate a location reference for noweb cross referencing style
        \smallskip\noindent
    \else
        \setchaptertitle{\secno}{#2}%
        \putxref{\MNM}{0}{}.% generate a chapter location reference for noweb cross referencing style
    \fi
%
    \ifpdftex\smash{\raise\baselineskip\hbox to0pt{%
       \let\*=\empty\ifmakepdf\pdfdest num \secstar fith\fi}}% this space is a bug in the original cwebmac.tex; AS
    \else\ifpdf\smash{\raise\baselineskip\hbox to0pt{%
       \let\*=\empty\special{%
         pdf: dest (\romannumeral\secstar) [ @thispage /FitH @ypos ]}}}\fi\fi}

\def\setchaptertitle#1#2{%
    \global\chapterheadtrue
    \null
    {\ninepoint\tempda=\baselineskip
     \multiply\tempda by 17
     \vskip\tempda plus\baselineskip minus 2pt
    }%
    \vbox to 0pt{%
        \vss
        \tabskip=0pt plus 1 fil
        \halign to\hsize{%
            \hfil##\tabskip=0pt\cr
            \hugetitle\setsafechapno{#1}\cr
            \noalign{\vskip 1pc}%
            \midtitle#2\cr
        }%
    }%
    \prevdepth=0pt
    \bigskip
    \noindent{\let\*=\empty\llap{\tentitle\setsafesecno{\secno}\quad}}%
}

\let\startsection\stseclap

\expandafter\def\expandafter\oldB\expandafter{\oldB\global\csectionstarttrue\putxref{\BBB}{}{}.}

\def\putxref#1#2.{% it is important that #2 contains at least two braced groups (as in {}{})
                  % otherwise the parameter scanning mechanism will strip the outside braces
    \ifxreflocal
        {\edef\next{\write\xrefstream{\nx\nx\nx#1{\nx\the\nx\pageno}{\secno}#2\harmlesscomment}}\next}%
    \fi
}

\def\X#1:#2\X{\ifmmode\gdef\XX{\null$\null}\else\gdef\XX{}\fi %$% section name
  \XX$\langle\,${\let\I=\ne#2\eightrm\kern.5em
    \ifacro{\pdfnote#1.}\else#1\fi}$\,\rangle$\putpathindex{\secno}{#1}\XX\putxrefx{#1}{#2}}

\def\putxrefx#1#2{%
    \ifxreflocal
        {\def\sname{#2}%
         \def\stripprefix##1>{ }%
         \let\*\empty
         \edef\next{\write\xrefstream{\nx\nx\nx\XXX{\nx\the\nx\pageno}{\secno}{#1}\harmlesscomment
             \expandafter\stripprefix\meaning\sname}}\next}%
    \fi
}

\newif\ifcsectionstart % does this section name appear at the beginning of a \Cee\ section?

\def\putpathindex#1#2{%
    \ifcsectionstart
        \global\csectionstartfalse\strut
        \vadjust{%
            \setbox0=\hbox{\strut}%
            \kern-\dp0 
            \vbox to 0pt{
                \vss
                \hbox to\pagewidth{%
                    \hfil\rlap{%
                        \let\*\empty
                        \kern1em \setpathlinks{#1}{#2}%
                    }%
                }%
            }%
            \setbox0=\hbox{\strut}%
            \kern\dp0 
        }%
    \fi
}

\def\setpathlinks#1#2{%
    \expandafter\ifx\csname xchain[#1][#2]down\endcsname\relax
        \expandafter\ifx\csname xchain[#1][#2]up\endcsname\relax
        \else
            \setuplink{#1}{#2}{\hfil##\hfil}%
        \fi
    \else
        \expandafter\ifx\csname xchain[#1][#2]up\endcsname\relax
            \setdownlink{#1}{#2}{\hfil##\hfil}%
        \else
            \setuplink{#1}{#2}{##\hfil}
            \setdownlink{#1}{#2}{\hfil##}%
        \fi
    \fi
}

\def\setdownlink#1#2#3{%
    {\setbox0\vtop{
        \sevenpoint\halign{#3\cr
            \xref@nowebsection{\csname xchain[#1][#2]down\endcsname}\cr
            \relax\ifacro
                \pdflink{\csname xchain[#1][#2]down\endcsname}{\raise3.5pt\hbox{$\scriptscriptstyle\bigtriangledown$}}%
            \else
                \raise3.5pt\hbox{$\scriptscriptstyle\bigtriangledown$}%
            \fi
            \cr
        }
    }%
    \dp0=0pt \box0}%
}

\def\setuplink#1#2#3{%
    {\setbox0\vbox{
        \sevenpoint\halign{#3\cr
            \relax\ifacro
                \pdflink{\csname xchain[#1][#2]up\endcsname}{\lower1.3pt\hbox{$\scriptscriptstyle\bigtriangleup$}}%
            \else
                \lower1.3pt\hbox{$\scriptscriptstyle\bigtriangleup$}%
            \fi
            \cr
            \xref@nowebsection{\csname xchain[#1][#2]up\endcsname}\cr
        }
    }%
    \box0}%
}

\def\makenote{%
    \addtokens\toksB{\noexpand\pdflink{\the\toksC}{\setsafesecno{\the\toksC}}}%
    \toksC={}\global\countC=0
}

% redefine \pdflink; N.B.: the semantics of this command sequence have changed, since now
% #2 is not ignored (as it would have been if using pdftex); instead it becomes a typesetting
% template for the link

\ifpdftex
  \ifx\pdfannotlink\undefined\let\pdfannotlink\pdfstartlink\fi% for pdfTeX 0.14
  \def\pdflink#1#2{\hbox{\pdfannotlink height\ht\strutbox depth\dp\strutbox
    attr{/Border [0 0 0]} goto num #1 \BlueGreen #2\Black\pdfendlink}}
\else\def\pdflink#1#2{\setbox0=\hbox{\special{pdf: bc [ \pdflinkcolor ]}{#2}%
    \special{pdf: ec}}\special{pdf: ann width \thewidth height \theheight
      depth \thedepth << /Type /Annot /Subtype /Link
      /Border [0 0 0] /A << /S /GoTo /D (\romannumeral#1) >> >>}\box0\relax}\fi

% while typesetting the table of contents, show the actual section numbers
% TODO: introduce section accounting for book style typesetting.

\def\contentsline#1#2#3#4#5{\ifnum#2=0 \smallbreak\fi
    \line{\consetup{#2}#1
      \rm\leaders\hbox to .5em{.\hfil}\hfil
      \ \ifacro\pdflink{#3}{#3}\else#3\fi\hbox to3em{\hss#4}}}

\newif\ifxreflocal % are we generating references in noweb style (as page no., position pairs)?

\newread\trystream % generic stream to test for an existence of a file

\def\readlxrefs{% see if the local reference file exists
    \openin\trystream=\jobname.xxr
    \ifeof\trystream
    \else
        \closein\trystream
        \input \jobname.xxr
    \fi
}

\newif\ifcsecactive

\def\BBB#1#2#3#4{% #1 is the page number
             % #2 is the section number
    \csecactivetrue
}

\def\lxrcurrentpage{-1}

\newcount\lxrcurrentindex
\newcount\chaptercount

\lxrcurrentindex=\z@

\def\MNM#1#2#3#4{% #1 is the page number
                 % #2 is the section number
                 % #3 is the chapter flag or empty
                 % #4 is reserved
    \ifnum#1=\lxrcurrentpage
    \else
        \lxrcurrentindex=\z@
        \def\lxrcurrentpage{#1}%
    \fi
    {%
        \tomodalpha\lxrcurrentindex\next
        \def\nnext{\expandafter\noexpand\csname lxref[#2]\endcsname}%
        \yystringempty{#3}{%
            \let\nnnext\empty
        }{%
            \global\advance\chaptercount\@ne
            \def\nnnext{\the\chaptercount}%
        }%
        \edef\next{\def\nnext{{#1}{\next}{\nnnext}}}%
        \expandafter
    }\next
    \advance\lxrcurrentindex\@ne
}

\def\xref@nowebsection#1{% translate the \CWEB\ section number into a \noweb\ reference
    \expandafter\xr@f@nowebsection\romannumeral-1\csname lxref[#1]\endcsname.%
}

\def\xr@f@nowebsection#1#2#3.{%
    {#1}{#2}%
}

\def\xref@chapter#1{% translate the \CWEB\ section number into a chapter number
    \expandafter\xr@f@chapter\romannumeral-1\csname lxref[#1]\endcsname.%
}

\def\xr@f@chapter#1#2#3#4.{%
    \yystringempty{#3}{{000}}{%
        {#3}%
    }%
}

\def\setsafesecno#1{%
    \expandafter\ifx\csname lxref[#1]\endcsname\relax
        #1%
    \else
        \xref@nowebsection{#1}%
    \fi
}

\def\setsafechapno#1{%
    \expandafter\ifx\csname lxref[#1]\endcsname\relax
        #1%
    \else
        \xref@chapter{#1}%
    \fi
}

\def\tomodalpha#1#2{%
    \tempca=#1
    \let#2\empty
    \bloop
        \tempcb=\tempca
        \divide\tempca by 26
        \tempcc=\tempca
        \multiply\tempcc by 26
        \advance\tempcb by -\tempcc
        \advance\tempcb by `\a
        \uccode`\.=\tempcb
        \uppercase{\edef#2{.#2}}%
        \advance\tempca\m@ne
    \ifnum\tempca<\z@
    \else
    \repeat
}

\def\XXX#1#2#3{%
    \ifcsecactive % previous command was \BBB (produced by the \B macro)
%        \expandafter\def\csname lxref[][]\endcsname{}%
        \csecactivefalse
    \fi
}

\def\SSS{\let\XXX\xxxchain} % reference separator

\def\lxrefseparator{%
    \write\xrefstream{\nx\SSS}%
}

\def\xxxchain#1#2#3{%
    \xxxch@in{}{}#3, .%
}

\newif\iftracenowebchains

\def\xxxch@in#1#2#3, #4.{% #1 is the chain head
                         % #2 is the chain previous
                         % #3 is the chain next
                         % #4 is the remaining chain
    \yystringempty{#1}{% start the chain
        \yystringempty{#3}{%
            \errmessage{The reference chain (#3) is malformed.}%
        }{%
            \xxxch@in{#3}{}#4, .%
        }%
    }{%
        \yystringempty{#2}{% potentially the second link
            \yystringempty{#3}{% this is a one link chain
            }{% this is a second link
                \expandafter\def\csname xchain[#1][#1]down\endcsname{#3}%
                \expandafter\def\csname xchain[#3][#1]up\endcsname{#1}%
                \iftracenowebchains\message{s(#1.->#3 #3->#1.)}\fi
                \xxxch@in{#1}{#3}#4, .%
            }%
        }{% this is the middle or the end of the chain
            \yystringempty{#3}{% this is the end of the chain
            }{%
                \expandafter\def\csname xchain[#3][#1]up\endcsname{#2}%
                \expandafter\def\csname xchain[#2][#1]down\endcsname{#3}%
                \iftracenowebchains\message{m(#1)(#2->#3 #3->#2)}\fi
                \xxxch@in{#1}{#3}#4, .%
            }%
        }%
    }%
}

% replace the default reference setting macros in gindex.sty;
% typeset references so that, for example, \(77){8, 9} becomes 
% \(\pdflink{77}{8c}), \(\pdfpagelink{9}) provided section 77 
% is section 8c by noweb's reckoning.

\def\consumeonexref#1#2{% #1 is the accumulated references
                        % #2 is the section number
    \expandafter\ifx\csname lxref[#2]\endcsname\relax
        \errmessage{Section #2 does not have a local index.}%
    \else
        \yybreak{\expandafter\c@nsumeonexref\romannumeral-1\xref@nowebsection{#2}{#2}{#1}}%
    \yycontinue
}

\def\c@nsumeonexref#1#2#3#4#5{% #1 is the page number of the beginning of the section
                              % #2 is the (\noweb) section number (within the page) where the term appears
                              % #3 is the (\CWEB) section number there the term appears
                              % #4 is the accumulated references
                              % #5 is a list of pages where this term appears
    \c@nsume@nexref{#1}{#2}{#3}{#4}{}#5, \end
}

\def\c@nsume@nexref#1#2#3#4#5#6, {% #1 is the page number of the beginning of the section
                                  % #2 is the (\noweb) section number (within the page) where the term appears
                                  % #3 is the (\CWEB) section number where the term appears
                                  % #4 is a total list of processed references
                                  % #5 is a local list of processed references
                                  % #6 is a page number from the list
    \ifnum#1=#6 % the term appears on the same page where the section begins
        \yybreak{\c@nsume@nexr@f{#1}{#2}{#3}#4{#5}{\compoundlink{#3}{#1#2}}}%
    \else
        \yybreak{\c@nsume@nexr@f{#1}{#2}{#3}#4{#5}{\pagelink{#6}}}%
    \yycontinue
}

\def\c@nsume@nexr@f#1#2#3#4#5#6#7#8{%
    \c@nsume@n@xr@f{#1}{#2}{#3}{{#4}{#5}{#6}}{#7, #5#8#4}%
}

\def\c@nsume@n@xr@f#1#2#3#4#5#6\end{% no more page numbers
    \yystringempty{#6}{%
        \attachlocallist{#1}{#2}{#3}#4{#5}%
    }{%
        \c@nsume@nexref{#1}{#2}{#3}{#4}{#5}#6\end
    }%
}

\def\attachlocallist#1#2#3#4#5#6#7{%
    \grabfinexrefs{#6#7}%
}