summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/syst-ini.mkiv
blob: 4b5cc616b46de23b6e7b11aa35a6d1d433514aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
%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 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.

%catcode`\^^@   =  9 % ascii null is ignored
%catcode`\\     =  0 % backslash is TeX escape character

\catcode`\{     =  1 % left brace is begin-group character
\catcode`\}     =  2 % right brace is end-group character
\catcode`\$     =  3 % dollar sign is math shift
\catcode`\&     =  4 % ampersand is alignment tab
\catcode`\#     =  6 % hash mark is macro parameter character
\catcode`\^     =  7 % circumflex and uparrow are for superscripts
\catcode`\_     =  8 % underline and downarrow are for subscripts
\catcode`\^^I   = 10 % ascii tab is a blank space

%catcode`\^^M   =  5 % ascii return is end-line
%catcode`\%     = 14 % percent sign is comment character
%catcode`\      = 10 % ascii space is blank space
%catcode`\^^?   = 15 % ascii delete is invalid

\catcode`\~     = 13 % tilde is active
\catcode`\^^L   = 13 % ascii form-feed

%catcode`\A     = 11
%.......
%catcode`\Z     = 11

%catcode`\a     = 11
%.......
%catcode`\z     = 11

\def ^^L{\par}
\def\^^M{\   } % control <return> = control <space>
\def\^^I{\   } % same for <tab>

%D In \CONTEXT, we simply ignore end||of||file tokens:

