%% The LaTeX package tcolorbox - version 2.02 (2013/03/13)
%% tcbdocumentation.code.tex: Code for LaTeX documentations
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2006-2013 by Prof. Dr. Dr. Thomas F. Sturm <thomas dot sturm at unibw dot de>
%% -------------------------------------------------------------------------------------------
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) 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 work has the LPPL maintenance status `author-maintained'.
%%
%% This work consists of all files listed in README
%%
\makeatletter
\def\tcblibrary@documentation@loaded{}

\tcbuselibrary{listings}

\RequirePackage{doc}[2010/02/04]
\RequirePackage{pifont}[2005/04/12]
\RequirePackage{marvosym}[2011/07/20]
\RequirePackage{hyperref}[2012/02/06]
\RequirePackage{makeidx}[2000/03/29]

\definecolor{Option}{rgb}{0.118,0.546,0.222}
\definecolor{Definition}{rgb}{0.784,0.06,0.176}
\definecolor{ExampleFrame}{rgb}{0.628,0.705,0.942}
\definecolor{ExampleBack}{rgb}{0.963,0.971,0.994}
\definecolor{Hyperlink}{rgb}{0.281,0.275,0.485}

\def\colDef#1{\textcolor{\kvtcb@coldef}{#1}}
\def\colOpt#1{\textcolor{\kvtcb@colopt}{#1}}

\lstdefinestyle{tcbdocumentation}{language={[LaTeX]TeX},
    aboveskip={0\p@ \@plus 6\p@},
    belowskip={0\p@ \@plus 6\p@},
    columns=fullflexible,
    keepspaces=true,
    breaklines=true,
    prebreak={\Righttorque},
    postbreak={\space\Lefttorque},
    breakatwhitespace=true,
    basicstyle=\ttfamily\footnotesize,
    extendedchars=true,
    moredelim={[is][\itshape\rmfamily]{§}{§}},
    nolol,inputencoding=\kvtcb@listingencoding}

% The following macros are taken from ltxdoc
\DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}
\def\marg#1{%
  {\ttfamily\char`\{}{\rmfamily\meta{#1}}{\ttfamily\char`\}}}
\def\oarg#1{%
  \colOpt{{\ttfamily[}{\rmfamily\meta{#1}}{\ttfamily]}}}

% language specific texts
\tcbset{
  doclang/.cd,
  color/.store in=\kvtcb@text@color,
  colors/.store in=\kvtcb@text@colors,
  environment content/.store in=\kvtcb@text@envcontent,
  environment/.store in=\kvtcb@text@env,
  environments/.store in=\kvtcb@text@envs,
  key/.store in=\kvtcb@text@key,
  index/.store in=\kvtcb@text@index,
  pageshort/.store in=\kvtcb@text@pageshort,
}

% documentation options
\tcbset{
  documentation listing style/.store in=\kvtcb@doclstsytle,
  color option/.store in=\kvtcb@colopt,
  color definition/.store in=\kvtcb@coldef,
  color hyperlink/.store in=\kvtcb@colhyper,
  color frame/.store in=\kvtcb@colhyper,
  color hyperlink/.store in=\kvtcb@colhyper,
  before example/.store in=\kvtcb@beforeexample,
  after example/.store in=\kvtcb@afterexample,
  index actual/.store in=\idx@actual,
  index quote/.store in=\idx@quote,
  index level/.store in=\idx@level,
}

% styles
\tcbset{
  docexample/.style={colframe=ExampleFrame,colback=ExampleBack,fontlower=\footnotesize},
  index default settings/.style={index actual={@},index quote={"},index level={!}},
  index german settings/.style={index actual={=},index quote={!},index level={>}},
  english language/.code={\tcbset{doclang/.cd,
    color=color,
    colors=Colors,
    environment content=environment content,
    environment=environment,
    environments=Environments,
    key=key,
    index=Index,
    pageshort={P.}}},
}

% presets
\tcbset{
  english language,
  documentation listing style=tcbdocumentation,
  index default settings,
  color option=Option,
  color definition=Definition,
  color hyperlink=Hyperlink,
  before example=\par\smallskip,
  after example=,
}

\AtBeginDocument{\hypersetup{
  citecolor=\kvtcb@colhyper,
  linkcolor=\kvtcb@colhyper,
  urlcolor=\kvtcb@colhyper,
  filecolor=\kvtcb@colhyper,
  menucolor=\kvtcb@colhyper
}}

% tcolorbox specific macros
\def\tcb@doc@usetemplisting{\lstinputlisting[style=\kvtcb@doclstsytle]{\kvtcb@tempfile}}

\let\dispExample\tcbwritetemp

\def\enddispExample{%
  \endtcbwritetemp%
  \kvtcb@beforeexample\begin{tcolorbox}[docexample]%
  \tcb@doc@usetemplisting%
  \tcblower%
  \tcbusetemp%
  \end{tcolorbox}\kvtcb@afterexample%
}

\let\dispListing\tcbwritetemp

\def\enddispListing{%
  \endtcbwritetemp%
  \kvtcb@beforeexample\begin{tcolorbox}[docexample]%
  \tcb@doc@usetemplisting%
  \end{tcolorbox}\kvtcb@afterexample%
}

% index auxiliary macros
\def\index@Col#1{\index{#1\idx@actual\texttt{#1} \kvtcb@text@color}\index{\kvtcb@text@colors\idx@level#1\idx@actual\texttt{#1}}}
\def\index@Com#1{\index{#1\idx@actual\cs{#1}}}
\def\index@Env#1{\index{#1\idx@actual\texttt{#1} \kvtcb@text@env}\index{\kvtcb@text@envs\idx@level#1\idx@actual\texttt{#1}}}
\def\index@Key#1{\index{#1\idx@actual\texttt{#1} \kvtcb@text@key}}%
\def\index@KeyPath#1#2{\index@Key{#2}\index{#1\idx@actual\texttt{/#1/}\idx@level#2\idx@actual\texttt{#2}}}%

\def\disp@com#1{\colDef{\ttfamily\bfseries#1}}

\def\brackets#1{{\ttfamily\char`\{}#1{\ttfamily\char`\}}}

\newenvironment{manual@entry}{\list{}{\leftmargin=2em\itemindent-\leftmargin\def\makelabel##1{\hss##1}}}{\endlist}

\def\manual@top#1{\itemsep=0pt\parskip=0pt\item\strut{#1}\par\topsep=0pt}

% main documentation environments
\newenvironment{docCommand}[2]{\begin{manual@entry}%
  \hypertarget{com:#1}{\manual@top{\disp@com{\cs{#1}}\index@Com{#1}\protected@edef\@currentlabel{\noexpand\cs{#1}}\label{com:#1}{\ttfamily #2}}}}%
  {\end{manual@entry}}

\newenvironment{docEnvironment}[2]{\begin{manual@entry}%
  \hypertarget{env:#1}{\manual@top{\cs{begin}\brackets{\disp@com{#1}}\index@Env{#1}\protected@edef\@currentlabel{#1}\label{env:#1}{\ttfamily #2}}}%
  \manual@top{~~\meta{\kvtcb@text@envcontent}}%
  \manual@top{\cs{end}\brackets{\disp@com{#1}}}}%
  {\end{manual@entry}}

\newenvironment{docKey}[4][\@empty]{\begin{manual@entry}%
  \ifx#1\@empty%
  \hypertarget{key:#2}{\manual@top{\disp@com{#2}\index@Key{#2}\protected@edef\@currentlabel{#2}\label{key:#2}{\ttfamily #3}\hfill(#4)}}
  \else\hypertarget{key:/#1/#2}{\manual@top{\disp@com{/#1/#2}\index@KeyPath{#1}{#2}\protected@edef\@currentlabel{/#1/#2}\label{key:/#1/#2}#3\hfill(#4)}}\fi}%
  {\end{manual@entry}}

\def\tcbmakedocSubKey#1#2{%
  \newenvironment{#1}[4][\@empty]{%
    \ifx##1\@empty\def\tcb@key@path{#2}\else\def\tcb@key@path{#2/##1}\fi%
    \begin{docKey}[\tcb@key@path]{##2}{##3}{##4}}%
      {\end{docKey}}%
}

\def\docColor#1{\disp@com{#1}\index@Col{#1}}%
\def\docAuxCommand#1{\disp@com{\cs{#1}}\index@Com{#1}}%

\def\ref@doc#1{\hyperlink{#1}{\texttt{\ref*{#1}}\textsuperscript{\ding{213}\,\kvtcb@text@pageshort\,\pageref*{#1}}}}

\def\refCom#1{\ref@doc{com:#1}}
\def\refEnv#1{\ref@doc{env:#1}}
\def\refKey#1{\ref@doc{key:#1}}

\newenvironment{absquote}
               {\list{}{\leftmargin2cm\rightmargin\leftmargin}%
                \item\relax\footnotesize}
               {\endlist}

%--- index macros taken from pgfmanual-en-macros
\def\index@prologue{\section*{\kvtcb@text@index}\addcontentsline{toc}{section}{\kvtcb@text@index}%
  \par\noindent%
}
\c@IndexColumns=2
  \def\theindex{\@restonecoltrue
    \columnseprule \z@  \columnsep 29\p@
    \twocolumn[\index@prologue]%
       \parindent -30pt%
       \columnsep 15pt%
       \parskip 0pt plus 1pt%
       \leftskip 30pt%
       \rightskip 0pt plus 2cm%
       \small%
       \def\@idxitem{\par}%
       \let\item\@idxitem\ignorespaces}
  \def\endtheindex{\onecolumn}
\def\noindexing{\let\index=\@gobble}


