syst-ini.mkxl /size: 56 Kb    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=syst-ini,
3%D        version=2008.11.04, % 2001.11.16, % 1999.03.17,  % an oldie: 1995.10.10
4%D          title=\CONTEXT\ System Macros,
5%D       subtitle=Bootstrapping \TEX,
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%D We used to load plain \TEX\ in a special way, but redefining a couple of
15%D primitives so that for instance font loading was ignored. For those interested,
16%D this loader is found in \type {syst-tex.tex}. Some of the comment's are Don
17%D Knuths and more of it can be found in the plain \TEX\ format.
18%D
19%D Successive versions will be adapted to \LUAMETATEX. That also means that we
20%D define things a bit differently than in \MKII\ and \MKIV. This is party due to
21%D the fact that in this engine we removed some side effects or added features. The
22%D original \TEX\ program is well defined and documented and is still the benchmark.
23%D The \ETEX\ extensions originally were meant as systematic follow up but that
24%D never went beyond a first upgrade. It had a dual mode: compatible or extensible.
25%D We always used the second mode. The \LUATEX\ engines don't have that.
26%D
27%D There are some hard coded properties that relate to for instance node types but
28%D we always used abstraction. There are also side effects that (probably) originate
29%D in the fact that \ETEX\ had to stay compatible with the dominant set of macros
30%D using the original \TEX\ ones (either or not wrapped). Even unwanted side effects
31%D eventually become features. In\LUATEX\ and even more in \LUAMETATEX\ we get
32%D around that by additional mode variables that you can find below.
33%D
34%D Side note: those who complain about \TEX\ as rather special language, \ETEX\
35%D extensions that should have been or be different, should really look into how
36%D \TEX\ and friends evolved, and not come to quick and wrong conclusions. There are
37%D only a very few languages that evolve this way. As a comparison one can wonder
38%D why \CCODE\ never had proper strings like \PASCAL\ bit it's just hindsight and
39%D discussing it has no use. In a similar fashion one should keep in mind that what
40%D you see here is just a logical follow up in \MKII\ where we had to bend the rules
41%D and \MKIV\ where we had a bit more available. Not many who look at the latest
42%D engines had to jump though all the hoops.
43%D
44%D This is for instance noticeable in the names of some primitvies: \CONTEXT\ has a
45%D concept of protection before \type {\protected} showed up, and similarly had
46%D expansion helpers. This is why in \MKII\ you find \type {\protected}, \type
47%D {\expanded}, and \type {\unexpanded} commands with different meanings than the
48%D later \ETEX\ primitives. In \MKIV\ we stuck to the \CONTEXT\ originals and use
49%D \type {\normal...} ones when needed (so \type {\normal...} is there for a
50%D reason!). In \LMTX\ we made the transition from \type {\unexpanded} to \type
51%D {\protected} so again this file looks different than the \MKIV\ ancestor.
52%D
53%D When primitives get initialized they come from the \TEX\ namespace (the
54%D originals), the \ETEX\ extensions (although we dropped some) and \LUATEX\ which
55%D includes the \LUAMETATEX\ ones. The most noticeable \ETEX\ extensions we kept are
56%D those that relate to expansion and the expression scanners. The last group has
57%D always been somewhat special because (we were told) it was modelled after a macro
58%D set so it had to be compatible. It is tempting to replace it (and eventually I
59%D might do that) but for now we stick to them (although with an integer divide
60%D extension (like the \LUA\ \type {//} and the source has commented binary
61%D operators but I haven't yet decided on the symbols to use because we need to
62%D avoid expansion issues). For the record: the more extensive, related to \ETEX\
63%D follow up \NTS\ never took of, probably because of the implementation language
64%D and because performance made it unuseable. This is also why \LUATEX\ took a
65%D different route: extending by an extension language. Of course in the end some
66%D core functionality got added too.
67%D
68%D The most noticeable \LUAMETATEX\ specific code here has to do with additional
69%D data types (integer and dimension constants) and macro argument extensions. We
70%D also set up some defaults in handling math, languages, etc. Elsewhere in the
71%D \CONTEXT\ source the real configuration and usage happens so don't consider this
72%D an tutorial (as the plain format is).
73%D
74%D Category codes are another areas where subtle changed happened. You might not
75%D grasp it (maybe because \CONTEXT\ is alien to you) but whenever you see something
76%D that looks weird keep in mind that there is a valid reason for it.
77%D
78%D In due time I will add some more comments here, also because some \CONTEXT\
79%D users might be interested in the hostiry. Now to the code!
80
81%D Characters can have special states, that can be triggered by setting their
82%D category coded. Some are preset, others are to be set as soon as possible,
83%D otherwise we cannot define any useful macros.
84%D
85%D First we define a bunch of constants. Normally we would \type {\setconstant}
86%D but we're prestine and have no macros defined yet. Abstraction also makes it
87%D possible to avoid the \type {^^} in the input. We no longer define the \ETEX\
88%D nodes here (we have not only more, but also different numbers) so we do it
89%D elsewhere.
90
91% cheatsheet
92%
93% # : 0--9
94% * : ignore spaces
95% + : keep the braces
96% - : discard and don't count the argument
97% . : ignore pars and spaces
98% , : push back space when no match
99% / : remove leading and trailing spaces and pars
100% : : pick up scanning here
101% ; : quit scanning
102% = : braces are mandate
103% ^ : keep leading spaces
104% _ : braces are mandate and kept
105
106\immutable\integerdef\escapecatcode             0
107\immutable\integerdef\begingroupcatcode         1
108\immutable\integerdef\endgroupcatcode           2
109\immutable\integerdef\mathshiftcatcode          3
110\immutable\integerdef\alignmentcatcode          4
111\immutable\integerdef\endoflinecatcode          5
112\immutable\integerdef\parametercatcode          6
113\immutable\integerdef\superscriptcatcode        7
114\immutable\integerdef\subscriptcatcode          8
115\immutable\integerdef\ignorecatcode             9
116\immutable\integerdef\spacecatcode             10
117\immutable\integerdef\lettercatcode            11
118\immutable\integerdef\othercatcode             12  % finally obsolete: \let\other \othercatcode
119\immutable\integerdef\activecatcode            13  % finally obsolete: \let\active\activecatcode
120\immutable\integerdef\commentcatcode           14
121\immutable\integerdef\invalidcatcode           15
122
123%immutable\integerdef\zeroasciicode             0
124\immutable\integerdef\tabasciicode              9
125\immutable\integerdef\newlineasciicode         10  % don't confuse this one with \endoflineasciicode
126\immutable\integerdef\formfeedasciicode        12
127\immutable\integerdef\endoflineasciicode       13  % somewhat messy but this can be the active \par
128\immutable\integerdef\endoffileasciicode       26
129\immutable\integerdef\spaceasciicode           32
130\immutable\integerdef\exclamationmarkasciicode 33 % ! used in namespace protection
131\immutable\integerdef\doublequoteasciicode     34 % "
132\immutable\integerdef\hashasciicode            35
133\immutable\integerdef\dollarasciicode          36
134\immutable\integerdef\commentasciicode         37
135\immutable\integerdef\ampersandasciicode       38
136\immutable\integerdef\singlequoteasciicode     39 % '
137\immutable\integerdef\primeasciicode           39 % '
138\immutable\integerdef\leftparentasciicode      40
139\immutable\integerdef\rightparentasciicode     41
140\immutable\integerdef\commaasciicode           44
141\immutable\integerdef\hyphenasciicode          45
142\immutable\integerdef\periodasciicode          46
143\immutable\integerdef\forwardslashasciicode    47 % /
144\immutable\integerdef\colonasciicode           58
145\immutable\integerdef\lessthanasciicode        60 % < used as alternative verbatim {
146\immutable\integerdef\morethanasciicode        62 % > used as alternative verbatim }
147\immutable\integerdef\questionmarkasciicode    63 % ? used in namespace protection
148\immutable\integerdef\atsignasciicode          64 % @ used in namespace protection
149\immutable\integerdef\leftbracketasciicode     91
150\immutable\integerdef\backslashasciicode       92 % `\\
151\immutable\integerdef\rightbracketasciicode    93
152\immutable\integerdef\circumflexasciicode      94
153\immutable\integerdef\underscoreasciicode      95
154\immutable\integerdef\leftbraceasciicode      123 % `\{
155\immutable\integerdef\barasciicode            124 % `\|
156\immutable\integerdef\rightbraceasciicode     125 % `\}
157\immutable\integerdef\tildeasciicode          126 % `\~
158\immutable\integerdef\delasciicode            127
159
160\aliased\let\percentasciicode\commentasciicode
161
162%catcode\zeroasciicode           \ignorecatcode      % `\^^@   ascii null is ignored
163\catcode\tabasciicode            \spacecatcode       % `\^^I   ascii tab is a blank space
164\catcode\formfeedasciicode       \activecatcode      % `\^^L   ascii form-feed (active, set later)
165%catcode\endoflineasciicode      \endoflinecatcode   % `\^^M   ascii return is end-line
166\catcode\endoffileasciicode      \ignorecatcode      % `\^^Z   endoffile (ignored in ConTeXt)
167%catcode\spaceasciicode          \spacecatcode       % `\      ascii space is blank space
168\catcode\hashasciicode           \parametercatcode   % `\#     hash mark is macro parameter character
169\catcode\dollarasciicode         \mathshiftcatcode   % `\$     dollar sign is math shift
170%catcode\commentasciicode        \commentcatcode     % `\%     percent sign is comment character
171\catcode\ampersandasciicode      \alignmentcatcode   % `\&     ampersand is alignment tab
172%catcode\backslashasciicode      \escapecatcode      % `\\     backslash is TeX escape character
173\catcode\circumflexasciicode     \superscriptcatcode % `\^     circumflex and uparrow are for superscripts
174\catcode\underscoreasciicode     \subscriptcatcode   % `\_     underline and downarrow are for subscripts
175\catcode\leftbraceasciicode      \begingroupcatcode  % `\{     left brace is begin-group character
176\catcode\rightbraceasciicode     \endgroupcatcode    % `\}     right brace is end-group character
177\catcode\tildeasciicode          \activecatcode      % `\~     tilde is active
178%catcode\delasciicode            \invalidcatcode     % `\^^?   ascii delete is invalid
179
180%D First we need to initialization the primitives. Because \CONTEXT\ is already a
181%D pretty old macro package, we have a couple of issues with respect to primitives.
182%D The \ETEX\ engine added a few as did \PDFTEX. The \LUATEX\ engine added even
183%D more. This means that there can be a potential clash between primitives and
184%D existing macros. The most noticeable ones are:
185%D
186%D \starttyping
187%D \protected
188%D \expanded
189%D \unexpanded
190%D \stoptyping
191%D
192%D Because we had macros like that before the primitives showed up. The protection
193%D related macros were there before we even knew about extensions to the engine.
194%D When the expansion related ones were introduced, we originally came up with
195%D different names but due to requests we used the current names, somethng that in
196%D retrospect was a bad idea: they should have gotten different names in \LUATEX, if
197%D only because at that time only \CONTEXT\ was using them in rolling releases.
198%D Anyway, we're now stuck with this situation, and it means that one should use the
199%D \type {\normal...} variants in low level code:
200%D
201%D \starttyping
202%D \normalexpanded
203%D \normalprotected
204%D \normalunexpanded
205%D \stoptyping
206%D
207%D In the end not using different names in \LUATEX\ for these kind of backfires. It
208%D makes not much sense to fix this in \LUAMETATEX\ because we're now to long on the
209%D road. It is actually the reason why we have the option in \LUATEX\ to alias all
210%D primitives in one go using a prefix. Actually this trick could be used to recover
211%D a primitive meaning: just enable it with some prefix and \type {\let} the
212%D original to that. But \unknown\ we prevent that trick below.
213%D
214%D The code below differs from \LUATEX: in \LUAMETATEX\ all primitives are already
215%D available; it cannot limit itself to being \TEX\ or \ETEX. It could not do that
216%D anyway because there are differences (no backend, to mention one).
217
218\directlua {
219    tex.enableprimitives("normal",true) % we default to everything
220    function tex.enableprimitives() end % so we kind of protect what's there
221}
222
223\untraced\permanent\let\undefined\undefined
224
225\immutable\def\space{ }
226\immutable\def\empty{}
227\mutable  \def\temp {}
228
229\immutable\letcharcode \formfeedasciicode \par    % \def ^^L{\par}  formfeed
230\immutable\letcharcode \tildeasciicode    \       %                 tilde
231\immutable\letcharcode \spaceasciicode    \space  %                 space
232
233\immutable\defcsname\Uchar\tabasciicode      \endcsname {\ }   % \def\^^I{\ }    tab
234\immutable\defcsname\Uchar\formfeedasciicode \endcsname {\par} % \def\^^L{\par}  formfeed
235\immutable\defcsname\Uchar\endoflineasciicode\endcsname {\ }   % \def\^^M{\ }    return
236
237%D For now:
238
239\permanent\def\gobbleoneargument#-{} % will be defined later on anyway
240
241%D First we define a simplified version of the \CONTEXT\ protection mechanism. Later
242%D we will implement a better variant.
243
244\def\unprotect
245  {\edef\protect
246     {\catcode\atsignasciicode         \the\catcode\atsignasciicode         \relax
247      \catcode\exclamationmarkasciicode\the\catcode\exclamationmarkasciicode\relax
248      \catcode\questionmarkasciicode   \the\catcode\questionmarkasciicode   \relax
249      \catcode\underscoreasciicode     \the\catcode\underscoreasciicode     \relax
250      \let\protect\relax}%
251   \catcode\atsignasciicode         \lettercatcode
252   \catcode\exclamationmarkasciicode\lettercatcode
253   \catcode\questionmarkasciicode   \lettercatcode
254   \catcode\underscoreasciicode     \lettercatcode}
255
256\let\protect\relax
257
258\unprotect
259
260%D Some pretty important definitions:
261
262\immutable\let\bgroup={
263\immutable\let\egroup=}
264
265%D \macros
266%D   {normalbgroup,normalgroup}
267%D
268%D No comment.
269
270%D Allocation of registers is done slightly different than in plain \TEX. First of
271%D all we use different reserved counters. We also don't implement a family handler
272%D because users are not supposed to implement their own math. We reserve the lowest
273%D 31 registers for scratch purposes. Keep in mind that in the core engine some
274%D registers are reserved: counters 0 upto 9, and counter 255.
275%D
276%D As with plain \TEX\ we recommend that macro designers always use \type {\global}
277%D assignments with respect to registers numbered 1, 3, 5 \unknown\ 31, and always
278%D non||\type {\global} assignments with respect to registers 0, 2, 4, \unknown\ 30.
279%D This will prevent \quote {save stack buildup} that might otherwise occur.
280%D
281%D We reserve some registers for special (management) purposes. In the mkiv file you
282%D can see how we also (even if we don't use it) deal with families and languages
283%D and distinguish read and write ranges. There we also use a more traditional
284%D insert allocator that operates in its own range. In \MKIV\ the ranges are:
285
286%   0 -  20 : scratch
287%  21 - 127 : internal
288% 128 - 254 : inserts   (no longer)
289% 255       : page
290% 256 -     : user defined
291
292%D However, in \LMTX\ we have:
293
294%   0 - 199 : scratch
295% 200 - 254 : internal
296% 255       : page
297% 256 -     : user defined
298
299%D Later on, the \type {\c_syst_max_allocated_register} variable will be halfed so
300%D that while local allocators will use the 22* range. So we have plenty of room
301%D reserved for more private ones.
302
303\permanent\countdef\c_syst_min_allocated_register  = 201 \c_syst_min_allocated_register  =   256 % can change
304\permanent\countdef\c_syst_max_allocated_register  = 202 \c_syst_max_allocated_register  = 65535 % is adapted later on
305\permanent\countdef\c_syst_min_allocated_iohandle  = 203 \c_syst_min_allocated_iohandle  =     0
306\permanent\countdef\c_syst_max_allocated_iohandle  = 204 \c_syst_max_allocated_iohandle  =  1023
307\permanent\countdef\c_syst_min_allocated_attribute = 205 \c_syst_min_allocated_attribute =  1024 % 0-1023 : private
308\permanent\countdef\c_syst_min_allocated_mark      = 206 \c_syst_min_allocated_mark      =    16 % a few scratch ones
309\permanent\countdef\c_syst_max_allocated_mark      = 207 \c_syst_max_allocated_mark      =  1024 % max 10K in luametatex anyway
310
311\permanent\countdef\c_syst_last_allocated_count    = 211 \c_syst_last_allocated_count  = \c_syst_min_allocated_register
312\permanent\countdef\c_syst_last_allocated_dimen    = 212 \c_syst_last_allocated_dimen  = \c_syst_min_allocated_register
313\permanent\countdef\c_syst_last_allocated_skip     = 213 \c_syst_last_allocated_skip   = \c_syst_min_allocated_register
314\permanent\countdef\c_syst_last_allocated_muskip   = 214 \c_syst_last_allocated_muskip = \c_syst_min_allocated_register
315\permanent\countdef\c_syst_last_allocated_box      = 215 \c_syst_last_allocated_box    = \c_syst_min_allocated_register
316\permanent\countdef\c_syst_last_allocated_toks     = 216 \c_syst_last_allocated_toks   = \c_syst_min_allocated_register
317\permanent\countdef\c_syst_last_allocated_read     = 217 \c_syst_last_allocated_read   = \c_syst_min_allocated_iohandle
318\permanent\countdef\c_syst_last_allocated_write    = 218 \c_syst_last_allocated_write  = \c_syst_min_allocated_iohandle
319\permanent\countdef\c_syst_last_allocated_marks    = 219 \c_syst_last_allocated_marks  = \c_syst_min_allocated_mark
320
321\permanent\countdef\c_syst_min_counter_value       = 253 \c_syst_min_counter_value = -"7FFFFFFF
322\permanent\countdef\c_syst_max_counter_value       = 254 \c_syst_max_counter_value =  "7FFFFFFF
323
324\immutable\integerdef\zerocount       0
325\immutable\integerdef\plusone         1
326\immutable\integerdef\minusone       -1
327
328\immutable\integerdef\normalpagebox 255  % hardcoded in pdftex/xetex
329
330\immutable\integerdef\statuswrite   \minusone
331
332%D So, effectively we start allocating from 256 and upwards. The inserts sit in the
333%D range 128 upto 254. Page numbers use the counters 0 upto 9 and the pagebox is
334%D 255. Users can use the scratch registers upto 31 without problem but all others
335%D are reserved.
336
337\permanent\let\wlog\gobbleoneargument % Let's get rid of this one.
338
339%D The allocators share a common helper macro. Marks might be changed to work in lower
340%D regions and we always assumes allocation.
341
342\permanent\protected\def\newcount {\syst_basics_allocate\c_syst_last_allocated_count \count \countdef  \c_syst_max_allocated_register}
343\permanent\protected\def\newdimen {\syst_basics_allocate\c_syst_last_allocated_dimen \dimen \dimendef  \c_syst_max_allocated_register}
344\permanent\protected\def\newskip  {\syst_basics_allocate\c_syst_last_allocated_skip  \skip  \skipdef   \c_syst_max_allocated_register}
345\permanent\protected\def\newmuskip{\syst_basics_allocate\c_syst_last_allocated_muskip\muskip\muskipdef \c_syst_max_allocated_register}
346\permanent\protected\def\newbox   {\syst_basics_allocate\c_syst_last_allocated_box   \box   \integerdef\c_syst_max_allocated_register}
347\permanent\protected\def\newtoks  {\syst_basics_allocate\c_syst_last_allocated_toks  \toks  \toksdef   \c_syst_max_allocated_register}
348\permanent\protected\def\newread  {\syst_basics_allocate\c_syst_last_allocated_read  \read  \integerdef\c_syst_max_allocated_iohandle}
349\permanent\protected\def\newwrite {\syst_basics_allocate\c_syst_last_allocated_write \write \integerdef\c_syst_max_allocated_iohandle}
350\permanent\protected\def\newmarks {\syst_basics_allocate\c_syst_last_allocated_marks \marks \integerdef\c_syst_max_allocated_mark    }
351
352\firstvalidlanguage \plusone % so zero is ignored in hyphenation, this might become the default
353
354%D Watch out, for the moment we disable the check for already being defined later we
355%D will revert this but first all chardefs must be replaced.
356
357\permanent\protected\def\setconstant         {}
358\permanent\protected\def\setconstantvalue#1#2{\csname#1\endcsname\numexpr#2\relax}
359
360\permanent\protected\def\newconstant#1%
361  {\ifrelax#1%
362    %\writestatus{!!!!!!!!!!}{\string #1 => RELAX}%
363     \unletfrozen#1%
364     \newcount#1%
365   \orelse\ifdefined#1%
366    %\writestatus{!!!!!!!!!!}{\string #1 => DEFINED}%
367   \else
368     \newcount#1%
369   \fi}
370
371\permanent\protected\def\setnewconstant#1{\newconstant#1#1}
372
373% %D The next definitions are really needed (in \CONTEXT):
374
375\newlinechar\newlineasciicode  \permanent\edef\outputnewlinechar{\Uchar\newlineasciicode} % {^^J}
376
377%D One reason to start high with allocation is that it permits us to allocate
378%D consecutive ranges more easily, for instance if for \MPLIB\ we want to allocate a
379%D continuous range of boxes. It also permits us to do a proper upward allocation
380%D for inserts. The current code evolved from code that dealt with older engines but
381%D as all engines now provide many registers we removed all traces. If you ever run
382%D into the \MKII\ \type {mptopdf} code, you'll notice that some register magic was
383%D needed to fit into existing macro packages. The same is true for \PICTEX\ that we
384%D used in (gheneric) \PPCHTEX.
385
386\ifdefined\writestatus \else
387   %\protected\def\writestatus#1#2{\immediate\write\statuswrite{#1: #2}}
388    \frozen\protected\def\writestatus#1#2{\message{#1: #2}}
389\fi
390
391\def\syst_basics_allocate_yes#1#2#3#4#5% last class method max name
392  {\ifnum#1<#4\relax
393     \global\advance#1\plusone
394     \permanent\global#3#5=#1\relax
395   \else
396     \writestatus{warning}{no room for \string#2\space \string#5\space (max: \number#4)}%
397   \fi}
398
399\def\syst_basics_allocate_nop#1#2#3#4#5% last class method max name
400  {\writestatus{warning}{\string#2 \string#5 is already defined (\string\relax\space it first)}}
401
402\def\syst_basics_allocate#1#2#3#4#5% last class method max name
403  {\ifrelax#5%
404     \expandafter\syst_basics_allocate_yes
405   \orelse\ifdefined#5%
406     \expandafter\syst_basics_allocate_nop
407   \else
408     \expandafter\syst_basics_allocate_yes
409   \fi
410   #1#2#3#4#5}
411
412%D Since the number of chars exceed 256 now, we can use \type {\chardef} instead of
413%D the more limited \type {\mathchardef}.
414
415% \protected\def\newbox  {\syst_basics_allocate\c_syst_last_allocated_box  \box  \chardef\c_syst_max_allocated_register}
416% \protected\def\newmarks{\syst_basics_allocate\c_syst_last_allocated_marks\marks\chardef\c_syst_max_allocated_register}
417
418%D Attributes are something very \LUATEX. In \CONTEXT\ you are not supposed to use
419%D the attributes directly but always allocate then first. For instance attribute~0
420%D is reserved for special purposes (this might change). Attributes in the range
421%D 128-1023 are private and should not be touched.
422
423\immutable\integerdef\attributeunsetvalue\c_syst_min_counter_value % used to be \minusone
424
425% \permanent\protected\def\newattribute{\syst_basics_allocate\c_syst_last_allocated_attribute\attribute\attributedef\c_syst_max_allocated_register}
426
427% %D Not used by \CONTEXT\ but for instance \PICTEX\ needs it. It's a trick to force
428% %D strings instead of tokens that take more memory.
429%
430% \permanent\protected\def\newhelp#1#2{\newtoks#1#1\expandafter{\detokenize{#2}}}
431
432%D \macros
433%D   {scratchcounter,
434%D    scratchdimen,scratchskip,scratchmuskip,
435%D    scratchbox,
436%D    scratchtoks}
437%D
438%D We now define a few scratch registers, so that successive loads at least have
439%D some available. The private ones are used in cases where we don't want to
440%D intrude on normal scratch ones.
441
442\newcount \scratchcounter \newcount \globalscratchcounter \newcount \privatescratchcounter
443\newdimen \scratchdimen   \newdimen \globalscratchdimen   \newdimen \privatescratchdimen
444\newskip  \scratchskip    \newskip  \globalscratchskip    \newskip  \privatescratchskip
445\newmuskip\scratchmuskip  \newmuskip\globalscratchmuskip  \newmuskip\privatescratchmuskip
446\newtoks  \scratchtoks    \newtoks  \globalscratchtoks    \newtoks  \privatescratchtoks
447\newbox   \scratchbox     \newbox   \globalscratchbox     \newbox   \privatescratchbox
448
449\newcount \scratchcounterone \newcount \scratchcountertwo \newcount \scratchcounterthree
450\newdimen   \scratchdimenone \newdimen   \scratchdimentwo \newdimen   \scratchdimenthree
451\newskip     \scratchskipone \newskip     \scratchskiptwo \newskip     \scratchskipthree
452\newmuskip \scratchmuskipone \newmuskip \scratchmuskiptwo \newmuskip \scratchmuskipthree
453\newtoks     \scratchtoksone \newtoks     \scratchtokstwo \newtoks     \scratchtoksthree
454\newbox       \scratchboxone \newbox       \scratchboxtwo \newbox       \scratchboxthree
455
456\newcount \scratchcounterfour \newcount \scratchcounterfive \newcount \scratchcountersix
457\newdimen   \scratchdimenfour \newdimen   \scratchdimenfive \newdimen   \scratchdimensix
458\newskip     \scratchskipfour \newskip     \scratchskipfive \newskip     \scratchskipsix
459\newmuskip \scratchmuskipfour \newmuskip \scratchmuskipfive \newmuskip \scratchmuskipsix
460\newtoks     \scratchtoksfour \newtoks     \scratchtoksfive \newtoks     \scratchtokssix
461\newbox       \scratchboxfour \newbox       \scratchboxfive \newbox       \scratchboxsix
462
463\newcount\globalscratchcounterone
464\newcount\globalscratchcountertwo
465\newcount\globalscratchcounterthree
466
467\mutable\let\scratchmacro   \relax
468\mutable\let\scratchmacroone\relax
469\mutable\let\scratchmacrotwo\relax
470
471%D \macros
472%D   {tempstring, scratchstring*}
473
474\mutable\let\tempstring      \empty
475
476\mutable\let\scratchstring   \empty
477\mutable\let\scratchstringone\empty
478\mutable\let\scratchstringtwo\empty
479
480%D \macros
481%D   {scratchwidth, scratchheight, scratchdepth, scratchoffset, scratchdistance}
482%D
483%D A few more scratch dimensions:
484
485\newdimen\scratchwidth
486\newdimen\scratchheight
487\newdimen\scratchdepth
488\newdimen\scratchtotal
489
490\newdimen\scratchoffset
491\newdimen\scratchleftoffset
492\newdimen\scratchrightoffset
493\newdimen\scratchtopoffset
494\newdimen\scratchbottomoffset
495
496\newdimen\scratchdistance
497
498\newdimen\scratchhsize
499\newdimen\scratchvsize
500
501\newdimen\scratchxoffset
502\newdimen\scratchyoffset
503\newdimen\scratchhoffset
504\newdimen\scratchvoffset
505
506\newdimen\scratchxposition
507\newdimen\scratchyposition
508
509\newcount\scratchnx
510\newcount\scratchny
511
512\newcount\scratchmx
513\newcount\scratchmy
514
515\newcount\scratchmin
516\newcount\scratchmax
517
518\newcount\scratchunicode
519
520\newdimen\scratchleftskip
521\newdimen\scratchrightskip
522\newdimen\scratchtopskip
523\newdimen\scratchbottomskip
524
525%D More allocations:
526
527% \newcount \maxcount      \immutable\maxcount      2147483647
528
529% \newdimen \zeropoint     \immutable\zeropoint     0pt
530% \newdimen \onepoint      \immutable\onepoint      1pt
531% \newdimen \halfapoint    \immutable\halfapoint    0.5pt
532% \newdimen \maxdimen      \immutable\maxdimen      16383.99999pt % 1073741823sp
533% \newdimen \onebasepoint  \immutable\onebasepoint  1bp
534% \newdimen \scaledpoint   \immutable\scaledpoint   1sp
535% \newdimen \thousandpoint \immutable\thousandpoint 1000pt
536
537\immutable\integerdef    \maxcount 2147483647
538
539\immutable\dimensiondef  \zeropoint     0pt
540\immutable\dimensiondef  \onepoint      1pt
541\immutable\dimensiondef  \halfapoint    0.5pt
542\immutable\dimensiondef  \maxdimen      16383.99999pt % 1073741823sp
543\immutable\dimensiondef  \onebasepoint  1bp
544\immutable\dimensiondef  \scaledpoint   1sp
545\immutable\dimensiondef  \thousandpoint 1000pt
546
547\immutable\gluespecdef   \zeroskip      0pt plus 0pt minus 0pt
548
549\immutable\mugluespecdef \zeromuskip    0mu
550\immutable\mugluespecdef \onemuskip     1mu
551\immutable\mugluespecdef \muquad        18mu
552
553\aliased\let\points   \onepoint
554\aliased\let\halfpoint\halfapoint
555
556\newtoks \emptytoks
557
558%D And even more: (todo: countdefs 60+). Watch out, we need a count for a negative constant
559%D but can use chardefs for positive ones. Actually, because we have plenty of counters we
560%D can also use counts for those.
561
562%immutable\integerdef\minusone              -1
563\immutable\integerdef\minustwo              -2
564%immutable\integerdef\zerocount              0
565%immutable\integerdef\plusone                1
566\immutable\integerdef\plustwo                2
567\immutable\integerdef\plusthree              3
568\immutable\integerdef\plusfour               4
569\immutable\integerdef\plusfive               5
570\immutable\integerdef\plussix                6
571\immutable\integerdef\plusseven              7
572\immutable\integerdef\pluseight              8
573\immutable\integerdef\plusnine               9
574\immutable\integerdef\plusten               10
575\immutable\integerdef\pluseleven            11
576\immutable\integerdef\plustwelve            12
577\immutable\integerdef\plussixteen           16
578\immutable\integerdef\plusfifty             50
579\immutable\integerdef\plushundred          100
580\immutable\integerdef\plusonehundred       100
581\immutable\integerdef\plustwohundred       200
582\immutable\integerdef\plusfivehundred      500
583\immutable\integerdef\pluscxxvii           127
584\immutable\integerdef\pluscxxviii          128
585\immutable\integerdef\pluscclv             255
586\immutable\integerdef\pluscclvi            256
587\immutable\integerdef\plusthousand        1000
588\immutable\integerdef\plustenthousand    10000
589\immutable\integerdef\plustwentythousand 20000
590\immutable\integerdef\medcard            32768
591\immutable\integerdef\maxcard            65536 % pdftex has less mathchars
592\immutable\integerdef\maxcardminusone    65535
593
594%D \macros
595%D   {doubleexpandafter,tripleexpandafter,expanded,startexpanded}
596%D
597%D A few handy shortcuts
598
599\permanent\let\singleexpandafter \expandafter
600\permanent\def\doubleexpandafter{\expandafter\expandafter\expandafter}
601\permanent\def\tripleexpandafter{\expandafter\doubleexpandafter\expandafter}
602
603%D We prefer the more readable variant than in plain \TEX. User should only
604%D use \type {\emptybox}:
605
606\newbox\voidbox  % public
607
608\permanent\def\unvoidbox{\unhbox\voidbox}
609\permanent\def\emptybox {\box   \voidbox}  % used in initializations so no attributes
610\permanent\def\emptyvbox{\normalvpack{}}   % no copy as we need to set attributes
611\permanent\def\emptyhbox{\normalhpack{}}   % no copy as we need to set attributes
612
613\aliased\let\leavevmode\unvoidbox % we prefer to use \dontleavehmode
614
615%D \macros
616%D   {dontcomplain,lessboxtracing}
617%D
618%D We need this one soon. The second one shows less box details (because that actually
619%D slows down noticeably due to the more extensive details in node content.
620
621% With \showboxdepth\maxcount and \showboxbreadth\maxcount the second one is way slower:
622%
623% \testfeatureonce{10}{\setbox\scratchbox\vbox        {\samplefile{tufte}}} \elapsedtime\par
624% \testfeatureonce{10}{\setbox\scratchbox\vbox to 10cm{\samplefile{tufte}}} \elapsedtime\par
625
626\permanent\protected\def\dontcomplain
627  {\hbadness\plustenthousand
628   \vbadness\plustenthousand
629   \hfuzz   \maxdimen
630   \vfuzz   \maxdimen}
631
632\permanent\protected\def\lessboxtracing
633  {\showboxdepth  \plusthree
634   \showboxbreadth\plusfive}
635
636%D Some expected plain variants follow. We don't reuse registers because we don't
637%D want clashes. These will go away and packages that need them have to define them.
638
639\aliased\let\p@     \onepoint
640\aliased\let\m@ne   \minusone
641\aliased\let\z@     \zeropoint
642\aliased\let\@ne    \plusone
643\aliased\let\tw@    \plustwo
644\aliased\let\thr@@  \plusthree
645\aliased\let\sixt@@n\plussixteen
646\aliased\let\@cclv  \pluscclv
647\aliased\let\@cclvi \pluscclvi
648\aliased\let\voidb@x\voidbox
649
650\newtoks    \toks@  %scratchtoks
651
652%D We define \type {\newif} a la plain \TEX, but will redefine it later. As Knuth
653%D says:
654%D
655%D \startnarrower
656%D And here's a different sort of allocation: for example,
657%D
658%D \starttyping
659%D \newif\iffoo
660%D \stoptyping
661%D
662%D creates \type {\footrue}, \type {\foofalse} to go with \type {\iffoo}.
663%D \stopnarrower
664
665% \protected\def\newif#1%
666%   {\count@\escapechar
667%    \escapechar\minusone
668%    \expandafter\expandafter\expandafter\def\new_if #1{true}{\let#1\iftrue }%
669%    \expandafter\expandafter\expandafter\def\new_if#1{false}{\let#1\iffalse}%
670%    \new_if#1{false}% the condition starts out false
671%    \escapechar\count@}
672%
673% \def\new_if#1#2%
674%   {\csname\expandafter\if@\string#1#2\endcsname}
675%
676% \bgroup % `if' is required
677%   \uccode`1=`i \uccode`2=`f \uppercase{\gdef\if@12{}}
678% \egroup
679
680% We use \csstring so there is no need to push/pop escapechar.
681% We use different names so that we get a better error message.
682%
683% \protected\def\newif#1%
684%   {\let\new_if_saved\newif
685%    \let\newif\new_if_check
686%    \expandafter\expandafter\expandafter\def\new_if_cs #1{true}{\let#1\iftrue }%
687%    \expandafter\expandafter\expandafter\def\new_if_cs#1{false}{\let#1\iffalse}%
688%    \new_if_cs#1{false}%
689%    \let\newif\new_if_saved}
690%
691% \protected\def\new_if_cs#1#2%
692%   {\csname\expandafter\newif\csstring#1#2\endcsname}
693%
694% We wrap all into one macro (the frozen stuff adds 10% runtime):
695
696\permanent\protected\def\newif#1%
697  {\permanent\protected\gdefcsname\expandafter\new_if_check\csstring#1true\endcsname {\overloaded\frozen\let#1\iftrue }%
698   \permanent\protected\gdefcsname\expandafter\new_if_check\csstring#1false\endcsname{\overloaded\frozen\let#1\iffalse}%
699   \csname\expandafter\new_if_check\csstring#1false\endcsname}
700
701\normalexpanded{\gdef\noexpand\new_if_check\string i\string f{}}
702
703%D Let's test this one:
704
705\newif\ifdone
706\newif\iffound
707
708\newif\ifscratchcondition
709\newif\ifscratchconditionone
710\newif\ifscratchconditiontwo
711
712%D This used to be a protected definition with a \type {dimexpr} but now we have
713%D it natively (less tracking noise).
714
715\aliased\let\htdp\boxtotal
716
717%D A few shortcuts (gone by now):
718
719% \permanent\protected\def\udef {\protected\def }
720% \permanent\protected\def\ugdef{\protected\gdef}
721% \permanent\protected\def\uedef{\protected\edef}
722% \permanent\protected\def\uxdef{\protected\xdef}
723
724% For now:
725
726\permanent\protected\def\defUmathtopaccent  #1#2#3#4{\global\immutable\protected\def#1{\Umathaccent     "#2 "#3 "#4 }}
727\permanent\protected\def\defUmathbotaccent  #1#2#3#4{\global\immutable\protected\def#1{\Umathbotaccent  "#2 "#3 "#4 }}
728\permanent\protected\def\defUdelimiterover  #1#2#3#4{\global\immutable\protected\def#1{\Udelimiterover  "#2 "#3     }}
729\permanent\protected\def\defUdelimiterunder #1#2#3#4{\global\immutable\protected\def#1{\Udelimiterunder "#2 "#3     }}
730\permanent\protected\def\defUdelimiter      #1#2#3#4{\global\immutable\protected\def#1{\Udelimiter      "#2 "#3 "#4 }}
731\permanent\protected\def\defUradical          #1#2#3{\global\immutable\protected\def#1{\Uradical        "#2 "#3     }}
732\permanent\protected\def\defUroot             #1#2#3{\global\immutable\protected\def#1{\Uroot           "#2 "#3     }}
733\permanent\protected\def\defUmathchar       #1#2#3#4{\global\immutable\Umathchardef #1                  "#2 "#3 "#4 }
734
735%D For a while we keep the following, as systems like tikz need it. Best not use
736%D that one \CONTEXT. It will probably move to the tikz loader.
737
738\aliased\let\active\activecatcode
739
740%D Constants to be used with \type {\interactionmode}.
741
742\immutable\integerdef\batchmodecode     \zerocount
743\immutable\integerdef\nonstopmodecode   \plusone
744\immutable\integerdef\scrollmodecode    \plustwo
745\immutable\integerdef\errorstopmodecode \plusthree
746
747%D Of course we want even bigger log files, so we copied this from the \ETEX\
748%D source files.
749%D
750%D When watching such logs, beware of nasty side effects of \type {\scantokens},
751%D as in:
752%D
753%D \starttyping
754%D \bgroup
755%D   \lccode`a=12\lowercase{\xdef\whatever{a}}\egroup
756%D   \def\whatever{test \whatever test}
757%D   \scantokens\expandafter{\whatever}
758%D \egroup
759%D \stoptyping
760%D
761%D In \LUATEX\ we have ways around this.
762
763% no longer \errorstopmode cf. plain tex 3.141592653
764
765\permanent\protected\def\tracingall
766  {\tracingonline    \plusone
767   \tracingcommands  \plusfour  % > 3: show mode change independent
768   \tracingmacros    \plusthree % > 2: obey \untraced
769   \tracingoutput    \plusone
770   \tracingpages     \plusone
771   \tracingparagraphs\plustwo
772   \tracingrestores  \plusone
773   \tracinggroups    \plusone
774   \tracingifs       \plusone
775   \tracingnesting   \plusthree
776   \tracingassigns   \plustwo
777   \tracingmath      \plusone
778   \tracingalignments\plustwo   % > 1: also show preamble
779   \showboxbreadth   \maxcount
780   \showboxdepth     \maxcount}
781
782\permanent\protected\def\tracingless
783  {\tracingall
784   \tracingoutput    \zerocount
785   \showboxbreadth   \maxcount
786   \showboxdepth     \plusten}
787
788\permanent\protected\def\loggingall
789  {\tracingall
790   \tracingonline    \zerocount}
791
792\permanent\protected\def\tracingnone
793  {\showboxdepth     \maxcount % \plusthree
794   \showboxbreadth   \plusten  % \plusfive
795   \tracingalignments\zerocount
796   \tracingmath      \zerocount
797   \tracingassigns   \zerocount
798   \tracingnesting   \zerocount
799   \tracingifs       \zerocount
800   \tracinggroups    \zerocount
801   \tracingrestores  \zerocount
802   \tracingparagraphs\zerocount
803   \tracingpages     \zerocount
804   \tracingoutput    \zerocount
805   \tracingmacros    \zerocount
806   \tracingcommands  \zerocount
807   \tracingonline    \zerocount}
808
809%D When we want to see a box we can as well show all of it.
810
811\showboxdepth  \maxcount
812\showboxbreadth\maxcount
813
814%D Just for tracing purposes we set:
815
816\tracingstats\plusone
817
818%D This is only used when we load \CONTEXT. Nesting is not supported here.
819
820\permanent\def\pushoverloadmode
821  {\enforced\permanent\protected\edef\popoverloadmode{\overloadmode\the\overloadmode\enforced\let\popoverloadmode\relax}%
822   \overloadmode\zerocount}
823
824\aliased\let\popoverloadmode\relax
825
826\let\pushrunstate\relax % will be defined later
827\let\poprunstate \relax % will be defined later
828
829\newtoks\everydump
830
831\pushoverloadmode
832
833\permanent\protected\def\dump
834  {\the\everydump
835   \global\everydump\emptytoks
836   \pushoverloadmode
837   \enforced\glet\dump\relax
838   \popoverloadmode
839   %let\pushoverloadmode\relax
840   %let\popoverloadmode\relax
841 % \tracingall
842   \normaldump}
843
844\permanent\protected\def\input
845  {\normalinput}
846
847\popoverloadmode
848
849%D \macros
850%D   {newconditional,
851%D    settrue, setfalse,
852%D    ifconditional,then}
853%D
854%D \TEX's lacks boolean variables, although the \PLAIN\ format implements \type
855%D {\newif}. The main disadvantage of this scheme is that it takes three hash table
856%D entries. A more memory saving alternative is presented here. A conditional is
857%D defined by:
858%D
859%D \starttyping
860%D \newconditional\doublesided
861%D \setfalse
862%D \stoptyping
863%D Setting a conditional is done by \type{\settrue} and
864%D \type{\setfalse}:
865%D
866%D \starttyping
867%D \settrue\doublesided
868%D \setfalse
869%D \stoptyping
870%D while testing is accomplished by:
871%D
872%D \starttyping
873%D \ifconditional\doublesided  ... \else ... \fi
874%D \setfalse
875%D \stoptyping
876%D We cannot use the simple scheme:
877%D
878%D \starttyping
879%D \def\settrue #1{\let#1=\iftrue}
880%D \def\setfalse#1{\let#1=\iffalse}
881%D \stoptyping
882%D
883%D Such an implementation gives problems with nested conditionals. The next
884%D implementation is about as fast and just as straightforward:
885
886\aliased\let\conditionalfalse\plusone    % maybe we will have a dedicated count/chardef
887\aliased\let\conditionaltrue \zerocount  % maybe we will have a dedicated count/chardef
888
889% \permanent\protected\def\settrue #1{\integerdef#1\conditionaltrue }
890% \permanent\protected\def\setfalse#1{\integerdef#1\conditionalfalse}
891%
892% \permanent\protected\def\settruevalue #1{\expandafter\integerdef\csname#1\endcsname\conditionaltrue }
893% \permanent\protected\def\setfalsevalue#1{\expandafter\integerdef\csname#1\endcsname\conditionalfalse}
894
895\permanent\protected\def\settrue       #1{\enforced\let#1\conditionaltrue }
896\permanent\protected\def\setfalse      #1{\enforced\let#1\conditionalfalse}
897\permanent\protected\def\setconditional#1{\enforced\let#1}
898
899\permanent\protected\def\settruevalue #1{\enforced\letcsname#1\endcsname\conditionaltrue }
900\permanent\protected\def\setfalsevalue#1{\enforced\letcsname#1\endcsname\conditionalfalse}
901
902
903\aliased\let\newconditional\setfalse
904\aliased\let\ifconditional \ifcase
905
906\permanent\protected\lettonothing\then % so that we can say: \ifnum1>2\then -)
907
908\permanent\def\truecondition {\iftrue}
909\permanent\def\falsecondition{\iffalse}
910
911% This one has to be unprotected otherwise we get a files-ends-to-soon but it's ok
912% as conditions expand anyway.
913
914\permanent\def\quitcondition{\orelse\iffalse}
915
916%D \macros
917%D   {newmacro,setnewmacro,newfraction}
918%D
919%D Let's be complete and also introduce some definers. These are not mandate
920%D but handy for grepping. We make these mutable because they are \type
921%D {\let} (the only advantage is that we often give them names with some
922%D prefix.
923
924\permanent\protected\def\newmacro   #1{\mutable\let#1\empty}
925\permanent\protected\def\setnewmacro#1{\mutable\let#1}
926
927\permanent\protected\def\newfraction#1{\mutable\let#1\!!plusone}
928
929\def\!!zerocount{0} % later redefined
930\def\!!plusone  {1} % later redefined
931
932%D It would be handy to have a primitive \type {\unless\ifcase} because then we
933%D could use nicer values. Anyhow, this conditional code used to be in the \type
934%D {syst-aux} module but is now promoted to here.
935
936%D \macros
937%D   {ifzeropt}
938%D
939%D The next macro is both cosmetic and byte saving. It is pretty \type
940%D {\if}||safe too. It can be used in cases like:
941%D
942%D \starttyping
943%D \ifzeropt \somedimen ... \else ... \fi
944%D \stoptyping
945
946\aliased\let\ifzeropt\ifcase
947
948% these token list helpers might move to syst-aux.mkiv
949%
950% we assume a \cs. not toks0 or so
951%
952% \protected\def\appendtotoks #1#{\def\temp{#1}\afterassignment\doappendtotoks \scratchtoks=}
953% \protected\def\prependtotoks#1#{\def\temp{#1}\afterassignment\doprependtotoks\scratchtoks=}
954
955\newtoks\t_syst_toks_temp \let\m_syst_toks_temp\t_syst_toks_temp
956
957\permanent\protected\def\appendtotoks #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_append_l \t_syst_toks_temp=}
958\permanent\protected\def\prependtotoks#1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_prepend_l\t_syst_toks_temp=}
959
960% \def\syst_toks_append {\m_syst_toks_temp\expandafter\expandafter\expandafter{\expandafter\the\expandafter\m_syst_toks_temp\the\t_syst_toks_temp}}}
961% \def\syst_toks_prepend{\m_syst_toks_temp\expandafter\expandafter\expandafter{\expandafter\the\expandafter\t_syst_toks_temp\the\m_syst_toks_temp}}}
962
963\permanent\protected\def\globalappendtotoks #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_append_g \t_syst_toks_temp=}
964\permanent\protected\def\globalprependtotoks#1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_prepend_g\t_syst_toks_temp=}
965
966\def\syst_toks_append_l {\normalexpanded{\m_syst_toks_temp{\the\m_syst_toks_temp\the\t_syst_toks_temp}}}
967\def\syst_toks_prepend_l{\normalexpanded{\m_syst_toks_temp{\the\t_syst_toks_temp\the\m_syst_toks_temp}}}
968
969\def\syst_toks_append_g {\global\syst_toks_append_l }
970\def\syst_toks_prepend_g{\global\syst_toks_prepend_l}
971
972\permanent\protected\def\addtotoks      #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_add_l\let\next}
973\permanent\protected\def\globaladdtotoks#1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_add_g\let\next}
974
975\def\syst_toks_add_l{\m_syst_toks_temp\expandafter\bgroup\the\m_syst_toks_temp}
976\def\syst_toks_add_g{\global\syst_toks_add_l}
977
978\permanent\protected\def\addtotokscs      #1#2{#1{\the#1#2}}        % saves a few bytes
979\permanent\protected\def\globaladdtotokscs#1#2{\global#1{\the#1#2}} % saves a few bytes
980
981%D \macros
982%D   {begcsname}
983%D
984%D Handy for \ETEX-only usage (avoids making \type {\relax}'s:
985
986% \def\begcsname#1\endcsname{\ifcsname#1\endcsname\csname#1\endcsname\fi}
987
988\aliased\let\begcsname\begincsname
989
990%D Now come a few macros that might be needed in successive loading. We redefine the
991%D \type {\par} primitive pretty soon so that we get the equivalents right.
992
993% too tricky: \par is use more often than a par starts so we have too much change
994% that we get assymetrical behaviour
995%
996% \newtoks\everyendpar
997%
998% \protected\def\endpar{\the\everyendpar\normalpar}
999% \protected\def\par   {\endpar}
1000%
1001% \protected\def\reseteverypar
1002%   {\everypar   \emptytoks
1003%    \everyendpar\emptytoks}
1004
1005\permanent\protected\def\reseteverypar
1006  {\everypar\emptytoks}
1007
1008\aliased\let\endgraf\par % plain, a kind of funny name
1009%aliased\let\endline\cr  % plain, not needed and probably also confusing
1010
1011\permanent\protected\def\null{\hpack{}}
1012
1013%D The following two might be overloaded later on but some modules need then
1014%D earlier. These functionality is reflected in the name and will not change.
1015
1016% \bgroup
1017%     \catcode`\^^M=\activecatcode%
1018%     \gdef\obeylines{\catcode`\^^M\activecatcode \let^^M\par}%
1019%     \glet^^M\par%
1020% \egroup
1021%
1022% \bgroup
1023%     \gdef\obeyspaces{\catcode`\ \activecatcode}%
1024%     \obeyspaces\glet =\space%
1025% \egroup
1026
1027\def\obeylines {\catcode\endoflineasciicode\activecatcode\letcharcode\endoflineasciicode\par  } % check if these can be \permanent
1028\def\obeyspaces{\catcode\spaceasciicode    \activecatcode\letcharcode\spaceasciicode    \space} % check if these can be \permanent
1029
1030% %D A constant:
1031%
1032% \let\endoflinetoken=^^M
1033
1034%D Also needed might be a simple loop structure and we borrow plain \TEX's one
1035%D as it is often expected to be present and it is about the fastest you can
1036%D get. Beware: this macro does not support nested loops. We use a namespace
1037%D prefix \type {@@pln}.
1038
1039\let\syst_plain_body\relax
1040
1041\permanent\def\loop#1\repeat{\def\syst_plain_body{#1}\syst_plain_iterate} % might go
1042
1043%D The following makes \type {\loop} \unknown\ \type {\if} \unknown\ \type
1044%D {\repeat} skippable (clever trick):
1045
1046\permanent\let\repeat\fi % so both \loop and \repeat are reserved words!
1047
1048%D The original (no \type {syst_plain_} there):
1049%D
1050%D \starttyping
1051%D \def\syst_plain_iterate
1052%D   {\syst_plain_body
1053%D    \let\next\syst_plain_iterate
1054%D    \else
1055%D      \let\next\relax
1056%D    \fi
1057%D    \next}
1058%D \stoptyping
1059%D
1060%D A more efficient alternative:
1061%D
1062%D \starttyping
1063%D \def\syst_plain_iterate
1064%D   {\syst_plain_body
1065%D    \expandafter\syst_plain_iterate
1066%D    \else\expandafter\relax\fi}
1067%D \stoptyping
1068%D
1069%D An even more efficient one:
1070
1071\def\syst_plain_iterate
1072  {\syst_plain_body
1073   \expandafter\syst_plain_iterate\else\fi}
1074
1075%D We don't define a real output routine yet but at least get rid of pages:
1076
1077\output{\shipout\box\normalpagebox}
1078
1079%D Although we don't add pagenumbers yet we alias the default register used
1080%D for counting pages:
1081
1082\permanent\countdef\pageno\zerocount \pageno\plusone % first page is number 1
1083
1084%D Beside the raw counter \type {\pageno} the \type {\folio} macro provides
1085%D the value.
1086
1087\permanent\def\folio{\the\pageno} % kind of expected and therefore reserved
1088
1089%D The following registers are kind of standard and (for the moment) we define
1090%D them here. This might change.
1091
1092\newskip \bigskipamount    \bigskipamount   = 12pt plus 4pt minus 4pt
1093\newskip \medskipamount    \medskipamount   =  6pt plus 2pt minus 2pt
1094\newskip \smallskipamount  \smallskipamount =  3pt plus 1pt minus 1pt
1095
1096\baselineskip  = 12pt
1097\lineskip      =  1pt
1098\lineskiplimit =  0pt
1099
1100%D Sometimes kerns make more sense than glue but we need to be in the right mode:
1101
1102\permanent\protected\def\vkern   {\ifhmode\par           \fi\kern}
1103\permanent\protected\def\hkern   {\ifvmode\dontleavehmode\fi\kern}
1104
1105\permanent\protected\def\vpenalty{\ifhmode\par           \fi\penalty}
1106\permanent\protected\def\hpenalty{\ifvmode\dontleavehmode\fi\penalty}
1107
1108%D Again a few kind-of-extensions the core. These come from plain \TEX\ but are
1109%D probably not used in \CONTEXT.
1110
1111\newskip \hideskip   \hideskip  = -1000pt plus 1fill
1112\newskip \centering  \centering = 0pt plus 1000pt minus 1000pt
1113
1114\permanent\def\hidewidth % for alignment entries that can stick out
1115  {\hskip\hideskip}
1116
1117\permanent\def\ialign % initialized \halign
1118  {\everycr\emptytoks
1119   \tabskip\zeroskip
1120   \halign}
1121
1122\newcount \mscount
1123
1124\permanent\def\spanomit{\span\omit} % bypass error message
1125
1126\permanent\def\multispan#1%
1127  {\omit
1128   \mscount#1\relax
1129   \loop
1130     \ifnum\mscount>\plusone
1131       \spanomit \advance\mscount\minusone
1132   \repeat}
1133
1134\let\nopdfcompression        \relax % later
1135\let\onlypdfobjectcompression\relax % later
1136\let\maximumpdfcompression   \relax % later
1137\let\normalpdfcompression    \relax % later
1138
1139%D Basic status stuff.
1140
1141% \newif\ifproductionrun % already defined
1142
1143%D For those who expect this \unknown
1144
1145\ifdefined\fmtname    \else \immutable\def\fmtname   {ConTeXt Initial TeX} \fi
1146\ifdefined\fmtversion \else \immutable\def\fmtversion{3.1415926}           \fi
1147
1148%D A few bonus macros:
1149
1150%permanent\def\modulonumber#1#2{\the\numexpr#2-((((#2+(#1/2))/#1)-1)*#1)\relax}
1151\permanent\def\modulonumber#1#2{\the\numexpr#2-(#2:#1)*#1\relax}
1152\permanent\def\dividenumber#1#2{\the\numexpr(#2-(#1/2))/#1\relax}
1153
1154%D These could be set at the \LUA\ end instead:
1155
1156\immutable\edef\texenginename         {\directlua{tex.print(LUATEXENGINE)}}
1157\immutable\edef\texengineversion      {\directlua{tex.print(LUATEXVERSION)}}
1158\immutable\edef\texenginefunctionality{\directlua{tex.print(LUATEXFUNCTIONALITY)}}
1159
1160%D We have no reason not to enable this:
1161
1162\savingvdiscards\plusone
1163
1164%D We only can set this one via directives (system.synctex) and we only support
1165%D the context variant. This will go away completely.
1166
1167\newcount\synctex % \let\normalsynctex\synctex
1168
1169%D We get rid of the funny \TEX\ offset defaults of one inch by setting them to zero.
1170
1171% \voffset\zeropoint \overloaded\let\voffset\relax \newdimen\voffset \let\normalvoffset\voffset
1172% \hoffset\zeropoint \overloaded\let\hoffset\relax \newdimen\hoffset \let\normalhoffset\hoffset
1173
1174\matheqnogapstep\zerocount % for now
1175
1176%D Now we define a few helpers that we need in a very early stage. We have no
1177%D message system yet but redundant definitions are fatal anyway.
1178
1179\newcount\c_syst_helpers_n_of_namespaces \c_syst_helpers_n_of_namespaces\pluseight % 1-8 reserved for catcodes
1180
1181\def\v_interfaces_prefix_template_system{\number    \c_syst_helpers_n_of_namespaces>>}
1182%def\v_interfaces_prefix_template_system{\characters\c_syst_helpers_n_of_namespaces>>} % no \characters yet
1183
1184\protected\def\installsystemnamespace#1% will be redefined
1185  {\ifcsname ??#1\endcsname
1186     \immediate\write\statuswrite{fatal error: duplicate system namespace '#1'}%
1187   \else
1188     \global\advance\c_syst_helpers_n_of_namespaces\plusone
1189     \immutable\edefcsname ??#1\endcsname{\v_interfaces_prefix_template_system}%
1190   \fi}
1191
1192%D It makes more sense to have these here:
1193
1194\aliased\let\normalsuperscript  \Usuperscript
1195\aliased\let\normalsubscript    \Usubscript
1196\aliased\let\normalnosuperscript\Unosuperscript
1197\aliased\let\normalnosubscript  \Unosubscript
1198\aliased\let\normalstartimath   \Ustartmath
1199\aliased\let\normalstopimath    \Ustopmath
1200\aliased\let\normalstartdmath   \Ustartdisplaymath
1201\aliased\let\normalstopdmath    \Ustopdisplaymath
1202
1203%D Also better here:
1204
1205\immutable\def\wildcardsymbol{*}
1206
1207%D For a while we will keep these useless numbers as for instance tikz checks for them:
1208
1209\immutable\integerdef\eTeXversion   2
1210\immutable\def       \eTeXrevision {2}
1211
1212%D Experiment:
1213
1214\glyphoptions"80 % replaces \glyphdimensionsmode=1
1215
1216%D Just in case users use this (or some styles still have it):
1217
1218\aliased\let\immediateassignment\immediate
1219\aliased\let\immediateassigned  \localcontrolled
1220
1221%D Needed (maybe no longer the multiple next ones):
1222
1223\mutable\let\nexttoken   \relax
1224\mutable\let\next        \relax
1225\mutable\let\nextnext    \relax
1226\mutable\let\nextnextnext\relax
1227
1228%D For now here: will get a proper solution
1229
1230% \pushoverloadmode
1231% \mutable\let\par\par
1232% \popoverloadmode
1233
1234%D Often used as cs key:
1235
1236\mutable\let\on  \relax % \empty
1237\mutable\let\of  \relax % \empty
1238\mutable\let\and \relax % \empty
1239\mutable\let\with\relax % \empty
1240\mutable\let\nop \empty % used often
1241\mutable\let\yes \empty % used often
1242
1243%D Another experiment:
1244
1245\def\syst_suggested_alias#1#2% expandable
1246  {\beginlocalcontrol
1247   \writestatus{suggestion}{use \string#2 instead of \string#1}%
1248   \enforced\glet#1#2%
1249   \endlocalcontrol
1250   #2}
1251
1252\permanent\protected\def\suggestedalias#1#2%
1253  {\ifflags#2\permanent\permanent\fi % the rest is taken with the copy
1254   \def#1{\syst_suggested_alias#1#2}}
1255
1256% A few traditional allocations (these might go too):
1257
1258% \countdef \count@   255 % hm, used in \newif .. todo: replace it there
1259% \dimendef \dimen@     0
1260% \dimendef \dimen@i    1 % global only
1261% \dimendef \dimen@ii   2
1262
1263\suggestedalias \count@   \scratchcounter
1264\suggestedalias \dimen@   \scratchdimen
1265\suggestedalias \dimen@i  \scratchdimenone
1266\suggestedalias \dimen@ii \scratchdimentwo
1267
1268%D The next feature is experimental and could go into another system module, but it
1269%D feels more natural to have it here.
1270%D
1271%D \starttyping
1272%D \begingroup
1273%D   \newlocaldimen\mydimena % \mydimena1\onepoint
1274%D   \newlocaldimen\mydimenb % \mydimenb2\onepoint
1275%D   \begingroup
1276%D     \newlocaldimen\mydimena % \mydimena3\onepoint
1277%D     \newlocaldimen\mydimenb % \mydimenb4\onepoint
1278%D     \newlocaldimen\mydimenc % \mydimenc5\onepoint
1279%D   \endgroup
1280%D \endgroup
1281%D
1282%D \begingroup
1283%D   \setlocaldimen\mydimena 1\onepoint
1284%D   \setlocaldimen\mydimenb 2\onepoint
1285%D   \begingrou
1286%D     \setlocaldimen\mydimena 3\onepoint
1287%D     \setlocaldimen\mydimenb 4\onepoint
1288%D     \setlocaldimen\mydimenc 5\onepoint
1289%D   \endgroup
1290%D \endgroup
1291%D \stoptyping
1292%D
1293%D There is a little extra overhead in the overload protection but not that much.
1294
1295\permanent\countdef\c_syst_local_count  = 221 \c_syst_local_count  = \c_syst_max_allocated_register
1296\permanent\countdef\c_syst_local_dimen  = 222 \c_syst_local_dimen  = \c_syst_local_count
1297\permanent\countdef\c_syst_local_skip   = 223 \c_syst_local_skip   = \c_syst_local_count
1298\permanent\countdef\c_syst_local_muskip = 224 \c_syst_local_muskip = \c_syst_local_count
1299\permanent\countdef\c_syst_local_box    = 225 \c_syst_local_box    = \c_syst_local_count
1300\permanent\countdef\c_syst_local_toks   = 226 \c_syst_local_toks   = \c_syst_local_count
1301\permanent\countdef\c_syst_local_read   = 227 \c_syst_local_read   = \c_syst_local_count
1302\permanent\countdef\c_syst_local_write  = 228 \c_syst_local_write  = \c_syst_local_count
1303
1304\c_syst_max_allocated_register \numexpr\c_syst_max_allocated_register:2\relax % floor
1305
1306\permanent\protected\def\syst_local_overflow#1#2{\writestatus\m!system{no room for local #1 \string#2}\wait}
1307
1308\permanent\protected\def\setnewlocaldimen#1%
1309  {\ifnum\c_syst_local_dimen>\c_syst_max_allocated_register
1310     \advance\c_syst_local_dimen\minusone\dimendef#1\c_syst_local_dimen
1311   \else
1312     \syst_local_overflow_dimen#1%
1313   \fi
1314   #1}
1315
1316\permanent\protected\def\setnewlocalcount#1%
1317  {\ifnum\c_syst_local_count>\c_syst_max_allocated_register
1318     \advance\c_syst_local_count\minusone
1319     \overloaded\frozen\countdef#1\c_syst_local_count
1320   \else
1321     \syst_local_overflow_count#1%
1322   \fi
1323   #1}
1324
1325\permanent\protected\def\setnewlocalskip#1%
1326  {\ifnum\c_syst_local_skip>\c_syst_max_allocated_register
1327     \advance\c_syst_local_skip\minusone
1328     \overloaded\frozen\skipdef#1\c_syst_local_skip
1329   \else
1330     \syst_local_overflow_skip#1%
1331   \fi
1332   #1}
1333
1334\permanent\protected\def\setnewlocalmuskip#1%
1335  {\ifnum\c_syst_local_muskip>\c_syst_max_allocated_register
1336     \advance\c_syst_local_muskip\minusone
1337     \overloaded\frozen\muskipdef#1\c_syst_local_muskip
1338   \else
1339     \syst_local_overflow_muskip#1%
1340   \fi
1341   #1}
1342
1343\permanent\protected\def\setnewlocaltoks#1%
1344  {\ifnum\c_syst_local_toks>\c_syst_max_allocated_register
1345     \advance\c_syst_local_toks\minusone
1346     \overloaded\frozen\toksdef#1\c_syst_local_toks
1347   \else
1348     \syst_local_overflow_toks#1%
1349   \fi
1350   #1}
1351
1352\permanent\protected\def\setnewlocalbox#1%
1353  {\ifnum\c_syst_local_box>\c_syst_max_allocated_register
1354     \advance\c_syst_local_box\minusone
1355     \overloaded\frozen\integerdef#1\c_syst_local_box
1356   \else
1357     \syst_local_overflow_box#1%
1358   \fi
1359   \setbox#1}
1360
1361\permanent\protected\def\newlocalread#1% so no set
1362  {\ifnum\c_syst_local_read>\c_syst_max_allocated_register
1363     \advance\c_syst_local_read\minusone
1364     \overloaded\frozen\integerdef#1\c_syst_local_read
1365   \else
1366     \syst_local_overflow_read#1%
1367   \fi}
1368
1369\permanent\protected\def\newlocalwrite#1% so no set
1370  {\ifnum\c_syst_local_write>\c_syst_max_allocated_register
1371     \advance\c_syst_local_write\minusone
1372     \overloaded\frozen\integerdef#1\c_syst_local_write
1373   \else
1374     \syst_local_overflow_write#1%
1375   \fi}
1376
1377%D The error handlers:
1378
1379\permanent\protected\def\syst_local_overflow_count {\syst_local_overflow {count}}
1380\permanent\protected\def\syst_local_overflow_dimen {\syst_local_overflow {dimen}}
1381\permanent\protected\def\syst_local_overflow_skip  {\syst_local_overflow  {skip}}
1382\permanent\protected\def\syst_local_overflow_muskip{\syst_local_overflow{muskip}}
1383\permanent\protected\def\syst_local_overflow_toks  {\syst_local_overflow  {toks}}
1384\permanent\protected\def\syst_local_overflow_box   {\syst_local_overflow   {box}}
1385\permanent\protected\def\syst_local_overflow_read  {\syst_local_overflow  {read}}
1386\permanent\protected\def\syst_local_overflow_write {\syst_local_overflow {write}}
1387
1388%D We sacrifice a little performance but save some tokens by not defining these
1389%D completely:
1390
1391\permanent\protected\def\newlocalcount #1{\setnewlocalcount #1\zerocount }
1392\permanent\protected\def\newlocaldimen #1{\setnewlocaldimen #1\zeropoint }
1393\permanent\protected\def\newlocalskip  #1{\setnewlocalskip  #1\zeroskip  }
1394\permanent\protected\def\newlocalmuskip#1{\setnewlocalmuskip#1\zeromuskip}
1395\permanent\protected\def\newlocaltoks  #1{\setnewlocaltoks  #1\emptytoks }
1396\permanent\protected\def\newlocalbox   #1{\setnewlocalbox   #1\emptybox  }
1397
1398%D Let's be detailed: grouplevel:inputlevel:catcodetable (bits 1 2 4)
1399
1400\tracinglevels\plusthree
1401
1402\permanent\protected\def\tracingcatcodes{\tracinglevels\plusseven}
1403
1404%D We just report duplicate patterns being ignored:
1405
1406\tracinghyphenation\plusone
1407
1408%D Sometimes nicer:
1409
1410\let\ifexpression\ifnumexpression
1411
1412%D This will be default:
1413
1414\autoparagraphmode\plusone  % we need at least 1 in order to be able to switch to 2
1415%autoparagraphmode\plustwo  % because we define two \par delimited macros
1416
1417%D The same values apply as \type {\shownodedetails}. When set the box is shown in
1418%D the the log file but that is pretty slow when we have much detail.
1419
1420\tracingfullboxes\zerocount
1421
1422\protect \endinput
1423