Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3cctab.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3cctab.dtx
4 %
5 % Copyright (C) 2018-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % http://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3cctab} module\\ Category code tables^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % A category code table enables rapid switching of all category codes in
53 % one operation. For \LuaTeX{}, this is possible over the entire Unicode
54 % range. For other engines, only the $8$-bit range ($0$--$255$) is covered by
55 % such tables. The implementation of category code tables in \pkg{expl3}
56 % also saves and restores the \TeX{} \tn{endlinechar} primitive value, meaning
57 % they could be used for example to implement \cs{ExplSyntaxOn}.
58 %
59 % \section{Creating and initializing category code tables}
60 %
61 % \begin{function}[updated = 2020-07-02]{\cctab_new:N,\cctab_new:c}
62 % \begin{syntax}
63 % \cs{cctab_new:N} \meta{category code table}
64 % \end{syntax}
65 % Creates a new \meta{category code table} variable or raises an error if
66 % the name is already taken. The declaration is global. The
67 % \meta{category code table} is initialized with the codes
68 % as used by \IniTeX{}.
69 % \end{function}
70 %
71 % \begin{function}[updated = 2020-07-07]{\cctab_const:Nn,\cctab_const:cn}
72 % \begin{syntax}
73 % \cs{cctab_const:Nn} \meta{category code table} \Arg{category code set up}
74 % \end{syntax}
75 % Creates a new \meta{category code table}, applies (in a group) the
76 % \meta{category code set up} on top of \IniTeX{} settings,
77 % then saves them globally as a constant
78 % table. The \meta{category code set up} can include a call to
79 % \cs{cctab_select:N}.
80 % \end{function}
81 %
82 % \begin{function}[updated = 2020-07-07]{\cctab_gset:Nn,\cctab_gset:cn}
83 % \begin{syntax}
84 % \cs{cctab_gset:Nn} \meta{category code table} \Arg{category code set up}
85 % \end{syntax}
86 % Starting from the \IniTeX{} category codes,
87 % applies (in a group) the \meta{category code set up}, then saves them
88 % globally in the \meta{category code table}. The \meta{category code set up}
89 % can include a call to \cs{cctab_select:N}.
90 % \end{function}
91 %
92 % \begin{function}[added = 2023-05-26]
93 % {\cctab_gsave_current:N,\cctab_gsave_current:c}
94 % \begin{syntax}
95 % \cs{cctab_gsave_current:N} \meta{category code table}
96 % \end{syntax}
97 % Saves the current prevailing category codes in the
98 % \meta{category code table}.
99 % \end{function}
100 %
101 % \section{Using category code tables}
102 %
103 % \begin{function}[updated = 2020-07-02]{\cctab_begin:N,\cctab_begin:c}
104 % \begin{syntax}
105 % \cs{cctab_begin:N} \meta{category code table}
106 % \end{syntax}
107 % Switches locally the category codes in force to those stored in the
108 % \meta{category code table}. The prevailing codes before the
109 % function is called are added to a stack, for use with
110 % \cs{cctab_end:}. This function does not start a \TeX{} group.
111 % \end{function}
112 %
113 % \begin{function}[updated = 2020-07-02]{\cctab_end:}
114 % \begin{syntax}
115 % \cs{cctab_end:}
116 % \end{syntax}
117 % Ends the scope of a \meta{category code table} started using
118 % \cs{cctab_begin:N}, returning the codes to those in force before the
119 % matching \cs{cctab_begin:N} was used. This must be used within the
120 % same \TeX{} group and at the same \TeX{} group level as the
121 % matching \cs{cctab_begin:N}.
122 % \end{function}
123 %
124 % \begin{function}[added = 2020-05-19, updated = 2020-07-02]{\cctab_select:N, \cctab_select:c}
125 % \begin{syntax}
126 % \cs{cctab_select:N} \meta{category code table}
127 % \end{syntax}
128 % Selects the \meta{category code table} for the scope of the current
129 % group. This is in particular useful in the \meta{setup} arguments
130 % of \cs{tl_set_rescan:Nnn}, \cs{tl_rescan:nn}, \cs{cctab_const:Nn},
131 % and \cs{cctab_gset:Nn}.
132 % \end{function}
133 %
134 % \begin{function}[EXP, added = 2021-05-10]{\cctab_item:Nn, \cctab_item:cn}
135 % \begin{syntax}
136 % \cs{cctab_item:Nn} \meta{category code table} \Arg{int expr}
137 % \end{syntax}
138 % Determines the \meta{character} with character code given by the
139 % \meta{int expr} and expands to its category code specified
140 % by the \meta{category code table}.
141 % \end{function}
142 %
143 % \section{Category code table conditionals}
144 %
145 % \begin{function}[pTF]{\cctab_if_exist:N, \cctab_if_exist:c}
146 % \begin{syntax}
147 % \cs{cctab_if_exist_p:N} \meta{category code table}
148 % \cs{cctab_if_exist:NTF} \meta{category code table} \Arg{true code} \Arg{false code}
149 % \end{syntax}
150 % Tests whether the \meta{category code table} is currently defined.
151 % This does not check that the \meta{category code table} really is a
152 % category code table.
153 % \end{function}
154 %
155 % \section{Constant and scratch category code tables}
156 %
157 % \begin{variable}[updated = 2020-07-10]{\c_code_cctab}
158 % Category code table for the \pkg{expl3} code environment; this does
159 % \emph{not} include \texttt{@}, which is retained as an \enquote{other}
160 % character. Sets the \tn{endlinechar} value to $32$ (a space).
161 % \end{variable}
162 %
163 % \begin{variable}[updated = 2020-07-08]{\c_document_cctab}
164 % Category code table for a standard \LaTeX{} document, as set by the \LaTeX{}
165 % kernel. In particular, the upper-half of the $8$-bit range will be set to
166 % \enquote{active} with \pdfTeX{} \emph{only}. No \pkg{babel} shorthands
167 % will be activated. Sets the \tn{endlinechar} value to $13$ (normal
168 % line ending).
169 % \end{variable}
170 %
171 % \begin{variable}[updated = 2020-07-02]{\c_initex_cctab}
172 % Category code table as set up by \IniTeX{}.
173 % \end{variable}
174 %
175 % \begin{variable}[updated = 2020-07-02]{\c_other_cctab}
176 % Category code table where all characters have category code $12$
177 % (other). Sets the \tn{endlinechar} value to $-1$.
178 % \end{variable}
179 %
180 % \begin{variable}[updated = 2020-07-02]{\c_str_cctab}
181 % Category code table where all characters have category code $12$
182 % (other) with the exception of spaces, which have category code
183 % $10$ (space). Sets the \tn{endlinechar} value to $-1$.
184 % \end{variable}
185 %
186 % \begin{variable}[added = 2023-05-26]{\g_tmpa_cctab, \g_tmpb_cctab}
187 % Scratch category code tables.
188 % \end{variable}
189 %
190 % \end{documentation}
191 %
192 % \begin{implementation}
193 %
194 % \section{\pkg{l3cctab} implementation}
195 %
196 % \begin{macrocode}
197 %<*code>
198 % \end{macrocode}
199 %
200 % \begin{macrocode}
201 %<@@=cctab>
202 % \end{macrocode}
203 %
204 % As \LuaTeX{} offers engine support for category code tables, and this
205 % is entirely lacking from the other engines, we need two complementary
206 % approaches. (Some future \XeTeX{} may add support, at which point the
207 % conditionals below would be different.)
208 %
209 % \subsection{Variables}
210 %
211 % \begin{variable}{\g_@@_stack_seq, \g_@@_unused_seq}
212 % List of catcode tables saved by nested \cs{cctab_begin:N}, to
213 % restore catcodes at the matching \cs{cctab_end:}. When popped from
214 % the \cs{g_@@_stack_seq} the table numbers are stored in
215 % \cs{g_@@_unused_seq} for later reuse.
216 % \begin{macrocode}
217 \seq_new:N \g_@@_stack_seq
218 \seq_new:N \g_@@_unused_seq
219 % \end{macrocode}
220 % \end{variable}
221 %
222 % \begin{variable}{\g_@@_group_seq}
223 % A stack to store the group level when a catcode table started.
224 % \begin{macrocode}
225 \seq_new:N \g_@@_group_seq
226 % \end{macrocode}
227 % \end{variable}
228 %
229 % \begin{variable}{\g_@@_allocate_int}
230 % Integer to keep track of what category code table to allocate. In
231 % \LuaTeX{} it is only used in format mode to implement
232 % \cs{cctab_new:N}. In other engines it is used to make csnames for
233 % dynamic tables.
234 % \begin{macrocode}
235 \int_new:N \g_@@_allocate_int
236 % \end{macrocode}
237 % \end{variable}
238 %
239 % \begin{variable}{\l_@@_tmpa_tl,\l_@@_tmpb_tl}
240 % Scratch space. For instance, when popping
241 % \cs{g_@@_stack_seq}/\cs{g_@@_unused_seq}, consists of the
242 % catcodetable number (integer denotation) in \LuaTeX{}, or of an
243 % intarray variable (as a single token) in other engines.
244 % \begin{macrocode}
245 \tl_new:N \l_@@_tmpa_tl
246 \tl_new:N \l_@@_tmpb_tl
247 % \end{macrocode}
248 % \end{variable}
249 %
250 % \begin{variable}{\g_@@_endlinechar_prop}
251 % In \LuaTeX{} we store the \tn{endlinechar} associated to each
252 % \tn{catcodetable} in a property list, unless it is the default
253 % value~$13$.
254 % \begin{macrocode}
255 \prop_new:N \g_@@_endlinechar_prop
256 % \end{macrocode}
257 % \end{variable}
258 %
259 % \subsection{Allocating category code tables}
260 %
261 % \begin{macro}{\cctab_new:N, \cctab_new:c, \@@_new:N, \@@_gstore:Nnn}
262 % The \cs{@@_new:N} auxiliary allocates a new catcode table but does
263 % not attempt to set its value consistently across engines. It is
264 % used both in \cs{cctab_new:N}, which sets catcodes to \IniTeX{}
265 % values, and in \cs{cctab_begin:N}/\cs{cctab_end:} for dynamically
266 % allocated tables.
267 %
268 % First, the \LuaTeX{} case.
269 % Creating a new category code table is done like other registers.
270 % In Con\TeX{}t, \tn{newcatcodetable} does not include the initialization,
271 % so that is added explicitly.
272 % \begin{macrocode}
273 \sys_if_engine_luatex:TF
274 {
275 \cs_new_protected:Npn \cctab_new:N #1
276 {
277 \__kernel_chk_if_free_cs:N #1
278 \@@_new:N #1
279 }
280 \cs_new_protected:Npn \@@_new:N #1
281 {
282 \newcatcodetable #1
283 \tex_initcatcodetable:D #1
284 }
285 }
286 % \end{macrocode}
287 % Now the case for other engines. Here, each table is an integer
288 % array. Following the \LuaTeX{} pattern, a new table starts with
289 % \IniTeX{} codes. The \cs{debug_suspend:} and \cs{debug_resume:}
290 % functions prevent errors and logging from \texttt{debug} commands
291 % which are either duplicate or false when \cs{@@_new:N} is used
292 % by \cs{cctab_new:N} or \cs{cctab_const:Nn}.
293 % The index base is out-by-one, so we have an
294 % internal function to handle that. The \IniTeX{} \tn{endlinechar} is
295 % $13$.
296 % \begin{macrocode}
297 {
298 \cs_new_protected:Npn \@@_new:N #1
299 {
300 \debug_suspend:
301 \intarray_new:Nn #1 { 257 }
302 \debug_resume:
303 }
304 \cs_new_protected:Npn \@@_gstore:Nnn #1#2#3
305 { \intarray_gset:Nnn #1 { #2 + 1 } {#3} }
306 \cs_new_protected:Npn \cctab_new:N #1
307 {
308 \__kernel_chk_if_free_cs:N #1
309 \@@_new:N #1
310 \int_step_inline:nn { 256 }
311 { \__kernel_intarray_gset:Nnn #1 {##1} { 12 } }
312 \__kernel_intarray_gset:Nnn #1 { 257 } { 13 }
313 \@@_gstore:Nnn #1 { 0 } { 9 }
314 \@@_gstore:Nnn #1 { 13 } { 5 }
315 \@@_gstore:Nnn #1 { 32 } { 10 }
316 \@@_gstore:Nnn #1 { 37 } { 14 }
317 \int_step_inline:nnn { 65 } { 90 }
318 { \@@_gstore:Nnn #1 {##1} { 11 } }
319 \@@_gstore:Nnn #1 { 92 } { 0 }
320 \int_step_inline:nnn { 97 } { 122 }
321 { \@@_gstore:Nnn #1 {##1} { 11 } }
322 \@@_gstore:Nnn #1 { 127 } { 15 }
323 }
324 }
325 \cs_generate_variant:Nn \cctab_new:N { c }
326 % \end{macrocode}
327 % \end{macro}
328 %
329 % \subsection{Saving category code tables}
330 %
331 % \begin{macro}{\@@_gset:n, \@@_gset_aux:n}
332 % In various functions we need to save the current catcodes (globally)
333 % in a table. In \LuaTeX{}, saving the catcodes is a primitives, but
334 % the \tn{endlinechar} needs more work: to avoid filling
335 % \cs{g_@@_endlinechar_prop} with many entries we special-case the
336 % default value $13$. In other engines we store $256$ current
337 % catcodes and the \tn{endlinechar} in an intarray variable.
338 % \begin{macrocode}
339 \sys_if_engine_luatex:TF
340 {
341 \cs_new_protected:Npn \@@_gset:n #1
342 { \exp_args:Nf \@@_gset_aux:n { \int_eval:n {#1} } }
343 \cs_new_protected:Npn \@@_gset_aux:n #1
344 {
345 \tex_savecatcodetable:D #1 \scan_stop:
346 \int_compare:nNnTF { \tex_endlinechar:D } = { 13 }
347 { \prop_gremove:Nn \g_@@_endlinechar_prop {#1} }
348 {
349 \prop_gput:NnV \g_@@_endlinechar_prop {#1}
350 \tex_endlinechar:D
351 }
352 }
353 }
354 {
355 \cs_new_protected:Npn \@@_gset:n #1
356 {
357 \int_step_inline:nn { 256 }
358 {
359 \__kernel_intarray_gset:Nnn #1 {##1}
360 { \char_value_catcode:n { ##1 - 1 } }
361 }
362 \__kernel_intarray_gset:Nnn #1 { 257 }
363 { \tex_endlinechar:D }
364 }
365 }
366 % \end{macrocode}
367 % \end{macro}
368 %
369 % \begin{macro}{\cctab_gset:Nn, \cctab_gset:cn}
370 % Category code tables are always global, so only one version of
371 % assignments is needed. Simply run the setup in a group and save the
372 % result in a category code table~|#1|, provided it is valid. The
373 % internal function is defined above depending on the engine.
374 % \begin{macrocode}
375 \cs_new_protected:Npn \cctab_gset:Nn #1#2
376 {
377 \@@_chk_if_valid:NT #1
378 {
379 \group_begin:
380 \cctab_select:N \c_initex_cctab
381 #2 \scan_stop:
382 \@@_gset:n {#1}
383 \group_end:
384 }
385 }
386 \cs_generate_variant:Nn \cctab_gset:Nn { c }
387 % \end{macrocode}
388 % \end{macro}
389 %
390 % \begin{macro}{\cctab_gsave_current:N, \cctab_gsave_current:c}
391 % Very simple.
392 % \begin{macrocode}
393 \cs_new_protected:Npn \cctab_gsave_current:N #1
394 {
395 \@@_chk_if_valid:NT #1
396 { \@@_gset:n {#1} }
397 }
398 \cs_generate_variant:Nn \cctab_gsave_current:N { c }
399 % \end{macrocode}
400 % \end{macro}
401 %
402 % \subsection{Using category code tables}
403 %
404 % \begin{variable}{\g_@@_tmp_cctab}
405 % \begin{macro}[EXP]{\@@_tmp_cctab_name: }
406 % In \LuaTeX{}, we must ensure that the saved tables are read-only.
407 % This is done by applying the saved table, then switching immediately
408 % to a scratch table. Any later catcode assignment will affect that
409 % scratch table rather than the saved one. If we simply switched to
410 % the saved tables, then \cs{char_set_catcode_other:N} in the example
411 % below would change \cs{c_document_cctab} and a later use of that
412 % table would give the wrong category code to |_|.
413 % \begin{verbatim}
414 % \use:n
415 % {
416 % \cctab_begin:N \c_document_cctab
417 % \char_set_catcode_other:N \_
418 % \cctab_end:
419 % \cctab_begin:N \c_document_cctab
420 % \int_compare:nTF { \char_value_catcode:n { `_ } = 8 }
421 % { \TRUE } { \ERROR }
422 % \cctab_end:
423 % }
424 % \end{verbatim}
425 % We must also make sure that a scratch table is never reused in a
426 % nested group: in the following example, the scratch table used by
427 % the first \cs{cctab_begin:N} would be changed globally by the second
428 % one issuing \tn{savecatcodetable}, and after \cs{group_end:} the
429 % wrong category codes (those of \cs{c_str_cctab}) would be imposed.
430 % Note that the inner \cs{cctab_end:} restores the correct catcodes
431 % only locally, so the problem really comes up because of the
432 % different grouping level. The simplest is to use a scratch table
433 % labeled by the \tn{currentgrouplevel}. We initialize one of them as
434 % an example.
435 % \begin{verbatim}
436 % \use:n
437 % {
438 % \cctab_begin:N \c_document_cctab
439 % \group_begin:
440 % \cctab_begin:N \c_str_cctab
441 % \cctab_end:
442 % \group_end:
443 % \cctab_end:
444 % }
445 % \end{verbatim}
446 % \begin{macrocode}
447 \sys_if_engine_luatex:T
448 {
449 \@@_new:N \g_@@_tmp_cctab
450 \cs_new:Npn \@@_tmp_cctab_name:
451 {
452 g_@@_tmp
453 \tex_romannumeral:D \tex_currentgrouplevel:D
454 _cctab
455 }
456 }
457 % \end{macrocode}
458 % \end{macro}
459 % \end{variable}
460 %
461 % \begin{macro}{\cctab_select:N, \cctab_select:c}
462 % \begin{macro}{\@@_select:N}
463 % The public function simply checks the \meta{cctab~var} exists before
464 % using the engine-dependent \cs{@@_select:N}. Skipping these checks
465 % would result in low-level engine-dependent errors. First, the
466 % \LuaTeX{} case. In other engines, selecting a catcode table is a matter
467 % of doing $256$ catcode assignments and setting the \tn{endlinechar}.
468 % \begin{macrocode}
469 \cs_new_protected:Npn \cctab_select:N #1
470 { \@@_chk_if_valid:NT #1 { \@@_select:N #1 } }
471 \cs_generate_variant:Nn \cctab_select:N { c }
472 \sys_if_engine_luatex:TF
473 {
474 \cs_new_protected:Npn \@@_select:N #1
475 {
476 \tex_catcodetable:D #1
477 \prop_get:NVNTF \g_@@_endlinechar_prop #1 \l_@@_tmpa_tl
478 { \int_set:Nn \tex_endlinechar:D { \l_@@_tmpa_tl } }
479 { \int_set:Nn \tex_endlinechar:D { 13 } }
480 \cs_if_exist:cF { \@@_tmp_cctab_name: }
481 { \exp_args:Nc \@@_new:N { \@@_tmp_cctab_name: } }
482 \exp_args:Nc \tex_savecatcodetable:D { \@@_tmp_cctab_name: }
483 \exp_args:Nc \tex_catcodetable:D { \@@_tmp_cctab_name: }
484 }
485 }
486 {
487 \cs_new_protected:Npn \@@_select:N #1
488 {
489 \int_step_inline:nn { 256 }
490 {
491 \char_set_catcode:nn { ##1 - 1 }
492 { \__kernel_intarray_item:Nn #1 {##1} }
493 }
494 \int_set:Nn \tex_endlinechar:D
495 { \__kernel_intarray_item:Nn #1 { 257 } }
496 }
497 }
498 % \end{macrocode}
499 % \end{macro}
500 % \end{macro}
501 %
502 % \begin{variable}{\g_@@_next_cctab}
503 % \begin{macro}{\@@_begin_aux:}
504 % For \cs{cctab_begin:N}/\cs{cctab_end:} we will need to allocate
505 % dynamic tables. This is done here by \cs{@@_begin_aux:}, which puts
506 % a table number (in \LuaTeX{}) or name (in other engines) into
507 % \cs{l_@@_tmpa_tl}. In \LuaTeX{} this simply calls \cs{@@_new:N}
508 % and uses the resulting catcodetable number; in other engines we need
509 % to give a name to the intarray variable and use that. In \LuaTeX{},
510 % to restore catcodes at \cs{cctab_end:} we cannot just set
511 % \tn{catcodetable} to its value before \cs{cctab_begin:N}, because
512 % that table may have been altered by other code in the mean time. So
513 % we must make sure to save the catcodes in a table we control and
514 % restore them at \cs{cctab_end:}.
515 % \begin{macrocode}
516 \sys_if_engine_luatex:TF
517 {
518 \cs_new_protected:Npn \@@_begin_aux:
519 {
520 \@@_new:N \g_@@_next_cctab
521 \tl_set:NV \l_@@_tmpa_tl \g_@@_next_cctab
522 \cs_undefine:N \g_@@_next_cctab
523 }
524 }
525 {
526 \cs_new_protected:Npn \@@_begin_aux:
527 {
528 \int_gincr:N \g_@@_allocate_int
529 \exp_args:Nc \@@_new:N
530 { g_@@_ \int_use:N \g_@@_allocate_int _cctab }
531 \exp_args:NNc \tl_set:Nn \l_@@_tmpa_tl
532 { g_@@_ \int_use:N \g_@@_allocate_int _cctab }
533 }
534 }
535 % \end{macrocode}
536 % \end{macro}
537 % \end{variable}
538 %
539 % \begin{macro}{\cctab_begin:N, \cctab_begin:c}
540 % Check the \meta{cctab~var} exists, to avoid low-level errors. Get
541 % in \cs{l_@@_tmpa_tl} the number/name of a dynamic table, either
542 % from \cs{g_@@_unused_seq} where we save tables that are not
543 % currently in use, or from \cs{@@_begin_aux:} if none are available.
544 % Then save the current catcodes into the table (pointed to by)
545 % \cs{l_@@_tmpa_tl} and save that table number in a stack before
546 % selecting the desired catcodes.
547 % \begin{macrocode}
548 \cs_new_protected:Npn \cctab_begin:N #1
549 {
550 \@@_chk_if_valid:NT #1
551 {
552 \seq_gpop:NNF \g_@@_unused_seq \l_@@_tmpa_tl
553 { \@@_begin_aux: }
554 \@@_chk_group_begin:e
555 { \@@_nesting_number:N \l_@@_tmpa_tl }
556 \seq_gpush:NV \g_@@_stack_seq \l_@@_tmpa_tl
557 \exp_args:NV \@@_gset:n \l_@@_tmpa_tl
558 \@@_select:N #1
559 }
560 }
561 \cs_generate_variant:Nn \cctab_begin:N { c }
562 % \end{macrocode}
563 % \end{macro}
564 %
565 % \begin{macro}{\cctab_end:}
566 % Make sure a \cs{cctab_begin:N} was used some time earlier, get in
567 % \cs{l_@@_tmpa_tl} the catcode table number/name in which the
568 % prevailing catcodes were stored, then restore these catcodes. The
569 % dynamic table is now unused hence stored in \cs{g_@@_unused_seq} for
570 % recycling by later \cs{cctab_begin:N}.
571 % \begin{macrocode}
572 \cs_new_protected:Npn \cctab_end:
573 {
574 \seq_gpop:NNTF \g_@@_stack_seq \l_@@_tmpa_tl
575 {
576 \seq_gpush:NV \g_@@_unused_seq \l_@@_tmpa_tl
577 \exp_args:Ne \@@_chk_group_end:n
578 { \@@_nesting_number:N \l_@@_tmpa_tl }
579 \@@_select:N \l_@@_tmpa_tl
580 }
581 { \msg_error:nn { cctab } { extra-end } }
582 }
583 % \end{macrocode}
584 % \end{macro}
585 %
586 % \begin{macro}
587 % {\@@_chk_group_begin:n,\@@_chk_group_begin:e, \@@_chk_group_end:n}
588 % Catcode tables are not allowed to be intermixed with groups, so here
589 % we check that they are properly nested regarding \TeX{} groups.
590 % \cs{@@_chk_group_begin:n} stores the current group level in a stack,
591 % and locally defines a dummy control sequence
592 % \cs[no-index]{@@_group_\meta{cctab-level}_chk:}.
593 %
594 % \cs{@@_chk_group_end:n} pops the stack, and compares the returned
595 % value with \cs{tex_currentgrouplevel:D}. If they differ,
596 % \cs{cctab_end:} is in a different grouping level than the matching
597 % \cs{cctab_begin:N}. If they are the same, both happened at the same
598 % level, however a group might have ended and another started between
599 % \cs{cctab_begin:N} and \cs{cctab_end:}:
600 % \begin{verbatim}
601 % \group_begin:
602 % \cctab_begin:N \c_document_cctab
603 % \group_end:
604 % \group_begin:
605 % \cctab_end:
606 % \group_end:
607 % \end{verbatim}
608 % In this case checking \cs{tex_currentgrouplevel:D} is not enough, so
609 % we locally define \cs[no-index]{@@_group_\meta{cctab-level}_chk:},
610 % and then check if it exist in \cs{cctab_end:}. If it doesn't,
611 % we know there was a group end where it shouldn't.
612 %
613 % The \meta{cctab-level} in the sentinel macro above cannot be
614 % replaced by the more convenient \cs{tex_currentgrouplevel:D} because
615 % with the latter we might be tricked. Suppose:
616 % \begin{verbatim}
617 % \group_begin:
618 % \cctab_begin:N \c_code_cctab % A
619 % \group_end:
620 % \group_begin:
621 % \cctab_begin:N \c_code_cctab % B
622 % \cctab_end: % C
623 % \cctab_end: % D
624 % \group_end:
625 % \end{verbatim}
626 % The line marked with |A| would start a |cctab| with a sentinel token
627 % named \cs[no-index]{@@_group_1_chk:}, which would disappear at the
628 % \cs{group_end:} that follows. But |B| would create the same
629 % sentinel token, since both are at the same group level. Line |C|
630 % would end the |cctab| from line |B| correctly, but so would line |D|
631 % because line |B| created the same sentinel token. Using
632 % \meta{cctab-level} works correctly because it signals that certain
633 % |cctab| level was activated somewhere, but if it doesn't exist when
634 % the \cs{cctab_end:} is reached, we had a problem.
635 %
636 % Unfortunately these tests only flag the wrong usage at the
637 % \cs{cctab_end:}, which might be far from the \cs{cctab_begin:N}.
638 % However it isn't possible to signal the wrong usage at the
639 % \cs{group_end:} without using \cs{tex_aftergroup:D}, which is
640 % unsafe in certain types of groups.
641 %
642 % The three cases checked here just raise an error, and no recovery is
643 % attempted: usually interleaving groups and catcode tables will work
644 % predictably.
645 % \begin{macrocode}
646 \cs_new_protected:Npn \@@_chk_group_begin:n #1
647 {
648 \seq_gpush:Ne \g_@@_group_seq
649 { \int_use:N \tex_currentgrouplevel:D }
650 \cs_set_eq:cN { @@_group_ #1 _chk: } \prg_do_nothing:
651 }
652 \cs_generate_variant:Nn \@@_chk_group_begin:n { e }
653 \cs_new_protected:Npn \@@_chk_group_end:n #1
654 {
655 \seq_gpop:NN \g_@@_group_seq \l_@@_tmpb_tl
656 \bool_lazy_and:nnF
657 {
658 \int_compare_p:nNn
659 { \tex_currentgrouplevel:D } = { \l_@@_tmpb_tl }
660 }
661 { \cs_if_exist_p:c { @@_group_ #1 _chk: } }
662 {
663 \msg_error:nne { cctab } { group-mismatch }
664 {
665 \int_sign:n
666 { \tex_currentgrouplevel:D - \l_@@_tmpb_tl }
667 }
668 }
669 \cs_undefine:c { @@_group_ #1 _chk: }
670 }
671 % \end{macrocode}
672 % \end{macro}
673 %
674 % \begin{macro}{\@@_nesting_number:N,\@@_nesting_number:w}
675 % This macro returns the numeric index of the current catcode table.
676 % In \LuaTeX{} this is just the argument, which is a count reference
677 % to a \tn{catcodetable} register. In other engines, the number is
678 % extracted from the |cctab| variable.
679 % \begin{macrocode}
680 \sys_if_engine_luatex:TF
681 { \cs_new:Npn \@@_nesting_number:N #1 {#1} }
682 {
683 \cs_new:Npn \@@_nesting_number:N #1
684 {
685 \exp_after:wN \exp_after:wN \exp_after:wN \@@_nesting_number:w
686 \exp_after:wN \token_to_str:N #1
687 }
688 \use:e
689 {
690 \cs_new:Npn \exp_not:N \@@_nesting_number:w
691 #1 \tl_to_str:n { g_@@_ } #2 \tl_to_str:n { _cctab } {#2}
692 }
693 }
694 % \end{macrocode}
695 % \end{macro}
696 %
697 % Finally, install some code at the end of the \TeX{} run to check that
698 % all \cs{cctab_begin:N} were ended by some \cs{cctab_end:}.
699 % \begin{macrocode}
700 \cs_if_exist:NT \hook_gput_code:nnn
701 {
702 \hook_gput_code:nnn { enddocument/end } { cctab }
703 {
704 \seq_if_empty:NF \g_@@_stack_seq
705 { \msg_error:nn { cctab } { missing-end } }
706 }
707 }
708 % \end{macrocode}
709 %
710 %
711 % \begin{macro}{\cctab_item:Nn, \cctab_item:cn}
712 % Evaluate the integer argument only once. In most engines the
713 % |cctab| variable only has $256$ entries so we only look up the
714 % catcode for these entries, otherwise we use the current catcode. In
715 % particular, for out-of-range values we use whatever fall-back
716 % \cs{char_value_catcode:n}. In \LuaTeX{}, we use the
717 % |tex.getcatcode| function.
718 % \begin{macrocode}
719 \cs_new:Npn \cctab_item:Nn #1#2
720 { \exp_args:Nf \@@_item:nN { \int_eval:n {#2} } #1 }
721 \sys_if_engine_luatex:TF
722 {
723 \cs_new:Npn \@@_item:nN #1#2
724 { \lua_now:e { tex.print(-2, tex.getcatcode(\int_use:N #2, #1)) } }
725 }
726 {
727 \cs_new:Npn \@@_item:nN #1#2
728 {
729 \int_compare:nNnTF {#1} < { 256 }
730 { \intarray_item:Nn #2 { #1 + 1 } }
731 { \char_value_catcode:n {#1} }
732 }
733 }
734 \cs_generate_variant:Nn \cctab_item:Nn { c }
735 % \end{macrocode}
736 % \end{macro}
737 %
738 % \subsection{Category code table conditionals}
739 %
740 % \begin{macro}[pTF]{\cctab_if_exist:N,\cctab_if_exist:c}
741 % Checks whether a \meta{cctab~var} is defined.
742 % \begin{macrocode}
743 \prg_new_eq_conditional:NNn \cctab_if_exist:N \cs_if_exist:N
744 { TF , T , F , p }
745 \prg_new_eq_conditional:NNn \cctab_if_exist:c \cs_if_exist:c
746 { TF , T , F , p }
747 % \end{macrocode}
748 % \end{macro}
749 %
750 % \begin{macro}[TF]{\@@_chk_if_valid:N}
751 % \begin{macro}{\@@_chk_if_valid_aux:NTF}
752 % Checks whether the argument is defined and whether it is a valid
753 % \meta{cctab~var}. In \LuaTeX{} the validity of the \meta{cctab~var}
754 % is checked by the engine, which complains if the argument is not a
755 % \cs{chardef}'ed constant. In other engines, check if the given
756 % command is an intarray variable (the underlying definition is a copy
757 % of the \texttt{cmr10} font).
758 % \begin{macrocode}
759 \prg_new_protected_conditional:Npnn \@@_chk_if_valid:N #1
760 { TF , T , F }
761 {
762 \cctab_if_exist:NTF #1
763 {
764 \@@_chk_if_valid_aux:NTF #1
765 { \prg_return_true: }
766 {
767 \msg_error:nne { cctab } { invalid-cctab }
768 { \token_to_str:N #1 }
769 \prg_return_false:
770 }
771 }
772 {
773 \msg_error:nne { kernel } { command-not-defined }
774 { \token_to_str:N #1 }
775 \prg_return_false:
776 }
777 }
778 \sys_if_engine_luatex:TF
779 {
780 \cs_new_protected:Npn \@@_chk_if_valid_aux:NTF #1
781 {
782 \int_compare:nNnTF {#1-1} < { \e@alloc@ccodetable@count }
783 }
784 \cs_if_exist:NT \c_syst_catcodes_n
785 {
786 \cs_gset_protected:Npn \@@_chk_if_valid_aux:NTF #1
787 {
788 \int_compare:nTF { #1 <= \c_syst_catcodes_n }
789 }
790 }
791 }
792 {
793 \cs_new_protected:Npn \@@_chk_if_valid_aux:NTF #1
794 {
795 \exp_args:Nf \str_if_in:nnTF
796 { \cs_meaning:N #1 }
797 { select~font~cmr10~at~ }
798 }
799 }
800 % \end{macrocode}
801 % \end{macro}
802 % \end{macro}
803 %
804 % \subsection{Constant category code tables}
805 %
806 % \begin{macro}{\cctab_const:Nn,\cctab_const:cn}
807 % Creates a new \meta{cctab~var} then sets it with the \IniTeX{} and
808 % user-supplied codes. To avoid false \texttt{debug} errors, we write
809 % out implementation of \cs{cctab_new:N} and \cs{cctab_gset:Nn}
810 % instead of directly using them here. The initialization part in
811 % \cs{cctab_new:N} in non-\LuaTeX{} is omitted as it's covered by
812 % the \IniTeX{} settings.
813 % \begin{macrocode}
814 \cs_new_protected:Npn \cctab_const:Nn #1#2
815 {
816 \__kernel_chk_if_free_cs:N #1
817 \@@_new:N #1
818 \group_begin:
819 \cctab_select:N \c_initex_cctab
820 #2 \scan_stop:
821 \@@_gset:n {#1}
822 \group_end:
823 }
824 \cs_generate_variant:Nn \cctab_const:Nn { c }
825 % \end{macrocode}
826 % \end{macro}
827 %
828 % \begin{variable}
829 % {
830 % \c_initex_cctab ,
831 % \c_other_cctab ,
832 % \c_str_cctab
833 % }
834 % Creating category code tables means thinking starting from \IniTeX{}.
835 % For all-other and the standard \enquote{string} tables that's easy.
836 % \begin{macrocode}
837 \cctab_new:N \c_initex_cctab
838 \cctab_const:Nn \c_other_cctab
839 {
840 \cctab_select:N \c_initex_cctab
841 \int_set:Nn \tex_endlinechar:D { -1 }
842 \int_step_inline:nnn { 0 } { 127 }
843 { \char_set_catcode_other:n {#1} }
844 }
845 \cctab_const:Nn \c_str_cctab
846 {
847 \cctab_select:N \c_other_cctab
848 \char_set_catcode_space:n { 32 }
849 }
850 % \end{macrocode}
851 % \end{variable}
852 %
853 % \begin{variable}{\c_code_cctab, \c_document_cctab}
854 % To pick up document-level category codes, we need to delay set up to the
855 % end of the format, where that's possible. Also, as there are a \emph{lot}
856 % of category codes to set, we avoid using the official interface and store the
857 % document codes using internal code. Depending on whether we are in the hook
858 % or not, the catcodes may be code or document, so we explicitly set up both
859 % correctly.
860 % \begin{macrocode}
861 \cs_if_exist:NTF \@expl@finalise@setup@@@@
862 { \tl_gput_right:Nn \@expl@finalise@setup@@@@ }
863 { \use:n }
864 {
865 \@@_new:N \c_code_cctab
866 \group_begin:
867 \int_set:Nn \tex_endlinechar:D { 32 }
868 \char_set_catcode_invalid:n { 0 }
869 \sys_if_engine_opentype:TF
870 { \int_step_function:nN { 31 } \char_set_catcode_invalid:n }
871 { \int_step_function:nN { 31 } \char_set_catcode_active:n }
872 \int_step_function:nnN { 33 } { 64 } \char_set_catcode_other:n
873 \int_step_function:nnN { 65 } { 90 } \char_set_catcode_letter:n
874 \int_step_function:nnN { 91 } { 96 } \char_set_catcode_other:n
875 \int_step_function:nnN { 97 } { 122 } \char_set_catcode_letter:n
876 \char_set_catcode_ignore:n { 9 } % tab
877 \char_set_catcode_other:n { 10 } % lf
878 \char_set_catcode_active:n { 12 } % ff
879 \char_set_catcode_end_line:n { 13 } % cr
880 \char_set_catcode_ignore:n { 32 } % space
881 \char_set_catcode_parameter:n { 35 } % hash
882 \char_set_catcode_math_toggle:n { 36 } % dollar
883 \char_set_catcode_comment:n { 37 } % percent
884 \char_set_catcode_alignment:n { 38 } % ampersand
885 \char_set_catcode_letter:n { 58 } % colon
886 \char_set_catcode_escape:n { 92 } % backslash
887 \char_set_catcode_math_superscript:n { 94 } % circumflex
888 \char_set_catcode_letter:n { 95 } % underscore
889 \char_set_catcode_group_begin:n { 123 } % left brace
890 \char_set_catcode_other:n { 124 } % pipe
891 \char_set_catcode_group_end:n { 125 } % right brace
892 \char_set_catcode_space:n { 126 } % tilde
893 \char_set_catcode_invalid:n { 127 } % ^^?
894 \sys_if_engine_opentype:F
895 { \int_step_function:nnN { 128 } { 255 } \char_set_catcode_active:n }
896 \@@_gset:n { \c_code_cctab }
897 \group_end:
898 \cctab_const:Nn \c_document_cctab
899 {
900 \cctab_select:N \c_code_cctab
901 \int_set:Nn \tex_endlinechar:D { 13 }
902 \char_set_catcode_space:n { 9 }
903 \char_set_catcode_space:n { 32 }
904 \char_set_catcode_other:n { 58 }
905 \char_set_catcode_math_subscript:n { 95 }
906 \char_set_catcode_active:n { 126 }
907 }
908 }
909 % \end{macrocode}
910 % \end{variable}
911 %
912 % \begin{variable}{\g_tmpa_cctab, \g_tmpb_cctab}
913 % \begin{macrocode}
914 \cctab_new:N \g_tmpa_cctab
915 \cctab_new:N \g_tmpb_cctab
916 % \end{macrocode}
917 % \end{variable}
918 %
919 % \subsection{Messages}
920 %
921 % \begin{macrocode}
922 \msg_new:nnnn { cctab } { stack-full }
923 { The~category~code~table~stack~is~exhausted. }
924 {
925 LaTeX~has~been~asked~to~switch~to~a~new~category~code~table,~
926 but~there~is~no~more~space~to~do~this!
927 }
928 \msg_new:nnnn { cctab } { extra-end }
929 { Extra~\iow_char:N\\cctab_end:~ignored~\msg_line_context:. }
930 {
931 LaTeX~came~across~a~\iow_char:N\\cctab_end:~without~a~matching~
932 \iow_char:N\\cctab_begin:N.~This~command~will~be~ignored.
933 }
934 \msg_new:nnnn { cctab } { missing-end }
935 { Missing~\iow_char:N\\cctab_end:~before~end~of~TeX~run. }
936 {
937 LaTeX~came~across~more~\iow_char:N\\cctab_begin:N~than~
938 \iow_char:N\\cctab_end:.
939 }
940 \msg_new:nnnn { cctab } { invalid-cctab }
941 { Invalid~\iow_char:N\\catcode~table. }
942 {
943 You~can~only~switch~to~a~\iow_char:N\\catcode~table~that~is~
944 initialized~using~\iow_char:N\\cctab_new:N~or~
945 \iow_char:N\\cctab_const:Nn.
946 }
947 \msg_new:nnnn { cctab } { group-mismatch }
948 {
949 \iow_char:N\\cctab_end:~occurred~in~a~
950 \int_case:nn {#1}
951 {
952 { 0 } { different~group }
953 { 1 } { higher~group~level }
954 { -1 } { lower~group~level }
955 } ~than~
956 the~matching~\iow_char:N\\cctab_begin:N.
957 }
958 {
959 Catcode~tables~and~groups~must~be~properly~nested,~but~
960 you~tried~to~interleave~them.~LaTeX~will~try~to~proceed,~
961 but~results~may~be~unexpected.
962 }
963 \prop_gput:Nnn \g_msg_module_name_prop { cctab } { LaTeX }
964 \prop_gput:Nnn \g_msg_module_type_prop { cctab } { }
965 % \end{macrocode}
966 %
967 % \begin{macrocode}
968 %</code>
969 % \end{macrocode}
970 %
971 % \end{implementation}
972 %
973 %\PrintIndex