%% The Code environment %%%

%% The following commands are so we can automatically number and symbolically reference
%% lines.
%% The ``codelinenumber'' counter counts the current line number
\newcounter{codelinenumber}
%% The \zeroli command zeros the line number counter
\newcommand{\zeroli}{\setcounter{codelinenumber}0}
\newcommand{\setli}[1]{\setcounter{codelinenumber}#1}

%% The \li command bumps the counter, outputs it, and skips some space
%% It also GLOBALLY sets the value of \@currentlabel, so that the \label
%% commands on the same line work OK.  The problem otherwise is that Latex
%% uses groups on each field of a line in tabbing mode, and this causes
%% \@currentlabel to get reset as soon as a field ends (e.g. at the \').
%% Now, the label can appear after the \\, as in
%%	\li \>\>$x\gets y$ \\ \label{li:assign-x}
%%

\makeatletter
\def\@startline{\global\@curtabmar\@nxttabmar\relax
   \global\@curtab\@curtabmar\setbox\@curline\hbox
    {}\@startfield\global\lifalse\strut}

\newenvironment{code}{%
\setbox\strutbox\hbox{\vrule height 9pt depth 4pt width0pt}%
\noindent\begin{tabbing}%
\zeroli\setlength{\tabbingsep}{1em}
\hspace*{1em}\=999\ifdoubledigit9\fi
\=\ {\bf then} \={\bf then} \={\bf then} \={\bf then}
	\={\bf then} \={\bf then} \={\bf then} \={\bf then} \={\bf then} \={\bf then}
	\={\bf then} \=\+\+\kill}{\end{tabbing}}

\newif\ifdoubledigit
\newcommand{\codebox}[1]{\setbox0=\vbox{\begin{code}#1\end{code}}
\ifnum\c@codelinenumber>9\global\doubledigittrue\else\doubledigitfalse\fi
\vskip1sp\noindent\hskip-14pt%% 
\parbox{\textwidth}{\begin{code}\protect#1\end{code}}\global\let\@currentlabel=\thesection}

\newif\ifli
\newcommand{\li}{\global\litrue\stepcounter{codelinenumber}%
\ifdoubledigit%
\hbox to8pt{\hss\thecodelinenumber\hskip5pt}\else%
\hbox to8pt{\hskip-1pt\thecodelinenumber\hss}\fi%
\xdef\@currentlabel{\p@codelinenumber\thecodelinenumber}\'}

\newcommand{\linolabel}{\global\litrue\stepcounter{codelinenumber}%
\hbox to8pt{}}


\makeatother

\newcommand{\For}{{\bf for} }
\newcommand{\Foreach}{{\bf foreach} }
\newcommand{\Endfor}{{\bf endfor} }
\newcommand{\End}{{\bf end} }
\newcommand{\To}{{\bf to} }
\newcommand{\By}{{\bf by} }
\newcommand{\Downto}{{\bf downto} }
\newcommand{\While}{{\bf while} }
\newcommand{\Repeat}{{\bf repeat} }
\newcommand{\Until}{{\bf until} }
\newcommand{\If}{{\bf if} }
\newcommand{\Then}{\>{\bf then}\>}
\newcommand{\Else}{\>{\bf else}\>}
\newcommand{\ElseIf}{{\bf elseif} }
\newcommand{\Endif}{{\bf endif} }
\newcommand{\ElseNoIf}{{\bf else} }
\newcommand{\Do}{\>\>{\bf do}\'}
\newcommand{\Return}{{\bf return} }
\newcommand{\Comment}{$\!\rhd$ }
\newcommand{\Inparallel}{{\bf in parallel} }	%This should be flushed
\newcommand{\Goto}{{\bf goto} }
\newcommand{\Error}{{\bf error} } % optionally followed by string argument

\newcommand{\const}[1]{{\hbox{\sc#1}}}
\newcommand{\proc}[1]{{\hbox{\sc#1}}}

%\newcommand{\proc}[1]{%
%\ifmmode\mathord{\mathcode`-="702D\rm#1\mathcode`\-="2200}\else{\rm#1}\fi}
\newcommand{\procdecl}[1]{\proc{#1}\vrule width0pt height0pt depth 7pt \relax}
\newcommand{\id}[1]{\ifmmode\mathord{\mathcode`-="702D\it #1\mathcode`\-="2200}\else$\mathord{\mathcode`-="702D\it #1\mathcode`\-="2200}$\fi}
%\newcommand{\func}[1]{\ifmmode\mathop{\mathcode`-="702D\sc #1\mathcode`\-="2200}\nolimits\else$\mathop{\mathcode`-="702D\sc #1\mathcode`\-="2200}\nolimits$\fi}
%\newcommand{\func}[1]{{\sc #1}}
