syst-ini.mkiv / last modification: 2020-01-30 14:16
%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 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 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 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.

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

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

%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

\chardef\statuswrite   128

%D Initialization of primitives.

\directlua {
    local primitives = tex.extraprimitives("core","tex","etex","luatex")

  % table.sort(primitives)

    tex.enableprimitives("",      primitives)

    function tex.enableprimitives() end

\def\space{ }

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

\expandafter\def\csname\Uchar\tabasciicode      \endcsname {\ }   % \def\^^I{\ }    tab
\expandafter\def\csname\Uchar\formfeedasciicode \endcsname {\par} % \def\^^L{\par}  formfeed
\expandafter\def\csname\Uchar\endoflineasciicode\endcsname {\ }   % \def\^^M{\ }    return

%D For now:

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

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

     {\catcode\atsignasciicode         \the\catcode\atsignasciicode         \relax
      \catcode\questionmarkasciicode   \the\catcode\questionmarkasciicode   \relax
      \catcode\underscoreasciicode     \the\catcode\underscoreasciicode     \relax
   \catcode\atsignasciicode         \lettercatcode
   \catcode\questionmarkasciicode   \lettercatcode
   \catcode\underscoreasciicode     \lettercatcode}



%D Some pretty important definitions:


%D \macros
%D   {normalbgroup,normalgroup}
%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 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 We reserve some registers for special (management) purposes:

%   0 -  20 : scratch
%  21 - 127 : internal
% 128 - 254 : inserts
% 255       : page
% 256 -     : user

\countdef   \c_syst_min_allocated_register   =    52 \c_syst_min_allocated_register   =   256 % can change
\countdef   \c_syst_max_allocated_register   =    53 \c_syst_max_allocated_register   = 32767
\countdef   \c_syst_min_allocated_read       =    54 \c_syst_min_allocated_read       =    -1
\countdef   \c_syst_max_allocated_read       =    55 \c_syst_max_allocated_read       =    16
\countdef   \c_syst_min_allocated_language   =    56 \c_syst_min_allocated_language   =     0
\countdef   \c_syst_max_allocated_language   =    57 \c_syst_max_allocated_language   =   255
\countdef   \c_syst_min_allocated_insert     =    58 \c_syst_min_allocated_insert     =   128
\countdef   \c_syst_max_allocated_insert     =    59 \c_syst_max_allocated_insert     =   254
\countdef   \c_syst_min_allocated_family     =    60 \c_syst_min_allocated_family     =   128
\countdef   \c_syst_max_allocated_family     =    61 \c_syst_max_allocated_family     =   255
\countdef   \c_syst_min_allocated_attribute  =    62 \c_syst_min_allocated_attribute  =  1024 % 0-1023 : private
\countdef   \c_syst_min_allocated_write      =    63 \c_syst_min_allocated_write      =     0
\countdef   \c_syst_max_allocated_write      =    64 \c_syst_max_allocated_write      =   127

\countdef   \c_syst_last_allocated_count     =    32 \c_syst_last_allocated_count     = \c_syst_min_allocated_register
\countdef   \c_syst_last_allocated_dimen     =    33 \c_syst_last_allocated_dimen     = \c_syst_min_allocated_register
\countdef   \c_syst_last_allocated_skip      =    34 \c_syst_last_allocated_skip      = \c_syst_min_allocated_register
\countdef   \c_syst_last_allocated_muskip    =    35 \c_syst_last_allocated_muskip    = \c_syst_min_allocated_register
\countdef   \c_syst_last_allocated_box       =    36 \c_syst_last_allocated_box       = \c_syst_min_allocated_register
\countdef   \c_syst_last_allocated_toks      =    37 \c_syst_last_allocated_toks      = \c_syst_min_allocated_register
\countdef   \c_syst_last_allocated_read      =    38 \c_syst_last_allocated_read      = \c_syst_min_allocated_read
\countdef   \c_syst_last_allocated_write     =    39 \c_syst_last_allocated_write     = \c_syst_min_allocated_write
\countdef   \c_syst_last_allocated_marks     =    40 \c_syst_last_allocated_marks     = \c_syst_min_allocated_register
\countdef   \c_syst_last_allocated_language  =    41 \c_syst_last_allocated_language  = \c_syst_min_allocated_language    % not used in context
\countdef   \c_syst_last_allocated_insertion =    42 \c_syst_last_allocated_insertion = \c_syst_min_allocated_insert
\countdef   \c_syst_last_allocated_family    =    43 \c_syst_last_allocated_family    = \c_syst_min_allocated_family      % not used in context
\countdef   \c_syst_last_allocated_attribute =    44 \c_syst_last_allocated_attribute = \c_syst_min_allocated_attribute   % not used in context

\countdef   \c_syst_min_counter_value =   125 \c_syst_min_counter_value = -"7FFFFFFF % beware, we use index 125 at the lua end
\countdef   \c_syst_max_counter_value =   126 \c_syst_max_counter_value =  "7FFFFFFF % beware, we use index 126 at the lua end

\countdef   \zerocount                =   120 \zerocount                =  0
\countdef   \plusone                  =   121 \plusone                  =  1
\countdef   \minusone                 =   122 \minusone                 = -1

\countdef   \normalpagebox            =   127 \normalpagebox            = 255  % hardcoded in pdftex/xetex

% Only to be used by developers in very special cases!

% \def\lastallocatedcount    {\the\c_syst_last_allocated_count}
% \def\lastallocateddimen    {\the\c_syst_last_allocated_dimen}
% \def\lastallocatedskip     {\the\c_syst_last_allocated_skip}
% \def\lastallocatedmuskip   {\the\c_syst_last_allocated_muskip}
% \def\lastallocatedbox      {\the\c_syst_last_allocated_dimen}
% \def\lastallocatedtoks     {\the\c_syst_last_allocated_toks}
% \def\lastallocatedattribute{\the\c_syst_last_allocated_attribute}

% A few traditional allocations (these might go):

\countdef \count@   255 % hm, used in \newif .. todo: replace it there
\dimendef \dimen@     0
\dimendef \dimen@i    1 % global only
\dimendef \dimen@ii   2

%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.

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

%D The allocators share a common helper macro.

\normalprotected\def\newcount   {\syst_basics_allocate\c_syst_last_allocated_count    \count   \countdef   \c_syst_max_allocated_register}
\normalprotected\def\newdimen   {\syst_basics_allocate\c_syst_last_allocated_dimen    \dimen   \dimendef   \c_syst_max_allocated_register}
\normalprotected\def\newskip    {\syst_basics_allocate\c_syst_last_allocated_skip     \skip    \skipdef    \c_syst_max_allocated_register}
\normalprotected\def\newmuskip  {\syst_basics_allocate\c_syst_last_allocated_muskip   \muskip  \muskipdef  \c_syst_max_allocated_register}
\normalprotected\def\newbox     {\syst_basics_allocate\c_syst_last_allocated_box      \box     \mathchardef\c_syst_max_allocated_register}
\normalprotected\def\newtoks    {\syst_basics_allocate\c_syst_last_allocated_toks     \toks    \toksdef    \c_syst_max_allocated_register}
\normalprotected\def\newread    {\syst_basics_allocate\c_syst_last_allocated_read     \read    \chardef    \c_syst_max_allocated_read}
\normalprotected\def\newwrite   {\syst_basics_allocate\c_syst_last_allocated_write    \write   \chardef    \c_syst_max_allocated_write}
\normalprotected\def\newmarks   {\syst_basics_allocate\c_syst_last_allocated_marks    \marks   \mathchardef\c_syst_max_allocated_register}
\normalprotected\def\newinsert  {\syst_basics_allocate\c_syst_last_allocated_insertion\insert  \chardef    \c_syst_max_allocated_insert}

%D We don't need these in \CONTEXT:

\normalprotected\def\newlanguage{\syst_basics_allocate\c_syst_last_allocated_language \language\chardef    \c_syst_max_allocated_language}
\normalprotected\def\newfamily  {\syst_basics_allocate\c_syst_last_allocated_family   \fam     \chardef    \c_syst_max_allocated_family}



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

\normalprotected\def\newconstant       #1{\ifdefined#1\let#1\undefined\fi\newcount#1}
\normalprotected\def\setnewconstant    #1{\ifdefined#1\let#1\undefined\fi\newcount#1#1} % just a number
\normalprotected\def\setconstant         {}                                             % dummy, no checking, so it warns

% maybe setconstant with check

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

\newlinechar\newlineasciicode  \edef\outputnewlinechar{\Uchar\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.

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

\def\syst_basics_allocate_yes#1#2#3#4#5% last class method max name
     \writestatus{warning}{no room for \string#2\space \string#5\space (max: \number#4)}%

\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

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

\normalprotected\def\newbox  {\syst_basics_allocate\c_syst_last_allocated_box  \box  \chardef\c_syst_max_allocated_register}

%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.

\let\attributeunsetvalue\c_syst_min_counter_value % used to be \minusone


%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. It's a trick to trick to force
%D strings. This macro is never used in \CONTEXT.


%D \macros
%D   {scratchcounter,
%D    scratchdimen,scratchskip,scratchmuskip,
%D    scratchbox,
%D    scratchtoks}
%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.

\newcount \scratchcounter  \newcount \globalscratchcounter  \newcount \privatescratchcounter
\newdimen \scratchdimen    \newdimen \globalscratchdimen    \newdimen \privatescratchdimen
\newskip  \scratchskip     \newskip  \globalscratchskip     \newskip  \privatescratchskip
\newmuskip\scratchmuskip   \newmuskip\globalscratchmuskip   \newmuskip\privatescratchmuskip
\newtoks  \scratchtoks     \newtoks  \globalscratchtoks     \newtoks  \privatescratchtoks
\newbox   \scratchbox      \newbox   \globalscratchbox      \newbox   \privatescratchbox

\newcount\scratchcounterone  \newcount\scratchcountertwo  \newcount\scratchcounterthree
\newdimen  \scratchdimenone  \newdimen  \scratchdimentwo  \newdimen  \scratchdimenthree
\newskip    \scratchskipone  \newskip    \scratchskiptwo  \newskip    \scratchskipthree
\newmuskip\scratchmuskipone  \newmuskip\scratchmuskiptwo  \newmuskip\scratchmuskipthree
\newtoks    \scratchtoksone  \newtoks    \scratchtokstwo  \newtoks    \scratchtoksthree
\newbox      \scratchboxone  \newbox      \scratchboxtwo  \newbox      \scratchboxthree

\newcount\scratchcounterfour \newcount\scratchcounterfive \newcount\scratchcountersix
\newdimen  \scratchdimenfour \newdimen  \scratchdimenfive \newdimen  \scratchdimensix
\newskip    \scratchskipfour \newskip    \scratchskipfive \newskip    \scratchskipsix
\newmuskip\scratchmuskipfour \newmuskip\scratchmuskipfive \newmuskip\scratchmuskipsix
\newtoks    \scratchtoksfour \newtoks    \scratchtoksfive \newtoks    \scratchtokssix
\newbox      \scratchboxfour \newbox      \scratchboxfive \newbox      \scratchboxsix


%D \macros
%D   {tempstring}


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












%D More allocations:

\newskip  \zeroskip      \zeroskip      0pt plus 0pt minus 0pt
\newdimen \zeropoint     \zeropoint     0pt
\newdimen \onepoint      \onepoint      1pt
\newdimen \halfapoint    \halfapoint    0.5pt
\newdimen \maxdimen      \maxdimen      16383.99999pt % 1073741823sp
\newcount \maxcount      \maxcount      2147483647
\newdimen \onebasepoint  \onebasepoint  1bp
\newdimen \scaledpoint   \scaledpoint   1sp
\newdimen \thousandpoint \thousandpoint 1000pt
\newmuskip\zeromuskip    \zeromuskip    0mu
\newmuskip\onemuskip     \onemuskip     1mu

\newmuskip\muquad        \muquad        18mu

\let\points   \onepoint

\newtoks \emptytoks

%D And even more: (todo: countdefs 60+)

%newcount\minusone \minusone    -1
\newcount\minustwo \minustwo    -2
%chardef \zerocount              0
%chardef \plusone                1
\chardef \plustwo                2
\chardef \plusthree              3
\chardef \plusfour               4
\chardef \plusfive               5
\chardef \plussix                6
\chardef \plusseven              7
\chardef \pluseight              8
\chardef \plusnine               9
\chardef \plusten               10
\chardef \plussixteen           16
\chardef \plusfifty             50
\chardef \plushundred          100
\chardef \plusonehundred       100
\chardef \plustwohundred       200
\chardef \plusfivehundred      500
\chardef \pluscxxvii           127
\chardef \pluscxxviii          128
\chardef \pluscclv             255
\chardef \pluscclvi            256
\chardef \plusthousand        1000
\chardef \plustenthousand    10000
\chardef \plustwentythousand 20000
\chardef \medcard            32768
\chardef \maxcard            65536 % pdftex has less mathchars
\chardef \maxcardminusone    65535

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

\let\singleexpandafter \expandafter

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

\newbox\voidbox  % public


\def\emptybox {\box   \voidbox}  % used in initializations so no attributes
\def\emptyvbox{\normalvpack{}}   % no copy as we need to set attributes
\def\emptyhbox{\normalhpack{}}   % no copy as we need to set attributes

\let\leavevmode\unvoidbox % we prefer to use \dontleavehmode

%D \macros
%D   {dontcomplain}
%D We need this one soon:

   \hfuzz   \maxdimen
   \vfuzz   \maxdimen}

%D Some expected plain variants follow. We don't reuse registers because we
%D don't want clashes.

\let     \p@             \onepoint
\let     \m@ne           \minusone
\let     \z@             \zeropoint
\let     \@ne            \plusone
\let     \tw@            \plustwo
\let     \thr@@          \plusthree
\let     \sixt@@n        \plussixteen
\let     \@cclv          \pluscclv
\let     \@cclvi         \pluscclvi
\let     \voidb@x        \voidbox
\newtoks \toks@        % \scratchtoks

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

% \normalprotected\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.
% \normalprotected\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}
% \normalprotected\def\new_if_cs#1#2%
%   {\csname\expandafter\newif\csstring#1#2\endcsname}
% We wrap all into one macro:

   \expandafter\def\csname\expandafter\newif\csstring#1true\endcsname {\let#1\iftrue }%

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

%D Let's test this one:



%D A few shortcuts:

\normalprotected\def\udef {\normalprotected\def }

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


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

\chardef\bottomlevelgroupcode   =  0
\chardef\simplegroupcode        =  1
\chardef\hboxgroupcode          =  2
\chardef\adjustedhboxgroupcode  =  3
\chardef\vboxgroupcode          =  4
\chardef\vtopgroupcode          =  5
\chardef\aligngroupcode         =  6
\chardef\noaligngroupcode       =  7
\chardef\outputgroupcode        =  8
\chardef\mathgroupcode          =  9
\chardef\discretionarygroupcode = 10
\chardef\insertgroupcode        = 11
\chardef\vcentergroupcode       = 12
\chardef\mathchoicegroupcode    = 13
\chardef\semisimplegroupcode    = 14
\chardef\mathshiftgroupcode     = 15
\chardef\mathleftgroupcode      = 16
\chardef\vadjustgroupcode       = \insertgroupcode

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

\chardef\batchmodecode     = 0
\chardef\nonstopmodecode   = 1
\chardef\scrollmodecode    = 2
\chardef\errorstopmodecode = 3

%D Constants to be used with \type {\lastnodetype}. The \type {\lastnodetype}
%D primitive is \ETEX\ compliant. The valid range is still -1 .. 15 and glyph nodes
%D have number 0 (used to be char node) and ligature nodes are mapped to 7. That way
%D macro packages can use the same symbolic names as in traditional \ETEX. Keep in
%D mind that the internal node numbers are different and that there are more node
%D types that 15. The mode parameter forces \LUAMETATEX\ to use the real node
%D numbers.



  % \chardef\gluenodecode          = 12
  % \chardef\kernnodecode          = 13
  % \chardef\penaltynodecode       = 14


  % \chardef\charnodecode          =  0
  % \chardef\hlistnodecode         =  1
  % \chardef\vlistnodecode         =  2
  % \chardef\rulenodecode          =  3
  % \chardef\insertnodecode        =  4
  % \chardef\marknodecode          =  5
  % \chardef\adjustnodecode        =  6
  % \chardef\ligaturenodecode      =  7
  % \chardef\discretionarynodecode =  8
  % \chardef\whatsitnodecode       =  9
  % \chardef\mathnodecode          = 10
    \chardef\gluenodecode          = 11
    \chardef\kernnodecode          = 12
    \chardef\penaltynodecode       = 13
  % \chardef\unsetnodecode         = 14
  % \chardef\mathsnodecode         = 15


%D Constants to be used with \type {\currentiftype}. I wonder if we will ever
%D use these in \CONTEXT. We have a few more anyway and in \LUAMETATEX\ we
%D have different numbers. So for now let's just not define them. The
%D previously mentioned mode parameters enables the real numbers (we have
%D some more and they are therefore ordered differently).

% \chardef\charifcode     =  1
% \chardef\catifcode      =  2
% \chardef\numifcode      =  3
% \chardef\dimifcode      =  4
% \chardef\oddifcode      =  5
% \chardef\vmodeifcode    =  6
% \chardef\hmodeifcode    =  7
% \chardef\mmodeifcode    =  8
% \chardef\innerifcode    =  9
% \chardef\voidifcode     = 10
% \chardef\hboxifcode     = 11
% \chardef\vboxifcode     = 12
% \chardef\xifcode        = 13
% \chardef\eofifcode      = 14
% \chardef\trueifcode     = 15
% \chardef\falseifcode    = 16
% \chardef\caseifcode     = 17
% \chardef\definedifcode  = 18
% \chardef\csnameifcode   = 19
% \chardef\fontcharifcode = 20

%D Of course we want even bigger log files, so we copied this from the \ETEX\
%D source files.
%D When watching such logs, beware of nasty side effects of \type {\scantokens},
%D as in:
%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 In \LUATEX\ we have ways around this.

% no longer \errorstopmode cf. plain tex 3.141592653

  {\tracingonline    \plusone
   \tracingcommands  \plusthree
   \tracingstats     \plustwo
   \tracingpages     \plusone
   \tracingoutput    \plusone
   \tracinglostchars \plustwo
   \tracingmacros    \plustwo
   \tracingrestores  \plusone
   \showboxbreadth   \maxdimen
   \showboxdepth     \maxdimen
   \tracinggroups    \plusone
   \tracingifs       \plusone
   \tracingnesting   \plustwo
   \tracingassigns   \plustwo}

   \tracingonline    \zerocount}

  {\tracingassigns   \zerocount
   \tracingnesting   \zerocount
   \tracingifs       \zerocount
   \tracinggroups    \zerocount
   \showboxdepth     \plusthree
   \showboxbreadth   \plusfive
   \tracingrestores  \zerocount
   \tracingmacros    \zerocount
   \tracinglostchars \plusone
   \tracingoutput    \zerocount
   \tracingpages     \zerocount
   \tracingstats     \zerocount
   \tracingcommands  \zerocount
   \tracingonline    \zerocount}

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

\showboxdepth  \maxdimen

%D Just for tracing purposes we set:


%D Here we also save \type {\input}, more will be saved later.

\ifdefined\normalinput \else \let\normalinput\input \fi

%D We don't like outer commands, and we always want access to the original
%D \type {\input} primitive.

\let\normalouter\outer  \def\outer{} % no longer \relax

%D To circumvent dependencies, we can postpone certain initializations to
%D dumping time, by appending them to the \type {\everydump} token register.

\ifdefined\normaldump \else \let\normaldump\dump \fi



%D The same applies for the startup actions.

\ifdefined\normaleveryjob \else \let\normaleveryjob\everyjob \fi

\let\everyjob\relax \newtoks\everyjob


%D \macros
%D   {newconditional,
%D    settrue, setfalse,
%D    ifconditional,then}
%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 presented here. A conditional is
%D defined by:
%D \starttyping
%D \newconditional\doublesided
%D \setfalse
%D \stoptyping
%D Setting a conditional is done by \type{\settrue} and
%D \type{\setfalse}:
%D \starttyping
%D \settrue\doublesided
%D \setfalse
%D \stoptyping
%D while testing is accomplished by:
%D \starttyping
%D \ifconditional\doublesided  ... \else ... \fi
%D \setfalse
%D \stoptyping
%D We cannot use the simple scheme:
%D \starttyping
%D \def\settrue #1{\let#1=\iftrue}
%D \def\setfalse#1{\let#1=\iffalse}
%D \stoptyping
%D Such an implementation gives problems with nested conditionals. The next
%D implementation is about as fast and just as straightforward:

\let\conditionalfalse\plusone    % maybe we will have a dedicated count/chardef
\let\conditionaltrue \zerocount  % maybe we will have a dedicated count/chardef

\normalprotected\def\settrue #1{\let#1\conditionaltrue }

\normalprotected\def\settruevalue #1{\expandafter\let\csname#1\endcsname\conditionaltrue }

\let\ifconditional \ifcase

\let\then\relax % so that we can say: \ifnum1>2\then -)

