%% linguex.sty version 1.0
%% Author: Wolfgang.Sternefeld@uni-tuebinhgen.de
%% filedate 06/25/97
\RequirePackage{xspace,cgloss4e}
%%%%%%%%%%%%%%%%%% FOOTNOTES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% to let \ex. know whether or not it is in a footnote 
\newif\if@noftnote\@noftnotetrue
\let\predefinedfootnotetext=\@footnotetext
\long\def\@footnotetext#1{\@noftnotefalse\predefinedfootnotetext{#1}%
         \@noftnotetrue}
%
%%%%%%%%%%%%% PREFIXING GRAMMATICALITY JUDGMENTS %%%%%%%%%%%%%% 
\def\Praefix#1{\makebox[0pt][r]{#1}}
%
%%%%%%%%%%%%%%% COUNTERS FOR LABELLING %%%%%%%%%%%%%%%%%%%%%%%%%%%
\@ifundefined{chapter}{\newcounter{ExNo}}{\newcounter{ExNo}[chapter]}
\newcounter{SubExNo}[\if@noftnote ExNo\else FnExNo\fi]
\newcounter{SubSubExNo}[SubExNo]
\newcounter{FnExNo}[footnote]
\newcounter{ABC}
\renewcommand{\theExNo}{(\arabic{ExNo})}
\renewcommand{\theFnExNo}{(\roman{FnExNo})}
\renewcommand{\theSubExNo}{\hbox{(\if@noftnote\arabic{ExNo}\else
   \roman{FnExNo}\fi-\alph{SubExNo})}}
\renewcommand{\theSubSubExNo}{%
   \hbox{(\if@noftnote\arabic{ExNo}\else\roman{FnExNo}\fi
          -\alph{SubExNo}-\if@noftnote\roman{SubSubExNo}%
                          \else\arabic{SubSubExNo}\fi)}}
