% 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