\def\truecondition {\iftrue}

%D \macros
%D   {newmacro,setnewmacro,newfraction}
%D Let's be complete and also introduce some definers. These are not mandate
%D but handy for grepping.

\normalprotected\def\newmacro   #1{\let#1\empty}

\def\!!plusone  {1}


%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 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 \starttyping
%D \ifzeropt \somedimen ... \else ... \fi
%D \stoptyping


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

\newtoks\t_syst_toks_temp \let\m_syst_toks_temp\t_syst_toks_temp

\normalprotected\def\appendtotoks #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_append_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}}}

\normalprotected\def\globalappendtotoks #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_append_g \t_syst_toks_temp=}

\def\syst_toks_append_l {\normalexpanded{\m_syst_toks_temp{\the\m_syst_toks_temp\the\t_syst_toks_temp}}}

\def\syst_toks_append_g {\global\syst_toks_append_l }

\normalprotected\def\addtotoks      #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_add_l\let\next}


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

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

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


%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
% \normalprotected\def\endpar{\the\everyendpar\normalpar}
% \normalprotected\def\par   {\endpar}
% \normalprotected\def\reseteverypar
%   {\everypar   \emptytoks
%    \everyendpar\emptytoks}




%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}
\def\obeyspaces{\catcode\spaceasciicode    \activecatcode\letcharcode\spaceasciicode    \space}

