-
Notifications
You must be signed in to change notification settings - Fork 2
/
gtl.dtx
1911 lines (1911 loc) · 63.1 KB
/
gtl.dtx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% \iffalse
%% File: gtl.dtx Copyright (C) 2013,2015,2017,2018,2024 Bruno Le Floch
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%%
%% This work has the LPPL maintenance status 'maintained'
%% and the current maintainer is Bruno Le Floch.
%%
%% This work consists of the files gtl.dtx and gtl.ins and
%% derived file gtl.sty.
%% -----------------------------------------------------------------------
%
%<*driver>
%\fi
%\iffalse
\documentclass[full]{l3doc}
\usepackage{gtl}
\begin{document}
\DocInput{gtl.dtx}
\end{document}
%</driver>
% \fi
%
% \title{The \textsf{gtl} package: \\
% manipulate unbalanced lists of tokens\thanks{This
% file has version number 0.6, last revised 2024/01/04.}}
% \author{Bruno Le Floch}
% \date{2024/01/04}
%
% \maketitle
% \tableofcontents
%
% \begin{documentation}
%
% \section{\pkg{gtl} documentation}
%
% The \texttt{expl3} programming language provides various tools to
% manipulate lists of tokens (package \pkg{l3tl}). However, those token
% lists must have balanced braces, or more precisely balanced
% begin-group and end-group characters. The \pkg{gtl} package
% manipulates instead lists of tokens which may be unbalanced, with more
% begin-group or more end-group characters.
%
% A technical comment: currently, all begin-group characters are
% assumed to have the character code of ``|{|'' and all end-group
% characters that of~``|}|''.
%
% Please report bugs (or suggestions) on the issue tracker
% (\url{https://github.com/blefloch/latex-gtl/issues}).
%
% ^^A The following code checks the test coverage
% \begingroup\ExplSyntaxOn
% \msg_new:nnn { gtldoc } { discarded } { Discarded~some~material~after~closing~brace~of~{function}~or~TEST~or~TESTEXP~argument:~``#1'' }
% \msg_new:nnn { gtldoc } { bad-test } { TEST~or~TESTEXP~followed~by~unexpected~character~``#1'' }
% \msg_new:nnn { gtldoc } { multi-function } { Some~functions~are~defined~twice }
% \msg_new:nnn { gtldoc } { doc-no-test } { ``#1''~is~documented~but~has~no~test }
% \msg_new:nnn { gtldoc } { test-no-doc } { ``#1''~is~tested~but~not~documented }
% \msg_new:nnn { gtldoc } { junk } { There~is~some~unexpected~junk.~Giving~up~on~this~line. }
% \tl_new:N \l__gtldoc_arg_tl
% \seq_new:N \l__gtldoc_internal_seq
% \cs_generate_variant:Nn \use:nn { no }
% \cs_generate_variant:Nn \tl_if_in:nnTF { no }
% \cs_generate_variant:Nn \tl_if_in:nnT { no }
% \cs_generate_variant:Nn \tl_remove_all:Nn { Nx }
% \cs_generate_variant:Nn \seq_set_filter:NNn { NNx }
% \group_begin:
% \seq_new:N \l__gtldoc_fn_seq
% \iow_new:N \g__gtldoc_ior
% \tl_new:N \l__gtldoc_internal_tl
% \str_new:N \l__gtldoc_TF_str
% \seq_new:N \l__gtldoc_arg_seq
% \seq_new:N \l__gtldoc_arg_TF_seq
% \cs_new:Npn \__gtldoc_remove_backslash:N #1
% { \exp_after:wN \token_if_eq_charcode:NNF \c_backslash_str #1 {#1} }
% \cs_new_protected:Npn \gtldoc_get_fn:n #1
% {
% \seq_clear:N \l__gtldoc_fn_seq
% \clist_map_inline:nn {#1}
% {
% \__gtldoc_get_fn:nn { function } {#1} ^^A todo: this is wasteful, going through the file twice!
% \__gtldoc_get_fn:nn { variable } {#1}
% }
% }
% \cs_new_protected:Npn \__gtldoc_get_fn:nn #1
% {
% \exp_args:Noo \__gtldoc_get_fn_aux:nnn
% { \tl_to_str:n { begin } } { \tl_to_str:n { {#1} } }
% }
% \cs_new_eq:NN \__gtldoc_test:w ?
% \cs_new_eq:NN \__gtldoc_skip:w ?
% \cs_new_protected:Npn \__gtldoc_get_fn_aux:nnn #1#2#3
% {
% \cs_gset_protected:Npn \__gtldoc_test:w ##1 #1 ##2 #2 { }
% \cs_gset_protected:Npn \__gtldoc_skip:w ##1 #1 ##2 #2
% {
% \tl_clear:N \l__gtldoc_arg_tl
% \str_clear:N \l__gtldoc_TF_str
% \tl_if_blank:nF {##2}
% {
% \msg_error:nn { gtldoc } { junk }
% \use_none_delimit_by_q_recursion_stop:w
% }
% \__gtldoc_aux:N
% }
% \ior_open:Nn \g__gtldoc_ior {#3}
% \ior_str_map_inline:Nn \g__gtldoc_ior
% {
% \tl_if_in:noT {##1} { \tl_to_str:n { end { documentation } } } ^^A careful if you remove spaces!
% { \ior_map_break: }
% \tl_if_empty:oF { \__gtldoc_test:w ##1 #1 #2 }
% { \__gtldoc_skip:w ##1 \q_recursion_tail \q_recursion_stop }
% }
% \ior_close:N \g__gtldoc_ior
% }
% \cs_new_protected:Npn \__gtldoc_aux:N #1
% {
% \token_if_eq_meaning:NNT #1 \q_recursion_tail
% { \__gtldoc_more:Nw \__gtldoc_aux:N }
% \str_case_e:nnF {#1}
% {
% \c_percent_str { \__gtldoc_aux:N } ^^A ignore percent
% ^ { \__gtldoc_hat:N } ^^A check for "^^A"
% [ { \__gtldoc_opt:w }
% \c_left_brace_str { \__gtldoc_arg:w }
% }
% { \__gtldoc_varg:N #1 }
% }
% \cs_new_protected:Npn \__gtldoc_more:Nw #1 #2 \q_recursion_stop
% { ^^A grab one more line and restart the loop
% \ior_str_get:NN \g__gtldoc_ior \l__gtldoc_internal_tl
% \exp_after:wN #1 \l__gtldoc_internal_tl
% \q_recursion_tail \q_recursion_stop
% }
% \cs_new_protected:Npn \__gtldoc_hat:N #1
% {
% \token_if_eq_charcode:NNTF #1 ^
% { \__gtldoc_hat_hat:N }
% { \__gtldoc_varg:N ^ #1 }
% }
% \cs_new_protected:Npn \__gtldoc_hat_hat:N #1 ^^A in principle we should worry about crazy people doing ^^01 or ^^5b etc
% {
% \token_if_eq_charcode:NNTF #1 A
% { \__gtldoc_more:Nw \__gtldoc_aux:N } ^^A we found a comment
% { \__gtldoc_varg:N ^ ^ #1 }
% }
% \cs_new_protected:Npn \__gtldoc_opt:w #1 \q_recursion_tail
% {
% \tl_if_in:nnTF {#1} { ] }
% { \__gtldoc_opt_aux:w #1 \q_recursion_tail }
% { \__gtldoc_more:Nw \__gtldoc_opt:w }
% }
% \cs_new_protected:Npn \__gtldoc_opt_aux:w #1 ]
% {
% \tl_if_in:noTF {#1} { \tl_to_str:n { pTF } }
% { \str_set:Nn \l__gtldoc_TF_str { pTF } }
% {
% \tl_if_in:noT {#1} { \tl_to_str:n { TF } }
% { \str_set:Nn \l__gtldoc_TF_str { TF } }
% }
% \__gtldoc_aux:N
% }
% \cs_new_protected:Npn \__gtldoc_arg:w
% { \exp_after:wN \__gtldoc_varg:N \c_right_brace_str }
% \cs_new_protected:Npn \__gtldoc_varg:N #1
% { \exp_args:No \__gtldoc_varg_aux:n { \token_to_str:N #1 } }
% \cs_new_protected:Npn \__gtldoc_varg_aux:n #1
% {
% \cs_gset_protected:Npn \__gtldoc_varg_aux:w ##1 #1 { {##1} }
% \cs_gset_protected:Npn \__gtldoc_varg_loop:w ##1 \q_recursion_tail
% {
% \tl_if_in:noTF {##1} {#1}
% {
% \exp_after:wN \__gtldoc_varg_last:nw
% \__gtldoc_varg_aux:w ##1 \q_recursion_tail
% }
% {
% \tl_put_right:Ne \l__gtldoc_arg_tl { ##1 \iow_char:N \^^M }
% \__gtldoc_more:Nw \__gtldoc_varg_loop:w
% }
% }
% \__gtldoc_varg_loop:w
% }
% \cs_new_eq:NN \__gtldoc_varg_aux:w ?
% \cs_new_eq:NN \__gtldoc_varg_loop:w ?
% \cs_new_protected:Npn \__gtldoc_varg_last:nw
% #1#2 \q_recursion_tail \q_recursion_stop
% {
% \tl_if_blank:nF {#2}
% { \msg_warning:nnn { gtldoc } { discarded } {#2} }
% \tl_put_right:Nn \l__gtldoc_arg_tl {#1}
% \tl_remove_all:Ne \l__gtldoc_arg_tl { \iow_char:N \^^M \iow_char:N \% } ^^A from l3doc
% \tl_remove_all:Ne \l__gtldoc_arg_tl { \tl_to_str:n { ^ ^ A } } ^^A from l3doc
% \tl_remove_all:Ne \l__gtldoc_arg_tl { \iow_char:N \^^I } ^^A from l3doc
% \tl_remove_all:Ne \l__gtldoc_arg_tl { \iow_char:N \^^M } ^^A from l3doc
% ^^A \@@_replace_at_at:N \l_@@_tmpa_tl ^^A from l3doc but not done here
% \exp_args:NNe \seq_set_from_clist:Nn \l__gtldoc_arg_seq
% { \tl_to_str:N \l__gtldoc_arg_tl }
% \str_if_empty:NF \l__gtldoc_TF_str
% {
% \seq_clear:N \l__gtldoc_arg_TF_seq
% \seq_map_inline:Nn \l__gtldoc_arg_seq
% {
% \seq_put_right:Ne \l__gtldoc_arg_TF_seq { ##1 TF }
% \seq_put_right:Ne \l__gtldoc_arg_TF_seq { ##1 T }
% \seq_put_right:Ne \l__gtldoc_arg_TF_seq { ##1 F }
% \str_if_eq:eeT { \l__gtldoc_TF_str } { pTF }
% { \seq_put_right:Ne \l__gtldoc_arg_TF_seq { \__gtldoc_pred:w ##1 } }
% }
% \seq_set_map_e:NNn \l__gtldoc_arg_seq
% \l__gtldoc_arg_TF_seq { \tl_to_str:n {##1} }
% }
% \seq_concat:NNN \l__gtldoc_fn_seq \l__gtldoc_fn_seq \l__gtldoc_arg_seq
% }
% \use:no { \cs_new:Npn \__gtldoc_pred:w #1 } { \token_to_str:N : } { #1 _p: }
% \group_end:
% \group_begin:
% \seq_new:N \l__gtldoc_tested_fn_seq
% \cs_new_protected:Npn \gtldoc_get_tests:n #1
% {
% \seq_clear:N \l__gtldoc_tested_fn_seq
% \clist_map_function:nN {#1} \__gtldoc_get_test:n
% }
% \cs_new_protected:Npn \__gtldoc_get_test:n #1
% {
% \ior_open:Nn \g__gtldoc_ior {#1}
% \ior_str_map_inline:Nn \g__gtldoc_ior
% { \__gtldoc_test_aux:n {##1} }
% \ior_close:N \g__gtldoc_ior
% }
% \cs_gset_protected:Npn \__gtldoc_tmp:nn #1#2
% {
% \cs_new_protected:Npn \__gtldoc_test_skipi:w ##1 #1 { }
% \cs_new_protected:Npn \__gtldoc_test_skipii:w ##1 #2 { }
% \cs_new_protected:Npn \__gtldoc_test_aux:n ##1
% {
% \tl_if_empty:oF { \__gtldoc_test_skipi:w ##1 #1 }
% {
% \tl_if_empty:oTF { \__gtldoc_test_skipii:w ##1 #2 }
% { \__gtldoc_test_auxi:w } { \__gtldoc_test_auxii:w }
% ##1 \q_recursion_tail \q_recursion_stop
% }
% }
% \cs_new_protected:Npn \__gtldoc_test_auxi:w ##1 #1
% { \__gtldoc_test_auxiii:N }
% \cs_new_protected:Npn \__gtldoc_test_auxii:w ##1 #2
% { \__gtldoc_test_auxiii:N }
% }
% \exp_args:Noo \__gtldoc_tmp:nn
% { \token_to_str:N \TEST }
% { \token_to_str:N \TESTEXP }
% \cs_new_protected:Npn \__gtldoc_test_auxiii:N #1
% {
% \token_if_eq_meaning:NNT \q_recursion_tail #1
% { \__gtldoc_more:Nw \__gtldoc_test_auxiii:N }
% \exp_after:wN \token_if_eq_charcode:NNTF \c_left_brace_str #1
% { \__gtldoc_test_arg:w }
% {
% \msg_error:nnn { gtldoc } { bad-test } {#1}
% \use_none_delimit_by_q_recursion_stop:w
% }
% }
% \cs_new_protected:Npn \__gtldoc_test_arg:w
% {
% \tl_clear:N \l__gtldoc_arg_tl
% \exp_args:No \__gtldoc_test_arg_aux:N \c_right_brace_str
% }
% \cs_new_protected:Npn \__gtldoc_test_arg_aux:N #1
% {
% \cs_gset_protected:Npn \__gtldoc_test_arg_aux:w ##1 #1 { {##1} }
% \cs_gset_protected:Npn \__gtldoc_test_arg_loop:w ##1 \q_recursion_tail
% {
% \tl_if_in:noTF {##1} {#1}
% {
% \exp_after:wN \__gtldoc_test_arg_last:nw
% \__gtldoc_test_arg_aux:w ##1 \q_recursion_tail
% }
% {
% \tl_put_right:Nn \l__gtldoc_arg_tl {##1}
% \__gtldoc_more:Nw \__gtldoc_test_arg_loop:w
% }
% }
% \__gtldoc_test_arg_loop:w
% }
% \cs_new_eq:NN \__gtldoc_test_arg_aux:w ?
% \cs_new_eq:NN \__gtldoc_test_arg_loop:w ?
% \cs_new_protected:Npn \__gtldoc_test_arg_last:nw
% #1#2 \q_recursion_tail \q_recursion_stop
% {
% \tl_if_blank:nF {#2}
% { \msg_warning:nnn { gtldoc } { discarded } {#2} }
% \tl_put_right:Nn \l__gtldoc_arg_tl {#1}
% \exp_args:NNe \seq_set_from_clist:Nn \l__gtldoc_arg_seq
% { \tl_to_str:N \l__gtldoc_arg_tl }
% \exp_args:NNNe \seq_set_filter:NNn \l__gtldoc_arg_seq \l__gtldoc_arg_seq
% { \exp_not:N \tl_if_head_eq_charcode_p:nN {##1} \c_backslash_str }
% \seq_concat:NNN \l__gtldoc_tested_fn_seq \l__gtldoc_tested_fn_seq \l__gtldoc_arg_seq
% }
% \group_end:
% \gtldoc_get_fn:n { gtl.dtx }
% \gtldoc_get_tests:n
% {
% ../../testfiles/gtl000.lvt ,
% ../../testfiles/gtl001.lvt ,
% ../../testfiles/gtl002.lvt ,
% ../../testfiles/gtl003.lvt ,
% ../../testfiles/gtl004.lvt ,
% ../../testfiles/gtl005.lvt ,
% ../../testfiles/gtl006.lvt ,
% ../../testfiles-plain/gtl007.lvt
% }
% \group_begin:
% \seq_set_eq:NN \l__gtldoc_internal_seq \l__gtldoc_fn_seq
% \seq_remove_duplicates:N \l__gtldoc_internal_seq
% \int_compare:nF
% { \seq_count:N \l__gtldoc_internal_seq = \seq_count:N \l__gtldoc_fn_seq }
% {
% \msg_error:nn { gtldoc } { multi-function }
% \seq_set_eq:NN \l__gtldoc_fn_seq \l__gtldoc_internal_seq
% }
% \seq_remove_duplicates:N \l__gtldoc_tested_fn_seq
% \seq_map_inline:Nn \l__gtldoc_fn_seq
% {
% \seq_if_in:NnTF \l__gtldoc_tested_fn_seq {#1}
% { \seq_remove_all:Nn \l__gtldoc_tested_fn_seq {#1} }
% { \msg_error:nnn { gtldoc } { doc-no-test } {#1} }
% }
% \seq_map_inline:Nn \l__gtldoc_tested_fn_seq
% { \msg_error:nnn { gtldoc } { test-no-doc } {#1} }
% \group_end:
% \endgroup
%
% \subsection{Creating and initialising extended token lists}
%
% \begin{function}{\gtl_new:N}
% \begin{syntax}
% \cs{gtl_new:N} \meta{gtl~var}
% \end{syntax}
% Creates a new \meta{gtl~var} or raises an error if the name is
% already taken. The declaration is global. The \meta{gtl~var} will
% initially be empty.
% \end{function}
%
% \begin{function}[updated = 2024-01-04]{\gtl_const:Nn, \gtl_const:Ne, \gtl_const:Nx}
% \begin{syntax}
% \cs{gtl_const:Nn} \meta{gtl~var} \Arg{token list}
% \end{syntax}
% Creates a new constant \meta{gtl~var} or raises an error if the name
% is already taken. The value of the \meta{gtl~var} will be set
% globally to the balanced \meta{token list}.
% \end{function}
%
% \begin{function}{\gtl_clear:N, \gtl_gclear:N}
% \begin{syntax}
% \cs{gtl_clear:N} \meta{gtl~var}
% \end{syntax}
% Empties the \meta{gtl~var}, locally or globally.
% \end{function}
%
% \begin{function}{\gtl_clear_new:N, \gtl_gclear_new:N}
% \begin{syntax}
% \cs{gtl_clear_new:N} \meta{gtl~var}
% \end{syntax}
% Ensures that the \meta{gtl~var} exists globally by applying
% \cs{gtl_new:N} if necessary, then applies \cs{gtl_(g)clear:N} to
% leave the \meta{gtl~var} empty.
% \end{function}
%
% \begin{function}{\gtl_set_eq:NN, \gtl_gset_eq:NN}
% \begin{syntax}
% \cs{gtl_set_eq:NN} \meta{gtl~var_1} \meta{gtl~var_2}
% \end{syntax}
% Sets the content of \meta{gtl~var_1} equal to that of
% \meta{gtl~var_2}.
% \end{function}
%
% \begin{function}{\gtl_concat:NNN, \gtl_gconcat:NNN}
% \begin{syntax}
% \cs{gtl_concat:NNN} \meta{gtl~var_1} \meta{gtl~var_2} \meta{gtl~var_3}
% \end{syntax}
% Concatenates the content of \meta{gtl~var_2} and \meta{gtl~var_3}
% together and saves the result in \meta{gtl~var_1}. The
% \meta{gtl~var_2} will be placed at the left side of the new extended
% token list.
% \end{function}
%
% \begin{function}[EXP, pTF]{\gtl_if_exist:N}
% \begin{syntax}
% \cs{gtl_if_exist_p:N} \meta{gtl~var}
% \cs{gtl_if_exist:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests whether the \meta{gtl~var} is currently defined. This does
% not check that the \meta{gtl~var} really is an extended token list
% variable.
% \end{function}
%
% \subsection{Adding data to token list variables}
%
% \begin{function}[updated = 2024-01-04]{\gtl_set:Nn, \gtl_set:Ne, \gtl_set:Nx, \gtl_gset:Nn, \gtl_gset:Ne, \gtl_gset:Nx}
% \begin{syntax}
% \cs{gtl_set:Nn} \meta{gtl~var} \Arg{token list}
% \end{syntax}
% Sets \meta{gtl~var} to contain the balanced \meta{token list},
% removing any previous content from the variable.
% \end{function}
%
% \begin{function}{\gtl_put_left:Nn, \gtl_gput_left:Nn}
% \begin{syntax}
% \cs{gtl_put_left:Nn} \meta{gtl~var} \Arg{token list}
% \end{syntax}
% Appends the balanced \meta{token list} to the left side of the
% current content of \meta{gtl~var}.
% \end{function}
%
% \begin{function}{\gtl_put_right:Nn, \gtl_gput_right:Nn}
% \begin{syntax}
% \cs{gtl_put_right:Nn} \meta{gtl~var} \Arg{token list}
% \end{syntax}
% Appends the balanced \meta{token list} to the right side of the
% current content of \meta{gtl~var}.
% \end{function}
%
% \subsection{Extended token list conditionals}
%
% \begin{function}[EXP, pTF]{\gtl_if_blank:N}
% \begin{syntax}
% \cs{gtl_if_blank_p:N} \Arg{gtl~var}
% \cs{gtl_if_blank:NTF} \Arg{gtl~var} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{gtl~var} consists only of blank spaces. The test
% is \texttt{true} if \meta{gtl~var} consists of zero or more explicit
% space characters (explicit tokens with character code~$32$ and
% category code~$10$), and is \texttt{false} otherwise.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_empty:N}
% \begin{syntax}
% \cs{gtl_if_empty_p:N} \meta{gtl~var}
% \cs{gtl_if_empty:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{gtl~var} is entirely empty (\emph{i.e.}~contains
% no tokens at all).
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_eq:NN}
% \begin{syntax}
% \cs{gtl_if_eq_p:NN} \Arg{gtl~var_1} \Arg{gtl~var_2}
% \cs{gtl_if_eq:NNTF} \Arg{gtl~var_1} \Arg{gtl~var_2} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if \meta{gtl~var_1} and \meta{gtl~var_2} have the same
% content. The test is \texttt{true} if the two contain the same list
% of tokens (identical in both character code and category code).
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_single_token:N}
% \begin{syntax}
% \cs{gtl_if_single_token_p:N} \meta{gtl~var}
% \cs{gtl_if_single_token:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the content of the \meta{gtl~var} consists of a single
% token. Such a token list has token count $1$ according to
% \cs{gtl_count_tokens:N}.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_tl:N}
% \begin{syntax}
% \cs{gtl_if_tl_p:N} \meta{gtl~var}
% \cs{gtl_if_tl:NTF} \meta{gtl~var} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{gtl~var} is balanced.
% \end{function}
%
% \subsection{The first token from an extended token list}
%
% \begin{function}[EXP]{\gtl_head:N}
% \begin{syntax}
% \cs{gtl_head:N} \meta{gtl~var}
% \end{syntax}
% Leaves in the input stream the first token in the \meta{gtl~var}.
% If the \meta{gtl~var} is empty, nothing is left in the input stream.
% \end{function}
%
% \begin{function}[EXP]{\gtl_head_do:NN}
% \begin{syntax}
% \cs{gtl_head_do:NN} \meta{gtl~var} \meta{cs}
% \end{syntax}
% Leaves in the input stream the \meta{control sequence} followed by
% the first token in \meta{gtl~var}. If the \meta{gtl~var} is empty,
% the \meta{cs} is followed by \cs{q_no_value}.
% \end{function}
%
% \begin{function}[EXP, TF]{\gtl_head_do:NN}
% \begin{syntax}
% \cs{gtl_head_do:NNTF} \meta{gtl~var} \meta{cs} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{gtl~var} is empty, leaves the \meta{false code} in the
% input stream. Otherwise leaves the \meta{control sequence}
% followed by the first token in \meta{gtl~var} and the \meta{true
% code}.
% \end{function}
%
% \begin{function}{\gtl_get_left:NN}
% \begin{syntax}
% \cs{gtl_get_left:NN} \meta{gtl~var_1} \meta{gtl~var_2}
% \end{syntax}
% Stores the first token from \meta{gtl~var_1} in \meta{gtl~var_2} as
% an single-token extended token list, without removing it from
% \meta{gtl~var_1}.
% \end{function}
%
% \begin{function}{\gtl_pop_left:N, \gtl_gpop_left:N}
% \begin{syntax}
% \cs{gtl_pop_left:N} \meta{gtl~var}
% \end{syntax}
% Remove the first token from \meta{gtl~var_1}.
% If the \meta{gtl~var_1} is empty nothing happens.
% \end{function}
%
% \begin{function}{\gtl_pop_left:NN, \gtl_gpop_left:NN}
% \begin{syntax}
% \cs{gtl_pop_left:NN} \meta{gtl~var_1} \meta{gtl~var_2}
% \end{syntax}
% Stores the first token from \meta{gtl~var_1} in \meta{gtl~var_2} as
% an single-token extended token list, and remove it from
% \meta{gtl~var_1}. If the \meta{gtl~var_1} is empty it remains so,
% and \meta{gtl~var_2} is set to contain \cs{q_no_value}.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_head_eq_catcode:NN}
% \begin{syntax}
% \cs{gtl_if_head_eq_catcode_p:NN} \Arg{gtl~var} \meta{test token}
% \cs{gtl_if_head_eq_catcode:NNTF} \Arg{gtl~var} \meta{test token}
% ~~\Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the first token in \meta{gtl~var} has the same category
% code as the \meta{test token}. In the case where \meta{gtl~var} is
% empty, the test will always be \texttt{false}.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_head_eq_charcode:NN}
% \begin{syntax}
% \cs{gtl_if_head_eq_charcode_p:NN} \Arg{gtl~var} \meta{test token}
% \cs{gtl_if_head_eq_charcode:NNTF} \Arg{gtl~var} \meta{test token}
% ~~\Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the first token in \meta{gtl~var} has the same character
% code as the \meta{test token}. In the case where \meta{gtl~var} is
% empty, the test will always be \texttt{false}.
% \end{function}
%
% \begin{function}[EXP,pTF]{\gtl_if_head_eq_meaning:NN}
% \begin{syntax}
% \cs{gtl_if_head_eq_meaning_p:NN} \Arg{gtl~var} \meta{test token}
% \cs{gtl_if_head_eq_meaning:NNTF} \Arg{gtl~var} \meta{test token}
% ~~\Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the first token in \meta{gtl~var} has the same meaning as
% the \meta{test token}. In the case where \meta{gtl~var} is empty,
% the test will always be \texttt{false}.
% \end{function}
%
% \begin{function}[EXP,pTF]
% {
% \gtl_if_head_is_group_begin:N,
% \gtl_if_head_is_group_end:N,
% \gtl_if_head_is_N_type:N,
% \gtl_if_head_is_space:N,
% }
% \begin{syntax}
% \cs{gtl_if_head_is_group_begin_p:N} \Arg{gtl~var}
% \cs{gtl_if_head_is_group_begin:NTF} \Arg{gtl~var}
% ~~\Arg{true code} \Arg{false code}
% \end{syntax}
% Tests whether the first token in \meta{gtl~var} is an explicit
% begin-group character, an explicit end-group character, an |N|-type
% token, or a space. In the case where \meta{gtl~var} is empty, the
% test will always be \texttt{false}.
% \end{function}
%
% \subsection{The first few tokens from an extended token list}
%
% \begin{function}[EXP]{\gtl_left_tl:N}
% \begin{syntax}
% \cs{gtl_left_tl:N} \meta{gtl~var}
% \end{syntax}
% Leaves in the input stream all tokens in \meta{gtl~var} until the
% first extra begin-group or extra end-group character, within
% \cs{exp_not:n}. This is the longest balanced token list starting
% from the left of \meta{gtl~var}.
% \end{function}
%
% \begin{function}{\gtl_pop_left_tl:N, \gtl_gpop_left_tl:N}
% \begin{syntax}
% \cs{gtl_pop_left_tl:N} \meta{gtl~var}
% \end{syntax}
% Remove from the \meta{gtl~var} all tokens before the first extra
% begin-group or extra end-group character. The tokens that are
% removed form the longest balanced token list starting from the left
% of \meta{gtl~var}.
% \end{function}
%
% \begin{function}[EXP]{\gtl_left_item:NF}
% \begin{syntax}
% \cs{gtl_left_item:NF} \meta{gtl~var} \Arg{false code}
% \end{syntax}
% Leaves in the input stream the first \meta{item} of the
% \meta{gtl~var}: this is identical to \cs{tl_head:n} applied to the
% result of \cs{gtl_left_tl:N}. If there is no such item, the
% \meta{false code} is left in the input stream.
% \end{function}
%
% \begin{function}[TF]{\gtl_pop_left_item:NN, \gtl_gpop_left_item:NN}
% \begin{syntax}
% \cs{gtl_pop_left_item:NNTF} \meta{gtl~var} \meta{tl~var}
% \Arg{true~code} \Arg{false~code}
% \end{syntax}
% Stores the first item of \meta{gtl~var} in \meta{tl~var}, locally,
% and removes it from \meta{gtl~var}, together with any space before
% it. If there is no such item, the \meta{gtl~var} is not affected,
% and the meta{tl~var} may or may not be affected.
% \end{function}
%
% \begin{function}[EXP]{\gtl_left_text:NF}
% \begin{syntax}
% \cs{gtl_left_text:NF} \meta{gtl~var} \Arg{false code}
% \end{syntax}
% Starting from the first token in \meta{gtl~var}, this function finds
% a pattern of the form \meta{tokens_1} \Arg{tokens_2}, where the
% \meta{tokens_1} contain no begin-group nor end-group characters,
% then leaves \meta{tokens_1} \Arg{tokens_2} in the input stream,
% within \cs{exp_not:n}. If no such pattern exists (this happens if
% the result of \cs{gtl_left_tl:N} contains no brace group), the
% \meta{false code} is run instead.
% \end{function}
%
% \begin{function}{\gtl_pop_left_text:N, \gtl_gpop_left_text:N}
% \begin{syntax}
% \cs{gtl_pop_left_text:N} \meta{gtl~var}
% \end{syntax}
% Starting from the first token in \meta{gtl~var}, this function finds
% a pattern of the form \meta{tokens_1} \Arg{tokens_2}, where the
% \meta{tokens_1} contain no begin-group nor end-group characters,
% then removes \meta{tokens_1} \Arg{tokens_2} from \meta{gtl~var}. If
% no such pattern exists (this happens if the result of
% \cs{gtl_left_tl:N} contains no brace group), the \meta{gtl~var} is
% not modified instead.
% \end{function}
%
% \subsection{Working with the contents of extended token lists}
%
% \begin{function}[EXP]{\gtl_count_tokens:N}
% \begin{syntax}
% \cs{gtl_count_tokens:N} \meta{gtl~var}
% \end{syntax}
% Counts the number of tokens in the \meta{gtl~var} and leaves this
% information in the input stream.
% \end{function}
%
% \begin{function}[EXP]{\gtl_extra_begin:N, \gtl_extra_end:N}
% \begin{syntax}
% \cs{gtl_extra_begin:N} \meta{gtl~var}
% \end{syntax}
% Counts the number of explicit extra begin-group (or end-group)
% characters in the \meta{gtl~var} and leaves this information in the
% input stream.
% \end{function}
%
% \begin{function}{\gtl_show:N, \gtl_log:N}
% \begin{syntax}
% \cs{gtl_show:N} \meta{gtl~var}
% \end{syntax}
% Displays the content of the \meta{gtl~var} on the terminal or in
% the log file.
% \end{function}
%
% \begin{function}[EXP, added = 2018-04-04]{\gtl_to_str:N, \gtl_to_str:n}
% \begin{syntax}
% \cs{gtl_to_str:N} \meta{gtl~var}
% \end{syntax}
% Converts the content of the \meta{gtl~var} into a series of
% characters with category code $12$ (other) with the exception of
% spaces, which retain category code $10$ (space). This string is
% then left in the input stream.
% \end{function}
%
% \subsection{Constant extended token lists}
%
% \begin{variable}{\c_empty_gtl}
% Constant that is always empty.
% \end{variable}
%
% \begin{variable}{\c_group_begin_gtl}
% An explicit begin-group character contained in an extended token
% list.
% \end{variable}
%
% \begin{variable}{\c_group_end_gtl}
% An explicit end-group character contained in an extended token list.
% \end{variable}
%
% \subsection{Future perhaps}
%
% \begin{itemize}
% \item Test if a token appears in an extended token list.
% \item Test if an extended token list appears in another.
% \item Remove an extended token list from another, once or every time
% it appears.
% \item Replace an extended token list by another in a third: once, or
% every time it appears.
% \item Case statement.
% \item Mapping?
% \item Inserting an extended token list into the input stream, with all
% its glorious unbalanced braces.
% \item Convert in various ways to a token list.
% \item Reverse the order of tokens.
% \item Extract a token given its position.
% \item Extract a range of tokens given their position.
% \item Trim spaces.
% \item Crazy idea below.
% \end{itemize}
%
% We could add (with lots of work) the expandable function
% \begin{quote}\ttfamily\obeylines\obeyspaces
% \cs{gtl_concat:nF}
% \{
% \Arg{tl_1} \Arg{start_1} \Arg{stop_1}
% \Arg{tl_2} \Arg{start_2} \Arg{stop_2}
% \ldots{}
% \Arg{tl_n} \Arg{start_n} \Arg{stop_n}
% \}
% \Arg{false code}
% \end{quote}
% For each triplet, this function builds the sub-token list of \meta{tl_i}
% corresponding to the tokens ranging from position \meta{start_i} to
% position \meta{stop_i} of \meta{tl_i}. The results obtained for each triplet
% are then concatenated. If nothing bad happens (see below), the
% concatenation is left in the input stream, and the \meta{false code} is
% removed. Two cases can lead to running the \meta{false code} (and dropping
% the first argument altogether). The first case is when the number of
% brace groups in \cs{gtl_concat:nF} is not a multiple of 3. The second
% case is when the concatenation gives rise to an unbalanced token list:
% then the result is not a valid token list. Note that each part is
% allowed to be unbalanced: only the full result must be balanced.
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{gtl} implementation}
%
% Some support packages are loaded first, then we declare the package's
% name, date, version, and purpose.
%
% \begin{macrocode}
%<*package>
% \end{macrocode}
%
% \begin{macrocode}
%<@@=gtl>
% \end{macrocode}
%
% Load \pkg{expl3}, either through \cs{RequirePackage} or through
% inputting the generic loader, depending on the format in use.
% \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
\input expl3-generic.tex
\else
\RequirePackage{expl3}[2017/11/14]
\fi
\ExplSyntaxOn
\cs_if_exist:NTF \ProvidesExplPackage
{
\cs_new_eq:NN \@@_end_package_hook: \prg_do_nothing:
\ExplSyntaxOff
\ProvidesExplPackage
}
{
\cs_new_eq:NN \@@_end_package_hook: \ExplSyntaxOff
\group_begin:
\ExplSyntaxOff
\cs_set_protected:Npn \@@_tmp:w #1#2#3#4
{
\group_end:
\tl_gset:ce { ver @ #1 . sty } { #2 ~ v#3 ~ #4 }
\cs_if_exist_use:NF \wlog { \iow_log:e }
{ Package: ~ #1 ~ #2 ~ v#3 ~ #4 }
}
\@@_tmp:w
}
{gtl} {2024/01/04} {0.6} {Manipulate unbalanced lists of tokens}
% \end{macrocode}
%
% \subsection{Helpers}
%
% \begin{macrocode}
\cs_generate_variant:Nn \use:nn { no }
% \end{macrocode}
%
% \begin{macro}[EXP]{\@@_exp_not_n:N}
% Used in one case where we need to prevent expansion of a token
% within an |x|-expanding definition. Using \cs{exp_not:N} there
% would fail when the argument is a macro parameter character.
% \begin{macrocode}
\cs_new:Npn \@@_exp_not_n:N #1 { \exp_not:n {#1} }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_brace:nn, \@@_brace_swap:nn}
% Those functions are used to add some tokens, |#1|, to an item |#2|
% in an extended token list: \cs{@@_brace:nn} adds tokens on the left,
% while \cs{@@_brace_swap:nn} adds them on the right.
% \begin{macrocode}
\cs_new:Npn \@@_brace:nn #1#2 { { #1 #2 } }
\cs_new:Npn \@@_brace_swap:nn #1#2 { { #2 #1 } }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_strip_nil_mark:w,}
% \begin{macro}[EXP]{\@@_strip_nil_mark_aux:w}
% Removes the following \cs{q_nil} \cs{q_mark} without losing any
% braces, and places the result into \cs{exp_not:n}.
% \begin{macrocode}
\cs_new:Npn \@@_strip_nil_mark:w
{ \@@_strip_nil_mark_aux:w \prg_do_nothing: }
\cs_new:Npn \@@_strip_nil_mark_aux:w #1 \q_nil \q_mark
{ \exp_not:o {#1} }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Structure of extended token lists}
%
% Token lists must have balanced braces (or rather, begin-group and
% end-group characters). Extended token lists lift this requirement,
% and can represent arbitrary lists of tokens. A list of tokens can
% fail to be balanced in two ways: one may encounter too many end-group
% characters near the beginning of the list, or too many begin-group
% characters near the end of the list. In fact, a list of tokens always
% has the form
% \begin{quote}\ttfamily
% \meta{b_1} \} \ldots{} \meta{b_n} \}
% \meta{c}
% \{ \meta{e_1} \ldots{} \{ \meta{e_p}
% \end{quote}
% where the \meta{b_i}, \meta{c}, and \meta{e_i} are all balanced token
% lists. This can be seen by listing the tokens, and keeping track of a
% counter, which starts at~$0$, and is incremented at each begin-group
% character, and decremented at each end-group character: then the
% \meta{b_i} are delimited by positions where the counter reaches a new
% minimum, whereas the \meta{e_i} are delimited by positions where the
% counter last takes a given negative value. Such a token list is
% stored as
% \begin{quote}\ttfamily
% \cs{s_@@}
% \{ \Arg{b_1} \ldots{} \Arg{b_n} \}
% \Arg{c}
% \{ \Arg{e_p} \ldots{} \Arg{e_1} \}
% \end{quote}
% Note that the \meta{e_i} are in a reversed order, as this makes the
% ends of extended token lists more accessible. Balanced token lists
% have $n = p = 0$: the first and third parts are empty, while the
% second contains the tokens.
%
% In the following code comments, the balanced token lists \meta{b_i}
% are called ``leading chunks'', \meta{c} is called ``middle chunk'',
% and \meta{e_i} are called ``trailing chunks''. It is important to
% note that a balanced sub-list of a gtl must be entirely contained in
% one of the chunk.
%
% \begin{variable}{\s_@@}
% This marker appears at the start of extended token lists.
% \begin{macrocode}
\cs_new_eq:NN \s_@@ \scan_stop:
% \end{macrocode}
% \end{variable}
%
% \begin{macro}
% {
% \gtl_set:Nn, \gtl_gset:Nn, \gtl_const:Nn,
% \gtl_set:Ne, \gtl_gset:Ne, \gtl_const:Ne,
% \gtl_set:Nx, \gtl_gset:Nx, \gtl_const:Nx
% }
% Storing a balanced token list into an extended token list variable
% simply means adding \cs{s_@@} and two empty brace
% groups: there are no leading nor trailing chunks.
% \begin{macrocode}
\cs_new_protected:Npn \gtl_set:Nn { \@@_set:NNn \tl_set:Nn }
\cs_new_protected:Npn \gtl_gset:Nn { \@@_set:NNn \tl_gset:Nn }
\cs_new_protected:Npn \gtl_const:Nn { \@@_set:NNn \tl_const:Nn }
\cs_generate_variant:Nn \gtl_set:Nn { Ne , Nx }
\cs_generate_variant:Nn \gtl_gset:Nn { Ne , Nx }
\cs_generate_variant:Nn \gtl_const:Nn { Ne , Nx }
\cs_new_protected:Npn \@@_set:NNn #1#2#3
{ #1 #2 { \s_@@ { } {#3} { } } }
% \end{macrocode}
% \end{macro}
%
% \begin{variable}{\c_empty_gtl}
% An empty extended token list, obtained thanks to the
% \cs{gtl_const:Nn} function just defined.
% \begin{macrocode}
\gtl_const:Nn \c_empty_gtl { }
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_group_begin_gtl, \c_group_end_gtl}
% Extended token lists with exactly one begin-group/end-group
% character are built by including a single (empty) leading or
% trailing chunk.
% \begin{macrocode}
\tl_const:Nn \c_group_end_gtl { \s_@@ { { } } { } { } }
\tl_const:Nn \c_group_begin_gtl { \s_@@ { } { } { { } } }
% \end{macrocode}
% \end{variable}
%
% \subsection{Creating extended token list variables}
%
% \begin{macro}{\gtl_new:N}
% A new extended token list is created empty.
% \begin{macrocode}
\cs_new_protected:Npn \gtl_new:N #1
{ \cs_new_eq:NN #1 \c_empty_gtl }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gtl_set_eq:NN, \gtl_gset_eq:NN}
% All the data about an extended token list is stored as a single
% token list, so copying is easy.
% \begin{macrocode}
\cs_new_eq:NN \gtl_set_eq:NN \tl_set_eq:NN
\cs_new_eq:NN \gtl_gset_eq:NN \tl_gset_eq:NN
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gtl_clear:N, \gtl_gclear:N}
% Clearing an extended token list by setting it to the empty one.
% \begin{macrocode}
\cs_new_protected:Npn \gtl_clear:N #1
{ \gtl_set_eq:NN #1 \c_empty_gtl }
\cs_new_protected:Npn \gtl_gclear:N #1
{ \gtl_gset_eq:NN #1 \c_empty_gtl }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\gtl_clear_new:N, \gtl_gclear_new:N}
% If the variable exists, clear it. Otherwise declare it.
% \begin{macrocode}
\cs_new_protected:Npn \gtl_clear_new:N #1
{ \gtl_if_exist:NTF #1 { \gtl_clear:N #1 } { \gtl_new:N #1 } }
\cs_new_protected:Npn \gtl_gclear_new:N #1
{ \gtl_if_exist:NTF #1 { \gtl_gclear:N #1 } { \gtl_new:N #1 } }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP, pTF]{\gtl_if_exist:N}
% Again a copy of token list functions.
% \begin{macrocode}
\prg_new_eq_conditional:NNn \gtl_if_exist:N \tl_if_exist:N
{ p , T , F , TF }
% \end{macrocode}
% \end{macro}
%
% \subsection{Adding data to extended token list variables}
%
% \begin{macro}{\gtl_put_left:Nn, \gtl_gput_left:Nn}
% \begin{macro}[rEXP]{\@@_put_left:wn}
% If there is no leading chunk in the gtl variable, then add the new
% material to the middle chunk. Otherwise add it to the first
% leading chunk, namely the first brace group in the first argument
% of \cs{@@_put_left:wn}.
% \begin{macrocode}
\cs_new_protected:Npn \gtl_put_left:Nn #1#2
{ \tl_set:Ne #1 { \exp_after:wN \@@_put_left:wn #1 {#2} } }
\cs_new_protected:Npn \gtl_gput_left:Nn #1#2
{ \tl_gset:Ne #1 { \exp_after:wN \@@_put_left:wn #1 {#2} } }
\cs_new:Npn \@@_put_left:wn \s_@@ #1#2#3 #4
{
\tl_if_empty:nTF {#1}
{ \exp_not:n { \s_@@ { } { #4 #2 } {#3} } }
{
\s_@@
{ \exp_not:o { \@@_brace:nn {#4} #1 } }
{ \exp_not:n {#2} }
{ \exp_not:n {#3} }
}
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\gtl_put_right:Nn, \gtl_gput_right:Nn}
% \begin{macro}[rEXP]{\@@_put_right:wn}
% Symmetric of \cs{gtl_put_left:Nn}.
% \begin{macrocode}
\cs_new_protected:Npn \gtl_put_right:Nn #1#2
{ \tl_set:Ne #1 { \exp_after:wN \@@_put_right:wn #1 {#2} } }