%%
%% This is file `tabularew.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% tabularew.dtx  (with options: `package')
%% DRAFT VERSION
%%
%% File `tabularew.dtx'.
%% Copyright (C) 2009 Diego Saba
%% This file may be distributed under the terms of the LPPL.
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tabularew}
          [2009/05/23 v0.1 tabularew package (DPC)]
\RequirePackage{array}
\catcode`\Z=14
\DeclareOption{debugshow}{\catcode`\Z=9\relax}
\ProcessOptions
\newdimen\ExcessWidth
\newcount\CurrentColumn
\newcount\NumberOfColumns
\def\tabularew{%
  \edef\TEW@{\@currenvir}%
  {\ifnum0=`}\fi
  \TEW@setup
  \toks@{}\TEW@get@body}

\long\def\TEW@get@body#1\end
  {\toks@\expandafter{\the\toks@#1}\TEW@find@end}

\def\TEW@find@end#1{%
  \def\@tempa{#1}%
  \ifx\@tempa\TEW@\def\@tempa{\end{#1}}\expandafter\@tempa
  \else\toks@\expandafter
    {\the\toks@\end{#1}}\expandafter\TEW@get@body\fi}

\def\endtabularew{%
Z \message{^^J^^JEW Table - first pass^^J}%
  \TEW@firstpass
Z \message{\@spaces\@spaces\space - second pass^^J}%
  \TEW@secondpass
Z \message{\@spaces\@spaces\space - last pass^^J}%
  \TEW@lastpass
  \TEW@cleanup
  \ifnum0=`{\fi}}

\def\TEW@setup{%
  \edef\@restorecounters{%
    \global\NumberOfColumns\the\NumberOfColumns
    \global\CurrentColumn\the\NumberOfColumns\relax}%
  \count@\z@
  \@tempswatrue
  \@whilesw\if@tempswa\fi{%
    \advance\count@\@ne
    \expandafter\ifx\csname TEW@\the\count@\endcsname\relax
      \@tempswafalse
    \else
      \expandafter\let\csname TEW@S\the\count@
        \expandafter\endcsname\csname TEW@\the\count@\endcsname
    \fi}%
  \let\@arraycr\TEW@arraycr
  \let\multicolumn\TEW@multicolumn
  \ExcessWidth\z@
  \let\spew\TEW@spew
  }

\def\TEW@cleanup{%
  \count@\z@
  \@tempswatrue
  \@whilesw\if@tempswa\fi{%
    \advance\count@\@ne
    \expandafter\ifx\csname TEW@S\the\count@\endcsname\relax
      \@tempswafalse
    \else
      \global\expandafter\let\csname TEW@\the\count@
        \expandafter\endcsname\csname TEW@S\the\count@\endcsname
    \fi}%
  \@restorecounters
  }

\def\TEW@firstpass{%
  \let\multicolumn\TEW@multicolumnempty
  \TEW@tabsample
  \let\multicolumn\TEW@multicolumn
  \let\@computation\TEW@firstcomp
  \TEW@measure
  }

\def\TEW@secondpass{%
  \TEW@tabsample
  \let\@computation\TEW@secondcomp
  \TEW@measure
  }

\def\TEW@lastpass{%
  \TEW@tabfinal
  }

\def\TEW@tabsample{%
  \let\GetExcessWidth\@GetExcessWidthz@
  \let\@mkpream\TEW@mkpream
  \setbox\z@\hbox{%
  \gdef\@halignto{}%
  \col@sep\tabcolsep
  \let\d@llarbegin\begingroup\let\d@llarend\endgroup
    \expandafter\TEW@tabarray\the\toks@
      \crcr\omit
      {\count@\NumberOfColumns
        \xdef\TEW@save@row{}%
        \loop
          \advance\count@\m@ne
        \ifnum\count@>\z@
          \xdef\TEW@save@row{\TEW@save@row&\omit}%
        \repeat}%
      \TEW@save@row
      \endarray
    \global\setbox\@ne\lastbox
    }%
  }

\def\TEW@tabfinal{%
  \leavevmode
  \let\GetExcessWidth\@GetExcessWidth
  \let\@mkpream\TEW@mkpream
  \gdef\@halignto{}%
  \col@sep\tabcolsep
  \let\d@llarbegin\begingroup\let\d@llarend\endgroup
  \expandafter\TEW@tabarray\the\toks@\endarray}

\def\TEW@tabarray{\@ifnextchar[{\TEW@array}{\@array[t]}}

\def\TEW@array[#1]{\@array[t]}

\let\TEW@@mkpream\@mkpream
\let\TEW@@arraycr\@arraycr
\let\TEW@@multicolumn\multicolumn

\def\TEW@mkpream{%
  \global\NumberOfColumns\@ne
  \global\CurrentColumn\@ne
  \let\@addamp\TEW@addamp
  \global\let\@mkpream\TEW@@mkpream % needed!
  \TEW@@mkpream}

\def\TEW@arraycr{%
  \global\CurrentColumn\@ne
  \TEW@@arraycr
  }

\long\def\TEW@multicolumn#1#2#3{%
  % Can't place anything before \omit
  \TEW@@multicolumn{#1}{#2}{\global\advance\CurrentColumn\@ne#3}%
  \global\advance\CurrentColumn#1%
  \global\advance\CurrentColumn\m@ne
  \ignorespaces}

\long\def\TEW@multicolumnempty#1#2#3{\multispan#1\relax}

\def\TEW@addamp{%
  \if@firstamp\@firstampfalse
  \else
    \global\advance\NumberOfColumns\@ne
    \expandafter\def\expandafter\@preamble\expandafter{\@preamble
      &\global\advance\CurrentColumn\@ne}%
  \fi
  }%

\def\TEW@measure{%
  \setbox\z@\vbox{\unvbox\@ne\unskip\global\setbox\@ne\lastbox}%
  \setbox\tw@\hbox{%
    \count@\NumberOfColumns
    \unhbox\@ne
    \loop
      \unskip
      \setbox\tw@\lastbox
    \ifhbox\tw@
      \@computation{\wd\tw@}%
      \advance\count@\m@ne
    \repeat
    }%
  }

\def\TEW@firstcomp#1{%
Z \message{Col \the\count@: Intrinsic Width=\the#1^^J}%
  \TEW@width\xdef{\the#1}}

\def\TEW@secondcomp#1{%
  \TEW@width\dimen@
  \advance\dimen@-#1%
  \multiply\dimen@\m@ne
Z \message{Col \the\count@: Excess width=\the\dimen@^^J}%
  \TEW@width\xdef{\the\dimen@}}

\def\TEW@width#1{%
  \expandafter#1\csname TEW@\the\count@\endcsname}

\def\@GetExcessWidth#1{%
  \count@\CurrentColumn
  \@ifnextchar+{\advance\count@}{%
    \@ifnextchar-{\advance\count@}{\count@}}#1\relax
  \@tempswafalse
  \ifnum\count@>\z@
    \ifnum\count@>\NumberOfColumns
    \else\@tempswatrue
    \fi\fi
  \if@tempswa\TEW@width\ExcessWidth
  \else\ExcessWidth\z@
    \TEW@warn{nonexistent column \the\count@, assuming EW=0pt}%
  \fi
Z \message{EW in col. \the\CurrentColumn\space references
Z   col. \the\count@: \the\ExcessWidth^^J}%
  }

\def\@GetExcessWidthz@#1{\ExcessWidth\z@}

\def\TEW@spew#1#2{\GetExcessWidth{#2}\hspace{#1\ExcessWidth}}

\def\TEW@warn{%
  \PackageWarning{tabularew}}

\catcode`\Z=11
\endinput
%%
%% End of file `tabularew.sty'.
