%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\typeout{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
  Prolog Documentation in LaTeX. Version 2.4 gene 10/93
% 
% Purpose:
%   Style option for LaTeX 2.09 to provide routines
%   to document Prolog programs
%
% Documentation: see seperate LaTeX document `pcode24.tex'
%
% Author: Gerd Neugebauer
%         FG Intellektik
%         TH Darmstadt
%         Alexanderstr. 10
%	  D-6100 Darmstadt (Germany)
% Mail:   gerd@intellektik.informatik.th-darmstadt.de
%
% Copyright (C) 1993 Gerd Neugebauer
%
% pcode.sty is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY.  No author or distributor
% accepts responsibility to anyone for the consequences of using it
% or for whether it serves any particular purpose or works at all,
% unless he says so in writing.
%
% Everyone is granted permission to copy, modify and redistribute
% pcode.sty, provided this copyright notice is preserved and any
% modifications are indicated.
%
%
% This style is still under development and may be replaced with a new
% version which provides an enhanced functionality.
% Any comments are welcome but don't expect ANY help from my side.
%
}

\def\PredicateFont{\tt}		% font to use in \Predicate for the body
\def\PredicateFileFont{\small\sf}% font to use in \Predicate for the file name
\def\PredicateSkip{\smallskip}	% skip command executed before and after \P...
\def\PredicateIndent{20mm}	% maximum indentation in \Predicate
\def\PredicateFileExtension{.pl}% text appended to file in \Predicate
\def\PredicateFileWidth{20mm}	% minumum width of the file name in \Predicate
\def\PredicateBoxSep{3pt}	% amount of space between the box and contents
\def\PredicateBoxRule{0.5pt}	% line thickness of box

\def\PrologFont{\small\tt}	% font to typeset the prolog code
\def\PrologIndent{2em}		% indentation of prolog code
\def\PrologNumberFont{\tiny}	% font to typeset the prolog line numbers

\def\PrologListFont{\small\tt}	% font to typeset a prolog list (exports)
\def\PrologListIndent{2em}	% indentation of prolog list (exports)

\def\PrologRuleWidth{0pt}	% width of rule seperating code and text

\def\PrologModule#1#2{\section{The Module {\tt #1}}}
\def\PrologFile#1#2{\section{The File {\tt #1}}}

\def\PrologEXPORTS{}
\def\PrologFILE{}		% current module name. no to be set by the user

\def\PredicateIndex#1{\index{#1}}% command to put a predicate in an index

\newif\ifPrologNumberLines	% switch to enable line numbering

\newif\ifPrologQuintus		% switch to allow Quintus like modules
	\PrologQuintustrue

%------------------------------------------------------------------------------
% 
%

\gdef\PROLOG@@delayed{}

\newcount\PLline

\newdimen\PredIndent
\newdimen\PredWidth

\newdimen\w \setbox0=\hbox{\PrologFont\space} \w=8\wd0 % tab amount


%------------------------------------------------------------------------------
% Modified version of the verbatim environment from the TeX-Book
%
\def\uncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}

\gdef\setupPcode{\PrologFont\parskip=0ex\parindent=0pt
  \ifx\PROLOG@@delayed\empty\else%
%    \hbox to \PrologIndent%
    {\ifPrologNumberLines \PrologNumberFont \the\PLline%
    \global\advance\PLline1\fi}{\ \ \ % <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    \PROLOG@@delayed}%
    \gdef\PROLOG@@delayed{}\par%
  \fi%
  \def\par{\leavevmode\egroup\box0\endgraf}
  \obeylines \uncatcodespecials \obeyspaces
  \catcode`\`=\active \catcode`\^^I=\active
  \everypar{\hbox to \PrologIndent%
	    {\ifPrologNumberLines \PrologNumberFont \the\PLline%
	    \global\advance\PLline1\fi\hfill}\startbox}}

\def\startbox{\setbox0=\hbox\bgroup}

