%  -*- Mode:TeX  -*-

% Allow @ in control sequence names  
\makeatletter

%%% \addtoks adds tokens to the end of the tokens already in a toks register.
\def\addtoks#1#2{%
  \expandafter#1\expandafter{\the#1#2}}

%%% \addpep adds "persistent everypar" tokens.  This adds the given tokens to
%%% \everypar, along with code that restores them, in case the code already in
%%% \everypar tried to remove them.
\def\addpep#1{%
  \addtoks\everypar{#1\addtoks\everypar{#1}}}

%%% Illustrations -- from ILLUSTRATIONS.TEX
\def\illustrationfileprefix{}                          % Default is no prefix

\input{psadobe}
\def\illustration#1{\begin{center}
{\psadobe{\illustrationfileprefix#1.ps}}
\end{center}}
\def\illustrationpercent#1#2{\begin{center}
{\psadobepercent{#2}{\illustrationfileprefix#1.ps}}
\end{center}}


%%% Theorems:
%% \newtheorem{definition}{Definition}


%%% Environments:

%%% aside environment: for things like rationales, compatability notes, etc.
%%% Example:
%%%   \begin{aside}{Rationale}
%%%     ...
%%%   \end{aside}
%%% Use \reaside for two adjacent asides.  Example:
%%% Example:
%%%   \begin{aside}{Rationale}
%%%     ...
%%%   \reaside{Implementation Note}
%%%     ...
%%%   \end{aside}
\def\aside#1{%
  \par
  \small
  \parindent 0pt
  \parskip \baselineskip
  \rightskip 1in
  \advance \linewidth by -\rightskip
  \hbox to \linewidth{\hrulefill}
  \penalty 10000
  \vskip -\parskip
  {\bf #1:}}
\def\endaside{%
  \par
  \penalty 10000
  \vskip 3pt
  \nointerlineskip
  \hbox to \linewidth{\hrulefill}}
\def\reaside#1{%
  \par
  \penalty 10000
  \vskip 3pt
  \nointerlineskip
  \hbox to \linewidth{\hrulefill}
  \penalty 10000
  \vskip -\parskip
  {\bf #1:}}

% %%% Figure environment for this document.
% %%% Usage:  \begin{fig}{LABEL}{CAPTION}
% \newenvironment{fig}[2]
%   {\begin{figure}%
%    \def\figlabel{#1}%
%    \def\figcaption{#2}%
%    \hrule width\linewidth height1pt%
%    \vskip \fboxsep}%
%   {\caption{\label{\figlabel}\figcaption}%
%    \vskip \fboxsep
%    \hrule width\linewidth height1pt%
%    \end{figure}}

%%% The numberedverbatim environment.

\newcount\linenumber
\newdimen\nvnumberwidth

%%% This is code modified from the LaTeX sources.  It defines a control
%%% sequence which reads everything until the text \end{numberedverbatim}.  We
%%% need such a sequence since otherwise we'd end up reading until
%%% \end{verbatim}.
\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=12 |gdef|@xnumberedverbatim#1\end{numberedverbatim}[#1|end[numberedverbatim]]
|endgroup

%%% This is code modified from the LaTeX sources.  We do everything that
%%% \verbatim does, plus some additional code described by the comments within.
\def\numberedverbatim{%
  \@verbatim
  %% Set the width for the numbering text to be the width of (999) in ordinary face.
  \setbox0=\hbox{{\rm (999)}\enspace} \nvnumberwidth=\wd0
  %% Reset the line number.
  \linenumber=0
  %% Add code to \everypar to add the line number to each line.
  \addpep{\everynumberedverbatim}%                     % New code
  \frenchspacing\@vobeyspaces \@xnumberedverbatim}
\let\endnumberedverbatim=\endtrivlist

\def\everynumberedverbatim{%
  \advance\linenumber by 1\hbox to \nvnumberwidth{{\rm (\the\linenumber)}\hss}}


%%% The verbatimbox environment.

%%% This is code modified from the LaTeX sources.  It defines a control
%%% sequence which reads everything until the text \end{verbatimbox}.  We
%%% need such a sequence since otherwise we'd end up reading until
%%% \end{verbatim}.
\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=12 |gdef|@xverbatimbox#1\end{verbatimbox}[|halign[##|hfil|cr#1]|end[verbatimbox]]
|endgroup

%%% This is code modified from the LaTeX sources.  We do everything that
%%% \verbatim does, plus some additional code described by the comments within.
\def\verbatimbox{\@verbatimbox\frenchspacing\@vobeyspaces\@xverbatimbox}
\def\@verbatimbox{%
  \let\par=\cr\obeylines\tt\catcode``=13\@noligs\let\do\@makeother\dospecials}
\let\endverbatimbox\relax

\def\everynumberedverbatim{%
  \advance\linenumber by 1\hbox to \nvnumberwidth{{\rm (\the\linenumber)}\hss}}

%%% The example environment.   This is similar to verbatim, but only treats end
%%% of line and space as verbatim; control sequences, braces, and such work as
%%% before.  One situation in which this is useful is when you want to include
%%% italic text.  Unfortunately, if you include {\it stuff} the space between
%%% \it and stuff is typeset, since spaces are treated verbatim.  One way to
%%% avoid this is to type {\it{}stuff}.  A better way is to say \i{stuff}; \i
%%% is defined within example for just this purpose.

%%% We reuse as much LaTeX code as possible -- in a sneaky way by making
%%% \dospecials be a no-op, thus preventing the deactivation of the special
%%% characters.  Because special characters still work, we don't need the
%%% hackery needed by \endverbatim.
%%% Adjacent lines of an example environment get \examplepenalty inserted
%%% between them.  Beginning a line with \hinge removes the immediately
%%% preceding penalty.  Thus, setting \examplepenalty to 10000 prevents
%%% line breaks except between lines separated by \hinge.
\newcount\examplepenalty
\examplepenalty=0
\def\example{%
  \def\{{\relax\char`\{}%
  \def\}{\relax\char`\}}%
  \let\temp=\dospecials\let\dospecials=\relax
  \let\@@@par=\@@par
  \def\@@par{\@@@par\penalty\examplepenalty}%
  \@verbatim
  \def\hinge{\penalty0\relax}%
  \let\dospecials=\temp
  \def\i##1{{\it ##1}}%
  \def\t##1{{\tt ##1}}%
%  \def\b##1{{\bf ##1}}%
  \def\b##1{{##1}}%
%%  \@displayspacing
  \frenchspacing\@vobeyspaces}
\def\endexample{\penalty0\endtrivlist}
  
%%% examplebox is to verbatimbox as example is to verbatim.
\def\examplealign{%
  \def\{{\relax\char`\{}%
  \def\}{\relax\char`\}}%
  \let\par=\cr\obeylines\tt\catcode``=13\@noligs
  \frenchspacing\@vobeyspaces
  \def\i##1{{\it ##1}}%
  \def\t##1{{\tt ##1}}%
  \def\b##1{{\bf ##1}}%
  \halign\bgroup##\hfil}
\def\endexamplealign{\egroup}
\def\examplebox#1{\begin{quotation}\begin{example}}
\def\endexamplebox{\end{example}\end{quotation}}


%%% The unbreakabledisplay environment.  This is just a display that cannot be
%%% broken across pages.  Footnotes don't work inside it though...
\def\unbreakabledisplay{%
  \begin{list}{}{}\item[]%
  \begin{minipage}{\linewidth}}
\def\endunbreakabledisplay{%
  \end{minipage}
  \end{list}}

\def\unbreakable{\vbox\bgroup}
\def\endunbreakable{\egroup}

%%% \smartspacing[DISPLAYSPACING]{NORMALSPACING} sets things up so that normal
%%% text is set using NORMALSPACING (e.g., 2 for double spacing), but displays,
%%% footnotes, captions, etc, are set using DISPLAYSPACING, which defaults to
%%% 1. \spacing{SPACING} can be used within the document to change the spacing;
%%% if done within a group, it has effect only within the group.
\def\smartspacing{\@ifnextchar [{\@smartspacing}{\@smartspacing[1]}}
\def\@smartspacing[#1]#2{%
  \def\@displayspacing{\spacing{#1}}%
  \let\@@@arrayparboxrestore\@arrayparboxrestore
  \def\@arrayparboxrestore{%
     \@@@arrayparboxrestore
     \@displayspacing}
  \spacing{#2}}
\def\@divide#1#2{\dimen0=100sp\dimen0=#2\dimen0\multiply#1 by 100\divide#1 by \dimen0}
\def\spacing#1{%
  \@divide\baselineskip\baselinestretch
  \@divide\normalbaselineskip\baselinestretch
  \def\baselinestretch{#1}%
  \baselineskip\baselinestretch\baselineskip
  \normalbaselineskip\baselinestretch\normalbaselineskip
  \@setstrut}

%%% \abbreviationperiod can be put at the end of a definition of an
%%% abbreviation, e.g. \def\etc{{\em etc}\abbreviationperiod}.  This generates
%%% nothing if the next token is a period, a period if the next token is not a
%%% letter, otherwise a period followed by non-sentence ending space.  If the
%%% next token is a control sequence, no space is generated, even if that
%%% sequence expands into something beginning with a letter.
\def\abbreviationperiod{\futurelet\@tempa\@abbreviationperiod}
\def\@abbreviationperiod{%
  \expandafter\ifx\@tempa.%
  \else
    \ifcat\noexpand\@tempa A.\ \else .\fi
  \fi}

%%% \@ifprecedesalpha{YES}{NO} expands into YES or NO, depending on whether
%%% the next token is an alphabetic token (with no expansion).
\def\@ifprecedesalpha#1#2{%
  \def\@tempa{#1}\def\@tempb{#2}%
  \futurelet\@tempc\@@ifprecedesalpha}
\def\@@ifprecedesalpha{%
  \ifcat\noexpand\@tempc A\@tempa \else \@tempb \fi}

%%% \maybespace expands to a space token if the next token is alphabetic, or to
%%% nothing otherwise.
\def\maybespace{\@ifprecedesalpha{ }{}}

%%% \newmath\CS[N]{EXPANSION} defines \CS to expand to EXPANSION, surrounded by
%%% dollar signs if not already in math mode.  If the optional argument N is
%%% supplied, \CS will take N arguments.  \newmath issues an error if \CS is
%%% already defined.
%%% \renewmath is like \newmath, but issues an error if \CS is NOT already defined.
\def\newmath#1{\@ifnextchar [{\@newmath#1}{\@newmath#1[0]}}
\def\@newmath#1[#2]#3{%
  \newcommand#1[#2]{\@ifprecedesalpha{\ifmmode #3\else $#3$\space\fi}%
                                     {\ifmmode #3\else $#3$\fi}}}
\def\renewmath#1{\@ifnextchar [{\@renewmath#1}{\@renewmath#1[0]}}
\def\@renewmath#1[#2]#3{%
  \renewcommand#1[#2]{\@ifprecedesalpha{\ifmmode #3\else $#3$\space\fi}%
                                       {\ifmmode #3\else $#3$\fi}}}

%%% \pagestyle{draft} puts the page number at the bottom center, and a draft
%%% indication at the bottom outside.  \def\draftdate{March 15, 1988} to set
%%% the draft date; default is \today.
\def\draftdate{\today}
\def\ps@draft{%
 \let\@mkboth\@gobbletwo
 \def\@oddhead{}\def\@evenhead{}%
 \def\@oddfoot{\rm\hfil\thepage\hfil\llap{{\sf DRAFT \draftdate}}}%
 \def\@evenfoot{\rm\rlap{{\sf DRAFT \draftdate}}\hfil\thepage\hfil}}

%%% Environment vtrivlist are designed to provide the same vertical spacing
%%% of trivlist, but instead of having items in its body should just contain
%%% vertical mode material.  Quite often the vertical mode material is just an
%%% alignment.
\def\vtrivlist{%
  \@topsep\topsep
  \advance\@topsep\partopsep
  \advance\@topsep\parskip
  \vskip\@topsep}
\def\endvtrivlist{%
  \@topsep\topsep
  \advance\@topsep\partopsep
  \vskip\@topsep}

%%% \tthyphenate is like \tt but makes hyphen an active character which
%%% expands to a hyphen followed by a discretionary break, so that lines
%%% can be broken at hyphens.
{\catcode`\-=13
\gdef\tthyphenate{%
  \tt\catcode`\-=13\def-{\char`\-\discretionary{}{}{}}}
}
%%%% \C macro:  like \tt, but within its scope underscore (_) acts like an
%%%% ordinary character, though lines can be broken there.
{\catcode`\_=13 \catcode`\-=13
\gdef\C{%
  \tt\catcode`\-=13\def-{\char`\-\discretionary{}{}{}} %
  \tt\catcode`\_=13\def_{\char`\_\discretionary{}{}{}}}
}


% ReDefinition of 'myheadings' page style.  no page numbers on top.
%
\def\ps@myheadings{\let\@mkboth\@gobbletwo
\def\@oddhead{\hbox{}\rightmark\hbox{}}%
\def\@oddfoot{\rm\hfil\thepage\hfil}\def\@evenhead{\hbox{}\leftmark\hbox {}}%
\def\@evenfoot{\rm\hfil\thepage\hfil}\def\chaptermark##1{}\def\sectionmark##1{}%
\def\subsectionmark##1{}}

%%% Make chapters start on odd pages.
\def\chapter{\cleardoublepage \thispagestyle{plain} \global\@topnum\z@
\@afterindentfalse \secdef\@chapter\@schapter} 

\def\@trivlist{\@topsepadd\topsep
  \if@noskipsec \leavevmode \fi
  \ifvmode \advance\@topsepadd\partopsep \else \unskip\par\fi
  \if@inlabel \@noparitemtrue \@noparlisttrue 
    \else \@noparlistfalse \@topsep\@topsepadd \fi
%    \advance\@topsep \parskip
  \leftskip\z@\rightskip\@rightskip \parfillskip\@flushglue
  \@setpar{\if@newlist\else{\@@par}\fi}%
  \global\@newlisttrue \@outerparskip\parskip}


\def\hfigure{\@float{figure}[tp]}
\def\endhfigure{\end@float}
\@namedef{hfigure*}{\@dblfloat{figure}[tp]}
\@namedef{hendfigure*}{\end@dblfloat}
\def\myfigure{\@float{figure}[tp]}
\def\endmyfigure{\end@float}
%%\newenvironment{hfigure}{\begin{figure}[h]}{\end{figure}}
%%\newenvironment{myfigure}{\begin{figure}[tp]}{\end{figure}}


% Restore @ to "other" status.
\makeatother
