% Copyright 2008 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\usepgflibrary{intersections}

\let\tikz@intersect@def=\def

\pgfkeys{%
  /tikz/name path global/.code={%
    % it might have been more efficient to eliminate the storage macro
	% \tikz@intersect@namedpaths. But I keep it maintain compatibility
	% with mixed occurances of local/global names.
    \let\tikz@intersect@def=\gdef
    \pgfkeys{/tikz/name path={#1}}%
  },
  /tikz/name path local/.code={%
    \let\tikz@intersect@def=\def
    \pgfkeys{/tikz/name path={#1}}%
  },
  /tikz/name path/.code={%
    \tikz@addmode{%
      \pgfsyssoftpath@getcurrentpath\tikz@intersect@temppath%
      \ifx\tikz@intersect@namedpaths\pgfutil@empty%
      \else%
        \tikz@intersect@namedpaths%
        \pgfutil@ifundefined{tikz@intersect@path@name@#1}{}%
        {%
          \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@intersect@@temppath%
          \expandafter\expandafter\expandafter{\csname tikz@intersect@path@name@#1\endcsname}%
          \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@intersect@temppath%
          \expandafter\expandafter\expandafter{\expandafter\tikz@intersect@temppath\tikz@intersect@temppath}%
        }%
      \fi%
      \tikz@intersect@addto@path@names{#1}%
    }%
  }
}

\def\tikz@intersect@addto@path@names#1{%
  % FIXME : I have the impression that something is not correct
  % here... the \tikz@intersect@def is not used correctly! 
  % if I am not mistaken, the \tikz@intersect@def will NOT be
  % invoked until \tikz@intersect@namedpaths. CHECK IT
  % It seems it works, but only partially.
  \def\tikz@marshal{\expandafter\tikz@intersect@def\csname tikz@intersect@path@name@#1\endcsname}%
  \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@marshal%
    \expandafter\expandafter\expandafter{\expandafter\tikz@marshal\expandafter{\tikz@intersect@temppath}}%
  \ifx\tikz@intersect@def\gdef
  	% This here is to *ensure* that the \gdef is invoked. 
	%
	% I think this solves the problem.
	%
	% Please revise this routine such that it works better.
  	\tikz@marshal
  \fi
  \expandafter\pgfutil@g@addto@macro\expandafter\tikz@intersect@namedpaths\expandafter{\tikz@marshal}%
}


\let\tikz@finish@orig=\tikz@finish
\def\tikz@finish{%
	\tikz@finish@orig%
	\tikz@intersect@finish%
}

\def\tikz@intersect@finish{%	
	\ifx\tikz@intersect@namedpaths\pgfutil@empty%
	\else%
		\tikz@intersect@namedpaths%
		\let\tikz@intersect@namedpaths=\pgfutil@empty%
	\fi%
}
	
\let\tikz@intersect@namedpaths=\pgfutil@empty
\let\tikz@intersect@path@a=\pgfutil@empty
\let\tikz@intersect@path@b=\pgfutil@empty
\let\tikz@intersect@name=\pgfutil@empty
\let\tikz@intersect@total=\pgfutil@empty

\tikzset{
  name intersections/.code={%
    \tikzset{/tikz/intersection/.cd,
      by=,
      #1}%
    \pgfutil@ifundefined{tikz@intersect@path@name@\tikz@intersect@path@a}{%
      \PackageError{tikz}{I do not know the path named `\tikz@intersect@path@a'. Perhaps you misspelt it}{}}%
    {\pgfutil@ifundefined{tikz@intersect@path@name@\tikz@intersect@path@b}{%
        \PackageError{tikz}{I do not know the path named `\tikz@intersect@path@b'. Perhaps you misspelt it}{}}%
      {%
        \pgfintersectionofpaths%
        {%
          \expandafter\pgfsetpath\csname tikz@intersect@path@name@\tikz@intersect@path@a\endcsname%
        }%
        {%
          \expandafter\pgfsetpath\csname tikz@intersect@path@name@\tikz@intersect@path@b\endcsname%
        }%
        \ifx\tikz@intersect@total\pgfutil@empty%
        \else%
          \expandafter\let\tikz@intersect@total=\pgfintersectionsolutions%
        \fi%
        \ifx\tikz@intersect@name\pgfutil@empty%
          \def\tikz@intersect@@name{intersection}%
        \else%
          \let\tikz@intersect@@name=\tikz@intersect@name%
        \fi%
        \pgfmathloop%
        \ifnum\pgfmathcounter>\pgfintersectionsolutions\relax%
        \else%
          \path[reset cm]\pgfextra{\pgftransformshift{\pgfpointintersectionsolution{\pgfmathcounter}}}%
            coordinate (\tikz@intersect@@name-\pgfmathcounter);
        \repeatpgfmathloop%
        \ifx\tikz@intersect@by\pgfutil@empty%
        \else%
          \foreach\tikz@intersect@name[count=\tikz@intersection@count] in \tikz@intersect@by%
          {\expandafter\tikz@intersect@name@parse\tikz@intersect@name\pgf@stop}%
        \fi%
      }%
    }%
  },%
  intersection/.cd,
  of/.code=\tikz@intersect@path@names@parse#1\tikz@stop,%
  name/.store in=\tikz@intersect@name,
  total/.store in=\tikz@intersect@total,
  by/.store in=\tikz@intersect@by,
  sort by/.code=\edef\tikz@intersect@sort@by{#1}\tikz@intersect@check@sort@by%
}


\def\tikz@intersect@check@sort@by{%
	\ifx\tikz@intersect@sort@by\tikz@intersect@path@a%
		\pgfintersectionsortbyfirstpath%
	\else%
		\ifx\tikz@intersect@sort@by\tikz@intersect@path@b%
			\pgfintersectionsortbysecondpath%
		\else%
			\pgf@intersect@sortfalse%
		\fi%
	\fi%
}
\def\tikz@intersect@path@names@parse#1 and #2\tikz@stop{%
  \def\tikz@intersect@path@a{#1}%
  \def\tikz@intersect@path@b{#2}%
}

\def\tikz@intersect@name@parse{\pgfutil@ifnextchar[\tikz@intersect@name@parse@opt{\tikz@intersect@name@parse@opt[]}}%}
\def\tikz@intersect@name@parse@opt[#1]#2\pgf@stop{%
  \coordinate [#1] (#2) at (\tikz@intersect@@name-\tikz@intersection@count);
}%

\endinput
