%% -*- Mode: TeX ; -*-
%  Manual.sty - LaTeX style for manual like documentation
%  Acknowledgements - mostly robbed from botex 

% Fonts
\message{
[Title,}
\def\argfont{\it}		%Font for typing functionargs	
\def\defunfont{\bf}		%Font for typing the names offunctions
\def\dffont{\small\sl}		%Font for the keywords

\font\titlerm = cmb10 scaled 1200

\def\AILab{\bf Artificial Intelligence Laboratory, Cambridge MA 02139 USA.}
%\def\us{\noexpand\_}
%\def\us{\_}
% Use \settitle{foo} to get a nice title
% In addition you can set \Paper and
%                         \myauthor
\def\settitle #1{\gdef\thistitle{#1}}
\def\Paper{}
\def\myauthor{}
\def\mytitlepage{%
\begin{titlepage}
\nobreak\vskip 5pt\hrule\kern 2truept\hrule\kern 3.5truept
\centerline{\titlerm \thistitle}
\vskip 10pt
\centerline{\myauthor}
\vskip 30pt
\centerline{\rm \Paper}
\vskip 10pt
\kern 3.5truept\hrule\kern 2truept\hrule
\par\vfill 
\centerline{\AILab}
\end{titlepage}
}%end def

%
% Page Heading and footing macros
% Counters get set here too.
\message{headings,}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}

\def\ps@myheadingswithtime{
	\def\@oddfoot{\hbox{}\small\sl\jobname.tex\hfil\date}
	\def\@evenfoot{\hbox{}\small\sl\jobname.tex\hfil\date}
	\def\@oddhead{\hbox{}\small\sl\rightmark\hfil\thepage}
	\def\@evenhead{\hbox{}\small\sl\rightmark\hfil\thepage}
	\def\sectionmark##1{\markright{##1}}
	\def\subsectionmark##1{}}

\def\ps@myheadings{
	\def\@oddfoot{} 
	\def\@evenfoot{}
	\def\@oddhead{\hbox to\textwidth{\small\sl\rightmark\hfill{\rm\thepage}}}
	\def\@evenhead{\hbox to\textwidth{{\rm\thepage}\hfill\small\sl\leftmark}}
	\def\chaptermark##1{\markboth {\ifnum \c@secnumdepth >\m@ne
		\@chapapp\ \thechapter\hskip 10pt \fi ##1}{}}
	\def\sectionmark##1{\markright{\ifnum \c@secnumdepth >\z@
		\S\thesection \hskip 10pt \fi ##1}}
	\def\subsectionmark##1{}}

\def\ps@regularheadings{
	\def\@oddfoot{\hbox{}\hfil\thepage\hfil}
	\def\@evenfoot{\hbox{}\hfil\thepage\hfil}
	\def\@oddhead{\hbox{}\hfil\small\sl\rightmark}
	\def\@evenhead{\hbox{}\small\sl\rightmark\hfil}
	\def\chaptermark##1{\markright{##1}}
	\def\sectionmark##1{}
	\def\subsectionmark##1{}}

% defuns
\message{defuns,}
% we define lastfunction to hold the name of the last function we have
% seen thus far. This will be used as subtopic to index the keyword under
\def\lastfunction{}
\def\lastvariable{}

%First do defname which outputs the header and an index entry
\def\defname #1#2#3{%
\vskip \parskip 
\vskip 0.2in
\noindent           % start a new par
\hbox to \hsize{{\defunfont #1}\hskip 0.25in{\argfont #2}\hfill{\dffont #3}}
}

\def\defnamewithparens #1#2#3{%
\vskip \parskip 
\vskip 0.2in
\noindent           % start a new par
\hbox to \hsize{{\defunfont #1}({\argfont #2})\hfill{\dffont #3}}
}

\message{and indices]
}
%                 Indices, and index files
%    This macro does the following things:
% \newindex{fn}{defun}{Function}
%    The first arg: filename extension for putting indices in.
%    The second arg: macroname to define
%    The third arg: what should we call it.
%    1. Opens a file called foo.fn (where foo.tex is the tex file)
%       for writing the function index entries to.
%    2. Defines a macro \fnindex (notice the first two letters are the
%       the same as the first arg to the \newindex macro), that adds
%       a function index entry
%    3. Defines a macro \defun that takes two arguments and formats
%       them as illustrated below.
%       \defun{function_name}{arg1 arg2}
%       will be formatted as 
%       function_name     arg1 arg2                            Function
%       The fonts of all three can be independently controlled.
% \makindex{fn}{Function Index} basically does a 
%    1. Closes the output file opened by newindex foo.fn.
%    2. does \theindex{Function Index}
%            \input foo.fns (notice the sorted index file)
%            \endtheindex
%

\def\defvar #1#2{%
\doind {vr}{#1} %
\defname{#1}{#2}{Variable}}

\def\newindex #1#2#3{
\expandafter\newwrite \csname#1indfile\endcsname% make number for output file
\immediate\openout \csname#1indfile\endcsname \jobname.#1 % open the file and
						% make fooindfile = the fp
\expandafter\xdef\csname#1index\endcsname##1{%
\noexpand\doind {#1}{##1}}%
\expandafter\xdef\csname#2\endcsname##1##2{%
\noexpand\doind {#1}{##1}%
{\noexpand\defname {##1}{##2}{#3}}}}%
%{\write \csname#1indfile\endcsname{% do prolog
%\string \par \string \vfill \string \eject %
%\string \theindex{\string #2}}}% then write  \theindex{foo}


\outer\def\makindex #1#2{%
%\write \csname#1indfile\endcsname{%   do epilogue
%\string \par \string \vfill \string \eject %
%\string \endtheindex}   %   finish up
\thispagestyle{plain}
\immediate\closeout \csname#1indfile\endcsname % close the file
{
\makeatletter
\theindex{#2}
\@input{\jobname.#1s}
\endtheindex
\makeatother
}
}

% The macro that does all the dirty work of writing the index entry
% into a file - texindex program needs entries of the form
% \entry {sortstring}{page}{topic}{subtopic} or
% \entry {sortstring}{page}{topic}
% For simplicity's sake, we just make do with the second of these
% formats and make the sortstring same as that of the topic.
\def\doind #1#2{%
\def\us{\noexpand\_}
\immediate\write \csname#1indfile\endcsname{%
\string \entry \space {#2}{\thepage}{#2}}%
\def\us{\_}
}%


%Actual entry macro
\def\entry#1#2{{\item #1 {\sl #2}}}
\def\initial#1{{\indexspace}{\bf #1}}

% The index environment
\def\theindex#1{\@restonecoltrue\if@twocolumn\@restonecolfalse\fi
\columnseprule \z@
\columnsep 35pt\twocolumn[\section*{#1}]
	\markboth{#1}{#1}\thispagestyle{plain}\parindent\z@
	\parskip\z@ plus .3pt\relax\let\item\@idxitem}

\def\@idxitem{\par\hangindent 40pt}
\def\subitem{\par\hangindent 40pt \hspace*{20pt}}
\def\subsubitem{\par\hangindent 40pt \hspace*{30pt}}
\def\endtheindex{\if@restonecol\onecolumn\else\clearpage\fi}
\def\indexspace{\par \vskip 10pt plus 5pt minus 3pt\relax}


%\def\defun #1#2{%
%\doind {fn}{#1} %
%\defnamewithparens{#1}{#2}{Function}}

