typo-swp.mkxl /size: 5901 b    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=typo-col,
3%D        version=2024.11.02,
4%D          title=\CONTEXT\ Typesetting Macros,
5%D       subtitle=Column Swapping,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14
15\writestatus{loading}{ConTeXt Spacing Macros / Column Swapping}
16
17\registerctxluafile{typo-swp}{autosuffix}
18
19\unprotect
20
21\installcorenamespace {rows}
22
23\installcommandhandler \??rows {rows} \??rows
24
25\definesystemattribute[swapping][global,public]
26
27\setuprows
28  [\c!n=2,%
29  %\c!alternative=\v!tight,  % empty | tight
30  %\c!height=\zeropoint,     % empty | fit | dimension | zeropoint
31  %\c!depth=\zeropoint,      % empty | fit | dimension | zeropoint
32  %\c!width=,                % empty | fit | dimension
33   \c!before=,
34   \c!after=,
35   \c!order=\v!vertical,
36   \c!distance=2\emwidth]    % kicks in when order when tight
37
38\newinteger\c_swapping_index
39
40\appendtoks
41    \ifcstok{\rowsparameter\c!define}\v!yes
42      \frozen\protected\instance\edefcsname\e!start\currentrows\endcsname{\startrows[\currentrows]}%
43      \frozen\protected\instance\defcsname \e!stop \currentrows\endcsname{\stoprows}%
44    \fi
45\to \everydefinerows
46
47\permanent\tolerant\protected\def\startrows[#1]#*[#2]%
48  {\begingroup
49   \clf_enableswapping
50   \ifhastok={#1}%
51     \setupcurrentrows[#1]%
52   \orelse\ifparameter#1\or
53     \cdef\currentrows{#1}%
54     \ifparameter#2\or
55       \setupcurrentrows[#2]%
56     \fi
57   \fi
58   \scratchcountertwo{\rowsparameter\c!n}%
59   \scratchdistance{\rowsparameter\c!distance}%
60   \setbox\scratchboxone\vbox\bgroup
61     \holdingmigrations\maxcount % nota bene, maybe we shuld registering how many inserts are holded
62     \ifempty{\rowsparameter\c!width}%
63       % nothing
64     \orelse\ifcstok{\lastnamedcs}\v!fit
65       \hsize\distributedhsize\hsize\scratchdistance\scratchcountertwo
66       \usealignparameter\rowsparameter
67     \orelse\ifchkdimexpr\lastnamedcs\or
68       \hsize{\lastchkdimension}%
69       \usealignparameter\rowsparameter
70     \fi}
71
72\permanent\protected\def\stoprows
73  {\egroup
74   \ifvoid\scratchboxone\else
75     \rowsparameter\c!before
76     \forgetall
77     \scratchwidth{%
78       \ifcstok{\rowsparameter\c!alternative}\v!tight
79         \zeropoint
80       \orelse\ifcstok{\rowsparameter\c!width}\v!fit
81         \zeropoint
82       \else
83         \hsize/\scratchcountertwo
84       \fi
85     }%
86     \scratchcounterone{%
87       \ifcstok{\rowsparameter\c!order}\v!vertical
88          \c_swapping_index * "10000
89        + \scratchcountertwo
90       \else
91         \attributeunsetvalue
92       \fi
93     }%
94     \global\advance\c_swapping_index\plusone
95     \ifempty{\rowsparameter\c!height}%
96       \scratchheight\zeropoint
97     \orelse\ifcstok{\lastnamedcs}\v!max
98       \scratchheight\boxlinemaxht\scratchboxone\relax
99       \ifdim\scratchheight<\strutht
100          \scratchheight\zeropoint
101       \fi
102     \orelse\ifchkdimexpr\lastnamedcs\or
103       \scratchheight\lastchkdimension
104     \else
105       \scratchheight\zeropoint
106     \fi
107     \ifempty{\rowsparameter\c!depth}%
108       \scratchheight\zeropoint
109     \orelse\ifcstok{\lastnamedcs}\v!max % \iflastnamedcs
110       \scratchdepth\boxlinemaxdp\scratchboxone\relax
111       \ifdim\scratchdepth<\strutdp
112          \scratchdepth\zeropoint
113       \fi
114     \orelse\ifchkdimexpr\lastnamedcs\or
115       \scratchdepth\lastchkdimension
116     \else
117       \scratchdepth\zeropoint
118     \fi
119     \dorecurse {\boxlines \scratchboxone} {%
120       \ifzero\scratchheight\else
121         \boxlineht\scratchboxone ##1 \scratchheight
122       \fi
123       \ifzero\scratchdepth\else
124         \boxlinedp\scratchboxone ##1 \scratchdepth
125       \fi
126       \ifnum##1=\plusone
127         \dontleavehmode
128         \hbox \bgroup
129         \global\globalscratchcounterone\plustwo
130       \orelse\ifnum\globalscratchcounterone>\scratchcountertwo
131         \boundary\zerocount
132         \egroup\par
133         \dontleavehmode
134         \hbox \bgroup
135         \global\globalscratchcounterone\plustwo
136       \else
137         \global\advance\globalscratchcounterone\plusone
138       \fi
139       \ifzero\scratchwidth
140         \ifnum\globalscratchcounterone>\plustwo
141           \hskip\scratchdistance
142         \fi
143         % no need for box when not sorting
144         \setbox\scratchbox\boxline \scratchboxone ##1\relax
145         \boxmigrate\scratchbox\maxcount % nota bene
146         \ifnum\scratchcounterone=\attributeunsetvalue
147           \box\scratchbox
148         \else
149           \boxattribute \scratchbox \swappingattribute \scratchcounterone
150           \boundary\zerocount
151           \box\scratchbox
152           \boundary\zerocount
153        \fi
154       \else
155         % todo: use the box line helpers
156         \setbox\scratchbox\hpack \s!to \scratchwidth \bgroup
157           \boxline \scratchboxone ##1\relax
158           \hss
159         \egroup
160         \boxmigrate\scratchbox\maxcount % nota bene
161         \ifnum\scratchcounterone=\attributeunsetvalue
162           \box\scratchbox
163         \else
164           \boxattribute \scratchbox \swappingattribute \scratchcounterone
165           \boundary\zerocount
166           \box\scratchbox
167           \boundary\zerocount
168         \fi
169       \fi
170     }%
171     \ifnum\globalscratchcounterone>\plusone
172       \ifnum\scratchcounterone=\attributeunsetvalue
173          % nothing to do here
174       \else
175         % todo: use loop
176         \dostepwiserecurse {\globalscratchcounterone} \scratchcountertwo {\plusone} {%
177           \ifzero\scratchwidth
178             \hskip{\rowsparameter\c!distance}%
179           \fi
180           \hpack \s!attr \swappingattribute \scratchcounterone \bgroup
181             \null
182           \egroup
183           \boundary\zerocount}%
184       \fi
185       \hss
186       \egroup\par
187     \fi
188     \rowsparameter\c!after
189   \fi
190   \endgroup}
191
192\protect
193