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

%% For the operators r-lr, r-rl, r-ud and r-du we need to check after TikZ found r
\let\tikz@orig@rect\tikz@rect
\def\tikz@rect{\pgfutil@ifnextchar e\tikz@orig@rect\qrr@tikz@r}
\def\qrr@tikz@r -{%
  \pgfutil@ifnextchar l\qrr@tikz@lr@lineto{%
    \pgfutil@ifnextchar r\qrr@tikz@rl@lineto{%
      \pgfutil@ifnextchar u\qrr@tikz@ud@lineto{%
        \pgfutil@ifnextchar d\qrr@tikz@du@lineto\tikz@expand
      }%
    }%
  }%
}
%% For the operator |-| we need to check after TikZ found |-
\let\tikz@orig@vh@lineto@next\tikz@vh@lineto@next
\def\tikz@vh@lineto@next{%
  \pgfutil@ifnextchar|{\expandafter\qrr@tikz@vhv@lineto\pgfutil@gobble}%
    {\pgfutil@ifnextchar\tikz@activebar{\expandafter\qrr@tikz@vhv@lineto\pgfutil@gobble}%
      {\tikz@orig@vh@lineto@next}}}
%% For the operator -|- we need to check after TikZ found -|
\let\tikz@orig@hv@lineto\tikz@hv@lineto
\def\tikz@hv@lineto{%
  \pgfutil@ifnextchar-{\expandafter\qrr@tikz@hvh@lineto\pgfutil@gobble}%
    {\tikz@orig@hv@lineto}}

