summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/expl3/l3basics.dtx
blob: 8a4170e79cfb6798a0a98c14ed9e679638b6e28b (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
% \iffalse
%% File: l3basics.dtx Copyright (C) 1990-1998 LaTeX3 project
%
%<*dtx>
          \ProvidesFile{l3basics.dtx}
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{l3basics}
%<driver> \ProvidesFile{l3basics.drv}
% \fi
%         \ProvidesFile{l3basics.dtx}
          [1998/05/04 v1.0i L3 Experimental basic definitions]
%
% \iffalse
%<*driver>
\documentclass{l3doc}

\begin{document}
\DocInput{l3basics.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{l3basics.dtx}
% \title{The \textsf{l3basics} package\thanks{This file
%         has version number \fileversion, last
%         revised \filedate.}\\
% Basic Definitions}
% \author{\Team}
% \date{\filedate}
% \maketitle
%
%
%    We start by ensuring that the required packages are loaded.  In
%    this case there is a boot-strapping problem since, to use much of
%    the code in this file, the `l3expan' package needs to be loaded;
%    but this file must be loaded first.
%    \begin{macrocode}
%<package>\RequirePackage{l3names}\par
%    \end{macrocode}
%
% As the name suggest this package holds some basic definitions which
% are needed by most or all other packages in this set.
%
%    \section{Basics}
%    
% Here we describe those functions that used all over the place.
% 
% \subsection{Functions}
% 
% \begin{function}{\cs_to_str:N}
% \begin{syntax}
%   "\cs_to_str:N" <cs>
% \end{syntax}
% This function return the name of <cs> as a sequence of letters with
% the escape character removed.
% \end{function}
% 
% \begin{function}{\cs_gen_sym:N |
%                  \cs_ggen_sym:N}
% \begin{syntax}
%   "\cs_gen_sym:N" <tlp>
% \end{syntax}
% These functions will generate a new control sequence name for use as a
% pointer, e.g.\ some tree structure like the LDB. The new unique name
% is returned locally in <tlp> for further use. The names are generated
% using the roman numeral representation of some special counters
% together with a prefix of "\l*" (local) or "\g*"( global).
% \end{function}
% 
% 
% \subsection{Predicates and conditionals}
% 
% \begin{function}{%
%                  \if_meaning:NN |
% }
% \begin{syntax}
%   "\if_meaning:NN" <cs1> <cs2> <true code> "\else:" <false code> "\fi:"
% \end{syntax}
% This conditional executes <true code> when the replacement text, i.e.,
% the expansion of <cs1> and <cs2> are the same, otherwise it executes
% <false code>.
% \begin{texnote}
% This is the primitive \tn{ifx}.
% \end{texnote}
% \end{function}
% \begin{function}{%
%                  \cs_eq_p:NN |
% }
% \begin{syntax}
%   "\cs_eq_p:NN" <cs1> <cs2>
% \end{syntax}
% Returns `true' if <cs1> and <cs2> are textually the same, i.e.\ have
% the same name, otherwise it returns `false'.
% \end{function}
% 
%
% \begin{function}{%
%                  \cs_free_p:N |
% }
% \begin{syntax}
%   "\cs_free_p:N" <cs>
% \end{syntax}
% Returns `true' if <cs> is either undefined or equal to "\scan_stop:".
% However, it returns `false' if <cs> is textually "\c_undefined" (the
% constantly undefined function), or  textually "\scan_stop:".
% \end{function}
% 
% \begin{function}{%
%                  \cs_free:NTF |
%                  \cs_free:NF |
%                  \cs_free:NT |
%                  \cs_free:cF |
%                  \cs_free:cTF |
% }
% \begin{syntax}
%    "\cs_free:NTF" <cs> "{"<true code>"}{"<false code>"}"
% \end{syntax}
% These functions check if <cs> is free and then execute either <true
% code> or <false code>.
% \begin{texnote}
% The conditional "\cs_free:cTF" is the \LaTeX3 implementation of the
% \LaTeX2 function \tn{@ifundefined}. The other functions haven't been
% around before.
% \end{texnote}
% \end{function}
% 
% \begin{function}{%
%                  \cs_exist_p:N |
% }
% \begin{syntax}
%   "\cs_exist_p:N" <cs>
% \end{syntax}
% This function does the opposite of "\cs_free_p:N".
% \end{function}
% 
% \begin{function}{%
%                  \chk_new_cs:N |
% }
% \begin{syntax}
%   "\chk_new_cs:N" <cs>
% \end{syntax}
% This function checks that <cs> is so far either undefined or equals
% "\scan_stop:" (the function that is assigned to newly created
% control sequences by \TeX{} when "\cs:w" "..." "\cs_end:" is
% used).
% \end{function}
% 
% \begin{function}{%
%                  \chk_exist_cs:N |
% }
% \begin{syntax}
%   "\chk_exist_cs:N" <cs>
% \end{syntax}
% This function checks that <cs> is defined. If it is not an error
% is generated.
% \end{function}
% 
% 
% \subsection{Internal functions}
% 
% \begin{function}{%
%                  \cs:w |
%                  \cs_end: |
% }    
% \begin{syntax}
%   "\cs:w" <tokens> "\cs_end:"
% \end{syntax}
% This is the \TeX{} internal way of generating a  control sequence from
% some token list. <tokens> get expanded and must ultimately result in a
% sequence of characters.
% \begin{texnote}
% These functions are the primitives \tn{csname} and \tn{endcsname}.
% "\cs:w" is considered weird because it expands tokens until it reaches
% "\cs_end:".
% \end{texnote}
% \end{function}
% 
% \begin{function}{\pref_global:D |
%                  \pref_long:D |
% }    
% \begin{syntax}
%   "\pref_global:D" "\def:Npn"
% \end{syntax}
% Prefix functions that can be used in front of some definition
% functions (namely \ldots). The result of prefixing a function
% definition with "\pref_global:D" makes the definition global,
% "\pref_long:D" change the argument scanning mechanism so that it
% allows "\par" tokens in the argument of the prefixed function.
% 
% None of these internal functions should be used by a programmer since
% the necessary combinations are all available as separate function,
% e.g., "\def_long:Npn" is internally implemented as "\pref_long:D"
% "\def_long:Npn". 
% \begin{texnote}
% These prefixes are the primitives \tn{global}, \tn{long}.
% The \tn{outer} isn't used at all within \LaTeX3
% because \ldots
% \end{texnote}
% \end{function}
% 
% \subsection{Choice functions}
%
% \begin{function}{%
%                  \use_choice_i:nn |
%                  \use_choice_ii:nn |
% }
% \begin{syntax}
%   "\use_choice_i:nn"  "{" <code1> "}{" <code2> "}"
% \end{syntax}
% Functions that execute the first or second argument respectively,
% after removing the surrounding braces. Used to implement conditionals.
% \end{function}
%
% \begin{function}{%
%                  \use_choice_i:nnn |
%                  \use_choice_ii:nnn |
%                  \use_choice_iii:nnn |
% }
% \begin{syntax}
%   "\use_choice_i:nnn"  "{" <arg1> "}{" <arg2> "}{" <arg2> "}"
% \end{syntax}
% Functions that pick up one of three arguments and execute them after
% removing the surrounding braces. Should be described somewhere else.
% \end{function}
%
% \begin{function}{\use:n}
% \begin{syntax}
%   "\use:n"  "{" <code1> "}"
% \end{syntax}
% Function that executes the next argument after removing the
% surrounding braces. Used to implement conditionals.
% \end{function}
%
% \section{Defining functions}
% 
% There are two types of function definitions in \LaTeX3:  versions
% that check if the function name is still unused, and versions that
% simply make the definition. The later are used for internal scratch
% functions that get new meanings all over the place.
% 
% (Parts of this module are a mess, as far as naming conventions are
% concerned.)
%
% \begin{texnote}
% While \TeX{} makes all definition functions directly available to the
% user \LaTeX3 hides them very carefully to avoid the problems with
% definitions that are overwritten accidentally. Many functions that are in
% \TeX{} a combination of prefixes and definition functions are provided
% as individual functions.
% \end{texnote}
% 
% \subsection{Defining new functions}
% 
% A definition of a new function can be done locally and globally. Currently
% nearly all function definitions are done locally on top level, in
% other words they are global but don't show it. Therefore I think it may
% be better to remove the local variants in the future and declare all
% checked function definitions global.
% 
% \begin{function}{\def_new:Npn |
%                  \def_new:Npx |
%                  \def_new:cpn |
%                  \def_new:cpx
% }
% \begin{syntax}
%   "\def_new:Npn" <cs> <parms> "{" <code> "}"
% \end{syntax}
% Defines a new function, making sure that <cs> is unused so far.
% <parms> may consist of arbitrary parameter specification in \TeX{}
% syntax. It is under the responsibility of the programmer to name the
% new function according to the rules laid out in the previous section.
% <code> is either passed literally or may be subject to expansion
% (under the "x" variants).
% \end{function}
%
% \begin{function}{\gdef_new:Npn
% }
% \begin{syntax}
%   "\gdef_new:Npn" <cs> <parms> "{" <code> "}"
% \end{syntax}
% Like "\def_new:Npn" but defines the new function globally. See
% comments above.
% \end{function}
% 
% \begin{function}{\def_long_new:Npn |
% }
% \begin{syntax}
%   "\def_long_new:Npn" <cs> <parms> "{" <code> "}"
% \end{syntax}
% Defines a function that may contain "\par" tokens in the argument(s)
% when called. This is not allowed for normal functions.
% \end{function}
% 
% \begin{function}{\let_new:NN |
%                  \glet_new:NN}
% \begin{syntax}
%   "\let_new:NN" <cs1> <cs2>
% \end{syntax}
% Gives the function <cs1> the current meaning of <cs2>. Again, we may
% do this alway globally.
% \end{function}
% 
% \subsection{Defining internal functions (no checks)}
% 
% Besides the function definitions that check whether or not their
% argument is an unused function we need function definitions that
% overwrite currently used definitions. The following functions are
% provided for this purpose.
% 
% \begin{function}{\def:Npn |
%                  \def:Npx |
%                  \def:cpn |
%                  \def:cpx |
% }
% \begin{syntax}
%   "\def:Npn" <cs> <parms> "{" <code> "}"
% \end{syntax}
% Like "\def_new:Npn" etc.\ but does not check the <cs> name.
% \begin{texnote}
% "\def:Npn" is the \LaTeX3 name for \TeX{}'s \tn{def} and "\def:Npx"
% corresponds to the primitive \tn{edef}. The "\def:cpn" function was
% known in \LaTeX2 as \tn{@namedef}. "\def:cpx" has no equivalent.
% \end{texnote}
% \end{function}
% 
% \begin{function}{\gdef:Npn |
%                  \gdef:Npx |
%                  \gdef:cpn |
%                  \gdef:cpx |
% }
% \begin{syntax}
%   "\gdef:Npn" <cs> <parms> "{" <code> "}"
% \end{syntax}
% Like "\def:Npn" but defines the <cs> globally.
% \begin{texnote}
% "\gdef:Npn" and "\gdef:Npx" are known to \TeX{}hackers as \tn{gdef}
% and \tn{xdef}.
% \end{texnote}
% \end{function}
% 
% \begin{function}{\def:No |
%                  \gdef:No
% }
% \begin{syntax}
%   "\def:No" <cs> "{" <code> "}"
% \end{syntax}
% Local and global variant that expands code once before defining <cs>.
% The function may not take <parms> as the others do. Perhaps this
% should be changed.
% \end{function}
% 
% \begin{function}{\def_long:Npn |
%                  \def_long:Npx |
%                  \def_long:cpn |
% }
% \begin{syntax}
%   "\def_long:Npn" <cs> <parms> "{" <code> "}"
% \end{syntax}
% Like "\def:Npn" but allows "\par" tokens in the arguments of the
% function being defined.
% \end{function}
% 
% \begin{function}{\gdef_long:Npn |
%                  \gdef_long:Npx |
% }
% \begin{syntax}
%   "\gdef_long:Npn" <cs> <parms> "{" <code> "}"
% \end{syntax}
% Global variant of "\def_long:Npn".
% \end{function}
% 
% \begin{function}{\let:NN |
%                  \glet:NN |
%                  \let:cN  |
%                  \let:Nc  |
%                  \let:cc  
%                 }
% \begin{syntax}
%   "\let:cN" <cs1> <cs2>
% \end{syntax}
% Gives the function <cs1> the current meaning of <cs2>. Again, we may
% always do this globally.
% \end{function}
% 
% \begin{function}{\let:NwN}
% \begin{syntax}
%   "\let:NwN"  <cs1> <cs2>
%   "\let:NwN"  <cs1> "=" <cs2>
% \end{syntax}
% These functions assign the meaning of <cs2> locally or globally to the
% function <cs1>. Because the \TeX{} primitive operation is being used
% which may have an equal sign and (a certain number of) spaces between
% <cs1> and <cs2> the name contains a "w". (Not happy about this
% convention!).
% \begin{texnote}
% "\let:NwN" is the \LaTeX3 name for \TeX{}'s \tn{let}.
% \end{texnote}
% \end{function}
%
%
% \StopEventually{}
% 
% \section{Renaming some \TeX{} primitives (again)}
%
% Having given all the tex primitives a consistent name, we need to
% give sensible names to the ones we actually want to use.
% These will be defined as needed in the apropriate modules, but
% do a few now, just to get started.\footnote{This renaming gets expensive
% in terms of csname usage, an alternative scheme would be to just use
% the ``tex\ldots D'' name in the cases where no good alternative exists.}
%    \begin{macrocode}
%<*package>
\tex_let:D \let:NwN            \tex_let:D
\let:NwN   \def:Npn            \tex_def:D
\let:NwN   \gdef:Npn           \tex_gdef:D
\let:NwN   \def:Npx            \tex_edef:D
\let:NwN   \gdef:Npx           \tex_xdef:D
\let:NwN   \if:w               \tex_if:D
\let:NwN   \if_num:w           \tex_ifnum:D
\let:NwN   \if_meaning:NN      \tex_ifx:D
\let:NwN   \if_false:          \tex_iffalse:D
\let:NwN   \else:              \tex_else:D
\let:NwN   \fi:                \tex_fi:D
\let:NwN   \io_put_deferred:Nx \tex_write:D
\let:NwN   \token_to_meaning:N \tex_meaning:D
\let:NwN   \token_to_string:N  \tex_string:D
\let:NwN   \cs:w               \tex_csname:D
\let:NwN   \cs_end:            \tex_endcsname:D
\let:NwN   \exp_after:NN       \tex_expandafter:D
\let:NwN   \scan_stop:         \tex_relax:D
\let:NwN   \exp_not:N          \tex_noexpand:D
\let:NwN   \group_begin:       \tex_begingroup:D
\let:NwN   \group_end:         \tex_endgroup:D
%    \end{macrocode}
%    
%    These following names are temporary and should be removed as soon
%    as possible (April 1998).
%    \begin{macrocode}
\let:NwN   \the_internal:D     \tex_the:D
\let:NwN   \pref_global:D      \tex_global:D
%    \end{macrocode}
%
%    
% \section {Defining and checking (new) functions}
%
%    We need some constants now, that should actually all be defined
%    together. We try to avoid using count registers for constants as
%    much as possible.
%
%    \begin{macrocode}
\let:NwN\c_minus_one\m@ne
\tex_chardef:D \c_one     = 1\scan_stop:
\tex_chardef:D \c_sixteen = 16\scan_stop:
\tex_mathchardef:D \c_two_hundred_fifty_six = 256\scan_stop:
%    \end{macrocode}
%
%    We provide two kinds of functions that can be used to define
%    control sequences. On the one hand we have functions that check
%    if their argument doesn't already exist, they are called
%    |\..._new|. The second type of defining functions doesn't check
%    if the argument is already defined.
%
%    Before we can define them, we need some auxiliary macros that
%    allow us to generate error messages. The definitions here are 
%    only temporary, they will be redefined later on.
%
%  \begin{macro}{\io_put_log:x}
%  \begin{macro}{\io_put_term:x}
%    We define a routine to write only to the log file. And a
%    similar one for writing to both the log file and the terminal.
%
%    \begin{macrocode}
\def:Npn \io_put_log:x{
      \tex_immediate:D\io_put_deferred:Nx \c_minus_one }
\def:Npn \io_put_term:x{
      \tex_immediate:D\io_put_deferred:Nx \c_sixteen }
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
% \begin{macro}{\err_latex_bug:n}
%    This will show internal errors.
%    \begin{macrocode}
\def:Npn\err_latex_bug:n#1{
   \io_put_term:x{This~is~a~LaTeX~bug!~Check~coding!}\tex_errmessage:D{#1}}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\cs_record_meaning:N}
%    This macro will be used later on for tracing purposes. But we
%    need some more modules to define it, so we just give some dummy
%    definition here.
%    \begin{macrocode}
%<*trace>
\def:Npn \cs_record_meaning:N#1{}
%</trace>
%    \end{macrocode}
%  \end{macro}
%    
% \subsection{Checking new definitions}
%
% \begin{macro}{\chk_new_cs:N}
%    This command is called by |\def_new:Npn| and |\let_new:NN| etc.\ 
%    to make sure that the argument sequence is not already in use. If
%    it is, an error is signalled.
%    It checks if \m{csname} is undefined or
%    |\scan_stop:|. Otherwise an error message is issued. 
%    \begin {macrocode}
\def:Npn \chk_new_cs:N #1{
     \if_meaning:NN #1\c_undefined
     \else:
       \if_meaning:NN #1\scan_stop:
       \else:
         \err_latex_bug:n {Command~name~`\token_to_string:N #1'~
                               already~defined!~
                           Current~meaning:~\token_to_meaning:N #1
                          }
       \fi:
     \fi:
%<*trace>
     \cs_record_meaning:N#1
%     \io_put_term:x{Defining~\token_to_string:N #1~on~
     \io_put_log:x{Defining~\token_to_string:N #1~on~
                         line~\tex_the:D \tex_inputlineno:D}
%</trace>
   }
%    \end{macrocode}
% \end {macro}
%
% \begin{macro}{\cs_exist_p:N}
%    Expands into |\c_true| if the control sequence given as its
%    argument {\em is} in use.
%
%    \begin{macrocode}
\def:Npn \cs_exist_p:N #1{
  \if:w \cs_free_p:N #1
    \c_false
  \else:
    \c_true \fi:}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\chk_exist_cs:N }
