%D \module
%D   [      file=t-rsteps,
%D        version=2004.11.10,
%D          title=\CONTEXT\ User Module,
%D       subtitle=Raw steps for slides,
%D         author=David Munger,
%D           date=\currentdate,
%D      copyright={David Munger}]

%D Thanks to Otared Kavian whose work inspired this module,
%D and who contributed to its development.

%%D Changed \Subject to \Title (OK, December 13, 2004)

\def\Title[#1]{\start%
\page\switchtobodyfont[palatino,24pt]\centerline{#1}\bigskip\stop }

\def\Step#1#2{\FromStep[#1]{#2}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% steps for slides
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\unprotect

\newif\ifUseSteps
\UseStepstrue
% \UseStepsfalse

\definecolor [highlightcolor] [blue]

\long\def\StartSteps[#1]#2\StopSteps{%
  \begingroup%
  \let\steps@number\plusone%
  \let\steps@counter\plusone%
  \ifUseSteps%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Workaround to make formula numbers "stick"
    \def\PlaceFormula[##1]{%
      \incrementnumber[formula]%
      \expandafter\xdef\csname steps@eqnum##1\endcsname{\rawnumber[formula]}
      \expandafter\xdef\csname steps@eqnum\endcsname{\rawnumber[formula]}
      \dodoubleempty\doplaceformula}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \loop%
      \let\steps@autocounter\zerocount%
      \Title[#1]\par\steps@startstep#2\steps@stopstep%
      \steps@updatenumber\steps@autocounter%
    \ifnum\steps@counter<\steps@number%
      \increment\steps@counter%
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %% Workaround to make formula numbers "stick"
      \def\PlaceFormula[####1]{%
	\setnumber[formula]{\csname steps@eqnum####1\endcsname}%
	\setfalse\incrementformulanumber%
	\ifnum\steps@counter=\steps@number%
	  \def\steps@fnext{\dodoubleempty\doplaceformula[####1]}%
	\else
	  \def\steps@fnext{\dodoubleempty\doplaceformula}%
	\fi\steps@fnext}
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \repeat%
  \else%
    \Title[#1]\par\steps@startstep#2\steps@stopstep%
  \fi%
  \endgroup}

\def\NextStep{\steps@stopstep\steps@startstep}

\def\OnlyStep[#1]{%
  \relax
  \steps@updatenumber#1%
  \def\steps@cond{\steps@counter=#1}%
  \dosingleempty\steps@dostep}

\def\FromStep[#1]{%
  \relax
  \steps@updatenumber#1%
  \let\steps@tmpcounter\zerocount%
  \increment(\steps@tmpcounter,#1-1)%
  \def\steps@cond{\steps@counter>\steps@tmpcounter}%
  \dosingleempty\steps@dostep}

\def\AfterStep[#1]{%
  \relax
  \steps@updatenumber#1%
  \def\steps@cond{\steps@counter>#1}%
  \dosingleempty\steps@dostep}

\def\UntilStep[#1]{%
  \relax
  \steps@updatenumber#1%
  \def\steps@cond{\steps@counter<#1}%
  \dosingleempty\steps@dostep}

\def\HighlightStep[#1]#2{%
  {\UntilStep[#1]{#2}\OnlyStep[#1]{\highlightcolor #2}\AfterStep[#1]{#2}}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% internal macros
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcounter\steps@counter
\newcounter\steps@autocounter
\newcounter\steps@number
\newcounter\steps@tmpcounter

\def\steps@startstep{%
  \increment\steps@autocounter%
  \def\steps@hide{no}%
  \ifUseSteps%
    \ifnum\steps@autocounter>\steps@counter%
      \def\steps@hide{yes}%
    \fi%
  \fi%
  \starthidden}

\def\steps@stopstep{\stophidden}

\def\steps@updatenumber#1{\ifnum\steps@number<#1%
  \doglobal\let\steps@number\zerocount%
  \doglobal\increment(\steps@number,#1)\fi}

\defineframedtext
  [step@frame]
  [offset=overlay,width=broad,height=fit,frame=off]

\def\starthidden{\startstep@frame[empty=\steps@hide]}
\def\stophidden{\stopstep@frame}


\long\def\steps@dostep[#1]#2{%
  \processallactionsinset
    [#1]
    [	reserve=>\let\steps@next=\steps@dostepreserve,
	\s!default=>\let\steps@next=\steps@dostepnoreserve]
  \steps@next{#2}}

\long\def\steps@dostepreserve#1{%
  \def\steps@hide{no}%
  \ifUseSteps%
    \ifnum\steps@cond\else%
      \def\steps@hide{yes}%
    \fi%
  \fi%
  \ifmmode\expandafter\mframed\else\expandafter\framed\fi%
  [empty=\steps@hide,frame=off,offset=overlay]{#1}%
  \ifmmode\;\fi}

\long\def\steps@dostepnoreserve#1{%
  \def\steps@next{#1\ifmmode\;\fi}%
  \ifUseSteps%
    \ifnum\steps@cond\else\def\steps@next{\relax}\fi%
  \fi\steps@next}

\protect

\doifnotmode{demo}{\endinput}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% usage example
% partly contributed by: Otared Kavian
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usemodule [pre-original]
%\usemodule [rsteps]

\starttext
\StartSteps[Slide Title]           % the title is passed to
                                   % the \Title macro

\startitemize
\item Item 1
\OnlyStep[2]  {\item (oops!)}
\UntilStep[4] {\item Item 2*}
\FromStep[4]  {\item Item 2}
\HighlightStep[3]{\item Here is another version of Navier-Stokes equation}
\stopitemize

\StopSteps

\StartSteps[Navier||Stokes equation]

\placeformula[eq:incompressibility]
\startformula
  \vec\nabla\cdot\vec u = 0
\stopformula

\placeformula[eq:NS]
\startformula
  \frac{D\vec u}{Dt} =
    \FromStep[2][reserve]{-{\red \frac1\rho \vec\nabla p}}
    \FromStep[3][reserve]{+\;{\green \vec g}}
    \FromStep[4][reserve]{+\;{\blue \frac1\rho \vec\nabla\cdot S}}
\stopformula

Momentum transport:     \NextStep
\item {\red   Pressure gradient}        \par\NextStep
\item {\green Gravity}                  \par\NextStep
\item {\blue  Stress (viscous, turbulent, Maxwell)}

\StopSteps

\StartSteps[Navier Stokes equation]


\placeformula[eq:div-nul]
\startformula
   \vec\nabla\cdot\vec u = 0
\stopformula

\placeformula[eq:NStokes]
\startformula
   { \partial u \over \partial t} - \Delta u
     \FromStep[1][reserve]{{\red  \; +\; (u\cdot \nabla)u}}
     \FromStep[2][reserve]{{\green \; +\;  \nabla p}}
     \FromStep[3][reserve]{{\blue \; =\;  f(t,x)}}
\stopformula

Comments:
\NextStep \item Nonlinear equation (same is true for equation
(\in[eq:NS]) \par\NextStep
\item One should add initial conditions \par\NextStep
\item and boundary conditions

\StopSteps

\stoptext