%
%  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}
%
%  amg roc hack
%
\def\@@@#1#2{\expandafter\edef\expandafter#1\expandafter{#1#2}}
%
\newdimen\l@ft \newdimen\w@dth \newdimen\d@lta \newdimen\dd@lta
\l@ft=0em \w@dth=\hsize \d@lta=0em \dd@lta=.05em
%
\newcount\it@r
\def\compensate{\advance\l@ft by \d@lta \advance\w@dth by -\d@lta
\advance\d@lta by \dd@lta \relax
\ifdim\d@lta>.2em \dd@lta=-.05em\fi
\ifdim\d@lta<-.2em \dd@lta=.05em\fi
\advance\it@r by -1
\immediate\write16{\the\l@ft===\the\w@dth}
\ifnum\it@r>0 \compensate \fi}
%
\newdimen\ll@ft \newdimen\ww@dth \newdimen\d@ltaa \newdimen\dd@ltaa
\def\makesh@pe{\advance\ll@ft by \d@ltaa \advance\ww@dth by -\d@ltaa
\advance\d@ltaa by \dd@ltaa \relax
\ifdim\d@ltaa>.2em  \message{>}\dd@ltaa=-.05em\fi
\ifdim\d@ltaa<-.2em \message{<}\dd@ltaa=.05em\fi
\@@@\sh@pe{ \the\ll@ft}\@@@\sh@pe{ \the\ww@dth}
\advance\it@r by -1
\ifnum\it@r>0 \makesh@pe \fi}
%
%\def\hangindent=1em\hangafter=1{%
\ifdim\w@dth>\hsize \w@dth\hsize\fi
\it@r=\prevgraf\compensate
\def\sh@pe{ 15 }%
\it@r=15
\ll@ft=\l@ft \ww@dth=\w@dth \d@ltaa=\d@lta \dd@lta=\dd@ltaa
\makesh@pe
\expandafter\parshape\sh@pe}
%
\def\gl@bal{\globaldefs=1}
\def\endgl@bal{\globaldefs=0}
\catcode`\@=\c@tcode
