%
%   Change bars are accomplished using the 'changebar' environment.
%   The text inside will be typeset with change bars in the left margin,
%   e.g.,
%		Input		    Output
%	    \begin{changebar}	    ...
%	    Some text here.\\	 |  Some text here.
%	    More text.		 |  More text.
%	    \end{changebar}
%
%   The text is first typeset into a vbox. Then that box is split up using
%   the \vsplit operation.  Each box is placed onto the current page along
%   with a rule of the proper dimensions. 
%
%   The width of the change bar is determined from \changebarwidth.  The
%   default width is \arrayrulewidth.  The separation of the rule from the
%   left margin is controlled by \changebarsep.
%
%   Restrictions:
%
%   You can't put a float, such as a 'figure' or 'table' environment or a
%   footnote, inside a 'changebar', not even via \begin{figure}[h], etc.,
%   to force the float into the change bar area.  You could instead put a
%   'changebar' inside of a 'figure' or 'table' or footnote.
%
%   Nested 'changebar' environments are not supported.
%
%   \changebar ==
%   BEGIN
%	\par						% Start a new paragraph
%	\box\changebarbox :=		    % Save up text in a temporary vbox
%	    \vbox\bgroup
%	    \hsize=\textwidth		% Set the horizontal size in this box
%   END
%
%   \endchangebar ==
%   BEGIN
%	\egroup				    % Matching brace for temporary vbox
%	\begingroup				% Localize parameter changes
%	    \vbadness := \@Mi	    % Suppress "Underfull \vbox" from \vsplit
%	    \vfuzz := \maxdimen		% Suppress "Overfull \vbox" from \vsplit
%	    \splittopskip := 0pt	    % Don't insert glue after \vsplit
%	    \offinterlineskip			    % No space between rules
%	    \changebarsize ::= \dimen0
%	    \lostglue ::= \skip0
%	    \thislinebox ::= \box0
%	    \changebarsize := height + depth of \changebarbox
%	    WHILE \changebarsize > 0pt
%	    BEGIN
%		\lostglue := height + depth of \changebarbox
%	    	\box\thislinebox := \vsplit\changebarbox	% Take a chunk
%		\box\thislinebox := \vbox{\unvbox\thislinebox}	    % Reset size
%		\lostglue := \lostglue - (height + depth of \changebarbox)
%			% \lostglue is now the difference between the old &
%			% new box
%		\lostglue := \lostglue - (height + depth of \thislinebox)
%			% \lostglue is now as big as the glue lost to the
%			% \vsplit operation, i.e.,
%			% \lostglue + \thislinebox =
%			%	(old \changebarbox - new \changebarbox)
%		\vtop{				% Align rule with chunk of text
%		    \llap					% No-width rule
%			{\vtop to 0pt			    % Box up the rule(s)
%			    {\offinterlineskip		% No glue between rules
%			    \hrule to size of \thislinebox
%			    \hrule to size of \lostglue
%			}
%		    \hskip \changebarsep}
%		    }
%		\box\thislinebox			    % Put box on page
%		\endgroup
%	    END

\newif\ifdebug
\newdimen\changebarwidth \changebarwidth=\arrayrulewidth
\newskip\changebarsep \changebarsep=\parindent
\newcount\@cbp
\def\@cbpenalty{\ifvmode \mark{\Penalty \@cbp}\fi\Penalty\@cbp}
\def\changebar{\par
%    \let\Penalty=\penalty
%    \def\penalty{\afterassignment\@cbpenalty \@cbp=}%
\chardef\@changebarbox=0
\setbox\@changebarbox = \vbox\bgroup}
\def\endchangebar{\egroup
    \vbadness\@Mi			% Suppress "Underfull box" from \vsplit
    \vfuzz\maxdimen			% Suppress "Overfull box" from \vsplit
    \splittopskip = 0pt			    % Don't insert glue after \vsplit
    \dimendef\changebarsize = 0
    \skipdef\lostglue = 0
    \chardef\thislinebox = 1
    \ifdebug \showbox\@changebarbox \fi
    \changebarsize = \ht\@changebarbox		    % Initialize loop control
    \advance\changebarsize by\dp\@changebarbox
    \noindent \leavevmode\strut
    \offinterlineskip					% No space between rules
    \@whiledim \changebarsize > \z@ \do
	{\lostglue = \changebarsize			    % Save old box size
	\setbox\thislinebox = \vsplit\@changebarbox to 1sp	% Take a chunk
	\setbox\thislinebox = \vbox{\unvbox\thislinebox	    % Reset the box size
	}\ifdebug \showbox\thislinebox \fi
%	\splitfirstmark			% Any marks (penalties!) before line?
	\changebarsize = \ht\@changebarbox
	\advance\changebarsize by\dp\@changebarbox	% Compute remaining size
	\advance\lostglue by-\changebarsize	% Difference between old and new
	\advance\lostglue by-\ht\thislinebox
	\advance\lostglue by-\dp\thislinebox		% Glue lost to \vsplit
	\ifdim\lostglue < \z@ \lostglue = \z@ \fi		% No glue lost
	\ifdebug \showthe\lostglue \fi
	\hbox
	{\llap
	    {\vtop to \z@		% Prepare an invisible box for the rule
		{\hrule \@height\ht\thislinebox \@depth\dp\thislinebox
			\@width\changebarwidth			    % First rule
%		\ifdebug \hrule \@height\changebarwidth \@depth \z@
%			\@width 10pt			%%%% marker rule %%%%%
%		\fi
		\hrule \@depth\lostglue \@height\z@
			\@width\changebarwidth			% Second rule
	    \vss}\hskip\changebarsep		% Space between bar and text
	}\box\thislinebox			% Now put the chunk on the page
	}\vskip\lostglue			% and insert the missing glue
%	\splitbotmark			    % Any marks (penalties!) after line?
	}\ifdebug \showlists \fi
}
