summaryrefslogtreecommitdiff
path: root/tex/context/base/supp-ali.tex
blob: 363c239bcb7751fc0b6f5e33d5e9b723a1c55de3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
%D \module
%D   [       file=supp-ali,
%D        version=2000.4.17,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=Alignment,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%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. 

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

\unprotect 

\chardef\characteralignmentmode=4

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

\def\alignmentcharacter{.}

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

\def\setfirstpasscharacteralign%
  {\popcharacteralign
   \expanded{\dosetfirstpasscharacteralign{\alignmentcharacter}}}

\def\dosetfirstpasscharacteralign#1%
  {\def\checkalignment##1%
     {\popcharacteralign
      \setbox\scratchbox=\hbox{#1}%
      \edef\characterassignwidth{\the\wd\scratchbox}%
      \setbox\scratchbox=\hbox{}%
      \docheckalignment#1##1#1\relax\relax
      \scratchdimen=-\wd\scratchbox
      \setbox\scratchbox=\hbox{##1}%
      \advance\scratchdimen by \wd\scratchbox
      \ifdim\scratchdimen>\beforeassignwidth\relax
        \edef\beforeassignwidth{\the\scratchdimen}%
      \fi
      \ifdim\scratchdimen=\!!zeropoint
        \setbox\scratchbox=\hbox{##1}%
        \scratchdimen=\wd\scratchbox
        \ifnum\characteralignmentmode=0
          % do nothing 
        \else\ifnum\characteralignmentmode<3
          \advance\scratchdimen by \characterassignwidth
          \ifdim\scratchdimen>\beforeassignwidth\relax
            \edef\beforeassignwidth{\the\scratchdimen}%
          \fi
        \else
          \ifdim\scratchdimen>\afterassignwidth\relax
            \edef\afterassignwidth{\the\scratchdimen}%
          \fi
        \fi\fi
      \fi
      \pushcharacteralign}%
   \def\docheckalignment##1#1##2##3\relax%
     {\ifx##2\relax
        \setbox\scratchbox=\hbox{##1}%
        \ifdim\wd\scratchbox>\afterassignwidth
          \edef\afterassignwidth{\the\wd\scratchbox}%
        \fi
      \else
        \docheckalignment##2##3\relax
      \fi}}

\def\setsecondpasscharacteralign%
  {\popcharacteralign
   \expanded{\dosetsecondpasscharacteralign{\alignmentcharacter}}}

\def\dosetsecondpasscharacteralign#1%
  {\def\checkalignment##1%
     {\popcharacteralign
      \setbox\scratchbox=\hbox{#1}%
      \edef\characterassignwidth{\the\wd\scratchbox}%
      \setbox\scratchbox=\hbox{}%
      \docheckalignment#1##1#1\relax\relax
      \scratchdimen=\wd\scratchbox
      \setbox\scratchbox=\hbox{##1}%
      \ifdim\scratchdimen=\wd\scratchbox
        \ifcase\characteralignmentmode
          \noindent\box\scratchbox
        \else
          \noindent\hbox
            {\dontcomplain
             \hbox to \beforeassignwidth
               {\ifcase\characteralignmentmode\or
                  \box\scratchbox\hss
                \or
                  \hss\box\scratchbox\hskip\characterassignwidth
                \or
                  \hss\rlap{\box\scratchbox}%
                \or
                  \hss\rlap{\hbox to \afterassignwidth{\hss\box\scratchbox}}%
                \fi}%
             \hskip\afterassignwidth}%
        \fi
      \else
        \noindent\hbox
          {\hbox to \beforeassignwidth
             {\hss\box\scratchbox\hskip-\scratchdimen}%
           \hskip\afterassignwidth}%
      \fi}%
   \def\docheckalignment##1#1##2##3\relax%
     {\ifx##2\relax
        \setbox\scratchbox=\hbox{##1}%
      \else
        \docheckalignment##2##3\relax
      \fi}}

% provide a means to use multiple alignments mixed

\def\pushcharacteralign%
  {\setxvalue{@cac@\alignmentclass}{\noexpand\do
     {\afterassignwidth}{\beforeassignwidth}{\alignmentcharacter}}}

\def\popcharacteralign%
  {\def\do##1##2##3%
     {\def\afterassignwidth  {##1}%
      \def\beforeassignwidth {##2}%
      \def\alignmentcharacter{##3}}%
   \getvalue{@cac@\alignmentclass}}

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

\protect \endinput