summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/expl3/l3keyval.dtx
blob: 086827188f4bdcc8a957122c1bc72d91f17b87de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
% \iffalse
%% File: l3keyval.dtx Copyright (C) 2006-2009 LaTeX3 project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version.  The latest version
%% of this license is in the file
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% This file is part of the ``expl3 bundle'' (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%%    http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%%   Snapshots taken from the repository represent work in progress and may
%%   not work or may contain conflicting material!  We therefore ask
%%   people _not_ to put them into distributions, archives, etc. without
%%   prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%<*driver|package>
\RequirePackage{l3names}
%</driver|package>
%\fi
\GetIdInfo$Id: l3keyval.dtx 1872 2010-03-24 07:53:45Z mittelba $
       {L3 Experimental keyval processing}
%\iffalse
%<*driver>
%\fi
\ProvidesFile{\filename.\filenameext}
  [\filedate\space v\fileversion\space\filedescription]
%\iffalse
\documentclass[full]{l3doc}
\begin{document}
\DocInput{l3keyval.dtx}
\end{document}
%</driver>
% \fi
%
%
% \title{The \textsf{l3keyval} package\thanks{This file
%         has version number \fileversion, last
%         revised \filedate.}\\
% Key-value parsing}
% \author{\Team}
% \date{\filedate}
% \maketitle
%
% \begin{documentation}
% 
% A key--value list is input of the form
%\begin{verbatim}
%  KeyOne = ValueOne ,
%  KeyTwo = ValueTwo ,
%  KeyThree          ,
%\end{verbatim} 
% where each key--value pair is separated by a comma from the rest of
% the list, and each key--value pair does not necessarily contain an
% equals sign or a value! Processing this type of input correctly 
% requires a number of careful steps, to correctly account for
% braces, spaces and the category codes of separators.
% 
% This module provides the low-level machinery for processing arbitrary
% key--value lists. The \pkg{l3keys} module provides a higher-level
% interface for managing run-time settings using key--value input,
% while other parts of \LaTeX3 also use key--value input based on
% \pkg{l3keyval} (for example the \pkg{xtemplate} module).
% 
%\section{Features of \pkg{l3keyval}} 
%
% As \pkg{l3keyval} is a low-level module, its functions are
% restricted to converting a \meta{keyval list} into keys and values
% for further processing. Each key and value (or key alone)
% has to be processed further by a function provided when
% \pkg{l3keyval} is called. Typically, this will be \emph{via}
% one of the \cs{KV_process\ldots} functions:
%\begin{verbatim}
%  \KV_process_space_removal_sanitize:NNn
%    \my_processor_function_one:n
%    \my_processor_function_two:nn
%    { <keyval list> }
%\end{verbatim} 
% The two processor functions here handle the cases where there is
% only a key, and where there is both a key and value, respectively.
% 
% \pkg{l3keyval} parses key--value lists in a manner that does not
% double "#" tokens or expand any input. The module has processor
% functions which will sanitize the category codes of \texttt{=}
% and \texttt{,} tokens (for use in the document body) as well
% as faster versions which do not do this (for use inside code
% blocks). Spaces can be removed from each end of the key and
% value (again for the document body), again with faster code
% to be used where this is not necessary. Values which are wrapped
% in braces will have exactly one set removed, meaning that
%\begin{verbatim}
%  key = {value here},
%\end{verbatim} 
% and 
%\begin{verbatim}
%  key = value here,
%\end{verbatim} 
% are treated as identical (assuming that space removal is in force).
% \pkg{l3keyval} 
% 
%\section{Functions for keyval processing}
%
% The \pkg{l3keyval} module should be accessed \emph{via} a small
% set of external functions. These correctly set up the module 
% internals for use by other parts of \LaTeX3.
% 
% In all cases, two functions have to be supplied by the programmer
% to apply to the items from the <keyval list> after \pkg{l3keyval}
% has separated out the entries. The first function should take
% one argument, and will receive the names of keys for which no
% value was supplied. The second function should take two arguments:
% a key name and the associated value. 
%
%\begin{function}{\KV_process_space_removal_sanitize:NNn}
%  \begin{syntax}
%    "\KV_process_space_removal_sanitize:NNn"
%    ~~~~<function 1> <function 2> \Arg{keyval list}
%  \end{syntax}
%  Parses the <keyval list> splitting it into keys and associated
%  values. Spaces are removed from the ends of both the key and
%  value by this function, and the category codes of non-braced
%  \texttt{=} and \texttt{,} tokens are normalised so that 
%  parsing is `category code safe'.  After parsing is completed, 
%  <function 1> is used to process keys without values and 
%  <function 2> deals with keys which have associated values. 
%\end{function}
%
%\begin{function}{\KV_process_space_removal_no_sanitize:NNn}
%  \begin{syntax}
%    "\KV_process_space_removal_no_sanitize:NNn"
%    ~~~~<function 1> <function 2> \Arg{keyval list}
%  \end{syntax}
%  Parses the <keyval list> splitting it into keys and associated
%  values. Spaces are removed from the ends of both the key and
%  value by this function, but category codes are not normalised.  
%  After parsing is completed, <function 1> is used to process keys 
%  without values and <function 2> deals with keys which have 
%  associated values. 
%\end{function}
%
%\begin{function}{\KV_process_no_space_removal_no_sanitize:NNn}
%  \begin{syntax}
%    "\KV_process_no_space_removal_no_sanitize:NNn"
%    ~~~~<function 1> <function 2> \Arg{keyval list}
%  \end{syntax}
%  Parses the <keyval list> splitting it into keys and associated
%  values. Spaces are \emph{not} removed from the ends of
%  the key and value, and category codes are \emph{not} normalised.  
%  After parsing is completed, <function 1> is used to process keys 
%  without values and <function 2> deals with keys which have 
%  associated values. 
%\end{function}
%
% \begin{variable}{\l_KV_remove_one_level_of_braces_bool}
%   This boolean controls whether or not one level of braces is
%   stripped from the key and value. The default value for this
%   boolean is \texttt{true} so that exactly one level of braces is
%   stripped. For certain applications it is desirable to keep the
%   braces in which case the programmer just has to set the boolean
%   false temporarily. Only applicable when spaces are being
%   removed.
% \end{variable}
%
%\section{Internal functions}
%
% The remaining functions provided by \pkg{l3keyval} do not
% have any protection for nesting of one call to the module 
% inside another. They should therefore not be called directly by
% other modules.
%
% \begin{function}{\KV_parse_no_space_removal_no_sanitize:n}
%   \begin{syntax}
%     "\KV_parse_no_space_removal_no_sanitize:n" \Arg{keyval list}
%   \end{syntax}
%   Parses the keys and values, passing the results to
%   \cs{KV_key_no_value_elt:n} and \cs{KV_key_value_elt:nn} as
%   appropriate. Spaces are not removed in the parsing process and
%   the category codes of \texttt{=} and \texttt{,} are not
%   normalised.
% \end{function}
%
% \begin{function}{\KV_parse_space_removal_no_sanitize:n}
%   \begin{syntax}
%     "\KV_parse_space_removal_no_sanitize:n" \Arg{keyval list}
%   \end{syntax}
%   Parses the keys and values, passing the results to
%   \cs{KV_key_no_value_elt:n} and \cs{KV_key_value_elt:nn} as
%   appropriate. Spaces are removed in the parsing process from the
%   ends of the key and value, but the category codes of \texttt{=} 
%   and \texttt{,} are not normalised.
% \end{function}
%
% \begin{function}{\KV_parse_space_removal_sanitize:n}
%   \begin{syntax}
%     "\KV_parse_space_removal_sanitize:n" \Arg{keyval list}
%   \end{syntax}
%   Parses the keys and values, passing the results to
%   \cs{KV_key_no_value_elt:n} and \cs{KV_key_value_elt:nn} as
%   appropriate. Spaces are removed in the parsing process from the
%   ends of the key and value and the category codes of \texttt{=} 
%   and \texttt{,} are normalised at the outer level 
%   (\emph{i.e}.~only unbraced tokens are affected).
% \end{function}
%
% \begin{function}{
%     \KV_key_no_value_elt:n |
%     \KV_key_value_elt:nn
% }
%   \begin{syntax}
%     "\KV_key_no_value_elt:n" \Arg{key} \\
%     "\KV_key_value_elt:n"    \Arg{key} \Arg{value}
%   \end{syntax}
%   Used by \cs{KV_parse\ldots} functions to further process
%   keys with no values and keys with values, respectively. The
%   standard definitions are error functions: the programmer should
%   provide appropriate definitions for both at point of use.
% \end{function}
% 
%\section{Variables and constants}
%
%\begin{variable}{\c_KV_single_equal_sign_tl} 
%  Constant token list to make finding \texttt{=} faster.
%\end{variable}
%
%\begin{variable}{ 
%  \l_KV_tmpa_tl | 
%  \l_KV_tmpb_tl
%}
%  Scratch token lists.
%\end{variable}
% 
%\begin{variable}{
%  \l_KV_parse_tl   | 
%  \l_KV_currkey_tl |
%  \l_KV_currval_tl 
%}
% Token list variables for various parts of the parsed input.
%\end{variable}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3keyval} implementation}
%
% \begin{function}{ \KV_sanitize_outerlevel_active_equals:N |
%                   \KV_sanitize_outerlevel_active_commas:N }
% \begin{syntax}
% "\KV_sanitize_outerlevel_active_equals:N" <tl var.>
% \end{syntax}
% Replaces catcode other "=" and "," within a <tl var.> with active characters.
% \end{function}
%
% \begin{function}{ \KV_remove_surrounding_spaces:nw |
%                   \KV_remove_surrounding_spaces_auxi:w / (EXP) }
% \begin{syntax}
%
% "\KV_remove_surrounding_spaces:nw" <tl> <token list> "\q_nil"
% "\KV_remove_surrounding_spaces_auxi:w" <token list> \verb*" Q"$\sb3$
% \end{syntax}
%   Removes a possible
%   leading space plus a possible ending space from a <token list>.
%   The first version (which is not used in the code) stores it in <tl>.
% \end{function}
%
% \begin{function}{ \KV_add_value_element:w | \KV_set_key_element:w }
% \begin{syntax}
% "\KV_set_key_element:w" <token list> "\q_nil"
% "\KV_add_value_element:w" "\q_stop" <token list> "\q_nil"
% \end{syntax}
% Specialised functions to strip spaces from their input and set the
% token registers "\l_KV_currkey_tl" or "\l_KV_currval_tl"
% respectively.
% \end{function}
%
% \begin{function}{ \KV_split_key_value_current:w                      |
%                   \KV_split_key_value_space_removal:w                |
%                   \KV_split_key_value_space_removal_detect_error:wTF |
%                   \KV_split_key_value_no_space_removal:w             }
% \begin{syntax}
% "\KV_split_key_value_current:w" \dots
% \end{syntax}
% These functions split keyval lists into chunks depending which 
% sanitising method is being used. "\KV_split_key_value_current:w" is "\cs_set_eq:NN"
% to whichever is appropriate.
% \end{function}
%
% \subsection{Module code}
%
%    We start by ensuring that the required packages are loaded.
%    \begin{macrocode}
%<*package>
\ProvidesExplPackage
  {\filename}{\filedate}{\fileversion}{\filedescription}
\package_check_loaded_expl:
%</package>
%<*initex|package>
%    \end{macrocode}
%
%
% \begin{macro}{\l_KV_tmpa_tl}
% \begin{macro}{\l_KV_tmpb_tl}
% \begin{macro}{\c_KV_single_equal_sign_tl}
% Various useful things.
%    \begin{macrocode}
\tl_new:N  \l_KV_tmpa_tl
\tl_new:N  \l_KV_tmpb_tl
\tl_const:Nn \c_KV_single_equal_sign_tl { = }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\l_KV_parse_tl}
% \begin{macro}{\l_KV_currkey_tl}
% \begin{macro}{\l_KV_currval_tl}
% Some more useful things.
%    \begin{macrocode}
\tl_new:N \l_KV_parse_tl
\tl_new:N \l_KV_currkey_tl
\tl_new:N \l_KV_currval_tl
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
%\begin{macro}{\l_KV_level_int}
% This is used to track how deeply nested calls to the keyval processor
% are, so that the correct functions are always in use.
%    \begin{macrocode}
\int_new:N \l_KV_level_int
%    \end{macrocode}
%\end{macro} 
%
% \begin{macro}{\l_KV_remove_one_level_of_braces_bool}
% A boolean to control 
%    \begin{macrocode}
\bool_new:N \l_KV_remove_one_level_of_braces_bool
\bool_set_true:N \l_KV_remove_one_level_of_braces_bool
%    \end{macrocode}
% \end{macro}
% 
%\begin{macro}{\KV_process_space_removal_sanitize:NNn}
%\begin{macro}{\KV_process_space_removal_no_sanitize:NNn}
%\begin{macro}{\KV_process_no_space_removal_no_sanitize:NNn}
%\begin{macro}[aux]{\KV_process_aux:NNNn}
% The wrapper function takes care of assigning the appropriate 
% \texttt{elt} functions before and after the parsing step. In that
% way there is no danger of a mistake with the wrong functions being
% used.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \KV_process_space_removal_sanitize:NNn {
  \KV_process_aux:NNNn \KV_parse_space_removal_sanitize:n
}
\cs_new_protected_nopar:Npn \KV_process_space_removal_no_sanitize:NNn {
  \KV_process_aux:NNNn \KV_parse_space_removal_no_sanitize:n
}
\cs_new_protected_nopar:Npn \KV_process_no_space_removal_no_sanitize:NNn {
  \KV_process_aux:NNNn \KV_parse_no_space_removal_no_sanitize:n
}
\cs_new_protected:Npn \KV_process_aux:NNNn #1#2#3#4 {
  \cs_set_eq:cN 
    { KV_key_no_value_elt_ \int_use:N \l_KV_level_int :n }
    \KV_key_no_value_elt:n 
  \cs_set_eq:cN 
    { KV_key_value_elt_ \int_use:N \l_KV_level_int :nn }
    \KV_key_value_elt:nn
  \cs_set_eq:NN \KV_key_no_value_elt:n #2
  \cs_set_eq:NN \KV_key_value_elt:nn #3
  \int_incr:N \l_KV_level_int 
  #1 {#4}  
  \int_decr:N \l_KV_level_int
  \cs_set_eq:Nc \KV_key_no_value_elt:n 
    { KV_key_no_value_elt_ \int_use:N \l_KV_level_int :n } 
  \cs_set_eq:Nc \KV_key_value_elt:nn
    { KV_key_value_elt_ \int_use:N \l_KV_level_int :nn }
}
%    \end{macrocode}
%\end{macro} 
%\end{macro} 
%\end{macro} 
%\end{macro} 
%
% \begin{macro}{\KV_sanitize_outerlevel_active_equals:N}
% \begin{macro}{\KV_sanitize_outerlevel_active_commas:N}
%   Some functions for sanitizing top level equals and commas. Replace
%   |=|$\sb{13}$ and |,|$\sb{13}$ with |=|$\sb{12}$ and |,|$\sb{12}$
%   resp.
%    \begin{macrocode}
\group_begin:
\char_set_catcode:nn{`\=}{13}
\char_set_catcode:nn{`\,}{13}
\char_set_lccode:nn{`\8}{`\=}
\char_set_lccode:nn{`\9}{`\,}
\tl_to_lowercase:n{\group_end:
\cs_new_protected_nopar:Npn \KV_sanitize_outerlevel_active_equals:N #1{
  \tl_replace_all_in:Nnn #1 = 8
}
\cs_new_nopar:Npn \KV_sanitize_outerlevel_active_commas:N #1{
  \tl_replace_all_in:Nnn #1 , 9
}
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
%
%
% \begin{macro}{\KV_remove_surrounding_spaces:nw}
% \begin{macro}{\KV_remove_surrounding_spaces_auxi:w}
% \begin{macro}[aux]{\KV_remove_surrounding_spaces_auxii:w}
% \begin{macro}{\KV_set_key_element:w}
% \begin{macro}{\KV_add_value_element:w}
%   The macro |\KV_remove_surrounding_spaces:nw| removes a possible
%   leading space plus a possible ending space from its second
%   argument and stores it in the token register |#1|.
%
%   Based on Around the Bend No.~15 but with some enhancements. For
%   instance, this definition is purely expandable.
%
%   We use a funny token |Q|$\sb3$ as a delimiter.
%    \begin{macrocode}
\group_begin:
\char_set_catcode:nn{`\Q}{3}
%    \end{macrocode}
%    \begin{macrocode}
\cs_gnew:Npn\KV_remove_surrounding_spaces:nw#1#2\q_nil{
%    \end{macrocode}
% The idea in this processing is to use a Q with strange catcode to
% remove a trailing space. But first, how to get this expansion going?
%
% If you have read the fine print in the \textsf{l3expan} module,
% you'll know that the |f| type expansion will expand until the first
% non-expandable token is seen and if this token is a space, it will
% be gobbled. Sounds useful for removing a leading space but we also
% need to make sure that it does nothing but removing that space!
% Therefore we prepend the argument to be trimmed with an
% |\exp_not:N|. Now why is that? |\exp_not:N| in itself is an
% expandable command so will allow the |f| expansion to continue. If
% the first token in the argument to be trimmed is a space, it will be
% gobbled and the expansion stop. If the first token isn't a space,
% the |\exp_not:N| turns it temporarily into |\scan_stop:| which is
% unexpandable. The processing stops but the token following directly
% after |\exp_not:N| is now back to normal.
%
% The function here allows you to insert arbitrary functions in the
% first argument but they should all be with an |f| type
% expansion. For the application in this module, we use
% |\tl_set:Nf|.
%
% Once the expansion has been kick-started, we apply
% |\KV_remove_surrounding_spaces_auxi:w| to the replacement text of
% |#2|, adding a leading |\exp_not:N|. Note that no braces are
% stripped off of the original argument.
%    \begin{macrocode}
  #1{\KV_remove_surrounding_spaces_auxi:w \exp_not:N#2Q~Q}
}
%    \end{macrocode}
% |\KV_remove_surrounding_spaces_auxi:w| removes a trailing space if
% present, then calls |\KV_remove_surrounding_spaces_auxii:w| to clean
% up any leftover bizarre Qs. In order for
% |\KV_remove_surrounding_spaces_auxii:w| to work properly we need to
% put back a Q first.
%    \begin{macrocode}
\cs_gnew:Npn\KV_remove_surrounding_spaces_auxi:w#1~Q{
  \KV_remove_surrounding_spaces_auxii:w #1 Q
}
%    \end{macrocode}
% Now all that is left to do is remove a leading space which should be
% taken care of by the function used to initiate the expansion. Simply
% return the argument before the funny Q.
%    \begin{macrocode}
\cs_gnew:Npn\KV_remove_surrounding_spaces_auxii:w#1Q#2{#1}
%    \end{macrocode}
% 
% Here are some specialized versions of the above. They do exactly
% what we want in one go. First trim spaces from the value and then
% put the result surrounded in braces onto |\l_KV_parse_tl|.
%    \begin{macrocode}
\cs_gnew_protected:Npn\KV_add_value_element:w\q_stop#1\q_nil{
  \tl_set:Nf\l_KV_currval_tl { 
    \KV_remove_surrounding_spaces_auxi:w \exp_not:N#1Q~Q
  }
  \tl_put_right:No\l_KV_parse_tl{
    \exp_after:wN { \l_KV_currval_tl }
  }
}
%    \end{macrocode}
% When storing the key we firstly remove spaces plus the prepended
% |\q_no_value|.
%    \begin{macrocode}
\cs_gnew_protected:Npn\KV_set_key_element:w#1\q_nil{
  \tl_set:Nf\l_KV_currkey_tl
  {
    \exp_last_unbraced:NNo \KV_remove_surrounding_spaces_auxi:w
      \exp_not:N \use_none:n #1Q~Q
  }
