%% The LaTeX package tcolorbox - version 2.02 (2013/03/13)
%% tcblistings.code.tex: Code for colorboxes with listings
%%
%% -------------------------------------------------------------------------------------------
%% 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@listings@loaded{}

\RequirePackage{listings}[2007/02/22]

\lstdefinestyle{tcblatex}{language={[LaTeX]TeX},
     aboveskip={0\p@ \@plus 6\p@}, belowskip={0\p@ \@plus 6\p@},
     columns=fullflexible, keepspaces=true,
     breaklines=true, breakatwhitespace=true,
     basicstyle=\ttfamily\small, extendedchars=true, nolol,
     inputencoding=\kvtcb@listingencoding}

\def\tcbuselistingtext{\input{\kvtcb@listingfile}}%

\def\tcbuselistinglisting{%
  \toks@=\expandafter{\kvtcb@listingoptions}%
  \edef\tcb@temp{\noexpand\lstinputlisting[\the\toks@]}%
  \tcb@temp{\kvtcb@listingfile}%
}%

\def\tcb@listing@listingAndText{%
   \tcbuselistinglisting%
   \tcblower%
   \tcbuselistingtext%
}

\def\tcb@listing@listingAndComment{%
   \tcbuselistinglisting%
   \tcblower%
   \kvtcb@listingcomment%
}

\def\tcb@listing@textAndListing{%
   \tcbuselistingtext%
   \tcblower%
   \tcbuselistinglisting%
}

\def\tcb@listing@commentAndListing{%
   \kvtcb@listingcomment%
   \tcblower%
   \tcbuselistinglisting%
}

\tcbset{
  listing file/.store in=\kvtcb@listingfile,
  listing options/.store in=\kvtcb@listingoptions,
  comment/.store in=\kvtcb@listingcomment,
  listing style/.style={listing options={style=#1}},
  listing and text/.code={\let\tcp@listing@process\tcb@listing@listingAndText},
  text and listing/.code={\let\tcp@listing@process\tcb@listing@textAndListing},
  listing and comment/.code={\let\tcp@listing@process\tcb@listing@listingAndComment},
  comment and listing/.code={\let\tcp@listing@process\tcb@listing@commentAndListing},
  listing only/.code={\let\tcp@listing@process\tcbuselistinglisting},
  text only/.code={\let\tcp@listing@process\tcbuselistingtext},
  listing inputencoding/.store in=\kvtcb@listingencoding,
}

\tcbset{
  listing file=\jobname.listing,
  listing style=tcblatex,
  comment=,
  listing and text,
  listing inputencoding=\inputencodingname,
}

\newenvironment{tcblisting}[1]{%
  \def\tcblisting@box{\begin{tcolorbox}[#1]\tcp@listing@process\end{tcolorbox}}%
  \begingroup\tcbverbatimwrite{\kvtcb@listingfile}%
}{\endtcbverbatimwrite\endgroup%
  \tcblisting@box%
}

\def\tcbinputlisting#1{%
  \begin{tcolorbox}[#1]\tcp@listing@process\end{tcolorbox}%
}

\newenvironment{tcboutputlisting}{%
  \begingroup\tcbverbatimwrite{\kvtcb@listingfile}%
}{\endtcbverbatimwrite\endgroup}

\def\tcbusetemplisting{%
  \toks@=\expandafter{\kvtcb@listingoptions}%
  \edef\tcb@temp{\noexpand\lstinputlisting[\the\toks@]}%
  \tcb@temp{\kvtcb@tempfile}%
}%