\catcode`\^^Z=9

%D It makes sense to know what engine we're running so let's try to deduce it.

\chardef\unknownengine = 0
\chardef\pdftexengine  = 1
\chardef\xetexengine   = 2
\chardef\luatexengine  = 3

\chardef\statuswrite   = 128

\ifx\directlua\undefined
    \ifx\XeTeXversion\undefined
        \ifx\pdftexversion\undefined
            \let\texengine\unknownengine
        \else
            \let\texengine\pdftexengine
        \fi
    \else
        \let\texengine\xetexengine
    \fi
\else
    \let\texengine\luatexengine
\fi

\ifnum\texengine=\luatexengine
    % for historic reasons we keep some mkii code around
\else
    \immediate\write\statuswrite{>>>}
    \immediate\write\statuswrite{>>> only luatex is supported}
    \immediate\write\statuswrite{>>>}
    \let\dump\relax
    \expandafter\end
\fi

%D Initialization of primitives.

\directlua 0 { % this info is stored in the format
    lua.name[0] = "main ctx instance"

    local coreprimitives   = tex.extraprimitives("core")
    local texprimitives    = tex.extraprimitives("tex")
    local etexprimitives   = tex.extraprimitives("etex")
    local luatexprimitives = tex.extraprimitives("luatex")

    tex.enableprimitives("",etexprimitives)
    tex.enableprimitives("",luatexprimitives)

    tex.enableprimitives("normal",coreprimitives)
    tex.enableprimitives("normal",texprimitives)
    tex.enableprimitives("normal",etexprimitives)
    tex.enableprimitives("normal",luatexprimitives)
}

%D \ETEX\ has a not so handy way of telling you the version number, i.e. the revision
%D number has a period in it:

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

\mathchardef\etexversion = \numexpr\eTeXversion*100+\expandafter\gobbleoneargument\eTeXrevision\relax

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

\def\unprotect
  {\edef\protect
     {\catcode`@=\the\catcode`@\relax
      \catcode`?=\the\catcode`?\relax
      \catcode`!=\the\catcode`!\relax
      \catcode`_=\the\catcode`_\relax
      \let\protect\relax}%
   \catcode`@=11
   \catcode`?=11
   \catcode`!=11
   \catcode`_=11 }

\let\protect\relax

\unprotect

%D Some pretty important definitions:

\let\bgroup={
\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:

%   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_max_allocated_insert     =    58 \c_syst_max_allocated_insert     =   254
\countdef   \c_syst_min_allocated_insert     =    59 \c_syst_min_allocated_insert     =   128
\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 % 127-1023 : private
\countdef   \c_syst_min_allocated_write      =    63 \c_syst_min_allocated_write      =     0 % luatex >= 0.82
\countdef   \c_syst_max_allocated_write      =    64 \c_syst_max_allocated_write      =   127 % luatex >= 0.82

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

\let\newfam\newfamily

\firstvalidlanguage\plusone

% 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
\normalprotected\def\setconstantvalue#1#2{\csname#1\endcsname\numexpr#2\relax}

% maybe setconstant with check

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

%newlinechar=10   \def\outputnewlinechar{\rawcharacter{10}}
\newlinechar=10  \edef\outputnewlinechar{^^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}}
\fi

\def\syst_basics_allocate_yes#1#2#3#4#5% last class method max name
  {\ifnum#1<#4\relax
     \global\advance#1\plusone
     \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
  {\ifx#5\undefined
     \expandafter\syst_basics_allocate_yes
   \else\ifx#5\relax
     \expandafter\expandafter\expandafter\syst_basics_allocate_yes
   \else
     \expandafter\expandafter\expandafter\syst_basics_allocate_nop
   \fi\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}.

\normalprotected\def\newbox  {\syst_basics_allocate\c_syst_last_allocated_box  \box  \chardef\c_syst_max_allocated_register}
\normalprotected\def\newmarks{\syst_basics_allocate\c_syst_last_allocated_marks\marks\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
\normalprotected\def\newattribute{\syst_basics_allocate\c_syst_last_allocated_attribute\attribute\attributedef\c_syst_max_allocated_register}

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

%normalprotected\def\newhelp#1#2{\newtoks#1#1\expandafter{\csname#2\endcsname}}
\normalprotected\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.

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

\let\tempstring\empty

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

\newdimen\scratchwidth
\newdimen\scratchheight
\newdimen\scratchdepth

\newdimen\scratchoffset
\newdimen\scratchleftoffset
\newdimen\scratchrightoffset
\newdimen\scratchtopoffset
\newdimen\scratchbottomoffset

\newdimen\scratchdistance

\newdimen\scratchhsize
\newdimen\scratchvsize

\newdimen\scratchxoffset
\newdimen\scratchyoffset
\newdimen\scratchhoffset
\newdimen\scratchvoffset

\newdimen\scratchxposition
\newdimen\scratchyposition

\newcount\scratchnx
\newcount\scratchny

\newcount\scratchmx
\newcount\scratchmy

\newcount\scratchmin
\newcount\scratchmax

\newcount\scratchunicode

\newdimen\scratchleftskip
\newdimen\scratchrightskip
\newdimen\scratchtopskip
\newdimen\scratchbottomskip

%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
\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
\let\halfpoint\halfapoint

\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    \plushundred        =   100
\chardef    \plustwohundred     =   200
\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
%D A few handy shortcuts

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

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

\newbox\voidbox  % public

\let\normalhbox\hbox
\let\normalvbox\vbox

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

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

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

\normalprotected\def\dontcomplain
  {\hbadness\plustenthousand
   \vbadness\plustenthousand
   \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
%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

\normalprotected\def\newif#1%
  {\count@\escapechar
   \escapechar\minusone
   \expandafter\expandafter\expandafter\def\@if #1{true}{\let#1\iftrue }%
   \expandafter\expandafter\expandafter\def\@if#1{false}{\let#1\iffalse}%
   \@if#1{false}% the condition starts out false
   \escapechar\count@}

\def\@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

%D Let's test this one:

\newif\ifdone
\newif\iffound

%D Potential primitive in \LUATEX:

\ifdefined\htdp \else \def\htdp#1{\dimexpr\ht#1+\dp#1\relax} \fi

%D A few shortcuts:

\normalprotected\def\glet {\global         \let }
\normalprotected\def\udef {\normalprotected\def }
\normalprotected\def\ugdef{\normalprotected\gdef}
\normalprotected\def\uedef{\normalprotected\edef}
\normalprotected\def\uxdef{\normalprotected\xdef}

%D The catcode constants will be redefined in later catcode related modules
%D but they can be used in the same way.

\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
\chardef\activecatcode      = 13
\chardef\commentcatcode     = 14
\chardef\invalidcatcode     = 15

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

\let\active\activecatcode

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

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

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

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

\normalprotected\def\loggingall
  {\tracingall
   \tracingonline    \zerocount}

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

%D Just for tracing purposes we set:

\tracingstats\plusone

%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

\newtoks\everydump

\def\dump{\the\everydump\normaldump}

%D The same applies for the startup actions.

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

\let\everyjob\relax \newtoks\everyjob

\normaleveryjob{\the\everyjob}

%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 presented here. A conditional is
%D defined by:
%D
%D \starttyping
%D \newconditional\doublesided
%D \setfalse
%D \stoptyping
%D Setting a conditional is done by \type{\settrue} and
%D \type{\setfalse}:
%D
%D \starttyping
%D \settrue\doublesided
%D \setfalse
%D \stoptyping
%D while testing is accomplished by:
%D
%D \starttyping
%D \ifconditional\doublesided  ... \else ... \fi
%D \setfalse
%D \stoptyping
%D We cannot use the simple scheme:
%D
%D \starttyping
%D \def\settrue #1{\let#1=\iftrue}
%D \def\setfalse#1{\let#1=\iffalse}
%D \stoptyping
%D
%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\setfalse#1{\let#1\conditionalfalse}

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

\let\newconditional\setfalse
\let\ifconditional \ifcase

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

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

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

\def\!!plusone{1}

\normalprotected\def\newfraction#1{\let#1\!!plusone}

%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

\let\ifzeropt\ifcase

% 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=}
\normalprotected\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}}}

\normalprotected\def\globalappendtotoks #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_append_g \t_syst_toks_temp=}
\normalprotected\def\globalprependtotoks#1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_prepend_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_prepend_l{\normalexpanded{\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}

\normalprotected\def\addtotoks      #1{\let\m_syst_toks_temp#1\afterassignment\syst_toks_add_l\let\next}
\normalprotected\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}

\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
%D Handy for \ETEX-only usage (avoids making \type {\relax}'s:

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

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

\normalprotected\def\reseteverypar
  {\everypar\emptytoks}

\let\endgraf\par
\let\endline\cr

\def\space{ }
\def\empty{}

\normalprotected\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}%
    \global\let^^M\par%
\egroup

\bgroup
    \gdef\obeyspaces{\catcode`\ \activecatcode}%
    \obeyspaces\global\let =\space%
