% Macros for the Snafu letter 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 letter.
\setcounter{secnumdepth}{0}

\regularspacing

% Postscripts.
\def\ps#1#2{%
\count0=#1{\bf
\par\ifnum\count0=1\vspace{2\parskip}\fi
\loop
\ifnum\count0>0
  \advance\count0 by -1
p%
  \repeat
s. }#2}

\def\enclosure{\par{\bf Enclosure}}

\def\contact#1#2{%
  \def\contacta{#1}%
  \def\contactb{phone}%
  \def\found{0}%
  \ifx\contacta\contactb Tel: \def\found{1}\fi
  \def\contactb{fax}%
  \ifx\contacta\contactb Fax: \def\found{1}\fi
  \def\contactb{email}%
  \ifx\contacta\contactb Email: \def\found{1}\fi
  \ifnum\found=0 #1: \fi
#2}

\long\def\subject#1{\def\@subject{#1}}
\def\@subject{}

% Redefine the default \opening, to allow a subject line and docnum.
\def\opening#1{%
  \global\@topnum\z@ % Stop figures appearing at top of page.
  \ifx\@empty\fromaddress
  \thispagestyle{firstpage}%
  {\raggedleft\@date}%
  \ifx\@empty\@docnum\else
    \\ref: \@docnum\fi\par
  \else
    \thispagestyle{empty}%
    {\raggedleft\begin{tabular}{l}\ignorespaces
      \fromaddress \\*[2\parskip]%
      \@date 
      \ifx\@empty\@docnum\else
        \\ref: \@docnum\fi
     \end{tabular}\par}%
  \fi
  \vspace{2\parskip}%
  {\raggedright \toname \\ \toaddress \par}%
  \vspace{2\parskip}%
  \ifx\@empty\@subject\else
    {\bf\@subject}\par\vspace{2\parskip}\fi
  #1\par\nobreak}


\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}}

\def\@cc{}
\def\cc#1{\def\@cc{#1}}

\long\def\closing#1{\par\nobreak\vspace{\parskip}%
  \stopbreaks
  \noindent
  \hspace*{\longindentation}%
  \parbox{\indentedwidth}%
  {\raggedright
    \ignorespaces
    #1
  }
}

\def\signed#1{%
  \stopbreaks
  \noindent
  \par\vspace{6\medskipamount}%
  \hspace*{\longindentation}%
  \parbox{\indentedwidth}%
  {\raggedright
    \ignorespaces
    #1
  }
}

% Put the document macros back to the standard latex.tex values. These
% are redefined by letter.sty such that cross-references no longer work.
\def\document{\endgroup
  \@colht\textheight  \@colroom\textheight \vsize\textheight
   \columnwidth\textwidth \@clubpenalty\clubpenalty
   \if@twocolumn \advance\columnwidth -\columnsep
      \divide\columnwidth\tw@ \hsize\columnwidth \@firstcolumntrue
   \fi
  \hsize\columnwidth \linewidth\hsize
  \begingroup\@floatplacement\@dblfloatplacement
   \makeatletter\let\@writefile\@gobbletwo
   \@input{\jobname.aux}\endgroup
  \if@filesw \immediate\openout\@mainaux=\jobname.aux
    \immediate\write\@mainaux{\relax}\fi
  \csname process@table\endcsname
  \let\glb@currsize\@empty  %% Force \baselineskip initialisation.
  \def\do##1{\let ##1\@notprerr}%
  \@preamblecmds
  \let\do\noexpand
  \@normalsize\everypar{}\@noskipsecfalse}

\def\enddocument{\@checkend{document}\clearpage\begingroup
\if@filesw \immediate\closeout\@mainaux
\def\global\@namedef##1##2{}\def\newlabel{\@testdef r}%
\def\bibcite{\@testdef b}\@tempswafalse \makeatletter\input \jobname.aux
\if@tempswa \@@warning{Label(s) may have changed.  Rerun to get
cross-references right}\fi\fi\endgroup\deadcycles\z@\@@end}

\@fileswtrue

\def\endletter{\ifx\@empty\@cc\relax
  \else{%
    \par\noindent
    \setbox0=\hbox{\bf cc: }%
    \dimen0=\hsize\advance\dimen0 by -\wd0
    \box0\vtop{\noindent\hsize=\the\dimen0
      \ignorespaces\strut\@cc\strut
    }%
  }%
  \fi
  \stopletter\@@par\pagebreak\@@par
}

% Add definitions for the figure environment (missing from letter.sty).
\def\figurename{Figure}
\long\def\@makecaption#1#2{%
   \vskip 10\p@
   \setbox\@tempboxa\hbox{#1: #2}%
   \ifdim \wd\@tempboxa >\hsize
       #1: #2\par
     \else
       \hbox to\hsize{\hfil\box\@tempboxa\hfil}%
   \fi}

\newcounter{figure}
\def\thefigure{\@arabic\c@figure}

\def\fps@figure{tbp}
\def\ftype@figure{1}
\def\ext@figure{lof}
\def\fnum@figure{\figurename~\thefigure}
\def\figure{\@float{figure}}
\let\endfigure\end@float
\@namedef{figure*}{\@dblfloat{figure}}
\@namedef{endfigure*}{\end@dblfloat}
