Debian Codegraph @

texmf-dist/source/generic/gtl/gtl.dtx

raw
1 % \iffalse
2 %% File: gtl.dtx Copyright (C) 2013,2015,2017,2018,2024 Bruno Le Floch
3 %%
4 %% This work may be distributed and/or modified under the
5 %% conditions of the LaTeX Project Public License, either version 1.3c
6 %% of this license or (at your option) any later version.
7 %% The latest version of this license is in
8 %% http://www.latex-project.org/lppl.txt
9 %%
10 %% This work has the LPPL maintenance status 'maintained'
11 %% and the current maintainer is Bruno Le Floch.
12 %%
13 %% This work consists of the files gtl.dtx and gtl.ins and
14 %% derived file gtl.sty.
15 %% -----------------------------------------------------------------------
16 %
17 %<*driver>
18 %\fi
19 %\iffalse
20 \documentclass[full]{l3doc}
21 \usepackage{gtl}
22 \begin{document}
23 \DocInput{gtl.dtx}
24 \end{document}
25 %</driver>
26 % \fi
27 %
28 % \title{The \textsf{gtl} package: \\
29 % manipulate unbalanced lists of tokens\thanks{This
30 % file has version number 0.6, last revised 2024/01/04.}}
31 % \author{Bruno Le Floch}
32 % \date{2024/01/04}
33 %
34 % \maketitle
35 % \tableofcontents
36 %
37 % \begin{documentation}
38 %
39 % \section{\pkg{gtl} documentation}
40 %
41 % The \texttt{expl3} programming language provides various tools to
42 % manipulate lists of tokens (package \pkg{l3tl}). However, those token
43 % lists must have balanced braces, or more precisely balanced
44 % begin-group and end-group characters. The \pkg{gtl} package
45 % manipulates instead lists of tokens which may be unbalanced, with more
46 % begin-group or more end-group characters.
47 %
48 % A technical comment: currently, all begin-group characters are
49 % assumed to have the character code of ``|{|'' and all end-group
50 % characters that of~``|}|''.
51 %
52 % Please report bugs (or suggestions) on the issue tracker
53 % (\url{https://github.com/blefloch/latex-gtl/issues}).
54 %
55 % ^^A The following code checks the test coverage
56 % \begingroup\ExplSyntaxOn
57 % \msg_new:nnn { gtldoc } { discarded } { Discarded~some~material~after~closing~brace~of~{function}~or~TEST~or~TESTEXP~argument:~``#1'' }
58 % \msg_new:nnn { gtldoc } { bad-test } { TEST~or~TESTEXP~followed~by~unexpected~character~``#1'' }
59 % \msg_new:nnn { gtldoc } { multi-function } { Some~functions~are~defined~twice }
60 % \msg_new:nnn { gtldoc } { doc-no-test } { ``#1''~is~documented~but~has~no~test }
61 % \msg_new:nnn { gtldoc } { test-no-doc } { ``#1''~is~tested~but~not~documented }
62 % \msg_new:nnn { gtldoc } { junk } { There~is~some~unexpected~junk.~Giving~up~on~this~line. }
63 % \tl_new:N \l__gtldoc_arg_tl
64 % \seq_new:N \l__gtldoc_internal_seq
65 % \cs_generate_variant:Nn \use:nn { no }
66 % \cs_generate_variant:Nn \tl_if_in:nnTF { no }
67 % \cs_generate_variant:Nn \tl_if_in:nnT { no }
68 % \cs_generate_variant:Nn \tl_remove_all:Nn { Nx }
69 % \cs_generate_variant:Nn \seq_set_filter:NNn { NNx }
70 % \group_begin:
71 % \seq_new:N \l__gtldoc_fn_seq
72 % \iow_new:N \g__gtldoc_ior
73 % \tl_new:N \l__gtldoc_internal_tl
74 % \str_new:N \l__gtldoc_TF_str
75 % \seq_new:N \l__gtldoc_arg_seq
76 % \seq_new:N \l__gtldoc_arg_TF_seq
77 % \cs_new:Npn \__gtldoc_remove_backslash:N #1
78 % { \exp_after:wN \token_if_eq_charcode:NNF \c_backslash_str #1 {#1} }
79 % \cs_new_protected:Npn \gtldoc_get_fn:n #1
80 % {
81 % \seq_clear:N \l__gtldoc_fn_seq
82 % \clist_map_inline:nn {#1}
83 % {
84 % \__gtldoc_get_fn:nn { function } {#1} ^^A todo: this is wasteful, going through the file twice!
85 % \__gtldoc_get_fn:nn { variable } {#1}
86 % }
87 % }
88 % \cs_new_protected:Npn \__gtldoc_get_fn:nn #1
89 % {
90 % \exp_args:Noo \__gtldoc_get_fn_aux:nnn
91 % { \tl_to_str:n { begin } } { \tl_to_str:n { {#1} } }
92 % }
93 % \cs_new_eq:NN \__gtldoc_test:w ?
94 % \cs_new_eq:NN \__gtldoc_skip:w ?
95 % \cs_new_protected:Npn \__gtldoc_get_fn_aux:nnn #1#2#3
96 % {
97 % \cs_gset_protected:Npn \__gtldoc_test:w ##1 #1 ##2 #2 { }
98 % \cs_gset_protected:Npn \__gtldoc_skip:w ##1 #1 ##2 #2
99 % {
100 % \tl_clear:N \l__gtldoc_arg_tl
101 % \str_clear:N \l__gtldoc_TF_str
102 % \tl_if_blank:nF {##2}
103 % {
104 % \msg_error:nn { gtldoc } { junk }
105 % \use_none_delimit_by_q_recursion_stop:w
106 % }
107 % \__gtldoc_aux:N
108 % }
109 % \ior_open:Nn \g__gtldoc_ior {#3}
110 % \ior_str_map_inline:Nn \g__gtldoc_ior
111 % {
112 % \tl_if_in:noT {##1} { \tl_to_str:n { end { documentation } } } ^^A careful if you remove spaces!
113 % { \ior_map_break: }
114 % \tl_if_empty:oF { \__gtldoc_test:w ##1 #1 #2 }
115 % { \__gtldoc_skip:w ##1 \q_recursion_tail \q_recursion_stop }
116 % }
117 % \ior_close:N \g__gtldoc_ior
118 % }
119 % \cs_new_protected:Npn \__gtldoc_aux:N #1
120 % {
121 % \token_if_eq_meaning:NNT #1 \q_recursion_tail
122 % { \__gtldoc_more:Nw \__gtldoc_aux:N }
123 % \str_case_e:nnF {#1}
124 % {
125 % \c_percent_str { \__gtldoc_aux:N } ^^A ignore percent
126 % ^ { \__gtldoc_hat:N } ^^A check for "^^A"
127 % [ { \__gtldoc_opt:w }
128 % \c_left_brace_str { \__gtldoc_arg:w }
129 % }
130 % { \__gtldoc_varg:N #1 }
131 % }
132 % \cs_new_protected:Npn \__gtldoc_more:Nw #1 #2 \q_recursion_stop
133 % { ^^A grab one more line and restart the loop
134 % \ior_str_get:NN \g__gtldoc_ior \l__gtldoc_internal_tl
135 % \exp_after:wN #1 \l__gtldoc_internal_tl
136 % \q_recursion_tail \q_recursion_stop
137 % }
138 % \cs_new_protected:Npn \__gtldoc_hat:N #1
139 % {
140 % \token_if_eq_charcode:NNTF #1 ^
141 % { \__gtldoc_hat_hat:N }
142 % { \__gtldoc_varg:N ^ #1 }
143 % }
144 % \cs_new_protected:Npn \__gtldoc_hat_hat:N #1 ^^A in principle we should worry about crazy people doing ^^01 or ^^5b etc
145 % {
146 % \token_if_eq_charcode:NNTF #1 A
147 % { \__gtldoc_more:Nw \__gtldoc_aux:N } ^^A we found a comment
148 % { \__gtldoc_varg:N ^ ^ #1 }
149 % }
150 % \cs_new_protected:Npn \__gtldoc_opt:w #1 \q_recursion_tail
151 % {
152 % \tl_if_in:nnTF {#1} { ] }
153 % { \__gtldoc_opt_aux:w #1 \q_recursion_tail }
154 % { \__gtldoc_more:Nw \__gtldoc_opt:w }
155 % }
156 % \cs_new_protected:Npn \__gtldoc_opt_aux:w #1 ]
157 % {
158 % \tl_if_in:noTF {#1} { \tl_to_str:n { pTF } }
159 % { \str_set:Nn \l__gtldoc_TF_str { pTF } }
160 % {
161 % \tl_if_in:noT {#1} { \tl_to_str:n { TF } }
162 % { \str_set:Nn \l__gtldoc_TF_str { TF } }
163 % }
164 % \__gtldoc_aux:N
165 % }
166 % \cs_new_protected:Npn \__gtldoc_arg:w
167 % { \exp_after:wN \__gtldoc_varg:N \c_right_brace_str }
168 % \cs_new_protected:Npn \__gtldoc_varg:N #1
169 % { \exp_args:No \__gtldoc_varg_aux:n { \token_to_str:N #1 } }
170 % \cs_new_protected:Npn \__gtldoc_varg_aux:n #1
171 % {
172 % \cs_gset_protected:Npn \__gtldoc_varg_aux:w ##1 #1 { {##1} }
173 % \cs_gset_protected:Npn \__gtldoc_varg_loop:w ##1 \q_recursion_tail
174 % {
175 % \tl_if_in:noTF {##1} {#1}
176 % {
177 % \exp_after:wN \__gtldoc_varg_last:nw
178 % \__gtldoc_varg_aux:w ##1 \q_recursion_tail
179 % }
180 % {
181 % \tl_put_right:Ne \l__gtldoc_arg_tl { ##1 \iow_char:N \^^M }
182 % \__gtldoc_more:Nw \__gtldoc_varg_loop:w
183 % }
184 % }
185 % \__gtldoc_varg_loop:w
186 % }
187 % \cs_new_eq:NN \__gtldoc_varg_aux:w ?
188 % \cs_new_eq:NN \__gtldoc_varg_loop:w ?
189 % \cs_new_protected:Npn \__gtldoc_varg_last:nw
190 % #1#2 \q_recursion_tail \q_recursion_stop
191 % {
192 % \tl_if_blank:nF {#2}
193 % { \msg_warning:nnn { gtldoc } { discarded } {#2} }
194 % \tl_put_right:Nn \l__gtldoc_arg_tl {#1}
195 % \tl_remove_all:Ne \l__gtldoc_arg_tl { \iow_char:N \^^M \iow_char:N \% } ^^A from l3doc
196 % \tl_remove_all:Ne \l__gtldoc_arg_tl { \tl_to_str:n { ^ ^ A } } ^^A from l3doc
197 % \tl_remove_all:Ne \l__gtldoc_arg_tl { \iow_char:N \^^I } ^^A from l3doc
198 % \tl_remove_all:Ne \l__gtldoc_arg_tl { \iow_char:N \^^M } ^^A from l3doc
199 % ^^A \@@_replace_at_at:N \l_@@_tmpa_tl ^^A from l3doc but not done here
200 % \exp_args:NNe \seq_set_from_clist:Nn \l__gtldoc_arg_seq
201 % { \tl_to_str:N \l__gtldoc_arg_tl }
202 % \str_if_empty:NF \l__gtldoc_TF_str
203 % {
204 % \seq_clear:N \l__gtldoc_arg_TF_seq
205 % \seq_map_inline:Nn \l__gtldoc_arg_seq
206 % {
207 % \seq_put_right:Ne \l__gtldoc_arg_TF_seq { ##1 TF }
208 % \seq_put_right:Ne \l__gtldoc_arg_TF_seq { ##1 T }
209 % \seq_put_right:Ne \l__gtldoc_arg_TF_seq { ##1 F }
210 % \str_if_eq:eeT { \l__gtldoc_TF_str } { pTF }
211 % { \seq_put_right:Ne \l__gtldoc_arg_TF_seq { \__gtldoc_pred:w ##1 } }
212 % }
213 % \seq_set_map_e:NNn \l__gtldoc_arg_seq
214 % \l__gtldoc_arg_TF_seq { \tl_to_str:n {##1} }
215 % }
216 % \seq_concat:NNN \l__gtldoc_fn_seq \l__gtldoc_fn_seq \l__gtldoc_arg_seq
217 % }
218 % \use:no { \cs_new:Npn \__gtldoc_pred:w #1 } { \token_to_str:N : } { #1 _p: }
219 % \group_end:
220 % \group_begin:
221 % \seq_new:N \l__gtldoc_tested_fn_seq
222 % \cs_new_protected:Npn \gtldoc_get_tests:n #1
223 % {
224 % \seq_clear:N \l__gtldoc_tested_fn_seq
225 % \clist_map_function:nN {#1} \__gtldoc_get_test:n
226 % }
227 % \cs_new_protected:Npn \__gtldoc_get_test:n #1
228 % {
229 % \ior_open:Nn \g__gtldoc_ior {#1}
230 % \ior_str_map_inline:Nn \g__gtldoc_ior
231 % { \__gtldoc_test_aux:n {##1} }
232 % \ior_close:N \g__gtldoc_ior
233 % }
234 % \cs_gset_protected:Npn \__gtldoc_tmp:nn #1#2
235 % {
236 % \cs_new_protected:Npn \__gtldoc_test_skipi:w ##1 #1 { }
237 % \cs_new_protected:Npn \__gtldoc_test_skipii:w ##1 #2 { }
238 % \cs_new_protected:Npn \__gtldoc_test_aux:n ##1
239 % {
240 % \tl_if_empty:oF { \__gtldoc_test_skipi:w ##1 #1 }
241 % {
242 % \tl_if_empty:oTF { \__gtldoc_test_skipii:w ##1 #2 }
243 % { \__gtldoc_test_auxi:w } { \__gtldoc_test_auxii:w }
244 % ##1 \q_recursion_tail \q_recursion_stop
245 % }
246 % }
247 % \cs_new_protected:Npn \__gtldoc_test_auxi:w ##1 #1
248 % { \__gtldoc_test_auxiii:N }
249 % \cs_new_protected:Npn \__gtldoc_test_auxii:w ##1 #2
250 % { \__gtldoc_test_auxiii:N }
251 % }
252 % \exp_args:Noo \__gtldoc_tmp:nn
253 % { \token_to_str:N \TEST }
254 % { \token_to_str:N \TESTEXP }
255 % \cs_new_protected:Npn \__gtldoc_test_auxiii:N #1
256 % {
257 % \token_if_eq_meaning:NNT \q_recursion_tail #1
258 % { \__gtldoc_more:Nw \__gtldoc_test_auxiii:N }
259 % \exp_after:wN \token_if_eq_charcode:NNTF \c_left_brace_str #1
260 % { \__gtldoc_test_arg:w }
261 % {
262 % \msg_error:nnn { gtldoc } { bad-test } {#1}
263 % \use_none_delimit_by_q_recursion_stop:w
264 % }
265 % }
266 % \cs_new_protected:Npn \__gtldoc_test_arg:w
267 % {
268 % \tl_clear:N \l__gtldoc_arg_tl
269 % \exp_args:No \__gtldoc_test_arg_aux:N \c_right_brace_str
270 % }
271 % \cs_new_protected:Npn \__gtldoc_test_arg_aux:N #1
272 % {
273 % \cs_gset_protected:Npn \__gtldoc_test_arg_aux:w ##1 #1 { {##1} }
274 % \cs_gset_protected:Npn \__gtldoc_test_arg_loop:w ##1 \q_recursion_tail
275 % {
276 % \tl_if_in:noTF {##1} {#1}
277 % {
278 % \exp_after:wN \__gtldoc_test_arg_last:nw
279 % \__gtldoc_test_arg_aux:w ##1 \q_recursion_tail
280 % }
281 % {
282 % \tl_put_right:Nn \l__gtldoc_arg_tl {##1}
283 % \__gtldoc_more:Nw \__gtldoc_test_arg_loop:w
284 % }
285 % }
286 % \__gtldoc_test_arg_loop:w
287 % }
288 % \cs_new_eq:NN \__gtldoc_test_arg_aux:w ?
289 % \cs_new_eq:NN \__gtldoc_test_arg_loop:w ?
290 % \cs_new_protected:Npn \__gtldoc_test_arg_last:nw
291 % #1#2 \q_recursion_tail \q_recursion_stop
292 % {
293 % \tl_if_blank:nF {#2}
294 % { \msg_warning:nnn { gtldoc } { discarded } {#2} }
295 % \tl_put_right:Nn \l__gtldoc_arg_tl {#1}
296 % \exp_args:NNe \seq_set_from_clist:Nn \l__gtldoc_arg_seq
297 % { \tl_to_str:N \l__gtldoc_arg_tl }
298 % \exp_args:NNNe \seq_set_filter:NNn \l__gtldoc_arg_seq \l__gtldoc_arg_seq
299 % { \exp_not:N \tl_if_head_eq_charcode_p:nN {##1} \c_backslash_str }
300 % \seq_concat:NNN \l__gtldoc_tested_fn_seq \l__gtldoc_tested_fn_seq \l__gtldoc_arg_seq
301 % }
302 % \group_end:
303 % \gtldoc_get_fn:n { gtl.dtx }
304 % \gtldoc_get_tests:n
305 % {
306 % ../../testfiles/gtl000.lvt ,
307 % ../../testfiles/gtl001.lvt ,
308 % ../../testfiles/gtl002.lvt ,
309 % ../../testfiles/gtl003.lvt ,
310 % ../../testfiles/gtl004.lvt ,
311 % ../../testfiles/gtl005.lvt ,
312 % ../../testfiles/gtl006.lvt ,
313 % ../../testfiles-plain/gtl007.lvt
314 % }
315 % \group_begin:
316 % \seq_set_eq:NN \l__gtldoc_internal_seq \l__gtldoc_fn_seq
317 % \seq_remove_duplicates:N \l__gtldoc_internal_seq
318 % \int_compare:nF
319 % { \seq_count:N \l__gtldoc_internal_seq = \seq_count:N \l__gtldoc_fn_seq }
320 % {
321 % \msg_error:nn { gtldoc } { multi-function }
322 % \seq_set_eq:NN \l__gtldoc_fn_seq \l__gtldoc_internal_seq
323 % }
324 % \seq_remove_duplicates:N \l__gtldoc_tested_fn_seq
325 % \seq_map_inline:Nn \l__gtldoc_fn_seq
326 % {
327 % \seq_if_in:NnTF \l__gtldoc_tested_fn_seq {#1}
328 % { \seq_remove_all:Nn \l__gtldoc_tested_fn_seq {#1} }
329 % { \msg_error:nnn { gtldoc } { doc-no-test } {#1} }
330 % }
331 % \seq_map_inline:Nn \l__gtldoc_tested_fn_seq
332 % { \msg_error:nnn { gtldoc } { test-no-doc } {#1} }
333 % \group_end:
334 % \endgroup
335 %
336 % \subsection{Creating and initialising extended token lists}
337 %
338 % \begin{function}{\gtl_new:N}
339 % \begin{syntax}
340 % \cs{gtl_new:N} \meta{gtl~var}
341 % \end{syntax}
342 % Creates a new \meta{gtl~var} or raises an error if the name is
343 % already taken. The declaration is global. The \meta{gtl~var} will
344 % initially be empty.
345 % \end{function}
346 %
347 % \begin{function}[updated = 2024-01-04]{\gtl_const:Nn, \gtl_const:Ne, \gtl_const:Nx}
348 % \begin{syntax}
349 % \cs{gtl_const:Nn} \meta{gtl~var} \Arg{token list}
350 % \end{syntax}
351 % Creates a new constant \meta{gtl~var} or raises an error if the name
352 % is already taken. The value of the \meta{gtl~var} will be set
353 % globally to the balanced \meta{token list}.
354 % \end{function}
355 %
356 % \begin{function}{\gtl_clear:N, \gtl_gclear:N}
357 % \begin{syntax}
358 % \cs{gtl_clear:N} \meta{gtl~var}
359 % \end{syntax}
360 % Empties the \meta{gtl~var}, locally or globally.
361 % \end{function}
362 %
363 % \begin{function}{\gtl_clear_new:N, \gtl_gclear_new:N}
364 % \begin{syntax}
365 % \cs{gtl_clear_new:N} \meta{gtl~var}
366 % \end{syntax}
367 % Ensures that the \meta{gtl~var} exists globally by applying
368 % \cs{gtl_new:N} if necessary, then applies \cs{gtl_(g)clear:N} to
369 % leave the \meta{gtl~var} empty.
370 % \end{function}
371 %
372 % \begin{function}{\gtl_set_eq:NN, \gtl_gset_eq:NN}
373 % \begin{syntax}
374 % \cs{gtl_set_eq:NN} \meta{gtl~var_1} \meta{gtl~var_2}
375 % \end{syntax}
376 % Sets the content of \meta{gtl~var_1} equal to that of
377 % \meta{gtl~var_2}.
378 % \end{function}
379 %
380 % \begin{function}{\gtl_concat:NNN, \gtl_gconcat:NNN}
381 % \begin{syntax}
382 % \cs{gtl_concat:NNN} \meta{gtl~var_1} \meta{gtl~var_2} \meta{gtl~var_3}
383 % \end{syntax}
384 % Concatenates the content of \meta{gtl~var_2} and \meta{gtl~var_3}
385 % together and saves the result in \meta{gtl~var_1}. The
386 % \meta{gtl~var_2} will be placed at the left side of the new extended
387 % token list.
388 % \end{function}
389 %
390 % \begin{function}[EXP, pTF]{\gtl_if_exist:N}
391 % \begin{syntax}
392 % \cs{gtl_if_exist_p:N} \meta{gtl~var}
393 % \cs{gtl_if_exist:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
394 % \end{syntax}
395 % Tests whether the \meta{gtl~var} is currently defined. This does
396 % not check that the \meta{gtl~var} really is an extended token list
397 % variable.
398 % \end{function}
399 %
400 % \subsection{Adding data to token list variables}
401 %
402 % \begin{function}[updated = 2024-01-04]{\gtl_set:Nn, \gtl_set:Ne, \gtl_set:Nx, \gtl_gset:Nn, \gtl_gset:Ne, \gtl_gset:Nx}
403 % \begin{syntax}
404 % \cs{gtl_set:Nn} \meta{gtl~var} \Arg{token list}
405 % \end{syntax}
406 % Sets \meta{gtl~var} to contain the balanced \meta{token list},
407 % removing any previous content from the variable.
408 % \end{function}
409 %
410 % \begin{function}{\gtl_put_left:Nn, \gtl_gput_left:Nn}
411 % \begin{syntax}
412 % \cs{gtl_put_left:Nn} \meta{gtl~var} \Arg{token list}
413 % \end{syntax}
414 % Appends the balanced \meta{token list} to the left side of the
415 % current content of \meta{gtl~var}.
416 % \end{function}
417 %
418 % \begin{function}{\gtl_put_right:Nn, \gtl_gput_right:Nn}
419 % \begin{syntax}
420 % \cs{gtl_put_right:Nn} \meta{gtl~var} \Arg{token list}
421 % \end{syntax}
422 % Appends the balanced \meta{token list} to the right side of the
423 % current content of \meta{gtl~var}.
424 % \end{function}
425 %
426 % \subsection{Extended token list conditionals}
427 %
428 % \begin{function}[EXP, pTF]{\gtl_if_blank:N}
429 % \begin{syntax}
430 % \cs{gtl_if_blank_p:N} \Arg{gtl~var}
431 % \cs{gtl_if_blank:NTF} \Arg{gtl~var} \Arg{true code} \Arg{false code}
432 % \end{syntax}
433 % Tests if the \meta{gtl~var} consists only of blank spaces. The test
434 % is \texttt{true} if \meta{gtl~var} consists of zero or more explicit
435 % space characters (explicit tokens with character code~$32$ and
436 % category code~$10$), and is \texttt{false} otherwise.
437 % \end{function}
438 %
439 % \begin{function}[EXP,pTF]{\gtl_if_empty:N}
440 % \begin{syntax}
441 % \cs{gtl_if_empty_p:N} \meta{gtl~var}
442 % \cs{gtl_if_empty:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
443 % \end{syntax}
444 % Tests if the \meta{gtl~var} is entirely empty (\emph{i.e.}~contains
445 % no tokens at all).
446 % \end{function}
447 %
448 % \begin{function}[EXP,pTF]{\gtl_if_eq:NN}
449 % \begin{syntax}
450 % \cs{gtl_if_eq_p:NN} \Arg{gtl~var_1} \Arg{gtl~var_2}
451 % \cs{gtl_if_eq:NNTF} \Arg{gtl~var_1} \Arg{gtl~var_2} \Arg{true code} \Arg{false code}
452 % \end{syntax}
453 % Tests if \meta{gtl~var_1} and \meta{gtl~var_2} have the same
454 % content. The test is \texttt{true} if the two contain the same list
455 % of tokens (identical in both character code and category code).
456 % \end{function}
457 %
458 % \begin{function}[EXP,pTF]{\gtl_if_single_token:N}
459 % \begin{syntax}
460 % \cs{gtl_if_single_token_p:N} \meta{gtl~var}
461 % \cs{gtl_if_single_token:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
462 % \end{syntax}
463 % Tests if the content of the \meta{gtl~var} consists of a single
464 % token. Such a token list has token count $1$ according to
465 % \cs{gtl_count_tokens:N}.
466 % \end{function}
467 %
468 % \begin{function}[EXP,pTF]{\gtl_if_tl:N}
469 % \begin{syntax}
470 % \cs{gtl_if_tl_p:N} \meta{gtl~var}
471 % \cs{gtl_if_tl:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
472 % \end{syntax}
473 % Tests if the \meta{gtl~var} is balanced.
474 % \end{function}
475 %
476 % \subsection{The first token from an extended token list}
477 %
478 % \begin{function}[EXP]{\gtl_head:N}
479 % \begin{syntax}
480 % \cs{gtl_head:N} \meta{gtl~var}
481 % \end{syntax}
482 % Leaves in the input stream the first token in the \meta{gtl~var}.
483 % If the \meta{gtl~var} is empty, nothing is left in the input stream.
484 % \end{function}
485 %
486 % \begin{function}[EXP]{\gtl_head_do:NN}
487 % \begin{syntax}
488 % \cs{gtl_head_do:NN} \meta{gtl~var} \meta{cs}
489 % \end{syntax}
490 % Leaves in the input stream the \meta{control sequence} followed by
491 % the first token in \meta{gtl~var}. If the \meta{gtl~var} is empty,
492 % the \meta{cs} is followed by \cs{q_no_value}.
493 % \end{function}
494 %
495 % \begin{function}[EXP, TF]{\gtl_head_do:NN}
496 % \begin{syntax}
497 % \cs{gtl_head_do:NNTF} \meta{gtl~var} \meta{cs} \Arg{true code} \Arg{false code}
498 % \end{syntax}
499 % If the \meta{gtl~var} is empty, leaves the \meta{false code} in the
500 % input stream. Otherwise leaves the \meta{control sequence}
501 % followed by the first token in \meta{gtl~var} and the \meta{true
502 % code}.
503 % \end{function}
504 %
505 % \begin{function}{\gtl_get_left:NN}
506 % \begin{syntax}
507 % \cs{gtl_get_left:NN} \meta{gtl~var_1} \meta{gtl~var_2}
508 % \end{syntax}
509 % Stores the first token from \meta{gtl~var_1} in \meta{gtl~var_2} as
510 % an single-token extended token list, without removing it from
511 % \meta{gtl~var_1}.
512 % \end{function}
513 %
514 % \begin{function}{\gtl_pop_left:N, \gtl_gpop_left:N}
515 % \begin{syntax}
516 % \cs{gtl_pop_left:N} \meta{gtl~var}
517 % \end{syntax}
518 % Remove the first token from \meta{gtl~var_1}.
519 % If the \meta{gtl~var_1} is empty nothing happens.
520 % \end{function}
521 %
522 % \begin{function}{\gtl_pop_left:NN, \gtl_gpop_left:NN}
523 % \begin{syntax}
524 % \cs{gtl_pop_left:NN} \meta{gtl~var_1} \meta{gtl~var_2}
525 % \end{syntax}
526 % Stores the first token from \meta{gtl~var_1} in \meta{gtl~var_2} as
527 % an single-token extended token list, and remove it from
528 % \meta{gtl~var_1}. If the \meta{gtl~var_1} is empty it remains so,
529 % and \meta{gtl~var_2} is set to contain \cs{q_no_value}.
530 % \end{function}
531 %
532 % \begin{function}[EXP,pTF]{\gtl_if_head_eq_catcode:NN}
533 % \begin{syntax}
534 % \cs{gtl_if_head_eq_catcode_p:NN} \Arg{gtl~var} \meta{test token}
535 % \cs{gtl_if_head_eq_catcode:NNTF} \Arg{gtl~var} \meta{test token}
536 % ~~\Arg{true code} \Arg{false code}
537 % \end{syntax}
538 % Tests if the first token in \meta{gtl~var} has the same category
539 % code as the \meta{test token}. In the case where \meta{gtl~var} is
540 % empty, the test will always be \texttt{false}.
541 % \end{function}
542 %
543 % \begin{function}[EXP,pTF]{\gtl_if_head_eq_charcode:NN}
544 % \begin{syntax}
545 % \cs{gtl_if_head_eq_charcode_p:NN} \Arg{gtl~var} \meta{test token}
546 % \cs{gtl_if_head_eq_charcode:NNTF} \Arg{gtl~var} \meta{test token}
547 % ~~\Arg{true code} \Arg{false code}
548 % \end{syntax}
549 % Tests if the first token in \meta{gtl~var} has the same character
550 % code as the \meta{test token}. In the case where \meta{gtl~var} is
551 % empty, the test will always be \texttt{false}.
552 % \end{function}
553 %
554 % \begin{function}[EXP,pTF]{\gtl_if_head_eq_meaning:NN}
555 % \begin{syntax}
556 % \cs{gtl_if_head_eq_meaning_p:NN} \Arg{gtl~var} \meta{test token}
557 % \cs{gtl_if_head_eq_meaning:NNTF} \Arg{gtl~var} \meta{test token}
558 % ~~\Arg{true code} \Arg{false code}
559 % \end{syntax}
560 % Tests if the first token in \meta{gtl~var} has the same meaning as
561 % the \meta{test token}. In the case where \meta{gtl~var} is empty,
562 % the test will always be \texttt{false}.
563 % \end{function}
564 %
565 % \begin{function}[EXP,pTF]
566 % {
567 % \gtl_if_head_is_group_begin:N,
568 % \gtl_if_head_is_group_end:N,
569 % \gtl_if_head_is_N_type:N,
570 % \gtl_if_head_is_space:N,
571 % }
572 % \begin{syntax}
573 % \cs{gtl_if_head_is_group_begin_p:N} \Arg{gtl~var}
574 % \cs{gtl_if_head_is_group_begin:NTF} \Arg{gtl~var}
575 % ~~\Arg{true code} \Arg{false code}
576 % \end{syntax}
577 % Tests whether the first token in \meta{gtl~var} is an explicit
578 % begin-group character, an explicit end-group character, an |N|-type
579 % token, or a space. In the case where \meta{gtl~var} is empty, the
580 % test will always be \texttt{false}.
581 % \end{function}
582 %
583 % \subsection{The first few tokens from an extended token list}
584 %
585 % \begin{function}[EXP]{\gtl_left_tl:N}
586 % \begin{syntax}
587 % \cs{gtl_left_tl:N} \meta{gtl~var}
588 % \end{syntax}
589 % Leaves in the input stream all tokens in \meta{gtl~var} until the
590 % first extra begin-group or extra end-group character, within
591 % \cs{exp_not:n}. This is the longest balanced token list starting
592 % from the left of \meta{gtl~var}.
593 % \end{function}
594 %
595 % \begin{function}{\gtl_pop_left_tl:N, \gtl_gpop_left_tl:N}
596 % \begin{syntax}
597 % \cs{gtl_pop_left_tl:N} \meta{gtl~var}
598 % \end{syntax}
599 % Remove from the \meta{gtl~var} all tokens before the first extra
600 % begin-group or extra end-group character. The tokens that are
601 % removed form the longest balanced token list starting from the left
602 % of \meta{gtl~var}.
603 % \end{function}
604 %
605 % \begin{function}[EXP]{\gtl_left_item:NF}
606 % \begin{syntax}
607 % \cs{gtl_left_item:NF} \meta{gtl~var} \Arg{false code}
608 % \end{syntax}
609 % Leaves in the input stream the first \meta{item} of the
610 % \meta{gtl~var}: this is identical to \cs{tl_head:n} applied to the
611 % result of \cs{gtl_left_tl:N}. If there is no such item, the
612 % \meta{false code} is left in the input stream.
613 % \end{function}
614 %
615 % \begin{function}[TF]{\gtl_pop_left_item:NN, \gtl_gpop_left_item:NN}
616 % \begin{syntax}
617 % \cs{gtl_pop_left_item:NNTF} \meta{gtl~var} \meta{tl~var}
618 % \Arg{true~code} \Arg{false~code}
619 % \end{syntax}
620 % Stores the first item of \meta{gtl~var} in \meta{tl~var}, locally,
621 % and removes it from \meta{gtl~var}, together with any space before
622 % it. If there is no such item, the \meta{gtl~var} is not affected,
623 % and the meta{tl~var} may or may not be affected.
624 % \end{function}
625 %
626 % \begin{function}[EXP]{\gtl_left_text:NF}
627 % \begin{syntax}
628 % \cs{gtl_left_text:NF} \meta{gtl~var} \Arg{false code}
629 % \end{syntax}
630 % Starting from the first token in \meta{gtl~var}, this function finds
631 % a pattern of the form \meta{tokens_1} \Arg{tokens_2}, where the
632 % \meta{tokens_1} contain no begin-group nor end-group characters,
633 % then leaves \meta{tokens_1} \Arg{tokens_2} in the input stream,
634 % within \cs{exp_not:n}. If no such pattern exists (this happens if
635 % the result of \cs{gtl_left_tl:N} contains no brace group), the
636 % \meta{false code} is run instead.
637 % \end{function}
638 %
639 % \begin{function}{\gtl_pop_left_text:N, \gtl_gpop_left_text:N}
640 % \begin{syntax}
641 % \cs{gtl_pop_left_text:N} \meta{gtl~var}
642 % \end{syntax}
643 % Starting from the first token in \meta{gtl~var}, this function finds
644 % a pattern of the form \meta{tokens_1} \Arg{tokens_2}, where the
645 % \meta{tokens_1} contain no begin-group nor end-group characters,
646 % then removes \meta{tokens_1} \Arg{tokens_2} from \meta{gtl~var}. If
647 % no such pattern exists (this happens if the result of
648 % \cs{gtl_left_tl:N} contains no brace group), the \meta{gtl~var} is
649 % not modified instead.
650 % \end{function}
651 %
652 % \subsection{Working with the contents of extended token lists}
653 %
654 % \begin{function}[EXP]{\gtl_count_tokens:N}
655 % \begin{syntax}
656 % \cs{gtl_count_tokens:N} \meta{gtl~var}
657 % \end{syntax}
658 % Counts the number of tokens in the \meta{gtl~var} and leaves this
659 % information in the input stream.
660 % \end{function}
661 %
662 % \begin{function}[EXP]{\gtl_extra_begin:N, \gtl_extra_end:N}
663 % \begin{syntax}
664 % \cs{gtl_extra_begin:N} \meta{gtl~var}
665 % \end{syntax}
666 % Counts the number of explicit extra begin-group (or end-group)
667 % characters in the \meta{gtl~var} and leaves this information in the
668 % input stream.
669 % \end{function}
670 %
671 % \begin{function}{\gtl_show:N, \gtl_log:N}
672 % \begin{syntax}
673 % \cs{gtl_show:N} \meta{gtl~var}
674 % \end{syntax}
675 % Displays the content of the \meta{gtl~var} on the terminal or in
676 % the log file.
677 % \end{function}
678 %
679 % \begin{function}[EXP, added = 2018-04-04]{\gtl_to_str:N, \gtl_to_str:n}
680 % \begin{syntax}
681 % \cs{gtl_to_str:N} \meta{gtl~var}
682 % \end{syntax}
683 % Converts the content of the \meta{gtl~var} into a series of
684 % characters with category code $12$ (other) with the exception of
685 % spaces, which retain category code $10$ (space). This string is
686 % then left in the input stream.
687 % \end{function}
688 %
689 % \subsection{Constant extended token lists}
690 %
691 % \begin{variable}{\c_empty_gtl}
692 % Constant that is always empty.
693 % \end{variable}
694 %
695 % \begin{variable}{\c_group_begin_gtl}
696 % An explicit begin-group character contained in an extended token
697 % list.
698 % \end{variable}
699 %
700 % \begin{variable}{\c_group_end_gtl}
701 % An explicit end-group character contained in an extended token list.
702 % \end{variable}
703 %
704 % \subsection{Future perhaps}
705 %
706 % \begin{itemize}
707 % \item Test if a token appears in an extended token list.
708 % \item Test if an extended token list appears in another.
709 % \item Remove an extended token list from another, once or every time
710 % it appears.
711 % \item Replace an extended token list by another in a third: once, or
712 % every time it appears.
713 % \item Case statement.
714 % \item Mapping?
715 % \item Inserting an extended token list into the input stream, with all
716 % its glorious unbalanced braces.
717 % \item Convert in various ways to a token list.
718 % \item Reverse the order of tokens.
719 % \item Extract a token given its position.
720 % \item Extract a range of tokens given their position.
721 % \item Trim spaces.
722 % \item Crazy idea below.
723 % \end{itemize}
724 %
725 % We could add (with lots of work) the expandable function
726 % \begin{quote}\ttfamily\obeylines\obeyspaces
727 % \cs{gtl_concat:nF}
728 % \{
729 % \Arg{tl_1} \Arg{start_1} \Arg{stop_1}
730 % \Arg{tl_2} \Arg{start_2} \Arg{stop_2}
731 % \ldots{}
732 % \Arg{tl_n} \Arg{start_n} \Arg{stop_n}
733 % \}
734 % \Arg{false code}
735 % \end{quote}
736 % For each triplet, this function builds the sub-token list of \meta{tl_i}
737 % corresponding to the tokens ranging from position \meta{start_i} to
738 % position \meta{stop_i} of \meta{tl_i}. The results obtained for each triplet
739 % are then concatenated. If nothing bad happens (see below), the
740 % concatenation is left in the input stream, and the \meta{false code} is
741 % removed. Two cases can lead to running the \meta{false code} (and dropping
742 % the first argument altogether). The first case is when the number of
743 % brace groups in \cs{gtl_concat:nF} is not a multiple of 3. The second
744 % case is when the concatenation gives rise to an unbalanced token list:
745 % then the result is not a valid token list. Note that each part is
746 % allowed to be unbalanced: only the full result must be balanced.
747 %
748 % \end{documentation}
749 %
750 % \begin{implementation}
751 %
752 % \section{\pkg{gtl} implementation}
753 %
754 % Some support packages are loaded first, then we declare the package's
755 % name, date, version, and purpose.
756 %
757 % \begin{macrocode}
758 %<*package>
759 % \end{macrocode}
760 %
761 % \begin{macrocode}
762 %<@@=gtl>
763 % \end{macrocode}
764 %
765 % Load \pkg{expl3}, either through \cs{RequirePackage} or through
766 % inputting the generic loader, depending on the format in use.
767 % \begin{macrocode}
768 \begingroup\expandafter\expandafter\expandafter\endgroup
769 \expandafter\ifx\csname RequirePackage\endcsname\relax
770 \input expl3-generic.tex
771 \else
772 \RequirePackage{expl3}[2017/11/14]
773 \fi
774 \ExplSyntaxOn
775 \cs_if_exist:NTF \ProvidesExplPackage
776 {
777 \cs_new_eq:NN \@@_end_package_hook: \prg_do_nothing:
778 \ExplSyntaxOff
779 \ProvidesExplPackage
780 }
781 {
782 \cs_new_eq:NN \@@_end_package_hook: \ExplSyntaxOff
783 \group_begin:
784 \ExplSyntaxOff
785 \cs_set_protected:Npn \@@_tmp:w #1#2#3#4
786 {
787 \group_end:
788 \tl_gset:ce { ver @ #1 . sty } { #2 ~ v#3 ~ #4 }
789 \cs_if_exist_use:NF \wlog { \iow_log:e }
790 { Package: ~ #1 ~ #2 ~ v#3 ~ #4 }
791 }
792 \@@_tmp:w
793 }
794 {gtl} {2024/01/04} {0.6} {Manipulate unbalanced lists of tokens}
795 % \end{macrocode}
796 %
797 % \subsection{Helpers}
798 %
799 % \begin{macrocode}
800 \cs_generate_variant:Nn \use:nn { no }
801 % \end{macrocode}
802 %
803 % \begin{macro}[EXP]{\@@_exp_not_n:N}
804 % Used in one case where we need to prevent expansion of a token
805 % within an |x|-expanding definition. Using \cs{exp_not:N} there
806 % would fail when the argument is a macro parameter character.
807 % \begin{macrocode}
808 \cs_new:Npn \@@_exp_not_n:N #1 { \exp_not:n {#1} }
809 % \end{macrocode}
810 % \end{macro}
811 %
812 % \begin{macro}[EXP]{\@@_brace:nn, \@@_brace_swap:nn}
813 % Those functions are used to add some tokens, |#1|, to an item |#2|
814 % in an extended token list: \cs{@@_brace:nn} adds tokens on the left,
815 % while \cs{@@_brace_swap:nn} adds them on the right.
816 % \begin{macrocode}
817 \cs_new:Npn \@@_brace:nn #1#2 { { #1 #2 } }
818 \cs_new:Npn \@@_brace_swap:nn #1#2 { { #2 #1 } }
819 % \end{macrocode}
820 % \end{macro}
821 %
822 % \begin{macro}[EXP]{\@@_strip_nil_mark:w,}
823 % \begin{macro}[EXP]{\@@_strip_nil_mark_aux:w}
824 % Removes the following \cs{q_nil} \cs{q_mark} without losing any
825 % braces, and places the result into \cs{exp_not:n}.
826 % \begin{macrocode}
827 \cs_new:Npn \@@_strip_nil_mark:w
828 { \@@_strip_nil_mark_aux:w \prg_do_nothing: }
829 \cs_new:Npn \@@_strip_nil_mark_aux:w #1 \q_nil \q_mark
830 { \exp_not:o {#1} }
831 % \end{macrocode}
832 % \end{macro}
833 % \end{macro}
834 %
835 % \subsection{Structure of extended token lists}
836 %
837 % Token lists must have balanced braces (or rather, begin-group and
838 % end-group characters). Extended token lists lift this requirement,
839 % and can represent arbitrary lists of tokens. A list of tokens can
840 % fail to be balanced in two ways: one may encounter too many end-group
841 % characters near the beginning of the list, or too many begin-group
842 % characters near the end of the list. In fact, a list of tokens always
843 % has the form
844 % \begin{quote}\ttfamily
845 % \meta{b_1} \} \ldots{} \meta{b_n} \}
846 % \meta{c}
847 % \{ \meta{e_1} \ldots{} \{ \meta{e_p}
848 % \end{quote}
849 % where the \meta{b_i}, \meta{c}, and \meta{e_i} are all balanced token
850 % lists. This can be seen by listing the tokens, and keeping track of a
851 % counter, which starts at~$0$, and is incremented at each begin-group
852 % character, and decremented at each end-group character: then the
853 % \meta{b_i} are delimited by positions where the counter reaches a new
854 % minimum, whereas the \meta{e_i} are delimited by positions where the
855 % counter last takes a given negative value. Such a token list is
856 % stored as
857 % \begin{quote}\ttfamily
858 % \cs{s_@@}
859 % \{ \Arg{b_1} \ldots{} \Arg{b_n} \}
860 % \Arg{c}
861 % \{ \Arg{e_p} \ldots{} \Arg{e_1} \}
862 % \end{quote}
863 % Note that the \meta{e_i} are in a reversed order, as this makes the
864 % ends of extended token lists more accessible. Balanced token lists
865 % have $n = p = 0$: the first and third parts are empty, while the
866 % second contains the tokens.
867 %
868 % In the following code comments, the balanced token lists \meta{b_i}
869 % are called ``leading chunks'', \meta{c} is called ``middle chunk'',
870 % and \meta{e_i} are called ``trailing chunks''. It is important to
871 % note that a balanced sub-list of a gtl must be entirely contained in
872 % one of the chunk.
873 %
874 % \begin{variable}{\s_@@}
875 % This marker appears at the start of extended token lists.
876 % \begin{macrocode}
877 \cs_new_eq:NN \s_@@ \scan_stop:
878 % \end{macrocode}
879 % \end{variable}
880 %
881 % \begin{macro}
882 % {
883 % \gtl_set:Nn, \gtl_gset:Nn, \gtl_const:Nn,
884 % \gtl_set:Ne, \gtl_gset:Ne, \gtl_const:Ne,
885 % \gtl_set:Nx, \gtl_gset:Nx, \gtl_const:Nx
886 % }
887 % Storing a balanced token list into an extended token list variable
888 % simply means adding \cs{s_@@} and two empty brace
889 % groups: there are no leading nor trailing chunks.
890 % \begin{macrocode}
891 \cs_new_protected:Npn \gtl_set:Nn { \@@_set:NNn \tl_set:Nn }
892 \cs_new_protected:Npn \gtl_gset:Nn { \@@_set:NNn \tl_gset:Nn }
893 \cs_new_protected:Npn \gtl_const:Nn { \@@_set:NNn \tl_const:Nn }
894 \cs_generate_variant:Nn \gtl_set:Nn { Ne , Nx }
895 \cs_generate_variant:Nn \gtl_gset:Nn { Ne , Nx }
896 \cs_generate_variant:Nn \gtl_const:Nn { Ne , Nx }
897 \cs_new_protected:Npn \@@_set:NNn #1#2#3
898 { #1 #2 { \s_@@ { } {#3} { } } }
899 % \end{macrocode}
900 % \end{macro}
901 %
902 % \begin{variable}{\c_empty_gtl}
903 % An empty extended token list, obtained thanks to the
904 % \cs{gtl_const:Nn} function just defined.
905 % \begin{macrocode}
906 \gtl_const:Nn \c_empty_gtl { }
907 % \end{macrocode}
908 % \end{variable}
909 %
910 % \begin{variable}{\c_group_begin_gtl, \c_group_end_gtl}
911 % Extended token lists with exactly one begin-group/end-group
912 % character are built by including a single (empty) leading or
913 % trailing chunk.
914 % \begin{macrocode}
915 \tl_const:Nn \c_group_end_gtl { \s_@@ { { } } { } { } }
916 \tl_const:Nn \c_group_begin_gtl { \s_@@ { } { } { { } } }
917 % \end{macrocode}
918 % \end{variable}
919 %
920 % \subsection{Creating extended token list variables}
921 %
922 % \begin{macro}{\gtl_new:N}
923 % A new extended token list is created empty.
924 % \begin{macrocode}
925 \cs_new_protected:Npn \gtl_new:N #1
926 { \cs_new_eq:NN #1 \c_empty_gtl }
927 % \end{macrocode}
928 % \end{macro}
929 %
930 % \begin{macro}{\gtl_set_eq:NN, \gtl_gset_eq:NN}
931 % All the data about an extended token list is stored as a single
932 % token list, so copying is easy.
933 % \begin{macrocode}
934 \cs_new_eq:NN \gtl_set_eq:NN \tl_set_eq:NN
935 \cs_new_eq:NN \gtl_gset_eq:NN \tl_gset_eq:NN
936 % \end{macrocode}
937 % \end{macro}
938 %
939 % \begin{macro}{\gtl_clear:N, \gtl_gclear:N}
940 % Clearing an extended token list by setting it to the empty one.
941 % \begin{macrocode}
942 \cs_new_protected:Npn \gtl_clear:N #1
943 { \gtl_set_eq:NN #1 \c_empty_gtl }
944 \cs_new_protected:Npn \gtl_gclear:N #1
945 { \gtl_gset_eq:NN #1 \c_empty_gtl }
946 % \end{macrocode}
947 % \end{macro}
948 %
949 % \begin{macro}{\gtl_clear_new:N, \gtl_gclear_new:N}
950 % If the variable exists, clear it. Otherwise declare it.
951 % \begin{macrocode}
952 \cs_new_protected:Npn \gtl_clear_new:N #1
953 { \gtl_if_exist:NTF #1 { \gtl_clear:N #1 } { \gtl_new:N #1 } }
954 \cs_new_protected:Npn \gtl_gclear_new:N #1
955 { \gtl_if_exist:NTF #1 { \gtl_gclear:N #1 } { \gtl_new:N #1 } }
956 % \end{macrocode}
957 % \end{macro}
958 %
959 % \begin{macro}[EXP, pTF]{\gtl_if_exist:N}
960 % Again a copy of token list functions.
961 % \begin{macrocode}
962 \prg_new_eq_conditional:NNn \gtl_if_exist:N \tl_if_exist:N
963 { p , T , F , TF }
964 % \end{macrocode}
965 % \end{macro}
966 %
967 % \subsection{Adding data to extended token list variables}
968 %
969 % \begin{macro}{\gtl_put_left:Nn, \gtl_gput_left:Nn}
970 % \begin{macro}[rEXP]{\@@_put_left:wn}
971 % If there is no leading chunk in the gtl variable, then add the new
972 % material to the middle chunk. Otherwise add it to the first
973 % leading chunk, namely the first brace group in the first argument
974 % of \cs{@@_put_left:wn}.
975 % \begin{macrocode}
976 \cs_new_protected:Npn \gtl_put_left:Nn #1#2
977 { \tl_set:Ne #1 { \exp_after:wN \@@_put_left:wn #1 {#2} } }
978 \cs_new_protected:Npn \gtl_gput_left:Nn #1#2
979 { \tl_gset:Ne #1 { \exp_after:wN \@@_put_left:wn #1 {#2} } }
980 \cs_new:Npn \@@_put_left:wn \s_@@ #1#2#3 #4
981 {
982 \tl_if_empty:nTF {#1}
983 { \exp_not:n { \s_@@ { } { #4 #2 } {#3} } }
984 {
985 \s_@@
986 { \exp_not:o { \@@_brace:nn {#4} #1 } }
987 { \exp_not:n {#2} }
988 { \exp_not:n {#3} }
989 }
990 }
991 % \end{macrocode}
992 % \end{macro}
993 % \end{macro}
994 %
995 % \begin{macro}{\gtl_put_right:Nn, \gtl_gput_right:Nn}
996 % \begin{macro}[rEXP]{\@@_put_right:wn}
997 % Symmetric of \cs{gtl_put_left:Nn}.
998 % \begin{macrocode}
999 \cs_new_protected:Npn \gtl_put_right:Nn #1#2
1000 { \tl_set:Ne #1 { \exp_after:wN \@@_put_right:wn #1 {#2} } }
1001 \cs_new_protected:Npn \gtl_gput_right:Nn #1#2
1002 { \tl_gset:Ne #1 { \exp_after:wN \@@_put_right:wn #1 {#2} } }
1003 \cs_new:Npn \@@_put_right:wn \s_@@ #1#2#3 #4
1004 {
1005 \tl_if_empty:nTF {#3}
1006 { \exp_not:n { \s_@@ {#1} { #2 #4 } { } } }
1007 {
1008 \s_@@
1009 { \exp_not:n {#1} }
1010 { \exp_not:n {#2} }
1011 { \exp_not:o { \@@_brace_swap:nn {#4} #3 } }
1012 }
1013 }
1014 % \end{macrocode}
1015 % \end{macro}
1016 % \end{macro}
1017 %
1018 % \begin{macro}{\gtl_concat:NNN, \gtl_gconcat:NNN}
1019 % \begin{macro}[rEXP]
1020 % {
1021 % \@@_concat:ww,
1022 % \@@_concat_aux:nnnnnn,
1023 % \@@_concat_auxi:nnnnnn,
1024 % \@@_concat_auxii:nnnnnn,
1025 % \@@_concat_auxiii:w,
1026 % \@@_concat_auxiv:nnnn,
1027 % \@@_concat_auxv:wnwnn,
1028 % \@@_concat_auxvi:nnwnwnn
1029 % }
1030 % Concatenating two lists of tokens of the form
1031 % \begin{quote}\ttfamily
1032 % \cs{s_@@}
1033 % \{ \Arg{b_1} \ldots{} \Arg{b_n} \}
1034 % \Arg{c}
1035 % \{ \Arg{e_p} \ldots{} \Arg{e_1} \}
1036 % \end{quote}
1037 % is not an easy task. The \meta{e} parts of the first join with the
1038 % \meta{b} parts of the second to make balanced pairs, and the
1039 % follow-up depends on whether there were more \meta{e} parts or more
1040 % \meta{b} parts.
1041 % \begin{macrocode}
1042 \cs_new_protected:Npn \gtl_concat:NNN #1#2#3
1043 { \tl_set:Ne #1 { \exp_last_two_unbraced:Noo \@@_concat:ww #2 #3 } }
1044 \cs_new_protected:Npn \gtl_gconcat:NNN #1#2#3
1045 { \tl_gset:Ne #1 { \exp_last_two_unbraced:Noo \@@_concat:ww #2 #3 } }
1046 \cs_new:Npn \@@_concat:ww \s_@@ #1#2#3 \s_@@ #4#5#6
1047 {
1048 \tl_if_blank:nTF {#3}
1049 {
1050 \tl_if_blank:nTF {#4}
1051 { \@@_concat_aux:nnnnnn }
1052 { \@@_concat_auxi:nnnnnn }
1053 }
1054 {
1055 \tl_if_blank:nTF {#4}
1056 { \@@_concat_auxii:nnnnnn }
1057 { \@@_concat_auxiv:nnnn }
1058 }
1059 {#1} {#2} {#3} {#4} {#5} {#6}
1060 }
1061 \cs_new:Npn \@@_concat_aux:nnnnnn #1#2#3#4#5#6
1062 { \exp_not:n { \s_@@ {#1} { #2 #5 } {#6} } }
1063 \cs_new:Npn \@@_concat_auxi:nnnnnn #1#2#3#4#5#6
1064 {
1065 \s_@@
1066 {
1067 \exp_not:n {#1}
1068 \exp_not:f
1069 { \@@_concat_auxiii:w \@@_brace:nn {#2} #4 ~ \q_stop }
1070 }
1071 { \exp_not:n {#5} }
1072 { \exp_not:n {#6} }
1073 }
1074 \cs_new:Npn \@@_concat_auxii:nnnnnn #1#2#3#4#5#6
1075 {
1076 \s_@@
1077 { \exp_not:n {#1} }
1078 { \exp_not:n {#2} }
1079 {
1080 \exp_not:n {#6}
1081 \exp_not:f
1082 { \@@_concat_auxiii:w \@@_brace_swap:nn {#5} #3 ~ \q_stop }
1083 }
1084 }
1085 \cs_new:Npn \@@_concat_auxiii:w #1 ~ #2 \q_stop {#1}
1086 \cs_new:Npn \@@_concat_auxiv:nnnn #1#2#3#4
1087 {
1088 \tl_if_single:nTF {#3}
1089 { \@@_concat_auxv:wnwnn }
1090 { \@@_concat_auxvi:nnwnwnn }
1091 #3 ~ \q_mark #4 ~ \q_mark {#1} {#2}
1092 }
1093 \cs_new:Npn \@@_concat_auxv:wnwnn
1094 #1#2 \q_mark #3#4 \q_mark #5#6
1095 {
1096 \@@_concat:ww
1097 \s_@@ {#5} { #6 { #1 #3 } } { }
1098 \s_@@ {#4}
1099 }
1100 \cs_new:Npn \@@_concat_auxvi:nnwnwnn
1101 #1#2#3 \q_mark #4#5 \q_mark #6#7
1102 {
1103 \@@_concat:ww
1104 \s_@@ {#6} {#7} { { #2 { #1 #4 } } #3 }
1105 \s_@@ {#5}
1106 }
1107 % \end{macrocode}
1108 % \end{macro}
1109 % \end{macro}
1110 %
1111 % \subsection{Showing extended token lists}
1112 %
1113 % \begin{macro}[EXP]{\gtl_to_str:N, \gtl_to_str:n}
1114 % \begin{macro}[EXP]
1115 % {
1116 % \@@_to_str:w,
1117 % \@@_to_str_loopi:nnw,
1118 % \@@_to_str_testi:nnw,
1119 % \@@_to_str_endi:nnn,
1120 % \@@_to_str_loopii:nnw,
1121 % \@@_to_str_endii:nnw
1122 % }
1123 % \begin{macrocode}
1124 \cs_new:Npn \gtl_to_str:N #1 { \exp_after:wN \@@_to_str:w #1 }
1125 \cs_new:Npn \gtl_to_str:n #1 { \@@_to_str:w #1 }
1126 \cs_new:Npn \@@_to_str:w \s_@@ #1#2#3
1127 { \@@_to_str_loopi:nnw { } #1 \q_nil \q_mark {#2} {#3} }
1128 \cs_new:Npe \@@_to_str_loopi:nnw #1#2
1129 {
1130 \exp_not:N \quark_if_nil:nTF {#2}
1131 { \exp_not:N \@@_to_str_testi:nnw {#1} {#2} }
1132 { \exp_not:N \@@_to_str_loopi:nnw { #1 #2 \iow_char:N \} } }
1133 }
1134 \cs_new:Npe \@@_to_str_testi:nnw #1#2#3 \q_mark
1135 {
1136 \exp_not:N \tl_if_empty:nTF {#3}
1137 { \exp_not:N \@@_to_str_endi:nnn {#1} }
1138 {
1139 \exp_not:N \@@_to_str_loopi:nnw
1140 { #1 #2 \iow_char:N \} } #3 \exp_not:N \q_mark
1141 }
1142 }
1143 \cs_new:Npn \@@_to_str_endi:nnn #1#2#3
1144 { \@@_to_str_loopii:nnw #3 { #1 #2 } \q_nil \q_stop }
1145 \cs_new:Npe \@@_to_str_loopii:nnw #1#2
1146 {
1147 \exp_not:N \quark_if_nil:nTF {#2}
1148 { \exp_not:N \@@_to_str_testii:nnw {#1} {#2} }
1149 { \exp_not:N \@@_to_str_loopii:nnw { #2 \iow_char:N \{ #1 } }
1150 }
1151 \cs_new:Npe \@@_to_str_testii:nnw #1#2#3 \q_stop
1152 {
1153 \exp_not:N \tl_if_empty:nTF {#3}
1154 { \exp_not:N \tl_to_str:n {#1} }
1155 {
1156 \exp_not:N \@@_to_str_loopii:nnw
1157 { #2 \iow_char:N \{ #1 } #3 \exp_not:N \q_stop
1158 }
1159 }
1160 % \end{macrocode}
1161 % \end{macro}
1162 % \end{macro}
1163 %
1164 % \begin{macro}{\gtl_show:N, \gtl_log:N}
1165 % \begin{macro}{\@@_show:NNN}
1166 % Display the variable name, then its string representation. Before
1167 % that, test that the variable indeed exists, and if appropriate
1168 % throw an error message by sending the undefined variable to
1169 % \cs{tl_show:N} or \cs{tl_log:N}.
1170 % \begin{macrocode}
1171 \cs_new_protected:Npn \gtl_show:N
1172 { \@@_show:NNN \tl_show:n \tl_show:N }
1173 \cs_new_protected:Npn \gtl_log:N
1174 { \@@_show:NNN \tl_log:n \tl_log:N }
1175 \cs_new_protected:Npn \@@_show:NNN #1#2#3
1176 {
1177 \gtl_if_exist:NTF #3
1178 { \exp_args:Ne #1 { \token_to_str:N #3 = \gtl_to_str:N #3 } }
1179 { #2 #3 }
1180 }
1181 % \end{macrocode}
1182 % \end{macro}
1183 % \end{macro}
1184 %
1185 % \subsection{Extended token list conditionals}
1186 %
1187 % \begin{macro}[EXP, pTF]{\gtl_if_eq:NN}
1188 % Two extended token lists are equal if the underlying token lists
1189 % are the same.
1190 % \begin{macrocode}
1191 \prg_new_eq_conditional:NNn \gtl_if_eq:NN \tl_if_eq:NN
1192 { p , T , F , TF }
1193 % \end{macrocode}
1194 % \end{macro}
1195 %
1196 % \begin{macro}[EXP, pTF]{\gtl_if_empty:N}
1197 % An extended token list is empty if it is equal to the empty gtl.
1198 % \begin{macrocode}
1199 \prg_new_conditional:Npnn \gtl_if_empty:N #1 { p , T , F , TF }
1200 {
1201 \tl_if_eq:NNTF #1 \c_empty_gtl
1202 { \prg_return_true: } { \prg_return_false: }
1203 }
1204 % \end{macrocode}
1205 % \end{macro}
1206 %
1207 % \begin{macro}[EXP, pTF]{\gtl_if_tl:N}
1208 % \begin{macro}[EXP]{\@@_if_tl_return:w}
1209 % A gtl is balanced if it has neither leading nor trailing chunk.
1210 % \begin{macrocode}
1211 \prg_new_conditional:Npnn \gtl_if_tl:N #1 { p , T , F , TF }
1212 { \exp_after:wN \@@_if_tl_return:w #1 }
1213 \cs_new:Npn \@@_if_tl_return:w \s_@@ #1#2#3
1214 {
1215 \tl_if_empty:nTF { #1 #3 }
1216 { \prg_return_true: } { \prg_return_false: }
1217 }
1218 % \end{macrocode}
1219 % \end{macro}
1220 % \end{macro}
1221 %
1222 % \begin{macro}[EXP, pTF]{\gtl_if_single_token:N}
1223 % \begin{macro}[EXP]{\@@_if_single_token_return:w}
1224 % If there are neither leading nor trailing chunks then the gtl is a
1225 % single token if and only if the middle chunk is a single token.
1226 % Otherwise the gtl is a single token only if it is exactly a
1227 % begin-group or an end-group token.
1228 % \begin{macrocode}
1229 \prg_new_conditional:Npnn \gtl_if_single_token:N #1 { p , T , F , TF }
1230 { \exp_after:wN \@@_if_single_token_return:w #1 #1 }
1231 \cs_new:Npn \@@_if_single_token_return:w \s_@@ #1#2#3 #4
1232 {
1233 \tl_if_empty:nTF { #1 #3 }
1234 {
1235 \tl_if_single_token:nTF {#2}
1236 { \prg_return_true: }
1237 { \prg_return_false: }
1238 }
1239 {
1240 \gtl_if_eq:NNTF #4 \c_group_begin_gtl
1241 { \prg_return_true: }
1242 {
1243 \gtl_if_eq:NNTF #4 \c_group_end_gtl
1244 { \prg_return_true: }
1245 { \prg_return_false: }
1246 }
1247 }
1248 }
1249 % \end{macrocode}
1250 % \end{macro}
1251 % \end{macro}
1252 %
1253 % \begin{macro}[EXP,pTF]{\gtl_if_blank:N}
1254 % \begin{macro}[EXP]{\@@_if_blank_return:w}
1255 % A gtl is blank if its middle chunk is blank and it has no leading
1256 % nor trailing chunk (those would lead to |#1| or |#3| containing
1257 % brace groups).
1258 % \begin{macrocode}
1259 \prg_new_conditional:Npnn \gtl_if_blank:N #1 { p , T , F , TF }
1260 { \exp_after:wN \@@_if_blank_return:w #1 }
1261 \cs_new:Npn \@@_if_blank_return:w \s_@@ #1#2#3
1262 {
1263 \tl_if_blank:nTF { #1 #2 #3 }
1264 { \prg_return_true: }
1265 { \prg_return_false: }
1266 }
1267 % \end{macrocode}
1268 % \end{macro}
1269 % \end{macro}
1270 %
1271 % \begin{macro}[EXP,pTF]
1272 % {
1273 % \gtl_if_head_is_group_begin:N,
1274 % \gtl_if_head_is_group_end:N,
1275 % \gtl_if_head_is_space:N,
1276 % \gtl_if_head_is_N_type:N,
1277 % }
1278 % Based on a five-way test \cs{@@_head:wnnnnn} defined later. The
1279 % five cases are: the gtl is empty, it starts with a begin-group,
1280 % with an end-group, with a space, or with an N-type token. In the
1281 % last case, the token is left in the input stream after the brace
1282 % group, hence the need for \cs{use_none:n} here.
1283 % \begin{macrocode}
1284 \prg_new_conditional:Npnn \gtl_if_head_is_group_begin:N #1
1285 { p , T , F , TF }
1286 {
1287 \exp_after:wN \@@_head:wnnnnn #1
1288 { \prg_return_false: }
1289 { \prg_return_true: }
1290 { \prg_return_false: }
1291 { \prg_return_false: }
1292 { \prg_return_false: \use_none:n }
1293 }
1294 \prg_new_conditional:Npnn \gtl_if_head_is_group_end:N #1
1295 { p , T , F , TF }
1296 {
1297 \exp_after:wN \@@_head:wnnnnn #1
1298 { \prg_return_false: }
1299 { \prg_return_false: }
1300 { \prg_return_true: }
1301 { \prg_return_false: }
1302 { \prg_return_false: \use_none:n }
1303 }
1304 \prg_new_conditional:Npnn \gtl_if_head_is_space:N #1
1305 { p , T , F , TF }
1306 {
1307 \exp_after:wN \@@_head:wnnnnn #1
1308 { \prg_return_false: }
1309 { \prg_return_false: }
1310 { \prg_return_false: }
1311 { \prg_return_true: }
1312 { \prg_return_false: \use_none:n }
1313 }
1314 \prg_new_conditional:Npnn \gtl_if_head_is_N_type:N #1
1315 { p , T , F , TF }
1316 {
1317 \exp_after:wN \@@_head:wnnnnn #1
1318 { \prg_return_false: }
1319 { \prg_return_false: }
1320 { \prg_return_false: }
1321 { \prg_return_false: }
1322 { \prg_return_true: \use_none:n }
1323 }
1324 % \end{macrocode}
1325 % \end{macro}
1326 %
1327 % \begin{macro}[EXP, pTF]{\gtl_if_head_eq_catcode:NN}
1328 % \begin{macro}[EXP, pTF]{\gtl_if_head_eq_charcode:NN}
1329 % \begin{macro}[EXP]{\@@_if_head_eq_code_return:NNN}
1330 % In the empty case, |?| can match with |#2|, but then
1331 % \cs{use_none:nn} gets rid of \cs{prg_return_true:} and \cs{else:},
1332 % to correctly leave \cs{prg_return_false:}. We could not simplify
1333 % this by placing the \cs{exp_not:N} |#2| after the construction
1334 % involving~|#1|, because |#2|~must be taken into the \TeX{} primitive
1335 % test, in case |#2|~itself is a primitive \TeX{} conditional, which
1336 % would mess up conditional nesting.
1337 % \begin{macrocode}
1338 \prg_new_conditional:Npnn \gtl_if_head_eq_catcode:NN #1#2
1339 { p , T , F , TF }
1340 { \@@_if_head_eq_code_return:NNN \if_catcode:w #1#2 }
1341 \prg_new_conditional:Npnn \gtl_if_head_eq_charcode:NN #1#2
1342 { p , T , F , TF }
1343 { \@@_if_head_eq_code_return:NNN \if_charcode:w #1#2 }
1344 \cs_new:Npn \@@_if_head_eq_code_return:NNN #1#2#3
1345 {
1346 #1
1347 \exp_not:N #3
1348 \exp_after:wN \@@_head:wnnnnn #2
1349 { ? \use_none:nn }
1350 { \c_group_begin_token }
1351 { \c_group_end_token }
1352 { \c_space_token }
1353 { \exp_not:N }
1354 \prg_return_true:
1355 \else:
1356 \prg_return_false:
1357 \fi:
1358 }
1359 % \end{macrocode}
1360 % \end{macro}
1361 % \end{macro}
1362 % \end{macro}
1363 %
1364 % \begin{macro}[EXP, pTF]{\gtl_if_head_eq_meaning:NN}
1365 % \begin{macro}[EXP]{\@@_if_head_eq_meaning_return:NN}
1366 % \begin{macrocode}
1367 \prg_new_conditional:Npnn \gtl_if_head_eq_meaning:NN #1#2
1368 { p , T , F , TF }
1369 { \@@_if_head_eq_meaning_return:NN #1#2 }
1370 \cs_new:Npn \@@_if_head_eq_meaning_return:NN #1#2
1371 {
1372 \exp_after:wN \@@_head:wnnnnn #1
1373 { \if_false: }
1374 { \if_meaning:w #2 \c_group_begin_token }
1375 { \if_meaning:w #2 \c_group_end_token }
1376 { \if_meaning:w #2 \c_space_token }
1377 { \if_meaning:w #2 }
1378 \prg_return_true:
1379 \else:
1380 \prg_return_false:
1381 \fi:
1382 }
1383 % \end{macrocode}
1384 % \end{macro}
1385 % \end{macro}
1386 %
1387 % \subsection{First token of an extended token list}
1388 %
1389 % \begin{macro}[EXP]{\@@_head:wnnnnn}
1390 % \begin{macro}[EXP]{\@@_head_aux:nwnnnn}
1391 % \begin{macro}[EXP]{\@@_head_auxii:N, \@@_head_auxiii:Nnn}
1392 % This function performs |#4|~if the \texttt{gtl} is empty, |#5|~if it
1393 % starts with a begin-group character, |#6|~if it starts with an
1394 % end-group character, |#7|~if it starts with a space, and in other
1395 % cases (when the first token is |N|-type), it performs |#8|~followed
1396 % by the first token.
1397 % \begin{macrocode}
1398 \cs_new:Npn \@@_head:wnnnnn \s_@@ #1#2#3 #4#5#6#7#8
1399 {
1400 \tl_if_empty:nTF {#1}
1401 {
1402 \tl_if_empty:nTF {#2}
1403 { \tl_if_empty:nTF {#3} {#4} {#5} }
1404 { \@@_head_aux:nwnnnn {#2} \q_stop {#5} {#6} {#7} {#8} }
1405 }
1406 { \@@_head_aux:nwnnnn #1 \q_stop {#5} {#6} {#7} {#8} }
1407 }
1408 \cs_new:Npn \@@_head_aux:nwnnnn #1#2 \q_stop #3#4#5#6
1409 {
1410 \tl_if_head_is_group:nTF {#1} {#3}
1411 {
1412 \tl_if_empty:nTF {#1} {#4}
1413 {
1414 \tl_if_head_is_space:nTF {#1} {#5}
1415 { \if_false: { \fi: \@@_head_auxii:N #1 } {#6} }
1416 }
1417 }
1418 }
1419 \cs_new:Npn \@@_head_auxii:N #1
1420 {
1421 \exp_after:wN \@@_head_auxiii:Nnn
1422 \exp_after:wN #1
1423 \exp_after:wN { \if_false: } \fi:
1424 }
1425 \cs_new:Npn \@@_head_auxiii:Nnn #1#2#3 { #3 #1 }
1426 % \end{macrocode}
1427 % \end{macro}
1428 % \end{macro}
1429 % \end{macro}
1430 %
1431 % \begin{macro}[EXP]{\gtl_head:N}
1432 % If |#1| is empty, do nothing. If it starts with a begin-group
1433 % character or an end-group character leave the appropriate brace
1434 % (thanks to \cs{if_false:} tricks). If it starts with a space, leave
1435 % that, and finally if it starts with a normal token, leave it, within
1436 % \cs{exp_not:n}.
1437 % \begin{macrocode}
1438 \cs_new:Npn \gtl_head:N #1
1439 {
1440 \exp_after:wN \@@_head:wnnnnn #1
1441 { }
1442 { \exp_after:wN { \if_false: } \fi: }
1443 { \if_false: { \fi: } }
1444 { ~ }
1445 { \@@_exp_not_n:N }
1446 }
1447 % \end{macrocode}
1448 % \end{macro}
1449 %
1450 % \begin{macro}[EXP]{\gtl_head_do:NN}
1451 % Similar to \cs{gtl_head:N}, but inserting |#2| before the resulting token.
1452 % \begin{macrocode}
1453 \cs_new:Npn \gtl_head_do:NN #1#2
1454 {
1455 \exp_after:wN \@@_head:wnnnnn #1
1456 { #2 \q_no_value }
1457 { \exp_after:wN #2 \exp_after:wN { \if_false: } \fi: }
1458 { \if_false: { \fi: #2 } }
1459 { #2 ~ }
1460 { #2 }
1461 }
1462 % \end{macrocode}
1463 % \end{macro}
1464 %
1465 % \begin{macro}[EXP, TF]{\gtl_head_do:NN}
1466 % Test for emptyness then use \cs{gtl_head_do:NN}, placing the
1467 % \meta{true code} or \meta{false code} as appropriate.
1468 % \begin{macrocode}
1469 \cs_new:Npn \gtl_head_do:NNT #1#2#3
1470 {
1471 \gtl_if_empty:NTF #1
1472 { }
1473 { \gtl_head_do:NN #1 #2 #3 }
1474 }
1475 \cs_new:Npn \gtl_head_do:NNF #1#2#3
1476 {
1477 \gtl_if_empty:NTF #1
1478 {#3}
1479 { \gtl_head_do:NN #1 #2 }
1480 }
1481 \cs_new:Npn \gtl_head_do:NNTF #1#2#3#4
1482 {
1483 \gtl_if_empty:NTF #1
1484 {#4}
1485 { \gtl_head_do:NN #1 #2 #3 }
1486 }
1487 % \end{macrocode}
1488 % \end{macro}
1489 %
1490 % \begin{macro}{\gtl_get_left:NN}
1491 % \begin{macrocode}
1492 \cs_new_protected:Npn \gtl_get_left:NN #1#2
1493 {
1494 \exp_after:wN \@@_head:wnnnnn #1
1495 { \gtl_set:Nn #2 { \q_no_value } }
1496 { \gtl_set_eq:NN #2 \c_group_begin_gtl }
1497 { \gtl_set_eq:NN #2 \c_group_end_gtl }
1498 { \gtl_set:Nn #2 { ~ } }
1499 { \gtl_set:Nn #2 }
1500 }
1501 % \end{macrocode}
1502 % \end{macro}
1503 %
1504 % \begin{macro}{\gtl_pop_left:N, \gtl_gpop_left:N}
1505 % \begin{macro}[rEXP]
1506 % {
1507 % \@@_pop_left:w,
1508 % \@@_pop_left_auxi:n,
1509 % \@@_pop_left_auxii:nnnw,
1510 % \@@_pop_left_auxiii:nnnw,
1511 % \@@_pop_left_auxiv:nn,
1512 % \@@_pop_left_auxv:nnn,
1513 % \@@_pop_left_auxvi:n
1514 % }
1515 % \begin{macrocode}
1516 \cs_new_protected:Npn \gtl_pop_left:N #1
1517 {
1518 \gtl_if_empty:NF #1
1519 { \tl_set:Ne #1 { \exp_after:wN \@@_pop_left:w #1 } }
1520 }
1521 \cs_new_protected:Npn \gtl_gpop_left:N #1
1522 {
1523 \gtl_if_empty:NF #1
1524 { \tl_gset:Ne #1 { \exp_after:wN \@@_pop_left:w #1 } }
1525 }
1526 \cs_new:Npn \@@_pop_left:w \s_@@ #1#2#3
1527 {
1528 \tl_if_empty:nTF {#1}
1529 {
1530 \tl_if_empty:nTF {#2}
1531 { \@@_pop_left_auxi:n {#3} }
1532 { \@@_pop_left_auxiv:nn {#2} {#3} }
1533 }
1534 { \@@_pop_left_auxv:nnn {#1} {#2} {#3} }
1535 }
1536 \cs_new:Npn \@@_pop_left_auxi:n #1
1537 {
1538 \s_@@
1539 { }
1540 \@@_pop_left_auxii:nnnw { } { } #1 \q_nil \q_stop
1541 }
1542 \cs_new:Npn \@@_pop_left_auxii:nnnw #1#2#3
1543 {
1544 \quark_if_nil:nTF {#3}
1545 { \@@_pop_left_auxiii:nnnw {#1} {#2} {#3} }
1546 { \@@_pop_left_auxii:nnnw { #1 #2 } { {#3} } }
1547 }
1548 \cs_new:Npn \@@_pop_left_auxiii:nnnw #1#2#3#4 \q_stop
1549 {
1550 \tl_if_empty:nTF {#4}
1551 { \exp_not:n { #2 {#1} } }
1552 { \@@_pop_left_auxii:nnnw { #1 #2 } { {#3} } }
1553 }
1554 \cs_new:Npn \@@_pop_left_auxiv:nn #1#2
1555 {
1556 \s_@@
1557 { \tl_if_head_is_group:nT {#1} { { \tl_head:n {#1} } } }
1558 { \tl_if_head_is_space:nTF {#1} { \exp_not:f } { \tl_tail:n } {#1} }
1559 { \exp_not:n {#2} }
1560 }
1561 \cs_new:Npn \@@_pop_left_auxv:nnn #1#2#3
1562 {
1563 \s_@@
1564 { \if_false: { \fi: \@@_pop_left_auxvi:n #1 } }
1565 { \exp_not:n {#2} }
1566 { \exp_not:n {#3} }
1567 }
1568 \cs_new:Npn \@@_pop_left_auxvi:n #1
1569 {
1570 \tl_if_empty:nF {#1}
1571 {
1572 \tl_if_head_is_group:nT {#1} { { \tl_head:n {#1} } }
1573 {
1574 \tl_if_head_is_space:nTF {#1}
1575 { \exp_not:f } { \tl_tail:n } {#1}
1576 }
1577 }
1578 \exp_after:wN \exp_not:n \exp_after:wN { \if_false: } \fi:
1579 }
1580 % \end{macrocode}
1581 % \end{macro}
1582 % \end{macro}
1583 %
1584 % \begin{macro}{\gtl_pop_left:NN, \gtl_gpop_left:NN}
1585 % Getting the first token and removing it from the extended token list
1586 % is done in two steps.
1587 % \begin{macrocode}
1588 \cs_new_protected:Npn \gtl_pop_left:NN #1#2
1589 {
1590 \gtl_get_left:NN #1 #2
1591 \gtl_pop_left:N #1
1592 }
1593 \cs_new_protected:Npn \gtl_gpop_left:NN #1#2
1594 {
1595 \gtl_get_left:NN #1 #2
1596 \gtl_gpop_left:N #1
1597 }
1598 % \end{macrocode}
1599 % \end{macro}
1600 %
1601 % \subsection{Longest token list starting an extended token list}
1602 %
1603 % \begin{macro}[EXP]{\gtl_left_tl:N}
1604 % \begin{macro}[EXP]{\@@_left_tl:w}
1605 % If there is no leading chunk, return the middle chunk, otherwise
1606 % the first leading chunk.
1607 % \begin{macrocode}
1608 \cs_new:Npn \gtl_left_tl:N #1
1609 { \exp_after:wN \@@_left_tl:w #1 }
1610 \cs_new:Npn \@@_left_tl:w \s_@@ #1#2#3
1611 { \tl_if_empty:nTF {#1} { \exp_not:n {#2} } { \tl_head:n {#1} } }
1612 % \end{macrocode}
1613 % \end{macro}
1614 % \end{macro}
1615 %
1616 % \begin{macro}{\gtl_pop_left_tl:N, \gtl_gpop_left_tl:N}
1617 % If there is no left chunk, remove the middle chunk, hence the
1618 % resulting gtl will start with two empty brace groups (one for the
1619 % absence of leading chunk, and one for the emptyness of the middle
1620 % chunk). If there are left chunks replace the first one by an empty
1621 % chunk.
1622 % \begin{macrocode}
1623 \cs_new_protected:Npn \gtl_pop_left_tl:N #1
1624 { \tl_set:Ne #1 { \exp_after:wN \@@_pop_left_tl:w #1 } }
1625 \cs_new_protected:Npn \gtl_gpop_left_tl:N #1
1626 { \tl_gset:Ne #1 { \exp_after:wN \@@_pop_left_tl:w #1 } }
1627 \cs_new:Npn \@@_pop_left_tl:w \s_@@ #1#2#3
1628 {
1629 \s_@@
1630 \tl_if_empty:nTF {#1}
1631 { { } { } }
1632 {
1633 { { } \tl_tail:n {#1} }
1634 { \exp_not:n {#2} }
1635 }
1636 { \exp_not:n {#3} }
1637 }
1638 % \end{macrocode}
1639 % \end{macro}
1640 %
1641 % \subsection{First item of an extended token list}
1642 %
1643 % \begin{macro}[EXP]{\gtl_left_item:NF}
1644 % \begin{macro}[EXP]{\@@_left_item:wF, \@@_left_item_auxi:nwF}
1645 % The left-most item of an extended token list is the head of its left
1646 % token list. The code thus starts like \cs{gtl_left_tl:N}. It ends
1647 % with a check to test if we should use the head, or issue the false
1648 % code.
1649 % \begin{macrocode}
1650 \cs_new:Npn \gtl_left_item:NF #1
1651 { \exp_after:wN \@@_left_item:wF #1 }
1652 \cs_new:Npn \@@_left_item:wF \s_@@ #1#2#3
1653 { \@@_left_item_auxi:nwF #1 {#2} \q_stop }
1654 \cs_new:Npn \@@_left_item_auxi:nwF #1#2 \q_stop #3
1655 { \tl_if_blank:nTF {#1} {#3} { \tl_head:n {#1} } }
1656 % \end{macrocode}
1657 % \end{macro}
1658 % \end{macro}
1659 %
1660 % \begin{macro}[TF]{\gtl_pop_left_item:NN, \gtl_gpop_left_item:NN}
1661 % \begin{macro}
1662 % {\@@_pop_left_item:wNNN, \@@_pop_left_item_aux:nwnnNNN}
1663 % If there is no extra end-group characters, and if the balanced part
1664 % is blank, we cannot extract an item: return \texttt{false}. If the
1665 % balanced part is not blank, store its first item into |#4|, and
1666 % store the altered generalized token list into~|#6|, locally or
1667 % globally. Otherwise, pick out the part before the first extra
1668 % end-group character as |#1| of the second auxiliary, and do
1669 % essentially the same: if it is blank, there is no item, and if it is
1670 % not blank, pop its first item.
1671 % \begin{macrocode}
1672 \prg_new_protected_conditional:Npnn \gtl_pop_left_item:NN #1#2 { TF , T , F }
1673 { \exp_after:wN \@@_pop_left_item:wNNN #1#2 \tl_set:Ne #1 }
1674 \prg_new_protected_conditional:Npnn \gtl_gpop_left_item:NN #1#2 { TF , T , F }
1675 { \exp_after:wN \@@_pop_left_item:wNNN #1#2 \tl_gset:Ne #1 }
1676 \cs_new_protected:Npn \@@_pop_left_item:wNNN
1677 \s_@@ #1#2#3 #4#5#6
1678 {
1679 \tl_if_empty:nTF {#1}
1680 {
1681 \tl_if_blank:nTF {#2} { \prg_return_false: }
1682 {
1683 \tl_set:Ne #4 { \tl_head:n {#2} }
1684 #5 #6
1685 {
1686 \s_@@ { } { \tl_tail:n {#2} }
1687 { \exp_not:n {#3} }
1688 }
1689 \prg_return_true:
1690 }
1691 }
1692 {
1693 \@@_pop_left_item_aux:nwnnNNN #1 \q_nil \q_stop
1694 {#2} {#3} #4 #5 #6
1695 }
1696 }
1697 \cs_new_protected:Npn \@@_pop_left_item_aux:nwnnNNN
1698 #1#2 \q_stop #3#4#5#6#7
1699 {
1700 \tl_if_blank:nTF {#1} { \prg_return_false: }
1701 {
1702 \tl_set:Ne #5 { \tl_head:n {#1} }
1703 #6 #7
1704 {
1705 \s_@@
1706 { { \tl_tail:n {#1} } \@@_strip_nil_mark:w #2 \q_mark }
1707 { \exp_not:n {#3} }
1708 { \exp_not:n {#4} }
1709 }
1710 \prg_return_true:
1711 }
1712 }
1713 % \end{macrocode}
1714 % \end{macro}
1715 % \end{macro}
1716 %
1717 % \subsection{First group in an extended token list}
1718 %
1719 % The functions of this section extract from an extended token list the
1720 % tokens that would be absorbed after |\def\foo|, namely tokens with no
1721 % begin-group nor end-group characters, followed by one group. Those
1722 % tokens are either left in the input stream or stored in a token list
1723 % variable, and the |pop| functions also remove those tokens from the
1724 % extended token list variable.
1725 %
1726 % \begin{macro}[EXP]{\gtl_left_text:NF}
1727 % \begin{macro}[EXP]
1728 % {
1729 % \@@_left_text:wF,
1730 % \@@_left_text_auxi:nwF,
1731 % \@@_left_text_auxii:wnwF,
1732 % \@@_left_text_auxiii:nnwF,
1733 % }
1734 % \begin{macrocode}
1735 \cs_new:Npn \gtl_left_text:NF #1
1736 { \exp_after:wN \@@_left_text:wF #1 }
1737 \cs_new:Npn \@@_left_text:wF \s_@@ #1#2#3
1738 {
1739 \tl_if_empty:nTF {#1}
1740 { \@@_left_text_auxi:nwF {#2} \q_stop }
1741 { \@@_left_text_auxi:nwF #1 \q_stop }
1742 }
1743 \cs_new:Npn \@@_left_text_auxi:nwF #1#2 \q_stop
1744 { \@@_left_text_auxii:wnwF #1 \q_mark { } \q_mark \q_stop }
1745 \cs_new:Npn \@@_left_text_auxii:wnwF #1 #
1746 { \@@_left_text_auxiii:nnwF {#1} }
1747 \cs_new:Npn \@@_left_text_auxiii:nnwF #1#2 #3 \q_mark #4 \q_stop #5
1748 { \tl_if_empty:nTF {#4} {#5} { \exp_not:n { #1 {#2} } } }
1749 % \end{macrocode}
1750 % \end{macro}
1751 % \end{macro}
1752 %
1753 % \begin{macro}{\gtl_pop_left_text:N, \gtl_gpop_left_text:N}
1754 % \begin{macro}[rEXP]
1755 % {
1756 % \@@_pop_left_text:w,
1757 % \@@_pop_left_text_auxi:n,
1758 % \@@_pop_left_text_auxii:wnw,
1759 % \@@_pop_left_text_auxiii:nnw,
1760 % \@@_pop_left_text_auxiv:nw,
1761 % }
1762 % \begin{macrocode}
1763 \cs_new_protected:Npn \gtl_pop_left_text:N #1
1764 { \tl_set:Ne #1 { \exp_after:wN \@@_pop_left_text:w #1 } }
1765 \cs_new_protected:Npn \gtl_gpop_left_text:N #1
1766 { \tl_gset:Ne #1 { \exp_after:wN \@@_pop_left_text:w #1 } }
1767 \cs_new:Npn \@@_pop_left_text:w \s_@@ #1#2#3
1768 {
1769 \s_@@
1770 \tl_if_empty:nTF {#1}
1771 {
1772 { }
1773 { \@@_pop_left_text_auxi:n {#2} }
1774 }
1775 {
1776 { \@@_pop_left_text_auxiv:nw #1 \q_nil \q_mark }
1777 { \exp_not:n {#2} }
1778 }
1779 { \exp_not:n {#3} }
1780 }
1781 \cs_new:Npn \@@_pop_left_text_auxi:n #1
1782 {
1783 \@@_pop_left_text_auxii:wnw #1
1784 \q_nil \q_mark { } \q_mark \q_stop
1785 }
1786 \cs_new:Npn \@@_pop_left_text_auxii:wnw #1 #
1787 { \@@_pop_left_text_auxiii:nnw {#1} }
1788 \cs_new:Npn \@@_pop_left_text_auxiii:nnw #1#2#3 \q_mark #4 \q_stop
1789 {
1790 \tl_if_empty:nTF {#4}
1791 { \@@_strip_nil_mark:w #1 }
1792 { \@@_strip_nil_mark:w #3 \q_mark }
1793 }
1794 \cs_new:Npn \@@_pop_left_text_auxiv:nw #1
1795 {
1796 { \@@_pop_left_text_auxi:n {#1} }
1797 \@@_strip_nil_mark:w
1798 }
1799 % \end{macrocode}
1800 % \end{macro}
1801 % \end{macro}
1802 %
1803 % \subsection{Counting tokens}
1804 %
1805 % \begin{macro}[EXP]{\@@_tl_count:n}
1806 % \begin{macro}[EXP]{\@@_tl_count_loop:n, \@@_tl_count_test:w}
1807 % A more robust version of \cs{tl_count:n}, which will however break
1808 % if the token list contains \cs{q_stop} at the outer brace level.
1809 % This cannot happen when \cs{@@_tl_count:n} is called with lists of
1810 % braced items. The technique is to loop, and when seeing
1811 % \cs{q_mark}, make sure that this is really the end of the list.
1812 % \begin{macrocode}
1813 \cs_new:Npn \@@_tl_count:n #1
1814 { \int_eval:n { 0 \@@_tl_count_loop:n #1 \q_nil \q_stop } }
1815 \cs_new:Npn \@@_tl_count_loop:n #1
1816 {
1817 \quark_if_nil:nTF {#1}
1818 { \@@_tl_count_test:w }
1819 { + 1 \@@_tl_count_loop:n }
1820 }
1821 \cs_new:Npn \@@_tl_count_test:w #1 \q_stop
1822 { \tl_if_empty:nF {#1} { + 1 \@@_tl_count_loop:n #1 \q_stop } }
1823 % \end{macrocode}
1824 % \end{macro}
1825 % \end{macro}
1826 %
1827 % \begin{macro}[EXP]{\gtl_extra_begin:N, \gtl_extra_end:N}
1828 % \begin{macro}[EXP]{\@@_extra_begin:w, \@@_extra_end:w}
1829 % Count the number of extra end-group or of extra begin-group
1830 % characters in an extended token list. This is the number of items
1831 % in the first or third brace groups. We cannot use \cs{tl_count:n},
1832 % as \pkg{gtl} is meant to be robust against inclusion of quarks.
1833 % \begin{macrocode}
1834 \cs_new:Npn \gtl_extra_end:N #1
1835 { \exp_after:wN \@@_extra_end:w #1 }
1836 \cs_new:Npn \@@_extra_end:w \s_@@ #1#2#3
1837 { \@@_tl_count:n {#1} }
1838 \cs_new:Npn \gtl_extra_begin:N #1
1839 { \exp_after:wN \@@_extra_begin:w #1 }
1840 \cs_new:Npn \@@_extra_begin:w \s_@@ #1#2#3
1841 { \@@_tl_count:n {#3} }
1842 % \end{macrocode}
1843 % \end{macro}
1844 % \end{macro}
1845 %
1846 % \begin{macro}[EXP]{\gtl_count_tokens:N}
1847 % \begin{macro}[rEXP]
1848 % {
1849 % \@@_count_tokens:w,
1850 % \@@_count_auxi:nw,
1851 % \@@_count_auxii:w,
1852 % \@@_count_auxiii:n
1853 % }
1854 % \begin{macrocode}
1855 \cs_new:Npn \gtl_count_tokens:N #1
1856 { \exp_after:wN \@@_count_tokens:w #1 }
1857 \cs_new:Npn \@@_count_tokens:w \s_@@ #1#2#3
1858 {
1859 \int_eval:n
1860 { -1 \@@_count_auxi:nw #1 {#2} #3 \q_nil \q_stop }
1861 }
1862 \cs_new:Npn \@@_count_auxi:nw #1
1863 {
1864 \quark_if_nil:nTF {#1}
1865 { \@@_count_auxii:w }
1866 {
1867 + 1
1868 \@@_count_auxiii:n {#1}
1869 \@@_count_auxi:nw
1870 }
1871 }
1872 \cs_new:Npn \@@_count_auxii:w #1 \q_stop
1873 {
1874 \tl_if_empty:nF {#1}
1875 {
1876 + 2
1877 \@@_count_auxi:nw #1 \q_stop
1878 }
1879 }
1880 \cs_new:Npn \@@_count_auxiii:n #1
1881 {
1882 \tl_if_empty:nF {#1}
1883 {
1884 \tl_if_head_is_group:nTF {#1}
1885 {
1886 + 2
1887 \exp_args:No \@@_count_auxiii:n { \use:n #1 }
1888 }
1889 {
1890 + 1
1891 \tl_if_head_is_N_type:nTF {#1}
1892 { \exp_args:No \@@_count_auxiii:n { \use_none:n #1 } }
1893 { \exp_args:Nf \@@_count_auxiii:n {#1} }
1894 }
1895 }
1896 }
1897 % \end{macrocode}
1898 % \end{macro}
1899 % \end{macro}
1900 %
1901 % \begin{macrocode}
1902 \@@_end_package_hook:
1903 % \end{macrocode}
1904 %
1905 % \begin{macrocode}
1906 %</package>
1907 % \end{macrocode}
1908 %
1909 % \end{implementation}
1910 %
1911 % \endinput