\startmodule steps
\unprotect

% This module defines two macros:
% \ONLY{count}{...} which displays text on a particular page
% \STEP{count}{...} which displays text from a particular page

\def\complexslides_only#1{\csname complexslides_steps_only_#1\endcsname}
\def\complexslides_step#1{\csname complexslides_steps_step_#1\endcsname}
\def\complexslides_hide#1{\csname complexslides_steps_hide_#1\endcsname}

\def\StartStep#1#2\StopStep{\complexslides_step{#1}{#2}}
\def\StartOnly#1#2\StopOnly{\complexslides_only{#1}{#2}}
\def\StartHide#1#2\StopHide{\complexslides_hide{#1}{#2}}

\long\def\gobble#1{}
\long\def\oneofone#1{#1}
\long\def\gobblebox#1%
    {\phantom{\framed[align=normal, frame=off]{#1}}}

\long\def\displaybox#1%
    {\framed[align=normal, frame=off]{#1}}

% Allow nesting of \StartStepping ... \StopStepping

\def\StartStepping#1#2\StopStepping%
  {\dorecurse{30}{\long\setvalue{complexslides_steps_step_\recurselevel}{\gobble}}%
   \dorecurse{30}{\long\setvalue{complexslides_steps_only_\recurselevel}{\gobble}}%
   \dorecurse{30}{\long\setvalue{complexslides_steps_hide_\recurselevel}{\gobblebox}}%
   \pushmacro\ONLY
   \pushmacro\STEP
   \pushmacro\HIDE
   \def\ONLY{\complexslides_only}%
   \def\STEP{\complexslides_step}%
   \def\HIDE{\complexslides_hide}%
   \dorecurse{#1}{%
   \edef\currentStepLevel{\recurselevel}%
   \long\setvalue{complexslides_steps_step_\currentStepLevel}{\oneofone}%
   \long\setvalue{complexslides_steps_only_\currentStepLevel}{\oneofone}%
   \long\setvalue{complexslides_steps_hide_\currentStepLevel}{\displaybox}%
   #2%
   \setvalue{complexslides_steps_only_\currentStepLevel}{\gobble}%
   \page\decrementnumber[userpage]}%
   \incrementnumber[userpage]%
   \popmacro\ONLY
   \popmacro\STEP
  }

\def\doifStepelse%
    {\doifelse\currentStepLevel}

% To be used in a group
\unexpanded\def\disablesteps
    {\def\complexslides_only##1{\gobble}% ##2
     \def\complexslides_step##1{\oneofone}%##2
    }

\unexpanded\def\SetStep#1#2% i of n
    {\dorecurse{#2}{\long\setvalue{complexslides_steps_step_\recurselevel}{\gobble}}%
     \dorecurse{#2}{\long\setvalue{complexslides_steps_only_\recurselevel}{\gobble}}%
     \dorecurse{#2}{\long\setvalue{complexslides_steps_hide_\recurselevel}{\gobblebox}}%
     \edef\currentStepLevel{#1}%
     \long\setvalue{complexslides_steps_step_\currentStepLevel}{\oneofone}%
     \long\setvalue{complexslides_steps_only_\currentStepLevel}{\oneofone}%
     \long\setvalue{complexslides_steps_hide_\currentStepLevel}{\displaybox}%
    }

\def\hidecomponent#1
    {\let\StartS\StartStepping
     \let\StopS\StopStepping
     \def\StartStepping{\grabbufferdata[Stepping][StartStepping][StopStepping]}
     \component[#1]
     \let\StartStepping\StartS
     \let\StopStepping\StopS}


\protect
\stopmodule
