%D \module
%D   [       file=t-algorithmic,
%D        version=2009.04.21,
%D          title=\CONTEXT\ User Module,
%D       subtitle=Algorithms,
%D         author=Wolfgang Schuster,
%D           date=\currentdate,
%D      copyright=Wolfgang Schuster,
%D          email=schuster.wolfgang@googlemail.com,
%D        license=Public Domain]

\writestatus{loading}{Context User Module / Algorithms}

\unprotect

\def\????al{@@@@al} % ALgorithmic

\def\startalgorithmic
  {\bgroup
   \dosingleempty\dostartalgorithmic}

\def\dostartalgorithmic[#1]%
  {\iffirstargument
     \setupalgorithmic[#1]%
   \fi
   \def\algorithmiccommands##1%
     {\uppercase{\expandafter\let\csname##1\expandafter\endcsname}\csname algorithmic!##1\endcsname}%
   \processcommacommand[\algorithmic!list!commands]\algorithmiccommands
   \@@@@albefore
   \setupwhitespace[\v!none]%
   \doadaptleftskip\@@@@almargin
   \doif\@@@@alnumbering\v!line
     {\setuplinenumbering
        [\c!start=\@@@@alstart,
         \c!stop=\@@@@alstop,
         \c!step=\@@@@alstep]%
      \startlinenumbering}}

\def\stopalgorithmic
  {\par
   \doif\@@@@alnumbering\v!line\stoplinenumbering
   \@@@@alafter
   \egroup}

\def\setupalgorithmic
  {\dodoubleargument\getparameters[\????al]}

% Commands

\def\algorithmic!list!commands
  {state,if,else,elsif,endif,for,forall,endfor,while,endwhile,repeat,%
   until,loop,endloop,require,ensure,return,true,false,print,comment}

\def\startsubalgorithmic
  {\par\doadaptleftskip\@@@@alwidth}

\def\stopsubalgorithmic
  {\par\doadaptleftskip{-\@@@@alwidth}}

\def\algorithmic!state   {\par}
\def\algorithmic!if    #1{\par\algorithmicif\ #1 \algorithmicthen\startsubalgorithmic}
\def\algorithmic!else    {\stopsubalgorithmic\algorithmicelse\startsubalgorithmic}
\def\algorithmic!elsif #1{\stopsubalgorithmic\algorithmicelsif\ #1 \algorithmicthen\startsubalgorithmic}
\def\algorithmic!endif   {\stopsubalgorithmic\algorithmicendif}
\def\algorithmic!for   #1{\par\algorithmicfor\ #1 \algorithmicdo\startsubalgorithmic}
\def\algorithmic!forall#1{\par\algorithmicforall\ #1 \algorithmicdo\startsubalgorithmic}
\def\algorithmic!endfor  {\stopsubalgorithmic\algorithmicendfor}
\def\algorithmic!while #1{\par\algorithmicwhile\ #1 \algorithmicdo\startsubalgorithmic}
\def\algorithmic!endwhile{\stopsubalgorithmic\algorithmicendwhile}
\def\algorithmic!repeat  {\par\algorithmicrepeat\startsubalgorithmic}
\def\algorithmic!until #1{\stopsubalgorithmic\algorithmicuntil\ #1}
\def\algorithmic!loop    {\par\algorithmicloop\startsubalgorithmic}
\def\algorithmic!endloop {\stopsubalgorithmic\algorithmicendloop}
\def\algorithmic!require {\par\algorithmicrequire\space}
\def\algorithmic!ensure  {\par\algorithmicensure\space}
\def\algorithmic!return  {\par\algorithmicreturn\space}
\def\algorithmic!true    {\algorithmictrue}
\def\algorithmic!false   {\algorithmicfalse}
\def\algorithmic!print   {\par\algorithmicprint\space}
\def\algorithmic!comment {\algorithmiccomment}

% I should make this multilingual

\def\algorithmicrequire  {{\bf Require:}}
\def\algorithmicensure   {{\bf Ensure:}}
\def\algorithmiccomment#1{\{#1\}}
\def\algorithmicend      {{\bf end}}
\def\algorithmicif       {{\bf if}}
\def\algorithmicthen     {{\bf then}}
\def\algorithmicelse     {{\bf else}}
\def\algorithmicelsif    {\algorithmicelse\ \algorithmicif}
\def\algorithmicendif    {\algorithmicend\ \algorithmicif}
\def\algorithmicfor      {{\bf for}}
\def\algorithmicforall   {{\bf for all}}
\def\algorithmicdo       {{\bf do}}
\def\algorithmicendfor   {\algorithmicend\ \algorithmicfor}
\def\algorithmicwhile    {{\bf while}}
\def\algorithmicendwhile {\algorithmicend\ \algorithmicwhile}
\def\algorithmicloop     {{\bf loop}}
\def\algorithmicendloop  {\algorithmicend\ \algorithmicloop}
\def\algorithmicrepeat   {{\bf repeat}}
\def\algorithmicuntil    {{\bf until}}
\def\algorithmicprint    {{\bf print}}
\def\algorithmicreturn   {{\bf return}}
\def\algorithmictrue     {{\bf true}}
\def\algorithmicfalse    {{\bf false}}

% Default values

\setupalgorithmic
  [\c!numbering=\v!no,
   \c!start=1,
   \c!stop=,
   \c!step=1,
   \c!width=1em,
   \c!margin=1em,
   \c!before=\blank,
   \c!after=\blank]

\protect \endinput
