%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%                  plain.sty
%                  %%%%%%%%%
%
% Defines a new environment plain, in which plain TeX files should be 
% LaTeX'able.  
% Defines \magnification and \bye to be no-op.
% Re-enables some plain commands (\line, \eqalign ...)
% Fakes some other plain commands (\footnote, \topinsert, ... )
% Keeps LaTeX's font definitions, and output routine.
%
%     Example use:
%     \begin{plain}
%     \input{Somebodys_nasty_plain_file.tex}
%     \end{plain}
%
%  You do not have to \input the plain commands, they can  be entered directly
%  inside the plain environment.

%  Note that this style file is designed to make plain TeX files do something
%  sensible inside LaTeX. The page breaks, widths of lines, fonts used etc
%  may not be the same as obtained from plain TeX.

%  If you want exactly the same result from LaTeX as plain TeX use: (!!!)
%  latex \&plain Somebodys_nasty_plain_file.tex
  
%                   David Carlisle
%                   carlisle@uk.ac.man.cs
%                   14/11/90

% All of the code is from (l)plain.tex except a few definitions
% which are marked with the comment:
%DPC

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\let\latex@end=\end

%DPC
% First a few \new's which have to be here as they are \outer 
\newif\ifus@@latex 
\newif\if@cr@latex
\newbox\tabs@latex
\newbox\tabsyet@latex
\newbox\tabsdone@latex


%DPC
% The plain environment:
\def\plain{%

%DPC
% put the \ifs and registers back
\let\ifus@\ifus@@latex 
\let\if@cr\if@cr@latex
\let\tabs\tabs@latex
\let\tabsyet\tabsyet@latex
\let\tabsdone\tabsdone@latex

\let\centering=\@centering

% \line changed to \@@line because LaTeX redefines \line
\def\line{\hbox to\hsize}

% LaTeX has its own tabbing environment, so PLAIN's is disabled.
\def\cleartabs{\global\setbox\tabsyet\null \setbox\tabs\null}
\def\settabs{\setbox\tabs\null \futurelet\next\sett@b}
\let\+=\relax % in case this file is being read in twice
\def\sett@b{\ifx\next\+\let\next\relax
    \def\next{\afterassignment\s@tt@b\let\next}%
  \else\let\next\s@tcols\fi\next}
\def\s@tt@b{\let\next\relax\us@false\m@ketabbox}
\def\tabalign{\us@true\m@ketabbox} % non-\outer version of \+
\outer\def\+{\tabalign}
\def\s@tcols##1\columns{\count@##1 \dimen@\hsize
  \loop\ifnum\count@>\z@ \@nother \repeat}
\def\@nother{\dimen@ii\dimen@ \divide\dimen@ii\count@
  \setbox\tabs\hbox{\hbox to\dimen@ii{}\unhbox\tabs}%
  \advance\dimen@-\dimen@ii \advance\count@\m@ne}

\def\m@ketabbox{\begingroup
  \global\setbox\tabsyet\copy\tabs
  \global\setbox\tabsdone\null
  \def\cr{\@crtrue\crcr\egroup\egroup
    \ifus@\unvbox\z@\lastbox\fi\endgroup
    \setbox\tabs\hbox{\unhbox\tabsyet\unhbox\tabsdone}}%
  \setbox\z@\vbox\bgroup\@crfalse
    \ialign\bgroup&\t@bbox####\t@bb@x\crcr}

\def\t@bbox{\setbox\z@\hbox\bgroup}
\def\t@bb@x{\if@cr\egroup % now \box\z@ holds the column
  \else\hss\egroup \global\setbox\tabsyet\hbox{\unhbox\tabsyet
      \global\setbox\@ne\lastbox}% now \box\@ne holds its size
    \ifvoid\@ne\global\setbox\@ne\hbox to\wd\z@{}%
    \else\setbox\z@\hbox to\wd\@ne{\unhbox\z@}\fi
    \global\setbox\tabsdone\hbox{\box\@ne\unhbox\tabsdone}\fi
  \box\z@}

% LaTeX has its own sectioning macros
\outer\def\beginsection##1\par{\filbreak\bigskip
  \message{##1}\leftline{\bf##1}\nobreak\smallskip\vskip-\parskip
  \noindent}

% LaTeX change: \bye is eliminated.
%DPC
% \bye made a no-op
\let\bye\relax

%DPC
% \end made no-op unless it is followed by {
\def\end{\@ifnextchar \bgroup{\latex@end}{\relax}}


% LaTeX change: \eqalign eliminated, since it is replaced by the
% eqnarray environment.
\def\eqalign##1{\null\,\vcenter{\openup\jot\m@th
  \ialign{\strut\hfil$\displaystyle{####}$&$\displaystyle{{}####}$\hfil
      \crcr##1\crcr}}\,}

%  LaTeX: The following \eqalign type macros are eliminated, since
%  they are replaced by the eqnarray environment.
\def\eqalignno##1{\displ@y \tabskip\centering
  \halign to\displaywidth{\hfil$\@lign\displaystyle{####}$\tabskip\z@skip
    &$\@lign\displaystyle{{}####}$\hfil\tabskip\centering
    &\llap{$\@lign####$}\tabskip\z@skip\crcr
    ##1\crcr}}
\def\leqalignno##1{\displ@y \tabskip\centering
  \halign to\displaywidth{\hfil$\@lign\displaystyle{####}$\tabskip\z@skip
    &$\@lign\displaystyle{{}####}$\hfil\tabskip\centering
    &\kern-\displaywidth\rlap{$\@lign####$}\tabskip\displaywidth\crcr
    ##1\crcr}}
 

%DPC
% Fake plain's \nopagenumers
\def\nopagenumbers{\pagestyle{empty}}

\def\advancepageno{\ifnum\pageno<\z@ \global\advance\pageno\m@ne
  \else\global\advance\pageno\@ne \fi} % increase |pageno|

%DPC
% Fake plain's \footnote with internal LaTeX commands
\def\footnote##1{\def\@thefnmark{##1}\@footnotemark\@footnotetext}

%DPC
% Re-instate the primitive \underline 
%(LaTeX's version generates errors in some valid (plain) contexts)
\let\underline\@@underline

%DPC
% Re-instate plain's \item
\def\item{\par\hang\textindent}

%DPC
% Fake plain's \[top|mid|page]insert with LaTeX's figure environment.
\def\topinsert{\begin{figure}}
\def\midinsert{\begin{figure}[htp]}
\def\pageinsert{\begin{figure}[p]}
\def\endinsert{\end{figure}}

% \magnification doesn't work in LaTeX
%
%DPC
% Make \magnification just gobble the next <number>
\def\magnification{\afterassignment\relax\count@}

%DPC
%end of \def\plain
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
