summaryrefslogtreecommitdiff
path: root/macros/plain/contrib/pdcmac/pdccode2.tex
blob: 513600e9cd4af6b7d7d0f5c17083220ed30a47a3 (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
%{{{ pdccode.tex -- used to make self-documenting TeX code
%{{{  rcs

%%% version: $Revision: 1.2 $

%  A simpler and less featureful system than the LaTeX "doc" package is used.
%  Lines between \code and \endcode are printed to a file
%  set with the \codefile{xx} command.  They are also listed
%  "verbatim"

\def\rcs#1: #2,v #3 #4 #5 #6 #7\endrcs{
    \def\filename{#2}
    \def\fileversion{#3}
    \def\filedate{#4}
    \def\fileauthor{#6}
}

\rcs$Id: pdccode.tex,v 1.2 1994/12/03 16:26:00 pdc Exp pdc $\endrcs
\message{\fileversion\space<\fileauthor\space\filedate>}
%}}}  rcs
%{{{  misc macros

\errorcontextlines10000
\def\expcs#1#2{\expandafter#1\csname#2\endcsname}
\def\noexpcs{\expcs\noexpand}

%}}}  misc macros
%{{{  verbatim text

%% Define verbatim text using |...|
% This is used in the commentary

%  Tell the verbatim macros that #1 is an active char; this
%  makes sure that the character will be catcoded appropriately
%  when in verbatim mode.  #1 should be a somethign that will
%  wortk after "`".
\def\declareactivechar#1{%
	\toks0=\expandafter{\verbatimplains\do#1}%
	\edef\verbatimplains{\the\toks0 }%
	\catcode`#1=13
}

\def\verbatimplains{\do\\\do\{\do\}\do\_\do\$\do\#\do\&\do\%} % \{}_$#&%
\def\verbatimactives{\do\`\do\'\do\~\do\^\do\ } % `'~^<space>

% \setupverbatim -- makes almost all special characters catcode 12
%  except for ` ' ~ ^, which produce slightly lowered glyphs
\bgroup \catcode`\^=13 \catcode`\'=13 \catcode`\`=13 \toks0={\egroup
    \def\setupverbatim{%
	\frenchspacing
	\spaceskip0pt \xspaceskip0pt % use spacing of font
	\def\do##1{\catcode\lq##1=12 }\verbatimplains
	\def\do##1{\catcode\lq##1=13 }\verbatimactives
	\let`=\ttlq \let'=\ttrq 
	\let~=\tttilde \let^=\ttcircum
	\the\everyverbatim
	\hyphenchar\font=-1
    }
}\the\toks0

\def\ttlq{\lower0.125ex \hbox{\char18 }}
\def\ttrq{\lower0.125ex \hbox{\char19 }}
\def\tttilde{\lower0.5ex \hbox{\char`\~ }}
\def\ttcircum{\lower0.5ex \hbox{\char`\^ }}

\newtoks\everyverbatim	% expanded at start of every verbatim text


%  Define #1 as a self-matching verbatim character.
%  #1 must be a one-char control-sequence
\def\defverbatim#1{%
	\declareactivechar#1
	\begingroup \uccode`\~=`#1 \uppercase{\toks0={\endgroup
		\def~{%
			\leavevmode
			\begingroup	% matched by closing char
			\tt
			\setupverbatim
			\catcode`#1=13 \let~=\endgroup
		}%
	}}\the\toks0
}

\declareactivechar\| \defverbatim\|
%}}}  verbatim text
%{{{  writing code sections

% \docodefiles is \do{xx}\do{yy} for a set of file identifiers
% Each file identifier xx has \ifxx, \xxfalse, \xxtrue, \xxwrite
% I am greatly hampered by the fact that \newwrite and \newif are \outer!
\def\docodefiles{}

\def\codefile#1#2{
	\toks0=\expandafter{\docodefiles\do{#1}}
	\edef\docodefiles{\the\toks0 }	
	\expcs\CODEnewwrite{#1write}
	\CODEnewif{#1} \csname#1true\endcsname
	\expcs\openout{#1write}=#2
	\expcs\def{filename#1}{#2}
}
\catcode`@=11
\def\CODEnewwrite#1{\alloc@7\write\chardef\sixt@@n #1}
\catcode`@=12
\def\CODEnewif#1{%
	\expcs\edef{#1true}{\let\noexpcs{if#1}\noexpand\iftrue}
	\expcs\edef{#1false}{\let\noexpcs{if#1}\noexpand\iffalse}
	\csname#1false\endcsname
}

\def\allfilesfalse{\begingroup
	\def\do##1{\global\csname##1false\endcsname}\docodefiles
    \endgroup
}
\def\allfilestrue{\begingroup
	\def\do##1{\global\csname##1true\endcsname}\docodefiles
    \endgroup
}


\def\endcodefile#1{
    \expcs\closeout{#1write}
    \csname #1false\endcsname
    \message{Code written to \csname filename#1\endcsname.}
}	

\def\code{
	\smallskip
	\begingroup
	\let|\CODEline		% code line without expansion
	\let\|\CODElinex	% code line with expansion
	\let\{=\CODElb \let\}=\CODErb \let\\=\CODEbslash \def~{ }
	\the\everycode
	\obeylines 
}
\def\endcode{
	\smallbreak
	\endgroup
}
\def\CODEline{%
	\begingroup
	\setupverbatim
	\catcode`\|=12
	\CODElinei
}


% In \CODFElinei, We must redefine the characters that are active
%  within verbatim text to expand to their catcode-12 equivalents when
%  they are written to the code file.  Since one of these is ^, we
%  temporarily give @ the meaning of ^ so that we may safely munge the
%  catcode of ^.

\begingroup \catcode`\'=13 \catcode`\`=13
		\catcode\lq\@=\catcode\lq\^ \catcode\lq\^=13 
		\catcode\lq\@@M=13 \toks0={\endgroup%
	\def\CODElinei#1@@M{%
		\begingroup\let`\lq \let'\rq \let^\CODEcircum \let~\CODEtilde %
		    \edef\tmp{{#1}}%
		    \def\do##1{\csname if##1\endcsname %
		        \expcs\write{##1write}\tmp \fi %
		    }\docodefiles %
		\endgroup
		\indent\the\everycodeline #1\par %
		\endgroup %
	}%
	\def\CODElinex#1@@M{%
		\def\do##1{\csname if##1\endcsname %
		    \edef\filename{\csname filename##1\endcsname} %
		    \edef\tmp{{#1}} %
		    \expcs\write{##1write}\tmp \fi %
	  	}\docodefiles %
		\def\filename{\<filename>}%
		{\indent\the\everycodeline #1\par}%
	}%
}\the\toks0

%  Make macros which expand to various special characters, with catcode 12:
\begingroup\catcode`\/=0 \catcode`\\=12 /toks0={/endgroup
       /def/CODEbslash{\}
}/the/toks0
\begingroup\catcode`\^=12 \toks0={\endgroup
       \def\CODEcircum{^}
}\the\toks0
\begingroup\catcode`\~=12 \toks0={\endgroup
       \def\CODEtilde{~}
}\the\toks0
\begingroup\catcode`\%=12 \toks0={\endgroup
	\def\%{%}
}\the\toks0
\begingroup\catcode`\[=1 \catcode`\]=2 \catcode`\{=12 \catcode`\}=12 \toks0=[\endgroup
	\def\CODElb[{] \def\CODErb[}]
]\the\toks0
\newtoks\everycode
\newtoks\everycodeline

\def\filenamesanssuffix{\expandafter\CODEsanssuffix\filename.\CODEsanssuffix}
\def\CODEsanssuffix#1.#2\CODEsanssuffix{#1}
%}}}  writing code sections
%{{{  layout


\newcount\seccount
\newcount\subseccount
\font\secfont=cmssdc10 at 12pt
\font\subsecfont=cmssi10

\def\section#1{%
	\advance\seccount1 \subseccount=0
	\bigbreak
	\begingroup
		\secfont \hyphenpenalty=10000 \tolerance=1000
		\parskip=0pt \parindent=0pt \rightskip=0pt plus 2em
		\number\seccount. #1
		\medskip
	\endgroup
}

\def\subsec#1{%
	\advance\subseccount1
	\medbreak
	\begingroup
		\subsecfont \hyphenpenalty=10000 \tolerance=1000
		\parskip=0pt \parindent=0pt \rightskip=0pt plus 2em
		\number\seccount.\number\subseccount. #1
		\nobreak\smallskip
	\endgroup
}

\def\notepar{
    \smallskip
    \begingroup
	\narrower\noindent\hang
	{\bf Note}\quad\it
	\ignorespaces
}
\def\endnotepar{
	\smallbreak
    \endgroup
}

%  	Leave a largish right margin; eventually marginalia will go
%  there.  Allow enough line length for 80-character code lines.  This
%  makes the text lines a bit longer than I'd like but I don't want to
%  have to resort to having code lines in a smaller font.
\hsize=40em \advance\hsize\parindent

%	(297mm-11in) is the strip of paper lost if printed on USA
%	letter paper; allow another bit for some laserprinters that
%	can't print to the edge of the paper.
\voffset=297mm \advance\voffset by -11in \advance\voffset by 0.25in
\vsize=297mm \advance\vsize-2\voffset \advance\vsize-2\baselineskip
\advance\voffset-1in

\def\do#1{\fontdimen3#1=0pt \fontdimen4#1=0pt }
\do\tenrm \do\tenit \do\tenbf

\rightskip=0pt plus 2em minus 2pt
\tolerance=500
\lefthyphenmin=3 \righthyphenmin=2

%}}}  layout
%{{{  abbrevs

\def\<#1>{\leavevmode\hbox{$\langle$\it#1\/$\rangle$}}
\def\cs#1{\leavevmode\hbox{\tt\char`\\#1}}
\def\arg#1{\leavevmode\hbox{{\tt\char`\{}#1{\tt\char`\}}}}
\def\pt{\,{\rm pt}}
\def\flushtop#1{%
	\leavevmode
	{%
		\setbox0=\hbox{#1}\setbox2=\hbox{X}%
		\dimen0=\ht2 \advance\dimen0-\ht0
		\raise\dimen0 \box0
	}%
}
\def\TeX{T\kern-0.1em\lower0.5ex\hbox{E}X}
\def\LaTeX{L\kern-0.167em\flushtop{a}\TeX}

%}}}  abbrev
\newskip\footnoterightskip \footnoterightskip=\rightskip
\catcode`@=11
\def\vfootnote#1{\insert\footins\bgroup
  \interlinepenalty\interfootnotelinepenalty
  \splittopskip\ht\strutbox % top baseline for broken footnotes
  \splitmaxdepth\dp\strutbox \floatingpenalty\@MM
  \leftskip\z@skip \rightskip\footnoterightskip
  \spaceskip\z@skip \xspaceskip\z@skip \rm
  \textindent{#1}\footstrut\futurelet\next\fo@t}
\catcode`@=11


%}}} pdccode.tex