%{{{ 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
