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
|
% \iffalse
%% File: l3candidates.dtx Copyright (C) 2005-2010 LaTeX3 project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version. The latest version
%% of this license is in the file
%%
%% http://www.latex-project.org/lppl.txt
%%
%% This file is part of the ``expl3 bundle'' (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%% http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%% Snapshots taken from the repository represent work in progress and may
%% not work or may contain conflicting material! We therefore ask
%% people _not_ to put them into distributions, archives, etc. without
%% prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%
%<*driver|package>
\RequirePackage{expl3}
%</driver|package>
%\fi
\errorcontextlines=999
\GetIdInfo$Id: l3candidates.dtx 2137 2011-01-23 23:13:58Z will $
{L3 Experimental 'extras' module}
%\iffalse
%<*driver>
%\fi
\ProvidesFile{\filename.\filenameext}
[\filedate\space v\fileversion\space\filedescription]
%\iffalse
\documentclass[full]{l3doc}
\begin{document}
\DocInput{l3candidates.dtx}
\end{document}
%</driver>
% \fi
%
%
% \title{The \textsf{l3candidates} package\thanks{This file
% has version number \fileversion, last
% revised \filedate.}}
% \author{\Team}
% \date{\filedate}
% \maketitle
%
% \begin{documentation}
%
% \section{Intro}
%
% This \emph{experimental} package contains additions to the \pkg{expl3}
% programming language before they're added to the `official' packages.
% The functions defined within will fall into three categories:
% \begin{itemize}
% \item Will be added directly to a corresponding \pkg{expl3} module at some stage.
% \item Will be re-worked and added to a module with slightly different name or behaviour.
% \item Will eventually vanish.
% \end{itemize}
% Beware, then, that loading this package and using its contents may cause your
% code to break down the track.
%
% \section{Functions for characters}
%
%
% \begin{function}{\char_active_set:Npn|\char_active_gset:Npn}
% \begin{syntax}
% |\char_active_set:Npn|~<char>~<param>~<definition>
% \end{syntax}
% Makes <char> active and gives it the definition specified as if it were
% a function.
% \end{function}
%
% \begin{function}{\char_active_set_eq:NN|\char_active_gset_eq:NN}
% \begin{syntax}
% |\char_active_set:Npn|~<char>~<function>
% \end{syntax}
% Makes <char> active and makes it equal to <function>.
% (Or another active char, etc.)
% \end{function}
%
%
%
% \section{Functions for token lists}
%
% \begin{function}{\tl_trim_spaces:n|\tl_trim_spaces:N}
% \begin{syntax}
% |\tl_trim_spaces:n|~|{|<token list>|}|
% |\tl_trim_spaces:n|~|{|\verb*| |<token list>\verb*| ||}|
% |\tl_trim_spaces:N| <tl var.>
% \end{syntax}
% After a full expansion, |\tl_trim_space:n| returns <token list> stripped
% of any preceeding and trailing spaces.
% For example, to set a token list variable to a trim-spaced argument,
% use something like this:
% \begin{verbatim}
% \tl_set:Nx \l_tmpa_tl { \tl_trim_spaces:n {#1} }
% \end{verbatim}
% |\tl_trim_spaces:N| removes preceeding and trailing spaces from the
% contents of its token list variable argument.
% \end{function}
%
%
% \section{Functions for sequences}
%
%
% \begin{function}{\seq_mapthread_function:NNN}
% \begin{syntax}
% "\seq_mapthread_function:NNN" <seq1> <seq2> <function>
% \end{syntax}
% Applies to <function> as arguments each pair of
% corresponding elements within <seq1> and <seq2> (which must have the same
% number of elements).
%
% For example, if "\l_mya_seq" contains "a", "b", "c" and
% "\l_myb_seq" contains "x", "y", "z", then
% \begin{verbatim}
% \seq_mapthread_function:NNN \l_mya_seq \l_myb_seq \my_func:nn
% \end{verbatim}
% is the same as executing
% \begin{verbatim}
% \my_func:nn {a} {x}
% \my_func:nn {b} {y}
% \my_func:nn {c} {z}
% \end{verbatim}
% \end{function}
%
%
% \begin{function}{\seq_get_length:NN}
% \begin{syntax}
% |\seq_get_length:NN| <seq> <int>
% \end{syntax}
% Sets <int> to the number of elements in <seq>.
% \end{function}
%
%
% \begin{function}{\seq_set_from_clist:NN|\seq_set_from_clist:Nn|
% \seq_gset_from_clist:NN|\seq_gset_from_clist:Nn}
% \begin{syntax}
% "\seq_set_from_clist:Nn" <seq> \Arg{clist}
% "\seq_set_from_clist:NN" <seq> <clist variable>
% \end{syntax}
% Transforms comma-list <clist> into sequence <seq>.
% \end{function}
%
%
% \section{Functions for comma-lists}
%
% \begin{function}{\clist_set_from_seq:NN|\clist_gset_from_seq:NN}
% \begin{syntax}
% "\clist_set_from_seq:NN" <clist> <seq>
% \end{syntax}
% Transforms sequence <seq> into comma-list variable <clist>.
% \end{function}
%
%
% \begin{function}{\clist_if_in_numrange:nn / (TF)}
% \begin{syntax}
% "\clist_if_in_numrange:nnTF" \Arg{intexpr} \Arg{numrange}
% \Arg{true} \Arg{false}
% \end{syntax}
% When parsing user input to specify whether numerical elements
% should be displayed/operated upon, it is common to use input such as
% `|-4,7-11,17-|' to indicate ranged but sometimes discontiguous sets.
% (In this particular example, we'd be specifying
% $x\le4 \wedge 7\le x\le 11 \wedge x\ge 17$.)
% This function can be used to test whether the input number
% (actually <intexpr>) exists within the specified <numrange>.
%
% <numrange> need not be sorted and may contain overlapping ranges.
% Numbers inside ranges may consist of integer expressions or anything
% considered by \TeX\ to be a number
% (such as `|`\A|', which is decimal number 65).
%
% This function could probably be made expandable with sufficient effort.
% \end{function}
%
%
%
% \begin{function}{\clist_length:N / (EXP)|\clist_length:n / (EXP)}
% \begin{syntax}
% "\clist_length:N" <clist>
% \end{syntax}
% Expands to the number of elements in the <clist>.
% \end{function}
%
% \begin{function}{\clist_element:Nn / (EXP)|\clist_element:nn / (EXP)}
% \begin{syntax}
% "\clist_element:Nn" <clist> \Arg{i}
% "\clist_element:nn" |{| <c0>|,| <c1>|,|\dots|,| <c$_{n-\1}$>|,| <c$_n$> |}| \Arg{i}
% "\clist_element:nn" |{| <c$_{-n-\1}$>|,| <c$_{-n}$>|,|\dots|,| <c$_{-\2}$>|,| <c$_{-\1}$> |}| \Arg{i}
% \end{syntax}
% Expands to <c$_i$>, the <i>-th element of the <clist>.
% The indexing counts from zero, and negative indices count from the end.
% (Negative indexing is slightly less efficent since the length of the list
% must first be calculated.)
% \end{function}
%
%
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{Implementation}
%
%
% \begin{macrocode}
%<*package>
\ProvidesExplPackage
{\filename}{\filedate}{\fileversion}{\filedescription}
\package_check_loaded_expl:
%</package>
%<*initex|package>
% \end{macrocode}
%
%
% \begin{macro}{\char_active_set:Npn,\char_active_set:Npx,\char_active_gset:Npn,\char_active_gset:Npx}
% \begin{macro}{\char_active_set_eq:NN,\char_active_gset_eq:NN}
% \begin{macrocode}
\group_begin:
\char_make_active:N \^^@
\cs_set:Npn \char_tmp:NN {
\cs_gnew:Npn #1 ##1 {
\char_make_active:n {`##1}
\group_begin:
\tex_lccode:D `\^^@ `##1
\tex_lowercase:D{\group_end: #2 ^^@}
}
}
\char_tmp:NN \char_active_set:Npn \cs_set:Npn
\char_tmp:NN \char_active_set:Npx \cs_set:Npx
\char_tmp:NN \char_active_gset:Npn \cs_gset:Npn
\char_tmp:NN \char_active_gset:Npx \cs_gset:Npx
\char_tmp:NN \char_active_set_eq:NN \cs_set_eq:NN
\char_tmp:NN \char_active_gset_eq:NN \cs_gset_eq:NN
\group_end:
% \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
% \begin{macro}{\tl_trim_spaces:n,\tl_trim_spaces:N}
% \begin{macrocode}
\cs_new:Npn \tl_trim_spaces:n #1 {
\exp_not:n \exp_after:wN \exp_after:wN \exp_after:wN
{ \tl_trim_spaces_exp:n {#1} }
}
\cs_new:Npn \tl_trim_spaces:N #1 {
\tl_set:Nx #1 { \exp_after:wN \tl_trim_spaces:n \exp_after:wN {#1} }
}
% \end{macrocode}
%
% \begin{macrocode}
\char_make_math_shift:N \Q
\cs_new:Npn \tl_trim_spaces_exp:n #1 {
\tex_romannumeral:D -`\q \tl_trim_spaces_aux_i:w \exp_not:N #1 Q~Q
}
\cs_new:Npn \tl_trim_spaces_aux_i:w #1~Q { \tl_trim_spaces_aux_ii:w #1 Q }
\cs_new:Npn \tl_trim_spaces_aux_ii:w #1 Q #2 {#1}
\char_make_letter:N \Q
% \end{macrocode}
% \end{macro}
%
%
%
%
% \begin{macro}{\seq_mapthread_function:NNN}
% \begin{macrocode}
\seq_new:N \l_tmpb_seq
% \end{macrocode}
%
%
% \begin{macro}{\seq_get_length:NN}
% \begin{macrocode}
\cs_new:Nn \seq_get_length:NN {
\int_zero:N #1
\seq_map_inline:Nn #2 { \int_incr:N #1}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
\cs_new:Nn \seq_mapthread_function:NNN {
\seq_get_length:NN \l_tmpa_int #1
\seq_get_length:NN \l_tmpb_int #2
\int_compare:nTF { \l_tmpa_int == \l_tmpb_int }{
\seq_set_eq:NN \l_tmpa_seq #1
\seq_set_eq:NN \l_tmpb_seq #2
\prg_stepwise_inline:nnnn{1}{1}{\l_tmpa_int}{
\seq_pop:NN \l_tmpa_seq \l_kernel_tmpa_tl
\seq_pop:NN \l_tmpb_seq \l_kernel_tmpb_tl
\exp_args:NVV #3 \l_kernel_tmpa_tl \l_kernel_tmpb_tl
}
}{\ERROR}
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\seq_set_from_clist:Nn,\seq_set_from_clist:NN}
% \begin{macro}{\seq_gset_from_clist:Nn,\seq_gset_from_clist:NN}
% \begin{macrocode}
\cs_set:Npn \seq_set_from_clist:Nn #1#2 {
\seq_clear:N #1
\clist_map_inline:nn {#2} { \seq_put_right:Nn #1 {##1} }
}
\cs_set:Npn \seq_set_from_clist:NN #1#2 {
\seq_clear:N #1
\clist_map_inline:Nn #2 { \seq_put_right:Nn #1 {##1} }
}
\cs_set:Npn \seq_gset_from_clist:Nn #1#2 {
\seq_gclear:N #1
\clist_map_inline:nn {#2} { \seq_gput_right:Nn #1 {##1} }
}
\cs_set:Npn \seq_gset_from_clist:NN #1#2 {
\seq_gclear:N #1
\clist_map_inline:Nn #2 { \seq_gput_right:Nn #1 {##1} }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
% \begin{macro}{\clist_set_from_seq:NN,\clist_gset_from_seq:NN}
% I assume this technique is more efficient than looping through each element
% and using "\clist_put_right:Nn" (Will).
% \begin{macrocode}
\cs_new:Npn \clist_set_from_seq_aux:NNN #1#2#3 {
\cs_set:Npn \seq_elt:w ##1 \seq_elt_end: ##2 {
\exp_not:n {##1}
\quark_if_nil:NF ##2 { , ##2 }
}
#1 #2 { #3 \q_nil }
}
\cs_new:Npn \clist_set_from_seq:NN { \clist_set_from_seq_aux:NNN \tl_set:Nx }
\cs_new:Npn \clist_gset_from_seq:NN { \clist_set_from_seq_aux:NNN \tl_gset:Nx }
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}[TF]{\clist_if_in_numrange:nn}
% A `numrange' is like |-2,5-8,17-| (can be unsorted).
% \begin{macrocode}
\prg_new_conditional:Nnn \clist_if_in_numrange:nn {TF,T,F} {
\bool_set_false:N \l_clist_numrange_bool
\clist_map_inline:nn {#2} {
\_clist_numrange_parse:nw {#1} ##1 - \q_nil - \q_stop
}
\bool_if:NTF \l_clist_numrange_bool \prg_return_true: \prg_return_false:
}
\cs_set:Npn \_clist_numrange_success: {
\bool_set_true:N \l_clist_numrange_bool
\clist_map_break:
}
\cs_set:Npn \_clist_numrange_parse:nw #1 #2 - #3 - #4 \q_stop {
\tl_if_empty:nTF {#4}{
\int_compare:nT {#1=#2} {\_clist_numrange_success:}
}{
\tl_if_empty:nTF {#3}{
\int_compare:nT {#1>=#2} {\_clist_numrange_success:}
}{
\tl_if_empty:nTF {#2}{
\int_compare:nT {#1<=#3} {\_clist_numrange_success:}
}{
\int_compare:nT {#1>=#2} {
\int_compare:nT {#1<=#3}
{\_clist_numrange_success:}
}
}
}
}
}
\bool_new:N \l_clist_numrange_bool
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\clist_length:N,\clist_length:n}
% \begin{macrocode}
\cs_new:Npn \clist_length:N {
\exp_args:No \clist_length:n
}
\cs_new:Npn \clist_length:n #1 {
\int_eval:n {
0 \clist_map_function:nN {#1} \tl_elt_count_aux:n
}
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\clist_element:Nn,\clist_element:nn}
% \begin{macrocode}
\cs_new:Npn \clist_element:Nn #1 {
\exp_args:No \clist_element:nn #1
}
\cs_new:Npn \clist_element:nn #1#2 {
% \end{macrocode}
% When negative indexing is used, calculate the length and subtract.
% This incurs a minor performance penalty, of course, but it's nothing to
% worry about.
% \begin{macrocode}
\int_compare:nNnTF {#2} < {0}
{
\exp_args:Nf \clist_element_aux:nw
{\clist_length:n {#1}+#2} #1 , \q_recursion_tail , \q_recursion_stop
}
{
\clist_element_aux:nw {#2} #1 , \q_recursion_tail , \q_recursion_stop
}
% \end{macrocode}
% Shorter but less readable version of the code block above:
% \begin{verbatim}
% \exp_args:Nf \clist_element_aux:nnw
% { #2 \int_compare:nNnT {#2} < {0} { +\clist_length:n {#1} } }
% #1 , \q_recursion_tail , \q_recursion_stop
% \end{verbatim}
% Note in both cases I've used |\exp_args:Nf| directly, since I'm starting
% to think that adding too many control sequences is bad for \TeX's
% performance.
% \begin{macrocode}
}
\cs_new:Npn \clist_element_aux:nw #1#2, {
\quark_if_recursion_tail_stop:n {#2}
\int_compare:nNnTF {#1} = {0}
{
\use_i_delimit_by_q_recursion_stop:nw {#2}
}
{
\clist_element_aux:nw {#1-1}
}
}
% \end{macrocode}
% \end{macro}
%
%
%
%
% \begin{macrocode}
%</initex|package>
% \end{macrocode}
%
% \begin{macrocode}
%<*showmemory>
\showMemUsage
%</showmemory>
% \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex
%
% \endinput
|