Debian Codegraph @

texmf-dist/source/latex/knowledge/knowledge-utils.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2016 by Thomas Colcombet
4 % -----------------------------------
5 %
6 % This file may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either version 1.2 % of this license or (at your option) any later version.
8 % The latest version of this license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.2 or later is part of all distributions of LaTeX
13 % version 1999/12/01 or later.
14 %
15 % \fi
16 %
17 % \iffalse
18 %
19 %<*driver>
20 \documentclass{ltxdoc}
21 \usepackage[utf8]{inputenc}
22 \usepackage[T1]{fontenc}
23 \usepackage{paralist}
24 \usepackage{hyperref}
25 \usepackage{expl3}
26 \EnableCrossrefs
27 \CodelineIndex
28 \RecordChanges
29
30
31 \newcommand\kl[2][]{#2}
32
33 \begin{document}
34 \DocInput{knowledge-utils.dtx}
35 \end{document}
36 %</driver>
37 % \fi
38 %
39 % \CheckSum{0}
40 %
41 % \CharacterTable
42 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
43 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
44 % Digits \0\1\2\3\4\5\6\7\8\9
45 % Exclamation \! Double quote \" Hash (number) \#
46 % Dollar \$ Percent \% Ampersand \&
47 % Acute accent \' Left paren \( Right paren \)
48 % Asterisk \* Plus \+ Comma \,
49 % Minus \- Point \. Solidus \/
50 % Colon \: Semicolon \; Less than \<
51 % Equals \= Greater than \> Question mark \?
52 % Commercial at \@ Left bracket \[ Backslash \\
53 % Right bracket \] Circumflex \^ Underscore \_
54 % Grave accent \` Left brace \{ Vertical bar \|
55 % Right brace \} Tilde \~}
56 %
57 %
58 % \changes{v2.0}{2016/02/26}{Initial version} %
59 % \GetFileInfo{knowledge-utils.sty} %
60 % \DoNotIndex{}
61 %
62 % \title{The \textsf{knowledge-utils} package}
63 % \author{Thomas Colcombet \\ \texttt{[email protected]}}
64 % \maketitle
65 %
66 % \begin{abstract}
67 % This code is incorporated in the knowledge.sty file. It essentially contains a certain number of useful functions.
68 %
69 % This includes dealing with the kaux file (a single file which is output during the compilation, and then postprocessed for extracting information).
70 % In contains some primitives for creating fields in tuples, macros for overloading other macros, and code for recreating an argument list (reversing par of the xparse syntax).
71 %
72 % This file needs to be cleaned and reorganize.
73 % \end{abstract}
74 %
75 %\tableofcontents
76 %
77 %
78 % \section{Introduction}
79 %
80 % The main file is knowledge.dtx.
81 %
82 %
83 %
84 %
85 %
86 % \section{Implementation}
87 %
88 % \def\cs{\ExplSyntaxOn\csARG}
89 % \def\csARG#1{\ExplSyntaxOff\texttt{\detokenize{#1}}}
90 %
91 %\let\ifcode\iffalse
92 %
93 % \subsection{Code preparation}
94 % \begin{macrocode}
95 \usepackage{xparse}
96 \usepackage{currfile}
97 % \end{macrocode}
98 % The interface of expl3 since Mars 2015 has changed and a small patch is required:
99 % \begin{macrocode}
100 \tl_if_exist:NTF
101 \c_sys_jobname_str
102 {}
103 {\tl_set_eq:Nc\c_sys_jobname_str{c_job_name_tl}}
104 % \end{macrocode}
105 % The interface of expl3 since Mars 2018 has changed and a small patch is required:
106 % \begin{macrocode}
107 %\tl_if_exist:NTF
108 % \ior_str_get:NN
109 % {}
110 % {\tl_set_eq:NN\ior_str_get:NN\ior_get_str:NN}
111 % \end{macrocode}
112 % Some useful adding (newset).
113 % \begin{macrocode}
114 \cs_new:Nn\tl_new:Nn
115 {\tl_new:N#1
116 \tl_set:Nn#1{#2}}
117 \cs_new:Nn\tl_gnew:Nn
118 {\tl_new:N#1
119 \tl_gset:Nn#1{#2}}
120 \cs_new:Nn\tl_new_eq:NN
121 {\tl_new:N#1
122 \tl_set_eq:NN#1#2}
123 \cs_new:Nn\bool_new_true:N
124 {\bool_new:N#1
125 \bool_set_true:N#1}
126 \cs_new:Nn\bool_new_false:N
127 {\bool_new:N#1
128 \bool_set_false:N#1}
129 \cs_new:Nn\bool_set_text:Nn
130 {\use:c{bool_set_#2:N}#1}
131 \cs_generate_variant:Nn\tl_new:Nn{Nx,cn,cx}
132 \cs_generate_variant:Nn\tl_new_eq:NN{Nc,cN,cc}
133 \cs_generate_variant:Nn\bool_new_true:N{c}
134 \cs_generate_variant:Nn\bool_new_false:N{c}
135 \cs_generate_variant:Nn\bool_set_text:Nn{cn}
136 % \end{macrocode}
137 % Some new \kl{latex3} commands:
138 % \cs{cs_new_with_variants:Nnn},
139 % \cs{if_mode_preamble:TF},
140 % \cs{kl_hide_begin:} and \cs{kl_hide_begin:}
141 % (that stand for \cs{@bsphack} and \cs{@esphack} of \kl{latex2e}).
142 %
143 % Creating new macros with their variants at the same time...
144 % \begin{macrocode}
145 \cs_new_protected:Nn\cs_new_protected_with_variants:Nnn
146 {\cs_new_protected:Nn#1{#3}
147 \cs_generate_variant:Nn#1{#2}}
148 \cs_generate_variant:Nn\cs_new_protected_with_variants:Nnn{cnn}
149
150 \cs_new_protected:Nn\cs_new_with_variants:Nnn
151 {\cs_new:Nn#1{#3}
152 \cs_generate_variant:Nn#1{#2}}
153 \cs_generate_variant:Nn\cs_new_with_variants:Nnn{cnn}
154
155 \cs_generate_variant:Nn\tl_show:n{x}
156 % \end{macrocode}
157 %
158 % Now comes a command for simultaneously testing existence and non emptiness.
159 % \begin{macrocode}
160 \cs_new:Npn\tl_if_exist_ne:NTF#1
161 {\tl_if_exist:NTF#1{\tl_if_empty:NTF#1\use_ii:nn\use_i:nn}\use_ii:nn}
162 \cs_new:Npn\tl_if_exist_ne:NT#1
163 {\tl_if_exist:NTF#1{\tl_if_empty:NTF#1\use_none:n\use:n}\use_none:n}
164 \cs_generate_variant:Nn\tl_if_empty:nTF{xTF}
165 % \end{macrocode}
166 %
167 %
168 % Immediate execution of code with parameters without explicit abstraction.
169 %\begin{description}
170 %\item \cs{kl_apply_inline:nnw} takes an argument specifier, a code with
171 % arguments ($\sharp$1,\dots), and sufficiently many arguments, and executes
172 % the code with arguments substituted (this is not expandable).
173 %\item \cs{kl_apply_inline_variant:nnnw} does the same thing, and furthermore use a variant
174 % of the code in which the arguments are executed as specified in the second argument.
175 % For instance,
176 %\begin{verbatim}
177 %\kl_apply_inline_variant:nnnw{nn}{VV}
178 % {\cs_new:Nn\wrap:n{#1##1#2}}
179 % \leftdelimiter\rightdelimiter
180 %\end{verbatim}
181 %Define a new function \cs{wrap:n} that takes a parameter, and encloses it with the \textbf{content} of
182 %\cs{leftdelimiter} and \cs{rightdelimiter}.
183 %\end{description}
184 %\ifcode
185 % \begin{macrocode}
186 \cs_new_protected:Npn\kl_apply_inline:nnw#1#2
187 {\group_begin:
188 \cs_set:cn{g_tmpa_cs:#1}
189 {\group_end:#2}
190 \use:c{g_tmpa_cs:#1}}
191
192 \cs_new_protected:Npn\kl_apply_inline_variant:nnnw#1#2#3
193 {\group_begin:
194 \cs_set:cn{g_tmpa_cs:#1}
195 {\group_end:#3}
196 \exp_args:Nc
197 \cs_generate_variant:Nn{g_tmpa_cs:#1}{#2}
198 \use:c{g_tmpa_cs:#2}}
199 % \end{macrocode}
200 %\fi
201 %
202 % \begin{macrocode}
203 \cs_new:Nn\mode_if_preamble:TF{#1}
204 \AtBeginDocument
205 {\cs_gset:Nn\mode_if_preamble:TF{#2}}
206 % \end{macrocode}
207 % \begin{macrocode}
208 \makeatletter
209 \int_new:N\kl_sphack_int
210 \cs_set_eq:NN\kl_stored_@bsphack\@bsphack
211 \cs_set_eq:NN\kl_stored_@esphack\@esphack
212 \cs_set:Nn\kl_hide_begin:
213 {\@bsphack
214 \cs_set_eq:NN\@bsphack\relax
215 \cs_set_eq:NN\@esphack\relax
216 \int_incr:N\kl_sphack_int}
217 \cs_set:Nn\kl_hide_end:
218 {\int_decr:N\kl_sphack_int
219 \int_compare:nNnTF{\kl_sphack_int}=0
220 {\cs_set_eq:NN\@bsphack\kl_stored_@bsphack
221 \cs_set_eq:NN\@esphack\kl_stored_@esphack}{}
222 \@esphack}
223 \makeatother
224 % \end{macrocode}
225 % \begin{macrocode}
226 \cs_new:Nn\if_true:TF{#1}
227 \cs_new:Nn\if_false:TF{#2}
228 %\cs_generate_variant:Nn\int_compare:nNnTF{cNnTF}
229 \cs_generate_variant:Nn\use:nn{nx}
230 % \end{macrocode}
231 % The command |\cs_apply_inline:n<n's>| takes a first argument
232 % with as many parameters as in |<n's>|, and apply it to them.
233 % \ifcode
234 % \begin{macrocode}
235 \cs_new:Npn\cs_apply_inline:nn#1
236 {\cs_set:Npn\g_tmpa_cs:##1{#1}
237 \g_tmpa_cs:}
238 \cs_new:Npn\cs_apply_inline:nnn#1
239 {\cs_set:Npn\g_tmpa_cs:##1##2{#1}
240 \g_tmpa_cs:}
241 \cs_new:Npn\cs_apply_inline:nnnn#1
242 {\cs_set:Npn\g_tmpa_cs:##1##2##3{#1}
243 \g_tmpa_cs:}
244 \cs_new:Npn\cs_apply_inline:nnnnn#1
245 {\cs_set:Npn\g_tmpa_cs:##1##2##3##4{#1}
246 \g_tmpa_cs:}
247 \cs_new:Npn\cs_apply_inline:nnnnnn#1
248 {\cs_set:Npn\g_tmpa_cs:##1##2##3##4##5{#1}
249 \g_tmpa_cs:}
250 \cs_new:Npn\cs_apply_inline:nnnnnnn#1
251 {\cs_set:Npn\g_tmpa_cs:##1##2##3##4##5##6{#1}
252 \g_tmpa_cs:}
253 \cs_new:Npn\cs_apply_inline:nnnnnnnn#1
254 {\cs_set:Npn\g_tmpa_cs:##1##2##3##4##5##6##7{#1}
255 \g_tmpa_cs:}
256 \cs_new:Npn\cs_apply_inline:nnnnnnnnn#1
257 {\cs_set:Npn\g_tmpa_cs:##1##2##3##4##5##6##7##8{#1}
258 \g_tmpa_cs:}
259 \cs_new:Npn\cs_apply_inline:nnnnnnnnnn#1
260 {\cs_set:Npn\g_tmpa_cs:##1##2##3##4##5##6##7##8##9{#1}
261 \g_tmpa_cs:}
262 % \end{macrocode}
263 %
264 % \begin{macrocode}
265 %\cs_generate_variant:Nn\cs_apply_inline:nn{nc}
266 %\cs_generate_variant:Nn\cs_apply_inline:nnn{ncc}
267 %\cs_generate_variant:Nn\cs_apply_inline:nnnn{nccc}
268 %\cs_generate_variant:Nn\cs_apply_inline:nnnnn{ncccc}
269 %\cs_generate_variant:Nn\cs_apply_inline:nnnnnn{nccccc}
270 %\cs_generate_variant:Nn\cs_apply_inline:nnnnnnn{ncccccc}
271 %\cs_generate_variant:Nn\cs_apply_inline:nnnnnnnn{nccccccc}
272 %\cs_generate_variant:Nn\cs_apply_inline:nnnnnnnnn{ncccccccc}
273 %\cs_generate_variant:Nn\cs_apply_inline:nnnnnnnnnn{nccccccccc}
274 % \end{macrocode}
275 %\fi
276 %
277 %
278 % \subsection{KAux file}
279 %
280 % We introduce here the KAux file.
281 % The principle of the KAux file is to play the role of an aux file, but at the same time it
282 % has some facilities for using it with several subpackages.
283 % Essentially, the code in the KAUX file is executed, and is supposed to use only functions defined by the
284 % |\NewKAuxCommand| (using \texttt{xparse} syntax). Such functions do nothing by default,
285 % unless explicitly activated when rereading the file.
286 % At the beginning of the KAUX file, each command that may occur is declared,
287 % the package that produced it is named, its arguments are provided, and its use
288 % is succinctly described.
289 %
290 % By default, the location is stored in the KAux file, unless the writing command has a star.
291 % When the KAux file is processed, the variables \cs{\kauxCurrentFile}
292 % and \cs{\kauxCurrentLine} contain the filename and the line at the moment the information was processed.
293 % The location can be explicitely written using \cs{\KAuwWriteLocation}.
294 % Thus, by the user can decide when to write the line (at the begining of a command may be better when it has several lines).
295 %
296 % The file is open for writing when \cs{KAuxActivate} is called the
297 % first time. So, commands may ensure that the file is activated using
298 % \cs{KAuxActivate}, but this should be used after the configuration
299 % is finished. So the rule is: when a package is loaded, one should never use
300 % \cs{KAuxActivate}.
301 % \begin{macrocode}
302 \AtBeginDocument{\KAuxActivate}
303
304 \NewDocumentCommand\KAuxActivate{}{
305 \cs_gset_eq:NN\KAuxActivate\relax
306 \KAuxOpen{}}
307 % \end{macrocode}
308 % \begin{macrocode}
309 \AtEndDocument{\KAuxClose}
310 % \end{macrocode}
311 % \begin{macrocode}
312 \iow_new:N\knowledge_kaux_iow
313
314 \NewDocumentCommand\KAuxOpen{}
315 {\kaux_pretreatment_tl
316 \tl_gclear:N\kaux_pretreatment_tl
317 \iow_open:Nn
318 \knowledge_kaux_iow
319 {\c_sys_jobname_str.kaux}
320 \KAuxWrite*{}
321 \kaux_inittreatment_tl
322 \tl_gclear:N\kaux_inittreatment_tl
323 \KAuxWrite*{}}
324
325 \NewDocumentCommand\KAuxClose{}
326 {\KAuxWrite*{\KAuxEOF {}}
327 \iow_close:N\knowledge_kaux_iow
328 \bool_gset_true:N\kaux_can_be_used_bool
329 \kaux_posttreatment_tl
330 }
331 % \end{macrocode}
332 % Testing if the KAux file is complete.
333 % |\kaux_if_exist_containline:nnTF| tests if a line of a file is equal to the argument.
334 % This has to be used with care: the searched line is transformed into a str, thus introducing
335 % potential extra spaces after the control sequences.
336 % Also, when lines are read from the files, the trailing spaces are lost.
337 % Anyway, |IfKAuxReadyTF| tests for the presence of |\KAuxEOF {}| in a proper way.
338 % \begin{macrocode}
339 \cs_new:Npn\kaux_if_exist_containline:nnTF#1#2{
340 \file_if_exist:nTF{#1}{
341 \ior_open:Nn\klg_tmpa_ior{#1}
342 \bool_gset_false:N\g_tmpa_bool
343 \bool_gset_false:N\g_tmpb_bool
344 \str_gset:Nn\g_tmpb_str{#2}
345 \bool_do_until:Nn\g_tmpa_bool{
346 \ior_if_eof:NTF\klg_tmpa_ior
347 {\bool_set_true:N\g_tmpa_bool}
348 {
349 \ior_str_get:NN\klg_tmpa_ior\g_tmpa_str
350 %\iow_term:x{(\exp_not:V\g_tmpa_str)=?(\exp_not:V\g_tmpb_str)}
351 \str_if_eq:NNTF\g_tmpa_str\g_tmpb_str
352 {\bool_gset_true:N\g_tmpa_bool
353 \bool_gset_true:N\g_tmpb_bool}
354 {}
355 }}
356 \ior_close:N\klg_tmpa_ior
357 %\bool_show:N\g_tmpb_bool
358 \bool_if:NTF\g_tmpb_bool
359 }
360 {\use_ii:nn}
361 }
362
363 \NewDocumentCommand\IfKAuxReadyTF{}
364 {\exp_args:Nx\kaux_if_exist_containline:nnTF{\c_sys_jobname_str.kaux}{\KAuxEOF {}}}
365 % \end{macrocode}
366 %
367 %
368 %
369 % Using the command |\KAuxBefore|, |\KAuxInit| and |\KAuxAfter|, one can add
370 % code to be executed before the opening, at the beginning and after the writing of the KAUX file.
371 % \begin{macrocode}
372 \NewDocumentCommand\KAuxBefore{m}
373 {\tl_gput_right:Nn\kaux_pretreatment_tl{#1}}
374 \NewDocumentCommand\KAuxAfter{m}
375 {\tl_gput_right:Nn\kaux_posttreatment_tl{#1}}
376 \NewDocumentCommand\KAuxInit{m}
377 {\tl_gput_right:Nn\kaux_inittreatment_tl{#1}}
378 % \end{macrocode}
379 % \begin{macrocode}
380 \tl_new:N\kaux_pretreatment_tl
381 \tl_gset:Nn\kaux_pretreatment_tl{}
382 \tl_new:N\kaux_posttreatment_tl
383 \tl_gset:Nn\kaux_posttreatment_tl{}
384 \tl_new:N\kaux_inittreatment_tl
385 \tl_gset:Nn\kaux_inittreatment_tl{}
386 % \end{macrocode}
387 %
388 %
389 % \subsubsection{KAux phases and commands}
390 %
391 % The idea is that the KAux file will be read several times. Once at the begining, before rewriting it,
392 % and then more times afterward (maybe several times). Each time it is read corresponds to a `phase'.
393 % By default, when a KAuxCommand is defined, then it does nothing (and absorbs its only parameter).
394 % Then, when a phase is activated (and several phases can be activated simultaneously), each command
395 % executes the corresponding code (declares using |\DeclareKAuxPhaseCommand|).
396 %
397 %
398 % The list of KAux commands are kept in |\kaux_command_list_tl|.
399 % \begin{macrocode}
400 \tl_new:N\kaux_command_list_tl
401 % \end{macrocode}
402 %
403 % When one wants to use some command in the KAux file, one has to first declare
404 % it using |\NewKAuxCommand\commandname{arguments}{description}|
405 % in which |\commandname| is the token of the command, |arguments|
406 % is the description of arguments following the \texttt{xparse} package syntax,
407 % and |description| is an informal description of what the command is intended to do.
408 % Note that for efficiency reason, in the KAux file itself,
409 % \emph{arguments have to be surrounded by curly brackets},
410 % but when used, these will parsed using the argument description as defined by \texttt{xparse}.
411 %
412 % Technically, |\NewKAuxCommand| declares the command in the KAux file using |\KAuxCommand|,
413 % then the arguments description is stored in |\kaux_commandname-args_tl|, and |\commandname|
414 % itself is created, as a code that executes, followed by the arguments, all the tokens in |\kaux_commandname-active_tl|. Also, the command is appended to |\kaux_command_list_tl|.
415 % \begin{macrocode}
416 \NewDocumentCommand\NewKAuxCommand{ m m m }
417 { \ProvideDocumentCommand#1{#2}{}
418 \KAuxInit
419 {\KAuxWrite*{\KAuxCommand#1{#2}{#3}}}
420 \tl_new:c{kaux_\cs_to_str:N#1-args_tl}
421 \tl_gset:cn{kaux_\cs_to_str:N#1-args_tl}{#2}
422 \tl_gput_right:Nn\kaux_command_list_tl{#1}
423 }
424 % \end{macrocode}
425 % As an example, |\KAuxEOF| describes is used at the end of the Kaux file.
426 % \begin{macrocode}
427 \NewKAuxCommand\KAuxEOF{m}
428 {Does~nothing,~useless~empty~argument
429 ,~and~is~searched~for~knowing~if~the~kaux~file~was~written~till~the~end.}
430 % \end{macrocode}
431 % \begin{macrocode}
432 \NewDocumentCommand\KAuxCommand{mmm}
433 { \cs_if_exist:NTF #1
434 {}
435 {\ProvideDocumentCommand#1{#2}}
436 }
437 % \end{macrocode}
438 %
439 % |\ActivateKAuxPhase{phase1,phase2,...}| sets the KAux commands to use
440 % for all KAux commands the
441 % code corresponding to |phase1|, |phase2|, \dots{} This is obtained by
442 % defining for each command |\commandname| the tl variable |\kaux_commandname-active_tl|
443 % accordingly.
444 % \begin{macrocode}
445 \NewDocumentCommand\ActivateKAuxPhase{m}{
446 \tl_map_inline:Nn\kaux_command_list_tl
447 {\kaux_activate_command_phase:Nn##1{#1}}
448 }
449 % \end{macrocode}
450 % \begin{macrocode}
451 \cs_new:Nn\kaux_activate_command_phase:Nn
452 { \tl_clear_new:c{kaux_\cs_to_str:N#1-active_tl}
453 %
454 \kaux_code_set:Nn\tmpa_code{}
455 \clist_map_inline:nn{#2}
456 {\cs_if_exist:cTF{kaux_\cs_to_str:N#1-##1:nnnnnnnnn}
457 {\exp_args:NNc
458 \kaux_code_put_right:NN
459 \tmpa_code{kaux_\cs_to_str:N#1-##1:nnnnnnnnn}}
460 {}}
461 \kaux_code_wrap:N\tmpa_code
462 \exp_args:NNx\kaux_code_put_left:Nn\tmpa_code
463 {\DeclareDocumentCommand\exp_not:N#1
464 {\exp_not:v{kaux_\cs_to_str:N#1-args_tl}}}
465 \kaux_code_exec:N\tmpa_code
466 }
467 % \end{macrocode}
468 %
469 % Here comes a bunch of commands for manipulating code.
470 % The idea is to replicate some of the commands for the type tl (in a much less efficient way, it is true),
471 % but make it compatible with arguments |#1|, |#2|. There may be much more efficient ways to do that,
472 % but the expansion system makes it not obvious for me.
473 % \begin{macrocode}
474 \cs_new:Nn\kaux_code_set_eq:NN
475 {\cs_set_eq:NN#1#2}
476 \cs_new:Nn\kaux_code_set:Nn
477 {\cs_set:Npn#1##1##2##3##4##5##6##7##8##9{#2}}
478 \cs_new:Nn\kaux_code_put_right:NN
479 {\exp_args:NNo\kaux_code_put_right:Nn#1
480 {#2{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}}}
481 \cs_new:Nn\kaux_code_put_right:Nn
482 {\exp_args:NNo\cs_set:Nn
483 \tmp:nnnnnnnnn
484 {#1{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}#2}
485 \cs_set_eq:NN#1\tmp:nnnnnnnnn}
486 \cs_new:Nn\kaux_code_put_left:Nn
487 {\kaux_code_set:Nn\g_tmpc_code{#2}
488 \kaux_code_put_right:NN\g_tmpc_code#1
489 \kaux_code_set_eq:NN#1\g_tmpc_code}
490 \cs_new:Nn\kaux_code_wrap:N
491 {\expandafter\cs_set:Nn
492 \expandafter\tmp:nnnnnnnnn
493 \expandafter{
494 \expandafter{#1{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}}}
495 \cs_set_eq:NN#1\tmp:nnnnnnnnn}
496 \cs_new:Nn\kaux_code_exec:N
497 {#1{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}}
498 % \end{macrocode}
499 % Declaring a code for a KAuxCommand associated to a specific phase
500 % is done using |\DeclareKAuxPhaseCommand\commandname{phase}{code}|.
501 % The code is stored in a `cs' with nine parameters.
502 % \begin{macrocode}
503 \NewDocumentCommand\DeclareKAuxPhaseCommand{mmm}
504 { \tl_if_exist:cTF{kaux_\cs_to_str:N#1-args_tl}{}
505 {\ERROR_KAuxCommand_UNDEFINED}
506 \cs_gset:cn{kaux_\cs_to_str:N#1-#2:nnnnnnnnn}{#3}}
507 % \end{macrocode}
508 %
509 % \begin{macrocode}
510 \NewDocumentCommand\KAuxWriteLocation{}
511 {\KAuxActivate
512 \group_begin:
513 \tl_set:Nx\l_tmpa_tl{{\currfiledir\currfilename}{\the\inputlineno}}
514 \tl_if_eq:NNTF\l_tmpa_tl\kaux_location_tl
515 {}
516 {\tl_gset_eq:NN\kaux_location_tl\l_tmpa_tl
517 \iow_now:Nx\knowledge_kaux_iow{}
518 \iow_now:Nx\knowledge_kaux_iow
519 {\KAuxFileAt\kaux_location_tl}}
520 \group_end:}
521
522 \tl_new:N\kaux_location_tl
523
524 \NewDocumentCommand\KAuxWriteX{sm}
525 {\KAuxActivate
526 \IfBooleanTF{#1}{}{\KAuxWriteLocation}
527 \exp_args:NNx\iow_now:Nn\knowledge_kaux_iow{#2}}
528
529 \NewDocumentCommand\KAuxWrite{sm}
530 {\KAuxActivate
531 \IfBooleanTF{#1}{}{\KAuxWriteLocation}
532 \iow_now:Nn\knowledge_kaux_iow{#2}}
533
534 \NewDocumentCommand\KAuxFileAt{mm}
535 {\tl_set:Nn\kauxCurrentFile{#1}
536 \tl_set:Nn\kauxCurrentLine{#2}}
537
538 \cs_new:Nn\kaux_current_location:{\kauxCurrentFile :\kauxCurrentLine :}
539 \tl_new:N\kauxCurrentFile
540 \tl_new:N\kauxCurrentLine
541 % \end{macrocode}
542 %
543 % The kaux file is allowed to be used at the beginning if it exists (of course),
544 % and if the aux file also exists. This is for tools that delete the aux file for cleaning the directory
545 % to virtually also delete the kaux file.
546 % \begin{macrocode}
547 \bool_new:N\kaux_can_be_used_bool
548 \bool_gset_false:N\kaux_can_be_used_bool
549
550 \file_if_exist:nT{\c_sys_jobname_str.aux}
551 {\file_if_exist:nT{\c_sys_jobname_str.kaux}
552 {\bool_gset_true:N\kaux_can_be_used_bool}}
553
554 \NewDocumentCommand\KAuxProcess{m}
555 { \bool_if:NT\kaux_can_be_used_bool
556 {\ActivateKAuxPhase{#1}
557 \input{\c_sys_jobname_str.kaux}}}
558 % \end{macrocode}
559 %
560 %
561 %
562 %
563 %
564 %
565 %
566 %
567 % \subsection{References}
568 %
569 % This is an old part of the code which is difficult to read.
570 % It should be cleaned. But this is a lot of work.
571 %
572 % When using \cs{\cell_new_type:nnn}, a new name space is created,
573 % in which can be stored information. The first parameter is the name space. The second
574 % is the description (expl3 style) of the parameters defining an entry, and the third is the
575 % string of character (that can use the arguments defined) used to address this space.
576 % For instance \verb|\cell_new_type:nnn{carots}{nn}{K@ROT:{#1}{#2}}|
577 % define a cell type named `carot' that is indexed by two parameters.
578 % In particular, it defines the macro \cs{\carot:Nnn} that takes a token, and parameters, that executes the
579 % token with as first parameter the token identifying the location of the carot.
580 % For instance, \verb|\carot_at:Nnn\show{A}{B}| will display the content of the carot defined by the parameters A and B.
581 % \begin{macrocode}
582 \cs_new:Nn\cell_new_type:nnn
583 {
584 \cs_new:cn{#1_id:#2}{#3}
585 \cs_new:cn{#1_id_b:#2}{{#3}}
586 \cs_new:cpx{#1_at:N#2}##1
587 {\exp_not:n{\exp_after:wN\exp_args:Nc\exp_after:wN}
588 ##1 \exp_not:c{#1_id_b:#2}}
589 }
590 % \end{macrocode}
591 % \begin{macrocode}
592 \cs_new:Nn\cell_specialize:nnn
593 {\cs_new:cpx{#1_#2_id:#3}
594 {\exp_not:c{#1_id:n#3}{#2}}
595 \cs_new:cpx{#1_#2_at:N#3}##1
596 {\exp_not:c{#1_at:Nn#3}##1{#2}}
597 }
598
599 \cs_new:Nn\cell_make_tl:nn
600 {\cs_set:Nn\l_tmpa_cs:nn
601 {\cs_new:cpx{#1_##1:#2##2}
602 {\exp_not:c{#1_at:N#2}\exp_not:c{tl_##1:N##2}}}
603 \l_tmpa_cs:nn{set}{n}
604 \l_tmpa_cs:nn{gset}{n}
605 \l_tmpa_cs:nn{set}{x}
606 \l_tmpa_cs:nn{gset}{x}
607 \l_tmpa_cs:nn{gput_right}{n}
608 \l_tmpa_cs:nn{gput_left}{n}
609 \l_tmpa_cs:nn{put_right}{n}
610 \l_tmpa_cs:nn{put_left}{n}
611 \l_tmpa_cs:nn{show}{}
612 \l_tmpa_cs:nn{use}{}
613 \l_tmpa_cs:nn{if_exist}{TF}
614 \l_tmpa_cs:nn{if_empty}{TF}
615 }
616
617 \cs_new:Nn\cell_make_int:nn
618 {
619 \cs_set:Nn\l_tmpa_cs:nn
620 {\cs_new:cpx{#1_##1:#2##2}
621 {\exp_not:c{#1_at:N#2}\exp_not:c{int_##1:N##2}}}
622 \l_tmpa_cs:nn{new}{}
623 \l_tmpa_cs:nn{show}{}
624 \l_tmpa_cs:nn{set}{n}
625 \l_tmpa_cs:nn{gset}{n}
626 \l_tmpa_cs:nn{incr}{}
627 \l_tmpa_cs:nn{gincr}{}
628 \l_tmpa_cs:nn{decr}{}
629 \l_tmpa_cs:nn{gdecr}{}
630 \l_tmpa_cs:nn{use}{}
631 \l_tmpa_cs:nn{to_arabic}{}
632 }
633
634
635 \cs_new:Nn\cell_make_bool:nn
636 {
637 \cs_set:Nn\l_tmpa_cs:nn
638 {\cs_new:cpx{#1_##1:#2##2}
639 {\exp_not:c{#1_at:N#2}\exp_not:c{bool_##1:N##2}}}
640 \l_tmpa_cs:nn{set_true}{}
641 \l_tmpa_cs:nn{set_false}{}
642 \l_tmpa_cs:nn{gset_true}{}
643 \l_tmpa_cs:nn{gset_false}{}
644 \l_tmpa_cs:nn{set}{n}
645 \l_tmpa_cs:nn{gset}{n}
646 \l_tmpa_cs:nn{if}{TF}
647 }
648
649 \cs_new:Nn\seq_use:N
650 {\seq_use:Nn#1,}
651
652 \cs_new:Nn\cell_make_seq:nn
653 {
654 \cs_set:Nn\l_tmpa_cs:nn
655 {\cs_new:cpx{#1_##1:#2##2}
656 {\exp_not:c{#1_at:N#2}\exp_not:c{seq_##1:N##2}}}
657 \l_tmpa_cs:nn{new}{}
658 \l_tmpa_cs:nn{set_from_clist}{n}
659 \l_tmpa_cs:nn{gset_from_clist}{n}
660 \l_tmpa_cs:nn{gpush}{n}
661 \l_tmpa_cs:nn{if_exist}{TF}
662 \l_tmpa_cs:nn{if_empty}{TF}
663 \l_tmpa_cs:nn{if_in}{nTF}
664 \l_tmpa_cs:nn{show}{}
665 \l_tmpa_cs:nn{use}{n}
666 \l_tmpa_cs:nn{use}{}
667 \l_tmpa_cs:nn{map_inline}{n}
668 }
669
670 \cs_new:Nn\cell_make_tuple:nn
671 {
672 \cs_set:Nn\l_tmpa_cs:nn
673 {\cs_new:cpx{#1_##1:#2##2}
674 {\exp_not:c{#1_at:N#2}\exp_not:c{tuple_##1:N##2}}}
675 }
676
677 \cs_new:Nn\cell_specialize_int:nnn
678 {
679 \cell_specialize:nnn{#1}{#2}{#3}
680 \cell_make_int:nn{#1_#2}{#3}
681 }
682
683 \cs_new:Nn\cell_specialize_tl:nnn
684 {
685 \cell_specialize:nnn{#1}{#2}{#3}
686 \cell_make_tl:nn{#1_#2}{#3}
687 }
688
689 \cs_new:Nn\cell_specialize_bool:nnn
690 {
691 \cell_specialize:nnn{#1}{#2}{#3}
692 \cell_make_bool:nn{#1_#2}{#3}
693 }
694
695 \cs_new:Nn\cell_specialize_seq:nnn
696 {
697 \cell_specialize:nnn{#1}{#2}{#3}
698 \cell_make_seq:nn{#1_#2}{#3}
699 }
700 % \end{macrocode}
701 %
702 % \subsection{Tuples}
703 %
704 % ensure
705 % \cs{\tuple_empty_tl}, \cs{\tuple_new:N},
706 % \cs{\tuple_gclear:N}, \cs{\tuple_if_exist:NTF},
707 % \cs{\tuple_if_no_index:NNTF},
708 % \cs{\tuple_gset:NNn}, \cs{\tuple_gset:NNN},
709 % \cs{\tuple_gdel:NN}, \cs{\tuple_apply:NNNn},
710 % \cs{\tuple_apply_inline:NNnn}, \cs{\tuple_get_default_tl:NNNn},
711 % \cs{\tuple_get_int:NNN}, \cs{\tuple_incr:NN},
712 % \cs{\tuple_decr:NN}, \cs{\tuple_gset_true:NN},
713 % \cs{\bool_set_false:NN}
714 %
715 % \cs\tuple_new_expandable_index:Nnn takes a token acting as an index, a name,
716 % and a default value, and creates a command |tuple_exp_|name|:N| which given
717 % a tuple, retrieve in an expandable way the index, and if it does not exists outputs the default value.
718 %
719 %
720 %
721 %
722 %
723 %
724 %
725 %
726 %
727 %
728 % \begin{macrocode}
729 \cs_new:Nn\tuple_new:n
730 {
731 \cs_new:cx{#1_new:N}
732 {\exp_not:N\tl_new:N##1
733 \exp_not:N\tl_gset:Nn##1{\exp_not:c{#1:}}}
734 \cs_new:cx{#1_gclear:N}
735 {\exp_not:N\tl_gset:Nn##1{\exp_not:c{#1:}}}
736 \cs_new:cx{#1_ensure:N}
737 {\exp_not:N\tl_if_exist:NTF##1{}
738 {\exp_not:N\tl_new:N##1
739 \exp_not:N\tl_gset:Nn##1{\exp_not:c{#1:}}}}
740 \cs_new:cn{#1:}{}
741 %
742 \cs_set:Nn\l_tmpa_cs:nn
743 { \cs_new:cpx{#1_##1:##2}
744 {\exp_not:c{tuple_##1:n##2}{\exp_not:n{#1}}}}
745 \l_tmpa_cs:nn{new_tl_index}{Nnn}
746 \l_tmpa_cs:nn{new_tl_index}{nn}
747 \l_tmpa_cs:nn{new_tl_index}{n}
748 \l_tmpa_cs:nn{new_int_index}{Nnn}
749 \l_tmpa_cs:nn{new_int_index}{nn}
750 \l_tmpa_cs:nn{new_int_index}{n}
751 \l_tmpa_cs:nn{new_bool_index}{Nn}
752 \l_tmpa_cs:nn{new_bool_index}{n}
753 \l_tmpa_cs:nn{show}{N}
754 \l_tmpa_cs:nn{if_exist}{NTF}
755 }
756 \cs_new:Npn\tuple_if_exist:nNTF#1{\tl_if_exist:NTF}
757 % \end{macrocode}
758 %
759 %
760 % \begin{macrocode}
761 \cs_new:Nn\tuple_new_bool_index:nNNn
762 {
763 \cs_new:cn{#1_#4_expands_to:TF}{
764 \cs_set:Nn#2{##1}
765 \cs_set:Nn#3{##2}}
766 \cs_gset:Nn#2{}
767 \cs_gset:Nn#3{}
768 %
769 \cs_new:cx{#1_#4_if:NTF}
770 {\exp_not:N\expandafter
771 \exp_not:c{#1_#4_if~i:w}
772 ##1 \exp_not:n{#2 ab #2 c \tuple_end:}{##3}{##2}}
773 \cs_new:cpn{#1_#4_if~i:w}
774 ##1 #2 ##2 #2 ##3 \tuple_end:
775 {\tl_if_single:nTF{##3}}
776 %
777 \cs_new:cx{#1_#4_gset_false:N}
778 {\exp_not:N\expandafter
779 \exp_not:c{#1_#4_gset_false~i:w}
780 ##1 \exp_not:n{#2 ab #2 c \tuple_end:}##1}
781 \cs_new:cpn{#1_#4_gset_false~i:w}
782 ##1 #2 ##2 #2 ##3 \tuple_end:##4
783 {\tl_if_single:nTF{##3}
784 {\tl_gset:Nn##4{##1}}
785 {\tl_gset:Nn##4{##1##2}}}
786 %
787 \cs_new:cx{#1_#4_gset_true:N}
788 {\exp_not:N\expandafter
789 \exp_not:c{#1_#4_gset_true~:w}
790 ##1 \exp_not:n{#2 ab #2 c \tuple_end:}##1}
791 \cs_new:cpn{#1_#4_gset_true~:w}
792 ##1 #2 ##2 #2 ##3 \tuple_end: ##4
793 {\tl_if_single:nTF{##4}
794 {\tl_gset:Nn##4{##1 #2}}{}}
795 }
796 \cs_new:Nn\tuple_new_bool_index:nn
797 {\use:x{\exp_not:n{\tuple_new_bool_index:nNNn{#1}}
798 \exp_not:c{BOOL_#1_#2_true:}
799 \exp_not:c{BOOL_#1_#2_false:}
800 {#2}}}
801 % \end{macrocode}
802 %
803 %
804 %
805 % \begin{macrocode}
806 \cs_new:Nn\tuple_new_index:nNnn{
807 \cs_new:cn{#1_#3_expands_to:n}{\cs_set:Nn#2{##1}}
808 \cs_gset:Nn#2{}
809 %
810 \cs_new:cx{#1_#3_use:N}
811 {\exp_not:N\expandafter
812 \exp_not:c{#1_#3_use~i:w}
813 ##1 \exp_not:n{#2 {#4}\tuple_end:}}
814 \cs_new:cpx{#1_#3_use~i:w}
815 ##1 #2 ##2 ##3 \tuple_end:{##2}
816 %
817 \cs_new:cx{#1_#3_if_exist:NTF}
818 {\exp_not:N\expandafter
819 \exp_not:c{#1_#3_if_exist~i:w}
820 ##1 \exp_not:n{#2 ab #2 c \tuple_end:}{##3}{##2}}
821 \cs_new:cpn{#1_#3_if_exist~i:w}
822 ##1 #2 ##2 ##3 #2 ##4 \tuple_end:
823 {\tl_if_single:nTF{##4}}
824 %
825 \cs_new:cx{#1_#3_gdel:N}
826 {\exp_not:N\expandafter
827 \exp_not:c{#1_#3_gdel~i:w}
828 ##1 \exp_not:n{#2 ab #2 c \tuple_end:}##1}
829 \cs_new:cpn{#1_#3_gdel~i:w}
830 ##1 #2 ##2 ##3 #2 ##4 \tuple_end:##5
831 {\tl_if_single:nTF{##4}
832 {\tl_gset:Nn##5{##1}}
833 {\tl_gset:Nn##5{##1##3}}}
834 %
835 \cs_new:cx{#1_#3_gapply:NN}
836 {\exp_not:N\expandafter
837 \exp_not:c{#1_#3_gapply~i:w}
838 ##1 \exp_not:n{#2 ab #2 c \tuple_end:}##1##2}
839 \cs_new:cpn{#1_#3_gapply~i:w}
840 ##1 #2 ##2 ##3 #2 ##4 \tuple_end: ##5 ##6
841 {\tl_if_single:nTF{##4}
842 {\tl_gset:Nx##5{\exp_not:n{##1 #2}{##6{#4}}}}
843 {\tl_gset:Nx##5{\exp_not:n{##1 #2}{##6{##2}} \exp_not:n{##3}}}}
844 }
845
846
847
848 \cs_new:Nn\tuple_new_tl_index:nNnn
849 {\tuple_new_index:nNnn{#1}#2{#3}{#4}
850 %
851 \cs_new:cx{#1_#3_gset:Nn}
852 {\exp_not:N\expandafter
853 \exp_not:c{#1_#3_gset~:w}
854 ##1 \exp_not:n{#2 ab #2 c \tuple_end:}##1{##2}}
855 \cs_new:cpn{#1_#3_gset~:w}
856 ##1 #2 ##2 ##3 #2 ##4 \tuple_end: ##5 ##6
857 {\tl_if_single:nTF{##4}
858 {\tl_gset:Nn##5{##1 #2{##6}}}
859 {\tl_gset:Nn##5{##1 #2{##6} ##3}}}
860 }
861 \cs_new:Nn\tuple_new_tl_index:nnn
862 {\exp_args:Nnc\tuple_new_tl_index:nNnn{#1}{TL_#1_#2_:n}{#2}{#3}}
863 \cs_new:Nn\tuple_new_tl_index:nn
864 {\tuple_new_tl_index:nnn{#1}{#2}{}}
865
866 \cs_new:Nn\int_incr:n{\int_eval:n{#1+1}}
867 \cs_new:Nn\int_decr:n{\int_eval:n{#1-1}}
868
869 \cs_new:Nn\tuple_new_int_index:nNnn{
870 \tuple_new_index:nNnn{#1}#2{#3}{#4}
871 %
872 \cs_new:cx{#1_#3_get_int:NN}
873 {\exp_not:n{\int_set:Nn} ##2 {\exp_not:c{#1_#3_use:N} \exp_not:N##1}}
874 %
875 \cs_new:cx{#1_#3_gincr:N}
876 {\exp_not:c{#1_#3_gapply:NN}##1\exp_not:N\int_incr:n}
877 %
878 \cs_new:cx{#1_#3_gdecr:N}
879 {\exp_not:c{#1_#3_gapply:NN}##1\exp_not:N\int_decr:n}
880 %
881 \cs_new:cx{#1_#3_gset:Nn}
882 {\exp_not:N\expandafter
883 \exp_not:c{#1_#3_gset~:w}
884 ##1 \exp_not:n{#2 ab #2 c \tuple_end:}##1{##2}}
885 \cs_new:cpn{#1_#3_gset~:w}
886 ##1 #2 ##2 ##3 #2 ##4 \tuple_end: ##5 ##6
887 {\tl_if_single:nTF{##4}
888 {\tl_gset:Nx##5{##1 \exp_not:N#2 {\int_eval:n{##6}}}}
889 {\tl_gset:Nx##5{##1 \exp_not:N#2 {\int_eval:n{##6}} ##3}}}
890 %
891 \cs_new:cpx{#1_#3_case:NnTF}##1
892 {\exp_not:N\int_case:nnTF{\exp_not:c{#1_#3_eval:n}##1}}
893 }
894 \cs_new:Nn\tuple_new_int_index:nnn
895 {\exp_args:Nnc\tuple_new_int_index:nNnn{#1}{INT_#1_#2_:n}{#2}{#3}}
896 \cs_new:Nn\tuple_new_int_index:nn
897 {\tuple_new_int_index:nnn{#1}{#2}{0}}
898 % \end{macrocode}
899 %
900 % \begin{macrocode}
901 %\cs_new:Nn\tuple_index_default_cs_set_eq:NN{
902 % \cs_set:Npn#1{\cs_set_eq:NN#2}}
903 %\cs_new:Nn\tuple_index_default_tl_set:NN{
904 % \cs_set:Npn#1{\tl_set:Nn#2}}
905 %\cs_new:Nn\tuple_index_default_tl_put_left:NN{
906 % \cs_set:Npn#1{\tl_put_left:Nn#2}}
907 %\cs_new:Nn\tuple_index_default_tl_put_right:NN{
908 % \cs_set:Npn#1{\tl_put_right:Nn#2}}
909 %\cs_new:Nn\tuple_index_default_int_set:NN{
910 % \int_new:N#2
911 % \cs_set:Npn#1{\int_set:Nn#2}}
912 %\cs_new:Nn\tuple_index_default_bool_set:NN{
913 % \bool_new:N#2
914 % \cs_set:Npn#1##1{##1#2}}
915 %\cs_new:Nn\tuple_index_default_ignore:N{
916 % \cs_set:Npn#1##1{}}
917 %\cs_new:Nn\tuple_index_default_show:N{
918 % \cs_set_eq:NN#1\tl_show:n}
919 % \end{macrocode}
920 %
921 %
922 % \subsection{Variables encoding}
923 %
924 % We use an automatic generator of variables, of different types.
925 % For global \texttt{tl} variables, the command is |\NewGTl|
926 %\begin{verbatim}
927 %\NewGTl{variable}[default value]{arguments}[identifier]
928 %\end{verbatim}
929 % The arguments satisfy the syntax of \texttt{xparse}, and the identifier uses these arguments for
930 % creating a token list that can be used in a csname.
931 % The result of this command is that new commands |\variableIfTF|, |\variableSet|,
932 % |\variableSetX|, |\variableGet|, |\variableApply|, |\variableApplyInline| and |\variableIfEmpty|
933 % are created.
934 % For instance
935 %\begin{verbatim}
936 % \NewGTl{Test}{ m o }[test default]{\IfNoValue{#1}{#2}{#2-#1}}
937 %\end{verbatim}
938 % Gives right to |\TestSet{first}[1]{success}|
939 % |\TestApplyInline{first}[1]{The test was a #1!}|.
940 %
941 %
942 %
943 % \begin{macrocode}
944 \cs_new:Nn\int_get_default:N
945 {\int_if_exist:NTF#1{\int_use:N#1}{0}}
946 \cs_generate_variant:Nn\int_get_default:N{c}
947 \cs_new:Nn\int_force:N
948 {\int_if_exist:NTF#1{}{\int_new:N#1}}
949 \cs_new:Npn\int_force_gset:Nn#1
950 {\int_force:N#1\int_gset:Nn#1}
951 % \end{macrocode}
952 %
953 %
954 % \begin{macrocode}
955 \NewDocumentCommand\NewGBool{ m m o }{
956 \IfNoValueTF{#3}{
957 \tl_gset:Nn\g_tmpa_tl{#2}
958 \tl_remove_all:Nn\g_tmpa_tl{~}
959 \int_case:nnTF{\tl_count:N\g_tmpa_tl}{
960 0 {\NewGBoolComplete {#1}{#2}{#1:_bool}}
961 1 {\NewGBoolComplete {#1}{#2}{#1:##1_bool}}
962 2 {\NewGBoolComplete {#1}{#2}{#1:##1...##2_bool}}
963 3 {\NewGBoolComplete {#1}{#2}{#1:##1...##2...##3_bool}}
964 4 {\NewGBoolComplete {#1}{#2}{#1:##1...##2...##3...##4_bool}}
965 5 {\NewGBoolComplete {#1}{#2}{#1:##1...##2...##3...##4...##5_bool}}
966 6 {\NewGBoolComplete {#1}{#2}{#1:##1...##2...##3...##4...##5...##6_bool}}
967 7 {\NewGBoolComplete {#1}{#2}{#1:##1...##2...##3...##4...##5...##6...##7_bool}}
968 8 {\NewGBoolComplete {#1}{#2}{#1:##1...##2...##3...##4...##5...##6...##7...##8_bool}}
969 9 {\NewGBoolComplete {#1}{#2}{#1:##1...##2...##3...##4...##5...##6...##7...##8...##9_bool}}
970 }{}{\ERROR}}{\NewGBoolComplete{#1}{#2}{#3}}}
971 % \end{macrocode}
972 %
973 % \begin{macrocode}
974 \NewDocumentCommand\NewGBoolComplete{ m m m }{
975 \exp_args:Nc\NewDocumentCommand{#1SetTrue}{#2}
976 {\bool_gset_true:c{#3}}
977 \exp_args:Nc\NewDocumentCommand{#1SetFalse}{#2}
978 {\bool_gset_false:c{#3}}
979 \exp_args:Nc\NewDocumentCommand{#1Set}{#2}
980 {\exp_args:Nc\bool_gset_tl:Nn{#3}}
981 \exp_args:Nc\DeclareExpandableDocumentCommand{#1IfTF}{#2}
982 {\bool_if:cTF{#3}}
983 }
984 \cs_new:Nn\bool_gset_tl:Nn
985 {\use:c{bool_gset_#2:N}#1}
986 % \end{macrocode}
987 %
988 %
989 %
990 % \begin{macrocode}
991 \NewDocumentCommand\NewGCs{ m m o }{
992 \IfNoValueTF{#3}{
993 \tl_gset:Nn\g_tmpa_tl{#2}
994 \tl_remove_all:Nn\g_tmpa_tl{~}
995 \int_case:nnTF{\tl_count:N\g_tmpa_tl}{
996 0 {\NewGCsComplete {#1}{#2}{#1_:}}
997 1 {\NewGCsComplete {#1}{#2}{#1_##1_cs:w}}
998 2 {\NewGCsComplete {#1}{#2}{#1_##1...##2_cs:w}}
999 3 {\NewGCsComplete {#1}{#2}{#1_##1...##2...##3_cs:w}}
1000 4 {\NewGCsComplete {#1}{#2}{#1_##1...##2...##3...##4_cs:w}}
1001 5 {\NewGCsComplete {#1}{#2}{#1_##1...##2...##3...##4...##5_cs:w}}
1002 6 {\NewGCsComplete {#1}{#2}{#1_##1...##2...##3...##4...##5...##6_cs:w}}
1003 7 {\NewGCsComplete {#1}{#2}{#1_##1...##2...##3...##4...##5...##6...##7_cs:w}}
1004 8 {\NewGCsComplete {#1}{#2}{#1_##1...##2...##3...##4...##5...##6...##7...##8_cs:w}}
1005 9 {\NewGCsComplete {#1}{#2}{#1_##1...##2...##3...##4...##5...##6...##7...##8...##9_cs:w}}
1006 }{}{\ERROR}}{\NewGCsComplete{#1}{#2}{#3}}}
1007 % \end{macrocode}
1008 %
1009 % \begin{macrocode}
1010 \NewDocumentCommand\NewGCsComplete{ m m m }{}
1011 % \exp_args:Nc\NewDocumentCommand{#1New_internal}{ m m m }
1012 % {\NewDocumentCommand##1{ m ##2}{##3} }
1013 % \exp_args:Nc\NewDocumentCommand{#1New}{#2}
1014 % {\cs_new:cn{#2}{#2_internal{#2}}
1015 % \exp_args:Nc{#2_internal}}
1016 % \end{macrocode}
1017 %
1018 %
1019 %
1020 %
1021 %
1022 % \subsection{The \texttt{knowledge} file handling}
1023 %
1024 %
1025 % The knowledge package uses two macros for file handling:
1026 %\begin{itemize}
1027 %\item |\kl_copy_file:nn|(|nV|, |Vn|, |VV|) takes the name of two files
1028 % and copies the first one to the second (does nothing if the first file does not exist),
1029 %\item |kl_compare_files:Nnn|(|NnV|, |NVn|, |NVV|) takes a boolean and the name of two files,
1030 % and sets the boolean (locally) according to the result of their comparison for equality,
1031 %\item |\kl_tl_to_file:nn| takes the name of a file, and a token list, and creates the file with the tl as content.
1032 % This is a conveninent way to create empty files (since deleting files is not possible with \kl{TeX}...)
1033 %\end{itemize}
1034 %
1035 % \begin{macrocode}
1036 \cs_new_protected_with_variants:Nnn\kl_copy_file:nn{nV,Vn,VV}
1037 {\file_if_exist:nT{#1}
1038 {\group_begin:
1039 \bool_set_false:N\l_tmpa_bool
1040 \ior_open:Nn\klg_tmpa_ior{#1}
1041 \iow_open:Nn\klg_tmpa_iow{#2}
1042 \bool_do_until:Nn\l_tmpa_bool{
1043 \ior_if_eof:NTF\klg_tmpa_ior
1044 {\bool_set_true:N\l_tmpa_bool}
1045 {\ior_str_get:NN
1046 \klg_tmpa_ior
1047 \l_tmpa_tl
1048 \exp_args:NNV
1049 \iow_now:Nn
1050 \klg_tmpa_iow
1051 \l_tmpa_tl}
1052 }
1053 \ior_close:N\klg_tmpa_ior
1054 \iow_close:N\klg_tmpa_iow
1055 \group_end:}}
1056 % \end{macrocode}
1057 % \begin{macrocode}
1058 \cs_new_protected_with_variants:Nnn\kl_compare_files:Nnn{NnV,NVn,NVV}
1059 {\group_begin:
1060 \bool_set_false:N\l_tmpa_bool% set when finished
1061 \bool_set_true:N\l_tmpb_bool% result
1062 \file_if_exist:nTF{#2}
1063 {\file_if_exist:nF{#3}
1064 {\bool_set_true:N\l_tmpa_bool
1065 \bool_set_false:N\l_tmpb_bool}}
1066 {\bool_set_true:N\l_tmpa_bool
1067 \file_if_exist:nT{#3}
1068 {\bool_set_false:N\l_tmpb_bool}}
1069 \bool_if:NF\l_tmpa_bool{
1070 \ior_open:Nn\klg_tmpa_ior{#2}
1071 \ior_open:Nn\klg_tmpb_ior{#3}
1072 %
1073 \bool_do_until:Nn\l_tmpa_bool{
1074 \ior_if_eof:NTF\klg_tmpa_ior
1075 {\bool_set_true:N\l_tmpa_bool
1076 \ior_if_eof:NF\klg_tmpb_ior
1077 {\bool_set_false:N\l_tmp_bool}}
1078 {\ior_if_eof:NTF\klg_tmpb_ior
1079 {\bool_set_true:N\l_tmpa_bool
1080 \bool_set_false:N\l_tmp_bool}
1081 {\ior_str_get:NN
1082 \klg_tmpa_ior
1083 \l_tmpa_tl
1084 \ior_str_get:NN
1085 \klg_tmpb_ior
1086 \l_tmpb_tl
1087 \tl_if_eq:NNF
1088 \l_tmpa_tl\l_tmpb_tl
1089 {\bool_set_true:N\l_tmpa_bool
1090 \bool_set_false:N\l_tmpb_bool}
1091 }}
1092 }
1093 \ior_close:N\klg_tmpa_ior
1094 \ior_close:N\klg_tmpb_ior}
1095 \bool_if:NTF\l_tmpb_bool
1096 {\group_end:\bool_set_true:N#1}
1097 {\group_end:\bool_set_false:N#1}
1098 }
1099 % \end{macrocode}
1100 % \begin{macrocode}
1101 \cs_new_protected:Nn\kl_tl_to_file:nn
1102 {\iow_open:Nn\klg_tmpa_iow{#1}
1103 \iow_now:Nn\klg_tmpa_iow{#2}
1104 \iow_close:N\klg_tmpa_iow}
1105 % \end{macrocode}
1106 %
1107 % These macros are used for testing the modification of knowledges.
1108 % At the begining, jobname.knowledge.aux is transferred to jobname.knowledge.tmp.
1109 % During the file reading, the file jobname.knowledge.aux is again written.
1110 % At the end, the two are compared.
1111 %
1112 % \begin{macrocode}
1113 \ior_new:N\klg_tmpa_ior
1114 \ior_new:N\klg_tmpb_ior
1115 \iow_new:N\klg_tmpa_iow
1116 % \end{macrocode}
1117 %
1118 %
1119 %
1120 %
1121 %
1122 % \subsection{Dealing with LaTeX commands}
1123 %
1124 %
1125 % We define now a certain number of commands that allows to
1126 % \begin{itemize}
1127 % \item Store known commands with a protected name.
1128 % \item Declare commands as obsolete when used in a document.
1129 % \item Create variants using \textsc{xsparse} that remove the hassle of optional parameters/stars, and so on.
1130 % \end{itemize}
1131 %
1132 %
1133 % \subsubsection{Overloading command}
1134 % Overloading a command is similar in syntax to |\NewDocumentCommand|
1135 % of the \texttt{xparse} package, but it further offers the macro |\SUPERcommand|
1136 % (where |\command| is the token under definition), which can be used for calling the original code.
1137 % Formally, the syntax is:\\
1138 % |\OverloadCommand\command{arguments}[variant name]{code}|\\
1139 % The arguments follox the \texttt{xparse} syntax. The variant name is by default ``\texttt{NEW}''.
1140 % Other variants of the commands can be defined by changing this text.
1141 % The macro |\ChooseCommand| allows to choose between the variant. It takes as first argument a control sequence (or a chain of control sequences), and as second argument the variant that should be taken.
1142 % Hence |\ChooseCommand\command{SUPER}| reverts |\command| to its original behaviour.
1143 % \begin{macrocode}
1144 \cs_new:Npn\commandVariant#1#2{#2\cs_to_str:N#1}
1145
1146 \cs_generate_variant:Nn\cs_gset_eq:NN{cN,cc}
1147 \NewDocumentCommand\OverloadCommand{mmO{NEW}m}
1148 {\cs_if_exist:cTF
1149 {\commandVariant#1{switch~}}
1150 {}
1151 {\exp_args:Nc\cs_gset_eq:NN
1152 {\commandVariant#1{SUPER}}
1153 #1
1154 \exp_args:NNx\cs_gset:Npn
1155 #1{\exp_not:c{\commandVariant#1{switch~}}}
1156 }
1157 \exp_args:Nc\NewDocumentCommand
1158 {\commandVariant#1{#3}}{#2}{#4}
1159 \ChooseCommand#1{#3}
1160 }
1161 \NewDocumentCommand\ChooseCommand{mm}{
1162 \tl_map_inline:nn{#1}
1163 {\cs_if_exist:cTF
1164 {\commandVariant##1{#2}}
1165 {\cs_gset_eq:cc
1166 {\commandVariant##1{switch~}}
1167 {\commandVariant##1{#2}}}
1168 {\tl_show:n
1169 {knowledge_utils~ERROR:~##1~has~no~variant~{#2}}}
1170 }
1171 }
1172 % \end{macrocode}
1173 %
1174 %
1175 %
1176 % \subsubsection{Xparse reversing}
1177 % The package \texttt{xparse} offers a convenient way for describing the profile of parameters
1178 % of new commands (optional star, optional parameters, and so on)
1179 % We provide converse commands, that recreates these arguments.
1180 % For instance, the command |\section| is supposed to have an optional star (numbering or not), followed by an optional argument (title in the toc), followed by a mandatory argument (the title of the section).
1181 % This would be defined with \texttt{xparse} as |\NewDocumentCommand\section{som}{...code...}|,
1182 % where \texttt{som} means star-optional-mandatory.
1183 % Imagine you want to overload this command. The command |\XParseArgs| is meant for that.
1184 % One first stores the old command |\section| as |\oldsection|:
1185 %\begin{verbatim}
1186 % \let\oldsection\section
1187 %\end{verbatim}
1188 % and then redefine it, calling in the code the old version:
1189 % \begin{verbatim}
1190 % \RenewDocumentCommand\section{som}{%
1191 % <pre-code>%
1192 % \XparseArgs\oldsection{som}{#1}{#2}{#3}
1193 % <post-code>}
1194 % \end{verbatim}
1195 % (please refer to \texttt{xparse} documentation for knowing how to use the arguments
1196 % in the pre- and post-code)
1197 %
1198 % The command |\ExpXparseArgs| is the expandable variant of it (I was unable to produce
1199 % a version that would work in both situations !).
1200 %
1201 % For parameter types treated so far are:
1202 % \begin{compactdesc}
1203 % \item[i]= ignore
1204 % \item[m]= mandatory
1205 % \item[o]= optional (under square brackets)
1206 % \item[s]= stars
1207 % \end{compactdesc}
1208 % \begin{macrocode}
1209 \NewDocumentCommand\XparseArgs{mm}
1210 {\kutils_reverting_args:Nn#1{#2}}
1211 \DeclareExpandableDocumentCommand\ExpXparseArgs{mm}
1212 {\kutils_exec_xreverting_args:Nn#1{#2}}
1213 % \end{macrocode}
1214 % \begin{macrocode}
1215 \cs_new:Nn\kutils_xrevert_arg_s:n
1216 { \IfBooleanTF{#1}{*}{}}
1217 \cs_new:Nn\kutils_xrevert_arg_o:n
1218 { \IfValueTF{#1}{[\exp_not:n{#1}]}{}}
1219 \cs_new:Nn\kutils_xrevert_arg_m:n
1220 { {\exp_not:n{#1}} }
1221 \cs_new:Nn\kutils_xrevert_arg_i:n
1222 { }
1223 % \end{macrocode}
1224 % \begin{macrocode}
1225 \cs_new:Nn\kutils_reverting_args:Nn{
1226 \use:c{kutils_process_\tl_count:n{#2}:w}
1227 #1{\kutils_exec_xreverting_args:n{#2}}}
1228 % \end{macrocode}
1229 % \begin{macrocode}
1230 \cs_new:Nn\kutils_exec_xreverting_args:n{
1231 \tl_if_empty:nTF{#1}{}
1232 {\kutils_exec_xreverting_args_internal:w#1\kutils_end:}}
1233 \cs_new:Nn\kutils_exec_xreverting_args:Nn{
1234 \exp_not:N#1
1235 \tl_if_empty:nTF{#2}{}
1236 {\kutils_exec_xreverting_args_internal:w#2\kutils_end:}}
1237 \cs_new:Npn\kutils_exec_xreverting_args_internal:w#1#2\kutils_end:#3
1238 { \use:c{kutils_xrevert_arg_#1:n}{#3}
1239 \tl_if_empty:nTF{#2}{}
1240 {\kutils_exec_xreverting_args_internal:w#2\kutils_end:}}
1241 % \end{macrocode}
1242 % The following internal commands |\cs_new:Nn\kutils_process_args:nn...| eat a token list `|\com|',
1243 % then an expandable command token list |\proc|, that is totally expanded with as arguments the following arguments (the number of which is fixed from the signature).
1244 % In the end, |\com| is prefixed.
1245 % Hence |\kutils_process_args:nnnn\com\proc a b|
1246 % completely expands |{\proc a b}| yielding some `result', and then executes |\com| followed by the `result'.
1247 % E.g. if |\swap#1#2| is |{#2}{#1}|, then |\kutils_process_args:nnnn\com\swap|
1248 % expands to the same as |\com| but with arguments reversed.
1249 % \begin{macrocode}
1250 \cs_new:Nn\kutils_process_args:nn
1251 {#1}
1252 \cs_new:Nn\kutils_process_args:nnn
1253 {\use:nx{#1}{#2{#3}}}
1254 \cs_new:Nn\kutils_process_args:nnnn
1255 {\use:nx{#1}{#2{#3}{#4}}}
1256 \cs_new:Nn\kutils_process_args:nnnnn
1257 {\use:nx{#1}{#2{#3}{#4}{#5}}}
1258 \cs_new:Nn\kutils_process_args:nnnnnn
1259 {\use:nx{#1}{#2{#3}{#4}{#5}{#6}}}
1260 \cs_new:Nn\kutils_process_args:nnnnnnn
1261 {\use:nx{#1}{#2{#3}{#4}{#5}{#6}{#7}}}
1262 \cs_new:Nn\kutils_process_args:nnnnnnnn
1263 {\use:nx{#1}{#2{#3}{#4}{#5}{#6}{#7}{#8}}}
1264 \cs_new:Nn\kutils_process_args:nnnnnnnnn
1265 {\use:nx{#1}{#2{#3}{#4}{#5}{#6}{#7}{#8}{#9}}}
1266 \exp_args:Nc\let{kutils_process_0:w}\kutils_process_args:nn
1267 \exp_args:Nc\let{kutils_process_1:w}\kutils_process_args:nnn
1268 \exp_args:Nc\let{kutils_process_2:w}\kutils_process_args:nnnn
1269 \exp_args:Nc\let{kutils_process_3:w}\kutils_process_args:nnnnn
1270 \exp_args:Nc\let{kutils_process_4:w}\kutils_process_args:nnnnnn
1271 \exp_args:Nc\let{kutils_process_5:w}\kutils_process_args:nnnnnnn
1272 \exp_args:Nc\let{kutils_process_6:w}\kutils_process_args:nnnnnnnn
1273 \exp_args:Nc\let{kutils_process_7:w}\kutils_process_args:nnnnnnnnn
1274 % \end{macrocode}
1275 %
1276 % \subsubsection{Obsolescence and vaulting}
1277 %
1278 % \begin{macrocode}
1279 \msg_new:nnn{scope}{LaTeX~obsolete}
1280 {The~command~'#1'~is~deprecated.~
1281 See~documentation~for~a~replacement.}
1282
1283 \cs_new:Nn\kutils_secure_latex_command:N
1284 {\cs_new_eq:cN{LaTeX\cs_to_str:N#1}#1}
1285
1286 \cs_new:Nn\kutils_latex_to_vault:N
1287 {\kutils_secure_latex_command:N#1
1288 \cs_undefine:N#1}
1289
1290 \cs_new:Nn\kutils_latex_to_obsolete:N
1291 {\kutils_latex_to_vault:N#1
1292 \cs_new:Npx#1{\exp_not:N\msg_error:nnn{scope}{LaTeX~obsolete}{\string #1}}}
1293
1294 \cs_new:Nn\kutils_secure_latex_command:n
1295 {\tl_map_function:nN{#1}\kutils_secure_latex_command:N}
1296 \cs_new:Nn\kutils_latex_to_vault:n
1297 {\tl_map_function:nN{#1}\kutils_latex_to_vault:N}
1298 \cs_new:Nn\kutils_latex_to_obsolete:n
1299 {\tl_map_function:nN{#1}\kutils_latex_to_obsolete:N}
1300 % \end{macrocode}
1301 %
1302 %
1303 %
1304 %
1305 % \Finale
1306 \endinput
1307
1308
1309
1310