%
%%%%%%%%%%%%%% LENGHTS FOR SPACING %%%%%%%%%%%%%%%%%%%%%%%%%%
\newlength{\currentlabel}
\newlength{\lessthanten}
\newlength{\lessthanhundred}
\newlength{\lessthanthousand}
\newlength{\addedlabelwidth}
\newlength{\addedleftmargin}
\newlength{\Extopsep}
\newlength{\Exindent}
\newlength{\SubExleftmargin}
\newlength{\SubSubExleftmargin}
\newlength{\Exlabelsep}
%
\newcommand{\ExTopSep}[1]{\setlength{\Extopsep}{#1}}
\newcommand{\ExIndent}[1]{\setlength{\Exindent}{#1}}
\newcommand{\SubExLeftMargin}[1]{\setlength{\SubExleftmargin}{#1}}
\newcommand{\SubSubExLeftMargin}[1]{\setlength{\SubSubExleftmargin}{#1}}
\newcommand{\ExLabelSep}[1]{\setlength{\Exlabelsep}{#1}}
%
\newcommand{\resetExdefaults}{%
   \setlength{\Exlabelsep}{1em}
   \setlength{\Extopsep}{.66\baselineskip}
   \setlength{\SubSubExleftmargin}{2.4em}
   \setlength{\SubExleftmargin}{2em}
   \setlength{\Exindent}{0pt}
}
\resetExdefaults
\settowidth{\currentlabel}{(11)}
%
%%%%%%%%%%%%%%%%%%% DEFINITION OF \ex. %%%%%%%%%%%%%%%%%%%%%%%%%% 
%
% redefinition of \@ifnextchar which does not ignore spaces
\def\my@ifnextchar#1#2#3{\let\reserved@d=#1%
  \def\reserved@a{#2}\def\reserved@b{#3}%
  \futurelet\@let@token\my@ifnch}
\def\my@ifnch{\ifx\@let@token\@sptoken%
     % \let\reserved@c\@xifnch
       \let\reserved@c\reserved@b%
   \else
    \ifx\@let@token\reserved@d%
      \let\reserved@c\reserved@a%
    \else
      \let\reserved@c\reserved@b%
    \fi
  \fi
  \reserved@c}
% redefinition of \@ifstar to stay compatible with amsgen.sty
\def\my@ifstar#1{\@ifnextchar *{\@firstoftwo{#1}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifembedded
\newcounter{ExDepth}
\newcounter{TempExDepth}
\setcounter{TempExDepth}{0}
\def\trivex.#1\par{\ifdim\lastskip=.66\baselineskip\vspace{-\baselineskip}\fi
     \begin{list}{}{\leftmargin=0pt%
     \labelwidth=0pt\labelsep=0pt\topsep=\Extopsep}%
     \item{}#1%
     \refstepcounter{ExDepth}\ExEnd}
\def\ex.#1\par{%
     \ifnum\theExDepth>0 \setcounter{TempExDepth}{\theExDepth}%
        \setcounter{ExDepth}{0}\embeddedtrue\fi%
     \ifdim\lastskip=.66\baselineskip\vspace{-\baselineskip}\fi%
     \refstepcounter{ExDepth}%
     \my@ifnextchar[{\OptArgEx}{\ifindex\expandafter\lookforwords\fi
     \NormalEx%
     \my@ifstar{\Praefix{*}}{}}#1%
     \addtocounter{ExDepth}{\theTempExDepth}\setcounter{TempExDepth}{0}%
     \embeddedfalse\ExEnd}
\newcommand{\NormalEx}{\ifnum\theExDepth >1 
     \typeout{Example numbering probably screwed up!}\fi
     \if@noftnote\refstepcounter{ExNo}%
        \Exformat[(\arabic{ExNo})]%
     \else
         \refstepcounter{FnExNo}\Exformat[(\roman{FnExNo})]%
     \fi}
\def\OptArgEx[#1]{\Exformat[#1]
     \my@ifstar{\Praefix{*}\ifindex\expandafter\lookforwords\fi}%
             {\ifindex\expandafter\lookforwords\fi}}
\newcommand{\ExEnd}{%
     \ifnum\theExDepth=0\relax\else%
          \end{list}\addtocounter{ExDepth}{-1}\ExEnd\fi}%
\newcommand{\listdecl}{\settowidth{\lessthanten}{(11)}%
   \settowidth{\lessthanhundred}{(111)}%
   \settowidth{\lessthanthousand}{(1111)}%
   \parsep=0pt%
   \itemsep=0pt% 
   \partopsep=0pt%
   \labelsep=0pt%
    \setlength{\addedleftmargin}{0pt}\setlength{\addedlabelwidth}{0pt}%
    \ifdim\currentlabel<\lessthanthousand%
      \addtolength{\addedleftmargin}{\Exlabelsep}%
      \addtolength{\addedlabelwidth}{\Exlabelsep}%
    \fi
   \ifdim\currentlabel<\lessthanten%
      \setlength{\leftmargin}{\lessthanten}%
        \else%
        \ifdim\currentlabel<\lessthanhundred%
          \setlength{\leftmargin}{\lessthanhundred}%
            \else 
              \ifdim\currentlabel<\lessthanthousand%
                \setlength{\leftmargin}{\lessthanthousand}%
   \fi\fi\fi%
   \setlength{\labelwidth}{\leftmargin}%
   \ifembedded\relax\else\addtolength{\addedleftmargin}{\Exindent}\fi%
   \addtolength{\leftmargin}{\addedleftmargin}%
   \addtolength{\labelwidth}{\addedlabelwidth}%
}
\def\Exformat[#1]{%
   \settowidth{\currentlabel}{#1}%
   \begin{list}{\makebox[\labelwidth][l]{#1}}{%
      \setlength{\leftmargin}{\currentlabel}%
      \listdecl%
      \if@noftnote\relax\else\addtolength{\topsep}{-.5\topsep}\fi
      \ifembedded\topsep=-\parsep\else\topsep=\Extopsep\fi
   }%
   \ifindex\firstwordtrue\fi\item{}}
%
%%%%%%%%%%%%%%%% SUBLISTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\a.{\ifindex\firstwordtrue\fi
        \ifnum\theExDepth=0 \trivex.\fi
     \refstepcounter{ExDepth}% 
     \begin{list}{\makebox[\labelwidth][l]{%
         \ifnum\theExDepth=1 \alph{ABC}.%
          \else
           \ifnum\theExDepth=2 \alph{SubExNo}.%
            \else
             \ifnum\theExDepth=3%
             \if@noftnote(\roman{SubSubExNo})\else\arabic{SubSubExNo})\fi
         \fi\fi\fi}}%
    {\labelsep=0em%
     \topsep=-\parsep%
     \itemsep=0pt%
     \ifnum\theExDepth=1 %
       \usecounter{ABC}%
       \settowidth{\currentlabel}{\theExNo}%
       \listdecl%   
     \else
      \ifnum\theExDepth=2 %
         \usecounter{SubExNo}%
         \leftmargin=\SubExleftmargin%
         \else
            \ifnum\theExDepth=3 \usecounter{SubSubExNo}\fi
         \leftmargin=\SubSubExleftmargin%
         \fi
    \labelwidth=\leftmargin%
    \fi}%
    \b.}
%
%%%%%%%%%%%%%%%%% ITEMS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\newExitem[#1]{\item[#1\hfill]\my@ifstar{\Praefix{*}}{}}
\let\oldb=\b
\renewcommand{\b}{\@ifnextchar.{\newb}{\oldb}}
\def\newb.{\ifindex\firstwordtrue\fi
        \my@ifnextchar[{\newExitem}{\item\my@ifstar{\Praefix{*}}{}}} 
\let\oldc=\c
\renewcommand{\c}{\@ifnextchar.{\b}{\oldc}}
\let\oldd=\d
\renewcommand{\d}{\@ifnextchar.{\b}{\oldd}}
\let\e=\b
\let\f=\b
\def\z.{\end{list}\addtocounter{ExDepth}{-1}}
%
%%%%%%%%%%%%%%%%%%%% INDEXED BRACKETS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifindex
\newif\iffirstword
\def\I#1#2 {#1\labelBr#2 }
\def\exi.#1\par{\indextrue\ex.#1 | blabla | \indexfalse\par} 
\def\finish|#1|{\ExEnd}
\newcommand{\adhoc}{\my@ifstar{\Praefix{*}\firstwordfalse\checkforbr}{\firstwordfalse}}
\def\doaword#1 {% 
       \iffirstword\expandafter\adhoc\fi%
       #1
       \ifgloss\expandafter\checkforbrorstar\else\expandafter\lookforwords\fi}
\newcommand{\checkstar}{\my@ifstar{\Praefix{*}\firstwordfalse\checkforbr}{\firstwordfalse}}
\newcommand{\checkforbrorstar}{\my@ifstar{\Praefix{*}\checkforbr}{\checkforbr}}
\newcommand{\lookforwords}{\@ifnextchar]{\firstwordfalse\aber}%
                          {\@ifnextchar[{\firstwordfalse\aber}%
           {\@ifnextchar|{\firstwordfalse\finish}{\doaword}}}}
\def\labelBr#1 {\mbox{$\hspace{.1em}_{\mbox{\scriptsize\rm#1}}$} }
\newcommand{\jetzt}{\my@ifnextchar[{\aber}{\my@ifnextchar]{\aber}{\schnell}}}
\def\aber#1#2 {#1\jetzt#2 }
\def\schnell#1 {\labelBr#1 \lookforwords}
\newcommand{\checkforbr}{\@ifnextchar[{\recurseonbr}{\@ifnextchar]{\recurseonbr}{}}}
\newcommand{\recurseonbr}{\my@ifnextchar[{\stripoffbr}{\my@ifnextchar]{\stripoffbr}%
     {\labelBr}}}
\def\stripoffbr#1#2 {#1\recurseonbr#2 }
%
%%%%%%%%%%%%%%%%%%%% GLOSSES %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifgloss
\def\getwords(#1,#2)#3 #4\\% #1=linebox, #2=\each, #3=1st word, #4=remainder
   {\setbox#1=\vbox{\hbox{#2\strut\ifindex\expandafter\checkforbr\fi#3 }%
                    \unvbox#1}%
    \def\more{#4}%
    \ifx\more\empty\let\more=\donewords
    \else\let\more=\getwords 
    \fi
    \more(#1,#2)#4\\%
   }
\def\gl@stop{{\hskip -\glossglue}\unhbox\gline\end{flushleft}\glossfalse%
         \ifindex\expandafter\lookforwords\fi {} }
\let\oldgll=\gll
\renewcommand{\gll}{\glosstrue\my@ifstar{\oldgll\Praefix{*}}{\oldgll}}
\def\exg.{\my@ifnextchar[{\newglossex}{\ex.\gll}}
\def\newglossex[#1]{\ex.[#1]\gll}
\def\ag.{\my@ifnextchar[{\newglossa}{\a.\gll}}
\def\newglossa[#1]{\a.[#1]\gll}
\def\newglossitem[#1]{\item[#1\hfill]\gll}
\def\bg.{\my@ifnextchar[{\newglossitem}{\item\gll}}
\def\cg.{\bg.}
\def\dg.{\bg.}
\def\eg.{\bg.}
\def\fg.{\bg.}
%
%%%%%%%%%%%%%%%%%%%% GLOSSES WITH INDEXED BRACKETS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\exig.{\my@ifnextchar[{\newglossexi}{\exi.\gll}}
\def\newglossexi[#1]{\exi.[#1]\gll}
\def\exgi.{\my@ifnextchar[{\newglossexi}{\exi.\gll}}
\def\newglossexi[#1]{\exi.[#1]\gll}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CROSS REFERENCES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{tmpaEx}
\newif\ifNoFnRef
\def\copyExNo{\strut\ifNoFnRef\setcounter{tmpaEx}{\value{ExNo}}\def\thetmpaEx{\arabic{tmpaEx}}%
           \else
           \setcounter{tmpaEx}{\value{FnExNo}}\def\thetmpaEx{\roman{tmpaEx}}\fi}
\def\complexExNo[#1]{\hbox{(\thetmpaEx-#1)}}
\def\printExNo{\@ifnextchar[{\complexExNo}{(\thetmpaEx)\xspace}}
\def\Next{\if@noftnote\NoFnReftrue\else\noFnReffalse\fi
          \copyExNo\refstepcounter{tmpaEx}\printExNo}
\def\NNext{\if@noftnote\NoFnReftrue\else\noFnReffalse\fi
          \copyExNo\addtocounter{tmpaEx}{2}\printExNo}
\def\TextNext{\NoFnReftrue\copyExNo\refstepcounter{tmpaEx}\printExNo}
\def\Last{\if@noftnote\NoFnReftrue\else
               \ifnum\arabic{FnExNo}=0 \NoFnReftrue\else\NoFnReffalse\fi
          \fi
          \copyExNo\printExNo}
\def\LLast{\if@noftnote\NoFnReftrue\copyExNo\addtocounter{tmpaEx}{-1}\else
               \ifnum\arabic{FnExNo}=0 \NoFnReftrue\copyExNo\addtocounter{tmpaEx}{-1}\else
                     \ifnum\arabic{FnExNo}=1 \NoFnReftrue\copyExNo\else
                        \NoFnReffalse\copyExNo\addtocounter{tmpaEx}{-1}\fi\fi\fi
             \printExNo}
%%%%%%%%%%%%%%%%%%%%%%%%%%% END %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%