Debian Codegraph @

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