%    This function issues a warning message when the control sequence 
%    in its argument does not exist.
%    \begin{macrocode}
\def:Npn \chk_exist_cs:N #1 {
  \if:w \cs_exist_p:N #1
  \else:
    \err_latex_bug:n{Command~ `\token_to_string:N #1'~
                     not~ yet~ defined!}
  \fi:}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cs_free_p:N}
%    Expands into |\c_true| if the control sequence given as its
%    argument is not yet in use.  Note that we make sure to expand
%    into |\c_false | if the control sequence is textually
%    |\c_undefined| or |\scan_stop:|, so that we don't end up
%    (re)defining them.
%    \begin{macrocode}
\def:Npn \cs_free_p:N #1{
  \if_meaning:NN#1\c_undefined
    \if:w\cs_eq_p:NN #1\c_undefined \c_false
    \else: \c_true \fi:
  \else:
    \if_meaning:NN#1\scan_stop:
      \if:w\cs_eq_p:NN #1\scan_stop: \c_false
       \else: \c_true \fi:
     \else:
       \c_false \fi:\fi:}
%    \end{macrocode}
% \end{macro}
%
% \subsection{More new definitions}
%
%  \begin{macro}{\let:NN}
%  \begin{macro}{\let:cN}
%  \begin{macro}{\let:Nc}
%  \begin{macro}{\let:cc}
%    These macros allow us to copy the definition of a control sequence
%    to another control sequence. Maybe we should implement a few more
%    of these.
%
%    \begin{macrocode}
\tex_long:D\def:Npn \let:NN #1{
%    \end{macrocode}
%    The |=| sign allows us to define funny char tokens like .|=|
%    itself or \verb*= = with this function. For the definition of
%    |\c_space_chartok{~}| to work we need the |~| after the |=|
%
%    \begin{macrocode}
                              \let:NwN #1=~}
\def:Npn\let:cN #1 {\exp_after:NN\let:NwN\cs:w#1\cs_end:=~}
\def:Npn\let:Nc{\exp_args:NNc\let:NN}
\def:Npn\let:cc{\exp_args:Ncc\let:NN}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%
%
% \begin {macro}{\def_new:Npn}
% \begin {macro}{\def_new:Npx}
% \begin {macro}{\let_new:NN}
%    These are like |\def:Npn| and |\let:NN|, but they first check that
%    the argument command is not already in use. You may use
%    |\tex_global:D|, |\tex_long:D|, and |\tex_outer:D| as
%    prefixes.
%     \begin {macrocode}
\def:Npn \def_new:Npn #1{\chk_new_cs:N #1
                         \def:Npn #1}
\def:Npn \def_new:Npx #1{\chk_new_cs:N #1
                         \def:Npx #1}
\def_new:Npn \let_new:NN #1{\chk_new_cs:N #1
                              \let:NN #1}
%    \end{macrocode}
% \end {macro}
% \end {macro}
% \end {macro}
%
% \begin {macro}{\def:cpn}
% \begin {macro}{\def:cpx}
% \begin {macro}{\def_new:cpn}
% \begin {macro}{\def_new:cpx}
%    Like |\def:Npn| and |\def_new:Npn|, except that the first
%    argument consists of the sequence of characters that should be
%    used to form the name of the desired control sequence ( the |c|
%    stands for csname argument, see the expansion module.).
%
%    |\def:cpn|\m{string}\m{rep-text} will turn \m{string} into a
%    csname and then assign \m {rep-text} to it by using |\def:Npn|.
%    This means that there might be a parameter string between the two
%    arguments.
%    \begin {macrocode}
\def_new:Npn \def:cpn #1{\exp_after:NN
                             \def:Npn
                             \cs:w #1\cs_end:}
\def_new:Npn \def:cpx #1{\exp_after:NN
                             \def:Npx
                             \cs:w #1\cs_end:}
\def_new:Npn \def_new:cpn #1{\exp_after:NN
                                  \def_new:Npn
                                  \cs:w #1\cs_end:}
\def_new:Npn \def_new:cpx #1{\exp_after:NN
                                  \def_new:Npx
                                  \cs:w #1\cs_end:}
%    \end{macrocode}
% \end {macro}
% \end {macro}
% \end {macro}
% \end {macro}
%
%
%  \begin{macro}{\def:No}
%    |\def:No| expands its second argument one time before making 
%    the definition.
%    \begin{macrocode}
\def_new:Npn \def:No{\exp_args:NNo\def:Npn}
%    \end{macrocode}
%  \end{macro}
%
% \begin {macro}{\def_long:Npn}
% \begin {macro}{\def_long_new:Npn}
% \begin {macro}{\def_long:cpn}
% \begin {macro}{\def_long:Npx}
%    |\def_long:Npn| stands for |\tex_long:D| |\def:Npn|.
%    |\def_long:Npx| expands its second argument.
%    \begin {macrocode}
\def_new:Npn \def_long:Npn {\tex_long:D\def:Npn}
\def_new:Npn \def_long_new:Npn #1{\chk_new_cs:N #1
                                 \def_long:Npn #1}
\def_new:Npn \def_long:cpn #1{\exp_after:NN
                              \def_long:Npn
                              \cs:w #1\cs_end:}
\def_new:Npn \def_long:Npx {\tex_long:D\def:Npx}
%    \end{macrocode}
% \end {macro}
% \end {macro}
% \end {macro}
% \end {macro}
%
% \begin {macro}{\glet:NN}
% \begin {macro}{\glet_new:NN}
% \begin {macro}{\gdef_new:Npn}
% \begin {macro}{\gdef:cpn}
% \begin {macro}{\gdef:cpx}
% \begin {macro}{\gdef:No}
%  These are global versions of some of the previosly defined functions.
%    \begin {macrocode}
\def_new:Npn \glet:NN {\tex_global:D \let:NN}
\def_new:Npn \glet_new:NN #1{\chk_new_cs:N #1
                               \tex_global:D\let:NN #1}
\def_new:Npn \gdef_new:Npn #1{\chk_new_cs:N #1
                              \gdef:Npn #1}
\def_new:Npn \gdef:cpn {\tex_global:D \def:cpn}
\def_new:Npn \gdef:cpx {\tex_global:D \def:cpx}
\def_new:Npn \gdef:No  {\exp_args:NNo\gdef:Npn}
%    \end{macrocode}
% \end {macro}
% \end {macro}
% \end {macro}
% \end {macro}
% \end {macro}
% \end {macro}

% \begin {macro}{\gdef_long:Npn}
% \begin {macro}{\gdef_long:Npx}
%    |\gdef_long:Npn| stands for |\tex_long:D| |\tex_global:D| |\def:Npn|.
%    |\gdef_long:Npx| expands its second argument.
%    \begin {macrocode}
\def_new:Npn \gdef_long:Npn {\tex_long:D\gdef:Npn}
\def_new:Npn \gdef_long:Npx {\tex_long:D\gdef:Npx}
%    \end{macrocode}
% \end {macro}
% \end {macro}
%
% \begin {macro}{\gfuturelet:NNN}
%    This is a global version of |\let_peek_after:NNN|
%    \begin {macrocode}
\def_new:Npn \gfuturelet:NNN{\tex_global:D \let_peek_after:NNN}
%    \end{macrocode}
% \end {macro}
%
% \subsection{Further checking}
%    
% \begin{macro}{\cs_free:NTF}
% \begin{macro}{\cs_free:NT}
% \begin{macro}{\cs_free:NF}
%    The old |\@ifundefined| of \LaTeX{} 2.09 is reimplemented in the
%    function |\cs_free:cTF|, again in a way that |\else:| and |\fi:|
%    are removed. In this implementation this is absolutely
%    necessary because functions inside the conditional parts expect
%    to read further input from outside the conditional.  Actually,
%    the first part of the code below is more general, since it checks
%    \m{csnames} directly and therefore allows both |\scan_stop:| and
%    |\c_undefined|.
%    \begin{macrocode}
\def_long_new:Npn \cs_free:NTF #1{\if:w\cs_free_p:N #1
  \exp_after:NN \use_choice_i:nn \else:
  \exp_after:NN \use_choice_ii:nn \fi:}
\def_long_new:Npn \cs_free:NT #1{\if:w\cs_free_p:N #1
  \exp_after:NN \use:n \else:
  \exp_after:NN \use_none:n \fi:}
\def_long_new:Npn \cs_free:NF #1{\if:w\cs_free_p:N #1
  \exp_after:NN \use_none:n \else:
  \exp_after:NN \use:n \fi:}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\cs_free:cTF}
% \begin{macro}{\cs_free:cT}
% \begin{macro}{\cs_free:cF}
%    We have to implement the |c| variants `by hand' because a different
%    test is necessary and I don't want the overhead for the test with
%    |\if:w|.  What a mistake Don made by making this a
%    feature of |\cs:w|. If I'm not totally mistaken this
%    feature alone has cost him more than 600\$ for bug-checks.
%    \begin{macrocode}
\def_long_new:Npn \cs_free:cTF #1{
   \exp_after:NN
      \if_meaning:NN
            \cs:w#1\cs_end:
            \scan_stop:
         \exp_after:NN \use_choice_i:nn
      \else:
         \exp_after:NN \use_choice_ii:nn
      \fi:}