% %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}.

\def\loop#1\repeat{\def\@@plnbody{#1}\@@plniterate} % might go

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

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

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


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


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

\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.

\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.

\newskip \bigskipamount    \bigskipamount   = 12pt plus 4pt minus 4pt
\newskip \medskipamount    \medskipamount   =  6pt plus 2pt minus 2pt
\newskip \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
%D right mode:

\normalprotected\def\vkern{\ifhmode\par           \fi\kern}

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

\newskip \hideskip   \hideskip  = -1000pt plus 1fill
\newskip \centering  \centering = 0pt plus 1000pt minus 1000pt

\def\hidewidth % for alignment entries that can stick out

\def\ialign % initialized \halign

\newcount \mscount

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

       \spanomit \advance\mscount\minusone

%D For the \LUATEX\ backend (overloaded anyway):


    \pdfvariable compresslevel       \plusthree
    \pdfvariable objcompresslevel    \plusone
  % \pdfvariable recompress          \zerocount
    \pdfvariable decimaldigits       \plussix
    \pdfvariable gamma               \plusthousand
    \pdfvariable imageresolution     300
    \pdfvariable imageapplygamma     \zerocount
    \pdfvariable imagegamma          2200
    \pdfvariable imagehicolor        \plusone
    \pdfvariable imageaddfilename    \plusone
    \pdfvariable pkresolution        1200
    \pdfvariable inclusioncopyfonts  \plusone
    \pdfvariable inclusionerrorlevel \zerocount
    \pdfvariable gentounicode        \plusone
    \pdfvariable omitcidset          \plusone
    \pdfvariable omitcharset         \plusone
    \pdfvariable pagebox             \zerocount
  % \pdfvariable majorversion      % \plusone
    \pdfvariable minorversion        \plusseven
    \pdfvariable uniqueresname       \zerocount
    \pdfvariable horigin             1in
    \pdfvariable vorigin             1in
  % \pdfvariable linkmargin          \zeropoint
  % \pdfvariable destmargin          \zeropoint
  % \pdfvariable threadmargin        \zeropoint
  % \pdfvariable xformmargin         \zeropoint
    \pdfvariable pkfixeddpi          \plusone
    \pdfvariable ignoreunknownimages \zerocount


