% output=pdf
%=======================================================================
% begin of file *t-algorithmic.tex*
%=======================================================================
% richard@mevis.de (c) 2003, 2004, 2005

%D \module
%D   [       file=algorithmic.tex,
%D        version=2005.02.12,
%D          title=Environments for algorithms,
%D         author=richard rascher friesenhausen,
%D           date=\currentdate,
%D      copyright={richard@home}]

%D algorithmic defs
\definefloat[algorithm][algorithms]
\setupfloat[algorithm][location=middle]
\setuplabeltext[de][algorithm={Algorithmus\,}]

\define[1]\Algorithm{%
\startframedtext[frame=off,%leftframe=on,%bottomframe=on,
                 framecolor=darkyellow,
                 background=screen,offset=none]
%  \setuplinenumbering[location=intext]
%  \startlinenumbering 
    #1 
%  \stoplinenumbering
\stopframedtext
}

%D names
\def\algorithmicend{{\bf end}}
\def\algorithmicif{{\bf if}}
\def\algorithmicthen{{\bf then}}
\def\algorithmicelse{{\bf else}}
\def\algorithmicelseif{\algorithmicelse\algorithmicif}
\def\algorithmicendif{\algorithmicend{\bf.}\algorithmicif}

\def\algorithmicfor{{\bf for}}
\def\algorithmicendfor{\algorithmicend{\bf.}\algorithmicfor}
\def\algorithmicforall{{\bf for all}}
\def\algorithmicdo{{\bf do}}

\def\algorithmicwhile{{\bf while}}
\def\algorithmicendwhile{\algorithmicend{\bf.}\algorithmicwhile}

\def\algorithmicrepeat{{\bf repeat}}
\def\algorithmicuntil{{\bf until}}

%D macros
\newdimen\algorithmicindent \algorithmicindent=0cm
\newdimen\algorithmicskip   \algorithmicskip=.5cm
\newcount\algorithmicline   \algorithmicline=0
\def\startIF{\advance\algorithmicindent by  \algorithmicskip}
\def\stopIF {\advance\algorithmicindent by -\algorithmicskip}

\let\startFOR=\startIF
\let\stopFOR =\stopIF

\let\startWHILE=\startIF
\let\stopWHILE =\stopIF

\let\startREPEAT=\startIF
\let\stopREPEAT =\stopIF

\def\algorithmiclineintro{%
  \inframed[width=.5cm,frame=off,offset=none,
            background=color,backgroundcolor=darkyellow]
    {\strut\hfill\color[black]{\the\algorithmicline}\,}\quad
  \hskip\algorithmicindent%
}
%D if then elseif else endif
\def\IF#1{%
  \advance\algorithmicline by 1
  \hbox{%
    \algorithmiclineintro
    \algorithmicif\ #1\ \algorithmicthen
  }\par
  \startIF
}
\def\ENDIF{%
  \stopIF
  \advance\algorithmicline by 1
  \hbox{%
    \algorithmiclineintro
%  \the\algorithmicline\ \hskip\algorithmicindent%
    \algorithmicendif
  }\par
}
\def\ELSE{%
  \stopIF
  \advance\algorithmicline by 1
  \hbox{%
    \algorithmiclineintro
%  \the\algorithmicline\ \hskip\algorithmicindent%
    \algorithmicelse
  }\par
  \startIF
}

\def\ELSEIF#1{%
  \stopIF
  \advance\algorithmicline by 1
  \hbox{%
    \algorithmiclineintro  
    \algorithmicelseif\ #1\ \algorithmicthen
  }\par
  \startIF
}

%D for forall do endfor
\def\FOR#1{%
  \advance\algorithmicline by 1
  \hbox{%
    \algorithmiclineintro
    \algorithmicfor\ #1\ \algorithmicdo
  }\par
  \startFOR
}
\def\FORALL#1{%
  \advance\algorithmicline by 1
  \hbox{%
    \algorithmiclineintro
    \algorithmicforall\ #1\ \algorithmicdo
  }\par
  \startFOR
}
\def\ENDFOR{%
  \stopFOR
  \advance\algorithmicline by 1
  \hbox{%
    \algorithmiclineintro
    \algorithmicendfor
  }\par
}