%% |-|[<opts>] and -|-[<opts>] are allowed
\def\qrr@tikz@vhv@lineto{\pgfutil@ifnextchar[{\qrr@tikz@collect@hvvh@options\qrr@tikz@vhv@lineto@next}{\qrr@tikz@collect@hvvh@options\qrr@tikz@vhv@lineto@next[]}}
\def\qrr@tikz@hvh@lineto{\pgfutil@ifnextchar[{\qrr@tikz@collect@hvvh@options\qrr@tikz@hvh@lineto@next}{\qrr@tikz@collect@hvvh@options\qrr@tikz@hvh@lineto@next[]}}

%% r-rl[<opts>], r-lr[<opts>], etc.
\def\qrr@tikz@rl@lineto rl{\pgfutil@ifnextchar[{\qrr@tikz@collect@hvvh@options\qrr@tikz@rl@lineto@next}{\qrr@tikz@collect@hvvh@options\qrr@tikz@rl@lineto@next[]}}
\def\qrr@tikz@lr@lineto lr{\pgfutil@ifnextchar[{\qrr@tikz@collect@hvvh@options\qrr@tikz@lr@lineto@next}{\qrr@tikz@collect@hvvh@options\qrr@tikz@lr@lineto@next[]}}
\def\qrr@tikz@du@lineto du{\pgfutil@ifnextchar[{\qrr@tikz@collect@hvvh@options\qrr@tikz@du@lineto@next}{\qrr@tikz@collect@hvvh@options\qrr@tikz@du@lineto@next[]}}
\def\qrr@tikz@ud@lineto ud{\pgfutil@ifnextchar[{\qrr@tikz@collect@hvvh@options\qrr@tikz@ud@lineto@next}{\qrr@tikz@collect@hvvh@options\qrr@tikz@ud@lineto@next[]}}

%% TikZ interjects are done, the rest is independent:
\input tikzlibrarypaths.ortho.tex

%% a few nice to haves, use with to or edge operator
\let\tikz@origtotarget\pgfutil@empty
\tikzset{
  node as new start/.is if=tikz@ortho@preflush,
  node as new start,
  horizontal vertical/.style={to path={-| (\tikztotarget) \tikztonodes}},
  vertical horizontal/.style={to path={|- (\tikztotarget) \tikztonodes}},
  horizontal vertical horizontal/.style={to path={-|- (\tikztotarget) \tikztonodes}},
  vertical horizontal vertical/.style={to path={|-| (\tikztotarget) \tikztonodes}},
  only vertical second/.style={to path={
    \pgfextra
      \let\tikz@ortho@anchor\pgfutil@empty
      \let\tikz@origtotarget\pgfutil@empty
      \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)\relax
      \iftikz@shapeborder
        \tikz@scan@one@point\pgfutil@firstofone(\tikztotarget)\relax
        \ifdim\pgf@y>\tikz@lasty\relax
          \edef\tikztostart{\tikztostart.north}%
          \iftikz@shapeborder\def\tikz@ortho@anchor{.south}\fi
        \else
          \edef\tikztostart{\tikztostart.south}%
          \iftikz@shapeborder\def\tikz@ortho@anchor{.north}\fi
        \fi
      \fi
      \def\tikz@tempa{#1}%
      \ifx\tikz@tempa\pgfutil@empty\else
        \let\tikz@origtotarget\tikztotarget
        \edef\tikztotarget{[xshift={#1}]\tikztotarget\tikz@ortho@anchor}%
      \fi
    \endpgfextra
    [insert path/.expanded={
      (perpendicular cs: horizontal line through={(\tikztostart)},
                         vertical line through={(\tikztotarget)}) -- (\tikztotarget)}] \tikztonodes
      \pgfextra
        \ifx\tikz@origtotarget\pgfutil@empty\else
          \iftikz@ortho@preflush(\tikz@origtotarget)\fi\fi\endpgfextra}},
  only vertical second/.default=,
  only horizontal second/.style={to path={
    \pgfextra
      \let\tikz@ortho@anchor\pgfutil@empty
      \let\tikz@origtotarget\pgfutil@empty
      \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)\relax
      \iftikz@shapeborder
        \tikz@scan@one@point\pgfutil@firstofone(\tikztotarget)\relax
        \ifdim\pgf@x>\tikz@lastx\relax
          \edef\tikztostart{\tikztostart.east}%
          \iftikz@shapeborder\def\tikz@ortho@anchor{.west}\fi
        \else
          \edef\tikztostart{\tikztostart.west}%
          \iftikz@shapeborder\def\tikz@ortho@anchor{.east}\fi
        \fi
      \fi
      \def\tikz@tempa{#1}%
      \ifx\tikz@tempa\pgfutil@empty\else
        \let\tikz@origtotarget\tikztotarget
        \edef\tikztotarget{[yshift={#1}]\tikztotarget\tikz@ortho@anchor}%
      \fi
    \endpgfextra
    [insert path/.expanded={
      (perpendicular cs: vertical line through={(\tikztostart)},
                         horizontal line through={(\tikztotarget)}) -- (\tikztotarget)}] \tikztonodes
    \pgfextra
      \ifx\tikz@origtotarget\pgfutil@empty\else
        \iftikz@ortho@preflush(\tikz@origtotarget)\fi\fi\endpgfextra}},
  only horizontal second/.default=,
  only vertical first/.style={to path={
    \pgfextra
      \let\tikz@ortho@anchor\pgfutil@empty
      \let\tikz@origtotarget\pgfutil@empty
      \tikz@scan@one@point\pgfutil@firstofone(\tikztotarget)\relax
      \iftikz@shapeborder
        \let\tikz@origtotarget\tikztotarget
        \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)\relax
        \ifdim\pgf@y>\tikz@lasty\relax
          \edef\tikztotarget{\tikztotarget.south}%
          \iftikz@shapeborder\def\tikz@ortho@anchor{.north}\fi
        \else
          \edef\tikztotarget{\tikztotarget.north}%
          \iftikz@shapeborder\def\tikz@ortho@anchor{.south}\fi
        \fi
      \fi
      \def\tikz@tempa{#1}%
      \ifx\tikz@tempa\pgfutil@empty\else
        \edef\tikztostart{[xshift={#1}]\tikztostart\tikz@ortho@anchor}%
      \fi
    \endpgfextra
    [insert path/.expanded={(\tikztostart)}] 
    -- (perpendicular cs: vertical line through/.expanded={(\tikztostart)},
                        horizontal line through={(\tikztotarget)})
    \tikztonodes
    \pgfextra
      \ifx\tikz@origtotarget\pgfutil@empty\else
        \iftikz@ortho@preflush(\tikz@origtotarget)\fi\fi\endpgfextra}},
  only vertical first/.default=,
  only horizontal first/.style={to path={
    \pgfextra
      \let\tikz@ortho@anchor\pgfutil@empty
      \let\tikz@origtotarget\pgfutil@empty
      \tikz@scan@one@point\pgfutil@firstofone(\tikztotarget)\relax
      \iftikz@shapeborder
        \let\tikz@origtotarget\tikztotarget
        \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)\relax
        \ifdim\pgf@x>\tikz@lastx\relax
          \edef\tikztotarget{\tikztotarget.west}%
          \iftikz@shapeborder\def\tikz@ortho@anchor{.east}\fi
        \else
          \edef\tikztotarget{\tikztotarget.east}%
          \iftikz@shapeborder\def\tikz@ortho@anchor{.west}\fi
        \fi
      \fi
      \def\tikz@tempa{#1}%
      \ifx\tikz@tempa\pgfutil@empty\else
        \edef\tikztostart{[yshift={#1}]\tikztostart\tikz@ortho@anchor}%
      \fi
    \endpgfextra
    [insert path/.expanded={(\tikztostart)}]
      -- (perpendicular cs: horizontal line through/.expanded={(\tikztostart)},
                              vertical line through={(\tikztotarget)})
    \tikztonodes \pgfextra
      \ifx\tikz@origtotarget\pgfutil@empty\else
        \iftikz@ortho@preflush(\tikz@origtotarget)\fi\fi\endpgfextra}},
  only horizontal first/.default=,
  only vertical first to center/.style={to path={
      \pgfextra
        \let\tikz@origtotarget\tikztotarget
      \endpgfextra
    (\tikztostart) -- (perpendicular cs: vertical line through={(\tikztostart)},
                                         horizontal line through={(\tikztotarget)})
    \tikztonodes \ifx\tikz@origtotarget\pgfutil@empty\else\iftikz@ortho@preflush(\tikz@origtotarget)\fi\fi}},
  only horizontal first to center/.style={to path={
      \pgfextra
        \let\tikz@origtotarget\tikztotarget
      \endpgfextra
    (\tikztostart) -- (perpendicular cs: horizontal line through={(\tikztostart)},
                                         vertical line through={(\tikztotarget)})
    \tikztonodes \ifx\tikz@origtotarget\pgfutil@empty\else\iftikz@ortho@preflush(\tikz@origtotarget)\fi\fi}}%
}
\endinput