% nwmac.tex -- plain TeX support for noweb
% DON'T read or edit this file!  Use ...noweb-source/tex/support.nw instead.
{\obeyspaces\global\let =\ } % from texbook, p 381
\def\nwdocspar{\par\semifilbreak}
\def\nwbackslash{\char92}
\def\nwlbrace{\char123}
\def\nwrbrace{\char125}
\catcode`\@=11
% scale cmbx10 instead of using cmbx12 because {\LaTeX} does, so fonts exist
\font\twlbf=cmbx10 scaled \magstep1
\font\frtbf=cmbx10 scaled \magstep2
% These fonts don't work with xdvi!

\advance\hoffset 0.5 true in
\advance\hsize -1.5 true in
\newdimen\textsize
\textsize=\hsize
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}
\long\def\ifundefined#1#2#3{%
   \expandafter\ifx\csname#1\endcsname\relax
       #2%
   \else#3%
   \fi}

\ifundefined{myheadline}
    {\headline={\hbox to \textsize{\tentt\firstmark\hfil\tenrm\today\hbox
                to 4em{\hss\folio}}\hss}}
    {\expandafter\headline\expandafter{\myheadline}}

\ifundefined{myfootline}
    {\footline={\hfil}}
    {\expandafter\footline\expandafter{\myfootline}}
\def\semifilbreak{\vskip0pt plus1.5in\penalty-200\vskip0pt plus -1.5in}
\raggedbottom
%
% \chapcenter macro to produce nice centered chapter titles
%
\def\chapcenter{\leftskip=0.5 true in plus 4em minus 0.5 true in
    \rightskip=\leftskip
    \parfillskip=0pt \spaceskip=.3333em \xspaceskip=.5em
    \pretolerance=9999 \tolerance=9999
    \hyphenpenalty=9999 \exhyphenpenalty=9999}
% \startsection{LEVEL}{INDENT}{BEFORESKIP}{AFTERSKIP}{STYLE}{HEADING}
%               #1     #2      #3          #4         #5     #6
%
%       LEVEL:          depth; e.g. part=0 chapter=1 sectino=2...
%       INDENT:         indentation of heading from left margin
%       BEFORESKIP:     skip before header
%       AFTERSKIP:      skip after header
%       STYLE:          style of heading; e.g.\bf
%       HEADING:        heading of the sectino
%
\def\startsection#1#2#3#4#5#6{\par\vskip#3 plus 2in
        \penalty-200\vskip 0pt plus -2in
    \noindent{\leftskip=#2 \rightskip=0.5true in plus 4em minus 0.5 true in
              \hyphenpenalty=9999 \exhyphenpenalty=9999
              #5#6\par}\vskip#4%
    {\def\code##1{[[}\def\edoc##1{]]}\message{[#6]}}
    \settocparms{#1}
    \def\themodtitle{#6}
%%%%    {\def\code{\string\code}\def\edoc{\string\edoc}%
    \edef\next{\noexpand\write\cont{\tocskip
        \tocline{\hskip\tocindent\tocstyle\relax\themodtitle}
                {\noexpand\the\pageno}}}\next % write to toc
    %}
}
\def\settocparms#1{
        \count@=#1
        \ifnum\count@<1
            \def\tocskip{\vskip3ptplus1in\penalty-100
                        \vskip0ptplus-1in}%
            \def\tocstyle{\bf}
            \def\tocindent{0pt}
        \else
            \def\tocskip{}
            \def\tocstyle{\rm}
            \dimen@=2em \advance\count@ by \m@ne \dimen@=\count@\dimen@
            \edef\tocindent{\the\dimen@}
        \fi
}
\def\tocline#1#2{\line{{\ignorespaces#1}\leaders\hbox to .5em{.\hfil}\hfil
     \hbox to1.5em{\hss#2}}}
\def\section#1{\par \vskip3ex\noindent {\bf #1}\par\nobreak\vskip1ex\nobreak}
\def\chapter#1{\vfil\eject\startsection{0}{0pt}{6ex}{3ex}{\frtbf\chapcenter}{#1}}
\def\section#1{\startsection{1}{0pt}{4ex}{2ex}{\twlbf}{#1}}
\def\subsection#1{\startsection{2}{0pt}{2ex}{1ex}{\bf}{#1}}
\def\subsubsection#1{\startsection{3}{0pt}{1ex}{0.5ex}{\it}{#1}}
\def\paragraph#1{\startsection{4}{0pt}{1.5ex}{0ex}{\it}{#1}}

% make \hsize in code sufficient for 88 columns
\setbox0=\hbox{\tt m}
\newdimen\codehsize
\codehsize=91\wd0 % 88 columns wasn't enough; I don't know why
\newdimen\codemargin
\codemargin=0pt
\newdimen\nwdefspace
\nwdefspace=\codehsize
% need to use \textwidth in {\LaTeX} to handle styles with
% non-standard margins (David Bruce).  Don't know why we sometimes
% wanted \hsize.  27 August 1997.
%% \advance\nwdefspace by -\hsize\relax
\ifx\textwidth\undefined
  \advance\nwdefspace by -\hsize\relax
\else
  \advance\nwdefspace by -\textwidth\relax
\fi
\chardef\other=12
\def\setupcode{%
  \chardef\\=`\\
  \chardef\{=`\{
  \chardef\}=`\}
  \catcode`\$=\other
  \catcode`\&=\other
  \catcode`\#=\other
  \catcode`\%=\other
  \catcode`\~=\other
  \catcode`\_=\other
  \catcode`\^=\other
  \catcode`\"=\other    % fixes problem with german.sty
  \obeyspaces\Tt
}
\let\nwlbrace=\{
\let\nwrbrace=\}
\def\nwendquote{\relax\ifhmode\spacefactor=1000 \fi}
{\catcode`\^^M=\active % make CR an active character
  \gdef\newlines{\catcode`\^^M=\active % make CR an active character
         \def^^M{\par\startline}}%
  \gdef\eatline#1^^M{\relax}%
}
%%% DON'T   \gdef^^M{\par\startline}}% in case ^^M appears in a \write
\def\startline{\noindent\hskip\parindent\ignorespaces}
\def\nwnewline{\ifvmode\else\hfil\break\leavevmode\hbox{}\fi}
\def\setupmodname{%
  \catcode`\$=3
  \catcode`\&=4
  \catcode`\#=6
  \catcode`\%=14
  \catcode`\~=13
  \catcode`\_=8
  \catcode`\^=7
  \catcode`\ =10
  \catcode`\^^M=5
  \let\{\nwlbrace
  \let\}\nwrbrace
  % bad news --- don't know what catcode to give "
  \Rm}
\def\LA{\begingroup\maybehbox\bgroup\setupmodname\It$\langle$}
\def\RA{\/$\rangle$\egroup\endgroup}
\def\code{\leavevmode\begingroup\setupcode\newlines}
\def\edoc{\endgroup}
\let\maybehbox\relax
\newbox\equivbox
\setbox\equivbox=\hbox{$\equiv$}
\newbox\plusequivbox
\setbox\plusequivbox=\hbox{$\mathord{+}\mathord{\equiv}$}
% \moddef can't have an argument because there might be \code...\edoc
\def\moddef{\leavevmode\kern-\codemargin\LA}
\def\endmoddef{\RA\ifmmode\equiv\else\unhcopy\equivbox\fi
               \nobreak\hfill\nobreak}
\def\plusendmoddef{\RA\ifmmode\mathord{+}\mathord{\equiv}\else\unhcopy\plusequivbox\fi
               \nobreak\hfill\nobreak}
\def\chunklist{%
\errhelp{I changed \chunklist to \nowebchunks.  
I'll try to avoid such incompatible changes in the future.}%
\errmessage{Use \string\nowebchunks\space instead of \string\chunklist}}
\def\nowebchunks{\message{<Warning: You need noweave -x to use \string\nowebchunks>}}
\def\nowebindex{\message{<Warning: You need noweave -index to use \string\nowebindex>}}
% here is support for the new-style (capitalized) font-changing commands
% thanks to Dave Love
\ifx\documentstyle\undefined
  \let\Rm=\rm \let\It=\it \let\Tt=\tt       % plain
\else\ifx\selectfont\undefined
  \let\Rm=\rm \let\It=\it \let\Tt=\tt       % LaTeX OFSS
\else                                       % LaTeX NFSS
  \def\Rm{\reset@font\rm}
  \def\It{\reset@font\it}
  \def\Tt{\reset@font\tt}
  \def\Bf{\reset@font\bf}
\fi\fi
\ifx\reset@font\undefined \let\reset@font=\relax \fi

\def\nwfilename#1{\vfil\eject\mark{#1}}

\def\nwbegindocs#1{\filbreak}
\def\nwenddocs{\par}
\def\nwbegincode#1{\par\nobreak
  \begingroup\setupcode\newlines\parindent=0pt\parskip=0pt
  \let\oendmoddef=\endmoddef \let\oplusendmoddef=\plusendmoddef
  \def\endmoddef{\oendmoddef\par}\def\plusendmoddef{\oplusendmoddef\par}%
  \hsize=\codehsize\noindent\bchack}
\def\nwendcode{\endgroup}
{\catcode`\^^M=\active % make CR an active character
  \gdef\bchack#1^^M{\relax#1}%
}
\edef\contentsfile{\jobname.toc } % file that gets table of contents info
\def\readcontents{\expandafter\input \contentsfile}