%D while do endwhile
\def\WHILE#1{%
  \advance\algorithmicline by 1
  \the\algorithmicline\ \hskip\algorithmicindent%
  \algorithmicwhile\ #1\ \algorithmicdo\par
  \startWHILE
}
\def\ENDWHILE{%
  \stopWHILE
  \advance\algorithmicline by 1
  \the\algorithmicline\ \hskip\algorithmicindent%
  \algorithmicendwhile\par
}

%D repeat until
\def\REPEAT{%
  \advance\algorithmicline by 1
  \hskip\algorithmicindent%
  \algorithmicrepeat\par
  \startREPEAT
}
\def\UNTIL#1{%
  \stopREPEAT
  \advance\algorithmicline by 1
  \the\algorithmicline\ \hskip\algorithmicindent%
  \algorithmicuntil\ #1\par
}

%D loop endloop

%D others
\def\STATE#1{%
  \advance\algorithmicline by 1
  \hbox{%
    \algorithmiclineintro
    #1
  }\par
}
  

\def\INPUT#1{%
  \hskip\algorithmicindent%
  \inframed[frame=off,width=broad,offset=1pt, %bottomframe=on,
            background=color,backgroundcolor=darkyellow]%
    {{\bf Input:}\ #1\hfill}\par}
\def\OUTPUT#1{%
  \hskip\algorithmicindent%
  \inframed[frame=off,width=broad,offset=1pt,%topframe=on,
            background=color,backgroundcolor=darkyellow]%
    {{\bf Output:}\ #1\hfill}\par}

\def\REQUIRE#1{%
  \hskip\algorithmicindent%
  \inframed[frame=off,width=broad,strut=yes,offset=0pt,
            background=color,backgroundcolor=darkyellow]
    {{\bf Require:}\ #1\hfill}\par
}
\def\ENSURE#1{%
  \hskip\algorithmicindent%
  \inframed[frame=off,width=broad,offset=1pt,
            background=color,backgroundcolor=darkyellow]%
    {{\bf Ensure:}\ #1\hfill}\par
}
\def\INVARIANT#1{%
  \hskip\algorithmicindent%
  \inframed[frame=off,width=broad,offset=none,
            background=color,backgroundcolor=darkyellow]%
    {{\bf Invariant:}\ #1\hfill}\par
}

\doifnotmode{demo}{\endinput}
%C======================================================================
%D
%D Some tests.
%D
%=======================================================================
\setupcolors[state=start]
%=======================================================================
\starttext
%=======================================================================
\startbuffer
\IF{$x=0$}
  \STATE{do it}
\ELSE
  \STATE{don't do it}
\ENDIF
\stopbuffer
\placealgorithm{ein Algorithmus}\Algorithm{\getbuffer}

\startbuffer
\FORALL{$x\in R$}
  \STATE{do it}
\ENDFOR
\stopbuffer
\placealgorithm{ein Algorithmus}\Algorithm{\getbuffer}

\startbuffer
  \INPUT{$n$ ganze Zahlen}
  \REQUIRE{$n>0$}
  \STATE{Lese $n$ Zahlen in das Feld $a_0,\ldots,a_{n-1}$ ein.}
  \FORALL{$i=0,1,\ldots,n-1$}
    \STATE{Suche das kleinste Element aus $a_i, \ldots, a_{n-1}$.}
    \STATE{Nehme an, es steht in $a_{\min}$.}
    \STATE{Vertausche $a_i$ mit $a_{\min}$.}
  \ENDFOR 
  \ENSURE{$a_0\le a_1\le \ldots\le a_{n-1}$}
  \OUTPUT{Sortierte Liste $a$ von ganzen Zahlen}
\stopbuffer
\placealgorithm{ein Algorithmus}{\setupbodyfont[10pt]\Algorithm{\getbuffer}}

\startbuffer
\IF{$i>0$}
  \STATE{$i$ ist gr\"osser als $0$}
  \FOR{$j=0,1,2$}
    \STATE{z\"ahlen...}
  \ENDFOR
\ELSEIF{$i=0$}
  \STATE{$i$ ist gleich $0$}
\ELSE
  \STATE{mmmhhh...}
\ENDIF
\stopbuffer
\placealgorithm{ein Algorithmus}\Algorithm{\getbuffer}

%=======================================================================
\stoptext
%=======================================================================
%=======================================================================
% end of file *t-algorithmic.tex*
%=======================================================================
