% Author: Alexey Shipunov, dactylorhiza@gmail.com

\ProvidesPackage{classif2}%
        [2008/02/10 v2.2 Layout for biological classifications]

% 2002/04/15 \IHOOK added (for indent justification)
% 2007/06/23 Infraregnum added
% 2007/07/26 Improved for numeric ranges
% 2008/02/06 Abbreviations added

\newif\if@Names
\DeclareOption{nonames}{\@Namesfalse}
\DeclareOption{names}{\@Namestrue}
\ExecuteOptions{names}
\ProcessOptions

%%%%%%%%%%%%%%%%%%%%%%%%%% Registers %%%%%%%%%%%%%%%%%%%%%%%%%%%

\newif\if@Slash
\newif\if@Repeat
\newif\ifHide
\newif\ifNumbers
\newif\ifGlobal
\newif\ifDigits

\Numberstrue

\newcount\Base@LevelC@
\newcount\View@LevelC@
\newcount\TMC@I
\newcount\TMC@II
\newcount\TMC@III

\newdimen\TML@
\newdimen\IHOOK

\IHOOK=0pt

\newcounter{GlobalLevel}
\newcounter{Leveliv}
\newcounter{Leveliii}[Leveliv]
\newcounter{Levelii}[Leveliii]
\newcounter{Leveli}[Levelii]
\newcounter{Level}[Leveli]

%%%%%%%%%%%%%%%%%%%%%%%% Internal tools %%%%%%%%%%%%%%%%%%%%%%%%

\def\@Concatenate#1.#2{#1#2}
\def\@DeleteSecond#1.#2{#1}
\def\TMP@{\relax}
\def\TMP@@{\relax}

%%%%%%%%%%%%%%%%%%%%%%% Level commands %%%%%%%%%%%%%%%%%%%%%%%%%

\def\Base@Level#1{\Base@LevelC@=\expandafter\@DeleteSecond#1 %
        \View@LevelC@=\expandafter\@Concatenate#1 }

