-
Notifications
You must be signed in to change notification settings - Fork 26
/
pgfkeys-handler-patch.tex
58 lines (56 loc) · 2.25 KB
/
pgfkeys-handler-patch.tex
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
\RequirePackage{pgfkeys}
\RequirePackage{xpatch}
\makeatletter
% usage:
% <key>/.patch code={search}{replace}
% similar to def of /handlers/.add code/.code 2 args={...}
\pgfkeys{/handlers/.patch/.code 2 args=%
% Find out, whether with args or not.
\pgfkeysifdefined{\pgfkeyscurrentpath/.@args}%
{% Yes, so add to body and reuse args
\pgfkeyspatchvalue{\pgfkeyscurrentpath/.@body}{#1}{#2}%
% Redefine code
{%
\pgfkeysgetvalue{\pgfkeyscurrentpath/.@args}{\pgfkeys@tempargs}%
\pgfkeysgetvalue{\pgfkeyscurrentpath/.@body}{\pgfkeys@tempbody}%
\def\pgfkeys@marshal{\expandafter\gdef\expandafter\pgfkeys@global@temp\pgfkeys@tempargs}%
\expandafter\pgfkeys@marshal\expandafter{\pgfkeys@tempbody}%
}%
\pgfkeysifdefined{\pgfkeyscurrentpath/.@@body}{%
% support for \pgfkeysndefargs:
\pgfkeyslet{\pgfkeyscurrentpath/.@@body}{\pgfkeys@global@temp}%
}{%
% support for \pgfkeysdefargs:
\pgfkeyslet{\pgfkeyscurrentpath/.@cmd}{\pgfkeys@global@temp}%
}%
}{% No, so single argument (simple \pgfkeysdef). Redefine accordingly.
\edef\pgf@expanded@path{\pgfkeyscurrentpath}%
{%
\pgfkeysifdefined{\pgf@expanded@path/.@cmd}{%
\expandafter\expandafter\expandafter\pgfkeys@temptoks
\expandafter\expandafter\expandafter{%
\csname pgfk@\pgf@expanded@path/.@body\endcsname}%
}{%
\expandafter\pgfkeys@temptoks\expandafter{%
\expandafter\pgfkeyssetvalue\expandafter{\pgf@expanded@path}{##1}}%
}%
\edef\pgfkeys@temp{\the\pgfkeys@temptoks}%
\pgfkeyspatchvalue\pgfkeys@temp{#1}{#2}%
\global\let\pgfkeys@global@temp\pgfkeys@temp
}%
\def\pgf@temp{\pgfkeyssetvalue{\pgf@expanded@path/.@body}}%
\expandafter\pgf@temp\expandafter{\pgfkeys@global@temp}%
\expandafter\long\expandafter\def\expandafter\pgfkeys@temp\expandafter##\expandafter1\expandafter\pgfeov\expandafter{\pgfkeys@global@temp}%
\pgfkeyslet{\pgf@expanded@path/.@cmd}\pgfkeys@temp
}%
}
% usage:
% \pgfkeyspatchvalue{<key>}{<search>}{<replace>}
% similar to \pgfkeysaddvalue
\long\def\pgfkeyspatchvalue#1#2#3{%
\pgfkeysifdefined{#1}{%
\expandafter\xpatchcmd\csname pgfk@#1\endcsname{#2}{#3}{}
{\PackageError{pgfkeys}{Patching for key #1 fails}}%
}{}%
}
\makeatother