\ProvidesPackage{cleveref}
\RequirePackage{labeltype}
\@primitives{cleveref}
%%alias mechanism
\newcommand{\crefalias}[2]{\def\csname @cr@#1@alias\endcsname{#2}}
\newcommand{\@cr@alias}[1]
{\ifu\csname @cr@#1@alias\endcsname{}#1\else\csname @cr@#1@alias\endcsname\fi}
\crefalias{item}{enumi}
%%extend \@deflabeltype from labeltype
\let\@cr@old@deflabeltype\@deflabeltype
\renewcommand{\@deflabeltype}[2]
{\@cr@old@deflabeltype{#1}{#2}%
\prim@def\csname @cf@#1@type\endcsname{\@cr@alias{#2}}}
%%Use alias mechanism for appendix
%%rely on \@base being either article or book
\newcommand{\@cr@article@appendix}{%
\@auxdowrite{\@print{\crefalias}\{section\}\{appendix\}\newline}%
\@auxdowrite{\@print{\crefalias}\{subsection\}\{subappendix\}\newline}%
\@auxdowrite{\@print{\crefalias}\{subsubsection\}\{subsubappendix\}\newline}%
}
\newcommand{\@cr@book@appendix}{%
\@auxdowrite{\@print{\crefalias}\{chapter\}\{appendix\}\newline}%
\@auxdowrite{\@print{\crefalias}\{section\}\{subappendix\}\newline}%
\@auxdowrite{\@print{\crefalias}\{subsection\}\{subsubappendix\}\newline}%
}
\let\@cr@old@appendix\appendix
\renewcommand{\appendix}
{\@cr@old@appendix%
\csname @cr@\@base{}@appendix\endcsname}
%%Some internal conditions, with defaults
\newif\if@cref@capitalise\@cref@capitalisefalse
\newif\if@cref@abbrev\@cref@abbrevtrue
\newif\if@cref@nameinlink\@cref@nameinlinkfalse
\DeclareOption{nameinlink}{\@cref@nameinlinktrue}
\DeclareOption{capitalise}{\@cref@capitalisetrue}
\DeclareOption{capitalize}{\@cref@capitalisetrue}
%Ignore multi-language hooks support
\newcommand{\cref@addlanguagedefs}[2]
{\def\csname cref@language@#1@defs\endcsname{#2}}
%Define internal names
%#1 cref/Cref #2 type #3 name #4 plural #5 extension (ignored)
%%% section names from type
\newcommand{\@@crefname}[5]
{%\hva@warn{@@crefname '#1' '#2' #3' '#4' '#5'}%
\def\csname #1@#2@name\endcsname{#3}%
\def\csname #1@#2@name@plural\endcsname{#4}}
\newcommand{\@@crefname@nondef}[5]
{%\hva@warn{@@crefname@nondef '#1' '#2' #3' '#4' '#5'}%
\ifu\csname #1@#2@name\endcsname\def\csname #1@#2@name\endcsname{#3}\else
%\hva@warn{NO DEF for '#1' '#2'}
\fi%
\ifu\csname  #1@#2@name@plural\endcsname\def\csname #1@#2@name@plural\endcsname{#4}\fi}
%%Define others
\def\@cr@cref@other{Cref}
\def\@cr@Cref@other{cref}
\newcommand{\@cr@other}[1]{\csname @cr@#1@other\endcsname}
\let\@cr@cref@othercom\MakeUppercase
\def\@cr@Cref@othercom{\if@cref@capitalise\let\@next\relax\else\let\@next\MakeLowercase\fi\@next}
\newcommand{\@@crefname@other}[5]{
\@@crefname@nondef
  {\csname @cr@#1@other\endcsname}
  {#2}
  {\csname @cr@#1@othercom\endcsname #3}
  {\csname @cr@#1@othercom\endcsname #4}
  {#5}}
\newcommand{\@crefname}[5]{%
\@@crefname{#1}{#2}{#3}{#4}{#5}%
\@@crefname@other{#1}{#2}{#3}{#4}{#5}}
\newcommand{\crefname}[3]{\@crefname{cref}{#1}{#2}{#3}{}}
\newcommand{\Crefname}[3]{\@crefname{Cref}{#1}{#2}{#3}{}}
\newcommand{\crefname@preamble}[3]{\@crefname{cref}{#1}{#2}{#3}{@preamble}}
\newcommand{\Crefname@preamble}[3]{\@crefname{Cref}{#1}{#2}{#3}{@preamble}}
%For some reason all this must be defined...
\iftrue
\newcommand{\crefrangeconjunction}{ to\nobreakspace}%
\newcommand\crefrangepreconjunction{}%
\newcommand\crefrangepostconjunction{}%
\newcommand{\crefpairconjunction}{ and\nobreakspace}%
\newcommand{\crefmiddleconjunction}{, }%
\newcommand{\creflastconjunction}{ and\nobreakspace}%
\newcommand{\crefpairgroupconjunction}{ and\nobreakspace}%
\newcommand{\crefmiddlegroupconjunction}{, }%
\newcommand{\creflastgroupconjunction}{, and\nobreakspace}%
\fi
\input{crlang.hva}
%%%
\newcommand{\@@cr@secname}[2]
{%\hva@warn@prim{SEC: '#1' '#2'}%
%\typeout{TYPE: '#2'}%
\ifu#2 ??\else\ifu\csname #1@#2@name\endcsname
\hva@warn@prim{No name for type #2}??\else
\csname #1@#2@name\endcsname\fi\fi}
%%
\newcommand{\@@cr@secname@plural}[2]
{%\hva@warn{SEC: '#1' '#2'}%
%\typeout{TYPE: '#2'}%
\ifu#2 ??\else\ifu\csname #1@#2@name@plural\endcsname
\hva@warn@prim{No name for type #2}??\else
\csname #1@#2@name@plural\endcsname\fi\fi}
\newcommand{\@cr@secname}[2]{\@@cr@secname{#1}{\csname @cf@#2@type\endcsname}}
\newcommand{\@cr@secname@plural}[2]{\@@cr@secname@plural{#1}{\csname @cf@#2@type\endcsname}}
\newcommand{\namecref}[1]{\@cr@secname{cref}{#1}}
\newcommand{\nameCref}[1]{\@cr@secname{Cref}{#1}}
\newcommand{\namecrefs}[1]{\@cr@secname@plural{cref}{#1}}
\newcommand{\nameCrefs}[1]{\@cr@secname@plural{Cref}{#1}}
\newcommand{\@cr@fmt}[3]{#2#1#3}
\newcommand{\@cr@apply@fmt}[1]
{\@cr@fmt{\@auxread{#1}}{\@openlocref{#1}}{\@closelocref}}
\newcommand{\crefdefaultlabelformat}{\@cr@def{\@cr@fmt}{3}}
\newcommand{\@@cr@fmt}[5]
{\if@cref@nameinlink
\@cr@fmt{\@@cr@secname{#1}{#2}~#3}{#4}{#5}%
\else\@@cr@secname{#1}{#2}~\@cr@fmt{#3}{#4}{#5}\fi}
\newcommand{\@cr@def@fmt}[2]
{\@cr@def@withother
{\csname @\@cr@other{#1}@#2@fmt\endcsname}
{\csname @cr@#1@othercom\endcsname}
{\csname @#1@#2@fmt\endcsname}{3}}
\newcommand{\crefformat}[1]{\@cr@def@fmt{cref}{#1}}
\newcommand{\Crefformat}[1]{\@cr@def@fmt{Cref}{#1}}
\newcommand{\@cr@find@fmt}[2]{%
\ifu#2\def\@next{\@@cr@fmt{#1}{#2}}\else
\ifu\csname @#1@#2@fmt\endcsname\def\@next{\@@cr@fmt{#1}{#2}}\else
\let\@next\csname @#1@#2@fmt\endcsname\fi\fi\@next}
\newcommand{\@cr@fmt@one}[2]
{\@cr@find@fmt
  {#1}{\csname @cf@#2@type\endcsname}
  {\@auxread{#2}}{\@openlocref{#2}}{\@closelocref}}
\newcommand{\@cr@fmt@two}[3]
{\@cr@secname@plural{#1}{#2}~\@cr@apply@fmt{#2}\crefpairconjunction\@cr@apply@fmt{#3}}
\newcommand{\@cr@ref}[2]{\@cr@sort@labels{#1}{#2}}
%%%
\newcommand{\cref}[1]{\@cr@ref{cref}{#1}}
\newcommand{\Cref}[1]{\@cr@ref{Cref}{#1}}
%%%%%%%Ranges, much easier no customisation (yet ?)
\newcommand{\@cr@range}[3]
{\@cr@secname@plural{#1}{#2}~\crefrangepreconjunction\@cr@apply@fmt{#2}\crefrangeconjunction\@cr@apply@fmt{#3}\crefrangepostconjunction}
\newcommand{\crefrange}[2]{\@cr@range{cref}{#1}{#2}}
\newcommand{\Crefrange}[2]{\@cr@range{Cref}{#1}{#2}}
%%%%%%%
%%We redefine \AtBeginDocument  so as to collect lask invokation
%%That way, last language def is executed now
%%and preamble \crefname will override it
\let\@cr@AtBeginDocument\AtBeginDocument
\newtokens{\@cr@at}
\renewcommand{\AtBeginDocument}[1]{\addtokens{\@cr@at}{#1}}
\ExecuteOptions{english}
\ProcessOptions*%
\let\AtBeginDocument\@cr@AtBeginDocument
\undef\@cr@AtBeginDocument
%%%execute option, code, notice that we do execute all of it
\@cr@at\csname cref@language@\cref@language{}@defs\endcsname
\undef\@cr@at
%%%The only problem is the non automatic definition of Capitalized
%%%counterparts for \crfename
