%%
%% This is file `stepslid.sty',
%% 
%% by Behdad Esfahbod <stepslid@behdad.org>, 2002
%%
%% This file is not copytighted -- public domain
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{stepslid}%
         [2002/10/20 Step Overlay Slide Support]

\RequirePackage{ifthen}
\RequirePackage{color}

\AtBeginDocument{%
	\global\let\old@slide\slide
	\global\let\old@endslide\endslide
	\global\let\old@overlay\overlay
	\global\let\old@endoverlay\endoverlay
}


\def\@getcolorhooks{%
	\global\let\old@color\color
	\global\let\old@textcolor\textcolor
	\global\let\old@pagecolor\pagecolor
	\global\let\old@colorbox\colorbox
	\global\let\old@fcolorbox\fcolorbox
}
\def\@putcolorhooks{%
	\let\color\old@color
	\let\textcolor\old@textcolor
	\let\colorbox\old@colorbox
	\let\fcolorbox\old@fcolorbox
}
\def\@setcolorhooks{%
	\let\color\fake@color
	\let\textcolor\fake@textcolor
	\let\colorbox\fake@colorbox
	\let\fcolorbox\fake@fcolorbox
}

\@getcolorhooks

\newcommand{\fake@color}[2][]{%
	\let\color@hook\old@color\set@pagecol
}
\newcommand{\fake@textcolor}[2][]{%
	\let\color@hook\old@textcolor\set@pagecol%
}
\newcommand{\fake@colorbox}[3][]{%
	\let\color@hook\old@colorbox\set@pagecol{%
		\let\color@hook\old@color\set@pagecol#3%
	}%
}
\newcommand{\fake@fcolorbox}[4][]{%
	\let\color@hook\old@fcolorbox
	\edef\@x{\noexpand\color@hook\page@color@i{\page@color@ii}{\page@color@ii}}%
        \@x{\let\color@hook\old@color\set@pagecol#4}%
}
\def\color#1#{%
	\new@col@r{#1}%
}
\def\new@col@r#1#2{%
	\def\text@color@i{#1}%
	\def\text@color@ii{#2}%
	\let\color@hook\old@color\set@textcol
}
\def\pagecolor#1#{%
	\new@pagecol@r{#1}%
}
\def\new@pagecol@r#1#2{%
	\gdef\page@color@i{#1}%
	\gdef\page@color@ii{#2}%
	\let\color@hook\old@pagecolor\set@pagecol
}
\def\page@color@i{}
\def\page@color@ii{white}

\def\make@invisible{%
	\setboolean{@invisible}{true}%
	\@setcolorhooks
	\let\color@hook\old@color\set@pagecol
}
\def\make@visible{%
	\setboolean{@invisible}{false}%
	\@putcolorhooks
	\let\color@hook\old@color\set@textcol
}
\def\set@pagecol{%
	\edef\@x{\noexpand\color@hook\page@color@i{\page@color@ii}}%
	\@x
}
\def\set@textcol{%
	\edef\@x{\noexpand\color@hook\text@color@i{\text@color@ii}}%
	\@x
}

\newcounter{@step}
\newcounter{@min@step@temp}
\newcounter{@turn}
\newcounter{@notyet}
\newboolean{@invisible}\setboolean{@invisible}{true}

\long\def\step@slide#1{%
	\setcounter{@step}{0}%
	\setcounter{@turn}{1}%
	\setcounter{@notyet}{1}%
	\whiledo{\value{@notyet}>0}{%
		\begingroup
		\setcounter{@notyet}{0}%
		\setcounter{@turn}{0}%
		\new@slide
		#1%
		\new@endslide
		\global\let\new@slide\old@overlay
		\global\let\new@endslide\old@endoverlay
		\stepcounter{@step}%
		\global\let\c@scade\relax
		\endgroup
	}%
}

\long\def\stepslide{%
	\global\let\new@slide\old@slide
	\global\let\new@endslide\old@endslide
	\step@slide
}

\long\def\stepoverlay{%
	\global\let\new@slide\old@overlay
	\global\let\new@endslide\old@endoverlay
	\step@slide
}

\def\step@start{%
	\penalty0%
}


\newcommand{\step}{%
	\step@start
	\ifthenelse{\not\value{@turn}<\value{@step}}{\stepcounter{@notyet}}{}%
	\ifthenelse{\value{@turn}=\value{@step}}{%
		\gdef\c@scade{\make@invisible\aftergroup\c@scade}%
		\c@scade
	}{}%
	\stepcounter{@turn}%
}

\newcommand{\atstep}[2]{%
	\begingroup
	\step@start
	\setboolean{@fillit}{true}%
	\@atstep{#1}{#2}
	\endgroup
}
\newcommand{\@atstep}[2]{%
	\ifthenelse{\value{@step}<#1}{\stepcounter{@notyet}}{}%
	\ifthenelse{\value{@step}=#1}%
	{\make@visible#2\setboolean{@wasvis}{true}}%
	{\ifthenelse{\boolean{@fillit}}{\make@invisible#2}{}}%
}
\newcommand{\@atsteprange}[3]{%
	\ifthenelse{\value{@step}<#2}{\stepcounter{@notyet}}{}%
	\ifthenelse{\not\(\value{@step}<#1\or\value{@step}>#2\)}%
	{\make@visible#3\setboolean{@wasvis}{true}}%
	{\ifthenelse{\boolean{@fillit}}{\make@invisible#3}{}}%
}

\newboolean{@wasvis}
\newboolean{@fillit}
\newcommand{\atsteps}[2]{%
	\begingroup
	\step@start
	\setboolean{@wasvis}{false}%
	\setboolean{@fillit}{true}%
	\@atstepsparsecomma#1,S@{#2}\relax%
	\ifthenelse{\boolean{@wasvis}}{}{\make@invisible#2}%
	\endgroup
}
\def\@atstepsparsecomma#1,#2@#3\relax{%
	\ifthenelse{\equal{#1}{S}\or\boolean{@wasvis}}{}{%
		\@atstepsparsedash#1-S-S@{#3}\relax
		\@atstepsparsecomma#2,S@{#3}\relax
	}%
}
\def\@atstepsparsedash#1-#2-#3@#4\relax{%
	\ifthenelse{\equal{#2}{S}}{%
		\@atstep{#1}{#4}%
	}{%
		\ifthenelse{\equal{#2}{}}{%
			\@atstep{#1}{#4}%
			\@ifstep{>#1}{#4}%	
		}{%
			\@atsteprange{#1}{#2}{#4}%
		}%
	}%
}

\newcommand{\ifstep}[2]{%
	\begingroup
	\step@start
	\setboolean{@fillit}{true}%
	\@ifstep{#1}{#2}%
	\endgroup
}
\newcommand{\@ifstep}[2]{%
	\extract@min@step@num#1\x@x
	\ifthenelse{\value{@step}<\value{@min@step@temp}}{\stepcounter{@notyet}}{}%
	\ifthenelse{\value{@step}#1}%
	{\make@visible#2\setboolean{@wasvis}{true}}%
	{\ifthenelse{\boolean{@fillit}}{\make@invisible#2}{}}%
}
\def\extract@min@step@num#1#2\x@x{%
	\setcounter{@min@step@temp}{#2}%
	\ifthenelse{\equal{#1}{>}}{\stepcounter{@min@step@temp}}{}
}

\long\gdef\xstep@slide#1\endslide{\stepslide{#1}}
\long\gdef\xstep@overlay#1\endoverlay{\stepoverlay{#1}}

\AtBeginDocument{%
	\gdef\slide{%
		\gdef\new@slide{\old@slide}%
		\gdef\new@endslide{\old@endslide}%
		\xstep@slide}%
	\gdef\endslide{}%
	\gdef\overlay{%
		\global\let\new@slide\old@overlay
		\global\let\new@endslide\old@endoverlay
		\xstep@overlay}%
	\gdef\endoverlay{}%
}

%%
%% End of file `stepslid.sty'.
