Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3text.dtx
4 %
5 % Copyright (C) 2020-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 % https://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,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3text} module\\ Text processing^^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 % This module deals with manipulation of (formatted) text; such material is
53 % comprised of a restricted set of token list content. The functions provided
54 % here concern conversion of textual content for example in case changing,
55 % generation of bookmarks and extraction to tags. All of the major functions
56 % operate by expansion. Begin-group and end-group tokens in the \meta{text}
57 % are normalized and become |{| and |}|, respectively.
58 %
59 % \section{Expanding text}
60 %
61 % \begin{function}[EXP, added = 2020-01-02, updated = 2023-06-09]
62 % {\text_expand:n}
63 % \begin{syntax}
64 % \cs{text_expand:n} \Arg{text}
65 % \end{syntax}
66 % Takes user input \meta{text} and expands the content.
67 % Protected commands (typically
68 % formatting) are left in place, and no processing of
69 % math mode material (as delimited by pairs given in
70 % \cs{l_text_math_delims_tl} or as the argument to commands listed
71 % in \cs{l_text_math_arg_tl}) takes place. Commands which are neither engine-
72 % nor \LaTeX{}-protected are expanded exhaustively.
73 % Any commands listed in \cs{l_text_expand_exclude_tl} are excluded from
74 % expansion, as are those in \cs{l_text_case_exclude_arg_tl} and
75 % \cs{l_text_math_arg_tl}.
76 % \end{function}
77 %
78 % \begin{function}[added = 2020-01-22]
79 % {
80 % \text_declare_expand_equivalent:Nn ,
81 % \text_declare_expand_equivalent:cn
82 % }
83 % \begin{syntax}
84 % \cs{text_declare_expand_equivalent:Nn} \meta{cmd} \Arg{replacement}
85 % \end{syntax}
86 % Declares that the \meta{replacement} tokens should be used whenever the
87 % \meta{cmd} (a single token) is encountered. The \meta{replacement} tokens
88 % should be expandable. A token can be \enquote{replaced} by itself if
89 % the defined replacement wraps it in \cs{exp_not:n}, for example
90 % \begin{verbatim}
91 % \text_declare_expand_equivalent:Nn \' { \exp_not:n { \' } }
92 % \end{verbatim}
93 % \end{function}
94 %
95 % \section{Case changing}
96 %
97 % \begin{function}[EXP, added = 2019-11-20, updated = 2023-07-08]
98 % {
99 % \text_lowercase:n, \text_uppercase:n, \text_titlecase_all:n,
100 % \text_titlecase_first:n,
101 % \text_lowercase:nn, \text_lowercase:Vn, \text_lowercase:en,
102 % \text_uppercase:nn, \text_uppercase:Vn, \text_uppercase:en,
103 % \text_titlecase_all:nn, \text_titlecase_all:Vn,
104 % \text_titlecase_all:en,
105 % \text_titlecase_first:nn, \text_titlecase_first:Vn,
106 % \text_titlecase_first:en
107 % }
108 % \begin{syntax}
109 % \cs{text_uppercase:n} \Arg{tokens}
110 % \cs{text_uppercase:nn} \Arg{BCP-47} \Arg{tokens}
111 % \end{syntax}
112 % Takes user input \meta{text} first applies \cs{text_expand:n}, then
113 % transforms the case of character tokens as specified by the
114 % function name. The category code of letters are not changed by this
115 % process when Unicode engines are used; in $8$-bit engines, case changed
116 % charters in the ASCII range will have the current prevailing category code,
117 % while those outside of it will be represented by active characters.
118 % \end{function}
119 %
120 % Upper- and lowercase have the obvious meanings. Titlecasing may be regarded
121 % informally as converting the first \emph{non-space} character of the
122 % \meta{tokens} to uppercase. However, the process is more complex
123 % than this as there are some situations where a single lowercase character
124 % maps to a special form, for example \texttt{ij} in Dutch which becomes
125 % \texttt{IJ}. There are two functions available for titlecasing: one which
126 % applies the change to each \enquote{word} and a second which only applies
127 % at the start of the input. (Here, \enquote{word} boundaries are spaces:
128 % at present, full Unicode word breaking is not attempted.)
129 %
130 % Importantly, notice that these functions are intended for working with
131 % user \emph{text for typesetting}. For case changing programmatic data see
132 % the \pkg{l3str} module and discussion there of \cs{str_lowercase:n},
133 % \cs{str_uppercase:n} and \cs{str_casefold:n}.
134 %
135 % Case changing does not take place within math mode material so for example
136 % \begin{verbatim}
137 % \text_uppercase:n { Some~text~$y = mx + c$~with~{Braces} }
138 % \end{verbatim}
139 % becomes
140 % \begin{verbatim}
141 % SOME TEXT $y = mx + c$ WITH {BRACES}
142 % \end{verbatim}
143 %
144 % The first mandatory argument of commands listed in
145 % \cs{l_text_case_exclude_arg_tl}
146 % is excluded from case changing; the latter are entirely non-textual
147 % content (such as labels).
148 %
149 % The standard mappings
150 % here follow those defined by the \href{http://www.unicode.org}^^A
151 % {Unicode Consortium} in \texttt{UnicodeData.txt} and
152 % \texttt{SpecialCasing.txt}. For \pTeX{}, only the ASCII range is
153 % covered as the engine treats input outside of this range as east Asian.
154 %
155 % Locale-sensitive conversions are enabled using the \meta{BCP-47}
156 % argument, and follow Unicode Consortium guidelines. Currently, the
157 % locale strings recognized for special handling are as follows.
158 % \begin{itemize}
159 % \item Armenian (\texttt{hy} and \texttt{hy-x-yiwn})
160 % The setting \texttt{hy} maps the codepoint U+0587, the ligature of
161 % letters ech and yiwn, to the codepoints for capital ech and vew
162 % when uppercasing: this follows the spelling reform which is used
163 % in Armenia. The alternative \texttt{hy-x-yiwn} maps U+0587 to
164 % capital ech and yiwn on uppercasing (also the output if Armenian
165 % is not selected at all).
166 % \item Azeri and Turkish (\texttt{az} and \texttt{tr}).
167 % The case pairs I/i-dotless and I-dot/i are activated for these
168 % languages. The combining dot mark is removed when lowercasing
169 % I-dot and introduced when upper casing i-dotless.
170 % \item German (\texttt{de-x-eszett}).
171 % An alternative mapping for German in which the lowercase
172 % \emph{Eszett} maps to a \emph{gro\ss{}es Eszett}.
173 % \item Greek (\texttt{el}).
174 % Removes accents from Greek letters when uppercasing; titlecasing
175 % leaves accents in place. A variant \texttt{el-x-iota} is available
176 % which converts the \textit{ypogegrammeni} (subscript muted iota)
177 % to capital iota when uppercasing: the standard version retains the
178 % subscript versions.
179 % \item Lithuanian (\texttt{lt}).
180 % The lowercase letters i and j should retain a dot above when the
181 % accents grave, acute or tilde are present. This is implemented for
182 % lowercasing of the relevant uppercase letters both when input as
183 % single Unicode codepoints and when using combining accents. The
184 % combining dot is removed when uppercasing in these cases. Note that
185 % \emph{only} the accents used in Lithuanian are covered: the behavior
186 % of other accents are not modified.
187 % \item Medieval Latin (\texttt{la-x-medieval}).
188 % The characters |u| and |V| are interchanged on case changing.
189 % \item Dutch (\texttt{nl}).
190 % Capitalization of \texttt{ij} at the beginning of titlecased
191 % input produces \texttt{IJ} rather than \texttt{Ij}.
192 % \end{itemize}
193 %
194 % Determining whether non-letter characters at the start of text should count
195 % as the uppercase element is controllable. When
196 % \cs{l_text_titlecase_check_letter_bool} is \texttt{true}, codepoints which are
197 % not letters (Unicode general category \texttt{L}) are not changed, and only
198 % the first \emph{letter} is uppercased.
199 % When \cs{l_text_titlecase_check_letter_bool} is \texttt{false}, the first
200 % codepoint is uppercased, irrespective of the general code of the character.
201 %
202 % \begin{function}[added = 2022-07-04]
203 % {\text_declare_case_equivalent:Nn}
204 % \begin{syntax}
205 % \cs{text_declare_case_equivalent:Nn} \meta{cmd} \Arg{replacement}
206 % \end{syntax}
207 % Declares that the \meta{replacement} tokens should be used whenever the
208 % \meta{cmd} (a single token) is encountered during case changing.
209 % \end{function}
210 %
211 % \begin{function}[added = 2023-04-11, updated = 2023-04-20]
212 % {
213 % \text_declare_lowercase_mapping:nn ,
214 % \text_declare_lowercase_mapping:nnn ,
215 % \text_declare_titlecase_mapping:nn ,
216 % \text_declare_titlecase_mapping:nnn ,
217 % \text_declare_uppercase_mapping:nn ,
218 % \text_declare_uppercase_mapping:nnn
219 % }
220 % \begin{syntax}
221 % \cs{text_declare_lowercase_mapping:nn} \Arg{codepoint} \Arg{replacement}
222 % \cs{text_declare_lowercase_mapping:nnn} \Arg{BCP-47} \Arg{codepoint} \\
223 % ~~\Arg{replacement}
224 % \end{syntax}
225 % Declares that the \meta{replacement} tokens should be used when case mapping
226 % the \meta{codepoint}, rather than the standard mapping given in the
227 % Unicode data files. The \texttt{nnn} version takes a BCP-47 tag, which
228 % can be used to specify that the customization only applies to that
229 % locale.
230 % \end{function}
231 %
232 % \begin{function}[added = 2025-06-24]
233 % {
234 % \text_declare_lowercase_exclusion:n ,
235 % \text_declare_titlecase_exclusion:n ,
236 % \text_declare_uppercase_exclusion:n
237 % }
238 % \begin{syntax}
239 % \cs{text_declare_lowercase_exclusion:n} \Arg{word}
240 % \end{syntax}
241 % Declares that the \meta{word} is excluded from case changing for the
242 % appropriate operation.
243 % \end{function}
244 %
245 % \begin{function}[EXP, added = 2022-07-04]{\text_case_switch:nnnn}
246 % \begin{syntax}
247 % \cs{text_case_switch:nnnn} \Arg{normal} \Arg{upper} \Arg{lower} \Arg{title}
248 % \end{syntax}
249 % Context-sensitive function which will expand to one of the \meta{normal},
250 % \meta{upper}, \meta{lower} or \meta{title} tokens depending on the current
251 % case changing operation. Outside of case changing, the \meta{normal} tokens
252 % are produced. Within case changing, the appropriate mapping tokens are
253 % inserted.
254 % \end{function}
255 %
256 % \section{Removing formatting from text}
257 %
258 % \begin{function}[EXP, added = 2020-03-05, updated = 2020-05-14]
259 % {\text_purify:n, \text_purify:V, \text_purify:v}
260 % \begin{syntax}
261 % \cs{text_purify:n} \Arg{text}
262 % \end{syntax}
263 % Takes user input \meta{text} and expands as described for
264 % \cs{text_expand:n}, then removes all functions from the resulting
265 % text. Math mode material (as delimited by pairs given in
266 % \cs{l_text_math_delims_tl} or as the argument to commands listed in
267 % \cs{l_text_math_arg_tl}) is left contained in a pair of |$| delimiters.
268 % Non-expandable functions present in the \meta{text} must either have a
269 % defined equivalent (see \cs{text_declare_purify_equivalent:Nn}) or will
270 % be removed from the result. Implicit tokens are converted to their
271 % explicit equivalent.
272 % \end{function}
273 %
274 % \begin{function}[added = 2020-03-05]
275 % {
276 % \text_declare_purify_equivalent:Nn ,
277 % \text_declare_purify_equivalent:Ne
278 % }
279 % \begin{syntax}
280 % \cs{text_declare_purify_equivalent:Nn} \meta{cmd} \Arg{replacement}
281 % \end{syntax}
282 % Declares that the \meta{replacement} tokens should be used whenever the
283 % \meta{cmd} (a single token) is encountered. The \meta{replacement} tokens
284 % should be expandable.
285 % \end{function}
286 %
287 % \section{Control variables}
288 %
289 % \begin{variable}{\l_text_math_arg_tl}
290 % Lists commands present in the \meta{text} where the argument of the
291 % command should be treated as math mode material. The treatment here is
292 % similar to \cs{l_text_math_delims_tl} but for a command rather than
293 % paired delimiters.
294 % \end{variable}
295 %
296 % \begin{variable}{\l_text_math_delims_tl}
297 % Lists pairs of tokens which delimit (in-line) math mode content; such
298 % content \emph{may} be excluded from processing.
299 % \end{variable}
300 %
301 % \begin{variable}{\l_text_case_exclude_arg_tl}
302 % Lists commands where the first mandatory argument is excluded from
303 % case changing.
304 % \end{variable}
305 %
306 % \begin{variable}{\l_text_expand_exclude_tl}
307 % Lists commands which are excluded from expansion. This protection
308 % includes everything up to and including their first braced argument.
309 % \end{variable}
310 %
311 % \begin{variable}{\l_text_titlecase_check_letter_bool}
312 % Controls how the start of titlecasing is handled: when \texttt{true}, the
313 % first \emph{letter} in text is considered. The standard setting is
314 % \texttt{true}.
315 % \end{variable}
316 %
317 % \section{Mapping to text}
318 %
319 % Grapheme splitting is implemented using the algorithm described in Unicode
320 % Standard Annex \#29. This includes support for extended grapheme clusters.
321 % Leading line feeds or carriage returns will be dropped due to standard
322 % \TeX{} processing. At present extended pictograms are
323 % not supported: these may be added in a future release. Some aspects of Indic
324 % grapheme breaking, introduced in Unicode~15, are also currently absent.
325 % In these functions, math mode is treated as a single indivisible unit,
326 % i.e.~one \enquote{word} or grapheme.
327 %
328 % \begin{function}[rEXP, added = 2022-08-04, updated = 2025-07-11]
329 % {\text_map_function:nN}
330 % \begin{syntax}
331 % \cs{text_map_function:nN} \Arg{text} \meta{function}
332 % \end{syntax}
333 % This takes the user input in \meta{text} and expands it as with
334 % \cs{text_expand:n}; it then maps over the \emph{graphemes} within the
335 % result, passing each grapheme to the \meta{function}.
336 % Broadly a grapheme is a \enquote{user perceived character}:
337 % the Unicode Consortium describe the decomposition of input to
338 % graphemes in depth, and the approach used here implements that
339 % algorithm. The \meta{function} should accept one argument as
340 % \meta{balanced text}: this may comprise codepoints or it may be a
341 % control sequence.
342 % With $8$-bit engines, the codepoint(s) themselves may of course be
343 % made up of multiple bytes: the mapping will pass the correct codepoints
344 % independent of the engine in use.
345 % See also \cs{text_map_inline:nn}.
346 % \end{function}
347 %
348 % \begin{function}[rEXP, added = 2025-06-22, updated = 2025-07-11]{\text_map_tokens:nn}
349 % \begin{syntax}
350 % \cs{text_map_tokens:nn} \Arg{text} \Arg{code}
351 % \end{syntax}
352 % This takes the user input in \meta{text} and expands it as with
353 % \cs{text_expand:n}; it then maps over the \emph{graphemes} within the
354 % result, passing each grapheme to the \meta{code} as a trailing brace group.
355 % Broadly a grapheme is a \enquote{user perceived character}:
356 % the Unicode Consortium describe the decomposition of input to
357 % graphemes in depth, and the approach used here implements that
358 % algorithm. The \meta{code} should accept one argument as
359 % \meta{balanced text}: this may comprise codepoints or it may be a
360 % control sequence.
361 % With $8$-bit engines, the codepoint(s) themselves may of course be
362 % made up of multiple bytes: the mapping will pass the correct codepoints
363 % independent of the engine in use.
364 % See also \cs{text_map_inline:nn}.
365 % \end{function}
366 %
367 % \begin{function}[added = 2022-08-04, updated = 2025-07-11]{\text_map_inline:nn}
368 % \begin{syntax}
369 % \cs{text_map_inline:nn} \Arg{text} \Arg{inline function}
370 % \end{syntax}
371 % This takes the user input in \meta{text} and expands it as with
372 % \cs{text_expand:n}; it then maps over the \emph{graphemes} within the
373 % result, passing each grapheme to the \meta{inline function}.
374 % Broadly a grapheme is a \enquote{user perceived character}:
375 % the Unicode Consortium describe the decomposition of input to
376 % graphemes in depth, and the approach used here implements that
377 % algorithm. The \meta{inline function} should consist of code which
378 % receives the grapheme as \meta{balanced text}: this may comprise
379 % codepoints or it may be a control sequence.
380 % With $8$-bit engines, the codepoint(s) themselves may of course be
381 % made up of multiple bytes: the mapping will pass the correct codepoints
382 % independent of the engine in use.
383 % See also \cs{text_map_function:nN}.
384 % \end{function}
385 %
386 % Word breaking is implemented using the standard algorithm described in Unicode
387 % Standard Annex \#29. Leading line feeds or carriage returns will be dropped due
388 % to standard \TeX{} processing. Spaces are always
389 % considered a break even if immediately followed by an extending character.
390 % At present extended pictograms are not supported: these may be added in a future
391 % release. Language-based tailoring may be added in a future release: at present
392 % the algorithm is exactly that described in the annex.
393 %
394 % \begin{function}[rEXP, added = 2025-02-12, updated = 2025-07-11]{\text_words_map_function:nN}
395 % \begin{syntax}
396 % \cs{text_words_map_function:nN} \Arg{text} \meta{function}
397 % \end{syntax}
398 % This takes the user input in \meta{text} and expands it as with
399 % \cs{text_expand:n}; it then maps over the \emph{words} within the
400 % result, passing each word to the \meta{function}. Word boundaries are
401 % determined using the standard algorithm described by the Unicode
402 % Consortium. The \meta{function} should accept one argument as
403 % \meta{balanced text}: this may comprise codepoints or it may be a
404 % control sequence. With $8$-bit engines, the codepoint(s) themselves
405 % may of course be made up of multiple bytes: the mapping will pass the
406 % correct codepoints independent of the engine in use.
407 % See also \cs{text_words_map_inline:nn}.
408 % \end{function}
409 %
410 % \begin{function}[rEXP, added = 2025-06-22, updated = 2025-07-11]{\text_words_map_tokens:nn}
411 % \begin{syntax}
412 % \cs{text_words_map_tokens:nn} \Arg{text} \Arg{code}
413 % \end{syntax}
414 % This takes the user input in \meta{text} and expands it as with
415 % \cs{text_expand:n}; it then maps over the \emph{words} within the
416 % result, passing each word to the \meta{code} as a trailing brace
417 % group. Word boundaries are
418 % determined using the standard algorithm described by the Unicode
419 % Consortium. The \meta{code} should accept one argument as
420 % \meta{balanced text}: this may comprise codepoints or it may be a
421 % control sequence. With $8$-bit engines, the codepoint(s) themselves
422 % may of course be made up of multiple bytes: the mapping will pass the
423 % correct codepoints independent of the engine in use.
424 % See also \cs{text_words_map_inline:nn}.
425 % \end{function}
426 %
427 % \begin{function}[rEXP, added = 2025-02-12, updated = 2025-07-11]{\text_words_map_inline:nn}
428 % \begin{syntax}
429 % \cs{text_words_map_inline:nn} \Arg{text} \Arg{inline function}
430 % \end{syntax}
431 % This takes the user input in \meta{text} and expands it as with
432 % \cs{text_expand:n}; it then maps over the \emph{words} within the
433 % result, passing each word to the \meta{function}. Word boundaries are
434 % determined using the standard algorithm described by the Unicode
435 % Consortium. The \meta{inline function} should consist of code that will
436 % accept one argument as \meta{balanced text}: this may comprise codepoints
437 % or it may be a control sequence. With $8$-bit engines, the codepoint(s)
438 % themselves may of course be made up of multiple bytes: the mapping will
439 % pass the correct codepoints independent of the engine in use.
440 % See also \cs{text_words_map_function:nN}.
441 % \end{function}
442 %
443 % \begin{function}[EXP, added = 2022-08-04]
444 % {\text_map_break:, \text_map_break:n}
445 % \begin{syntax}
446 % \cs{text_map_break:}
447 % \cs{text_map_break:n} \Arg{code}
448 % \end{syntax}
449 % Used to terminate a \cs[no-index]{text_map_\ldots} or
450 % \cs[no-index]{text_words_map_\ldots} function before all
451 % entries in the \meta{text} have been processed. This
452 % normally takes place within a conditional statement.
453 % \end{function}
454 %
455 % \subsection{Support utilities}
456 %
457 % Implementing text functions requires some support interfaces which do not
458 % directly handle text input but are best provided in this module.
459 %
460 % \begin{function}[rEXP, added = 2026-02-06]{\text_bcp_parse:n}
461 % \begin{syntax}
462 % \cs{text_bcp_parse:n} \Arg{BCP~47}
463 % \end{syntax}
464 % Used to parse a BCP~47 string to allow branching in text processing:
465 % see \url{https://en.wikipedia.org/wiki/IETF_language_tag} for an
466 % introduction to BCP~47 tags. The \meta{BCP~47} input is converted to a
467 % string and parsed such that full expansion results in seven brace
468 % groups for any valid input. Other than the first brace group,
469 % these may be empty. Where multiple subtags are allowed for one type
470 % (for example variant), nested brace groups are created. The full list
471 % is
472 % \begin{enumerate}
473 % \item The language subtag, for example |en| (English) or |gsw|
474 % (Swiss German/Schweizerdeutsch); this is the only required subtag
475 % and so will always have content if the input was valid.
476 % \item Extended language subtags: may contain up to three
477 % brace groups for these subtags, for example |en-419|
478 % will product |{419}| as the content of this subtag.
479 % \item The script subtag, for example |Latn|
480 % \item The region subtag, for example |GB|
481 % \item Variant subtags: contains an open-ended list of braced
482 % groups, each containing one variant.
483 % \item Extension subtags. The result here will comprise of the
484 % extension identifier (a single letter) followed by a brace
485 % group containing nested brace groups of each subtag. For example,
486 % the full input |gsw-u-sd-chzh| (Swiss German as used in the Canton
487 % of Zurich) would result in the extension subtag group containing
488 % |u{sd}{chzh}|.
489 % \item Private-use subtags: contains an open-ended list of braced
490 % groups, each containing one private subtag.
491 % \end{enumerate}
492 % Whilst the result of expansion is a token list, the parsed material is
493 % detokenized, i.e.~all category code 12 (spaces are not possible). The
494 % output of the function is case folded.
495 % \end{function}
496 %
497 % \end{documentation}
498 %
499 % \begin{implementation}
500 %
501 % \section{\pkg{l3text} implementation}
502 %
503 % \begin{macrocode}
504 %<*code>
505 % \end{macrocode}
506 %
507 % \begin{macrocode}
508 %<@@=text>
509 % \end{macrocode}
510 %
511 % \begin{macrocode}
512 \cs_generate_variant:Nn \tl_if_head_eq_meaning_p:nN { o }
513 % \end{macrocode}
514 %
515 % \subsection{Internal auxiliaries}
516 %
517 % \begin{variable}{\s_@@_stop}
518 % Internal scan marks.
519 % \begin{macrocode}
520 \scan_new:N \s_@@_stop
521 % \end{macrocode}
522 % \end{variable}
523 %
524 % \begin{variable}{\q_@@_nil}
525 % Internal quarks.
526 % \begin{macrocode}
527 \quark_new:N \q_@@_nil
528 % \end{macrocode}
529 % \end{variable}
530 %
531 % \begin{macro}[pTF]{\@@_quark_if_nil:n}
532 % Branching quark conditional.
533 % \begin{macrocode}
534 \__kernel_quark_new_conditional:Nn \@@_quark_if_nil:n { TF }
535 % \end{macrocode}
536 % \end{macro}
537 %
538 % \begin{variable}{\q_@@_recursion_tail,\q_@@_recursion_stop}
539 % Internal recursion quarks.
540 % \begin{macrocode}
541 \quark_new:N \q_@@_recursion_tail
542 \quark_new:N \q_@@_recursion_stop
543 % \end{macrocode}
544 % \end{variable}
545 %
546 % \begin{macro}[EXP]{\@@_use_i_delimit_by_q_recursion_stop:nw}
547 % Functions to gobble up to a quark.
548 % \begin{macrocode}
549 \cs_new:Npn \@@_use_i_delimit_by_q_recursion_stop:nw
550 #1 #2 \q_@@_recursion_stop {#1}
551 % \end{macrocode}
552 % \end{macro}
553 %
554 % \begin{macro}[EXP]{\@@_if_q_recursion_tail_stop_do:Nn}
555 % \begin{macro}[EXP]{\@@_if_q_recursion_tail_stop_do:nn}
556 % Functions to query recursion quarks.
557 % \begin{macrocode}
558 \__kernel_quark_new_test:N \@@_if_q_recursion_tail_stop_do:Nn
559 \__kernel_quark_new_test:N \@@_if_q_recursion_tail_stop_do:nn
560 % \end{macrocode}
561 % \end{macro}
562 % \end{macro}
563 %
564 % \begin{variable}{\s_@@_recursion_tail,\s_@@_recursion_stop}
565 % Internal scan marks quarks.
566 % \begin{macrocode}
567 \scan_new:N \s_@@_recursion_tail
568 \scan_new:N \s_@@_recursion_stop
569 % \end{macrocode}
570 % \end{variable}
571 %
572 % \begin{macro}[EXP]{\@@_use_i_delimit_by_s_recursion_stop:nw}
573 % Functions to gobble up to a scan mark.
574 % \begin{macrocode}
575 \cs_new:Npn \@@_use_i_delimit_by_s_recursion_stop:nw
576 #1 #2 \s_@@_recursion_stop {#1}
577 % \end{macrocode}
578 % \end{macro}
579 %
580 % \begin{macro}[EXP]{\@@_if_s_recursion_tail_stop_do:Nn}
581 % Functions to query recursion scan marks. Slower than a quark
582 % test but needed to avoid issues in the outer expansion loop
583 % with unterminated \tn{romannumeral} primitives.
584 % \begin{macrocode}
585 \cs_new:Npn \@@_if_s_recursion_tail_stop_do:Nn #1
586 {
587 \bool_lazy_and:nnTF
588 { \cs_if_eq_p:NN \s_@@_recursion_tail #1 }
589 { \str_if_eq_p:nn { \s_@@_recursion_tail } {#1} }
590 { \@@_use_i_delimit_by_s_recursion_stop:nw }
591 { \use_none:n }
592 }
593 % \end{macrocode}
594 % \end{macro}
595 %
596 % \subsection{Utilities}
597 %
598 % \begin{macro}{\@@_sep:}
599 % \begin{macrocode}
600 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
601 % \end{macrocode}
602 % \end{macro}
603 %
604 % \begin{macro}[EXP]
605 % {
606 % \@@_token_to_explicit:N ,
607 % \@@_token_to_explicit_char:N ,
608 % \@@_token_to_explicit_cs:N ,
609 % \@@_token_to_explicit_cs_aux:N
610 % }
611 % \begin{macro}[EXP]{\@@_token_to_explicit:n}
612 % \begin{macro}[EXP]
613 % {
614 % \@@_token_to_explicit_auxi:w ,
615 % \@@_token_to_explicit_auxii:w ,
616 % \@@_token_to_explicit_auxiii:w
617 % }
618 % The idea here is to take a token and ensure that if it's an implicit
619 % char, we output the explicit version. Otherwise, the token needs to be
620 % unchanged. First, we have to split between control sequences and everything
621 % else.
622 % \begin{macrocode}
623 \group_begin:
624 \char_set_catcode_active:n { 0 }
625 \cs_new:Npn \@@_token_to_explicit:N #1
626 {
627 \if_catcode:w \exp_not:N #1
628 \if_catcode:w \scan_stop: \exp_not:N #1
629 \scan_stop:
630 \else:
631 \exp_not:N ^^@
632 \fi:
633 \exp_after:wN \@@_token_to_explicit_cs:N
634 \else:
635 \exp_after:wN \@@_token_to_explicit_char:N
636 \fi:
637 #1
638 }
639 \group_end:
640 % \end{macrocode}
641 % For control sequences, we can check for macros versus other cases using
642 % \cs{if_meaning:w}, then explicitly check for \tn{chardef} and
643 % \tn{mathchardef}.
644 % \begin{macrocode}
645 \cs_new:Npn \@@_token_to_explicit_cs:N #1
646 {
647 \exp_after:wN \if_meaning:w \exp_not:N #1 #1
648 \exp_after:wN \use:nn \exp_after:wN
649 \@@_token_to_explicit_cs_aux:N
650 \else:
651 \exp_after:wN \exp_not:n
652 \fi:
653 {#1}
654 }
655 \cs_new:Npn \@@_token_to_explicit_cs_aux:N #1
656 {
657 \bool_lazy_or:nnTF
658 { \token_if_chardef_p:N #1 }
659 { \token_if_mathchardef_p:N #1 }
660 {
661 \char_generate:nn {#1}
662 {
663 \if_int_compare:w \char_value_catcode:n {#1} = 10 \exp_stop_f:
664 10
665 \else:
666 12
667 \fi:
668 }
669 }
670 {#1}
671 }
672 % \end{macrocode}
673 % For character tokens, we need to filter out the implicit characters from
674 % those that are explicit. That's done here, then if necessary we work out
675 % the category code and generate the char. To avoid issues with alignment
676 % tabs, that one is done by elimination rather than looking up the code
677 % explicitly. The trick with finding the charcode is that the \TeX{}
678 % messages are either \texttt{the \meta{something} character \meta{char}}
679 % or \texttt{the \meta{type} \meta{char}}.
680 % \begin{macrocode}
681 \cs_new:Npn \@@_token_to_explicit_char:N #1
682 {
683 \if:w
684 \if_catcode:w ^ \exp_args:No \str_tail:n { \token_to_str:N #1 } ^
685 \token_to_str:N #1 #1
686 \else:
687 AB
688 \fi:
689 \exp_after:wN \exp_not:n
690 \else:
691 \exp_after:wN \@@_token_to_explicit:n
692 \fi:
693 {#1}
694 }
695 \cs_new:Npn \@@_token_to_explicit:n #1
696 {
697 \exp_after:wN \@@_token_to_explicit_auxi:w
698 \int_value:w
699 \if_catcode:w \c_group_begin_token #1 1 \else:
700 \if_catcode:w \c_group_end_token #1 2 \else:
701 \if_catcode:w \c_math_toggle_token #1 3 \else:
702 \if_catcode:w ## #1 6 \else:
703 \if_catcode:w ^ #1 7 \else:
704 \if_catcode:w \c_math_subscript_token #1 8 \else:
705 \if_catcode:w \c_space_token #1 10 \else:
706 \if_catcode:w A #1 11 \else:
707 \if_catcode:w + #1 12 \else:
708 4 \fi: \fi: \fi: \fi: \fi: \fi: \fi: \fi: \fi:
709 \exp_after:wN \@@_sep:
710 \token_to_meaning:N #1 \s_@@_stop
711 }
712 \cs_new:Npn \@@_token_to_explicit_auxi:w #1 \@@_sep: #2 \s_@@_stop
713 {
714 \char_generate:nn
715 {
716 \if_int_compare:w #1 < 9 \exp_stop_f:
717 \exp_after:wN \@@_token_to_explicit_auxii:w
718 \else:
719 \exp_after:wN \@@_token_to_explicit_auxiii:w
720 \fi:
721 #2
722 }
723 {#1}
724 }
725 \exp_last_unbraced:NNNNo \cs_new:Npn \@@_token_to_explicit_auxii:w
726 #1 { \tl_to_str:n { character ~ } } { ` }
727 \cs_new:Npn \@@_token_to_explicit_auxiii:w #1 ~ #2 ~ { ` }
728 % \end{macrocode}
729 % \end{macro}
730 % \end{macro}
731 % \end{macro}
732 %
733 % \begin{macro}[EXP]{\@@_char_catcode:N}
734 % An idea from \pkg{l3char}: we need to get the category code of
735 % a specific token, not the general case.
736 % \begin{macrocode}
737 \cs_new:Npn \@@_char_catcode:N #1
738 {
739 \if_catcode:w \exp_not:N #1 \c_math_toggle_token
740 3
741 \else:
742 \if_catcode:w \exp_not:N #1 \c_alignment_token
743 4
744 \else:
745 \if_catcode:w \exp_not:N #1 \c_math_superscript_token
746 7
747 \else:
748 \if_catcode:w \exp_not:N #1 \c_math_subscript_token
749 8
750 \else:
751 \if_catcode:w \exp_not:N #1 \c_space_token
752 10
753 \else:
754 \if_catcode:w \exp_not:N #1 \c_catcode_letter_token
755 11
756 \else:
757 \if_catcode:w \exp_not:N #1 \c_catcode_other_token
758 12
759 \else:
760 13
761 \fi:
762 \fi:
763 \fi:
764 \fi:
765 \fi:
766 \fi:
767 \fi:
768 }
769 % \end{macrocode}
770 % \end{macro}
771 %
772 % \begin{macro}[EXP, TF]{\@@_if_expandable:N}
773 % Test for tokens that make sense to expand here: that is more
774 % restrictive than the engine view.
775 % \begin{macrocode}
776 \prg_new_conditional:Npnn \@@_if_expandable:N #1 { T , F , TF }
777 {
778 \token_if_expandable:NTF #1
779 {
780 \bool_lazy_any:nTF
781 {
782 { \token_if_protected_macro_p:N #1 }
783 { \token_if_protected_long_macro_p:N #1 }
784 { \token_if_eq_meaning_p:NN \q_@@_recursion_tail #1 }
785 }
786 { \prg_return_false: }
787 { \prg_return_true: }
788 }
789 { \prg_return_false: }
790 }
791 % \end{macrocode}
792 % \end{macro}
793 %
794 % \subsection{Codepoint utilities}
795 %
796 % For working with codepoints in an engine-neutral way.
797 %
798 % \begin{macro}[EXP]{\@@_codepoint_process:nN, \@@_codepoint_process_aux:nN}
799 % \begin{macro}[EXP]{\@@_codepoint_process:nNN}
800 % \begin{macro}[EXP]{\@@_codepoint_process:nNNN}
801 % \begin{macro}[EXP]{\@@_codepoint_process:nNNNN}
802 % Grab a codepoint and apply some code to it: here |#1| should expect one
803 % following \emph{balanced text}.
804 % \begin{macrocode}
805 \sys_if_engine_opentype:TF
806 {
807 \cs_new:Npn \@@_codepoint_process:nN #1#2 { #1 {#2} }
808 }
809 {
810 \cs_new:Npe \@@_codepoint_process:nN #1#2
811 {
812 \exp_not:N \int_compare:nNnTF {`#2} > { "80 }
813 {
814 \sys_if_engine_pdftex:TF
815 { \exp_not:N \@@_codepoint_process_aux:nN }
816 {
817 \exp_not:N \int_compare:nNnTF {`#2} > { "FF }
818 { \exp_not:N \use:n }
819 { \exp_not:N \@@_codepoint_process_aux:nN }
820 }
821 }
822 { \exp_not:N \use:n }
823 {#1} #2
824 }
825 \cs_new:Npn \@@_codepoint_process_aux:nN #1#2
826 {
827 \int_compare:nNnTF { `#2 } < { "E0 }
828 { \@@_codepoint_process:nNN }
829 {
830 \int_compare:nNnTF { `#2 } < { "F0 }
831 { \@@_codepoint_process:nNNN }
832 { \@@_codepoint_process:nNNNN }
833 }
834 {#1} #2
835 }
836 \cs_new:Npn \@@_codepoint_process:nNN #1#2#3
837 { #1 {#2#3} }
838 \cs_new:Npn \@@_codepoint_process:nNNN #1#2#3#4
839 { #1 {#2#3#4} }
840 \cs_new:Npn \@@_codepoint_process:nNNNN #1#2#3#4#5
841 { #1 {#2#3#4#5} }
842 }
843 % \end{macrocode}
844 % \end{macro}
845 % \end{macro}
846 % \end{macro}
847 % \end{macro}
848 %
849 % \begin{macro}[EXP, pTF]{\@@_codepoint_compare:nNn}
850 % \begin{macro}[EXP]
851 % {\@@_codepoint_from_chars:Nw, \@@_codepoint_from_chars_aux:Nw}
852 % \begin{macro}[EXP]{\@@_codepoint_from_chars:N}
853 % \begin{macro}[EXP]{\@@_codepoint_from_chars:NN}
854 % \begin{macro}[EXP]{\@@_codepoint_from_chars:NNN}
855 % \begin{macro}[EXP]{\@@_codepoint_from_chars:NNNN}
856 % Allows comparison for all engines using a first \enquote{character} followed
857 % by a codepoint.
858 % \begin{macrocode}
859 \sys_if_engine_opentype:TF
860 {
861 \prg_new_conditional:Npnn
862 \@@_codepoint_compare:nNn #1#2#3 { TF , p }
863 {
864 \int_compare:nNnTF {`#1} #2 {#3}
865 \prg_return_true: \prg_return_false:
866 }
867 \cs_new:Npn \@@_codepoint_from_chars:Nw #1 {`#1}
868 }
869 {
870 \prg_new_conditional:Npnn
871 \@@_codepoint_compare:nNn #1#2#3 { TF , p }
872 {
873 \int_compare:nNnTF { \@@_codepoint_from_chars:Nw #1 }
874 #2 {#3}
875 \prg_return_true: \prg_return_false:
876 }
877 \cs_new:Npe \@@_codepoint_from_chars:Nw #1
878 {
879 \exp_not:N \if_int_compare:w `#1 > "80 \exp_not:N \exp_stop_f:
880 \sys_if_engine_pdftex:TF
881 {
882 \exp_not:N \exp_after:wN
883 \exp_not:N \@@_codepoint_from_chars_aux:Nw
884 }
885 {
886 \exp_not:N \if_int_compare:w `#1 > "FF \exp_not:N \exp_stop_f:
887 \exp_not:N \exp_after:wN \exp_not:N \exp_after:wN
888 \exp_not:N \exp_after:wN
889 \exp_not:N \@@_codepoint_from_chars:N
890 \exp_not:N \else:
891 \exp_not:N \exp_after:wN \exp_not:N \exp_after:wN
892 \exp_not:N \exp_after:wN
893 \exp_not:N \@@_codepoint_from_chars_aux:Nw
894 \exp_not:N \fi:
895 }
896 \exp_not:N \else:
897 \exp_not:N \exp_after:wN \exp_not:N \@@_codepoint_from_chars:N
898 \exp_not:N \fi:
899 #1
900 }
901 \cs_new:Npn \@@_codepoint_from_chars_aux:Nw #1
902 {
903 \if_int_compare:w `#1 < "E0 \exp_stop_f:
904 \exp_after:wN \@@_codepoint_from_chars:NN
905 \else:
906 \if_int_compare:w `#1 < "F0 \exp_stop_f:
907 \exp_after:wN \exp_after:wN \exp_after:wN
908 \@@_codepoint_from_chars:NNN
909 \else:
910 \exp_after:wN \exp_after:wN \exp_after:wN
911 \@@_codepoint_from_chars:NNNN
912 \fi:
913 \fi:
914 #1
915 }
916 \cs_new:Npn \@@_codepoint_from_chars:N #1 {`#1}
917 \cs_new:Npn \@@_codepoint_from_chars:NN #1#2
918 { (`#1 - "C0) * "40 + `#2 - "80 }
919 \cs_new:Npn \@@_codepoint_from_chars:NNN #1#2#3
920 { (`#1 - "E0) * "1000 + (`#2 - "80) * "40 + `#3 - "80 }
921 \cs_new:Npn \@@_codepoint_from_chars:NNNN #1#2#3#4
922 {
923 (`#1 - "F0) * "40000
924 + (`#2 - "80) * "1000
925 + (`#3 - "80) * "40
926 + `#4 - "80
927 }
928 }
929 % \end{macrocode}
930 % \end{macro}
931 % \end{macro}
932 % \end{macro}
933 % \end{macro}
934 % \end{macro}
935 % \end{macro}
936 %
937 % \subsection{Configuration variables}
938 %
939 % \begin{variable}[deprecated]{\l_text_accents_tl, \l_text_letterlike_tl}
940 % Used to be used for excluding these ideas from expansion: now deprecated.
941 % \begin{macrocode}
942 \tl_new:N \l_text_accents_tl
943 \tl_new:N \l_text_letterlike_tl
944 % \end{macrocode}
945 % \end{variable}
946 %
947 % \begin{variable}{\l_text_case_exclude_arg_tl}
948 % Non-text arguments, including covering the case of \tn{protected@edef}
949 % applied to \cs{cite}.
950 % \begin{macrocode}
951 \tl_new:N \l_text_case_exclude_arg_tl
952 \tl_set:Ne \l_text_case_exclude_arg_tl
953 {
954 \exp_not:n { \begin \cite \end \label \ref }
955 \exp_not:c { cite ~ }
956 \exp_not:n { \babelshorthand }
957 }
958 % \end{macrocode}
959 % \end{variable}
960 %
961 % \begin{variable}{\l_text_math_arg_tl}
962 % Math mode as arguments.
963 % \begin{macrocode}
964 \tl_new:N \l_text_math_arg_tl
965 \tl_set:Nn \l_text_math_arg_tl { \ensuremath }
966 % \end{macrocode}
967 % \end{variable}
968 %
969 % \begin{variable}{\l_text_math_delims_tl}
970 % Paired math mode delimiters.
971 % \begin{macrocode}
972 \tl_new:N \l_text_math_delims_tl
973 \tl_set:Nn \l_text_math_delims_tl { $ $ \( \) }
974 % \end{macrocode}
975 % \end{variable}
976 %
977 % \begin{variable}{\l_text_expand_exclude_tl}
978 % Commands which need not to expand. We start with a somewhat historical
979 % list, and tidy up if possible.
980 % \begin{macrocode}
981 \tl_new:N \l_text_expand_exclude_tl
982 \tl_set:Nn \l_text_expand_exclude_tl
983 { \begin \cite \end \label \ref }
984 \bool_lazy_and:nnT
985 { \str_if_eq_p:Vn \fmtname { LaTeX2e } }
986 { \tl_if_exist_p:N \@expl@finalise@setup@@@@ }
987 {
988 \tl_gput_right:Nn \@expl@finalise@setup@@@@
989 {
990 \tl_gput_right:Nn \@kernel@after@begindocument
991 {
992 \group_begin:
993 \cs_set_protected:Npn \@@_tmp:w #1
994 {
995 \tl_clear:N \l_text_expand_exclude_tl
996 \tl_map_inline:nn {#1}
997 {
998 \bool_lazy_any:nF
999 {
1000 { \token_if_protected_macro_p:N ##1 }
1001 { \token_if_protected_long_macro_p:N ##1 }
1002 {
1003 \str_if_eq_p:ee
1004 { \cs_replacement_spec:N ##1 }
1005 { \exp_not:n { \protect ##1 } \c_space_tl }
1006 }
1007 }
1008 { \tl_put_right:Nn \l_text_expand_exclude_tl {##1} }
1009 }
1010 }
1011 \exp_args:NV \@@_tmp:w \l_text_expand_exclude_tl
1012 \exp_args:NNNV \group_end:
1013 \tl_set:Nn \l_text_expand_exclude_tl \l_text_expand_exclude_tl
1014 }
1015 }
1016 }
1017 % \end{macrocode}
1018 % \end{variable}
1019 %
1020 % \begin{macro}{\l_@@_math_mode_tl}
1021 % Used to control math mode output: internal as there is a dedicated
1022 % setter.
1023 % \begin{macrocode}
1024 \tl_new:N \l_@@_math_mode_tl
1025 % \end{macrocode}
1026 % \end{macro}
1027 %
1028 % \subsection{Expansion to formatted text}
1029 %
1030 % \begin{variable}{\c_@@_chardef_space_token, \c_@@_mathchardef_space_token}
1031 % \begin{variable}
1032 % {\c_@@_chardef_group_begin_token, \c_@@_mathchardef_group_begin_token}
1033 % \begin{variable}
1034 % {\c_@@_chardef_group_end_token, \c_@@_mathchardef_group_end_token}
1035 % Markers for implicit char handling.
1036 % \begin{macrocode}
1037 \tex_global:D \tex_chardef:D \c_@@_chardef_space_token = `\ %
1038 \tex_global:D \tex_mathchardef:D \c_@@_mathchardef_space_token = `\ %
1039 \tex_global:D \tex_chardef:D \c_@@_chardef_group_begin_token = `\{ % `\}
1040 \tex_global:D \tex_mathchardef:D \c_@@_mathchardef_group_begin_token = `\{ % `\} `\{
1041 \tex_global:D \tex_chardef:D \c_@@_chardef_group_end_token = `\} % `\{
1042 \tex_global:D \tex_mathchardef:D \c_@@_mathchardef_group_end_token = `\} %
1043 % \end{macrocode}
1044 % \end{variable}
1045 % \end{variable}
1046 % \end{variable}
1047 %
1048 % \begin{macro}[EXP]{\text_expand:n, \@@_expand:n}
1049 % \begin{macro}[EXP]{\@@_expand_result:n}
1050 % \begin{macro}[EXP]{\@@_expand_store:n, \@@_expand_store:o}
1051 % \begin{macro}[EXP]{\@@_expand_store:nw}
1052 % \begin{macro}[EXP]{\@@_expand_end:w}
1053 % \begin{macro}[EXP]{\@@_expand_loop:w}
1054 % \begin{macro}[EXP]{\@@_expand_group:n}
1055 % \begin{macro}[EXP]{\@@_expand_space:w}
1056 % \begin{macro}[EXP]{\@@_expand_N_type:N}
1057 % \begin{macro}[EXP]{\@@_expand_math_search:NNN}
1058 % \begin{macro}[EXP]{\@@_expand_math_loop:Nw}
1059 % \begin{macro}[EXP]{\@@_expand_math_N_type:NN}
1060 % \begin{macro}[EXP]{\@@_expand_math_group:Nn}
1061 % \begin{macro}[EXP]{\@@_expand_math_space:Nw}
1062 % \begin{macro}[EXP]
1063 % {
1064 % \@@_expand_explicit:N ,
1065 % \@@_expand_exclude:N
1066 % }
1067 % \begin{macro}[EXP]{\@@_expand_exclude_switch:Nnnnn}
1068 % \begin{macro}[EXP]{\@@_expand_exclude:nN}
1069 % \begin{macro}[EXP]{\@@_expand_exclude:NN}
1070 % \begin{macro}[EXP]{\@@_expand_exclude:Nw}
1071 % \begin{macro}[EXP]{\@@_expand_exclude:Nnn}
1072 % \begin{macro}[EXP]{\@@_expand_accent:N}
1073 % \begin{macro}[EXP]{\@@_expand_accent:NN}
1074 % \begin{macro}[EXP]{\@@_expand_letterlike:N}
1075 % \begin{macro}[EXP]{\@@_expand_letterlike:NN}
1076 % \begin{macro}[EXP]{\@@_expand_cs:N}
1077 % \begin{macro}[EXP]{\@@_expand_protect:w}
1078 % \begin{macro}[EXP]{\@@_expand_protect:N}
1079 % \begin{macro}[EXP]{\@@_expand_protect:nN}
1080 % \begin{macro}[EXP]{\@@_expand_protect:Nw}
1081 % \begin{macro}[EXP]{\@@_expand_testopt:N}
1082 % \begin{macro}[EXP]{\@@_expand_testopt:NNn}
1083 % \begin{macro}[EXP]{\@@_expand_encoding:N, \@@_expand_encoding_escape:N}
1084 % \begin{macro}[EXP]{\@@_expand_replace:N}
1085 % \begin{macro}[EXP]{\@@_expand_replace:n}
1086 % \begin{macro}[EXP]{\@@_expand_cs_expand:N}
1087 % \begin{macro}[EXP]{\@@_expand_unexpanded:w}
1088 % \begin{macro}[EXP]{\@@_expand_unexpanded_test:w}
1089 % \begin{macro}[EXP]{\@@_expand_unexpanded:N}
1090 % \begin{macro}[EXP]{\@@_expand_unexpanded:n}
1091 % After precautions against |&| tokens, start a simple loop: that of
1092 % course means that \enquote{text} cannot contain the two recursion
1093 % quarks. The loop here must be \texttt{f}-type expandable; we have
1094 % arbitrary user commands which might be protected \emph{and} take
1095 % arguments, and if the expansion code is used in a typesetting
1096 % context, that will otherwise explode. (The same issue applies more
1097 % clearly to case changing: see the example there.) The outer
1098 % loop has to use scan marks as delimiters to protect against
1099 % unterminated \tn{romannumeral} usage in the input.
1100 % \begin{macrocode}
1101 \cs_new:Npn \text_expand:n #1
1102 {
1103 \__kernel_exp_not:w \exp_after:wN
1104 {
1105 \exp:w
1106 \@@_expand:n {#1}
1107 }
1108 }
1109 \cs_new:Npn \@@_expand:n #1
1110 {
1111 \group_align_safe_begin:
1112 \@@_expand_loop:w #1
1113 \s_@@_recursion_tail \s_@@_recursion_stop
1114 \@@_expand_result:n { }
1115 }
1116 % \end{macrocode}
1117 % The approach to making the code \texttt{f}-type expandable is to usee
1118 % a marker result token and to shuffle the collected tokens
1119 % \begin{macrocode}
1120 \cs_new:Npn \@@_expand_store:n #1
1121 { \@@_expand_store:nw {#1} }
1122 \cs_generate_variant:Nn \@@_expand_store:n { o }
1123 \cs_new:Npn \@@_expand_store:nw #1#2 \@@_expand_result:n #3
1124 { #2 \@@_expand_result:n { #3 #1 } }
1125 \cs_new:Npn \@@_expand_end:w #1 \@@_expand_result:n #2
1126 {
1127 \group_align_safe_end:
1128 \exp_end:
1129 #2
1130 }
1131 % \end{macrocode}
1132 % The main loop is a standard \enquote{tl action}; groups are handled
1133 % recursively, while spaces are just passed through. Thus all of the
1134 % action is in handling \texttt{N}-type tokens.
1135 % \begin{macrocode}
1136 \cs_new:Npn \@@_expand_loop:w #1 \s_@@_recursion_stop
1137 {
1138 \tl_if_head_is_N_type:nTF {#1}
1139 { \@@_expand_N_type:N }
1140 {
1141 \tl_if_head_is_group:nTF {#1}
1142 { \@@_expand_group:n }
1143 { \@@_expand_space:w }
1144 }
1145 #1 \s_@@_recursion_stop
1146 }
1147 \cs_new:Npn \@@_expand_group:n #1
1148 {
1149 \@@_expand_store:o
1150 {
1151 \exp_after:wN
1152 {
1153 \exp:w
1154 \@@_expand:n {#1}
1155 }
1156 }
1157 \@@_expand_loop:w
1158 }
1159 \exp_last_unbraced:NNo \cs_new:Npn \@@_expand_space:w \c_space_tl
1160 {
1161 \@@_expand_store:n { ~ }
1162 \@@_expand_loop:w
1163 }
1164 % \end{macrocode}
1165 % The first step in dealing with \texttt{N}-type tokens is to look for
1166 % math mode material: that needs to be left alone. The starting function
1167 % has to be split into two as we need \cs{quark_if_recursion_tail_stop:N}
1168 % first before we can trigger the search. We then look for matching
1169 % pairs of delimiters, allowing for the case where math mode starts
1170 % but does not end. Within math mode, we simply pass all the tokens
1171 % through unchanged, just checking the \texttt{N}-type ones against the
1172 % end marker.
1173 % \begin{macrocode}
1174 \cs_new:Npn \@@_expand_N_type:N #1
1175 {
1176 \@@_if_s_recursion_tail_stop_do:Nn #1
1177 { \@@_expand_end:w }
1178 \exp_after:wN \@@_expand_math_search:NNN
1179 \exp_after:wN #1 \l_text_math_delims_tl
1180 \q_@@_recursion_tail \q_@@_recursion_tail
1181 \q_@@_recursion_stop
1182 }
1183 \cs_new:Npn \@@_expand_math_search:NNN #1#2#3
1184 {
1185 \@@_if_q_recursion_tail_stop_do:Nn #2
1186 { \@@_expand_explicit:N #1 }
1187 \token_if_eq_meaning:NNTF #1 #2
1188 {
1189 \@@_use_i_delimit_by_q_recursion_stop:nw
1190 {
1191 \@@_expand_store:n {#1}
1192 \@@_expand_math_loop:Nw #3
1193 }
1194 }
1195 { \@@_expand_math_search:NNN #1 }
1196 }
1197 \cs_new:Npn \@@_expand_math_loop:Nw #1#2 \s_@@_recursion_stop
1198 {
1199 \tl_if_head_is_N_type:nTF {#2}
1200 { \@@_expand_math_N_type:NN }
1201 {
1202 \tl_if_head_is_group:nTF {#2}
1203 { \@@_expand_math_group:Nn }
1204 { \@@_expand_math_space:Nw }
1205 }
1206 #1#2 \s_@@_recursion_stop
1207 }
1208 \cs_new:Npn \@@_expand_math_N_type:NN #1#2
1209 {
1210 \@@_if_s_recursion_tail_stop_do:Nn #2
1211 { \@@_expand_end:w }
1212 \token_if_eq_meaning:NNF #2 \exp_not:N
1213 { \@@_expand_store:n {#2} }
1214 \token_if_eq_meaning:NNTF #2 #1
1215 { \@@_expand_loop:w }
1216 { \@@_expand_math_loop:Nw #1 }
1217 }
1218 \cs_new:Npn \@@_expand_math_group:Nn #1#2
1219 {
1220 \@@_expand_store:n { {#2} }
1221 \@@_expand_math_loop:Nw #1
1222 }
1223 \exp_after:wN \cs_new:Npn \exp_after:wN \@@_expand_math_space:Nw
1224 \exp_after:wN # \exp_after:wN 1 \c_space_tl
1225 {
1226 \@@_expand_store:n { ~ }
1227 \@@_expand_math_loop:Nw #1
1228 }
1229 % \end{macrocode}
1230 % At this stage, either we have a control sequence or a simple character:
1231 % split and handle. The need to check for non-protected actives arises
1232 % from handling of legacy input encodings: they need to end up in a
1233 % representation we can deal with in further processing. The tests for
1234 % explicit parts of the \LaTeXe{} UTF-8 mechanism cover the case of
1235 % bookmarks, where definitions change and are no longer protected. The
1236 % same is true for \pkg{babel} shorthands. Ideally, all implicit tokens would
1237 % be converted to their explicit equivalents here. However, that runs into
1238 % issues with constructs that use \tn{chardef} or similar tokens. The one
1239 % case we do convert is \cs{exp_stop_f:}, as that is predictable and should
1240 % output a space.
1241 % \begin{macrocode}
1242 \cs_new:Npn \@@_expand_explicit:N #1
1243 {
1244 \token_if_cs:NTF #1
1245 { \@@_expand_exclude:N #1 }
1246 {
1247 \bool_lazy_and:nnTF
1248 { \token_if_active_p:N #1 }
1249 {
1250 ! \bool_lazy_any_p:n
1251 {
1252 { \token_if_protected_macro_p:N #1 }
1253 { \token_if_protected_long_macro_p:N #1 }
1254 { \tl_if_head_eq_meaning_p:oN {#1} \UTFviii@two@octets }
1255 { \tl_if_head_eq_meaning_p:oN {#1} \UTFviii@three@octets }
1256 { \tl_if_head_eq_meaning_p:oN {#1} \UTFviii@four@octets }
1257 { \tl_if_head_eq_meaning_p:oN {#1} \active@prefix }
1258 }
1259 }
1260 { \exp_after:wN \@@_expand_loop:w #1 }
1261 {
1262 \str_if_eq:nnTF {#1} { \exp_stop_f: }
1263 { \@@_expand_store:n { ~ } }
1264 { \@@_expand_store:n {#1} }
1265 \@@_expand_loop:w
1266 }
1267 }
1268 }
1269 % \end{macrocode}
1270 % Next we exclude math commands: this is mainly as there \emph{might} be an
1271 % \cs{ensuremath}. The switching command for case needs special handling
1272 % as it has to work by meaning.
1273 % \begin{macrocode}
1274 \cs_new:Npn \@@_expand_exclude:N #1
1275 {
1276 \cs_if_eq:NNTF #1 \text_case_switch:nnnn
1277 { \@@_expand_exclude_switch:Nnnnn #1 }
1278 {
1279 \exp_args:Ne \@@_expand_exclude:nN
1280 {
1281 \exp_not:V \l_text_math_arg_tl
1282 \exp_not:V \l_text_expand_exclude_tl
1283 \exp_not:V \l_text_case_exclude_arg_tl
1284 }
1285 #1
1286 }
1287 }
1288 \cs_new:Npn \@@_expand_exclude_switch:Nnnnn #1#2#3#4#5
1289 {
1290 \@@_expand_store:n { #1 {#2} {#3} {#4} {#5} }
1291 \@@_expand_loop:w
1292 }
1293 \cs_new:Npn \@@_expand_exclude:nN #1#2
1294 {
1295 \@@_expand_exclude:NN #2 #1
1296 \q_@@_recursion_tail \q_@@_recursion_stop
1297 }
1298 \cs_new:Npn \@@_expand_exclude:NN #1#2
1299 {
1300 \@@_if_q_recursion_tail_stop_do:Nn #2
1301 { \@@_expand_accent:N #1 }
1302 \str_if_eq:nnTF {#1} {#2}
1303 {
1304 \@@_use_i_delimit_by_q_recursion_stop:nw
1305 { \@@_expand_exclude:Nw #1 }
1306 }
1307 { \@@_expand_exclude:NN #1 }
1308 }
1309 \cs_new:Npn \@@_expand_exclude:Nw #1#2#
1310 { \@@_expand_exclude:Nnn #1 {#2} }
1311 \cs_new:Npn \@@_expand_exclude:Nnn #1#2#3
1312 {
1313 \@@_expand_store:n { #1#2 {#3} }
1314 \@@_expand_loop:w
1315 }
1316 % \end{macrocode}
1317 % Accents.
1318 % \begin{macrocode}
1319 \cs_new:Npn \@@_expand_accent:N #1
1320 {
1321 \exp_after:wN \@@_expand_accent:NN \exp_after:wN
1322 #1 \l_text_accents_tl
1323 \q_@@_recursion_tail \q_@@_recursion_stop
1324 }
1325 \cs_new:Npn \@@_expand_accent:NN #1#2
1326 {
1327 \@@_if_q_recursion_tail_stop_do:Nn #2
1328 { \@@_expand_letterlike:N #1 }
1329 \cs_if_eq:NNTF #2 #1
1330 {
1331 \@@_use_i_delimit_by_q_recursion_stop:nw
1332 {
1333 \@@_expand_store:n {#1}
1334 \@@_expand_loop:w
1335 }
1336 }
1337 { \@@_expand_accent:NN #1 }
1338 }
1339 % \end{macrocode}
1340 % Another list of exceptions: these ones take no arguments so are
1341 % easier to handle.
1342 % \begin{macrocode}
1343 \cs_new:Npn \@@_expand_letterlike:N #1
1344 {
1345 \exp_after:wN \@@_expand_letterlike:NN \exp_after:wN
1346 #1 \l_text_letterlike_tl
1347 \q_@@_recursion_tail \q_@@_recursion_stop
1348 }
1349 \cs_new:Npn \@@_expand_letterlike:NN #1#2
1350 {
1351 \@@_if_q_recursion_tail_stop_do:Nn #2
1352 { \@@_expand_cs:N #1 }
1353 \cs_if_eq:NNTF #2 #1
1354 {
1355 \@@_use_i_delimit_by_q_recursion_stop:nw
1356 {
1357 \@@_expand_store:n {#1}
1358 \@@_expand_loop:w
1359 }
1360 }
1361 { \@@_expand_letterlike:NN #1 }
1362 }
1363 % \end{macrocode}
1364 % \LaTeXe{}'s \cs{protect} makes life interesting. Where possible, we
1365 % simply remove it and replace with the \enquote{parent} command; of course,
1366 % the \cs{protect} might be explicit, in which case we need to leave it alone.
1367 % That includes the case where it's not even followed by an \texttt{N}-type
1368 % token. There is also the case of a straight \tn{@protected@testopt} to
1369 % cover.
1370 % \begin{macrocode}
1371 \cs_new:Npe \@@_expand_cs:N #1
1372 {
1373 \exp_not:N \str_if_eq:nnTF {#1} { \exp_not:N \protect }
1374 { \exp_not:N \@@_expand_protect:w }
1375 {
1376 \bool_lazy_and:nnTF
1377 { \cs_if_exist_p:N \fmtname }
1378 { \str_if_eq_p:Vn \fmtname { LaTeX2e } }
1379 { \exp_not:N \@@_expand_testopt:N #1 }
1380 { \exp_not:N \@@_expand_replace:N #1 }
1381 }
1382 }
1383 \cs_new:Npn \@@_expand_protect:w #1 \s_@@_recursion_stop
1384 {
1385 \tl_if_head_is_N_type:nTF {#1}
1386 { \@@_expand_protect:N }
1387 {
1388 \@@_expand_store:n { \protect }
1389 \@@_expand_loop:w
1390 }
1391 #1 \s_@@_recursion_stop
1392 }
1393 \cs_new:Npn \@@_expand_protect:N #1
1394 {
1395 \@@_if_s_recursion_tail_stop_do:Nn #1
1396 {
1397 \@@_expand_store:n { \protect }
1398 \@@_expand_end:w
1399 }
1400 \exp_args:Ne \@@_expand_protect:nN
1401 { \cs_to_str:N #1 } #1
1402 }
1403 \cs_new:Npn \@@_expand_protect:nN #1#2
1404 { \@@_expand_protect:Nw #2 #1 \q_@@_nil #1 ~ \q_@@_nil \q_@@_nil \s_@@_stop }
1405 \cs_new:Npn \@@_expand_protect:Nw #1 #2 ~ \q_@@_nil #3 \q_@@_nil #4 \s_@@_stop
1406 {
1407 \@@_quark_if_nil:nTF {#4}
1408 {
1409 \cs_if_exist:cTF {#2}
1410 { \exp_args:Ne \@@_expand_store:n { \exp_not:c {#2} } }
1411 { \@@_expand_store:n { \protect #1 } }
1412 }
1413 { \@@_expand_store:n { \protect #1 } }
1414 \@@_expand_loop:w
1415 }
1416 \cs_new:Npn \@@_expand_testopt:N #1
1417 {
1418 \token_if_eq_meaning:NNTF #1 \@protected@testopt
1419 { \@@_expand_testopt:NNn }
1420 { \@@_expand_encoding:N #1 }
1421 }
1422 \cs_new:Npn \@@_expand_testopt:NNn #1#2#3
1423 {
1424 \@@_expand_store:n {#1}
1425 \@@_expand_loop:w
1426 }
1427 % \end{macrocode}
1428 % Deal with encoding-specific commands
1429 % \begin{macrocode}
1430 \cs_new:Npn \@@_expand_encoding:N #1
1431 {
1432 \bool_lazy_or:nnTF
1433 { \cs_if_eq_p:NN #1 \@current@cmd }
1434 { \cs_if_eq_p:NN #1 \@changed@cmd }
1435 { \exp_after:wN \@@_expand_loop:w \@@_expand_encoding_escape:NN }
1436 { \@@_expand_replace:N #1 }
1437 }
1438 \cs_new:Npn \@@_expand_encoding_escape:NN #1#2 { \exp_not:n {#1} }
1439 % \end{macrocode}
1440 % See if there is a dedicated replacement, and if there is, insert it.
1441 % \begin{macrocode}
1442 \cs_new:Npn \@@_expand_replace:N #1
1443 {
1444 \bool_lazy_and:nnTF
1445 { \cs_if_exist_p:c { l_@@_expand_ \token_to_str:N #1 _tl } }
1446 {
1447 \bool_lazy_or_p:nn
1448 { \token_if_cs_p:N #1 }
1449 { \token_if_active_p:N #1 }
1450 }
1451 {
1452 \exp_args:Nv \@@_expand_replace:n
1453 { l_@@_expand_ \token_to_str:N #1 _tl }
1454 }
1455 { \@@_expand_cs_expand:N #1 }
1456 }
1457 \cs_new:Npn \@@_expand_replace:n #1 { \@@_expand_loop:w #1 }
1458 % \end{macrocode}
1459 % Finally, expand any macros which can be: this then loops back around to
1460 % deal with what they produce. The only issue is if the token is
1461 % \cs{exp_not:n}, as that must apply to the following balanced text.
1462 % \begin{macrocode}
1463 \cs_new:Npn \@@_expand_cs_expand:N #1
1464 {
1465 \@@_if_expandable:NTF #1
1466 {
1467 \token_if_eq_meaning:NNTF #1 \exp_not:n
1468 { \@@_expand_unexpanded:w }
1469 { \exp_after:wN \@@_expand_loop:w #1 }
1470 }
1471 {
1472 \@@_expand_store:n {#1}
1473 \@@_expand_loop:w
1474 }
1475 }
1476 % \end{macrocode}
1477 % Since \cs{exp_not:n} is actually a primitive, it allows a strange syntax
1478 % and it particular the primitive expands what follows and discards spaces
1479 % and \cs{scan_stop:} until finding a braced argument (the opening brace
1480 % can be implicit but we will not support this here). Here, we repeatedly
1481 % |f|-expand after such an \cs{exp_not:n}, and test what follows. If
1482 % it is a brace group, then we found the intended argument of
1483 % \cs{exp_not:n}. If it is a space, then the next |f|-expansion will
1484 % eliminate it. If it is an |N|-type token then
1485 % \cs{@@_expand_unexpanded:N} leaves the token to be expanded if it is
1486 % expandable, and otherwise removes it, assuming that it is
1487 % \cs{scan_stop:}. This silently hides errors when \cs{exp_not:n} is
1488 % incorrectly followed by some non-expandable token other than
1489 % \cs{scan_stop:}, but this should be pretty rare, and there is no good
1490 % error recovery anyways.
1491 % \begin{macrocode}
1492 \cs_new:Npn \@@_expand_unexpanded:w
1493 {
1494 \exp_after:wN \@@_expand_unexpanded_test:w
1495 \exp:w \exp_end_continue_f:w
1496 }
1497 \cs_new:Npn \@@_expand_unexpanded_test:w #1 \s_@@_recursion_stop
1498 {
1499 \tl_if_head_is_group:nTF {#1}
1500 { \@@_expand_unexpanded:n }
1501 {
1502 \@@_expand_unexpanded:w
1503 \tl_if_head_is_N_type:nT {#1} { \@@_expand_unexpanded:N }
1504 }
1505 #1 \s_@@_recursion_stop
1506 }
1507 \cs_new:Npn \@@_expand_unexpanded:N #1
1508 {
1509 \exp_after:wN \if_meaning:w \exp_not:N #1 #1
1510 \else:
1511 \exp_after:wN #1
1512 \fi:
1513 }
1514 \cs_new:Npn \@@_expand_unexpanded:n #1
1515 {
1516 \@@_expand_store:n {#1}
1517 \@@_expand_loop:w
1518 }
1519 % \end{macrocode}
1520 % \end{macro}
1521 % \end{macro}
1522 % \end{macro}
1523 % \end{macro}
1524 % \end{macro}
1525 % \end{macro}
1526 % \end{macro}
1527 % \end{macro}
1528 % \end{macro}
1529 % \end{macro}
1530 % \end{macro}
1531 % \end{macro}
1532 % \end{macro}
1533 % \end{macro}
1534 % \end{macro}
1535 % \end{macro}
1536 % \end{macro}
1537 % \end{macro}
1538 % \end{macro}
1539 % \end{macro}
1540 % \end{macro}
1541 % \end{macro}
1542 % \end{macro}
1543 % \end{macro}
1544 % \end{macro}
1545 % \end{macro}
1546 % \end{macro}
1547 % \end{macro}
1548 % \end{macro}
1549 % \end{macro}
1550 % \end{macro}
1551 % \end{macro}
1552 % \end{macro}
1553 % \end{macro}
1554 % \end{macro}
1555 % \end{macro}
1556 % \end{macro}
1557 % \end{macro}
1558 % \end{macro}
1559 %
1560 % \begin{macro}
1561 % {
1562 % \text_declare_expand_equivalent:Nn ,
1563 % \text_declare_expand_equivalent:cn
1564 % }
1565 % Create equivalents to allow replacement.
1566 % \begin{macrocode}
1567 \cs_new_protected:Npn \text_declare_expand_equivalent:Nn #1#2
1568 {
1569 \tl_clear_new:c { l_@@_expand_ \token_to_str:N #1 _tl }
1570 \tl_set:cn { l_@@_expand_ \token_to_str:N #1 _tl } {#2}
1571 }
1572 \cs_generate_variant:Nn \text_declare_expand_equivalent:Nn { c }
1573 % \end{macrocode}
1574 % \end{macro}
1575 %
1576 % Prevent expansion of various standard values.
1577 % \begin{macrocode}
1578 \tl_map_inline:nn
1579 { \` \' \^ \~ \= \u \. \" \r \H \v \d \c \k \b \t }
1580 { \text_declare_expand_equivalent:Nn #1 { \exp_not:n {#1} } }
1581 \tl_map_inline:nn
1582 {
1583 \AA \aa
1584 \AE \ae
1585 \DH \dh
1586 \DJ \dj
1587 \IJ \ij
1588 \L \l
1589 \NG \ng
1590 \O \o
1591 \OE \oe
1592 \SS \ss
1593 \TH \th
1594 }
1595 { \text_declare_expand_equivalent:Nn #1 { \exp_not:n {#1} } }
1596 \text_declare_expand_equivalent:Nn \@setfontsize \use_none:nnn
1597 % \end{macrocode}
1598 %
1599 % \begin{macrocode}
1600 %</code>
1601 % \end{macrocode}
1602 %
1603 % \end{implementation}
1604 %
1605 % \PrintIndex