| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3prg.dtx |
| 4 |
% |
| 5 |
% Copyright (C) 2005-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{l3prg} module\\ Control structures^^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 |
% Conditional processing in \LaTeX3 is defined as something that |
| 53 |
% performs a series of tests, possibly involving assignments and |
| 54 |
% calling other functions that do not read further ahead in the input |
| 55 |
% stream. After processing the input, a \emph{state} is returned. The |
| 56 |
% states returned are \meta{true} and \meta{false}. |
| 57 |
% |
| 58 |
% \LaTeX3 has two forms of conditional flow processing based |
| 59 |
% on these states. The first form is predicate functions that turn the |
| 60 |
% returned state into a boolean \meta{true} or \meta{false}. For |
| 61 |
% example, the function \cs{cs_if_free_p:N} checks whether the control |
| 62 |
% sequence given as its argument is free and then returns the boolean |
| 63 |
% \meta{true} or \meta{false} values to be used in testing with |
| 64 |
% \cs{if_predicate:w} or in functions to be described below. The second form |
| 65 |
% is the kind of functions choosing a particular argument from the |
| 66 |
% input stream based on the result of the testing as in |
| 67 |
% \cs{cs_if_free:NTF} which also takes one argument (the |N|) and then |
| 68 |
% executes either \texttt{true} or \texttt{false} depending on the |
| 69 |
% result. |
| 70 |
% |
| 71 |
% \begin{texnote} |
| 72 |
% The arguments are executed after exiting the underlying |
| 73 |
% |\if...\fi:| structure. |
| 74 |
% \end{texnote} |
| 75 |
% |
| 76 |
% \section{Defining a set of conditional functions} |
| 77 |
% \label{sec:l3prg:new-conditional-functions} |
| 78 |
% |
| 79 |
% \begin{function}[updated = 2022-11-01] |
| 80 |
% { |
| 81 |
% \prg_new_conditional:Npnn, \prg_set_conditional:Npnn, \prg_gset_conditional:Npnn, |
| 82 |
% \prg_new_protected_conditional:Npnn, \prg_set_protected_conditional:Npnn, \prg_gset_protected_conditional:Npnn, |
| 83 |
% \prg_new_conditional:Nnn, \prg_set_conditional:Nnn, \prg_gset_conditional:Nnn, |
| 84 |
% \prg_new_protected_conditional:Nnn, \prg_set_protected_conditional:Nnn, \prg_gset_protected_conditional:Nnn |
| 85 |
% } |
| 86 |
% \begin{syntax} |
| 87 |
% \cs{prg_new_conditional:Npnn} \cs[no-index]{\meta{name}:\meta{arg spec}} \meta{parameters} \Arg{conditions} \Arg{code} \\ |
| 88 |
% \cs{prg_new_conditional:Nnn} \cs[no-index]{\meta{name}:\meta{arg spec}} \Arg{conditions} \Arg{code} |
| 89 |
% \end{syntax} |
| 90 |
% These functions create a family of conditionals using the same |
| 91 |
% \meta{code} to perform the test created. Those non-protected |
| 92 |
% conditionals are expandable if \meta{code} is. |
| 93 |
% The \texttt{new} versions check |
| 94 |
% for existing definitions and perform assignments globally |
| 95 |
% (\emph{cf.}~\cs{cs_new:Npn}) whereas the \texttt{set} versions do no |
| 96 |
% check and perform assignments locally (\emph{cf.}~\cs{cs_set:Npn}). |
| 97 |
% The conditionals created are dependent on the comma-separated list |
| 98 |
% of \meta{conditions}, which should be one or more of |
| 99 |
% \texttt{T}, \texttt{F} and \texttt{TF}, and for non-protected conditionals |
| 100 |
% \texttt{p}. |
| 101 |
% For public conditionals, a full set of forms should be provided: |
| 102 |
% this contrasts with strictly internal conditionals, where only the |
| 103 |
% required subset need be defined. |
| 104 |
% \end{function} |
| 105 |
% |
| 106 |
% The conditionals are defined by \cs{prg_new_conditional:Npnn} and |
| 107 |
% friends as: |
| 108 |
% \begin{itemize} |
| 109 |
% \item \cs[no-index]{\meta{name}_p:\meta{arg spec}} --- a predicate function |
| 110 |
% which will supply either a logical \texttt{true} or logical |
| 111 |
% \texttt{false}. This function is intended for use in cases where |
| 112 |
% one or more logical tests are combined to lead to a final outcome. |
| 113 |
% This function cannot be defined for \texttt{protected} |
| 114 |
% conditionals. |
| 115 |
% \item \cs[no-index]{\meta{name}:\meta{arg spec}T} --- a function with one more |
| 116 |
% argument than the original \meta{arg spec} demands. The \meta{true |
| 117 |
% branch} code in this additional argument will be left on the |
| 118 |
% input stream only if the test is \texttt{true}. |
| 119 |
% \item \cs[no-index]{\meta{name}:\meta{arg spec}F} --- a function with one more |
| 120 |
% argument than the original \meta{arg spec} demands. The |
| 121 |
% \meta{false branch} code in this additional argument will be left |
| 122 |
% on the input stream only if the test is \texttt{false}. |
| 123 |
% \item \cs[no-index]{\meta{name}:\meta{arg spec}TF} --- a function with two |
| 124 |
% more argument than the original \meta{arg spec} demands. The |
| 125 |
% \meta{true branch} code in the first additional argument will be |
| 126 |
% left on the input stream if the test is \texttt{true}, while the |
| 127 |
% \meta{false branch} code in the second argument will be left on |
| 128 |
% the input stream if the test is \texttt{false}. |
| 129 |
% \end{itemize} |
| 130 |
% The \meta{code} of the test may use \meta{parameters} as specified by |
| 131 |
% the second argument to \cs{prg_set_conditional:Npnn}: this should |
| 132 |
% match the \meta{argument specification} but this is not enforced. The |
| 133 |
% |Nnn| versions infer the number of arguments from the argument |
| 134 |
% specification given (\emph{cf.}~\cs{cs_new:Nn}, etc.). Within |
| 135 |
% the \meta{code}, the functions \cs{prg_return_true:} and |
| 136 |
% \cs{prg_return_false:} are used to indicate the logical outcomes of |
| 137 |
% the test. |
| 138 |
% |
| 139 |
% An example can easily clarify matters here: |
| 140 |
% \begin{verbatim} |
| 141 |
% \prg_set_conditional:Npnn \foo_if_bar:NN #1#2 { p , T , TF } |
| 142 |
% { |
| 143 |
% \if_meaning:w \l_tmpa_tl #1 |
| 144 |
% \prg_return_true: |
| 145 |
% \else: |
| 146 |
% \if_meaning:w \l_tmpa_tl #2 |
| 147 |
% \prg_return_true: |
| 148 |
% \else: |
| 149 |
% \prg_return_false: |
| 150 |
% \fi: |
| 151 |
% \fi: |
| 152 |
% } |
| 153 |
% \end{verbatim} |
| 154 |
% This defines the function |\foo_if_bar_p:NN|, |\foo_if_bar:NNTF| and |
| 155 |
% |\foo_if_bar:NNT| but not |\foo_if_bar:NNF| (because |F| is missing |
| 156 |
% from the \meta{conditions} list). The return statements take care of |
| 157 |
% resolving the remaining \cs{else:} and \cs{fi:} before returning the |
| 158 |
% state. There must be a return statement for each branch; failing to do |
| 159 |
% so will result in erroneous output if that branch is executed. |
| 160 |
% |
| 161 |
% The special case where the code of a conditional ends with |
| 162 |
% \cs{prg_return_true:} \cs{else:} \cs{prg_return_false:} \cs{fi:} is |
| 163 |
% optimized. |
| 164 |
% |
| 165 |
% \begin{function}[updated = 2023-05-26] |
| 166 |
% { |
| 167 |
% \prg_new_eq_conditional:NNn, |
| 168 |
% \prg_set_eq_conditional:NNn, |
| 169 |
% \prg_gset_eq_conditional:NNn |
| 170 |
% } |
| 171 |
% \begin{syntax} |
| 172 |
% \cs{prg_new_eq_conditional:NNn} \cs[no-index]{\meta{name_1}:\meta{arg spec}} \cs[no-index]{\meta{name_2}:\meta{arg spec}} \Arg{conditions} |
| 173 |
% \end{syntax} |
| 174 |
% These functions copy a family of conditionals. The \texttt{new} version |
| 175 |
% checks for existing definitions (\emph{cf.}~\cs{cs_new_eq:NN}) whereas |
| 176 |
% the \texttt{set} version does not (\emph{cf.}~\cs{cs_set_eq:NN}). The |
| 177 |
% conditionals copied are depended on the comma-separated list of |
| 178 |
% \meta{conditions}, which should be one or more of \texttt{p}, \texttt{T}, |
| 179 |
% \texttt{F} and \texttt{TF}. |
| 180 |
% \end{function} |
| 181 |
% |
| 182 |
% \begin{function}[EXP]{\prg_return_true:, \prg_return_false:} |
| 183 |
% \begin{syntax} |
| 184 |
% \cs{prg_return_true:} |
| 185 |
% \cs{prg_return_false:} |
| 186 |
% \end{syntax} |
| 187 |
% These \enquote{return} functions define the logical state of a conditional statement. |
| 188 |
% They appear within the code for a conditional |
| 189 |
% function generated by \cs{prg_set_conditional:Npnn}, \emph{etc}, to indicate |
| 190 |
% when a true or false branch should be taken. |
| 191 |
% While they may appear multiple times each within the code of such conditionals, |
| 192 |
% the execution of the conditional must result in the expansion of one of these |
| 193 |
% two functions \emph{exactly once}. |
| 194 |
% |
| 195 |
% The return functions trigger what is internally an \texttt{f}-expansion process to complete |
| 196 |
% the evaluation of the conditional. Therefore, after \cs{prg_return_true:} or \cs{prg_return_false:} |
| 197 |
% there must be no non-expandable material in the input stream for the remainder of |
| 198 |
% the expansion of the conditional code. This includes other instances of either of these functions. |
| 199 |
% \end{function} |
| 200 |
% |
| 201 |
% \begin{function}[added = 2017-12-12]{\prg_generate_conditional_variant:Nnn} |
| 202 |
% \begin{syntax} |
| 203 |
% \cs{prg_generate_conditional_variant:Nnn} \cs[no-index]{\meta{name}:\meta{arg spec}} \Arg{variant argument specifiers} \Arg{condition specifiers} |
| 204 |
% \end{syntax} |
| 205 |
% Defines argument-specifier variants of conditionals. This is |
| 206 |
% equivalent to running \cs{cs_generate_variant:Nn} \meta{conditional} |
| 207 |
% \Arg{variant argument specifiers} on each \meta{conditional} |
| 208 |
% described by the \meta{condition specifiers}. These base-form |
| 209 |
% \meta{conditionals} are obtained from the \meta{name} and \meta{arg |
| 210 |
% spec} as described for \cs{prg_new_conditional:Npnn}, and they |
| 211 |
% should be defined. |
| 212 |
% \end{function} |
| 213 |
% |
| 214 |
% \section{The boolean data type} |
| 215 |
% |
| 216 |
% This section describes a boolean data type which is closely |
| 217 |
% connected to conditional processing as sometimes you want to |
| 218 |
% execute some code depending on the value of a switch |
| 219 |
% (\emph{e.g.},~draft/final) and other times you perhaps want to use it as a |
| 220 |
% predicate function in an \cs{if_predicate:w} test. The problem of the |
| 221 |
% primitive \cs{if_false:} and \cs{if_true:} tokens is that it is not |
| 222 |
% always safe to pass them around as they may interfere with scanning |
| 223 |
% for termination of primitive conditional processing. Therefore, we |
| 224 |
% employ two canonical booleans: \cs{c_true_bool} or |
| 225 |
% \cs{c_false_bool}. Besides preventing problems as described above, it |
| 226 |
% also allows us to implement a simple boolean parser supporting the |
| 227 |
% logical operations And, Or, Not, etc.\ which can then be used on |
| 228 |
% both the boolean type and predicate functions. |
| 229 |
% |
| 230 |
% All conditional |\bool_| functions except assignments are expandable |
| 231 |
% and expect the input to also be fully expandable (which generally |
| 232 |
% means being constructed from predicate functions and booleans, possibly nested). |
| 233 |
% |
| 234 |
% \begin{texnote} |
| 235 |
% The \texttt{bool} data type is not implemented using the |
| 236 |
% \tn{iffalse}/\tn{iftrue} primitives, in contrast to \tn{newif}, |
| 237 |
% etc., in plain \TeX{}, \LaTeXe{} and so on. Programmers should |
| 238 |
% not base use of \texttt{bool} switches on any particular expectation |
| 239 |
% of the implementation. |
| 240 |
% \end{texnote} |
| 241 |
% |
| 242 |
% \begin{function}{\bool_new:N, \bool_new:c} |
| 243 |
% \begin{syntax} |
| 244 |
% \cs{bool_new:N} \meta{boolean} |
| 245 |
% \end{syntax} |
| 246 |
% Creates a new \meta{boolean} or raises an error if the |
| 247 |
% name is already taken. The declaration is global. The |
| 248 |
% \meta{boolean} is initially \texttt{false}. |
| 249 |
% \end{function} |
| 250 |
% |
| 251 |
% \begin{function}[added = 2017-11-28]{\bool_const:Nn, \bool_const:cn} |
| 252 |
% \begin{syntax} |
| 253 |
% \cs{bool_const:Nn} \meta{boolean} \Arg{boolexpr} |
| 254 |
% \end{syntax} |
| 255 |
% Creates a new constant \meta{boolean} or raises an error if the name |
| 256 |
% is already taken. The value of the \meta{boolean} is set globally to |
| 257 |
% the result of evaluating the \meta{boolexpr}. |
| 258 |
% \end{function} |
| 259 |
% |
| 260 |
% \begin{function} |
| 261 |
% { |
| 262 |
% \bool_set_false:N , \bool_set_false:c , |
| 263 |
% \bool_gset_false:N, \bool_gset_false:c |
| 264 |
% } |
| 265 |
% \begin{syntax} |
| 266 |
% \cs{bool_set_false:N} \meta{boolean} |
| 267 |
% \end{syntax} |
| 268 |
% Sets \meta{boolean} logically \texttt{false}. |
| 269 |
% \end{function} |
| 270 |
% |
| 271 |
% \begin{function} |
| 272 |
% { |
| 273 |
% \bool_set_true:N , \bool_set_true:c , |
| 274 |
% \bool_gset_true:N , \bool_gset_true:c |
| 275 |
% } |
| 276 |
% \begin{syntax} |
| 277 |
% \cs{bool_set_true:N} \meta{boolean} |
| 278 |
% \end{syntax} |
| 279 |
% Sets \meta{boolean} logically \texttt{true}. |
| 280 |
% \end{function} |
| 281 |
% |
| 282 |
% \begin{function} |
| 283 |
% { |
| 284 |
% \bool_set_eq:NN , \bool_set_eq:cN , \bool_set_eq:Nc , \bool_set_eq:cc , |
| 285 |
% \bool_gset_eq:NN, \bool_gset_eq:cN, \bool_gset_eq:Nc, \bool_gset_eq:cc |
| 286 |
% } |
| 287 |
% \begin{syntax} |
| 288 |
% \cs{bool_set_eq:NN} \meta{boolean_1} \meta{boolean_2} |
| 289 |
% \end{syntax} |
| 290 |
% Sets \meta{boolean_1} to the current value of \meta{boolean_2}. |
| 291 |
% \end{function} |
| 292 |
% |
| 293 |
% \begin{function}[updated = 2017-07-15] |
| 294 |
% {\bool_set:Nn, \bool_set:cn, \bool_gset:Nn, \bool_gset:cn} |
| 295 |
% \begin{syntax} |
| 296 |
% \cs{bool_set:Nn} \meta{boolean} \Arg{boolexpr} |
| 297 |
% \end{syntax} |
| 298 |
% Evaluates the \meta{boolean expression} as described for |
| 299 |
% \cs{bool_if:nTF}, and sets the \meta{boolean} variable to |
| 300 |
% the logical truth of this evaluation. |
| 301 |
% \end{function} |
| 302 |
% |
| 303 |
% \begin{function}[added = 2018-05-10] |
| 304 |
% { |
| 305 |
% \bool_set_inverse:N , \bool_set_inverse:c , |
| 306 |
% \bool_gset_inverse:N, \bool_gset_inverse:c |
| 307 |
% } |
| 308 |
% \begin{syntax} |
| 309 |
% \cs{bool_set_inverse:N} \meta{boolean} |
| 310 |
% \end{syntax} |
| 311 |
% Toggles the \meta{boolean} from \texttt{true} to \texttt{false} and |
| 312 |
% conversely: sets it to the inverse of its current value. |
| 313 |
% \end{function} |
| 314 |
% |
| 315 |
% \begin{function}[EXP,pTF, updated = 2017-07-15]{\bool_if:N, \bool_if:c} |
| 316 |
% \begin{syntax} |
| 317 |
% \cs{bool_if_p:N} \meta{boolean} |
| 318 |
% \cs{bool_if:NTF} \meta{boolean} \Arg{true code} \Arg{false code} |
| 319 |
% \end{syntax} |
| 320 |
% Tests the current truth of \meta{boolean}, and continues expansion |
| 321 |
% based on this result. |
| 322 |
% \end{function} |
| 323 |
% |
| 324 |
% \begin{function}[EXP, added = 2021-11-01, updated = 2023-11-14] |
| 325 |
% {\bool_to_str:N, \bool_to_str:c, \bool_to_str:n} |
| 326 |
% \begin{syntax} |
| 327 |
% \cs{bool_to_str:N} \meta{boolean} |
| 328 |
% \cs{bool_to_str:n} \Arg{boolean expression} |
| 329 |
% \end{syntax} |
| 330 |
% Expands to the string \texttt{true} or \texttt{false} depending on |
| 331 |
% the logical truth of the \meta{boolean} or \meta{boolean |
| 332 |
% expression}. |
| 333 |
% \end{function} |
| 334 |
% |
| 335 |
% \begin{function}[added = 2012-02-09, updated = 2021-04-29]{\bool_show:N, \bool_show:c} |
| 336 |
% \begin{syntax} |
| 337 |
% \cs{bool_show:N} \meta{boolean} |
| 338 |
% \end{syntax} |
| 339 |
% Displays the logical truth of the \meta{boolean} on the terminal. |
| 340 |
% \end{function} |
| 341 |
% |
| 342 |
% \begin{function}[added = 2012-02-09, updated = 2017-07-15]{\bool_show:n} |
| 343 |
% \begin{syntax} |
| 344 |
% \cs{bool_show:n} \Arg{boolean expression} |
| 345 |
% \end{syntax} |
| 346 |
% Displays the logical truth of the \meta{boolean expression} on the |
| 347 |
% terminal. |
| 348 |
% \end{function} |
| 349 |
% |
| 350 |
% \begin{function}[added = 2014-08-22, updated = 2021-04-29]{\bool_log:N, \bool_log:c} |
| 351 |
% \begin{syntax} |
| 352 |
% \cs{bool_log:N} \meta{boolean} |
| 353 |
% \end{syntax} |
| 354 |
% Writes the logical truth of the \meta{boolean} in the log file. |
| 355 |
% \end{function} |
| 356 |
% |
| 357 |
% \begin{function}[added = 2014-08-22, updated = 2017-07-15]{\bool_log:n} |
| 358 |
% \begin{syntax} |
| 359 |
% \cs{bool_log:n} \Arg{boolean expression} |
| 360 |
% \end{syntax} |
| 361 |
% Writes the logical truth of the \meta{boolean expression} in the log |
| 362 |
% file. |
| 363 |
% \end{function} |
| 364 |
% |
| 365 |
% \begin{function}[EXP, pTF, added=2012-03-03] |
| 366 |
% {\bool_if_exist:N, \bool_if_exist:c} |
| 367 |
% \begin{syntax} |
| 368 |
% \cs{bool_if_exist_p:N} \meta{boolean} |
| 369 |
% \cs{bool_if_exist:NTF} \meta{boolean} \Arg{true code} \Arg{false code} |
| 370 |
% \end{syntax} |
| 371 |
% Tests whether the \meta{boolean} is currently defined. This does not |
| 372 |
% check that the \meta{boolean} really is a boolean variable. |
| 373 |
% \end{function} |
| 374 |
% |
| 375 |
% \subsection{Constant and scratch booleans} |
| 376 |
% |
| 377 |
% \begin{variable}{\c_true_bool, \c_false_bool} |
| 378 |
% Constants that represent |true| and |false|, respectively. Used to |
| 379 |
% implement predicates. |
| 380 |
% \end{variable} |
| 381 |
% |
| 382 |
% \begin{variable}{\l_tmpa_bool, \l_tmpb_bool} |
| 383 |
% A scratch boolean for local assignment. It is never used by |
| 384 |
% the kernel code, and so is safe for use with any \LaTeX3-defined |
| 385 |
% function. However, it may be overwritten by other non-kernel |
| 386 |
% code and so should only be used for short-term storage. |
| 387 |
% \end{variable} |
| 388 |
% |
| 389 |
% \begin{variable}{\g_tmpa_bool, \g_tmpb_bool} |
| 390 |
% A scratch boolean for global assignment. It is never used by |
| 391 |
% the kernel code, and so is safe for use with any \LaTeX3-defined |
| 392 |
% function. However, it may be overwritten by other non-kernel |
| 393 |
% code and so should only be used for short-term storage. |
| 394 |
% \end{variable} |
| 395 |
% |
| 396 |
% \section{Boolean expressions} |
| 397 |
% |
| 398 |
% As we have a boolean datatype and predicate functions returning |
| 399 |
% boolean \meta{true} or \meta{false} values, it seems only fitting |
| 400 |
% that we also provide a parser for \meta{boolean expressions}. |
| 401 |
% |
| 402 |
% A boolean expression is an expression which given input in the form |
| 403 |
% of predicate functions and boolean variables, return boolean |
| 404 |
% \meta{true} or \meta{false}. It supports the logical operations And, |
| 405 |
% Or and Not as the well-known infix operators |&&| and \verb"||" and prefix~|!| |
| 406 |
% with their usual precedences (namely, |&&| binds more tightly than |
| 407 |
% \verb"||"). In addition to this, parentheses can be used to isolate |
| 408 |
% sub-expressions. For example, |
| 409 |
% \begin{verbatim} |
| 410 |
% \int_compare_p:n { 1 = 1 } && |
| 411 |
% ( |
| 412 |
% \int_compare_p:n { 2 = 3 } || |
| 413 |
% \int_compare_p:n { 4 <= 4 } || |
| 414 |
% \str_if_eq_p:nn { abc } { def } |
| 415 |
% ) && |
| 416 |
% ! \int_compare_p:n { 2 = 4 } |
| 417 |
% \end{verbatim} |
| 418 |
% is a valid boolean expression. |
| 419 |
% |
| 420 |
% Contrarily to some other programming languages, the operators |&&| and |
| 421 |
% \verb"||" evaluate both operands in all cases, even when the first |
| 422 |
% operand is enough to determine the result. This \enquote{eager} |
| 423 |
% evaluation should be contrasted with the \enquote{lazy} evaluation of |
| 424 |
% \cs[no-index]{bool_lazy_\ldots{}} functions. |
| 425 |
% |
| 426 |
% \begin{texnote} |
| 427 |
% The eager evaluation of boolean expressions is unfortunately |
| 428 |
% necessary in \TeX{}. Indeed, a lazy parser can get confused if |&&| |
| 429 |
% or \verb"||" or parentheses appear as (unbraced) arguments of some |
| 430 |
% predicates. For instance, the innocuous-looking expression below |
| 431 |
% would break (in a lazy parser) if |#1| were a closing parenthesis |
| 432 |
% and \cs[no-index]{l_tmpa_bool} were \texttt{true}. |
| 433 |
% \begin{verbatim} |
| 434 |
% ( \l_tmpa_bool || \token_if_eq_meaning_p:NN X #1 ) |
| 435 |
% \end{verbatim} |
| 436 |
% \end{texnote} |
| 437 |
% |
| 438 |
% Minimal (lazy) evaluation can be obtained using the conditionals |
| 439 |
% \cs{bool_lazy_all:nTF}, \cs{bool_lazy_and:nnTF}, \cs{bool_lazy_any:nTF}, or |
| 440 |
% \cs{bool_lazy_or:nnTF}, which only evaluate their boolean expression |
| 441 |
% arguments when they are needed to determine the resulting truth |
| 442 |
% value. For example, when evaluating the boolean expression |
| 443 |
% \begin{verbatim} |
| 444 |
% \bool_lazy_and_p:nn |
| 445 |
% { |
| 446 |
% \bool_lazy_any_p:n |
| 447 |
% { |
| 448 |
% { \int_compare_p:n { 2 = 3 } } |
| 449 |
% { \int_compare_p:n { 4 <= 4 } } |
| 450 |
% { \int_compare_p:n { 1 = \error } } % skipped |
| 451 |
% } |
| 452 |
% } |
| 453 |
% { ! \int_compare_p:n { 2 = 4 } } |
| 454 |
% \end{verbatim} |
| 455 |
% the line marked with |skipped| is not expanded because the result |
| 456 |
% of \cs{bool_lazy_any_p:n} is known once the second boolean expression is |
| 457 |
% found to be logically \texttt{true}. On the other hand, the last |
| 458 |
% line is expanded because its logical value is needed to determine the |
| 459 |
% result of \cs{bool_lazy_and_p:nn}. |
| 460 |
% |
| 461 |
% \begin{function}[EXP, pTF, updated = 2017-07-15]{\bool_if:n} |
| 462 |
% \begin{syntax} |
| 463 |
% \cs{bool_if_p:n} \Arg{boolean expression} |
| 464 |
% \cs{bool_if:nTF} \Arg{boolean expression} \Arg{true code} \Arg{false code} |
| 465 |
% \end{syntax} |
| 466 |
% Tests the current truth of \meta{boolean expression}, and |
| 467 |
% continues expansion based on this result. The |
| 468 |
% \meta{boolean expression} should consist of a series of predicates |
| 469 |
% or boolean variables with the logical relationship between these |
| 470 |
% defined using |&&| (\enquote{And}), \verb"||" (\enquote{Or}), |
| 471 |
% |!| (\enquote{Not}) and parentheses. The logical Not applies to |
| 472 |
% the next predicate or group. |
| 473 |
% \end{function} |
| 474 |
% |
| 475 |
% \begin{function}[EXP, pTF, added = 2015-11-15, updated = 2017-07-15]{\bool_lazy_all:n} |
| 476 |
% \begin{syntax} |
| 477 |
% \cs{bool_lazy_all_p:n} \{ \Arg{boolexpr_1} \Arg{boolexpr_2} $\cdots$ \Arg{boolexpr_N} \} |
| 478 |
% \cs{bool_lazy_all:nTF} \{ \Arg{boolexpr_1} \Arg{boolexpr_2} $\cdots$ \Arg{boolexpr_N} \} \Arg{true code} \Arg{false code} |
| 479 |
% \end{syntax} |
| 480 |
% Implements the \enquote{And} operation on the \meta{boolean |
| 481 |
% expressions}, hence is \texttt{true} if all of them are |
| 482 |
% \texttt{true} and \texttt{false} if any of them is \texttt{false}. |
| 483 |
% Contrarily to the infix operator |&&|, only the \meta{boolean |
| 484 |
% expressions} which are needed to determine the result of |
| 485 |
% \cs{bool_lazy_all:nTF} are evaluated. See also \cs{bool_lazy_and:nnTF} |
| 486 |
% when there are only two \meta{boolean expressions}. |
| 487 |
% \end{function} |
| 488 |
% |
| 489 |
% \begin{function}[EXP, pTF, added = 2015-11-15, updated = 2017-07-15]{\bool_lazy_and:nn} |
| 490 |
% \begin{syntax} |
| 491 |
% \cs{bool_lazy_and_p:nn} \Arg{boolexpr_1} \Arg{boolexpr_2} |
| 492 |
% \cs{bool_lazy_and:nnTF} \Arg{boolexpr_1} \Arg{boolexpr_2} \Arg{true code} \Arg{false code} |
| 493 |
% \end{syntax} |
| 494 |
% Implements the \enquote{And} operation between two boolean |
| 495 |
% expressions, hence is \texttt{true} if both are \texttt{true}. |
| 496 |
% Contrarily to the infix operator |&&|, the \meta{boolexpr_2} is only |
| 497 |
% evaluated if it is needed to determine the result of |
| 498 |
% \cs{bool_lazy_and:nnTF}. See also \cs{bool_lazy_all:nTF} when there are more |
| 499 |
% than two \meta{boolean expressions}. |
| 500 |
% \end{function} |
| 501 |
% |
| 502 |
% \begin{function}[EXP, pTF, added = 2015-11-15, updated = 2017-07-15]{\bool_lazy_any:n} |
| 503 |
% \begin{syntax} |
| 504 |
% \cs{bool_lazy_any_p:n} \{ \Arg{boolexpr_1} \Arg{boolexpr_2} $\cdots$ \Arg{boolexpr_N} \} |
| 505 |
% \cs{bool_lazy_any:nTF} \{ \Arg{boolexpr_1} \Arg{boolexpr_2} $\cdots$ \Arg{boolexpr_N} \} \Arg{true code} \Arg{false code} |
| 506 |
% \end{syntax} |
| 507 |
% Implements the \enquote{Or} operation on the \meta{boolean |
| 508 |
% expressions}, hence is \texttt{true} if any of them is |
| 509 |
% \texttt{true} and \texttt{false} if all of them are \texttt{false}. |
| 510 |
% Contrarily to the infix operator \verb"||", only the \meta{boolean |
| 511 |
% expressions} which are needed to determine the result of |
| 512 |
% \cs{bool_lazy_any:nTF} are evaluated. See also \cs{bool_lazy_or:nnTF} |
| 513 |
% when there are only two \meta{boolean expressions}. |
| 514 |
% \end{function} |
| 515 |
% |
| 516 |
% \begin{function}[EXP, pTF, added = 2015-11-15, updated = 2017-07-15]{\bool_lazy_or:nn} |
| 517 |
% \begin{syntax} |
| 518 |
% \cs{bool_lazy_or_p:nn} \Arg{boolexpr_1} \Arg{boolexpr_2} |
| 519 |
% \cs{bool_lazy_or:nnTF} \Arg{boolexpr_1} \Arg{boolexpr_2} \Arg{true code} \Arg{false code} |
| 520 |
% \end{syntax} |
| 521 |
% Implements the \enquote{Or} operation between two boolean |
| 522 |
% expressions, hence is \texttt{true} if either one is \texttt{true}. |
| 523 |
% Contrarily to the infix operator \verb"||", the \meta{boolexpr_2} |
| 524 |
% is only evaluated if it is needed to determine the result of |
| 525 |
% \cs{bool_lazy_or:nnTF}. See also \cs{bool_lazy_any:nTF} when there are more |
| 526 |
% than two \meta{boolean expressions}. |
| 527 |
% \end{function} |
| 528 |
% |
| 529 |
% \begin{function}[EXP, updated = 2017-07-15]{\bool_not_p:n} |
| 530 |
% \begin{syntax} |
| 531 |
% \cs{bool_not_p:n} \Arg{boolean expression} |
| 532 |
% \end{syntax} |
| 533 |
% Function version of |!(|\meta{boolean expression}|)| within a boolean |
| 534 |
% expression. |
| 535 |
% \end{function} |
| 536 |
% |
| 537 |
% \begin{function}[EXP, pTF, added = 2018-05-09]{\bool_xor:nn} |
| 538 |
% \begin{syntax} |
| 539 |
% \cs{bool_xor_p:nn} \Arg{boolexpr_1} \Arg{boolexpr_2} |
| 540 |
% \cs{bool_xor:nnTF} \Arg{boolexpr_1} \Arg{boolexpr_2} \Arg{true code} \Arg{false code} |
| 541 |
% \end{syntax} |
| 542 |
% Implements an \enquote{exclusive or} operation between two boolean |
| 543 |
% expressions. There is no infix operation for this logical operation. |
| 544 |
% \end{function} |
| 545 |
% |
| 546 |
% \section{Logical loops} |
| 547 |
% |
| 548 |
% Loops using either boolean expressions or stored boolean values. |
| 549 |
% |
| 550 |
% \begin{function}[rEXP, updated = 2017-07-15]{\bool_do_until:Nn, \bool_do_until:cn} |
| 551 |
% \begin{syntax} |
| 552 |
% \cs{bool_do_until:Nn} \meta{boolean} \Arg{code} |
| 553 |
% \end{syntax} |
| 554 |
% Places the \meta{code} in the input stream for \TeX{} to process, |
| 555 |
% and then checks the logical value of the \meta{boolean}. If it is |
| 556 |
% \texttt{false} then the \meta{code} is inserted into the input |
| 557 |
% stream again and the process loops until the \meta{boolean} is |
| 558 |
% \texttt{true}. |
| 559 |
% \end{function} |
| 560 |
% |
| 561 |
% \begin{function}[rEXP, updated = 2017-07-15]{\bool_do_while:Nn, \bool_do_while:cn} |
| 562 |
% \begin{syntax} |
| 563 |
% \cs{bool_do_while:Nn} \meta{boolean} \Arg{code} |
| 564 |
% \end{syntax} |
| 565 |
% Places the \meta{code} in the input stream for \TeX{} to process, |
| 566 |
% and then checks the logical value of the \meta{boolean}. If it is |
| 567 |
% \texttt{true} then the \meta{code} is inserted into the input |
| 568 |
% stream again and the process loops until the \meta{boolean} is |
| 569 |
% \texttt{false}. |
| 570 |
% \end{function} |
| 571 |
% |
| 572 |
% \begin{function}[rEXP, updated = 2017-07-15]{\bool_until_do:Nn, \bool_until_do:cn} |
| 573 |
% \begin{syntax} |
| 574 |
% \cs{bool_until_do:Nn} \meta{boolean} \Arg{code} |
| 575 |
% \end{syntax} |
| 576 |
% This function first checks the logical value of the \meta{boolean}. |
| 577 |
% If it is \texttt{false} the \meta{code} is placed in the input stream |
| 578 |
% and expanded. After the completion of the \meta{code} the truth |
| 579 |
% of the \meta{boolean} is re-evaluated. The process then loops |
| 580 |
% until the \meta{boolean} is \texttt{true}. |
| 581 |
% \end{function} |
| 582 |
% |
| 583 |
% \begin{function}[rEXP, updated = 2017-07-15]{\bool_while_do:Nn, \bool_while_do:cn} |
| 584 |
% \begin{syntax} |
| 585 |
% \cs{bool_while_do:Nn} \meta{boolean} \Arg{code} |
| 586 |
% \end{syntax} |
| 587 |
% This function first checks the logical value of the \meta{boolean}. |
| 588 |
% If it is \texttt{true} the \meta{code} is placed in the input stream |
| 589 |
% and expanded. After the completion of the \meta{code} the truth |
| 590 |
% of the \meta{boolean} is re-evaluated. The process then loops |
| 591 |
% until the \meta{boolean} is \texttt{false}. |
| 592 |
% \end{function} |
| 593 |
% |
| 594 |
% \begin{function}[rEXP, updated = 2017-07-15]{\bool_do_until:nn} |
| 595 |
% \begin{syntax} |
| 596 |
% \cs{bool_do_until:nn} \Arg{boolean expression} \Arg{code} |
| 597 |
% \end{syntax} |
| 598 |
% Places the \meta{code} in the input stream for \TeX{} to process, |
| 599 |
% and then checks the logical value of the \meta{boolean expression} |
| 600 |
% as described for \cs{bool_if:nTF}. If it is \texttt{false} then the |
| 601 |
% \meta{code} is inserted into the input stream again and the |
| 602 |
% process loops until the \meta{boolean expression} evaluates to |
| 603 |
% \texttt{true}. |
| 604 |
% \end{function} |
| 605 |
% |
| 606 |
% \begin{function}[rEXP, updated = 2017-07-15]{\bool_do_while:nn} |
| 607 |
% \begin{syntax} |
| 608 |
% \cs{bool_do_while:nn} \Arg{boolean expression} \Arg{code} |
| 609 |
% \end{syntax} |
| 610 |
% Places the \meta{code} in the input stream for \TeX{} to process, |
| 611 |
% and then checks the logical value of the \meta{boolean expression} |
| 612 |
% as described for \cs{bool_if:nTF}. If it is \texttt{true} then the |
| 613 |
% \meta{code} is inserted into the input stream again and the |
| 614 |
% process loops until the \meta{boolean expression} evaluates to |
| 615 |
% \texttt{false}. |
| 616 |
% \end{function} |
| 617 |
% |
| 618 |
% \begin{function}[rEXP, updated = 2017-07-15]{\bool_until_do:nn} |
| 619 |
% \begin{syntax} |
| 620 |
% \cs{bool_until_do:nn} \Arg{boolean expression} \Arg{code} |
| 621 |
% \end{syntax} |
| 622 |
% This function first checks the logical value of the |
| 623 |
% \meta{boolean expression} (as described for \cs{bool_if:nTF}). |
| 624 |
% If it is \texttt{false} the \meta{code} is placed in the input stream |
| 625 |
% and expanded. After the completion of the \meta{code} the truth |
| 626 |
% of the \meta{boolean expression} is re-evaluated. The process |
| 627 |
% then loops until the \meta{boolean expression} is \texttt{true}. |
| 628 |
% \end{function} |
| 629 |
% |
| 630 |
% \begin{function}[rEXP, updated = 2017-07-15]{\bool_while_do:nn} |
| 631 |
% \begin{syntax} |
| 632 |
% \cs{bool_while_do:nn} \Arg{boolean expression} \Arg{code} |
| 633 |
% \end{syntax} |
| 634 |
% This function first checks the logical value of the |
| 635 |
% \meta{boolean expression} (as described for \cs{bool_if:nTF}). |
| 636 |
% If it is \texttt{true} the \meta{code} is placed in the input stream |
| 637 |
% and expanded. After the completion of the \meta{code} the truth |
| 638 |
% of the \meta{boolean expression} is re-evaluated. The process |
| 639 |
% then loops until the \meta{boolean expression} is \texttt{false}. |
| 640 |
% \end{function} |
| 641 |
% |
| 642 |
% \begin{function}[added = 2023-05-03, EXP, noTF]{\bool_case:n} |
| 643 |
% \begin{syntax} |
| 644 |
% \cs{bool_case:nTF} \\ |
| 645 |
% ~~|{| \\ |
| 646 |
% ~~~~\Arg{boolexpr case_1} \Arg{code case_1} \\ |
| 647 |
% ~~~~\Arg{boolexpr case_2} \Arg{code case_2} \\ |
| 648 |
% ~~~~\ldots \\ |
| 649 |
% ~~~~\Arg{boolexpr case_n} \Arg{code case_n} \\ |
| 650 |
% ~~|}| \\ |
| 651 |
% ~~\Arg{true code} |
| 652 |
% ~~\Arg{false code} |
| 653 |
% \end{syntax} |
| 654 |
% Evaluates in turn each of the \meta{boolean expression case}s until |
| 655 |
% the first one that evaluates to \texttt{true}. |
| 656 |
% The \meta{code} associated to this first case is left in the input |
| 657 |
% stream, followed by the \meta{true code}, and other cases are |
| 658 |
% discarded. If none of the cases match then only the \meta{false |
| 659 |
% code} is inserted. The function \cs{bool_case:n}, |
| 660 |
% which does nothing if there is no match, is |
| 661 |
% also available. For example |
| 662 |
% \begin{verbatim} |
| 663 |
% \bool_case:nF |
| 664 |
% { |
| 665 |
% { \dim_compare_p:n { \l__mypkg_wd_dim <= 10pt } } |
| 666 |
% { Fits } |
| 667 |
% { \int_compare_p:n { \l__mypkg_total_int >= 10 } } |
| 668 |
% { Many } |
| 669 |
% { \l__mypkg_special_bool } |
| 670 |
% { Special } |
| 671 |
% } |
| 672 |
% { No idea! } |
| 673 |
% \end{verbatim} |
| 674 |
% leaves \enquote{\texttt{Fits}} or \enquote{\texttt{Many}} or |
| 675 |
% \enquote{\texttt{Special}} or \enquote{\texttt{No idea!}} in the |
| 676 |
% input stream, in a way similar to some other language's |
| 677 |
% \enquote{\texttt{if} \ldots\ \texttt{elseif} \ldots\ \texttt{elseif} \ldots\ |
| 678 |
% \texttt{else} \ldots}. |
| 679 |
% \end{function} |
| 680 |
% |
| 681 |
% \section{Producing multiple copies} |
| 682 |
% |
| 683 |
% \begin{function}[updated = 2011-07-04, EXP]{\prg_replicate:nn} |
| 684 |
% \begin{syntax} |
| 685 |
% \cs{prg_replicate:nn} \Arg{integer expression} \Arg{tokens} |
| 686 |
% \end{syntax} |
| 687 |
% Evaluates the \meta{integer expression} (which should be |
| 688 |
% zero or positive) and creates the resulting number of copies |
| 689 |
% of the \meta{tokens}. The function is both expandable and safe for |
| 690 |
% nesting. It yields its result after two expansion steps. |
| 691 |
% \end{function} |
| 692 |
% |
| 693 |
% \section{Detecting \TeX{}'s mode} |
| 694 |
% |
| 695 |
% \begin{function}[EXP,pTF]{\mode_if_horizontal:} |
| 696 |
% \begin{syntax} |
| 697 |
% \cs{mode_if_horizontal_p:} |
| 698 |
% \cs{mode_if_horizontal:TF} \Arg{true code} \Arg{false code} |
| 699 |
% \end{syntax} |
| 700 |
% Detects if \TeX{} is currently in horizontal mode. |
| 701 |
% \end{function} |
| 702 |
% |
| 703 |
% \begin{function}[EXP,pTF]{\mode_if_inner:} |
| 704 |
% \begin{syntax} |
| 705 |
% \cs{mode_if_inner_p:} |
| 706 |
% \cs{mode_if_inner:TF} \Arg{true code} \Arg{false code} |
| 707 |
% \end{syntax} |
| 708 |
% Detects if \TeX{} is currently in inner mode. |
| 709 |
% \end{function} |
| 710 |
% |
| 711 |
% \begin{function}[updated = 2011-09-05, EXP,pTF]{\mode_if_math:} |
| 712 |
% \begin{syntax} |
| 713 |
% \cs{mode_if_math_p:} |
| 714 |
% \cs{mode_if_math:TF} \Arg{true code} \Arg{false code} |
| 715 |
% \end{syntax} |
| 716 |
% Detects if \TeX{} is currently in maths mode. |
| 717 |
% \end{function} |
| 718 |
% |
| 719 |
% \begin{function}[EXP,pTF]{\mode_if_vertical:} |
| 720 |
% \begin{syntax} |
| 721 |
% \cs{mode_if_vertical_p:} |
| 722 |
% \cs{mode_if_vertical:TF} \Arg{true code} \Arg{false code} |
| 723 |
% \end{syntax} |
| 724 |
% Detects if \TeX{} is currently in vertical mode. |
| 725 |
% \end{function} |
| 726 |
% |
| 727 |
% \section{Primitive conditionals} |
| 728 |
% |
| 729 |
% \begin{function}[EXP]{\if_predicate:w} |
| 730 |
% \begin{syntax} |
| 731 |
% \cs{if_predicate:w} \meta{predicate} \meta{true code} \cs{else:} \meta{false code} \cs{fi:} |
| 732 |
% \end{syntax} |
| 733 |
% This function takes a predicate function and |
| 734 |
% branches according to the result. (In practice this function would also |
| 735 |
% accept a single boolean variable in place of the \meta{predicate} but to make the |
| 736 |
% coding clearer this should be done through \cs{if_bool:N}.) |
| 737 |
% \end{function} |
| 738 |
% |
| 739 |
% \begin{function}[EXP]{\if_bool:N} |
| 740 |
% \begin{syntax} |
| 741 |
% \cs{if_bool:N} \meta{boolean} \meta{true code} \cs{else:} \meta{false code} \cs{fi:} |
| 742 |
% \end{syntax} |
| 743 |
% This function takes a boolean variable and |
| 744 |
% branches according to the result. |
| 745 |
% \end{function} |
| 746 |
% |
| 747 |
% \section{Nestable recursions and mappings} |
| 748 |
% |
| 749 |
% There are a number of places where recursion or mapping constructs are used in |
| 750 |
% \pkg{expl3}. At a low-level, these typically require insertion of tokens |
| 751 |
% at the end of the content to allow \enquote{clean up}. To support such |
| 752 |
% mappings in a nestable form, the following functions are provided. |
| 753 |
% |
| 754 |
% \begin{function}[EXP, added = 2018-03-26]{\prg_break_point:Nn} |
| 755 |
% \begin{syntax} |
| 756 |
% \cs{prg_break_point:Nn} \cs[no-index]{\meta{type}_map_break:} \Arg{code} |
| 757 |
% \end{syntax} |
| 758 |
% Used to mark the end of a recursion or mapping: the functions |
| 759 |
% \cs[no-index]{\meta{type}_map_break:} and |
| 760 |
% \cs[no-index]{\meta{type}_map_break:n} use this to break out of the loop |
| 761 |
% (see \cs{prg_map_break:Nn} for how to set these up). |
| 762 |
% After the loop ends, the \meta{code} is inserted into the input stream. This |
| 763 |
% occurs even if the break functions are \emph{not} applied: |
| 764 |
% \cs{prg_break_point:Nn} is functionally-equivalent in these cases |
| 765 |
% to \cs{use_ii:nn}. |
| 766 |
% \end{function} |
| 767 |
% |
| 768 |
% \begin{function}[EXP, added = 2018-03-26]{\prg_map_break:Nn} |
| 769 |
% \begin{syntax} |
| 770 |
% \cs{prg_map_break:Nn} \cs[no-index]{\meta{type}_map_break:} \Arg{user code} |
| 771 |
% \ldots{} |
| 772 |
% \cs{prg_break_point:Nn} \cs[no-index]{\meta{type}_map_break:} \Arg{ending code} |
| 773 |
% \end{syntax} |
| 774 |
% Breaks a recursion in mapping contexts, inserting in the input |
| 775 |
% stream the \meta{user code} after the \meta{ending code} for the |
| 776 |
% loop. The function breaks loops, inserting their \meta{ending |
| 777 |
% code}, until reaching a loop with the same \meta{type} as its |
| 778 |
% first argument. This \cs[no-index]{\meta{type}_map_break:} argument |
| 779 |
% must be defined; it is simply used as a recognizable marker for the \meta{type}. |
| 780 |
% |
| 781 |
% For types with mappings defined in the kernel, |
| 782 |
% \cs[no-index]{\meta{type}_map_break:} and \cs[no-index]{\meta{type}_map_break:n} |
| 783 |
% are defined as \cs{prg_map_break:Nn} \cs[no-index]{\meta{type}_map_break:} |{}| |
| 784 |
% and the same with |{}| omitted. |
| 785 |
% \end{function} |
| 786 |
% |
| 787 |
% \subsection{Simple mappings} |
| 788 |
% |
| 789 |
% In addition to the more complex mappings above, non-nestable mappings are |
| 790 |
% used in a number of locations and support is provided for these. |
| 791 |
% |
| 792 |
% \begin{function}[EXP, added = 2018-03-27]{\prg_break_point:} |
| 793 |
% This copy of \cs{prg_do_nothing:} is used to mark the end of a fast |
| 794 |
% short-term recursion: the function \cs{prg_break:n} uses this to |
| 795 |
% break out of the loop. |
| 796 |
% \end{function} |
| 797 |
% |
| 798 |
% \begin{function}[EXP, added = 2018-03-27]{\prg_break:, \prg_break:n} |
| 799 |
% \begin{syntax} |
| 800 |
% \cs{prg_break:n} \Arg{code} \ldots{} \cs{prg_break_point:} |
| 801 |
% \end{syntax} |
| 802 |
% Breaks a recursion which has no \meta{ending code} and which is not |
| 803 |
% a user-breakable mapping (see for instance |
| 804 |
% implementation of \cs{int_step_function:nnnN}), and |
| 805 |
% inserts the \meta{code} in the input stream. |
| 806 |
% \end{function} |
| 807 |
% |
| 808 |
% \section{Internal programming functions} |
| 809 |
% |
| 810 |
% \begin{function}[updated = 2011-08-11, EXP] |
| 811 |
% {\group_align_safe_begin:, \group_align_safe_end:} |
| 812 |
% \begin{syntax} |
| 813 |
% \cs{group_align_safe_begin:} |
| 814 |
% \ldots |
| 815 |
% \cs{group_align_safe_end:} |
| 816 |
% \end{syntax} |
| 817 |
% These functions are used to enclose material in a \TeX{} alignment |
| 818 |
% environment within a specially-constructed group. This group is |
| 819 |
% designed in such a way that it does not add brace groups to the |
| 820 |
% output but does act as a group for the |&| token inside |
| 821 |
% \tn{halign}. This is necessary to allow grabbing of tokens |
| 822 |
% for testing purposes, as \TeX{} uses group level to determine the |
| 823 |
% effect of alignment tokens. Without the special grouping, the use of |
| 824 |
% a function such as \cs{peek_after:Nw} would result in a forbidden |
| 825 |
% comparison of the internal \tn{endtemplate} token, yielding a |
| 826 |
% fatal error. Each \cs{group_align_safe_begin:} must be matched by a |
| 827 |
% \cs{group_align_safe_end:}, although this does not have to occur |
| 828 |
% within the same function. |
| 829 |
% \end{function} |
| 830 |
% |
| 831 |
% \end{documentation} |
| 832 |
% |
| 833 |
% \begin{implementation} |
| 834 |
% |
| 835 |
% \section{\pkg{l3prg} implementation} |
| 836 |
% |
| 837 |
% \TestFiles{m3prg001.lvt,m3prg002.lvt,m3prg003.lvt} |
| 838 |
% |
| 839 |
% \begin{macrocode} |
| 840 |
%<*code> |
| 841 |
% \end{macrocode} |
| 842 |
% |
| 843 |
% \subsection{Primitive conditionals} |
| 844 |
% |
| 845 |
% \begin{macro}{\if_bool:N} |
| 846 |
% \begin{macro}{\if_predicate:w} |
| 847 |
% Those two primitive \TeX{} conditionals are synonyms. |
| 848 |
% \cs{if_bool:N} is defined in \pkg{l3basics}, as it's needed |
| 849 |
% earlier to define quark test functions. |
| 850 |
% \begin{macrocode} |
| 851 |
\cs_new_eq:NN \if_predicate:w \tex_ifodd:D |
| 852 |
% \end{macrocode} |
| 853 |
% \end{macro} |
| 854 |
% \end{macro} |
| 855 |
% |
| 856 |
% \subsection{Defining a set of conditional functions} |
| 857 |
% |
| 858 |
% \begin{macro} |
| 859 |
% { |
| 860 |
% \prg_set_conditional:Npnn, |
| 861 |
% \prg_new_conditional:Npnn, |
| 862 |
% \prg_set_protected_conditional:Npnn, |
| 863 |
% \prg_new_protected_conditional:Npnn |
| 864 |
% } |
| 865 |
% \begin{macro} |
| 866 |
% { |
| 867 |
% \prg_set_conditional:Nnn, |
| 868 |
% \prg_new_conditional:Nnn, |
| 869 |
% \prg_set_protected_conditional:Nnn, |
| 870 |
% \prg_new_protected_conditional:Nnn |
| 871 |
% } |
| 872 |
% \begin{macro}{\prg_set_eq_conditional:NNn, \prg_new_eq_conditional:NNn} |
| 873 |
% \begin{macro}{\prg_return_true:} |
| 874 |
% \TestMissing |
| 875 |
% {This function is implicitly tested with all other conditionals!} |
| 876 |
% \begin{macro}{\prg_return_false:} |
| 877 |
% \TestMissing |
| 878 |
% {This function is also implicitly tested with all other conditionals!} |
| 879 |
% These are all defined in \pkg{l3basics}, as they are needed |
| 880 |
% \enquote{early}. This is just a reminder! |
| 881 |
% \end{macro} |
| 882 |
% \end{macro} |
| 883 |
% \end{macro} |
| 884 |
% \end{macro} |
| 885 |
% \end{macro} |
| 886 |
% |
| 887 |
% \subsection{The boolean data type} |
| 888 |
% |
| 889 |
% \begin{macrocode} |
| 890 |
%<@@=bool> |
| 891 |
% \end{macrocode} |
| 892 |
% |
| 893 |
% \begin{macro}{\bool_new:N, \bool_new:c} |
| 894 |
% \UnitTested |
| 895 |
% Boolean variables have to be initiated when they are created. Other |
| 896 |
% than that there is not much to say here. |
| 897 |
% \begin{macrocode} |
| 898 |
\cs_new_protected:Npn \bool_new:N #1 { \cs_new_eq:NN #1 \c_false_bool } |
| 899 |
\cs_generate_variant:Nn \bool_new:N { c } |
| 900 |
% \end{macrocode} |
| 901 |
% \end{macro} |
| 902 |
% |
| 903 |
% \begin{macro}[added = 2017-11-28]{\bool_const:Nn, \bool_const:cn} |
| 904 |
% A merger between \cs{tl_const:Nn} and \cs{bool_set:Nn}. |
| 905 |
% \begin{macrocode} |
| 906 |
\cs_new_protected:Npn \bool_const:Nn #1#2 |
| 907 |
{ |
| 908 |
\__kernel_chk_if_free_cs:N #1 |
| 909 |
\tex_global:D \tex_chardef:D #1 = \bool_if_p:n {#2} |
| 910 |
} |
| 911 |
\cs_generate_variant:Nn \bool_const:Nn { c } |
| 912 |
% \end{macrocode} |
| 913 |
% \end{macro} |
| 914 |
% |
| 915 |
% \begin{macro} |
| 916 |
% { |
| 917 |
% \bool_set_true:N, \bool_set_true:c, |
| 918 |
% \bool_gset_true:N, \bool_gset_true:c, |
| 919 |
% \bool_set_false:N, \bool_set_false:c, |
| 920 |
% \bool_gset_false:N, \bool_gset_false:c |
| 921 |
% } |
| 922 |
% \UnitTested |
| 923 |
% Setting is already pretty easy. |
| 924 |
% When \texttt{check-declarations} is active, the definitions are patched to |
| 925 |
% make sure the boolean exists. This is needed because booleans are |
| 926 |
% not based on token lists nor on \TeX{} registers. |
| 927 |
% \begin{macrocode} |
| 928 |
\cs_new_protected:Npn \bool_set_true:N #1 |
| 929 |
{ \cs_set_eq:NN #1 \c_true_bool } |
| 930 |
\cs_new_protected:Npn \bool_set_false:N #1 |
| 931 |
{ \cs_set_eq:NN #1 \c_false_bool } |
| 932 |
\cs_new_protected:Npn \bool_gset_true:N #1 |
| 933 |
{ \cs_gset_eq:NN #1 \c_true_bool } |
| 934 |
\cs_new_protected:Npn \bool_gset_false:N #1 |
| 935 |
{ \cs_gset_eq:NN #1 \c_false_bool } |
| 936 |
\cs_generate_variant:Nn \bool_set_true:N { c } |
| 937 |
\cs_generate_variant:Nn \bool_set_false:N { c } |
| 938 |
\cs_generate_variant:Nn \bool_gset_true:N { c } |
| 939 |
\cs_generate_variant:Nn \bool_gset_false:N { c } |
| 940 |
% \end{macrocode} |
| 941 |
% \end{macro} |
| 942 |
% |
| 943 |
% \begin{macro} |
| 944 |
% { |
| 945 |
% \bool_set_eq:NN, \bool_set_eq:cN, |
| 946 |
% \bool_set_eq:Nc, \bool_set_eq:cc, |
| 947 |
% \bool_gset_eq:NN, \bool_gset_eq:cN, |
| 948 |
% \bool_gset_eq:Nc, \bool_gset_eq:cc |
| 949 |
% } |
| 950 |
% \UnitTested |
| 951 |
% The usual copy code. While it would be cleaner semantically to copy |
| 952 |
% the \cs{cs_set_eq:NN} family of functions, we copy \cs{tl_set_eq:NN} |
| 953 |
% because that has the correct checking code. |
| 954 |
% \begin{macrocode} |
| 955 |
\cs_new_eq:NN \bool_set_eq:NN \tl_set_eq:NN |
| 956 |
\cs_new_eq:NN \bool_gset_eq:NN \tl_gset_eq:NN |
| 957 |
\cs_generate_variant:Nn \bool_set_eq:NN { Nc, cN, cc } |
| 958 |
\cs_generate_variant:Nn \bool_gset_eq:NN { Nc, cN, cc } |
| 959 |
% \end{macrocode} |
| 960 |
% \end{macro} |
| 961 |
% |
| 962 |
% \begin{macro}{\bool_set:Nn, \bool_set:cn} |
| 963 |
% \begin{macro}{\bool_gset:Nn, \bool_gset:cn} |
| 964 |
% This function evaluates a boolean expression and assigns the first |
| 965 |
% argument the meaning \cs{c_true_bool} or \cs{c_false_bool}. Again, |
| 966 |
% we include some checking code. It is important to evaluate the |
| 967 |
% expression before applying the \tn{chardef} primitive, because that |
| 968 |
% primitive sets the left-hand side to \cs{scan_stop:} before looking |
| 969 |
% for the right-hand side. |
| 970 |
% \begin{macrocode} |
| 971 |
\cs_new_protected:Npn \bool_set:Nn #1#2 |
| 972 |
{ |
| 973 |
\exp_last_unbraced:NNNf |
| 974 |
\tex_chardef:D #1 = { \bool_if_p:n {#2} } |
| 975 |
} |
| 976 |
\cs_new_protected:Npn \bool_gset:Nn #1#2 |
| 977 |
{ |
| 978 |
\exp_last_unbraced:NNNNf |
| 979 |
\tex_global:D \tex_chardef:D #1 = { \bool_if_p:n {#2} } |
| 980 |
} |
| 981 |
\cs_generate_variant:Nn \bool_set:Nn { c } |
| 982 |
\cs_generate_variant:Nn \bool_gset:Nn { c } |
| 983 |
% \end{macrocode} |
| 984 |
% \end{macro} |
| 985 |
% \end{macro} |
| 986 |
% |
| 987 |
% \begin{macro}[added = 2018-05-10] |
| 988 |
% {\bool_set_inverse:N, \bool_set_inverse:c, \bool_gset_inverse:N, \bool_gset_inverse:c} |
| 989 |
% Set to \texttt{false} or \texttt{true} locally or globally. |
| 990 |
% \begin{macrocode} |
| 991 |
\cs_new_protected:Npn \bool_set_inverse:N #1 |
| 992 |
{ \bool_if:NTF #1 { \bool_set_false:N } { \bool_set_true:N } #1 } |
| 993 |
\cs_generate_variant:Nn \bool_set_inverse:N { c } |
| 994 |
\cs_new_protected:Npn \bool_gset_inverse:N #1 |
| 995 |
{ \bool_if:NTF #1 { \bool_gset_false:N } { \bool_gset_true:N } #1 } |
| 996 |
\cs_generate_variant:Nn \bool_gset_inverse:N { c } |
| 997 |
% \end{macrocode} |
| 998 |
% \end{macro} |
| 999 |
% |
| 1000 |
% \subsection{Internal auxiliaries} |
| 1001 |
% |
| 1002 |
% \begin{variable}{\q_@@_recursion_tail,\q_@@_recursion_stop} |
| 1003 |
% Internal recursion quarks. |
| 1004 |
% \begin{macrocode} |
| 1005 |
\quark_new:N \q_@@_recursion_tail |
| 1006 |
\quark_new:N \q_@@_recursion_stop |
| 1007 |
% \end{macrocode} |
| 1008 |
% \end{variable} |
| 1009 |
% |
| 1010 |
% \begin{macro}[EXP]{\@@_use_i_delimit_by_q_recursion_stop:nw} |
| 1011 |
% Functions to gobble up to a quark. |
| 1012 |
% \begin{macrocode} |
| 1013 |
\cs_new:Npn \@@_use_i_delimit_by_q_recursion_stop:nw |
| 1014 |
#1 #2 \q_@@_recursion_stop {#1} |
| 1015 |
% \end{macrocode} |
| 1016 |
% \end{macro} |
| 1017 |
% |
| 1018 |
% \begin{macro}[EXP]{\@@_if_recursion_tail_stop_do:nn} |
| 1019 |
% Functions to query recursion quarks. |
| 1020 |
% \begin{macrocode} |
| 1021 |
\__kernel_quark_new_test:N \@@_if_recursion_tail_stop_do:nn |
| 1022 |
% \end{macrocode} |
| 1023 |
% \end{macro} |
| 1024 |
% |
| 1025 |
% \begin{macro}[pTF]{\bool_if:N, \bool_if:c} |
| 1026 |
% \UnitTested |
| 1027 |
% Straight forward here. We could optimize here if we wanted to as |
| 1028 |
% the boolean can just be input directly. |
| 1029 |
% \begin{macrocode} |
| 1030 |
\prg_new_conditional:Npnn \bool_if:N #1 { p , T , F , TF } |
| 1031 |
{ |
| 1032 |
\if_bool:N #1 |
| 1033 |
\prg_return_true: |
| 1034 |
\else: |
| 1035 |
\prg_return_false: |
| 1036 |
\fi: |
| 1037 |
} |
| 1038 |
\prg_generate_conditional_variant:Nnn \bool_if:N { c } { p , T , F , TF } |
| 1039 |
% \end{macrocode} |
| 1040 |
% \end{macro} |
| 1041 |
% |
| 1042 |
% \begin{macro}[EXP]{\bool_to_str:N, \bool_to_str:c, \bool_to_str:n} |
| 1043 |
% Expands to string literal \texttt{true} or \texttt{false}. |
| 1044 |
% \begin{macrocode} |
| 1045 |
\cs_new:Npe \bool_to_str:N #1 |
| 1046 |
{ |
| 1047 |
\exp_not:N \bool_if:NTF #1 |
| 1048 |
{ \tl_to_str:n { true } } { \tl_to_str:n { false } } |
| 1049 |
} |
| 1050 |
\cs_generate_variant:Nn \bool_to_str:N { c } |
| 1051 |
\cs_new:Npe \bool_to_str:n #1 |
| 1052 |
{ |
| 1053 |
\exp_not:N \bool_if:nTF {#1} |
| 1054 |
{ \tl_to_str:n { true } } { \tl_to_str:n { false } } |
| 1055 |
} |
| 1056 |
% \end{macrocode} |
| 1057 |
% \end{macro} |
| 1058 |
% |
| 1059 |
% \begin{macro}{\bool_show:n, \bool_log:n} |
| 1060 |
% Show the truth value of the boolean. |
| 1061 |
% \begin{macrocode} |
| 1062 |
\cs_new_protected:Npn \bool_show:n |
| 1063 |
{ \__kernel_msg_show_eval:Nn \bool_to_str:n } |
| 1064 |
\cs_new_protected:Npn \bool_log:n |
| 1065 |
{ \__kernel_msg_log_eval:Nn \bool_to_str:n } |
| 1066 |
% \end{macrocode} |
| 1067 |
% \end{macro} |
| 1068 |
% |
| 1069 |
% \begin{macro}{\bool_show:N, \bool_show:c, \bool_log:N, \bool_log:c, \@@_show:NN} |
| 1070 |
% Show the truth value of the boolean, as \texttt{true} or |
| 1071 |
% \texttt{false}. |
| 1072 |
% \begin{macrocode} |
| 1073 |
\cs_new_protected:Npn \bool_show:N { \@@_show:NN \tl_show:n } |
| 1074 |
\cs_generate_variant:Nn \bool_show:N { c } |
| 1075 |
\cs_new_protected:Npn \bool_log:N { \@@_show:NN \tl_log:n } |
| 1076 |
\cs_generate_variant:Nn \bool_log:N { c } |
| 1077 |
\cs_new_protected:Npn \@@_show:NN #1#2 |
| 1078 |
{ |
| 1079 |
\__kernel_chk_defined:NT #2 |
| 1080 |
{ |
| 1081 |
\token_case_meaning:NnF #2 |
| 1082 |
{ |
| 1083 |
\c_true_bool { \exp_args:Ne #1 { \token_to_str:N #2 = true } } |
| 1084 |
\c_false_bool { \exp_args:Ne #1 { \token_to_str:N #2 = false } } |
| 1085 |
} |
| 1086 |
{ |
| 1087 |
\msg_error:nneee { kernel } { bad-type } |
| 1088 |
{ \token_to_str:N #2 } { \token_to_meaning:N #2 } { bool } |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} |
| 1092 |
% \end{macrocode} |
| 1093 |
% \end{macro} |
| 1094 |
% |
| 1095 |
% \begin{variable}{\l_tmpa_bool, \l_tmpb_bool, \g_tmpa_bool, \g_tmpb_bool} |
| 1096 |
% A few booleans just if you need them. |
| 1097 |
% \begin{macrocode} |
| 1098 |
\bool_new:N \l_tmpa_bool |
| 1099 |
\bool_new:N \l_tmpb_bool |
| 1100 |
\bool_new:N \g_tmpa_bool |
| 1101 |
\bool_new:N \g_tmpb_bool |
| 1102 |
% \end{macrocode} |
| 1103 |
% \end{variable} |
| 1104 |
% |
| 1105 |
% \begin{macro}[pTF]{\bool_if_exist:N, \bool_if_exist:c} |
| 1106 |
% Copies of the \texttt{cs} functions defined in \pkg{l3basics}. |
| 1107 |
% \begin{macrocode} |
| 1108 |
\prg_new_eq_conditional:NNn \bool_if_exist:N \cs_if_exist:N |
| 1109 |
{ TF , T , F , p } |
| 1110 |
\prg_new_eq_conditional:NNn \bool_if_exist:c \cs_if_exist:c |
| 1111 |
{ TF , T , F , p } |
| 1112 |
% \end{macrocode} |
| 1113 |
% \end{macro} |
| 1114 |
% |
| 1115 |
% \subsection{Boolean expressions} |
| 1116 |
% |
| 1117 |
% \begin{macro}[pTF]{\bool_if:n} |
| 1118 |
% \UnitTested |
| 1119 |
% Evaluating the truth value of a list of predicates is done using an |
| 1120 |
% input syntax somewhat similar to the one found in other programming |
| 1121 |
% languages with |(| and |)| for grouping, |!| for logical |
| 1122 |
% \enquote{Not}, |&&| for logical \enquote{And} and \verb"||" for |
| 1123 |
% logical \enquote{Or}. However, they perform eager evaluation. |
| 1124 |
% We shall use the terms Not, And, Or, Open and |
| 1125 |
% Close for these operations. |
| 1126 |
% |
| 1127 |
% Any expression is terminated by a Close operation. Evaluation |
| 1128 |
% happens from left to right in the following manner using a GetNext |
| 1129 |
% function: |
| 1130 |
% \begin{itemize} |
| 1131 |
% \item If an Open is seen, start evaluating a new expression using |
| 1132 |
% the Eval function and call GetNext again. |
| 1133 |
% \item If a Not is seen, remove the |!| and call a GetNext |
| 1134 |
% function with the logic reversed. |
| 1135 |
% \item If none of the above, reinsert the token found (this is |
| 1136 |
% supposed to be a predicate function) in front of an Eval |
| 1137 |
% function, which evaluates it to the boolean value \meta{true} or |
| 1138 |
% \meta{false}. |
| 1139 |
% \end{itemize} |
| 1140 |
% The Eval function then contains a post-processing operation which |
| 1141 |
% grabs the instruction following the predicate. This is either And, |
| 1142 |
% Or or Close. In each case the truth value is used to determine |
| 1143 |
% where to go next. The following situations can arise: |
| 1144 |
% \begin{description} |
| 1145 |
% \item[\meta{true}And] Current truth value is true, logical And |
| 1146 |
% seen, continue with GetNext to examine truth value of next |
| 1147 |
% boolean (sub-)expression. |
| 1148 |
% \item[\meta{false}And] Current truth value is false, logical And |
| 1149 |
% seen, stop using the values of predicates within this |
| 1150 |
% sub-expression until the next Close. Then return \meta{false}. |
| 1151 |
% \item[\meta{true}Or] Current truth value is true, logical Or seen, |
| 1152 |
% stop using the values of predicates within this sub-expression |
| 1153 |
% until the nearest Close. Then return \meta{true}. |
| 1154 |
% \item[\meta{false}Or] Current truth value is false, logical Or |
| 1155 |
% seen, continue with GetNext to examine truth value of next |
| 1156 |
% boolean (sub-)expression. |
| 1157 |
% \item[\meta{true}Close] Current truth value is true, Close |
| 1158 |
% seen, return \meta{true}. |
| 1159 |
% \item[\meta{false}Close] Current truth value is false, Close |
| 1160 |
% seen, return \meta{false}. |
| 1161 |
% \end{description} |
| 1162 |
% \begin{macrocode} |
| 1163 |
\prg_new_conditional:Npnn \bool_if:n #1 { T , F , TF } |
| 1164 |
{ |
| 1165 |
\if_predicate:w \bool_if_p:n {#1} |
| 1166 |
\prg_return_true: |
| 1167 |
\else: |
| 1168 |
\prg_return_false: |
| 1169 |
\fi: |
| 1170 |
} |
| 1171 |
% \end{macrocode} |
| 1172 |
% \end{macro} |
| 1173 |
% |
| 1174 |
% \begin{macro}[EXP]{\bool_if_p:n, \@@_if_p:n, \@@_if_p_aux:w} |
| 1175 |
% To speed up the case of a single predicate, \texttt{f}-expand and |
| 1176 |
% check whether the result is one token (possibly surrounded by |
| 1177 |
% spaces), which must be \cs{c_true_bool} or \cs{c_false_bool}. We |
| 1178 |
% use a version of \cs{tl_if_single:nTF} optimized for speed since we |
| 1179 |
% know that an empty~|#1| is an error. The auxiliary |
| 1180 |
% \cs{@@_if_p_aux:w} removes the trailing parenthesis and gets rid of |
| 1181 |
% any space, then returns \cs{c_true_bool} or \cs{c_false_bool} as |
| 1182 |
% appropriate. This extra work around is because in a |
| 1183 |
% \cs{bool_set:Nn}, the underlying \tn{chardef} turns the bool being |
| 1184 |
% set temporarily equal to \tn{relax}, thus assigning a boolean to |
| 1185 |
% itself would fail (gh/1055). For the general case, first issue a |
| 1186 |
% \cs{group_align_safe_begin:} as we are using |&&| as |
| 1187 |
% syntax shorthand for the And operation and we need to hide it for |
| 1188 |
% \TeX{}. This group is closed after \cs{@@_get_next:NN} returns |
| 1189 |
% \cs{c_true_bool} or \cs{c_false_bool}. That function requires the |
| 1190 |
% trailing parenthesis to know where the expression ends. |
| 1191 |
% \begin{macrocode} |
| 1192 |
\cs_new:Npn \bool_if_p:n { \exp_args:Nf \@@_if_p:n } |
| 1193 |
\cs_new:Npn \@@_if_p:n #1 |
| 1194 |
{ |
| 1195 |
\tl_if_empty:oT { \use_none:nn #1 . } { \@@_if_p_aux:w } |
| 1196 |
\group_align_safe_begin: |
| 1197 |
\exp_after:wN |
| 1198 |
\group_align_safe_end: |
| 1199 |
\exp:w \exp_end_continue_f:w % ( |
| 1200 |
\@@_get_next:NN \use_i:nnnn #1 ) |
| 1201 |
} |
| 1202 |
\cs_new:Npn \@@_if_p_aux:w #1 \use_i:nnnn #2#3 |
| 1203 |
{ \bool_if:NTF #2 \c_true_bool \c_false_bool } |
| 1204 |
% \end{macrocode} |
| 1205 |
% \end{macro} |
| 1206 |
% |
| 1207 |
% \begin{macro}{\@@_get_next:NN} |
| 1208 |
% The GetNext operation. Its first argument is \cs{use_i:nnnn}, |
| 1209 |
% \cs{use_ii:nnnn}, \cs{use_iii:nnnn}, or \cs{use_iv:nnnn} (we call |
| 1210 |
% these \enquote{states}). In the first state, this function |
| 1211 |
% eventually expand to the truth value \cs{c_true_bool} or |
| 1212 |
% \cs{c_false_bool} of the expression which follows until the next |
| 1213 |
% unmatched closing parenthesis. For instance |
| 1214 |
% \enquote{\cs{@@_get_next:NN} \cs{use_i:nnnn} \cs{c_true_bool} |
| 1215 |
% \texttt{\&\&} \cs{c_true_bool} \texttt{)}} (including the closing |
| 1216 |
% parenthesis) expands to \cs{c_true_bool}. In the second state |
| 1217 |
% (after a |!|) the logic is reversed. We call these two states |
| 1218 |
% \enquote{normal} and the next two \enquote{skipping}. In the third |
| 1219 |
% state (after \cs{c_true_bool}\verb"||") it always returns |
| 1220 |
% \cs{c_true_bool}. In the fourth state (after \cs{c_false_bool}|&&|) |
| 1221 |
% it always returns \cs{c_false_bool} and also stops when encountering |
| 1222 |
% \verb"||", not only parentheses. This code itself is a switch: if |
| 1223 |
% what follows is neither |!| nor |(|, we assume it is a predicate. |
| 1224 |
% \begin{macrocode} |
| 1225 |
\cs_new:Npn \@@_get_next:NN #1#2 |
| 1226 |
{ |
| 1227 |
\use:c |
| 1228 |
{ |
| 1229 |
@@_ |
| 1230 |
\if_meaning:w !#2 ! \else: \if_meaning:w (#2 ( \else: p \fi: \fi: |
| 1231 |
:Nw |
| 1232 |
} |
| 1233 |
#1 #2 |
| 1234 |
} |
| 1235 |
% \end{macrocode} |
| 1236 |
% \end{macro} |
| 1237 |
% |
| 1238 |
% \begin{macro}{\@@_!:Nw} |
| 1239 |
% The Not operation reverses the logic: it discards the |!|~token and |
| 1240 |
% calls the GetNext operation with the appropriate first argument. |
| 1241 |
% Namely the first and second states are interchanged, but after |
| 1242 |
% \cs{c_true_bool}\verb"||" or \cs{c_false_bool}|&&| the |!|~is |
| 1243 |
% ignored. |
| 1244 |
% \begin{macrocode} |
| 1245 |
\cs_new:cpn { @@_!:Nw } #1#2 |
| 1246 |
{ |
| 1247 |
\exp_after:wN \@@_get_next:NN |
| 1248 |
#1 \use_ii:nnnn \use_i:nnnn \use_iii:nnnn \use_iv:nnnn |
| 1249 |
} |
| 1250 |
% \end{macrocode} |
| 1251 |
% \end{macro} |
| 1252 |
% |
| 1253 |
% \begin{macro}{\@@_(:Nw} |
| 1254 |
% The Open operation starts a sub-expression after discarding the open |
| 1255 |
% parenthesis. This is done by calling GetNext (which eventually |
| 1256 |
% discards the corresponding closing parenthesis), with a |
| 1257 |
% post-processing step which looks for And, Or or Close after the |
| 1258 |
% group. |
| 1259 |
% \begin{macrocode} |
| 1260 |
\cs_new:cpn { @@_(:Nw } #1#2 |
| 1261 |
{ |
| 1262 |
\exp_after:wN \@@_choose:NNN \exp_after:wN #1 |
| 1263 |
\int_value:w \@@_get_next:NN \use_i:nnnn |
| 1264 |
} |
| 1265 |
% \end{macrocode} |
| 1266 |
% \end{macro} |
| 1267 |
% |
| 1268 |
% \begin{macro}{\@@_p:Nw} |
| 1269 |
% If what follows GetNext is neither |!| nor |(|, evaluate the |
| 1270 |
% predicate using the primitive \cs{int_value:w}. The canonical |
| 1271 |
% \texttt{true} and \texttt{false} values have numerical values $1$ |
| 1272 |
% and $0$ respectively. Look for And, Or or Close afterwards. |
| 1273 |
% \begin{macrocode} |
| 1274 |
\cs_new:cpn { @@_p:Nw } #1 |
| 1275 |
{ \exp_after:wN \@@_choose:NNN \exp_after:wN #1 \int_value:w } |
| 1276 |
% \end{macrocode} |
| 1277 |
% \end{macro} |
| 1278 |
% |
| 1279 |
% \begin{macro}{\@@_choose:NNN} |
| 1280 |
% \begin{macro}+\@@_)_0:+ |
| 1281 |
% \begin{macro}+\@@_)_1:+ |
| 1282 |
% \begin{macro}+\@@_)_2:+ |
| 1283 |
% \begin{macro}+\@@_&_0:+ |
| 1284 |
% \begin{macro}+\@@_&_1:+ |
| 1285 |
% \begin{macro}+\@@_&_2:+ |
| 1286 |
% \begin{macro}+\@@_|_0:+ |
| 1287 |
% \begin{macro}+\@@_|_1:+ |
| 1288 |
% \begin{macro}+\@@_|_2:+ |
| 1289 |
% The arguments are |#1|: a function such as \cs{use_i:nnnn}, |#2|: |
| 1290 |
% $0$ or $1$ encoding the current truth value, |#3|: the next |
| 1291 |
% operation, And, Or or Close. We distinguish three cases according |
| 1292 |
% to a combination of |#1| and |#2|. Case~2 is when |#1| is |
| 1293 |
% \cs{use_iii:nnnn} (state~3), namely after |
| 1294 |
% \cs{c_true_bool}~\verb"||". Case~1 is when |#1| is \cs{use_i:nnnn} |
| 1295 |
% and |#2| is \texttt{true} or when |#1| is \cs{use_ii:nnnn} and |#2| |
| 1296 |
% is \texttt{false}, for instance for |!|\cs{c_false_bool}. Case~0 |
| 1297 |
% includes the same with \texttt{true}/\texttt{false} interchanged and |
| 1298 |
% the case where |#1| is \cs{use_iv:nnnn} namely after |
| 1299 |
% \cs{c_false_bool}~|&&|. |
| 1300 |
% |
| 1301 |
% When seeing~|)| the current subexpression is done, leave the |
| 1302 |
% appropriate boolean. When seeing~|&| in case~0 go into state~4, |
| 1303 |
% equivalent to having seen \cs{c_false_bool}~|&&|. In case~1, namely |
| 1304 |
% when the argument is \texttt{true} and we are in a normal state |
| 1305 |
% continue in the normal state~1. In case~2, namely when skipping |
| 1306 |
% alternatives in an Or, continue in the same state. When |
| 1307 |
% seeing~\verb"|" in case~0, continue in a normal state; in particular |
| 1308 |
% stop skipping for \cs{c_false_bool}~|&&| because that binds more |
| 1309 |
% tightly than~\verb"||". In the other two cases start skipping for |
| 1310 |
% \cs{c_true_bool}~\verb"||". |
| 1311 |
% \begin{macrocode} |
| 1312 |
\cs_new:Npn \@@_choose:NNN #1#2#3 |
| 1313 |
{ |
| 1314 |
\use:c |
| 1315 |
{ |
| 1316 |
@@_ \token_to_str:N #3 _ |
| 1317 |
#1 #2 { \if_meaning:w 0 #2 1 \else: 0 \fi: } 2 0 : |
| 1318 |
} |
| 1319 |
} |
| 1320 |
\cs_new:cpn { @@_)_0: } { \c_false_bool } |
| 1321 |
\cs_new:cpn { @@_)_1: } { \c_true_bool } |
| 1322 |
\cs_new:cpn { @@_)_2: } { \c_true_bool } |
| 1323 |
\cs_new:cpn { @@_&_0: } & { \@@_get_next:NN \use_iv:nnnn } |
| 1324 |
\cs_new:cpn { @@_&_1: } & { \@@_get_next:NN \use_i:nnnn } |
| 1325 |
\cs_new:cpn { @@_&_2: } & { \@@_get_next:NN \use_iii:nnnn } |
| 1326 |
\cs_new:cpn { @@_|_0: } | { \@@_get_next:NN \use_i:nnnn } |
| 1327 |
\cs_new:cpn { @@_|_1: } | { \@@_get_next:NN \use_iii:nnnn } |
| 1328 |
\cs_new:cpn { @@_|_2: } | { \@@_get_next:NN \use_iii:nnnn } |
| 1329 |
% \end{macrocode} |
| 1330 |
% \end{macro} |
| 1331 |
% \end{macro} |
| 1332 |
% \end{macro} |
| 1333 |
% \end{macro} |
| 1334 |
% \end{macro} |
| 1335 |
% \end{macro} |
| 1336 |
% \end{macro} |
| 1337 |
% \end{macro} |
| 1338 |
% \end{macro} |
| 1339 |
% \end{macro} |
| 1340 |
% |
| 1341 |
% \begin{macro}[pTF]{\bool_lazy_all:n} |
| 1342 |
% \begin{macro}{\@@_lazy_all:n} |
| 1343 |
% \UnitTested |
| 1344 |
% Go through the list of expressions, stopping whenever an expression |
| 1345 |
% is \texttt{false}. If the end is reached without finding any |
| 1346 |
% \texttt{false} expression, then the result is \texttt{true}. |
| 1347 |
% \begin{macrocode} |
| 1348 |
\cs_new:Npn \bool_lazy_all_p:n #1 |
| 1349 |
{ \@@_lazy_all:n #1 \q_@@_recursion_tail \q_@@_recursion_stop } |
| 1350 |
\prg_new_conditional:Npnn \bool_lazy_all:n #1 { T , F , TF } |
| 1351 |
{ |
| 1352 |
\if_predicate:w \bool_lazy_all_p:n {#1} |
| 1353 |
\prg_return_true: |
| 1354 |
\else: |
| 1355 |
\prg_return_false: |
| 1356 |
\fi: |
| 1357 |
} |
| 1358 |
\cs_new:Npn \@@_lazy_all:n #1 |
| 1359 |
{ |
| 1360 |
\@@_if_recursion_tail_stop_do:nn {#1} { \c_true_bool } |
| 1361 |
\bool_if:nF {#1} |
| 1362 |
{ \@@_use_i_delimit_by_q_recursion_stop:nw { \c_false_bool } } |
| 1363 |
\@@_lazy_all:n |
| 1364 |
} |
| 1365 |
% \end{macrocode} |
| 1366 |
% \end{macro} |
| 1367 |
% \end{macro} |
| 1368 |
% |
| 1369 |
% \begin{macro}[pTF]{\bool_lazy_and:nn} |
| 1370 |
% \UnitTested |
| 1371 |
% Only evaluate the second expression if the first is \texttt{true}. |
| 1372 |
% Note that |#2| must be removed as an argument, not just by skipping |
| 1373 |
% to the \cs{else:} branch of the conditional since |#2| may contain |
| 1374 |
% unbalanced \TeX{} conditionals. |
| 1375 |
% \begin{macrocode} |
| 1376 |
\prg_new_conditional:Npnn \bool_lazy_and:nn #1#2 { p , T , F , TF } |
| 1377 |
{ |
| 1378 |
\if_predicate:w |
| 1379 |
\bool_if:nTF {#1} { \bool_if_p:n {#2} } { \c_false_bool } |
| 1380 |
\prg_return_true: |
| 1381 |
\else: |
| 1382 |
\prg_return_false: |
| 1383 |
\fi: |
| 1384 |
} |
| 1385 |
% \end{macrocode} |
| 1386 |
% \end{macro} |
| 1387 |
% |
| 1388 |
% \begin{macro}[pTF]{\bool_lazy_any:n} |
| 1389 |
% \begin{macro}{\@@_lazy_any:n} |
| 1390 |
% \UnitTested |
| 1391 |
% Go through the list of expressions, stopping whenever an expression |
| 1392 |
% is \texttt{true}. If the end is reached without finding any |
| 1393 |
% \texttt{true} expression, then the result is \texttt{false}. |
| 1394 |
% \begin{macrocode} |
| 1395 |
\cs_new:Npn \bool_lazy_any_p:n #1 |
| 1396 |
{ \@@_lazy_any:n #1 \q_@@_recursion_tail \q_@@_recursion_stop } |
| 1397 |
\prg_new_conditional:Npnn \bool_lazy_any:n #1 { T , F , TF } |
| 1398 |
{ |
| 1399 |
\if_predicate:w \bool_lazy_any_p:n {#1} |
| 1400 |
\prg_return_true: |
| 1401 |
\else: |
| 1402 |
\prg_return_false: |
| 1403 |
\fi: |
| 1404 |
} |
| 1405 |
\cs_new:Npn \@@_lazy_any:n #1 |
| 1406 |
{ |
| 1407 |
\@@_if_recursion_tail_stop_do:nn {#1} { \c_false_bool } |
| 1408 |
\bool_if:nT {#1} |
| 1409 |
{ \@@_use_i_delimit_by_q_recursion_stop:nw { \c_true_bool } } |
| 1410 |
\@@_lazy_any:n |
| 1411 |
} |
| 1412 |
% \end{macrocode} |
| 1413 |
% \end{macro} |
| 1414 |
% \end{macro} |
| 1415 |
% |
| 1416 |
% \begin{macro}[pTF]{\bool_lazy_or:nn} |
| 1417 |
% \UnitTested |
| 1418 |
% Only evaluate the second expression if the first is \texttt{false}. |
| 1419 |
% \begin{macrocode} |
| 1420 |
\prg_new_conditional:Npnn \bool_lazy_or:nn #1#2 { p , T , F , TF } |
| 1421 |
{ |
| 1422 |
\if_predicate:w |
| 1423 |
\bool_if:nTF {#1} { \c_true_bool } { \bool_if_p:n {#2} } |
| 1424 |
\prg_return_true: |
| 1425 |
\else: |
| 1426 |
\prg_return_false: |
| 1427 |
\fi: |
| 1428 |
} |
| 1429 |
% \end{macrocode} |
| 1430 |
% \end{macro} |
| 1431 |
% |
| 1432 |
% \begin{macro}{\bool_not_p:n} |
| 1433 |
% \UnitTested |
| 1434 |
% The Not variant just reverses the outcome of \cs{bool_if_p:n}. Can |
| 1435 |
% be optimized but this is nice and simple and according to the |
| 1436 |
% implementation plan. Not even particularly useful to have it when |
| 1437 |
% the infix notation is easier to use. |
| 1438 |
% \begin{macrocode} |
| 1439 |
\cs_new:Npn \bool_not_p:n #1 { \bool_if_p:n { ! ( #1 ) } } |
| 1440 |
% \end{macrocode} |
| 1441 |
% \end{macro} |
| 1442 |
% |
| 1443 |
% \begin{macro}[pTF]{\bool_xor:nn} |
| 1444 |
% \UnitTested |
| 1445 |
% Exclusive or. If the boolean expressions have same truth value, |
| 1446 |
% return \texttt{false}, otherwise return \texttt{true}. |
| 1447 |
% \begin{macrocode} |
| 1448 |
\prg_new_conditional:Npnn \bool_xor:nn #1#2 { p , T , F , TF } |
| 1449 |
{ |
| 1450 |
\bool_if:nT {#1} \reverse_if:N |
| 1451 |
\if_predicate:w \bool_if_p:n {#2} |
| 1452 |
\prg_return_true: |
| 1453 |
\else: |
| 1454 |
\prg_return_false: |
| 1455 |
\fi: |
| 1456 |
} |
| 1457 |
% \end{macrocode} |
| 1458 |
% \end{macro} |
| 1459 |
% |
| 1460 |
% \subsection{Logical loops} |
| 1461 |
% |
| 1462 |
% \begin{macro}{\bool_while_do:Nn, \bool_while_do:cn} |
| 1463 |
% \UnitTested |
| 1464 |
% \begin{macro}{\bool_until_do:Nn, \bool_until_do:cn} |
| 1465 |
% \UnitTested |
| 1466 |
% A |while| loop where the boolean is tested before executing the |
| 1467 |
% statement. The \enquote{while} version executes the code as long as the |
| 1468 |
% boolean is true; the \enquote{until} version executes the code as |
| 1469 |
% long as the boolean is false. |
| 1470 |
% \begin{macrocode} |
| 1471 |
\cs_new:Npn \bool_while_do:Nn #1#2 |
| 1472 |
{ \bool_if:NT #1 { #2 \bool_while_do:Nn #1 {#2} } } |
| 1473 |
\cs_new:Npn \bool_until_do:Nn #1#2 |
| 1474 |
{ \bool_if:NF #1 { #2 \bool_until_do:Nn #1 {#2} } } |
| 1475 |
\cs_generate_variant:Nn \bool_while_do:Nn { c } |
| 1476 |
\cs_generate_variant:Nn \bool_until_do:Nn { c } |
| 1477 |
% \end{macrocode} |
| 1478 |
% \end{macro} |
| 1479 |
% \end{macro} |
| 1480 |
% |
| 1481 |
% \begin{macro}{\bool_do_while:Nn, \bool_do_while:cn} |
| 1482 |
% \UnitTested |
| 1483 |
% \begin{macro}{\bool_do_until:Nn, \bool_do_until:cn} |
| 1484 |
% \UnitTested |
| 1485 |
% A |do-while| loop where the body is performed at least once and the |
| 1486 |
% boolean is tested after executing the body. Otherwise identical to |
| 1487 |
% the above functions. |
| 1488 |
% \begin{macrocode} |
| 1489 |
\cs_new:Npn \bool_do_while:Nn #1#2 |
| 1490 |
{ #2 \bool_if:NT #1 { \bool_do_while:Nn #1 {#2} } } |
| 1491 |
\cs_new:Npn \bool_do_until:Nn #1#2 |
| 1492 |
{ #2 \bool_if:NF #1 { \bool_do_until:Nn #1 {#2} } } |
| 1493 |
\cs_generate_variant:Nn \bool_do_while:Nn { c } |
| 1494 |
\cs_generate_variant:Nn \bool_do_until:Nn { c } |
| 1495 |
% \end{macrocode} |
| 1496 |
% \end{macro} |
| 1497 |
% \end{macro} |
| 1498 |
% |
| 1499 |
% \begin{macro} |
| 1500 |
% { |
| 1501 |
% \bool_while_do:nn, \bool_do_while:nn , |
| 1502 |
% \bool_until_do:nn, \bool_do_until:nn |
| 1503 |
% } |
| 1504 |
% \UnitTested |
| 1505 |
% Loop functions with the test either before or after the first body |
| 1506 |
% expansion. |
| 1507 |
% \begin{macrocode} |
| 1508 |
\cs_new:Npn \bool_while_do:nn #1#2 |
| 1509 |
{ |
| 1510 |
\bool_if:nT {#1} |
| 1511 |
{ |
| 1512 |
#2 |
| 1513 |
\bool_while_do:nn {#1} {#2} |
| 1514 |
} |
| 1515 |
} |
| 1516 |
\cs_new:Npn \bool_do_while:nn #1#2 |
| 1517 |
{ |
| 1518 |
#2 |
| 1519 |
\bool_if:nT {#1} { \bool_do_while:nn {#1} {#2} } |
| 1520 |
} |
| 1521 |
\cs_new:Npn \bool_until_do:nn #1#2 |
| 1522 |
{ |
| 1523 |
\bool_if:nF {#1} |
| 1524 |
{ |
| 1525 |
#2 |
| 1526 |
\bool_until_do:nn {#1} {#2} |
| 1527 |
} |
| 1528 |
} |
| 1529 |
\cs_new:Npn \bool_do_until:nn #1#2 |
| 1530 |
{ |
| 1531 |
#2 |
| 1532 |
\bool_if:nF {#1} { \bool_do_until:nn {#1} {#2} } |
| 1533 |
} |
| 1534 |
% \end{macrocode} |
| 1535 |
% \end{macro} |
| 1536 |
% |
| 1537 |
% \begin{variable}{\s_@@_mark,\s_@@_stop} |
| 1538 |
% Internal scan marks. |
| 1539 |
% \begin{macrocode} |
| 1540 |
\scan_new:N \s_@@_mark |
| 1541 |
\scan_new:N \s_@@_stop |
| 1542 |
% \end{macrocode} |
| 1543 |
% \end{variable} |
| 1544 |
% |
| 1545 |
% \begin{macro}[EXP, noTF]{\bool_case:n} |
| 1546 |
% \begin{macro}{\@@_case:NnTF} |
| 1547 |
% \begin{macro}{\@@_case:w,a\@@_case_end:nw} |
| 1548 |
% For boolean cases the overall idea is the same as for |
| 1549 |
% \cs{str_case:nnTF} as described in \pkg{l3str}. |
| 1550 |
% \begin{macrocode} |
| 1551 |
\cs_new:Npn \bool_case:nTF |
| 1552 |
{ \exp:w \@@_case:nTF } |
| 1553 |
\cs_new:Npn \bool_case:nT #1#2 |
| 1554 |
{ \exp:w \@@_case:nTF {#1} {#2} { } } |
| 1555 |
\cs_new:Npn \bool_case:nF #1 |
| 1556 |
{ \exp:w \@@_case:nTF {#1} { } } |
| 1557 |
\cs_new:Npn \bool_case:n #1 |
| 1558 |
{ \exp:w \@@_case:nTF {#1} { } { } } |
| 1559 |
\cs_new:Npn \@@_case:nTF #1#2#3 |
| 1560 |
{ |
| 1561 |
\@@_case:w |
| 1562 |
#1 \c_true_bool { } \s_@@_mark {#2} \s_@@_mark {#3} \s_@@_stop |
| 1563 |
} |
| 1564 |
\cs_new:Npn \@@_case:w #1#2 |
| 1565 |
{ |
| 1566 |
\bool_if:nTF {#1} |
| 1567 |
{ \@@_case_end:nw {#2} } |
| 1568 |
{ \@@_case:w } |
| 1569 |
} |
| 1570 |
\cs_new:Npn \@@_case_end:nw #1#2#3 \s_@@_mark #4#5 \s_@@_stop |
| 1571 |
{ \exp_end: #1 #4 } |
| 1572 |
% \end{macrocode} |
| 1573 |
% \end{macro} |
| 1574 |
% \end{macro} |
| 1575 |
% \end{macro} |
| 1576 |
% |
| 1577 |
% \subsection{Producing multiple copies} |
| 1578 |
% |
| 1579 |
% \begin{macrocode} |
| 1580 |
%<@@=prg> |
| 1581 |
% \end{macrocode} |
| 1582 |
% |
| 1583 |
% \begin{macro}{\prg_replicate:nn} |
| 1584 |
% \UnitTested |
| 1585 |
% \begin{macro}{\@@_replicate:N, \@@_replicate_first:N} |
| 1586 |
% \begin{macro}{\@@_replicate_} |
| 1587 |
% \begin{macro} |
| 1588 |
% { |
| 1589 |
% \@@_replicate_0:n, |
| 1590 |
% \@@_replicate_1:n, |
| 1591 |
% \@@_replicate_2:n, |
| 1592 |
% \@@_replicate_3:n, |
| 1593 |
% \@@_replicate_4:n, |
| 1594 |
% \@@_replicate_5:n, |
| 1595 |
% \@@_replicate_6:n, |
| 1596 |
% \@@_replicate_7:n, |
| 1597 |
% \@@_replicate_8:n, |
| 1598 |
% \@@_replicate_9:n |
| 1599 |
% } |
| 1600 |
% \begin{macro} |
| 1601 |
% { |
| 1602 |
% \@@_replicate_first_-:n, |
| 1603 |
% \@@_replicate_first_0:n, |
| 1604 |
% \@@_replicate_first_1:n, |
| 1605 |
% \@@_replicate_first_2:n, |
| 1606 |
% \@@_replicate_first_3:n, |
| 1607 |
% \@@_replicate_first_4:n, |
| 1608 |
% \@@_replicate_first_5:n, |
| 1609 |
% \@@_replicate_first_6:n, |
| 1610 |
% \@@_replicate_first_7:n, |
| 1611 |
% \@@_replicate_first_8:n, |
| 1612 |
% \@@_replicate_first_9:n |
| 1613 |
% } |
| 1614 |
% This function uses a cascading csname technique by David Kastrup |
| 1615 |
% (who else :-) |
| 1616 |
% |
| 1617 |
% The idea is to make the input |25| result in first adding five, and |
| 1618 |
% then 20 copies of the code to be replicated. The technique uses |
| 1619 |
% cascading csnames which means that we start building several csnames |
| 1620 |
% so we end up with a list of functions to be called in reverse |
| 1621 |
% order. This is important here (and other places) because it means |
| 1622 |
% that we can for instance make the function that inserts five copies |
| 1623 |
% of something to also hand down ten to the next function in |
| 1624 |
% line. This is exactly what happens here: in the example with |25| |
| 1625 |
% then the next function is the one that inserts two copies but it |
| 1626 |
% sees the ten copies handed down by the previous function. In order |
| 1627 |
% to avoid the last function to insert say, $100$ copies of the original |
| 1628 |
% argument just to gobble them again we define separate functions to |
| 1629 |
% be inserted first. These functions also close the expansion of |
| 1630 |
% \cs{exp:w}, which ensures that \cs{prg_replicate:nn} only |
| 1631 |
% requires two steps of expansion. |
| 1632 |
% |
| 1633 |
% This function has one flaw though: Since it constantly passes down |
| 1634 |
% ten copies of its previous argument it severely affects the main |
| 1635 |
% memory once you start demanding hundreds of thousands of copies. Now |
| 1636 |
% I don't think this is a real limitation for any ordinary use, and if |
| 1637 |
% necessary, it is possible to write \cs{prg_replicate:nn} |{1000}| |
| 1638 |
% |{| \cs{prg_replicate:nn} |{1000}| \Arg{code}~|}|. An |
| 1639 |
% alternative approach is to create a string of |m|'s with |
| 1640 |
% \cs{exp:w} which can be done with just four macros but that |
| 1641 |
% method has its own problems since it can exhaust the string |
| 1642 |
% pool. Also, it is considerably slower than what we use here so the |
| 1643 |
% few extra csnames are well spent I would say. |
| 1644 |
% \begin{macrocode} |
| 1645 |
\cs_new:Npn \prg_replicate:nn #1 |
| 1646 |
{ |
| 1647 |
\exp:w |
| 1648 |
\exp_after:wN \@@_replicate_first:N |
| 1649 |
\int_value:w \int_eval:n {#1} |
| 1650 |
\cs_end: |
| 1651 |
} |
| 1652 |
\cs_new:Npn \@@_replicate:N #1 |
| 1653 |
{ \cs:w @@_replicate_#1 :n \@@_replicate:N } |
| 1654 |
\cs_new:Npn \@@_replicate_first:N #1 |
| 1655 |
{ \cs:w @@_replicate_first_ #1 :n \@@_replicate:N } |
| 1656 |
% \end{macrocode} |
| 1657 |
% Then comes all the functions that do the hard work of inserting all |
| 1658 |
% the copies. The first function takes |:n| as a parameter. |
| 1659 |
% \begin{macrocode} |
| 1660 |
\cs_new:Npn \@@_replicate_ :n #1 { \cs_end: } |
| 1661 |
\cs_new:cpn { @@_replicate_0:n } #1 |
| 1662 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} } |
| 1663 |
\cs_new:cpn { @@_replicate_1:n } #1 |
| 1664 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1 } |
| 1665 |
\cs_new:cpn { @@_replicate_2:n } #1 |
| 1666 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1#1 } |
| 1667 |
\cs_new:cpn { @@_replicate_3:n } #1 |
| 1668 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1#1#1 } |
| 1669 |
\cs_new:cpn { @@_replicate_4:n } #1 |
| 1670 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1#1#1#1 } |
| 1671 |
\cs_new:cpn { @@_replicate_5:n } #1 |
| 1672 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1#1#1#1#1 } |
| 1673 |
\cs_new:cpn { @@_replicate_6:n } #1 |
| 1674 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1#1#1#1#1#1 } |
| 1675 |
\cs_new:cpn { @@_replicate_7:n } #1 |
| 1676 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1#1#1#1#1#1#1 } |
| 1677 |
\cs_new:cpn { @@_replicate_8:n } #1 |
| 1678 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1#1#1#1#1#1#1#1 } |
| 1679 |
\cs_new:cpn { @@_replicate_9:n } #1 |
| 1680 |
{ \cs_end: {#1#1#1#1#1#1#1#1#1#1} #1#1#1#1#1#1#1#1#1 } |
| 1681 |
% \end{macrocode} |
| 1682 |
% Users shouldn't ask for something to be replicated once or even |
| 1683 |
% not at all but\dots |
| 1684 |
% \begin{macrocode} |
| 1685 |
\cs_new:cpn { @@_replicate_first_-:n } #1 |
| 1686 |
{ |
| 1687 |
\exp_end: |
| 1688 |
\msg_expandable_error:nn { prg } { negative-replication } |
| 1689 |
} |
| 1690 |
\cs_new:cpn { @@_replicate_first_0:n } #1 { \exp_end: } |
| 1691 |
\cs_new:cpn { @@_replicate_first_1:n } #1 { \exp_end: #1 } |
| 1692 |
\cs_new:cpn { @@_replicate_first_2:n } #1 { \exp_end: #1#1 } |
| 1693 |
\cs_new:cpn { @@_replicate_first_3:n } #1 { \exp_end: #1#1#1 } |
| 1694 |
\cs_new:cpn { @@_replicate_first_4:n } #1 { \exp_end: #1#1#1#1 } |
| 1695 |
\cs_new:cpn { @@_replicate_first_5:n } #1 { \exp_end: #1#1#1#1#1 } |
| 1696 |
\cs_new:cpn { @@_replicate_first_6:n } #1 { \exp_end: #1#1#1#1#1#1 } |
| 1697 |
\cs_new:cpn { @@_replicate_first_7:n } #1 { \exp_end: #1#1#1#1#1#1#1 } |
| 1698 |
\cs_new:cpn { @@_replicate_first_8:n } #1 { \exp_end: #1#1#1#1#1#1#1#1 } |
| 1699 |
\cs_new:cpn { @@_replicate_first_9:n } #1 |
| 1700 |
{ \exp_end: #1#1#1#1#1#1#1#1#1 } |
| 1701 |
% \end{macrocode} |
| 1702 |
% \end{macro} |
| 1703 |
% \end{macro} |
| 1704 |
% \end{macro} |
| 1705 |
% \end{macro} |
| 1706 |
% \end{macro} |
| 1707 |
% |
| 1708 |
% \subsection{Detecting \TeX{}'s mode} |
| 1709 |
% |
| 1710 |
% \begin{macro}[pTF]{\mode_if_vertical:} |
| 1711 |
% \UnitTested |
| 1712 |
% For testing vertical mode. Strikes me here on the bus with David, |
| 1713 |
% that as long as we are just talking about returning true and |
| 1714 |
% false states, we can just use the primitive conditionals for this |
| 1715 |
% and gobbling the \cs{exp_end:} in the input stream. However this |
| 1716 |
% requires knowledge of the implementation so we keep things nice |
| 1717 |
% and clean and use the return statements. |
| 1718 |
% \begin{macrocode} |
| 1719 |
\prg_new_conditional:Npnn \mode_if_vertical: { p , T , F , TF } |
| 1720 |
{ \if_mode_vertical: \prg_return_true: \else: \prg_return_false: \fi: } |
| 1721 |
% \end{macrocode} |
| 1722 |
% \end{macro} |
| 1723 |
% |
| 1724 |
% \begin{macro}[pTF]{\mode_if_horizontal:} |
| 1725 |
% \UnitTested |
| 1726 |
% For testing horizontal mode. |
| 1727 |
% \begin{macrocode} |
| 1728 |
\prg_new_conditional:Npnn \mode_if_horizontal: { p , T , F , TF } |
| 1729 |
{ \if_mode_horizontal: \prg_return_true: \else: \prg_return_false: \fi: } |
| 1730 |
% \end{macrocode} |
| 1731 |
% \end{macro} |
| 1732 |
% |
| 1733 |
% \begin{macro}[pTF]{\mode_if_inner:} |
| 1734 |
% \UnitTested |
| 1735 |
% For testing inner mode. |
| 1736 |
% \begin{macrocode} |
| 1737 |
\prg_new_conditional:Npnn \mode_if_inner: { p , T , F , TF } |
| 1738 |
{ \if_mode_inner: \prg_return_true: \else: \prg_return_false: \fi: } |
| 1739 |
% \end{macrocode} |
| 1740 |
% \end{macro} |
| 1741 |
% |
| 1742 |
% \begin{macro}[pTF]{\mode_if_math:} |
| 1743 |
% \UnitTested |
| 1744 |
% For testing math mode. At the beginning of an alignment cell, |
| 1745 |
% this should be used only inside a non-expandable function. |
| 1746 |
% \begin{macrocode} |
| 1747 |
\prg_new_conditional:Npnn \mode_if_math: { p , T , F , TF } |
| 1748 |
{ \if_mode_math: \prg_return_true: \else: \prg_return_false: \fi: } |
| 1749 |
% \end{macrocode} |
| 1750 |
% \end{macro} |
| 1751 |
% |
| 1752 |
% \subsection{Internal programming functions} |
| 1753 |
% |
| 1754 |
% \begin{macro}{\group_align_safe_begin:, \group_align_safe_end:} |
| 1755 |
% \TeX{}'s alignment structures present many problems. As Knuth says |
| 1756 |
% himself in \emph{\TeX : The Program}: \enquote{It's sort of a miracle |
| 1757 |
% whenever \tn{halign} or \tn{valign} work, [\ldots]} One problem relates |
| 1758 |
% to commands that internally issue a \tn{cr} but also peek ahead for |
| 1759 |
% the next character for use in, say, an optional argument. If the |
| 1760 |
% next token happens to be a |&| with category code~4 we get some |
| 1761 |
% sort of weird error message because the underlying |
| 1762 |
% \tn{futurelet} stores the token at the end of the alignment |
| 1763 |
% template. This could be a |&|$_4$ giving a message like |
| 1764 |
% |! Misplaced \cr.| or even worse: it could be the \tn{endtemplate} |
| 1765 |
% token causing even more trouble! To solve this we have to open a |
| 1766 |
% special group so that \TeX{} still thinks it's on safe ground but at |
| 1767 |
% the same time we don't want to introduce any brace group that may |
| 1768 |
% find its way to the output. The following functions help with this |
| 1769 |
% by using behavior documented only in Appendix~D of |
| 1770 |
% \emph{The \TeX{}book}\dots |
| 1771 |
% In short evaluating |`{| and |`}| as numbers will not change the counter |
| 1772 |
% \TeX{} uses to keep track of its state in an alignment, whereas gobbling a |
| 1773 |
% brace using \cs{if_false:} will affect \TeX's state without producing any |
| 1774 |
% real group. |
| 1775 |
% We place the \cs{if_false:} |{| \cs{fi:} part at that place so |
| 1776 |
% that the successive expansions of \cs{group_align_safe_begin/end:} |
| 1777 |
% are always brace balanced. |
| 1778 |
% \begin{macrocode} |
| 1779 |
\group_begin: |
| 1780 |
\tex_catcode:D `\^^@ = 2 \exp_stop_f: |
| 1781 |
\cs_new:Npn \group_align_safe_begin: |
| 1782 |
{ \exp:w \if_false: { \fi: `^^@ \exp_stop_f: } |
| 1783 |
\tex_catcode:D `\^^@ = 1 \exp_stop_f: |
| 1784 |
\cs_new:Npn \group_align_safe_end: |
| 1785 |
{ \exp:w `^^@ \if_false: } \fi: \exp_stop_f: } |
| 1786 |
\group_end: |
| 1787 |
% \end{macrocode} |
| 1788 |
% \end{macro} |
| 1789 |
% |
| 1790 |
% \begin{variable}{\g__kernel_prg_map_int} |
| 1791 |
% A nesting counter for mapping. |
| 1792 |
% \begin{macrocode} |
| 1793 |
\int_new:N \g__kernel_prg_map_int |
| 1794 |
% \end{macrocode} |
| 1795 |
% \end{variable} |
| 1796 |
% |
| 1797 |
% \begin{macro}{\prg_break_point:Nn} |
| 1798 |
% \begin{macro}{\prg_map_break:Nn} |
| 1799 |
% These are defined in \pkg{l3basics}, as they are needed |
| 1800 |
% \enquote{early}. This is just a reminder that is the case! |
| 1801 |
% \end{macro} |
| 1802 |
% \end{macro} |
| 1803 |
% |
| 1804 |
% \begin{macro}{\prg_break_point:} |
| 1805 |
% \begin{macro}{\prg_break:, \prg_break:n} |
| 1806 |
% Also done in \pkg{l3basics}. |
| 1807 |
% \end{macro} |
| 1808 |
% \end{macro} |
| 1809 |
% |
| 1810 |
% \begin{macrocode} |
| 1811 |
%</code> |
| 1812 |
% \end{macrocode} |
| 1813 |
% |
| 1814 |
% \end{implementation} |
| 1815 |
% |
| 1816 |
% \PrintIndex |