% gloss.sty: macros to create glossed sentences
%
% BASED ON gloss.tex: csvax.cs.caltech.edu [131.215.131.131] in pub/tex
%
% copyright (c) 1990 Marcel R. van der Goot
%	You can use these macros to typeset your own documents,
%	or you can use them as basis for your own macros. In the
%	latter case, you must (1) give proper credit, and
%	(2) document the usage of your macros/changes (if usage
%	of your macros is not worth documenting, they must not
%	be worth using).
%
% USAGE:
%   See the file gloss.doc and below.
% 
% Caltech, Pasadena  ---  Marcel van der Goot
%			  marcel@vlsi.cs.caltech.edu
%			    Caltech 256--80
%			    Pasadena, CA 91125
%			    (818) 356--4603
%

% update history (differences with next version):
% version 6 Nov 1990: Could not easily typeset a '+', handled
%	linespacing incorrectly.
% version 7 Nov 1990: Used the names \fontone and \fonttwo instead of
%	\eachwordone and \eachwordtwo.
% version 8 Nov 1990: Same as this, but with less readable documentation.
% version 9 Nov 1990: This one.

% Made into a latex .sty file and expanded by Chris Manning
%
% INSTRUCTIONS (in addition to those given in gloss.doc)
%
% If you want automatic example numbering, use this with lingmacros.sty.
%
% Glossed text is entered roughly as described in gloss.doc but we provide
% 4 commands: \gloss, \dgloss, \mgloss, \mdgloss, which give two orthoganal
% options:
%	d: Double gloss: three lines of lined up text instead of two.
%	m: Line up morphemes. Put spaces in the input at the end of morphs
%	   and these morphs will be lined up without any spaces inserted
%	   between them.  To get spaces between two things, you must insert
%	   one explicitly with '~' or '\ ' or similar.
% The default text style has been changed to \rm for all lines of the gloss.
% To change these defaults you set the values of \eachwordtop, \eachwordmid
% and \eachwordbot.
% Additionally, you can get the top line in italics with the command 
% \italictext and you can get the bottom line in italics by enclosing the
% call to gloss within a {\nllt  ... } command.
%
% \bglossed: put all of the alignment and translation into a vtop'ed box,
% which is useful when you want to do things like squeezing two
% alignments onto a single line.
%
% The macros now just put the box, and you have to use
% calls to lingmacros explicitly for sentence numbering (much better).  Oct 93.
% Glossed text is then displayed with the following commands:
%	\glossed{Translation}
%	\notglossed
%	\Glossed{Translation}
%	\notGlossed
% The first two only make one line of text (overfull hbox if too long).
% The latter two beautifully make multiple lines (but are slower).
% The translation can be any number of lines (either wrapping as usual, or
% explicitly ended with \\ etc.)
% The \not pair are for when there is no translation.


\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
\glossglue = 0pt plus 2pt minus 1pt % allow stretch/shrink between words
\newdimen\glosssep % extra space before translation
\glosssep=0pt
\newif\ifnotdone

\let\eachwordtop=\rm % roman is a more useful default
\let\eachwordmid=\rm
\let\eachwordbot=\rm

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

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

