% Glossing code.
% Copied and adapted from covingtn.tex
% Send bugs and suggestions to jamtrup@att.net
% Thanks to Marcel R. van der Goot for permission to reproduce code.
% The following TeX code is adapted, with permission, from:
% gloss.tex: Macros for vertically aligning words in consecutive sentences.
% Version: 1.0  release: 26 November 1990
% Copyright (c) 1991 Marcel R. van der Goot (marcel@cs.caltech.edu).
% Original gloss.tex and gloss.doc are available from
% csvax.cs.caltech.edu [131.215.131.131] in pub/tex
% and many other anonymous ftp archives.

\newbox\lineone % boxes with words from first line
\newbox\linetwo
\newbox\linethree
\newbox\wordone % a word from the first line (hbox)
\newbox\wordtwo
\newbox\wordthree
\newbox\gline % the constructed double line (hbox)
\newskip\glossglue % extra glue between glossed pairs or triples
% \glossglue = 0pt plus 2pt minus 1pt % allow stretch/shrink between words
\glossglue = 5pt plus 2pt minus 1pt % allow stretch/shrink between words
\newif\ifnotdone

\let\eachwordone=\rm
\let\eachwordtwo=\rm
\let\eachwordthree=\rm


% Introduces 2-line text-and-gloss.
\def\gll{%
  \begin{flushleft}%
    \vskip\baselineskip%      % Added 1992 Nov 8 to force single spacing
    \def\baselinestretch{1}%
    \ifx\@currsize\normalsize\@normalsize\else\@normalsize\fi%
    \vskip-\baselineskip%
    \bgroup
    \catcode`\^^M=12
    \twosent
}

% Introduces 3-line text-and-gloss.
\def\glll{%
  \begin{flushleft}
    \vskip\baselineskip%      % Added 1992 Nov 8 to force single spacing
    \def\baselinestretch{1}%
    \ifx\@currsize\normalsize\@normalsize\else\@normalsize\fi%
    \vskip-\baselineskip%
    \bgroup
    \catcode`\^^M=12
    \threesent
}


% Translation in a gloss
% Required even if there is no translation
\def\glt{{\hskip -\glossglue}\unhbox\gline\smallskip\newline}

% Alternative to glt if no translation 
\def\gln{{\hskip -\glossglue}\unhbox\gline\hfill}

% End the glossing environment
\def\glend{\end{flushleft}}

% #1 = \each, #2 = line box, #3 = word box
\def\lastword#1#2#3{%
  \setbox#2=\vbox{\unvbox#2%
    \global\setbox#3=\lastbox
  }%
  % extra space following \strut in case #1 needs a space
  \ifvoid#3\global\setbox#3=\hbox{#1\strut{} }\fi
}

\def\testdone{%
  \ifdim\ht\lineone=0pt
    \ifdim\ht\linetwo=0pt \notdonefalse % tricky space after pt
    \else\notdonetrue
    \fi
  \else\notdonetrue
  \fi
}


{\catcode`\^^M=12 \endlinechar=-1 % 12 = other

  % #1=linebox, #2=\each, #3=1st word, #4=remainder
  \gdef\getwords(#1,#2)#3 #4^^M%
  {\setbox#1=\vbox{\hbox{#2\strut#3 }% adds space
      \unvbox#1%
      }%
    \def\more{#4}%
    \ifx\more\empty\let\more=\donewords
      \else\let\more=\getwords
    \fi
    \more(#1,#2)#4^^M%
  }

\gdef\donewords(#1,#2)^^M{}%

% #1 = first line, #2 = second line
\gdef\twosent#1^^M#2^^M{%
  \getwords(\lineone,\eachwordone)#1 ^^M%
  \getwords(\linetwo,\eachwordtwo)#2 ^^M%
  \loop\lastword{\eachwordone}{\lineone}{\wordone}%
    \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
    \global\setbox\gline=\hbox{\unhbox\gline
      \hskip\glossglue
      \vtop{\box\wordone   % vtop was vbox
        \nointerlineskip
        \box\wordtwo
      }%
    }%
    \testdone
    \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
}


% #1 = first line, #2 = second line, #3 = third
\gdef\threesent#1^^M#2^^M#3^^M{
  \getwords(\lineone,\eachwordone)#1 ^^M%
  \getwords(\linetwo,\eachwordtwo)#2 ^^M%
  \getwords(\linethree,\eachwordthree)#3 ^^M%
  \loop\lastword{\eachwordone}{\lineone}{\wordone}%
    \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
    \lastword{\eachwordthree}{\linethree}{\wordthree}%
    \global\setbox\gline=\hbox{\unhbox\gline
      \hskip\glossglue
      \vtop{\box\wordone   % vtop was vbox
        \nointerlineskip
        \box\wordtwo
        \nointerlineskip
        \box\wordthree
      }%
    }%
    \testdone
    \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
}

} % restore \catcode`\^^M




% Example environment. Uses covington stuff.
\makeatletter
\newlength{\txtminusexam}
\setlength{\txtminusexam}{\textwidth}
\addtolength{\txtminusexam}{-.43in}
\newenvironment{exam}{%
\mbox{}\par\noindent
\addtocounter{equation}{1}
\begin{minipage}[t]{.4in}(\theequation)\end{minipage}%
\begin{minipage}[t]{\txtminusexam}\vspace*{-\baselineskip}}%
{\end{minipage}\par\vskip\baselineskip\noindent}

