% verbments.sty   v1.2   2011/08/20
%
% Copyright (C) 2011 by Dejan Zivkovic
% -----------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% 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.2 or later is part of all distributions of LaTeX
% version 1999/12/01 or later.
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{verbments}[2011/08/20 v1.2 Pygments/fancyvrb source code highlighting for LaTeX]
\RequirePackage{xkeyval,fancyvrb,framed,xcolor,calc}
%
\newcommand{\@listofpyglistingsname}{Listings}%
\newcommand{\listofpyglistingsname}[1]{%
  \renewcommand\@listofpyglistingsname{#1}
}
\newcommand{\listofpyglistings}{%
  \begingroup
    \let\contentsname\@listofpyglistingsname
    \let\@pygtoc\@starttoc \def\@starttoc##1{\@pygtoc{pyg.lol}}%
    \tableofcontents%
  \endgroup
}
\newcommand{\lol@caption}[1]{%
  \addcontentsline{pyg.lol}{pyglist}{#1}
}
\newcommand{\l@pyglist}[2]{%
  \@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}
}
%
\newlength{\@tmpwidth}
%
\newskip\@tmpvskip
\def\max@vskip #1{%
   \@tmpvskip = #1\relax
   \ifdim\lastskip < \@tmpvskip
     \ifdim\lastskip = 0pt
     \else \vskip -\lastskip
     \fi
     \vskip \@tmpvskip
   \fi%
}

\setlength{\FrameSep}{0pt}
\setlength{\FrameRule}{0pt}
%
\define@cmdkey{pyglist}[pl@]{caption}{}
\define@cmdkey{pyglist}[pl@]{captionfont}[{}]{}
\define@cmdkey{pyglist}[pl@]{abovecaptionskip}[\bigskipamount]{}
\define@cmdkey{pyglist}[pl@]{belowcaptionskip}[0pt]{}
\define@cmdkey{pyglist}[pl@]{listingname}[Listing]{}
\define@cmdkey{pyglist}[pl@]{listingnamefont}[{}]{}
\define@cmdkey{pyglist}[pl@]{captionbgcolor}{}
\define@cmdkey{pyglist}[pl@]{label}{}
%
\define@cmdkey{pyglist}[pl@]{bgcolor}[white]{}
\define@cmdkey{pyglist}[pl@]{language}[text]{}
\define@cmdkey{pyglist}[pl@]{encoding}[latin1]{}
\define@boolkey{pyglist}[pl@]{mathescape}[false]{}
\define@boolkey{pyglist}[pl@]{texcl}[false]{}
\define@cmdkey{pyglist}[pl@]{style}[default]{}
\define@cmdkey{pyglist}[pl@]{gobble}[0]{}
%
\define@cmdkey{pyglist}[pl@]{fvset}{\fvset{#1}}
\define@cmdkey{pyglist}[pl@]{fontsize}{\fvset{fontsize=#1}}
\define@cmdkey{pyglist}[pl@]{numbers}{\fvset{numbers=#1}}
\define@cmdkey{pyglist}[pl@]{numbersep}{\fvset{numbersep=#1}}
\define@boolkey{pyglist}[pl@]{showspaces}{\fvset{showspaces=#1}}

\newcommand\plset[1]{%
  \setkeys{pyglist}{#1}%
}

\newenvironment{pyglist}{%
    \catcode`\^^M=\active
    \@ifnextchar[%
        {\catcode`\^^M=5 \pyglist@}
        {\catcode`\^^M=5 \pyglist@[]}
}{%
   \end{VerbatimOut}%
  \immediate\write18{\pyg@cmd}
  \immediate\write18{\pyg@style}
  \input{\jobname.pyg.sty}%
%
  \partopsep = 0pt\parskip = 0pt\parindent = 0pt%
  \@ifundefined{pl@caption}%
  {\setlength{\topsep}{0pt}}%
  {\par%
   \max@vskip{\pl@abovecaptionskip}%
   \penalty -100% encourage page break
   \refstepcounter{pllisting}%
   \@ifundefined{pl@label}{}{\label{\pl@label}}%
   \@tmpvskip=3\baselineskip%
   \vskip \@tmpvskip\penalty 1000\vskip -\@tmpvskip\vskip \z@skip% prevent page break
   \settowidth{\@tmpwidth}{\pl@listingnamefont\pl@listingname~\thepllisting:\space}%
   \@ifundefined{pl@captionbgcolor}%
   {\mbox{\pl@listingnamefont\pl@listingname~\thepllisting:\space}%
    \parbox[t]{\linewidth-\@tmpwidth}%
      {\leavevmode\pl@captionfont\pl@caption}%
   }
   {\setlength{\fboxrule}{0pt}\setlength{\fboxsep}{4pt}%
    \colorbox{\pl@captionbgcolor}{%
    \mbox{\pl@listingnamefont\pl@listingname~\thepllisting:\space}%
    \parbox[t]{\linewidth-\@tmpwidth-2\fboxsep}%
      {\leavevmode\pl@captionfont\pl@caption}%
    }%
   }%
   \par\nobreak\vskip\pl@belowcaptionskip% 
   \setlength{\topsep}{\smallskipamount}%
   \lol@caption{\rlap{\thepllisting}\hskip5ex \pl@caption}
  }%
%
  \definecolor{shadecolor}{named}{\pl@bgcolor}% 
  \begin{snugshade}
  \input{\jobname.pyg.vrb}
  \end{snugshade}
}

\def\pyglist@[#1]{%
  \setkeys{pyglist}{#1}
  \edef\pyg@cmd{pygmentize -P encoding=\pl@encoding\space -l \pl@language\space
     -F tokenmerge \ifnum\pl@gobble=0\space \else -F gobble:n=\pl@gobble\space\fi
     -f latex
     \ifpl@texcl -P texcomments\space\fi
     \ifpl@mathescape -P mathescape\space\fi
     -o \jobname.pyg.vrb \jobname.pyg.lst}
  \edef\pyg@style{pygmentize -S \pl@style\space -f latex > \jobname.pyg.sty}
  \VerbatimEnvironment
  \begin{VerbatimOut}{\jobname.pyg.lst}% NO char whatsoever to the next brace
}

\AtBeginDocument{
  \@ifundefined{thechapter}{
    \newcounter{pllisting}
    \gdef\thepllisting{\@arabic\c@pllisting}
  }{
    \newcounter{pllisting}[chapter]
    \gdef\thepllisting%
      {\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@pllisting}
  }
}

% Set default key values
\plset{captionfont,abovecaptionskip,belowcaptionskip,listingname,listingnamefont,%
       bgcolor,language,encoding,mathescape,texcl,style,gobble%
}
\endinput
