%D \module
%D   [       file=spac-cha,   % was supp-ali,
%D        version=2012.06.08, % 2000.04.17,
%D          title=\CONTEXT\ Spacing Macros,
%D       subtitle=Character Alignment,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Yet undocumented but nevertheless used.

% 0 = centered
% 1 = left  in before
% 2 = right in before
% 3 = left  in after
% 4 = right in after

\unprotect

% \starttabulate[|cg{.}|cg{,}|cg{,}|]
% \NC period     \NC comma      \NC comma   \NC\NR
% \NG 100.000,00 \NG 100.000,00 \NG 100,00  \NC\NR
% \NG 10.000,00  \NG 10.000,00  \NG 1000,00 \NC\NR
% \NG 100,00     \NG 100,00     \NG 10,00   \NC\NR
% \NG 100,00     \NG 100,00     \NG 10,00   \NC\NR
% \NG 10\\       \NG 10\\       \NG 0,00    \NC\NR
% \NG 10         \NG 10         \NG 0,00    \NC\NR
% \NG 10         \NG 10         \NG 0,00    \NC\NR
% \stoptabulate

% We gain not much by luafication and actually make things worse.

\chardef\characteralignmentmode\plusfour
\chardef\characteralignmentslot\plusone

\let\afterassignwidth \!!zeropoint
\let\beforeassignwidth\!!zeropoint

\def\alignmentcharacter{.}

\newdimen\d_supp_charalign_width
\newtoks \t_supp_charalign_list

\let\alignmentclass\s!default % can be used to handle multiple mixed ones

\installcorenamespace{characteralign}

\unexpanded\def\supp_charalign_push
  {\ifcsname\??characteralign\alignmentclass\endcsname\else
     \normalexpanded{\global\t_supp_charalign_list{\the\t_supp_charalign_list\supp_charalign_do{\alignmentclass}}}%
   \fi
   \setxvalue{\??characteralign\alignmentclass}{\supp_charalign_do
     {\afterassignwidth}{\beforeassignwidth}{\alignmentcharacter}}}

\unexpanded\def\supp_charalign_pop_do#1#2#3%
  {\def\afterassignwidth  {#1}%
   \def\beforeassignwidth {#2}%
   \def\alignmentcharacter{#3}}

\unexpanded\def\supp_charalign_pop
  {\let\supp_charalign_do\supp_charalign_pop_do
   \executeifdefined{\??characteralign\alignmentclass}\donothing}

\unexpanded\def\supp_charalign_reset_do#1%
  {\global\letbeundefined{\??characteralign#1}} % global !

\unexpanded\def\resetcharacteralign
  {\let\supp_charalign_do\supp_charalign_reset_do
   \the\t_supp_charalign_list
   \global\t_supp_charalign_list\emptytoks}

\unexpanded\def\supp_charalign_firstpass_one#1#2%
  {\supp_charalign_pop
   \let\\\empty
   \setbox\scratchbox\hbox{#1}%
   \d_supp_charalign_width\wd\scratchbox
   \setbox\scratchbox\emptyhbox
   \supp_charalign_check#2#1\relax\relax
   \scratchdimen-\wd\scratchbox
   \setbox\scratchbox\hbox{\ignorespaces#2\unskip}%
   \advance\scratchdimen \wd\scratchbox
   \ifdim\scratchdimen>\beforeassignwidth\relax
     \edef\beforeassignwidth{\the\scratchdimen}%
   \fi
   \ifdim\scratchdimen=\zeropoint
     \setbox\scratchbox\hbox{\ignorespaces#2\unskip}%
     \scratchdimen\wd\scratchbox
     \ifcase\characteralignmentmode
       % do nothing
     \else\ifnum\characteralignmentmode<\plusthree
       \advance\scratchdimen\d_supp_charalign_width\relax
       \ifdim\scratchdimen>\beforeassignwidth\relax
         \edef\beforeassignwidth{\the\scratchdimen}%
       \fi
     \else
       \ifdim\scratchdimen>\afterassignwidth\relax
         \edef\afterassignwidth{\the\scratchdimen}%
       \fi
     \fi\fi
   \fi
   \supp_charalign_push}

\unexpanded\def\supp_charalign_firstpass_two#1#2#3%
  {\ifx#2\relax
     \setbox\scratchbox\hbox{\ignorespaces#1\unskip}%
     \ifdim\wd\scratchbox>\afterassignwidth
       \edef\afterassignwidth{\the\wd\scratchbox}%
     \fi
   \else
     \supp_charalign_check#2#3\relax\relax
   \fi}

\unexpanded\def\supp_charalign_secondpass_one#1#2%
  {\supp_charalign_pop
   \let\\\empty % beware, no grouping
   \setbox\scratchbox\hbox{#1}%
   \d_supp_charalign_width\wd\scratchbox
   \setbox\scratchbox\emptyhbox
   % new 12,34 vs 10\\ where 10 aligns on 12 if #1 = ,
   \ifcase\characteralignmentslot
     \supp_charalign_check#2#1\relax\relax
     \scratchdimen\wd\scratchbox
     \setbox\scratchbox\hbox{\ignorespaces##1\unskip}%
   \else
     \def\\{#1}%
     \normalexpanded{\supp_charalign_check#2#1\relax\relax}%
     \scratchdimen\wd\scratchbox
     \setbox\scratchbox\hbox{\def\\{\hphantom{#1}}\ignorespaces#2\unskip}%
   \fi
   \noindent
   \ifdim\scratchdimen=\wd\scratchbox
     \ifcase\characteralignmentmode
       \box\scratchbox
     \else
       \hbox
         {\dontcomplain
          \hbox to \beforeassignwidth
            {\ifcase\characteralignmentmode\or
               \box\scratchbox\hss
             \or
               \hss\box\scratchbox\hskip\d_supp_charalign_width
             \or
               \hss\rlap{\box\scratchbox}%
             \or
               \hss\rlap{\hbox to \afterassignwidth{\hss\box\scratchbox}}%
             \fi}%
          \hskip\afterassignwidth}%
     \fi
   \else
     \hbox
       {\hbox to \beforeassignwidth
          {\hss\box\scratchbox\hskip-\scratchdimen}%
        \hskip\afterassignwidth}%
   \fi}

\unexpanded\def\supp_charalign_secondpass_two#1#2#3%
  {\ifx#2\relax
     \setbox\scratchbox\hbox{\ignorespaces#1\unskip}%
   \else
     \supp_charalign_check#2#3\relax\relax
   \fi}

\unexpanded\def\supp_charalign_firstpass#1%
  {\unexpanded\def\checkalignment                    ##1{\supp_charalign_firstpass_one{#1}{##1}}%
   \unexpanded\def\supp_charalign_check##1#1##2##3\relax{\supp_charalign_firstpass_two{##1}{##2}{##3}}}

\unexpanded\def\supp_charalign_secondpass#1%
  {\unexpanded\def\checkalignment                    ##1{\supp_charalign_secondpass_one{#1}{##1}}%
   \unexpanded\def\supp_charalign_check##1#1##2##3\relax{\supp_charalign_secondpass_two{##1}{##2}{##3}}}

\unexpanded\def\setfirstpasscharacteralign
  {\supp_charalign_pop
   \normalexpanded{\supp_charalign_firstpass{\alignmentcharacter}}}

\unexpanded\def\setsecondpasscharacteralign
  {\supp_charalign_pop
   \normalexpanded{\supp_charalign_secondpass{\alignmentcharacter}}}

\unexpanded\def\startcharacteralign#1\stopcharacteralign
  {\bgroup
   \setfirstpasscharacteralign #1%
   \setsecondpasscharacteralign#1%
   \egroup}

\let\stopcharacteralign\relax

\protect \endinput
