Debian Codegraph @

texmf-dist/source/latex/expkv-bundle/expkv-def.dtx

raw
1 % \iffalse meta-comment
2 %
3 % File: expkv-def.dtx Copyright (C) 2020-2024 Jonathan P. Spratte
4 %
5 % This work may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this license or
7 % (at your option) any later version. The latest version of this license is in
8 % the file:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % ------------------------------------------------------------------------------
13 %
14 %<*driver>^^A>>=
15 \def\expkvDocNoGenerate{}
16 \input expkv-bundle.ins
17 \generate{\file{expkv-def.sty}{\from{expkv-def.dtx}{pkg}}}
18 \generate{\file{expkv-def.tex}{\from{expkv-def.dtx}{tex}}}
19 \generate{\file{t-expkv-def.tex}{\from{expkv-def.dtx}{ctx}}}
20 \endbatchfile
21 %</driver>^^A=<<
22 % \fi
23 %
24 % \section{\expkvd}
25 %^^A the LaTeX package >>=
26 % \subsection{The \LaTeX\ Package}
27 % Just like for \expkv\ we provide a small \LaTeX\ package that sets up things
28 % such that we behave nicely on \LaTeX\ packages and files system. It'll
29 % |\input| the generic code which implements the functionality.
30 % \gobbledocstriptag
31 %<*pkg>
32 % \begin{macrocode}
33 \RequirePackage{expkv-pop}
34 \def\ekvd@tmp
35 {%
36 \ProvidesFile{expkv-def.tex}%
37 [\ekvdDate\space v\ekvdVersion\space a key-defining frontend for expkv]%
38 }
39 \input{expkv-def.tex}
40 \ProvidesPackage{expkv-def}%
41 [\ekvdDate\space v\ekvdVersion\space a key-defining frontend for expkv]
42 % \end{macrocode}
43 % \gobbledocstriptag
44 %</pkg>
45 %^^A=<<
46 %^^A the ConTeXt module >>=
47 % \subsection{The \ConTeXt\ module}
48 % \gobbledocstriptag
49 %<*ctx>
50 % \begin{macrocode}
51 \writestatus{loading}{ConTeXt User Module / expkv-def}
52 \usemodule[expkv-pop]
53 \unprotect
54 \input expkv-def.tex
55 \writestatus{loading}
56 {ConTeXt User Module / expkv-def / Version \ekvdVersion\space loaded}
57 \protect\endinput
58 % \end{macrocode}
59 % \gobbledocstriptag
60 %</ctx>
61 %^^A=<<
62 %^^A main file >>=
63 % \subsection{The Generic Code}
64 % The rest of this implementation will be the generic code.
65 % \gobbledocstriptag
66 %<*tex>
67 %
68 % Load \expkv\ if the package didn't already do so -- since \expkv\ has
69 % safeguards against being loaded twice this does no harm and the overhead
70 % isn't that big. Also we reuse some of the internals of \expkv\ to save us from
71 % retyping them. Additionally load \expkvp, which aids in defining the type
72 % system (and \expkvp\ will actually do the \expkv\ loading).
73 % \begin{macrocode}
74 \input expkv-pop
75 % \end{macrocode}
76 %
77 % We make sure that \file{expkv-def.tex} is only input once:
78 % \begin{macrocode}
79 \expandafter\ifx\csname ekvdVersion\endcsname\relax
80 \else
81 \expandafter\endinput
82 \fi
83 % \end{macrocode}
84 %
85 % \begin{macro}{\ekvdVersion,\ekvdDate}
86 % We're on our first input, so lets store the version and date in a macro.
87 % \begin{macrocode}
88 \def\ekvdVersion{1.1}
89 \def\ekvdDate{2024-12-16}
90 % \end{macrocode}
91 % \end{macro}
92 %
93 % If the \LaTeX\ format is loaded we want to be a good file and report back who
94 % we are, for this the package will have defined |\ekvd@tmp| to use
95 % |\ProvidesFile|, else this will expand to a |\relax| and do no harm.
96 % \begin{macrocode}
97 \csname ekvd@tmp\endcsname
98 % \end{macrocode}
99 %
100 % Store the category code of |@| to later be able to reset it and change it to
101 % 11 for now.
102 % \begin{macrocode}
103 \expandafter\chardef\csname ekvd@tmp\endcsname=\catcode`\@
104 \catcode`\@=11
105 % \end{macrocode}
106 % |\ekvd@tmp| will be reused later to handle expansion during the key
107 % defining. But we don't need it to ever store information long-term after
108 % \expkvd\ was initialized.
109 %
110 % \begin{macro}[internal]{\ekvd@ifprimitive}
111 % \begin{macrocode}
112 \protected\long\def\ekvd@ifprimitive#1%
113 {%
114 \begingroup
115 \edef\ekvd@tmpa{\string #1}%
116 \edef\ekvd@tmpb{\meaning#1}%
117 \expandafter
118 \endgroup
119 \ifx\ekvd@tmpa\ekvd@tmpb
120 \ekv@fi@firstoftwo
121 \fi
122 \@secondoftwo
123 }
124 % \end{macrocode}
125 % \end{macro}
126 %
127 % \begin{macro}[internal]
128 % {\ekvd@long,\ekvd@prot,\ekvd@clear@prefixes,\ekvd@ifalso}
129 % \expkvd\ will use |\ekvd@long|, |\ekvd@prot|, and |\ekvd@ifalso| to store
130 % whether a key should be defined as |\long| or |\protected| or adds an action
131 % to an existing key, and we have to clear them for every new key. By default
132 % |long| and |protected| will just be empty, |ifalso| will be
133 % |\@secondoftwo|, and |ifnew| will just use its third argument.
134 % \begin{macrocode}
135 \protected\def\ekvd@clear@prefixes
136 {%
137 \let\ekvd@long\ekv@empty
138 \let\ekvd@prot\ekv@empty
139 \let\ekvd@ifalso\@secondoftwo
140 \let\ekvd@do@new\@gobbletwo
141 }
142 \ekvd@clear@prefixes
143 % \end{macrocode}
144 % \end{macro}
145 %
146 % We define the parser for our front-facing macro and its \prefix{}es here:
147 % \begin{macrocode}
148 \ekvpNewParser{ekvd@definekeys}
149 \ekvpDefNoValuePrefix{ekvd@definekeys}
150 {\let\ekvd@ifnoarg\@firstoftwo}
151 {\let\ekvd@ifnoarg\@secondoftwo}
152 \ekvpDefPrefixLet{ekvd@definekeys}{long} \ekvd@long\long \ekv@empty
153 \ekvpDefPrefixLet{ekvd@definekeys}{protected}\ekvd@prot\protected\ekv@empty
154 \ekvpDefPrefixLet{ekvd@definekeys}{protect} \ekvd@prot\protected\ekv@empty
155 \ekvpDefPrefixLet{ekvd@definekeys}{also}\ekvd@ifalso\@firstoftwo\@secondoftwo
156 \ekvpDefPrefixLet{ekvd@definekeys}{new}\ekvd@do@new\ekvd@assert@new\@gobbletwo
157 % \end{macrocode}
158 % We ease the process of error throwing a bit for now by using our own macro
159 % instead of relying on \expkvp's argument forwarding.
160 % \begin{macrocode}
161 \ekvpDefAutoPrefix{ekvd@definekeys}{\edef\ekvd@cur{\detokenize{#3}}}{}
162 % \end{macrocode}
163 %
164 % \begin{macro}{\ekvdefinekeys}
165 % This is the one front-facing macro which provides the interface to define
166 % keys. It stores the \set\ for which the keys should be defined in
167 % |\ekvd@set| and calls a parser defined with \expkvp.
168 % \begin{macrocode}
169 \protected\edef\ekvdefinekeys#1%
170 {%
171 \ekv@unexpanded{\def\ekvd@set}{#1}%
172 \ekv@unexpanded{\ekvpParse@unsafe@auto\ekvp@@p@ekvd@definekeys}%
173 \ekv@unexpanded\expandafter{\csname\ekvp@@p@ekvd@definekeys{ppa}\endcsname}%
174 }
175 % \end{macrocode}
176 % \end{macro}
177 %
178 % \subsubsection{Key Types}
179 %
180 % \begin{macro}[internal]{\ekvd@def@type,\ekvd@def@type@fwd}
181 % To reduce some typing the following is a shortcut to |\ekvpDefType|. The
182 % |@fwd| variant will forward the key name and value and remove the
183 % unprocessed key. The other variant automatically sets up a helper macro,
184 % unfortunately this is necessary due to the design decision of \expkvp\ to
185 % not |\detokenize| the key names while \expkvd\ used to do this very early.
186 % \begin{macrocode}
187 \protected\def\ekvd@def@type#1#2%
188 {%
189 \ekv@exparg{\ekvpDefType{ekvd@definekeys}{#1}}
190 {%
191 \expandafter\expandafter\csname ekvd@th@#1\endcsname
192 \detokenize{##1}\ekv@stop{##2}{##3}%
193 }%
194 \long\expandafter\def\csname ekvd@th@#1\endcsname##1\ekv@stop##2##3{#2}%
195 }
196 \protected\long\def\ekvd@def@type@fwd#1#2%
197 {\ekvpDefType{ekvd@definekeys}{#1}{\ekv@exparg{#2}{\detokenize{##1}}{##3}}}
198 % \end{macrocode}
199 % \end{macro}
200 %
201 % \begin{macro}{set}
202 % \begin{macro}[internal]{\ekvd@type@set}
203 % \begin{macrocode}
204 \ekvd@def@type{set}
205 {\ekvd@ifnoarg{\ekvd@type@set{#1}{#1}}{\ekvd@type@set{#1}{#3}}}
206 \protected\def\ekvd@type@set#1#2%
207 {%
208 \ekvd@assert@not@long
209 \ekvd@assert@not@protected
210 \ekvd@do@new{NoVal}{#1}%
211 \ekv@ifempty{#2}%
212 {\ekvd@err@missing@definition}%
213 {%
214 \ekvd@ifalso
215 {%
216 \ekv@expargtwice{\ekvd@add@noval{#1}}%
217 {\ekvchangeset{#2}}%
218 \ekvd@assert@not@protected@also
219 }%
220 {\ekv@expargtwice{\ekvdefNoVal\ekvd@set{#1}}{\ekvchangeset{#2}}}%
221 }%
222 }
223 % \end{macrocode}
224 % \end{macro}
225 % \end{macro}
226 %
227 % \begin{macro}{noval,enoval}
228 % \begin{macro}[internal]{\ekvd@type@noval}
229 % Another pretty simple type, |noval| just needs to assert that there is a
230 % definition and that |long| wasn't specified.
231 % There are types where the difference in the variants is so small, that we
232 % define a common handler for them, those common handlers are named with
233 % |@type@|. |noval| and |enoval| are so similar that we can use such a |@type@|
234 % macro, even if we could've done |noval| in a slightly faster way without it.
235 % \begin{macrocode}
236 \ekvd@def@type@fwd{noval}{\ekvd@type@noval\def}
237 \ekvd@def@type@fwd{enoval}{\ekvd@type@noval\edef}
238 \protected\long\def\ekvd@type@noval#1#2#3%
239 {%
240 \ekvd@do@new{NoVal}{#2}%
241 \ekvd@assert@arg
242 \ekvd@assert@not@long
243 \ekvd@prot#1\ekvd@tmp{#3}%
244 \ekvd@ifalso
245 {\ekv@exparg{\ekvd@add@noval{#2}}\ekvd@tmp{}}%
246 {\ekvletNoVal\ekvd@set{#2}\ekvd@tmp}%
247 }
248 % \end{macrocode}
249 % \end{macro}
250 % \end{macro}
251 %
252 % \begin{macro}{code,ecode}
253 % \begin{macro}[internal]{\ekvd@type@code}
254 % |code| is simple as well, |ecode| has to use |\edef| on a temporary macro,
255 % since \expkv\ doesn't provide an |\ekvedef|.
256 % \begin{macrocode}
257 \ekvd@def@type@fwd{code}{\ekvd@type@code\def}
258 \ekvd@def@type@fwd{ecode}{\ekvd@type@code\edef}
259 \protected\long\def\ekvd@type@code#1#2#3%
260 {%
261 \ekvd@do@new{}{#2}%
262 \ekvd@assert@arg
263 \ekvd@prot\ekvd@long#1\ekvd@tmp##1{#3}%
264 \ekvd@ifalso
265 {\ekv@exparg{\ekvd@add@val{#2}}{\ekvd@tmp{##1}}{}}%
266 {\ekvlet\ekvd@set{#2}\ekvd@tmp}%
267 }
268 % \end{macrocode}
269 % \end{macro}
270 % \end{macro}
271 %
272 % \begin{macro}{default,qdefault,odefault,fdefault,edefault}
273 % \begin{macro}[internal]
274 % {
275 % \ekvd@type@default,\ekvd@t@default,\ekvd@t@qdefault,\ekvd@t@odefault,
276 % \ekvd@t@fdefault
277 % }
278 % |\ekvd@type@default| asserts there was an argument, also the key for which one
279 % wants to set a default has to be already defined (this is not so important for
280 % |default|, but |qdefault| requires it). If everything is good, |\edef| a
281 % temporary macro that expands |\ekvd@set| and the |\csname| for the key.
282 % The different expansion variants are implemented via |\ekv@unexpanded| and
283 % some |\expandafter|s.
284 % \begin{macrocode}
285 \ekvd@def@type@fwd{default}{\ekvd@type@default{\ekv@unexpanded\expandafter}{}}
286 \ekvd@def@type@fwd{odefault}
287 {\ekvd@type@default{\ekv@unexpanded\expandafter\expandafter\expandafter}{}}
288 \ekvpLet{ekvd@definekeys}{type}{qdefault}{odefault}
289 \ekvd@def@type@fwd{fdefault}
290 {\ekvd@type@default{\ekv@unexpanded\expandafter}{\romannumeral`\^^@}}
291 \protected\long\def\ekvd@type@default#1#2#3#4%
292 {%
293 \ekvd@assert@arg
294 \ekvd@do@new{NoVal}{#3}%
295 \ekvd@assert@not@long
296 \ekvifdefined\ekvd@set{#3}%
297 {%
298 \ekvd@prot\edef\ekvd@tmp
299 {#1{#2\csname\ekv@name\ekvd@set{#3}\endcsname{#4}}}%
300 \ekvd@ifalso
301 {\ekv@exparg{\ekvd@add@noval{#3}}\ekvd@tmp{}}%
302 {\ekvletNoVal\ekvd@set{#3}\ekvd@tmp}%
303 }%
304 {\ekvd@err@undefined@key{#3}}%
305 }
306 \ekvd@def@type{edefault}
307 {%
308 \ekvd@assert@arg
309 \ekvd@do@new{NoVal}{#1}%
310 \ekvd@assert@not@long
311 \ekvifdefined\ekvd@set{#1}%
312 {%
313 \ekvd@prot\edef\ekvd@tmp
314 {%
315 \ekv@unexpanded\ekv@expanded
316 {{\csname\ekv@name\ekvd@set{#1}\endcsname{#3}}}%
317 }%
318 \ekvd@ifalso
319 {\ekv@exparg{\ekvd@add@noval{#1}}\ekvd@tmp{}}%
320 {\ekvletNoVal\ekvd@set{#1}\ekvd@tmp}%
321 }%
322 {\ekvd@err@undefined@key{#1}}%
323 }
324 % \end{macrocode}
325 % \end{macro}
326 % \end{macro}
327 %
328 % \begin{macro}{initial,oinitial,finitial,einitial}
329 % \begin{macro}[internal]{\ekvd@type@initial}
330 % \begin{macrocode}
331 \ekvd@def@type@fwd{initial}{\ekvd@type@initial\@firstofone}
332 \ekvd@def@type@fwd{oinitial}{\ekvd@type@initial\ekv@exparg}
333 \ekvd@def@type@fwd{einitial}{\ekvd@type@initial\ekv@expandedarg}
334 \ekvd@def@type@fwd{finitial}{\ekvd@type@initial\ekv@fexparg}
335 \long\def\ekvd@type@initial#1#2#3%
336 {%
337 \ekvd@assert@not@new
338 \ekvd@assert@not@also
339 \ekvd@assert@not@long
340 \ekvd@assert@not@protected
341 \ekvd@ifnoarg
342 {%
343 \ekvifdefinedNoVal\ekvd@set{#2}%
344 {\csname\ekv@name\ekvd@set{#2}N\endcsname}%
345 {\ekvd@err@undefined@noval{#2}}%
346 }%
347 {%
348 \ekvifdefined\ekvd@set{#2}%
349 {#1{\csname\ekv@name\ekvd@set{#2}\endcsname}{#3}}%
350 {\ekvd@err@undefined@key{#2}}%
351 }%
352 }
353 % \end{macrocode}
354 % \end{macro}
355 % \end{macro}
356 %
357 % \begin{macro}{bool,gbool,boolTF,gboolTF,invbool,ginvbool,invboolTF,ginvboolTF}
358 % \begin{macro}[internal]{\ekvd@type@bool}
359 % The boolean types are a quicker version of a |choice| that accept |true| and
360 % |false|, and set up the |NoVal| action to be identical to \texttt{\key=true}.
361 % The |true| and |false| actions are always just |\let|ting the macro in |#7| to
362 % some other macro (\emph{e.g.}, \cs[no-index]{iftrue}).
363 % \begin{macrocode}
364 \ekvd@def@type@fwd{bool}{\ekvd@type@bool{}\iftrue\iffalse}
365 \ekvd@def@type@fwd{gbool}{\ekvd@type@bool\global\iftrue\iffalse}
366 \ekvd@def@type@fwd{boolTF}{\ekvd@type@bool{}\@firstoftwo\@secondoftwo}
367 \ekvd@def@type@fwd{gboolTF}{\ekvd@type@bool\global\@firstoftwo\@secondoftwo}
368 \ekvd@def@type@fwd{invbool}{\ekvd@type@bool{}\iffalse\iftrue}
369 \ekvd@def@type@fwd{ginvbool}{\ekvd@type@bool\global\iffalse\iftrue}
370 \ekvd@def@type@fwd{invboolTF}{\ekvd@type@bool{}\@secondoftwo\@firstoftwo}
371 \ekvd@def@type@fwd{ginvboolTF}{\ekvd@type@bool\global\@secondoftwo\@firstoftwo}
372 \protected\def\ekvd@type@bool#1#2#3#4#5%
373 {%
374 \ekvd@do@new{}{#4}%
375 \ekvd@do@new{NoVal}{#4}%
376 \ekvd@assert@filledarg{#5}%
377 \ekvd@newlet#5#3%
378 \ekvd@type@choice{#4}%
379 \protected\ekvdefNoVal\ekvd@set{#4}{#1\let#5#2}%
380 \protected\expandafter\def
381 \csname\ekvd@choice@name\ekvd@set{#4}{true}\endcsname
382 {#1\let#5#2}%
383 \protected\expandafter\def
384 \csname\ekvd@choice@name\ekvd@set{#4}{false}\endcsname
385 {#1\let#5#3}%
386 }
387 % \end{macrocode}
388 % \end{macro}
389 % \end{macro}
390 %
391 % \begin{macro}{boolpair,gboolpair,boolpairTF,gboolpairTF}
392 % \begin{macro}[internal]{\ekvd@type@boolpair}
393 % The boolean pair types are essentially the same as the boolean types, but set
394 % two macros instead of one.
395 % \begin{macrocode}
396 \ekvd@def@type{boolpair}
397 {\ekvd@assert@twoargs{#3}\ekvd@type@boolpair{}\iftrue\iffalse{#1}#3}
398 \ekvd@def@type{gboolpair}
399 {\ekvd@assert@twoargs{#3}\ekvd@type@boolpair\global\iftrue\iffalse{#1}#3}
400 \ekvd@def@type{boolpairTF}
401 {\ekvd@assert@twoargs{#3}\ekvd@type@boolpair{}\@firstoftwo\@secondoftwo{#1}#3}
402 \ekvd@def@type{gboolpairTF}
403 {%
404 \ekvd@assert@twoargs{#3}%
405 \ekvd@type@boolpair\global\@firstoftwo\@secondoftwo{#1}#3%
406 }
407 \protected\def\ekvd@type@boolpair#1#2#3#4#5#6%
408 {%
409 \ekvd@do@new{}{#4}%
410 \ekvd@do@new{NoVal}{#4}%
411 \ekvd@newlet#5#3%
412 \ekvd@newlet#6#2%
413 \ekvd@type@choice{#4}%
414 \protected\ekvdefNoVal\ekvd@set{#4}{#1\let#5#2#1\let#6#3}%
415 \protected\expandafter\def
416 \csname\ekvd@choice@name\ekvd@set{#4}{true}\endcsname
417 {#1\let#5#2#1\let#6#3}%
418 \protected\expandafter\def
419 \csname\ekvd@choice@name\ekvd@set{#4}{false}\endcsname
420 {#1\let#5#3#1\let#6#2}%
421 }
422 % \end{macrocode}
423 % \end{macro}
424 % \end{macro}
425 %
426 % \begin{macro}{data,gdata,dataT,gdataT}
427 % \begin{macro}[internal]{\ekvd@type@data}
428 % \begin{macrocode}
429 \ekvd@def@type@fwd{data}
430 {\ekvd@type@data\@secondoftwo\edef{####2}{\ekv@unexpanded{##1}}}
431 \ekvd@def@type@fwd{edata}
432 {%
433 \ekvd@type@data\@secondoftwo\edef
434 {####2}{\ekv@unexpanded\ekv@expanded{{##1}}}%
435 }
436 \ekvd@def@type@fwd{gdata}
437 {\ekvd@type@data\@secondoftwo\xdef{####2}{\ekv@unexpanded{##1}}}
438 \ekvd@def@type@fwd{xdata}
439 {%
440 \ekvd@type@data\@secondoftwo\xdef
441 {####2}{\ekv@unexpanded\ekv@expanded{{##1}}}%
442 }
443 \ekvd@def@type@fwd{dataT}{\ekvd@type@data\@gobble\edef{}{\ekv@unexpanded{##1}}}
444 \ekvd@def@type@fwd{edataT}
445 {\ekvd@type@data\@gobble\edef{}{\ekv@unexpanded\ekv@expanded{{##1}}}}
446 \ekvd@def@type@fwd{gdataT}{\ekvd@type@data\@gobble\xdef{}{\ekv@unexpanded{##1}}}
447 \ekvd@def@type@fwd{xdataT}
448 {\ekvd@type@data\@gobble\xdef{}{\ekv@unexpanded\ekv@expanded{{##1}}}}
449 \protected\def\ekvd@type@data#1#2#3#4#5#6%
450 {%
451 \ekvd@do@new{}{#5}%
452 \ekvd@assert@filledarg{#6}%
453 \ekvd@newlet#6#1%
454 \ekvd@ifalso
455 {%
456 \let\ekvd@prot\protected
457 \ekvd@add@val{#5}{\long#2#6####1#3{####1{#4}}}{}%
458 }%
459 {%
460 \protected\ekvd@long\ekvdef\ekvd@set{#5}%
461 {\long#2#6####1#3{####1{#4}}}%
462 }%
463 }
464 % \end{macrocode}
465 % \end{macro}
466 % \end{macro}
467 %
468 % \begin{macro}{box,gbox}
469 % \begin{macro}[internal]{\ekvd@type@box}
470 % Set up our boxes. Though we're a generic package we want to be colour safe, so
471 % we put an additional grouping level inside the box contents, for the case that
472 % someone uses \pkg{color}. |\ekvd@newreg| is a small wrapper which tests
473 % whether the first argument is defined and if not does
474 % |\csname new#2\endcsname#1|.
475 % \begin{macrocode}
476 \ekvd@def@type@fwd{box}{\ekvd@type@box{}}
477 \ekvd@def@type@fwd{gbox}{\ekvd@type@box\global}
478 \protected\def\ekvd@type@box#1#2#3%
479 {%
480 \ekvd@do@new{}{#2}%
481 \ekvd@assert@filledarg{#3}%
482 \ekvd@newreg#3{box}%
483 \ekvd@ifalso
484 {%
485 \let\ekvd@prot\protected
486 \ekvd@add@val{#2}{#1\setbox#3=\hbox{\begingroup##1\endgroup}}{}%
487 }%
488 {%
489 \protected\ekvd@long\ekvdef\ekvd@set{#2}%
490 {#1\setbox#3=\hbox{\begingroup##1\endgroup}}%
491 }%
492 }
493 % \end{macrocode}
494 % \end{macro}
495 % \end{macro}
496 %
497 % \begin{macro}{toks,gtoks}
498 % \begin{macro}[internal]{\ekvd@type@toks}
499 % Similar to |box|, but set the |toks|.
500 % \begin{macrocode}
501 \ekvd@def@type@fwd{toks}{\ekvd@type@toks{}}
502 \ekvd@def@type@fwd{gtoks}{\ekvd@type@toks\global}
503 \protected\def\ekvd@type@toks#1#2#3%
504 {%
505 \ekvd@do@new{}{#2}%
506 \ekvd@assert@filledarg{#3}%
507 \ekvd@newreg#3{toks}%
508 \ekvd@ifalso
509 {%
510 \let\ekvd@prot\protected
511 \ekvd@add@val{#2}{#1#3={##1}}{}%
512 }%
513 {\protected\ekvd@long\ekvdef\ekvd@set{#2}{#1#3={##1}}}%
514 }
515 % \end{macrocode}
516 % \end{macro}
517 % \end{macro}
518 %
519 % \begin{macro}[internal]{\ekvd@type@preapptoks,\ekvd@t@apptoks,\ekvd@t@gapptoks}
520 % Just like |toks|, but expand the current contents of the |toks| register to
521 % append the new contents.
522 % \begin{macrocode}
523 \ekvd@ifprimitive\toksapp
524 {%
525 \ekvd@def@type@fwd{apptoks}{\ekvd@type@preapptoks\toksapp}
526 \ekvd@def@type@fwd{gapptoks}{\ekvd@type@preapptoks\gtoksapp}
527 \ekvd@def@type@fwd{pretoks}{\ekvd@type@preapptoks\tokspre}
528 \ekvd@def@type@fwd{gpretoks}{\ekvd@type@preapptoks\gtokspre}
529 \protected\def\ekvd@type@preapptoks#1#2#3%
530 {%
531 \ekvd@do@new{}{#2}%
532 \ekvd@assert@filledarg{#3}%
533 \ekvd@newreg#3{toks}%
534 \ekvd@ifalso
535 {%
536 \let\ekvd@prot\protected
537 \ekvd@add@val{#2}{#1#3{##1}}{}%
538 }%
539 {\protected\ekvd@long\ekvdef\ekvd@set{#2}{#1#3{##1}}}%
540 }
541 }
542 {%
543 \ekvd@def@type@fwd{apptoks}{\ekvd@type@apptoks{}}
544 \ekvd@def@type@fwd{gapptoks}{\ekvd@type@apptoks\global}
545 \protected\def\ekvd@type@apptoks#1#2#3%
546 {%
547 \ekvd@do@new{}{#2}%
548 \ekvd@assert@filledarg{#3}%
549 \ekvd@newreg#3{toks}%
550 \ekvd@ifalso
551 {%
552 \let\ekvd@prot\protected
553 \ekvd@add@val{#2}{#1#3=\expandafter{\the#3##1}}{}%
554 }%
555 {%
556 \protected\ekvd@long\ekvdef\ekvd@set{#2}%
557 {#1#3=\expandafter{\the#3##1}}%
558 }%
559 }
560 \ekvd@def@type@fwd{pretoks}{\ekvd@type@pretoks{}}
561 \ekvd@def@type@fwd{gpretoks}{\ekvd@type@pretoks\global}
562 \newtoks\ekvd@toks
563 \protected\def\ekvd@type@pretoks#1#2#3%
564 {%
565 \ekvd@do@new{}{#2}%
566 \ekvd@assert@filledarg{#3}%
567 \ekvd@newreg#3{toks}%
568 \ekvd@ifalso
569 {%
570 \let\ekvd@prot\protected
571 \ekvd@add@val{#2}%
572 {#1#3=\ekv@expanded{{\ekv@unexpanded{##1}\the#3}}}%
573 {}%
574 }%
575 {%
576 \protected\ekvd@long\ekvdef\ekvd@set{#2}%
577 {#1#3=\ekv@expanded{{\ekv@unexpanded{##1}\the#3}}}%
578 }%
579 }
580 }
581 % \end{macrocode}
582 % \end{macro}
583 %
584 % \begin{macro}
585 % {int,eint,gint,xint,dimen,edimen,gdimen,xdimen,skip,eskip,gskip,xskip}
586 % \begin{macro}[internal]{\ekvd@type@register}
587 % The |\ekvd@type@register| can handle all the types for which the assignment will
588 % just be \texttt{\meta{register}=\meta{value}}.
589 % \begin{macrocode}
590 \ekvd@def@type@fwd {int}{\ekvd@type@register{count}{}{}}
591 \ekvd@def@type@fwd{eint}{\ekvd@type@register{count}{}\numexpr}
592 \ekvd@def@type@fwd{gint}{\ekvd@type@register{count}\global{}}
593 \ekvd@def@type@fwd{xint}{\ekvd@type@register{count}\global\numexpr}
594 \ekvd@def@type@fwd {dimen}{\ekvd@type@register{dimen}{}{}}
595 \ekvd@def@type@fwd{edimen}{\ekvd@type@register{dimen}{}\dimexpr}
596 \ekvd@def@type@fwd{gdimen}{\ekvd@type@register{dimen}\global{}}
597 \ekvd@def@type@fwd{xdimen}{\ekvd@type@register{dimen}\global\dimexpr}
598 \ekvd@def@type@fwd {skip}{\ekvd@type@register{skip}{}{}}
599 \ekvd@def@type@fwd{eskip}{\ekvd@type@register{skip}{}\glueexpr}
600 \ekvd@def@type@fwd{gskip}{\ekvd@type@register{skip}\global{}}
601 \ekvd@def@type@fwd{xskip}{\ekvd@type@register{skip}\global\glueexpr}
602 \protected\def\ekvd@type@register#1#2#3#4#5%
603 {%
604 \ekvd@do@new{}{#4}%
605 \ekvd@assert@filledarg{#5}%
606 \ekvd@newreg#5{#1}%
607 \ekvd@ifalso
608 {%
609 \let\ekvd@prot\protected
610 \ekvd@add@val{#4}{#2#5=#3##1\relax}{}%
611 }%
612 {\protected\ekvd@long\ekvdef\ekvd@set{#4}{#2#5=#3##1\relax}}%
613 }
614 % \end{macrocode}
615 % \end{macro}
616 % \end{macro}
617 %
618 % \begin{macro}{store,gstore,estore,xstore}
619 % \begin{macro}[internal]{\ekvd@type@store}
620 % The none-expanding |store| types use an |\edef| or |\xdef| and |\unexpanded|
621 % to be able to also store |#| easily.
622 % \begin{macrocode}
623 \ekvd@def@type@fwd{store}{\ekvd@type@store\edef{\ekv@unexpanded{##1}}}
624 \ekvd@def@type@fwd{gstore}{\ekvd@type@store\xdef{\ekv@unexpanded{##1}}}
625 \ekvd@def@type@fwd{estore}
626 {\ekvd@type@store\edef{\ekv@unexpanded\ekv@expanded{{##1}}}}
627 \ekvd@def@type@fwd{xstore}
628 {\ekvd@type@store\xdef{\ekv@unexpanded\ekv@expanded{{##1}}}}
629 \protected\def\ekvd@type@store#1#2#3#4%
630 {%
631 \ekvd@do@new{}{#3}%
632 \ekvd@assert@filledarg{#4}%
633 \ekvd@newlet#4\ekv@empty
634 \ekvd@ifalso
635 {%
636 \let\ekvd@prot\protected
637 \ekvd@add@val{#3}{#1#4{#2}}{}%
638 }%
639 {\protected\ekvd@long\ekvdef\ekvd@set{#3}{#1#4{#2}}}%
640 }
641 % \end{macrocode}
642 % \end{macro}
643 % \end{macro}
644 %
645 % \begin{macro}{meta,nmeta}
646 % \begin{macro}[internal]{\ekvd@type@meta}
647 % |meta| sets up things such that another instance of |\ekvset| will be run on
648 % the argument, with the same \set.
649 % \begin{macrocode}
650 \ekvd@def@type@fwd{meta}
651 {\ekvd@type@meta{}{##1}\ekvd@add@val{{##1}}{}\ekv@exparg}
652 \ekvd@def@type@fwd{nmeta}
653 {%
654 \ekvd@assert@not@long
655 \ekvd@type@meta
656 {NoVal}{}\ekvd@add@noval{}\ekvd@assert@not@long@also\ekv@expargtwice
657 }
658 \protected\long\def\ekvd@type@meta#1#2#3#4#5#6#7#8%
659 {%
660 \ekvd@do@new{#1}{#7}%
661 \ekvd@assert@filledarg{#8}%
662 #6{\ekvd@prot\ekvd@long\def\ekvd@tmp#2}{\ekvmorekv{#8}}%
663 \ekvd@ifalso
664 {\ekv@exparg{#3{#7}}{\ekvd@tmp#4}{#5}}%
665 {\csname ekvlet#1\endcsname\ekvd@set{#7}\ekvd@tmp}%
666 }
667 % \end{macrocode}
668 % \end{macro}
669 % \end{macro}
670 %
671 % \begin{macro}{smeta,snmeta}
672 % \begin{macro}[internal]
673 % {\ekvd@type@smeta,\ekvd@type@smeta@}
674 % |smeta| is pretty similar to |meta|, but needs two arguments inside of \val,
675 % such that the first is the \set\ for which the sub-|\ekvset| and the second is
676 % the \kv\ list.
677 % \begin{macrocode}
678 \ekvd@def@type@fwd{smeta}{\ekvd@type@smeta{}{##1}\ekvd@add@val{{##1}}{}}
679 \ekvd@def@type@fwd{snmeta}
680 {%
681 \ekvd@assert@not@long
682 \ekvd@type@smeta{NoVal}{}\ekvd@add@noval{}\ekvd@assert@not@long@also
683 }
684 \protected\long\def\ekvd@type@smeta#1#2#3#4#5#6#7%
685 {%
686 \ekvd@do@new{#1}{#6}%
687 \ekvd@assert@twoargs{#7}%
688 \ekvd@type@smeta@#7{#2}%
689 \ekvd@ifalso
690 {\ekv@exparg{#3{#6}}{\ekvd@tmp#4}{#5}}%
691 {\csname ekvlet#1\endcsname\ekvd@set{#6}\ekvd@tmp}%
692 }
693 \protected\long\def\ekvd@type@smeta@#1#2#3%
694 {\ekvd@prot\ekvd@long\ekv@expargtwice{\def\ekvd@tmp#3}{\ekvset{#1}{#2}}}
695 % \end{macrocode}
696 % \end{macro}
697 % \end{macro}
698 %
699 % \begin{macro}{alias}
700 % A few more or less standard tests and then copy over the definitions.
701 % \begin{macrocode}
702 \ekvd@def@type{alias}
703 {%
704 \ekvd@assert@arg
705 \ekvd@assert@not@protected
706 \ekvd@assert@not@long
707 \ekvd@assert@not@also
708 % \end{macrocode}
709 % In order to do nothing if |new| should be checked and the error condition
710 % for either the |Val|- or |NoVal|-key is met we need to check for the defined
711 % keys without actually copying here (the alternative would be using an
712 % auxiliary variable which we would then need to check anyway).
713 % \begin{macrocode}
714 \ekvifdefined\ekvd@set{#3}{\ekvd@do@new{}{#1}}{}%
715 \ekvifdefinedNoVal\ekvd@set{#3}{\ekvd@do@new{NoVal}{#1}}{}%
716 % \end{macrocode}
717 % Assert that at least one of |Val|- or |NoVal|-keys exist.
718 % \begin{macrocode}
719 \ekvpAssertTF
720 {\ekvifdefined\ekvd@set{#3}\@firstoftwo{\ekvifdefinedNoVal\ekvd@set{#3}}}%
721 {undefined key `#3'}%
722 % \end{macrocode}
723 % Copy the definitions.
724 % \begin{macrocode}
725 \ekvifdefined\ekvd@set{#3}{\ekvletkv\ekvd@set{#1}\ekvd@set{#3}}{}%
726 \ekvifdefinedNoVal\ekvd@set{#3}{\ekvletkvNoVal\ekvd@set{#1}\ekvd@set{#3}}{}%
727 }
728 % \end{macrocode}
729 % \end{macro}
730 %
731 % \begin{macro}{choice}
732 % \begin{macro}[internal]{\ekvd@type@choice,\ekvd@populate@choice}
733 % The real key definition of a |choice| type is pretty simple, the heavy
734 % lifting is done by a helper macro at run time. Though setting up the choices
735 % needs a bit of work. First the key macro definition.
736 % \begin{macrocode}
737 \protected\def\ekvd@type@choice#1%
738 {%
739 \ekvd@assert@not@long
740 \ekv@expargtwice{\ekvd@prot\def\ekvd@tmp##1}%
741 {%
742 \expandafter\expandafter\expandafter
743 \ekvd@h@choice
744 \expandafter\expandafter\expandafter
745 {\expandafter\ekvd@choice@name\expandafter{\ekvd@set}{#1}{##1}}%
746 }%
747 \ekvd@ifalso
748 {%
749 \ekvd@assert@val{#1}%
750 \ekvd@if@not@already@choice{#1}%
751 {%
752 \ekv@exparg
753 {%
754 \expandafter\ekvd@add@aux
755 \csname\ekv@name\ekvd@set{#1}\endcsname{{##1}}{#1}%
756 }%
757 {\ekvd@tmp{##1}}%
758 {\ekvd@long\ekvdef}\ekvd@assert@not@long@also
759 }%
760 }%
761 {\ekvlet\ekvd@set{#1}\ekvd@tmp}%
762 }
763 % \end{macrocode}
764 % The set up of different choices is done through another parser, that one
765 % needs relatively few types though.
766 % \begin{macrocode}
767 \ekvpNewParser{ekvd@populate@choice}
768 \ekvpValueAlwaysRequired{ekvd@populate@choice}
769 \ekvpDefType{ekvd@populate@choice}{protect}
770 {\ekvd@populate@choice\protected{#1}{#3}}
771 \ekvpDefType{ekvd@populate@choice}{unprotect}{\ekvd@populate@choice{}{#1}{#3}}
772 \ekvpDefNoType{ekvd@populate@choice} {\ekvd@populate@choice{}{#1}{#3}}
773 \ekvpLet{ekvd@populate@choice}{type}{protected}{protect}
774 \ekvpLet{ekvd@populate@choice}{type}{unprotected}{unprotect}
775 % \end{macrocode}
776 % The choice definition is done by the following little helper:
777 % \begin{macrocode}
778 \protected\long\def\ekvd@populate@choice#1#2#3%
779 {%
780 #1\expandafter\edef
781 \csname\ekvd@choice@name\ekvd@set\ekvd@set@choice{#2}\endcsname
782 {\ekv@unexpanded{#3}}%
783 }
784 % \end{macrocode}
785 % And we define the real type:
786 % \begin{macrocode}
787 \ekvd@def@type{choice}
788 {%
789 \ekvd@do@new{}{#1}%
790 \ekvd@assert@arg
791 \ekvd@type@choice{#1}%
792 \def\ekvd@set@choice{#1}%
793 \ekvpParse@unsafe\ekvp@@p@ekvd@populate@choice{#3}%
794 }
795 % \end{macrocode}
796 % \end{macro}
797 % \end{macro}
798 %
799 % \begin{macro}{choice-store,choice-enum}
800 % These two types define a special kind of |choice| key and are quite similar,
801 % the only difference is what the different choices do (hence they use a
802 % shared initialisation which differs in the chosen |populate| step).
803 % \begin{macrocode}
804 \ekvd@def@type@fwd{choice-store}
805 {\ekvd@type@choicespecial\ekvd@populate@choicestore}
806 \ekvd@def@type@fwd{choice-enum}
807 {\ekvd@type@choicespecial\ekvd@populate@choiceenum}
808 % \end{macrocode}
809 % \begin{macro}[internal]{\ekvd@type@choicespecial}
810 % Initialise similar to a |choice| key. The difference is that we require two
811 % arguments (which we assert), a macro to store things in, and a |csv|-list
812 % containing the allowed values. |#1| is the |populate| macro according to the
813 % type used.
814 % \begin{macrocode}
815 \protected\long\def\ekvd@type@choicespecial#1#2#3%
816 {%
817 \ekvd@do@new{}{#2}%
818 \ekvd@assert@twoargs{#3}%
819 \ekvd@type@choice{#2}%
820 \def\ekvd@set@choice{#2}%
821 #1#3%
822 }
823 % \end{macrocode}
824 % \end{macro}
825 % \begin{macro}[internal]
826 % {\ekvd@populate@choicestore,\ekvd@populate@choicestore@}
827 % We initialise the storing macro if it doesn't yet exist, and then we loop
828 % over the value list. The |\edef|s with |\unexpanded| are both necessary to
829 % be able to store macro parameter tokens (the outer protects at define time,
830 % the inner at use time).
831 % \begin{macrocode}
832 \protected\long\def\ekvd@populate@choicestore#1%
833 {%
834 \ekvd@newlet#1\ekv@empty
835 \ekvparse{\ekvd@populate@choicestore@k#1}{\ekvd@populate@choicestore@kv#1}%
836 }
837 \protected\long\def\ekvd@populate@choicestore@kv#1#2#3%
838 {%
839 \protected\expandafter\edef
840 \csname\ekvd@choice@name\ekvd@set\ekvd@set@choice{#2}\endcsname
841 {\ekv@unexpanded{\edef#1{\ekv@unexpanded{#3}}}}%
842 }
843 \ekv@exparg{\protected\long\def\ekvd@populate@choicestore@k#1#2}%
844 {\ekvd@populate@choicestore@kv{#1}{#2}{#2}}
845 % \end{macrocode}
846 % \end{macro}
847 % \begin{macro}[internal]
848 % {\ekvd@populate@choiceenum,\ekvd@populate@choiceenum@}
849 % This is similar to the population of a |choice-store| type, but instead of
850 % storing the values in a macro this initialises a count and stores the
851 % position of the value in the list inside that count (zero-indexed). The
852 % space is necessary to terminate the number scanning, which is the reason we
853 % use |\@firstofone| (so that the space after the macro name isn't gobbled by
854 % \TeX).
855 % \begin{macrocode}
856 \protected\long\def\ekvd@populate@choiceenum#1%
857 {%
858 \ekvd@newreg#1{count}%
859 \def\ekvd@tmp{0}%
860 \ekvcsvloop{\ekvd@populate@choiceenum@#1}%
861 }
862 \protected\long\def\ekvd@populate@choiceenum@#1#2%
863 {%
864 \protected\expandafter\edef
865 \csname\ekvd@choice@name\ekvd@set\ekvd@set@choice{#2}\endcsname
866 {#1=\@firstofone{\ekvd@tmp} }%
867 \edef\ekvd@tmp{\the\numexpr\ekvd@tmp+1\relax}%
868 }
869 % \end{macrocode}
870 % \end{macro}
871 % \end{macro}
872 %
873 % \begin{macro}{choice-aliases}
874 % \begin{macro}[internal]{\ekvd@t@choicealias}
875 % Choice alias shall copy over separate choices to new names for the same key.
876 % The code is pretty straight forward, we just make sure no disallowed
877 % prefixes were used, the key exists, is already a choice key, and then we
878 % loop through the list of aliases.
879 % \begin{macrocode}
880 \ekvd@def@type{choice-aliases}
881 {%
882 \ekvd@assert@arg
883 \ekvd@assert@not@protected
884 \ekvd@assert@not@long
885 \ekvd@assert@not@also
886 \ekvd@assert@already@choice{#1}%
887 \ekvparse\ekvd@err@missing@value{\ekvd@t@choicealias{#1}}{#3}%
888 }
889 % \end{macrocode}
890 % The copying of the definitions is rather straight forward, check if choice
891 % of that name exists, and if so copy it. The |new| prefix will apply for each
892 % individual new choice, so we need to check this here.
893 % \begin{macrocode}
894 \protected\long\def\ekvd@t@choicealias#1#2#3%
895 {%
896 \ifx\ekvd@do@new\ekvd@assert@new
897 \ekv@fi@firstofone
898 \fi
899 \@gobble
900 {%
901 \ekv@ifdefined{\ekvd@choice@name\ekvd@set{#1}{#2}}%
902 {\ekvd@err@not@new@choice{#2}\ekv@gobbleto@stop}%
903 {}%
904 }%
905 \ekv@ifdefined{\ekvd@choice@name\ekvd@set{#1}{#3}}%
906 {%
907 \expandafter\let
908 \csname\ekvd@choice@name\ekvd@set{#1}{#2}\expandafter\endcsname
909 \csname\ekvd@choice@name\ekvd@set{#1}{#3}\endcsname
910 }%
911 {\ekvd@err@undefined@choice{#1}{#3}}%
912 \@gobble\ekv@stop
913 }
914 % \end{macrocode}
915 % \end{macro}
916 % \end{macro}
917 %
918 % \begin{macro}{unknown-choice}
919 % \begin{macrocode}
920 \ekvd@def@type{unknown-choice}
921 {%
922 \ekvd@do@new@for@name{\ekvd@unknown@choice@name\ekvd@set{#1}}%
923 \ekvd@assert@arg
924 \ekvd@assert@not@long
925 \ekvd@assert@not@also
926 \ekvd@prot\expandafter
927 \def\csname\ekvd@unknown@choice@name\ekvd@set{#1}\endcsname##1{#3}%
928 }
929 % \end{macrocode}
930 % \end{macro}
931 %
932 % \begin{macro}{unknown}
933 % \begin{macro}[internal]{\ekvd@type@unknown@code,\ekvd@type@unknown@noval}
934 % The |unknown| type has different subtypes which would be the key names for
935 % other types. It is first checked whether that subtype is defined, if it
936 % isn't throw an error, else use that subtype.
937 % \begin{macrocode}
938 \ekvd@def@type{unknown}
939 {%
940 \ekv@ifdefined{ekvd@type@unknown@\detokenize{#1}}%
941 {\csname ekvd@type@unknown@\detokenize{#1}\endcsname{#3}}%
942 \ekvd@err@misused@unknown
943 }
944 % \end{macrocode}
945 % The |unknown noval| type can use |\ekvdefunknownNoVal| directly (after
946 % asserting some prefixes).
947 % \begin{macrocode}
948 \protected\long\def\ekvd@type@unknown@noval#1%
949 {%
950 \ekvd@do@new@for@name{\ekv@name\ekvd@set{}uN}%
951 \ekvd@assert@arg
952 \ekvd@assert@not@also
953 \ekvd@prot\ekvd@long\ekvdefunknownNoVal\ekvd@set{#1}%
954 }
955 % \end{macrocode}
956 % The |unknown code| type uses some trickery during the definition in order to
957 % swap out |#1| and |#2| in the user supplied definition. This is done via a
958 % temporary macro that stores the definition but gets the parameter numbers
959 % reversed while the real definition is done.
960 % \begin{macrocode}
961 \protected\long\def\ekvd@type@unknown@code#1%
962 {%
963 \ekvd@do@new@for@name{\ekv@name\ekvd@set{}u}%
964 \ekvd@assert@arg
965 \ekvd@assert@not@also
966 \begingroup
967 \def\ekvd@tmp##1##2##3{#1}%
968 \ekv@exparg
969 {%
970 \endgroup
971 \ekvd@prot\ekvd@long\ekvdefunknown\ekvd@set
972 }%
973 {\ekvd@tmp{##2}{##1}{##3}}%
974 }
975 % \end{macrocode}
976 % \end{macro}
977 % \end{macro}
978 %
979 % \begin{macro}[internal]
980 % {
981 % \ekvd@type@unknown@redirect,
982 % \ekvd@type@unknown@redirect-code,
983 % \ekvd@type@unknown@redirect-noval
984 % }
985 % The |unknown redirect| types also just forward to |\ekvredirectunknown|
986 % after asserting some prefixes.
987 % \begin{macrocode}
988 \protected\edef\ekvd@type@unknown@redirect#1%
989 {%
990 \expandafter\noexpand\csname ekvd@type@unknown@redirect-code\endcsname{#1}%
991 \expandafter\noexpand\csname ekvd@type@unknown@redirect-noval\endcsname{#1}%
992 }
993 \protected\expandafter\def\csname ekvd@type@unknown@redirect-code\endcsname#1%
994 {%
995 \ekvd@do@new@for@name{\ekv@name\ekvd@set{}u}%
996 \ekvd@assert@arg
997 \ekvd@assert@not@also
998 \ekvd@assert@not@protected
999 \expandafter\ekvredirectunknown\expandafter{\ekvd@set}{#1}%
1000 }
1001 \protected\expandafter\def\csname ekvd@type@unknown@redirect-noval\endcsname#1%
1002 {%
1003 \ekvd@do@new@for@name{\ekv@name\ekvd@set{}uN}%
1004 \ekvd@assert@arg
1005 \ekvd@assert@not@also
1006 \ekvd@assert@not@protected
1007 \expandafter\ekvredirectunknownNoVal\expandafter{\ekvd@set}{#1}%
1008 }
1009 % \end{macrocode}
1010 % \end{macro}
1011 %
1012 % \subsubsection{Key Type Helpers}
1013 % There are some keys that might need helpers during their execution (not during
1014 % their definition, which are gathered as |@type@| macros). These helpers are
1015 % named |@h@|.
1016 %
1017 % \begin{macro}[internal]{\ekvd@h@choice,\ekvd@h@choice@}
1018 % The |choice| helper will just test whether the given choice was defined, if
1019 % not throw an error expandably, else call the macro which stores the code for
1020 % this choice.
1021 % \begin{macrocode}
1022 \def\ekvd@h@choice#1%
1023 {%
1024 \expandafter\ekvd@h@choice@
1025 \csname\ifcsname#1\endcsname#1\else relax\fi\endcsname
1026 {#1}%
1027 }
1028 \def\ekvd@h@choice@#1#2%
1029 {%
1030 \ifx#1\relax
1031 \ekvd@err@choice@invalid{#2}%
1032 \expandafter\@gobble
1033 \fi
1034 #1%
1035 }
1036 % \end{macrocode}
1037 % \end{macro}
1038 %
1039 %
1040 % \subsubsection{Handling \texttt{also}}
1041 %
1042 % \begin{macro}[internal]
1043 % {\ekvd@add@val,\ekvd@add@noval,\ekvd@add@aux,\ekvd@add@aux@}
1044 % \begin{macrocode}
1045 \protected\long\def\ekvd@add@val#1#2#3%
1046 {%
1047 \ekvd@assert@val{#1}%
1048 \expandafter\ekvd@add@aux\csname\ekv@name\ekvd@set{#1}\endcsname{{##1}}%
1049 {#1}{#2}{\ekvd@long\ekvdef}{#3}%
1050 }
1051 \protected\long\def\ekvd@add@noval#1#2#3%
1052 {%
1053 \ekvd@assert@noval{#1}%
1054 \expandafter\ekvd@add@aux\csname\ekv@name\ekvd@set{#1}N\endcsname{}%
1055 {#1}{#2}\ekvdefNoVal{#3}%
1056 }
1057 \protected\long\def\ekvd@add@aux#1#2%
1058 {%
1059 \ekvd@extract@prefixes#1%
1060 \expandafter\ekvd@add@aux@\expandafter{#1#2}%
1061 }
1062 % \end{macrocode}
1063 % Once we're done with adding something to the definition of a key we need to
1064 % clear the prefixes. Maybe it would be better to only restore them if they
1065 % were changed by the |\ekvd@extract@prefixes| mechanism, but I think this is
1066 % fine with just resetting all (there should be no code dependent on any of
1067 % the prefix storing macros after this was run).
1068 % \begin{macrocode}
1069 \protected\long\def\ekvd@add@aux@#1#2#3#4#5%
1070 {%
1071 #5%
1072 \ekvd@prot#4\ekvd@set{#2}{#1#3}%
1073 \ekvd@clear@prefixes
1074 }
1075 % \end{macrocode}
1076 % \end{macro}
1077 %
1078 % \begin{macro}[internal]
1079 % {
1080 % \ekvd@extract@prefixes,\ekvd@extract@prefixes@,
1081 % \ekvd@extract@prefixes@long,\ekvd@extract@prefixes@prot
1082 % }
1083 % This macro checks which prefixes were used for the definition of a macro and
1084 % sets |\ekvd@long| and |\ekvd@prot| accordingly.
1085 % \begin{macrocode}
1086 \protected\def\ekvd@extract@prefixes#1%
1087 {%
1088 \expandafter\ekvd@extract@prefixes@\meaning#1\ekvd@stop
1089 }
1090 % \end{macrocode}
1091 % In the following definition |#1| will get replaced by |macro:|, |#2| by
1092 % |\long| and |#3| by |\protected| (in each, all tokens will have category
1093 % other). This allows us to parse the |\meaning| of a macro for those strings.
1094 % \begin{macrocode}
1095 \protected\def\ekvd@extract@prefixes@#1#2#3%
1096 {%
1097 \protected\def\ekvd@extract@prefixes@##1#1##2\ekvd@stop
1098 {%
1099 \ekvd@extract@prefixes@long
1100 ##1\ekvd@mark\@firstofone#2\ekvd@mark\@gobble\ekvd@stop
1101 {\let\ekvd@long\long}%
1102 \ekvd@extract@prefixes@prot
1103 ##1\ekvd@mark\@firstofone#3\ekvd@mark\@gobble\ekvd@stop
1104 {\let\ekvd@prot\protected}%
1105 }%
1106 \protected\def\ekvd@extract@prefixes@long##1#2##2\ekvd@mark##3##4\ekvd@stop
1107 {##3}%
1108 \protected\def\ekvd@extract@prefixes@prot##1#3##2\ekvd@mark##3##4\ekvd@stop
1109 {##3}%
1110 }
1111 % \end{macrocode}
1112 % We use a temporary macro to expand the three arguments of
1113 % |\ekvd@extract@prefixes@|, which will set up the real meaning of itself and
1114 % the parsing for |\long| and |\protected|.
1115 % \begin{macrocode}
1116 \begingroup
1117 \edef\ekvd@tmp
1118 {%
1119 \endgroup
1120 \ekvd@extract@prefixes@
1121 {\detokenize{macro:}}%
1122 {\string\long}%
1123 {\string\protected}%
1124 }
1125 \ekvd@tmp
1126 % \end{macrocode}
1127 % \end{macro}
1128 %
1129 %
1130 % \subsubsection{Tests}
1131 %
1132 % \begin{macro}[internal]{\ekvd@newlet,\ekvd@newreg}
1133 % These macros test whether a control sequence is defined, if it isn't they
1134 % define it, either via |\let| or via the correct \cs[no-index]{new\meta{reg}}.
1135 % \begin{macrocode}
1136 \protected\def\ekvd@newlet#1#2%
1137 {%
1138 \ifdefined#1%
1139 \ifx#1\relax\ekv@fi@gobble\fi\@firstofone
1140 \ekv@fi@gobble
1141 \fi
1142 \@firstofone{\let#1#2}%
1143 }
1144 \protected\def\ekvd@newreg#1#2%
1145 {%
1146 \ifdefined#1%
1147 \ifx#1\relax\ekv@fi@gobble\fi\@firstofone
1148 \ekv@fi@gobble
1149 \fi
1150 \@firstofone{\csname new#2\endcsname#1}%
1151 }
1152 % \end{macrocode}
1153 % \end{macro}
1154 %
1155 % \begin{macro}[internal]
1156 % {\ekvd@assert@twoargs,\ekvd@ifnottwoargs,\ekvd@ifempty@gtwo}
1157 % A test for exactly two tokens can be reduced for an empty-test after gobbling
1158 % two tokens, in the case that there are fewer tokens than two in the argument,
1159 % only macros will be gobbled that are needed for the true branch, which doesn't
1160 % hurt, and if there are more this will not be empty.
1161 % \begin{macrocode}
1162 \long\def\ekvd@assert@twoargs#1%
1163 {\ekvd@ifnottwoargs{#1}{\ekvd@err@missing@definition\ekvpGobbleT}{}}
1164 \long\def\ekvd@ifnottwoargs#1%
1165 {%
1166 \ekvd@ifempty@gtwo#1\ekv@ifempty@B
1167 \ekv@ifempty@false\ekv@ifempty@A\ekv@ifempty@B\@firstoftwo
1168 }
1169 \long\def\ekvd@ifempty@gtwo#1#2{\ekv@ifempty@\ekv@ifempty@A}
1170 % \end{macrocode}
1171 % \end{macro}
1172 %
1173 % \begin{macro}[internal]
1174 % {
1175 % \ekvd@assert@val,\ekvd@assert@val@,\ekvd@assert@noval,\ekvd@assert@noval@,
1176 % \ekvd@extract@args,\ekvd@extracted@args,\ekvd@one@arg@string
1177 % }
1178 % Assert that a given key is defined as a value taking key or a |NoVal| key with
1179 % the correct argument structure, respectively.
1180 % \begin{macrocode}
1181 \protected\def\ekvd@assert@val#1%
1182 {%
1183 \ekvifdefined\ekvd@set{#1}%
1184 {\expandafter\ekvd@assert@val@\csname\ekv@name\ekvd@set{#1}\endcsname}%
1185 {%
1186 \ekvifdefinedNoVal\ekvd@set{#1}%
1187 \ekvd@err@add@val@on@noval
1188 {\ekvd@err@undefined@key{#1}}%
1189 \ekvpGobbleT
1190 }%
1191 }
1192 \protected\def\ekvd@assert@val@#1%
1193 {%
1194 \expandafter\ekvd@extract@args\meaning#1\ekvd@stop
1195 \unless\ifx\ekvd@extracted@args\ekvd@one@arg@string
1196 \ekvd@err@unsupported@arg
1197 \expandafter\ekvpGobbleT
1198 \fi
1199 }%
1200 \protected\def\ekvd@assert@noval#1%
1201 {%
1202 \ekvifdefinedNoVal\ekvd@set{#1}%
1203 {\expandafter\ekvd@assert@noval@\csname\ekv@name\ekvd@set{#1}N\endcsname}%
1204 {%
1205 \ekvifdefined\ekvd@set{#1}%
1206 \ekvd@err@add@noval@on@val
1207 {\ekvd@err@undefined@key{#1}}%
1208 \ekvpGobbleT
1209 }%
1210 }
1211 \protected\def\ekvd@assert@noval@#1%
1212 {%
1213 \expandafter\ekvd@extract@args\meaning#1\ekvd@stop
1214 \unless\ifx\ekvd@extracted@args\ekv@empty
1215 \ekvd@err@unsupported@arg
1216 \expandafter\ekvpGobbleT
1217 \fi
1218 }
1219 \protected\def\ekvd@extract@args#1%
1220 {%
1221 \protected\def\ekvd@extract@args##1#1##2->##3\ekvd@stop
1222 {\def\ekvd@extracted@args{##2}}%
1223 }
1224 \expandafter\ekvd@extract@args\expandafter{\detokenize{macro:}}
1225 \edef\ekvd@one@arg@string{\string#1}
1226 % \end{macrocode}
1227 % \end{macro}
1228 %
1229 % \begin{macro}[internal]{\ekvd@assert@arg,\ekvd@ifnoarg}
1230 % The |\ekvd@ifnoarg| macro is initialised as |\@secondoftwo|. Each time a key
1231 % without an argument is encountered it will be set to |\@firstoftwo| for the
1232 % scope of that key's parsing.
1233 % \begin{macrocode}
1234 \def\ekvd@assert@arg{\ekvd@ifnoarg{\ekvd@err@missing@definition\ekvpGobbleT}{}}
1235 \let\ekvd@ifnoarg\@secondoftwo
1236 % \end{macrocode}
1237 % \end{macro}
1238 %
1239 % \begin{macro}[internal]{\ekvd@assert@filledarg,\ekvd@ifnoarg@or@empty}
1240 % \begin{macrocode}
1241 \long\def\ekvd@assert@filledarg#1%
1242 {\ekvd@ifnoarg@or@empty{#1}{\ekvd@err@missing@definition\ekvpGobbleT}{}}
1243 \long\def\ekvd@ifnoarg@or@empty#1%
1244 {%
1245 \ekvd@ifnoarg
1246 \@firstoftwo
1247 {\ekv@ifempty{#1}}%
1248 }
1249 % \end{macrocode}
1250 % \end{macro}
1251 %
1252 % \begin{macro}[internal]
1253 % {
1254 % \ekvd@assert@not@long,\ekvd@assert@not@protected,\ekvd@assert@not@also,
1255 % \ekvd@assert@not@long@also,\ekvd@assert@not@protected@also,
1256 % \ekvd@assert@new,\ekvd@assert@not@new
1257 % }
1258 % Some key-types don't want to be |also|, |\long| or |\protected|, so we provide
1259 % macros to test this and throw an error, this could be silently ignored but now
1260 % users will learn to not use unnecessary stuff which slows the compilation
1261 % down.
1262 % \begin{macrocode}
1263 \def\ekvd@assert@not@long{\ifx\ekvd@long\long\ekvd@err@no@prefix{long}\fi}
1264 \def\ekvd@assert@not@protected
1265 {\ifx\ekvd@prot\protected\ekvd@err@no@prefix{protected}\fi}
1266 \def\ekvd@assert@not@also{\ekvd@ifalso{\ekvd@err@no@prefix{also}}{}}
1267 \def\ekvd@assert@not@long@also
1268 {\ifx\ekvd@long\long\ekvd@err@no@prefix@also{long}\fi}
1269 \def\ekvd@assert@not@protected@also
1270 {\ifx\ekvd@prot\protected\ekvd@err@no@prefix@also{protected}\fi}
1271 \def\ekvd@assert@new#1#2%
1272 {%
1273 \csname ekvifdefined#1\endcsname\ekvd@set{#2}%
1274 {\ekvd@err@not@new\ekvpGobbleT}%
1275 {}%
1276 }
1277 \def\ekvd@assert@not@new
1278 {\ifx\ekvd@do@new\ekvd@assert@new\ekvd@err@no@prefix{new}\fi}
1279 \def\ekvd@do@new@for@name#1%
1280 {%
1281 \ifx\ekvd@do@new\ekvd@assert@new
1282 \ekv@fi@firstofone
1283 \fi
1284 \@gobble{\ekv@ifdefined{#1}{\ekvd@err@not@new\ekvpGobbleT}{}}%
1285 }
1286 % \end{macrocode}
1287 % \end{macro}
1288 %
1289 % \begin{macro}[internal]
1290 % {
1291 % \ekvd@if@not@already@choice, \ekvd@if@not@already@choice@a,
1292 % \ekvd@if@not@already@choice@b
1293 % }
1294 % It is bad to use |also| on a key that already contains a |choice|, as both
1295 % choices would share the same valid values and thus lead to each callback being
1296 % used twice. The following is a rudimentary test against this.
1297 % \begin{macrocode}
1298 \protected\def\ekvd@if@not@already@choice#1%
1299 {%
1300 \ekvifdefined\ekvd@set{#1}%
1301 {%
1302 \expandafter
1303 \ekvd@if@not@already@choice@a
1304 \csname\ekv@name\ekvd@set{#1}\endcsname
1305 {}\ekvd@h@choice\ekvd@stop
1306 }%
1307 {\@firstofone}%
1308 }
1309 \protected\def\ekvd@if@not@already@choice@a
1310 {%
1311 \expandafter\ekvd@if@not@already@choice@b
1312 }
1313 \long\protected\def\ekvd@if@not@already@choice@b#1\ekvd@h@choice#2\ekvd@stop
1314 {%
1315 \ekv@ifempty{#2}\@firstofone\@gobble
1316 }
1317 % \end{macrocode}
1318 % \end{macro}
1319 %
1320 % \begin{macro}[internal]{\ekvd@assert@already@choice}
1321 % A small assertion that a key is defined as a choice key. With the test
1322 % |\ekvd@if@not@already@choice| this becomes pretty easy.
1323 % \begin{macrocode}
1324 \long\def\ekvd@assert@already@choice#1%
1325 {\ekvd@if@not@already@choice{#1}{\ekvd@err@not@choice\ekvpGobbleT}}
1326 % \end{macrocode}
1327 % \end{macro}
1328 %
1329 % \begin{macro}[internal]{\ekvd@ifspace,\ekvd@ifspace@}
1330 % Yet another test which can be reduced to an if-empty, this time by gobbling
1331 % everything up to the first space.
1332 % \begin{macrocode}
1333 \long\def\ekvd@ifspace#1%
1334 {%
1335 \ekvd@ifspace@#1 \ekv@ifempty@B
1336 \ekv@ifempty@false\ekv@ifempty@A\ekv@ifempty@B\@firstoftwo
1337 }
1338 \long\def\ekvd@ifspace@#1 % keep this space
1339 {%
1340 \ekv@ifempty@\ekv@ifempty@A
1341 }
1342 % \end{macrocode}
1343 % \end{macro}
1344 %
1345 % \subsubsection{Messages}
1346 %
1347 % Most messages of \expkvd\ are not expandable, since they only appear during
1348 % key-definition, which is not expandable anyway.
1349 %
1350 % \begin{macro}[internal]
1351 % {
1352 % \ekvd@errm,\ekvd@err@missing@definition,
1353 % \ekvd@err@missing@type,
1354 % \ekvd@err@undefined@prefix,\ekvd@err@undefined@key,
1355 % \ekvd@err@undefined@noval,\ekvd@err@undefined@choice,
1356 % \ekvd@err@no@prefix,\ekvd@err@no@prefix@also,
1357 % \ekvd@err@add@val@on@noval,\ekvd@err@add@noval@on@val,
1358 % \ekvd@err@unsupported@arg,\ekvd@err@not@new,\ekvd@err@not@new@choice,
1359 % \ekvd@err@not@choice,\ekvd@err@missing@value
1360 % }
1361 % The non-expandable error messages are boring, so here they are:
1362 % \begin{macrocode}
1363 \protected\long\def\ekvd@errm#1{\errmessage{expkv-def Error: #1}}
1364 \protected\def\ekvd@err@missing@definition
1365 {\ekvd@errm{Missing definition for key `\ekvd@cur'}}
1366 \protected\def\ekvd@err@missing@type
1367 {\ekvd@errm{Missing type prefix for key `\ekvd@cur'}}
1368 \protected\def\ekvd@err@undefined@prefix#1%
1369 {%
1370 \ekvd@errm
1371 {%
1372 Undefined prefix `\ekv@unexpanded{#1}' found while processing
1373 `\ekvd@cur'%
1374 }%
1375 }
1376 \protected\def\ekvd@err@undefined@key#1%
1377 {%
1378 \ekvd@errm
1379 {Undefined key `\ekv@unexpanded{#1}' found while processing `\ekvd@cur'}%
1380 }
1381 \protected\def\ekvd@err@undefined@noval#1%
1382 {%
1383 \ekvd@errm
1384 {%
1385 Undefined NoVal key `\ekv@unexpanded{#1}' found while processing
1386 `\ekvd@cur'%
1387 }%
1388 }
1389 \protected\def\ekvd@err@no@prefix#1%
1390 {\ekvd@errm{prefix `#1' not accepted in `\ekvd@cur'}}
1391 \protected\def\ekvd@err@no@prefix@also#1%
1392 {\ekvd@errm{`\ekvd@cur' not allowed with a `#1' key}}
1393 \protected\def\ekvd@err@add@val@on@noval
1394 {\ekvd@errm{`\ekvd@cur' not allowed with a NoVal key}}
1395 \protected\def\ekvd@err@add@noval@on@val
1396 {\ekvd@errm{`\ekvd@cur' not allowed with a value taking key}}
1397 \protected\def\ekvd@err@unsupported@arg
1398 {%
1399 \ekvd@errm
1400 {%
1401 Existing key-macro has the unsupported argument string
1402 `\ekvd@extracted@args' for key `\ekvd@cur'%
1403 }%
1404 }
1405 \protected\def\ekvd@err@not@new
1406 {\ekvd@errm{The key for `\ekvd@cur' is already defined}}
1407 \protected\def\ekvd@err@not@new@choice#1%
1408 {\ekvd@errm{The choice `\detokenize{#1}' is already defined for `\ekvd@cur'}}
1409 \protected\def\ekvd@err@misused@unknown
1410 {\ekvd@errm{Misuse of the unknown type found while processing `\ekvd@cur'}}
1411 \protected\long\def\ekvd@err@not@choice
1412 {\ekvd@errm{The key for `\ekvd@cur' is not an existing choice key}}
1413 \protected\long\def\ekvd@err@missing@value#1%
1414 {\ekvd@errm{Missing value for `#1' in `\ekvd@cur'}}
1415 \protected\long\def\ekvd@err@undefined@choice#1#2%
1416 {%
1417 \ekvd@errm
1418 {Undefined choice `#2' for key `#1' found while processing `\ekvd@cur'}%
1419 }
1420 % \end{macrocode}
1421 % \end{macro}
1422 %
1423 % \begin{macro}[internal]
1424 % {
1425 % \ekvd@err@choice@invalid,\ekvd@err@choice@invalid@,\ekvd@choice@name,
1426 % \ekvd@unknown@choice@name
1427 % }
1428 % |\ekvd@err@choice@invalid| will have to use this mechanism to throw its
1429 % message. Also we have to retrieve the name parts of the choice in an easy way,
1430 % so we use parentheses of catcode 8 here, which should suffice in most cases to
1431 % allow for a correct separation.
1432 % \begin{macrocode}
1433 \def\ekvd@err@choice@invalid#1%
1434 {%
1435 \ekvd@err@choice@invalid@#1%
1436 }
1437 \begingroup
1438 \catcode40=8
1439 \catcode41=8
1440 \@firstofone{\endgroup
1441 \def\ekvd@choice@name#1#2#3%
1442 {%
1443 ekvd#1(#2)\detokenize{#3}%
1444 }
1445 \def\ekvd@unknown@choice@name#1#2%
1446 {%
1447 ekvd:u:#1(#2)%
1448 }
1449 \def\ekvd@err@choice@invalid@ ekvd#1(#2)\detokenize#3%
1450 {%
1451 \ekv@ifdefined{\ekvd@unknown@choice@name{#1}{#2}}%
1452 {\csname\ekvd@unknown@choice@name{#1}{#2}\endcsname{#3}}%
1453 {\ekvd@err{invalid choice `#3' for `#2' in set `#1'}}%
1454 }
1455 }
1456 % \end{macrocode}
1457 % \end{macro}
1458 %
1459 % \begin{macro}[internal]{\ekvd@err}
1460 % The expandable error messages use |\ekvd@err|, which is just like |\ekv@err|
1461 % from \expkv. It uses a runaway argument to start the error message.
1462 % \begin{macrocode}
1463 \ekv@exparg{\long\def\ekvd@err#1}{\ekverr{expkv-def}{#1}}
1464 % \end{macrocode}
1465 % \end{macro}
1466 %
1467 % Now everything that's left is to reset the category code of |@|.
1468 % \begin{macrocode}
1469 \catcode`\@=\ekvd@tmp
1470 % \end{macrocode}
1471 %
1472 % \gobbledocstriptag
1473 %</tex>
1474 %^^A=<<