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
|
%D \module
%D [ file=catc-ini,
%D version=2006.09.18,
%D title=\CONTEXT\ System Macros,
%D subtitle=Catcode Handling,
%D author=Hans Hagen,
%D date=\currentdate,
%D copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%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've split the functionality of syst-cat.* over more files
%D now so that we can load more selectively.
\registerctxluafile{catc-ini}{1.001}
%D A long standing wish has been the availability of catcode
%D arrays. Because traditional \TEX\ does not provide this we
%D implement a fake method in the \MKII\ file. There is some
%D overlap in code with \MKII\ but we take that for granted.
\setnewconstant\escapecatcode 0
\setnewconstant\begingroupcatcode 1
\setnewconstant\endgroupcatcode 2
\setnewconstant\mathshiftcatcode 3
\setnewconstant\alignmentcatcode 4
\setnewconstant\endoflinecatcode 5
\setnewconstant\parametercatcode 6
\setnewconstant\superscriptcatcode 7
\setnewconstant\subscriptcatcode 8
\setnewconstant\ignorecatcode 9
\setnewconstant\spacecatcode 10
\setnewconstant\lettercatcode 11
\setnewconstant\othercatcode 12 \let\other \othercatcode
\setnewconstant\activecatcode 13 \let\active\activecatcode
\setnewconstant\commentcatcode 14
\ifx\gobbleoneargument\undefined \long\def\gobbleoneargument#1{} \fi
\newif \ifrecatcodeuppercharacters % only used in good old tex
\newcount\cctdefcounter \cctdefcounter\zerocount % 0 = signal, so advance before allocate
\newcount\cctcountera
\newcount\cctcounterb
\newcount\cctcounterc
\def\newcatcodetable#1%
{\global\advance\cctdefcounter\plusone
\expandafter\xdef\csname @@ccn:\number\cctdefcounter\endcsname{\string#1}% logging
\newconstant#1%
#1\cctdefcounter
\ctxlua{catcodes.register("\expandafter\gobbleoneargument\string#1",\number#1)}}
\newcatcodetable \scratchcatcodes \initcatcodetable\scratchcatcodes
\newtoks \setdefaultcatcodes
\setdefaultcatcodes
{\catcode`\\ \othercatcode
\catcode`\^^M \othercatcode
\catcode`\ \othercatcode
\catcode`\% \othercatcode
\catcode127 \othercatcode}
\long\normalprotected\def\startcatcodetable#1#2\stopcatcodetable
{\bgroup
\catcodetable\scratchcatcodes
\the\setdefaultcatcodes
#2%
\savecatcodetable#1\relax
\egroup}
\newcatcodetable\dummycatcodes
\long\normalprotected\def\startextendcatcodetable#1#2\stopextendcatcodetable
{\bgroup
\catcodetable#1\relax
\globaldefs\plusone
#2%
\globaldefs\zerocount
\egroup}
% ==
%
% \long\normalprotected\def\startextendcatcodetable#1#2\stopextendcatcodetable
% {\bgroup
% \scratchcounter\the\catcodetable
% \catcodetable #1 #2
% \catcodetable\scratchcounter
% \egroup}
\def\letcatcodecommand
{\afterassignment\letcatcodecommanda\cctcountera}
\def\letcatcodecommanda
{\afterassignment\letcatcodecommandb\cctcounterb}
\let\currentcatcodetable\catcodetable
%D The next command can be defined in a cleaner way in the
%D Mk IV file but we want to have a fast one with a minimal
%D chance for interference. Do we still need this complex
%D mechanism? Future versions of \MKIV\ might only use
%D active characters for very special cases.
\setnewconstant\activehackcode `\~
%D Once a catcode is assigned, the next assignments will happen faster.
% (expandable) let
\def\letcatcodecommand {\afterassignment\letcatcodecommanda\cctcountera}
\def\letcatcodecommanda{\afterassignment\letcatcodecommandb\cctcounterb}
\def\letcatcodecommandb % each time
{\ifcsname CCL:\number\cctcountera:\number\cctcounterb\endcsname
\csname CCL:\number\cctcountera:\number\cctcounterb\expandafter\endcsname
\else
\expandafter\letcatcodecommandc
\fi}
\def\letcatcodecommandc % only first time
{\expandafter\gdef\csname CCL:\number\cctcountera:\number\cctcounterb\expandafter\endcsname\expandafter
{\expandafter\let\csname CCC:\number\cctcountera:\number\cctcounterb\endcsname}%
\reinstatecatcodecommandua % unexpanded
\csname CCL:\number\cctcountera:\number\cctcounterb\endcsname}
% expandable def
\def\defcatcodecommand {\afterassignment\defcatcodecommanda\cctcountera}
\def\defcatcodecommanda{\afterassignment\defcatcodecommandb\cctcounterb}
\def\defcatcodecommandb % each time
{\ifcsname CCD:\number\cctcountera:\number\cctcounterb\endcsname
\csname CCD:\number\cctcountera:\number\cctcounterb\expandafter\endcsname
\else
\expandafter\defcatcodecommandc
\fi}
\def\defcatcodecommandc % only first time (we could use \normalexpanded here)
{\expandafter\gdef\csname CCD:\number\cctcountera:\number\cctcounterb\expandafter\endcsname
\expandafter##\expandafter1\expandafter
{\expandafter\def\csname CCC:\number\cctcountera:\number\cctcounterb\endcsname{##1}}%
\reinstatecatcodecommanda
\csname CCD:\number\cctcountera:\number\cctcounterb\endcsname}
% unexpandable def (e.g. used for discretionaries)
\def\uedcatcodecommand {\afterassignment\uedcatcodecommanda\cctcountera}
\def\uedcatcodecommanda{\afterassignment\uedcatcodecommandb\cctcounterb}
\def\uedcatcodecommandb % each time
{\ifcsname CCU:\number\cctcountera:\number\cctcounterb\endcsname
\csname CCU:\number\cctcountera:\number\cctcounterb\expandafter\endcsname
\else
\expandafter\uedcatcodecommandc
\fi}
\def\uedcatcodecommandc % only first time
{\expandafter\gdef\csname CCU:\number\cctcountera:\number\cctcounterb\expandafter\endcsname
\expandafter##\expandafter1\expandafter
{\expandafter\normalprotected\expandafter\def\csname CCC:\number\cctcountera:\number\cctcounterb\endcsname{##1}}%
\reinstatecatcodecommandua % unexpanded
\csname CCU:\number\cctcountera:\number\cctcounterb\endcsname}
\def\reinstatecatcodecommand{\afterassignment\reinstatecatcodecommanda\cctcounterb}
\def\reinstatecatcodecommanda % can be used when a direct definition has been done
{\bgroup % and the selector has been lost
\uccode\activehackcode\cctcounterb
\catcode\uccode\activehackcode\activecatcode
\uppercase{\xdef~{\noexpand\catcodecommand{\number\cctcounterb}}}%
\egroup}
\def\reinstatecatcodecommandua % can be used when a direct definition has been done
{\bgroup % and the selector has been lost
\uccode\activehackcode\cctcounterb
\catcode\uccode\activehackcode\activecatcode
\uppercase{\normalprotected\xdef~{\noexpand\catcodecommand{\number\cctcounterb}}}%
\egroup}
\newconstant\defaultcatcodetable
\def\catcodecommand#1%
{\csname CCC:\number
\ifcsname CCC:\number\currentcatcodetable:\number#1\endcsname
\currentcatcodetable \else \defaultcatcodetable
\fi
:\number#1\endcsname}
%D \macros
%D {restorecatcodes,
%D beginrestorecatcodes,endrestorecatcodes}
%D
%D We're not finished dealing \CATCODES\ yet. In \CONTEXT\ we
%D use only one auxiliary file, which deals with tables of
%D contents, registers, two pass tracking, references etc. This
%D file, as well as files concerning graphics, is processed when
%D needed, which can be in the mid of typesetting verbatim.
%D However, when reading in data in verbatim mode, we should
%D temporary restore the normal \CATCODES, and that's exactly
%D what the next macros do. Saving the catcodes can be
%D disabled by saying \type{\localcatcodestrue}.
\let\savedcatcodetable\relax
\newcount\catcoderestorelevel
\def\pushcatcodetable
{\advance\catcoderestorelevel\plusone
\tracepushcatcodetable
\expandafter\chardef\csname scct:\number\catcoderestorelevel\endcsname\currentcatcodetable}
\def\popcatcodetable
{\ifcase\catcoderestorelevel
\showcatcodenestingerror
\else
\expandafter\catcodetable\csname scct:\number\catcoderestorelevel\endcsname
\tracepopcatcodetable
\advance\catcoderestorelevel\minusone
\fi}
\def\showcatcodenestingerror % can be overloaded
{\immediate\write16{}%
\immediate\write16{Fatal error: catcode push/pop mismatch. Fix this!}\wait\end
\immediate\write16{}}
\def\restorecatcodes % takes previous level
{\ifnum\catcoderestorelevel>\plusone
\expandafter\catcodetable\csname scct:\number\numexpr\catcoderestorelevel-1\relax\endcsname
\fi}
\newtoks\everycatcodetable
\def\setcatcodetable#1%
{\catcodetable#1%
\the\everycatcodetable
\tracesetcatcodetable}
\def\dotracecatcodetable#1{\immediate\write16{[#1]}}
\def\tracecatcodetables
{\def\tracesetcatcodetable {\dotracecatcodetable{set \catcodetablename\space at \number\catcoderestorelevel}}%
\def\tracepushcatcodetable{\dotracecatcodetable{push \catcodetablename\space from \catcodetableprev\space at \number\catcoderestorelevel}}%
\def\tracepopcatcodetable {\dotracecatcodetable{pop \catcodetablename\space to \catcodetableprev\space at \number\catcoderestorelevel}}}
\def\catcodetableprev
{\ifnum\numexpr\catcoderestorelevel-1\relax>\zerocount
\csname @@ccn:\number\csname scct:\number\numexpr\catcoderestorelevel-1\relax\endcsname\endcsname
\else
-%
\fi}
\def\catcodetablename
{\ifnum\currentcatcodetable>\zerocount
\csname @@ccn:\number\currentcatcodetable\endcsname
\else
-%
\fi}
\ifx\empty\undefined \def\empty{} \fi
\let\tracesetcatcodetable \empty
\let\tracepushcatcodetable\empty
\let\tracepopcatcodetable \empty
\def\beginrestorecatcodes{\pushcatcodetable}
\def\endrestorecatcodes {\popcatcodetable}
%D Handy for debugging:
% \tracecatcodetables
%D Only in \MKIV\ (to be used when crossing pages with changed catcodes
%D in the current vector):
%D
%D \starttyping
%D \normalprotected\def\startcrap
%D {\bgroup
%D \pushcatcodes
%D \whitespace
%D \obeylines
%D \activatespacehandler\v!yes
%D \strut}
%D
%D \normalprotected\def\stopcrap
%D {\popcatcodes
%D \egroup}
%D \stoptyping
\newcount\catcodetablelevel
\def\pushcatcodes
{\bgroup
\global\advance\catcodetablelevel\plusone
\ifcsname @@ccf:\number\catcodetablelevel\endcsname \else
\global\advance\cctdefcounter\plusone
\expandafter\global\expandafter\chardef\csname @@ccf:\number\catcodetablelevel\endcsname\cctdefcounter
\fi
\catcodetable\ctxcatcodes
\expandafter\savecatcodetable\csname @@ccf:\number\catcodetablelevel\endcsname
\egroup
\expandafter\catcodetable\csname @@ccf:\number\catcodetablelevel\endcsname}
\def\popcatcodes
{\global\advance\catcodetablelevel\minusone}
\endinput
|