%    \end{macrocode}
% Afterwards we gobble an extra level of braces if that's what we are
% asked to do.  
%    \begin{macrocode}
  \bool_if:NT \l_KV_remove_one_level_of_braces_bool 
  {
    \exp_args:NNo \tl_set:No \l_KV_currkey_tl {
      \exp_after:wN \KV_add_element_aux:w  \l_KV_currkey_tl \q_nil
    }
  }
}
\group_end:
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% 
% \begin{macro}[aux]{\KV_add_element_aux:w}
%   A helper function for fixing braces around keys and values.
%    \begin{macrocode}
\cs_new:Npn \KV_add_element_aux:w#1\q_nil{#1}
%    \end{macrocode}
% \end{macro}
%
%
% Parse a list of keyvals, put them into list form with entries like
% |\KV_key_no_value_elt:n{key1}| and |\KV_key_value_elt:nn{key2}{val2}|.
%
% \begin{macro}[aux]{\KV_parse_sanitize_aux:n}
%   The slow parsing algorithm sanitizes active commas and equal signs
%   at the top level first. Then uses |#1| as inspector of each
%   element in the comma list.
%    \begin{macrocode}
\cs_new_protected:Npn \KV_parse_sanitize_aux:n #1 {
  \group_begin:
    \tl_clear:N \l_KV_parse_tl
    \tl_set:Nn \l_KV_tmpa_tl {#1}
    \KV_sanitize_outerlevel_active_equals:N \l_KV_tmpa_tl
    \KV_sanitize_outerlevel_active_commas:N \l_KV_tmpa_tl
    \exp_last_unbraced:NNV \KV_parse_elt:w \q_no_value 
      \l_KV_tmpa_tl , \q_nil ,
%    \end{macrocode}
% We evaluate the parsed keys and values outside the group so the
% token register is restored to its previous value.
%    \begin{macrocode}
  \exp_after:wN \group_end:
  \l_KV_parse_tl
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[aux]{\KV_parse_no_sanitize_aux:n}
%   Like above but we don't waste time sanitizing. This is probably
%   the one we will use for preamble parsing where catcodes of |=| and
%   |,| are as expected!
%    \begin{macrocode}
\cs_new_protected:Npn \KV_parse_no_sanitize_aux:n #1{
  \group_begin:
    \tl_clear:N \l_KV_parse_tl
    \KV_parse_elt:w \q_no_value #1 , \q_nil ,
  \exp_after:wN \group_end:
  \l_KV_parse_tl
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}[aux]{\KV_parse_elt:w}
%   This function will always have a |\q_no_value| stuffed in as the
%   rightmost token in |#1|. In case there was a blank entry in the
%   comma separated list we just run it again. The |\use_none:n| makes
%   sure to gobble the quark |\q_no_value|. A similar test is made to
%   check if we hit the end of the recursion.
%    \begin{macrocode}
\cs_set:Npn \KV_parse_elt:w #1,{
  \tl_if_blank:oTF{\use_none:n #1}
  { \KV_parse_elt:w \q_no_value }
  {
    \quark_if_nil:oF {\use_ii:nn #1 }
%    \end{macrocode}
% If we made it to here we can start parsing the key and value. When
% done try, try again.
%    \begin{macrocode}
    {
      \KV_split_key_value_current:w #1==\q_nil
      \KV_parse_elt:w \q_no_value
    }
  }
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\KV_split_key_value_current:w}
% The function called to split the keys and values.
%    \begin{macrocode}
\cs_new:Npn \KV_split_key_value_current:w {\ERROR}
%    \end{macrocode}
% \end{macro}
%
% We provide two functions for splitting keys and values. The reason
% being that most of the time, we should probably be in the special
% coding regime where spaces are ignored. Hence it makes no sense to
% spend time searching for extra space tokens and we can do the
% settings directly. When comparing these two versions (neither doing
% any sanitizing) the |no_space_removal| version is more than 40\%
% faster than |space_removal|.
%
% It is up to functions like |\DeclareTemplate| to check which catcode
% regime is active and then pick up the version best suited for it.
%
%
%
% \begin{macro}{\KV_split_key_value_space_removal:w}
% \begin{macro}{\KV_split_key_value_space_removal_detect_error:wTF}
% \begin{macro}[aux]{\KV_split_key_value_space_removal_aux:w}
%   The code below removes extraneous spaces around the keys and
%   values plus one set of braces around the entire value.
%
%   Unlike the version to be used when spaces are ignored, this one
%   only grabs the key which is everything up to the first = and save
%   the rest for closer inspection. Reason is that if a user has
%   entered |mykey={{myval}},| then the outer braces have already been
%   removed before we even look at what might come after the key. So
%   this is slightly more tedious (but only slightly) but at least it
%   always removes only one level of braces.
%    \begin{macrocode}
\cs_new_protected:Npn \KV_split_key_value_space_removal:w #1 = #2\q_nil{
%    \end{macrocode}
%   First grab the key.
%    \begin{macrocode}
  \KV_set_key_element:w#1\q_nil
%    \end{macrocode}
% Then we start checking. If only a key was entered, |#2| contains
% |=| and nothing else, so we test for that first.
%    \begin{macrocode}
  \tl_set:Nn\l_KV_tmpa_tl{#2}
  \tl_if_eq:NNTF\l_KV_tmpa_tl\c_KV_single_equal_sign_tl
%    \end{macrocode}
% Then we just insert the default key.
%    \begin{macrocode}
  {
    \tl_put_right:No\l_KV_parse_tl{
      \exp_after:wN \KV_key_no_value_elt:n 
      \exp_after:wN {\l_KV_currkey_tl}
    }
  }
%    \end{macrocode}
% Otherwise we must take a closer look at what is left. The remainder
% of the original list up to the comma is now stored in |#2| plus an
% additional |==|, which wasn't gobbled during the initial reading of
% arguments.  If there is an error then we can see at least one more
% |=| so we call an auxiliary function to check for this.
%    \begin{macrocode}
  {
    \KV_split_key_value_space_removal_detect_error:wTF#2\q_no_value\q_nil
    {\KV_split_key_value_space_removal_aux:w \q_stop #2}
    { \msg_kernel_error:nn { keyval } { misplaced-equals-sign } }
  }
}
%    \end{macrocode}
% The error test.
%    \begin{macrocode}
\cs_new_protected:Npn
  \KV_split_key_value_space_removal_detect_error:wTF#1=#2#3\q_nil{
    \tl_if_head_eq_meaning:nNTF{#3}\q_no_value
}
%    \end{macrocode}
% Now we can start extracting the value. Recall that |#1| here starts
% with |\q_stop| so all braces are still there! First we try to see
% how much is left if we gobble three brace groups from |#1|. If |#1|
% is empty or blank, all three quarks are gobbled. If |#1| consists of
% exactly one token or brace group, only the latter quark is left.
%    \begin{macrocode}
\cs_new:Npn \KV_val_preserve_braces:NnN #1#2#3{{#2}}
\cs_new_protected:Npn\KV_split_key_value_space_removal_aux:w #1=={
  \tl_set:Nx\l_KV_tmpa_tl{\exp_not:o{\use_none:nnn#1\q_nil\q_nil}}
  \tl_put_right:No\l_KV_parse_tl{
    \exp_after:wN \KV_key_value_elt:nn 
    \exp_after:wN {\l_KV_currkey_tl}
  }
%    \end{macrocode}
% If there a blank space or nothing at all, |\l_KV_tmpa_tl| is now
% completely empty.
%    \begin{macrocode}
    \tl_if_empty:NTF\l_KV_tmpa_tl
%    \end{macrocode}
% We just put an empty value on the stack.
%    \begin{macrocode}
  { \tl_put_right:Nn\l_KV_parse_tl{{}} }
  {
%    \end{macrocode}
% If there was exactly one brace group or token in |#1|,
% |\l_KV_tmpa_tl| is now equal to |\q_nil|. Then we can just pick it
% up as the second argument of |#1|. This will also take care of any
% spaces which might surround it.
%    \begin{macrocode}
    \quark_if_nil:NTF\l_KV_tmpa_tl
    {
      \bool_if:NTF \l_KV_remove_one_level_of_braces_bool
      {
        \tl_put_right:No\l_KV_parse_tl{
          \exp_after:wN{\use_ii:nnn #1\q_nil}
        } 
      }
      {
        \tl_put_right:No\l_KV_parse_tl{
          \exp_after:wN{\KV_val_preserve_braces:NnN #1\q_nil}
        }
      }
    }
%    \end{macrocode}
% Otherwise we grab the value.
%    \begin{macrocode}
    { \KV_add_value_element:w #1\q_nil }
  }
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\KV_split_key_value_no_space_removal:w}
%   This version is for when in the special coding regime where spaces
%   are ignored so there is no need to do any fancy space hacks,
%   however fun they may be. Since there are no spaces, a set of
%   braces around a value is automatically stripped by \TeX.
%    \begin{macrocode}
\cs_new_protected:Npn \KV_split_key_value_no_space_removal:w #1#2=#3=#4\q_nil{
  \tl_set:Nn\l_KV_tmpa_tl{#4}
  \tl_if_empty:NTF \l_KV_tmpa_tl
  {
    \tl_put_right:Nn\l_KV_parse_tl{\KV_key_no_value_elt:n{#2}}
  }
  {
    \tl_if_eq:NNTF\c_KV_single_equal_sign_tl\l_KV_tmpa_tl
    {
      \tl_put_right:Nn\l_KV_parse_tl{\KV_key_value_elt:nn{#2}{#3}}
    }
    { \msg_kernel_error:nn { keyval } { misplaced-equals-sign } }
  }
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\KV_key_no_value_elt:n}
% \begin{macro}{\KV_key_value_elt:nn}
%    \begin{macrocode}
\cs_new:Npn \KV_key_no_value_elt:n #1{\ERROR}
\cs_new:Npn \KV_key_value_elt:nn #1#2{\ERROR}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% 
%
% \begin{macro}{\KV_parse_no_space_removal_no_sanitize:n}
%   Finally we can put all the things
%   together. |\KV_parse_no_space_removal_no_sanitize:n| is the
%   version that disallows unmatched conditional and does no space
%   removal.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \KV_parse_no_space_removal_no_sanitize:n {
  \cs_set_eq:NN \KV_split_key_value_current:w \KV_split_key_value_no_space_removal:w
  \KV_parse_no_sanitize_aux:n
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\KV_parse_space_removal_sanitize:n}
% \begin{macro}{\KV_parse_space_removal_no_sanitize:n}
%   The other varieties can be defined in a similar manner. For the
%   version needed at the document level, we can use this one.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \KV_parse_space_removal_sanitize:n {
  \cs_set_eq:NN \KV_split_key_value_current:w \KV_split_key_value_space_removal:w
  \KV_parse_sanitize_aux:n
}
%    \end{macrocode}
% For preamble use by the non-programmer this is probably best.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \KV_parse_space_removal_no_sanitize:n {
  \cs_set_eq:NN \KV_split_key_value_current:w \KV_split_key_value_space_removal:w
  \KV_parse_no_sanitize_aux:n 
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
\msg_kernel_new:nnnn { keyval } { misplaced-equals-sign }
  {Misplaced~equals~sign~in~key--value~input~\msg_line_context:}
  {
    I~am~trying~to~read~some~key--value~input~but~found~two~equals~
    signs\\%
    without~a~comma~between~them.
  }  
%    \end{macrocode}
%
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
% 
%    \begin{macrocode}
%<*showmemory>
\showMemUsage
%</showmemory>
%    \end{macrocode}
%
% \end{implementation}
% \PrintIndex
%
% \endinput