\newwrite\cont
\openout\cont=\contentsfile
\write\cont{\string\catcode`\string\@=11}% a hack to make contents
                                 % take stuff in plain.tex
\def\bye{%
    \write\cont{}% ensure that the contents file isn't empty
    \closeout\cont
    \vfil\eject\pageno=-1 % new page causes contents to be really closed
    \topofcontents\readcontents\botofcontents
    \vfil\eject\end}
\def\topofcontents{\vfil\mark{{\bf Contents}}}
\def\botofcontents{}
\let\em=\it
% used to produce an itemized (bulleted) list in plain {\TeX}
% such lists can be nested
% mostly useful with WEB

% Usage:
% \itemize
% \item First thing
% \item second thing
% \enditemize

\newcount\listlevel
\listlevel=0
\newdimen\itemwidth
\itemwidth=3em

\def\itemize{\begingroup\advance\listlevel by1
    \def\item{\par\noindent
         \raise2pt\llap{$\scriptstyle\bullet$\ }\ignorespaces}%
    \def\nameditem##1{\par\noindent
         \llap{\rlap{##1}\hskip\itemwidth}\ignorespaces}%
    \par\advance\leftskip by\itemwidth\advance\rightskip by0.5\itemwidth}
\def\enditemize{\par\endgroup\noindent\ignorespaces}

\let\begindocument=\relax
\catcode`\@=12