\let\nopdfcompression        \relax
\let\maximumpdfcompression   \relax
\let\normalpdfcompression    \relax

    \outputmode \zerocount % we generate the format in this mode

%D Basic status stuff.


%D For those who expect this \unknown

\ifx\fmtname   \undefined \def\fmtname   {ConTeXt Initial TeX} \fi
\ifx\fmtversion\undefined \def\fmtversion{3.1415926}           \fi

%D A few bonus macros:


\ifdefined\texenginename \else

\ifdefined\texengineversion \else

\ifdefined\texenginefunctionality \else

%D We have no reason not to enable this:


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

\let\synctex\undefined \newcount\synctex \let\normalsynctex\synctex

%D We get rid of the funny \TEX\ offset defaults of one inch by setting them to zero.

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

    \let\pageleftoffset  \hoffset \let\normalpageleftoffset  \hoffset
    \let\pagerightoffset \hoffset \let\normalpagerightoffset \hoffset
    \let\pagetopoffset   \voffset \let\normalpagetopoffset   \voffset
    \let\pagebottomoffset\voffset \let\normalpagebottomoffset\voffset

%D Handy (this will change, again).

\ifdefined\suppresslongerror        \suppresslongerror        \plusone  \fi
\ifdefined\suppressoutererror       \suppressoutererror       \plusone  \fi
\ifdefined\suppressmathparerror     \suppressmathparerror     \plusone  \fi
\ifdefined\suppressifcsnameerror    \suppressifcsnameerror    \plusone  \fi