\def_long_new:Npn \cs_free:cF #1{\exp_after:NN
  \if_meaning:NN \cs:w#1\cs_end: \scan_stop:
  \exp_after:NN \use_none:nn \fi: \use:n}
\def_long_new:Npn \cs_free:cT #1{\exp_after:NN
  \if_meaning:NN \cs:w#1\cs_end: \scan_stop:
  \else:
  \exp_after:NN \use_none:nn \fi: \use:n}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsection{Freeing memory}
%
% \begin{macro}{\gundefine:N }
%    The following function is used to free the main memory from the
%    definition of some function that isn't in use any longer.
%    \begin{macrocode}
\def_new:Npn \gundefine:N #1{\glet:NN #1\c_undefined}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Gobbling tokens from input}
%
% \begin{macro}{\use_none:n}
% \begin{macro}{\use_none:nn}
% \begin{macro}{\use_none:nnn}
% \begin{macro}{\use_none:nnnn}
%    To gobble tokens from the input we use a standard naming
%    convention: the number of tokens gobbled is given by the number
%    of |n|'s following the |:| in the name.
%    \begin{macrocode}
\def_long_new:Npn \use_none:n #1{}
\def_long_new:Npn \use_none:nn #1#2{}
\def_long_new:Npn \use_none:nnn #1{\use_none:nn}
\def_long_new:Npn \use_none:nnnn
    {\exp_after:NN\use_none:nn \use_none:nn}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsection{Selecting tokens}
