% \iffalse meta-comment
%%% File: algo-switch.sty
%% Copyright (C) 2017 Michael N\"usken.
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3a
% of this license or (at your option) any later version.
% The latest version of this license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.2 or later is part of all distributions of LaTeX 
% version 1999/12/01 or later.
%
% -----------------------------------------------------------
% 
%<*chartab>
%\fi
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
% \iffalse meta-comment
%</chartab>
% \fi
% \iffalse
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{algo-switch}[2017/04/21 v0.94 %
Switches for algorithms. MNuesken]
% \fi
%
% \CheckSum{0}
%
%
%
%  \GetFileInfo{algo-switch.sty}
%  \CodelinePrefix{A}
%
% \section{Source of \filename (\fileprefix)}
%
%  This file (\file{\filename}) has version number \fileversion, last
%  revised \filedate.
%    \begin{macrocode}
%<*algoswitch>
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareOption{override-cc}{%
}
\ProcessOptions
%    \end{macrocode}

%%% \forgetitem drops an already prepared label w/o resetting an
%%% associated, already stepped counter.
\def\forgetitem{%
  \global\setbox\@labels\copy\voidb@x
  \global\@inlabelfalse}


%%% Special command to conditionally display stuff or forget the item
%%% just issued.
\newcommand{\echoifnonempty}[1]{%
  \setbox0\hbox{%
    \begin{minipage}[t]{\linewidth}%
      #1
    \end{minipage}%
  }%
  %%% \@ifempty{#1}%
  %%% {\forgetitem}%
  %%% {\next{\box0}}%
  \ifdim\ht0<2sp\expandafter\forgetitem
  \else
  %%% \fboxsep-\fboxrule
  %%% \show\next\show\colorbox\show\color@box\show\color@b@x
  %%% \showthe\fboxsep
  \leavevmode\kern-\fboxsep\kern-\fboxrule\next{\box0}\kern-\fboxsep\kern-\fboxrule\fi
}

%%% These commands serve to repeat certain stuff.  Depending on the
%%% desires of the caller this stuff is or is not be displayed.
\def\lastRecall{}
\def\Recall#1{\gdef\lastRecall{#1}\lastRecall}
\def\RECALL#1{\forgetitem}

\AtBeginDocument{% Just make the gamehop-mark command no-ops if that
  % package is not loaded...
  %
  \@ifundefined{ifmarked}{%
    %%% \providemarkifs
    \expandafter\newif\csname ifmarked\endcsname
    \expandafter\newif\csname ifmarkedred\endcsname
    \expandafter\newif\csname ifmarkednew\endcsname
    \let\thismark\relax
    %%% 
  }{}%
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Commands for the display of modifications: switch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newdimen\switchdimen
\let\switchrule\vrule
\newcommand{\orignoswitch}[1]{#1}
\newcommand{\origswitch}[3]{%
  %%% Mind that \wd\hbox{~~~~~} should be about 0.04\linewidth
  \fboxsep0pt
  \vskip0.1\baselineskip
  \switchrule~%
  \begin{minipage}[t]{0.48\linewidth}
    \leavevmode\color{darkgreen}#1\strut
  \end{minipage}%
  \setbox0\hbox{%
    \begin{minipage}[t]{0.48\linewidth}
      \leavevmode\color{blue}#3\strut
    \end{minipage}%
  }%
  \switchdimen.7\baselineskip\ifdim\switchdimen<\ht0\switchdimen\ht0\fi
  ~~~\switchrule\raise\switchdimen\hbox{\textsuperscript{\rlap{#2}}}~%
  \let\next\relax
  \ifmarkednew\xdef\next{\noexpand\colorbox{\thisnewcolor}}\fi
  \next{\box0}%
}%
\newcommand{\origswitchc}[5]{%
  %%% Mind that \wd\hbox{~~~~~} should be about 0.04\linewidth
  %%% \markoff\MARKOFF
  \fboxsep0pt
  \vskip0.1\baselineskip
  \switchrule~%
  \begin{minipage}[t]{0.32\linewidth}\raggedright
    \leavevmode\color{darkgreen}#1\strut
  \end{minipage}%
  \setbox0\hbox{%
    \begin{minipage}[t]{0.32\linewidth}\raggedright
      \leavevmode\color{darkred}#3\strut
    \end{minipage}%
  }%
  \switchdimen.7\baselineskip\ifdim\switchdimen<\ht0\switchdimen\ht0\fi
  ~\switchrule\raise\switchdimen\hbox{\textsuperscript{\rlap{#2}}}~%
  \let\next\relax
  \ifmarkedred\edef\next{\noexpand\colorbox{\thisredcolor}}\fi
  \next{\box0}%
  \setbox0\hbox{%
    \begin{minipage}[t]{0.32\linewidth}\raggedright
      \leavevmode\color{blue}#5\strut
    \end{minipage}%
  }%
  \switchdimen.7\baselineskip\ifdim\switchdimen<\ht0\switchdimen\ht0\fi
  ~\switchrule\raise\switchdimen\hbox{\textsuperscript{\rlap{#4}}}~%
  \let\next\relax
  \ifmarkednew\edef\next{\noexpand\colorbox{\thisnewcolor}}\fi
  \next{\box0}%
}%
\newcommand{\origswitchd}[6][]{%
  %%% Mind that \wd\hbox{~~~~~} should be about 0.04\linewidth
  %%% \markoff\MARKOFF
  \fboxsep0pt
  \vskip0.1\baselineskip
  \setbox0\hbox{%
    \begin{minipage}[t]{0.32\linewidth}\raggedright
      \leavevmode\color{darkgreen}#2\strut
    \end{minipage}%
  }%
  \switchdimen.7\baselineskip\ifdim\switchdimen<\ht0\switchdimen\ht0\fi
  \switchrule\raise\switchdimen\hbox{\textsuperscript{\rlap{#1}}}~%
  \let\next\relax
  \ifmarkedred\edef\next{\noexpand\colorbox{\thisredcolor}}\fi
  \next{\box0}%
  \setbox0\hbox{%
    \begin{minipage}[t]{0.32\linewidth}\raggedright
      \leavevmode\color{darkred}#4\strut
    \end{minipage}%
  }%
  \switchdimen.7\baselineskip\ifdim\switchdimen<\ht0\switchdimen\ht0\fi
  ~\switchrule\raise\switchdimen\hbox{\textsuperscript{\rlap{#3}}}~%
  \let\next\relax
  \ifmarkedred\edef\next{\noexpand\colorbox{\thisredcolor}}\fi
  \next{\box0}%
  \setbox0\hbox{%
    \begin{minipage}[t]{0.32\linewidth}\raggedright
      \leavevmode\color{blue}#6\strut
    \end{minipage}%
  }%
  \switchdimen.7\baselineskip\ifdim\switchdimen<\ht0\switchdimen\ht0\fi
  ~\switchrule\raise\switchdimen\hbox{\textsuperscript{\rlap{#5}}}~%
  \let\next\relax
  \ifmarkednew\edef\next{\noexpand\colorbox{\thisnewcolor}}\fi
  \next{\box0}%
}%
\let\noswitch\orignoswitch
\let\switch\origswitch
\let\switchc\origswitchc
\let\switchd\origswitchd
%%%
\newcommand{\leftnoswitch}[1]{}
\newcommand{\leftswitch}[3]{\csname @ifempty\endcsname{#1}{\forgetitem}{{#1}}}
\newcommand{\leftswitchc}[5]{\csname @ifempty\endcsname{#1}{\forgetitem}{{#1}}}
\newcommand{\leftswitchd}[6][]{\csname @ifempty\endcsname{#2}{\forgetitem}{{#2}}}
\newcommand{\gamesetleft}{%
  \def\Recall##1{\gdef\lastRecall{##1}\forgetitem}%
  \let\noswitch\leftnoswitch
  \let\switch\leftswitch
  \let\switchc\leftswitchc
  \let\switchd\leftswitchd
}
%%% 
\newcommand{\rightnoswitch}[1]{#1}
\newcommand{\rightswitch}[3]{\csname @ifempty\endcsname{#3}{\forgetitem}{{#3}}}
\newcommand{\rightswitchc}[5]{\csname @ifempty\endcsname{#5}{\forgetitem}{{#5}}}
\newcommand{\rightswitchd}[6][]{\csname @ifempty\endcsname{#6}{\forgetitem}{{#6}}}
\newcommand{\gamesetright}{%
  \def\Recall##1{\gdef\lastRecall{##1}\forgetitem}%
  \let\noswitch\rightnoswitch
  \let\switch\rightswitch
  \let\switchc\rightswitchc
  \let\switchd\rightswitchd
}
%%% 
\newcommand{\markednoswitch}[1]{\ifmarked#1\fi}
\newcommand{\markedswitch}[3]{%
  {\def\Recall##1{\gdef\lastRecall{##1}\lastRecall}\def\RECALL##1{\lastRecall}%
    \setbox0\hbox{#1}%
  }%
  \xdef\next{\noexpand\colorbox{white}}%
  %%% \ifmarked
    \ifmarkednew
      \xdef\next{\noexpand\colorbox{\thisnewcolor}}%
      \echoifnonempty{#3}% new marked
    \else
      \echoifnonempty{#1}% nothing marked
    \fi
  %%% \else
  %%%   \echoifnonempty{#1}%
  %%% \fi
}%
\newcommand{\markedswitchc}[5]{%
  {\def\Recall##1{\gdef\lastRecall{##1}\lastRecall}\def\RECALL##1{\lastRecall}%
    \setbox0\hbox{#1}%
  }%
  \xdef\next{\noexpand\colorbox{white}}%
  %%% \ifmarked
    \ifmarkedred
      \ifmarkednew
      \origswitchc{#1}{#2}{#3}{#4}{#5}% new & red marked
      \else
      \xdef\next{\noexpand\colorbox{\thisredcolor}}%
      \echoifnonempty{#3}% red marked
      \fi
    \else
      \ifmarkednew
      \xdef\next{\noexpand\colorbox{\thisnewcolor}}%
      \echoifnonempty{#5}% new marked
      \else
      \echoifnonempty{#1}% nothing marked
      \fi
    \fi
  %%% \else
  %%%   \echoifnonempty{#1}%
  %%% \fi%
}%
\newcommand{\gamesetmarked}{%
  \def\Recall##1{\gdef\lastRecall{##1}\forgetitem}%
  \let\noswitch\markednoswitch
  \let\switch\markedswitch
  \let\switchc\markedswitchc
  \let\switchd\leftswitchd%%% not used so far...
}
%%% 
\newcommand{\markedfullnoswitch}[1]{\ifmarked#1\fi}
\newcommand{\markedfullswitch}[3]{%
  {\def\Recall##1{\gdef\lastRecall{##1}\lastRecall}\def\RECALL##1{\lastRecall}%
    \setbox0\hbox{#1}%
  }%
  %%% \global\markswafalse\global\markswbfalse\setbox0\hbox{#2}%
  \xdef\next{\noexpand\colorbox{white}}%
  \ifmarkednew
    \xdef\next{\noexpand\colorbox{\thisnewcolor}}%
    \origswitch{#1}{#2}{#3}%
  \else
    \echoifnonempty{#1}%
  \fi
}%
\newcommand{\markedfullswitchc}[5]{%
  {\def\Recall##1{\gdef\lastRecall{##1}\lastRecall}\def\RECALL##1{\lastRecall}%
    \setbox0\hbox{#1}%
    %%% \show\lastRecall
  }%
  \xdef\next{\noexpand\colorbox{white}}%
  \markedfalse
  \ifmarkednew\markedtrue\fi
  \ifmarkedred\markedtrue\fi
  \ifmarked
  \origswitchc{#1}{#2}{#3}{#4}{#5}%
  %%% \gdef\next{\colorbox[red!20]}%
  %%% \echoifnonempty{#3}%
  \else
  \echoifnonempty{#1}%
  \fi%
}%
\newcommand{\gamesetmarkedfull}{%
  \gdef\lastRecall{}%
  \let\noswitch\markedfullnoswitch
  \let\switch\markedfullswitch
  \let\switchc\markedfullswitchc
  \let\switchd\leftswitchd%%% not used so far...
}

% \begin{macrocode}
%</algoswitch>
% \end{macrocode}