% Macros for the Snafu memo document type.
% Gary Houston.

\input{gftext.sty}
\input{gfdisplay.sty}
\input{gfhead.sty}
\input{gffig.sty}
\input{gfstructure.sty}
\input{gflink.sty}

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

\regularspacing

% Set up the header commands.
\long\def\subject#1{\def\@subject{#1}}
\def\@subject{}
\long\def\from#1{\def\@from{#1}}
\def\@from{}
\long\def\actionby#1{\def\@actionby{#1}}
\def\@actionby{}
\long\def\date#1{\def\@date{#1}}
\def\@date{\today}
\def\to#1{\def\@to{#1}}
\def\@to{}
\def\cc#1{\def\@cc{#1}}
\def\@cc{}
\def\through#1{\def\@through{#1}}
\def\@through{}

% 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.
\def\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
% 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

\long\def\memo{\opening}
\def\endmemo{}

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

\def\@nobreakvspace{\@ifstar{\@nobreakvspacex}{\@nobreakvspacex}}

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

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

\long\def\closing#1{\par\nobreak\vspace{\parskip}%
  \stopbreaks
  \noindent
  \ignorespaces #1}

\def\signed#1{\stopbreaks\par\vspace{6\medskipamount}#1}