\egroup

%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
%D \starttyping
%D \def\@@plniterate{\@@plnbody\let\next\@@plniterate\else\let\next\relax\fi\next}
%D \stoptyping
%D
%D A more efficient alternative:
%D
%D \starttyping
%D \def\@@plniterate{\@@plnbody\expandafter\@@plniterate\else\expandafter\relax\fi}
%D \stoptyping
%D
%D An even more efficient one:

\def\@@plniterate{\@@plnbody\expandafter\@@plniterate\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:

\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 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
  {\hskip\hideskip}

\def\ialign % initialized \halign
  {\everycr\emptytoks
   \tabskip\zeroskip
   \halign}

\newcount \mscount

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

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

%D Backend:

% For the moment we define some \pdf... helpers but this will move to the backend
% module after which the official interfaces have to be used. This is needed for
% modules not made by ctx developers.

\normalprotected\def\pdfliteral               {\pdfextension literal }
\normalprotected\def\pdfcolorstack            {\pdfextension colorstack }
\normalprotected\def\pdfsetmatrix             {\pdfextension setmatrix }
\normalprotected\def\pdfsave                  {\pdfextension save\relax}
\normalprotected\def\pdfrestore               {\pdfextension restore\relax}
\normalprotected\def\pdfobj                   {\pdfextension obj }
\normalprotected\def\pdfrefobj                {\pdfextension refobj }
\normalprotected\def\pdfannot                 {\pdfextension annot }
\normalprotected\def\pdfstartlink             {\pdfextension startlink }
\normalprotected\def\pdfendlink               {\pdfextension endlink\relax}
\normalprotected\def\pdfoutline               {\pdfextension outline }
\normalprotected\def\pdfdest                  {\pdfextension dest }
\normalprotected\def\pdfthread                {\pdfextension thread }
\normalprotected\def\pdfstartthread           {\pdfextension startthread }
\normalprotected\def\pdfendthread             {\pdfextension endthread\relax}
\normalprotected\def\pdfinfo                  {\pdfextension info }
\normalprotected\def\pdfcatalog               {\pdfextension catalog }
\normalprotected\def\pdfnames                 {\pdfextension names }
\normalprotected\def\pdfincludechars          {\pdfextension includechars }
\normalprotected\def\pdffontattr              {\pdfextension fontattr }
\normalprotected\def\pdfmapfile               {\pdfextension mapfile }
\normalprotected\def\pdfmapline               {\pdfextension mapline }
\normalprotected\def\pdftrailer               {\pdfextension trailer }
\normalprotected\def\pdfglyphtounicode        {\pdfextension glyphtounicode }

% \chardef\pdfnofullbanner   =   1
% \chardef\pdfnofilename     =   2
% \chardef\pdfnopagenumber   =   4
% \chardef\pdfnoinfodict     =   8
% \chardef\pdfnocreator      =  16
% \chardef\pdfnocreationdate =  32
% \chardef\pdfnomoddate      =  64
% \chardef\pdfnoproducer     = 128
% \chardef\pdfnotrapped      = 256
% \chardef\pdfnoid           = 512

\def\pdftexversion           {\numexpr\pdffeedback version\relax}
\def\pdftexrevision                  {\pdffeedback revision}
\def\pdflastlink             {\numexpr\pdffeedback lastlink\relax}
\def\pdfretval               {\numexpr\pdffeedback retval\relax}
\def\pdflastobj              {\numexpr\pdffeedback lastobj\relax}
\def\pdflastannot            {\numexpr\pdffeedback lastannot\relax}
\def\pdfxformname            {\numexpr\pdffeedback xformname\relax}
\def\pdfcreationdate                 {\pdffeedback creationdate}
\def\pdffontname             {\numexpr\pdffeedback fontname\relax}
\def\pdffontobjnum           {\numexpr\pdffeedback fontobjnum\relax}
\def\pdffontsize             {\dimexpr\pdffeedback fontsize\relax}
\def\pdfpageref              {\numexpr\pdffeedback pageref\relax}
\def\pdfcolorstackinit               {\pdffeedback colorstackinit}

\let\pdfxform                 \saveboxresource
\let\pdflastxform             \lastsavedboxresourceindex
\let\pdfrefxform              \useboxresource
\let\pdfximage                \saveimageresource
\let\pdflastximage            \lastsavedimageresourceindex
\let\pdflastximagepages       \lastsavedimageresourcepages
\let\pdfrefximage             \useimageresource
\let\pdfsavepos               \savepos
\let\pdflastxpos              \lastxpos
\let\pdflastypos              \lastypos

\edef\pdfcompresslevel       {\pdfvariable compresslevel}       \pdfcompresslevel         \plusnine
\edef\pdfobjcompresslevel    {\pdfvariable objcompresslevel}    \pdfobjcompresslevel      \plusone
\edef\pdfdecimaldigits       {\pdfvariable decimaldigits}       \pdfdecimaldigits         \plusfive
\edef\pdfgamma               {\pdfvariable gamma}               \pdfgamma                 \plusthousand
\edef\pdfimageresolution     {\pdfvariable imageresolution}     \pdfimageresolution       300
\edef\pdfimageapplygamma     {\pdfvariable imageapplygamma}     \pdfimageapplygamma       \zerocount
\edef\pdfimagegamma          {\pdfvariable imagegamma}          \pdfimagegamma            2200
\edef\pdfimagehicolor        {\pdfvariable imagehicolor}        \pdfimagehicolor          \plusone
\edef\pdfimageaddfilename    {\pdfvariable imageaddfilename}    \pdfimageaddfilename      \plusone
\edef\pdfpkresolution        {\pdfvariable pkresolution}        \pdfpkresolution          1200
\edef\pdfinclusioncopyfonts  {\pdfvariable inclusioncopyfonts}  \pdfinclusioncopyfonts    \plusone
\edef\pdfinclusionerrorlevel {\pdfvariable inclusionerrorlevel} \pdfinclusionerrorlevel   \zerocount
\edef\pdfgentounicode        {\pdfvariable gentounicode}        \pdfgentounicode          \plusone
\edef\pdfpagebox             {\pdfvariable pagebox}             \pdfpagebox               \zerocount
\edef\pdfmajorversion        {\pdfvariable majorversion}      % \pdfmajorversion          \plusone
\edef\pdfminorversion        {\pdfvariable minorversion}        \pdfminorversion          \plusseven
\edef\pdfuniqueresname       {\pdfvariable uniqueresname}       \pdfuniqueresname         \zerocount
\edef\pdfhorigin             {\pdfvariable horigin}             \pdfhorigin               1in
\edef\pdfvorigin             {\pdfvariable vorigin}             \pdfvorigin               \pdfhorigin
\edef\pdflinkmargin          {\pdfvariable linkmargin}          \pdflinkmargin            \zeropoint
\edef\pdfdestmargin          {\pdfvariable destmargin}          \pdfdestmargin            \zeropoint
\edef\pdfthreadmargin        {\pdfvariable threadmargin}        \pdfthreadmargin          \zeropoint
\edef\pdfxformmargin         {\pdfvariable xformmargin}         \pdfxformmargin           \zeropoint
\edef\pdfpkfixeddpi          {\pdfvariable pkfixeddpi}          \pdfpkfixeddpi            \plusone
\edef\pdfignoreunknownimages {\pdfvariable ignoreunknownimages} \pdfignoreunknownimages   \zerocount

\edef\pdfpagesattr           {\pdfvariable pagesattr}
\edef\pdfpageattr            {\pdfvariable pageattr}
\edef\pdfpageresources       {\pdfvariable pageresources}
\edef\pdfxformattr           {\pdfvariable xformattr}
\edef\pdfxformresources      {\pdfvariable xformresources}
\edef\pdfpkmode              {\pdfvariable pkmode}

\edef\pdfsuppressoptionalinfo{\pdfvariable suppressoptionalinfo }
\let \pdfsuppressptexinfo              \pdfsuppressoptionalinfo
\edef\pdftrailerid           {\pdfvariable trailerid }
\edef\pdfinfoomitdate        {\pdfvariable suppressoptionalinfo \numexpr32+64\relax}

\normalprotected\def\nopdfcompression     {\pdfobjcompresslevel\zerocount \pdfcompresslevel\zerocount}
\normalprotected\def\maximumpdfcompression{\pdfobjcompresslevel\plusnine  \pdfcompresslevel\plusnine }
\normalprotected\def\normalpdfcompression {\pdfobjcompresslevel\plusthree \pdfcompresslevel\plusthree}

\normalpdfcompression

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

%D Basic status stuff.

\newif\ifproductionrun

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

\def\modulonumber#1#2{\the\numexpr#2-((((#2+(#1/2))/#1)-1)*#1)\relax}
\def\dividenumber#1#2{\the\numexpr(#2-(#1/2))/#1\relax}

\ifdefined\texenginename \else
    %edef\texenginename{luatex}
    \edef\texenginename{\directlua{tex.print(LUATEXENGINE)}}
\fi

\ifdefined\texengineversion \else
    %edef\texengineversion{\dividenumber{100}\luatexversion.\modulonumber{100}\luatexversion.\luatexrevision}
    \edef\texengineversion{\directlua{tex.print(LUATEXVERSION)}}
\fi

\ifdefined\texenginefunctionality \else
    \edef\texenginefunctionality{\directlua{tex.print(LUATEXFUNCTIONALITY)}}
\fi

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

\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 % prevent messing up
\hoffset\zeropoint \let\hoffset\relax \newdimen\hoffset % prevent messing up

%D Handy.

\suppresslongerror    \plusone % \let\suppresslongerror    \relax
\suppressoutererror   \plusone % \let\suppressoutererror   \relax
\suppressmathparerror \plusone % \let\suppressmathparerror \relax
\suppressifcsnameerror\plusone % \let\suppressifcsnameerror\relax

\matheqnogapstep      \zerocount % fr now

%D While cleaning this code up a bit I was listening to Heather Nova's \CD\ Redbird.
%D The first song on that \CD\ ends with a few lines suitable for ending this
%D initialization module:
%D
%D \startlines
%D And there's so much I can do for you
%D Given time I know that I can prove
%D Now my world is opened up to you
%D Come inside
%D
%D Welcome to my life
%D Welcome to my world
%D Come inside
%D \stoplines
%D
%D So let's see what \TEX\ can do now that we've opened up the basic machinery.

%D Now we define a few helpers that we need in a very early stage. We hav eno message system
%D 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

\normalprotected\def\installsystemnamespace#1%
  {\ifcsname ??#1\endcsname
     \immediate\write\statuswrite{fatal error: duplicate system namespace '#1'}%
     \expandafter\normalend
   \else
     \global\advance\c_syst_helpers_n_of_namespaces\plusone
     \expandafter\edef\csname ??#1\endcsname{\v_interfaces_prefix_template_system}%
   \fi}

%D It makes more sense to have these here:

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

%D For now:

\ifdefined\protrusionboundary     \else \let\protrusionboundary\boundary   \fi
\ifdefined\wordboundary           \else \let\wordboundary      \noboundary \fi

\ifdefined\mathrulesfam           \else \newcount\mathrulesfam       \fi
\ifdefined\mathrulesmode          \else \newcount\mathrulesmode      \fi
\ifdefined\mathsurroundmode       \else \newcount\mathsurroundmode   \fi
\ifdefined\mathitalicsmode        \else \newcount\mathitalicsmode    \fi
\ifdefined\mathdelimitersmode     \else \newcount\mathdelimitersmode \fi
\ifdefined\mathscriptboxmode      \else \newcount\mathscriptboxmode  \fi

\ifdefined\hyphenpenaltymode      \else \newcount\hyphenpenaltymode      \fi
\ifdefined\automatichyphenpenalty \else \newcount\automatichyphenpenalty \fi
\ifdefined\automatichyphenmode    \else \newcount\automatichyphenmode    \fi
\ifdefined\explicithyphenpenalty  \else \newcount\explicithyphenpenalty  \fi

\ifdefined\explicitdiscretionary  \else \let\explicitdiscretionary  \-                   \fi
\ifdefined\automaticdiscretionary \else \def\automaticdiscretionary{\Uchar\exhyphenchar} \fi

\ifdefined\mathpenaltiesmode      \else \newcount\mathpenaltiesmode \fi
\ifdefined\prebinoppenalty        \else \newcount\prebinoppenalty   \fi
\ifdefined\prerelpenalty          \else \newcount\prerelpenalty     \fi

\ifdefined\breakafterdirmode      \else \newcount\breakafterdirmode \fi

\protect \endinput