%%
%% Package: spectralsequences v1.3.3 2023-01-28
%% Author: Hood Chatham
%% Email: hood@mit.edu
%% Date: 2023-01-28
%% License: Latex Project Public License
%%
%% File: sseqcheckdefinitions.code.tex
%%
%%    A few tikz commands are modified in sseqpages. This file checks the definitions of all commands modified. Of course, this is insufficient to ensure that
%%    the behavior isn't completely wrong, but at least it's a sanity check.
%%

% TODO: we could recover from failing to patch keys.

\sseq@tempiftrue
\begingroup
\def\sseq@checkprotecteddef#1{\def\sseq@temp{\sseq@checkdef@{#1}}\afterassignment\sseq@temp\protected\long\@xp\def\csname sseq@check@\sseq@macroname#1\endcsname}
\def\sseq@checkdef#1{\def\sseq@temp{\sseq@checkdef@{#1}}\afterassignment\sseq@temp\@xp\def\csname sseq@check@\sseq@macroname#1\endcsname}
\def\sseq@checkdef@#1{
    \@xp\ifx\csname sseq@check@\sseq@macroname#1\endcsname#1\else
        \sseq@tempiffalse
        % \@xp\typeout\@xp{\sseq@macroname#1}
    \fi
}

%%
%% sseqmacromakers.code.tex
%%

