%%% -*-latex-*-
%%%
%%% Styles and other hacking for the Sod manual
%%%
%%% (c) 2015 Straylight/Edgeware
%%%

%%%----- Licensing notice ---------------------------------------------------
%%%
%%% This file is part of the Sensble Object Design, an object system for C.
%%%
%%% SOD is free software; you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation; either version 2 of the License, or
%%% (at your option) any later version.
%%%
%%% SOD is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with SOD; if not, write to the Free Software Foundation,
%%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

\ProvidesPackage{sod}

%% More reference types.
\defxref{p}{part}

%% Other languages with special typesetting.
\def\Cplusplus{C\kern-\p@++}
\def\Csharp{C\#}

%% Special maths notation.
\def\chain#1#2{\mathsf{ch}_{#1}(#2)}
\def\chainhead#1#2{\mathsf{hd}_{#1}(#2)}
\def\chaintail#1#2{\mathsf{tl}_{#1}(#2)}

%% Other mathematical tweaks.
\let\implies\Rightarrow
\let\epsilon\varepsilon

%% A table heading cell.  Clone and hack \multicolumn.
\def\thd{\omit\@ifnextchar[\thd@{\thd@[l]}}
\def\thd@[#1]#2{%
  \begingroup
    \tab@multicol \tab@initread \let\tab@looped\tab@err@multi
    \tab@preamble{}\def\tab@midtext{\bfseries#2}\tab@readpreamble{#1}%
    \the\tab@preamble
  \endgroup \ignorespaces
}

%% Unix manpage references.
\def\man#1#2{\textbf{#1}(#2)}

%% Listings don't need to be small.
\let\listingsize\relax

%% Metavariables are italics without decoration.
\def\syntleft{\normalfont\itshape}
\let\syntright\empty

%% Literal code is in sans face.
\let\codeface\sffamily
\def\code#1{\ifmmode\hbox\fi{\normalfont\codeface\/#1\/}}
\def\ulitleft{\normalfont\codeface}
\let\ulitright\empty

%% Conditionally enter maths mode.  Can't use \ensuremath here because we
%% aren't necessarily sure where the maths will actually end.
\let\m@maybe@end\relax
\def\m@maybe{\ifmmode\else$\let\m@maybe@end$\fi}

%% Standard syntax shortcuts.
\atdef <#1>{\synt{#1}\@scripts}
\atdef "#1"{\lit*{#1}\@scripts}
\atdef `#1'{\lit{#1}\@scripts}
\atdef |#1|{\textsf{#1}\@scripts}

%% A handy abbreviation; `\\' itself is too good to steal.
\atdef \\{\textbackslash}

%% Intercept grammar typesetting and replace the vertical bar with the
%% maths-font version.
\let\@@grammar\grammar
\def\grammar{\def\textbar{\hbox{$|$}}\@@grammar}

%% Collect super- and subscripts.  (Note that underscores are active for the
%% most part.)  When we're done, end maths mode if we entered it
%% conditionally.
\def\@scripts{\futurelet\@ch\@scripts@i}
\begingroup\lccode`\~=`\_\lowercase{\endgroup
\def\@scripts@i{\if1\ifx\@ch~1\else\ifx\@ch^1\else0\fi\fi%
  \expandafter\@scripts@ii\else\expandafter\m@maybe@end\fi}}
\def\@scripts@ii#1#2{\m@maybe#1{#2}\@scripts}

%% Doubling characters, maybe.  Either way, chain onto \@scripts.
\def\dbl@maybe#1{\let\@tempa#1\futurelet\@ch\dbl@maybe@i}
\def\dbl@maybe@i{\m@maybe\ifx\@ch\@tempa\@tempa\!\@tempa%
  \expandafter\@firstoftwo\expandafter\@scripts%
  \else\@tempa\expandafter\@scripts\fi}

%% Extra syntax for Lisp templates.  These produce the maths-font versions of
%% characters, which should contrast well against the sans face used for
%% literals.
\atdef [{\dbl@maybe[}
\atdef ]{\dbl@maybe]}
\atdef {{\m@maybe\{\@scripts}
\atdef }{\m@maybe\}\@scripts}
\atdef ({\m@maybe(\@scripts}
\atdef ){\m@maybe)\@scripts}
\atdef !{\m@maybe|\@scripts}
\def\returns{\m@maybe\longrightarrow\m@maybe@end\hspace{0.5em}\ignorespaces}
\atdef >{\leavevmode\unskip\hspace{0.5em}\returns}
\atdef -#1{\if>#1{\ensuremath\rightarrow}\fi}

%% Comment setting.
\atdef ;#1\\{\normalfont\itshape;#1\\}

%% Environment for setting programs.  Newlines are explicit, because
%% otherwise I need comments in weird places to make the vertical spacing
%% come out properly.  You can write `\obeylines' if you really want to.
\def\prog{\codeface\quote\tabbing}
\def\endprog{\endtabbing\endquote}
\def\ind{\quad\=\+\kill}

%% Put a chunk of text in a box.
\newenvironment{boxy}[1][\q@]{%
  \savenotes
  \dimen@\linewidth\advance\dimen@-1.2pt\advance\dimen@-2ex%
  \medskip%
  \vbox\bgroup\hrule\hbox\bgroup\vrule%
  \vbox\bgroup\vskip1ex\hbox\bgroup\hskip1ex\minipage\dimen@%
  \def\@temp{#1}\ifx\@temp\q@\else\leavevmode{\headfam\bfseries#1\quad}\fi%
}{%
  \endminipage\hskip1ex\egroup\vskip1ex\egroup%
  \vrule\egroup\hrule\egroup%
  \medskip%
  \spewnotes%
}

%% Lisp documentation machinery.
\def\definedescribecategory#1#2{\@namedef{cat!#1}{#2}}
\def\describecategoryname#1{%
  \expandafter\let\expandafter\@tempa\csname cat!#1\endcsname%
  \ifx\@tempa\relax#1\else\@tempa\fi}
\definedescribecategory{fun}{function}
\definedescribecategory{gf}{generic function}
\definedescribecategory{var}{variable}
\definedescribecategory{const}{constant}
\definedescribecategory{meth}{primary method}
\definedescribecategory{ar-meth}{around-method}
\definedescribecategory{be-meth}{before-method}
\definedescribecategory{af-meth}{after-method}
\definedescribecategory{cls}{class}
\definedescribecategory{ty}{type}
\definedescribecategory{mac}{macro}
\def\nlret{\\\hspace{4em}\returns}

\def\q@{\q@}
\newenvironment{describe}[3][\q@]{%
  \normalfont%
  \par\goodbreak%
  \vspace{\bigskipamount}%
  \setbox\z@\hbox{\bfseries[\describecategoryname{#2}]}%
  \dimen@\linewidth\advance\dimen@-\wd\z@%
  \def\@temp##1 ##2\q@{%
    \message{#2:##1}%
    {\def\@uscore####1{-}\edef\@tempb{\noexpand\label{#2:##1}}\@tempb}%
  }%
  \def\@tempa{#1}\ifx\@tempa\q@\@temp#3 \q@\else\@temp{#1} \q@\fi%
  \edef\@temp{{\the\linewidth}{@{}p{\the\dimen@}%
      @{\extracolsep{\fill}}l@{\extracolsep{0pt}}}}%
  \noindent\csname tabular*\expandafter\endcsname\@temp%
  \tabbing\codeface#3\endtabbing&\unhbox\z@\\\endtabular%
%  \@afterheading%
  \list{}{\rightmargin\z@}\item%
}{%
  \endlist%
}

%%%----- That's all, folks --------------------------------------------------
\endinput
