% Copyright 2022 by Qrrbrbirlbel
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%

\RequirePackage{pgffor}
\unless\ifcsname iftikzext@pgffor@nolist\endcsname
  \input pgffor-ext.code.tex
\fi

\pgfqkeys{/pgf/foreach/ext}{
  .code=\pgfqkeys{/pgf/foreach/ext}{#1},
  xparser/.code 2 args={%
    \DeclareDocumentCommand\pgffor@scan@custom{ #1 u, }{%
      \def\pgffor@value{#2}%
      \pgffor@scanned
    }%
    \def\pgffor@scan{%
      \pgfutil@ifnextchar\pgffor@stop\pgffor@scanone\pgffor@scan@custom
    }%
  },
  xparser Om/.default=,
  xparser Om/.style={/pgf/foreach/ext/xparser={ O{#1} }{{##1}/{##2}}},
}
\pgfqkeys{/handlers}{
  .ext_list xparse/.code n args={3}{%
    \def\pgf@keys@temp{}%
    \foreach[xparser={#1}{#2}]\pgf@keys@key in{#3}{%
      \expandafter\pgfutil@g@addto@macro\expandafter\pgf@keys@temp\expandafter{\expandafter{\pgf@keys@key}}}%
    \edef\pgf@keys@list@path{\pgfkeyscurrentpath}%
    \expandafter\expandafter\expandafter\pgf@keys@do@list%
      \expandafter\expandafter\expandafter{%
      \expandafter\pgf@keys@list@path\expandafter}%
      \pgf@keys@temp\pgf@stop}%
}

%%% Compatibility
\tikzextset{
  compat/pgffor-ext/warn/.append code={%
    \tikzextutil@compatwarning@key{pgffor-ext}{pre 0.6}{/pgf/foreach/xparser}{/pgf/foreach/ext/xparser}%
    \tikzextutil@compatwarning@key{pgffor-ext}{pre 0.6}{/pgf/foreach/xparser Om}[]{/pgf/foreach/ext/xparser Om}%
    \tikzextutil@compatwarning@handler{pgffor-ext}{pre 0.6}{.list xparse}{.ext list xparse}%
  },
  compat/pgffor-ext/pre 0.6/.append code={%
    \pgfkeysdef{/pgf/foreach/xparser}{\pgfkeysvalueof{/pgf/foreach/ext/normal list/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/pgf/foreach/xparser Om}{\pgfkeysvalueof{/pgf/foreach/ext/xparser Om/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/pgf/foreach/xparser Om/.@def}{}%
    \pgfkeysdef{/handlers/.list xparse}{\pgfkeysvalueof{/handlers/.ext list xparse/.@cmd}##1\pgfeov}%
  },
  compat/pgffor-ext/\pgfkeysvalueof{/tikz/ext/compat/pgffor-ext}%
}
\endinput