\ExplSyntaxOn
% I should work out how old the version of xparse has to be before this breaks,
% but I'm too lazy. It would also be nice if I could figure out how to ensure
% that my trick of setting \l__cmd_grab_expandably_bool to false works
% correctly, but this might be difficult
\@ifpackagelater{xparse}{2017/02/08}{
  \cs_if_exist:NTF \__xparse_grab_u:w {
    \global\sseq@patchxparseUtrue
  }{
    \sseq@checkprotecteddef \__cmd_grab_u:w #1#2 \__cmd_run_code:{
        \__cmd_grab_u_aux:nnN {#1} {#2} \cs_set_protected_nopar:Npn
    }

    \sseq@checkprotecteddef\__cmd_grab_u_aux:nnN #1#2#3 {
        \tl_set:Nn \l__cmd_signature_tl {#2}
        \exp_after:wN #3 \l__cmd_fn_tl ##1 #1
          { \__cmd_add_arg:n {##1} }
        \l__cmd_fn_tl
    }


    \sseq@checkprotecteddef \__cmd_normalize_type_u:w #1 {
        \quark_if_recursion_tail_stop_do:nn {#1} { \__cmd_bad_arg_spec:wn }
        \__cmd_normalize_check_lu:N u
        \__cmd_add_arg_spec_mandatory:n { u {#1} } % Oct 17, 2018
%        \int_incr:N \l__cmd_mandatory_args_int
%        \tl_clear:N \l__cmd_last_delimiters_tl
        \__cmd_normalize_arg_spec_loop:n
    }

    \ifsseq@tempif
        \global\sseq@patchxparseUtrue
    \else
        \global\sseq@patchxparseUfalse
    \fi
    \sseq@tempiftrue
  }
}{}


\ExplSyntaxOff
\sseq@tempiftrue
%%
%% sseqkeys.code.tex
%%


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

\ifsseq@tempif\else
  % Try again with new v3.1.10 definition
  \sseq@tempiftrue
  \sseq@checkdef\pgfkeys@case@one{%
    \pgfkeysifdefined{\pgfkeyscurrentkey/.@cmd}%
    {\pgfkeysgetvalue{\pgfkeyscurrentkey/.@cmd}{\pgfkeys@code}%
      \ifx\pgfkeys@code\relax\expandafter\pgfkeys@firstoftwo\else\expandafter\pgfkeys@secondoftwo\fi
      {\pgfkeys@unknown}%
      {\expandafter\pgfkeys@code\pgfkeyscurrentvalue\pgfeov}}
    {\pgfkeys@case@two}%
  }
\fi


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


\ifsseq@tempif\else
    \endgroup
    \sseq@tempiffalse % We'll throw a critical error in spectralsequences.sty to end input
    \endinput
\fi
\sseq@tempiftrue
%%
%% sseqdrawing.code.tex
%%

%from \pgf\frontendlayer\tikz\libraries\tikzlibraryfit.code.tex line 81
\sseq@checkdef\tikz@lib@fit@scan@handle#1{%
  \iftikz@shapeborder%
    % Ok, fit all four external anchors, if they exist
    \tikz@lib@fit@adjust{\pgfpointanchor{\tikz@shapeborder@name}{west}}%
    \tikz@lib@fit@adjust{\pgfpointanchor{\tikz@shapeborder@name}{east}}%
    \tikz@lib@fit@adjust{\pgfpointanchor{\tikz@shapeborder@name}{north}}%
    \tikz@lib@fit@adjust{\pgfpointanchor{\tikz@shapeborder@name}{south}}%
  \else%
    \tikz@lib@fit@adjust{#1}%
  \fi%
  \tikz@lib@fit@scan%
}

% from \pgf\frontendlayer\tikz\tikz.code.tex line 5164
\sseq@checkdef\tikz@calc@anchor#1.#2\tikz@stop{%
    \pgfpointanchor{\tikz@pp@name{#1}}{#2}%
}

\ifsseq@tempif
    \global\sseq@patchfittrue
\else
    % New version of \tikz@calc@anchor
    \sseq@tempiftrue
    \sseq@checkdef\tikz@calc@anchor#1.#2\tikz@stop{%
        \ifcsname pgf@sh@ns@\tikz@pp@name {#1}\endcsname
            \pgfpointanchor {\tikz@pp@name {#1}}{#2}%
        \else
            \pgfpointanchor {#1}{#2}%
        \fi
    }
    \ifsseq@tempif
        \global\sseq@patchfittrue
    \else
        \sseq@warning{fit-patch-failed}
        \global\sseq@patchfitfalse
    \fi
\fi

\sseq@tempiftrue


%%
%% sseqforeach.code.tex
%%
\sseq@checkdef\pgffor@@vars@opt[#1]{\pgfkeys{/pgf/foreach/.cd,#1}\pgffor@vars}

\sseq@checkdef\pgffor@doloop{%
  \pgffor@begingroup
    \edef\pgffor@temp{\expandafter\Pgffor@geT\the\pgffor@iter}%
    \edef\pgffor@incheck{{.0/}{\pgffor@temp/}}%
    \expandafter\pgfutil@in@\pgffor@incheck%
    \ifpgfutil@in@%
      \expandafter\pgffor@strip\pgffor@temp%
    \fi%
    \expandafter\pgfutil@in@\expandafter/\expandafter{\pgffor@var}%
    \ifpgfutil@in@%
      \expandafter\def\expandafter\pgffor@valuerest\expandafter{\pgffor@temp//\relax}%
      \expandafter\pgffor@multiassign\pgffor@var/\pgffor@stop/\pgffor@stop/\relax%
    \else%
    	% Convert to alphabetic sequence, if necessary.
    	\ifpgffor@alphabeticsequence%
    		\pgffor@makealphabetic\pgffor@temp%
    		\expandafter\let\pgffor@var=\pgffor@temp%    						
    	\else%
    		\expandafter\expandafter\expandafter\def\expandafter\pgffor@var\expandafter{\pgffor@temp}%
      \fi%
    \fi%
    % Insert any context, if any.
    \ifpgffor@context%
      \let\pgffor@temp=\pgffor@dots@pre%
	    \expandafter\pgfutil@append@macrotomacro\expandafter%
	    	{\expandafter\pgffor@temp\expandafter}\expandafter{\pgffor@var}%
	    \expandafter\pgfutil@append@macrotomacro\expandafter%
	    	{\expandafter\pgffor@temp\expandafter}\expandafter{\pgffor@dots@post}%
	    \expandafter\let\pgffor@var=\pgffor@temp%
	  \fi%
	% Perform assignments before loop body.
	\ifx\pgffor@assign@before@code\pgfutil@empty%
    \else%
    	\pgffor@assign@before@code%
    \fi%
    %
    \expandafter\expandafter\expandafter\pgffor@reset@hooks\expandafter\pgffor@beginhook\expandafter\pgffor@body\pgffor@endhook%
    %
    % Perform assignments after loop body.
	  \ifx\pgffor@assign@after@code\pgfutil@empty%
    \else%
    	\pgffor@assign@after@code%
    \fi%
  \pgffor@endgroup%
  \pgffor@loop%
}

\sseq@checkdef\pgffor@invokebody{%
	\pgffor@begingroup%
    \expandafter\pgfutil@in@\expandafter/\expandafter{\pgffor@var}%
    \ifpgfutil@in@%
      \expandafter\def\expandafter\pgffor@valuerest\expandafter{\pgffor@value//\relax}%
      \expandafter\pgffor@multiassign\pgffor@var/\pgffor@stop/\pgffor@stop/\relax%
    \else%
      \expandafter\expandafter\expandafter\def\expandafter\pgffor@var\expandafter{\pgffor@value}%
    \fi%
    % Execute assign once code.
    \ifx\pgffor@assign@once@code\pgfutil@empty%
  	\else\pgffor@assign@once@code%
  	\fi%
    % Execute assign before code.
    \ifx\pgffor@assign@before@code\pgfutil@empty%
    \else\pgffor@assign@before@code%
    \fi%
    %
    \expandafter\expandafter\expandafter\pgffor@reset@hooks\expandafter\pgffor@beginhook\expandafter\pgffor@body\pgffor@endhook%
    % Execute assign after code.
    \ifx\pgffor@assign@after@code\pgfutil@empty%
    \else%
    	\pgffor@assign@after@code%
    \fi%
    %
  \pgffor@endgroup%
}

\ifsseq@tempif
    \global\sseq@patchforeachtrue
\else
    \sseq@warning{foreach-patch-failed}
    \global\sseq@patchforeachfalse
\fi

\endgroup 