Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3clist.dtx
4 %
5 % Copyright (C) 2004-2011 Frank Mittelbach, The LaTeX Project
6 % (C) 2012-2026 The LaTeX Project
7 %
8 % It may be distributed and/or modified under the conditions of the
9 % LaTeX Project Public License (LPPL), either version 1.3c of this
10 % license or (at your option) any later version. The latest version
11 % of this license is in the file
12 %
13 % https://www.latex-project.org/lppl.txt
14 %
15 % This file is part of the "l3kernel bundle" (The Work in LPPL)
16 % and all files in that bundle must be distributed together.
17 %
18 % -----------------------------------------------------------------------
19 %
20 % The development version of the bundle can be found at
21 %
22 % https://github.com/latex3/latex3
23 %
24 % for those people who are interested.
25 %
26 %<*driver>
27 \documentclass[full,kernel]{l3doc}
28 \begin{document}
29 \DocInput{\jobname.dtx}
30 \end{document}
31 %</driver>
32 % \fi
33 %
34 % \title{^^A
35 % The \pkg{l3clist} module\\ Comma separated lists^^A
36 % }
37 %
38 % \author{^^A
39 % The \LaTeX{} Project\thanks
40 % {^^A
41 % E-mail:
42 % \href{mailto:[email protected]}
43 % {[email protected]}^^A
44 % }^^A
45 % }
46 %
47 % \date{Released 2026-05-15}
48 %
49 % \maketitle
50 %
51 % \begin{documentation}
52 %
53 % Comma lists (in short, |clist|) contain ordered data where items can
54 % be added to the left or right end of the list. This data type allows
55 % basic list
56 % manipulations such as adding/removing items, applying a function to
57 % every item, removing duplicate items, extracting a given item, using
58 % the comma list with specified separators, and so on. Sequences
59 % (defined in \pkg{l3seq}) are safer, faster, and provide more features,
60 % so they should often be preferred to comma lists. Comma lists are
61 % mostly useful when interfacing with \LaTeXe{} or other code that
62 % expects or provides items separated by commas.
63 %
64 % Several items can be added at once. To ease input of comma lists from
65 % data provided by a user outside an \cs{ExplSyntaxOn} \ldots{}
66 % \cs{ExplSyntaxOff} block, spaces are removed from both sides of each
67 % comma-delimited argument upon input. Blank arguments are ignored, to
68 % allow for trailing commas or repeated commas (which may otherwise
69 % arise when concatenating comma lists \enquote{by hand}). In addition,
70 % a set of braces is removed if the result of space-trimming is braced:
71 % this allows the storage of any item in a comma list. For instance,
72 % \begin{verbatim}
73 % \clist_new:N \l_my_clist
74 % \clist_put_left:Nn \l_my_clist { ~a~ , ~{b}~ , c~\d }
75 % \clist_put_right:Nn \l_my_clist { ~{e~} , , {{f}} , }
76 % \end{verbatim}
77 % results in |\l_my_clist| containing |a,b,c~\d,{e~},{{f}}| namely the
78 % five items |a|, |b|, |c~\d|, |e~| and~|{f}|. Comma lists normally do
79 % not contain empty or blank items so the following gives an empty comma list:
80 % \begin{verbatim}
81 % \clist_clear_new:N \l_my_clist
82 % \clist_set:Nn \l_my_clist { , ~ , , }
83 % \clist_if_empty:NTF \l_my_clist { true } { false }
84 % \end{verbatim}
85 % and it leaves \texttt{true} in the input stream. To include an
86 % \enquote{unsafe} item (empty, or one that contains a comma, or starts
87 % or ends with a space, or is a single brace group), surround it with
88 % braces.
89 %
90 % Any |n|-type token list is a valid comma list input for \pkg{l3clist}
91 % functions, which will split the token list at every comma and process
92 % the items as described above. On the other hand, |N|-type functions
93 % expect comma list variables, which are particular token list variables
94 % in which this processing of items (and removal of blank items) has
95 % already occurred. Because comma list variables are token list
96 % variables, expanding them once yields their items separated by commas,
97 % and \pkg{l3tl} functions such as \cs{tl_show:N} can be applied to
98 % them. (These functions often have \pkg{l3clist} analogues, which
99 % should be preferred.)
100 %
101 % Almost all operations on comma lists are
102 % noticeably slower than those on sequences so converting the data to
103 % sequences using \cs{seq_set_from_clist:Nn} (see \pkg{l3seq}) may be
104 % advisable if speed is important. The exception is that
105 % \cs{clist_if_in:NnTF} and \cs{clist_remove_duplicates:N} may be faster
106 % than their sequence analogues for large lists. However, these
107 % functions work slowly for \enquote{unsafe} items that must be braced,
108 % and may produce errors when their argument contains |{|, |}| or |#|
109 % (assuming the usual \TeX{} category codes apply). The sequence
110 % data type should thus certainly be preferred to comma lists to store
111 % such items.
112 %
113 % \section{Creating and initializing comma lists}
114 %
115 % \begin{function}{\clist_new:N, \clist_new:c}
116 % \begin{syntax}
117 % \cs{clist_new:N} \meta{clist~var}
118 % \end{syntax}
119 % Creates a new \meta{clist~var} or raises an error if the name is
120 % already taken. The declaration is global. The \meta{clist~var}
121 % initially contains no items.
122 % \end{function}
123 %
124 % \begin{function}[added = 2014-07-05]
125 % {
126 % \clist_const:Nn, \clist_const:Ne,
127 % \clist_const:cn, \clist_const:ce
128 % }
129 % \begin{syntax}
130 % \cs{clist_const:Nn} \meta{clist~var} \Arg{comma list}
131 % \end{syntax}
132 % Creates a new constant \meta{clist~var} or raises an error
133 % if the name is already taken. The value of the
134 % \meta{clist~var} is set globally to the
135 % \meta{comma list}.
136 % \end{function}
137 %
138 % \begin{function}
139 % {\clist_clear:N, \clist_clear:c, \clist_gclear:N, \clist_gclear:c}
140 % \begin{syntax}
141 % \cs{clist_clear:N} \meta{clist~var}
142 % \end{syntax}
143 % Clears all items from the \meta{clist~var}.
144 % \end{function}
145 %
146 % \begin{function}
147 % {
148 % \clist_clear_new:N, \clist_clear_new:c,
149 % \clist_gclear_new:N, \clist_gclear_new:c
150 % }
151 % \begin{syntax}
152 % \cs{clist_clear_new:N} \meta{clist~var}
153 % \end{syntax}
154 % Ensures that the \meta{clist~var} exists globally by applying
155 % \cs{clist_new:N} if necessary, then applies
156 % \cs[index=clist_clear:N]{clist_(g)clear:N} to leave
157 % the list empty.
158 % \end{function}
159 %
160 % \begin{function}
161 % {
162 % \clist_set_eq:NN, \clist_set_eq:cN,
163 % \clist_set_eq:Nc, \clist_set_eq:cc,
164 % \clist_gset_eq:NN, \clist_gset_eq:cN,
165 % \clist_gset_eq:Nc, \clist_gset_eq:cc
166 % }
167 % \begin{syntax}
168 % \cs{clist_set_eq:NN} \meta{clist~var_1} \meta{clist~var_2}
169 % \end{syntax}
170 % Sets the content of \meta{clist~var_1} equal to that of
171 % \meta{clist~var_2}. To set a token list variable equal to a comma
172 % list variable, use \cs{tl_set_eq:NN}. Conversely, setting a comma
173 % list variable to a token list is unadvisable unless one checks
174 % space-trimming and related issues.
175 % \end{function}
176 %
177 % \begin{function}[added = 2014-07-17]
178 % {
179 % \clist_set_from_seq:NN, \clist_set_from_seq:cN,
180 % \clist_set_from_seq:Nc, \clist_set_from_seq:cc,
181 % \clist_gset_from_seq:NN, \clist_gset_from_seq:cN,
182 % \clist_gset_from_seq:Nc, \clist_gset_from_seq:cc
183 % }
184 % \begin{syntax}
185 % \cs{clist_set_from_seq:NN} \meta{clist~var} \meta{seq~var}
186 % \end{syntax}
187 % Converts the data in the \meta{seq~var} into a \meta{clist~var}:
188 % the original \meta{seq~var} is unchanged.
189 % Items which contain either spaces or commas are surrounded by braces.
190 % \end{function}
191 %
192 % \begin{function}
193 % {
194 % \clist_concat:NNN, \clist_concat:ccc,
195 % \clist_gconcat:NNN, \clist_gconcat:ccc
196 % }
197 % \begin{syntax}
198 % \cs{clist_concat:NNN} \meta{clist~var_1} \meta{clist~var_2} \meta{clist~var_3}
199 % \end{syntax}
200 % Concatenates the content of \meta{clist~var_2} and \meta{clist~var_3}
201 % together and saves the result in \meta{clist~var_1}. The items in
202 % \meta{clist~var_2} are placed at the left side of the new comma list.
203 % \end{function}
204 %
205 % \begin{function}[EXP, pTF, added=2012-03-03]
206 % {\clist_if_exist:N, \clist_if_exist:c}
207 % \begin{syntax}
208 % \cs{clist_if_exist_p:N} \meta{clist~var}
209 % \cs{clist_if_exist:NTF} \meta{clist~var} \Arg{true code} \Arg{false code}
210 % \end{syntax}
211 % Tests whether the \meta{clist~var} is currently defined. This does
212 % not check that the \meta{clist~var} really is a comma list.
213 % \end{function}
214 %
215 % \section{Adding data to comma lists}
216 %
217 % \begin{function}[added = 2011-09-06]
218 % {
219 % \clist_set:Nn, \clist_set:NV, \clist_set:Nv, \clist_set:Ne,
220 % \clist_set:No,
221 % \clist_set:cn, \clist_set:cV, \clist_set:cv, \clist_set:ce,
222 % \clist_set:co,
223 % \clist_gset:Nn, \clist_gset:NV, \clist_gset:Nv, \clist_gset:Ne,
224 % \clist_gset:No,
225 % \clist_gset:cn, \clist_gset:cV, \clist_gset:cv, \clist_gset:ce,
226 % \clist_gset:co
227 % }
228 % \begin{syntax}
229 % \cs{clist_set:Nn} \meta{clist~var} |{|\meta{item_1},\ldots{},\meta{item_n}|}|
230 % \end{syntax}
231 % Sets \meta{clist~var} to contain the \meta{items},
232 % removing any previous content from the variable.
233 % Blank items are omitted, spaces are removed from both sides of each
234 % item, then a set of braces is removed if the resulting space-trimmed
235 % item is braced.
236 % To store some \meta{tokens} as a single \meta{item} even if the
237 % \meta{tokens} contain commas or spaces, add a set of braces:
238 % \cs{clist_set:Nn} \meta{clist~var} |{| \Arg{tokens} |}|.
239 % \end{function}
240 %
241 % \begin{function}[updated = 2011-09-05]
242 % {
243 % \clist_put_left:Nn, \clist_put_left:NV,
244 % \clist_put_left:Nv, \clist_put_left:Ne,
245 % \clist_put_left:No,
246 % \clist_put_left:cn, \clist_put_left:cV,
247 % \clist_put_left:cv, \clist_put_left:ce,
248 % \clist_put_left:co,
249 % \clist_gput_left:Nn, \clist_gput_left:NV,
250 % \clist_gput_left:Nv, \clist_gput_left:Ne,
251 % \clist_gput_left:No,
252 % \clist_gput_left:cn, \clist_gput_left:cV,
253 % \clist_gput_left:cv, \clist_gput_left:ce,
254 % \clist_gput_left:co
255 % }
256 % \begin{syntax}
257 % \cs{clist_put_left:Nn} \meta{clist~var} |{|\meta{item_1},\ldots{},\meta{item_n}|}|
258 % \end{syntax}
259 % Appends the \meta{items} to the left of the \meta{clist~var}.
260 % Blank items are omitted, spaces are removed from both sides of each
261 % item, then a set of braces is removed if the resulting space-trimmed
262 % item is braced.
263 % To append some \meta{tokens} as a single \meta{item} even if the
264 % \meta{tokens} contain commas or spaces, add a set of braces:
265 % \cs{clist_put_left:Nn} \meta{clist~var} |{| \Arg{tokens} |}|.
266 % \end{function}
267 %
268 % \begin{function}[updated = 2011-09-05]
269 % {
270 % \clist_put_right:Nn, \clist_put_right:NV,
271 % \clist_put_right:Nv, \clist_put_right:Ne,
272 % \clist_put_right:No,
273 % \clist_put_right:cn, \clist_put_right:cV,
274 % \clist_put_right:cv, \clist_put_right:ce,
275 % \clist_put_right:co,
276 % \clist_gput_right:Nn, \clist_gput_right:NV,
277 % \clist_gput_right:Nv, \clist_gput_right:Ne,
278 % \clist_gput_right:No,
279 % \clist_gput_right:cn, \clist_gput_right:cV,
280 % \clist_gput_right:cv, \clist_gput_right:ce,
281 % \clist_gput_right:co
282 % }
283 % \begin{syntax}
284 % \cs{clist_put_right:Nn} \meta{clist~var} |{|\meta{item_1},\ldots{},\meta{item_n}|}|
285 % \end{syntax}
286 % Appends the \meta{items} to the right of the \meta{clist~var}.
287 % Blank items are omitted, spaces are removed from both sides of each
288 % item, then a set of braces is removed if the resulting space-trimmed
289 % item is braced.
290 % To append some \meta{tokens} as a single \meta{item} even if the
291 % \meta{tokens} contain commas or spaces, add a set of braces:
292 % \cs{clist_put_right:Nn} \meta{clist~var} |{| \Arg{tokens} |}|.
293 % \end{function}
294 %
295 % \section{Modifying comma lists}
296 %
297 % While comma lists are normally used as ordered lists, it may be
298 % necessary to modify the content. The functions here may be used
299 % to update comma lists, while retaining the order of the unaffected
300 % entries.
301 %
302 % \begin{function}
303 % {
304 % \clist_remove_duplicates:N, \clist_remove_duplicates:c,
305 % \clist_gremove_duplicates:N, \clist_gremove_duplicates:c
306 % }
307 % \begin{syntax}
308 % \cs{clist_remove_duplicates:N} \meta{clist~var}
309 % \end{syntax}
310 % Removes duplicate items from the \meta{clist~var}, leaving the
311 % left most copy of each item in the \meta{clist~var}. The \meta{item}
312 % comparison takes place on a token basis, as for \cs{tl_if_eq:nnTF}.
313 % \begin{texnote}
314 % This function iterates through every item in the \meta{clist~var} and
315 % does a comparison with the \meta{items} already checked. It is therefore
316 % relatively slow with large comma lists.
317 % Furthermore, it may fail if any of the items in the
318 % \meta{clist~var} contains |{|, |}|, or |#|
319 % (assuming the usual \TeX{} category codes apply).
320 % \end{texnote}
321 % \end{function}
322 %
323 % \begin{function}[updated = 2011-09-06]
324 % {
325 % \clist_remove_all:Nn, \clist_remove_all:cn,
326 % \clist_remove_all:NV, \clist_remove_all:cV,
327 % \clist_remove_all:Ne, \clist_remove_all:ce,
328 % \clist_gremove_all:Nn, \clist_gremove_all:cn,
329 % \clist_gremove_all:NV, \clist_gremove_all:cV,
330 % \clist_gremove_all:Ne, \clist_gremove_all:ce
331 % }
332 % \begin{syntax}
333 % \cs{clist_remove_all:Nn} \meta{clist~var} \Arg{item}
334 % \end{syntax}
335 % Removes every occurrence of \meta{item} from the \meta{clist~var}.
336 % The \meta{item} comparison takes place on a token basis, as for
337 % \cs{tl_if_eq:nnTF}.
338 % \begin{texnote}
339 % The function may fail if the \meta{item} contains |{|, |}|, or |#|
340 % (assuming the usual \TeX{} category codes apply).
341 % \end{texnote}
342 % \end{function}
343 %
344 % \begin{function}[added = 2014-07-18]
345 % {
346 % \clist_reverse:N, \clist_reverse:c,
347 % \clist_greverse:N, \clist_greverse:c
348 % }
349 % \begin{syntax}
350 % \cs{clist_reverse:N} \meta{clist~var}
351 % \end{syntax}
352 % Reverses the order of items stored in the \meta{clist~var}.
353 % \end{function}
354 %
355 % \begin{function}[added = 2014-07-18, EXP]{\clist_reverse:n}
356 % \begin{syntax}
357 % \cs{clist_reverse:n} \Arg{comma list}
358 % \end{syntax}
359 % Leaves the items in the \meta{comma list} in the input stream in
360 % reverse order. Contrarily to other what is done for other
361 % \texttt{n}-type \meta{comma list} arguments, braces and spaces are
362 % preserved by this process.
363 % \begin{texnote}
364 % The result is returned within \tn{unexpanded}, which means that the
365 % comma list does not expand further when appearing in an
366 % \texttt{e}-type or \texttt{x}-type argument expansion.
367 % \end{texnote}
368 % \end{function}
369 %
370 % \begin{function}[added = 2017-02-06]
371 % {\clist_sort:Nn, \clist_sort:cn, \clist_gsort:Nn, \clist_gsort:cn}
372 % \begin{syntax}
373 % \cs{clist_sort:Nn} \meta{clist var} \Arg{comparison code}
374 % \end{syntax}
375 % Sorts the items in the \meta{clist var} according to the
376 % \meta{comparison code}, and assigns the result to
377 % \meta{clist var}. The details of sorting comparison are
378 % described in Section~\ref{sec:l3sort:mech}.
379 % \end{function}
380 %
381 % \section{Comma list conditionals}
382 %
383 % \begin{function}[EXP,pTF]{\clist_if_empty:N, \clist_if_empty:c}
384 % \begin{syntax}
385 % \cs{clist_if_empty_p:N} \meta{clist~var}
386 % \cs{clist_if_empty:NTF} \meta{clist~var} \Arg{true code} \Arg{false code}
387 % \end{syntax}
388 % Tests if the \meta{clist~var} is empty (containing no items).
389 % \end{function}
390 %
391 % \begin{function}[EXP, pTF, added = 2014-07-05]{\clist_if_empty:n}
392 % \begin{syntax}
393 % \cs{clist_if_empty_p:n} \Arg{comma list}
394 % \cs{clist_if_empty:nTF} \Arg{comma list} \Arg{true code} \Arg{false code}
395 % \end{syntax}
396 % Tests if the \meta{comma~list} is empty (containing no items).
397 % The rules for space trimming are as for other \texttt{n}-type
398 % comma-list functions, hence the comma list |{~,~,,~}| (without
399 % outer braces) is empty, while |{~,{},}| (without outer braces)
400 % contains one element, which happens to be empty: the comma-list
401 % is not empty.
402 % \end{function}
403 %
404 % \begin{function}[updated = 2011-09-06, TF]
405 % {
406 % \clist_if_in:Nn, \clist_if_in:NV, \clist_if_in:No, \clist_if_in:Ne,
407 % \clist_if_in:cn, \clist_if_in:cV, \clist_if_in:co, \clist_if_in:ce,
408 % \clist_if_in:nn, \clist_if_in:nV, \clist_if_in:no, \clist_if_in:ne
409 % }
410 % \begin{syntax}
411 % \cs{clist_if_in:NnTF} \meta{clist~var} \Arg{item} \Arg{true code} \Arg{false code}
412 % \end{syntax}
413 % Tests if the \meta{item} is present in the \meta{clist~var}.
414 % In the case of an \texttt{n}-type \meta{comma list}, the usual rules
415 % of space trimming and brace stripping apply. Hence,
416 % \begin{verbatim}
417 % \clist_if_in:nnTF { a , {b}~ , {b} , c } { b } {true} {false}
418 % \end{verbatim}
419 % yields \texttt{true}.
420 % \begin{texnote}
421 % The function may fail if the \meta{item} contains |{|, |}|, or |#|
422 % (assuming the usual \TeX{} category codes apply).
423 % \end{texnote}
424 % \end{function}
425 %
426 % \section{Mapping over comma lists}
427 %
428 % The functions described in this section apply a specified function
429 % to each item of a comma list.
430 % All mappings are done at the current group level, i.e., any
431 % local assignments made by the \meta{function} or \meta{code} discussed
432 % below remain in effect after the loop.
433 %
434 % When the comma list is given explicitly, as an \texttt{n}-type argument,
435 % spaces are trimmed around each item.
436 % If the result of trimming spaces is empty, the item is ignored.
437 % Otherwise, if the item is surrounded by braces, one set is removed,
438 % and the result is passed to the mapped function. Thus, if the
439 % comma list that is being mapped is \verb*|{a , {{b} }, ,{}, {c},}|
440 % then the arguments passed to the mapped function are
441 % `\verb*|a|', `\verb*|{b} |', an empty argument, and `\verb*|c|'.
442 %
443 % When the comma list is given as an \texttt{N}-type argument, spaces
444 % have already been trimmed on input, and items are simply stripped
445 % of one set of braces if any. This case is more efficient than using
446 % \texttt{n}-type comma lists.
447 %
448 % \begin{function}[rEXP, updated = 2012-06-29]
449 % {\clist_map_function:NN, \clist_map_function:cN, \clist_map_function:nN, \clist_map_function:eN}
450 % \begin{syntax}
451 % \cs{clist_map_function:NN} \meta{clist~var} \meta{function}
452 % \end{syntax}
453 % Applies \meta{function} to every \meta{item} stored in the
454 % \meta{clist~var}. The \meta{function} receives one argument for
455 % each iteration. The \meta{items} are returned from left to right.
456 % The function \cs{clist_map_inline:Nn} is in general more efficient
457 % than \cs{clist_map_function:NN}.
458 % \end{function}
459 %
460 % \begin{function}[updated = 2012-06-29]
461 % {\clist_map_inline:Nn, \clist_map_inline:cn, \clist_map_inline:nn}
462 % \begin{syntax}
463 % \cs{clist_map_inline:Nn} \meta{clist~var} \Arg{inline function}
464 % \end{syntax}
465 % Applies \meta{inline function} to every \meta{item} stored
466 % within the \meta{clist~var}. The \meta{inline function} should
467 % consist of code which receives the \meta{item} as |#1|.
468 % The \meta{items} are returned from left to right.
469 % \end{function}
470 %
471 % \begin{function}[updated = 2012-06-29]
472 % {\clist_map_variable:NNn, \clist_map_variable:cNn, \clist_map_variable:nNn}
473 % \begin{syntax}
474 % \cs{clist_map_variable:NNn} \meta{clist~var} \meta{variable} \Arg{code}
475 % \end{syntax}
476 % Stores each \meta{item} of the \meta{clist~var} in turn in the
477 % (token list) \meta{variable} and applies the \meta{code}. The
478 % \meta{code} will usually make use of the \meta{variable}, but this
479 % is not enforced. The assignments to the \meta{variable} are local.
480 % Its value after the loop is the last \meta{item} in the \meta{clist~var},
481 % or its original value if there were no \meta{item}. The
482 % \meta{items} are returned from left to right.
483 % \end{function}
484 %
485 % \begin{function}[rEXP, added = 2021-05-05]
486 % {\clist_map_tokens:Nn, \clist_map_tokens:cn, \clist_map_tokens:nn}
487 % \begin{syntax}
488 % \cs{clist_map_tokens:Nn} \meta{clist~var} \Arg{code}
489 % \cs{clist_map_tokens:nn} \Arg{comma list} \Arg{code}
490 % \end{syntax}
491 % Calls \meta{code} \Arg{item} for every \meta{item} stored in the
492 % \meta{clist~var}. The \meta{code} receives each \meta{item} as a
493 % trailing brace group. If the \meta{code} consists of a single
494 % function this is equivalent to \cs{clist_map_function:nN}.
495 % \end{function}
496 %
497 % \begin{function}[rEXP, updated = 2012-06-29]{\clist_map_break:}
498 % \begin{syntax}
499 % \cs{clist_map_break:}
500 % \end{syntax}
501 % Used to terminate a \cs[no-index]{clist_map_\ldots{}} function before all
502 % entries in the \meta{comma list} have been processed. This
503 % normally takes place within a conditional statement, for example
504 % \begin{verbatim}
505 % \clist_map_inline:Nn \l_my_clist
506 % {
507 % \str_if_eq:nnTF { #1 } { bingo }
508 % { \clist_map_break: }
509 % {
510 % % Do something useful
511 % }
512 % }
513 % \end{verbatim}
514 % Use outside of a \cs[no-index]{clist_map_\ldots{}} scenario leads to low
515 % level \TeX{} errors.
516 % \begin{texnote}
517 % When the mapping is broken, additional tokens may be inserted
518 % before further items are taken
519 % from the input stream. This depends on the design of the mapping
520 % function.
521 % \end{texnote}
522 % \end{function}
523 %
524 % \begin{function}[updated = 2012-06-29, rEXP]{\clist_map_break:n}
525 % \begin{syntax}
526 % \cs{clist_map_break:n} \Arg{code}
527 % \end{syntax}
528 % Used to terminate a \cs[no-index]{clist_map_\ldots{}} function before all
529 % entries in the \meta{comma list} have been processed, inserting
530 % the \meta{code} after the mapping has ended. This
531 % normally takes place within a conditional statement, for example
532 % \begin{verbatim}
533 % \clist_map_inline:Nn \l_my_clist
534 % {
535 % \str_if_eq:nnTF { #1 } { bingo }
536 % { \clist_map_break:n { <code> } }
537 % {
538 % % Do something useful
539 % }
540 % }
541 % \end{verbatim}
542 % Use outside of a \cs[no-index]{clist_map_\ldots{}} scenario leads to low
543 % level \TeX{} errors.
544 % \begin{texnote}
545 % When the mapping is broken, additional tokens may be inserted
546 % before the \meta{code} is
547 % inserted into the input stream.
548 % This depends on the design of the mapping function.
549 % \end{texnote}
550 % \end{function}
551 %
552 % \begin{function}[EXP, added = 2012-07-13]
553 % {\clist_count:N, \clist_count:c, \clist_count:n, \clist_count:e}
554 % \begin{syntax}
555 % \cs{clist_count:N} \meta{clist~var}
556 % \end{syntax}
557 % Leaves the number of items in the \meta{clist~var} in the input
558 % stream as an \meta{integer denotation}. The total number of items
559 % in a \meta{clist~var} includes those which are duplicates,
560 % i.e., every item in a \meta{clist~var} is counted.
561 % \end{function}
562 %
563 % \section{Using the content of comma lists directly}
564 %
565 % \begin{function}[EXP, added = 2013-05-26]{\clist_use:Nnnn, \clist_use:cnnn}
566 % \begin{syntax}
567 % \cs{clist_use:Nnnn} \meta{clist~var} \Arg{separator~between~two} \\
568 % ~~\Arg{separator~between~more~than~two} \Arg{separator~between~final~two}
569 % \end{syntax}
570 % Places the contents of the \meta{clist~var} in the input stream,
571 % with the appropriate \meta{separator} between the items. Namely, if
572 % the comma list has more than two items, the \meta{separator between
573 % more than two} is placed between each pair of items except the
574 % last, for which the \meta{separator between final two} is used. If
575 % the comma list has exactly two items, then they are placed in the input
576 % stream separated by the \meta{separator between two}. If the comma
577 % list has a single item, it is placed in the input stream, and a comma
578 % list with no items produces no output. An error is raised if
579 % the variable does not exist or if it is invalid.
580 %
581 % For example,
582 % \begin{verbatim}
583 % \clist_set:Nn \l_tmpa_clist { a , b , , c , {de} , f }
584 % \clist_use:Nnnn \l_tmpa_clist { ~and~ } { ,~ } { ,~and~ }
585 % \end{verbatim}
586 % inserts \enquote{\texttt{a, b, c, de, and f}} in the input
587 % stream. The first separator argument is not used in this case
588 % because the comma list has more than $2$ items.
589 % \begin{texnote}
590 % The result is returned within the \tn{unexpanded}
591 % primitive (\cs{exp_not:n}), which means that the \meta{items}
592 % do not expand further when appearing in an \texttt{e}-type
593 % or \texttt{x}-type argument expansion.
594 % \end{texnote}
595 % \end{function}
596 %
597 % \begin{function}[EXP, added = 2013-05-26]{\clist_use:Nn, \clist_use:cn}
598 % \begin{syntax}
599 % \cs{clist_use:Nn} \meta{clist~var} \Arg{separator}
600 % \end{syntax}
601 % Places the contents of the \meta{clist~var} in the input stream,
602 % with the \meta{separator} between the items. If the comma
603 % list has a single item, it is placed in the input stream, and a comma
604 % list with no items produces no output. An error is raised if
605 % the variable does not exist or if it is invalid.
606 %
607 % For example,
608 % \begin{verbatim}
609 % \clist_set:Nn \l_tmpa_clist { a , b , , c , {de} , f }
610 % \clist_use:Nn \l_tmpa_clist { ~and~ }
611 % \end{verbatim}
612 % inserts \enquote{\texttt{a and b and c and de and f}} in the input
613 % stream.
614 % \begin{texnote}
615 % The result is returned within the \tn{unexpanded}
616 % primitive (\cs{exp_not:n}), which means that the \meta{items}
617 % do not expand further when appearing in an \texttt{e}-type
618 % or \texttt{x}-type argument expansion.
619 % \end{texnote}
620 % \end{function}
621 %
622 % \begin{function}[EXP, added = 2024-11-12]{\clist_use:N, \clist_use:c}
623 % \begin{syntax}
624 % \cs{clist_use:N} \meta{clist~var}
625 % \end{syntax}
626 % Places the contents of the \meta{clist~var} in the input stream,
627 % with a comma between each item. The result is exactly the stored
628 % \meta{clist}, which will include braces around (for example)
629 % entries with retained spaces at the ends.
630 % \begin{texnote}
631 % The result is returned as-is, in the same way as \cs{tl_use:N}
632 % and \emph{without} protection from expansion,
633 % cf.~\cs{clist_use:Nnnnn}, etc. It is equivalent to \texttt{V}-type
634 % expansion of a \texttt{clist}.
635 % \end{texnote}
636 % \end{function}
637 %
638 % \begin{function}[EXP, added = 2021-05-10]{\clist_use:nnnn, \clist_use:nn}
639 % \begin{syntax}
640 % \cs{clist_use:nnnn} \Arg{comma~list} \Arg{separator~between~two} \\
641 % ~~\Arg{separator~between~more~than~two} \Arg{separator~between~final~two}
642 % \cs{clist_use:nn} \Arg{comma~list} \Arg{separator}
643 % \end{syntax}
644 % Places the contents of the \meta{comma~list} in the input stream,
645 % with the appropriate \meta{separator} between the items. As for
646 % \cs{clist_set:Nn}, blank items are omitted, spaces are removed from
647 % both sides of each item, then a set of braces is removed if the
648 % resulting space-trimmed item is braced. The \meta{separators} are
649 % then inserted in the same way as for \cs{clist_use:Nnnn} and
650 % \cs{clist_use:Nn}, respectively.
651 % \begin{texnote}
652 % The result is returned within the \tn{unexpanded}
653 % primitive (\cs{exp_not:n}), which means that the \meta{items}
654 % do not expand further when appearing in an \texttt{e}-type
655 % or \texttt{x}-type argument expansion.
656 % \end{texnote}
657 % \end{function}
658 %
659 % \section{Comma lists as stacks}
660 %
661 % Comma lists can be used as stacks, where data is pushed to and popped
662 % from the top of the comma list. (The left of a comma list is the top, for
663 % performance reasons.) The stack functions for comma lists are not
664 % intended to be mixed with the general ordered data functions detailed
665 % in the previous section: a comma list should either be used as an
666 % ordered data type or as a stack, but not in both ways.
667 %
668 % \begin{function}[noTF, added = 2012-05-14, updated = 2019-02-16]
669 % {\clist_get:NN, \clist_get:cN}
670 % \begin{syntax}
671 % \cs{clist_get:NN} \meta{clist~var} \meta{tl~var}
672 % \end{syntax}
673 % Stores the left-most item from a \meta{clist~var} in the
674 % \meta{tl~var} without removing it from the
675 % \meta{clist~var}. The \meta{tl~var} is assigned locally.
676 % In the non-branching version, if the \meta{clist~var} is empty the
677 % \meta{tl~var} is set to the marker value \cs{q_no_value}.
678 % \end{function}
679 %
680 % \begin{function}[updated = 2011-09-06]{\clist_pop:NN, \clist_pop:cN}
681 % \begin{syntax}
682 % \cs{clist_pop:NN} \meta{clist~var} \meta{tl~var}
683 % \end{syntax}
684 % Pops the left-most item from a \meta{clist~var} into the
685 % \meta{tl~var}, i.e., removes the item from the
686 % comma list and stores it in the \meta{tl~var}.
687 % Both of the variables are assigned locally.
688 % \end{function}
689 %
690 % \begin{function}{\clist_gpop:NN, \clist_gpop:cN}
691 % \begin{syntax}
692 % \cs{clist_gpop:NN} \meta{clist~var} \meta{tl~var}
693 % \end{syntax}
694 % Pops the left-most item from a \meta{clist~var} into the
695 % \meta{tl~var}, i.e., removes the item from the
696 % comma list and stores it in the \meta{tl~var}.
697 % The \meta{clist~var} is modified globally, while the assignment of
698 % the \meta{tl~var} is local.
699 % \end{function}
700 %
701 % \begin{function}[TF, added = 2012-05-14]{\clist_pop:NN, \clist_pop:cN}
702 % \begin{syntax}
703 % \cs{clist_pop:NNTF} \meta{clist~var} \meta{tl~var} \Arg{true code} \Arg{false code}
704 % \end{syntax}
705 % If the \meta{clist~var} is empty, leaves the \meta{false code} in the
706 % input stream. The value of the \meta{tl~var} is
707 % not defined in this case and should not be relied upon. If the
708 % \meta{clist~var} is non-empty, pops the top item from the
709 % \meta{clist~var} in the \meta{tl~var}, i.e., removes
710 % the item from the \meta{clist~var}. Both the \meta{clist~var} and the
711 % \meta{tl~var} are assigned locally.
712 % \end{function}
713 %
714 % \begin{function}[TF, added = 2012-05-14]{\clist_gpop:NN, \clist_gpop:cN}
715 % \begin{syntax}
716 % \cs{clist_gpop:NNTF} \meta{clist~var} \meta{tl~var} \Arg{true code} \Arg{false code}
717 % \end{syntax}
718 % If the \meta{clist~var} is empty, leaves the \meta{false code} in the
719 % input stream. The value of the \meta{tl~var} is
720 % not defined in this case and should not be relied upon. If the
721 % \meta{clist~var} is non-empty, pops the top item from the
722 % \meta{clist~var} in the \meta{tl~var}, i.e., removes
723 % the item from the \meta{clist~var}. The \meta{clist~var} is modified
724 % globally, while the \meta{tl~var} is assigned locally.
725 % \end{function}
726 %
727 % \begin{function}
728 % {
729 % \clist_push:Nn, \clist_push:NV, \clist_push:No,
730 % \clist_push:cn, \clist_push:cV, \clist_push:co,
731 % \clist_gpush:Nn, \clist_gpush:NV, \clist_gpush:No,
732 % \clist_gpush:cn, \clist_gpush:cV, \clist_gpush:co,
733 % }
734 % \begin{syntax}
735 % \cs{clist_push:Nn} \meta{clist~var} \Arg{items}
736 % \end{syntax}
737 % Adds the \Arg{items} to the top of the \meta{clist~var}.
738 % Spaces are removed from both sides of each item as for any
739 % \texttt{n}-type comma list.
740 % \end{function}
741 %
742 % \section{Using a single item}
743 %
744 % \begin{function}[added = 2014-07-17, EXP]
745 % {
746 % \clist_item:Nn, \clist_item:cn,
747 % \clist_item:nn, \clist_item:Vn, \clist_item:vn,
748 % \clist_item:on, \clist_item:en
749 % }
750 % \begin{syntax}
751 % \cs{clist_item:Nn} \meta{clist~var} \Arg{int expr}
752 % \end{syntax}
753 % Indexing items in the \meta{clist~var} from~$1$ at the top (left), this
754 % function evaluates the \meta{int expr} and leaves the
755 % appropriate item from the comma list in the input stream. If the
756 % \meta{int expr} is negative, indexing occurs from the
757 % bottom (right) of the comma list. When the \meta{int expr}
758 % is larger than the number of items in the \meta{clist~var} (as
759 % calculated by \cs{clist_count:N}) then the function expands to
760 % nothing.
761 % \begin{texnote}
762 % The result is returned within the \tn{unexpanded}
763 % primitive (\cs{exp_not:n}), which means that the \meta{item}
764 % does not expand further when appearing in an \texttt{e}-type
765 % or \texttt{x}-type argument expansion.
766 % \end{texnote}
767 % \end{function}
768 %
769 % \begin{function}[EXP, added = 2016-12-06]
770 % {\clist_rand_item:N, \clist_rand_item:n, \clist_rand_item:c}
771 % \begin{syntax}
772 % \cs{clist_rand_item:N} \meta{clist~var}
773 % \cs{clist_rand_item:n} \Arg{comma list}
774 % \end{syntax}
775 % Selects a pseudo-random item of the \meta{clist~var}/\meta{comma list}.
776 % If the \meta{comma list} has no item, the result is empty.
777 % \begin{texnote}
778 % The result is returned within the \tn{unexpanded}
779 % primitive (\cs{exp_not:n}), which means that the \meta{item}
780 % does not expand further when appearing in an \texttt{e}-type
781 % or \texttt{x}-type argument expansion.
782 % \end{texnote}
783 % \end{function}
784 %
785 % \section{Viewing comma lists}
786 %
787 % \begin{function}[updated = 2021-04-29]{\clist_show:N, \clist_show:c}
788 % \begin{syntax}
789 % \cs{clist_show:N} \meta{clist~var}
790 % \end{syntax}
791 % Displays the entries in the \meta{clist~var} in the terminal.
792 % \end{function}
793 %
794 % \begin{function}[updated = 2013-08-03]{\clist_show:n}
795 % \begin{syntax}
796 % \cs{clist_show:n} \Arg{tokens}
797 % \end{syntax}
798 % Displays the entries in the comma list in the terminal.
799 % \end{function}
800 %
801 % \begin{function}[added = 2014-08-22, updated = 2021-04-29]{\clist_log:N, \clist_log:c}
802 % \begin{syntax}
803 % \cs{clist_log:N} \meta{clist~var}
804 % \end{syntax}
805 % Writes the entries in the \meta{clist~var} in the log file. See
806 % also \cs{clist_show:N} which displays the result in the terminal.
807 % \end{function}
808 %
809 % \begin{function}[added = 2014-08-22]{\clist_log:n}
810 % \begin{syntax}
811 % \cs{clist_log:n} \Arg{tokens}
812 % \end{syntax}
813 % Writes the entries in the comma list in the log file. See also
814 % \cs{clist_show:n} which displays the result in the terminal.
815 % \end{function}
816 %
817 % \section{Constant and scratch comma lists}
818 %
819 % \begin{variable}[added = 2012-07-02]{\c_empty_clist}
820 % Constant that is always empty.
821 % \end{variable}
822 %
823 % \begin{variable}[added = 2011-09-06]{\l_tmpa_clist, \l_tmpb_clist}
824 % Scratch comma lists for local assignment. These are never used by
825 % the kernel code, and so are safe for use with any \LaTeX3-defined
826 % function. However, they may be overwritten by other non-kernel
827 % code and so should only be used for short-term storage.
828 % \end{variable}
829 %
830 % \begin{variable}[added = 2011-09-06]{\g_tmpa_clist, \g_tmpb_clist}
831 % Scratch comma lists for global assignment. These are never used by
832 % the kernel code, and so are safe for use with any \LaTeX3-defined
833 % function. However, they may be overwritten by other non-kernel
834 % code and so should only be used for short-term storage.
835 % \end{variable}
836 %
837 % \end{documentation}
838 %
839 % \begin{implementation}
840 %
841 % \section{\pkg{l3clist} implementation}
842 %
843 % \TestFiles{m3clist002}
844 %
845 % \begin{macrocode}
846 %<*code>
847 % \end{macrocode}
848 %
849 % \begin{macrocode}
850 %<@@=clist>
851 % \end{macrocode}
852 %
853 % \begin{variable}{\c_empty_clist}
854 % An empty comma list is simply an empty token list.
855 % \begin{macrocode}
856 \cs_new_eq:NN \c_empty_clist \c_empty_tl
857 % \end{macrocode}
858 % \end{variable}
859 %
860 % \begin{variable}{\l_@@_tmp_clist}
861 % Scratch space for various internal uses. This comma list variable
862 % cannot be declared as such because it comes before \cs{clist_new:N}
863 % \begin{macrocode}
864 \tl_new:N \l_@@_tmp_clist
865 % \end{macrocode}
866 % \end{variable}
867 %
868 % \begin{variable}{\s_@@_mark,\s_@@_stop}
869 % Internal scan marks.
870 % \begin{macrocode}
871 \scan_new:N \s_@@_mark
872 \scan_new:N \s_@@_stop
873 % \end{macrocode}
874 % \end{variable}
875 %
876 % \begin{macro}[EXP]{
877 % \@@_use_none_delimit_by_s_mark:w,
878 % \@@_use_none_delimit_by_s_stop:w,
879 % \@@_use_i_delimit_by_s_stop:nw
880 % }
881 % Functions to gobble up to a scan mark.
882 % \begin{macrocode}
883 \cs_new:Npn \@@_use_none_delimit_by_s_mark:w #1 \s_@@_mark { }
884 \cs_new:Npn \@@_use_none_delimit_by_s_stop:w #1 \s_@@_stop { }
885 \cs_new:Npn \@@_use_i_delimit_by_s_stop:nw #1 #2 \s_@@_stop {#1}
886 % \end{macrocode}
887 % \end{macro}
888 %
889 % \begin{macro}{\@@_tmp:w}
890 % A temporary function for various purposes.
891 % \begin{macrocode}
892 \cs_new_protected:Npn \@@_tmp:w { }
893 % \end{macrocode}
894 % \end{macro}
895 %
896 % \subsection{Removing spaces around items}
897 %
898 % \begin{macro}{\@@_trim_next:w}
899 % Called as \cs{exp:w} \cs{@@_trim_next:w} \cs{prg_do_nothing:}
900 % \meta{comma list} \ldots{} it expands to \Arg{trimmed item} where
901 % the \meta{trimmed item} is the first non-empty result from removing
902 % spaces from both ends of comma-delimited items in the \meta{comma
903 % list}. The \cs{prg_do_nothing:} marker avoids losing braces. The
904 % test for blank items is a somewhat optimized \cs{tl_if_empty:oTF}
905 % construction; if blank, another item is sought, otherwise trim
906 % spaces.
907 % \begin{macrocode}
908 \cs_new:Npn \@@_trim_next:w #1 ,
909 {
910 \tl_if_empty:oTF { \use_none:nn #1 ? }
911 { \@@_trim_next:w \prg_do_nothing: }
912 { \tl_trim_spaces_apply:oN {#1} \exp_end: }
913 }
914 % \end{macrocode}
915 % \end{macro}
916 %
917 % \begin{macro}[rEXP]{\@@_sanitize:n}
918 % \begin{macro}{\@@_sanitize:Nn}
919 % The auxiliary \cs{@@_sanitize:Nn} receives a delimiter
920 % (\cs{c_empty_tl} the first time, afterwards a comma) and that item
921 % as arguments. Unless we are done with the loop it calls
922 % \cs{@@_wrap_item:w} to unbrace the item (using a comma delimiter is
923 % safe since |#2| came from removing spaces from an argument delimited
924 % by a comma) and possibly re-brace it if needed.
925 % \begin{macrocode}
926 \cs_new:Npn \@@_sanitize:n #1
927 {
928 \exp_after:wN \@@_sanitize:Nn \exp_after:wN \c_empty_tl
929 \exp:w \@@_trim_next:w \prg_do_nothing:
930 #1 , \s_@@_stop \prg_break: , \prg_break_point:
931 }
932 \cs_new:Npn \@@_sanitize:Nn #1#2
933 {
934 \@@_use_none_delimit_by_s_stop:w #2 \s_@@_stop
935 #1 \@@_wrap_item:w #2 ,
936 \exp_after:wN \@@_sanitize:Nn \exp_after:wN ,
937 \exp:w \@@_trim_next:w \prg_do_nothing:
938 }
939 % \end{macrocode}
940 % \end{macro}
941 % \end{macro}
942 %
943 % \begin{macro}[TF]{\@@_if_wrap:n}
944 % \begin{macro}{\@@_if_wrap:w}
945 % True if the argument must be wrapped to avoid getting altered by some
946 % clist operations. That is the case whenever the argument
947 % \begin{itemize}
948 % \item starts or end with a space or contains a comma,
949 % \item is empty, or
950 % \item consists of a single braced group.
951 % \end{itemize}
952 % If the argument starts or ends with a space or contains
953 % a comma then one of the three arguments of \cs{@@_if_wrap:w} will
954 % have its end delimiter (partly) in one of the three copies of |#1|
955 % in \cs{@@_if_wrap:nTF}; this has a knock-on effect meaning that the
956 % result of the expansion is not empty; in that case, wrap.
957 % Otherwise, the argument is safe unless it starts with a brace group
958 % (or is empty) and it is empty or consists of a single
959 % \texttt{n}-type argument.
960 % \begin{macrocode}
961 \prg_new_conditional:Npnn \@@_if_wrap:n #1 { TF }
962 {
963 \tl_if_empty:oTF
964 {
965 \@@_if_wrap:w
966 \s_@@_mark ? #1 ~ \s_@@_mark ? ~ #1
967 \s_@@_mark , ~ \s_@@_mark #1 ,
968 }
969 {
970 \tl_if_head_is_group:nTF { #1 { } }
971 {
972 \tl_if_empty:nTF {#1}
973 { \prg_return_true: }
974 {
975 \tl_if_empty:oTF { \use_none:n #1}
976 { \prg_return_true: }
977 { \prg_return_false: }
978 }
979 }
980 { \prg_return_false: }
981 }
982 { \prg_return_true: }
983 }
984 \cs_new:Npn \@@_if_wrap:w #1 \s_@@_mark ? ~ #2 ~ \s_@@_mark #3 , { }
985 % \end{macrocode}
986 % \end{macro}
987 % \end{macro}
988 %
989 % \begin{macro}{\@@_wrap_item:w}
990 % Safe items are put in \cs{exp_not:n}, otherwise we put an extra set
991 % of braces.
992 % \begin{macrocode}
993 \cs_new:Npn \@@_wrap_item:w #1 ,
994 { \@@_if_wrap:nTF {#1} { \exp_not:n { {#1} } } { \exp_not:n {#1} } }
995 % \end{macrocode}
996 % \end{macro}
997 %
998 % \subsection{Allocation and initialization}
999 %
1000 % \begin{macro}{\clist_new:N, \clist_new:c}
1001 % \UnitTested
1002 % Internally, comma lists are just token lists.
1003 % \begin{macrocode}
1004 \cs_new_eq:NN \clist_new:N \tl_new:N
1005 \cs_new_eq:NN \clist_new:c \tl_new:c
1006 % \end{macrocode}
1007 % \end{macro}
1008 %
1009 % \begin{macro}
1010 % {
1011 % \clist_const:Nn, \clist_const:Ne, \clist_const:Nx,
1012 % \clist_const:cn, \clist_const:ce, \clist_const:cx
1013 % }
1014 % Creating and initializing a constant comma list is done by
1015 % sanitizing all items (stripping spaces and braces).
1016 % \begin{macrocode}
1017 \cs_new_protected:Npn \clist_const:Nn #1#2
1018 { \tl_const:Ne #1 { \@@_sanitize:n {#2} } }
1019 \cs_generate_variant:Nn \clist_const:Nn { Ne , c , ce }
1020 \cs_generate_variant:Nn \clist_const:Nn { Nx , cx }
1021 % \end{macrocode}
1022 % \end{macro}
1023 %
1024 % \begin{macro}{\clist_clear:N, \clist_clear:c}
1025 % \UnitTested
1026 % \begin{macro}{\clist_gclear:N, \clist_gclear:c}
1027 % \UnitTested
1028 % Clearing comma lists is just the same as clearing token lists.
1029 % \begin{macrocode}
1030 \cs_new_eq:NN \clist_clear:N \tl_clear:N
1031 \cs_new_eq:NN \clist_clear:c \tl_clear:c
1032 \cs_new_eq:NN \clist_gclear:N \tl_gclear:N
1033 \cs_new_eq:NN \clist_gclear:c \tl_gclear:c
1034 % \end{macrocode}
1035 % \end{macro}
1036 % \end{macro}
1037 %
1038 % \begin{macro}{\clist_clear_new:N, \clist_clear_new:c}
1039 % \UnitTested
1040 % \begin{macro}{\clist_gclear_new:N, \clist_gclear_new:c}
1041 % \UnitTested
1042 % Once again a copy from the token list functions.
1043 % \begin{macrocode}
1044 \cs_new_eq:NN \clist_clear_new:N \tl_clear_new:N
1045 \cs_new_eq:NN \clist_clear_new:c \tl_clear_new:c
1046 \cs_new_eq:NN \clist_gclear_new:N \tl_gclear_new:N
1047 \cs_new_eq:NN \clist_gclear_new:c \tl_gclear_new:c
1048 % \end{macrocode}
1049 % \end{macro}
1050 % \end{macro}
1051 %
1052 % \begin{macro}
1053 % {\clist_set_eq:NN, \clist_set_eq:cN, \clist_set_eq:Nc, \clist_set_eq:cc}
1054 % \UnitTested
1055 % \begin{macro}
1056 % {
1057 % \clist_gset_eq:NN, \clist_gset_eq:cN,
1058 % \clist_gset_eq:Nc, \clist_gset_eq:cc
1059 % }
1060 % \UnitTested
1061 % Once again, these are simple copies from the token list functions.
1062 % \begin{macrocode}
1063 \cs_new_eq:NN \clist_set_eq:NN \tl_set_eq:NN
1064 \cs_new_eq:NN \clist_set_eq:Nc \tl_set_eq:Nc
1065 \cs_new_eq:NN \clist_set_eq:cN \tl_set_eq:cN
1066 \cs_new_eq:NN \clist_set_eq:cc \tl_set_eq:cc
1067 \cs_new_eq:NN \clist_gset_eq:NN \tl_gset_eq:NN
1068 \cs_new_eq:NN \clist_gset_eq:Nc \tl_gset_eq:Nc
1069 \cs_new_eq:NN \clist_gset_eq:cN \tl_gset_eq:cN
1070 \cs_new_eq:NN \clist_gset_eq:cc \tl_gset_eq:cc
1071 % \end{macrocode}
1072 % \end{macro}
1073 % \end{macro}
1074 %
1075 % \begin{macro}
1076 % {
1077 % \clist_set_from_seq:NN, \clist_set_from_seq:cN,
1078 % \clist_set_from_seq:Nc, \clist_set_from_seq:cc
1079 % }
1080 % \UnitTested
1081 % \begin{macro}
1082 % {
1083 % \clist_gset_from_seq:NN, \clist_gset_from_seq:cN,
1084 % \clist_gset_from_seq:Nc, \clist_gset_from_seq:cc
1085 % }
1086 % \UnitTested
1087 % \begin{macro}{\@@_set_from_seq:NNNN, \@@_set_from_seq:n}
1088 % Setting a comma list from a comma-separated list is done using a
1089 % simple mapping. Safe items are put in \cs{exp_not:n}, otherwise we
1090 % put an extra set of braces. The first comma must be removed, except
1091 % in the case of an empty comma-list.
1092 % \begin{macrocode}
1093 \cs_new_protected:Npn \clist_set_from_seq:NN
1094 { \@@_set_from_seq:NNNN \clist_clear:N \__kernel_tl_set:Nx }
1095 \cs_new_protected:Npn \clist_gset_from_seq:NN
1096 { \@@_set_from_seq:NNNN \clist_gclear:N \__kernel_tl_gset:Nx }
1097 \cs_new_protected:Npn \@@_set_from_seq:NNNN #1#2#3#4
1098 {
1099 \seq_if_empty:NTF #4
1100 { #1 #3 }
1101 {
1102 #2 #3
1103 {
1104 \exp_after:wN \use_none:n \exp:w \exp_end_continue_f:w
1105 \seq_map_function:NN #4 \@@_set_from_seq:n
1106 }
1107 }
1108 }
1109 \cs_new:Npn \@@_set_from_seq:n #1
1110 {
1111 ,
1112 \@@_if_wrap:nTF {#1}
1113 { \exp_not:n { {#1} } }
1114 { \exp_not:n {#1} }
1115 }
1116 \cs_generate_variant:Nn \clist_set_from_seq:NN { Nc }
1117 \cs_generate_variant:Nn \clist_set_from_seq:NN { c , cc }
1118 \cs_generate_variant:Nn \clist_gset_from_seq:NN { Nc }
1119 \cs_generate_variant:Nn \clist_gset_from_seq:NN { c , cc }
1120 % \end{macrocode}
1121 % \end{macro}
1122 % \end{macro}
1123 % \end{macro}
1124 %
1125 % \begin{macro}{\clist_concat:NNN, \clist_concat:ccc}
1126 % \UnitTested
1127 % \begin{macro}{\clist_gconcat:NNN, \clist_gconcat:ccc}
1128 % \UnitTested
1129 % \begin{macro}{\@@_concat:NNNN}
1130 % Concatenating comma lists is not quite as easy as it seems, as
1131 % there needs to be the correct addition of a comma to the output. So
1132 % a little work to do.
1133 % \begin{macrocode}
1134 \cs_new_protected:Npn \clist_concat:NNN
1135 { \@@_concat:NNNN \__kernel_tl_set:Nx }
1136 \cs_new_protected:Npn \clist_gconcat:NNN
1137 { \@@_concat:NNNN \__kernel_tl_gset:Nx }
1138 \cs_new_protected:Npn \@@_concat:NNNN #1#2#3#4
1139 {
1140 #1 #2
1141 {
1142 \exp_not:o #3
1143 \clist_if_empty:NF #3 { \clist_if_empty:NF #4 { , } }
1144 \exp_not:o #4
1145 }
1146 }
1147 \cs_generate_variant:Nn \clist_concat:NNN { ccc }
1148 \cs_generate_variant:Nn \clist_gconcat:NNN { ccc }
1149 % \end{macrocode}
1150 % \end{macro}
1151 % \end{macro}
1152 % \end{macro}
1153 %
1154 % \begin{macro}[pTF]{\clist_if_exist:N, \clist_if_exist:c}
1155 % Copies of the \texttt{cs} functions defined in \pkg{l3basics}.
1156 % \begin{macrocode}
1157 \prg_new_eq_conditional:NNn \clist_if_exist:N \cs_if_exist:N
1158 { TF , T , F , p }
1159 \prg_new_eq_conditional:NNn \clist_if_exist:c \cs_if_exist:c
1160 { TF , T , F , p }
1161 % \end{macrocode}
1162 % \end{macro}
1163 %
1164 % \subsection{Adding data to comma lists}
1165 %
1166 % \begin{macro}
1167 % {
1168 % \clist_set:Nn, \clist_set:NV, \clist_set:Nv, \clist_set:Ne,
1169 % \clist_set:No, \clist_set:Nx,
1170 % \clist_set:cn, \clist_set:cV, \clist_set:cv, \clist_set:ce,
1171 % \clist_set:co, \clist_set:cx
1172 % }
1173 % \begin{macro}
1174 % {
1175 % \clist_gset:Nn, \clist_gset:NV, \clist_gset:Nv, \clist_gset:Ne,
1176 % \clist_gset:No, \clist_gset:Nx,
1177 % \clist_gset:cn, \clist_gset:cV, \clist_gset:cv, \clist_gset:ce,
1178 % \clist_gset:co, \clist_gset:cx,
1179 % }
1180 % \begin{macrocode}
1181 \cs_new_protected:Npn \clist_set:Nn #1#2
1182 { \__kernel_tl_set:Nx #1 { \@@_sanitize:n {#2} } }
1183 \cs_new_protected:Npn \clist_gset:Nn #1#2
1184 { \__kernel_tl_gset:Nx #1 { \@@_sanitize:n {#2} } }
1185 \cs_generate_variant:Nn \clist_set:Nn { NV , Nv , Ne , c , cV , cv , ce }
1186 \cs_generate_variant:Nn \clist_set:Nn { No , Nx , co , cx }
1187 \cs_generate_variant:Nn \clist_gset:Nn { NV , Nv , Ne , c , cV , cv , ce }
1188 \cs_generate_variant:Nn \clist_gset:Nn { No , Nx , co , cx }
1189 % \end{macrocode}
1190 % \end{macro}
1191 % \end{macro}
1192 %
1193 % \begin{macro}
1194 % {
1195 % \clist_put_left:Nn, \clist_put_left:NV,
1196 % \clist_put_left:Nv, \clist_put_left:Ne,
1197 % \clist_put_left:No, \clist_put_left:Nx,
1198 % \clist_put_left:cn, \clist_put_left:cV,
1199 % \clist_put_left:cv, \clist_put_left:ce,
1200 % \clist_put_left:co, \clist_put_left:cx,
1201 % }
1202 % \UnitTested
1203 % \begin{macro}
1204 % {
1205 % \clist_gput_left:Nn, \clist_gput_left:NV,
1206 % \clist_gput_left:Nv, \clist_gput_left:Ne,
1207 % \clist_gput_left:No, \clist_gput_left:Nx,
1208 % \clist_gput_left:cn, \clist_gput_left:cV,
1209 % \clist_gput_left:cv, \clist_gput_left:ce,
1210 % \clist_gput_left:co, \clist_gput_left:cx,
1211 % }
1212 % \UnitTested
1213 % \begin{macro}{\@@_put_left:NNNn}
1214 % Everything is based on concatenation after storing in
1215 % \cs{l_@@_tmp_clist}. This avoids having to worry here about
1216 % space-trimming and so on.
1217 % \begin{macrocode}
1218 \cs_new_protected:Npn \clist_put_left:Nn
1219 { \@@_put_left:NNNn \clist_concat:NNN \clist_set:Nn }
1220 \cs_new_protected:Npn \clist_gput_left:Nn
1221 { \@@_put_left:NNNn \clist_gconcat:NNN \clist_set:Nn }
1222 \cs_new_protected:Npn \@@_put_left:NNNn #1#2#3#4
1223 {
1224 #2 \l_@@_tmp_clist {#4}
1225 #1 #3 \l_@@_tmp_clist #3
1226 }
1227 \cs_generate_variant:Nn \clist_put_left:Nn { NV , Nv , Ne , c , cV , cv , ce }
1228 \cs_generate_variant:Nn \clist_put_left:Nn { No , Nx , co , cx }
1229 \cs_generate_variant:Nn \clist_gput_left:Nn { NV , Nv , Ne , c , cV , cv , ce }
1230 \cs_generate_variant:Nn \clist_gput_left:Nn { No , Nx , co , cx }
1231 % \end{macrocode}
1232 % \end{macro}
1233 % \end{macro}
1234 % \end{macro}
1235 %
1236 % \begin{macro}
1237 % {
1238 % \clist_put_right:Nn, \clist_put_right:NV,
1239 % \clist_put_right:Nv, \clist_put_right:Ne,
1240 % \clist_put_right:No, \clist_put_right:Nx,
1241 % \clist_put_right:cn, \clist_put_right:cV,
1242 % \clist_put_right:cv, \clist_put_right:ce,
1243 % \clist_put_right:co, \clist_put_right:cx
1244 % }
1245 % \UnitTested
1246 % \begin{macro}
1247 % {
1248 % \clist_gput_right:Nn, \clist_gput_right:NV,
1249 % \clist_gput_right:Nv, \clist_gput_right:Ne,
1250 % \clist_gput_right:No, \clist_gput_right:Nx,
1251 % \clist_gput_right:cn, \clist_gput_right:cV,
1252 % \clist_gput_right:cv, \clist_gput_right:ce,
1253 % \clist_gput_right:cx, \clist_gput_right:co
1254 % }
1255 % \UnitTested
1256 % \begin{macro}{\@@_put_right:NNNn}
1257 % \begin{macrocode}
1258 \cs_new_protected:Npn \clist_put_right:Nn
1259 { \@@_put_right:NNNn \clist_concat:NNN \clist_set:Nn }
1260 \cs_new_protected:Npn \clist_gput_right:Nn
1261 { \@@_put_right:NNNn \clist_gconcat:NNN \clist_set:Nn }
1262 \cs_new_protected:Npn \@@_put_right:NNNn #1#2#3#4
1263 {
1264 #2 \l_@@_tmp_clist {#4}
1265 #1 #3 #3 \l_@@_tmp_clist
1266 }
1267 \cs_generate_variant:Nn \clist_put_right:Nn
1268 { NV , Nv , Ne , c , cV , cv , ce }
1269 \cs_generate_variant:Nn \clist_put_right:Nn
1270 { No , Nx , co , cx }
1271 \cs_generate_variant:Nn \clist_gput_right:Nn
1272 { NV , Nv , Ne , c , cV , cv , ce }
1273 \cs_generate_variant:Nn \clist_gput_right:Nn
1274 { No , Nx , co , cx }
1275 % \end{macrocode}
1276 % \end{macro}
1277 % \end{macro}
1278 % \end{macro}
1279 %
1280 % \subsection{Comma lists as stacks}
1281 %
1282 % \begin{macro}{\clist_get:NN, \clist_get:cN}
1283 % \UnitTested
1284 % \begin{macro}{\@@_get:wN}
1285 % Getting an item from the left of a comma list is pretty easy: just
1286 % trim off the first item using the comma. No need to trim spaces as
1287 % comma-list \emph{variables} are assumed to have \enquote{cleaned-up}
1288 % items. (Note that grabbing a comma-delimited item removes an outer
1289 % pair of braces if present, exactly as needed to uncover the
1290 % underlying item.)
1291 % \begin{macrocode}
1292 \cs_new_protected:Npn \clist_get:NN #1#2
1293 {
1294 \if_meaning:w #1 \c_empty_clist
1295 \tl_set:Nn #2 { \q_no_value }
1296 \else:
1297 \exp_after:wN \@@_get:wN #1 , \s_@@_stop #2
1298 \fi:
1299 }
1300 \cs_new_protected:Npn \@@_get:wN #1 , #2 \s_@@_stop #3
1301 { \tl_set:Nn #3 {#1} }
1302 \cs_generate_variant:Nn \clist_get:NN { c }
1303 % \end{macrocode}
1304 % \end{macro}
1305 % \end{macro}
1306 %
1307 % \begin{macro}{\clist_pop:NN, \clist_pop:cN}
1308 % \UnitTested
1309 % \begin{macro}{\clist_gpop:NN, \clist_gpop:cN}
1310 % \UnitTested
1311 % \begin{macro}
1312 % {\@@_pop:NNN, \@@_pop:wwNNN, \@@_pop:wN}
1313 % An empty clist leads to \cs{q_no_value}, otherwise grab until the
1314 % first comma and assign to the variable. The second argument of
1315 % \cs{@@_pop:wwNNN} is a comma list ending in a comma and
1316 % \cs{s_@@_mark}, unless the original clist contained exactly one item:
1317 % then the argument is just \cs{s_@@_mark}. The next auxiliary picks
1318 % either \cs{exp_not:n} or \cs{use_none:n} as |#2|, ensuring that the
1319 % result can safely be an empty comma list.
1320 % \begin{macrocode}
1321 \cs_new_protected:Npn \clist_pop:NN
1322 { \@@_pop:NNN \__kernel_tl_set:Nx }
1323 \cs_new_protected:Npn \clist_gpop:NN
1324 { \@@_pop:NNN \__kernel_tl_gset:Nx }
1325 \cs_new_protected:Npn \@@_pop:NNN #1#2#3
1326 {
1327 \if_meaning:w #2 \c_empty_clist
1328 \tl_set:Nn #3 { \q_no_value }
1329 \else:
1330 \exp_after:wN \@@_pop:wwNNN #2 , \s_@@_mark \s_@@_stop #1#2#3
1331 \fi:
1332 }
1333 \cs_new_protected:Npn \@@_pop:wwNNN #1 , #2 \s_@@_stop #3#4#5
1334 {
1335 \tl_set:Nn #5 {#1}
1336 #3 #4
1337 {
1338 \@@_pop:wN \prg_do_nothing:
1339 #2 \exp_not:o
1340 , \s_@@_mark \use_none:n
1341 \s_@@_stop
1342 }
1343 }
1344 \cs_new:Npn \@@_pop:wN #1 , \s_@@_mark #2 #3 \s_@@_stop { #2 {#1} }
1345 \cs_generate_variant:Nn \clist_pop:NN { c }
1346 \cs_generate_variant:Nn \clist_gpop:NN { c }
1347 % \end{macrocode}
1348 % \end{macro}
1349 % \end{macro}
1350 % \end{macro}
1351 %
1352 % \begin{macro}[TF]{\clist_get:NN, \clist_get:cN}
1353 % \begin{macro}[TF]{\clist_pop:NN, \clist_pop:cN}
1354 % \begin{macro}[TF]{\clist_gpop:NN, \clist_gpop:cN}
1355 % \begin{macro}{\@@_pop_TF:NNN}
1356 % The same, as branching code: very similar to the above.
1357 % \begin{macrocode}
1358 \prg_new_protected_conditional:Npnn \clist_get:NN #1#2 { T , F , TF }
1359 {
1360 \if_meaning:w #1 \c_empty_clist
1361 \prg_return_false:
1362 \else:
1363 \exp_after:wN \@@_get:wN #1 , \s_@@_stop #2
1364 \prg_return_true:
1365 \fi:
1366 }
1367 \prg_generate_conditional_variant:Nnn \clist_get:NN { c } { T , F , TF }
1368 \prg_new_protected_conditional:Npnn \clist_pop:NN #1#2 { T , F , TF }
1369 { \@@_pop_TF:NNN \__kernel_tl_set:Nx #1 #2 }
1370 \prg_new_protected_conditional:Npnn \clist_gpop:NN #1#2 { T , F , TF }
1371 { \@@_pop_TF:NNN \__kernel_tl_gset:Nx #1 #2 }
1372 \cs_new_protected:Npn \@@_pop_TF:NNN #1#2#3
1373 {
1374 \if_meaning:w #2 \c_empty_clist
1375 \prg_return_false:
1376 \else:
1377 \exp_after:wN \@@_pop:wwNNN #2 , \s_@@_mark \s_@@_stop #1#2#3
1378 \prg_return_true:
1379 \fi:
1380 }
1381 \prg_generate_conditional_variant:Nnn \clist_pop:NN { c } { T , F , TF }
1382 \prg_generate_conditional_variant:Nnn \clist_gpop:NN { c } { T , F , TF }
1383 % \end{macrocode}
1384 % \end{macro}
1385 % \end{macro}
1386 % \end{macro}
1387 % \end{macro}
1388 %
1389 % \begin{macro}{
1390 % \clist_push:Nn, \clist_push:NV, \clist_push:No, \clist_push:Nx,
1391 % \clist_push:cn, \clist_push:cV, \clist_push:co, \clist_push:cx,
1392 % }
1393 % \UnitTested
1394 % \begin{macro}{
1395 % \clist_gpush:Nn, \clist_gpush:NV, \clist_gpush:No, \clist_gpush:Nx,
1396 % \clist_gpush:cn, \clist_gpush:cV, \clist_gpush:co, \clist_gpush:cx,
1397 % }
1398 % \UnitTested
1399 % Pushing to a comma list is the same as adding on the left.
1400 % \begin{macrocode}
1401 \cs_new_eq:NN \clist_push:Nn \clist_put_left:Nn
1402 \cs_generate_variant:Nn \clist_push:Nn { NV , No , Nx , c , cV , co , cx }
1403 \cs_new_eq:NN \clist_gpush:Nn \clist_gput_left:Nn
1404 \cs_generate_variant:Nn \clist_gpush:Nn { NV , No , Nx , c , cV , co , cx }
1405 % \end{macrocode}
1406 % \end{macro}
1407 % \end{macro}
1408 %
1409 % \subsection{Modifying comma lists}
1410 %
1411 % \begin{variable}{\l_@@_remove_clist, \l_@@_remove_seq}
1412 % An internal comma list and a sequence for the removal routines.
1413 % \begin{macrocode}
1414 \clist_new:N \l_@@_remove_clist
1415 \seq_new:N \l_@@_remove_seq
1416 % \end{macrocode}
1417 % \end{variable}
1418 %
1419 % \begin{macro}{\clist_remove_duplicates:N, \clist_remove_duplicates:c}
1420 % \UnitTested
1421 % \begin{macro}{\clist_gremove_duplicates:N, \clist_gremove_duplicates:c}
1422 % \UnitTested
1423 % \begin{macro}{\@@_remove_duplicates:NN}
1424 % Removing duplicates means making a new list then copying it.
1425 % \begin{macrocode}
1426 \cs_new_protected:Npn \clist_remove_duplicates:N
1427 { \@@_remove_duplicates:NN \clist_set_eq:NN }
1428 \cs_new_protected:Npn \clist_gremove_duplicates:N
1429 { \@@_remove_duplicates:NN \clist_gset_eq:NN }
1430 \cs_new_protected:Npn \@@_remove_duplicates:NN #1#2
1431 {
1432 \clist_clear:N \l_@@_remove_clist
1433 \clist_map_inline:Nn #2
1434 {
1435 \clist_if_in:NnF \l_@@_remove_clist {##1}
1436 {
1437 \tl_put_right:Ne \l_@@_remove_clist
1438 {
1439 \clist_if_empty:NF \l_@@_remove_clist { , }
1440 \@@_if_wrap:nTF {##1} { \exp_not:n { {##1} } } { \exp_not:n {##1} }
1441 }
1442 }
1443 }
1444 #1 #2 \l_@@_remove_clist
1445 }
1446 \cs_generate_variant:Nn \clist_remove_duplicates:N { c }
1447 \cs_generate_variant:Nn \clist_gremove_duplicates:N { c }
1448 % \end{macrocode}
1449 % \end{macro}
1450 % \end{macro}
1451 % \end{macro}
1452 %
1453 % \begin{macro}
1454 % {
1455 % \clist_remove_all:Nn, \clist_remove_all:cn,
1456 % \clist_remove_all:NV, \clist_remove_all:cV,
1457 % \clist_remove_all:Ne, \clist_remove_all:ce
1458 % }
1459 % \UnitTested
1460 % \begin{macro}
1461 % {
1462 % \clist_gremove_all:Nn, \clist_gremove_all:cn,
1463 % \clist_gremove_all:NV, \clist_gremove_all:cV,
1464 % \clist_gremove_all:Ne, \clist_gremove_all:ce
1465 % }
1466 % \UnitTested
1467 % \begin{macro}{\@@_remove_all:NNNn}
1468 % \begin{macro}{\@@_remove_all:w}
1469 % \begin{macro}{\@@_remove_all:}
1470 % The method used here for safe items is very similar to
1471 % \cs{tl_replace_all:Nnn}. However, if the item contains commas or
1472 % leading/trailing spaces, or is empty, or consists of a single brace
1473 % group, we know that it can only appear within braces so the code
1474 % would fail; instead just convert to a sequence and do the removal
1475 % with \pkg{l3seq} code (it involves somewhat elaborate code to do
1476 % most of the work expandably but the final token list comparisons
1477 % non-expandably).
1478 %
1479 % For \enquote{safe} items, build a function delimited by the
1480 % \meta{item} that should be removed,
1481 % surrounded with commas, and call that function followed by
1482 % the expanded comma list, and another copy of the \meta{item}.
1483 % The loop is controlled by the argument grabbed by
1484 % \cs{@@_remove_all:w}: when the item was found,
1485 % the \cs{s_@@_mark} delimiter used is the one inserted by
1486 % \cs{@@_tmp:w}, and \cs{@@_use_none_delimit_by_s_stop:w}
1487 % is deleted. At the end, the final \meta{item} is
1488 % grabbed, and the argument of \cs{@@_tmp:w} contains
1489 % \cs{s_@@_mark}: in that case, \cs{@@_remove_all:w}
1490 % removes the second \cs{s_@@_mark} (inserted by \cs{@@_tmp:w}),
1491 % and lets \cs{@@_use_none_delimit_by_s_stop:w} act.
1492 %
1493 % No brace is lost because items are always grabbed with a leading comma.
1494 % The result of the first assignment has an extra leading comma,
1495 % which we remove in a second assignment.
1496 % Two exceptions: if the clist lost all of its elements, the result
1497 % is empty, and we shouldn't remove anything; if the clist started up
1498 % empty, the first step happens to turn it into a single comma, and
1499 % the second step removes it.
1500 % \begin{macrocode}
1501 \cs_new_protected:Npn \clist_remove_all:Nn
1502 { \@@_remove_all:NNNn \clist_set_from_seq:NN \__kernel_tl_set:Nx }
1503 \cs_new_protected:Npn \clist_gremove_all:Nn
1504 { \@@_remove_all:NNNn \clist_gset_from_seq:NN \__kernel_tl_gset:Nx }
1505 \cs_new_protected:Npn \@@_remove_all:NNNn #1#2#3#4
1506 {
1507 \@@_if_wrap:nTF {#4}
1508 {
1509 \seq_set_from_clist:NN \l_@@_remove_seq #3
1510 \seq_remove_all:Nn \l_@@_remove_seq {#4}
1511 #1 #3 \l_@@_remove_seq
1512 }
1513 {
1514 \cs_set:Npn \@@_tmp:w ##1 , #4 ,
1515 {
1516 ##1
1517 , \s_@@_mark , \@@_use_none_delimit_by_s_stop:w ,
1518 \@@_remove_all:
1519 }
1520 #2 #3
1521 {
1522 \exp_after:wN \@@_remove_all:
1523 #3 , \s_@@_mark , #4 , \s_@@_stop
1524 }
1525 \clist_if_empty:NF #3
1526 {
1527 #2 #3
1528 {
1529 \exp_args:No \exp_not:o
1530 { \exp_after:wN \use_none:n #3 }
1531 }
1532 }
1533 }
1534 }
1535 \cs_new:Npn \@@_remove_all:
1536 { \exp_after:wN \@@_remove_all:w \@@_tmp:w , }
1537 \cs_new:Npn \@@_remove_all:w #1 , \s_@@_mark , #2 , { \exp_not:n {#1} }
1538 \cs_generate_variant:Nn \clist_remove_all:Nn { c , NV , cV , Ne , ce }
1539 \cs_generate_variant:Nn \clist_gremove_all:Nn { c , NV , cV , Ne , ce }
1540 % \end{macrocode}
1541 % \end{macro}
1542 % \end{macro}
1543 % \end{macro}
1544 % \end{macro}
1545 % \end{macro}
1546 %
1547 % \begin{macro}
1548 % {
1549 % \clist_reverse:N, \clist_reverse:c,
1550 % \clist_greverse:N, \clist_greverse:c
1551 % }
1552 % Use \cs{clist_reverse:n} in an \texttt{e}-expanding assignment. The
1553 % extra work that \cs{clist_reverse:n} does to preserve braces and
1554 % spaces would not be needed for the well-controlled case of
1555 % \texttt{N}-type comma lists, but the slow-down is not too bad.
1556 % \begin{macrocode}
1557 \cs_new_protected:Npn \clist_reverse:N #1
1558 { \__kernel_tl_set:Nx #1 { \exp_args:No \clist_reverse:n {#1} } }
1559 \cs_new_protected:Npn \clist_greverse:N #1
1560 { \__kernel_tl_gset:Nx #1 { \exp_args:No \clist_reverse:n {#1} } }
1561 \cs_generate_variant:Nn \clist_reverse:N { c }
1562 \cs_generate_variant:Nn \clist_greverse:N { c }
1563 % \end{macrocode}
1564 % \end{macro}
1565 %
1566 % \begin{macro}[EXP]{\clist_reverse:n}
1567 % \begin{macro}[EXP]{\@@_reverse:wwNww, \@@_reverse_end:ww}
1568 % The reversed token list is built one item at a time, and stored
1569 % between \cs{s_@@_stop} and \cs{s_@@_mark}, in the form of |?| followed by
1570 % zero or more instances of \enquote{\meta{item}\texttt{,}}. We start from a comma
1571 % list \enquote{\meta{item_1}\texttt{,\ldots,}\meta{item_n}}. During the loop,
1572 % the auxiliary \cs{@@_reverse:wwNww} receives \enquote{\texttt{?}\meta{item_i}} as
1573 % |#1|, \enquote{\meta{item_{i+1}}\texttt{,\ldots,}\meta{item_n}} as |#2|,
1574 % \cs{@@_reverse:wwNww} as |#3|, what remains until \cs{s_@@_stop} as
1575 % |#4|, and \enquote{\meta{item_{i-1}}\texttt{,\ldots,}\meta{item_1}\texttt{,}} as |#5|.
1576 % The auxiliary moves |#1| just before |#5|, with a comma, and calls
1577 % itself (|#3|). After the last item is moved, \cs{@@_reverse:wwNww}
1578 % receives \enquote{\cs{s_@@_mark} \cs{@@_reverse:wwNww} \texttt{!}} as its argument
1579 % |#1|, thus \cs{@@_reverse_end:ww} as its argument |#3|. This second
1580 % auxiliary cleans up until the marker~|!|, removes the trailing comma
1581 % (introduced when the first item was moved after \cs{s_@@_stop}), and
1582 % leaves its argument~|#1| within \cs{exp_not:n}. There is also a
1583 % need to remove a leading comma, hence \cs{exp_not:o} and
1584 % \cs{use_none:n}.
1585 % \begin{macrocode}
1586 \cs_new:Npn \clist_reverse:n #1
1587 {
1588 \@@_reverse:wwNww ? #1 ,
1589 \s_@@_mark \@@_reverse:wwNww ! ,
1590 \s_@@_mark \@@_reverse_end:ww
1591 \s_@@_stop ? \s_@@_mark
1592 }
1593 \cs_new:Npn \@@_reverse:wwNww
1594 #1 , #2 \s_@@_mark #3 #4 \s_@@_stop ? #5 \s_@@_mark
1595 { #3 ? #2 \s_@@_mark #3 #4 \s_@@_stop #1 , #5 \s_@@_mark }
1596 \cs_new:Npn \@@_reverse_end:ww #1 ! #2 , \s_@@_mark
1597 { \exp_not:o { \use_none:n #2 } }
1598 % \end{macrocode}
1599 % \end{macro}
1600 % \end{macro}
1601 %
1602 % \begin{macro}
1603 % {\clist_sort:Nn, \clist_sort:cn, \clist_gsort:Nn, \clist_gsort:cn}
1604 % Implemented in \pkg{l3sort}.
1605 % \end{macro}
1606 %
1607 % \subsection{Comma list conditionals}
1608 %
1609 % \begin{macro}[pTF]{\clist_if_empty:N, \clist_if_empty:c}
1610 % \UnitTested
1611 % Simple copies from the token list variable material.
1612 % \begin{macrocode}
1613 \prg_new_eq_conditional:NNn \clist_if_empty:N \tl_if_empty:N
1614 { p , T , F , TF }
1615 \prg_new_eq_conditional:NNn \clist_if_empty:c \tl_if_empty:c
1616 { p , T , F , TF }
1617 % \end{macrocode}
1618 % \end{macro}
1619 %
1620 % \begin{macro}[EXP, pTF]{\clist_if_empty:n}
1621 % \begin{macro}[EXP]{\@@_if_empty_n:w}
1622 % \begin{macro}[EXP]{\@@_if_empty_n:wNw}
1623 % As usual, we insert a token (here |?|) before grabbing
1624 % any argument: this avoids losing braces. The argument
1625 % of \cs{tl_if_empty:oTF} is empty if |#1| is |?| followed
1626 % by blank spaces (besides, this particular variant of
1627 % the emptiness test is optimized). If the item of the
1628 % comma list is blank, grab the next one. As soon as one
1629 % item is non-blank, exit: the second auxiliary grabs
1630 % \cs{prg_return_false:} as |#2|, unless every item in
1631 % the comma list was blank and the loop actually got broken
1632 % by the trailing \cs{s_@@_mark} \cs{prg_return_false:} item.
1633 % \begin{macrocode}
1634 \prg_new_conditional:Npnn \clist_if_empty:n #1 { p , T , F , TF }
1635 {
1636 \@@_if_empty_n:w ? #1
1637 , \s_@@_mark \prg_return_false:
1638 , \s_@@_mark \prg_return_true:
1639 \s_@@_stop
1640 }
1641 \cs_new:Npn \@@_if_empty_n:w #1 ,
1642 {
1643 \tl_if_empty:oTF { \use_none:nn #1 ? }
1644 { \@@_if_empty_n:w ? }
1645 { \@@_if_empty_n:wNw }
1646 }
1647 \cs_new:Npn \@@_if_empty_n:wNw #1 \s_@@_mark #2#3 \s_@@_stop {#2}
1648 % \end{macrocode}
1649 % \end{macro}
1650 % \end{macro}
1651 % \end{macro}
1652 %
1653 % \begin{macro}[TF]
1654 % {
1655 % \clist_if_in:Nn, \clist_if_in:NV, \clist_if_in:No,
1656 % \clist_if_in:cn, \clist_if_in:cV, \clist_if_in:co,
1657 % \clist_if_in:nn, \clist_if_in:nV, \clist_if_in:no
1658 % }
1659 % \begin{macro}{\@@_if_in_return:nnN}
1660 % \UnitTested
1661 % For \enquote{safe} items, we simply surround the comma list, and the
1662 % item, with commas, then use the same code as for \cs{tl_if_in:Nn}.
1663 % For \enquote{unsafe} items we follow the same route as
1664 % \cs{seq_if_in:Nn}, mapping through the list a comparison function.
1665 % If found, return \texttt{true} and remove \cs{prg_return_false:}.
1666 % \begin{macrocode}
1667 \prg_new_protected_conditional:Npnn \clist_if_in:Nn #1#2 { T , F , TF }
1668 {
1669 \exp_args:No \@@_if_in_return:nnN #1 {#2} #1
1670 }
1671 \prg_new_protected_conditional:Npnn \clist_if_in:nn #1#2 { T , F , TF }
1672 {
1673 \clist_set:Nn \l_@@_tmp_clist {#1}
1674 \exp_args:No \@@_if_in_return:nnN \l_@@_tmp_clist {#2}
1675 \l_@@_tmp_clist
1676 }
1677 \cs_new_protected:Npn \@@_if_in_return:nnN #1#2#3
1678 {
1679 \@@_if_wrap:nTF {#2}
1680 {
1681 \cs_set:Npe \@@_tmp:w ##1
1682 {
1683 \exp_not:N \tl_if_eq:nnT {##1}
1684 \exp_not:n
1685 {
1686 {#2}
1687 { \clist_map_break:n { \prg_return_true: \use_none:n } }
1688 }
1689 }
1690 \clist_map_function:NN #3 \@@_tmp:w
1691 \prg_return_false:
1692 }
1693 {
1694 \cs_set:Npn \@@_tmp:w ##1 ,#2, { }
1695 \tl_if_empty:oTF
1696 { \@@_tmp:w ,#1, {} {} ,#2, }
1697 { \prg_return_false: } { \prg_return_true: }
1698 }
1699 }
1700 \prg_generate_conditional_variant:Nnn \clist_if_in:Nn
1701 { NV , No , Ne , c , cV , co , ce } { T , F , TF }
1702 \prg_generate_conditional_variant:Nnn \clist_if_in:nn
1703 { nV , no , ne } { T , F , TF }
1704 % \end{macrocode}
1705 % \end{macro}
1706 % \end{macro}
1707 %
1708 % \subsection{Mapping over comma lists}
1709 %
1710 % \begin{macro}{\clist_map_function:NN, \clist_map_function:cN}
1711 % \UnitTested
1712 % \begin{macro}{\@@_map_function:Nw, \@@_map_function_end:w}
1713 % If the variable is empty, the mapping is skipped (otherwise,
1714 % that comma-list would be seen as consisting of one empty item).
1715 % Then loop over the comma-list, grabbing eight comma-delimited items
1716 % at a time. The end is marked by \cs{s_@@_stop}, which may not appear
1717 % in any of the items. Once the last group of eight items has been
1718 % reached, we go through them more slowly using
1719 % \cs{@@_map_function_end:w}. The auxiliary function
1720 % \cs{@@_map_function:Nw} is also used in some other clist mappings.
1721 % \begin{macrocode}
1722 \cs_new:Npn \clist_map_function:NN #1#2
1723 {
1724 \clist_if_empty:NF #1
1725 {
1726 \exp_after:wN \@@_map_function:Nw \exp_after:wN #2 #1 ,
1727 \s_@@_stop , \s_@@_stop , \s_@@_stop , \s_@@_stop ,
1728 \s_@@_stop , \s_@@_stop , \s_@@_stop , \s_@@_stop ,
1729 \prg_break_point:Nn \clist_map_break: { }
1730 }
1731 }
1732 \cs_new:Npn \@@_map_function:Nw #1 #2, #3, #4, #5, #6, #7, #8, #9,
1733 {
1734 \@@_use_none_delimit_by_s_stop:w
1735 #9 \@@_map_function_end:w \s_@@_stop
1736 #1 {#2} #1 {#3} #1 {#4} #1 {#5} #1 {#6} #1 {#7} #1 {#8} #1 {#9}
1737 \@@_map_function:Nw #1
1738 }
1739 \cs_new:Npn \@@_map_function_end:w \s_@@_stop #1#2
1740 {
1741 \@@_use_none_delimit_by_s_stop:w #2 \clist_map_break: \s_@@_stop
1742 #1 {#2}
1743 \@@_map_function_end:w \s_@@_stop
1744 }
1745 \cs_generate_variant:Nn \clist_map_function:NN { c }
1746 % \end{macrocode}
1747 % \end{macro}
1748 % \end{macro}
1749 %
1750 % \begin{macro}{\clist_map_function:nN, \clist_map_function:eN}
1751 % \UnitTested
1752 % \begin{macro}{\@@_map_function_n:Nn}
1753 % \begin{macro}{\@@_map_unbrace:wn}
1754 % The \texttt{n}-type mapping function is a bit more awkward,
1755 % since spaces must be trimmed from each item.
1756 % Space trimming is again based on \cs{@@_trim_next:w}.
1757 % The auxiliary \cs{@@_map_function_n:Nn} receives as arguments the
1758 % function, and the next non-empty item (after space trimming but
1759 % before brace removal). One level of braces is removed by
1760 % \cs{@@_map_unbrace:wn}.
1761 % \begin{macrocode}
1762 \cs_new:Npn \clist_map_function:nN #1#2
1763 {
1764 \exp_after:wN \@@_map_function_n:Nn \exp_after:wN #2
1765 \exp:w \@@_trim_next:w \prg_do_nothing: #1 ,
1766 \s_@@_stop \clist_map_break: ,
1767 \prg_break_point:Nn \clist_map_break: { }
1768 }
1769 \cs_generate_variant:Nn \clist_map_function:nN { e }
1770 \cs_new:Npn \@@_map_function_n:Nn #1 #2
1771 {
1772 \@@_use_none_delimit_by_s_stop:w #2 \s_@@_stop
1773 \@@_map_unbrace:wn #2 , #1
1774 \exp_after:wN \@@_map_function_n:Nn \exp_after:wN #1
1775 \exp:w \@@_trim_next:w \prg_do_nothing:
1776 }
1777 \cs_new:Npn \@@_map_unbrace:wn #1, #2 { #2 {#1} }
1778 % \end{macrocode}
1779 % \end{macro}
1780 % \end{macro}
1781 % \end{macro}
1782 %
1783 % \begin{macro}{\clist_map_inline:Nn, \clist_map_inline:cn}
1784 % \UnitTested
1785 % \begin{macro}{\clist_map_inline:nn}
1786 % \UnitTested
1787 % Inline mapping is done by creating a suitable function
1788 % \enquote{on the fly}: this is done globally to avoid
1789 % any issues with \TeX{}'s groups. We use a different
1790 % function for each level of nesting.
1791 %
1792 % Since the mapping is non-expandable, we can perform
1793 % the space-trimming needed by the \texttt{n} version
1794 % simply by storing the comma-list in a variable. We
1795 % don't need a different comma-list for each nesting
1796 % level: the comma-list is expanded before the mapping
1797 % starts.
1798 % \begin{macrocode}
1799 \cs_new_protected:Npn \clist_map_inline:Nn #1#2
1800 {
1801 \clist_if_empty:NF #1
1802 {
1803 \int_gincr:N \g__kernel_prg_map_int
1804 \cs_gset_protected:cpn
1805 { @@_map_ \int_use:N \g__kernel_prg_map_int :w } ##1 {#2}
1806 \exp_last_unbraced:Nco \@@_map_function:Nw
1807 { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
1808 #1 ,
1809 \s_@@_stop , \s_@@_stop , \s_@@_stop , \s_@@_stop ,
1810 \s_@@_stop , \s_@@_stop , \s_@@_stop , \s_@@_stop ,
1811 \prg_break_point:Nn \clist_map_break:
1812 { \int_gdecr:N \g__kernel_prg_map_int }
1813 }
1814 }
1815 \cs_new_protected:Npn \clist_map_inline:nn #1
1816 {
1817 \clist_set:Nn \l_@@_tmp_clist {#1}
1818 \clist_map_inline:Nn \l_@@_tmp_clist
1819 }
1820 \cs_generate_variant:Nn \clist_map_inline:Nn { c }
1821 % \end{macrocode}
1822 % \end{macro}
1823 % \end{macro}
1824 %
1825 % \begin{macro}{\clist_map_variable:NNn, \clist_map_variable:cNn}
1826 % \UnitTested
1827 % \begin{macro}{\clist_map_variable:nNn}
1828 % \begin{macro}{\@@_map_variable:Nnn}
1829 % The |N|-type version is a straightforward application of
1830 % \cs{clist_map_tokens:Nn}, calling \cs{@@_map_variable:Nnn} for each
1831 % item to assign the variable and run the user's code. The |n|-type
1832 % version is \emph{not} implemented in terms of the |n|-type function
1833 % \cs{clist_map_tokens:Nn}, because here we are allowed to clean up
1834 % the |n|-type comma list non-expandably.
1835 % \begin{macrocode}
1836 \cs_new_protected:Npn \clist_map_variable:NNn #1#2#3
1837 { \clist_map_tokens:Nn #1 { \@@_map_variable:Nnn #2 {#3} } }
1838 \cs_generate_variant:Nn \clist_map_variable:NNn { c }
1839 \cs_new_protected:Npn \@@_map_variable:Nnn #1#2#3
1840 { \tl_set:Nn #1 {#3} #2 }
1841 \cs_new_protected:Npn \clist_map_variable:nNn #1
1842 {
1843 \clist_set:Nn \l_@@_tmp_clist {#1}
1844 \clist_map_variable:NNn \l_@@_tmp_clist
1845 }
1846 % \end{macrocode}
1847 % \end{macro}
1848 % \end{macro}
1849 % \end{macro}
1850 %
1851 % \begin{macro}{\clist_map_tokens:Nn, \clist_map_tokens:cn}
1852 % \begin{macro}{\@@_map_tokens:nw, \@@_map_tokens_end:w}
1853 % Essentially a copy of \cs{clist_map_function:NN} with braces added.
1854 % \begin{macrocode}
1855 \cs_new:Npn \clist_map_tokens:Nn #1#2
1856 {
1857 \clist_if_empty:NF #1
1858 {
1859 \exp_last_unbraced:Nno \@@_map_tokens:nw {#2} #1 ,
1860 \s_@@_stop , \s_@@_stop , \s_@@_stop , \s_@@_stop ,
1861 \s_@@_stop , \s_@@_stop , \s_@@_stop , \s_@@_stop ,
1862 \prg_break_point:Nn \clist_map_break: { }
1863 }
1864 }
1865 \cs_new:Npn \@@_map_tokens:nw #1 #2, #3, #4, #5, #6, #7, #8, #9,
1866 {
1867 \@@_use_none_delimit_by_s_stop:w
1868 #9 \@@_map_tokens_end:w \s_@@_stop
1869 \use:n {#1} {#2} \use:n {#1} {#3} \use:n {#1} {#4} \use:n {#1} {#5}
1870 \use:n {#1} {#6} \use:n {#1} {#7} \use:n {#1} {#8} \use:n {#1} {#9}
1871 \@@_map_tokens:nw {#1}
1872 }
1873 \cs_new:Npn \@@_map_tokens_end:w \s_@@_stop \use:n #1#2
1874 {
1875 \@@_use_none_delimit_by_s_stop:w #2 \clist_map_break: \s_@@_stop
1876 #1 {#2}
1877 \@@_map_tokens_end:w \s_@@_stop
1878 }
1879 \cs_generate_variant:Nn \clist_map_tokens:Nn { c }
1880 % \end{macrocode}
1881 % \end{macro}
1882 % \end{macro}
1883 %
1884 % \begin{macro}{\clist_map_tokens:nn, \@@_map_tokens_n:nw}
1885 % Similar to \cs{clist_map_function:nN} but with a different way of
1886 % grabbing items because we cannot use \cs{exp_after:wN} to pass the
1887 % \meta{code}.
1888 % \begin{macrocode}
1889 \cs_new:Npn \clist_map_tokens:nn #1#2
1890 {
1891 \@@_map_tokens_n:nw {#2}
1892 \prg_do_nothing: #1 , \s_@@_stop \clist_map_break: ,
1893 \prg_break_point:Nn \clist_map_break: { }
1894 }
1895 \cs_new:Npn \@@_map_tokens_n:nw #1#2 ,
1896 {
1897 \tl_if_empty:oF { \use_none:nn #2 ? }
1898 {
1899 \@@_use_none_delimit_by_s_stop:w #2 \s_@@_stop
1900 \tl_trim_spaces_apply:oN {#2} \use_ii_i:nn
1901 \@@_map_unbrace:wn , {#1}
1902 }
1903 \@@_map_tokens_n:nw {#1} \prg_do_nothing:
1904 }
1905 % \end{macrocode}
1906 % \end{macro}
1907 %
1908 % \begin{macro}{\clist_map_break:, \clist_map_break:n}
1909 % The break statements use the general \cs{prg_map_break:Nn} mechanism.
1910 % \begin{macrocode}
1911 \cs_new:Npn \clist_map_break:
1912 { \prg_map_break:Nn \clist_map_break: { } }
1913 \cs_new:Npn \clist_map_break:n
1914 { \prg_map_break:Nn \clist_map_break: }
1915 % \end{macrocode}
1916 % \end{macro}
1917 %
1918 % \begin{macro}{\clist_count:N, \clist_count:c, \clist_count:n, \clist_count:e}
1919 % \begin{macro}{\@@_count:n}
1920 % \begin{macro}{\@@_count:w}
1921 % Counting the items in a comma list is done using the same approach as for
1922 % other token count functions: turn each entry into a \texttt{+1} then use
1923 % integer evaluation to actually do the mathematics.
1924 % In the case of an \texttt{n}-type comma-list, we could of course use
1925 % \cs{clist_map_function:nN}, but that is very slow, because it carefully
1926 % removes spaces. Instead, we loop manually, and skip blank items
1927 % (but not |{}|, hence the extra spaces).
1928 % \begin{macrocode}
1929 \cs_new:Npn \clist_count:N #1
1930 {
1931 \int_eval:n
1932 {
1933 0
1934 \clist_map_function:NN #1 \@@_count:n
1935 }
1936 }
1937 \cs_generate_variant:Nn \clist_count:N { c }
1938 \cs_new:Npn \@@_count:n #1 { + 1 }
1939 \cs_set_protected:Npn \@@_tmp:w #1
1940 {
1941 \cs_new:Npn \clist_count:n ##1
1942 {
1943 \int_eval:n
1944 {
1945 0
1946 \@@_count:w #1
1947 ##1 , \s_@@_stop \prg_break: , \prg_break_point:
1948 }
1949 }
1950 \cs_new:Npn \@@_count:w ##1 ,
1951 {
1952 \@@_use_none_delimit_by_s_stop:w ##1 \s_@@_stop
1953 \tl_if_blank:nF {##1} { + 1 }
1954 \@@_count:w #1
1955 }
1956 }
1957 \exp_args:No \@@_tmp:w \c_space_tl
1958 \cs_generate_variant:Nn \clist_count:n { e }
1959 % \end{macrocode}
1960 % \end{macro}
1961 % \end{macro}
1962 % \end{macro}
1963 %
1964 % \subsection{Using comma lists}
1965 %
1966 % \begin{macro}[EXP]{\clist_use:Nnnn, \clist_use:cnnn}
1967 % \begin{macro}[EXP]
1968 % {\@@_use:wwn, \@@_use:nwwwwnwn, \@@_use:nwwn}
1969 % \begin{macro}[EXP]{\clist_use:Nn, \clist_use:cn}
1970 % First check that the variable exists. Then count the items in the
1971 % comma list. If it has none, output nothing. If it has one item,
1972 % output that item, brace stripped (note that space-trimming has
1973 % already been done when the comma list was assigned). If it has two,
1974 % place the \meta{separator~between~two} in the middle.
1975 %
1976 % Otherwise, \cs{@@_use:nwwwwnwn} takes the following arguments; 1:
1977 % a \meta{separator}, 2, 3, 4: three items from the comma list (or
1978 % quarks), 5: the rest of the comma list, 6: a \meta{continuation}
1979 % function (\texttt{use_ii} or \texttt{use_iii} with its
1980 % \meta{separator} argument), 7: junk, and 8: the temporary result,
1981 % which is built in a brace group following \cs{s_@@_stop}. The
1982 % \meta{separator} and the first of the three items are placed in the
1983 % result, then we use the \meta{continuation}, placing the remaining
1984 % two items after it. When we begin this loop, the three items really
1985 % belong to the comma list, the first \cs{s_@@_mark} is taken as a
1986 % delimiter to the \texttt{use_ii} function, and the continuation is
1987 % \texttt{use_ii} itself. When we reach the last two items of the
1988 % original token list, \cs{s_@@_mark} is taken as a third item, and now
1989 % the second \cs{s_@@_mark} serves as a delimiter to \texttt{use_ii},
1990 % switching to the other \meta{continuation}, \texttt{use_iii}, which
1991 % uses the \meta{separator between final two}.
1992 % \begin{macrocode}
1993 \cs_new:Npn \clist_use:Nnnn #1#2#3#4
1994 {
1995 \clist_if_exist:NTF #1
1996 {
1997 \int_case:nnF { \clist_count:N #1 }
1998 {
1999 { 0 } { }
2000 { 1 } { \exp_after:wN \@@_use:wwn #1 , , { } }
2001 { 2 } { \exp_after:wN \@@_use:wwn #1 , {#2} }
2002 }
2003 {
2004 \exp_after:wN \@@_use:nwwwwnwn
2005 \exp_after:wN { \exp_after:wN } #1 ,
2006 \s_@@_mark , { \@@_use:nwwwwnwn {#3} }
2007 \s_@@_mark , { \@@_use:nwwn {#4} }
2008 \s_@@_stop { }
2009 }
2010 }
2011 {
2012 \msg_expandable_error:nnn
2013 { kernel } { bad-variable } {#1}
2014 }
2015 }
2016 \cs_generate_variant:Nn \clist_use:Nnnn { c }
2017 \cs_new:Npn \@@_use:wwn #1 , #2 , #3 { \exp_not:n { #1 #3 #2 } }
2018 \cs_new:Npn \@@_use:nwwwwnwn
2019 #1#2 , #3 , #4 , #5 \s_@@_mark , #6#7 \s_@@_stop #8
2020 { #6 {#3} , {#4} , #5 \s_@@_mark , {#6} #7 \s_@@_stop { #8 #1 #2 } }
2021 \cs_new:Npn \@@_use:nwwn #1#2 , #3 \s_@@_stop #4
2022 { \exp_not:n { #4 #1 #2 } }
2023 \cs_new:Npn \clist_use:Nn #1#2
2024 { \clist_use:Nnnn #1 {#2} {#2} {#2} }
2025 \cs_generate_variant:Nn \clist_use:Nn { c }
2026 % \end{macrocode}
2027 % \end{macro}
2028 % \end{macro}
2029 % \end{macro}
2030 %
2031 % \begin{macro}{\clist_use:N, \clist_use:c}
2032 % \begin{macrocode}
2033 \cs_new_eq:NN \clist_use:N \tl_use:N
2034 \cs_generate_variant:Nn \clist_use:N { c }
2035 % \end{macrocode}
2036 % \end{macro}
2037 %
2038 % \begin{macro}
2039 % {
2040 % \clist_use:nnnn, \clist_use:nn, \@@_use:Nw,
2041 % \@@_use_one:w, \@@_use_end:w, \@@_use_more:w
2042 % }
2043 % Items are grabbed by \cs{@@_use:Nw}, which detects blank items with
2044 % a \cs{tl_if_empty:oTF} test (in which case it recurses). Non-blank
2045 % items are either the end of the list, in which case the argument
2046 % |#1| of \cs{@@_use:Nw} is used to properly end the list, or are
2047 % normal items, which must be trimmed and properly unbraced. As we
2048 % find successive items, the long list of \cs{@@_use:Nw} calls gets
2049 % shortened and we end up calling \cs{@@_use_more:w} once we have
2050 % found $3$ items. This auxiliary leaves the first-found item and the
2051 % general separator, and calls \cs{@@_use:Nw} to find more items.
2052 % A subtlety is that we use \cs{@@_use_end:w} both in the case of a
2053 % two-item list and for the last two items of a general list: to get
2054 % the correct separator, \cs{@@_use_more:w} replaces the
2055 % separator-of-two by the last-separator when called, namely as soon
2056 % as we have found three items.
2057 % \begin{macrocode}
2058 \cs_new:Npn \clist_use:nnnn #1#2#3#4
2059 {
2060 \@@_use:Nw \@@_use_none_delimit_by_s_stop:w
2061 \@@_use:Nw \@@_use_one:w
2062 \@@_use:Nw \@@_use_end:w
2063 \@@_use_more:w ;
2064 {#2} {#3} {#4} ;
2065 \prg_do_nothing: #1 , \s_@@_mark ,
2066 \s_@@_stop
2067 }
2068 \cs_new:Npn \@@_use:Nw #1#2 ; #3 ; #4 ,
2069 {
2070 \tl_if_empty:oTF { \use_none:nn #4 ? }
2071 { \@@_use:Nw #1#2 ; }
2072 {
2073 \@@_use_none_delimit_by_s_mark:w #4 #1 \s_@@_mark
2074 \tl_trim_spaces_apply:oN {#4} \use_ii_i:nn
2075 \@@_map_unbrace:wn , { #2 ; }
2076 }
2077 #3 ; \prg_do_nothing:
2078 }
2079 \cs_new:Npn \@@_use_one:w \s_@@_mark #1 , #2#3#4 \s_@@_stop
2080 { \exp_not:n {#3} }
2081 \cs_new:Npn \@@_use_end:w
2082 \s_@@_mark #1 , #2#3#4#5#6 \s_@@_stop
2083 { \exp_not:n { #4 #5 #3 } }
2084 \cs_new:Npn \@@_use_more:w ; #1#2#3#4#5#6 ;
2085 {
2086 \exp_not:n { #3 #5 }
2087 \@@_use:Nw \@@_use_end:w \@@_use_more:w ;
2088 {#1} {#2} {#6} {#5} {#6} ;
2089 }
2090 \cs_new:Npn \clist_use:nn #1#2 { \clist_use:nnnn {#1} {#2} {#2} {#2} }
2091 % \end{macrocode}
2092 % \end{macro}
2093 %
2094 % \subsection{Using a single item}
2095 %
2096 % \begin{macro}{\clist_item:Nn, \clist_item:cn}
2097 % \begin{macro}{\@@_item:nnnN, \@@_item:ffoN, \@@_item:ffnN}
2098 % \begin{macro}{\@@_item_N_loop:nw}
2099 % To avoid needing to test the end of the list at each step,
2100 % we first compute the \meta{length} of the list. If the item number
2101 % is~$0$, less than $-\meta{length}$, or more than $\meta{length}$,
2102 % the result is empty. If it is negative, but not less than $-\meta{length}$,
2103 % add $\meta{length}+1$ to the item number before performing the loop.
2104 % The loop itself is very simple, return the item if the counter
2105 % reached~$1$, otherwise, decrease the counter and repeat.
2106 % \begin{macrocode}
2107 \cs_new:Npn \clist_item:Nn #1#2
2108 {
2109 \@@_item:ffoN
2110 { \clist_count:N #1 }
2111 { \int_eval:n {#2} }
2112 #1
2113 \@@_item_N_loop:nw
2114 }
2115 \cs_new:Npn \@@_item:nnnN #1#2#3#4
2116 {
2117 \int_compare:nNnTF {#2} < 0
2118 {
2119 \int_compare:nNnTF {#2} < { - #1 }
2120 { \@@_use_none_delimit_by_s_stop:w }
2121 { \exp_args:Nf #4 { \int_eval:n { #2 + 1 + #1 } } }
2122 }
2123 {
2124 \int_compare:nNnTF {#2} > {#1}
2125 { \@@_use_none_delimit_by_s_stop:w }
2126 { #4 {#2} }
2127 }
2128 { } , #3 , \s_@@_stop
2129 }
2130 \cs_generate_variant:Nn \@@_item:nnnN { ffo, ff }
2131 \cs_new:Npn \@@_item_N_loop:nw #1 #2,
2132 {
2133 \int_compare:nNnTF {#1} = 0
2134 { \@@_use_i_delimit_by_s_stop:nw { \exp_not:n {#2} } }
2135 { \exp_args:Nf \@@_item_N_loop:nw { \int_eval:n { #1 - 1 } } }
2136 }
2137 \cs_generate_variant:Nn \clist_item:Nn { c }
2138 % \end{macrocode}
2139 % \end{macro}
2140 % \end{macro}
2141 % \end{macro}
2142 %
2143 % \begin{macro}
2144 % {
2145 % \clist_item:nn,
2146 % \clist_item:Vn,
2147 % \clist_item:vn,
2148 % \clist_item:on,
2149 % \clist_item:en
2150 % }
2151 % \begin{macro}{
2152 % \@@_item_n:nw,
2153 % \@@_item_n_loop:nw,
2154 % \@@_item_n_end:n,
2155 % \@@_item_n_strip:n,
2156 % \@@_item_n_strip:w}
2157 % This starts in the same way as \cs{clist_item:Nn} by counting the items
2158 % of the comma list. The final item should be space-trimmed before being
2159 % brace-stripped, hence we insert a couple of odd-looking
2160 % \cs{prg_do_nothing:} to avoid losing braces. Blank items are ignored.
2161 % \begin{macrocode}
2162 \cs_new:Npn \clist_item:nn #1#2
2163 {
2164 \@@_item:ffnN
2165 { \clist_count:n {#1} }
2166 { \int_eval:n {#2} }
2167 {#1}
2168 \@@_item_n:nw
2169 }
2170 \cs_generate_variant:Nn \clist_item:nn { V , v , o , e }
2171 \cs_new:Npn \@@_item_n:nw #1
2172 { \@@_item_n_loop:nw {#1} \prg_do_nothing: }
2173 \cs_new:Npn \@@_item_n_loop:nw #1 #2,
2174 {
2175 \exp_args:No \tl_if_blank:nTF {#2}
2176 { \@@_item_n_loop:nw {#1} \prg_do_nothing: }
2177 {
2178 \int_compare:nNnTF {#1} = 0
2179 { \exp_args:No \@@_item_n_end:n {#2} }
2180 {
2181 \exp_args:Nf \@@_item_n_loop:nw
2182 { \int_eval:n { #1 - 1 } }
2183 \prg_do_nothing:
2184 }
2185 }
2186 }
2187 \cs_new:Npn \@@_item_n_end:n #1 #2 \s_@@_stop
2188 { \tl_trim_spaces_apply:nN {#1} \@@_item_n_strip:n }
2189 \cs_new:Npn \@@_item_n_strip:n #1 { \@@_item_n_strip:w #1 , }
2190 \cs_new:Npn \@@_item_n_strip:w #1 , { \exp_not:n {#1} }
2191 % \end{macrocode}
2192 % \end{macro}
2193 % \end{macro}
2194 %
2195 % \begin{macro}{\clist_rand_item:n, \clist_rand_item:N, \clist_rand_item:c}
2196 % \begin{macro}{\@@_rand_item:nn}
2197 % The |N|-type function is not implemented through the |n|-type
2198 % function for efficiency: for instance comma-list variables do not
2199 % require space-trimming of their items. Even testing for emptiness
2200 % of an |n|-type comma-list is slow, so we count items first and use
2201 % that both for the emptiness test and the pseudo-random integer.
2202 % Importantly, \cs{clist_item:Nn} and \cs{clist_item:nn} only evaluate
2203 % their argument once.
2204 % \begin{macrocode}
2205 \cs_new:Npn \clist_rand_item:n #1
2206 { \exp_args:Nf \@@_rand_item:nn { \clist_count:n {#1} } {#1} }
2207 \cs_new:Npn \@@_rand_item:nn #1#2
2208 {
2209 \int_compare:nNnF {#1} = 0
2210 { \clist_item:nn {#2} { \int_rand:nn { 1 } {#1} } }
2211 }
2212 \cs_new:Npn \clist_rand_item:N #1
2213 {
2214 \clist_if_empty:NF #1
2215 { \clist_item:Nn #1 { \int_rand:nn { 1 } { \clist_count:N #1 } } }
2216 }
2217 \cs_generate_variant:Nn \clist_rand_item:N { c }
2218 % \end{macrocode}
2219 % \end{macro}
2220 % \end{macro}
2221 %
2222 % \subsection{Viewing comma lists}
2223 %
2224 % \begin{macro}{\clist_show:N, \clist_show:c, \clist_log:N, \clist_log:c, \@@_show:NN}
2225 % Apply the general \cs{__kernel_chk_tl_type:NnnT} with \cs{exp_not:o}
2226 % |#2| serving as a dummy code to prevent a check performed by this
2227 % auxiliary.
2228 % \begin{macrocode}
2229 \cs_new_protected:Npn \clist_show:N { \@@_show:NN \msg_show:nneeee }
2230 \cs_generate_variant:Nn \clist_show:N { c }
2231 \cs_new_protected:Npn \clist_log:N { \@@_show:NN \msg_log:nneeee }
2232 \cs_generate_variant:Nn \clist_log:N { c }
2233 \cs_new_protected:Npn \@@_show:NN #1#2
2234 {
2235 \__kernel_chk_tl_type:NnnT #2 { clist } { \exp_not:o #2 }
2236 {
2237 \int_compare:nNnTF { \clist_count:N #2 }
2238 = { \exp_args:No \clist_count:n #2 }
2239 {
2240 #1 { clist } { show }
2241 { \token_to_str:N #2 }
2242 { \clist_map_function:NN #2 \msg_show_item:n }
2243 { } { }
2244 }
2245 {
2246 \msg_error:nnee { clist } { non-clist }
2247 { \token_to_str:N #2 } { \tl_to_str:N #2 }
2248 }
2249 }
2250 }
2251 % \end{macrocode}
2252 % \end{macro}
2253 %
2254 % \begin{macro}{\clist_show:n, \clist_log:n, \@@_show:Nn}
2255 % A variant of the above: no existence check, empty first argument for
2256 % the message.
2257 % \begin{macrocode}
2258 \cs_new_protected:Npn \clist_show:n { \@@_show:Nn \msg_show:nneeee }
2259 \cs_new_protected:Npn \clist_log:n { \@@_show:Nn \msg_log:nneeee }
2260 \cs_new_protected:Npn \@@_show:Nn #1#2
2261 {
2262 #1 { clist } { show }
2263 { } { \clist_map_function:nN {#2} \msg_show_item:n } { } { }
2264 }
2265 % \end{macrocode}
2266 % \end{macro}
2267 %
2268 % \subsection{Scratch comma lists}
2269 %
2270 % \begin{variable}{\l_tmpa_clist, \l_tmpb_clist}
2271 % \begin{variable}{\g_tmpa_clist, \g_tmpb_clist}
2272 % Temporary comma list variables.
2273 % \begin{macrocode}
2274 \clist_new:N \l_tmpa_clist
2275 \clist_new:N \l_tmpb_clist
2276 \clist_new:N \g_tmpa_clist
2277 \clist_new:N \g_tmpb_clist
2278 % \end{macrocode}
2279 % \end{variable}
2280 % \end{variable}
2281 %
2282 % \begin{macrocode}
2283 %</code>
2284 % \end{macrocode}
2285 %
2286 % \end{implementation}
2287 %
2288 % \PrintIndex