page-ins.mkxl /size: 7882 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=page-ins,
3%D        version=2002.04.16, % was core-ins
4%D          title=\CONTEXT\ Insertion Macros,
5%D       subtitle=Insertions,
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\writestatus{loading}{ConTeXt Core Macros / Insertions}
15
16%D Insertions are special data collections that are associated to \TEX's internal
17%D page builder. When multiple footnote classes were introduced, I decided to
18%D isolate some of the functionality in a module.
19
20\registerctxluafile{page-ins}{}
21
22\unprotect
23
24%D Because we need to deal with inserts at the \LUA\ end as well, we provide a
25%D proper installer.
26
27\installcorenamespace{insertion}
28\installcorenamespace{insertionnumber}
29
30\installcommandhandler \??insertion {insertion} \??insertion
31
32\setupinsertion
33  [%c!n=\plusone,
34   %c!distance=\zeropoint,
35   %c!maxheight=\maxdimen,
36   %c!factor=\plusthousand,
37   \c!location=\v!page]
38
39\newconstant\insertionmigrationmode
40
41\def\page_inserts_check_anchoring
42  {\edef\p_anchoring{\namedinsertionparameter\empty\c!anchoring}%
43   \insertionmigrationmode\ifx\p_anchoring\v!auto\plusone\else\zerocount\fi
44   \clf_setinsertmigration{\p_anchoring}}
45
46\appendtoks
47    \ifempty\currentinsertionparent
48        \page_inserts_check_anchoring
49    \fi
50\to \everysetupinsertion
51
52\setupinsertion
53  [\c!anchoring=\v!auto]
54
55\appendtoks
56    \page_inserts_check_anchoring
57\to \everystarttext % or \everyjob
58
59\newinteger\currentinsertionnumber % This is a count and not a macro !
60
61\newtoks\t_page_inserts_list
62
63\mutable\let\doprocessinsert\gobbleoneargument % probably kind of obsolete now
64
65%D Maybe some day we will move settings here.
66
67\permanent\protected\def\setcurrentinsertion#1%
68  {\cdef\currentinsertion{#1}%
69   \currentinsertionnumber\csname\??insertionnumber\currentinsertion\endcsname}
70
71\permanent\def\namedinsertionnumber#1{\csname\??insertionnumber#1\endcsname}
72
73\protected\def\page_inserts_synchronize_registers
74  {\currentinsertionnumber\csname\??insertionnumber\currentinsertion\endcsname}
75
76\appendtoks
77    \ifempty\currentinsertionparent \else
78        \page_inserts_synchronize_registers
79    \fi
80\to \everysetupinsertion
81
82\protected\def\page_inserts_process#1% beware, this adapts currentinsertion !
83  {\cdef\currentinsertion{#1}%
84   \currentinsertionnumber\csname\??insertionnumber\currentinsertion\endcsname
85   \doprocessinsert\currentinsertionnumber} % old method
86
87\permanent\protected\def\processinsertions
88  {\expand\t_page_inserts_list}
89
90% \permanent\protected\def\synchronizeinsertions
91%   {\let\doprocessinsert\page_inserts_synchronize
92%    \processinsertions}
93%
94% \protected\def\page_inserts_synchronize#1% yes or no
95%   {\ifvoid#1\else
96%      \page_inserts_inject#1{\unvbox#1}%
97%    \fi}
98
99\permanent\protected\def\doifelseinsertion#1%
100  {\ifcsname\??insertionnumber#1\endcsname
101     \expandafter\firstoftwoarguments
102   \else
103     \expandafter\secondoftwoarguments
104   \fi}
105
106\aliased\let\doifinsertionelse\doifelseinsertion
107
108% \protected\def\startinsertion[#1]%
109%   {\page_inserts_inject\csname\??insertionnumber#1\endcsname\bgroup}
110%
111% \protected\def\stopinsertion
112%   {\egroup}
113
114% For the moment we use the regular insertion allocator so that users can still
115% define their own insertions (not that they will play nicely with all context
116% mechanisms then). We can use the dimensions at the \LUA\ end so we don't need to
117% pass pass them explicitly. Actually, when we see an insertion node at that end,
118% we already know the number.
119
120\insertmode\plustwo
121
122% \ifnum\insertmode=\plustwo
123
124    \protected\def\page_inserts_inject        {\insert}
125
126    \protected\def\page_inserts_set_limit     {\insertlimit}
127    \protected\def\page_inserts_set_distance  {\insertdistance}
128    \protected\def\page_inserts_set_multiplier{\insertmultiplier}
129    \protected\def\page_inserts_set_penalty   {\insertpenalty}
130    \protected\def\page_inserts_set_maxdepth  {\insertmaxdepth}
131
132    \protected\def\page_inserts_get_limit     {\insertlimit}
133    \protected\def\page_inserts_get_distance  {\insertdistance}
134    \protected\def\page_inserts_get_multiplier{\insertmultiplier}
135    \protected\def\page_inserts_get_penalty   {\insertpenalty}
136    \protected\def\page_inserts_get_maxdepth  {\insertmaxdepth}
137
138    \protected\def\page_inserts_get_boxed     {\insertbox}
139    \protected\def\page_inserts_get_copied    {\insertcopy}
140    \protected\def\page_inserts_get_unboxed   {\insertunbox}
141    \protected\def\page_inserts_get_uncopied  {\insertuncopy}
142
143    \protected\def\page_inserts_get_height    {\insertheight}
144    \protected\def\page_inserts_get_depth     {\insertdepth}
145    \protected\def\page_inserts_get_width     {\insertwidth}
146
147% \else
148%
149%     \protected\def\page_inserts_inject        {\insert}
150%
151%     \protected\def\page_inserts_set_limit     {\global\dimen}
152%     \protected\def\page_inserts_set_distance  {\global\skip }
153%     \protected\def\page_inserts_set_multiplier{\global\count}
154%
155%     \protected\def\page_inserts_get_limit     {\dimen}
156%     \protected\def\page_inserts_get_distance  {\skip }
157%     \protected\def\page_inserts_get_multiplier{\count}
158%
159%     \protected\def\page_inserts_get_boxed     {\box}
160%     \protected\def\page_inserts_get_copied    {\copy}
161%     \protected\def\page_inserts_get_unboxed   {\unvbox}
162%     \protected\def\page_inserts_get_uncopied  {\unvcopy}
163%
164%     \protected\def\page_inserts_get_height    {\ht}
165%     \protected\def\page_inserts_get_depth     {\dp}
166%     \protected\def\page_inserts_get_width     {\wd}
167%
168% \fi
169
170\newinteger\c_page_inserts_last_allocated
171
172\appendtoks
173    \ifempty\currentinsertionparent
174        \ifcsname\??insertionnumber\currentinsertion\endcsname
175          % bad news
176        \else
177          %expandafter\newinsert\csname\??insertionnumber\currentinsertion\endcsname
178          \global\advanceby\c_page_inserts_last_allocated\plusone
179          \global\expandafter\integerdef\csname\??insertionnumber\currentinsertion\endcsname\c_page_inserts_last_allocated
180          \page_inserts_synchronize_registers
181          \clf_defineinsertion
182            {\currentinsertion}%
183            {%
184                number \currentinsertionnumber
185            }%
186          \relax
187%           \t_page_inserts_list\expandafter\expandafter\expandafter
188%             {\expandafter\the\expandafter\t_page_inserts_list
189%              \expandafter\page_inserts_process\csname\??insertionnumber\currentinsertion\endcsname}%
190          \xtoksapp\t_page_inserts_list{\page_inserts_process{\currentinsertion}}%
191          \page_inserts_set_multiplier\currentinsertionnumber\plusthousand
192          \page_inserts_set_distance  \currentinsertionnumber\zeropoint
193          \page_inserts_set_limit     \currentinsertionnumber\maxdimen
194       \fi
195    \else
196      \letcsname\??insertionnumber\currentinsertion\expandafter\endcsname
197         \csname\??insertionnumber\currentinsertionparent\endcsname
198    \fi
199\to \everydefineinsertion
200
201\appendtoks
202    \ifempty\currentinsertionparent \else
203        \clf_setupinsertion
204          {\currentinsertion}
205          {%
206            location {\insertionparameter\c!location}%
207          }%
208        \relax
209    \fi
210\to \everysetupinsertion
211
212\protected\def\page_inserts_set_location#1#2% fast one
213  {\clf_setinsertionlocation{#1}{#2}}
214
215%D Auxiliary macros:
216
217\def\page_insert_insertion_height#1%
218  {\dimexpr\expandafter\page_insert_insertion_height_indeed\csname\??insertionnumber#1\endcsname\relax}
219
220% \def\page_insert_insertion_height_indeed#1%
221%   {\ifvoid#1\zeropoint\else1\page_inserts_get_distance#1+\ht#1\fi}
222
223\def\page_insert_insertion_height_indeed#1%
224  {\ifzeropt\page_inserts_get_height#1\zeropoint\else\dimexpr\page_inserts_get_distance#1\relax+\page_inserts_get_height#1\fi}
225
226\protect \endinput
227