Debian Codegraph @

texmf-dist/source/latex-dev/base/lttemplates.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 1999 Frank Mittelbach, Chris Rowley, David Carlisle
4 % Copyright (C) 2004-2010 Frank Mittelbach, The LaTeX Project
5 % Copyright (C) 2011-2026
6 % The LaTeX Project and any individual authors listed elsewhere
7 % in this file.
8 %
9 % This file is part of the LaTeX base system.
10 % -------------------------------------------
11 %
12 % It may be distributed and/or modified under the
13 % conditions of the LaTeX Project Public License, either version 1.3c
14 % of this license or (at your option) any later version.
15 % The latest version of this license is in
16 % https://www.latex-project.org/lppl.txt
17 % and version 1.3c or later is part of all distributions of LaTeX
18 % version 2008 or later.
19 %
20 % This file has the LPPL maintenance status "maintained".
21 %
22 % The list of all files belonging to the LaTeX base distribution is
23 % given in the file `manifest.txt'. See also `legal.txt' for additional
24 % information.
25 %
26 % The list of derived (unpacked) files belonging to the distribution
27 % and covered by LPPL is defined by the unpacking scripts (with
28 % extension .ins) which are part of the distribution.
29 %
30 % \fi
31 %
32 % \iffalse
33 %
34 %%% From File: lttemplates.dtx
35 %
36 %<*driver>
37 % \fi
38 \ProvidesFile{lttemplates.dtx}
39 [2026-04-21 v1.0j LaTeX Kernel (Prototype document functions)]
40 % \iffalse
41 \documentclass{l3in2edoc}
42 \GetFileInfo{lttemplates.dtx}
43 \begin{document}
44 \DocInput{lttemplates.dtx}
45 \end{document}
46 %</driver>
47 % \fi
48 %
49 % \title{Templates: Prototype document functions\thanks{This file has version
50 % \fileversion\ dated \filedate, \copyright\ \LaTeX\
51 % Project.}}
52 % \author{^^A
53 % Frank Mittelbach, Chris Rowley, David Carlisle, \LaTeX{} Project\thanks
54 % {^^A
55 % E-mail:
56 % \href{mailto:[email protected]}
57 % {[email protected]}^^A
58 % }^^A
59 % }
60 %
61 % \maketitle
62 %
63 % \section{Introduction}
64 %
65 % There are three broad \enquote{layers} between putting down ideas into
66 % a source file and ending up with a typeset document. These layers of
67 % document writing are
68 % \begin{enumerate}
69 % \item authoring of the text with mark-up;
70 % \item document layout design;
71 % \item implementation (with \TeX{} programming) of the design.
72 % \end{enumerate}
73 % We write the text as an author, and we see the visual output of the design
74 % after the document is generated; the \TeX{} implementation in the middle is
75 % the glue between the two.
76 %
77 % \LaTeX{}'s greatest success has been to standardise a system of mark-up that
78 % balances the trade-off between ease of reading and ease of writing to suit
79 % almost all forms of technical writing. It's
80 % other original strength was a good background in typographical design; while
81 % the standard \LaTeXe{} classes look somewhat dated now in terms of their
82 % visual design, their typography is generally sound (barring the occasional
83 % minor faults).
84 %
85 % However, \LaTeXe{} has always lacked a standard approach to customising
86 % the visual design of a document. Changing the looks of the standard classes
87 % involved either:
88 % \begin{itemize}
89 % \item Creating a new version of the implementation code of the class and
90 % editing it.
91 % \item Loading one of the many packages to customise certain elements of
92 % the standard classes.
93 % \item Loading a completely different document class, such as
94 % \textsf{KOMA-Script} or \textsf{memoir}, that allows easy customization.
95 % \end{itemize}
96 % All three of these approaches have their drawbacks and learning curves.
97 %
98 % The idea behind \pkg{lttemplates} is to cleanly separate the three layers
99 % introduced at the beginning of this section, so that document authors who
100 % are not programmers can easily change the design of their documents.
101 % \pkg{lttemplates} also makes it easier for \LaTeX{} programmers to provide
102 % their own customizations on top of a pre-existing class.
103 %
104 % \section{What is a document?}
105 %
106 % Besides the textual content of the words themselves, the source file
107 % of a document contains mark-up elements that add structure to the
108 % document. These elements include sectional divisions, figure/table
109 % captions, lists of various sorts, theorems/proofs, and so on.
110 % The list will be different for every document that can be written.
111 %
112 % Each element can be represented logically without worrying about the
113 % formatting, with mark-up such as \cs{section}, \cs{caption},
114 % |\begin{enumerate}| and so on. The output of each one of these
115 % document elements will be a typeset representation of the information
116 % marked up, and the visual arrangement and design of these elements
117 % can vary widely in producing a variety of desired outcomes.
118 %
119 % For each type of document element, there may be design variations that
120 % contain the same sort of information but present it in slightly
121 % different ways. For example, the difference between a numbered and an
122 % unnumbered section, \cs{section} and |\section*|, or the difference
123 % between an itemized list or an enumerated list.
124 %
125 % There are three distinct layers in the definition of
126 % \enquote{a document} at this level
127 % \begin{enumerate}
128 % \item semantic elements such as the ideas of sections and lists;
129 % \item a set of design solutions for representing these elements
130 % visually;
131 % \item specific variations for these designs that represent the
132 % elements in the document.
133 % \end{enumerate}
134 % In the parlance of the template system, these are called types,
135 % templates, and instances, and they are discussed below in sections
136 % \ref{sec:types}, \ref{sec:templates}, and~\ref{sec:instances},
137 % respectively.
138 %
139 % \section {Types, templates, and instances}
140 %
141 % By formally declaring documents to be composed of mark-up elements
142 % grouped into types, which are interpreted and typeset with a set of
143 % templates, each of which has one or more instances with which to
144 % compose each and every semantic unit of the text, we can cleanly
145 % separate the components of document construction.
146 %
147 % All of the structures provided by the template system are global,
148 % and do not respect \TeX{} grouping.
149 %
150 % \section{Template types}
151 % \label{sec:types}
152 %
153 % An \emph{template type} (sometimes just \enquote{type}) is an
154 % abstract idea of a document element that takes a fixed number of
155 % arguments corresponding to the information from the document author
156 % that it is representing. A sectioning type, for example, might take
157 % three inputs: \enquote{title}, \enquote{short title}, and
158 % \enquote{label}.
159 %
160 % Any given document class will define which types are to be
161 % used in the document, and any template of a given type can be
162 % used to generate an instance for the type. (Of course, different
163 % templates will produce different typeset representations, but the
164 % underlying content will be the same.)
165 %
166 % \begin{function}{\NewTemplateType}
167 % \begin{syntax}
168 % \cs{NewTemplateType} \Arg{template type} \Arg{no. of args}
169 % \end{syntax}
170 % This function defines an \meta{template type} taking
171 % \meta{number of arguments}, where the \meta{type} is an
172 % abstraction as discussed above. For example,
173 % \begin{verbatim}
174 % \NewTemplateType{sectioning}{3}
175 % \end{verbatim}
176 % creates a type \enquote{sectioning}, where each use of that
177 % type will need three arguments.
178 % \end{function}
179 %
180 % \section{Templates}
181 % \label{sec:templates}
182 %
183 % A \emph{template} is a generalized design solution for representing
184 % the information of a specified type. Templates that do the same
185 % thing, but in different ways, are grouped together by their type
186 % and given separate names. There are two important parts to a template:
187 % \begin{itemize}
188 % \item the parameters it takes to vary the design it is producing;
189 % \item the implementation of the design.
190 % \end{itemize}
191 % As a document author or designer does not care about the
192 % implementation but rather only the interface to the template, these two
193 % aspects of the template definition are split into two independent
194 % declarations, \cs{DeclareTemplateInterface} and
195 % \cs{DeclareTemplateCode}.
196 %
197 % \begin{function}{\DeclareTemplateInterface}
198 % \begin{syntax}
199 % \cs{DeclareTemplateInterface}
200 % ~~\Arg{type} \Arg{template} \Arg{no.~of args}
201 % ~~\Arg{key list}
202 % \end{syntax}
203 % A \meta{template} interface is declared for a particular
204 % \meta{type}, where the \meta{number of arguments} must
205 % agree with the type declaration. The interface itself is
206 % defined by the \meta{key list}, which is itself a key--value list
207 % taking a specialized format:
208 % \begin{quotation}
209 % \obeylines
210 % \noindent
211 % \meta{key1}~":"~\meta{key type1}~","
212 % \meta{key2}~":"~\meta{key type2}~","
213 % \meta{key3}~":"~\meta{key type3}~"="~\meta{default3}~","
214 % \meta{key4}~":"~\meta{key type4}~"="~\meta{default4}~","
215 % \ldots
216 % \end{quotation}
217 % Each \meta{key} name should consist of \textsc{ascii} characters,
218 % with the exception of |,|, |=| and |:|. The recommended form
219 % for key names is to use lower case letters, with dashes to separate
220 % out different parts. Spaces are ignored in key names, so they can be
221 % included or missed out at will. Each \meta{key} must have a
222 % \meta{key type}, which defines the type of input that the \meta{key}
223 % requires. A full list of key types is given in
224 % Table~\ref{tab:key-types}. Each key may have a \meta{default}
225 % value, which will be used in by the template if the \meta{key} is
226 % not set explicitly. The \meta{default} should be of the correct
227 % form to be accepted by the \meta{key type} of the \meta{key}: this
228 % is not checked by the code. Expressions for numerical values are
229 % evaluated when the template is used, thus for example values given
230 % in terms of |em| or |ex| will be set respecting the prevailing font.
231 % \end{function}
232 %
233 % \begin{table}
234 % \centering
235 % \begin{tabular}{>{\ttfamily}ll}
236 % \toprule
237 % \multicolumn{1}{l}{Key-type} & Description of input \\
238 % \midrule
239 % boolean & \texttt{true} or \texttt{false} \\
240 % choice\Arg{choices}
241 % & A list of pre-defined \meta{choices} \\
242 % commalist & A comma-separated list \\
243 % function\Arg{$N$}
244 % & A (protected) function definition with $N$ arguments
245 % ($N$ from $0$ to $9$) \\
246 % instance\Arg{name}
247 % & An instance of type \meta{name} \\
248 % integer & An integer or integer expression \\
249 % length & A fixed length \\
250 % muskip & A math length with shrink and stretch components \\
251 % real & A real (floating point) value \\
252 % skip & A length with shrink and stretch components \\
253 % tokenlist & A token list: any text or commands \\
254 % \bottomrule
255 % \end{tabular}
256 % \caption{Key-types for defining template interfaces with
257 % \cs{DeclareTemplateInterface}.}
258 % \label{tab:key-types}
259 % \end{table}
260 %
261 % \begin{function}{\KeyValue}
262 % \begin{syntax}
263 % \cs{KeyValue} \Arg{key name}
264 % \end{syntax}
265 % There are occasions where the default (or value) for one key
266 % should be taken from another. The \cs{KeyValue} function can be
267 % used to transfer this information without needing to know the
268 % internal implementation of the key:
269 % \begin{verbatim}
270 % \DeclareTemplateInterface { type } { template } { no. of args }
271 % {
272 % key-name-1 : key-type = value ,
273 % key-name-2 : key-type = \KeyValue { key-name-1 },
274 % ...
275 % }
276 % \end{verbatim}
277 % \end{function}
278 %
279 % \begin{function}{\DeclareTemplateCode}
280 % \begin{syntax}
281 % \cs{DeclareTemplateCode}
282 % ~~\Arg{type} \Arg{template} \Arg{no.~of args}
283 % ~~\Arg{key bindings} \Arg{code}
284 % \end{syntax}
285 % The relationship between a templates keys and the internal
286 % implementation is created using the \cs{DeclareTemplateCode}
287 % function. As with \cs{DeclareTemplateInterface}, the
288 % \meta{template} name is given along with the \meta{type}
289 % and \meta{number of arguments} required. The \meta{key bindings}
290 % argument is a key--value list which specifies the relationship
291 % between each \meta{key} of the template interface with an
292 % underlying \meta{variable}.
293 %
294 % \begin{quotation}
295 % \obeylines
296 % \noindent
297 % \meta{key1}~"="~\meta{variable1},
298 % \meta{key2}~"="~\meta{variable2},
299 % \meta{key3}~"="~global~\meta{variable3},
300 % \meta{key4}~"="~global~\meta{variable4},
301 % \ldots
302 % \end{quotation}
303 % With the exception of the choice, code and function key types,
304 % the \meta{variable} here should be the name of an existing
305 % \LaTeX3 register. As illustrated, the key word \enquote{global}
306 % may be included in the listing to indicate that the \meta{variable}
307 % should be assigned globally. A full list of variable bindings is
308 % given in Table~\ref{tab:key-vars}.
309 %
310 % The \meta{code} argument of \cs{DeclareTemplateCode} is used
311 % as the replacement text for the template when it is used, either
312 % directly or as an instance. This may therefore accept arguments
313 % |#1|, |#2|, \emph{etc}.~as detailed by the \meta{number of arguments}
314 % taken by the type.
315 % \end{function}
316 %
317 % \begin{table}
318 % \centering
319 % \begin{tabular}{>{\ttfamily}ll}
320 % \toprule
321 % \multicolumn{1}{l}{Key-type} & Description of binding \\
322 % \midrule
323 % boolean & Boolean variable, \emph{e.g}.~\cs{l_tmpa_bool} \\
324 % choice
325 % & List of choice implementations
326 % (see Section~\ref{sec:choices-key}) \\
327 % commalist & Comma list, \emph{e.g}.~\cs{l_tmpa_clist} \\
328 % function
329 % & Function taking $N$ arguments, \emph{e.g}.~\cs{use_i:nn} \\
330 % instance \\
331 % integer & Integer variable, \emph{e.g}.~\cs{l_tmpa_int} \\
332 % length & Dimension variable, \emph{e.g}.~\cs{l_tmpa_dim} \\
333 % muskip & Muskip variable, \emph{e.g}.~\cs{l_tmpa_muskip} \\
334 % real & Floating-point variable, \emph{e.g}.~\cs{l_tmpa_fp} \\
335 % skip & Skip variable, \emph{e.g}.~\cs{l_tmpa_skip} \\
336 % tokenlist & Token list variable, \emph{e.g}.~\cs{l_tmpa_tl} \\
337 % \bottomrule
338 % \end{tabular}
339 % \caption{Bindings required for different key types when defining
340 % template implementations with \cs{DeclareTemplateCode}. Apart
341 % from \texttt{choice} and \texttt{function}
342 % all of these accept the key word \texttt{global} to carry
343 % out a global assignment.}
344 % \label{tab:key-vars}
345 % \end{table}
346 %
347 % \begin{function}{\AssignTemplateKeys}
348 % \begin{syntax}
349 % \cs{AssignTemplateKeys}
350 % \end{syntax}
351 % In the final argument of \cs{DeclareTemplateCode} the assignment of
352 % keys defined by the template may be delayed by including the command
353 % \cs{AssignTemplateKeys}. If this is \emph{not} present, keys are assigned
354 % immediately before the template code. If an
355 % \cs{AssignTemplateKeys} command is
356 % present, assignment is delayed until this point. Note that the
357 % command must be \emph{directly} present in the code, not placed
358 % within a nested command/macro.
359 % \end{function}
360 %
361 % \begin{function}{\SetKnownTemplateKeys,\SetTemplateKeys,\UnusedTemplateKeys}
362 % \begin{syntax}
363 % \cs{SetKnownTemplateKeys} \Arg{type} \Arg{template} \Arg{keyvals}
364 % \cs{SetTemplateKeys} \Arg{type} \Arg{template} \Arg{keyvals}
365 %
366 % \cs{UnusedTemplateKeys} \% all \meta{keyvals} unused by previous \cs{SetKnownTemplateKeys}
367 % \end{syntax}
368 %
369 % In the final argument of \cs{DeclareTemplateCode} one can also
370 % overwrite (some of) the current template key value settings by
371 % using the command \cs{SetKnownTemplateKeys} or
372 % \cs{SetTemplateKeys}, i.e., they can overwrite the template default values and
373 % the values assigned by the instance.
374 %
375 % The \cs{SetKnownTemplateKeys} and \cs{SetTemplateKeys} commands
376 % are only supported within the code of a template; using them
377 % elsewhere has unpredictable results. If they are used together
378 % with \cs{AssignTemplateKeys} then the latter command should come first
379 % in the template code.
380 %
381 % The main use case for these commands is the situation where there
382 % is an argument (normally \texttt{\#1}) to the template in which
383 % a key/value list can be specified that overwrites the normal
384 % settings. In that case one could use
385 %\begin{quote}
386 % \verb/\SetKnownTemplateKeys/\Arg{type}\Arg{template}\verb/{#1}/
387 %\end{quote}
388 % to process this key/value list inside the template.
389 %
390 % If \cs{SetKnownTemplateKeys} is executed and the \meta{keyvals} argument contains keys not known to the
391 % \meta{template} they are simply ignored and stored in the
392 % tokenlist \cs{UnusedTemplateKeys}
393 % without generating an error. This way it is possible to
394 % apply the same key/val list specified by the user on a
395 % document-level command or environment to several templates, which
396 % is useful, if the command or environment is implemented by calling several different template instances.
397 %
398 % \end{function}
399 %
400 % As a variation of that, you can use this key/val
401 % list the first time, and for the next template instance use what remains in
402 % \cs{UnusedTemplateKeys} (i.e., the key/val list with only the keys that have not been processed previously). The final processing step could then be
403 % \cs{SetTemplateKeys}, which unconditionally attempts to set the
404 % \meta{keyvals} received in its third argument. This command
405 % complains if any of them are unknown keys. Alternatively, you
406 % could use \cs{SetKnownTemplateKeys} and afterwards check whether \cs{UnusedTemplateKeys} is empty.\footnote{Using
407 % \cs{SetTemplateKeys} exposes the inner structure of the template
408 % keys when generating an error. This is something one may want to
409 % avoid as it can be confusing to the user, especially if several
410 % templates are involved. In that
411 % case use \cs{SetKnownTemplateKeys} and afterwards check whether
412 % \cs{UnusedTemplateKeys} is empty; if it is not empty then generate your own
413 % error message.}
414 %
415 % For example, a list, such as \env{enumerate}, is made up from a
416 % \texttt{blockenv}, \texttt{block}, \texttt{list}, and a
417 % \texttt{para} template and in the single user-supplied optional
418 % argument of \env{enumerate} key/values for any of these templates might
419 % be specified.
420 %
421 % In fact, in the particular example of list environments,
422 % the supplied key/value list is also saved and then applied to each
423 % \cs{item} which is implemented through an \texttt{item}
424 % template. This way, one can specify one-off settings for all the items
425 % of a single list (on the environment level), as well as to
426 % individual items within that list (by specifying them in the
427 % optional argument of an \cs{item}). With
428 % \cs{SetKnownTemplateKeys} and \cs{SetTemplateKeys} working
429 % together, it is possible to provide this flexibility and still
430 % alert the user when one of their keys is misspelled.
431 %
432 % On the other hand you may want to allow for
433 % \enquote{misspellings} without generating an error or a
434 % warning. For example, if you define a template that accepts only a
435 % few keys, you might just want to ignore anything specified in the
436 % source when you use this template in place of a different one,
437 % without the need to alter the document source. Or you might just
438 % generate a warning message, which is easy, given that the unused
439 % key/values are available in the \cs{UnusedTemplateKeys} variable.
440 %
441 %
442 % \begin{function}{\DeclareTemplateCopy}
443 % \begin{syntax}
444 % \cs{DeclareTemplateCopy}
445 % ~~\Arg{type} \Arg{template2} \Arg{template1}
446 % \end{syntax}
447 % Copies \meta{template1} of \meta{type} to a new name \meta{template2}:
448 % the copy can then be edited independent of the original.
449 % \end{function}
450 %
451 % \section{Multiple choices}
452 % \label{sec:choices-key}
453 %
454 % The \texttt{choice} key type implements multiple choice input. At the
455 % interface level, only the list of valid choices is needed:
456 % \begin{verbatim}
457 % \DeclareTemplateInterface { foo } { bar } { 0 }
458 % { key-name : choice { A, B, C } }
459 % \end{verbatim}
460 % where the choices are given as a comma-list (which must therefore
461 % be wrapped in braces). A default value can also be given:
462 % \begin{verbatim}
463 % \DeclareTemplateInterface { foo } { bar } { 0 }
464 % { key-name : choice { A, B, C } = A }
465 % \end{verbatim}
466 %
467 % At the implementation level, each choice is associated with code,
468 % using a nested key--value list.
469 % \begin{verbatim}
470 % \DeclareTemplateCode { foo } { bar } { 0 }
471 % {
472 % key-name =
473 % {
474 % A = Code-A ,
475 % B = Code-B ,
476 % C = Code-C
477 % }
478 % }
479 % { ... }
480 % \end{verbatim}
481 % The two choice lists should match, but in the implementation a
482 % special \texttt{unknown} choice is also available. This can be used
483 % to ignore values and implement an \enquote{else} branch:
484 % \begin{verbatim}
485 % \DeclareTemplateCode { foo } { bar } { 0 }
486 % {
487 % key-name =
488 % {
489 % A = Code-A ,
490 % B = Code-B ,
491 % C = Code-C ,
492 % unknown = Else-code
493 % }
494 % }
495 % { ... }
496 % \end{verbatim}
497 % The \texttt{unknown} entry must be the last one given, and should
498 % \emph{not} be listed in the interface part of the template.
499 %
500 % For keys which accept the values \texttt{true} and \texttt{false}
501 % both the boolean and choice key types can be used. As template
502 % interfaces are intended to prompt clarity at the design level, the
503 % boolean key type should be favored, with the choice type reserved
504 % for keys which take arbitrary values.
505 %
506 % \section{Instances}
507 % \label{sec:instances}
508 %
509 % After a template is defined it still needs to be put to use. The
510 % parameters that it expects need to be defined before it can be used in
511 % a document. Every time a template has parameters given to it, an
512 % \emph{instance} is created, and this is the code that ends up in the
513 % document to perform the typesetting of whatever pieces of information
514 % are input into it.
515 %
516 % For example, a template might say \enquote{here is a section with or
517 % without a number that might be centered or left aligned and print its
518 % contents in a certain font of a certain size, with a bit of a gap
519 % before and after it} whereas an instance declares \enquote{this is a
520 % section with a number, which is centered and set in $12\,\text{pt}$
521 % italic with a $10\,\text{pt}$ skip before and a
522 % $12\,\text{pt}$ skip after it}. Therefore, an instance is just a
523 % frozen version of a template with specific settings as chosen by the
524 % designer.
525 %
526 % \begin{function}{\DeclareInstance}
527 % \begin{syntax}
528 % \cs{DeclareInstance}
529 % ~~\Arg{type} \Arg{instance} \Arg{template} \Arg{parameters}
530 % \end{syntax}
531 % This function uses a \meta{template} for an \meta{type}
532 % to create an \meta{instance}. The \meta{instance} will be set
533 % up using the \meta{parameters}, which will set some of the
534 % \meta{keys} in the \meta{template}.
535 %
536 % As a practical example, consider a type for document sections
537 % (which might include chapters, parts, sections, \emph{etc}.), which
538 % is called \texttt{sectioning}. One possible template for this
539 % type might be called \texttt{basic}, and one instance of this
540 % template would be a numbered section. The instance declaration might
541 % read:
542 % \begin{verbatim}
543 % \DeclareInstance { sectioning } { section-num } { basic }
544 % {
545 % numbered = true ,
546 % justification = center ,
547 % font =\normalsize\itshape ,
548 % before-skip = 10pt ,
549 % after-skip = 12pt ,
550 % }
551 % \end{verbatim}
552 % Of course, the key names here are entirely imaginary, but illustrate
553 % the general idea of fixing some settings.
554 % \end{function}
555 %
556 % \begin{function}[EXP]{\InstanceValue}
557 % \begin{syntax}
558 % \cs{InstanceValue} \Arg{type} \Arg{instance} \Arg{key}
559 % \end{syntax}
560 % Expands to the current value for the \meta{key} stored in the
561 % \meta{instance} of \meta{type}. If the \meta{instance} does not exist,
562 % the expansion is empty. The result is returned within the
563 % |\unexpanded| primitive (|\exp_not:n|),
564 % \end{function}
565 %
566 % \begin{function}{\IfInstanceExistsT, \IfInstanceExistsF, \IfInstanceExistsTF}
567 % \begin{syntax}
568 % \cs{IfInstanceExistsTF} \Arg{type} \Arg{instance} \Arg{true code} \Arg{false code}
569 % \end{syntax}
570 % Tests if the named \meta{instance} of a \meta{type} exists, and
571 % then inserts the appropriate code into the input stream.
572 % \end{function}
573 %
574 % \begin{function}{\DeclareInstanceCopy}
575 % \begin{syntax}
576 % \cs{DeclareInstanceCopy}
577 % ~~\Arg{type} \Arg{instance2} \Arg{instance1}
578 % \end{syntax}
579 % Copies the \meta{values} for \meta{instance1} for an
580 % \meta{type} to \meta{instance2}.
581 % \end{function}
582 %
583 % \section{Document interface}
584 %
585 % After the instances have been chosen, document commands must be
586 % declared to use those instances in the document. \cs{UseInstance}
587 % calls instances directly, and this command should be used internally
588 % in document-level mark-up.
589 %
590 % \begin{function}{\UseInstance}
591 % \begin{syntax}
592 % \cs{UseInstance}
593 % ~~\Arg{type} \Arg{instance} \meta{arguments}
594 % \end{syntax}
595 % Uses an \meta{instance} of the \meta{type}, which will require
596 % \meta{arguments} as determined by the number specified for the
597 % \meta{type}. The \meta{instance} must have been declared
598 % before it can be used, otherwise an error is raised.
599 % \end{function}
600 %
601 % \begin{function}{\UseTemplate}
602 % \begin{syntax}
603 % \cs{UseTemplate} \Arg{type} \Arg{template}
604 % ~~\Arg{settings} \meta{arguments}
605 % \end{syntax}
606 % Uses the \meta{template} of the specified \meta{type},
607 % applying the \meta{settings} and absorbing \meta{arguments} as
608 % detailed by the \meta{type} declaration. This in effect
609 % is the same as creating an instance using \cs{DeclareInstance}
610 % and immediately using it with \cs{UseInstance}, but without the
611 % instance having any further existence. This command is therefore useful when
612 % a template needs to be used only once.
613 %
614 % This function can also be used as the argument to \texttt{instance}
615 % key types:
616 % \begin{verbatim}
617 % \DeclareInstance { type } { template } { instance }
618 % {
619 % instance-key =
620 % \UseTemplate { type2 } { template2 } { <settings> }
621 % }
622 % \end{verbatim}
623 % \end{function}
624 %
625 % \section{Changing existing definitions}
626 %
627 % Template parameters may be assigned specific defaults for instances
628 % to use if the instance declaration doesn't explicit set those
629 % parameters. In some cases, the document designer will wish to edit
630 % these defaults to allow them to \enquote{cascade} to the instances.
631 % The alternative would be to set each parameter identically for each
632 % instance declaration, a tedious and error-prone process.
633 %
634 % \begin{function}{\EditTemplateDefaults}
635 % \begin{syntax}
636 % \cs{EditTemplateDefaults}
637 % ~~\Arg{type} \Arg{template} \Arg{new defaults}
638 % \end{syntax}
639 % Edits the \meta{defaults} for a \meta{template} for an
640 % \meta{type}. The \meta{new defaults}, given as a key--value
641 % list, replace the existing defaults for the \meta{template}. This
642 % means that the change will apply to instances declared after the
643 % editing, but that instances which have already been created are
644 % unaffected.
645 % \end{function}
646 %
647 % \begin{function}{\EditInstance}
648 % \begin{syntax}
649 % \cs{EditInstance}
650 % ~~\Arg{type} \Arg{instance} \Arg{new values}
651 % \end{syntax}
652 % Edits the \meta{values} for an \meta{instance} for an
653 % \meta{type}. The \meta{new values}, given as a key--value
654 % list, replace the existing values for the \meta{instance}. This
655 % function is complementary to \cs{EditTemplateDefaults}:
656 % \cs{EditInstance} changes a single instance while leaving the
657 % template untouched.
658 % \end{function}
659 %
660 % \subsection{Expanding the values of keys}
661 %
662 % To allow the user to apply expansion of values when the key is set,
663 % key names can be
664 % followed by an expansion specifier. This is given by appending |:| and a
665 % single letter specifier to the key name. These letters are the normal
666 % argument specifiers for \pkg{expl3}, thus they may be one of \texttt{n}
667 % (redundant but supported), \texttt{o}, \texttt{V}, \texttt{v}, \texttt{e},
668 % \texttt{N} (again redundant) or \texttt{c}. Expansion of a control
669 % sequence name is particularly
670 % useful when you need to refer to an internal \LaTeXe{} or an L3 programming
671 % layer variable, e.g.,
672 % \begin{verbatim}
673 % key-a:c = @itemdepth , % use \@itemdepth as the value
674 % key-b:v = @itemdepth % use the current value of \@itempdepth as the value
675 % \end{verbatim}
676 %
677 % \section{Getting information about templates and instances}
678 %
679 % \begin{function}{\ShowInstanceValues}
680 % \begin{syntax}
681 % \cs{ShowInstanceValues} \Arg{type} \Arg{instance}
682 % \end{syntax}
683 % Shows the \meta{values} for an \meta{instance} of the given
684 % \meta{type} at the terminal.
685 % \end{function}
686 %
687 % \begin{function}{\ShowTemplateCode}
688 % \begin{syntax}
689 % \cs{ShowTemplateCode} \Arg{type} \Arg{template}
690 % \end{syntax}
691 % Shows the \meta{code} of a \meta{template} for an \meta{type}
692 % in the terminal.
693 % \end{function}
694 %
695 % \begin{function}{\ShowTemplateDefaults}
696 % \begin{syntax}
697 % \cs{ShowTemplateDefaults} \Arg{type} \Arg{template}
698 % \end{syntax}
699 % Shows the \meta{default} values of a \meta{template} for an
700 % \meta{type} in the terminal.
701 % \end{function}
702 %
703 % \begin{function}{\ShowTemplateInterface}
704 % \begin{syntax}
705 % \cs{ShowTemplateInterface} \Arg{type} \Arg{template}
706 % \end{syntax}
707 % Shows the \meta{keys} and associated \meta{key types} of a
708 % \meta{template} for an \meta{type} in the terminal.
709 % \end{function}
710 %
711 % \begin{function}{\ShowTemplateVariables}
712 % \begin{syntax}
713 % \cs{ShowTemplateVariables} \Arg{type} \Arg{template}
714 % \end{syntax}
715 % Shows the \meta{variables} and associated \meta{keys} of a
716 % \meta{template} for an \meta{type} in the terminal. Note that
717 % \texttt{code} and \texttt{choice} keys do not map directly to variables
718 % but to arbitrary code. For \texttt{choice} keys, each valid choice
719 % is shown as a separate entry in the list, with the key name and choice
720 % separated by a space, for example
721 % \begin{verbatim}
722 % Template 'example' of type 'example' has variable mapping:
723 % > demo unknown => \def \demo {?}
724 % > demo c => \def \demo {c}
725 % > demo b => \def \demo {b}
726 % > demo a => \def \demo {a}.
727 % \end{verbatim}
728 % would be shown for a choice key \texttt{demo} with valid choices
729 % \texttt{a}, \texttt{b} and \texttt{c}, plus code for an \texttt{unknown}
730 % branch.
731 % \end{function}
732 %
733 % \section{Debugging support}
734 %
735 % \begin{function}{\DebugTemplatesOn, \DebugTemplatesOff}
736 % \begin{syntax}
737 % \cs{DebugTemplatesOn}
738 % \end{syntax}
739 % Turn on or off debugging support for use of templates. Debugging information
740 % is provided at the point of \emph{use} of templates and instances, i.e.~where
741 % normal messages should be avoided for performance reasons.
742 % \end{function}
743 %
744 % \MaybeStop{\setlength\IndexMin{200pt}\PrintIndex}
745 %
746 % \section{The implementation}
747 %
748 % \begin{macrocode}
749 %<@@=template>
750 % \end{macrocode}
751 %
752 % \begin{macrocode}
753 %<*2ekernel>
754 \message{templates,}
755 %</2ekernel>
756 % \end{macrocode}
757 %
758 % \begin{macrocode}
759 %<*2ekernel|latexrelease>
760 % \end{macrocode}
761 %
762 % \begin{macrocode}
763 \ExplSyntaxOn
764 % \end{macrocode}
765 %
766 % \begin{macrocode}
767 %<latexrelease>\NewModuleRelease{2024/06/01}{lttemplates}
768 %<latexrelease> {Prototype~document~commands}%
769 % \end{macrocode}
770 %
771 % \subsection{Variables and constants}
772 %
773 % \begin{variable}
774 % {
775 % \c_@@_code_root_tl ,
776 % \c_@@_defaults_root_tl ,
777 % \c_@@_instances_root_tl ,
778 % \c_@@_keytypes_root_tl ,
779 % \c_@@_key_order_root_tl ,
780 % \c_@@_restrict_root_tl ,
781 % \c_@@_values_root_tl ,
782 % \c_@@_vars_root_tl
783 % }
784 % So that literal values are kept to a minimum.
785 % \begin{macrocode}
786 \tl_const:Nn \c_@@_code_root_tl { template~code~>~ }
787 \tl_const:Nn \c_@@_defaults_root_tl { template~defaults~>~ }
788 \tl_const:Nn \c_@@_instances_root_tl { template~instance~>~ }
789 \tl_const:Nn \c_@@_keytypes_root_tl { template~key~types~>~ }
790 \tl_const:Nn \c_@@_key_order_root_tl { template~key~order~>~ }
791 \tl_const:Nn \c_@@_values_root_tl { template~values~>~ }
792 \tl_const:Nn \c_@@_vars_root_tl { template~vars~>~ }
793 % \end{macrocode}
794 % \end{variable}
795 %
796 % \begin{variable}{\c_@@_keytypes_arg_seq}
797 % A list of keytypes which also need additional data (an argument),
798 % used to parse the keytype correctly.
799 % \begin{macrocode}
800 \seq_const_from_clist:Nn \c_@@_keytypes_arg_seq
801 { choice , function , instance }
802 % \end{macrocode}
803 % \end{variable}
804 %
805 % \begin{variable}{\g_@@_type_prop}
806 % For storing types and the associated number of arguments.
807 % \begin{macrocode}
808 \prop_new:N \g_@@_type_prop
809 % \end{macrocode}
810 % \end{variable}
811 %
812 % \begin{variable}{\l_@@_assignments_tl}
813 % When creating an instance, the assigned values are collected here.
814 % \begin{macrocode}
815 \tl_new:N \l_@@_assignments_tl
816 % \end{macrocode}
817 % \end{variable}
818 %
819 % \begin{variable}{\l_@@_default_tl}
820 % The default value for a key is recovered here from the property list
821 % in which it is stored.
822 % \begin{macrocode}
823 \tl_new:N \l_@@_default_tl
824 % \end{macrocode}
825 % \end{variable}
826 %
827 % \begin{variable}{\l_@@_error_bool}
828 % A flag for errors to be carried forward.
829 % \begin{macrocode}
830 \bool_new:N \l_@@_error_bool
831 % \end{macrocode}
832 % \end{variable}
833 %
834 % \begin{variable}{\l_@@_global_bool}
835 % Used to indicate that assignments should be global.
836 % \begin{macrocode}
837 \bool_new:N \l_@@_global_bool
838 % \end{macrocode}
839 % \end{variable}
840 %
841 % \begin{variable}
842 % {
843 % \l_@@_key_name_tl ,
844 % \l_@@_keytype_tl ,
845 % \l_@@_keytype_arg_tl ,
846 % \l_@@_value_tl ,
847 % \l_@@_var_tl
848 % }
849 % When defining each key in a template, the name and type of the key
850 % need to be separated and stored. Any argument needed by the
851 % keytype is also stored separately.
852 % \begin{macrocode}
853 \tl_new:N \l_@@_key_name_tl
854 \tl_new:N \l_@@_keytype_tl
855 \tl_new:N \l_@@_keytype_arg_tl
856 \tl_new:N \l_@@_value_tl
857 \tl_new:N \l_@@_var_tl
858 % \end{macrocode}
859 % \end{variable}
860 %
861 % \begin{variable}{\l_@@_value_exp_str}
862 % \changes{2025-07-08}{v1.0f}{New variable}
863 % \begin{macrocode}
864 \str_new:N \l_@@_value_exp_str
865 % \end{macrocode}
866 % \end{variable}
867 %
868 % \begin{variable}
869 % {
870 % \l_@@_keytypes_prop ,
871 % \l_@@_key_order_seq ,
872 % \l_@@_values_prop ,
873 % \l_@@_vars_prop
874 % }
875 % To avoid needing too many difficult-to-follow csname assignments,
876 % various scratch token registers are used to build up data, which is
877 % then transferred
878 % \begin{macrocode}
879 \prop_new:N \l_@@_keytypes_prop
880 \seq_new:N \l_@@_key_order_seq
881 \prop_new:N \l_@@_values_prop
882 \prop_new:N \l_@@_vars_prop
883 % \end{macrocode}
884 % \end{variable}
885 %
886 % \begin{variable}
887 % {
888 % \l_@@_tmp_clist ,
889 % \l_@@_tmp_dim ,
890 % \l_@@_tmp_int ,
891 % \l_@@_tmp_muskip ,
892 % \l_@@_tmp_skip ,
893 % \l_@@_tmp_tl
894 % }
895 % Scratch space.
896 % \begin{macrocode}
897 \clist_new:N \l_@@_tmp_clist
898 \dim_new:N \l_@@_tmp_dim
899 \int_new:N \l_@@_tmp_int
900 \muskip_new:N \l_@@_tmp_muskip
901 \skip_new:N \l_@@_tmp_skip
902 \tl_new:N \l_@@_tmp_tl
903 % \end{macrocode}
904 % \end{variable}
905 %
906 % \begin{variable}{\s_@@_mark, \s_@@_stop}
907 % Internal scan marks.
908 % \begin{macrocode}
909 \scan_new:N \s_@@_mark
910 \scan_new:N \s_@@_stop
911 % \end{macrocode}
912 % \end{variable}
913 %
914 % \begin{variable}{\q_@@_nil}
915 % Internal quarks.
916 % \begin{macrocode}
917 \quark_new:N \q_@@_nil
918 % \end{macrocode}
919 % \end{variable}
920 %
921 % \begin{macro}[pTF]{\@@_quark_if_nil:n}
922 % Branching quark conditional.
923 % \begin{macrocode}
924 \__kernel_quark_new_conditional:Nn \@@_quark_if_nil:N { F }
925 % \end{macrocode}
926 % \end{macro}
927 %
928 % \subsection{Debugging support}
929 %
930 % \changes{2026-03-1}{v1.0i}{Add debugging support}
931 %
932 % \begin{macro}{\@@_debug_on:, \@@_debug_off:}
933 % \changes{2026-04-21}{v1.0j}{Show debugging on the terminal}
934 % \begin{macrocode}
935 \cs_new_protected:Npn \@@_debug_on:
936 {
937 \cs_set_protected:Npn \@@_debug:n ##1 {##1}
938 \cs_set_protected:Npn \@@_debug_typeout:n ##1
939 { \iow_term:e { [ Template ] ~ ==> ~ ##1 } }
940 }
941 \cs_new_protected:Npn \@@_debug_off:
942 {
943 \cs_set_protected:Npn \@@_debug:n ##1 { }
944 \cs_set_protected:Npn \@@_debug_typeout:n ##1 { }
945 }
946 % \end{macrocode}
947 % \end{macro}
948 %
949 % \begin{macro}{\@@_debug:n,\@@_debug_typeout:n}
950 % \begin{macrocode}
951 \cs_new_protected:Npn \@@_debug:n #1 { }
952 \cs_new_protected:Npn \@@_debug_typeout:n #1 { }
953 % \end{macrocode}
954 % \end{macro}
955 %
956 % \subsection{Testing existence and validity}
957 %
958 % There are a number of checks needed for either the existence of
959 % a type, template or instance. There are also some for the
960 % validity of a particular call. All of these are collected up here.
961 %
962 % \begin{macro}{\@@_execute_if_arg_agree:nnT}
963 % A test agreement between the number of arguments for the template
964 % type and that specified when creating a template. This is not done as a
965 % separate conditional for efficiency and better error message
966 % \begin{macrocode}
967 \cs_new_protected:Npn \@@_execute_if_arg_agree:nnT #1#2#3
968 {
969 \prop_get:NnN \g_@@_type_prop {#1} \l_@@_tmp_tl
970 \int_compare:nNnTF {#2} = \l_@@_tmp_tl
971 {#3}
972 {
973 \msg_error:nneee { template } { argument-number-mismatch }
974 {#1} { \l_@@_tmp_tl } {#2}
975 }
976 }
977 % \end{macrocode}
978 % \end{macro}
979 %
980 % \begin{macro}{\@@_execute_if_code_exist:nnT}
981 % A template is only fully declared if the code has been set up,
982 % which can be checked by looking for the template function itself.
983 % \begin{macrocode}
984 \cs_new_protected:Npn \@@_execute_if_code_exist:nnT #1#2#3
985 {
986 \cs_if_exist:cTF { \c_@@_code_root_tl #1 / #2 }
987 {#3}
988 { \msg_error:nnnn { template } { no-template-code } {#1} {#2} }
989 }
990 % \end{macrocode}
991 % \end{macro}
992 %
993 % \begin{macro}
994 % {\@@_execute_if_keytype_exist:nT, \@@_execute_if_keytype_exist:VT}
995 % The test for valid keytypes looks for a function to set up the key,
996 % which is part of the \enquote{code} side of the template definition.
997 % This avoids having different lists for the two parts of the process.
998 % \begin{macrocode}
999 \cs_new_protected:Npn \@@_execute_if_keytype_exist:nT #1#2
1000 {
1001 \cs_if_exist:cTF { @@_store_value_ #1 :n }
1002 {#2}
1003 { \msg_error:nnn { template } { unknown-keytype } {#1} }
1004 }
1005 \cs_generate_variant:Nn \@@_execute_if_keytype_exist:nT { V }
1006 % \end{macrocode}
1007 % \end{macro}
1008 %
1009 % \begin{macro}{\@@_execute_if_type_exist:nT}
1010 % To check that a particular type is valid.
1011 % \begin{macrocode}
1012 \cs_new_protected:Npn \@@_execute_if_type_exist:nT #1#2
1013 {
1014 \prop_if_in:NnTF \g_@@_type_prop {#1}
1015 {#2}
1016 { \msg_error:nnn { template } { unknown-type } {#1} }
1017 }
1018 % \end{macrocode}
1019 % \end{macro}
1020 %
1021 % \begin{macro}{\@@_execute_if_keys_exist:nnT}
1022 % To check that the keys for a template have been set up before trying
1023 % to create any code, a simple check for the correctly-named keytype
1024 % property list.
1025 % \begin{macrocode}
1026 \cs_new_protected:Npn \@@_if_keys_exist:nnT #1#2#3
1027 {
1028 \cs_if_exist:cTF { \c_@@_keytypes_root_tl #1 / #2 }
1029 {#3}
1030 { \msg_error:nnnn { template } { unknown-template } {#1} {#2} }
1031 }
1032 % \end{macrocode}
1033 % \end{macro}
1034 %
1035 % \begin{macro}[TF]{\@@_if_key_value:n, \@@_if_key_value:V}
1036 % Tests for the first token in a string being \cs{KeyValue}.
1037 % \begin{macrocode}
1038 \prg_new_conditional:Npnn \@@_if_key_value:n #1 { T , F , TF }
1039 {
1040 \str_if_eq:noTF { \KeyValue } { \tl_head:w #1 \q_nil \q_stop }
1041 \prg_return_true:
1042 \prg_return_false:
1043 }
1044 \prg_generate_conditional_variant:Nnn \@@_if_key_value:n { V } { T , F , TF }
1045 % \end{macrocode}
1046 % \end{macro}
1047 %
1048 % \begin{macro}[TF]{\@@_if_instance_exist:nn}
1049 % Testing for an instance
1050 % \begin{macrocode}
1051 \prg_new_conditional:Npnn \@@_if_instance_exist:nn #1#2 { T, F, TF }
1052 {
1053 \cs_if_exist:cTF { \c_@@_instances_root_tl #1 / #2 }
1054 \prg_return_true:
1055 \prg_return_false:
1056 }
1057 % \end{macrocode}
1058 % \end{macro}
1059 %
1060 % \begin{macro}{\@@_if_use_template:nTF}
1061 % Tests for the first token in a string being \cs{UseTemplate}.
1062 % \begin{macrocode}
1063 \prg_new_conditional:Npnn \@@_if_use_template:n #1 { TF }
1064 {
1065 \str_if_eq:noTF { \UseTemplate } { \tl_head:w #1 \q_nil \q_stop }
1066 \prg_return_true:
1067 \prg_return_false:
1068 }
1069 % \end{macrocode}
1070 % \end{macro}
1071 %
1072 % \subsection{Saving and recovering property lists}
1073 %
1074 % The various property lists for templates have to be shuffled in
1075 % and out of storage.
1076 %
1077 % \begin{macro}
1078 % {
1079 % \@@_store_defaults:nn ,
1080 % \@@_store_keytypes:nn ,
1081 % \@@_store_values:nn ,
1082 % \@@_store_vars:nn
1083 % }
1084 % The defaults and keytypes are transferred from the scratch property
1085 % lists to the \enquote{proper} lists for the template being created.
1086 % \begin{macrocode}
1087 \cs_new_protected:Npn \@@_store_defaults:nn #1#2
1088 {
1089 \debug_suspend:
1090 \prop_gclear_new:c { \c_@@_defaults_root_tl #1 / #2 }
1091 \prop_gset_eq:cN { \c_@@_defaults_root_tl #1 / #2 }
1092 \l_@@_values_prop
1093 \debug_resume:
1094 }
1095 \cs_new_protected:Npn \@@_store_keytypes:nn #1#2
1096 {
1097 \debug_suspend:
1098 \prop_if_exist:cTF { \c_@@_keytypes_root_tl #1 / #2 }
1099 {
1100 \msg_info:nnnn { template } { declare-template-interface } {#1} {#2}
1101 \prop_gclear:c { \c_@@_keytypes_root_tl #1 / #2 }
1102 }
1103 { \prop_new:c { \c_@@_keytypes_root_tl #1 / #2 } }
1104 \prop_gset_eq:cN { \c_@@_keytypes_root_tl #1 / #2 }
1105 \l_@@_keytypes_prop
1106 \seq_gclear_new:c { \c_@@_key_order_root_tl #1 / #2 }
1107 \seq_gset_eq:cN { \c_@@_key_order_root_tl #1 / #2 }
1108 \l_@@_key_order_seq
1109 \debug_resume:
1110 }
1111 \cs_new_protected:Npn \@@_store_values:nn #1#2
1112 {
1113 \debug_suspend:
1114 \prop_clear_new:c { \c_@@_values_root_tl #1 / #2 }
1115 \prop_set_eq:cN { \c_@@_values_root_tl #1 / #2 }
1116 \l_@@_values_prop
1117 \debug_resume:
1118 }
1119 \cs_new_protected:Npn \@@_store_vars:nn #1#2
1120 {
1121 \debug_suspend:
1122 \prop_gclear_new:c { \c_@@_vars_root_tl #1 / #2 }
1123 \prop_gset_eq:cN { \c_@@_vars_root_tl #1 / #2 }
1124 \l_@@_vars_prop
1125 \debug_resume:
1126 }
1127 % \end{macrocode}
1128 % \end{macro}
1129 %
1130 % \begin{macro}
1131 % {
1132 % \@@_recover_defaults:nn ,
1133 % \@@_recover_keytypes:nn ,
1134 % \@@_recover_values:nn ,
1135 % \@@_recover_vars:nn
1136 % }
1137 % Recovering the stored data for a template is rather less complex
1138 % than storing it. All that happens is the data is transferred from
1139 % the permanent to the scratch storage. However, we need to check the
1140 % scratch storage does exist.
1141 % \begin{macrocode}
1142 \cs_new_protected:Npn \@@_recover_defaults:nn #1#2
1143 {
1144 \prop_if_exist:cTF
1145 { \c_@@_defaults_root_tl #1 / #2 }
1146 {
1147 \prop_set_eq:Nc \l_@@_values_prop
1148 { \c_@@_defaults_root_tl #1 / #2 }
1149 }
1150 { \prop_clear:N \l_@@_values_prop }
1151 }
1152 \cs_new_protected:Npn \@@_recover_keytypes:nn #1#2
1153 {
1154 \prop_if_exist:cTF
1155 { \c_@@_keytypes_root_tl #1 / #2 }
1156 {
1157 \prop_set_eq:Nc \l_@@_keytypes_prop
1158 { \c_@@_keytypes_root_tl #1 / #2 }
1159 }
1160 { \prop_clear:N \l_@@_keytypes_prop }
1161 \seq_if_exist:cTF { \c_@@_key_order_root_tl #1 / #2 }
1162 {
1163 \seq_set_eq:Nc \l_@@_key_order_seq
1164 { \c_@@_key_order_root_tl #1 / #2 }
1165 }
1166 { \seq_clear:N \l_@@_key_order_seq }
1167 }
1168 \cs_new_protected:Npn \@@_recover_values:nn #1#2
1169 {
1170 \prop_if_exist:cTF
1171 { \c_@@_values_root_tl #1 / #2 }
1172 {
1173 \prop_set_eq:Nc \l_@@_values_prop
1174 { \c_@@_values_root_tl #1 / #2 }
1175 }
1176 { \prop_clear:N \l_@@_values_prop }
1177 }
1178 \cs_new_protected:Npn \@@_recover_vars:nn #1#2
1179 {
1180 \prop_if_exist:cTF
1181 { \c_@@_vars_root_tl #1 / #2 }
1182 {
1183 \prop_set_eq:Nc \l_@@_vars_prop
1184 { \c_@@_vars_root_tl #1 / #2 }
1185 }
1186 { \prop_clear:N \l_@@_vars_prop }
1187 }
1188 % \end{macrocode}
1189 % \end{macro}
1190 %
1191 % \subsection{Creating new template types}
1192 %
1193 % \begin{macro}{\@@_define_type:nn, \@@_declare_type:nn}
1194 % Although the type is the \enquote{top level} of the template
1195 % system, it is actually very easy to implement. All that happens is that
1196 % the number of arguments required is recorded, indexed by the name of the
1197 % type.
1198 % \begin{macrocode}
1199 \cs_new_protected:Npn \@@_define_type:nn #1#2
1200 {
1201 \prop_if_in:NnTF \g_@@_type_prop {#1}
1202 { \msg_error:nnn { template } { type-already-defined } {#1} }
1203 { \@@_declare_type:nn {#1} {#2} }
1204 }
1205 \cs_new_protected:Npn \@@_declare_type:nn #1#2
1206 {
1207 \int_set:Nn \l_@@_tmp_int {#2}
1208 \int_compare:nTF { 0 <= \l_@@_tmp_int <= 9 }
1209 {
1210 \msg_info:nnnV { template } { declare-type }
1211 {#1} \l_@@_tmp_int
1212 \prop_gput:NnV \g_@@_type_prop {#1}
1213 \l_@@_tmp_int
1214 }
1215 {
1216 \msg_error:nnnV { template } { bad-number-of-arguments }
1217 {#1} \l_@@_tmp_int
1218 }
1219 }
1220 % \end{macrocode}
1221 % \end{macro}
1222 %
1223 % \subsection{Design part of template declaration}
1224 %
1225 % The \enquote{design} part of a template declaration defines the general
1226 % behaviour of each key, and possibly a default value. However, it does
1227 % not include the implementation. This means that what happens here is
1228 % the two properties are saved to appropriate lists, which can then
1229 % be used later to recover the information when implementing the keys.
1230 %
1231 % \begin{macro}{\@@_declare_template_keys:nnnn}
1232 % The main function for the \enquote{design} part of creating a template
1233 % starts by checking that the type exists and that the number of
1234 % arguments required agree. If that is all fine, then the two storage
1235 % areas for defaults and keytypes are initialised. The mechanism is then
1236 % set up for the \pkg{l3keys} module to actually parse the keys.
1237 % Finally, the code hands of to the storage routine to save the parsed
1238 % information properly.
1239 % \begin{macrocode}
1240 \cs_new_protected:Npn \@@_declare_template_keys:nnnn #1#2#3#4
1241 {
1242 \@@_execute_if_type_exist:nT {#1}
1243 {
1244 \@@_execute_if_arg_agree:nnT {#1} {#3}
1245 {
1246 \prop_clear:N \l_@@_values_prop
1247 \prop_clear:N \l_@@_keytypes_prop
1248 \seq_clear:N \l_@@_key_order_seq
1249 \keyval_parse:NNn
1250 \@@_parse_keys_elt:n \@@_parse_keys_elt:nn {#4}
1251 \@@_store_defaults:nn {#1} {#2}
1252 \@@_store_keytypes:nn {#1} {#2}
1253 }
1254 }
1255 }
1256 % \end{macrocode}
1257 % \end{macro}
1258 %
1259 % \begin{macro}{\@@_parse_keys_elt:n}
1260 % \begin{macro}{\@@_parse_keys_elt_aux:n}
1261 % \begin{macro}{\@@_parse_keys_elt_aux:}
1262 % Processing the key part of the key--value pair is always carried out
1263 % using this function, even if a value was found. First, the key name
1264 % is separated from the keytype, and if necessary the keytype is
1265 % separated into two parts. This information is then used to check that
1266 % the keytype is valid, before storing the keytype (plus argument if
1267 % necessary) as a property of the key name. The key name is also stored
1268 % (in braces) in the token list to record the order the keys are defined
1269 % in.
1270 % \begin{macrocode}
1271 \cs_new_protected:Npn \@@_parse_keys_elt:n #1
1272 {
1273 \@@_split_keytype:n {#1}
1274 \bool_if:NF \l_@@_error_bool
1275 {
1276 \@@_execute_if_keytype_exist:VT \l_@@_keytype_tl
1277 {
1278 \seq_map_function:NN \c_@@_keytypes_arg_seq
1279 \@@_parse_keys_elt_aux:n
1280 \bool_if:NF \l_@@_error_bool
1281 {
1282 \seq_if_in:NoTF \l_@@_key_order_seq
1283 \l_@@_key_name_tl
1284 {
1285 \msg_error:nnV { template } { duplicate-key-interface }
1286 \l_@@_key_name_tl
1287 }
1288 { \@@_parse_keys_elt_aux: }
1289 }
1290 }
1291 }
1292 }
1293 \cs_new_protected:Npn \@@_parse_keys_elt_aux:n #1
1294 {
1295 \str_if_eq:VnT \l_@@_keytype_tl {#1}
1296 {
1297 \tl_if_empty:NT \l_@@_keytype_arg_tl
1298 {
1299 \msg_error:nnn { template } { keytype-requires-argument } {#1}
1300 \bool_set_true:N \l_@@_error_bool
1301 \seq_map_break:
1302 }
1303 }
1304 }
1305 \cs_new_protected:Npn \@@_parse_keys_elt_aux:
1306 {
1307 \tl_set:Ne \l_@@_tmp_tl
1308 {
1309 \l_@@_keytype_tl
1310 \tl_if_empty:NF \l_@@_keytype_arg_tl
1311 { { \l_@@_keytype_arg_tl } }
1312 }
1313 \prop_put:NVV \l_@@_keytypes_prop \l_@@_key_name_tl
1314 \l_@@_tmp_tl
1315 \seq_put_right:NV \l_@@_key_order_seq \l_@@_key_name_tl
1316 \str_if_eq:VnT \l_@@_keytype_tl { choice }
1317 {
1318 \clist_if_in:NnT \l_@@_keytype_arg_tl { unknown }
1319 { \msg_error:nn { template } { choice-unknown-reserved } }
1320 }
1321 }
1322 % \end{macrocode}
1323 % \end{macro}
1324 % \end{macro}
1325 % \end{macro}
1326 %
1327 % \begin{macro}{\@@_parse_keys_elt:nn}
1328 % For keys which have a default, the keytype and key name are first
1329 % separated out by the \cs{@@_parse_keys_elt:n}
1330 % routine, before storing the default value in the scratch property list.
1331 % \begin{macrocode}
1332 \cs_new_protected:Npn \@@_parse_keys_elt:nn #1#2
1333 {
1334 \@@_parse_keys_elt:n {#1}
1335 \use:c { @@_store_value_ \l_@@_keytype_tl :n } {#2}
1336 }
1337 % \end{macrocode}
1338 % \end{macro}
1339 %
1340 % \begin{macro}{\@@_split_keytype:n}
1341 % \begin{macro}{\@@_split_keytype_aux:w}
1342 % The keytype and key name should be separated by |:|. As the
1343 % definition might be given inside or outside of a code block,
1344 % the category code of colons is standardised. After
1345 % that, the standard delimited argument method is used to separate the
1346 % two parts.
1347 % \begin{macrocode}
1348 \cs_new_protected:Npe \@@_split_keytype:n #1
1349 {
1350 \exp_not:N \bool_set_false:N \exp_not:N \l_@@_error_bool
1351 \tl_set:Nn \exp_not:N \l_@@_tmp_tl {#1}
1352 \tl_replace_all:Nnn \exp_not:N \l_@@_tmp_tl { : } { \token_to_str:N : }
1353 \tl_if_in:VnTF \exp_not:N \l_@@_tmp_tl { \token_to_str:N : }
1354 {
1355 \exp_not:n
1356 {
1357 \tl_clear:N \l_@@_key_name_tl
1358 \exp_after:wN \@@_split_keytype_aux:w
1359 \l_@@_tmp_tl \s_@@_stop
1360 }
1361 }
1362 {
1363 \exp_not:N \bool_set_true:N \exp_not:N \l_@@_error_bool
1364 \msg_error:nnn { template } { missing-keytype } {#1}
1365 }
1366 }
1367 \use:e
1368 {
1369 \cs_new_protected:Npn \exp_not:N \@@_split_keytype_aux:w
1370 #1 \token_to_str:N : #2 \s_@@_stop
1371 {
1372 \tl_put_right:Ne \exp_not:N \l_@@_key_name_tl
1373 {
1374 \exp_not:N \tl_trim_spaces:e
1375 { \exp_not:N \tl_to_str:n {#1} }
1376 }
1377 \tl_if_in:nnTF {#2} { \token_to_str:N : }
1378 {
1379 \tl_put_right:Nn \exp_not:N \l_@@_key_name_tl
1380 { \token_to_str:N : }
1381 \exp_not:N \@@_split_keytype_aux:w #2 \s_@@_stop
1382 }
1383 {
1384 \exp_not:N \tl_if_empty:NTF \exp_not:N \l_@@_key_name_tl
1385 {
1386 \msg_error:nnn { template } { empty-key-name }
1387 { \token_to_str:N : #2 }
1388 }
1389 { \exp_not:N \@@_split_keytype_arg:n {#2} }
1390 }
1391 }
1392 }
1393 % \end{macrocode}
1394 % \end{macro}
1395 % \end{macro}
1396 %
1397 % \begin{macro}{\@@_split_keytype_arg:n, \@@_split_keytype_arg:V}
1398 % \begin{macro}{\@@_split_keytype_arg_aux:n}
1399 % \begin{macro}{\@@_split_keytype_arg_aux:w}
1400 % The second stage of sorting out the keytype is to check for an
1401 % argument. As there is no convenient delimiting token to look for,
1402 % a check is made instead for each possible text value for the keytype.
1403 % To keep things faster, this only involves the keytypes that need an
1404 % argument. If a match is made, then a check is also needed to see that
1405 % it is at the start of the keytype information. All being well, the
1406 % split can then be applied. Any non-matching keytypes are assumed to
1407 % be \enquote{correct} as given, and are left alone (this is checked by
1408 % other code).
1409 % \begin{macrocode}
1410 \cs_new_protected:Npn \@@_split_keytype_arg:n #1
1411 {
1412 \tl_set:Ne \l_@@_keytype_tl { \tl_trim_spaces:n {#1} }
1413 \tl_clear:N \l_@@_keytype_arg_tl
1414 \cs_set_protected:Npn \@@_split_keytype_arg_aux:n ##1
1415 {
1416 \tl_if_in:nnT {#1} {##1}
1417 {
1418 \cs_set:Npn \@@_split_keytype_arg_aux:w
1419 ####1 ##1 ####2 \s_@@_stop
1420 {
1421 \tl_if_blank:nT {####1}
1422 {
1423 \tl_set:Ne \l_@@_keytype_tl
1424 { \tl_trim_spaces:n {##1} }
1425 \tl_if_blank:nF {####2}
1426 {
1427 \tl_set:Ne \l_@@_keytype_arg_tl
1428 { \use:n ####2 }
1429 }
1430 \seq_map_break:
1431 }
1432 }
1433 \@@_split_keytype_arg_aux:w #1 \s_@@_stop
1434 }
1435 }
1436 \seq_map_function:NN \c_@@_keytypes_arg_seq
1437 \@@_split_keytype_arg_aux:n
1438 }
1439 \cs_generate_variant:Nn \@@_split_keytype_arg:n { V }
1440 \cs_new:Npn \@@_split_keytype_arg_aux:n #1 { }
1441 \cs_new:Npn \@@_split_keytype_arg_aux:w #1 \s_@@_stop { }
1442 % \end{macrocode}
1443 % \end{macro}
1444 % \end{macro}
1445 % \end{macro}
1446 %
1447 % \subsubsection{Storing values}
1448 %
1449 % As \pkg{lttemplates} pre-processes key values for efficiency reasons,
1450 % there is a need to convert the values given as defaults into
1451 % \enquote{ready to use} data. The same general idea is true when an instance
1452 % is declared. However, assignments are not made until an instance is
1453 % used, and so there has to be some intermediate storage. Furthermore,
1454 % the ability to delay evaluation of results is needed. To achieve these
1455 % aims, a series of \enquote{process and store} functions are defined here.
1456 %
1457 % All of the information about the key (the key name and the keytype)
1458 % is already stored as variables. The same property list is always used
1459 % to store the data, meaning that the only argument required is the
1460 % value to be processed and potentially stored.
1461 %
1462 % \begin{macro}{\@@_store_value_boolean:n}
1463 % \begin{macrocode}
1464 \cs_new_protected:Npn \@@_store_value_boolean:n #1
1465 { \prop_put:Non \l_@@_values_prop \l_@@_key_name_tl {#1} }
1466 % \end{macrocode}
1467 % \end{macro}
1468 %
1469 % \begin{macro}{\@@_store_value:n, \@@_store_value_choice:n,
1470 % \@@_store_value_function:n, \@@_store_value_instance:n}
1471 % With no need to worry about delayed evaluation, these keytypes all
1472 % just store the input directly.
1473 % \begin{macrocode}
1474 \cs_new_protected:Npn \@@_store_value:n #1
1475 { \prop_put:Non \l_@@_values_prop \l_@@_key_name_tl {#1} }
1476 \cs_new_eq:NN \@@_store_value_choice:n \@@_store_value:n
1477 \cs_new_eq:NN \@@_store_value_function:n \@@_store_value:n
1478 \cs_new_eq:NN \@@_store_value_instance:n \@@_store_value:n
1479 % \end{macrocode}
1480 % \end{macro}
1481 %
1482 % \begin{macro}{\@@_store_value_aux:Nn, \@@_store_value_integer:n,
1483 % \@@_store_value_length:n, \@@_store_value_muskip:n,
1484 % \@@_store_value_real:n, \@@_store_value_skip:n,
1485 % \@@_store_value_tokenlist:n, \@@_store_value_commalist:n}
1486 % Storing values in \cs{l_@@_values_prop} is in most cases the same.
1487 % \begin{macrocode}
1488 \cs_new_protected:Npn \@@_store_value_aux:Nn #1#2
1489 { \prop_put:Non \l_@@_values_prop \l_@@_key_name_tl {#2} }
1490 \cs_new_protected:Npn \@@_store_value_integer:n
1491 { \@@_store_value_aux:Nn \int_eval:n }
1492 \cs_new_protected:Npn \@@_store_value_length:n
1493 { \@@_store_value_aux:Nn \dim_eval:n }
1494 \cs_new_protected:Npn \@@_store_value_muskip:n
1495 { \@@_store_value_aux:Nn \muskip_eval:n }
1496 \cs_new_protected:Npn \@@_store_value_real:n
1497 { \@@_store_value_aux:Nn \fp_eval:n }
1498 \cs_new_protected:Npn \@@_store_value_skip:n
1499 { \@@_store_value_aux:Nn \skip_eval:n }
1500 \cs_new_protected:Npn \@@_store_value_tokenlist:n
1501 { \@@_store_value_aux:Nn \use:n }
1502 \cs_new_eq:NN \@@_store_value_commalist:n \@@_store_value_tokenlist:n
1503 % \end{macrocode}
1504 % \end{macro}
1505 %
1506 % \subsection{Implementation part of template declaration}
1507 %
1508 % \begin{macro}{\@@_declare_template_code:nnnnn}
1509 % \changes{2025-01-20}{v1.0e}{Speed up test for \cs{AssignTemplateKeys}}
1510 % \begin{macro}{\@@_declare_template_code:nnnn}
1511 % The main function for implementing a template starts with a couple of
1512 % simple checks to make sure that there are no obvious mistakes: the
1513 % number of arguments must agree and the template keys must have been
1514 % declared.
1515 % \begin{macrocode}
1516 \cs_new_protected:Npn \@@_declare_template_code:nnnnn #1#2#3#4#5
1517 {
1518 \@@_execute_if_type_exist:nT {#1}
1519 {
1520 \@@_execute_if_arg_agree:nnT {#1} {#3}
1521 {
1522 \@@_if_keys_exist:nnT {#1} {#2}
1523 {
1524 \@@_store_key_implementation:nnn {#1} {#2} {#4}
1525 \str_if_in:nnTF {#5} { AssignTemplateKeys }
1526 {
1527 \regex_match:nnTF { \c { AssignTemplateKeys } } {#5}
1528 { \@@_declare_template_code:nnnn {#1} {#2} {#3} {#5} }
1529 {
1530 \@@_declare_template_code:nnnn
1531 {#1} {#2} {#3} { \AssignTemplateKeys #5 }
1532 }
1533 }
1534 {
1535 \@@_declare_template_code:nnnn
1536 {#1} {#2} {#3} { \AssignTemplateKeys #5 }
1537 }
1538 }
1539 }
1540 }
1541 }
1542 \cs_new_protected:Npn \@@_declare_template_code:nnnn #1#2#3#4
1543 {
1544 \cs_if_exist:cT { \c_@@_code_root_tl #1 / #2 }
1545 { \msg_info:nnnn { template } { declare-template-code } {#1} {#2} }
1546 \cs_generate_from_arg_count:cNnn
1547 { \c_@@_code_root_tl #1 / #2 }
1548 \cs_gset_protected:Npn {#3} {#4}
1549 }
1550 % \end{macrocode}
1551 % \end{macro}
1552 % \end{macro}
1553 %
1554 % \begin{macro}{\@@_store_key_implementation:nnn}
1555 % Actually storing the implementation part of a template is quite easy
1556 % as it only requires the list of keys given to be turned into a
1557 % property list. There is also some error-checking to do, hence the need
1558 % to have the list of defined keytypes available. In certain cases
1559 % (when choices are involved) parsing the key results in changes to the
1560 % default values. That is why they are loaded and then saved again.
1561 % \begin{macrocode}
1562 \cs_new_protected:Npn \@@_store_key_implementation:nnn #1#2#3
1563 {
1564 \@@_recover_defaults:nn {#1} {#2}
1565 \@@_recover_keytypes:nn {#1} {#2}
1566 \prop_clear:N \l_@@_vars_prop
1567 \keyval_parse:nnn
1568 { \@@_parse_vars_elt:n } { \@@_parse_vars_elt:nnn { #1 / #2 } } {#3}
1569 \@@_store_vars:nn {#1} {#2}
1570 \prop_map_inline:Nn \l_@@_keytypes_prop
1571 { \msg_error:nnnnn { template } { key-not-implemented } {##1} {#2} {#1} }
1572 }
1573 % \end{macrocode}
1574 % \end{macro}
1575 %
1576 % \begin{macro}{\@@_parse_vars_elt:n}
1577 % At the implementation stage, every key must have a value given. So
1578 % this is an error function.
1579 % \begin{macrocode}
1580 \cs_new_protected:Npn \@@_parse_vars_elt:n #1
1581 { \msg_error:nnn { template } { key-no-variable } {#1} }
1582 % \end{macrocode}
1583 % \end{macro}
1584 %
1585 % \begin{macro}{\@@_parse_vars_elt:nnn}
1586 % \begin{macro}{\@@_parse_vars_elt_aux:nn}
1587 % \begin{macro}{\@@_parse_vars_elt_aux:nw}
1588 % \begin{macro}{\@@_parse_vars_elt_aux:nnn, \@@_parse_vars_elt_aux:nne}
1589 % \begin{macro}{\@@_parse_vars_elt_key:nn}
1590 % The actual storage part here is very simple: the storage bin name
1591 % is placed into the property list. At the same time, a comparison is
1592 % made with the keytypes defined earlier: if there is a mismatch then
1593 % an error is raised.
1594 % \begin{macrocode}
1595 \cs_new_protected:Npn \@@_parse_vars_elt:nnn #1#2#3
1596 {
1597 \tl_set:Ne \l_@@_key_name_tl
1598 { \tl_trim_spaces:e { \tl_to_str:n {#2} } }
1599 \prop_get:NVNTF \l_@@_keytypes_prop
1600 \l_@@_key_name_tl
1601 \l_@@_keytype_tl
1602 {
1603 \@@_split_keytype_arg:V \l_@@_keytype_tl
1604 \@@_parse_vars_elt_aux:nn {#1} {#3}
1605 \prop_remove:NV \l_@@_keytypes_prop \l_@@_key_name_tl
1606 }
1607 { \msg_error:nnn { template } { unknown-key } {#2} }
1608 }
1609 % \end{macrocode}
1610 % Split off any leading \texttt{global} and they look for the way to
1611 % implement.
1612 % \begin{macrocode}
1613 \cs_new_protected:Npn \@@_parse_vars_elt_aux:nn #1#2
1614 {
1615 \@@_parse_vars_elt_aux:nw {#1} #2 global global \s_@@_stop
1616 }
1617 \cs_new_protected:Npn \@@_parse_vars_elt_aux:nw
1618 #1#2 global #3 global #4 \s_@@_stop
1619 {
1620 \tl_if_blank:nTF {#4}
1621 { \@@_parse_vars_elt_aux:nnn {#1} { } {#2} }
1622 {
1623 \tl_if_blank:nTF {#2}
1624 {
1625 \@@_parse_vars_elt_aux:nne
1626 {#1} { global } { \tl_trim_spaces:n {#3} }
1627 }
1628 { \msg_error:nnn { template } { bad-variable } { #2 global #3 } }
1629 }
1630 }
1631 \cs_new_protected:Npn \@@_parse_vars_elt_aux:nnn #1#2#3
1632 {
1633 \str_case:VnF \l_@@_keytype_tl
1634 {
1635 { choice } { \@@_implement_choices:nn {#1} {#3} }
1636 { function }
1637 {
1638 \cs_if_exist:NF #3
1639 { \cs_new:Npn #3 { } }
1640 \@@_parse_vars_elt_key:nn {#1}
1641 {
1642 .code:n =
1643 {
1644 \cs_generate_from_arg_count:NNnn
1645 \exp_not:N #3
1646 \exp_not:c
1647 { cs_ \str_if_eq:nnT {#1} { global } { g } set:Npn }
1648 { \exp_not:V \l_@@_keytype_arg_tl }
1649 {##1}
1650 }
1651 }
1652 \prop_put:NVn \l_@@_vars_prop
1653 \l_@@_key_name_tl {#2#3}
1654 }
1655 { instance }
1656 {
1657 \@@_parse_vars_elt_key:nn {#1}
1658 {
1659 .code:n =
1660 {
1661 \exp_not:c
1662 { cs_ \str_if_eq:nnT {#1} { global } { g } set:Npn }
1663 \exp_not:N #3 { \UseInstance {##1} }
1664 }
1665 }
1666 \prop_put:NVn \l_@@_vars_prop
1667 \l_@@_key_name_tl {#2#3}
1668 }
1669 }
1670 {
1671 \tl_if_single:nTF {#3}
1672 {
1673 \cs_if_exist:NF #3
1674 { \use:c { \@@_map_var_type: _new:N } #3 }
1675 \@@_parse_vars_elt_key:nn {#1}
1676 {
1677 . \@@_map_var_type:
1678 _ \str_if_eq:nnT {#1} { global } { g } set:N
1679 = \exp_not:N #3
1680 }
1681 \prop_put:NVn \l_@@_vars_prop
1682 \l_@@_key_name_tl {#2#3}
1683 }
1684 { \msg_error:nnn { template } { bad-variable } {#2#3} }
1685 }
1686 }
1687 \cs_generate_variant:Nn \@@_parse_vars_elt_aux:nnn { nne }
1688 \cs_new_protected:Npn \@@_parse_vars_elt_key:nn #1#2
1689 {
1690 \keys_define:ne { template / #1 }
1691 { \l_@@_key_name_tl #2 }
1692 }
1693 % \end{macrocode}
1694 % \end{macro}
1695 % \end{macro}
1696 % \end{macro}
1697 % \end{macro}
1698 % \end{macro}
1699 %
1700 % \begin{macro}[EXP]{\@@_map_var_type:}
1701 % Turn a \enquote{friendly} variable type into an \texttt{expl3} one.
1702 % \begin{macrocode}
1703 \cs_new:Npn \@@_map_var_type:
1704 {
1705 \str_case:Vn \l_@@_keytype_tl
1706 {
1707 { boolean } { bool }
1708 { commalist } { clist }
1709 { integer } { int }
1710 { length } { dim }
1711 { muskip } { muskip }
1712 { real } { fp }
1713 { skip } { skip }
1714 { tokenlist } { tl }
1715 }
1716 }
1717 % \end{macrocode}
1718 % \end{macro}
1719 %
1720 % \begin{macro}{\@@_implement_choices:nn}
1721 % \begin{macro}{\@@_implement_choices_default:}
1722 % Implementing choices requires a second key--value loop. So after a
1723 % little set-up, the standard parser is called.
1724 % \begin{macrocode}
1725 \cs_new_protected:Npn \@@_implement_choices:nn #1#2
1726 {
1727 \clist_set:NV \l_@@_tmp_clist \l_@@_keytype_arg_tl
1728 \prop_put:NVn \l_@@_vars_prop \l_@@_key_name_tl { }
1729 \keys_define:ne { template / #1 } { \l_@@_key_name_tl .choice: }
1730 \keyval_parse:nnn
1731 { \@@_implement_choice_elt:n }
1732 { \@@_implement_choice_elt:nnn {#1} }
1733 {#2}
1734 \prop_get:NVNT \l_@@_values_prop \l_@@_key_name_tl
1735 \l_@@_tmp_tl
1736 { \@@_implement_choices_default: }
1737 \clist_if_empty:NF \l_@@_tmp_clist
1738 {
1739 \clist_map_inline:Nn \l_@@_tmp_clist
1740 { \msg_error:nnn { template } { choice-not-implemented } {##1} }
1741 }
1742 }
1743 % \end{macrocode}
1744 % A sanity check for the default value, so that an error is raised
1745 % now and not when converting to assignments.
1746 % \begin{macrocode}
1747 \cs_new_protected:Npn \@@_implement_choices_default:
1748 {
1749 \tl_set:Ne \l_@@_tmp_tl
1750 { \l_@@_key_name_tl \c_space_tl \l_@@_tmp_tl }
1751 \prop_if_in:NVF \l_@@_vars_prop \l_@@_tmp_tl
1752 {
1753 \tl_set:Ne \l_@@_tmp_tl
1754 { \l_@@_key_name_tl \c_space_tl \l_@@_tmp_tl }
1755 \prop_if_in:NVF \l_@@_vars_prop \l_@@_tmp_tl
1756 {
1757 \prop_get:NVN \l_@@_keytypes_prop \l_@@_key_name_tl
1758 \l_@@_tmp_tl
1759 \@@_split_keytype_arg:V \l_@@_tmp_tl
1760 \prop_get:NVN \l_@@_values_prop \l_@@_key_name_tl
1761 \l_@@_tmp_tl
1762 \msg_error:nnVV { template } { unknown-default-choice }
1763 \l_@@_key_name_tl
1764 \l_@@_key_name_tl
1765 }
1766 }
1767 }
1768 % \end{macrocode}
1769 % \end{macro}
1770 % \end{macro}
1771 %
1772 % \begin{macro}{\@@_implement_choice_elt:nnn, \@@_implement_choice_elt_aux:nnn}
1773 % \begin{macro}{\@@_implement_choice_elt_aux:n}
1774 % \begin{macro}{\@@_implement_choice_elt:n}
1775 % The actual storage of the implementation of a choice is mainly about
1776 % error checking. The code here ensures that all choices have to have
1777 % been declared, apart from the special \texttt{unknown} choice, which
1778 % must come last. The code for each choice is stored along with the
1779 % key name in the variables property list.
1780 % \begin{macrocode}
1781 \cs_new_protected:Npn \@@_implement_choice_elt:nnn #1#2#3
1782 {
1783 \clist_if_empty:NTF \l_@@_tmp_clist
1784 {
1785 \str_if_eq:nnTF {#2} { unknown }
1786 { \@@_implement_choice_elt_aux:nnn {#1} {#2} {#3} }
1787 { \@@_implement_choice_elt_aux:n {#2} }
1788 }
1789 {
1790 \clist_if_in:NnTF \l_@@_tmp_clist {#2}
1791 {
1792 \clist_remove_all:Nn \l_@@_tmp_clist {#2}
1793 \@@_implement_choice_elt_aux:nnn {#1} {#2} {#3}
1794 }
1795 { \@@_implement_choice_elt_aux:n {#2} }
1796 }
1797 }
1798 \cs_new_protected:Npn \@@_implement_choice_elt_aux:n #1
1799 {
1800 \prop_get:NVN \l_@@_keytypes_prop \l_@@_key_name_tl
1801 \l_@@_tmp_tl
1802 \@@_split_keytype_arg:V \l_@@_tmp_tl
1803 \msg_error:nnVn { template } { unknown-choice } \l_@@_key_name_tl {#1}
1804 }
1805 \cs_new_protected:Npn \@@_implement_choice_elt_aux:nnn #1#2#3
1806 {
1807 \keys_define:ne { template / #1 }
1808 { \l_@@_key_name_tl / #2 .code:n = { \exp_not:n {#3} } }
1809 \tl_set:Ne \l_@@_tmp_tl
1810 { \l_@@_key_name_tl \c_space_tl #2 }
1811 \prop_put:NVn \l_@@_vars_prop \l_@@_tmp_tl {#3}
1812 }
1813 \cs_new_protected:Npn \@@_implement_choice_elt:n #1
1814 {
1815 \msg_error:nnVn { template } { choice-requires-code }
1816 \l_@@_key_name_tl {#1}
1817 }
1818 % \end{macrocode}
1819 % \end{macro}
1820 % \end{macro}
1821 % \end{macro}
1822 %
1823 % \subsection{Editing template defaults}
1824 %
1825 % \begin{macro}{\@@_edit_defaults:nnn}
1826 % Editing the template defaults means getting the values back out
1827 % of the store, then parsing the list of new values before putting
1828 % the updated list back into storage.
1829 % \begin{macrocode}
1830 \cs_new_protected:Npn \@@_edit_defaults:nnn #1#2#3
1831 {
1832 \@@_if_keys_exist:nnT {#1} {#2}
1833 {
1834 \@@_recover_defaults:nn {#1} {#2}
1835 \@@_parse_values:nnn {#1} {#2} {#3}
1836 \@@_store_defaults:nn {#1} {#2}
1837 }
1838 }
1839 % \end{macrocode}
1840 % \end{macro}
1841 %
1842 % \begin{macro}{\@@_parse_values:nnn}
1843 % The routine to parse values is the same for both editing a
1844 % template and setting up an instance. So the code here does only the
1845 % minimum necessary for reading the values.
1846 % \begin{macrocode}
1847 \cs_new_protected:Npn \@@_parse_values:nnn #1#2#3
1848 {
1849 \@@_recover_keytypes:nn {#1} {#2}
1850 \keyval_parse:NNn
1851 \@@_parse_values_elt:n \@@_parse_values_elt:nn {#3}
1852 }
1853 % \end{macrocode}
1854 % \end{macro}
1855 %
1856 % \begin{macro}{\@@_parse_values_elt:n}
1857 % Every key needs a value, so this is just an error routine.
1858 % \begin{macrocode}
1859 \cs_new_protected:Npn \@@_parse_values_elt:n #1
1860 {
1861 \bool_set_true:N \l_@@_error_bool
1862 \msg_error:nnn { template } { key-no-value } {#1}
1863 }
1864 % \end{macrocode}
1865 % \end{macro}
1866 %
1867 % \begin{macro}{\@@_parse_values_elt:nn}
1868 % \begin{macro}{\@@_parse_values_elt_aux:w}
1869 % \changes{2025-07-08}{v1.0f}{New macro}
1870 % \begin{macro}{\@@_parse_values_elt_aux:n}
1871 % \changes{2025-07-08}{v1.0f}{Allow for value expansion}
1872 % \begin{macro}
1873 % {
1874 % \@@_parse_values_exp:n, \@@_parse_values_exp:o,
1875 % \@@_parse_values_exp:V, \@@_parse_values_exp:v,
1876 % \@@_parse_values_exp:e
1877 % }
1878 % \changes{2025-07-08}{v1.0f}{New macro}
1879 % \begin{macro}{\@@_parse_values_exp:N, \@@_parse_values_exp:c}
1880 % \changes{2025-07-08}{v1.0f}{New macro}
1881 % To store the value, find the keytype then call the saving function.
1882 % These need the current key name, stored in \cs{l_@@_key_name_tl}.
1883 % \begin{macrocode}
1884 \cs_new_protected:Npn \@@_parse_values_elt:nn #1#2
1885 {
1886 \use:e
1887 {
1888 \@@_parse_values_elt_aux:w
1889 \tl_trim_spaces:e { \tl_to_str:n { #1 : n : } }
1890 \exp_not:N \q_stop
1891 }
1892 \prop_get:NVNTF \l_@@_keytypes_prop \l_@@_key_name_tl
1893 \l_@@_tmp_tl
1894 { \@@_parse_values_elt_aux:n {#2} }
1895 { \msg_error:nnV { template } { unknown-key } \l_@@_key_name_tl }
1896 }
1897 \use:e
1898 {
1899 \cs_new_protected:Npn \exp_not:N \@@_parse_values_elt_aux:w
1900 #1 \token_to_str:N : #2 \token_to_str:N : #3 \exp_not:N \q_stop
1901 }
1902 {
1903 \tl_set:Nn \l_@@_key_name_tl {#1}
1904 \str_set:Nn \l_@@_value_exp_str {#2}
1905 }
1906 \cs_new_protected:Npn \@@_parse_values_elt_aux:n #1
1907 {
1908 \@@_split_keytype_arg:V \l_@@_tmp_tl
1909 \cs_if_exist_use:cF { @@_parse_values_exp: \l_@@_value_exp_str }
1910 {
1911 \msg_error:nnV { template } { unknown-expansion } \l_@@_value_exp_str
1912 \use_none:n
1913 }
1914 {#1}
1915 }
1916 \cs_new_protected:Npn \@@_parse_values_exp:n #1
1917 { \use:c { @@_store_value_ \l_@@_keytype_tl :n } {#1} }
1918 \cs_generate_variant:Nn \@@_parse_values_exp:n { o , V , v , e }
1919 \cs_new_eq:NN \@@_parse_values_exp:N \@@_parse_values_exp:n
1920 \cs_generate_variant:Nn \@@_parse_values_exp:N { c }
1921 % \end{macrocode}
1922 % \end{macro}
1923 % \end{macro}
1924 % \end{macro}
1925 % \end{macro}
1926 % \end{macro}
1927 %
1928 % \begin{macro}{\@@_template_set_eq:nnn}
1929 % To copy a template, each of the lists plus the code has to be copied
1930 % across. To keep this independent of the list storage system, it is
1931 % all done with two-part shuffles.
1932 % \begin{macrocode}
1933 \cs_new_protected:Npn \@@_template_set_eq:nnn #1#2#3
1934 {
1935 \@@_recover_defaults:nn {#1} {#3}
1936 \@@_store_defaults:nn {#1} {#2}
1937 \@@_recover_keytypes:nn {#1} {#3}
1938 \@@_store_keytypes:nn {#1} {#2}
1939 \@@_recover_vars:nn {#1} {#3}
1940 \@@_store_vars:nn {#1} {#2}
1941 \cs_if_exist:cT { \c_@@_code_root_tl #1 / #2 }
1942 { \msg_info:nnnn { template } { declare-template-code } {#1} {#2} }
1943 \cs_gset_eq:cc { \c_@@_code_root_tl #1 / #2 }
1944 { \c_@@_code_root_tl #1 / #3 }
1945 }
1946 % \end{macrocode}
1947 % \end{macro}
1948 %
1949 % \subsection{Creating instances of templates}
1950 %
1951 % \begin{macro}
1952 % {
1953 % \@@_declare_instance:nnnn,
1954 % \@@_declare_instance_aux:nnnn
1955 % }
1956 % Making an instance has two distinct parts. First, the keys given are
1957 % parsed to transfer the values into the structured data format used
1958 % internally. This allows the default and given values to be combined
1959 % with no repetition. In the second step, the structured data is
1960 % converted to pre-defined variable assignments, and these are stored
1961 % in the function for the instance.
1962 % \begin{macrocode}
1963 \cs_new_protected:Npn \@@_declare_instance:nnnn #1#2#3#4
1964 {
1965 \@@_execute_if_code_exist:nnT {#1} {#2}
1966 {
1967 \@@_recover_defaults:nn {#1} {#2}
1968 \@@_recover_vars:nn {#1} {#2}
1969 \@@_declare_instance_aux:nnnn {#1} {#2} {#3} {#4}
1970 }
1971 }
1972 \cs_new_protected:Npn \@@_declare_instance_aux:nnnn #1#2#3#4
1973 {
1974 \bool_set_false:N \l_@@_error_bool
1975 \@@_parse_values:nnn {#1} {#2} {#4}
1976 \bool_if:NF \l_@@_error_bool
1977 {
1978 \prop_put:Nnn \l_@@_values_prop { from~template } {#2}
1979 \@@_store_values:nn {#1} {#3}
1980 \@@_convert_to_assignments:
1981 \cs_if_exist:cT { \c_@@_instances_root_tl #1 / #3 }
1982 { \msg_info:nnnn { template } { declare-instance } {#3} {#1} }
1983 \cs_set_protected:cpe { \c_@@_instances_root_tl #1 / #3 }
1984 {
1985 \exp_not:N \@@_assignments_push:n
1986 { \exp_not:V \l_@@_assignments_tl }
1987 \exp_not:c { \c_@@_code_root_tl #1 / #2 }
1988 }
1989 }
1990 }
1991 % \end{macrocode}
1992 % \end{macro}
1993 %
1994 % \begin{macro}{\@@_instance_set_eq:nnn}
1995 % Copy--paste an instance.
1996 % \begin{macrocode}
1997 \cs_new_protected:Npn \@@_instance_set_eq:nnn #1#2#3
1998 {
1999 \@@_if_instance_exist:nnTF {#1} {#3}
2000 {
2001 \@@_recover_values:nn {#1} {#3}
2002 \@@_store_values:nn {#1} {#2}
2003 \cs_if_exist:cT { \c_@@_instances_root_tl #1 / #2 }
2004 { \msg_info:nnnn { template } { declare-instance } {#2} {#1} }
2005 \cs_set_eq:cc { \c_@@_instances_root_tl #1 / #2 }
2006 { \c_@@_instances_root_tl #1 / #3 }
2007 }
2008 { \msg_error:nnnn { template } { unknown-instance } {#1} {#3} }
2009 }
2010 % \end{macrocode}
2011 % \end{macro}
2012 %
2013 % \begin{macro}{\@@_edit_instance:nnn}
2014 % \begin{macro}
2015 % {\@@_edit_instance_aux:nnnnn, \@@_edit_instance_aux:nVnnn}
2016 % Editing an instance is almost identical to declaring one. The only
2017 % variation is the source of the values to use. When editing, they are
2018 % recovered from the previous instance run.
2019 % \begin{macrocode}
2020 \cs_new_protected:Npn \@@_edit_instance:nnn #1#2#3
2021 {
2022 \@@_if_instance_exist:nnTF {#1} {#2}
2023 {
2024 \@@_recover_values:nn {#1} {#2}
2025 \prop_get:NnN \l_@@_values_prop { from~template }
2026 \l_@@_tmp_tl
2027 \@@_edit_instance_aux:nVnn
2028 {#1} \l_@@_tmp_tl {#2} {#3}
2029 }
2030 { \msg_error:nnnn { template } { unknown-instance } {#1} {#2} }
2031 }
2032 \cs_new_protected:Npn \@@_edit_instance_aux:nnnn #1#2#3#4
2033 {
2034 \@@_recover_vars:nn {#1} {#2}
2035 \@@_declare_instance_aux:nnnn {#1} {#2} {#3} {#4}
2036 }
2037 \cs_generate_variant:Nn \@@_edit_instance_aux:nnnn { nV }
2038 % \end{macrocode}
2039 % \end{macro}
2040 % \end{macro}
2041 %
2042 % \begin{macro}{\@@_convert_to_assignments:}
2043 % \begin{macro}{\@@_convert_to_assignments_aux:n}
2044 % \begin{macro}
2045 % {\@@_convert_to_assignments_aux:nn, \@@_convert_to_assignments_aux:nV}
2046 % The idea on converting to a set of assignments is to loop over each
2047 % key, so that the loop order follows the declaration order of the keys.
2048 % This is done using a sequence as property lists are not
2049 % \enquote{ordered}.
2050 % \begin{macrocode}
2051 \cs_new_protected:Npn \@@_convert_to_assignments:
2052 {
2053 \tl_clear:N \l_@@_assignments_tl
2054 \seq_map_function:NN \l_@@_key_order_seq
2055 \@@_convert_to_assignments_aux:n
2056 }
2057 \cs_new_protected:Npn \@@_convert_to_assignments_aux:n #1
2058 {
2059 \prop_get:NnN \l_@@_keytypes_prop {#1} \l_@@_tmp_tl
2060 \@@_convert_to_assignments_aux:nV {#1} \l_@@_tmp_tl
2061 }
2062 % \end{macrocode}
2063 % The second auxiliary function actually does the work. The
2064 % arguments here are the key name (|#1|) and the keytype (|#2|).
2065 % From those, the value to assign and the name of the appropriate
2066 % variable are recovered. A bit of work is then needed to sort out
2067 % keytypes with arguments (for example instances), and to look for
2068 % global assignments. Once that is done, a hand-off can be made to the
2069 % handler for the relevant keytype.
2070 % \begin{macrocode}
2071 \cs_new_protected:Npn \@@_convert_to_assignments_aux:nn #1#2
2072 {
2073 \prop_get:NnNT \l_@@_values_prop {#1} \l_@@_value_tl
2074 {
2075 \prop_get:NnNTF \l_@@_vars_prop {#1} \l_@@_var_tl
2076 {
2077 \@@_split_keytype_arg:n {#2}
2078 \str_if_eq:VnF \l_@@_keytype_tl { choice }
2079 {
2080 \str_if_eq:VnF \l_@@_keytype_tl { code }
2081 { \@@_find_global: }
2082 }
2083 \tl_set:Nn \l_@@_key_name_tl {#1}
2084 \cs_if_exist_use:cF { @@_assign_ \l_@@_keytype_tl : }
2085 { \@@_assign_variable: }
2086 }
2087 { \msg_error:nnn { template } { unknown-attribute } {#1} }
2088 }
2089 }
2090 \cs_generate_variant:Nn \@@_convert_to_assignments_aux:nn { nV }
2091 % \end{macrocode}
2092 % \end{macro}
2093 % \end{macro}
2094 % \end{macro}
2095 %
2096 % \begin{macro}{\@@_find_global:}
2097 % \begin{macro}{\@@_find_global_aux:w}
2098 % Global assignments should have the phrase |global| at the front.
2099 % This is pretty easy to find: no other error checking, though.
2100 % \begin{macrocode}
2101 \cs_new_protected:Npn \@@_find_global:
2102 {
2103 \bool_set_false:N \l_@@_global_bool
2104 \tl_if_in:onT \l_@@_var_tl { global }
2105 {
2106 \exp_after:wN \@@_find_global_aux:w \l_@@_var_tl \s_@@_stop
2107 }
2108 }
2109 \cs_new_protected:Npn \@@_find_global_aux:w #1 global #2 \s_@@_stop
2110 {
2111 \tl_set:Nn \l_@@_var_tl {#2}
2112 \bool_set_true:N \l_@@_global_bool
2113 }
2114 % \end{macrocode}
2115 % \end{macro}
2116 % \end{macro}
2117 %
2118 % \begin{macro}[EXP]{\@@_instance_value:nnn}
2119 % \changes{2025-11-21}{v1.0g}{New function}
2120 % For editing templates.
2121 % \begin{macrocode}
2122 \cs_new:Npn \@@_instance_value:nnn #1#2#3
2123 {
2124 \@@_if_instance_exist:nnT {#1} {#2}
2125 { \prop_item:cn { \c_@@_values_root_tl #1 / #2 } {#3} }
2126 }
2127 % \end{macrocode}
2128 % \end{macro}
2129 %
2130 % \subsection{Using templates directly}
2131 %
2132 % \begin{macro}{\@@_use_template:nnn}
2133 % Directly use a template with a particular parameter setting.
2134 % This is also picked up if used in a nested fashion inside a parameter
2135 % list. The idea is essentially the same as creating an instance,
2136 % just with no saving of the result.
2137 % \begin{macrocode}
2138 \cs_new_protected:Npn \@@_use_template:nnn #1#2#3
2139 {
2140 \@@_execute_if_code_exist:nnT {#1} {#2}
2141 {
2142 \@@_recover_defaults:nn {#1} {#2}
2143 \@@_recover_vars:nn {#1} {#2}
2144 \@@_parse_values:nnn {#1} {#2} {#3}
2145 \@@_convert_to_assignments:
2146 \@@_debug_typeout:n
2147 { Using~template~'#2'~of~type~'#1'~directly~ \msg_line_context: }
2148 \use:c { \c_@@_code_root_tl #1 / #2 }
2149 }
2150 }
2151 % \end{macrocode}
2152 % \end{macro}
2153 %
2154 % \subsection{Assigning values to variables}
2155 %
2156 % \begin{macro}{\@@_assign_boolean:}
2157 % \begin{macro}{\@@_assign_boolean_aux:n}
2158 % Setting a Boolean value is slightly different to everything else
2159 % as the value can be used to work out which \texttt{set} function to
2160 % call. As long as there is no need to recover things from another
2161 % variable, everything is pretty easy. If there is, then we need to allow
2162 % for the fact that the recovered value here will \emph{not} be expandable,
2163 % so needs to be converted to something that is.
2164 % \begin{macrocode}
2165 \cs_new_protected:Npn \@@_assign_boolean:
2166 {
2167 \bool_if:NTF \l_@@_global_bool
2168 { \@@_assign_boolean_aux:n { bool_gset } }
2169 { \@@_assign_boolean_aux:n { bool_set } }
2170 }
2171 \cs_new_protected:Npn \@@_assign_boolean_aux:n #1
2172 {
2173 \@@_if_key_value:VTF \l_@@_value_tl
2174 {
2175 \@@_key_to_value:
2176 \tl_put_right:Ne \l_@@_assignments_tl
2177 {
2178 \exp_not:c { #1 _eq:NN }
2179 \exp_not:V \l_@@_var_tl
2180 \exp_not:V \l_@@_value_tl
2181 }
2182 }
2183 {
2184 \tl_put_right:Ne \l_@@_assignments_tl
2185 {
2186 \exp_not:c { #1 _ \l_@@_value_tl :N }
2187 \exp_not:V \l_@@_var_tl
2188 }
2189 }
2190 }
2191 % \end{macrocode}
2192 % \end{macro}
2193 % \end{macro}
2194 %
2195 % \begin{macro}{\@@_assign_choice:}
2196 % \begin{macro}
2197 % {\@@_assign_choice_aux:nF, \@@_assign_choice_aux:eF}
2198 % The idea here is to find either the choice as-given or else the
2199 % special |unknown| choice, and to copy the appropriate code across.
2200 % \begin{macrocode}
2201 \cs_new_protected:Npn \@@_assign_choice:
2202 {
2203 \@@_assign_choice_aux:eF
2204 { \l_@@_key_name_tl \c_space_tl \l_@@_value_tl }
2205 {
2206 \@@_assign_choice_aux:eF
2207 { \l_@@_key_name_tl \c_space_tl unknown }
2208 {
2209 \prop_get:NVN \l_@@_keytypes_prop \l_@@_key_name_tl
2210 \l_@@_tmp_tl
2211 \@@_split_keytype_arg:V \l_@@_tmp_tl
2212 \msg_error:nnVV { template } { unknown-choice }
2213 \l_@@_key_name_tl
2214 \l_@@_value_tl
2215 }
2216 }
2217 }
2218 \cs_new_protected:Npn \@@_assign_choice_aux:nF #1
2219 {
2220 \prop_get:NnNTF \l_@@_vars_prop {#1} \l_@@_tmp_tl
2221 { \tl_put_right:NV \l_@@_assignments_tl \l_@@_tmp_tl }
2222 }
2223 \cs_generate_variant:Nn \@@_assign_choice_aux:nF { e }
2224 % \end{macrocode}
2225 % \end{macro}
2226 % \end{macro}
2227 %
2228 % \begin{macro}{\@@_assign_function:}
2229 % \changes{2025-12-24}{v1.0h}{Create protected functions}
2230 % \begin{macro}{\@@_assign_function_aux:N}
2231 % This looks a bit messy but is only actually one function.
2232 % \begin{macrocode}
2233 \cs_new_protected:Npn \@@_assign_function:
2234 {
2235 \bool_if:NTF \l_@@_global_bool
2236 { \@@_assign_function_aux:N \cs_gset_protected:Npn }
2237 { \@@_assign_function_aux:N \cs_set_protected:Npn }
2238 }
2239 \cs_new_protected:Npn \@@_assign_function_aux:N #1
2240 {
2241 \tl_put_right:Ne \l_@@_assignments_tl
2242 {
2243 \cs_generate_from_arg_count:NNnn
2244 \exp_not:V \l_@@_var_tl
2245 \exp_not:N #1
2246 { \exp_not:V \l_@@_keytype_arg_tl }
2247 { \exp_not:V \l_@@_value_tl }
2248 }
2249 }
2250 % \end{macrocode}
2251 % \end{macro}
2252 % \end{macro}
2253 %
2254 % \begin{macro}{\@@_assign_instance:}
2255 % \begin{macro}{\@@_assign_instance_aux:N}
2256 % Using an instance means adding the appropriate function creation to
2257 % the tl. No checks are made at this stage, so if the instance is
2258 % not valid then errors will arise later.
2259 % \begin{macrocode}
2260 \cs_new_protected:Npn \@@_assign_instance:
2261 {
2262 \bool_if:NTF \l_@@_global_bool
2263 { \@@_assign_instance_aux:N \cs_gset_protected:Npn }
2264 { \@@_assign_instance_aux:N \cs_set_protected:Npn }
2265 }
2266 \cs_new_protected:Npn \@@_assign_instance_aux:N #1
2267 {
2268 \tl_put_right:Ne \l_@@_assignments_tl
2269 {
2270 \exp_not:N #1 \exp_not:V \l_@@_var_tl
2271 {
2272 \@@_use_instance:nn
2273 { \exp_not:V \l_@@_keytype_arg_tl }
2274 { \exp_not:V \l_@@_value_tl }
2275 }
2276 }
2277 }
2278 % \end{macrocode}
2279 % \end{macro}
2280 % \end{macro}
2281 %
2282 % \begin{macro}{\@@_assign_variable:}
2283 % \changes{2024-10-07}{v1.0d}{Correct passing of \cs{KeyValue} contents}
2284 % \begin{macro}{\@@_assign_variable:n}
2285 % A general-purpose function for all of the other assignments.
2286 % As long as the value is not coming from another variable, the stored
2287 % value is simply transferred for output. We use \texttt{V}-type expansion
2288 % for the \cs{KeyValue} case: for token lists this is essential, whilst
2289 % for register-based variables, it does no harm and avoids needing a
2290 % low-level test.
2291 % \begin{macrocode}
2292 \cs_new_protected:Npn \@@_assign_variable:
2293 {
2294 \exp_args:Ne \@@_assign_variable:n
2295 {
2296 \@@_map_var_type:
2297 _
2298 \bool_if:NT \l_@@_global_bool { g }
2299 set:N
2300 }
2301 }
2302 % \end{macrocode}
2303 % Notice we need a \texttt{V}-type variant for each \texttt{(g)set}
2304 % operation here: these need to be provided by \pkg{expl3}.
2305 % \begin{macrocode}
2306 \cs_new_protected:Npn \@@_assign_variable:n #1
2307 {
2308 \@@_if_key_value:VTF \l_@@_value_tl
2309 {
2310 \@@_key_to_value:
2311 \tl_put_right:Ne \l_@@_assignments_tl
2312 {
2313 \exp_not:c { #1 V } \exp_not:V \l_@@_var_tl
2314 \exp_not:V \l_@@_value_tl
2315 }
2316 }
2317 {
2318 \tl_put_right:Ne \l_@@_assignments_tl
2319 {
2320 \exp_not:c { #1 n } \exp_not:V \l_@@_var_tl
2321 { \exp_not:V \l_@@_value_tl }
2322 }
2323 }
2324 }
2325 % \end{macrocode}
2326 % \end{macro}
2327 % \end{macro}
2328 %
2329 % \begin{macro}{\@@_key_to_value:}
2330 % \begin{macro}{\@@_key_to_value_auxi:w}
2331 % \begin{macro}{\@@_key_to_value_auxii:w}
2332 % The idea here is to recover the attribute value of another key. To
2333 % do that, the marker is removed and a look up takes place. If this
2334 % is successful, then the name of the variable of the attribute is
2335 % returned. This assumes that the value will be used in context where
2336 % it will be converted to a value, for example when setting a number.
2337 % There is also a need to check in case the copied value happens to be
2338 % \texttt{global}.
2339 % \begin{macrocode}
2340 \cs_new_protected:Npn \@@_key_to_value:
2341 { \exp_after:wN \@@_key_to_value_auxi:w \l_@@_value_tl }
2342 \cs_new_protected:Npn \@@_key_to_value_auxi:w \KeyValue #1
2343 {
2344 \tl_set:Ne \l_@@_tmp_tl { \tl_trim_spaces:e { \tl_to_str:n {#1} } }
2345 \prop_get:NVNTF \l_@@_vars_prop \l_@@_tmp_tl
2346 \l_@@_value_tl
2347 {
2348 \exp_after:wN \@@_key_to_value_auxii:w \l_@@_value_tl
2349 \s_@@_mark global \q_@@_nil \s_@@_stop
2350 }
2351 { \msg_error:nnV { template } { unknown-attribute } \l_@@_tmp_tl }
2352 }
2353 \cs_new_protected:Npn \@@_key_to_value_auxii:w #1 global #2#3 \s_@@_stop
2354 {
2355 \@@_quark_if_nil:NF #2
2356 { \tl_set:Nn \l_@@_value_tl {#2} }
2357 }
2358 % \end{macrocode}
2359 % \end{macro}
2360 % \end{macro}
2361 % \end{macro}
2362 %
2363 % \subsection{Using instances}
2364 %
2365 % \begin{macro}{\@@_use_instance:nn}
2366 % \begin{macro}{\@@_use_instance_aux:nNnnn}
2367 % \begin{macro}{\@@_use_instance_auxi:nn, \@@_use_instance_auxii:nn}
2368 % Using an instance is just a question of finding the appropriate function.
2369 % If nothing is found, an error is raised. One complication is that
2370 % if the first token of argument |#2| is \cs{UseTemplate} then that
2371 % is also valid. There is an error-test to make sure that the
2372 % types agree, and if so the template is used directly.
2373 % \begin{macrocode}
2374 \cs_new_protected:Npn \@@_use_instance:nn #1#2
2375 {
2376 \@@_if_use_template:nTF {#2}
2377 { \@@_use_instance_aux:nNnnn {#1} #2 }
2378 { \@@_use_instance_auxi:nn {#1} {#2} }
2379 }
2380 \cs_new_protected:Npn \@@_use_instance_aux:nNnnn #1#2#3#4#5
2381 {
2382 \str_if_eq:nnTF {#1} {#3}
2383 { \@@_use_template:nnn {#3} {#4} {#5} }
2384 { \msg_error:nnnn { template } { type-mismatch } {#1} {#3} }
2385 }
2386 \cs_new_protected:Npn \@@_use_instance_auxi:nn #1#2
2387 {
2388 \@@_if_instance_exist:nnTF {#1} {#2}
2389 { \@@_use_instance_auxii:nn {#1} {#2} }
2390 { \msg_error:nnnn { template } { unknown-instance } {#1} {#2} }
2391 }
2392 \cs_new_protected:Npn \@@_use_instance_auxii:nn #1#2
2393 {
2394 \@@_debug_typeout:n
2395 { Using~instance~'#2'~of~type~'#1'~ \msg_line_context: }
2396 \use:c { \c_@@_instances_root_tl #1 / #2 }
2397 }
2398 % \end{macrocode}
2399 % \end{macro}
2400 % \end{macro}
2401 % \end{macro}
2402 %
2403 %\subsection{Assignment manipulation}
2404 %
2405 % A few functions to transfer assignments about, as this is needed by
2406 % \cs{AssignTemplateKeys}.
2407 %
2408 % \begin{macro}{\@@_assignments_pop:}
2409 % To actually use the assignments.
2410 % \begin{macrocode}
2411 \cs_new:Npn \@@_assignments_pop: { \l_@@_assignments_tl }
2412 % \end{macrocode}
2413 % \end{macro}
2414 %
2415 % \begin{macro}{\@@_assignments_push:n}
2416 % Here, the assignments are stored for later use.
2417 % \begin{macrocode}
2418 \cs_new_protected:Npn \@@_assignments_push:n #1
2419 { \tl_set:Nn \l_@@_assignments_tl {#1} }
2420 % \end{macrocode}
2421 % \end{macro}
2422 %
2423 % \subsection{Showing templates and instances}
2424 %
2425 % \begin{macro}{\@@_show_code:nn}
2426 % Showing the code for a template is just a translation of
2427 % \cs{cs_show:c}.
2428 % \begin{macrocode}
2429 \cs_new_protected:Npn \@@_show_code:nn #1#2
2430 { \cs_show:c { \c_@@_code_root_tl #1 / #2 } }
2431 % \end{macrocode}
2432 % \end{macro}
2433 %
2434 % \begin{macro}
2435 % {
2436 % \@@_show_defaults:nn,
2437 % \@@_show_keytypes:nn,
2438 % \@@_show_vars:nn
2439 % }
2440 % \begin{macro}{\@@_show:Nnnn}
2441 % A modified version of the property-list printing code, such that
2442 % the output refers to templates and instances rather than to the
2443 % underlying structures.
2444 % \begin{macrocode}
2445 \cs_new_protected:Npn \@@_show_defaults:nn #1#2
2446 {
2447 \@@_if_keys_exist:nnT {#1} {#2}
2448 {
2449 \@@_recover_defaults:nn {#1} {#2}
2450 \@@_show:Nnnn \l_@@_values_prop
2451 {#1} {#2} { default~values }
2452 }
2453 }
2454 \cs_new_protected:Npn \@@_show_keytypes:nn #1#2
2455 {
2456 \@@_if_keys_exist:nnT {#1} {#2}
2457 {
2458 \@@_recover_keytypes:nn {#1} {#2}
2459 \@@_show:Nnnn \l_@@_keytypes_prop
2460 {#1} {#2} { interface }
2461 }
2462 }
2463 \cs_new_protected:Npn \@@_show_vars:nn #1#2
2464 {
2465 \@@_execute_if_code_exist:nnT {#1} {#2}
2466 {
2467 \@@_recover_vars:nn {#1} {#2}
2468 \@@_show:Nnnn \l_@@_vars_prop
2469 {#1} {#2} { variable~mapping }
2470 }
2471 }
2472 \cs_new_protected:Npn \@@_show:Nnnn #1#2#3#4
2473 {
2474 \msg_show:nneeee { template } { show-attribute }
2475 { \tl_to_str:n {#2} }
2476 { \tl_to_str:n {#3} }
2477 { \tl_to_str:n {#4} }
2478 { \prop_map_function:NN #1 \msg_show_item_unbraced:nn }
2479 }
2480 % \end{macrocode}
2481 % \end{macro}
2482 % \end{macro}
2483 %
2484 % \begin{macro}{\@@_show_values:nn}
2485 % \changes{2025-11-21}{v1.0g}{Extend message}
2486 % Instance values are a little more complex, as is the template to consider.
2487 % \begin{macrocode}
2488 \cs_new_protected:Npn \@@_show_values:nn #1#2
2489 {
2490 \@@_if_instance_exist:nnTF {#1} {#2}
2491 {
2492 \@@_recover_values:nn {#1} {#2}
2493 \msg_show:nneee { template } { show-values }
2494 { \tl_to_str:n {#1} }
2495 { \tl_to_str:n {#2} }
2496 {
2497 \prop_map_function:NN \l_@@_values_prop
2498 \msg_show_item_unbraced:nn
2499 }
2500 }
2501 { \msg_info:nnnn { template } { unknown-instance } {#1} {#2} }
2502 }
2503 % \end{macrocode}
2504 % \end{macro}
2505 %
2506 % \subsection{Messages}
2507 %
2508 % The text for error messages: short and long text for all of them.
2509 % \begin{macrocode}
2510 \msg_new:nnnn { template } { argument-number-mismatch }
2511 { Template~type~'#1'~takes~#2~argument(s). }
2512 {
2513 Templates~of~type~'#1'~require~#2~argument(s).\\
2514 You~have~tried~to~make~a~template~for~'#1'~
2515 with~#3~argument(s),~which~is~not~possible:~
2516 the~number~of~arguments~must~agree.
2517 }
2518 \msg_new:nnnn { template } { bad-number-of-arguments }
2519 { Bad~number~of~arguments~for~template~type~'#1'. }
2520 {
2521 A~template~may~accept~between~0~and~9~arguments.\\
2522 You~asked~to~use~#2~arguments:~this~is~not~supported.
2523 }
2524 \msg_new:nnnn { template } { bad-variable }
2525 { Incorrect~variable~description~'#1'. }
2526 {
2527 The~argument~'#1'~is~not~of~the~form \\
2528 ~~'<variable>'\\
2529 ~or~\\
2530 ~~'global~<variable>'.\\
2531 It~must~be~given~in~one~of~these~formats~to~be~used~in~a~template.
2532 }
2533 \msg_new:nnnn { template } { choice-not-implemented }
2534 { The~choice~'#1'~has~no~implementation. }
2535 {
2536 Each~choice~listed~in~the~interface~for~a~template~must~
2537 have~an~implementation.
2538 }
2539 \msg_new:nnnn { template } { choice-no-code }
2540 { The~choice~'#1'~requires~implementation~details. }
2541 {
2542 When~creating~template~code~using~\DeclareTemplateCode,~
2543 each~choice~name~must~have~an~associated~implementation.\\
2544 This~should~be~given~after~a~'='~sign:~LaTeX~did~not~find~one.
2545 }
2546 \msg_new:nnnn { template } { choice-requires-code }
2547 { The~choice~'#2'~for~key~'#1'~requires~an~implementation. }
2548 {
2549 You~should~have~put:\\
2550 \ \ #1~:~choice~{~#2 = <code> ~} \\
2551 but~LaTeX~did~not~find~any~<code>.
2552 }
2553 \msg_new:nnnn { template } { duplicate-key-interface }
2554 { Key~'#1'~appears~twice~in~interface~definition~\msg_line_context:. }
2555 {
2556 Each~key~can~only~have~one~interface~declared~in~a~template.\\
2557 LaTeX~found~two~interfaces~for~'#1'.
2558 }
2559 \msg_new:nnnn { template } { keytype-requires-argument }
2560 { The~key~type~'#1'~requires~an~argument~\msg_line_context:. }
2561 {
2562 You~should~have~put:\\
2563 \ \ <key-name>~:~#1~{~<argument>~} \\
2564 but~LaTeX~did~not~find~an~<argument>.
2565 }
2566 \msg_new:nnnn { template } { invalid-keytype }
2567 { The~key~'#1'~is~missing~a~key-type~\msg_line_context:. }
2568 {
2569 Each~key~in~a~template~requires~a~key-type,~given~in~the~form:\\
2570 \ \ <key>~:~<key-type>\\
2571 LaTeX~could~not~find~a~<key-type>~in~your~input.
2572 }
2573 \msg_new:nnnn { template } { key-no-value }
2574 { The~key~'#1'~has~no~value~\msg_line_context:. }
2575 {
2576 When~creating~an~instance~of~a~template~
2577 every~key~listed~must~include~a~value:\\
2578 \ \ <key>~=~<value>
2579 }
2580 \msg_new:nnnn { template } { key-no-variable }
2581 { The~key~'#1'~requires~implementation~details~\msg_line_context:. }
2582 {
2583 When~creating~template~code~using~\DeclareTemplateCode,~
2584 each~key~name~must~have~an~associated~implementation.\\
2585 This~should~be~given~after~a~'='~sign:~LaTeX~did~not~find~one.
2586 }
2587 \msg_new:nnnn { template } { key-not-implemented }
2588 { Key~'#1'~has~no~implementation~\msg_line_context:. }
2589 {
2590 The~definition~of~key~implementations~for~template~'#2'~
2591 of~template~type~'#3'~does~not~include~any~details~for~key~'#1'.\\
2592 The~key~was~declared~in~the~interface~definition,~
2593 and~so~an~implementation~is~required.
2594 }
2595 \msg_new:nnnn { template } { missing-keytype }
2596 { The~key~'#1'~is~missing~a~key-type~\msg_line_context:. }
2597 {
2598 Key~interface~definitions~should~be~of~the~form\\
2599 \ \ #1~:~<key-type>\\
2600 but~LaTeX~could~not~find~a~<key-type>.
2601 }
2602 \msg_new:nnnn { template } { no-template-code }
2603 {
2604 The~template~'#2'~of~type~'#1'~is~unknown~
2605 or~has~no~implementation.
2606 }
2607 {
2608 There~is~no~code~available~for~the~template~name~given.\\
2609 This~should~be~given~using~\DeclareTemplateCode.
2610 }
2611 \msg_new:nnnn { template } { type-already-defined }
2612 { Template~type~'#1'~already~defined. }
2613 {
2614 You~have~used~\NewTemplateType~
2615 with~a~template~type~that~has~already~been~defined.
2616 }
2617 \msg_new:nnnn { template } { type-mismatch }
2618 { Template~types~'#1'~and~'#2'~do~not~agree. }
2619 {
2620 You~are~trying~to~use~a~template~directly~with~\UseInstance
2621 (or~a~similar~function),~but~the~template~types~do~not~match.
2622 }
2623 \msg_new:nnnn { template } { unknown-attribute }
2624 { The~template~attribute~'#1'~is~unknown. }
2625 {
2626 There~is~a~definition~in~the~current~template~reading\\
2627 \ \ \token_to_str:N \KeyValue {~#1~} \\
2628 but~there~is~no~key~called~'#1'.
2629 }
2630 \msg_new:nnnn { template } { unknown-choice }
2631 { The~choice~'#2'~was~not~declared~for~key~'#1'. }
2632 {
2633 The~key~'#1'~takes~a~fixed~list~of~choices~
2634 and~this~list~does~not~include~'#2'.
2635 }
2636 \msg_new:nnnn { template } { unknown-default-choice }
2637 { The~default~choice~'#2'~was~not~declared~for~key~'#1'. }
2638 {
2639 The~key~'#1'~takes~a~fixed~list~of~choices~
2640 and~this~list~does~not~include~'#2'.
2641 }
2642 \msg_new:nnnn { template } { unknown-expansion }
2643 { The~expansion~type~'#1'~is~unknown. }
2644 {
2645 Key~values~can~only~be~expanded~using~one~of~the~pre-defined~methods:~
2646 n,~o,~V,~v,~e,~N~or~c.
2647 }
2648 \msg_new:nnnn { template } { unknown-instance }
2649 { The~instance~'#2'~of~type~'#1'~is~unknown. }
2650 {
2651 You~have~asked~to~use~an~instance~'#2',~
2652 but~this~has~not~been~created.
2653 }
2654 \msg_new:nnnn { template } { unknown-key }
2655 { Unknown~template~key~'#1'. }
2656 {
2657 The~key~'#1'~was~not~declared~in~the~interface~
2658 for~the~current~template.
2659 }
2660 \msg_new:nnnn { template } { unknown-keytype }
2661 { The~key-type~'#1'~is~unknown. }
2662 {
2663 Valid~key-types~are:\\
2664 -~boolean;\\
2665 -~choice;\\
2666 -~commalist;\\
2667 -~function;\\
2668 -~instance;\\
2669 -~integer;\\
2670 -~length;\\
2671 -~muskip;\\
2672 -~real;\\
2673 -~skip;\\
2674 -~tokenlist.
2675 }
2676 \msg_new:nnnn { template } { unknown-type }
2677 { The~template~type~'#1'~is~unknown. }
2678 {
2679 A~template~type~needs~to~be~defined~with~\NewTemplateType
2680 prior~to~using~it.
2681 }
2682 \msg_new:nnnn { template } { unknown-template }
2683 { The~template~'#2'~of~type~'#1'~is~unknown. }
2684 {
2685 No~interface~has~been~declared~for~a~template~
2686 '#2'~of~template~type~'#1'.
2687 }
2688 % \end{macrocode}
2689 %
2690 % Information messages only have text: more text should not be needed.
2691 % \begin{macrocode}
2692 \msg_new:nnn { template } { declare-instance }
2693 { Declaring~instance~~'#1'~of~type~#2~\msg_line_context:. }
2694 \msg_new:nnn { template } { declare-template-code }
2695 { Declaring~code~for~template~'#2'~of~template~type~'#1'~\msg_line_context:. }
2696 \msg_new:nnn { template } { declare-template-interface }
2697 {
2698 Declaring~interface~for~template~'#2'~of~template~type~'#1'~
2699 \msg_line_context:.
2700 }
2701 \msg_new:nnn { template } { declare-type }
2702 { Declaring~template~type~'#1'~taking~#2~argument(s)~\msg_line_context:. }
2703 \msg_new:nnn { template } { show-attribute }
2704 {
2705 The~template~'#2'~of~type~'#1'~has~
2706 \tl_if_empty:nTF {#4} { no~#3. } { #3 : #4 }
2707 }
2708 \msg_new:nnn { template } { show-values }
2709 {
2710 The~instance~'#2'~of~type~'#1'~has~
2711 \tl_if_empty:nTF {#3} { no~values. } { values: #3 }
2712 }
2713 % \end{macrocode}
2714 %
2715 % Also add \pkg{template} to the \pkg{LaTeX} messages.
2716 % \begin{macrocode}
2717 \prop_gput:Nnn \g_msg_module_type_prop { template } { LaTeX }
2718 % \end{macrocode}
2719 %
2720 % \subsection{User functions}
2721 %
2722 % \begin{macro}{\NewTemplateType}
2723 % \begin{macro}{\DeclareTemplateInterface}
2724 % \begin{macro}{\DeclareTemplateCode}
2725 % \begin{macro}{\DeclareTemplateCopy}
2726 % \begin{macro}{\EditTemplateDefaults}
2727 % \begin{macro}{\UseTemplate}
2728 % \begin{macro}{\DeclareInstance}
2729 % \begin{macro}{\DeclareInstanceCopy}
2730 % \begin{macro}{\EditInstance}
2731 % \begin{macro}{\UseInstance}
2732 % All simple translations.
2733 % \begin{macrocode}
2734 \cs_new_protected:Npn \NewTemplateType #1#2
2735 { \@@_define_type:nn {#1} {#2} }
2736 \cs_new_protected:Npn \DeclareTemplateInterface #1#2#3#4
2737 { \@@_declare_template_keys:nnnn {#1} {#2} {#3} {#4} }
2738 \cs_new_protected:Npn \DeclareTemplateCode #1#2#3#4#5
2739 { \@@_declare_template_code:nnnnn {#1} {#2} {#3} {#4} {#5} }
2740 \cs_new_protected:Npn \DeclareTemplateCopy #1#2#3
2741 { \@@_template_set_eq:nnn {#1} {#2} {#3} }
2742 \cs_new_protected:Npn \EditTemplateDefaults #1#2#3
2743 { \@@_edit_defaults:nnn {#1} {#2} {#3} }
2744 \cs_new_protected:Npn \UseTemplate #1#2#3
2745 { \@@_use_template:nnn {#1} {#2} {#3} }
2746 \cs_new_protected:Npn \DeclareInstance #1#2#3#4
2747 { \@@_declare_instance:nnnn {#1} {#3} {#2} {#4} }
2748 \cs_new_protected:Npn \DeclareInstanceCopy #1#2#3
2749 { \@@_instance_set_eq:nnn {#1} {#2} {#3} }
2750 \cs_new_protected:Npn \EditInstance #1#2#3
2751 { \@@_edit_instance:nnn {#1} {#2} {#3} }
2752 \cs_new_protected:Npn \UseInstance #1#2
2753 { \@@_use_instance:nn {#1} {#2} }
2754 % \end{macrocode}
2755 % \end{macro}
2756 % \end{macro}
2757 % \end{macro}
2758 % \end{macro}
2759 % \end{macro}
2760 % \end{macro}
2761 % \end{macro}
2762 % \end{macro}
2763 % \end{macro}
2764 % \end{macro}
2765 %
2766 % \begin{macro}{\ShowTemplateCode}
2767 % \begin{macro}{\ShowTemplateDefaults}
2768 % \begin{macro}{\ShowTemplateInterface}
2769 % \begin{macro}{\ShowTemplateVariables}
2770 % \begin{macro}{\ShowInstanceValues}
2771 % The show functions are again just translation.
2772 % \begin{macrocode}
2773 \cs_new_protected:Npn \ShowTemplateCode #1#2
2774 { \@@_show_code:nn {#1} {#2} }
2775 \cs_new_protected:Npn \ShowTemplateDefaults #1#2
2776 { \@@_show_defaults:nn {#1} {#2} }
2777 \cs_new_protected:Npn \ShowTemplateInterface #1#2
2778 { \@@_show_keytypes:nn {#1} {#2} }
2779 \cs_new_protected:Npn \ShowTemplateVariables #1#2
2780 { \@@_show_vars:nn {#1} {#2} }
2781 \cs_new_protected:Npn \ShowInstanceValues #1#2
2782 { \@@_show_values:nn {#1} {#2} }
2783 % \end{macrocode}
2784 % \end{macro}
2785 % \end{macro}
2786 % \end{macro}
2787 % \end{macro}
2788 % \end{macro}
2789 %
2790 % \begin{macro}{\IfInstanceExistsT, \IfInstanceExistsF, \IfInstanceExistsTF}
2791 % \changes{2024-02-15}{v1.0b}{New macros}
2792 % \changes{2024-04-17}{v1.0c}{Use plural names}
2793 % More direct translation.
2794 % \begin{macrocode}
2795 \cs_new:Npn \IfInstanceExistsTF #1#2
2796 { \@@_if_instance_exist:nnTF {#1} {#2} }
2797 \cs_new:Npn \IfInstanceExistsT #1#2
2798 { \@@_if_instance_exist:nnT {#1} {#2} }
2799 \cs_new:Npn \IfInstanceExistsF #1#2
2800 { \@@_if_instance_exist:nnF {#1} {#2} }
2801 % \end{macrocode}
2802 % \end{macro}
2803 %
2804 % \begin{macro}{\KeyValue}
2805 % Simply dump the argument when executed: this should not happen.
2806 % \begin{macrocode}
2807 \cs_new_protected:Npn \KeyValue #1 {#1}
2808 % \end{macrocode}
2809 % \end{macro}
2810 %
2811 % \begin{macro}{\InstanceValue}
2812 % \changes{2025-11-21}{v1.0g}{New command}
2813 % \begin{macrocode}
2814 \cs_new:Npn \InstanceValue #1#2#3
2815 { \@@_instance_value:nnn {#1} {#2} {#3} }
2816 % \end{macrocode}
2817 % \end{macro}
2818 %
2819 % \begin{macro}{\AssignTemplateKeys}
2820 % A short call to use a token register by proxy.
2821 % \begin{macrocode}
2822 \cs_new_protected:Npn \AssignTemplateKeys { \@@_assignments_pop: }
2823 % \end{macrocode}
2824 % \end{macro}
2825 %
2826 % \begin{macro}{\SetKnownTemplateKeys,\SetTemplateKeys}
2827 % A friendly wrapper, with some speed up for the common case of the
2828 % third argument being empty.
2829 % \changes{2025-01-08}{v1.0d}{Test for empty key/val list to speed up processing}
2830 % \begin{macrocode}
2831 \cs_new_protected:Npn \SetKnownTemplateKeys #1#2#3
2832 {
2833 \tl_if_empty:oTF {#3}
2834 {
2835 \tl_set_eq:NN \UnusedTemplateKeys \c_empty_tl
2836 }
2837 {
2838 \keys_set_known:noN { template / #1 / #2 } {#3} \UnusedTemplateKeys
2839 }
2840 }
2841 % \end{macrocode}
2842 %
2843 % \begin{macrocode}
2844 \cs_new_protected:Npn \SetTemplateKeys #1#2#3
2845 {
2846 \tl_if_empty:oF {#3}
2847 {
2848 \keys_set:no { template / #1 / #2 } {#3}
2849 }
2850 }
2851 % \end{macrocode}
2852 %
2853 % \begin{macrocode}
2854 \tl_new:N \UnusedTemplateKeys
2855 % \end{macrocode}
2856 % \end{macro}
2857 %
2858 % \begin{macro}{\DebugTemplatesOn,\DebugTemplatesOff}
2859 % \begin{macrocode}
2860 \cs_new_protected:Npn \DebugTemplatesOn { \@@_debug_on: }
2861 \cs_new_protected:Npn \DebugTemplatesOff { \@@_debug_off: }
2862 % \end{macrocode}
2863 % \end{macro}
2864 %
2865 % \begin{macrocode}
2866 %<latexrelease>\IncludeInRelease{0000/00/00}{lttemplates}%
2867 %<latexrelease> {Prototype~document~commands}%
2868 %<latexrelease>
2869 %<latexrelease>\EndModuleRelease
2870 % \end{macrocode}
2871 %
2872 % \begin{macrocode}
2873 \ExplSyntaxOff
2874 % \end{macrocode}
2875 %
2876 % \begin{macrocode}
2877 %</2ekernel|latexrelease>
2878 % \end{macrocode}
2879 %
2880 % We need to stop DocStrip treating |@@| in a special way at this point.
2881 % \begin{macrocode}
2882 %<@@=>
2883 % \end{macrocode}
2884 %
2885 % \Finale