\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{biocon}[2001/08/18]


\RequirePackage{keyval}
\RequirePackage{ifthen}
\newcommand{\curr@ntid}{}
\newcounter{Q@D@counter}
\newcommand{\T@mpFirst}{}
\newcommand{\T@mpRest}{}
\newcommand{\Gl@balF@llStyle}{}
\newcommand{\Gl@bal@bbrStyle}{}
\newcommand{\Gl@balPE@llStyle}{}
\newcommand{\Gl@balFE@llStyle}{}
\newcommand{\Gl@balAE@llStyle}{}
\newcommand{\Gl@balBE@llStyle}{}
\newcommand{\newplant}[2]{\n@wsp@cies{P}{#1}{#2}}
\newcommand{\newfungus}[2]{\n@wsp@cies{F}{#1}{#2}}
\newcommand{\newanimal}[2]{\n@wsp@cies{A}{#1}{#2}}
\newcommand{\newbact}[2]{\n@wsp@cies{B}{#1}{#2}}

\newcommand{\plantlike}[2][]
  {\n@wsp@cies{}{Q@D}{#2}\plant[#1]{Q@D}\expandafter\Q@DCleanup\Q@DCleanList+}
\newcommand{\funguslike}[2][]
  {\n@wsp@cies{}{Q@D}{#2}\fungus[#1]{Q@D}\expandafter\Q@DCleanup\Q@DCleanList+}
\newcommand{\animallike}[2][]
  {\n@wsp@cies{}{Q@D}{#2}\animal[#1]{Q@D}\expandafter\Q@DCleanup\Q@DCleanList+}
\newcommand{\bactlike}[2][]
  {\n@wsp@cies{}{Q@D}{#2}\bact[#1]{Q@D}\expandafter\Q@DCleanup\Q@DCleanList+}
\newcommand{\newtaxastyle}[2]{\expandafter\def\csname Sp@cies#1\endcsname{#2}}
\newcommand{\defaultplante}[1]
  {\renewcommand{\Gl@balPE@llStyle}{\csname Sp@cies#1\endcsname}}
\newcommand{\defaultfunguse}[1]
  {\renewcommand{\Gl@balFE@llStyle}{\csname Sp@cies#1\endcsname}}
\newcommand{\defaultanimale}[1]
  {\renewcommand{\Gl@balAE@llStyle}{\csname Sp@cies#1\endcsname}}
\newcommand{\defaultbacte}[1]
  {\renewcommand{\Gl@balBE@llStyle}{\csname Sp@cies#1\endcsname}}
\newcommand{\defaultfull}[1]
  {\renewcommand{\Gl@balF@llStyle}{\csname Sp@cies#1\endcsname}}
\newcommand{\defaultabbr}[1]
  {\renewcommand{\Gl@bal@bbrStyle}{\csname Sp@cies#1\endcsname}}
\newcommand{\plant}[2][]{\sh@wsp@cies{P}{#2}{#1}}
\newcommand{\fungus}[2][]{\sh@wsp@cies{F}{#2}{#1}}
\newcommand{\animal}[2][]{\sh@wsp@cies{A}{#2}{#1}}
\newcommand{\bact}[2][]{\sh@wsp@cies{B}{#2}{#1}}
\newcommand{\n@wsp@cies}[3]{%
\renewcommand{\curr@ntid}{#1#2@}%
\ifthenelse{\equal{#2}{Q@D}}%
  {\relax}%
  {\newcounter{\curr@ntid counter}}%
\setcounter{\curr@ntid counter}{0}%
\setkeys{SpeciesParams}{#3}%
}
\def\Q@DCleanup#1+{\n@wsp@cies{}{Q@D}{#1}}

\newcommand{\add@species@key}[2]{%
  \define@key{SpeciesParams}{#1}{#2}
  \expandafter\ifx\csname Q@DCleanList\endcsname\relax%
     \def\Q@DCleanList{#1=\relax}%
  \else%
     \edef\Q@DCleanList{\Q@DCleanList,#1=\relax}%
  \fi%
}
\def\SplitG@nusL@tters(#1#2){%
  \uppercase{\renewcommand{\T@mpFirst}{#1}}\lowercase{\renewcommand{\T@mpRest}{#2}}}
\add@species@key{genus}{%
  \SplitG@nusL@tters(#1)%
  \expandafter\edef\csname\curr@ntid genus\endcsname{\T@mpFirst\T@mpRest}%
}
\add@species@key{oldgenus}{%
  \SplitG@nusL@tters(#1)%
  \expandafter\edef\csname\curr@ntid oldgenus\endcsname{\T@mpFirst\T@mpRest}%
}
\add@species@key{epithet}{\lowercase{\expandafter\edef\csname\curr@ntid epithet\endcsname{#1}}}
\add@species@key{oldepithet}{\lowercase{\expandafter\edef\csname\curr@ntid oldepithet\endcsname{#1}}}
\add@species@key{author}{\expandafter\edef\csname\curr@ntid author\endcsname{#1}}
\add@species@key{year}{\expandafter\edef\csname\curr@ntid year\endcsname{#1}}
\add@species@key{oldauthor}{\expandafter\edef\csname\curr@ntid oldauthor\endcsname{#1}}
\add@species@key{oldyear}{\expandafter\edef\csname\curr@ntid oldyear\endcsname{#1}}
\define@key{SpeciesParams}{fullstyle}
  {\expandafter\def\csname\curr@ntid fullstyle\endcsname{\csname Sp@cies#1\endcsname}}
\define@key{SpeciesParams}{abbrstyle}
  {\expandafter\def\csname\curr@ntid abbrstyle\endcsname{\csname Sp@cies#1\endcsname}}
\newcommand{\newtaxon}[1]{\add@species@key{#1}{\expandafter\edef\csname\curr@ntid #1\endcsname{##1}}}
\newcommand{\taxon}[1]{\Sh@wTax@n+(#1)+n+}
\newcommand{\taxonfirst}[1]{\Sh@wTax@n+(#1)+a+}

\def\Sh@wTax@n+(#1!#2!#3)+#4+{%
  \expandafter\ifx\csname\curr@ntid#2\endcsname%
    \relax%
  \else%
    \ifthenelse{\equal{#4}{n}}{%
      #1\csname\curr@ntid#2\endcsname#3%
    }{%
      \edef\T@mpTax@n{\csname\curr@ntid#2\endcsname}%
      #1\expandafter\Sh@wFirst\T@mpTax@n+#3%
    }%
  \fi%
}

\def\Sh@wFirst#1#2+{#1}
\DeclareRobustCommand\taxitalics
        {\@nomath\em \ifdim \fontdimen\@ne\font >\z@
                       \upshape \else \itshape \fi}
\DeclareTextFontCommand{\taxit}{\taxitalics}
\newtaxastyle{ePlant}
  {\taxit{\taxon{!genus!}\taxon{ !epithet!}}\taxon{ (!oldauthor!)}\taxon{ !author!}}
\newtaxastyle{eAnimal}
  {\taxit{\taxon{!genus!}\taxon{ (!oldgenus!)}\taxon{ !epithet!}}\taxon{ (!oldauthor!}%
   \taxon{, !oldyear!)}\taxon{!author!}\taxon{, !year!}}
\newtaxastyle{f}
  {\taxit{\taxon{!genus!}\taxon{ !epithet!}}}
\newtaxastyle{a}
  {\taxit{\taxonfirst{!genus!.}\taxon{ !epithet!}}}
\newtaxastyle{g}
  {\taxit{\taxon{!genus!}}}
\defaultplante{ePlant}
\defaultfunguse{ePlant}
\defaultanimale{eAnimal}
\defaultbacte{eAnimal}
\defaultfull{f}
\defaultabbr{a}
\newcommand{\sh@wsp@cies}[3]{%
  \ifthenelse{\equal{#2}{Q@D}}%
    {\renewcommand{\curr@ntid}{#2@}}%
    {\renewcommand{\curr@ntid}{#1#2@}}%
  \ifthenelse{\equal{#3}{}}{%
    \ifnum\value{\curr@ntid counter}=0%
      \setcounter{\curr@ntid counter}{1}%
      \expandafter\ifx\csname\curr@ntid fullstyle\endcsname\relax%
        \csname Gl@balF@llStyle\endcsname%
      \else%
        \csname\curr@ntid fullstyle\endcsname %
      \fi%
    \else%
      \expandafter\ifx\csname\curr@ntid abbrstyle\endcsname\relax%
        \csname Gl@bal@bbrStyle\endcsname%
      \else%
        \csname\curr@ntid abbrstyle\endcsname%
      \fi%
    \fi%
  }{%
    \ifthenelse{\equal{#3}{e}}{%
      \csname Gl@bal#1E@llStyle\endcsname%
    }{%
      \csname Sp@cies#3\endcsname%
    }%
  }%
}
