Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % File: expkv.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.sty}{\from{expkv.dtx}{pkg}}}
18 \generate{\file{expkv.tex}{\from{expkv.dtx}{tex}}}
19 \generate{\file{t-expkv.tex}{\from{expkv.dtx}{ctx}}}
20 \endbatchfile
21 %</driver>^^A=<<
22 % \fi
23 %
24 % \section{\expkv}
25 %^^A the LaTeX package >>=
26 % \subsection{The \LaTeX\ Package}
27 % First we set up the \LaTeX\ package. That one doesn't really do much except
28 % |\input|ting the generic code and identifying itself as a package. Additional
29 % an |all| option is declared to load all the sub modules in one go.
30 % \gobbledocstriptag
31 %<*pkg>
32 % \begin{macrocode}
33 \def\ekv@tmp
34 {%
35 \ProvidesFile{expkv.tex}%
36 [\ekvDate\space v\ekvVersion\space an expandable key=val implementation]%
37 }
38 \input{expkv.tex}
39 \ProvidesPackage{expkv}%
40 [\ekvDate\space v\ekvVersion\space an expandable key=val implementation]
41 \let\ekv@tmp\@empty
42 \DeclareOption{all}
43 {%
44 \def\ekv@tmp
45 {\RequirePackage{expkv-pop,expkv-cs,expkv-def,expkv-opt}}%
46 }
47 \ProcessOptions\relax
48 \ekv@tmp
49 % \end{macrocode}
50 % \gobbledocstriptag
51 %</pkg>
52 %^^A=<<
53 %^^A the ConTeXt module >>=
54 % \subsection{The \ConTeXt\ module}
55 % This is pretty straight forward, we just have to change the error throwing
56 % mechanism for \ConTeXt\ (the approach taken for plain and \LaTeX\ breaks in
57 % \ConTeXt, effectively breaking \ConTeXt, dropping you in an interactive \TeX\
58 % session with almost no means of escape).
59 % \gobbledocstriptag
60 %<*ctx>
61 % \begin{macrocode}
62 \writestatus{loading}{ConTeXt User Module / expkv}
63 \unprotect
64 \input expkv.tex
65 \long\def\ekv@err@collect#1\par#2%
66 {\directlua{tex.error[[\detokenize{#2} Error: #1]]}}
67 \writestatus{loading}
68 {ConTeXt User Module / expkv / Version \ekvVersion\space loaded}
69 \protect\endinput
70 % \end{macrocode}
71 % \gobbledocstriptag
72 %</ctx>
73 %^^A=<<
74 %^^A main file >>=
75 % \subsection{The Generic Code}
76 % The rest of this implementation will be the generic code.
77 % \gobbledocstriptag
78 %<*tex>
79 %
80 % We make sure that it's only input once:
81 % \begin{macrocode}
82 \expandafter\ifx\csname ekvVersion\endcsname\relax
83 \else
84 \expandafter\endinput
85 \fi
86 % \end{macrocode}
87 %
88 % Check whether \eTeX\ and |\expanded| are available -- \expkv\ requires \eTeX.
89 % \begin{macrocode}
90 \begingroup
91 \edef\ekvtmpa{\string\expanded}
92 \edef\ekvtmpb{\meaning\expanded}
93 \expandafter
94 \endgroup
95 \ifx\ekvtmpa\ekvtmpb
96 \expandafter\let\csname ekv@expanded\endcsname\expanded
97 \expandafter\let\csname ekv@unexpanded\endcsname\unexpanded
98 \else
99 \begingroup
100 \edef\ekvtmpa{\string\expanded}
101 \edef\ekvtmpb{\meaning\normalexpanded}
102 \expandafter
103 \endgroup
104 \ifx\ekvtmpa\ekvtmpb
105 \expandafter\let\csname ekv@expanded\endcsname\normalexpanded
106 \expandafter\let\csname ekv@unexpanded\endcsname\normalunexpanded
107 \else
108 \errmessage
109 {expkv Error: e-TeX and the \noexpand\expanded primitive required}%
110 \expandafter\endinput
111 \fi
112 \fi
113 % \end{macrocode}
114 %
115 % \begin{macro}{\ekvVersion,\ekvDate}
116 % We're on our first input, so lets store the version and date in a macro.
117 % \begin{macrocode}
118 \def\ekvVersion{2.1}
119 \def\ekvDate{2024-12-26}
120 % \end{macrocode}
121 % \end{macro}
122 %
123 % If the \LaTeX\ format is loaded we want to be a good file and report back who
124 % we are, for this the package will have defined |\ekv@tmp| to use
125 % |\ProvidesFile|, else this will expand to a |\relax| and do no harm.
126 % \begin{macrocode}
127 \csname ekv@tmp\endcsname
128 % \end{macrocode}
129 %
130 % Store the category code of |@| to later be able to reset it and change it to
131 % 11 for now.
132 % \begin{macrocode}
133 \expandafter\chardef\csname ekv@tmp\endcsname=\catcode`\@
134 \catcode`\@=11
135 % \end{macrocode}
136 % |\ekv@tmp| might later be reused to gobble any prefixes which might be
137 % provided to |\ekvdef| and similar in case the names are invalid, we just
138 % temporarily use it here as means to store the current category code of |@| to
139 % restore it at the end of the file, we never care for the actual definition of
140 % it.
141 %
142 % \begin{macro}[internal]{\ekv@if@lastnamedcs}
143 % If the primitive |\lastnamedcs| is available, we can be a bit faster than
144 % without it. So we test for this and save the test's result in this macro.
145 % \begin{macrocode}
146 \begingroup
147 \edef\ekv@tmpa{\string \lastnamedcs}
148 \edef\ekv@tmpb{\meaning\lastnamedcs}
149 \ifx\ekv@tmpa\ekv@tmpb
150 \def\ekv@if@lastnamedcs{\long\def\ekv@if@lastnamedcs##1##2{##1}}
151 \else
152 \def\ekv@if@lastnamedcs{\long\def\ekv@if@lastnamedcs##1##2{##2}}
153 \fi
154 \expandafter
155 \endgroup
156 \ekv@if@lastnamedcs
157 % \end{macrocode}
158 % \end{macro}
159 %
160 % \begin{macro}[internal]{\ekv@empty}
161 % Sometimes we have to introduce a token to prevent accidental brace stripping.
162 % This token would then need to be removed by |\@gobble| or similar. Instead we
163 % can use |\ekv@empty| which will just expand to nothing, that is faster than
164 % gobbling an argument.
165 % \begin{macrocode}
166 \def\ekv@empty{}
167 % \end{macrocode}
168 % \end{macro}
169 %
170 % \begin{macro}[internal]
171 % {
172 % \@gobble,\@gobbletwo,\@gobblethree,
173 % \@firstofone,\@firstoftwo,\@secondoftwo,
174 % \ekv@fi@gobble,\ekv@fi@firstofone,\ekv@fi@firstoftwo,\ekv@fi@secondoftwo,
175 % \ekv@gobble@mark,\ekv@gobbleto@stop,\ekv@gobble@from@mark@to@stop
176 % }
177 % Since branching tests are often more versatile than |\if...\else...\fi|
178 % constructs, we define helpers that are branching pretty fast. Also here are
179 % some other utility functions that just grab some tokens. The ones that are
180 % also contained in \LaTeX\ don't use the |ekv| prefix. Not all of the ones
181 % defined here are really needed by \expkv\ but are provided because packages
182 % like \expkvd\ or \expkvo\ need them (and I don't want to define them in each
183 % package which might need them).
184 % \begin{macrocode}
185 \long\def\@gobble#1{}
186 \long\def\@gobbletwo#1#2{}
187 \long\def\@gobblethree#1#2#3{}
188 \long\def\@firstofone#1{#1}
189 \long\def\@firstoftwo#1#2{#1}
190 \long\def\@secondoftwo#1#2{#2}
191 \long\def\ekv@fi@gobble\fi\@firstofone#1{\fi}
192 \long\def\ekv@fi@firstofone\fi\@gobble#1{\fi#1}
193 \long\def\ekv@fi@firstoftwo\fi\@secondoftwo#1#2{\fi#1}
194 \long\def\ekv@fi@secondoftwo\fi\@firstoftwo#1#2{\fi#2}
195 \def\ekv@gobble@mark\ekv@mark{}
196 \long\def\ekv@gobbleto@stop#1\ekv@stop{}
197 \long\def\ekv@gobble@from@mark@to@stop\ekv@mark#1\ekv@stop{}
198 % \end{macrocode}
199 % \end{macro}
200 % As you can see |\ekv@gobbleto@stop| uses a special marker |\ekv@stop|. The
201 % package will use three such markers, the one you've seen already, |\ekv@mark|
202 % and |\ekv@nil|. Contrarily to how for instance \pkg{expl3} does things, we
203 % don't define them, as we don't need them to have an actual meaning. This has
204 % the advantage that if they somehow get expanded -- which should never happen
205 % if things work out -- they'll throw an error directly.
206 %
207 % \begin{macro}[internal]
208 % {
209 % \ekv@ifempty,\ekv@ifempty@,\ekv@ifempty@true,\ekv@ifempty@false,
210 % \ekv@ifempty@true@F,\ekv@ifempty@true@F@gobble,
211 % \ekv@ifempty@true@F@gobbletwo
212 % }
213 % We can test for a lot of things building on an if-empty test, so lets define a
214 % really fast one. Since some tests might have reversed logic (true if something
215 % is not empty) we also set up macros for the reversed branches.
216 % \begin{macrocode}
217 \long\def\ekv@ifempty#1%
218 {%
219 \ekv@ifempty@\ekv@ifempty@A#1\ekv@ifempty@B\ekv@ifempty@true
220 \ekv@ifempty@A\ekv@ifempty@B\@secondoftwo
221 }
222 \long\def\ekv@ifempty@#1\ekv@ifempty@A\ekv@ifempty@B{}
223 \long\def\ekv@ifempty@true\ekv@ifempty@A\ekv@ifempty@B\@secondoftwo#1#2{#1}
224 \long\def\ekv@ifempty@false\ekv@ifempty@A\ekv@ifempty@B\@firstoftwo#1#2{#2}
225 \long\def\ekv@ifempty@true@F\ekv@ifempty@A\ekv@ifempty@B\@firstofone#1{}
226 \long\def\ekv@ifempty@true@F@gobble\ekv@ifempty@A\ekv@ifempty@B\@firstofone#1#2%
227 {}
228 \long\def\ekv@ifempty@true@F@gobbletwo
229 \ekv@ifempty@A\ekv@ifempty@B\@firstofone#1#2#3%
230 {}
231 % \end{macrocode}
232 % \end{macro}
233 %
234 % \begin{macro}[internal]
235 % {\ekv@ifblank,\ekv@ifblank@,\ekv@ifblank@false,\ekv@ifblank@gobbletrue}
236 % The obvious test that can be based on an if-empty is if-blank, meaning a test
237 % checking whether the argument is empty or consists only of spaces. But instead
238 % of building atop |\ekv@ifempty| our version will utilise the fact that the
239 % argument in none of our usages can contain |\ekv@stop| (due to the way we
240 % check for end-of-list), and hence we can use an even faster check. This here
241 % works due to the fact that \TeX\ will read at least one token (or a group) for
242 % an undelimited parameter which might be the end marker of the delimited next
243 % parameter. So if |#1| to |\ekv@ifblank| is indeed blank, |#1| of
244 % |\ekv@ifblank@| will be the placed |\ekv@stop| and |#2| will be anything until
245 % the next |\ekv@stop|. The wrapper |\ekv@ifblank| will not be used by \expkv\
246 % for speed reasons but \expkvo\ uses it. Also, not only a |TF| variant is
247 % provided but also a rather strange one that assumes that only the |F| branch
248 % is provided and that this branch consists only of a single token.
249 % \begin{macrocode}
250 \long\def\ekv@ifblank#1%
251 {\ekv@ifblank@#1\ekv@stop\ekv@ifblank@false\ekv@stop\@firstoftwo}
252 \long\def\ekv@ifblank@\ekv@mark#1#2\ekv@stop{}
253 \long\def\ekv@ifblank@false\ekv@stop\@firstoftwo#1#2{#2}
254 \def\ekv@ifblank@gobbletrue\ekv@stop#1{}
255 % \end{macrocode}
256 % \end{macro}
257 %
258 % \begin{macro}[internal]{\ekv@ifatmostone}
259 % Our check for at most one group or single non-space token is rather straight
260 % forward. There are in essence three cases.
261 %
262 % If |#1| is blank the auxiliary |\ekv@ifatmostone@| will read
263 % |\ekv@stop|\hspace{0pt}|\ekv@iflabk@false| as |#1#2| and |#3| will be empty.
264 % Leading to the test being true.
265 %
266 % If |#1| has exactly one group or single non-space token the auxiliary will
267 % read that argument plus |\ekv@stop| as |#1#2| and |\ekv@ifblank@false| as
268 % |#3|. Leading to the test being true.
269 %
270 % If |#1| has two or more normal arguments the auxiliary will gobble the first
271 % two as |#1#2| and the possibly empty remainder until the first |\ekv@stop|
272 % as |#3|. Then |\ekv@ifblank@false| kicks in and the test is indeed false.
273 % \begin{macrocode}
274 \long\def\ekv@ifatmostone#1%
275 {\ekv@ifatmostone@#1\ekv@stop\ekv@ifblank@false\ekv@stop\@firstoftwo}
276 \long\def\ekv@ifatmostone@#1#2#3\ekv@stop{}
277 % \end{macrocode}
278 % \end{macro}
279 %
280 % \begin{macro}[internal]{\ekv@ifdefined}
281 % We'll need to check whether something is defined quite frequently, so why not
282 % define a macro that does this. The following test is expandable and pretty
283 % fast. The version with |\lastnamedcs| is the fastest version to test for an
284 % undefined macro I know of (that considers both undefined macros and those with
285 % the meaning |\relax|).
286 % \begin{macrocode}
287 \ekv@if@lastnamedcs
288 {%
289 \long\def\ekv@ifdefined#1{\ifcsname#1\endcsname\ekv@ifdef@\fi\@secondoftwo}
290 \def\ekv@ifdef@\fi\@secondoftwo
291 {%
292 \fi
293 \expandafter\ifx\lastnamedcs\relax
294 \ekv@fi@secondoftwo
295 \fi
296 \@firstoftwo
297 }
298 }
299 {%
300 \long\def\ekv@ifdefined#1%
301 {%
302 \ifcsname#1\endcsname\ekv@ifdef@\fi\ekv@ifdef@false#1\endcsname\relax
303 \ekv@fi@secondoftwo
304 \fi
305 \@firstoftwo
306 }
307 \def\ekv@ifdef@\fi\ekv@ifdef@false{\fi\expandafter\ifx\csname}
308 \long\def\ekv@ifdef@false
309 #1\endcsname\relax\ekv@fi@secondoftwo\fi\@firstoftwo#2#3%
310 {#3}
311 }
312 % \end{macrocode}
313 % \end{macro}
314 %
315 % \begin{macro}[internal]{\ekv@strip,\ekv@strip@a,\ekv@strip@b,\ekv@strip@c}
316 % We borrow some ideas of \pkg{expl3}'s \pkg{l3tl} to strip spaces
317 % from keys and values. This |\ekv@strip| also strips one level of outer braces
318 % \emph{after} stripping spaces, so an input of | {abc} | becomes |abc| after
319 % stripping. It should be used with |#1| prefixed by |\ekv@mark|. Also this
320 % implementation at most strips \emph{one} space from both sides (which should
321 % be fine most of the time, since \TeX\ reads consecutive spaces as a single one
322 % during tokenisation).
323 % \begin{macrocode}
324 \def\ekv@strip#1%
325 {%
326 \long\def\ekv@strip##1%
327 {%
328 \ekv@strip@a
329 ##1\ekv@nil
330 \ekv@mark#1%
331 #1\ekv@nil
332 }%
333 \long\def\ekv@strip@a##1\ekv@mark#1{\ekv@strip@b##1\ekv@mark}%
334 }
335 \ekv@strip{ }
336 \long\def\ekv@strip@b#1 \ekv@nil{\ekv@strip@c#1\ekv@nil}
337 \long\def\ekv@strip@c\ekv@mark#1\ekv@nil\ekv@mark#2\ekv@nil#3{#3{#1}}
338 % \end{macrocode}
339 % \end{macro}
340 %
341 % \begin{macro}[internal]
342 % {\ekv@strip@key,\ekv@strip@key@a,\ekv@strip@key@b,\ekv@strip@key@c}
343 % Same idea and code as |\ekv@strip|, but the second argument to
344 % |\ekv@strip@key| must be a single token and the result of the stripping will
345 % be |\detokenize|d.
346 % \begin{macrocode}
347 \def\ekv@strip@key#1%
348 {%
349 \long\def\ekv@strip@key##1{\ekv@strip@key@a##1\ekv@nil\ekv@mark#1#1\ekv@nil}
350 \long\def\ekv@strip@key@a##1\ekv@mark#1{\ekv@strip@key@b##1\ekv@mark}
351 }
352 \ekv@strip@key{ }
353 \long\def\ekv@strip@key@b#1 \ekv@nil{\ekv@strip@key@c#1\ekv@nil}
354 \long\def\ekv@strip@key@c\ekv@mark#1\ekv@nil\ekv@mark#2\ekv@nil#3%
355 {\expandafter#3\detokenize{#1}\ekv@mark{#1}}
356 % \end{macrocode}
357 % \end{macro}
358 %
359 % \begin{macro}[internal]
360 % {
361 % \ekv@exparg,\ekv@exparg@,
362 % \ekv@expandedarg,\ekv@fexparg,\ekv@expandafter
363 % \ekv@expargtwice,\ekv@expargtwice@,\ekv@zero
364 % }
365 % To reduce some code doublets while gaining some speed (and also as convenience
366 % for other packages in the family), it is often useful to expand the first
367 % token in a definition once. Let's define a wrapper for this. And while we're
368 % at it, also define a helper for |f|-~and |e|-expansion as well.
369 %
370 % Also, to end a |\romannumeral| expansion, we want to use |\z@|, which is
371 % contained in both plain \TeX\ and \LaTeX, but we use a private name for it to
372 % make it easier to spot and hence easier to manage.
373 % \begin{macrocode}
374 \let\ekv@zero\z@
375 \long\def\ekv@exparg#1#2%
376 {\ekv@expanded{\ekv@unexpanded{#1}\expandafter}\expandafter{#2}}
377 \long\def\ekv@expandafter#1{\ekv@expanded{\ekv@unexpanded{#1}\expandafter}}
378 \long\def\ekv@exparg@#1#2{#2{#1}}%
379 \long\def\ekv@expandedarg#1#2{\ekv@expanded{\ekv@unexpanded{#1}{#2}}}
380 \long\def\ekv@fexparg#1#2%
381 {%
382 \ekv@expanded{\ekv@unexpanded{#1}\expandafter}\expandafter
383 {\romannumeral`\^^@#2}%
384 }
385 \long\def\ekv@expargtwice#1#2{\expandafter\ekv@expargtwice@\expandafter{#2}{#1}}
386 \def\ekv@expargtwice@{\expandafter\ekv@exparg@\expandafter}
387 % \end{macrocode}
388 % \end{macro}
389 %
390 % \begin{macro}{\ekvcsvloop}
391 % \begin{macro}[internal]{\ekv@csv@loop@active,\ekv@csv@loop@active@end}
392 % An |\ekvcsvloop| will just loop over a csv list in a simple manner. First we
393 % split at active commas (gives better performance this way), next we have to
394 % check whether we're at the end of the list (checking for |\ekv@stop|). If
395 % not we go on splitting at commas of category other.
396 % \begin{macrocode}
397 \begingroup
398 \def\ekvcsvloop#1{%
399 \endgroup
400 \long\def\ekvcsvloop##1##2%
401 {\ekv@csv@loop@active{##1}\ekv@mark##2#1\ekv@stop#1}
402 % \end{macrocode}
403 % This does the same as |\ekv@csv@loop| but for active commas.
404 % \begin{macrocode}
405 \long\def\ekv@csv@loop@active##1##2#1%
406 {%
407 \ekv@gobble@from@mark@to@stop##2\ekv@csv@loop@active@end\ekv@stop
408 \ekv@csv@loop{##1}##2,\ekv@stop,%
409 }%
410 \long\def\ekv@csv@loop@active@end
411 \ekv@stop
412 \ekv@csv@loop##1\ekv@mark\ekv@stop,\ekv@stop,%
413 {}%
414 }
415 % \end{macrocode}
416 % Do the definitions with the weird catcode.
417 % \begin{macrocode}
418 \catcode`\,=13
419 \ekvcsvloop,
420 % \end{macrocode}
421 % \end{macro}
422 % \end{macro}
423 %
424 % \begin{macro}[internal]{\ekv@csv@loop,\ekv@csv@loop@do,\ekv@csv@loop@end}
425 % We use temporary macros and an |\expandafter| chain to preexpand
426 % |\ekv@strip| here. After splitting at other commas we check again for end
427 % the end of the sublist, check for blank elements which should be ignored,
428 % and else strip spaces and execute the user code (protecting it from further
429 % expanding with |\unexpanded|).
430 % \begin{macrocode}
431 \def\ekv@csv@loop#1%
432 {%
433 \long\def\ekv@csv@loop##1##2,%
434 {%
435 \ekv@gobble@from@mark@to@stop##2\ekv@csv@loop@end\ekv@stop
436 \ekv@ifblank@##2\ekv@stop\ekv@ifblank@gobbletrue
437 \ekv@stop\ekv@csv@loop@blank
438 #1\ekv@csv@loop@do{##1}%
439 }%
440 }
441 \expandafter\ekv@csv@loop\expandafter{\ekv@strip{#2}}
442 \long\def\ekv@csv@loop@do#1#2{\ekv@unexpanded{#2{#1}}\ekv@csv@loop{#2}\ekv@mark}
443 \def\ekv@csv@loop@end#1%
444 {%
445 \long\def\ekv@csv@loop@end
446 \ekv@stop
447 \ekv@ifblank@\ekv@mark\ekv@stop\ekv@stop\ekv@ifblank@gobbletrue
448 \ekv@stop\ekv@csv@loop@blank
449 #1\ekv@csv@loop@do##1%
450 {\ekv@csv@loop@active{##1}\ekv@mark}%
451 }
452 \expandafter\ekv@csv@loop@end\expandafter{\ekv@strip{\ekv@mark\ekv@stop}}
453 \long\expandafter\def\expandafter\ekv@csv@loop@blank
454 \ekv@strip{\ekv@mark#1}\ekv@csv@loop@do#2%
455 {\ekv@csv@loop{#2}\ekv@mark}
456 % \end{macrocode}
457 % \end{macro}
458 %
459 % \begin{macro}{\ekv@name,\ekv@name@set,\ekv@name@key}
460 % The keys will all follow the same naming scheme, so we define it here.
461 % \begin{macrocode}
462 \def\ekv@name@set#1{ekv#1(}
463 \long\def\ekv@name@key#1{#1)}
464 \long\ekv@expandedarg{\def\ekv@name#1#2}%
465 {%
466 \ekv@unexpanded\expandafter{\ekv@name@set{#1}}%
467 \ekv@unexpanded\expandafter{\ekv@name@key{\detokenize{#2}}}%
468 }
469 % \end{macrocode}
470 % \end{macro}
471 %
472 % \begin{macro}[internal]{\ekv@undefined@set}
473 % We can misuse the macro name we use to expandably store the set-name in a
474 % single token -- since this increases performance drastically, especially for
475 % long set-names -- to throw a more meaningful error message in case a set isn't
476 % defined. The name of |\ekv@undefined@set| is a little bit misleading, as it is
477 % called in either case inside of |\csname|, but the result will be a control
478 % sequence with meaning |\relax| if the set is undefined, hence will break the
479 % |\csname| building the key-macro which will throw the error message.
480 % \begin{macrocode}
481 \def\ekv@undefined@set#1{! expkv Error: Set `#1' undefined.}
482 % \end{macrocode}
483 % \end{macro}
484 %
485 % \begin{macro}[internal]{\ekv@checkvalid}
486 % We place some restrictions on the allowed names, though, namely sets and
487 % keys are not allowed to be empty -- blanks are fine (meaning \mbox{set-
488 % or} key-names consisting of spaces). The |\def\ekv@tmp| gobbles any \TeX\
489 % prefixes which would otherwise throw errors. This will, however, break the
490 % package if an |\outer| has been gobbled this way. I consider that good,
491 % because keys shouldn't be defined |\outer| anyways.
492 % \begin{macrocode}
493 \protected\ekv@expandedarg{\def\ekv@checkvalid#1#2}%
494 {%
495 \ekv@unexpanded\expandafter{\ekv@ifempty{#1}}%
496 \ekv@unexpanded
497 {{%
498 \def\ekv@tmp{}%
499 \ekv@errm{empty set name not allowed}%
500 }}%
501 {%
502 \ekv@unexpanded\expandafter{\ekv@ifempty{#2}}%
503 \ekv@unexpanded
504 {%
505 {%
506 \def\ekv@tmp{}%
507 \ekv@errm{empty key name not allowed}%
508 }%
509 \@secondoftwo
510 }%
511 }%
512 \ekv@unexpanded{\@gobble}%
513 }
514 % \end{macrocode}
515 % \end{macro}
516 %
517 % \begin{macro}{\ekvifdefined,\ekvifdefinedNoVal}
518 % And provide user-level macros to test whether a key is defined.
519 % \begin{macrocode}
520 \ekv@expargtwice{\long\def\ekvifdefined#1#2}%
521 {\expandafter\ekv@ifdefined\expandafter{\ekv@name{#1}{#2}}}
522 \ekv@expargtwice{\long\def\ekvifdefinedNoVal#1#2}%
523 {\expandafter\ekv@ifdefined\expandafter{\ekv@name{#1}{#2}N}}
524 % \end{macrocode}
525 % \end{macro}
526 %
527 % \begin{macro}
528 % {
529 % \ekvdef,\ekvdefNoVal,\ekvlet,\ekvletNoVal,\ekvletkv,\ekvletkvNoVal,
530 % \ekvdefunknown,\ekvdefunknownNoVal,\ekvletunknown,\ekvletunknownNoVal
531 % }
532 % Set up the key defining macros |\ekvdef| etc. We use temporary macros to set
533 % these up with a few expansions already done.
534 % \begin{macrocode}
535 \def\ekvdef#1#2#3#4#5#6%
536 {%
537 \protected\long\def\ekvdef##1##2##3%
538 {#1{\expandafter\def\csname#2\endcsname####1{##3}#4}}%
539 \protected\long\def\ekvdefNoVal##1##2##3%
540 {#1{\expandafter\def\csname#2N\endcsname{##3}#4}}%
541 \protected\long\def\ekvlet##1##2##3%
542 {#1{\expandafter\let\csname#2\endcsname##3#4}}%
543 \protected\long\def\ekvletNoVal##1##2##3%
544 {#1{\expandafter\let\csname#2N\endcsname##3#4}}%
545 \ekv@expargtwice{\protected\long\def\ekv@defunknown##1##2##3##4}%
546 {%
547 \romannumeral
548 \ekv@exparg
549 {\ekv@zero\ekv@checkvalid{##3}.}%
550 {%
551 \expandafter\expandafter\expandafter
552 \def\expandafter\csname\ekv@name{##3}{}u##1\endcsname##2{##4}%
553 #6%
554 }%
555 }%
556 \ekv@expargtwice{\protected\long\def\ekv@letunknown##1##2##3}%
557 {%
558 \romannumeral
559 \ekv@exparg
560 {\ekv@zero\ekv@checkvalid{##2}.}%
561 {%
562 \expandafter\expandafter\expandafter
563 \let\expandafter\csname\ekv@name{##2}{}u##1\endcsname##3%
564 #5%
565 }%
566 }%
567 \protected\long\def\ekvletkv##1##2##3##4%
568 {%
569 #1%
570 {%
571 \expandafter\let\csname#2\expandafter\endcsname
572 \csname#3\endcsname
573 #4%
574 }%
575 }%
576 \protected\long\def\ekvletkvNoVal##1##2##3##4%
577 {%
578 #1%
579 {%
580 \expandafter\let\csname#2N\expandafter\endcsname
581 \csname#3N\endcsname
582 #4%
583 }%
584 }%
585 }
586 \edef\ekvdefNoVal
587 {%
588 {\ekv@unexpanded\expandafter{\ekv@checkvalid{#1}{#2}}}%
589 {\ekv@unexpanded\expandafter{\ekv@name{#1}{#2}}}%
590 {\ekv@unexpanded\expandafter{\ekv@name{#3}{#4}}}%
591 {%
592 \ekv@unexpanded{\expandafter\ekv@defsetmacro\csname}%
593 \ekv@unexpanded\expandafter{\ekv@undefined@set{#1}\endcsname{#1}}%
594 }%
595 {%
596 \ekv@unexpanded{\expandafter\ekv@defsetmacro\csname}%
597 \ekv@unexpanded\expandafter{\ekv@undefined@set{#2}\endcsname{#2}}%
598 }%
599 {%
600 \ekv@unexpanded{\expandafter\ekv@defsetmacro\csname}%
601 \ekv@unexpanded\expandafter{\ekv@undefined@set{#3}\endcsname{#3}}%
602 }%
603 }
604 \expandafter\ekvdef\ekvdefNoVal
605 \ekv@exparg{\protected\long\def\ekvdefunknown#1#2}%
606 {\ekv@defunknown{}{##1##2##3}{#1}{#2}}
607 \ekv@exparg{\protected\long\def\ekvdefunknownNoVal#1#2}%
608 {\ekv@defunknown{N}{##1##2}{#1}{#2}}
609 \ekv@exparg{\protected\long\def\ekvletunknown#1#2}%
610 {\ekv@letunknown{}{#1}{#2}}
611 \ekv@exparg{\protected\long\def\ekvletunknownNoVal#1#2}%
612 {\ekv@letunknown{N}{#1}{#2}}
613 \let\ekv@defunknown\ekv@undefined
614 \let\ekv@letunknown\ekv@undefined
615 % \end{macrocode}
616 % \end{macro}
617 %
618 % \begin{macro}{\ekvredirectunknown,\ekvredirectunknownNoVal}
619 % \begin{macro}[internal]
620 % {
621 % \ekv@defredirectunknown,\ekv@redirectunknown@aux,
622 % \ekv@redirectunknownNoVal@aux
623 % }
624 % The redirection macros prepare the unknown function by looping over the
625 % provided list of sets and leaving a |\ekv@redirect@kv| or |\ekv@redirect@k|
626 % for each set. Only the first of these internals will receive the \key\ and
627 % \val\ as arguments.
628 % \begin{macrocode}
629 \protected\def\ekv@defredirectunknown#1#2#3#4#5#6%
630 {%
631 \begingroup
632 \edef\ekv@tmp
633 {%
634 \ekvcsvloop#1{#6}%
635 \ekv@unexpanded{#2}%
636 {\ekvcsvloop{}{#5,#6}}%
637 }%
638 \ekv@expargtwice
639 {\endgroup\long#3{#5}}%
640 {\expandafter#4\ekv@tmp\ekv@stop}%
641 }
642 \ekv@exparg{\protected\def\ekvredirectunknown#1#2}%
643 {%
644 \ekv@defredirectunknown
645 \ekv@redirect@kv
646 \ekv@err@redirect@kv@notfound
647 \ekvdefunknown
648 \ekv@redirectunknown@aux
649 {#1}{#2}%
650 }
651 \ekv@exparg{\protected\def\ekvredirectunknownNoVal#1#2}%
652 {%
653 \ekv@defredirectunknown
654 \ekv@redirect@k
655 \ekv@err@redirect@k@notfound
656 \ekvdefunknownNoVal
657 \ekv@redirectunknownNoVal@aux
658 {#1}{#2}%
659 }
660 \def\ekv@redirectunknown@aux#1{#1{##1}{##2}}
661 \def\ekv@redirectunknownNoVal@aux#1{#1{##1}}
662 \let\ekv@defredirectunknown\ekv@undefined
663 % \end{macrocode}
664 % \end{macro}
665 % \end{macro}
666 %
667 % \begin{macro}[internal]
668 % {
669 % \ekv@redirect@k,\ekv@redirect@k@a,\ekv@redirect@k@a@,
670 % \ekv@redirect@k@b,\ekv@redirect@k@c,\ekv@redirect@k@d,
671 % \ekv@redirect@kv,\ekv@redirect@kv@a,\ekv@redirect@kv@a@,
672 % \ekv@redirect@kv@b,\ekv@redirect@kv@c,\ekv@redirect@kv@d
673 % }
674 % The redirect code works by some simple loop over all the sets, which we
675 % already preprocessed in |\ekv@defredirectunknown|. For some optimisation we
676 % blow this up a bit code wise, essentially, all this does is |\ekvifdefined|
677 % or |\ekvifdefinedNoVal| in each set, if there is a match gobble the
678 % remainder of the specified sets and execute the key macro, else go on with
679 % the next set (to which the \key\ and \val\ are forwarded).
680 %
681 % First we set up some code which is different depending on |\lastnamedcs|
682 % being available or not. All this is stored in a temporary macro to have
683 % pre-expanded |\ekv@name| constellations ready.
684 % \begin{macrocode}
685 \def\ekv@redirect@k#1#2#3#4%
686 {%
687 \ekv@if@lastnamedcs
688 {%
689 \def\ekv@redirect@k##1##2##3%
690 {%
691 \ifcsname#1\endcsname\ekv@redirect@k@a\fi
692 ##3{##1}%
693 }%
694 \def\ekv@redirect@k@a\fi{\fi\expandafter\ekv@redirect@k@b\lastnamedcs}%
695 \long\def\ekv@redirect@kv##1##2##3##4%
696 {%
697 \ifcsname#2\endcsname\ekv@redirect@kv@a\fi\@gobble{##1}%
698 ##4{##1}{##2}%
699 }%
700 \def\ekv@redirect@kv@a\fi\@gobble
701 {\fi\expandafter\ekv@redirect@kv@b\lastnamedcs}%
702 }
703 {%
704 \def\ekv@redirect@k##1##2##3%
705 {%
706 \ifcsname#1\endcsname\ekv@redirect@k@a\fi\ekv@redirect@k@a@
707 #1\endcsname
708 ##3{##1}%
709 }%
710 \def\ekv@redirect@k@a@#3\endcsname{}%
711 \def\ekv@redirect@k@a\fi\ekv@redirect@k@a@
712 {\fi\expandafter\ekv@redirect@k@b\csname}%
713 \long\def\ekv@redirect@kv##1##2##3##4%
714 {%
715 \ifcsname#2\endcsname\ekv@redirect@kv@a\fi\ekv@redirect@kv@a@
716 #2\endcsname{##1}%
717 ##4{##1}{##2}%
718 }%
719 \long\def\ekv@redirect@kv@a@#4\endcsname##3{}%
720 \def\ekv@redirect@kv@a\fi\ekv@redirect@kv@a@
721 {\fi\expandafter\ekv@redirect@kv@b\csname}%
722 }%
723 }
724 % \end{macrocode}
725 % The key name given to this loop will already be |\detokenize|d by |\ekvset|,
726 % so we can safely remove the |\detokenize| here for some performance gain.
727 % \begin{macrocode}
728 \def\ekv@redirect@kv#1\detokenize#2#3\ekv@stop{\ekv@unexpanded{#1#2#3}}
729 \edef\ekv@redirect@kv
730 {%
731 {\expandafter\ekv@redirect@kv\ekv@name{#2}{#1}N\ekv@stop}%
732 {\expandafter\ekv@redirect@kv\ekv@name{#3}{#2}\ekv@stop}%
733 {\expandafter\ekv@redirect@kv\ekv@name{#1}{#2}N\ekv@stop}%
734 {\expandafter\ekv@redirect@kv\ekv@name{#1}{#2}\ekv@stop}%
735 }
736 % \end{macrocode}
737 % Everything is ready to make the real definitions.
738 % \begin{macrocode}
739 \expandafter\ekv@redirect@k\ekv@redirect@kv
740 % \end{macrocode}
741 % The remaining macros here are independent on |\lastnamedcs|, starting from
742 % the |@b| we know that there is a hash table entry, and get the macro as a
743 % parameter. We still have to test whether the macro is |\relax|, depending on
744 % the result of that test we have to either remove the remainder of the
745 % current test, or the remainder of the set list and invoke the macro.
746 % \begin{macrocode}
747 \def\ekv@redirect@k@b#1%
748 {\ifx\relax#1\ekv@redirect@k@c\fi\ekv@redirect@k@d#1}
749 \def\ekv@redirect@k@c\fi\ekv@redirect@k@d#1{\fi}
750 \def\ekv@redirect@k@d#1#2\ekv@stop{#1}
751 \def\ekv@redirect@kv@b#1%
752 {\ifx\relax#1\ekv@redirect@kv@c\fi\ekv@redirect@kv@d#1}
753 \long\def\ekv@redirect@kv@c\fi\ekv@redirect@kv@d#1#2{\fi}
754 \long\def\ekv@redirect@kv@d#1#2#3\ekv@stop{#1{#2}}
755 % \end{macrocode}
756 % \end{macro}
757 %
758 % \begin{macro}[internal]{\ekv@defsetmacro}
759 % In order to enhance the speed the set name given to |\ekvset| will be turned
760 % into a control sequence pretty early, so we have to define that control
761 % sequence.
762 % \begin{macrocode}
763 \edef\ekv@defsetmacro
764 {%
765 \ekv@unexpanded{\ifx#1\relax\edef#1##1}%
766 {%
767 \ekv@unexpanded\expandafter{\ekv@name@set{#2}}%
768 \ekv@unexpanded\expandafter{\ekv@name@key{##1}}%
769 }%
770 \ekv@unexpanded{\fi}%
771 }
772 \ekv@exparg{\protected\def\ekv@defsetmacro#1#2}{\ekv@defsetmacro}
773 % \end{macrocode}
774 % \end{macro}
775 %
776 % \begin{macro}{\ekvifdefinedset}
777 % \begin{macrocode}
778 \ekv@expargtwice{\def\ekvifdefinedset#1}%
779 {\expandafter\ekv@ifdefined\expandafter{\ekv@undefined@set{#1}}}
780 % \end{macrocode}
781 % \end{macro}
782 %
783 % \begin{macro}[internal]{\ekv@ifexp}
784 % We want to be able to include expansion control. This is started by a
785 % specific structure, namely if the stuff to the left of the equals sign
786 % contains a colon followed by a space followed by at least one non-space
787 % token. There is an additional rule namely the contents in front of the
788 % \verb*|: | marker mustn't contain spaces outside of braces, but
789 % unfortunately we can test this only after we did the real split and not as
790 % part of a fast test. The temporary meaning we set up here will get
791 % \tkn{:}{13}\vissp{ } and \tkn{:}{11}\vissp{ } as |#1| and |#2|.
792 %
793 % The real definition will take three arguments (two curried), namely the
794 % un-|\detokenize|d name as |#1|, what should be done if the \expnotation\
795 % should start as |#2|, and what should be done if no \expnotation\ is found
796 % as |#3|.
797 % \begin{macrocode}
798 \def\ekv@ifexp#1#2%
799 {%
800 % \end{macrocode}
801 % In the following explanations I use \tkn{:}{12}, \tkn{:}{13}, and
802 % \tkn{:}{11}, but I always mean that token followed by a space.
803 % \begin{macro}[internal]{\ekv@ifexp@split@o@or@other}
804 % \tkn{:}{12} splitter also used to test for \tkn{:}{13} and \tkn{:}{11}
805 % \begin{macrocode}
806 \long\def\ekv@ifexp@split@o@or@other
807 ##1: ##2\ekv@stop##3%
808 ##4#1##5\ekv@stop##6%
809 ##7#2##8\ekv@stop##9%
810 {##9##6##3##1\ekv@stop##2}%
811 % \end{macrocode}
812 % \end{macro}
813 % \begin{macro}[internal]{\ekv@ifexp@split@a@or@l}
814 % \tkn{:}{13} splitter also used to test for \tkn{:}{11}
815 % \begin{macrocode}
816 \long\def\ekv@ifexp@split@a@or@l
817 ##1#1##2\ekv@stop##3%
818 ##4#2##5\ekv@stop##6%
819 {##6##3##1\ekv@stop##2}%
820 % \end{macrocode}
821 % \end{macro}
822 % \begin{macro}[internal]
823 % {\ekv@ifexp@split@a,\ekv@ifexp@split@l}
824 % \tkn{:}{13} splitter
825 % \begin{macrocode}
826 \long\def\ekv@ifexp@split@a##1#1##2\ekv@stop##3%
827 {##3##1\ekv@stop##2}%
828 % \end{macrocode}
829 % \tkn{:}{11} splitter
830 % \begin{macrocode}
831 \long\def\ekv@ifexp@split@l##1#2##2\ekv@stop##3%
832 {##3##1\ekv@stop##2}%
833 % \end{macrocode}
834 % \end{macro}
835 % To get good performance in the most common case (standard category codes
836 % apply) we try to split on \tkn{:}{12}, due to the way this split works there
837 % are a few possible outcomes:
838 % \begin{enumerate}
839 % \item |\ekv@ifexp@none| is the first token if no colon was
840 % found at all
841 % \item |\ekv@ifexp@o| is the first token if only \tkn{:}{12}
842 % were part of \key
843 % \item |\ekv@ifexp@a| is put in front of either of the two
844 % aforementioned cases if there was at least one \tkn{:}{13} (in that case
845 % we don't yet know which was the first colon)
846 % \item |\ekv@ifexp@o| is put in front of any of the
847 % aforementioned cases if there was at least one \tkn{:}{11} (in which
848 % case we also don't know yet which was the first colon)
849 % \end{enumerate}
850 % \begin{macrocode}
851 \long\def\ekv@ifexp##1%
852 {%
853 \ekv@ifexp@split@o@or@other
854 ##1\ekv@nil\ekv@stop\ekv@ifexp@o
855 : \ekv@stop\ekv@ifexp@none
856 ##1\ekv@stop\ekv@ifexp@a#1\ekv@stop{}%
857 ##1\ekv@stop\ekv@ifexp@l#2\ekv@stop{}%
858 }%
859 % \end{macrocode}
860 % \begin{macro}[internal]
861 % {\ekv@ifexp@o,\ekv@ifexp@none}
862 % If any of the next two are called we already know the outcome and can
863 % directly expand to it.
864 % \begin{macrocode}
865 \long\def\ekv@ifexp@o##1\ekv@stop
866 {\ekv@ifexp@done{##1}\ekv@mark}%
867 \long\def\ekv@ifexp@none
868 ##1\ekv@nil\ekv@stop\ekv@ifexp@o\ekv@stop
869 ##2##3%
870 {##3}%
871 % \end{macrocode}
872 % \end{macro}
873 % \begin{macro}[internal]
874 % {\ekv@ifexp@a,\ekv@ifexp@a@i,\ekv@ifexp@a@ii}
875 % If this is called we know that there was no \tkn{:}{13}, if the first token
876 % now is |\ekv@ifexp@none| there was no \tkn{:}{12} as well
877 % \begin{macrocode}
878 \def\ekv@ifexp@a##1%
879 {%
880 \ekv@ifexp@a@i
881 ##1\ekv@ifexp@a@ii
882 \ekv@ifexp@none\ekv@ifexp@a@or@o
883 }%
884 \def\ekv@ifexp@a@i##1\ekv@ifexp@none{}%
885 % \end{macrocode}
886 % Easy route, no \tkn{:}{12}, simply grab and pack
887 % \begin{macrocode}
888 \long\def\ekv@ifexp@a@ii
889 \ekv@ifexp@none\ekv@ifexp@a@or@o
890 ##1#1##2\ekv@stop\ekv@ifexp@o\ekv@stop
891 {\ekv@ifexp@done{##1}\ekv@mark##2}%
892 % \end{macrocode}
893 % \end{macro}
894 % \begin{macro}[internal]
895 % {
896 % \ekv@ifexp@a@or@o,
897 % \ekv@ifexp@a@not@o,\ekv@ifexp@a@not@o@,
898 % \ekv@ifexp@o@not@a
899 % }
900 % other route, there is a \tkn{:}{12}, re-split at \tkn{:}{13}
901 % \begin{macrocode}
902 \long\def\ekv@ifexp@a@or@o##1\ekv@stop
903 {%
904 \ekv@ifexp@split@a
905 ##1\ekv@nil\ekv@stop\ekv@ifexp@a@not@o
906 #1\ekv@stop\ekv@ifexp@o@not@a
907 }%
908 % \end{macrocode}
909 % \tkn{:}{13} came earlier than \tkn{:}{12}
910 % \begin{macrocode}
911 \long\def\ekv@ifexp@a@not@o##1\ekv@stop
912 {\ekv@ifexp@a@not@o@{##1}\ekv@mark}
913 \long\def\ekv@ifexp@a@not@o@
914 ##1##2\ekv@nil#1\ekv@stop\ekv@ifexp@o@not@a
915 {\ekv@ifexp@done{##1}##2: }%
916 % \end{macrocode}
917 % \tkn{:}{12} came earlier than \tkn{:}{13}
918 % \begin{macrocode}
919 \long\def\ekv@ifexp@o@not@a
920 ##1\ekv@nil\ekv@stop\ekv@ifexp@a@not@o\ekv@stop
921 {\ekv@ifexp@done{##1}\ekv@mark}%
922 % \end{macrocode}
923 % \end{macro}
924 % \begin{macro}[internal]{\ekv@ifexp@l,\ekv@ifexp@l@i,\ekv@ifexp@l@ii}
925 % Now this is the most complicated case, if |\ekv@ifexp@l| is
926 % called there are a few possibilities:
927 % \begin{enumerate}
928 % \item |#1| is |\ekv@ifexp@none|: easiest route, only
929 % \tkn{:}{11} was there
930 % \item |#1| is |\ekv@ifexp@o|: complicated route, there was a
931 % \tkn{:}{11} and a \tkn{:}{12}
932 % \item |#1| is |\ekv@ifexp@a| and after that comes
933 % |\ekv@ifexp@none|: more complicated, there was a
934 % \tkn{:}{11} and a \tkn{:}{13}
935 % \item |#1| is |\ekv@ifexp@a| and after that comes
936 % |\ekv@ifexp@o|: cursed route, there were \tkn{:}{11},
937 % \tkn{:}{12}, and \tkn{:}{13}
938 % \end{enumerate}
939 % \begin{macrocode}
940 \def\ekv@ifexp@l##1%
941 {%
942 \ekv@ifexp@l@i
943 ##1\ekv@ifexp@l@ii
944 \ekv@ifexp@none{}%
945 ##1\ekv@ifexp@l@or@o
946 \ekv@ifexp@o
947 \ekv@ifexp@l@or@a
948 }%
949 \def\ekv@ifexp@l@i
950 ##1\ekv@ifexp@none##2%
951 ##3\ekv@ifexp@o
952 {##2}%
953 % \end{macrocode}
954 % easiest route, just \tkn{:}{11}
955 % \begin{macrocode}
956 \long\def\ekv@ifexp@l@ii
957 \ekv@ifexp@l@or@a##1#2##2\ekv@stop
958 \ekv@ifexp@o\ekv@stop#2\ekv@stop##3%
959 {\ekv@ifexp@done{##1}\ekv@mark##2}%
960 % \end{macrocode}
961 % \end{macro}
962 % \begin{macro}[internal]
963 % {
964 % \ekv@ifexp@l@or@o,
965 % \ekv@ifexp@l@not@o,\ekv@ifexp@l@not@o@,
966 % \ekv@ifexp@o@not@l
967 % }
968 % slightly harder, \tkn{:}{11} or \tkn{:}{12}, re-split at \tkn{:}{11}
969 % \begin{macrocode}
970 \long\def\ekv@ifexp@l@or@o
971 \ekv@ifexp@o\ekv@ifexp@l@or@a
972 ##1\ekv@stop
973 {%
974 \ekv@ifexp@split@l
975 ##1\ekv@nil\ekv@stop\ekv@ifexp@l@not@o
976 #2\ekv@stop\ekv@ifexp@o@not@l
977 }%
978 % \end{macrocode}
979 % \tkn{:}{11} came earlier than \tkn{:}{12}
980 % \begin{macrocode}
981 \long\def\ekv@ifexp@l@not@o##1\ekv@stop
982 {\ekv@ifexp@l@not@o@{##1}\ekv@mark}
983 \long\def\ekv@ifexp@l@not@o@
984 ##1##2\ekv@nil#2\ekv@stop\ekv@ifexp@o@not@l
985 ##3#2\ekv@stop##4%
986 {\ekv@ifexp@done{##1}##2: ##3}%
987 % \end{macrocode}
988 % \tkn{:}{12} came earlier than \tkn{:}{11}
989 % \begin{macrocode}
990 \long\def\ekv@ifexp@o@not@l
991 ##1\ekv@nil\ekv@stop\ekv@ifexp@l@not@o\ekv@stop
992 ##2#2\ekv@stop##3%
993 {\ekv@ifexp@done{##1}\ekv@mark##2}%
994 % \end{macrocode}
995 % \end{macro}
996 % \begin{macro}[internal]
997 % {
998 % \ekv@ifexp@l@or@a,
999 % \ekv@ifexp@l@or@a@not@o,
1000 % \ekv@ifexp@l@not@a,\ekv@ifexp@l@not@a@,
1001 % \ekv@ifexp@a@not@l
1002 % }
1003 % \tkn{:}{13} was in the mix with \tkn{:}{11}, we need to see whether there
1004 % was a \tkn{:}{12} as well
1005 % \begin{macrocode}
1006 \def\ekv@ifexp@l@or@a##1%
1007 {%
1008 % \end{macrocode}
1009 % don't get confused by the name, this gobbles until |@none|
1010 % \begin{macrocode}
1011 \ekv@ifexp@a@i
1012 ##1\ekv@ifexp@l@or@a@not@o
1013 \ekv@ifexp@none
1014 \ekv@ifexp@l@or@a@or@o
1015 }%
1016 % \end{macrocode}
1017 % phew, no \tkn{:}{12} in the mix, we split at \tkn{:}{13} and see whether an
1018 % earlier \tkn{:}{11} is found
1019 % \begin{macrocode}
1020 \long\def\ekv@ifexp@l@or@a@not@o
1021 \ekv@ifexp@none\ekv@ifexp@l@or@a@or@o
1022 ##1#1%
1023 {%
1024 \ekv@ifexp@split@l
1025 ##1\ekv@nil\ekv@stop\ekv@ifexp@l@not@a
1026 #2\ekv@stop\ekv@ifexp@a@not@l
1027 }%
1028 \long\def\ekv@ifexp@l@not@a##1\ekv@stop
1029 {\ekv@ifexp@l@not@a@{##1}\ekv@mark}%
1030 \long\def\ekv@ifexp@l@not@a@
1031 ##1##2\ekv@nil#2\ekv@stop\ekv@ifexp@a@not@l
1032 ##3\ekv@stop\ekv@ifexp@o\ekv@stop#2\ekv@stop##4%
1033 {\ekv@ifexp@done{##1}##2#1##3}%
1034 \long\def\ekv@ifexp@a@not@l
1035 ##1\ekv@nil\ekv@stop\ekv@ifexp@l@not@a\ekv@stop
1036 ##2\ekv@stop\ekv@ifexp@o\ekv@stop#2\ekv@stop##3%
1037 {\ekv@ifexp@done{##1}\ekv@mark##2}%
1038 % \end{macrocode}
1039 % \end{macro}
1040 % \begin{macro}[internal]
1041 % {
1042 % \ekv@ifexp@l@or@a@or@o,\ekv@ifexp@l@or@a@or@o@i,\ekv@ifexp@l@or@a@or@o@ii,
1043 % \ekv@ifexp@l@or@a@or@o@iii,\ekv@ifexp@l@or@a@or@o@iv,
1044 % \ekv@ifexp@o@not@l@or@a,
1045 % \ekv@ifexp@a@not@l@or@o,\ekv@ifexp@a@not@l@or@o@
1046 % }
1047 % ooh no, the ugliest path, we got ourself a \tkn{:}{11}, a \tkn{:}{12}, and a
1048 % \tkn{:}{13}, first grab everything left after splitting the first
1049 % \tkn{:}{12} and try a \tkn{:}{13} split
1050 % \begin{macrocode}
1051 \long\def\ekv@ifexp@l@or@a@or@o##1\ekv@stop
1052 {%
1053 \ekv@ifexp@split@a@or@l
1054 ##1\ekv@nil\ekv@stop\ekv@ifexp@a@not@l@or@o
1055 #1\ekv@stop\ekv@ifexp@o@not@l@or@a
1056 ##1\ekv@stop\ekv@ifexp@l@or@a@or@o@i#2\ekv@stop{}%
1057 }%
1058 % \end{macrocode}
1059 % \tkn{:}{12} splitting was correct
1060 % \begin{macrocode}
1061 \long\def\ekv@ifexp@o@not@l@or@a
1062 ##1\ekv@nil\ekv@stop\ekv@ifexp@a@not@l@or@o\ekv@stop
1063 ##2#2\ekv@stop##3%
1064 {\ekv@ifexp@done{##1}\ekv@mark##2}%
1065 % \end{macrocode}
1066 % \tkn{:}{13} split is correct, \tkn{:}{12} has to be reverted
1067 % \begin{macrocode}
1068 \long\def\ekv@ifexp@a@not@l@or@o##1\ekv@stop
1069 {\ekv@ifexp@a@not@l@or@o@{##1}\ekv@mark}
1070 \long\def\ekv@ifexp@a@not@l@or@o@
1071 ##1##2\ekv@nil##3#2\ekv@stop##4%
1072 {\ekv@ifexp@done{##1}##2: ##3}%
1073 % \end{macrocode}
1074 % there is still a \tkn{:}{11} left, we need to take care of \tkn{:}{13} as
1075 % well, but \tkn{:}{12} was wrong
1076 % \begin{macrocode}
1077 \def\ekv@ifexp@l@or@a@or@o@i##1%
1078 {%
1079 \ekv@ifexp@l@or@a@or@o@ii
1080 ##1\ekv@ifexp@l@or@a@or@o@iii
1081 \ekv@ifexp@o@not@l@or@a
1082 \ekv@ifexp@why
1083 }%
1084 \def\ekv@ifexp@l@or@a@or@o@ii
1085 ##1\ekv@ifexp@o@not@l@or@a
1086 {}%
1087 % \end{macrocode}
1088 % at least it's not absolutely the worst, \tkn{:}{11} is correct, revert the
1089 % \tkn{:}{12} split
1090 % \begin{macrocode}
1091 \long\def\ekv@ifexp@l@or@a@or@o@iii
1092 \ekv@ifexp@o@not@l@or@a\ekv@ifexp@why
1093 ##1#2%
1094 {\ekv@ifexp@l@or@a@or@o@iv{##1}\ekv@mark}%
1095 \long\def\ekv@ifexp@l@or@a@or@o@iv
1096 ##1##2\ekv@nil\ekv@stop\ekv@ifexp@a@not@l@or@o\ekv@stop
1097 #2\ekv@stop##3##4#2\ekv@stop##5%
1098 {\ekv@ifexp@done{##1}##2: ##4}%
1099 % \end{macrocode}
1100 % \end{macro}
1101 % \begin{macro}[internal]
1102 % {
1103 % \ekv@ifexp@why,
1104 % \ekv@ifexp@why@l,\ekv@ifexp@why@l@i,\ekv@ifexp@why@l@ii,
1105 % \ekv@ifexp@why@a
1106 % }
1107 % why have you forsaken me? We know \tkn{:}{12} was wrong, but now we have
1108 % something that might be correctly split at \tkn{:}{13}, but might as well be
1109 % not
1110 % \begin{macrocode}
1111 \long\def\ekv@ifexp@why##1\ekv@stop
1112 {%
1113 \ekv@ifexp@split@l
1114 ##1\ekv@nil\ekv@stop\ekv@ifexp@why@l
1115 #2\ekv@stop\ekv@ifexp@why@a
1116 }%
1117 % \end{macrocode}
1118 % \tkn{:}{11} is correct, rebuild stuff undoing the wrong \tkn{:}{13} split
1119 % \begin{macrocode}
1120 \long\def\ekv@ifexp@why@l##1\ekv@stop
1121 {\ekv@ifexp@why@l@i{##1}\ekv@mark}
1122 \long\def\ekv@ifexp@why@l@i
1123 ##1##2\ekv@nil#2\ekv@stop\ekv@ifexp@why@a
1124 {\ekv@ifexp@why@l@ii{##1}##2#1}
1125 % \end{macrocode}
1126 % and undoing the wrong \tkn{:}{12} split
1127 % \begin{macrocode}
1128 \long\def\ekv@ifexp@why@l@ii
1129 ##1##2\ekv@nil#2\ekv@stop##3##4#2\ekv@stop##5%
1130 {\ekv@ifexp@done{##1}##2: ##4}%
1131 % \end{macrocode}
1132 % \tkn{:}{13} is correct, we know there is a \tkn{:}{11} hiding somewhere, so
1133 % we don't need to protect against brace strip using a two-step grab here,
1134 % just undoing the wrong \tkn{:}{12} split is enough
1135 % \begin{macrocode}
1136 \long\def\ekv@ifexp@why@a
1137 ##1\ekv@nil\ekv@stop\ekv@ifexp@why@l\ekv@stop
1138 ##2\ekv@nil#2\ekv@stop##3##4#2\ekv@stop##5%
1139 {\ekv@ifexp@done{##1}\ekv@mark##2: ##4}%
1140 % \end{macrocode}
1141 % \end{macro}
1142 % \begin{macro}[internal]
1143 % {
1144 % \ekv@ifexp@done,\ekv@ifexp@done@,
1145 % \ekv@ifexp@done@test,
1146 % \ekv@ifexp@panic
1147 % }
1148 % once the colon split is done we need to check that there are no spaces in
1149 % the now split of part, if there are \emph{panic} (we did all of this for
1150 % nothing), else we're finally completely done with the splitting business.
1151 % \begin{macrocode}
1152 \ekv@exparg{\long\def\ekv@ifexp@done##1##2\ekv@nil}%
1153 {%
1154 \@firstofone
1155 {\ekv@ifexp@done@test##1\ekv@nil\ekv@ifexp@panic} \ekv@nil{}%
1156 ##2\ekv@stop\ekv@ifexp@done@{##1}{##2}\ekv@stop\@secondoftwo
1157 }
1158 \long\def\ekv@ifexp@done@test##1 ##2\ekv@nil##3##4##5##6\ekv@stop{##3}
1159 \ekv@exparg{\long\def\ekv@ifexp@done@##1##2\ekv@stop\@secondoftwo##3}%
1160 {\ekv@strip{##2}{##3{##1}}}%
1161 % \end{macrocode}
1162 % Panic in this case means give up on expansion parsing, instead the normal
1163 % action that would've been done if the expansion mark isn't found is
1164 % executed.
1165 % \begin{macrocode}
1166 \long\def\ekv@ifexp@panic##1\@secondoftwo##2##3{##3}
1167 }
1168 % \end{macrocode}
1169 % \end{macro}
1170 % We set up the strange category codes for colons and do the real definition
1171 % after the temporary one.
1172 % \begin{macrocode}
1173 \begingroup
1174 \catcode`\~=13
1175 \lccode`\~=`\:
1176 \catcode`\z=11
1177 \lccode`\z=`\:
1178 \lowercase{\endgroup
1179 \ekv@ifexp{~ }{z }}
1180 % \end{macrocode}
1181 % \end{macro}
1182 %
1183 % \begin{macro}{\ekvset}
1184 % Set up |\ekvset|, which should not be affected by active commas and equal
1185 % signs. The equal signs are a bit harder to cope with and we'll do that later,
1186 % but the active commas can be handled by just doing two comma-splitting loops
1187 % one at actives one at others. That's why we define |\ekvset| here with a
1188 % temporary meaning just to set up the things with two different category codes.
1189 % |#1| will be a \texttt{,\textsubscript{13}} and |#2| will be a
1190 % \texttt{=\textsubscript{13}}.
1191 % \begin{macrocode}
1192 \begingroup
1193 \def\ekvset#1#2{%
1194 \endgroup
1195 \ekv@exparg{\long\def\ekvset##1##2}%
1196 {%
1197 \expandafter\expandafter\expandafter
1198 \ekv@set\expandafter\csname\ekv@undefined@set{##1}\endcsname
1199 \ekv@mark##2#1\ekv@stop#1{}%
1200 }
1201 % \end{macrocode}
1202 % \end{macro}
1203 %
1204 % \begin{macro}[internal]{\ekv@set}
1205 % |\ekv@set| will split the \kv\ list at active commas. Then it has to check
1206 % whether there were unprotected other commas and resplit there.
1207 % \begin{macrocode}
1208 \long\def\ekv@set##1##2#1%
1209 {%
1210 % \end{macrocode}
1211 % Test whether we're at the end, if so invoke |\ekv@endset|,
1212 % \begin{macrocode}
1213 \ekv@gobble@from@mark@to@stop##2\ekv@endset\ekv@stop
1214 % \end{macrocode}
1215 % else go on with other commas.
1216 % \begin{macrocode}
1217 \ekv@set@other##1##2,\ekv@stop,%
1218 }
1219 % \end{macrocode}
1220 % \end{macro}
1221 %
1222 % \begin{macro}[internal]{\ekv@endset}
1223 % |\ekv@endset| is a hungry little macro. It will eat everything that remains
1224 % of |\ekv@set| and unbrace the sneaked stuff.
1225 % \begin{macrocode}
1226 \long\def\ekv@endset\ekv@stop\ekv@set@other##1\ekv@mark\ekv@stop,\ekv@stop,##2%
1227 {##2}
1228 % \end{macrocode}
1229 % \end{macro}
1230 %
1231 % \begin{macro}[internal]{\ekv@eq@other@or@active,\ekv@eq@active}
1232 % Splitting at equal signs will be done in a way that checks whether there is
1233 % an equal sign and splits at the same time. This gets quite messy and the
1234 % code might look complicated, but this is pretty fast (faster than first
1235 % checking for an equal sign and splitting if one is found). The splitting
1236 % code will be adapted for |\ekvset| and |\ekvparse| to get the most speed,
1237 % but some of these macros don't require such adaptions.
1238 % |\ekv@eq@other@or@active| and |\ekv@eq@active| will split the argument at
1239 % the first equal sign. |\ekv@eq@active| will insert the macro which comes
1240 % after the first following |\ekv@mark|, while |\ekv@eq@other@or@active| will
1241 % also check for an active equals sign or none.
1242 % Usage should look like this:
1243 % \begin{syntaxexample*}
1244 % |\ekv@eq@other@or@active|
1245 % \indent|#1\ekv@nil\ekv@mark|\meta{code for \tkn{=}{12}}
1246 % \indent\tkn{=}{12}|\ekv@mark|\meta{code for no equals sign}
1247 % \indent|#1\ekv@mark|\meta{code for \tkn{=}{13} or mixed}
1248 % \indent\tkn{=}{13}|\ekv@mark{}|
1249 % |\ekv@eq@active|
1250 % \indent|#1\ekv@nil\ekv@mark|\meta{code for \tkn{=}{13}}
1251 % \indent\tkn{=}{13}|\ekv@mark|\meta{code for no \tkn{=}{13}}
1252 % \end{syntaxexample*}
1253 % To prevent accidental brace stripping |#1| should be handed in with a
1254 % leading |\ekv@mark|, also that's what the |\ekv@nil| after the first |#1| is
1255 % used for. In |\ekv@eq@other@or@active| four cases are distinguished:
1256 % \begin{enumerate}
1257 % \item
1258 % Only equals signs of category 12 are found, then \meta{code for
1259 % \tkn{=}{12}} will be inserted.
1260 % \item
1261 % No equals sign is found, then \meta{code for no equals sign} will be
1262 % inserted.
1263 % \item
1264 % Only equals signs of category 13 are found, then \meta{code for
1265 % \tkn{=}{13} or mixed} will be inserted, directly followed by
1266 % \meta{code for no equals sign}.
1267 % \item
1268 % Equals signs of categories 12 and 13 are found, then \meta{code for
1269 % \tkn{=}{13} or mixed} directly followed by \meta{code for \tkn{=}{12}}
1270 % will be inserted.
1271 % \end{enumerate}
1272 % This allows for fast branching based on \TeX's argument grabbing rules and
1273 % we don't have to split after the branching if the equal sign was there.
1274 % \begin{macrocode}
1275 \long\def\ekv@eq@other@or@active
1276 ##1=##2\ekv@mark##3%
1277 ##4#2##5\ekv@mark##6%
1278 {##6##3##1\ekv@stop\ekv@mark##2}
1279 \long\def\ekv@eq@other##1=##2\ekv@mark##3{##3##1\ekv@stop\ekv@mark##2}
1280 \long\def\ekv@eq@active##1#2##2\ekv@mark##3{##3##1\ekv@stop\ekv@mark##2}
1281 % \end{macrocode}
1282 % \end{macro}
1283 %
1284 % \begin{macro}[internal]{\ekv@set@other,\ekv@set@next@other}
1285 % The macro |\ekv@set@other| is guaranteed to get only single \kv\ pairs.
1286 % \begin{macrocode}
1287 \long\def\ekv@set@other##1##2,%
1288 {%
1289 % \end{macrocode}
1290 % First we test whether we're done.
1291 % \begin{macrocode}
1292 \ekv@gobble@from@mark@to@stop##2\ekv@endset@other\ekv@stop
1293 % \end{macrocode}
1294 % If not we split at the equal sign of category other.
1295 % \begin{macrocode}
1296 \ekv@eq@other@or@active
1297 ##2\ekv@nil\ekv@mark\ekv@set@eq@other
1298 =\ekv@mark\ekv@set@noeq
1299 ##2\ekv@mark\ekv@set@eq@active
1300 #2\ekv@mark{}%
1301 % \end{macrocode}
1302 % And put the set name after the splitting.
1303 % \begin{macrocode}
1304 ##1%
1305 \ekv@mark
1306 }
1307 % \end{macrocode}
1308 % The second macro is used as a shortcut to gobble two arguments and do the
1309 % same as a following |\ekv@set@other|.
1310 % \begin{macrocode}
1311 \ekv@exparg{\long\def\ekv@set@next@other##1##2\ekv@set@other##3##4,}%
1312 {\ekv@set@other{##3}{##4},}
1313 % \end{macrocode}
1314 % \end{macro}
1315 %
1316 % \begin{macro}[internal]{\ekv@endset@other}
1317 % Breaking the current inner loop for \tkn{,}{12} is done by gobbling all the
1318 % remaining tokens and grabbing the next split at an active comma
1319 % (incorporating the next |\ekv@set| call).
1320 % \begin{macrocode}
1321 \ekv@exparg{\long\def\ekv@endset@other
1322 \ekv@stop\ekv@eq@other@or@active
1323 \ekv@mark\ekv@stop\ekv@nil\ekv@mark\ekv@set@eq@other
1324 =\ekv@mark\ekv@set@noeq
1325 \ekv@mark\ekv@stop\ekv@mark\ekv@set@eq@active#2\ekv@mark##1%
1326 ##2##3#1}%
1327 {\ekv@set{##2}{##3}#1}
1328 % \end{macrocode}
1329 % \end{macro}
1330 %
1331 % \begin{macro}[internal]{\ekv@set@eq@other}
1332 % If this gets called we know that there is no \tkn{=}{13}, hence are done
1333 % with the splitting business. This doesn't even need much clean up, we can
1334 % just strip the \key-name and forward it to |\ekv@set@pair|.
1335 % \begin{macrocode}
1336 \ekv@exparg{\long\def\ekv@set@eq@other##1\ekv@stop}%
1337 {\ekv@strip@key{##1}\ekv@set@pair}
1338 % \end{macrocode}
1339 % \end{macro}
1340 %
1341 % \begin{macro}[internal]{\ekv@set@if@noeq}
1342 % In the case that there was at least one \tkn{=}{13} we need a way to know
1343 % whether there also was a \tkn{=}{12}. This here can be used to check this by
1344 % only looking for |\ekv@set@noeq|.
1345 % \begin{macrocode}
1346 \def\ekv@set@if@noeq##1\ekv@set@noeq{}
1347 % \end{macrocode}
1348 % \end{macro}
1349 %
1350 % \begin{macro}[internal]{\ekv@set@eq@active,\ekv@set@eq@active@}
1351 % The first token after this will either be |\ekv@set@noeq| or
1352 % |\ekv@set@eq@other|. We test this and branch accordingly.
1353 % \begin{macrocode}
1354 \def\ekv@set@eq@active##1%
1355 {\ekv@set@if@noeq##1\ekv@set@eq@active@\ekv@set@noeq\ekv@set@eq@mixed}
1356 % \end{macrocode}
1357 % Since we know that there was no \tkn{=}{12} when the next macro is called we
1358 % can simply do the correct split without any checks and forward to
1359 % |\ekv@set@pair|.
1360 % \begin{macrocode}
1361 \ekv@exparg{\long\def\ekv@set@eq@active@
1362 \ekv@set@noeq\ekv@set@eq@mixed
1363 ##1#2##2\ekv@mark
1364 \ekv@set@eq@other\ekv@stop\ekv@mark#2\ekv@mark##3}%
1365 {\ekv@strip@key{##1}\ekv@set@pair\ekv@mark##2}
1366 % \end{macrocode}
1367 % \end{macro}
1368 %
1369 % \begin{macro}[internal]
1370 % {\ekv@set@eq@mixed,\ekv@set@eq@mixed@o,\ekv@set@mixed@a}
1371 % In the mixed case we grab until the first split of \tkn{=}{12} already done,
1372 % and resplit at \tkn{=}{13} using the conditonal splitter described earlier.
1373 % \begin{macrocode}
1374 \long\def\ekv@set@eq@mixed##1\ekv@stop
1375 {%
1376 \ekv@eq@active##1\ekv@nil\ekv@mark\ekv@set@eq@mixed@a
1377 #2\ekv@mark\ekv@set@eq@mixed@o
1378 }
1379 % \end{macrocode}
1380 % If there actually was no \tkn{=}{13} before the first \tkn{=}{12} everything
1381 % was fine. We pick up our first split and remove the gibberish left by the
1382 % conditional splitter.
1383 % \begin{macrocode}
1384 \ekv@exparg{\long\def\ekv@set@eq@mixed@o
1385 ##1\ekv@nil\ekv@mark\ekv@set@eq@mixed@a\ekv@stop\ekv@mark
1386 ##2\ekv@nil#2\ekv@mark##3}%
1387 {\ekv@strip@key{##1}\ekv@set@pair##2\ekv@nil}
1388 % \end{macrocode}
1389 % |\ekv@set@eq@mixed@a| will trigger if before the first \tkn{=}{12} there was
1390 % already a \tkn{=}{13}. So we use the new split and revert the erroneous
1391 % split at \tkn{=}{12}.
1392 % \begin{macrocode}
1393 \ekv@exparg{\long\def\ekv@set@eq@mixed@a
1394 ##1\ekv@stop
1395 ##2\ekv@nil#2\ekv@mark\ekv@set@eq@mixed@o\ekv@mark
1396 ##3#2\ekv@mark##4}%
1397 {\ekv@strip@key{##1}\ekv@set@pair##2=##3}
1398 % \end{macrocode}
1399 % \end{macro}
1400 %
1401 % \begin{macro}[internal]{\ekv@set@noeq,\ekv@set@was@blank}
1402 % If there was no equals sign the current element might in fact be blank.
1403 % Blank elements are ignored, hence we need to check for that, else we can
1404 % call |\ekv@set@key|.
1405 % \begin{macrocode}
1406 \ekv@expandedarg
1407 {%
1408 \long\def\ekv@set@noeq
1409 ##1\ekv@nil\ekv@mark\ekv@set@eq@other\ekv@stop\ekv@mark
1410 }%
1411 {%
1412 \ekv@unexpanded
1413 {%
1414 \ekv@ifblank@##1\ekv@stop\ekv@ifblank@gobbletrue
1415 \ekv@stop\ekv@set@was@blank
1416 }%
1417 \ekv@unexpanded\expandafter{\ekv@strip@key{##1}\ekv@set@key}%
1418 }
1419 % \end{macrocode}
1420 % If in fact the element was blank we remove the |\ekv@set@key| call and
1421 % instead do the next iteration of |\ekv@set@other|.
1422 % \begin{macrocode}
1423 \ekv@exparg{\long\expandafter\def\expandafter\ekv@set@was@blank
1424 \ekv@strip@key{\ekv@mark##1}\ekv@set@key
1425 ##2##3,}%
1426 {\ekv@set@other{##2}{##3},}
1427 % \end{macrocode}
1428 % \end{macro}
1429 %
1430 % \begin{macro}{\ekvbreak,\ekvbreakPreSneak,\ekvbreakPostSneak}
1431 % Provide macros that can completely stop the parsing of |\ekvset|, who knows
1432 % what it'll be useful for.
1433 % \begin{macrocode}
1434 \long\def\ekvbreak##1##2\ekv@stop#1##3{##1}
1435 \long\def\ekvbreakPreSneak ##1##2\ekv@stop#1##3{##1##3}
1436 \long\def\ekvbreakPostSneak##1##2\ekv@stop#1##3{##3##1}
1437 % \end{macrocode}
1438 % \end{macro}
1439 %
1440 % \begin{macro}{\ekvsneak,\ekvsneakPre}
1441 % One last thing we want to do for |\ekvset| is to provide macros that just
1442 % smuggle stuff after |\ekvset|'s effects.
1443 % \begin{macrocode}
1444 \long\def\ekvsneak##1##2\ekv@stop#1##3{##2\ekv@stop#1{##3##1}}
1445 \long\def\ekvsneakPre##1##2\ekv@stop#1##3{##2\ekv@stop#1{##1##3}}
1446 % \end{macrocode}
1447 % \end{macro}
1448 %
1449 % \begin{macro}{\ekvparse}
1450 % Additionally to the |\ekvset| macro we also want to provide an |\ekvparse|
1451 % macro, that has the same scope as |\keyval_parse:NNn| from \pkg{expl3}.
1452 % This is pretty analogue to the |\ekvset| implementation, we just put an
1453 % |\unexpanded| here and there instead of other macros to stop the |\expanded|
1454 % on our output. The |\unexpanded\expanded{{...}}| ensures that the material
1455 % is in an alignment safe group at all time, and that it doesn't expand any
1456 % further in an |\edef| or |\expanded| context.
1457 % \begin{macrocode}
1458 \long\def\ekvparse##1##2##3%
1459 {%
1460 \ekv@unexpanded\ekv@expanded
1461 {{\ekv@parse{##1}{##2}\ekv@mark##3#1\ekv@stop#1}}%
1462 }
1463 % \end{macrocode}
1464 % \end{macro}
1465 %
1466 % \begin{macro}[internal]{\ekv@parse}
1467 % \begin{macrocode}
1468 \long\def\ekv@parse##1##2##3#1%
1469 {%
1470 \ekv@gobble@from@mark@to@stop##3\ekv@endparse\ekv@stop
1471 \ekv@parse@other{##1}{##2}##3,\ekv@stop,%
1472 }
1473 % \end{macrocode}
1474 % \end{macro}
1475 %
1476 % \begin{macro}[internal]{\ekv@endparse}
1477 % \begin{macrocode}
1478 \long\def\ekv@endparse\ekv@stop\ekv@parse@other##1\ekv@mark\ekv@stop,\ekv@stop,%
1479 {}
1480 % \end{macrocode}
1481 % \end{macro}
1482 %
1483 % \begin{macro}[internal]{\ekv@parse@other}
1484 % \begin{macrocode}
1485 \long\def\ekv@parse@other##1##2##3,%
1486 {%
1487 \ekv@gobble@from@mark@to@stop##3\ekv@endparse@other\ekv@stop
1488 \ekv@eq@other@or@active
1489 ##3\ekv@nil\ekv@mark\ekv@parse@eq@other
1490 =\ekv@mark\ekv@parse@noeq
1491 ##3\ekv@mark\ekv@parse@eq@active#2\ekv@mark{}%
1492 {##1}{##2}%
1493 \ekv@mark
1494 }
1495 % \end{macrocode}
1496 % \end{macro}
1497 %
1498 % \begin{macro}[internal]{\ekv@endparse@other}
1499 % \begin{macrocode}
1500 \ekv@exparg{\long\def\ekv@endparse@other
1501 \ekv@stop\ekv@eq@other@or@active
1502 \ekv@mark\ekv@stop\ekv@nil\ekv@mark\ekv@parse@eq@other
1503 =\ekv@mark\ekv@parse@noeq
1504 \ekv@mark\ekv@stop\ekv@mark\ekv@parse@eq@active#2\ekv@mark##1%
1505 ##2##3##4#1}%
1506 {\ekv@parse{##2}{##3}{##4}#1}
1507 % \end{macrocode}
1508 % \end{macro}
1509 %
1510 % \begin{macro}[internal]{\ekv@parse@eq@other}
1511 % \begin{macrocode}
1512 \ekv@exparg{\long\def\ekv@parse@eq@other##1\ekv@stop}%
1513 {\ekv@strip{##1}\ekv@parse@pair}%
1514 % \end{macrocode}
1515 % \end{macro}
1516 %
1517 % \begin{macro}[internal]{\ekv@parse@if@noeq}
1518 % \begin{macrocode}
1519 \def\ekv@parse@if@noeq##1\ekv@parse@noeq{}
1520 % \end{macrocode}
1521 % \end{macro}
1522 %
1523 % \begin{macro}[internal]{\ekv@parse@eq@active,\ekv@parse@eq@active@}
1524 % \begin{macrocode}
1525 \def\ekv@parse@eq@active##1%
1526 {%
1527 \ekv@parse@if@noeq##1\ekv@parse@eq@active@\ekv@parse@noeq
1528 \ekv@parse@eq@mixed
1529 }
1530 \ekv@exparg{\long\def\ekv@parse@eq@active@
1531 \ekv@parse@noeq\ekv@parse@eq@mixed
1532 ##1#2##2\ekv@mark
1533 \ekv@parse@eq@other\ekv@stop\ekv@mark#2\ekv@mark##3}%
1534 {\ekv@strip{##1}\ekv@parse@pair\ekv@mark##2}
1535 % \end{macrocode}
1536 % \end{macro}
1537 %
1538 % \begin{macro}[internal]
1539 % {\ekv@parse@eq@mixed,\ekv@parse@eq@mixed@o,\ekv@parse@eq@mixed@a}
1540 % \begin{macrocode}
1541 \long\def\ekv@parse@eq@mixed##1\ekv@stop
1542 {%
1543 \ekv@eq@active##1\ekv@nil\ekv@mark\ekv@parse@eq@mixed@a
1544 #2\ekv@mark\ekv@parse@eq@mixed@o
1545 }
1546 \ekv@exparg{\long\def\ekv@parse@eq@mixed@a
1547 ##1\ekv@stop
1548 ##2\ekv@nil#2\ekv@mark\ekv@parse@eq@mixed@o\ekv@mark
1549 ##3#2\ekv@mark##4}%
1550 {\ekv@strip{##1}\ekv@parse@pair##2=##3}
1551 \ekv@exparg{\long\def\ekv@parse@eq@mixed@o
1552 ##1\ekv@nil\ekv@mark\ekv@parse@eq@mixed@a\ekv@stop\ekv@mark
1553 ##2\ekv@nil#2\ekv@mark##3}%
1554 {\ekv@strip{##1}\ekv@parse@pair##2\ekv@nil}
1555 % \end{macrocode}
1556 % \end{macro}
1557 %
1558 % \begin{macro}[internal]{\ekv@parse@noeq,\ekv@parse@was@blank}
1559 % \begin{macrocode}
1560 \ekv@expandedarg
1561 {%
1562 \long\def\ekv@parse@noeq
1563 ##1\ekv@nil\ekv@mark\ekv@parse@eq@other\ekv@stop\ekv@mark
1564 }%
1565 {%
1566 \ekv@unexpanded
1567 {%
1568 \ekv@ifblank@##1\ekv@stop\ekv@ifblank@gobbletrue
1569 \ekv@stop\ekv@parse@was@blank
1570 }%
1571 \ekv@unexpanded\expandafter{\ekv@strip{##1}\ekv@parse@key}%
1572 }
1573 \ekv@exparg{\long\expandafter\def\expandafter\ekv@parse@was@blank
1574 \ekv@strip{\ekv@mark##1}\ekv@parse@key
1575 ##2##3##4,}%
1576 {\ekv@parse@other{##2}{##3}{##4},}
1577 % \end{macrocode}
1578 % \end{macro}
1579 %
1580 % \begin{macro}[internal]{\ekv@parse@pair,\ekv@parse@pair@}
1581 % \begin{macrocode}
1582 \ekv@exparg{\long\def\ekv@parse@pair##1##2\ekv@nil}%
1583 {\ekv@strip{##2}\ekv@parse@pair@{##1}}
1584 \ekv@exparg{\long\def\ekv@parse@pair@##1##2##3##4}%
1585 {%
1586 \ekv@ifexp{##2}%
1587 {\ekv@expansion@parse@pair{##1}{##4}}%
1588 {\ekv@unexpanded{##4{##2}{##1}}}%
1589 \ekv@parse@other{##3}{##4}%
1590 }
1591 % \end{macrocode}
1592 % \end{macro}
1593 %
1594 % \begin{macro}[internal]{\ekv@parse@key}
1595 % \begin{macrocode}
1596 \ekv@exparg{\long\def\ekv@parse@key##1##2}%
1597 {%
1598 \ekv@ifexp{##1}%
1599 {\ekv@expansion@parse@key{##2}}%
1600 {\ekv@unexpanded{##2{##1}}}%
1601 \ekv@parse@other{##2}%
1602 }
1603 % \end{macrocode}
1604 % \end{macro}
1605 %
1606 % Finally really setting things up with |\ekvset|'s temporary meaning:
1607 % \begin{macrocode}
1608 }
1609 \catcode`\,=13
1610 \catcode`\==13
1611 \ekvset,=
1612 % \end{macrocode}
1613 %
1614 % \begin{macro}{\ekvsetSneaked}
1615 % This macro can be defined just by expanding |\ekvsneak| once after expanding
1616 % |\ekvset|. To expand everything as much as possible early on we use a
1617 % temporary definition.
1618 % \begin{macrocode}
1619 \edef\ekvsetSneaked
1620 {%
1621 \ekv@unexpanded{\ekvsneak{#2}}%
1622 \ekv@unexpanded\expandafter{\ekvset{#1}{#3}}%
1623 }
1624 \ekv@expargtwice{\long\def\ekvsetSneaked#1#2#3}{\ekvsetSneaked}
1625 % \end{macrocode}
1626 % \end{macro}
1627 %
1628 % \begin{macro}{\ekvchangeset}
1629 % Provide a macro that is able to switch out the current \set\ in |\ekvset|.
1630 % This operation allows something similar to \pkg{pgfkeys}'s
1631 % \texttt{\meta{key}/.cd} mechanism. However this operation can be more
1632 % expensive than |/.cd| as we can't just redefine some token to reflect this,
1633 % but have to switch out the set expandably, so this works similar to the
1634 % |\ekvsneak| macros reading and reinserting things, but it only has to read and
1635 % reinsert the remainder of the current key's replacement code.
1636 % \begin{macrocode}
1637 \ekv@exparg{\def\ekvchangeset#1}%
1638 {%
1639 \expandafter\expandafter\expandafter
1640 \ekv@changeset\expandafter\csname\ekv@undefined@set{#1}\endcsname\ekv@empty
1641 }
1642 % \end{macrocode}
1643 % \begin{macro}[internal]{\ekv@changeset}
1644 % This macro does the real change-out of |\ekvchangeset|. |#2| will have a
1645 % leading |\ekv@empty| so that braces aren't stripped accidentally, but that
1646 % will not hurt and just expand to nothing in one step.
1647 % \begin{macrocode}
1648 \long\def\ekv@changeset#1#2\ekv@set@other#3{#2\ekv@set@other#1}
1649 % \end{macrocode}
1650 % \end{macro}
1651 % \end{macro}
1652 %
1653 % \begin{macro}{\ekvmorekv}
1654 % |\ekvmorekv| injects additional keys for consideration without needing a
1655 % nested |\ekvset|. The issue we're facing here is that we can only inject
1656 % after the next |\ekv@set@other| to not destroy the order of things, but in
1657 % theory the argument to |\ekvmorekv| could contain an active comma. Now we
1658 % have two options:
1659 % \begin{enumerate}
1660 % \item reread all keys until the end of the current |\ekv@set@other| loop
1661 % to change the commas there to an active one and insert |\ekv@set|
1662 % instead
1663 % \item loop over the argument of |\ekvmorekv| to remove all top-level
1664 % active commas and replace them with ones of category 12.
1665 % \end{enumerate}
1666 % The following implements the second approach. We use a temporary definition
1667 % of |\ekvmorekv| to get us |#1| for active commas. For the comma replacement
1668 % we set up a small loop. The |\ekv@empty| at the end of |\ekvmorekv|'s
1669 % definition protects against accidental brace loss in user code.
1670 % \begin{macrocode}
1671 \def\ekvmorekv#1%
1672 {%
1673 \long\def\ekvmorekv##1%
1674 {%
1675 \expandafter\ekv@morekv\ekv@expanded
1676 {{\ekv@morekv@loop\ekv@empty##1\ekv@stop\ekv@morekv@done#1}}%
1677 \ekv@empty
1678 }%
1679 % \end{macrocode}
1680 % \begin{macro}[internal]{\ekv@parse@more,\ekv@parse@more@}
1681 % For the expansion control we also need a way to support reinsertion in
1682 % |\ekvparse|. Since there no user code is executed this is an internal macro.
1683 % It works in the same way but the argument order exchange is different. Also,
1684 % since this is only used internally, we know it is safe to omit the trailing
1685 % |\ekv@empty|.
1686 % \begin{macrocode}
1687 \long\def\ekv@parse@more##1\@gobble##2%
1688 {%
1689 \expandafter\ekv@parse@more@\ekv@expanded
1690 {{\ekv@morekv@loop\ekv@empty##1\ekv@stop\ekv@morekv@done#1}}%
1691 }%
1692 \long\def\ekv@parse@more@##1\ekv@parse@other##2##3\ekv@mark
1693 {\ekv@parse@other{##2}{##3}\ekv@mark##1,}%
1694 % \end{macrocode}
1695 % \end{macro}
1696 % \begin{macro}[internal]{\ekv@morekv}
1697 % This is just a simple change of argument order, injecting the new comma
1698 % separated list after the current |\ekv@set@other| call.
1699 % \begin{macrocode}
1700 \long\def\ekv@morekv##1##2\ekv@set@other##3\ekv@mark
1701 {##2\ekv@set@other##3\ekv@mark##1,}
1702 % \end{macrocode}
1703 % \end{macro}
1704 % \begin{macro}[internal]
1705 % {\ekv@morekv@loop,\ekv@morekv@done,\ekv@morekv@wrap}
1706 % And here is the heavy lifting of the comma replacement. Each list element
1707 % until the next active comma is read and left as an argument for
1708 % |\ekv@morekv@wrap|, that will remove the leading |\ekv@empty|. The trick for
1709 % a fast end of the loop is that |\ekv@morekv@wrap| will read until the next
1710 % |\ekv@stop|, of which an additional one is put before |\ekv@morekv@done| so
1711 % that |wrap| will wrap the real end of the list and then |done| removes the
1712 % excess code.
1713 % \begin{macrocode}
1714 \long\def\ekv@morekv@loop##1#1%
1715 {\ekv@morekv@wrap##1\ekv@stop,\ekv@morekv@loop\ekv@empty}%
1716 \long\def\ekv@morekv@wrap##1\ekv@stop{\ekv@unexpanded\expandafter{##1}}%
1717 \def\ekv@morekv@done\ekv@stop,\ekv@morekv@loop\ekv@empty{}%
1718 }
1719 \begingroup\catcode`\,=13 \@firstofone{\endgroup\ekvmorekv,}
1720 % \end{macrocode}
1721 % \end{macro}
1722 % \end{macro}
1723 %
1724 %
1725 % \begin{macro}[internal]
1726 % {
1727 % \ekv@def@expansion@rule,\ekv@def@expansion@rule@ea,
1728 % \ekv@expansion@rule@,\ekv@expansion@csname
1729 % }
1730 % All expansion rules share the same basic structure, they do their expansion
1731 % step and then call |\ekv@expansion@rule@| (this could be done faster by
1732 % letting each rule build the macro for the next rule in |\csname|, but that
1733 % would require a more complex argument grabbing structure of the rules, and
1734 % we'd need to use |\expanded{\unexpanded{\csname ...\endcsname}\expandafter}|
1735 % or similar to not do the expansion steps of the \val\ inside of |\csname|).
1736 % As a result of this the \val\ needs to be grabbed twice per expansion step.
1737 % \begin{macrocode}
1738 \protected\def\ekv@def@expansion@rule#1#2#3#4%
1739 {%
1740 \long\expandafter\def\csname ekv@expansion@rule@#1\endcsname##1#2%
1741 {#3\ekv@expansion@rule@#4}%
1742 }
1743 \protected\def\ekv@def@expansion@rule@ea#1#2%
1744 {\ekv@def@expansion@rule{#1}{#2}\expandafter}
1745 % \end{macrocode}
1746 % Rules follow this naming scheme. Note that for an undefined rule an error is
1747 % thrown and the result of the |\csname| is |\ekv@expansion@rule@|.
1748 % \begin{macrocode}
1749 \long\def\ekv@expansion@csname#1\ekv@stop
1750 {%
1751 ekv@expansion@rule@%
1752 \ifcsname ekv@expansion@rule@#1\endcsname
1753 #1%
1754 \else
1755 \ekv@err@undefined@expansion{#1}%
1756 \fi
1757 }
1758 % \end{macrocode}
1759 % This auxiliary macro simply builds the next rule, it is used to start an
1760 % expansion chain, as well as for each defined or undefined rule.
1761 % \begin{macrocode}
1762 \long\def\ekv@expansion@rule@#1#2%
1763 {\csname\expandafter\ekv@expansion@csname\string#2\ekv@stop\endcsname{#1}}
1764 % \end{macrocode}
1765 % \end{macro}
1766 %
1767 % \begin{macro}[internal]{\ekv@expansion@rule@\ekv@mark}
1768 % The expansion chain is ended by an internal rule that behaves very different
1769 % from the others. It'll test whether the |r|-rule was used (in which case the
1770 % third argument will not be empty, and also execute that reinsertion) or grab
1771 % the code for the next step after the expansion.
1772 % \begin{macrocode}
1773 \ekv@exparg
1774 {%
1775 \long\expandafter
1776 \def\csname ekv@expansion@rule@\string\ekv@mark\endcsname
1777 #1\ekv@stop#2#3#4#5%
1778 }%
1779 {%
1780 \romannumeral\ekv@exparg{\expandafter\ekv@zero\ekv@ifempty{#3}{#5{#2}{#1}}}%
1781 {\ekv@ifempty{#2}{#4{#1}}{\ekv@err@val@no@reinsert{#2}}}%
1782 }
1783 % \end{macrocode}
1784 % \end{macro}
1785 %
1786 % \begin{macro}[internal]
1787 % {
1788 % \ekv@expansion@rule@o,\ekv@expansion@rule@e,\ekv@expansion@rule@c,
1789 % \ekv@expansion@rule@f,\ekv@expansion@rule@V,\ekv@expansion@rule@v,
1790 % \ekv@expansion@rule@s,\ekv@expansion@rule@b,\ekv@expansion@rule@\r,
1791 % \ekv@expansion@rule@g,\ekv@expansion@rule@p,\ekv@expansion@rule@P,
1792 % \ekv@expansion@rule@r,\ekv@expansion@rule@R
1793 % }
1794 % All the expansion rules are executed inside a |\csname| construct, so they
1795 % have to issue an |\endcsname| after the intended expansion took place. I
1796 % guess the code is pretty straight forward here.
1797 % \begin{macrocode}
1798 \ekv@def@expansion@rule@ea{o}{}{\expandafter{#1}}
1799 \ekv@def@expansion@rule@ea{e}{}{\ekv@expanded{{#1}}}
1800 \ekv@def@expansion@rule@ea{c}{}{\csname#1\endcsname}
1801 \ekv@def@expansion@rule@ea{f}{}{\expandafter{\romannumeral`\^^@#1}}
1802 \ekv@exparg{\ekv@def@expansion@rule{s}{}}{\ekv@strip{\ekv@mark#1}}{}
1803 \ekv@def@expansion@rule{b}{}{}{{{#1}}}
1804 \ekv@def@expansion@rule{\string\r}{#2\ekv@stop#3#4}{}{{#1}#2\ekv@stop{#3}{#4r}}
1805 \ekv@def@expansion@rule@ea{g}{}{\expandafter{\@gobble#1}}
1806 \ekv@def@expansion@rule{p}{#2}{}{{#2#1}}
1807 \ekv@def@expansion@rule{P}{#2}{}{{#1#2}}
1808 % \end{macrocode}
1809 % The |V| and |v| rules are a bit more complicated. They use some auxiliaries
1810 % but otherwise should be more or less obvious as well. The |v| one uses some
1811 % expansion to get a more meaningful error message in case the resulting
1812 % control sequence would be undefined (without actually using |\csname|, to
1813 % not accidentally define anything as |\relax|).
1814 % \begin{macrocode}
1815 \long\def\ekv@expansion@rule@V#1%
1816 {%
1817 \ifx\relax#1\ekv@err@erroneous@variable{#1}\ekv@expansion@@clean@V\fi
1818 \ekv@expansion@@V{#1}%
1819 }
1820 \ekv@expandedarg{\long\def\ekv@expansion@rule@v#1}%
1821 {%
1822 \ekv@unexpanded{\ekv@ifdefined{#1}%
1823 {\expandafter\ekv@expansion@@V\csname#1\endcsname}}%
1824 {%
1825 \ekv@unexpanded{\expandafter\ekv@err@erroneous@variable\ekv@expanded}%
1826 {{\expandafter\@gobble\string\\#1}}%
1827 \ekv@unexpanded{\ekv@expansion@rule@}{}%
1828 }%
1829 }
1830 \ekv@exparg{\long\def\ekv@expansion@rule@r#1#2\ekv@stop#3#4}%
1831 {\ekv@expansion@rule@v{#1}#2\ekv@stop{#3}{#4r}}
1832 \ekv@exparg{\long\def\ekv@expansion@rule@R#1#2\ekv@stop#3#4}%
1833 {\ekv@expansion@rule@V{#1}#2\ekv@stop{#3}{#4r}}
1834 % \end{macrocode}
1835 % \end{macro}
1836 %
1837 % \begin{macro}[internal]{\ekv@expansion@@V,\ekv@expansion@@clean@V}
1838 % The actual |V| evaluation is pretty much the same as in \pkg{expl3}, but we
1839 % need to put braces around the expansion result, hence branch slightly
1840 % differently. In general, a macro will temporarily have the meaning of
1841 % |\relax| if hit by |\noexpand| and hence the \cs[no-index]{ifx} test will
1842 % result in false (as the meaning isn't the same now), so we can tell
1843 % whether it would expand directly. Else we assume this is a register type and
1844 % use |\the|.
1845 % \begin{macrocode}
1846 \long\def\ekv@expansion@@V#1%
1847 {%
1848 \expandafter\ifx\noexpand#1#1%
1849 \ekv@fi@firstoftwo
1850 \fi
1851 \@secondoftwo
1852 {\expandafter\ekv@expansion@rule@\expandafter{\the#1}}%
1853 {\expandafter\ekv@expansion@rule@\expandafter{#1}}%
1854 }
1855 \long\def\ekv@expansion@@clean@V\fi\ekv@expansion@@V#1%
1856 {\fi\ekv@expansion@rule@{}}
1857 % \end{macrocode}
1858 % \end{macro}
1859 %
1860 % \begin{macro}[internal]
1861 % {\ekv@expansion@rule@\key,\ekv@expansion@rule@\ekv@key@after}
1862 % The |\key|-rule works by calling a nested expansion chain with swapped \key\
1863 % and \val. Instead of |\ekv@mark| marking the end of the expansion chain
1864 % we use a different final step called |\ekv@key@after|, that simply checks
1865 % whether an |r|-rule was executed (\emph{error}) and ends the current
1866 % expansion step in the outer chain.
1867 % \begin{macrocode}
1868 \ekv@def@expansion@rule{\string\key}{#2#3\ekv@stop#4}%
1869 {}{{#4}#2\ekv@key@after\ekv@stop{#1}{}{#3}}
1870 \ekv@exparg{\ekv@def@expansion@rule{\string\ekv@key@after}{\ekv@stop#2#3#4}}%
1871 {\ekv@ifempty{#3}{}\ekv@err@key@no@reinsert}
1872 {{#2}#4\ekv@stop{#1}}
1873 % \end{macrocode}
1874 % \end{macro}
1875 %
1876 %
1877 % \begin{macro}[internal]
1878 % {
1879 % \ekv@set@pair,\ekv@set@pair@,
1880 % \ekv@set@pair@relax,\ekv@set@pair@undefined,
1881 % \ekv@set@pair@u,\ekv@set@pair@u@relax,\ekv@set@pair@no@u
1882 % }
1883 % |\ekv@set@pair| gets invoked with the space and brace stripped key-name as
1884 % its first, the value as the second (delimited by |\ekv@nil|), and the set
1885 % name as the third argument. It provides tests for the key-macros and
1886 % everything to be able to throw meaningful error messages if it isn't
1887 % defined. We have two routes here, one if |\lastnamedcs| is defined and one
1888 % if it isn't. The big difference is that if it is we can omit a |\csname| and
1889 % instead just expand |\lastnamedcs| once to get the control sequence.
1890 % If the macro is defined the value will be space and brace stripped and the
1891 % key-macro called. Else branch into the error handling provided by
1892 % |\ekv@set@pair@undefined|.
1893 % \begin{macrocode}
1894 \ekv@if@lastnamedcs
1895 {%
1896 \long\def\ekv@set@pair#1\ekv@mark#2#3\ekv@nil#4%
1897 {%
1898 \ifcsname#4{#1}\endcsname\expandafter\ekv@set@pair@\lastnamedcs\fi
1899 \ekv@set@pair@undefined{#3}{#1}{#2}%
1900 \ekv@set@other#4%
1901 }
1902 \ekv@exparg{\long\def\ekv@set@pair@undefined#1#2#3\ekv@set@other#4}%
1903 {%
1904 \romannumeral
1905 \ekv@exparg
1906 {\expandafter\ekv@zero\ekv@ifexp{#3}}%
1907 {\ekv@strip{#1}\ekv@expansion@set@pair}%
1908 {%
1909 \ifcsname#4{}u\endcsname\expandafter\ekv@set@pair@u\lastnamedcs\fi
1910 \ekv@set@pair@no@u{#1}{#2}{#3}%
1911 }%
1912 \ekv@set@other#4%
1913 }
1914 }
1915 {%
1916 \long\def\ekv@set@pair#1\ekv@mark#2#3\ekv@nil#4%
1917 {%
1918 \ifcsname#4{#1}\endcsname
1919 \expandafter\ekv@set@pair@\csname#4{#1}\endcsname
1920 \fi
1921 \ekv@set@pair@undefined{#3}{#1}{#2}%
1922 \ekv@set@other#4%
1923 }
1924 \ekv@exparg{\long\def\ekv@set@pair@undefined#1#2#3\ekv@set@other#4}%
1925 {%
1926 \romannumeral
1927 \ekv@exparg
1928 {\expandafter\ekv@zero\ekv@ifexp{#3}}%
1929 {\ekv@strip{#1}\ekv@expansion@set@pair}%
1930 {%
1931 \ifcsname#4{}u\endcsname
1932 \expandafter\ekv@set@pair@u\csname#4{}u\endcsname
1933 \fi
1934 \ekv@set@pair@no@u{#1}{#2}{#3}%
1935 }%
1936 \ekv@set@other#4%
1937 }
1938 }
1939 \ekv@expandedarg{\long\def\ekv@set@pair@#1\fi\ekv@set@pair@undefined#2}%
1940 {%
1941 \ekv@unexpanded{\fi\ifx#1\relax\ekv@set@pair@relax\fi}%
1942 \ekv@unexpanded\expandafter{\ekv@strip{#2}#1\ekv@set@next@other}%
1943 }
1944 \ekv@expandafter{\long\def\ekv@set@pair@relax\fi}%
1945 \ekv@strip{#1}#2\ekv@set@next@other
1946 {\fi\ekv@set@pair@undefined{#1}}
1947 \ekv@expandedarg{\long\def\ekv@set@pair@u#1\fi\ekv@set@pair@no@u#2}%
1948 {%
1949 \ekv@unexpanded
1950 {%
1951 \fi
1952 \ifx\relax#1%
1953 \ekv@set@pair@u@relax
1954 \fi
1955 }%
1956 \ekv@unexpanded\expandafter{\ekv@strip{#2}#1}%
1957 }
1958 \ekv@expandafter{\long\def\ekv@set@pair@u@relax\fi}\ekv@strip{\ekv@mark#1}#2%
1959 {\fi\ekv@set@pair@no@u.}
1960 \long\def\ekv@set@pair@no@u#1#2#3\ekv@set@other#4%
1961 {%
1962 \ekv@ifdefined{#4{#2}N}%
1963 \ekv@err@noarg
1964 \ekv@err@unknown
1965 #4{#2}{#3}%
1966 \ekv@set@other#4%
1967 }
1968 % \end{macrocode}
1969 % \end{macro}
1970 %
1971 % \begin{macro}[internal]
1972 % {
1973 % \ekv@set@key,\ekv@set@key@,
1974 % \ekv@set@key@relax,\ekv@set@key@undefined,
1975 % \ekv@set@key@u,\ekv@set@key@u@relax,\ekv@set@key@no@u
1976 % }
1977 % Analogous to |\ekv@set@pair|, |\ekv@set@key| builds the \Nkey-macro and
1978 % provides an error-branch. It'll test whether the key-macro is defined and if
1979 % so call it, else there might be some \expnotation, or errors are thrown.
1980 % \begin{macrocode}
1981 \ekv@if@lastnamedcs
1982 {%
1983 \long\def\ekv@set@key#1\ekv@mark#2#3%
1984 {%
1985 \ifcsname#3{#1}N\endcsname\expandafter\ekv@set@key@\lastnamedcs\fi
1986 \ekv@set@key@undefined{#1}{#2}%
1987 \ekv@set@other#3%
1988 }
1989 \ekv@exparg{\long\def\ekv@set@key@undefined#1#2\ekv@set@other#3}%
1990 {%
1991 \ekv@ifexp{#2}%
1992 \ekv@expansion@set@key
1993 {%
1994 \ifcsname#3{}uN\endcsname\expandafter\ekv@set@key@u\lastnamedcs\fi
1995 \ekv@set@key@no@u{#1}{#2}%
1996 }%
1997 \ekv@set@other#3%
1998 }
1999 }
2000 {%
2001 \long\def\ekv@set@key#1\ekv@mark#2#3%
2002 {%
2003 \ifcsname#3{#1}N\endcsname
2004 \expandafter\ekv@set@key@\csname#3{#1}N\endcsname
2005 \fi
2006 \ekv@set@key@undefined{#1}{#2}%
2007 \ekv@set@other#3%
2008 }
2009 \ekv@exparg{\long\def\ekv@set@key@undefined#1#2\ekv@set@other#3}%
2010 {%
2011 \ekv@ifexp{#2}%
2012 \ekv@expansion@set@key
2013 {%
2014 \ifcsname#3{}uN\endcsname
2015 \expandafter\ekv@set@key@u\csname#3{}uN\endcsname
2016 \fi
2017 \ekv@set@key@no@u{#1}{#2}%
2018 }%
2019 \ekv@set@other#3%
2020 }
2021 }
2022 \long\def\ekv@set@key@#1\fi\ekv@set@key@undefined
2023 {\fi\ifx#1\relax\ekv@set@key@relax\fi#1\ekv@set@next@other}
2024 \long\def\ekv@set@key@relax\fi#1\ekv@set@next@other{\fi\ekv@set@key@undefined}
2025 \long\def\ekv@set@key@u#1\fi\ekv@set@key@no@u
2026 {\fi\ifx\relax#1\ekv@set@key@u@relax\fi#1}
2027 \def\ekv@set@key@u@relax\fi#1{\fi\ekv@set@key@no@u}
2028 \long\def\ekv@set@key@no@u#1#2\ekv@set@other#3%
2029 {%
2030 \ekv@ifdefined{#3{#1}}%
2031 \ekv@err@reqval
2032 \ekv@err@unknown
2033 #3{#1}{#2}%
2034 \ekv@set@other#3%
2035 }
2036 % \end{macrocode}
2037 % \end{macro}
2038 %
2039 % \begin{macro}[internal]
2040 % {
2041 % \ekv@expansion@set@key,\ekv@expansion@set@key@,
2042 % \ekv@expansion@set@pair,\ekv@expansion@set@pair@
2043 % }
2044 % These macros just pick up the pieces of the \expansion\ prefix as separate
2045 % arguments. The \val\ for the \expansion-rules is left empty. Once expansion
2046 % is done we loop back to |\ekv@set@key|, that'll pick up the processed \key\
2047 % and set it if it's now defined. The same trick is used for a \kv\ pair.
2048 % \begin{macrocode}
2049 \long\def\ekv@expansion@set@key#1#2#3%
2050 {%
2051 \ekv@expansion@rule@{#2}#1\ekv@mark\ekv@stop{}{}\ekvmorekv
2052 \ekv@expansion@set@key@
2053 }
2054 \long\def\ekv@expansion@set@key@#1#2\ekv@set@other#3%
2055 {\expandafter\ekv@set@key\detokenize{#2}\ekv@mark{#2}#3}
2056 \long\def\ekv@expansion@set@pair#1#2#3#4%
2057 {%
2058 \ekv@expansion@rule@{#1}#2\ekv@mark\ekv@stop{#3}{}\ekvmorekv
2059 \ekv@expansion@set@pair@
2060 }
2061 \long\def\ekv@expansion@set@pair@#1#2\ekv@set@other#3%
2062 {\expandafter\ekv@set@pair\detokenize{#1}\ekv@mark{#1}\ekv@mark{#2}\ekv@nil#3}
2063 % \end{macrocode}
2064 % \end{macro}
2065 %
2066 % \begin{macro}[internal]
2067 % {
2068 % \ekv@expansion@parse@key,\ekv@expansion@parse@key@,
2069 % \ekv@expansion@parse@pair,\ekv@expansion@parse@pair@
2070 % }
2071 % In |\ekvparse| we don't need to resort to looping back into |\ekv@parse@key|
2072 % or the like, we can simply leave the result via |\ekv@unexpanded|.
2073 % \begin{macrocode}
2074 \long\def\ekv@expansion@parse@key#1#2#3#4%
2075 {%
2076 \ekv@expansion@rule@{#3}#2\ekv@mark\ekv@stop{}{}\ekv@parse@more
2077 \ekv@expansion@parse@key@\@gobble{#1}%
2078 }
2079 \long\def\ekv@expansion@parse@key@#1#2\@gobble#3{\ekv@unexpanded{#3{#2}}}
2080 \long\def\ekv@expansion@parse@pair#1#2#3#4#5%
2081 {%
2082 \ekv@expansion@rule@{#1}#3\ekv@mark\ekv@stop{#4}{}\ekv@parse@more
2083 \ekv@expansion@parse@pair@\@gobble{#2}%
2084 }
2085 \long\def\ekv@expansion@parse@pair@#1#2\@gobble#3{\ekv@unexpanded{#3{#1}{#2}}}
2086 % \end{macrocode}
2087 % \end{macro}
2088 %
2089 % \begin{macro}{\ekvsetdef}
2090 % Provide a macro to define a shorthand to use |\ekvset| on a specified \set.
2091 % To gain the maximum speed |\ekvset| is expanded twice by
2092 % |\ekv@exparg| so that during runtime the macro storing the set name
2093 % is already built and one |\expandafter| doesn't have to be used.
2094 % \begin{macrocode}
2095 \ekv@exparg{\protected\def\ekvsetdef#1#2}%
2096 {%
2097 \romannumeral
2098 \ekv@exparg{\expandafter\ekv@zero\ekv@exparg{\def#1##1}}%
2099 {\ekvset{#2}{##1}}%
2100 }
2101 % \end{macrocode}
2102 % \end{macro}
2103 %
2104 % \begin{macro}{\ekvsetSneakeddef,\ekvsetdefSneaked}
2105 % And do the same for |\ekvsetSneaked| in the two possible ways, with a fixed
2106 % sneaked argument and with a flexible one.
2107 % \begin{macrocode}
2108 \ekv@exparg{\protected\def\ekvsetSneakeddef#1#2}%
2109 {%
2110 \romannumeral
2111 \ekv@exparg{\expandafter\ekv@zero\ekv@exparg{\def#1##1##2}}%
2112 {\ekvsetSneaked{#2}{##1}{##2}}%
2113 }
2114 \ekv@exparg{\protected\long\def\ekvsetdefSneaked#1#2#3}%
2115 {%
2116 \romannumeral
2117 \ekv@exparg{\expandafter\ekv@zero\ekv@exparg{\def#1##1}}%
2118 {\ekvsetSneaked{#2}{#3}{##1}}%
2119 }
2120 % \end{macrocode}
2121 % \end{macro}
2122 %
2123 % \begin{macro}[internal]{\ekv@alignsafe,\ekv@endalignsafe}
2124 % These macros protect the usage of ampersands inside of alignment contexts.
2125 % \begin{macrocode}
2126 \begingroup
2127 \catcode`\^^@=2
2128 \@firstofone{\endgroup
2129 \def\ekv@alignsafe{\romannumeral\iffalse{\fi`^^@ }
2130 }
2131 \begingroup
2132 \catcode`\^^@=1
2133 \@firstofone{\endgroup
2134 \edef\ekv@endalignsafe{\ekv@unexpanded{\romannumeral`^^@\iffalse}\fi} }
2135 }
2136 % \end{macrocode}
2137 % \end{macro}
2138 %
2139 % \begin{macro}{\ekvoptarg,\ekvoptargTF}
2140 % Provide macros to expandably collect an optional argument in brackets. The
2141 % macros here are pretty simple in nature compared to \pkg{xparse}'s
2142 % possibilities (they don't care for nested bracket levels).
2143 %
2144 % We start with a temporary definition to pre-expand |\ekv@alignsafe| (will be
2145 % |#1|) and |\ekv@endalignsafe| (will be |#2|). As |\ekv@alignsafe| starts with
2146 % a |\romannumeral| we use that to also control the number of steps needed
2147 % instead of adding another |\romannumeral|. For this we have to remove the
2148 % space token from the end of |\ekv@alignsafe|'s definition.
2149 % \begin{macrocode}
2150 \begingroup
2151 \def\ekvoptarg#1#2{%
2152 \endgroup
2153 % \end{macrocode}
2154 % The real definition starts an expansion context and afterwards grabs the
2155 % arguments. |#1| will be the next step, |#2| the default value, and |#3| might
2156 % be an opening bracket, or the mandatory argument. We check for the opening
2157 % bracket, if it is found grab the optional argument, else leave |#1{#2}| in the
2158 % input stream after ending the expansion context.
2159 % \begin{macrocode}
2160 \def\ekvoptarg{#1\ekv@optarg@a}
2161 \long\def\ekv@optarg@a##1##2##3%
2162 {%
2163 \ekv@optarg@if\ekv@mark##3\ekv@mark\ekv@optarg@b\ekv@mark[\ekv@mark
2164 #2%
2165 \@firstofone{ ##1}{##2}{##3}%
2166 }%
2167 % \end{macrocode}
2168 % The other variant of this will do roughly the same. Here, |#1| will be the
2169 % next step if an optional argument is found, |#2| the next step else, and |#3|
2170 % might be the opening bracket or mandatory argument.
2171 % \begin{macrocode}
2172 \def\ekvoptargTF{#1\ekv@optargTF@a}
2173 \long\def\ekv@optargTF@a##1##2##3%
2174 {%
2175 \ekv@optarg@if\ekv@mark##3\ekv@mark\ekv@optargTF@b{##1}\ekv@mark[\ekv@mark
2176 #2%
2177 \@firstofone{ ##2}{##3}%
2178 }
2179 % \end{macrocode}
2180 % The two macros to grab the optional argument have to remove the remainder of
2181 % the test and the wrong next step as well as grabbing the argument.
2182 % \begin{macrocode}
2183 \@firstofone{\long\def\ekv@optarg@b
2184 \ekv@mark[\ekv@mark\romannumeral`##1\fi} \@firstofone##2##3##4##5]%
2185 {#2##2{##5}}
2186 \@firstofone{\long\def\ekv@optargTF@b
2187 ##1\ekv@mark[\ekv@mark\romannumeral`##2\fi} \@firstofone##3##4##5]%
2188 {#2 ##1{##5}}
2189 }
2190 % \end{macrocode}
2191 % Do the definitions and add the test macro. We use |\ekv@strip| to remove the
2192 % trailing space from the definition of |\ekv@alignsafe|.
2193 % \begin{macrocode}
2194 \ekv@exparg
2195 {\ekv@exparg\ekv@strip{\expandafter\ekv@mark\ekv@alignsafe}\ekvoptarg}%
2196 \ekv@endalignsafe
2197 \long\def\ekv@optarg@if#1\ekv@mark[\ekv@mark{}
2198 % \end{macrocode}
2199 % \end{macro}
2200 %
2201 % \begin{macro}[internal]{\ekv@ifstar}
2202 % Internal helper to get an expandable |ifstar| implementation. This is pretty
2203 % straight forward.
2204 % \begin{macrocode}
2205 \long\def\ekv@ifstar#1#2#3%
2206 {%
2207 \ekv@ifatmostone{#3}%
2208 {\if*\string#3\ekv@fi@firstoftwo\fi}%
2209 {}%
2210 \@secondoftwo
2211 {#1}%
2212 {#2{#3}}%
2213 }
2214 % \end{macrocode}
2215 % \end{macro}
2216 %
2217 % \begin{macro}{\ekvcompile}
2218 % The idea of key-compilation is to preparse some list into a form that sets
2219 % that list very fast (but is frozen), in our case this means we do everything
2220 % except expanding the key's code. There are two levels of expansion possible,
2221 % either expand until the key-macro or expand the key-macro once as well (the
2222 % latter might break if the key-macros take additional arguments). The
2223 % following supports both by using a starred form.
2224 % \begin{macrocode}
2225 \protected\long\def\ekvcompile
2226 {%
2227 \ekv@alignsafe
2228 \ekv@ifstar{\ekv@compile{}}{\ekv@compile{\expandafter\expandafter}}%
2229 }
2230 % \end{macrocode}
2231 % \begin{macro}[internal]
2232 % {\ekv@compile,\ekv@compile@,\ekv@compile@key,\ekv@compile@pair}
2233 % |\ekvparse| starts with an |\unexpanded| that we don't need. Hence we use a
2234 % temporary definition to remove that. Else this is pretty straight forward.
2235 % There is a minor indirection (the parsing step of |\ekv@compile|) which is
2236 % necessary to grab any argument specification.
2237 % \begin{macrocode}
2238 \def\ekv@compile#1#2#3#{\ekv@compile@{#1}{#2#3}}
2239 \def\ekv@compile@#1#2#3{#2#3}
2240 \ekv@exparg{\protected\long\def\ekv@compile@#1#2#3#4}%
2241 {%
2242 \expandafter\ekv@expanded\expandafter
2243 {%
2244 \romannumeral\ekv@expargtwice{\ekv@zero\def\ekv@unexpanded{#2}}%
2245 {%
2246 \expandafter\ekv@compile@
2247 \ekvparse
2248 {\ekv@compile@key{#1}{#3}}%
2249 {\ekv@compile@pair{#1}{#3}}%
2250 {#4}%
2251 }%
2252 }%
2253 \ekv@endalignsafe
2254 }
2255 % \end{macrocode}
2256 % The compilation step just checks whether the keys are defined and leaves
2257 % their code (maybe expanded once).
2258 % \begin{macrocode}
2259 \ekv@exparg{\long\def\ekv@compile@key#1#2#3}%
2260 {%
2261 \ekvifdefinedNoVal{#2}{#3}%
2262 {\ekv@unexpanded#1\expandafter{\csname\ekv@name{#2}{#3}N\endcsname}}%
2263 {%
2264 \ekv@ifdefined{\ekv@name{#2}{}uN}
2265 {%
2266 \ekv@unexpanded#1\expandafter
2267 {%
2268 \csname\ekv@name{#2}{}uN\expandafter\endcsname
2269 \expandafter{\detokenize{#3}}{#3}%
2270 }%
2271 }%
2272 {%
2273 \ekvifdefined{#2}{#3}%
2274 {\ekv@err{missing value for `#3' in set `#2'}}%
2275 {\ekv@err{unknown key `#3' in set `#2'}}%
2276 }%
2277 }%
2278 }
2279 \ekv@exparg{\long\def\ekv@compile@pair#1#2#3#4}%
2280 {%
2281 \ekvifdefined{#2}{#3}%
2282 {\ekv@unexpanded#1\expandafter{\csname\ekv@name{#2}{#3}\endcsname{#4}}}%
2283 {%
2284 \ekv@ifdefined{\ekv@name{#2}{}u}%
2285 {%
2286 \ekv@unexpanded#1\expandafter
2287 {%
2288 \csname\ekv@name{#2}{}u\ekv@expanded{\endcsname
2289 {\ekv@unexpanded{#4}}{\detokenize{#3}}}{#3}%
2290 }%
2291 }%
2292 {%
2293 \ekvifdefinedNoVal{#2}{#3}%
2294 {\ekv@err{unwanted value for `#3' in set `#2'}}%
2295 {\ekv@err{unknown key `#3' in set `#2'}}%
2296 }%
2297 }%
2298 }
2299 % \end{macrocode}
2300 % \end{macro}
2301 % \end{macro}
2302 %
2303 % \begin{macro}{\ekverr}
2304 % \begin{macro}[internal]{\ekv@err@collect,\ekv@err@cleanup}
2305 % Since |\ekvset| is fully expandable as long as the code of the keys is (which
2306 % is unlikely) we want to somehow throw expandable errors, in our case via
2307 % a runaway argument (to my knowledge the first version of this method was
2308 % implemented by Jean-François Burnol, many thanks to him). The first step is to
2309 % ensure that the second argument (which might contain user input) doesn't
2310 % contain tokens we use as delimiters (in this case |\par|), this will be done
2311 % by the front facing macro |\ekverr|. But first we set some other things up.
2312 %
2313 % We use a temporary definition for |\ekverr| to get multiple consecutive
2314 % spaces. Then we set up the macro that will collect the error and the macro
2315 % that will throw the error. The latter will have an unreasonable long name.
2316 % This way we can convey more information. Though the information in the macro
2317 % name is static and has to be somewhat general to fit every occurence. The
2318 % important bit is that the long named macro has a delimited argument and is
2319 % short which will throw the error at the |\par| at the end of
2320 % |\ekv@err@collect|. This macro has the drawback that it will only print nicely
2321 % if the |\newlinechar| is |^^J|.
2322 % \begin{macrocode}
2323 \def\ekv@err@cleanup\par{}
2324 \def\ekv@err@collect#1%
2325 {%
2326 \def\ekv@err@collect##1\par##2%
2327 {%
2328 \expandafter
2329 \ekv@err@cleanup
2330 #1! ##2 Error: ##1\par
2331 }%
2332 \def#1##1\thanks@jfbu{}%
2333 }
2334 \def\ekverr{ }
2335 \expandafter\ekv@err@collect\csname <an-expandable-macro>^^J%
2336 completed due to above exception. \ekverr If the error^^J%
2337 summary is \ekverr not comprehensible \ekverr see the package^^J%
2338 documentation.^^J%
2339 I will try to recover now. \ekverr If you're in inter-^^J%
2340 active mode hit <return> \ekverr at the ? prompt and I^^J%
2341 continue hoping recovery\endcsname
2342 % \end{macrocode}
2343 % \begin{macrocode}
2344 \long\def\ekverr#1#2{\expandafter\ekv@err@collect\detokenize{#2}\par{#1}}
2345 % \end{macrocode}
2346 % \end{macro}
2347 % \end{macro}
2348 %
2349 % \begin{macro}[internal]{\ekv@err,\ekv@errm}
2350 % We define shorthands to throw errors in \expkv.
2351 % \begin{macrocode}
2352 \ekv@exparg{\long\def\ekv@err#1}{\ekverr{expkv}{#1}}
2353 \protected\long\def\ekv@errm#1{\errmessage{expkv Error: #1}}
2354 % \end{macrocode}
2355 % \end{macro}
2356 %
2357 % \begin{macro}[internal]
2358 % {
2359 % \ekv@err@common,\ekv@err@common@,
2360 % \ekv@err@unknown,\ekv@err@noarg,\ekv@err@reqval,
2361 % \ekv@err@undefined@expansion
2362 % }
2363 % Now we can use |\ekv@err| to set up some error messages so that we can later
2364 % use those instead of the full strings.
2365 % \begin{macrocode}
2366 \long\def\ekv@err@common #1#2{\expandafter\ekv@err@common@\string#2{#1}}
2367 \ekv@exparg{\long\def\ekv@err@common@#1`#2' #3.#4#5}%
2368 {\ekv@err{#4 `#5' in set `#2'}}
2369 \ekv@exparg{\long\def\ekv@err@unknown#1#2}{\ekv@err@common{unknown key}{#1}}
2370 \ekv@exparg{\long\def\ekv@err@noarg#1#2}%
2371 {\ekv@err@common{unwanted value for}{#1}}
2372 \ekv@exparg{\long\def\ekv@err@reqval#1#2}%
2373 {\ekv@err@common{missing value for}{#1}}
2374 \ekv@exparg{\long\def\ekv@err@redirect@kv@notfound#1#2#3\ekv@stop}%
2375 {\ekv@err{no key `#2' in sets #3}}
2376 \ekv@exparg{\def\ekv@err@redirect@k@notfound#1#2\ekv@stop}%
2377 {\ekv@err{no NoVal key `#1' in sets #2}}
2378 \ekv@exparg{\def\ekv@err@undefined@expansion#1\fi}%
2379 {\expandafter\fi\ekv@err{Undefined expansion rule `#1'}}
2380 \ekv@exparg{\def\ekv@err@erroneous@variable#1}%
2381 {\ekv@err{Erroneous variable `#1' used}}
2382 \ekv@exparg{\def\ekv@err@key@no@reinsert}%
2383 {\ekv@err{Reinsertion in \key expansion forbidden}}
2384 \ekv@exparg{\def\ekv@err@val@no@reinsert#1}%
2385 {\ekv@err{Reinsertion with key `#1' forbidden}}
2386 % \end{macrocode}
2387 % \end{macro}
2388 %
2389 % Now everything that's left is to reset the category code of |@|.
2390 % \begin{macrocode}
2391 \catcode`\@=\ekv@tmp
2392 % \end{macrocode}
2393 %
2394 % \gobbledocstriptag
2395 %</tex>
2396 %^^A=<<