{\catcode`\^^I=\active
 \gdef^^I{\leavevmode\egroup
   \dimen0=\wd0 % the width so far, or since the previous tab
   \divide\dimen0 by\w
   \multiply\dimen0 by\w % compute previous multiple of \w
   \advance\dimen0 by\w  % advance to next multiple of \w
   \wd0=\dimen0 \box0 \startbox}%
}

\def\Pcode*/{\Pcode@init\par\begingroup\setupPcode\doPcode}

{\catcode`\|=0 \catcode`\\=12 % | is temporary escape character
  |obeylines|gdef|doPcode^^M#1/*Pcode{#1|endgroup|Pcode@exit}%
}


\def\PL*/{\Pcode@init\begingroup\setupPcode\doPL}
{\catcode`\|=0 \catcode`\\=12 % | is temporary escape character
  |obeylines|gdef|doPL^^M#1/*PL{#1|endgroup|Pcode@exit}}



\def\Pcode@init{%
	\ifdim\PrologRuleWidth>0pt%
	  \par\noindent\rule{\textwidth}{\PrologRuleWidth}\par%
	\else\medskip\par\fi}
\def\Pcode@exit{%
	\ifdim\PrologRuleWidth>0pt%
	  \vspace{-2.3ex}\noindent\rule{\textwidth}{\PrologRuleWidth}\par%
	\else\smallskip\par\fi}

%------------------------------------------------------------------------------
% Typeset a boxed predicate
%
\def\Predicate{\Prolog@US@start\Predicate@}
\def\Predicate@#1/#2(#3).{\PredicateSkip\par\noindent%
{\setbox1=\hbox{\PredicateFileFont \PrologFILE\PredicateFileExtension}%
\fboxrule=\PredicateBoxRule%
\fboxsep=\PredicateBoxSep%
\fbox{\PredicateIndex{#1/#2}%
  \dimen255=\wd1
  \ifdim\dimen255<\PredicateFileWidth \dimen255=\PredicateFileWidth \fi
  \dimen255=-\dimen255 \advance\dimen255 by-6mm \advance\dimen255 by \textwidth
  \parbox{\dimen255}{\raggedright
	{ \setbox0=\hbox{\normalsize\PredicateFont #1(}
	  \global\PredIndent=\wd0
	}
	\ifdim\PredIndent>\PredicateIndent \PredIndent=\PredicateIndent \fi
	\PredWidth=\dimen255 \advance\PredWidth by -\PredIndent
	\parshape=2 0mm \dimen255 \the\PredIndent \the\PredWidth
	\normalsize\PredicateFont #1\ifx\@empty#3 \else(#3)\fi
  }}
  \hfill \box1\PredicateSkip\par}\Prolog@US@end}

%------------------------------------------------------------------------------
% Dirty hack.
% make : active to catch :-
% use a group to hide the changes
\def\PROLOG@init{\begingroup\catcode`:=\active}
\def\PROLOG@exit{\endgroup}

\let\PrologInit=\PROLOG@init

