chiark / gitweb /
doc/sod.sty: Rewrite `@-' to cope with other things following.
[sod] / doc / sod.sty
1 %%% -*-latex-*-
2 %%%
3 %%% Styles and other hacking for the Sod manual
4 %%%
5 %%% (c) 2015 Straylight/Edgeware
6 %%%
7
8 %%%----- Licensing notice ---------------------------------------------------
9 %%%
10 %%% This file is part of the Sensible Object Design, an object system for C.
11 %%%
12 %%% SOD is free software; you can redistribute it and/or modify
13 %%% it under the terms of the GNU General Public License as published by
14 %%% the Free Software Foundation; either version 2 of the License, or
15 %%% (at your option) any later version.
16 %%%
17 %%% SOD is distributed in the hope that it will be useful,
18 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
19 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 %%% GNU General Public License for more details.
21 %%%
22 %%% You should have received a copy of the GNU General Public License
23 %%% along with SOD; if not, write to the Free Software Foundation,
24 %%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 \ProvidesPackage{sod}
27
28 %% More reference types.
29 \defxref{p}{part}
30
31 \def\instead#1#2{#1}
32
33 %% Other languages with special typesetting.
34 \def\Cplusplus{C\kern-\p@++}
35 \def\Csharp{C\#}
36
37 %% Special maths notation.
38 \def\chain#1#2{\mathsf{ch}_{#1}(#2)}
39 \def\chainhead#1#2{\mathsf{hd}_{#1}(#2)}
40 \def\chaintail#1#2{\mathsf{tl}_{#1}(#2)}
41
42 %% Other mathematical tweaks.
43 \let\implies\Rightarrow
44 \let\epsilon\varepsilon
45
46 %% A table heading cell.  Clone and hack \multicolumn.
47 \def\thd{\omit\@ifnextchar[\thd@{\thd@[l]}}
48 \def\thd@[#1]#2{%
49   \begingroup
50     \tab@multicol \tab@initread \let\tab@looped\tab@err@multi
51     \tab@preamble{}\def\tab@midtext{\bfseries#2}\tab@readpreamble{#1}%
52     \the\tab@preamble
53   \endgroup \ignorespaces
54 }
55
56 %% Unix manpage references.
57 \def\man#1#2{\textbf{#1}(#2)}
58
59 %% Listings don't need to be small.
60 \let\listingsize\relax
61
62 %% Notes for later.
63 \def\fixme#1{\leavevmode\marginpar{FIXME}\index{FIXME}[FIXME: #1]}
64
65 %% Diagram settings.
66 \tikzset{
67   every picture/.style={>=stealth, thick},
68   lit/.style={font=\sffamily}
69 }
70
71 %% Metavariables are italics without decoration.
72 \def\syntleft{\normalfont\itshape}
73 \let\syntright\empty
74
75 %% Literal code is in sans face.
76 \def\codeface{\upshape\sffamily}
77 \DeclareRobustCommand\code[1]{%
78   \ifmmode\hbox\else\leavevmode\fi%
79   {\normalfont\codeface\/#1\/}%
80 }
81 \def\ulitleft{\normalfont\codeface}
82 \let\ulitright\empty
83 \def\lit@i#1#2#3{\ifmmode\leavevmode\hbox\fi{#1{#3\/}#2}}
84
85 %% Conditionally enter maths mode.  Can't use \ensuremath here because we
86 %% aren't necessarily sure where the maths will actually end.
87 \let\m@maybe@end\relax
88 \def\m@maybe{\ifmmode\else$\let\m@maybe@end$\fi}
89
90 %% Standard syntax shortcuts.
91 \atdef <#1>{\synt{#1}\@scripts}
92 \atdef "#1"{\lit*{#1}\@scripts}
93 \atdef `#1'{\lit{#1}\@scripts}
94 \atdef |#1|{\lit*{#1}\@scripts}
95
96 %% A handy abbreviation; `\\' itself is too good to steal.
97 \atdef \\{\textbackslash}
98
99 %% Intercept grammar typesetting and replace the vertical bar with the
100 %% maths-font version.
101 \let\@@grammar\grammar
102 \def\grammar{\def\textbar{\hbox{$|$}}\@@grammar}
103
104 %% Collect super- and subscripts.  (Note that underscores are active for the
105 %% most part.)  When we're done, end maths mode if we entered it
106 %% conditionally.
107 \def\@scripts{\futurelet\@ch\@scripts@i}
108 \begingroup\lccode`\~=`\_\lowercase{\endgroup
109 \def\@scripts@i{\if1\ifx\@ch~1\else\ifx\@ch^1\else0\fi\fi%
110   \expandafter\@scripts@ii\else\expandafter\m@maybe@end\fi}}
111 \def\@scripts@ii#1#2{\m@maybe#1{#2}\@scripts}
112
113 %% Doubling characters, maybe.  Either way, chain onto \@scripts.
114 \def\dbl@maybe#1{\let\@tempa#1\futurelet\@ch\dbl@maybe@i}
115 \def\dbl@maybe@i{\m@maybe\ifx\@ch\@tempa\@tempa\!\@tempa%
116   \expandafter\@firstoftwo\expandafter\@scripts%
117   \else\@tempa\expandafter\@scripts\fi}
118
119 %% Lookahead without eating spaces.
120 \def\@ifnextchar@preserve#1#2#3{%
121   \let\want@= #1\def\@tempa{#2}\def\@tempb{#3}%
122   \futurelet\nch@\@ifnch@p%
123 }
124 \def\@ifnch@p{%
125   \ifx\want@\nch@\expandafter\@tempa\else\expandafter\@tempb\fi}
126
127 %% Extra syntax for Lisp templates.  These produce the maths-font versions of
128 %% characters, which should contrast well against the sans face used for
129 %% literals.
130 \atdef [{\dbl@maybe[}
131 \atdef ]{\dbl@maybe]}
132 \atdef {{\m@maybe\{\@scripts}
133 \atdef }{\m@maybe\}\@scripts}
134 \atdef ({\m@maybe(\@scripts}
135 \atdef ){\m@maybe)\@scripts}
136 \atdef !{\m@maybe|\@scripts}
137 \def\returns{\m@maybe\longrightarrow\m@maybe@end\hspace{0.5em}\ignorespaces}
138 \atdef >{\leavevmode\unskip\hspace{0.5em}\returns}
139 \atdef -{\leavevmode\hbox\bgroup\futurelet\ch@\@dash}
140 \def\@dash{%
141   \ifx\ch@>%
142     --\raisebox{.4pt}{>}%
143     \def\next@{\@firstoftwo{\egroup\penalty200\relax}}%
144   \else%
145     --%
146     \let\next@\egroup%
147   \fi\fi
148   \next@%
149 }
150
151 %% Comment setting.
152 \def\comment#1{\mbox{\normalfont\itshape\/#1\/}}
153 \atdef /*#1*/{/*\comment{#1}*/}
154 \def\@semis{\@ifnextchar;{;\@semis}\@semicomment}
155 \def\@semicomment#1\\{\comment{#1}\\}
156 \atdef ;#1\\{;\@semis}
157
158 %% Environment for setting programs.  Newlines are explicit, because
159 %% otherwise I need comments in weird places to make the vertical spacing
160 %% come out properly.  You can write `\obeylines' if you really want to.
161 \def\@prog{\let\prog@@cr\@tabcr\let\@tabcr\@progcr\codeface\tabbing}
162 \def\prog{\quote\@prog}
163 \def\endprog{\endtabbing\endquote}
164 \let\nprog\@prog
165 \let\endnprog\endtabbing
166 \def\ind{\quad\=\+\kill}
167 \def\@progcr{\futurelet\@tempa\@progcr@i}
168 {\def\:{\gdef\@progcr@sp}\: {\@progcr}}
169 \atdef~{\textasciitilde}
170 \def\@progcr@i{%
171   \ifx\@tempa\@sptoken\let\next@\@progcr@sp\else
172   \if1\ifx\@tempa[1\else
173       \ifx\@tempa*1\else
174       0\fi\fi
175     \let\next@\prog@@cr\else
176   \if1\ifx\@tempa+1\else
177       \ifx\@tempa-1\else
178       0\fi\fi
179     \let\next@\@progcr@ii\else
180   \let\next@\@progcr@a\fi\fi\fi
181   \next@}
182 \def\@progcr@a{\prog@@cr*{}\ignorespaces}
183 \def\@progcr@ii#1{\csname @progcr@#1\endcsname\ignorespaces}
184 \@namedef{@progcr@+}{\prog@@cr[\medskipamount]}
185 \@namedef{@progcr@-}{\prog@@cr*[\jot]}
186 \def\macsl{\`\textbackslash\hskip\leftmargin}
187
188 %% Put a chunk of text in a box.
189 \newenvironment{boxy}[1][\q@]{%
190   \savenotes
191   \dimen@\linewidth\advance\dimen@-1.2pt\advance\dimen@-2ex%
192   \medskip%
193   \vbox\bgroup\hrule\hbox\bgroup\vrule%
194   \vbox\bgroup\vskip1ex\hbox\bgroup\hskip1ex\minipage\dimen@%
195   \def\@temp{#1}\ifx\@temp\q@\else\leavevmode{\headfam\bfseries#1\quad}\fi%
196 }{%
197   \endminipage\hskip1ex\egroup\vskip1ex\egroup%
198   \vrule\egroup\hrule\egroup%
199   \medskip%
200   \spewnotes%
201 }
202
203 %% Lisp documentation machinery.
204 \def\definedescribecategory#1{%
205   \@ifnextchar[{\def@desc{#1}}{\def@desc{#1}[plain]}}
206 \def\def@desc#1[#2]#3{\@namedef{cat!#1}##1{#3}\@namedef{catsw!#1}{#2}}
207 \def\describecategoryname{\@ifnextchar[\@descname@i{\@descname@i[]}}
208 \def\@descname@i[#1]#2{%
209   \expandafter\let\expandafter\@tempa\csname cat!#2\endcsname%
210   \expandafter\let\expandafter\@tempb\csname modcat/#1\endcsname%
211   \ifx\@tempa\relax\@tempb{#2}\else\@tempa\@tempb\fi}
212 \def\@mod@dispatch#1#2{\csname #1/#2\endcsname}
213 \def\@desc@dispatch#1#2{%
214   \csname #1/%
215   \expandafter\ifx\csname catsw!#2\endcsname\relax plain%
216   \else \csname catsw!#2\endcsname \fi%
217   \endcsname%
218 }
219
220 \definedescribecategory{sym}{symbol}
221 \definedescribecategory{fun}{#1{function}}
222 \definedescribecategory{gf}{generic #1{function}}
223 \definedescribecategory{msg}{message}
224 \definedescribecategory{var}{variable}
225 \definedescribecategory{modvar}{module variable}
226 \definedescribecategory{const}{constant}
227 \definedescribecategory{meth}[method]{primary #1{method}}
228 \definedescribecategory{ar-meth}[method]{\code{:around} #1{method}}
229 \definedescribecategory{be-meth}[method]{\code{:before} #1{method}}
230 \definedescribecategory{af-meth}[method]{\code{:after} #1{method}}
231 \definedescribecategory{cls}{class}
232 \definedescribecategory{rst}{restart}
233 \definedescribecategory{ty}{type}
234 \definedescribecategory{type}{type}
235 \definedescribecategory{mac}{#1{macro}}
236 \definedescribecategory{feat}{feature macro}
237 \definedescribecategory{lmac}{local #1{macro}}
238 \definedescribecategory{parse}{parser spec}
239 \definedescribecategory{parseform}{parser form}
240 \definedescribecategory{opt}{option handler}
241 \definedescribecategory{optmac}{option macro}
242 \definedescribecategory{plug}{pluggable parser}
243 \def\nlret{\\\hspace{4em}\returns}
244
245 \@namedef{modcat/}#1{#1}
246 \@namedef{modlabel/}#1{#1}
247 \@namedef{modindex/}#1{#1@\noexpand\code{#1}}
248
249 \@namedef{modcat/setf}#1{\code{setf}-#1}
250 \@namedef{modlabel/setf}#1{setf/#1}
251 \@namedef{modindex/setf}#1{#1@\noexpand\code{#1}}
252
253 \@namedef{modcat/muffs}#1{#1}
254 \@namedef{modlabel/muffs}#1{*#1*}
255 \@namedef{modindex/muffs}#1{#1@\noexpand\code{*#1*}}
256
257 \@namedef{modcat/kwd}#1{#1}
258 \@namedef{modlabel/kwd}#1{:#1}
259 \@namedef{modindex/kwd}#1{#1@\noexpand\code{:#1}}
260
261 \@namedef{descargs/plain}#1{#1{}}
262 \@namedef{desclabel/plain}#1#2#3{#1:\@mod@dispatch{modlabel}{#2}{#3}}
263 \@namedef{descindex/plain}#1#2#3{%
264   \@mod@dispatch{modindex}{#2}{#3}!%
265   \protect\describecategoryname[#2]{#1}%
266 }
267
268 \@namedef{descargs/method}#1#2{#1{{#2}}}
269 \@namedef{desclabel/method}#1#2#3#4%
270   {#1:\@mod@dispatch{modlabel}{#2}{#3}(#4)}
271 \@namedef{descindex/method}#1#2#3#4{%
272   \@mod@dispatch{modindex}{#2}{#3}!%
273   \protect\describecategoryname[#2]{#1}%
274   \protect\fmtspecs{ specialized at }{#4}%
275 }
276
277 \def\q@{\q@}
278 \def\@setf{setf}
279
280 \def\fmtspecs#1#2{%
281   \if!#2!\else\count@\z@\toks@{}#1\@fmtspecs@i#2,\q@,\fi}
282 \def\@fmtspecs@i#1,{%
283   \def\@tempa{#1}%
284   \ifx\@tempa\q@%
285     \ifcase\count@(no args?!)%
286     \or% nothin' doin
287     \or \space and %
288     \else, and %
289     \fi%
290     \the\toks@%
291   \else%
292     \ifnum\count@>\@ne, \fi%
293     \the\toks@%
294     \toks@{\code{#1}}\advance\count@\@ne%
295     \expandafter\@fmtspecs@i%
296   \fi%
297 }
298
299 %% \parse@dhd{NEXT}[MOD]{CAT}{...}...[NAME]{SYNOPSIS}
300 %% call NEXT{MOD}{CAT}{{...}...}{NAME}{SYNOPSIS}
301 %%            #1   #2      #3     #4      #5
302 \def\@setf{setf}
303 \def\@starstar{**}
304 \def\parse@dhd#1{%  {NEXT}
305   \@ifnextchar[{\parse@dhd@a{#1}}{\parse@dhd@c{#1}}}
306 \def\parse@dhd@a#1[#2]#3{%  {NEXT}[MOD]{CAT}
307   \@desc@dispatch{descargs}{#3}{\parse@dhd@ab{#1}{#2}{#3}}}
308 \def\parse@dhd@ab#1#2#3#4{%  {NEXT}{MOD}{CAT}{{...}...}
309   \@ifnextchar[{\parse@dhd@ac{#1}{#2}{#3}{#4}}{\parse@dhd@ad{#1}{#2}{#3}{#4}}}
310 \def\parse@dhd@ac#1#2#3#4[#5]#6{%  {NEXT}{MOD}{CAT}{{...}...}[NAME]{SYNOPSIS}
311   #1{#2}{#3}{#4}{#5}{#6}}
312 \def\parse@dhd@ad#1#2#3#4#5{%  {NEXT}{MOD}{CAT}{{...}...}{NAME [ARGS...]}
313   \parse@dhd@ae{#1}{#2}{#3}{#4}{#5}#5 \q@}
314 \def\parse@dhd@ae#1#2#3#4#5#6 #7\q@{%
315   % {NEXT}{MOD}{CAT}{{...}...}{SYNSOPSIS}NAME [ARGS...]\q@
316   #1{#2}{#3}{#4}{#6}{#5}}
317
318 \def\parse@dhd@c#1#2{%  {NEXT}{CAT}
319   \@desc@dispatch{descargs}{#2}{\parse@dhd@cb{#1}{#2}}}
320 \def\parse@dhd@cb#1#2#3{%  {NEXT}{CAT}{{...}...}
321   \@ifnextchar[{\parse@dhd@cc{#1}{#2}{#3}}{\parse@dhd@cd{#1}{#2}{#3}}}
322 \def\parse@dhd@cc#1#2#3[#4]#5{%  {NEXT}{CAT}{{...}...}[NAME]{SYNOPSIS}
323   #1{}{#2}{#3}{#4}{#5}}
324 \def\parse@dhd@cd#1#2#3#4{%  {NEXT}{CAT}{{...}...}{SYNOPSIS}
325   \parse@dhd@ce{#1}{#2}{#3}{#4}#4 \q@}
326 \def\parse@dhd@ce#1#2#3#4#5 #6\q@{%
327   % {NEXT}{CAT}{{...}...}{SYNOPSIS}NAME [ARGS...]\q@
328   \def\temp@{#5}%
329   \ifx\@setf\temp@\def\next@{\parse@dhd@csetf{#1}{#2}{#3}{#4}#6 \q@}%
330   \else\def\temp@##1##2*##3\q@{\def\temp@{##1##3}}\temp@#5*\q@%
331     \ifx\temp@\@starstar\def\next@{\parse@dhd@cmuffs{#1}{#2}{#3}{#4}#5}%
332     \else\def\temp@##1##2\q@{\def\temp@{##1}}\temp@#5\q@%
333       \if:\temp@\def\next@{\parse@dhd@ckwd{#1}{#2}{#3}{#4}#5\q@}%
334       \else\def\next@{#1{}{#2}{#3}{#5}{#4}}\fi\fi\fi%
335   \next@%
336 }
337 \def\parse@dhd@csetf#1#2#3#4(#5 #6\q@{%
338   % {NEXT}{CAT}{{...}...}{SYNOPSIS}(NAME [ARGS...])\q@
339   #1{setf}{#2}{#3}{#5}{#4}}
340 \def\parse@dhd@cmuffs#1#2#3#4*#5*{%
341   % {NEXT}{CAT}{{...}...}{SYNOPSIS}*NAME*
342   #1{muffs}{#2}{#3}{#5}{#4}}
343 \def\parse@dhd@ckwd#1#2#3#4:#5\q@{%
344   % {NEXT}{CAT}{{...}...}{SYNOPSIS}:NAME\q@
345   #1{kwd}{#2}{#3}{#5}{#4}}
346
347 \newif\if@dheadfirst
348 \def\dhead{\parse@dhd\dhead@}
349 \def\dhead@#1#2#3#4#5{%  {MOD}{CAT}{{...}...}{NAME}{SYNOPSIS}
350   \if@dheadfirst\global\@dheadfirstfalse\else\relax\\*[\smallskipamount]\fi%
351   \phantomsection%
352   {\let\protect\@empty\let\@uscore\relax%
353    \edef\temp@{\@desc@dispatch{desclabel}{#2}{#2}{#1}{#4}#3}%
354    \def\@uscore{_\@gobble}\expandafter\message\expandafter{\temp@}%
355    \def\@uscore{-\@gobble}\expandafter\label\expandafter{\temp@}}%
356   {\begingroup\lccode`\~=`\_\lowercase{\endgroup\def~{_}}%
357    \protected@edef\@tempa##1{%
358      \noexpand\index{\@desc@dispatch{descindex}{#2}{#2}{#1}{#4}#3##1}}%
359    \toks@\expandafter{\@tempa{|)}}%
360    \toks\tw@\expandafter{\after@desc}%
361    \xdef\after@desc{\the\toks\tw@\the\toks@}%
362    \@tempa{|(}}%
363   \rlap{\hb@xt@\linewidth{\hfil\normalfont\bfseries
364       [\describecategoryname[#1]{#2}]}}%
365   #5%
366 }
367
368 \def\desc@begin#1{%
369   \let\saved@after@desc\after@desc%
370   \gdef\after@desc{}%
371   \normalfont%
372   \if@nobreak\else\par\goodbreak\fi%
373   \global\@dheadfirsttrue%
374   \begingroup%
375     \let\@endparenv\relax%
376     \clubpenalty\@M \widowpenalty\@M \interlinepenalty50%
377     \@prog#1\endtabbing%
378   \endgroup%
379   \penalty\@M\@afterheading%
380   \list{}{\rightmargin\z@\topsep\z@}\item%
381 }
382 \def\desc@end{\endlist\after@desc\global\let\after@desc\saved@after@desc}
383
384 \@namedef{describe*}#1{\desc@begin{#1}}
385 \expandafter\let\csname enddescribe*\endcsname\desc@end
386 \def\describe{\parse@dhd\desc@}
387 \def\desc@#1#2#3#4#5{\desc@begin{\dhead@{#1}{#2}{#3}{#4}{#5}}}
388 \let\enddescribe\desc@end
389
390 \def\descref{\@ifstar%
391   {\descref@i{}\@gobble{}}%
392   {\descref@i{ (}{\noexpand\autopageref}{)}}}
393 \def\descref@i#1#2#3#4#5{\@ifnextchar@preserve[%
394   {\descref@ii{#1}{#2}{#3}{#4}{#5}}%
395   {\descref@iii{#1}{#2}{#3}{#4}{#5}{}}}
396 \def\descref@ii#1#2#3#4#5[#6]{\descref@iii{#1}{#2}{#3}{#4}{#5}{ #6}}
397 \def\descref@iii#1#2#3#4#5#6{%
398   \begingroup%
399     \let\protect\@empty%
400     \def\@uscore{-\@gobble}%
401     \edef\@tempa##1{%
402       \endgroup%
403       \noexpand\hyperref[#4:#5]%
404       ##1%
405       #2{#4:#5}%
406     }%
407   \@tempa{{\code{#5}}#6#1}#3%
408 }
409
410 %%%----- That's all, folks --------------------------------------------------
411 \endinput