%
%  Support for n-column mode
%
%%%%
%
%  Depends on margins.tex
%
%%%%
%
\expandafter\xdef\csname c@tcode\endcsname{\the\catcode`\@}
\catcode`\@=11
\def\extend#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
%
\newcount\c@lumns\c@lumns=1
\newskip\c@lumnskip\c@lumnskip=.2in
\newbox\realpage
\newsignal\c@lumnchange
\newbox\spl@ta\newbox\spl@tb
\newdimen\spl@tht
%
%\tracingmacros=2 \tracingcommands=2
%
\def\columns{\doc@lumns\afterassignment\postc@lumns\c@lumns}
\def\columnsep{\doc@lumns\afterassignment\setmargins\c@lumnskip}
\def\c@lumnsep{\hskip\c@lumnskip} % should support rules, too
%
\defsig\c@lumnchange{\global\setbox\realpage\vbox{\box\realpage
\columnsplit\c@lumns\box255}\gl@bal\setmargins\endgl@bal}
%
\def\doc@lumns{\outputsignal\c@lumnchange}
%
\newcount\kludge
\def\columnsplit#1{%  number of columns, vbox with text
\def\numc@ls{#1}\kludge=#1\relax
\afterassignment\c@lumnsplit\setbox\spl@ta}
%
\def\c@lumnsplit{%  go for it  -- see rigidbalance, TB p. 397
\setbox\spl@ta\vbox{\unvbox\spl@ta}%\unskip}
\spl@tht=\ht\spl@ta
\advance\spl@tht by .2in
\divide\spl@tht by \numc@ls
\advance\spl@tht by 2\ht\strutbox %  this is a cheap way out!  Fix it!!!!
\setbox\spl@tb\box\voidb@x
\c@lumniter\box\spl@tb}
%
\def\c@lumniter{\setbox\spl@tb\hbox{%
\ifvoid\spl@tb\else\unhbox\spl@tb\c@lumnsep\fi
\vsplit\spl@ta to \spl@tht}%
\ifvoid\spl@ta\else\c@lumniter\fi}
%
\tracingpages=1
\def\postc@lumns{\setmargins
\count\footins=1000 \multiply\count\footins by \c@lumns\relax}
%
\let\tr@emargins\setmargins
\def\setmargins{% modify this to handle columns
\tr@emargins
\advance\hsize by \c@lumnskip
\divide\hsize by \c@lumns
\advance\hsize by -\c@lumnskip
\advance\vsize by -\pagetotal
\advance\vsize by -\ht\realpage
%\advance\vsize by -2\ht\strutbox  % this makes everything fit :-)
\multiply\vsize by \c@lumns}
%
\def\pagecontents{%
\ifvoid\topins \else\unvbox\topins\fi
\setbox\realpage=\vbox{\unvbox\realpage\columnsplit\c@lumns\box255}%
\dimen0=\dp\realpage \unvbox\realpage
\ifvoid\footins\else
\vskip\skip\footins
\footnoterule
\unvbox\footins\fi
\ifr@ggedbottom\kern-\dimen@\vfil\fi
}
%
%\extend\foo{\begingroup\c@lumns=1\setmargins}
%\extend\p@stoutput{\endgroup}
\extend\pr@output{\tr@emargins}
\extend\p@stalloutput{\gl@bal\setmargins\endgl@bal}
%
\def\gl@bal{\globaldefs=1}
\def\endgl@bal{\globaldefs=0}
\catcode`\@=\c@tcode
