summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/utilities/pgfkeys.code.tex
blob: dd660fd6ab706a685c94156f7fd0cc2911e185f2 (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
% Copyright 2007 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.


% This file is perfectly self-contained, except that the catcode of @ should be made a letter. 


% Guard against reading twice
\ifx\pgfkeysloaded\undefined
  \let\pgfkeysloaded=\relax
\else
  \expandafter\endinput
\fi
  
% The purpose of this file is to provide a general settings engine that 
% works with all TeX formats and has no save-stack impact


% This is useful:

\def\pgfkeys@ifcsname#1\endcsname#2\else#3\fi{\expandafter\ifx\csname#1\endcsname\relax#3\else#2\fi}%
\ifx\eTeXrevision\undefined%
\else%
  \expandafter\let\expandafter\pgfkeys@ifcsname\csname ifcsname\endcsname%
\fi

\def\pgfkeys@empty{}

% This also:

\ifx\PackageError\undefined
  \def\pgfkeys@error#1{\immediate\write-1{Package pgfkeys: Error! #1.}}%
\else
  \def\pgfkeys@error#1{\PackageError{pgfkeys}{#1}{}}%
\fi

% Set a key to a value
%
% #1 = key
% #2 = tokens
%
% Description:
%
% This command sets the key to the given tokens. The tokens are stored as
% is and can even contain things like #9. 
%
% Keys are organized hierarchically using something similar to Unix
% paths. Thus, a typically key might be called "/tikz/length" or
% "/tikz/length dimension/.@cmd". Some keys starting with a dot are
% special, so they should not be used as normal key names (they are
% similar to Unix files starting with a dot -- you can use them, but
% be careful).
%
% Keys are always local to the current TeX group.
%
% Example:
%
% \pgfkeyssetvalue{/tikz/length}{2cm-3cm}
% \pgfkeyssetvalue{/algo/swap}{{#2}{#1}}

\def\pgfkeyssetvalue#1#2{%
  \pgfkeys@temptoks{#2}\expandafter\edef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}%
}



% Add text to a key at the end
%
% #1 = key
% #2 = a value to be added at the beginning
% #3 = a value to be added at the end
%
% Description:
%
% This command adds #2 to the definition of the key. The key should
% have been set previously using \pgfkeyssetvalue.
%
% Example:
%
% \pgfkeysaddvalue{/tikz/length}{}{-3cm}

\def\pgfkeysaddvalue#1#2#3{%
  {%
    \toks0{#1}%
    \pgfkeysifdefined{#1}
    {\pgfkeys@temptoks\expandafter\expandafter\expandafter{\csname pgfk@#1\endcsname}}%
    {\pgfkeys@temptoks{}}%
    \toks1{#2}%
    \xdef\pgfkeys@global@temp{\the\toks0 \the\pgfkeys@temptoks \the\toks1}% believe or don't: the spaces are important
  }%
  \pgfkeyslet{#1}\pgfkeys@global@temp%
}



% Makes a key equal a given code
%
% #1 = key
% #2 = a code name
%
% Description:
%
% This command executes a \let command so that a key gets the same
% value as the parameter #2.
%
% Keys are always local to the current TeX group.
%
% Example:
%
% \pgfkeyslet{/algo/swap}{\myswap}

\def\pgfkeyslet#1#2{%
  \expandafter\let\csname pgfk@#1\endcsname#2%
}


% Retrieve the code stored in a key into a code
%
% #1 = key
% #2 = code
%
% Description:
%
% This command will set #2 to "point" to the value stored in the key.
%
% Example:
%
% \pgfkeysgetvalue{/tikz/swap}{\myswap}

\def\pgfkeysgetvalue#1#2{\expandafter\let\expandafter#2\csname pgfk@#1\endcsname}



% Retrieve the value stored in a key
%
% #1 = key
%
% Description:
%
% This command will expand to the value stored in the key. The key
% should previously have been set using \pgfkeyasetkey or \pgfkeyslet. 
%
% Example:
%
% The length is \pgfkeysvalue{/tikz/length}.

\def\pgfkeysvalueof#1{\csname pgfk@#1\endcsname}



% If for testing whether a key exists
%
% #1 = key
% #2 = if-case
% #3 = else-case
%
% Description:
%
% This if will be executed if the key exists. In eTeX mode this works
% like a normal if, in normal TeX mode you need to provide an \else.
%
% Example:
%
% \pgfkeysifdefined{/tikz/length}{key exists}{does not exist}

\def\pgfkeysifdefined#1#2#3{\pgfkeys@ifcsname pgfk@#1\endcsname#2\else#3\fi}




% Execute settings
%
% #1 = list of settings
%
% Description:
%
% The list of settings should contain comma-separated settings. Each
% setting has the following form:
%
% /path/key=value
%
% The parts "/path/" and "=value" are optional. When the path is not
% specified, the value of the token register "\pgfkeypath" is used. If
% "=value" is missing, the value of the setting "/path/key/.@def" is used
% instead. If this key is set to "\pgfvaluerequired", the key
% "/errors/value required/.@cmd" is executed. Theis error handler,
% like all other error handlers, will get the current key as its first
% parameter (unexpanded) and the current value as its second value
% (also unexpanded).
%
% Any spaces at the beginning and at the end and around the
% equals-sign are removed. The key with the complete path is set to
% the code \pgfcurrentkey.
%
% The setting is then processed according to the following rules:
%
% 1) If the key /path/key/.@cmd" is present, its code is executed
%    with the value computed above, followed by \pgfeov (end of
%    value). So, to handle
%
%    "/stuff/height=  1.5 ,"
%
%    /stuff/height/.@cmd should be set to some code, that can
%    handle the parameter
%
%    "1.5\pgfeov"
%
%    For instance, saying
%
%    \pgfkeys{/stuff/height/.@cmd}{#1\pgfeov}{\def\myheight{#1}}
%
%    will do nicely.
%
% 2) Otherwise, if the key /path/key is present, this key is
%    set to the value computed above.
%
% 3) Otherwise, if the key /handlers/key/.@cmd is present, it is executed
%    with the same parameters as in 1). Additionally, the
%    token register \pgfcurrentkeypath will be set to "/path/" and the
%    macor \pgfcurrentkeywithoutpath to "key". So, in the above
%    example if neither "/stuff/height/.@cmd" nor
%    "/stuff/height" is present, but "/handlers/height" is,
%    then "/handlers/height" is executed with the parameters:
%
%    "1.5\pgfeov"
%
%    and \pgfcurrentkey is set to "/stuff/height" and \pgfcurrentkeypath
%    is set to "/stuff/" and \pgfcurrentkeywithoutpath to "height".
%
% 4) Otherwise, if the key "/path/.unknown/.@cmd" is present, its code is
%    executed with the same parameters as in 3).
%
% 5) Otherwise, the key "/handlers/.unknown/.@cmd" is executed with the same
%    parameters as in 1).
%
% After all settings have been processed, the value of the token
% register \pgfdefaultkeypath is set to its original value. Thus, any local
% change of this token register has no effect outside the call.
%
% Example:
%
% \pgfkeys{/tikz/.is family}
% \pgfkeys{/tikz/line width/.cd,
%         .def=\pgfsetlinewidth{##1},
%         .set default=.4pt}
% \pgfkeys{tikz,line width=1pt}

\newtoks\pgfkeys@pathtoks
\def\pgfkeyscurrentpath{\the\pgfkeys@pathtoks}
\newtoks\pgfkeys@temptoks

\def\pgfkeys@root{/}
\let\pgfkeysdefaultpath\pgfkeys@root

\def\pgfkeys{\expandafter\pgfkeys@@set\expandafter{\pgfkeysdefaultpath}}%
\def\pgfkeys@@set#1#2{%
  \let\pgfkeysdefaultpath\pgfkeys@root%
  \pgfkeys@parse#2,\pgfkeys@mainstop%
  \def\pgfkeysdefaultpath{#1}}

\def\pgfkeys@parse{\futurelet\pgfkeys@possiblerelax\pgfkeys@parse@main}
\def\pgfkeys@parse@main{%
  \ifx\pgfkeys@possiblerelax\pgfkeys@mainstop%
    \expandafter\pgfkeys@cleanup%
  \else%
    \expandafter\pgfkeys@normal%
  \fi%
}
\def\pgfkeys@normal#1,{%
  \pgfkeys@unpack#1=\pgfkeysnovalue=\pgfkeys@stop%
  \pgfkeys@parse%
}
\def\pgfkeys@cleanup\pgfkeys@mainstop{}

\def\pgfkeys@mainstop{\pgfkeys@mainstop} % equals only itself
\def\pgfkeys@novalue{} % equals only itself
\def\pgfkeysnovalue{\pgfkeys@novalue} % equals only itself
\def\pgfkeysnovalue@text{\pgfkeysnovalue}
\def\pgfkeysvaluerequired{\pgfkeysvaluerequired} % equals only itself

\def\pgfkeys@unpack#1=#2=#3\pgfkeys@stop{%
  \pgfkeys@spdef\pgfkeyscurrentkey{#1}%
  \edef\pgfkeyscurrentkey{\pgfkeyscurrentkey}%
  \ifx\pgfkeyscurrentkey\pgfkeys@empty%
    % Skip
  \else%
    \pgfkeys@add@path@as@needed%
    \pgfkeys@spdef\pgfkeyscurrentvalue{#2}%
    \ifx\pgfkeyscurrentvalue\pgfkeysnovalue@text% Hmm... no value
      \pgfkeysifdefined{\pgfkeyscurrentkey/.@def}%
      {\pgfkeysgetvalue{\pgfkeyscurrentkey/.@def}{\pgfkeyscurrentvalue}}
      {}% no default, so leave it
    \fi%
    \ifx\pgfkeyscurrentvalue\pgfkeysvaluerequired%
      \pgfkeysvalueof{/errors/value required/.@cmd}\pgfkeyscurrentkey\pgfkeyscurrentvalue\pgfeov%
    \else%
      \pgfkeys@case@one%
    \fi%
  \fi}

\def\pgfkeys@case@one{%
  \pgfkeysifdefined{\pgfkeyscurrentkey/.@cmd}%
  {\pgfkeysgetvalue{\pgfkeyscurrentkey/.@cmd}{\pgfkeys@code}%
   \expandafter\pgfkeys@code\pgfkeyscurrentvalue\pgfeov}
  {\pgfkeys@case@two}%
}

\def\pgfkeys@case@two{%
  \pgfkeysifdefined{\pgfkeyscurrentkey}%
  {\pgfkeys@case@two@extern}%
  {\pgfkeys@case@three}%
}

\def\pgfkeys@case@two@extern{%
  \ifx\pgfkeyscurrentvalue\pgfkeysnovalue@text%
    \pgfkeysvalueof{\pgfkeyscurrentkey}%
  \else%
    \pgfkeyslet{\pgfkeyscurrentkey}\pgfkeyscurrentvalue%
  \fi%
}


\def\pgfkeys@case@three{%
  \pgfkeys@split@path%
  \pgfkeysifdefined{/handlers/\pgfkeyscurrentname/.@cmd}%
  {\pgfkeysgetvalue{/handlers/\pgfkeyscurrentname/.@cmd}{\pgfkeys@code}%
    \expandafter\pgfkeys@code\pgfkeyscurrentvalue\pgfeov}
  {\pgfkeys@unknown}%
}

\def\pgfkeys@unknown{%
  \pgfkeysifdefined{\pgfkeyscurrentpath/.unknown/.@cmd}%
  {%
    \pgfkeysgetvalue{\pgfkeyscurrentpath/.unknown/.@cmd}{\pgfkeys@code}%
    \expandafter\pgfkeys@code\pgfkeyscurrentvalue\pgfeov}
  {%
    \pgfkeysgetvalue{/handlers/.unknown/.@cmd}{\pgfkeys@code}%
    \expandafter\pgfkeys@code\pgfkeyscurrentvalue\pgfeov%
  }%
}


\def\pgfkey@argumentisspace#1{%
  \def\pgfkeys@spdef##1##2{%
    \futurelet\pgfkeys@possiblespace\pgfkeys@sp@a##2\pgfkeys@stop\pgfkeys@stop#1\pgfkeys@stop\relax##1}%
  \def\pgfkeys@sp@a{%
    \ifx\pgfkeys@possiblespace\pgfkeys@sptoken%
      \expandafter\pgfkeys@sp@b%
    \else%
      \expandafter\pgfkeys@sp@b\expandafter#1%
    \fi}%
  \def\pgfkeys@sp@b#1##1 \pgfkeys@stop{\pgfkeys@sp@c##1}%
}
\pgfkey@argumentisspace{ }
\def\pgfkeys@sp@c#1\pgfkeys@stop#2\relax#3{\pgfkeys@temptoks{#1}\edef#3{\the\pgfkeys@temptoks}}
{\def\:{\global\let\pgfkeys@sptoken= } \: }



\def\pgfkeys@add@path@as@needed{% Should add the path if the
  % \pgfkeyscurrentkey does not start with /
  \expandafter\futurelet\expandafter\pgfkeys@possibleslash\expandafter\pgfkeys@check@slash\pgfkeyscurrentkey\relax%
}
\def\pgfkeys@check@slash{%
  \ifx\pgfkeys@possibleslash/%
    \expandafter\pgfkeys@nevermind%
  \else%
    \expandafter\pgfkeys@addpath%
  \fi%
}

\def\pgfkeys@nevermind#1\relax{}
\def\pgfkeys@addpath#1\relax{\edef\pgfkeyscurrentkey{\pgfkeysdefaultpath#1}}

\def\pgfkeys@split@path{% Should assign the two codes
                       % \pgfkeyscurrentname and \pgfcurrentlkeypath
  \pgfkeys@pathtoks{}%
  \expandafter\pgfkeys@splitter\pgfkeyscurrentkey//%
}
\def\pgfkeys@splitter#1/#2/{%
  \def\pgfkeys@temp{#2}%
  \ifx\pgfkeys@temp\pgfkeys@empty%
    % Ah. done
    \def\pgfkeyscurrentname{#1}%
    \expandafter\pgfkeys@gobbletoslash%
  \else%
    \expandafter\pgfkeys@pathtoks\expandafter{\the\pgfkeys@pathtoks#1/}%
  \fi%
  \pgfkeys@splitter#2/%
}
\def\pgfkeys@gobbletoslash\pgfkeys@splitter/{\expandafter\pgfkeys@remove@slash\the\pgfkeys@pathtoks\relax}%
\def\pgfkeys@remove@slash#1/\relax{\pgfkeys@pathtoks{#1}}



% Quickly set keys
%
% #1 = default path
% #2 = key-value pairs
%
% Desscription:
%
% This command starts the execution with the default path set to
% #1. This command should only be used when speed is important (like
% in a heavily used macro like \tikzset). Normally, keys should be
% used to set the path. Note that if #1 equals /, then \pgfkeys will
% actually be quicker!
%
% Example:
%
% \pgfqkeys{/tikz}{myother length/.code=\def\myotherlength{#1}\pgfkeysalso{length=#1}}

\def\pgfqkeys{\expandafter\pgfkeys@@qset\expandafter{\pgfkeysdefaultpath}}%
\def\pgfkeys@@qset#1#2#3{\def\pgfkeysdefaultpath{#2/}\pgfkeys@parse#3,\pgfkeys@mainstop\def\pgfkeysdefaultpath{#1}}


% Sets keys while setting keys
%
% #1 = key-value pairs
%
% Desscription:
%
% This code may only be called inside the code that is executed for a
% key. The #1 should be a list of settings pairs. They will be executed
% as if they had been given as the argument to the \pgfkeys command.
%
% Example:
%
% \pgfkeys{tikz,myother length/.code=\def\myotherlength{#1}\pgfkeysalso{length=#1}}

\def\pgfkeysalso#1{\pgfkeys@parse#1,\pgfkeys@mainstop}



% Quickly sets keys while setting keys
%
% #1 = default path
% #2 = key-value pairs
%
% Desscription:
%
% This command executes #2 with the default path set to #1. This
% command will cause chaos if used incorrectly. The only safe
% place to use it instead of \pgfkeys is at the beginning of a TeX group.
%
% Example:
%
% \begingroup
%   \pgfqkeysalso{/tikz}{myother length/.code=\def\myotherlength{#1}\pgfkeysalso{length=#1}}

\def\pgfqkeysalso#1#2{\def\pgfkeysdefaultpath{#1/}\pgfkeys@parse#2,\pgfkeys@mainstop}




% Now setup the default handelers and keys:

% Define a key macro with one argument (\def or \edef)
%
% #1 = key
% #2 = code
%
% Description:
%
% This command will setup things so the key/.@cmd contains a macro
% that takes one parameter and has #2 as its code.
%
% Example:
%
% \pgfkeysdef{/my key}{\show#1}

\def\pgfkeysdef#1#2{%
  \def\pgfkeys@temp##1\pgfeov{#2}%
  \pgfkeyslet{#1/.@cmd}{\pgfkeys@temp}%
}
\def\pgfkeysedef#1#2{%
  \edef\pgfkeys@temp##1\pgfeov{#2}%
  \pgfkeyslet{#1/.@cmd}{\pgfkeys@temp}%
}


% Define a key macro with mutliple arguments (\def or \edef)
%
% #1 = key
% #2 = argument pattern
% #2 = code
%
% Description:
%
% This command will setup things so the key/.@cmd contains a macro
% that takes #2 as its parameter pattern and has #3 as its code.
%
% Example:
%
% \pgfkeysdefargs{/swap}{#1#2}{#2#1}

\def\pgfkeysdefargs#1#2#3{%
  \def\pgfkeys@temp#2\pgfeov{#3}%
  \pgfkeyslet{#1/.@cmd}{\pgfkeys@temp}%
  \pgfkeyssetvalue{#1/.@args}{#2\pgfeov}%
  \pgfkeyssetvalue{#1/.@body}{#3}%
}
\def\pgfkeysedefargs#1#2#3{%
  \edef\pgfkeys@temp#2\pgfeov{#3}%
  \pgfkeyslet{#1/.@cmd}{\pgfkeys@temp}%
  \pgfkeyssetvalue{#1/.@args}{#2\pgfeov}%
  \pgfkeyssetvalue{#1/.@body}{#3}%
}



% Defining a key command

\pgfkeysdef{/handlers/.code}{\pgfkeysdef{\pgfkeyscurrentpath}{#1}}
\pgfkeysdef{/handlers/.code 2 args}{\pgfkeysdefargs{\pgfkeyscurrentpath}{##1##2}{#1}}
\pgfkeysdef{/handlers/.ecode}{\pgfkeysedef{\pgfkeyscurrentpath}{#1}}
\pgfkeysdef{/handlers/.ecode 2 args}{\pgfkeysedefargs{\pgfkeyscurrentpath}{##1##2}{#1}}
\pgfkeysdefargs{/handlers/.code args}{#1#2}{\pgfkeysdefargs{\pgfkeyscurrentpath}{#1}{#2}}
\pgfkeysdefargs{/handlers/.ecode args}{#1#2}{\pgfkeysedefargs{\pgfkeyscurrentpath}{#1}{#2}}

% Adding to a key command

\pgfkeys{/handlers/.add code/.code 2 args=%
  % Find out, whether with args or not.
  \pgfkeysifdefined{\pgfkeyscurrentpath/.@args}%
  {% Yes, so add to body and reuse args
    \pgfkeysaddvalue{\pgfkeyscurrentpath/.@body}{#1}{#2}%
    % Redefine code
    \pgfkeysgetvalue{\pgfkeyscurrentpath/.@args}{\pgfkeys@tempargs}
    \pgfkeysgetvalue{\pgfkeyscurrentpath/.@body}{\pgfkeys@tempbody}
    \expandafter\expandafter\expandafter\def\expandafter\pgfkeys@temp\expandafter\pgfkeys@tempargs\expandafter{\pgfkeys@tempbody}%
    \pgfkeyslet{\pgfkeyscurrentpath/.@cmd}{\pgfkeys@temp}%
  }%
  {%
    % No, so single argument. Redefine accordingly.
    {%
      \toks0{#1}%
      \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}%
      {\pgfkeys@temptoks\expandafter\expandafter\expandafter{\csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname##1\pgfeov}}%
      {\pgfkeys@temptoks{}}%
      \toks1{#2}%
      \xdef\pgfkeys@global@temp{\the\toks0 \the\pgfkeys@temptoks \the\toks1}%
    }%
    \expandafter\def\expandafter\pgfkeys@temp\expandafter##\expandafter1\expandafter\pgfeov\expandafter{\pgfkeys@global@temp}%
    \pgfkeyslet{\pgfkeyscurrentpath/.@cmd}\pgfkeys@temp%
  }%
}
\pgfkeys{/handlers/.prefix code/.code=\pgfkeys{\pgfkeyscurrentpath/.add code={#1}{}}}%
\pgfkeys{/handlers/.append code/.code=\pgfkeys{\pgfkeyscurrentpath/.add code={}{#1}}}%


% Defining a style

\pgfkeys{/handlers/.style/.code=\pgfkeys{\pgfkeyscurrentpath/.code=\pgfkeysalso{#1}}}
\pgfkeys{/handlers/.estyle/.code=\pgfkeys{\pgfkeyscurrentpath/.ecode=\noexpand\pgfkeysalso{#1}}}
\pgfkeys{/handlers/.style args/.code 2 args=\pgfkeys{\pgfkeyscurrentpath/.code args={#1}{\pgfkeysalso{#2}}}}
\pgfkeys{/handlers/.estyle args/.code 2 args=\pgfkeys{\pgfkeyscurrentpath/.ecode args={#1}{\noexpand\pgfkeysalso{#2}}}}
\pgfkeys{/handlers/.style 2 args/.code=\pgfkeys{\pgfkeyscurrentpath/.code 2 args=\pgfkeysalso{#1}}}

% Adding to a style

\pgfkeys{/handlers/.add style/.code 2 args=\pgfkeys{\pgfkeyscurrentpath/.add code={\pgfkeysalso{#1}}{\pgfkeysalso{#2}}}}%
\pgfkeys{/handlers/.prefix style/.code=\pgfkeys{\pgfkeyscurrentpath/.add code={\pgfkeysalso{#1}}{}}}%
\pgfkeys{/handlers/.append style/.code=\pgfkeys{\pgfkeyscurrentpath/.add code={}{\pgfkeysalso{#1}}}}%


% Defining a value

\pgfkeys{/handlers/.initial/.code=\pgfkeyssetvalue{\pgfkeyscurrentpath}{#1}}
\pgfkeys{/handlers/.add/.code 2 args=\pgfkeysaddvalue{\pgfkeyscurrentpath}{#1}{#2}}
\pgfkeys{/handlers/.get/.code=\pgfkeysgetvalue{\pgfkeyscurrentpath}{#1}}

% Defining a default

\pgfkeys{/handlers/.default/.code=\pgfkeyssetvalue{\pgfkeyscurrentpath/.@def}{#1}}
\pgfkeys{/handlers/.value required/.code=\pgfkeyssetvalue{\pgfkeyscurrentpath/.@def}{\pgfkeysvaluerequired}}
\pgfkeys{/handlers/.value forbidden/.code=\pgfkeys{\pgfkeyscurrentpath/.add code=%
{%
  \ifx\pgfkeyscurrentvalue\pgfkeysnovalue@text%
  \else%
    \pgfkeysvalueof{/errors/value forbidden/.@cmd}\pgfkeyscurrentkey\pgfkeyscurrentvalue\pgfeov%
  \fi%
}{}}}


% High-level cmds

\pgfkeys{/handlers/.store in/.code=\pgfkeysalso{\pgfkeyscurrentpath/.code=\def#1{##1}}}
\pgfkeys{/handlers/.estore in/.code=\pgfkeysalso{\pgfkeyscurrentpath/.code=\edef#1{##1}}}

\pgfkeys{/handlers/.is if/.code=\pgfkeysalso{%
    \pgfkeyscurrentpath/.code=\pgfkeys@handle@boolean{#1}{##1},
    \pgfkeyscurrentpath/.default=true%
  }%
}
\def\pgfkeys@handle@boolean#1#2{%
  \pgfkeys@ifcsname#1#2\endcsname%
    \csname#1#2\endcsname%
  \else%
    \pgfkeysvalueof{/errors/boolean expected/.@cmd}\pgfkeyscurrentkey{#2}\pgfeov%
  \fi
}

\pgfkeys{/handlers/.is choice/.code=%
  \pgfkeys{%
    \pgfkeyscurrentpath/.cd,%
    .code=\expandafter\pgfkeysalso\expandafter{\pgfkeyscurrentkey/##1},
    .unknown/.style={/errors/unknown choice value=\pgfkeyscurrentkey\pgfkeyscurrentvalue}}}


% Inspection handlers

\pgfkeys{/handlers/.show value/.code=\pgfkeysgetvalue{\pgfkeyscurrentpath}{\pgfkeysshower}\show\pgfkeysshower} % inspect the value 
\pgfkeys{/handlers/.show code/.code=\pgfkeysgetvalue{\pgfkeyscurrentpath/.@cmd}{\pgfkeysshower}\show\pgfkeysshower} % inspect the body of the command


% Path handling

\pgfkeys{/handlers/.is family/.code=\pgfkeys{\pgfkeyscurrentpath/.ecode=\edef\noexpand\pgfkeysdefaultpath{\pgfkeyscurrentpath/}}}
\pgfkeys{/handlers/.cd/.code=\edef\pgfkeysdefaultpath{\pgfkeyscurrentpath/}}


% Value expansion

\pgfkeys{/handlers/.expand once/.code=\expandafter\pgfkeys@exp@call\expandafter{#1}}
\pgfkeys{/handlers/.expand twice/.code=\expandafter\expandafter\expandafter\pgfkeys@exp@call\expandafter\expandafter\expandafter{#1}}
\pgfkeys{/handlers/.expanded/.code=\edef\pgfkeys@temp{#1}\expandafter\pgfkeys@exp@call\expandafter{\pgfkeys@temp}}

\def\pgfkeys@exp@call#1{\pgfkeysalso{\pgfkeyscurrentpath=#1}}

% Try to set a key and do nothing if not define

\newif\ifpgfkeyssuccess
\pgfkeys{/handlers/.try/.code=\pgfkeys@try}
\pgfkeys{/handlers/.retry/.code=\ifpgfkeyssuccess\else\pgfkeys@try\fi}
\def\pgfkeys@try{%
  \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}%
  {%
    \pgfkeysgetvalue{\pgfkeyscurrentpath/.@cmd}{\pgfkeys@code}%
    \ifx\pgfkeyscurrentvalue\pgfkeysnovalue@text% Hmm... no value
      \pgfkeysifdefined{\pgfkeyscurrentpath/.@def}%
      {\pgfkeysgetvalue{\pgfkeyscurrentpath/.@def}{\pgfkeyscurrentvalue}}
      {}% no default, so leave it
    \fi%
    \expandafter\pgfkeys@code\pgfkeyscurrentvalue\pgfeov%
    \pgfkeyssuccesstrue%
  }%
  {%
    \pgfkeysifdefined{\pgfkeyscurrentpath}%
    {%
      \ifx\pgfkeyscurrentvalue\pgfkeysnovalue@text%
        \pgfkeysvalueof{\pgfkeyscurrentpath}%
      \else%
        \pgfkeyslet{\pgfkeyscurrentpath}\pgfkeyscurrentvalue%
      \fi%
      \pgfkeyssuccesstrue%
    }%
    {\pgfkeyssuccessfalse}%
  }%
}

% Utilities

\pgfkeys{/utils/exec/.code=#1} % simply execute the given code directly.


% Errors

\pgfkeys{/errors/boolean expected/.code 2 args=\pgfkeys@error{%
    Boolean parameter of key '#1' must be 'true' or 'false', not
    '#2'. I am going to ignore it}}  
\pgfkeys{/errors/value required/.code 2 args=\pgfkeys@error{%
    The key '#1' requires a value. I am going to ignore this key}{}}  
\pgfkeys{/errors/value forbidden/.code 2 args=\pgfkeys@error{%
    You may not specify a value for the key '#1'. I am going to ignore
    the value '#2' that you provided}}
\pgfkeys{/errors/unknown choice value/.code 2 args=\pgfkeys@error{%
    Choice '\pgfkeyscurrentname' unknown in key
    '\pgfkeyscurrentpath'. I am going to ignore this key}}
\pgfkeys{/errors/unknown key/.code 2 args=\pgfkeys@error{%
    I do not know the key '#1' and I am going to ignore it. Perhaps
    you misspelled it}}

\pgfkeys{/handlers/.unknown/.code=%
  {%
    \let\pgfkeys@orig@key=\pgfkeyscurrentkey%
    \pgfkeysalso{/errors/unknown key=\pgfkeys@orig@key{}}%
  }
}



\endinput