% gb4e-emulate.sty --
% emulation of gb4e using enumitem for increased flexibility of formatting
% Copyright 2010 by Alan Munn <amunn@msu.edu>
% based on code in gb4e.sty by
% Craig Thiersch, Hans-Peter Kolb and Alexis Dimitriadis
%
%
% This package may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or 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 package has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this package is Alan Munn.
%
% This package consists of the file gb4e-emulate.sty
%
% Version 1.0 2010/07/31
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{gb4e-emulate}[2010/07/31 gb4e-emulate Package version 1.0]
\RequirePackage{enumitem}
\RequirePackage{cgloss4e}
% Set up equivalent lists
\newlist{exe}{enumerate}{1}
\setlist[exe]{
  resume=exe@series,
  label={(\arabic*)},
  widest=888,
  align=left,
  topsep=7\p@ plus 2\p@ minus 3\p@,
  itemsep=3\p@ plus 2\p@,
  ref=\arabic*,
  leftmargin=*}
\newlist{xlist}{enumerate}{3}
\setlist[xlist]{
  topsep=1.5\p@ plus \p@,
  itemsep=1.5\p@ plus \p@,
  labelsep=*,
  parsep=\p@ plus .5\p@ minus .5\p@,
  itemindent=0em,
  align=left,
  leftmargin=*}
\setlist[xlist,1]{
  label={\alph*.},
  ref={\arabic{exei}\alph*},
  widest=a.}
\setlist[xlist,2]{
  label={\roman*.},
  ref={\thexlisti.\roman*},
  widest=iv.}
\setlist[xlist,3]{
  label={\arabic*.},
  ref={\thexlistii.\arabic*},
  widest=9.}
% emulate specific lists
\newenvironment{xlisti}{%
  \begin{xlist}[label={\roman*.},
                ref={\arabic{exei}\roman*},
                widest=iv.]}{\end{xlist}}
\newenvironment{xlista}{%
  \begin{xlist}[label={\alph*.},
                ref={\arabic{exei}\alph*},
                widest=a.]}{\end{xlist}}
\newenvironment{xlistn}{%
  \begin{xlist}[label={\arabic*.},
                ref={\arabic{exei}\arabic*},
                widest=9.]}{\end{xlist}}
\newenvironment{xlistI}{%
  \begin{xlist}[label={\Roman*.},
                ref={\arabic{exei}\Roman*},
                widest=IV.]}{\end{xlist}}
\newenvironment{xlistA}{%
  \begin{xlist}[label={\Alph*.},
                ref={\arabic{exei}\Alph*},
                widest=M.]}{\end{xlist}}

%%% subeex macro without vertical spacing code from gb4e
\def\@subex#1#2{%
  \settowidth{\labelwidth}{#1}%
  \itemindent\z@\labelsep#2%
  \parsep\p@ plus .5\p@ minus .5\p@%
  \leftmargin\labelwidth%
  \advance\leftmargin#2\relax
}
%%% the example-items from gb4e
\def\ex{\@ifnextchar [{\@ex}{\item}}
\def\@ex[#1]#2{\item\@exj[#1]{#2}}
\def\@exj[#1]#2{\@exjbg{#1} #2 \end{list}}
\def\exi#1{\item[#1]\@ifnextchar [{\@exj}{}}
\def\judgewidth#1{\def\@jwidth{#1}}
\judgewidth{?}
\def\@exjbg#1{\begin{list}{#1}{\@subex{\@jwidth}{0ex}}\item}
\def\exr#1{\exi{{(\ref{#1})}}}
\def\exp#1{\exi{{(\ref{#1}$'$)}}}
\def\sn{\exi{}}

%%%%%%%%%%% PRIMEBARS

\let\prmbrs=0
\def\primebars{\let\prmbrs=1}
\def\obar#1{\ifmmode#1^{0}\else#1$^{0}$\fi}  %% FIX
\def\mbar#1{\ifmmode#1^{\mathrm{max}}\else#1$^{\mathrm{max}}$\fi}
\def\ibar#1{\ifx\prmbrs0%
                 \ifmmode\overline{\mathrm{#1}}\else$\overline{\mbox{#1}}$\fi%
            \else\ifmmode#1^{'}\else#1$^{'}$\fi\fi}
\def\iibar#1{\ifx\prmbrs0%
                  \ifmmode\overline{\overline{\mathrm{#1}}}%
                  \else$\overline{\overline{\mbox{#1}}}$\fi%
             \else #1P\fi}

%%%%%%%%%%% AUTOMATH

% this allows _ to be used in horizontal mode (from J.Frampton):
% Reimplemented to allow this feature to be turned on and off.
% The new version also combines sub- and superscripts properly.
% -Alexis

% \automath     Make _ and ^ work outside math mode
% \noautomath   Restore normal sub/superscript behavior

{ % Temporarily change catcodes
  \catcode`\_=\active
  \catcode`\^=\active

  \global\def\automath{%
    \catcode`\_=\active
    \catcode`\^=\active
    \def_##1{\gb@ifnextchar^{\automath@two_{##1}}{\ensuremath{\sb{##1}}}}%
    \def^##1{\gb@ifnextchar_{\automath@two^{##1}}{\ensuremath{\sp{##1}}}}}
}
\def\automath@two#1#2#3#4{\ensuremath{#1{#2}\relax #3{#4}}}
% Restore default catcodes for ^, _
\def\noautomath{\catcode`\_=8 \catcode`\^=7 }

% The original \@ifnextchar discards spaces when looking for the next
% ``character''. This variant accepts any token.
\long\def\gb@ifnextchar#1#2#3{%
  \let\reserved@d=#1%
  \def\reserved@a{#2}%
  \def\reserved@b{#3}%
  \futurelet\@let@token\@gbifnch}
\def\@gbifnch{%
    \ifx\@let@token\reserved@d
      \let\reserved@c\reserved@a
    \else
      \let\reserved@c\reserved@b
    \fi
  \reserved@c}

\automath % On by default, for backward compatibility

%%%%%%%%%
\endinput
