% \iffalse meta-comment
%
% Copyright (C) 2012 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Sigitas Tolu\v{s}is.
%
% This work consists of the files flushend.dtx and flushend.ins
% and the derived filebase flushend.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{flushend.dtx}
%</driver>
%<*package>
%% Copyright (C) 1997-2012 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
%% VTeX Ltd., Akademijos 4, Vilnius, Lithuania
%% http://www.vtex.lt/tex/download/macros/
%% --------------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% PURPOSE:   Balanced columns on last page in twocolumn mode.
%%
%</package>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{flushend}
%<*package>
    [2012/05/29 v1.1 Balancing columns at last page]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{flushend}[2012/05/29]
\EnableCrossrefs
\CodelineIndex
%%\RecordChanges
\begin{document}
  \DocInput{flushend.dtx}
  %%\PrintChanges
  %%\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{285}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v0.1}{1997/05/16}{first version}
% \changes{v0.2}{1997/09/09}{support for compatibility with cuted.sty}
% \changes{v1.0}{1997/10/01}{\string\vipersep changed to \string\stripsep for compatibility with cuted.sty}
% \changes{v1.1}{2012/05/29}{Converted to DTX file}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{flushend.dtx}
% \title{The \textsf{flushend} package}
% \author{Sigitas Tolu\v{s}is \\ \url{sigitas@vtex.lt}}
% \date{\fileversion~from \filedate}
%
% \maketitle
%
% \section{Introduction}
%
% The package is used to balance columns on the last page in twocolumn mode.
%
% \section{Usage}
%
% Just load the package: that's all.
% 
% \DescribeMacro{\flushend}
%
% This macro switches on column balancing on the last page. It is used
% by default.
%
% \medskip
% \DescribeMacro{\raggedend}
%
% This macro switches off column balancing on the last page.
%
% \medskip
% \DescribeMacro{\atColsBreak}
% \marg{tokens}
%
% Adds \meta{tokens} in place of the original column break (made without balancing).
%
% \medskip
% \textit{Example}: |\atColsBreak{\vskip-2pt}|
%
% \medskip
% \DescribeMacro{\showcolsendrule}
%
% It can be used just for debugging: adds rule to the bottom of columns.
%
% \section{Tips}
%
% Sometimes the balancing can be not so good. In that case to stretch
% right column by \meta{tex dimension} add command |\vskip-|\meta{tex dimension} just before
% |\end{document}|. To shrink right column use |\vskip|\meta{tex dimension}.
%
% \medskip
% \textit{Example}: 
% 
% |\vskip-10pt%|
%
% |\end{document}|
%
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{flushend}[1997/10/01]
%    \end{macrocode}
%
%    \begin{macrocode}
\newbox\@aaa
\newbox\@ccc
\@ifundefined{@viper}{\newbox\@viper}{}
\@ifundefined{hold@viper}{\newbox\hold@viper}{}
\newtoks\atColsBreak \atColsBreak={}
\newdimen\@extra@skip \@extra@skip\z@
\newdimen\@nd@page@rule \@nd@page@rule\z@
%    \end{macrocode}
%
% \begin{macro}{\last@outputdblcol}
%    \begin{macrocode}
\def\last@outputdblcol{%
  \if@firstcolumn
    \global \@firstcolumnfalse
    \global \setbox\@leftcolumn \box\@outputbox
  \else
    \global \@firstcolumntrue
    \if@lastpage
      \@tempdima\ht\@leftcolumn
      \splittopskip\topskip\splitmaxdepth\maxdepth
      \setbox\@tempboxa\vbox{%
        \unvbox\@leftcolumn\setbox0\lastbox\unskip
        \the\atColsBreak
        \unvbox\@outputbox\setbox0\lastbox\unskip
        }%
      \@tempdimb .5\ht\@tempboxa
      \loop
        \setbox\@aaa\copy\@tempboxa
        \setbox\@ccc\vbox to\@tempdimb{%
          \vsplit\@aaa to\@tempdimb
          \vss
          \vsplit\@aaa to\@tempdimb
          }%
        \wlog{Extra height:\the\ht\@aaa\space when \the\@tempdimb}%
      \ifvoid\@aaa 
      \else 
        \advance\@tempdimb 1\p@
      \repeat
      \loop
        \setbox\@aaa\copy\@tempboxa
        \setbox\@ccc\vbox to\@tempdimb{%
          \vsplit\@aaa to\@tempdimb
          \vss
          }%
        \wlog{(2)Left:\the\ht\@ccc\space 
                Right:\the\ht\@aaa\space
               Output:\the\@tempdimb
             }%
      \ifdim \ht\@ccc<\ht\@aaa 
        \@tempdimb \the\ht\@aaa 
      \repeat
      \wlog{- LAST -^^JExtra skip:\the\@extra@skip
                    ^^JLeft:\the\ht\@ccc
                    ^^JRight:\the\ht\@aaa
                    ^^JOutput:\the\@tempdimb
           }%
      \setbox\@ccc\vbox to\@tempdimb{%
        \vsplit\@tempboxa to\@tempdimb
        \vss
        }%
      \setbox\@leftcolumn\vbox to\@tempdima{%
        \vbox to\@tempdimb{\unvbox\@ccc}%
        \hrule\@height\@nd@page@rule
        \vss
        }%
      \setbox\@outputbox\vbox to\@tempdima{%
        \vbox to\@tempdimb{%
          \unvbox\@tempboxa
          \vfilneg
          \vskip\@extra@skip
          }%
        \hrule\@height\@nd@page@rule
        \vss
        }%
      \setbox\@outputbox \vbox {%
        \hb@xt@\textwidth {%
          \hb@xt@\columnwidth {\box\@leftcolumn \hss}%
          \hfil
          \vrule \@width\columnseprule
          \hfil
          \hb@xt@\columnwidth {\box\@outputbox \hss}%
          }%
        }%
    \else
      \setbox\@outputbox \vbox {%
        \hb@xt@\textwidth {%
          \hb@xt@\columnwidth {\box\@leftcolumn \hss}%
          \hfil
          \vrule \@width\columnseprule
          \hfil
          \hb@xt@\columnwidth {\box\@outputbox \hss}%
          }%
        }%
    \fi
    \ifvoid\hold@viper
    \else
      \setbox\@outputbox \vbox{\box\hold@viper\box\@outputbox}%
    \fi
    \@combinedblfloats
    \@outputpage
    \begingroup
      \@dblfloatplacement
      \@startdblcolumn
      \@whilesw\if@fcolmade \fi
        {\@outputpage \@startdblcolumn}%
      \ifvoid\@viper
      \else
        \global\setbox\@viper\vbox{%
          \vskip-\stripsep
          \unvbox\@viper
          }%
        \@viperoutput
      \fi
    \endgroup
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\let\prev@enddocument\enddocument
\newif\if@lastpage \@lastpagefalse
%    \end{macrocode}
%
% \begin{macro}{\enddocument}
%    \begin{macrocode}
\def\enddocument{%
  \global\@lastpagetrue
  \let\@outputdblcol\last@outputdblcol
  \prev@enddocument
  }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\raggedend}
%    \begin{macrocode}
\def\raggedend{\global\let\enddocument\prev@enddocument}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\flushend}
%    \begin{macrocode}
\def\flushend{%
  \gdef\enddocument{%
    \global\@lastpagetrue
    \let\@outputdblcol\last@outputdblcol
    \prev@enddocument
    }%
  }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\showcolsendrule}
%    \begin{macrocode}
\def\showcolsendrule{\global\@nd@page@rule=.4pt}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%\endinput
%    \end{macrocode}
%
% \iffalse
%</package>
% \fi
%
% \Finale