{\catcode`\^^M=12 % 12 = other
\gdef\getwords(#1,#2,#3)#4 #5^^M% #1=Mp #2=linebox #3=\each #4=1st w, #5=rem
{\setbox#2=\vbox{\hbox{#3\strut#4\if W#1 \fi}% adds space if words
% try: {\setbox#2=\vbox{\hbox{#3\strut#4\if W#1\penalty-100\ \fi}% adds space
		    \unvbox#2%
		   }%
    \def\more{#5}%
    \ifx\more\empty\let\more=\donewords %
    \else\let\more=\getwords %
    \fi %
    \more(#1,#2,#3)#5^^M%
   } %
%
\gdef\donewords(#1,#2,#3)^^M{}%
%
\gdef\twosent#1 #2^^M#3^^M% #1 = morphp #2 = first line, #3 = second line
   {\getwords(#1,\lineone,\eachwordtop)#2 ^^M%
    \getwords(#1,\linetwo,\eachwordbot)#3 ^^M%
    \loop\lastword{\eachwordtop}{\lineone}{\wordone}{#1}%
         \lastword{\eachwordbot}{\linetwo}{\wordtwo}{#1}%
	 \global\setbox\gline=\hbox{\unhbox\gline %
				    \hskip\glossglue %
				    \vtop{\box\wordone %
					  \nointerlineskip %
				          \box\wordtwo %
				         }%
			   	   }%
	 \testdone %
	 \ifnotdone %
    \repeat %
    \egroup % matches \bgroup in \gloss
   } %
\gdef\threesent#1 #2^^M#3^^M#4^^M% #1=morphp #2 = 1st line #3 = 2nd #4 = 3rd
   {\getwords(#1,\lineone,\eachwordtop)#2 ^^M%
    \getwords(#1,\linetwo,\eachwordmid)#3 ^^M%
    \getwords(#1,\linethree,\eachwordbot)#4 ^^M%
    \loop\lastword{\eachwordtop}{\lineone}{\wordone}{#1}%
         \lastword{\eachwordmid}{\linetwo}{\wordtwo}{#1}%
	 \lastword{\eachwordbot}{\linethree}{\wordthree}{#1}%
	 \global\setbox\gline=\hbox{\unhbox\gline %
				    \hskip\glossglue %
				    \vtop{\box\wordone %
					  \nointerlineskip %
				          \box\wordtwo %
					  \nointerlineskip %
					  \box\wordthree %
				         }%
			   	   }%
	 \testdone %
	 \ifnotdone %
    \repeat %
    \egroup % matches \bgroup in \gloss
   } %
} % restore \catcode`\^^M

% for use in footnotes
\def\getwordsa(#1,#2,#3)#4 #5\\% #1=Mp #2=linebox #3=\each #4=1st w, #5=rem
{\setbox#2=\vbox{\hbox{#3\strut#4\if W#1 \fi}% adds space if words
% try: {\setbox#2=\vbox{\hbox{#3\strut#4\if W#1\penalty-100\ \fi}% adds space}}
		    \unvbox#2%
		   }%
    \def\more{#5}%
    \ifx\more\empty\let\more=\donewordsa %
    \else\let\more=\getwordsa %
    \fi %
    \more(#1,#2,#3)#5\\%
} %
%
\def\donewordsa(#1,#2,#3)\\{}%
%
\def\twosenta#1 #2\\ #3\\{% #1 = morphp #2 = first line, #3 = second line
    \getwordsa(#1,\lineone,\eachwordtop)#2 \\%
    \getwordsa(#1,\linetwo,\eachwordbot)#3 \\%
    \loop\lastword{\eachwordtop}{\lineone}{\wordone}{#1}%
         \lastword{\eachwordbot}{\linetwo}{\wordtwo}{#1}%
	 \global\setbox\gline=\hbox{\unhbox\gline %
				    \hskip\glossglue %
				    \vtop{\box\wordone %
					  \nointerlineskip %
				          \box\wordtwo %
				         }%
			   	   }%
	 \testdone %
	 \ifnotdone %
    \repeat %
   }%

\def\lgloss
   {\twosenta W
   }

\def\lmgloss
   {\twosenta M
   }

\def\gloss
   {\bgroup
    \catcode`\^^M=12
    \twosent W
   }

\def\dgloss
   {\bgroup
    \catcode`\^^M=12
    \threesent W
   }

\def\mgloss
   {\bgroup
    \catcode`\^^M=12
    \twosent M
   }

\def\mdgloss
   {\bgroup
    \catcode`\^^M=12
    \threesent M
   }


\def\nllt{\let\eachwordbot=\it}
\def\italictext{\let\eachwordtop=\it}

\def\unotglossed{\leavevmode\box\gline}
\def\unotGlossed{\raggedright\leavevmode\unhbox\gline}
\def\uglossed#1{\leavevmode\box\gline\nopagebreak
\\*[\glosssep]\nopagebreak{#1}}
\def\bglossed#1{\leavevmode\hbox{\vtop{\box\gline
%\\*[\glosssep]
\hbox{#1}}}}
\def\uGlossed#1{\raggedright\leavevmode\unhbox\gline\nopagebreak
\\*[\glosssep]\nopagebreak{#1}}
%\def\uglossed#1{\leavevmode\box\gline\\\kern\glosssep{#1}}
%\def\uGlossed#1{\raggedright\leavevmode\unhbox\gline\\\kern\glosssep{#1}}

\let\glossed=\uglossed
\let\Glossed=\uGlossed
\let\notglossed=\unotglossed
\let\notGlossed=\unotGlossed
%
% Old versions disbanded
% \def\glossed{\@ifnextchar[{\@glossed}% %]
% {\refstepcounter{enums}\@glossed[(\theenums)]}}
% 
% \long\def\@glossed[#1]#2{\begin{list}{}{}
% \item[{#1}] \leavevmode\box\gline%
% \newline{#2}
% \end{list}}
% 
% \def\Glossed{\@ifnextchar[{\@Glossed}% %]
% {\refstepcounter{enums}\@Glossed[(\theenums)]}}
% 
% \long\def\@Glossed[#1]#2{\begin{list}{}{}
% \item[{#1}] \leavevmode\unhbox\gline%
% \newline{#2}
% \end{list}}
% 
% \def\notglossed{\@ifnextchar[{\@notglossed}% %]
% {\refstepcounter{enums}\@notglossed[(\theenums)]}}
% 
% \long\def\@notglossed[#1]{\begin{list}{}{}
% \item[{#1}] \leavevmode\box\gline%
% \end{list}}
% 
% \def\notGlossed{\@ifnextchar[{\@Glossed}% %]
% {\refstepcounter{enums}\@notGlossed[(\theenums)]}}
% 
% \long\def\@notGlossed[#1]{\begin{list}{}{}
% \item[{#1}] \leavevmode\unhbox\gline%
% \end{list}}
