page-ins.mkxl /size: 8722 b    last modification: 2025-02-21 11:03
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}{autosuffix}
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% Per January 2025 we use the primitives directly becuase we won't switch back
121% to the old mechanism.
122
123\insertmode\plustwo \pushoverloadmode \newinteger\insertmode \popoverloadmode
124
125% \ifnum\insertmode=\plustwo
126
127%   \protected\def\page_inserts_inject        {\insert}
128
129%   \protected\def\page_inserts_set_limit     {\insertlimit}
130%   \protected\def\page_inserts_set_distance  {\insertdistance}
131%   \protected\def\page_inserts_set_multiplier{\insertmultiplier}
132%   \protected\def\page_inserts_set_penalty   {\insertpenalty}
133%   \protected\def\page_inserts_set_maxdepth  {\insertmaxdepth}
134%   \protected\def\page_inserts_set_lineheight{\insertlineheight}
135%   \protected\def\page_inserts_set_linedepth {\insertlinedepth}
136
137%   \protected\def\page_inserts_get_limit     {\insertlimit}
138%   \protected\def\page_inserts_get_distance  {\insertdistance}
139%   \protected\def\page_inserts_get_multiplier{\insertmultiplier}
140%   \protected\def\page_inserts_get_penalty   {\insertpenalty}
141%   \protected\def\page_inserts_get_maxdepth  {\insertmaxdepth}
142
143%   \protected\def\page_inserts_get_boxed     {\insertbox}
144%   \protected\def\page_inserts_get_copied    {\insertcopy}
145%   \protected\def\page_inserts_get_unboxed   {\insertunbox}
146%   \protected\def\page_inserts_get_uncopied  {\insertuncopy}
147
148%   \protected\def\page_inserts_get_height    {\insertheight}
149%   \protected\def\page_inserts_get_depth     {\insertdepth}
150%   \protected\def\page_inserts_get_width     {\insertwidth}
151
152% \else
153%
154%     \protected\def\page_inserts_inject        {\insert}
155%
156%     \protected\def\page_inserts_set_limit     {\global\dimen}
157%     \protected\def\page_inserts_set_distance  {\global\skip }
158%     \protected\def\page_inserts_set_multiplier{\global\count}
159%
160%     \protected\def\page_inserts_get_limit     {\dimen}
161%     \protected\def\page_inserts_get_distance  {\skip }
162%     \protected\def\page_inserts_get_multiplier{\count}
163%
164%     \protected\def\page_inserts_get_boxed     {\box}
165%     \protected\def\page_inserts_get_copied    {\copy}
166%     \protected\def\page_inserts_get_unboxed   {\unvbox}
167%     \protected\def\page_inserts_get_uncopied  {\unvcopy}
168%
169%     \protected\def\page_inserts_get_height    {\ht}
170%     \protected\def\page_inserts_get_depth     {\dp}
171%     \protected\def\page_inserts_get_width     {\wd}
172%
173% \fi
174
175\newinteger\c_page_inserts_last_allocated
176
177\appendtoks
178    \ifempty\currentinsertionparent
179        \ifcsname\??insertionnumber\currentinsertion\endcsname
180          % bad news
181        \else
182          %expandafter\newinsert\csname\??insertionnumber\currentinsertion\endcsname
183          \global\advanceby\c_page_inserts_last_allocated\plusone
184          \global\expandafter\integerdef\csname\??insertionnumber\currentinsertion\endcsname\c_page_inserts_last_allocated
185          \page_inserts_synchronize_registers
186          \clf_defineinsertion
187            {\currentinsertion}%
188            {%
189                number \currentinsertionnumber
190            }%
191          \relax
192%           \t_page_inserts_list\expandafter\expandafter\expandafter
193%             {\expandafter\the\expandafter\t_page_inserts_list
194%              \expandafter\page_inserts_process\csname\??insertionnumber\currentinsertion\endcsname}%
195          \xtoksapp\t_page_inserts_list{\page_inserts_process{\currentinsertion}}%
196          \insertmultiplier\currentinsertionnumber\plusthousand
197          \insertdistance  \currentinsertionnumber\zeropoint
198          \insertlimit     \currentinsertionnumber\maxdimen
199       \fi
200    \else
201      \letcsname\??insertionnumber\currentinsertion\expandafter\endcsname
202         \csname\??insertionnumber\currentinsertionparent\endcsname
203    \fi
204\to \everydefineinsertion
205
206\appendtoks
207    \ifempty\currentinsertionparent \else
208        \clf_setupinsertion
209          {\currentinsertion}
210          {%
211            location {\insertionparameter\c!location}%
212          }%
213        \relax
214    \fi
215\to \everysetupinsertion
216
217\protected\def\page_inserts_set_location#1#2% fast one
218  {\clf_setinsertionlocation{#1}{#2}}
219
220%D Auxiliary macros:
221
222\def\page_insert_insertion_height#1%
223  {\dimexpr\expandafter\page_insert_insertion_height_indeed\csname\??insertionnumber#1\endcsname\relax}
224
225% \def\page_insert_insertion_height_indeed#1%
226%   {\ifvoid#1\zeropoint\else1\page_inserts_get_distance#1+\ht#1\fi}
227
228\def\page_insert_insertion_height_indeed#1%
229  {\ifzeropt\insertheight#1\zeropoint\else\dimexpr\insertdistance#1\relax+\insertheight#1\fi}
230
231%D Tricky stuff:
232%D
233%D \starttyping
234%D \page_inserts_detach\floatbox      % \detachinsertions\floatbox
235%D \page_otr_command_flush_float_box
236%D \page_inserts_attach               % \attachinsertions\attachinsert
237%D \stoptyping
238
239\permanent\protected\def\attachinsert#1%
240  {\dontleavehmode#1\par}
241
242\def\page_inserts_detach#1%
243  {\detachinsertions#1\relax}
244
245\def\page_inserts_attach_indeed
246  {\dorecurse
247     {\nofdetachedinsertions\currentnoteinsertionnumber}
248     {\attachinsertion\currentnoteinsertionnumber\attachinsert}}
249
250\def\page_inserts_attach
251  {\strc_notes_process\page_inserts_attach_indeed}
252
253\protect \endinput
254