%% simplex.sty — Linguistic example environments for LaTeX
%%
%% Copyright (C) Kyle Gorman <kylebgorman@gmail.com>
%%
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3 of this license
%% or (at your option) any later version. The latest version of this
%% license is in:
%%
%%   http://www.latex-project.org/lppl.txt
%%
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Kyle Gorman.
%%
%% This work consists of the files simplex.sty and simplex.tex.
%%
%% Portions of this file were adapted from:
%%   gloss.tex (v1.0, 1990-11-26)
%%   Copyright (C) 1991 Marcel R. van der Goot (marcel@cs.caltech.edu)
%%
%% Additional adaptations were derived from covington.sty
%%   by Michael Covington and others.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{simplex}[v1.0 Linguistic example environments]

\RequirePackage{xparse}

%% ---------------------------------------------------------------------------
%% PART 1: Numbered example environments
%% ---------------------------------------------------------------------------

\newcounter{exampleno}
\setcounter{exampleno}{0}

% unlabeledexample — a numbered example with no label.
\newenvironment{unlabeledexample}%
  {\begin{flushleft}%
   \refstepcounter{exampleno}%
   \noindent(\arabic{exampleno})~~\ignorespaces\noindent\ignorespaces}%
  {\end{flushleft}\ignorespacesafterend}

% shortexample — a numbered example with an optional same-line label.
\NewDocumentEnvironment{shortexample}{O{}}%
  {\begin{flushleft}%
   \refstepcounter{exampleno}%
   \noindent(\arabic{exampleno})~~#1:~~\noindent\ignorespaces}%
  {\end{flushleft}\ignorespacesafterend}

% example — a numbered example with an optional label on its own line.
\NewDocumentEnvironment{example}{O{}}%
  {\begin{flushleft}%
   \refstepcounter{exampleno}%
   \noindent(\arabic{exampleno})~~#1:\vskip .5\baselineskip%
   \noindent\ignorespaces}%
  {\end{flushleft}\ignorespacesafterend}


%% ---------------------------------------------------------------------------
%% PART 2: Interlinear gloss macros
%%         Adapted from covington.sty and gloss.tex (van der Goot, 1991)
%% ---------------------------------------------------------------------------

% Font selection compatibility shim.
% Thanks to Hans-Peter Kolb for the original macro (via covington.sty).
\@ifundefined{new@fontshape}%
  {\def\@selfnt{\ifx\@currsize\normalsize\@normalsize\else\@currsize\fi}}%
  {\def\@selfnt{\selectfont}}

% \gll — 2-line interlinear gloss.
\def\gll%
  {\def\baselinestretch{1}%
   \@selfnt%
   \bgroup
   \catcode`\^^M=12
   \twosent
  }

% \glll — 3-line interlinear gloss.
\def\glll%
  {\def\baselinestretch{1}%
   \@selfnt%
   \bgroup
   \catcode`\^^M=12
   \threesent
  }

% \glt — Free translation line. Required to close a gloss block.
\def\glt{\hskip-\glossglue\unhbox\gline\smallskip\newline\hskip 10em}

% \gln — Like \glt but for use when no translation text follows.
\def\gln{\hskip-\glossglue\unhbox\gline}

% \glend — Terminates the gloss environment (currently a no-op).
\def\glend{}

%% Internal box registers

\newbox\lineone
\newbox\linetwo
\newbox\linethree
\newbox\wordone
\newbox\wordtwo
\newbox\wordthree
\newbox\gline

\newskip\glossglue
\glossglue = 5pt plus 2pt minus 1pt

\newif\ifnotdone

% Per-line font hooks. Override to change fonts, e.g.:
%   \let\eachwordone=\textit
\let\eachwordone=\rm
\let\eachwordtwo=\rm
\let\eachwordthree=\rm

\def\lastword#1#2#3%
  {\setbox#2=\vbox{\unvbox#2%
                   \global\setbox#3=\lastbox
                  }%
   \ifvoid#3\global\setbox#3=\hbox{#1\strut{} }\fi
  }

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

\begingroup
  \catcode`\^^M=12 \endlinechar=-1 % <--- Add a space and % here
  \gdef\getwords(#1,#2)#3 #4^^M%
    {\setbox#1=\vbox{\hbox{#2\strut#3 }%
                     \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{}%
  \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
                                           \nointerlineskip
                                           \box\wordtwo
                                          }%
                                    }%
          \testdone
          \ifnotdone
     \repeat
     \egroup
    }
  \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
                                           \nointerlineskip
                                           \box\wordtwo
                                           \nointerlineskip
                                           \box\wordthree
                                          }%
                                    }%
          \testdone
          \ifnotdone
     \repeat
     \egroup
    }
\endgroup

\endinput