%
% \begin{macro}{\use:n}
%    This macro grabs its argument and returns it back to the input
%    (with outer braces removed).
%    \begin{macrocode}
\def_long_new:Npn \use:n #1{#1}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\use:c}
% \begin{macro}{\use:cc}
%    This macro grabs its argument and returns a csname from it.
%    \begin{macrocode}
\def_new:Npn \use:c #1{\cs:w #1\cs_end:}

%    \end{macrocode}
%    THE NAME IS COMPLETELY WRONG!!!!!
%    \begin{macrocode}
\def_new:Npn \use:cc #1#2
  {\cs:w #1\exp_after:NN\cs_end:\cs:w #2\cs_end:}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\use_choice_i:nn}
% \begin{macro}{\use_choice_ii:nn}
%    These macros are needed to provide functions with true and false
%    cases, as introduced by Michael some time ago. By using
%    |\exp_after:NN| |\use_choice_i:nn | |\else:| constructions it
%    is possible to write code where the true or false case is able to
%    access the following tokens from the input stream, which is not
%    possible if the |\c_true| syntax is used.
%    \begin{macrocode}
\def_long_new:Npn \use_choice_i:nn #1#2{#1}
\def_long_new:Npn \use_choice_ii:nn #1#2{#2}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
%  \begin{macro}{\use_choice_i:nnn}
%  \begin{macro}{\use_choice_ii:nnn}
%  \begin{macro}{\use_choice_iii:nnn}
%    We also need something for picking up arguments from a longer
%    list.  
%    \begin{macrocode}
\def_long_new:Npn\use_choice_i:nnn#1#2#3{#1}
\def_long_new:Npn\use_choice_ii:nnn#1#2#3{#2}
\def_long_new:Npn\use_choice_iii:nnn#1#2#3{#3}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%
%
% \subsection{Scratch functions}
%
% \begin{macro}{\gtmp:w}
%    This function is for global scratch definitions that are used
%    immediately afterwards. It should be used when we need a function
%    that operates on input, i.e.\ has arguments. If we want to save
%    only some tokens for later use, token-list scratch variables
%    should be used.
%    \begin{macrocode}
\def_new:Npn \gtmp:w {}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tmp:w}
%    This is a local version of the previous function.
%    \begin{macrocode}
\def_new:Npn \tmp:w {}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\use_noop:}
%    I don't think this function belongs here, but one place is as
%    good as any other. I want to use this function when I want to
%    express `no operation'.
%    \begin{macrocode}
\def_new:Npn \use_noop: {}
%    \end{macrocode}
% \end{macro}
%
% \section{Strings and input stream token lists}
%
% \begin{macro}{\cs_to_str:N}
%   This converts a control sequence into the character string of its 
%   name, removing the leading escape character.
%    \begin{macrocode}
\def_new:Npn \cs_to_str:N {\exp_after:NN\use_none:n \token_to_string:N}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tlist_eq:nnTF}
% \begin{macro}{\tlist_eq:onTF}
%    |\tlist_eq:nnTF| tests whether the first two arguments are equal and
%    executes either its third or fourth argument.
%    |\tlist_eq:onTF| expands its first argument once.
%    \begin{macrocode}
\def_new:Npn \tlist_eq:nnTF #1#2{
  \tlp_gset:Nn \g_testa_tlp {#1}
  \tlp_gset:Nn \g_testb_tlp {#2}
  \if_meaning:NN\g_testa_tlp \g_testb_tlp
    \exp_after:NN\use_choice_i:nn \else:
    \exp_after:NN\use_choice_ii:nn \fi:}
\def_new:Npn \tlist_eq:onTF {\exp_args:No \tlist_eq:nnTF}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\tlist_empty:nTF}
% \begin{macro}{\tlist_empty:nF}
%    These test whether a token list is empty and act accordingly.
%    \begin{macrocode}
\def_new:Npn \tlist_empty:nTF #1{\tlp_gset:Nn \g_testa_tlp {#1}
  \if_meaning:NN\g_testa_tlp \c_empty_tlp
      \exp_after:NN \use_choice_i:nn \else:
      \exp_after:NN \use_choice_ii:nn \fi:}
\def_new:Npn \tlist_empty:nF #1{\tlp_gset:Nn \g_testa_tlp {#1}
  \if_meaning:NN\g_testa_tlp \c_empty_tlp
      \exp_after:NN \use_none:nn \fi: \use:n}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\str_eq_p:nn}
%   Takes 2 lists of characters as arguments and expands into
%   |\c_true| if they are equal, and |\c_false| otherwise. I'm
%    pretty sure that the non expandable version above is faster, but
%    it's not expandable so we also include this one.
%    \begin{macrocode}
\def_new:Npn \str_eq_p:nn #1#2{\str_eq_p_aux:w #1\scan_stop:\\#2\scan_stop:\\}
\def_new:Npn \str_eq_p_aux:w #1#2\\#3#4\\{
  \if_meaning:NN#1#3
    \if_meaning:NN#1\scan_stop:\c_true \else:
    \if_meaning:NN#3\scan_stop:\c_false \else:
    \str_eq_p_aux:w #2\\#4\\\fi:\fi:
  \else:\c_false \fi:}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cs_eq_p:NN}
%   An application of the above function, already streamlined for
%   speed, so I put it in here.  It takes two control sequences as
%   arguments and expands into true iff they have the same name.
%    \begin{macrocode}
\def:Npn \cs_eq_p:NN #1#2{
  \exp_after:NN\exp_after:NN
  \exp_after:NN\str_eq_p_aux:w
  \exp_after:NN\token_to_string:N
  \exp_after:NN#1
  \exp_after:NN\scan_stop:
  \exp_after:NN\\
  \token_to_string:N#2\scan_stop:\\}
%    \end{macrocode}
% \end{macro}
%
%  Finally some code that is needed as we 
%  do not distribute the file module at the moment (so we simply define
%  the needed function via an existing \LaTeX{} command) and some other
%  stuff which was set up elsewhere, in undistributed modules.
%    \begin{macrocode}
\def_new:Npn\file_not_found:nTF #1#2#3{\IfFileExists{#1}{#3}{#2}}
%    \end{macrocode}
%
%    
% \section{Predicates and conditionals}
% \label{sec:predicates}
%
% \LaTeX3 has three concepts for conditional flow processing:
% \begin{enumerate}
%
% \item
%   Functions that carry out a test an then execute, depending on its
% result, either the code supplied in the <true arg> or the <false
% arg>. These arguments are denoted with "T" and "F" repectively. An
% example would be
% \begin{quote}
%  "\cs_free:cTF{abc}{...}{...}"
% \end{quote}
% a function that will turn the first argument into a control sequence
% (since its marked as "c") then checks whether this control sequence is
% still free and then depending on the result carry out the code in the
% second argument (true case) or in the third argument (false case).
%
% \item
%   Functions that return a special type of boolean value which can be
% tested by the function "\if:w". All functions of this type
% have names that end with "_p" in the description part. For example
% \begin{quote}
%  "\cs_free_p:N"
% \end{quote}
% would be a predicate function for the same type of test as the
% function above. It would return `true' if its argument (a single token
% denoted by "N") is still free for definition. It would be used in
% constructions like
% \begin{quote}
%  "\if:w \cs_free_p:N \l_foo_bar ... \else: ... \fi:"
% \end{quote}
%
% \item
%   Actually there is a third one, namely the original concept used in
% plain \TeX{}. This belongs to the second form but needs further
% thoughts.
% \end{enumerate}
%
% Important to note is that conditionals with <true code> and/or <false
% code> are always defined in a way that the code of the chosen
% alternative can operate on following tokens in the input stream while
% the predicate implementations always have an "\else:" or "\fi:"
% interfering. This can be important in scanner implementations.
%
% \subsection{Variables}
%
% \begin{variable}{%
%                  \c_true | \c_false |
% }
%  Constants that represend `true' or `false', respectively. Used to
% implement predicates.
% \end{variable}
%
% \begin{variable}{\l_testa_tlp |
%                  \l_testb_tlp |
%                  \g_testa_tlp |
%                  \g_testb_tlp |
% }
% All conditionals and predicates never use tmp variables. Instead they
% use the following reserved variables.
% \end{variable}
%
% \subsection{Predicate implementation}
%
%    I think Michael originated the idea of expandable boolean tests.
%    They must expand into either \texttt{TT} or \texttt{TF} and are
%    tested using |\if:w|.
%
%
% \begin{macro}{\c_true}
% \begin{macro}{\c_false}
%    Here are the canonical boolean values.
%    \begin{macrocode}
\def_new:Npn \c_true  {TT}
\def_new:Npn \c_false {TF}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</package>
%<*showmemory>
\showMemUsage
%</showmemory>
%    \end{macrocode}
%