%D \module
%D   [       file=syst-ini,
%D        version=2008.11.04, % 2001.11.16, % 1999.03.17,  % an oldie: 1995.10.10
%D          title=\CONTEXT\ System Macros,
%D       subtitle=Bootstrapping \TEX,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 We used to load plain \TEX\ in a special way, but redefining a couple of
%D primitives so that for instance font loading was ignored. For those interested,
%D this loader is found in \type {syst-tex.tex}. Some of the comment's are Don
%D Knuths and more of it can be found in the plain \TEX\ format.
%D
%D Successive versions will be adapted to \LUAMETATEX. That also means that we
%D define things a bit differently than in \MKII\ and \MKIV. This is party due to
%D the fact that in this engine we removed some side effects or added features. The
%D original \TEX\ program is well defined and documented and is still the benchmark.
%D The \ETEX\ extensions originally were meant as systematic follow up but that
%D never went beyond a first upgrade. It had a dual mode: compatible or extensible.
%D We always used the second mode. The \LUATEX\ engines don't have that.
%D
%D There are some hard coded properties that relate to for instance node types but
%D we always used abstraction. There are also side effects that (probably) originate
%D in the fact that \ETEX\ had to stay compatible with the dominant set of macros
%D using the original \TEX\ ones (either or not wrapped). Even unwanted side effects
%D eventually become features. In\LUATEX\ and even more in \LUAMETATEX\ we get
%D around that by additional mode variables that you can find below.
%D
%D Side note: those who complain about \TEX\ as rather special language, \ETEX\
%D extensions that should have been or be different, should really look into how
%D \TEX\ and friends evolved, and not come to quick and wrong conclusions. There are
%D only a very few languages that evolve this way. As a comparison one can wonder
%D why \CCODE\ never had proper strings like \PASCAL\ bit it's just hindsight and
%D discussing it has no use. In a similar fashion one should keep in mind that what
%D you see here is just a logical follow up in \MKII\ where we had to bend the rules
%D and \MKIV\ where we had a bit more available. Not many who look at the latest
%D engines had to jump though all the hoops.
%D
%D This is for instance noticeable in the names of some primitvies: \CONTEXT\ has a
%D concept of protection before \type {\protected} showed up, and similarly had
%D expansion helpers. This is why in \MKII\ you find \type {\protected}, \type
%D {\expanded}, and \type {\unexpanded} commands with different meanings than the
%D later \ETEX\ primitives. In \MKIV\ we stuck to the \CONTEXT\ originals and use
%D \type {\normal...} ones when needed (so \type {\normal...} is there for a
%D reason!). In \LMTX\ we made the transition from \type {\unexpanded} to \type
%D {\protected} so again this file looks different than the \MKIV\ ancestor.
%D
%D When primitives get initialized they come from the \TEX\ namespace (the
%D originals), the \ETEX\ extensions (although we dropped some) and \LUATEX\ which
%D includes the \LUAMETATEX\ ones. The most noticeable \ETEX\ extensions we kept are
%D those that relate to expansion and the expression scanners. The last group has
%D always been somewhat special because (we were told) it was modelled after a macro
%D set so it had to be compatible. It is tempting to replace it (and eventually I
%D might do that) but for now we stick to them (although with an integer divide
%D extension (like the \LUA\ \type {//} and the source has commented binary
%D operators but I haven't yet decided on the symbols to use because we need to
%D avoid expansion issues). For the record: the more extensive, related to \ETEX\
%D follow up \NTS\ never took of, probably because of the implementation language
%D and because performance made it unuseable. This is also why \LUATEX\ took a
%D different route: extending by an extension language. Of course in the end some
%D core functionality got added too.
%D
%D The most noticeable \LUAMETATEX\ specific code here has to do with additional
%D data types (integer and dimension constants) and macro argument extensions. We
%D also set up some defaults in handling math, languages, etc. Elsewhere in the
%D \CONTEXT\ source the real configuration and usage happens so don't consider this
%D an tutorial (as the plain format is).
%D
%D Category codes are another areas where subtle changed happened. You might not
%D grasp it (maybe because \CONTEXT\ is alien to you) but whenever you see something
%D that looks weird keep in mind that there is a valid reason for it.
%D
%D In due time I will add some more comments here, also because some \CONTEXT\
%D users might be interested in the hostiry. Now to the code!

%D Characters can have special states, that can be triggered by setting their
%D category coded. Some are preset, others are to be set as soon as possible,
%D otherwise we cannot define any useful macros.
%D
%D First we define a bunch of constants. Normally we would \type {\setconstant}
%D but we're prestine and have no macros defined yet. Abstraction also makes it
%D possible to avoid the \type {^^} in the input. We no longer define the \ETEX\
%D nodes here (we have not only more, but also different numbers) so we do it
%D elsewhere.

% cheatsheet
%
% # : 0--9
% * : ignore spaces
% + : keep the braces
% - : discard and don't count the argument
% . : ignore pars and spaces
% , : push back space when no match
% / : remove leading and trailing spaces and pars
% : : pick up scanning here
% ; : quit scanning
% = : braces are mandate
% ^ : keep leading spaces
% _ : braces are mandate and kept

\permanent\immutable\integerdef\escapecatcode             0
\permanent\immutable\integerdef\begingroupcatcode         1
\permanent\immutable\integerdef\endgroupcatcode           2
\permanent\immutable\integerdef\mathshiftcatcode          3
\permanent\immutable\integerdef\alignmentcatcode          4
\permanent\immutable\integerdef\endoflinecatcode          5
\permanent\immutable\integerdef\parametercatcode          6
\permanent\immutable\integerdef\superscriptcatcode        7
\permanent\immutable\integerdef\subscriptcatcode          8
\permanent\immutable\integerdef\ignorecatcode             9
\permanent\immutable\integerdef\spacecatcode             10
\permanent\immutable\integerdef\lettercatcode            11
\permanent\immutable\integerdef\othercatcode             12  % finally obsolete: \let\other \othercatcode
\permanent\immutable\integerdef\activecatcode            13  % finally obsolete: \let\active\activecatcode
\permanent\immutable\integerdef\commentcatcode           14
\permanent\immutable\integerdef\invalidcatcode           15

%permanent\immutable\integerdef\zeroasciicode             0
\permanent\immutable\integerdef\tabasciicode              9
\permanent\immutable\integerdef\newlineasciicode         10  % don't confuse this one with \endoflineasciicode
\permanent\immutable\integerdef\formfeedasciicode        12
\permanent\immutable\integerdef\endoflineasciicode       13  % somewhat messy but this can be the active \par
\permanent\immutable\integerdef\endoffileasciicode       26
\permanent\immutable\integerdef\spaceasciicode           32
\permanent\immutable\integerdef\exclamationmarkasciicode 33 % ! used in namespace protection
\permanent\immutable\integerdef\doublequoteasciicode     34 % "
\permanent\immutable\integerdef\hashasciicode            35
\permanent\immutable\integerdef\dollarasciicode          36
\permanent\immutable\integerdef\commentasciicode         37
\permanent\immutable\integerdef\ampersandasciicode       38
\permanent\immutable\integerdef\singlequoteasciicode     39 % '
\permanent\immutable\integerdef\primeasciicode           39 % '
\permanent\immutable\integerdef\leftparentasciicode      40
\permanent\immutable\integerdef\rightparentasciicode     41
\permanent\immutable\integerdef\commaasciicode           44
\permanent\immutable\integerdef\hyphenasciicode          45
\permanent\immutable\integerdef\periodasciicode          46
\permanent\immutable\integerdef\forwardslashasciicode    47 % /
\permanent\immutable\integerdef\zeroasciicode            48
\permanent\immutable\integerdef\nineasciicode            57
\permanent\immutable\integerdef\colonasciicode           58
\permanent\immutable\integerdef\semicolonasciicode       59
\permanent\immutable\integerdef\lessthanasciicode        60 % < used as alternative verbatim {
\permanent\immutable\integerdef\morethanasciicode        62 % > used as alternative verbatim }
\permanent\immutable\integerdef\questionmarkasciicode    63 % ? used in namespace protection
\permanent\immutable\integerdef\atsignasciicode          64 % @ used in namespace protection
\permanent\immutable\integerdef\leftbracketasciicode     91
\permanent\immutable\integerdef\backslashasciicode       92 % `\\
\permanent\immutable\integerdef\rightbracketasciicode    93
\permanent\immutable\integerdef\circumflexasciicode      94
\permanent\immutable\integerdef\underscoreasciicode      95
\permanent\immutable\integerdef\alowercaseasciicode      97
\permanent\immutable\integerdef\zlowercaseasciicode     122
\permanent\immutable\integerdef\leftbraceasciicode      123 % `\{
\permanent\immutable\integerdef\barasciicode            124 % `\|
\permanent\immutable\integerdef\rightbraceasciicode     125 % `\}
\permanent\immutable\integerdef\tildeasciicode          126 % `\~
\permanent\immutable\integerdef\delasciicode            127

\aliased\let\percentasciicode\commentasciicode

%catcode\zeroasciicode           \ignorecatcode      % `\^^@   ascii null is ignored
\catcode\tabasciicode            \spacecatcode       % `\^^I   ascii tab is a blank space
\catcode\formfeedasciicode       \activecatcode      % `\^^L   ascii form-feed (active, set later)
%catcode\endoflineasciicode      \endoflinecatcode   % `\^^M   ascii return is end-line
\catcode\endoffileasciicode      \ignorecatcode      % `\^^Z   endoffile (ignored in ConTeXt)
%catcode\spaceasciicode          \spacecatcode       % `\      ascii space is blank space
\catcode\hashasciicode           \parametercatcode   % `\#     hash mark is macro parameter character
\catcode\dollarasciicode         \mathshiftcatcode   % `\$     dollar sign is math shift
%catcode\commentasciicode        \commentcatcode     % `\%     percent sign is comment character
\catcode\ampersandasciicode      \alignmentcatcode   % `\&     ampersand is alignment tab
%catcode\backslashasciicode      \escapecatcode      % `\\     backslash is TeX escape character
\catcode\circumflexasciicode     \superscriptcatcode % `\^     circumflex and uparrow are for superscripts
\catcode\underscoreasciicode     \subscriptcatcode   % `\_     underline and downarrow are for subscripts
\catcode\leftbraceasciicode      \begingroupcatcode  % `\{     left brace is begin-group character
\catcode\rightbraceasciicode     \endgroupcatcode    % `\}     right brace is end-group character
\catcode\tildeasciicode          \activecatcode      % `\~     tilde is active
%catcode\delasciicode            \invalidcatcode     % `\^^?   ascii delete is invalid

%D First we need to initialization the primitives. Because \CONTEXT\ is already a
%D pretty old macro package, we have a couple of issues with respect to primitives.
%D The \ETEX\ engine added a few as did \PDFTEX. The \LUATEX\ engine added even
%D more. This means that there can be a potential clash between primitives and
%D existing macros. The most noticeable ones are:
%D
%D \starttyping
%D \protected
%D \expanded
%D \unexpanded
%D \stoptyping
%D
%D Because we had macros like that before the primitives showed up. The protection
%D related macros were there before we even knew about extensions to the engine.
%D When the expansion related ones were introduced, we originally came up with
%D different names but due to requests we used the current names, somethng that in
%D retrospect was a bad idea: they should have gotten different names in \LUATEX, if
%D only because at that time only \CONTEXT\ was using them in rolling releases.
%D Anyway, we're now stuck with this situation, and it means that one should use the
%D \type {\normal...} variants in low level code:
%D
%D \starttyping
%D \normalexpanded
%D \normalprotected  % no longer needed, we adapted over time
%D \normalunexpanded % idem, use \notexpanded so that we get syntax highlighting too
%D \stoptyping
%D
%D In the end not using different names in \LUATEX\ for these kind of backfires. It
%D makes not much sense to fix this in \LUAMETATEX\ because we're now to long on the
%D road. It is actually the reason why we have the option in \LUATEX\ to alias all
%D primitives in one go using a prefix. Actually this trick could be used to recover
%D a primitive meaning: just enable it with some prefix and \type {\let} the
%D original to that. But \unknown\ we prevent that trick below.
%D
%D The code below differs from \LUATEX: in \LUAMETATEX\ all primitives are already
%D available; it cannot limit itself to being \TEX\ or \ETEX. It could not do that
%D anyway because there are differences (no backend, to mention one).

%D We need to keep \normaldef fro the mkiv parser

\directlua {
    tex.enableprimitives("normal",true) % we default to everything
    function tex.enableprimitives() end % so we kind of protect what's there
}

\untraced\permanent\let\undefined\undefined

\permanent\cdef\space{ }
\permanent \def\empty{}
\mutable   \def\temp {}

\permanent\letcharcode \formfeedasciicode \par    % \def ^^L{\par}  formfeed
\permanent\letcharcode \tildeasciicode    \       %                 tilde
\permanent\letcharcode \spaceasciicode    \space  %                 space

\permanent\defcsname\tocharacter\tabasciicode      \endcsname {\ }   % \def\^^I{\ }    tab
\permanent\defcsname\tocharacter\formfeedasciicode \endcsname {\par} % \def\^^L{\par}  formfeed
\permanent\defcsname\tocharacter\endoflineasciicode\endcsname {\ }   % \def\^^M{\ }    return

%D For now:

\permanent\def\gobbleoneargument#-{} % will be defined later on anyway

%D First we define a simplified version of the \CONTEXT\ protection mechanism. Later
%D we will implement a better variant.

\def\unprotect
  {\edef\protect
     {\catcode\atsignasciicode         \the\catcode\atsignasciicode         \relax
      \catcode\exclamationmarkasciicode\the\catcode\exclamationmarkasciicode\relax
      \catcode\questionmarkasciicode   \the\catcode\questionmarkasciicode   \relax
      \catcode\underscoreasciicode     \the\catcode\underscoreasciicode     \relax
      \let\protect\relax}%
   \catcode\atsignasciicode         \lettercatcode
   \catcode\exclamationmarkasciicode\lettercatcode
   \catcode\questionmarkasciicode   \lettercatcode
   \catcode\underscoreasciicode     \lettercatcode}

\let\protect\relax

\unprotect

%D Some pretty important definitions:

\permanent\let\bgroup={
\permanent\let\egroup=}

%D \macros
%D   {normalbgroup,normalgroup}
%D
%D No comment.

%D Allocation of registers is done slightly different than in plain \TEX. First of
%D all we use different reserved counters. We also don't implement a family handler
%D because users are not supposed to implement their own math. We reserve the lowest
%D 31 registers for scratch purposes. Keep in mind that in the core engine some
%D registers are reserved: counters 0 upto 9, and counter 255.
%D
%D As with plain \TEX\ we recommend that macro designers always use \type {\global}
%D assignments with respect to registers numbered 1, 3, 5 \unknown\ 31, and always
%D non||\type {\global} assignments with respect to registers 0, 2, 4, \unknown\ 30.
%D This will prevent \quote {save stack buildup} that might otherwise occur.
%D
%D We reserve some registers for special (management) purposes. In the mkiv file you
%D can see how we also (even if we don't use it) deal with families and languages
%D and distinguish read and write ranges. There we also use a more traditional
%D insert allocator that operates in its own range. In \MKIV\ the ranges are:

%   0 -  20 : scratch
%  21 - 127 : internal
% 128 - 254 : inserts   (no longer)
% 255       : page
% 256 -     : user defined

%D However, in \LMTX\ we have:

%   0 - 199 : scratch
% 200 - 254 : internal
% 255       : page
% 256 -     : user defined

%D Later on, the \type {\c_syst_max_allocated_*} variables will be halfed so that
%D while local allocators will use the 23* range. So we have plenty of room reserved
%D for more private ones.

\directlua {tex.magicconstants = status.getconstants()} % this will be reset later on

\permanent\countdef\c_syst_min_allocated_register  = 201 \immutable\c_syst_min_allocated_register  =   256 % can change

\permanent\countdef\c_syst_min_allocated_iohandle  = 203 \immutable\c_syst_min_allocated_iohandle  =     0
\permanent\countdef\c_syst_max_allocated_iohandle  = 204 \immutable\c_syst_max_allocated_iohandle  =  1023

\permanent\countdef\c_syst_min_allocated_mark      = 205 \immutable\c_syst_min_allocated_mark      =    16 % a few scratch ones
\permanent\countdef\c_syst_max_allocated_mark      = 206 \immutable\c_syst_max_allocated_mark      =  1024 % max 10K in luametatex anyway

\permanent\countdef\c_syst_min_allocated_attribute = 207 \immutable\c_syst_min_allocated_attribute =  1024 % 0-1023 : private
\permanent\countdef\c_syst_max_allocated_attribute = 208 \immutable\c_syst_max_allocated_attribute = \directlua{tex.write(tex.magicconstants.max_attribute_register_index)}

\permanent\countdef\c_syst_max_allocated_count     = 211 \c_syst_max_allocated_count     = \directlua{tex.write(tex.magicconstants.max_integer_register_index)}
\permanent\countdef\c_syst_max_allocated_dimen     = 212 \c_syst_max_allocated_dimen     = \directlua{tex.write(tex.magicconstants.max_dimension_register_index)}
\permanent\countdef\c_syst_max_allocated_skip      = 213 \c_syst_max_allocated_skip      = \directlua{tex.write(tex.magicconstants.max_glue_register_index)}
\permanent\countdef\c_syst_max_allocated_muskip    = 214 \c_syst_max_allocated_muskip    = \directlua{tex.write(tex.magicconstants.max_muglue_register_index)}
\permanent\countdef\c_syst_max_allocated_box       = 215 \c_syst_max_allocated_box       = \directlua{tex.write(tex.magicconstants.max_box_register_index)}
\permanent\countdef\c_syst_max_allocated_toks      = 216 \c_syst_max_allocated_toks      = \directlua{tex.write(tex.magicconstants.max_toks_register_index)}
\permanent\countdef\c_syst_max_allocated_read      = 217 \c_syst_max_allocated_read      = \c_syst_max_allocated_iohandle
\permanent\countdef\c_syst_max_allocated_write     = 218 \c_syst_max_allocated_write     = \c_syst_max_allocated_iohandle
\permanent\countdef\c_syst_max_allocated_float     = 220 \c_syst_max_allocated_float     = \directlua{tex.write(tex.magicconstants.max_float_register_index or 0)}     % idem

\permanent\countdef\c_syst_last_allocated_count    = 221 \c_syst_last_allocated_count    = \c_syst_min_allocated_register
\permanent\countdef\c_syst_last_allocated_dimen    = 222 \c_syst_last_allocated_dimen    = \c_syst_min_allocated_register
\permanent\countdef\c_syst_last_allocated_skip     = 223 \c_syst_last_allocated_skip     = \c_syst_min_allocated_register
\permanent\countdef\c_syst_last_allocated_muskip   = 224 \c_syst_last_allocated_muskip   = \c_syst_min_allocated_register
\permanent\countdef\c_syst_last_allocated_box      = 225 \c_syst_last_allocated_box      = \c_syst_min_allocated_register
\permanent\countdef\c_syst_last_allocated_toks     = 226 \c_syst_last_allocated_toks     = \c_syst_min_allocated_register
\permanent\countdef\c_syst_last_allocated_read     = 227 \c_syst_last_allocated_read     = \c_syst_min_allocated_iohandle
\permanent\countdef\c_syst_last_allocated_write    = 228 \c_syst_last_allocated_write    = \c_syst_min_allocated_iohandle
\permanent\countdef\c_syst_last_allocated_marks    = 229 \c_syst_last_allocated_marks    = \c_syst_min_allocated_mark
\permanent\countdef\c_syst_last_allocated_float    = 230 \c_syst_last_allocated_float    = \c_syst_min_allocated_register

\permanent\countdef\c_syst_min_allocated_mvl       = 231 \c_syst_min_allocated_mvl       = \directlua{tex.write(tex.magicconstants.min_mvl_index)}
\permanent\countdef\c_syst_max_allocated_mvl       = 232 \c_syst_max_allocated_mvl       = \directlua{tex.write(tex.magicconstants.max_mvl_index)}
\permanent\countdef\c_syst_last_allocated_mvl      = 233 \c_syst_last_allocated_mvl      = \c_syst_min_allocated_mvl

\permanent\countdef\c_syst_min_counter_value       = 253 \c_syst_min_counter_value       = -"7FFFFFFF
\permanent\countdef\c_syst_max_counter_value       = 254 \c_syst_max_counter_value       =  "7FFFFFFF

\permanent\immutable\integerdef  \zerocount 0
\permanent\immutable\dimensiondef\zeropoint 0pt
\permanent\immutable\positdef    \zerofloat 0

\permanent\immutable\integerdef  \plusone   1
\permanent\immutable\integerdef  \minusone -1

\permanent\immutable\integerdef  \normalpagebox  255
\permanent\immutable\integerdef  \delayedpagebox 254

\permanent\immutable\integerdef  \statuswrite \minusone

%D These alternatives avoid the indirect references. Don't use these for generic
%D code because one never knows what happens elsewhere.

% \permanent\protected\def\newinteger   #1{\ifdefined#1\else\integerdef   #1\fi\zerocount} % can be relax
% \permanent\protected\def\newdimension #1{\ifdefined#1\else\dimensiondef #1\fi\zeropoint}
% \permanent\protected\def\newgluespec  #1{\ifdefined#1\else\gluespecdef  #1\fi\zeropoint}
% \permanent\protected\def\newmugluespec#1{\ifdefined#1\else\mugluespecdef#1\fi\zeropoint}

\permanent\protected\def\newinteger   #1{\integerdef   #1\zerocount}
\permanent\protected\def\newdimension #1{\dimensiondef #1\zeropoint}
\permanent\protected\def\newgluespec  #1{\gluespecdef  #1\zeropoint}
\permanent\protected\def\newmugluespec#1{\mugluespecdef#1\zeropoint}
\permanent\protected\def\newposit     #1{\positdef     #1\zerocount}

%D The aliasing here is tricky: we need to make sure the alias is not let to the
%D current value but behaves dynamic. Instead we can use just \newcount, unless of
%D course I introduce a reference in the engine which is not more efficient so then
%D one can as well use registers.

\protected\def\aliasinteger  #1#2{\untraced\protected\def#1{#2}}% name parent
\protected\def\aliasdimension#1#2{\untraced\protected\def#1{#2}}% name parent
\protected\def\aliasposit    #1#2{\untraced\protected\def#1{#2}}% name parent

%D So, effectively we start allocating from 256 and upwards. The inserts sit in the
%D range 128 upto 254. Page numbers use the counters 0 upto 9 and the pagebox is
%D 255. Users can use the scratch registers upto 31 without problem but all others
%D are reserved.

\permanent\let\wlog\gobbleoneargument % Let's get rid of this one.

%D The allocators share a common helper macro. Marks might be changed to work in lower
%D regions and we always assumes allocation.

\permanent\protected\def\newcount {\syst_basics_allocate\c_syst_last_allocated_count \count   \countdef  \c_syst_max_allocated_count}
\permanent\protected\def\newdimen {\syst_basics_allocate\c_syst_last_allocated_dimen \dimen   \dimendef  \c_syst_max_allocated_dimen}
\permanent\protected\def\newskip  {\syst_basics_allocate\c_syst_last_allocated_skip  \skip    \skipdef   \c_syst_max_allocated_skip}
\permanent\protected\def\newmuskip{\syst_basics_allocate\c_syst_last_allocated_muskip\muskip  \muskipdef \c_syst_max_allocated_muskip}
\permanent\protected\def\newbox   {\syst_basics_allocate\c_syst_last_allocated_box   \box     \integerdef\c_syst_max_allocated_box}
\permanent\protected\def\newtoks  {\syst_basics_allocate\c_syst_last_allocated_toks  \toks    \toksdef   \c_syst_max_allocated_toks}
\permanent\protected\def\newread  {\syst_basics_allocate\c_syst_last_allocated_read  \read    \integerdef\c_syst_max_allocated_read}
\permanent\protected\def\newwrite {\syst_basics_allocate\c_syst_last_allocated_write \write   \integerdef\c_syst_max_allocated_write}
\permanent\protected\def\newmarks {\syst_basics_allocate\c_syst_last_allocated_marks \marks   \integerdef\c_syst_max_allocated_mark}
\permanent\protected\def\newfloat {\syst_basics_allocate\c_syst_last_allocated_float \float   \floatdef  \c_syst_max_allocated_float}
\permanent\protected\def\newmvl   {\syst_basics_allocate\c_syst_last_allocated_mvl   \flushmvl\integerdef\c_syst_max_allocated_mvl}

\firstvalidlanguage \plusone % so zero is ignored in hyphenation, this might become the default

%D Watch out, for the moment we disable the check for already being defined later we
%D will revert this but first all chardefs must be replaced.

\permanent\protected\def\setconstant         {}
\permanent\protected\def\setconstantvalue#1#2{\csname#1\endcsname\numexpr#2\relax}

\permanent\protected\def\newconstant#1%
  {\ifrelax#1%
    %\writestatus{!!!!!!!!!!}{\string #1 => RELAX}%
%      \unletfrozen#1%
   % \newcount#1%
     \newinteger#1%
   \orelse\ifdefined#1%
    %\writestatus{!!!!!!!!!!}{\string #1 => DEFINED}%
   \else
   % \newcount#1%
     \newinteger#1%
   \fi}

\permanent\protected\def\setnewconstant#1{\newconstant#1#1}

% %D The next definitions are really needed (in \CONTEXT):

\newlinechar\newlineasciicode  \permanent\edef\outputnewlinechar{\tocharacter\newlineasciicode} % {^^J}

%D One reason to start high with allocation is that it permits us to allocate
%D consecutive ranges more easily, for instance if for \MPLIB\ we want to allocate a
%D continuous range of boxes. It also permits us to do a proper upward allocation
%D for inserts. The current code evolved from code that dealt with older engines but
%D as all engines now provide many registers we removed all traces. If you ever run
%D into the \MKII\ \type {mptopdf} code, you'll notice that some register magic was
%D needed to fit into existing macro packages. The same is true for \PICTEX\ that we
%D used in (generic) \PPCHTEX.

\ifdefined\writestatus \else
   %\protected\def\writestatus#1#2{\immediate\write\statuswrite{#1: #2}}
    \frozen\protected\def\writestatus#1#2{\message{#1: #2}}
\fi

%D The above command will normally hook into the interface commands and is a
%D protected command. We also have an \type {\diagnostic} predefined which is an
%D expandable command:
%D
%D \starttyping
%D test \diagnostic     {here 1} test
%D test \diagnostic[2]  {here 2} test
%D test \diagnostic[.05]{here 3} test
%D \stoptyping
%D
%D The optional argument triggers a sleep in seconds.

\def\syst_basics_allocate_yes#1#2#3#4#5% last class method max name
  {\ifnum#1<#4\relax
     \global\advanceby#1\plusone
     \permanent\global#3#5=#1\relax
   \else
     \writestatus{warning}{no room for \string#2\space \string#5\space (max: \number#4)}%
   \fi}

\def\syst_basics_allocate_nop#1#2#3#4#5% last class method max name
  {\writestatus{warning}{\string#2 \string#5 is already defined (\string\relax\space it first)}}

\def\syst_basics_allocate#1#2#3#4#5% last class method max name
  {\ifrelax#5%
     \expandafter\syst_basics_allocate_yes
   \orelse\ifdefined#5%
     \expandafter\syst_basics_allocate_nop
   \else
     \expandafter\syst_basics_allocate_yes
   \fi
   #1#2#3#4#5}

%D Since the number of chars exceed 256 now, we can use \type {\chardef} instead of
%D the more limited \type {\mathchardef}.

%D Attributes are something very \LUATEX. In \CONTEXT\ you are not supposed to use
%D the attributes directly but always allocate then first. For instance attribute~0
%D is reserved for special purposes (this might change). Attributes in the range
%D 128-1023 are private and should not be touched.

\permanent\immutable\integerdef\attributeunsetvalue\c_syst_min_counter_value % used to be \minusone

% \permanent\protected\def\newattribute{\syst_basics_allocate\c_syst_last_allocated_attribute\attribute\attributedef\c_syst_max_allocated_attribute}

% %D Not used by \CONTEXT\ but for instance \PICTEX\ needs it. It's a trick to force
% %D strings instead of tokens that take more memory.
%
% \permanent\protected\def\newhelp#1#2{\newtoks#1#1\expandafter{\detokenize{#2}}}

%D \macros
%D   {scratchcounter,
%D    scratchdimen,scratchskip,scratchmuskip,
%D    scratchbox,
%D    scratchtoks}
%D
%D We now define a few scratch registers, so that successive loads at least have
%D some available. The private ones are used in cases where we don't want to
%D intrude on normal scratch ones.

\newinteger  \scratchcounter    \newinteger  \globalscratchcounter \newinteger  \privatescratchcounter
\newposit    \scratchfloat      \newposit    \globalscratchfloat   \newposit    \privatescratchfloat
\newdimension\scratchdimen      \newdimension\globalscratchdimen   \newdimension\privatescratchdimen
\newgluespec \scratchskip       \newgluespec \globalscratchskip    \newgluespec \privatescratchskip
\newmuskip   \scratchmuskip     \newmuskip   \globalscratchmuskip  \newmuskip   \privatescratchmuskip
\newtoks     \scratchtoks       \newtoks     \globalscratchtoks    \newtoks     \privatescratchtoks
\newbox      \scratchbox        \newbox      \globalscratchbox     \newbox      \privatescratchbox

\newinteger  \scratchcounterone \newinteger  \scratchcountertwo    \newinteger  \scratchcounterthree
\newposit    \scratchfloatone   \newposit    \scratchfloattwo      \newposit    \scratchfloatthree
\newdimension\scratchdimenone   \newdimension\scratchdimentwo      \newdimension\scratchdimenthree
\newgluespec \scratchskipone    \newgluespec \scratchskiptwo       \newgluespec \scratchskipthree
\newmuskip   \scratchmuskipone  \newmuskip   \scratchmuskiptwo     \newmuskip   \scratchmuskipthree
\newtoks     \scratchtoksone    \newtoks     \scratchtokstwo       \newtoks     \scratchtoksthree
\newbox      \scratchboxone     \newbox      \scratchboxtwo        \newbox      \scratchboxthree

\newinteger  \scratchcounterfour\newinteger  \scratchcounterfive   \newinteger  \scratchcountersix
\newposit    \scratchfloatfour  \newposit    \scratchfloatfive     \newposit    \scratchfloatsix
\newdimension\scratchdimenfour  \newdimension\scratchdimenfive     \newdimension\scratchdimensix
\newgluespec \scratchskipfour   \newgluespec \scratchskipfive      \newgluespec \scratchskipsix
\newmuskip   \scratchmuskipfour \newmuskip   \scratchmuskipfive    \newmuskip   \scratchmuskipsix
\newtoks     \scratchtoksfour   \newtoks     \scratchtoksfive      \newtoks     \scratchtokssix
\newbox      \scratchboxfour    \newbox       \scratchboxfive      \newbox      \scratchboxsix

\newinteger\globalscratchcounterone
\newinteger\globalscratchcountertwo
\newinteger\globalscratchcounterthree

\mutable\let\scratchmacro   \relax
\mutable\let\scratchmacroone\relax
\mutable\let\scratchmacrotwo\relax

\newtoks\ignoredtoks

%D \macros
%D   {tempstring, scratchstring*}

\mutable\lettonothing\tempstring

\mutable\lettonothing\scratchstring
\mutable\lettonothing\scratchstringone
\mutable\lettonothing\scratchstringtwo

%D \macros
%D   {scratchwidth, scratchheight, scratchdepth, scratchoffset, scratchdistance}
%D
%D A few more scratch dimensions:

\newdimension\scratchwidth
\newdimension\scratchheight
\newdimension\scratchdepth
\newdimension\scratchitalic
\newdimension\scratchtotal

\newdimension\scratchoffset
\newdimension\scratchleftoffset
\newdimension\scratchrightoffset
\newdimension\scratchtopoffset
\newdimension\scratchbottomoffset

\newdimension\scratchdistance

\newdimension\scratchhsize
\newdimension\scratchvsize

\newdimension\scratchxoffset
\newdimension\scratchyoffset
\newdimension\scratchhoffset
\newdimension\scratchvoffset

\newdimension\scratchxposition
\newdimension\scratchyposition

\newinteger\scratchnx
\newinteger\scratchny

\newinteger\scratchmx
\newinteger\scratchmy

\newinteger\scratchsx
\newinteger\scratchsy

\newinteger\scratchmin
\newinteger\scratchmax

\newinteger\scratchunicode
\newinteger\scratchunicodeone
\newinteger\scratchunicodetwo
\newinteger\scratchunicodethree

\newinteger\scratchclass

\newdimension\scratchleftskip
\newdimension\scratchrightskip
\newdimension\scratchtopskip
\newdimension\scratchbottomskip

%D More allocations:

% \newcount \maxcount      \immutable\maxcount      2147483647

% \newdimen \zeropoint     \immutable\zeropoint     0pt
% \newdimen \onepoint      \immutable\onepoint      1pt
% \newdimen \halfapoint    \immutable\halfapoint    0.5pt
% \newdimen \maxdimen      \immutable\maxdimen      16383.99999pt % 1073741823sp
% \newdimen \onebasepoint  \immutable\onebasepoint  1bp
% \newdimen \scaledpoint   \immutable\scaledpoint   1sp
% \newdimen \thousandpoint \immutable\thousandpoint 1000pt

\permanent\immutable\integerdef    \maxcount 2147483647

%permanent\immutable\dimensiondef  \zeropoint     0pt
\permanent\immutable\dimensiondef  \onepoint      1pt
\permanent\immutable\dimensiondef  \twopoints     2pt
\permanent\immutable\dimensiondef  \threepoints   3pt
\permanent\immutable\dimensiondef  \halfapoint    0.5pt
\permanent\immutable\dimensiondef  \maxdimen      16383.99999pt % 1073741823sp
\permanent\immutable\dimensiondef  \onebasepoint  1bp
\permanent\immutable\dimensiondef  \scaledpoint   1sp
\permanent\immutable\dimensiondef  \thousandpoint 1000pt

\permanent\immutable\gluespecdef   \zeroskip            \zeropoint plus \zeropoint     minus \zeropoint
\permanent\immutable\gluespecdef   \centeringskip       \zeropoint plus \thousandpoint minus \thousandpoint
\permanent\immutable\gluespecdef   \stretchingskip      \zeropoint plus \thousandpoint
\permanent\immutable\gluespecdef   \shrinkingskip       \zeropoint                     minus \thousandpoint
\permanent\immutable\gluespecdef   \centeringfillskip   \zeropoint plus 1fill          minus 1fill
\permanent\immutable\gluespecdef   \stretchingfillskip  \zeropoint plus 1fill
\permanent\immutable\gluespecdef   \shrinkingfillskip   \zeropoint                     minus 1fill
\permanent\immutable\gluespecdef   \centeringfilllskip  \zeropoint plus 1filll         minus 1filll
\permanent\immutable\gluespecdef   \stretchingfilllskip \zeropoint plus 1filll
\permanent\immutable\gluespecdef   \shrinkingfilllskip  \zeropoint                     minus 1filll

\permanent\immutable\mugluespecdef \zeromuskip 0mu
\permanent\immutable\mugluespecdef \onemuskip  1mu
\permanent\immutable\mugluespecdef \muquad     18mu

\aliased\let\points   \onepoint
\aliased\let\halfpoint\halfapoint

\newtoks \emptytoks

%D And even more: (todo: countdefs 60+). Watch out, we need a count for a negative constant
%D but can use chardefs for positive ones. Actually, because we have plenty of counters we
%D can also use counts for those.

%permanent\immutable\integerdef\minusone                  -1
\permanent\immutable\integerdef\minustwo                  -2
%permanent\immutable\integerdef\zerocount                  0
%permanent\immutable\integerdef\plusone                    1
\permanent\immutable\integerdef\plustwo                    2  % also fitness value
\permanent\immutable\integerdef\plusthree                  3
\permanent\immutable\integerdef\plusfour                   4
\permanent\immutable\integerdef\plusfive                   5
\permanent\immutable\integerdef\plussix                    6
\permanent\immutable\integerdef\plusseven                  7
\permanent\immutable\integerdef\pluseight                  8
\permanent\immutable\integerdef\plusnine                   9
\permanent\immutable\integerdef\plusten                   10
\permanent\immutable\integerdef\pluseleven                11
\permanent\immutable\integerdef\plustwelve                12  % also fitness value
\permanent\immutable\integerdef\plusthirteen              13
\permanent\immutable\integerdef\plusfourteen              14
\permanent\immutable\integerdef\plusfifteen               15
\permanent\immutable\integerdef\plussixteen               16
\permanent\immutable\integerdef\plustwentyfour            24
\permanent\immutable\integerdef\plustwentyfive            25
\permanent\immutable\integerdef\plusthirtytwo             32
\permanent\immutable\integerdef\plusfourtytwo             42  % also fitness value
\permanent\immutable\integerdef\plusfifty                 50
\permanent\immutable\integerdef\plussixty                 60
\permanent\immutable\integerdef\plusninetynine            99  % also fitness value
\permanent\immutable\integerdef\plushundred              100
\permanent\immutable\integerdef\plusonehundred           100
\permanent\immutable\integerdef\plusonehundredtwentyfive 125
\permanent\immutable\integerdef\plustwohundred           200
\permanent\immutable\integerdef\plustwohundredfifty      250
\permanent\immutable\integerdef\plusfivehundred          500
\permanent\immutable\integerdef\pluscxxvii               127
\permanent\immutable\integerdef\pluscxxviii              128
\permanent\immutable\integerdef\pluscclv                 255
\permanent\immutable\integerdef\pluscclvi                256
\permanent\immutable\integerdef\plusthousand            1000
\permanent\immutable\integerdef\plusonethousand         1000
\permanent\immutable\integerdef\plustwothousand         2000
\permanent\immutable\integerdef\plusthreethousand       3000
\permanent\immutable\integerdef\plusfourthousand        4000
\permanent\immutable\integerdef\plusfivethousand        5000
\permanent\immutable\integerdef\plussixthousand         6000
\permanent\immutable\integerdef\plusseventhousand       7000
\permanent\immutable\integerdef\pluseightthousand       8000
\permanent\immutable\integerdef\plusninethousand        9000
\permanent\immutable\integerdef\plustenthousand        10000
\permanent\immutable\integerdef\plustwentythousand     20000

\permanent\immutable\integerdef\plusthousandfifty       1050 % kind of special defaults
\permanent\immutable\integerdef\plustwelvehundredfifty  1250
\permanent\immutable\integerdef\plusfifteenhundred      1500
\permanent\immutable\integerdef\plustwentyfivehundred   2500
\permanent\immutable\integerdef\plusfortyfivehundred    4500
\permanent\immutable\integerdef\plusseventyfivehundred  7500

\permanent\immutable\integerdef\medcard          32768
\permanent\immutable\integerdef\maxcard          65536 % pdftex has less mathchars
\permanent\immutable\integerdef\maxcardminusone  65535
\permanent\immutable\integerdef\maxiterator     "FFFFF

\permanent\immutable\integerdef\binaryshiftedten          1024
\permanent\immutable\integerdef\binaryshiftedtwenty    1048576
\permanent\immutable\integerdef\binaryshiftedthirty 1073741824

%D \macros
%D   {doubleexpandafter,tripleexpandafter,expanded,startexpanded}
%D
%D A few handy shortcuts

\permanent\let\singleexpandafter \expandafter
\permanent\def\doubleexpandafter{\expandafter\expandafter\expandafter}
\permanent\def\tripleexpandafter{\expandafter\doubleexpandafter\expandafter}

%D We prefer the more readable variant than in plain \TEX. User should only
%D use \type {\emptybox}:

%D This one is public, so best not mess with it (only a built in can be safe, as
%D with \type {\undefined}).

% tricky: integerdefs are not really immutable, their definition is
% but counters can be .. maybe i need to split that: immutable for
% content; a chardef could also work

\newbox\voidbox % see end of file:

\immutable\setbox\voidbox\box\voidbox % prevent setting the box

\permanent\def\unvoidbox{\unhbox\voidbox}
\permanent\def\emptybox {\box   \voidbox} % used in initializations so no attributes
\permanent\def\emptyvbox{\vpack{}}        % no copy as we need to set attributes
\permanent\def\emptyhbox{\hpack{}}        % idem

\aliased\let\dontleavehmode\quitvmode

% Here's one plain/latex loves a lot and it's the advertized solution
% there but we prefer to use \dontleavehmode instead and never use the
% next one. We put it here a reminder.

\aliased\let\leavevmode\unvoidbox

%D \macros
%D   {dontcomplain,lessboxtracing}
%D
%D We need this one soon. The second one shows less box details (because that actually
%D slows down noticeably due to the more extensive details in node content.

% With \showboxdepth\maxcount and \showboxbreadth\maxcount the second one is way slower:
%
% \testfeatureonce{10}{\setbox\scratchbox\vbox        {\samplefile{tufte}}} \elapsedtime\par
% \testfeatureonce{10}{\setbox\scratchbox\vbox to 10cm{\samplefile{tufte}}} \elapsedtime\par

\permanent\protected\def\dontcomplain
  {\hbadness\plustenthousand
   \vbadness\plustenthousand
   \hfuzz   \maxdimen
   \vfuzz   \maxdimen}

\permanent\protected\def\noboxtracing
  {\showboxdepth  \zerocount
   \showboxbreadth\zerocount}

\permanent\protected\def\lessboxtracing
  {\showboxdepth  \plusfive
   \showboxbreadth\plusten}

\permanent\protected\def\moreboxtracing
  {\showboxbreadth\maxcount
   \showboxdepth  \maxcount}

%D We define \type {\newif} a la plain \TEX, but will redefine it later. As Knuth
%D says:
%D
%D \startnarrower
%D And here's a different sort of allocation: for example,
%D
%D \starttyping
%D \newif\iffoo
%D \stoptyping
%D
%D creates \type {\footrue}, \type {\foofalse} to go with \type {\iffoo}.
%D \stopnarrower

% \protected\def\newif#1%
%   {\count@\escapechar
%    \escapechar\minusone
%    \expandafter\expandafter\expandafter\def\new_if #1{true}{\let#1\iftrue }%
%    \expandafter\expandafter\expandafter\def\new_if#1{false}{\let#1\iffalse}%
%    \new_if#1{false}% the condition starts out false
%    \escapechar\count@}
%
% \def\new_if#1#2%
%   {\csname\expandafter\if@\string#1#2\endcsname}
%
% \bgroup % `if' is required
%   \uccode`1=`i \uccode`2=`f \uppercase{\gdef\if@12{}}
% \egroup

% We use \csstring so there is no need to push/pop escapechar.
% We use different names so that we get a better error message.
%
% \protected\def\newif#1%
%   {\let\new_if_saved\newif
%    \let\newif\new_if_check
%    \expandafter\expandafter\expandafter\def\new_if_cs #1{true}{\let#1\iftrue }%
%    \expandafter\expandafter\expandafter\def\new_if_cs#1{false}{\let#1\iffalse}%
%    \new_if_cs#1{false}%
%    \let\newif\new_if_saved}
%
% \protected\def\new_if_cs#1#2%
%   {\csname\expandafter\newif\csstring#1#2\endcsname}
%
% We wrap all into one macro (the frozen stuff adds 10% runtime):

\permanent\protected\def\new_if_define#1#2%
  {\permanent\protected\gdefcsname\expandafter\new_if_check\csstring#2true\endcsname {\enforced#1#2\iftrue }%
   \permanent\protected\gdefcsname\expandafter\new_if_check\csstring#2false\endcsname{\enforced#1#2\iffalse}%
   \csname\expandafter\new_if_check\csstring#2false\endcsname}

\expanded{\gdef\noexpand\new_if_check\string i\string f{}}

\permanent\protected\def\newif      {\new_if_define\let }
\permanent\protected\def\globalnewif{\new_if_define\glet}

%D Let's test this one:

\newif\ifdone  \globalnewif\ifglobaldone
\newif\iffound \globalnewif\ifglobalfound

\newif\ifscratchcondition
\newif\ifscratchconditionone
\newif\ifscratchconditiontwo

%D This used to be a protected definition with a \type {dimexpr} but now we have
%D it natively (less tracking noise).

\aliased\let\htdp\boxtotal

%D A few shortcuts (gone by now):

% \permanent\protected\def\udef {\protected\def }
% \permanent\protected\def\ugdef{\protected\gdef}
% \permanent\protected\def\uedef{\protected\edef}
% \permanent\protected\def\uxdef{\protected\xdef}

%D For a while we keep the following, as systems like tikz need it. Best not use
%D that one \CONTEXT. It will probably move to the tikz loader.

\aliased\let\active\activecatcode

%D Constants to be used with \type {\interactionmode}.

% \permanent\immutable\integerdef\batchmodecode     \zerocount
% \permanent\immutable\integerdef\nonstopmodecode   \plusone
% \permanent\immutable\integerdef\scrollmodecode    \plustwo
% \permanent\immutable\integerdef\errorstopmodecode \plusthree

%D Of course we want even bigger log files, so we copied this from the \ETEX\
%D source files.
%D
%D When watching such logs, beware of nasty side effects of \type {\scantokens},
%D as in:
%D
%D \starttyping
%D \bgroup
%D   \lccode`a=12\lowercase{\xdef\whatever{a}}\egroup
%D   \def\whatever{test \whatever test}
%D   \scantokens\expandafter{\whatever}
%D \egroup
%D \stoptyping
%D
%D In \LUATEX\ we have ways around this.

% no longer \errorstopmode cf. plain tex 3.141592653

%D We have more tracers in \LUAMETATEX\ so we make two categories and are
%D generous when one wants them all. In the end only very few users will
%D do this kind of tracing and if you don't know the internals of \TEX\ or
%D how \CONTEXT\ works, it makes little sense anyway. This kind of tracing
%D might evolve over time.

% group:input:catcodes: ......
%
% \nodetype<nodeindex>[subtype]<attrindex>#count[name=value,...]

\untraced\permanent\protected\def\syst_tracingprocess#1%
  {\tracingassigns    #1%
   \tracingcommands   #1%
   \tracingexpressions#1%
   \tracinggroups     #1%
   \tracingifs        #1%
   \tracinglevels     #1%
   \tracingmacros     #1%
   \tracingnesting    #1%
   \tracingnodes      #1%
   \tracingrestores   #1%
   \tracingstats      #1}

\untraced\permanent\protected\def\syst_tracingtypesetting#1%
  {\tracingadjusts    #1%
   \tracingalignments #1%
   \tracingbalancing  #1%
   \tracingfitness    #1%
   \tracingfullboxes  #1%
   \tracinghyphenation#1%
   \tracinginserts    #1%
   \tracinglists      #1%
   \tracingloners     #1%
   \tracinglostchars  #1%
   \tracingmarks      #1%
   \tracingmath       #1%
   \tracingmvl        #1%
   \tracingorphans    #1%
   \tracingoutput     #1%
   \tracingpages      #1%
   \tracingparagraphs #1%
   \tracingpasses     #1%
   \tracingpenalties  #1%
   \tracinglooseness  #1%
   \tracingsnapping   #1%
   \tracingtoddlers   #1}

\untraced\permanent\protected\def\syst_tracingall
  {\syst_tracingprocess    \maxcount
   \syst_tracingtypesetting\maxcount
   \showboxbreadth         \maxcount
   \showboxdepth           \plushundred}

\untraced\permanent\protected\def\syst_tracingnone
  {\syst_tracingprocess    \zerocount
   \syst_tracingtypesetting\zerocount
   \showboxdepth           \plusfive
   \showboxbreadth         \plusten}

\untraced\permanent\protected\def\tracingprocess    {\syst_tracingprocess    \maxcount\tracingonline\plusone}
\untraced\permanent\protected\def\tracingtypesetting{\syst_tracingtypesetting\maxcount\tracingonline\plusone}

\untraced\permanent\protected\def\tracingall {\syst_tracingall \tracingonline\plusone  }
\untraced\permanent\protected\def\loggingall {\syst_tracingall \tracingonline\zerocount}
\untraced\permanent\protected\def\tracingnone{\syst_tracingnone\tracingonline\zerocount}
%untraced\permanent\protected\def\loggingnone{\syst_tracingnone\tracingonline\zerocount}

%D When we want to see a box we can as well show all of it.

% \showboxdepth  \maxcount
% \showboxbreadth\maxcount

\lessboxtracing

%D Just for tracing purposes we set:

\tracingstats\plusone

%D This is only used when we load \CONTEXT. Nesting is not supported here.

\permanent\def\pushoverloadmode
  {\enforced\permanent\protected\edef\popoverloadmode{\normaloverloadmode\the\normaloverloadmode\enforced\let\popoverloadmode\relax}%
   \normaloverloadmode\zerocount}

\aliased\let\popoverloadmode\relax

\let\pushrunstate\relax % will be defined later
\let\poprunstate \relax % will be defined later

\newtoks\everydump

\pushoverloadmode

\permanent\protected\def\dump
  {\the\everydump
   \global\everydump\emptytoks
   \pushoverloadmode
   \glet\everydump\undefined      % catches bad usage
   \enforced\glet\dump\relax      % silently ignored
 % \enforced\glet\dump\normaldump % quits with an error
   \popoverloadmode
   \normaldump}

\permanent\protected\def\input
  {\normalinput}

\popoverloadmode

%D \macros
%D   {newconditional,
%D    settrue, setfalse,
%D    ifconditional,then}
%D
%D \TEX's lacks boolean variables, although the \PLAIN\ format implements \type
%D {\newif}. The main disadvantage of this scheme is that it takes three hash table
%D entries. A more memory saving alternative is using a variable. In \MKXL\ we do it
%D a bit different than in \MKIV\ where we use aliases and thereby one macro per
%D conditional. Here we also need one but we are more strict and demand a proper
%D definition. This permits us to apply permanent as well as immutable properties.

\aliased\let\conditionalfalse\plusone
\aliased\let\conditionaltrue \zerocount

%permanent\protected\def\setconditional#1{#1}
\permanent\protected\def\settrue       #1{#1\conditionaltrue }
\permanent\protected\def\setfalse      #1{#1\conditionalfalse}
\permanent\protected\def\settruevalue  #1{\csname#1\endcsname\conditionaltrue }
\permanent\protected\def\setfalsevalue #1{\csname#1\endcsname\conditionalfalse}

\permanent\protected\def\newconditional#1{\permanent\integerdef#1\conditionalfalse}

%aliased\let\ifconditional \ifcase
\aliased\let\ifconditional \ifzeronum   % permits \unless\ifconditional

\permanent\protected\lettonothing\then % so that we can say: \ifnum1>2\then -)

\permanent\def\truecondition {\iftrue}
\permanent\def\falsecondition{\iffalse}

% This one has to be unprotected otherwise we get a files-ends-to-soon but it's ok
% as conditions expand anyway.

\permanent\def\quitcondition{\orelse\iffalse}

%D \macros
%D   {newmacro,setnewmacro,newfraction}
%D
%D Let's be complete and also introduce some definers. These are not mandate
%D but handy for grepping. We make these mutable because they are \type
%D {\let} (the only advantage is that we often give them names with some
%D prefix.

\permanent\protected\def\newmacro   #1{\mutable\lettonothing#1}
\permanent\protected\def\setnewmacro#1{\mutable\let#1}

\permanent\protected\def\newfraction#1{\mutable\let#1\!!plusone}

\def\!!zerocount{0} % later redefined .. kind of obsolete
\def\!!plusone  {1} % later redefined .. kind of obsolete

%D It would be handy to have a primitive \type {\unless\ifcase} because then we
%D could use nicer values. Anyhow, this conditional code used to be in the \type
%D {syst-aux} module but is now promoted to here.

%D \macros
%D   {ifzeropt}
%D
%D The next macro is both cosmetic and byte saving. It is pretty \type
%D {\if}||safe too. It can be used in cases like:
%D
%D \starttyping
%D \ifzeropt \somedimen ... \else ... \fi
%D \stoptyping

\aliased\let\ifzeropt\ifzerodim % was \let\ifzeropt\ifcase
\aliased\let\ifzero  \ifzeronum % was \let\ifzeropt\ifcase

% these token list helpers might move to syst-aux.mkiv
%
% we assume a \cs. not toks0 or so
%
% \protected\def\appendtotoks #1#{\def\temp{#1}\afterassignment\doappendtotoks \scratchtoks=}
% \protected\def\prependtotoks#1#{\def\temp{#1}\afterassignment\doprependtotoks\scratchtoks=}

\newtoks\t_syst_toks_temp \let\m_syst_toks_temp\t_syst_toks_temp

\permanent\protected\def\appendtotoks #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_append_l \t_syst_toks_temp=}
\permanent\protected\def\prependtotoks#1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_prepend_l\t_syst_toks_temp=}

% \def\syst_toks_append {\m_syst_toks_temp\expandafter\expandafter\expandafter{\expandafter\the\expandafter\m_syst_toks_temp\the\t_syst_toks_temp}}}
% \def\syst_toks_prepend{\m_syst_toks_temp\expandafter\expandafter\expandafter{\expandafter\the\expandafter\t_syst_toks_temp\the\m_syst_toks_temp}}}

\permanent\protected\def\globalappendtotoks #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_append_g \t_syst_toks_temp=}
\permanent\protected\def\globalprependtotoks#1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_prepend_g\t_syst_toks_temp=}

\def\syst_toks_append_l {\expanded{\m_syst_toks_temp{\the\m_syst_toks_temp\the\t_syst_toks_temp}}}
\def\syst_toks_prepend_l{\expanded{\m_syst_toks_temp{\the\t_syst_toks_temp\the\m_syst_toks_temp}}}

\def\syst_toks_append_g {\global\syst_toks_append_l }
\def\syst_toks_prepend_g{\global\syst_toks_prepend_l}

\permanent\protected\def\addtotoks      #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_add_l\let\next}
\permanent\protected\def\globaladdtotoks#1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_add_g\let\next}

\def\syst_toks_add_l{\m_syst_toks_temp\expandafter\bgroup\the\m_syst_toks_temp}
\def\syst_toks_add_g{\global\syst_toks_add_l}

\permanent\protected\def\addtotokscs      #1#2{#1{\the#1#2}}        % saves a few bytes
\permanent\protected\def\globaladdtotokscs#1#2{\global#1{\the#1#2}} % saves a few bytes

%D \macros
%D   {begcsname}
%D
%D Handy for \ETEX-only usage (avoids making \type {\relax}'s:

% \def\begcsname#1\endcsname{\ifcsname#1\endcsname\csname#1\endcsname\fi}

\aliased\let\begcsname\begincsname

%D Now come a few macros that might be needed in successive loading. We redefine the
%D \type {\par} primitive pretty soon so that we get the equivalents right.

% too tricky: \par is use more often than a par starts so we have too much change
% that we get assymetrical behaviour
%
% \newtoks\everyendpar
%
% \protected\def\endpar{\the\everyendpar\normalpar}
% \protected\def\par   {\endpar}
%
% \protected\def\reseteverypar
%   {\everypar   \emptytoks
%    \everyendpar\emptytoks}

\permanent\protected\def\reseteverypar
  {\everypar\emptytoks}

\aliased\let\endgraf\par % plain, a kind of funny name
%aliased\let\endline\cr  % plain, not needed and probably also confusing

\permanent\protected\def\null{\hpack{}}

%D The following two might be overloaded later on but some modules need then
%D earlier. These functionality is reflected in the name and will not change.

% \bgroup
%     \catcode`\^^M=\activecatcode%
%     \gdef\obeylines{\catcode`\^^M\activecatcode \let^^M\par}%
%     \glet^^M\par%
% \egroup
%
% \bgroup
%     \gdef\obeyspaces{\catcode`\ \activecatcode}%
%     \obeyspaces\glet =\space%
% \egroup

\def\obeylines {\catcode\endoflineasciicode\activecatcode\letcharcode\endoflineasciicode\par  } % check if these can be \permanent
\def\obeyspaces{\catcode\spaceasciicode    \activecatcode\letcharcode\spaceasciicode    \space} % check if these can be \permanent

% %D A constant:
%
% \let\endoflinetoken=^^M

%D Also needed might be a simple loop structure and we borrow plain \TEX's one
%D as it is often expected to be present and it is about the fastest you can
%D get. Beware: this macro does not support nested loops. We use a namespace
%D prefix \type {@@pln}.

\let\syst_plain_body\relax

\permanent\def\loop#1\repeat{\def\syst_plain_body{#1}\syst_plain_iterate} % might go

%D The following makes \type {\loop} \unknown\ \type {\if} \unknown\ \type
%D {\repeat} skippable (clever trick):

\permanent\let\repeat\fi % so both \loop and \repeat are reserved words!

%D The original (no \type {syst_plain_} there):
%D
%D \starttyping
%D \def\syst_plain_iterate
%D   {\syst_plain_body
%D    \let\next\syst_plain_iterate
%D    \else
%D      \let\next\relax
%D    \fi
%D    \next}
%D \stoptyping
%D
%D A more efficient alternative:
%D
%D \starttyping
%D \def\syst_plain_iterate
%D   {\syst_plain_body
%D    \expandafter\syst_plain_iterate
%D    \else\expandafter\relax\fi}
%D \stoptyping
%D
%D An even more efficient one:

\def\syst_plain_iterate
  {\syst_plain_body
   \expandafter\syst_plain_iterate\else\fi}

%D We don't define a real output routine yet but at least get rid of pages:

\output{\shipout\box\normalpagebox}

%D Although we don't add pagenumbers yet we alias the default register used
%D for counting pages:

\permanent\countdef\pageno\zerocount \pageno\plusone % first page is number 1

%D Beside the raw counter \type {\pageno} the \type {\folio} macro provides
%D the value.

\permanent\def\folio{\the\pageno} % kind of expected and therefore reserved

%D The following registers are kind of standard and (for the moment) we define
%D them here. This might change.

\newgluespec \bigskipamount    \bigskipamount   = 12pt plus 4pt minus 4pt
\newgluespec \medskipamount    \medskipamount   =  6pt plus 2pt minus 2pt
\newgluespec \smallskipamount  \smallskipamount =  3pt plus 1pt minus 1pt

\baselineskip  = 12pt
\lineskip      =  1pt
\lineskiplimit =  0pt

% %D Sometimes kerns make more sense than glue but we need to be in the right mode:

% \ifdefined\vkern    \else \permanent\protected\def\vkern   {\ifhmode\par           \fi\kern}    \fi
% \ifdefined\hkern    \else \permanent\protected\def\hkern   {\ifvmode\dontleavehmode\fi\kern}    \fi
%
% \ifdefined\vpenalty \else \permanent\protected\def\vpenalty{\ifhmode\par           \fi\penalty} \fi
% \ifdefined\hpenalty \else \permanent\protected\def\hpenalty{\ifvmode\dontleavehmode\fi\penalty} \fi

%D Again a few kind-of-extensions the core. These come from plain \TEX\ but are
%D probably not used in \CONTEXT.

\newgluespec \hideskip   \immutable\hideskip  -1000pt plus 1fill           % obsolete
\newgluespec \centering  \immutable\centering 0pt plus 1000pt minus 1000pt % obsolete

% \permanent\def\hidewidth % for alignment entries that can stick out
%   {\hskip\hideskip}
%
% \permanent\def\ialign % initialized \halign, to be used grouped!
%   {\everycr\emptytoks
%    \tabskip\zeroskip
%    \halign}

\newcount \mscount

\permanent\def\spanomit{\span\omit} % bypass error message

\permanent\def\multispan#1%
  {\omit
   \mscount#1\relax
   \loop
     \ifnum\mscount>\plusone
       \spanomit \advanceby\mscount\minusone
   \repeat}

\let\nopdfcompression        \relax % later
\let\onlypdfobjectcompression\relax % later
\let\maximumpdfcompression   \relax % later
\let\normalpdfcompression    \relax % later

%D Basic status stuff.

% \newif\ifproductionrun % already defined

%D For those who expect this \unknown

\ifdefined\fmtname    \else \permanent\def\fmtname   {ConTeXt Initial TeX} \fi
\ifdefined\fmtversion \else \permanent\def\fmtversion{3.1415926}           \fi

%D A few bonus macros:

%permanent\def\modulonumber#1#2{\tointeger{#2-((((#2+(#1/2))/#1)-1)*#1)}}
\permanent\def\modulonumber#1#2{\tointeger {#2-(#2:#1)*#1}}
\permanent\def\dividenumber#1#2{\tointeger{(#2-(#1/2))/#1}}

%D These could be set at the \LUA\ end instead:

\permanent\edef\texenginename         {\directlua{tex.print(LUATEXENGINE)}}
\permanent\edef\texengineversion      {\directlua{tex.print(LUATEXVERSION)}}
\permanent\edef\texenginefunctionality{\directlua{tex.print(LUATEXFUNCTIONALITY)}}

%D We have no reason not to enable this:

\savingvdiscards\plusone

%D We only can set this one via directives (system.synctex) and we only support
%D the context variant. This will go away completely.

\newcount\synctex % \let\normalsynctex\synctex

%D We get rid of the funny \TEX\ offset defaults of one inch by setting them to
%D zero. In fact, they're completely gone now.

% \voffset\zeropoint \overloaded\let\voffset\relax \newdimen\voffset \let\normalvoffset\voffset
% \hoffset\zeropoint \overloaded\let\hoffset\relax \newdimen\hoffset \let\normalhoffset\hoffset

\matheqnogapstep\zerocount % for now

%D Now we define a few helpers that we need in a very early stage. We have no
%D message system yet but redundant definitions are fatal anyway.

\newcount\c_syst_helpers_n_of_namespaces \c_syst_helpers_n_of_namespaces\pluseight % 1-8 reserved for catcodes

\def\v_interfaces_prefix_template_system{\the       \c_syst_helpers_n_of_namespaces>>}
%def\v_interfaces_prefix_template_system{\characters\c_syst_helpers_n_of_namespaces>>} % no \characters yet

\protected\def\installsystemnamespace#1% will be redefined
  {\ifcsname ??#1\endcsname
     \immediate\write\statuswrite{fatal error: duplicate system namespace '#1'}%
   \else
     \global\advanceby\c_syst_helpers_n_of_namespaces\plusone
     \permanent\cdefcsname ??#1\endcsname{\v_interfaces_prefix_template_system}%
   \fi}

%D It makes more sense to have these here:

\aliased\let\normalstartimath  \Ustartmath
\aliased\let\normalstopimath   \Ustopmath
\aliased\let\normalstartdmath  \Ustartdisplaymath
\aliased\let\normalstopdmath   \Ustopdisplaymath

\aliased\let\normalUchar       \tocharacter

% These are the old U* commands:

\aliased\let\Usuperscript      \superscript
\aliased\let\Usubscript        \subscript
\aliased\let\Uprimescript      \primescript
\aliased\let\Unosuperscript    \nosuperscript
\aliased\let\Unosubscript      \nosubscript
\aliased\let\Usuperprescript   \superprescript
\aliased\let\Usubprescript     \subprescript
\aliased\let\Unosuperprescript \nosuperprescript
\aliased\let\Unosubprescript   \nosubprescript

\aliased\let\Ustack            \mathstack
\aliased\let\Umathdict         \mathdictionary
\aliased\let\Umathclass        \mathclass

\aliased\let\Ustyle            \givenmathstyle

\aliased\let\Uchar             \tocharacter

% Todo:

% \aliased\let\Umathchar            \mathcharacter
% \aliased\let\Umathchardef         \mathcharacterdef
% \aliased\let\Umathdictdef         \mathdictionarydef
% \aliased\let\Umathchardef         \mathcharacterdef

% \aliased\let\Udelimited           \mathdelimited           % left right math
% \aliased\let\Udelimiterover       \mathdelimiterover       % top math
% \aliased\let\Udelimiterunder      \mathdelimiterunder      % bottom math

% \aliased\let\Uhextensible         \mathhextensible         % middle
% \aliased\let\Uoverdelimiter       \mathoverdelimiter       % top math
% \aliased\let\Uunderdelimiter      \mathunderdelimiter      % bottom math

% \aliased\let\Uradical             \mathdelimitedradical    % left math
% \aliased\let\Uroot                \mathdelimitedroot       % left degree math
% \aliased\let\Urooted              \mathdelimitedrooted     % left right degree math

% \aliased\let\Uabove               \mathabove
% \aliased\let\Uabovewithdelims     \mathabovewithdelims
% \aliased\let\Uatop                \mathatop
% \aliased\let\Uatopwithdelims      \mathatopwithdelims
% \aliased\let\Uover                \mathover
% \aliased\let\Uoverwithdelims      \mathoverwithdelims
% \aliased\let\Uskewed              \mathskewed
% \aliased\let\Uskewedwithdelims    \mathskewedwithdelims
% \aliased\let\Ustretched           \mathstretched
% \aliased\let\Ustretchedwithdelims \mathstretchedwithdelims

% \aliased\let\Uleft                \mathleftfence
% \aliased\let\Umiddle              \mathmiddlefence
% \aliased\let\Uoperator            \mathoperator
% \aliased\let\Uright               \mathrightfence
% \aliased\let\Uvextensible         \mathvextensible

% \aliased\let\overline             \mathoverline
% \aliased\let\underline            \mathunderline

% \aliased\let\Udelimiter           \Umathdelimiter
% \aliased\let\Umathaccent          \Umathaccented
% \aliased\let\Udelcode             \mathdelimitercode
% \aliased\let\Umathcode            \mathcharactercode

% \aliased\let\Umathadapttoleft     \mathadapttoleft
% \aliased\let\Umathadapttoright    \mathadapttoright
% \aliased\let\Umathlimits          \mathlimits
% \aliased\let\Umathnoaxis          \mathnoaxis
% \aliased\let\Umathnolimits        \mathnolimits
% \aliased\let\Umathopenupdepth     \mathopenupdepth
% \aliased\let\Umathopenupheight    \mathopenupheight
% \aliased\let\Umathphantom         \mathphantom
% \aliased\let\Umathsource          \mathsource
% \aliased\let\Umathuseaxis         \mathuseaxis
% \aliased\let\Umathvoid            \mathvoid

% \aliased\let\Ustartdisplaymath    \mathstartdisplaymath % otherwise conflict
% \aliased\let\Ustartmath           \mathstartmath        % idem
% \aliased\let\Ustartmathmode       \mathstartmathmode    % idem
% \aliased\let\Ustopdisplaymath     \mathstopdisplaymath  % idem
% \aliased\let\Ustopmath            \mathstopmath         % idem
% \aliased\let\Ustopmathmode        \mathstopmathmode     % idem

%D Also better here:

\permanent\cdef\wildcardsymbol{*}
%permanent\cdef\plussymbol    {+}
%permanent\cdef\minussymbol   {-}

%D For a while we will keep these useless numbers as for instance tikz checks for them:

\permanent\immutable\integerdef\eTeXversion    2
\permanent          \cdef      \eTeXrevision {.2} % there is a funny period here

% %D Just in case users use this (or some styles still have it):

\aliased\let\immediateassigned\localcontrolled

%D Needed (maybe no longer the multiple next ones):

\mutable\let\nexttoken   \relax
\mutable\let\next        \relax
\mutable\let\nextnext    \relax
\mutable\let\nextnextnext\relax

%D For now here: will get a proper solution

% \pushoverloadmode
% \mutable\let\par\par
% \popoverloadmode

%D Often used as cs key:

\mutable\let\on  \relax % \empty
\mutable\let\of  \relax % \empty
\mutable\let\and \relax % \empty
\mutable\let\with\relax % \empty

\mutable\lettonothing\nop % used often
\mutable\lettonothing\yes % used often

%D Another experiment. This one will go away as we dropped the aliases
%D in \type {syst-obs.mkxl}).

\def\syst_suggested_alias#1#2% expandable
  {\beginlocalcontrol
   \writestatus{suggestion}{use \string#2 instead of \string#1}%
   \enforced\glet#1#2%
   \endlocalcontrol
   #2}

\permanent\protected\def\suggestedalias#1#2% old new
  {\ifflags#2\permanent\permanent\fi % the rest is taken with the copy
   \def#1{\syst_suggested_alias#1#2}}

%D The next feature is experimental and could go into another system module, but it
%D feels more natural to have it here.
%D
%D \starttyping
%D \begingroup
%D   \newlocaldimen\mydimena % \mydimena1\onepoint
%D   \newlocaldimen\mydimenb % \mydimenb2\onepoint
%D   \begingroup
%D     \newlocaldimen\mydimena % \mydimena3\onepoint
%D     \newlocaldimen\mydimenb % \mydimenb4\onepoint
%D     \newlocaldimen\mydimenc % \mydimenc5\onepoint
%D   \endgroup
%D \endgroup
%D
%D \begingroup
%D   \setlocaldimen\mydimena 1\onepoint
%D   \setlocaldimen\mydimenb 2\onepoint
%D   \begingrou
%D     \setlocaldimen\mydimena 3\onepoint
%D     \setlocaldimen\mydimenb 4\onepoint
%D     \setlocaldimen\mydimenc 5\onepoint
%D   \endgroup
%D \endgroup
%D \stoptyping
%D
%D There is a little extra overhead in the overload protection but not that much.

% integerdef is local

\permanent\countdef\c_syst_local_count  = 231 \c_syst_local_count  = \c_syst_max_allocated_count
\permanent\countdef\c_syst_local_dimen  = 232 \c_syst_local_dimen  = \c_syst_max_allocated_dimen
\permanent\countdef\c_syst_local_skip   = 233 \c_syst_local_skip   = \c_syst_max_allocated_skip
\permanent\countdef\c_syst_local_muskip = 234 \c_syst_local_muskip = \c_syst_max_allocated_muskip
\permanent\countdef\c_syst_local_box    = 235 \c_syst_local_box    = \c_syst_max_allocated_box
\permanent\countdef\c_syst_local_toks   = 236 \c_syst_local_toks   = \c_syst_max_allocated_toks
\permanent\countdef\c_syst_local_read   = 237 \c_syst_local_read   = \c_syst_max_allocated_read
\permanent\countdef\c_syst_local_write  = 238 \c_syst_local_write  = \c_syst_max_allocated_write
\permanent\countdef\c_syst_local_float  = 239 \c_syst_local_float  = \c_syst_max_allocated_float

\immutable\c_syst_max_allocated_count  \numexpr\c_syst_max_allocated_count   :2\relax % floor
\immutable\c_syst_max_allocated_dimen  \numexpr\c_syst_max_allocated_dimen   :2\relax % floor
\immutable\c_syst_max_allocated_skip   \numexpr\c_syst_max_allocated_skip    :2\relax % floor
\immutable\c_syst_max_allocated_muskip \numexpr\c_syst_max_allocated_muskip  :2\relax % floor
\immutable\c_syst_max_allocated_toks   \numexpr\c_syst_max_allocated_toks    :2\relax % floor
\immutable\c_syst_max_allocated_box    \numexpr\c_syst_max_allocated_box     :2\relax % floor
\immutable\c_syst_max_allocated_read   \numexpr\c_syst_max_allocated_read    :2\relax % floor
\immutable\c_syst_max_allocated_write  \numexpr\c_syst_max_allocated_write   :2\relax % floor
\immutable\c_syst_max_allocated_float  \numexpr\c_syst_max_allocated_float   :2\relax % floor

\permanent\protected\def\syst_local_overflow#1#2{\writestatus\m!system{no room for local #1 \string#2}\wait}

\permanent\protected\def\setnewlocalcount#1%
  {\ifnum\c_syst_local_count>\c_syst_max_allocated_count
     \advanceby\c_syst_local_count\minusone
     \overloaded\frozen\countdef#1\c_syst_local_count
   \else
     \syst_local_overflow_count#1%
   \fi
   #1}

\permanent\protected\def\setnewlocaldimen#1%
  {\ifnum\c_syst_local_dimen>\c_syst_max_allocated_dimen
     \advanceby\c_syst_local_dimen\minusone\dimendef#1\c_syst_local_dimen
   \else
     \syst_local_overflow_dimen#1%
   \fi
   #1}

\permanent\protected\def\setnewlocalskip#1%
  {\ifnum\c_syst_local_skip>\c_syst_max_allocated_skip
     \advanceby\c_syst_local_skip\minusone
     \overloaded\frozen\skipdef#1\c_syst_local_skip
   \else
     \syst_local_overflow_skip#1%
   \fi
   #1}

\permanent\protected\def\setnewlocalmuskip#1%
  {\ifnum\c_syst_local_muskip>\c_syst_max_allocated_muskip
     \advanceby\c_syst_local_muskip\minusone
     \overloaded\frozen\muskipdef#1\c_syst_local_muskip
   \else
     \syst_local_overflow_muskip#1%
   \fi
   #1}

\permanent\protected\def\setnewlocaltoks#1%
  {\ifnum\c_syst_local_toks>\c_syst_max_allocated_toks
     \advanceby\c_syst_local_toks\minusone
     \overloaded\frozen\toksdef#1\c_syst_local_toks
   \else
     \syst_local_overflow_toks#1%
   \fi
   #1}

\permanent\protected\def\setnewlocalbox#1%
  {\ifnum\c_syst_local_box>\c_syst_max_allocated_box
     \advanceby\c_syst_local_box\minusone
     \overloaded\frozen\integerdef#1\c_syst_local_box
   \else
     \syst_local_overflow_box#1%
   \fi
   \setbox#1}

\permanent\protected\def\newlocalread#1% so no set
  {\ifnum\c_syst_local_read>\c_syst_max_allocated_read
     \advanceby\c_syst_local_read\minusone
     \overloaded\frozen\integerdef#1\c_syst_local_read
   \else
     \syst_local_overflow_read#1%
   \fi}

\permanent\protected\def\newlocalwrite#1% so no set
  {\ifnum\c_syst_local_write>\c_syst_max_allocated_write
     \advanceby\c_syst_local_write\minusone
     \overloaded\frozen\integerdef#1\c_syst_local_write
   \else
     \syst_local_overflow_write#1%
   \fi}

\permanent\protected\def\newlocalfloat#1% so no set
  {\ifnum\c_syst_local_float>\c_syst_max_allocated_float
     \advanceby\c_syst_local_float\minusone
     \overloaded\frozen\integerdef#1\c_syst_local_float
   \else
     \syst_local_overflow_float#1%
   \fi}

%D The error handlers:

\permanent\protected\def\syst_local_overflow_count {\syst_local_overflow {count}}
\permanent\protected\def\syst_local_overflow_dimen {\syst_local_overflow {dimen}}
\permanent\protected\def\syst_local_overflow_skip  {\syst_local_overflow  {skip}}
\permanent\protected\def\syst_local_overflow_muskip{\syst_local_overflow{muskip}}
\permanent\protected\def\syst_local_overflow_toks  {\syst_local_overflow  {toks}}
\permanent\protected\def\syst_local_overflow_box   {\syst_local_overflow   {box}}
\permanent\protected\def\syst_local_overflow_read  {\syst_local_overflow  {read}}
\permanent\protected\def\syst_local_overflow_write {\syst_local_overflow {write}}
\permanent\protected\def\syst_local_overflow_float {\syst_local_overflow {float}}

%D We sacrifice a little performance but save some tokens by not defining these
%D completely:

\permanent\protected\def\newlocalcount #1{\setnewlocalcount #1\zerocount }
\permanent\protected\def\newlocaldimen #1{\setnewlocaldimen #1\zeropoint }
\permanent\protected\def\newlocalskip  #1{\setnewlocalskip  #1\zeroskip  }
\permanent\protected\def\newlocalmuskip#1{\setnewlocalmuskip#1\zeromuskip}
\permanent\protected\def\newlocaltoks  #1{\setnewlocaltoks  #1\emptytoks }
\permanent\protected\def\newlocalbox   #1{\setnewlocalbox   #1\emptybox  }

%D Let's be detailed: grouplevel:inputlevel:catcodetable (bits 1 2 4)

\tracinglevels\plusthree

\permanent\protected\def\tracingcatcodes{\tracinglevels\plusseven}

%D We just report duplicate patterns being ignored:

\tracinghyphenation\plusone

%D Sometimes nicer:

\let\ifexpression\ifnumexpression

%D So we register implicit (newline) pars. When we set bit 2 and define a macro
%D with \type {\par} as delimiter, an explicit \par is expected and newline pars
%D are then part of the argument.

% newline :
%     bit 1: text
%         set  : <end_paragraph_cmd,new_line_end_paragraph_code>
%         unset: par_loc
%     bit 2: macro
%         set  : par_command_match
%         unset: use token
%     bit 3: go on
%         set  : don't increment loc
%         unset: increment loc
%
%     default:
%         inject : par_loc
%         macro  : use token
%         input  : increment loc
%
%     only      bit 1 set: macros work with \par as normal
%     when also bit 2 set: embedded empty lines grabbed too

\autoparagraphmode\plusone

%D The same values apply as \type {\shownodedetails}. When set the box is shown in
%D the the log file but that is pretty slow when we have much detail.

\tracingfullboxes\zerocount

%D We don't do this:

% \setdefaultmathcodes % initializes traditional codes

%D Catch

\ifdefined\mathpunctuation \else \let\mathpunctuation\mathpunct \fi % fixed in 2.10.06

%D We keep this for a while (suggested correction by KB):

\ifdefined\linebreakcriterion   \aliased\let\linebreakcriterium   \linebreakcriterion   \fi
\ifdefined\ignoredepthcriterion \aliased\let\ignoredepthcriterium \ignoredepthcriterion \fi

%D Special purpose:

\permanent\protected\def\if_false{\iffalse}
\permanent\protected\def\if_true {\iftrue}

%D Pseudo primitives. We now have endless loops so these are kind of obsolete
%D and will be removed some day!

\permanent\untraced\def\localcontrolledrepeating{\localcontrolledrepeat\maxcount}
\permanent\untraced\def\expandedrepeating       {\expandedrepeat       \maxcount}
\permanent\untraced\def\unexpandedrepeating     {\unexpandedrepeat     \maxcount}

\aliased\let\lastchkinteger\lastchknumber
\aliased\let\ifchkinteger  \ifchknumber

%D Let's be neat:

\permanent\untraced\def\userunitcode#1#2%
  {\numexpr(`#1-\alowercaseasciicode)*26+(`#2-\alowercaseasciicode)\relax}

\permanent\protected\def\newuserunit#1#2#3%
  {\associateunit#1=\numexpr(`#2-\alowercaseasciicode)*26+(`#3-\alowercaseasciicode)\relax}

\newdimension\piunit \piunit 3.1415926535898pt % 205887sp % for Mikael
\newdimension\ftunit \ftunit 12in              % foot     % for Alan
\newdimension\uunit  \uunit  1cm

\newuserunit \piunit pi
\newuserunit \ftunit ft
\newuserunit \uunit  uu  % vu is we want to be Knuth's basic compatible

%D We enable special escapes:

\parametermode \plusone

%D While the buildbot is down:

\ifdefined\protecteddetokenize\else
    \aliased\let\protecteddetokenize\detokenize
\fi

%D Consistency:

\let\boxwidth \wd
\let\boxheight\ht
\let\boxdepth \dp

%D Because we use the variable class:

\variablefam-\plustwo % obey class 7

%D For now here:

% \fitnessdemerits 5
%     99          \plusone
%     12          \plusone
%     \zerocount  \plusone
%     12          \plusone
%     99          \plusone

\pushoverloadmode

%D We now group these penalties in specification sets. For practical purposes we no longer
%D use the built in single penalties but map them onto the plurals. We also redefine the
%D display math penalties but we never use these. We keep the \type {\normal..} ones! These
%D might move to \type {toks-prm.mkxl}.

\permanent\protected\untraced\def\widowpenalty       {\widowpenalties       \minusone} % once
\permanent\protected\untraced\def\clubpenalty        {\clubpenalties        \minusone} % once
\permanent\protected\untraced\def\displaywidowpenalty{\displaywidowpenalties\minusone} % once
\permanent\protected\untraced\def\brokenpenalty      {\brokenpenalties      \minusone} % singular anyway
\permanent\protected\untraced\def\orphanpenalty      {\orphanpenalties      \minusone} % once
\permanent\protected\untraced\def\toddlerpenalty     {\toddlerpenalties     \minusone} % once
\permanent\protected\untraced\def\interlinepenalty   {\interlinepenalties   \plusone } % repeats
\permanent\protected\untraced\def\adjdemerits        {\adjacentdemerits     \minusone}

\popoverloadmode

%D See \type {mult-sys.mkxl} for an explanation of \type {\blankspace} definition. This one
%D is more explanatory when ending (for instance) a keyword.

\ifdefined\relaxedspace
  % could be a primitive
\else
    \expanded{\permanent\let\noexpand\relaxedspace=\space\space}
\fi

%D Tracing:

% tracing:

% \untraced\permanent\protected\def\thespecification#1%
%   {\the#1\zerocount :%
%    \ifcase#1\maxcount
%      \expandedloop \plusone #1\zerocount \plusone { %
%          \the#1\currentloopiterator
%      }%
%    \else
%      \expandedloop \plusone #1\zerocount \plusone { %
%          \the#1\currentloopiterator\plusone,%
%          \the#1\currentloopiterator\plustwo
%      }%
%    \fi}

\untraced\permanent\protected\def\thespecification#1%
  {\the\specificationcount#1:%
   \ifbitwiseand\specificationoptions#1\plustwo
     \expandedloop\plusone\specificationcount#1\plusone{ %
         \the\specificationfirst #1\currentloopiterator,%
         \the\specificationsecond#1\currentloopiterator
     }%
   \else
     \expandedloop\plusone\specificationcount#1\plusone{ %
         \the\specificationfirst #1\currentloopiterator
     }%
   \fi}

% \edef\foo{\expand\thespecification\parshape}

\ifdefined\parshapelength \else

    \protected\untraced\def\parshapelength{\specificationcount \parshape}
    \protected\untraced\def\parshapeindent{\specificationfirst \parshape}
    \protected\untraced\def\parshapewidth {\specificationsecond\parshape}

    \aliased\let\parshapedimen\parshapewidth % weird etex name

\fi

%D Only when testing non \CONTEXT\ usage:

% \let\saved_reqno \eqno
% \let\saved_leqno \leqno

%D This is a way to protect the \type {\voidbox} reference:

% \meaningfull\voidbox \par % global permanent constant integer
% \voidbox <number>
% \meaningfull\voidbox \par % global constant integer (we loose permanent)

% \pushoverloadmode
%     \scratchcounter\voidbox           % to be used box register number
%     \let\voidbox\undefined            % so that we can redefine as counter
%     \newcount\voidbox                 % now it's a counter as reference
%     \immutable\voidbox\scratchcounter % fixed index of the used register
% \popoverloadmode

% These were "internal counters" and are now "break properties" and also are
% bound to nesting levels. We might replace them with a backtrack over the list
% variant (because they get reset when a new list is entered). The main problem
% is that things can happen between the previous and current paragraph so these
% states are sort of unreliable.

\aliased\let\lastlinewidth\breaklastlinewidth
\aliased\let\lastlinecount\breaklastlinecount

\protect \endinput

% performance experiment:
%
% \def\footest
%   {\iftrue
%      \relax\relax\relax\relax\relax\relax\relax\relax\relax\relax
%      \relax\relax\relax\relax\relax\relax\relax\relax\relax\relax
%    % \quitfinow\ignorerest % n=11000000 : 0.304 -> 0.224
%    \else
%      \relax\relax\relax\relax\relax\relax\relax\relax\relax\relax
%      \relax\relax\relax\relax\relax\relax\relax\relax\relax\relax
%    \fi}
