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