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
|
%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.
%D A long standing wish has been the availability of catcode
%D arrays. Because traditional \TEX\ does ot provide this we
%D implement a fake method in the Mark II file.
\ifx\zerocount\undefined \chardef \zerocount= 0 \fi
\ifx\plusone \undefined \chardef \plusone = 1 \fi
\ifx\minusone \undefined \newcount\minusone \minusone =-1 \fi
\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\other = 12
\chardef\activecatcode = 13 \chardef\active = 13
\chardef\commentcatcode = 14
\newif \ifrecatcodeuppercharacters % only used in good old tex
% \newcount\cctdefcounter \cctdefcounter\plusone % 0 = signal
\newcount\cctdefcounter \cctdefcounter\zerocount % 0 = signal, so advance before allocate
\newcount\cctcountera
\newcount\cctcounterb
\newcount\cctcounterc
\def\newcatcodetable#1%
{\global\advance\cctdefcounter\plusone
\global\mathchardef#1\cctdefcounter
\expandafter\xdef\csname @@ccn:\number\cctdefcounter\endcsname{\string#1}% logging
\expandafter\newtoks\csname @@cct:\number\cctdefcounter\endcsname}
\mathchardef\currentcatcodetable\zerocount
\newtoks \setdefaultlowercatcodes
\newtoks \setdefaultuppercatcodes
\def\next#1% we don't have a proper loop defined yet
{\edef\nextnext{#1{\the#1\catcode\the\cctcountera\space
\ifnum\catcode\cctcountera=\lettercatcode \lettercatcode\else\othercatcode\fi}}%
\nextnext\ifnum\cctcountera<\cctcounterb \advance\cctcountera\plusone \expandafter\next\expandafter#1\fi}
\cctcountera 0 \cctcounterb 127 \next\setdefaultlowercatcodes
\cctcountera 128 \cctcounterb 255 \next\setdefaultuppercatcodes
\recatcodeuppercharactersfalse
\def\catcodetable#1%
{\mathchardef\currentcatcodetable#1%
\the\setdefaultlowercatcodes
\ifrecatcodeuppercharacters\the\setdefaultuppercatcodes\fi
\the\csname @@cct:\number#1\endcsname}
\long\def\startcatcodetable#1#2\stopcatcodetable
{\global\csname @@cct:\number#1\endcsname{#2}}
\long\def\startextendcatcodetable#1#2\stopextendcatcodetable
{\global\csname @@cct:\number#1\endcsname\expandafter{\the\csname @@cct:\number#1\endcsname#2}}
%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.
\chardef\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}%
\reinstatecatcodecommanda
\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
{\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}
% un expandable 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\unexpanded\expandafter\def\csname CCC:\number\cctcountera:\number\cctcounterb\endcsname{##1}}%
\reinstatecatcodecommanda
\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}
\chardef\defaultcatcodetable\zerocount
\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\mathchardef\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
\endinput
|