% 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.
%

%% foreach
%% http://tex.stackexchange.com/a/126418/16595
%% instead of \foreach \var in {start, start + delta, ..., end}
%% this allows to use \foreach[use int={start to end step delta}] without(!) a list in {}
%% the values start, end and delta are evaluated by PGFmath at the start of the loop.
%% In addition to use int, there's also use float.
\unless\ifcsname pgfextset\endcsname
  \input tikzext-util.tex
\fi

\newif\iftikzext@pgffor@nolist
\let\tikzextorig@pgffor@scan\pgffor@scan
\pgfqkeys{/pgf/foreach/ext}{
  normal list/.code={%
    \tikzext@pgffor@nolistfalse
    \let\pgffor@scan\tikzextorig@pgffor@scan
  },
  no separator/.code={% token for token
    \def\tikzext@pgffor@scan@custom##1{%
      \def\pgffor@value{##1}%
      \pgffor@scanned
    }%
    \def\tikzext@pgffor@scan@custom@comma,{%
      \pgfutil@ifnextchar\pgffor@stop
        \pgffor@scanone
        {\tikzext@pgffor@scan@custom,}%
    }%
    \def\pgffor@scan{%
      \pgfutil@ifnextchar,%
        \tikzext@pgffor@scan@custom@comma
        {%
          \pgfutil@ifnextchar\pgffor@stop
            \pgffor@scanone
            \tikzext@pgffor@scan@custom
        }%
    }%
  },
  use int/.code={%
    \tikzext@pgffor@nolist@parse\pgfmathtruncatemacro#1\pgffor@stop
  },
  use float/.code={%
    \tikzext@pgffor@nolist@parse\pgfmathsetmacro#1\pgffor@stop
  }
}

\def\tikzext@pgffor@nolist@parse#1#2to#3\pgffor@stop{%
  \tikzext@pgffor@nolisttrue
  #1\foreachStart{#2}%
  \pgfutil@in@{step}{#3}
  \ifpgfutil@in@
    \tikzext@pgffor@nolist@parse@step{#1}#3\pgffor@stop
  \else
    \tikzext@pgffor@nolist@parse@step{#1}#3step1\pgffor@stop
  \fi
  \edef\tikzext@pgffor@nolist@list{\foreachStart,\foreachSecond,...,\foreachEnd}%
}
\def\tikzext@pgffor@nolist@parse@step#1#2step#3\pgffor@stop{%
  #1\foreachEnd{#2}%
  #1\foreachSecond{\foreachStart+#3}%
}

\let\tikzextorig@pgffor@@vars\pgffor@@vars
\def\pgffor@@vars{%
  \iftikzext@pgffor@nolist\expandafter\pgfutil@firstoftwo\else
                          \expandafter\pgfutil@secondoftwo\fi
    {\tikzext@pgffor@nolistfalse\pgffor@macro@list\tikzext@pgffor@nolist@list}
    {\tikzextorig@pgffor@@vars}%
}

%%% Compatibility
\tikzextset{
  /tikz-ext/compat/add library={pgffor-ext}{pre 0.6},
  compat/pgffor-ext/warn/.append code={%
    \tikzextutil@compatwarning@key{pgffor-ext}{pre 0.6}{/pgf/foreach/normal list}{/pgf/foreach/ext/normal list}%
    \tikzextutil@compatwarning@key{pgffor-ext}{pre 0.6}{/pgf/foreach/no separator}{/pgf/foreach/ext/no separator}%
    \tikzextutil@compatwarning@key{pgffor-ext}{pre 0.6}{/pgf/foreach/use int}{/pgf/foreach/ext/use int}%
    \tikzextutil@compatwarning@key{pgffor-ext}{pre 0.6}{/pgf/foreach/use float}{/pgf/foreach/ext/use float}%
  },
  compat/pgffor-ext/pre 0.6/.append code={%
    \pgfkeysdef{/pgf/foreach/normal list}{\pgfkeysvalueof{/pgf/foreach/ext/normal list/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/pgf/foreach/no separator}{\pgfkeysvalueof{/pgf/foreach/ext/no separator/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/pgf/foreach/use int}{\pgfkeysvalueof{/pgf/foreach/ext/use int/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/pgf/foreach/use float}{\pgfkeysvalueof{/pgf/foreach/ext/use float/.@cmd}##1\pgfeov}%
  },
  compat/pgffor-ext/\pgfkeysvalueof{/tikz/ext/compat/pgffor-ext}%
}
\endinput