\def\LevelName#1#2{\TMC@I=\expandafter\@Concatenate#1 %
        \expandafter\def\csname Level@TMB@\romannumeral\TMC@I\endcsname{#2 }}

\def\Level{\@ifstar{\Level@S}{\Level@N}}

%\Level*
\def\Level@S#1.#2 #3 #4 #5\par{{%
        \reset@font
        \TMC@I=#1\TMC@II=#2\TMC@III=#1#2%
	\ifnum\TMC@II>0\def\CF{#1.#2} \else\def\CF{#1} \fi
                \advance\TMC@I by -\Base@LevelC@
                \advance\TMC@III by -\View@LevelC@
        \ifnum\TMC@III<0
                \ifHide \relax
                \else
\Level@Size
   \Level@Skip
      \Level@Indent
        {\Level@Style
           {\upshape #3 }%
             #4 }%
             #5%
\After@Level\fi
        \else
\Level@Size
   \Level@Skip
      \Level@Indent
        {\Level@Style
           {\upshape #3 }%
             #4 }%
             #5%
\After@Level\fi}}

%\Level
\def\Level@N#1.#2 #3 #4\par{{
	\TMP@\TMP@@
        \reset@font
        \TMC@I=#1\TMC@II=#2\TMC@III=#1#2%
	\ifnum\TMC@II>0\def\CF{#1.#2} \else\def\CF{#1} \fi
                \advance\TMC@I by -\Base@LevelC@
                \advance\TMC@III by -\View@LevelC@
        \ifnum\TMC@III<0 \relax
        \else
\Level@Size
   \Level@Skip
     \Level@Indent
        {\Level@Style
           {\upshape \Repeat@Or@Name
              \Level@Number
                \ifDigits #1.#2 %
                \fi}%
                   #3 }%
                   #4%
\After@Level\fi}}

\def\Level@Size{\ifnum\TMC@I=0 \normalsize
\else \ifnum\TMC@I=1 \large
       \else \ifnum\TMC@I=2 \Large
              \else \ifnum\TMC@I=3 \LARGE
                    \else \ifnum\TMC@I<0 \small
                          \else \huge
                          \fi
                    \fi
              \fi
       \fi
\fi}

\def\Level@Skip{%
\ifnum \TMC@III<0 \TML@=\parskip
       \else
       \TML@=\TMC@III ex%
       \divide\TML@ by 10%
\fi
\ifnum \TMC@I=0%
       \if@Repeat \nopagebreak\TML@=.5 ex%
       \fi
\fi
\addvspace{\TML@}}

\def\Level@Indent{%
        \TML@=\TMC@III mm%
        \hangindent.2\columnwidth
        \advance\hangindent by -\TML@
        \advance\hangindent by \IHOOK
\ifnum\TMC@III>0\else%
        \settowidth{\TML@}{\Level@Name}%
        \advance\hangindent by \TML@
\fi \parindent=\hangindent}

\def\Level@Style{\ifnum\TMC@III<0 \mdseries\itshape
                 \else
\ifnum\TMC@II=0 \ifnum\TMC@I=0 \mdseries\itshape
                \else \ifnum\TMC@I=1 \mdseries\scshape
                      \else \bfseries\upshape
                      \fi
                \fi
\else \ifnum\TMC@II=2 \mdseries\itshape
      \else \ifnum\TMC@II=5 \bfseries\upshape
            \else \ifnum\TMC@II=8 \bfseries\itshape
                  \else \mdseries\upshape
                  \fi
             \fi
      \fi
\fi
                 \fi}

\def\Level@Name{%
        \advance\TMC@III by \View@LevelC@
\csname Level@TMB@\romannumeral\TMC@III\endcsname
        \advance\TMC@III by -\View@LevelC@}

\def\Repeat@Or@Name{%
\ifnum\TMC@III=0%
      \if@Repeat\makebox[0pt][r]{\Level@Name
                \ifNumbers \gdef\TMP@@{\Numberstrue}%
                \else \gdef\TMP@@{\Numbersfalse}%
                \fi}\gdef\TMP@{\@Repeatfalse}%
      \fi
\else \Level@Name \gdef\TMP@{\@Repeattrue}%
\fi}

\def\Level@Number{%
\ifNumbers \ifnum\TMC@III=0
                \stepcounter{Level}%
                \arabic{Level}%
                        \ifGlobal \stepcounter{GlobalLevel}%
                                  (\arabic{GlobalLevel}). %
                        \else . %
                        \fi
           \else \ifnum\TMC@II=0
                        \stepcounter{Level\romannumeral\TMC@I}%
                        \arabic{Level\romannumeral\TMC@I}. %
                 \else
                 \fi
           \fi
\fi}

\def\After@Level{\par\ifnum\TMC@I>0 \nopagebreak\fi}

\def\CF{}

\def\N#1{[\kern.2em$^{\CF}$\kern-.15em\textit{#1}\kern.15em]}

%%%%%%%%%%%%%%%%%%%%% Environment commands %%%%%%%%%%%%%%%%%%%%%

{\catcode`\/=\active
\gdef\Start@Slash{%
\global\catcode`\/=\active
    \global\let/=\Level}}

\newenvironment{classif}[1]
        {\Start@Slash\Base@Level{#1}}
        {\setcounter{GlobalLevel}{0}%
        \setcounter{Leveliv}{0}%
        \setcounter{Leveliii}{0}%
        \setcounter{Levelii}{0}%
        \setcounter{Leveli}{0}%
        \setcounter{Level}{0}%
        \global\catcode`\/12\relax}

%%%%%%%%%%%%%%%%%%%%%%%%% Optional %%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\if@Names
\LevelName{0.5}{Varietes}%
\LevelName{0.8}{Subspecies}%
\LevelName{1.0}{Species}%
\LevelName{1.2}{Subsectio}%
\LevelName{1.5}{Sectio}%
\LevelName{1.8}{Subgenus}%
\LevelName{2.0}{Genus}%
\LevelName{2.2}{Subtribus}%
\LevelName{2.5}{Tribus}%
\LevelName{2.8}{Subfamilia}%
\LevelName{3.0}{Familia}%
\LevelName{3.2}{Superfamilia}%
\LevelName{3.5}{Infraordo}%
\LevelName{3.8}{Subordo}%
\LevelName{4.0}{Ordo}%
\LevelName{4.2}{Superordo}%
\LevelName{4.5}{Infraclassis}%
\LevelName{4.8}{Subclassis}%
\LevelName{5.0}{Classis}%
\LevelName{5.2}{Superclassis}%
\LevelName{5.8}{Subphylum}%
\LevelName{6.0}{Phylum}%
\LevelName{6.2}{Superphylum}%
\LevelName{6.5}{Infraregnum}%
\LevelName{6.8}{Subregnum}%
\LevelName{7.0}{Regnum}%
\LevelName{7.2}{Superregnum}%
\fi

%%%%%%%%%%%%%%%%%%%%%%%% Abbreviations %%%%%%%%%%%%%%%%%%%%%%%%%

\RequirePackage{xspace}

\def\H@Sp{\nobreak%
        \hskip .5\fontdimen2\font%
        \@plus .5\fontdimen3\font%
        \@minus .5\fontdimen4\font%
\ignorespaces}

\def\Sl{s.\H@Sp l.\xspace}
\def\Sr{s.\H@Sp str.\xspace}
\def\Pp{p.\H@Sp p.\xspace}
\def\Se{stat.\H@Sp m.\xspace}
\def\St{sed.\H@Sp m.\xspace}
\def\Is{i.\H@Sp s.\xspace}
\def\Si{sed.\H@Sp poss.\xspace}

%%%%%%%%%%%%%%%%%%%%%%%% User tools %%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\Comm{\ifnum \TMC@III<0 \TML@=\parskip
       \else
       \TML@=\TMC@III ex%
       \divide\TML@ by 30%
       \fi
\\*[\TML@]\normalsize\normalfont}

% Usage:
% /5.0 Angiospermae \Comm Something

\def\EmptyName#1{\settowidth{\TML@}{#1}\makebox[\TML@]{}}

% Usage (for big indent without `\LevelName'):
% \LevelName{1.0}{\EmptyName{Species}}

\def\Reset#1{\setcounter{#1}{0}}
\newcommand\Numerate[2][\arabic]{\stepcounter{#2} #1{#2}.}

% `\Reset' and `\Numerate' usage:
% \newcounter{Temp}
% \LevelName{6.2}{Superphylum\Numerate[\Alph]{Temp}}
% \LevelName{7.0}{Regnum\Numbersfalse\Reset{Temp}}
% And even like that:
% \newcounter{Temp}[Leveliii]
% (main levels are numbered with `Level<i,ii,...>' counters)
% \LevelName{5.0}{Classis\Numerate[\Roman]{Temp}\Numbersfalse}

% Some goodies:
\let\K\textit
\let\INCL\footnote
\def\KURN#1 #2 {\textit{#1 #2 }}
\def\IN#1.#2.{#1.\nobreak\hskip.16em #2.~\ignorespaces}

\endinput
