Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3flag.dtx
4 %
5 % Copyright (C) 2011-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3flag} module\\ Expandable flags^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % Flags are the only data-type that can be modified in expansion-only
53 % contexts. This module is meant mostly for kernel use: in almost all
54 % cases, booleans or integers should be preferred to flags because they
55 % are very significantly faster.
56 %
57 % A flag can hold any (small) non-negative value, which we call its
58 % \meta{height}. In expansion-only contexts, a flag can only be
59 % \enquote{raised}: this increases the \meta{height} by $1$. The \meta{height}
60 % can also be queried expandably. However, decreasing it, or setting it
61 % to zero requires non-expandable assignments.
62 %
63 % Flag variables are always local.
64 %
65 % A typical use case of flags would be to keep track of whether an
66 % exceptional condition has occurred during expandable processing, and
67 % produce a meaningful (non-expandable) message after the end of the
68 % expandable processing. This is exemplified by \pkg{l3str-convert},
69 % which for performance reasons performs conversions of individual
70 % characters expandably and for readability reasons produces a single
71 % error message describing incorrect inputs that were encountered.
72 %
73 % Flags should not be used without carefully considering the fact that
74 % raising a flag takes a time and memory proportional to its height and
75 % that the memory cannot be reclaimed even if the flag is cleared.
76 % Flags should not be used unless it is unavoidable.
77 %
78 % In earlier versions, flags were referenced by an \texttt{n}-type
79 % \meta{flag name} such as \texttt{fp_overflow}, used as part of
80 % \cs{use:c} constructions. All of the commands described below have
81 % \texttt{n}-type analogues that can still appear in old code, but the
82 % \texttt{N}-type commands are to be preferred moving forward. The
83 % \texttt{n}-type \meta{flag name} is simply mapped to
84 % \cs[no-index]{l_\meta{flag name}_flag}, which makes it easier for
85 % packages using public flags (such as \pkg{l3fp}) to retain backwards
86 % compatibility.
87 %
88 % \section{Setting up flags}
89 %
90 % \begin{function}[added = 2024-01-12]{\flag_new:N, \flag_new:c}
91 % \begin{syntax}
92 % \cs{flag_new:N} \meta{flag~var}
93 % \end{syntax}
94 % Creates a new \meta{flag~var}, or raises an error if the name is
95 % already taken. The declaration is global, but flags are always local
96 % variables. The \meta{flag~var} initially has zero height.
97 % \end{function}
98 %
99 % \begin{function}[added = 2024-01-12]{\flag_clear:N, \flag_clear:c}
100 % \begin{syntax}
101 % \cs{flag_clear:N} \meta{flag~var}
102 % \end{syntax}
103 % Sets the height of the \meta{flag~var} to zero. The assignment is local.
104 % \end{function}
105 %
106 % \begin{function}[added = 2024-01-12]{\flag_clear_new:N, \flag_clear_new:c}
107 % \begin{syntax}
108 % \cs{flag_clear_new:N} \meta{flag~var}
109 % \end{syntax}
110 % Ensures that the \meta{flag~var} exists globally by applying
111 % \cs{flag_new:N} if necessary, then applies \cs{flag_clear:N}, setting
112 % the height to zero locally.
113 % \end{function}
114 %
115 % \begin{function}[added = 2024-01-12]{\flag_show:N, \flag_show:c}
116 % \begin{syntax}
117 % \cs{flag_show:N} \meta{flag~var}
118 % \end{syntax}
119 % Displays the height of the \meta{flag~var} in the terminal.
120 % \end{function}
121 %
122 % \begin{function}[added = 2024-01-12]{\flag_log:N, \flag_log:c}
123 % \begin{syntax}
124 % \cs{flag_log:N} \meta{flag~var}
125 % \end{syntax}
126 % Writes the height of the \meta{flag~var} in the log file.
127 % \end{function}
128 %
129 % \section{Expandable flag commands}
130 %
131 % \begin{function}[EXP, pTF, added = 2024-01-12]{\flag_if_exist:N, \flag_if_exist:c}
132 % \begin{syntax}
133 % \cs{flag_if_exist_p:N} \meta{flag~var}
134 % \cs{flag_if_exist:NTF} \meta{flag~var} \Arg{true code} \Arg{false code}
135 % \end{syntax}
136 % This function returns \texttt{true} if the \meta{flag~var} is
137 % currently defined, and \texttt{false} otherwise. This does not check
138 % that the \meta{flag~var} really is a flag variable.
139 % \end{function}
140 %
141 % \begin{function}[EXP, pTF, added = 2024-01-12]{\flag_if_raised:N, \flag_if_raised:c}
142 % \begin{syntax}
143 % \cs{flag_if_raised_p:N} \meta{flag~var}
144 % \cs{flag_if_raised:NTF} \meta{flag~var} \Arg{true code} \Arg{false code}
145 % \end{syntax}
146 % This function returns \texttt{true} if the \meta{flag~var} has non-zero
147 % height, and \texttt{false} if the \meta{flag~var} has zero height.
148 % \end{function}
149 %
150 % \begin{function}[EXP, added = 2024-01-12]{\flag_height:N, \flag_height:c}
151 % \begin{syntax}
152 % \cs{flag_height:N} \meta{flag~var}
153 % \end{syntax}
154 % Expands to the height of the \meta{flag~var} as an integer denotation.
155 % \end{function}
156 %
157 % \begin{function}[EXP, added = 2024-01-12]{\flag_raise:N, \flag_raise:c}
158 % \begin{syntax}
159 % \cs{flag_raise:N} \meta{flag~var}
160 % \end{syntax}
161 % The height of \meta{flag~var} is increased by $1$ locally.
162 % \end{function}
163 %
164 % \begin{function}[EXP, added = 2024-01-12]{\flag_ensure_raised:N, \flag_ensure_raised:c}
165 % \begin{syntax}
166 % \cs{flag_ensure_raised:N} \meta{flag~var}
167 % \end{syntax}
168 % Ensures the \meta{flag~var} is raised by making its height at least~$1$,
169 % locally.
170 % \end{function}
171 %
172 % \begin{variable}[added = 2024-01-12]{\l_tmpa_flag, \l_tmpb_flag}
173 % Scratch flag for local assignment. These are never used by
174 % the kernel code, and so are safe for use with any \LaTeX3-defined
175 % function. However, they may be overwritten by other non-kernel
176 % code and so should only be used for short-term storage.
177 % \end{variable}
178 %
179 % \end{documentation}
180 %
181 % \begin{implementation}
182 %
183 % \section{\pkg{l3flag} implementation}
184 %
185 % \begin{macrocode}
186 %<*code>
187 % \end{macrocode}
188 %
189 % \begin{macrocode}
190 %<@@=flag>
191 % \end{macrocode}
192 %
193 % \TestFiles{m3flag001}
194 %
195 % \begin{macro}{\@@_sep:}
196 % \begin{macrocode}
197 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
198 % \end{macrocode}
199 % \end{macro}
200 %
201 % \subsection{Protected flag commands}
202 %
203 % The height $h$ of a flag (which is initially zero) is stored by
204 % setting control sequences of the form \cs[no-index]{\meta{flag
205 % name}\meta{integer}} to \tn{relax} for $0\leq\meta{integer}<h$. These
206 % control sequences are produced by \cs{cs:w} \meta{flag~var}
207 % \meta{integer} \cs{cs_end:}, namely the \meta{flag~var} is actually a
208 % (protected) macro expanding to its own csname.
209 %
210 % \begin{macro}{\flag_new:N, \flag_new:c}
211 % Evaluate the csname of~|#1| for use in constructing the various
212 % indexed macros.
213 % \begin{macrocode}
214 \cs_new_protected:Npn \flag_new:N #1
215 { \cs_new_protected:Npe #1 { \cs_to_str:N #1 } }
216 \cs_generate_variant:Nn \flag_new:N { c }
217 % \end{macrocode}
218 % \end{macro}
219 %
220 % \begin{variable}{\l_tmpa_flag, \l_tmpb_flag}
221 % Two flag variables for scratch use.
222 % \begin{macrocode}
223 \flag_new:N \l_tmpa_flag
224 \flag_new:N \l_tmpb_flag
225 % \end{macrocode}
226 % \end{variable}
227 %
228 % \begin{macro}{\flag_clear:N, \flag_clear:c}
229 % \begin{macro}{\@@_clear:wN}
230 % Undefine control sequences, starting from the |0| flag, upwards,
231 % until reaching an undefined control sequence. We don't use
232 % \cs{cs_undefine:c} because that would act globally.
233 % \begin{macrocode}
234 \cs_new_protected:Npn \flag_clear:N #1
235 {
236 \@@_clear:wN 0 \@@_sep: #1
237 \prg_break_point:
238 }
239 \cs_generate_variant:Nn \flag_clear:N { c }
240 \cs_new_protected:Npn \@@_clear:wN #1 \@@_sep: #2
241 {
242 \if_cs_exist:w #2 #1 \cs_end: \else:
243 \prg_break:n
244 \fi:
245 \cs_set_eq:cN { #2 #1 } \tex_undefined:D
246 \exp_after:wN \@@_clear:wN
247 \int_value:w \int_eval:w \c_one_int + #1 \@@_sep: #2
248 }
249 % \end{macrocode}
250 % \end{macro}
251 % \end{macro}
252 %
253 % \begin{macro}{\flag_clear_new:N, \flag_clear_new:c}
254 % As for other datatypes, clear the \meta{flag~var} or create a new one,
255 % as appropriate.
256 % \begin{macrocode}
257 \cs_new_protected:Npn \flag_clear_new:N #1
258 { \flag_if_exist:NTF #1 { \flag_clear:N } { \flag_new:N } #1 }
259 \cs_generate_variant:Nn \flag_clear_new:N { c }
260 % \end{macrocode}
261 % \end{macro}
262 %
263 % \begin{macro}{\flag_show:N, \flag_show:c, \flag_log:N, \flag_log:c, \@@_show:NN}
264 % Show the height (terminal or log file) using appropriate \pkg{l3msg}
265 % auxiliaries.
266 % \begin{macrocode}
267 \cs_new_protected:Npn \flag_show:N { \@@_show:NN \tl_show:n }
268 \cs_generate_variant:Nn \flag_show:N { c }
269 \cs_new_protected:Npn \flag_log:N { \@@_show:NN \tl_log:n }
270 \cs_generate_variant:Nn \flag_log:N { c }
271 \cs_new_protected:Npn \@@_show:NN #1#2
272 {
273 \__kernel_chk_defined:NT #2
274 { \exp_args:Ne #1 { \tl_to_str:n { #2 height } = \flag_height:N #2 } }
275 }
276 % \end{macrocode}
277 % \end{macro}
278 %
279 % \subsection{Expandable flag commands}
280 %
281 % \begin{macro}[EXP, pTF]{\flag_if_exist:N, \flag_if_exist:c}
282 % Copies of the \texttt{cs} functions defined in \pkg{l3basics}.
283 % \begin{macrocode}
284 \prg_new_eq_conditional:NNn \flag_if_exist:N \cs_if_exist:N
285 { TF , T , F , p }
286 \prg_new_eq_conditional:NNn \flag_if_exist:c \cs_if_exist:c
287 { TF , T , F , p }
288 % \end{macrocode}
289 % \end{macro}
290 %
291 % \begin{macro}[EXP, pTF]{\flag_if_raised:N, \flag_if_raised:c}
292 % Test if the flag has a non-zero height, by checking the |0| control sequence.
293 % \begin{macrocode}
294 \prg_new_conditional:Npnn \flag_if_raised:N #1 { p , T , F , TF }
295 {
296 \if_cs_exist:w #1 0 \cs_end:
297 \prg_return_true:
298 \else:
299 \prg_return_false:
300 \fi:
301 }
302 \prg_generate_conditional_variant:Nnn \flag_if_raised:N
303 { c } { p , T , F , TF }
304 % \end{macrocode}
305 % \end{macro}
306 %
307 % \begin{macro}[EXP]{\flag_height:N, \flag_height:c}
308 % \begin{macro}[EXP]{\@@_height_loop:wN, \@@_height_end:wN}
309 % Extract the value of the flag by going through all of the
310 % control sequences starting from |0|.
311 % \begin{macrocode}
312 \cs_new:Npn \flag_height:N #1 { \@@_height_loop:wN 0 \@@_sep: #1 }
313 \cs_new:Npn \@@_height_loop:wN #1 \@@_sep: #2
314 {
315 \if_cs_exist:w #2 #1 \cs_end: \else:
316 \exp_after:wN \@@_height_end:wN
317 \fi:
318 \exp_after:wN \@@_height_loop:wN
319 \int_value:w \int_eval:w \c_one_int + #1 \@@_sep: #2
320 }
321 \cs_new:Npn \@@_height_end:wN #1 + #2 \@@_sep: #3 {#2}
322 \cs_generate_variant:Nn \flag_height:N { c }
323 % \end{macrocode}
324 % \end{macro}
325 % \end{macro}
326 %
327 % \begin{macro}[EXP]{\flag_raise:N, \flag_raise:c}
328 % Change the appropriate control sequence to \tn{relax} by expanding a
329 % \cs{cs:w} \ldots{} \cs{cs_end:} construction, then pass it to
330 % \cs{use_none:n} to avoid leaving anything in the input stream.
331 % \begin{macrocode}
332 \cs_new:Npn \flag_raise:N #1
333 { \exp_after:wN \use_none:n \cs:w #1 \flag_height:N #1 \cs_end: }
334 \cs_generate_variant:Nn \flag_raise:N { c }
335 % \end{macrocode}
336 % \end{macro}
337 %
338 % \begin{macro}[EXP]{\flag_ensure_raised:N, \flag_ensure_raised:c}
339 % Pass the control sequence with name \meta{flag name}\texttt{0} to
340 % \cs{use_none:n}. Constructing the control sequence ensures that it
341 % changes from being undefined (if it was so) to being \tn{relax}.
342 % \begin{macrocode}
343 \cs_new:Npn \flag_ensure_raised:N #1
344 { \exp_after:wN \use_none:n \cs:w #1 0 \cs_end: }
345 \cs_generate_variant:Nn \flag_ensure_raised:N { c }
346 % \end{macrocode}
347 % \end{macro}
348 %
349 % \subsection{Old \texttt{n}-type flag commands}
350 %
351 % Here we keep the old flag commands since our policy is to no longer
352 % delete deprecated functions. The idea is to simply map \meta{flag
353 % name} to \cs[no-index]{l_\meta{flag name}_flag}. When the debugging
354 % code is activated, it checks existence of the \texttt{N}-type flag
355 % variables that result.
356 %
357 % \begin{macro}[no-user-doc]{\flag_new:n, \flag_clear:n, \flag_clear_new:n}
358 % \begin{macro}[EXP, pTF, no-user-doc]{\flag_if_exist:n, \flag_if_raised:n}
359 % \begin{macro}[EXP, no-user-doc]{\flag_height:n, \flag_raise:n, \flag_ensure_raised:n}
360 % \begin{macrocode}
361 \cs_new_protected:Npn \flag_new:n #1 { \flag_new:c { l_#1_flag } }
362 \cs_new_protected:Npn \flag_clear:n #1 { \flag_clear:c { l_#1_flag } }
363 \cs_new_protected:Npn \flag_clear_new:n #1 { \flag_clear_new:c { l_#1_flag } }
364 \cs_new:Npn \flag_if_exist_p:n #1 { \flag_if_exist_p:c { l_#1_flag } }
365 \cs_new:Npn \flag_if_exist:nT #1 { \flag_if_exist:cT { l_#1_flag } }
366 \cs_new:Npn \flag_if_exist:nF #1 { \flag_if_exist:cF { l_#1_flag } }
367 \cs_new:Npn \flag_if_exist:nTF #1 { \flag_if_exist:cTF { l_#1_flag } }
368 \cs_new:Npn \flag_if_raised_p:n #1 { \flag_if_raised_p:c { l_#1_flag } }
369 \cs_new:Npn \flag_if_raised:nT #1 { \flag_if_raised:cT { l_#1_flag } }
370 \cs_new:Npn \flag_if_raised:nF #1 { \flag_if_raised:cF { l_#1_flag } }
371 \cs_new:Npn \flag_if_raised:nTF #1 { \flag_if_raised:cTF { l_#1_flag } }
372 \cs_new:Npn \flag_height:n #1 { \flag_height:c { l_#1_flag } }
373 \cs_new:Npn \flag_raise:n #1 { \flag_raise:c { l_#1_flag } }
374 \cs_new:Npn \flag_ensure_raised:n #1 { \flag_ensure_raised:c { l_#1_flag } }
375 % \end{macrocode}
376 % \end{macro}
377 % \end{macro}
378 % \end{macro}
379 %
380 % \begin{macro}[no-user-doc]{\flag_show:n, \flag_log:n, \@@_show:Nn}
381 % To avoid changing the output here we mostly keep the old code.
382 % \begin{macrocode}
383 \cs_new_protected:Npn \flag_show:n { \@@_show:Nn \tl_show:n }
384 \cs_new_protected:Npn \flag_log:n { \@@_show:Nn \tl_log:n }
385 \cs_new_protected:Npn \@@_show:Nn #1#2
386 {
387 \exp_args:Nc \__kernel_chk_defined:NT { l_#2_flag }
388 {
389 \exp_args:Ne #1
390 { \tl_to_str:n { flag~#2~height } = \flag_height:n {#2} }
391 }
392 }
393 % \end{macrocode}
394 % \end{macro}
395 %
396 % \begin{macrocode}
397 %</code>
398 % \end{macrocode}
399 %
400 % \end{implementation}
401 %
402 % \PrintIndex