%  MACROS FOR ALGORITHMS OR PROGRAMS     REQUIRES genmacs.tex
%  CAN HAVE TROUBLE WITH HYPERREF FAILING TO INTERACT WELL WITH \refstepcounter
%  \proglabel is like \label but uses progline counter or \progitem argument.

\newcounter{proglevel}\newcounter{progline}\newcommand{\linelab}{}
\newlength{\linenosep}
\newlength{\subprogindent}\newlength{\progitemhangindent}
\newlength{\addprogitemsep}

\newenvironment{program}[1]%
{\setcounter{proglevel}{0}\setcounter{progline}{0}
 \settowidth{\linenosep}{\quad}
 \settowidth{\subprogindent}{\qquad}\settowidth{\progitemhangindent}{\quad}
 \setlength{\addprogitemsep}{0pt}
\newcommand{\setsubprogtabs}{#1\=\hspace*{\linenosep}
                             \hspace*{\value{proglevel}\subprogindent}\=
                             \hspace*{\progitemhangindent}\=\+\+\+\kill}
 \begin{tabpackage}\setlength{\tabbingsep}{0pt}\setsubprogtabs}%
{\end{tabpackage}}

% NOT TO BE USED AS AN ENVIRONMENT; USE ONLY \subprogram AND \endsubprogram
\newenvironment{subprogram}%
{\stepcounter{proglevel}\pushtabs\<\<\<\-\-\-\setsubprogtabs}%
{\poptabs\addtocounter{proglevel}{-1}}

\makeatletter\def\progitem{\@ifnextchar[{\progitemwarg}{\progitemwoarg}}\makeatother
\let\prit=\progitem

\newlength{\progitemstrutheight}
\setlength{\progitemstrutheight}{0pt} % really start at {\baselineskip} ??
\addtolength{\progitemstrutheight}{\addprogitemsep}

\def\progitemwarg[#1]{\<\< #1\'\>\rule{0pt}{\progitemstrutheight}\gdef\linelab{#1}\ignorespaces}
\def\progitemwoarg{\<\< \stepcounter{progline}\theprogline \'\>\rule{0pt}{\progitemstrutheight}\gdef\linelab{\theprogline}\ignorespaces}
 % Using \refstepcounter instead of \stepcounter doesn't do the job, because the \proglabel won't be in the same scope.

\makeatletter
\newcommand{\proglabel}[1]{\@bsphack
  \begingroup
  \protected@write\@auxout{}%
      {\string\newlabel{#1}{%
         {\linelab}%
         {\thepage}%
         {\@currentlabelname}%
         {\@currentHref}{}%
    }}%
  \endgroup
  \@esphack\ignorespaces}
\makeatother

\newcommand{\progcom}[1]{\qquad $\langle${\sl #1}$\rangle$}

\newcommand{\ite}[3]
    {{\it if} #1 \begin{description}
                                  \item[\hfill {\it then}] #2
                                  \item[\hfill {\it else}] #3
                              \end{description}}

\newcommand{\proc}[1]{\mbox{\sc #1}}
\newcommand{\var}[1]{\mbox{\it #1}}

\newcommand{\Begin}{{\bf begin\ }}
\newcommand{\End}{{\bf end\ }}
\newcommand{\Do}{{\bf do\ }}
\newcommand{\Od}{{\bf od\ }}
\newcommand{\Enddo}{{\bf enddo\ }}
\newcommand{\Pardo}{{\bf pardo\ }}
\newcommand{\Loop}{{\bf loop\ }}
\newcommand{\Endloop}{{\bf endloop\ }}
\newcommand{\Foreach}{{\bf foreach\ }}
\newcommand{\For}{{\bf for\ }}
\newcommand{\Endfor}{{\bf endfor\ }}
\newcommand{\To}{{\bf to\ }}
\newcommand{\Downto}{{\bf downto\ }}
\newcommand{\By}{{\bf by\ }}
\newcommand{\If}{{\bf if\ }}
\newcommand{\Then}{{\bf then\ }}
\newcommand{\Else}{{\bf else\ }}
\newcommand{\Elif}{{\bf elif\ }}
\newcommand{\Fi}{{\bf fi\ }}
\newcommand{\Endif}{{\bf endif\ }}
\newcommand{\While}{{\bf while\ }}
\newcommand{\Endwhile}{{\bf endwhile\ }}
\newcommand{\Repeat}{{\bf repeat\ }}
\newcommand{\Until}{{\bf until\ }}
\newcommand{\Break}{{\bf break\ }}
\newcommand{\Case}{{\bf case\ }}
\newcommand{\Of}{{\bf of\ }}
\newcommand{\Procedure}{{\bf procedure\ }}
\newcommand{\Return}{{\bf return\ }}
\newcommand{\Call}{{\bf call\ }}
\newcommand{\Goto}{{\bf goto\ }}
\newcommand{\Halt}{{\bf halt\ }}
\newcommand{\Noop}{{\bf noop}}
\newcommand{\With}{{\bf with\ }}
\newcommand{\Imports}{{\bf imports\ }}
\newcommand{\Variable}{{\bf var\ }}
\newcommand{\Type}{{\bf type\ }}
\newcommand{\Assert}{{\bf assert\ }}

\newcommand{\True}{\mbox{\sc true\ }}
\newcommand{\False}{\mbox{\sc false\ }}
\newcommand{\lispt}{\mbox{\sc t}}
\newcommand{\nil}{\mbox{\sc nil}}
\newcommand{\NULL}{\mbox{\sc null}}