\let     \suppresslongerror        \relax
\newcount\suppresslongerror                           \let\normalsuppresslongerror        \suppresslongerror
\let     \suppressoutererror       \suppresslongerror \let\normalsuppressoutererror       \suppresslongerror
\let     \suppressmathparerror     \suppresslongerror \let\normalsuppressmathparerror     \suppresslongerror
\let     \suppressifcsnameerror    \suppresslongerror \let\normalsuppressifcsnameerror    \suppresslongerror
\let     \suppressfontnotfounderror\suppresslongerror \let\normalsuppressfontnotfounderror\suppresslongerror

\matheqnogapstep\zerocount % for now

%D Experiment:

\ifdefined\frozen \else \def\frozen{} \let\normalfrozen\frozen \fi

%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{\number    \c_syst_helpers_n_of_namespaces>>}
%def\v_interfaces_prefix_template_system{\characters\c_syst_helpers_n_of_namespaces>>} % no \characters yet

  {\ifcsname ??#1\endcsname
     \immediate\write\statuswrite{fatal error: duplicate system namespace '#1'}%
     \expandafter\edef\csname ??#1\endcsname{\v_interfaces_prefix_template_system}%

%D It makes more sense to have these here:

\let\normalsuperscript  \Usuperscript
\let\normalsubscript    \Usubscript
\let\normalnosubscript  \Unosubscript
\let\normalstartimath   \Ustartmath
\let\normalstopimath    \Ustopmath
\let\normalstartdmath   \Ustartdisplaymath
\let\normalstopdmath    \Ustopdisplaymath

%D Also better here:


%D Get rid of this, we don't need it. If we ever need to check the order we will
%D use proper symbolic constants.

\let\eTeXglueshrinkorder \undefined

\protect \endinput