% Macros for the Snafu memo document type.
% Copyright (C) 1993, 1994 Gary Houston
% This program may be copied and modified under the terms of the 
% GNU General Public License version 2.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{smemo}[1994/08/26 gf Snafu memo class]

% The "linememo" option can be used to get a lined memo header.
\newif\if@lined
\@linedfalse
\DeclareOption{linememo}{\@linedtrue}

\DeclareOption*{%
  \PassOptionsToClass{\CurrentOption}{article}%
}
\ProcessOptions

\LoadClass{article}

\RequirePackage{gftext}
\RequirePackage{gfdisplay}
\RequirePackage{gfhead}
\RequirePackage{gffig}
\RequirePackage{gfstructure}
\RequirePackage{gflink}

% Don't normally number sections in a memo.
\setcounter{secnumdepth}{0}

\regularspacing

% Set up the header commands.
\let\@subject\@empty
\newcommand{\subject}[1]{\renewcommand{\@subject}{#1}}

\let\@from\@empty
\newcommand{\from}[1]{\renewcommand{\@from}{#1}}

\let\@actionby\@empty
\newcommand{\actionby}[1]{\renewcommand{\@actionby}{#1}}

\renewcommand{\@date}{\today}
\renewcommand{\date}[1]{\renewcommand{\@date}{#1}}

% \to is defined as \rightarrow by LaTeX.
\let\@to\@empty
\renewcommand{\to}[1]{\renewcommand{\@to}{#1}}

\let\@cc\@empty
\newcommand{\cc}[1]{\renewcommand{\@cc}{#1}}

\let\@through\@empty
\newcommand{\through}[1]{\renewcommand{\@through}{#1}}

% Set up token lists for extended headers.
% See Texbook section "List macros".
\def\@xheadtags{}
\def\@xheadtext{}
\toksdef\ta=0 \toksdef\tb=2 % Token list registers for temporary use.
\long\def\rightappenditem#1\to#2{\ta={ {#1}}\tb=\expandafter{#2}%
  \edef#2{\the\tb\the\ta}}
\def\lop#1\to#2{\expandafter\lopoff#1\lopoff#1#2}
\long\def\lopoff #1#2\lopoff#3#4{\def#4{#1}\def#3{#2}}
\def\xheadt#1{\rightappenditem#1\to\@xheadtags}
\def\xheadd#1{\rightappenditem#1\to\@xheadtext}

% Opening of the memo. In order to handle very long lines or multiple
% lines in a single header, vboxes are constructed to precise widths.
% This is the default header: it is redefined for a lined memo.
\newcommand{\opening}
 {%
  \global\@topnum\z@ % Stop figures appearing at top of page.
  \noindent % do not remove this, or everything will fail
  \thispagestyle{docnum}%
  % Subject (compulsory)
  \setbox0=\hbox{\bf Subject: }%
  \dimen0=\hsize
  \advance\dimen0 by -\wd0
  \box0\vtop{\noindent\hsize=\the\dimen0
    \ignorespaces\strut\@subject\strut}\newline
  % To (compulsory) and ActionBy (optional)
  % ActionBy is assumed to be reasonably short
  \setbox0=\hbox{\bf To: }%
  \dimen0=\hsize
  \advance\dimen0 by -\wd0
  \ifx\@empty\@actionby\relax\else{%
    \global\setbox1=\hbox{{\bf Action By: }\ignorespaces\@actionby}%
    \global\advance\dimen0 by -\wd1
  }\fi%
  \box0\vtop{\noindent\hsize=\the\dimen0\ignorespaces\strut\@to\strut}%
  \ifx\@empty\@actionby\relax\else{%
    \box1
  }\fi\newline
  % cc (optional)
  \ifx\@empty\@cc\relax\else{%
    \setbox0=\hbox{\bf cc: }%
    \dimen0=\hsize
    \advance\dimen0 by -\wd0
    \box0\vtop{\noindent\hsize=\the\dimen0
      \ignorespaces\strut\@cc\strut}\newline
  }\fi
  % Through (optional)
  \ifx\@empty\@through\relax\else{%
    \setbox0=\hbox{\bf Through: }%
    \dimen0=\hsize
    \advance\dimen0 by -\wd0
    \box0\vtop{\noindent\hsize=\the\dimen0
      \ignorespaces\strut\@through\strut}\newline
  }\fi
  % Don't put any blank lines here.
  % Extended tags, extracted from token lists.
  \def\@done{0}%
  \loop\ifx\@xheadtags\empty\def\@done{1}\fi
  \ifnum\@done=0
    \lop\@xheadtags\to\@headtag
    \lop\@xheadtext\to\@headtext
    \setbox0=\hbox{\bf\@headtag{}: }%
    \dimen0=\hsize
    \advance\dimen0 by -\wd0
    \box0\vtop{\noindent\hsize=\the\dimen0
    \ignorespaces\strut\@headtext\strut}\newline
  \repeat
  % From (compulsory) and Date (supplied or imputed).
  % Date is assumed to be reasonably short.
  \setbox0=\hbox{\bf From: }%
  \dimen0=\hsize
  \advance\dimen0 by -\wd0
  \global\setbox1=\hbox{\ignorespaces\@date}%
  \global\advance\dimen0 by -\wd1
  \box0\vtop{\noindent\hsize=\the\dimen0\ignorespaces\strut\@from\strut}%
  \box1\newline
  \par\noindent\ignorespaces
} % end of \opening

% Load the lined memo opening if required.
\if@lined\RequirePackage{linememo}\fi

\newenvironment{memo}
 {\opening}
 {}

\newcommand{\stopbreaks}{\interlinepenalty \@M
   \def\par{\@@par\nobreak}\let\\=\@nobreakcr
   \let\vspace\@nobreakvspace}

\newcommand{\@nobreakvspace}{\@ifstar{\@nobreakvspacex}{\@nobreakvspacex}}

\newcommand{\@nobreakvspacex}[1]
  {\ifvmode\nobreak\vskip #1\relax\else
   \@bsphack\vadjust{\nobreak\vskip #1}\@esphack\fi}

\newcommand{\@nobreakcr}
  {\vadjust{\penalty\@M}\@ifstar{\@xnewline}{\@xnewline}}

\newcommand{\closing}[1]
 {\par\nobreak\vspace{\parskip}%
  \stopbreaks
  \noindent
  \ignorespaces #1}

\newcommand{\signed}[1]
 {\stopbreaks\par\vspace{6\medskipamount}#1}
