%
% $Id: pseudocode.tex,v 1.2 1994/04/26 04:05:46 qjb Exp $
% $Source: /home/qjb/tex/macros/RCS/pseudocode.tex,v $
% $Author: qjb $
%
% TeX pseudocode environment
%
% Within the environment, use \pline to start a line or
% \plab{label} to start a line that you wish to refer to later.
% Use \pcref{label} to refer to it.  Use & to separate
% indentation levels.  Use \kwd{keyword} for keywords.	Use \cmt
% for a comment character.  Example:
%
% \beginpseudocode
% \pline 	 \> \kwd{for} $i \leftarrow 1$ \kwd{to} $n$ \cr
%                \> \cmt Print the value of $i$ \cr
% \plab{f:print} \> \> \kwd{print} $i$ \cr
% \pline         \> \kwd{end} \kwd{for} \cr
% \endpseudocode
% The print statement occured in line \pcref{f:print}.
%


\newcount\pseudoline
\newbox\pcbox
\newdimen\pcindent \pcindent=2em
\newif\ifpcfirsttab\pcfirsttabfalse

\def\pcref#1{\csname #1 \endcsname}
\def\beginpseudocode{
\begingroup
\goodbreak
\vskip\baselineskip
\pseudoline=1
\parindent=0pt
\parskip=0pt
\overfullrule=0pt
\def\kwd##1{{\bf ##1}}
\def\var##1{{\hbox{\it ##1}}}
\def\svar##1{##1} % XXX -- not exactly right
\def\cmt{\hbox{$\triangleright$} }
\def\advancepline{\global\advance\pseudoline by 1}
\def\pline{\number\pseudoline \advancepline}
\def\plab##1{
	\number\pseudoline
	\global\edef \csname ##1 \endcsname{\number\pseudoline}
	\advancepline}
\def\>{\pcshowfield\pcstartfield}
\def\cr{\pcshowfield\par\pcstartline}
\def\pcendfield{\hfil\egroup}
\def\pcshowfield{\pcendfield
	\ifpcfirsttab\pcfirsttabfalse\noindent\hskip.5\pcindent
	\else\fi\copy\pcbox\kern-\wd\pcbox\hskip\pcindent}
\def\pcstartfield{\setbox\pcbox=\hbox\bgroup\ignorespaces}
\def\pcstartline{\pcfirsttabtrue\pcstartfield}
\pcstartline
}
\def\endpseudocode{\pcendfield\endgroup}