%------------------------------------------------------------------------------
% To input a Prolog file means it starts with 
%	:- module(_,_). /*
% or
%	:- nomodule(_,_). /*
% Thus we make : active and include the file
%
\def\PrologInput#1{%
  \gdef\PrologFILE{#1}%
  \gdef\PrologEXPORTS{}%
  \PROLOG@init%
  \input{#1}}

%------------------------------------------------------------------------------
% 
%
\PROLOG@init
 \gdef:{\@ifnextchar-{\PROLOG@goal}{: }}
 \gdef\PROLOG@goal-{%
	\PROLOG@exit
	\Prolog@US@start
	\ifPrologQuintus
	  \let\PROLOG@@MODULE\PROLOG@start@module
	  \let\PROLOG@@FILE\PROLOG@start@file
	\else
	  \let\PROLOG@@MODULE\PROLOG@start@module@sepia
	  \let\PROLOG@@FILE\PROLOG@start@file@sepia
	\fi
	\@ifnextchar m{\PROLOG@@MODULE}%
		      {\@ifnextchar t{\PROLOG@start@true}%
		      {\PROLOG@@FILE}}}
\PROLOG@exit

%------------------------------------------------------------------------------
%
\def\PROLOG@start@module@sepia mod#1(#2). /*{%
	\setbox0=\hbox{\PrologFont\space} \global\w=8\wd0% tab amount
	\global\PLline=1
	\gdef\PROLOG@@delayed{:- mod#1(#2).}
	\gdef\PrologFILE{#2}%
	\catcode`\,=\active %
	\gdef\PrologEXPORTS{}%
	\PrologModule{\PrologFILE}{}%
	\Prolog@US@end}

\def\PROLOG@start@file@sepia #1(#2). /*{%
	\setbox0=\hbox{\PrologFont\space} \global\w=8\wd0% tab amount
	\global\PLline=1
	\gdef\PROLOG@@delayed{:- #1(#2).}
	\gdef\PrologFILE{#2}%
	\catcode`,=\active %
	\gdef\PrologEXPORTS{}%
	\PrologFile{\PrologFILE}{}%
	\Prolog@US@end}

%------------------------------------------------------------------------------
%
\def\PROLOG@start@module module(#1,{%
	\setbox0=\hbox{\PrologFont\space} \global\w=8\wd0% tab amount
	\global\PLline=1
	\gdef\PrologFILE{#1}%
	\catcode`\,=\active %
	\PROLOG@start@module@@}
\def\PROLOG@start@module@@[#1]). /*{%
	\gdef\PrologEXPORTS{#1}%
	\PrologModule{\PrologFILE}{#1}%
	\Prolog@US@end}

%------------------------------------------------------------------------------
% 
\def\PROLOG@start@file #1(#2,{%
	\setbox0=\hbox{\PrologFont\space} \global\w=8\wd0% tab amount
	\global\PLline=1
	\gdef\PrologFILE{#2}%
	\catcode`,=\active %
	\PROLOG@start@file@@}
\def\PROLOG@start@file@@[#1]). /*{%
	\gdef\PrologEXPORTS{#1}%
	\PrologFile{\PrologFILE}{#1}%
	\Prolog@US@end}

\def\PROLOG@start@true true. /*{%
	\setbox0=\hbox{\PrologFont\space} \global\w=8\wd0% tab amount
	\global\PLline=1%
	\Prolog@US@end}

%------------------------------------------------------------------------------
% 
%
\def\StopProlog*/{%
	\gdef\PrologFILE{}%
	\gdef\PrologEXPORTS{}%
}
\def\EndProlog*/{%
	\gdef\PrologFILE{}%
	\gdef\PrologEXPORTS{}%
}

%------------------------------------------------------------------------------
% 
%
\def\PrologList{\par\noindent%
	\Prolog@US@start
	\PrologListFont
	\catcode`\,=\active%
	\parindent=\PrologListIndent\parskip=0pt\par
	\Prolog@List}
{\catcode`\,=\active 
\gdef\Prolog@List[#1]{%
	\def,{\par}%
	#1
	\Prolog@US@end\par}
}

\def\PrologListEXPORTS{\PrologList[\PrologEXPORTS]}


%------------------------------------------------------------------------------
% Activate _ temporarily
%
\def\Prolog@US@start{\begingroup\catcode`\_=\other }
\def\Prolog@US@end{\endgroup }


%\def\pc@Id{%
%    \bgroup
%	\catcode`\_=\active
%	\pc@setId
%    }
%\def\pc@setId#1{%
%	\tt #1%
%    \egroup
%    }


\def\WithActiveUnderscore{\begingroup\catcode`\_=\active\WithActiveUnderscore@}
\def\WithActiveUnderscore@#1{#1\endgroup}


\def\currentFile{\PrologFILE}
\PLline=1

