\ProvidesPackage{listings}
\RequirePackage{keyval}
\@primitives{listings}
%%%%%%%%%%% interface keyval
\def\lst@define@key#1#2{\define@key{lst}{#1}{\@funcall{#2}{##1}}}
\newenvironment{lst@protect}
  {\@clearstyle\def\%{\@print{\%}}%
   \def\#{\@print{\#}}%
   \def\${\@print{\$}}%
   \def\{{\@print{\{}}%
   \def\}{\@print{\}}}%
   \def\_{\@print{\_}}%
   \def\&{\@print{\&}}%
   \def\ref{\@print{\ref}}}
  {}
\def\lst@define@key@opt#1#2#3{\define@key{lst}{#1}[#3]
  {\@callopt{#2}{\begin{lst@protect}##1\end{lst@protect}}}}
\def\ignore@key#1{\define@key{lst}{#1}{}}
%%%%%%%%%%%
% Helpers %
%%%%%%%%%%%
\def\lstinfo#1{\typeout{LST, #1}}
\def\lst@iter#1#2{%\hva@warn{\lst@iter '#1' '#2'}%
\@iter#1,{#2}}
%For getting rid of two (optional) levels of braces
\def\lst@eat@again#1XXX#2{\@callsubst{#2}{#1\@empty\@empty\@empty\@empty\@empty}}
\def\lst@eat@key#1XXX#2{\@callsubst{\lst@eat@again}{#1XXX#2}}
%%%%%%%% Interface with keyval
\def\lstset#1{\@setkeys{lst}{#1}}
%%%%%%%% Def if not empty
\newcommand{\lst@defne}[2]{\ifx#1\@empty\def#1{#2}\fi}
%%%%%%%%%%%%%
% Toks regs %
%%%%%%%%%%%%%
\newcommand{\lst@Empty}[1]{\let#1\@empty}
%%%%%%%%%%%%
% Hooks    %
%%%%%%%%%%%%
\newcommand{\lst@Hook}[1]
  {\newtokens{\csname lsthk@#1\endcsname}}
\newcommand{\lst@ResetHook}[1]
  {\resettokens{\csname lsthk@#1\endcsname}}
\newcommand{\lst@UseHook}[1]
  {\csname lsthk@#1\endcsname}
\newcommand{\lst@AddToHook}[2]
  {\addtokens{\csname lsthk@#1\endcsname}{#2}}
\newcommand{\lst@AddToHookExe}[2]
  {\lst@AddToHook{#1}{#2}#2}
\lst@Hook{EOL}\lst@Hook{InitVarEOL}
\lst@Hook{InitVarBOL}\lst@Hook{EveryLine}\lst@Hook{LastLine}
\lst@Hook{PreSet}
\lst@Hook{InitVar}
\lst@Hook{Init}
\lst@Hook{DeInit}
\lst@Hook{SelectCharTable}
\lst@Hook{SetLanguage}
\lst@Hook{AfterSetLanguage}
\lst@AddToHook{SetLanguage}{\lst@ResetHook{AfterSetLanguage}}
\lst@Hook{SetStyle}
\def\lststy@{\lsthk@EmptyStyle}
\lst@Hook{EmptyStyle}
\lst@Hook{EmptyLanguage}
\lst@Hook{InlineUnsave}
%%Avoid explicit TT in listings, they are useless with style sheets
\lst@AddToHook{Init}{\let\tt\@empty}
%%%%%%%%%%%%
% Keywords %
%%%%%%%%%%%%
\newtokens{\lst@delete@all@kwd@class}%
\def\lst@record@kwd@class#1#2{%
%\hva@warn{RECORD '#1' '#2'}%
\ifu\csname lst@allkwd@#1\endcsname%
\addtokens{\lst@delete@all@kwd@class}{\lst@delete@kwd@class{#1}}\fi%
\providecommand{\csname lst@allkwd@#1\endcsname}{}%
\lst@AddTo,{\csname lst@allkwd@#1\endcsname}{\lst@normalize{#2}}}
\def\lst@delete@kwd@class#1{%
%\hva@warn{DELETE '#1'}%
\ifu\csname lst@allkwd@#1\endcsname\else
\@callprim{\lst@delete@kwds}{\{\csname lst@allkwd@#1\endcsname\}}%
\lst@Empty{\csname lst@allkwd@#1\endcsname}\fi}
\newcommand{\lst@allotherkeywords}{}
\def\lst@record@okwds#1{\lst@AddTo,{\lst@allotherkeywords}{\lst@normalize{#1}}}
\newcommand{\lst@alldirectives}{}
\def\lst@record@directives#1{\lst@AddTo,{\lst@alldirectives}{\lst@normalize{#1}}}
\lst@AddToHook{SetLanguage}
  {\lst@delete@all@kwd@class%
  \@callprim{\lst@delete@okwds}{\{\@rawchars{\lst@allotherkeywords}\}}%
  \@callprim{\lst@delete@directives}{\{\lst@alldirectives\}}}
\def\lst@install@kwd#1#2{%
%\hva@warn{INSTALL: #2 -> #1}%
\def\csname lstk@\lst@normalize{#2}\endcsname{#1}}
\def\lst@install@kwds#1#2{%
%     \hva@warn{RESET: «#1»}%
    \lst@delete@kwd@class{#1}%
    \lst@record@kwd@class{#1}{#2}%
    \lst@iter{\lst@install@kwd{\csname lst@keywordstyle#1\endcsname}}{#2}}
\def\lst@more@kwds#1#2{%
%    \hva@warn{MORE: «#1»}%
    \lst@record@kwd@class{#1}{#2}%
    \lst@iter{\lst@install@kwd{\csname lst@keywordstyle#1\endcsname}}{#2}}
\def\lst@install@directive#1#2{\def\csname lstd@\lst@normalize{#2}\endcsname{#1}}
\def\lst@install@directives
  #1#2{%
    \lst@directivestrue%
    \lst@record@directives{#2}%
    \lst@iter{\lst@install@directive{#1}}{#2}}
\def\lst@install@okwds
  #1#2{%
    \lst@record@okwds{#2}%
    \lst@iter{\lst@install@kwd{#1}}{#2}}
\def\lst@delete@kwd#1{%
%\hva@warn{DELETE #1}%
\def\csname lstk@\@rawchars{#1}\endcsname{\lst@identifierstyle}}
\def\lst@delete@directive
  #1{\def\csname lstd@\@rawchars{#1}\endcsname{}}
\def\lst@delete@okwds#1{\lst@iter{\lst@delete@okwd}{#1}}
\def\lst@delete@kwds#1{%\hva@warn{\lst@delete@kwds '#1'}%
\lst@iter{\lst@delete@kwd}{#1}}
\def\lst@delete@directives#1{\lst@iter{\lst@delete@directive}{#1}}
\def\lst@delete@okwd
  #1{\def\csname lstk@\@rawchars{#1}\endcsname{}}
\def\lst@delay@install#1#2{\lst@AddToHook{AfterSetLanguage}
{\lst@install@kwds{#1}{#2}}}
\def\lst@delay@more#1#2{\lst@AddToHook{AfterSetLanguage}
{\lst@more@kwds{#1}{#2}}}
\def\lst@delay@delete#1#2{\lst@AddToHook{AfterSetLanguage}{\lst@delete@kwds{#2}}}
\def\lst@keywords@key#1#2\@empty{\lst@install@kwds{#1}{#2}}
\def\lst@morekeywords@key#1#2\@empty{\lst@more@kwds{#1}{#2}}
\def\lst@deletekeywords@key#1#2\@empty{\lst@delete@kwds{#2}}
\define@key{lst}{keywords}[]{\@callsubstopt{\lst@keywords@key}{1}{#1\@empty}}
\define@key{lst}{morekeywords}[]
  {\@callsubstopt{\lst@morekeywords@key}{1}{#1\@empty}}
\define@key{lst}{deletekeywords}[]
  {\@callsubstopt{\lst@deletekeywords@key}{1}{#1\@empty}}
%%2nd
\define@key{lst}{ndkeywords}[]
  {\@funcall{\lst@keywords@key}{{2}#1\@empty}}
\define@key{lst}{morendkeywords}[]
  {\@funcall{\lst@morekeywords@key}{{2}#1\@empty}}
\define@key{lst}{deletendkeywords}[]
  {\@funcall{\lst@deletekeywords@key}{{2}#1\@empty}}
%%3rd
\define@key{lst}{rdkeywords}[]{\@callsubstopt{\lst@keywords@key}{3}{#1\@empty}}
\define@key{lst}{morerdkeywords}[]
  {\@funcall{\lst@morekeywords@key}{{3}#1\@empty}}
\define@key{lst}{deleterdkeywords}[]
  {\@funcall{\lst@deletekeywords@key}{{3}#1\@empty}}
%%% Directives
\def\lst@delay@dinstall#1#2{\lst@AddToHook{AfterSetLanguage}{\lst@install@directives{#1}{#2}}}
\def\lst@delay@ddelete#1{\lst@AddToHook{AfterSetLanguage}{\lst@delete@directives{#1}}}
\define@key{lst}{directives}{\lst@delay@dinstall\lst@directivestyle#1}
\define@key{lst}{moredirectives}{\lst@delay@dinstall\lst@directivestyle#1}
%%%%%%% Styles applied to keywords, strings etc %%%%%%
\newcommand{\lst@keywordstyle@key}[2][1]
{\def\csname lst@keywordstyle#1\endcsname##1{#2{##1}}}
\def\lst@keywordstyle{\csname lst@keywordstyle1\endcsname}
\define@key{lst}{keywordstyle}{\@calloptsimple{\lst@keywordstyle@key}{#1}}
%\define@key{lst}{keywordstyle}{\def\lst@kwdstyle##1{#1{##1}}}
\define@key{lst}{ndkeywordstyle}{\@calloptsimple{\lst@keywordstyle@key}{[2]#1}}
\define@key{lst}{rdkeywordstyle}{\@calloptsimple{\lst@keywordstyle@key}{[3]#1}}
\define@key{lst}{otherkeywordstyle}{\def\lst@okwdstyle##1{#1{##1}}}
\define@key{lst}{directivestyle}{\def\lst@directivestyle##1{#1{##1}}}
\define@key{lst}{identifierstyle}[]{\def\lst@identifierstyle##1{#1{##1}}}
\define@key{lst}{basicstyle}{\def\lst@basicstyle{#1}}
\define@key{lst}{commentstyle}{\def\lst@commentstyle{#1}}
\define@key{lst}{stringstyle}{\def\lst@stringstyle{#1}}
\lst@AddToHook{EmptyStyle}{\let\lst@stringstyle\@empty}
\lst@AddToHook{EmptyStyle}{\let\lst@commentstyle\em}
\lst@AddToHook{EmptyStyle}{\let\lst@identifierstyle\@empty}
\lst@AddToHook{EmptyStyle}{\let\lst@basicstyle\@empty}
\lst@AddToHook{EmptyStyle}{\lst@keywordstyle@key[1]{\bfseries}}
\lst@AddToHook{EmptyStyle}
{\lst@keywordstyle@key[2]{\csname lst@keywordstyle1\endcsname}}
\lst@AddToHook{EmptyStyle}
{\lst@keywordstyle@key[3]{\csname lst@keywordstyle1\endcsname}}
\lst@AddToHook{EmptyStyle}{\def\lst@okwdstyle{\lst@keywordstyle}}
\lst@AddToHook{EmptyStyle}{\def\lst@directivestyle{\lst@keywordstyle}}
\lst@AddToHook{EmptyStyle}{\def\lst@directivestyle{\lst@keywordstyle}}
%%%%%% Emph identifiers, classes seems to be implemented properly
\def\lst@defall#1#2{\def\csname #1\endcsname{#2}}
\def\lst@emphstyle@key#1#2\@empty{\lst@defall{lst@emphstyle#1}{#2}}
\def\lst@emphstyle{\csname lst@emphstyle1\endcsname}
\lst@AddToHook{EmptyStyle}{\let\csname lst@emphstyle1\endcsname\@empty}
\define@key{lst}{emphstyle}{\@callsubstopt{\lst@emphstyle@key}{1}{#1\@empty}}
\def\lst@record@class#1#2{%
  \providecommand{\csname lst@allemph@#1\endcsname}{}%
  \lst@AddTo,{\csname lst@allemph@#1\endcsname}{\lst@normalize{#2}}}
\def\lst@delete@class#1{%
  \ifu\csname lst@allemph@#1\endcsname\else
  \@callprim{\lst@delete@kwds}{\{\csname lst@allemph@#1\endcsname\}}%
  \lst@Empty{\csname lst@allemph@#1\endcsname}\fi}
\def\lst@moreemph@key#1#2\@empty{%
  \lst@record@class{#1}{#2}%
  \lst@iter{\lst@install@kwd{\csname lst@emphstyle#1\endcsname}}{#2}}
\define@key{lst}{moreemph}{\@callsubstopt{\lst@moreemph@key}{1}{#1\@empty}}
%%%Class number is not checked...
\def\lst@deleteemph@key#1#2\@empty{\lst@iter{\lst@delete@kwd}{#2}}
\define@key{lst}{deleteemph}{\@callsubstopt{\lst@deleteemph@key}{1}{#1\@empty}}
\def\lst@emph@key#1#2\@empty{%
  \lst@delete@class{#1}%  
  \lst@record@class{#1}{#2}%
  \lst@iter{\lst@install@kwd{\csname lst@emphstyle#1\endcsname}}{#2}}
\define@key{lst}{emph}[]{\@callsubstopt\lst@emph@key{1}{#1\@empty}}
%%%%%% Style definitions
\define@key{lst}{style}[{}]{\lsthk@SetStyle\csname lst@sty#1\endcsname}
\newcommand{\lstdefinestyle}[2]
  {\def\csname lst@sty#1\endcsname{\lstset{#2}}}
%%%%%% Language definitions
\newcommand{\lst@define@dd}[2][]
  {\newcommand{\csname lstdd@\MakeLowercase{#2}\endcsname}{\MakeLowercase{#1}}}
\newcommand{\lst@mklang}[2][]
  {\ifthenelse{\equal{#1}{}}
    {lstlang@\MakeLowercase{#2}@\csname lstdd@\MakeLowercase{#2}\endcsname}
    {lstlang@\MakeLowercase{#2}@\MakeLowercase{#1}}}
\newcommand{\lst@language@key}[2][]
  {\lst@UseHook{SetLanguage}%
  \csname\lst@mklang[#1]{#2}\endcsname%
  \lst@UseHook{AfterSetLanguage}}
%  \lst@UseHook{AfterSetLanguage}}
\lst@define@key@opt{language}{\lst@language@key}{}
\newcommand{\lst@checkdd}[2]
  {\@ifundefined{lstdd@\MakeLowercase{#1}}{\lst@define@dd[#2]{#1}}{}}
%Awfull hack to avoid defining XML language (hevea crash)
\def\lst@XML{XML}
\newcommand{\lst@definelanguage@}[3]
  {\def\@tmp{#1}\ifx\@tmp\lst@XML\else
  \lst@checkdd{#1}{#2}\def\csname\lst@mklang[#2]{#1}\endcsname{\lstset{#3}}\fi}
\newcommand{\lst@derivelanguage@}[5]
  {\lst@checkdd{#1}{#2}%
  \def\csname\lst@mklang[#2]{#1}\endcsname{\csname\lst@mklang[#4]{#3}\endcsname
%    \lst@UseHook{AfterSetLanguage}\lst@ResetHook{AfterSetLanguage}%
    \lstset{#5}}}
\lst@define@key@opt{defaultdialect}{\lst@define@dd}{}
%%%%%%% Frames
\newstyle{.lstlisting}{font-family:monospace;white-space:pre;margin-right:auto;margin-left:0pt;text-align:left}
\setenvclass{lstlisting}{lstlisting}
\setenvclass{lstframe}{lstframe}
\def\lst@bgcolor@in@temp{%
   \let\old@color\color%
   \renewcommand{\color}[2][!*!]{\@getstylecolor[##1]{##2}}%
   \prim@def\lst@temp@bgcolor{background-color:\lst@bgcolor;}%
   \let\color\old@color}
\def\lst@rulecolor@in@temp{%
   \let\old@color\color%
   \renewcommand{\color}[2][!*!]{\@getstylecolor[##1]{##2}}%
   \prim@def\lst@temp@rulecolor{border-color:\lst@rulecolor;}%
   \let\color\old@color}
\def\lst@border@none{border-style:none;}
\def\lst@border@leftline{border-left-style:solid;}
\def\lst@border@topline{border-top-style:solid;}
\def\lst@border@bottomline{border-bottom-style:solid;}
\def\lst@border@lines{border-style:solid none;}
\def\lst@border@single{border-style:solid;}
\def\lst@border@shadowbox{border-style:outset;}
\def\lst@frame@in@temp{%
   \ifu\csname lst@border@\lst@frame\endcsname
   \prim@def\lst@temp@frame{\lst@see@frame{\lst@frame}}%
   \else
   \let\lst@temp@frame\csname lst@border@\lst@frame\endcsname
   \fi
}
\let\lst@whitepre\whitepretrue
\lst@AddToHook{InitVar}
  {\ifu\lst@cell@needed
   \def\@@open@lstbox{\@open{DIV}{\envclass@attr{lstlisting}}}%
   \def\@close@lstbox{\@close{DIV}}%
   \else
   \ifu\lst@bgcolor\def\lst@temp@bgcolor{}\else\lst@bgcolor@in@temp\fi%
   \ifu\lst@rulecolor\def\lst@temp@rulecolor{}\else\lst@rulecolor@in@temp\fi%
   \ifu\lst@frame\def\lst@temp@frame{}\else\lst@frame@in@temp\fi%
   \def\@@open@lstbox{\@opencell{\envclass@attr[ ]{lstframe}style="padding:1ex;\lst@temp@bgcolor\lst@temp@frame\lst@temp@rulecolor"}{}{\envclass@attr{lstlisting}}}%
   \def\@close@lstbox{\@closecell}%
   \fi
   \def\@open@lstbox{\@@open@lstbox\lst@whitepre}}
\def\colorwarning{\hva@warn{Colors in listings and 'color' package not loaded}}
\define@key{lst}{backgroundcolor}
{\ifu\color\colorwarning\else\def\lst@cell@needed{}\def\lst@bgcolor{#1}\fi}
\define@key{lst}{frame}{\def\lst@cell@needed{}\def\lst@frame{#1}}
\define@key{lst}{rulecolor}
{\ifu\color\colorwarning\else\def\lst@cell@needed{}\def\lst@rulecolor{#1}\fi}
\lst@iter{\ignore@key}
   {framexleftmargin,framexrightmargin,framextopmargin,framexbottommargin}
\lst@iter{\ignore@key}{fillcolor,rulesepcolor}
\lst@iter{\ignore@key}{framesep,rulesep,framerule}
\lst@iter{\ignore@key}{frameround,captionpos}
%%%%%%% Margins and line shape
\def\lst@doindent#1{}
\define@key{lst}{indent}{\def\lst@doindent{\hspace{#1}}}
\define@key{lst}{xleftmargin}{\def\lst@doindent{\hspace{#1}}}
\lst@iter{\ignore@key}
  {linewitdh,xrightmargin,resetmargin,prebreak,postbreak,%
breakindent,breakautoindent}
%%%%%%% Lines
%%% Those two I don't care
\define@key{lst}{numbersep}{}
\define@key{lst}{numberblanklines}{}
\newcounter{lstlabel}\newcounter{lstlastline}
\lst@AddToHook{InitVar}{\let\@currentlabel\thelstlabel}
\newcommand{\lst@saved@lastline}
  {\@ifundefined{lst@intname}{}
    {\@ifundefined{thelst@saved@\lst@intname}
      {0}
      {\value{lst@saved@\lst@intname}}}}
\def\@lst@compute@firstline{%
\@ifundefined{lst@intname}{\def\lst@firstline{1}}
   {\@ifundefined{thelst@saved@\lst@intname}{\def\lst@firstline{1}}
   {\def\lst@firstline{\csname thelst@saved@\lst@intname\endcsname+1}}}}
\def\@auto{auto}\def\@last{last}
\lst@AddToHook{Init}
  {\@ifundefined{lst@firstline}
    {\@lst@compute@firstline}
    {\ifx\lst@firstline\@auto\@lst@compute@firstline\fi
     \ifx\lst@firstline\@last\def\lst@firstline{\thelstlastline+1}\fi}}
\define@key{lst}{name}{\def\lst@intname{#1}}
\define@key{lst}{firstlabel}{\def\lst@firstline{#1}}
\define@key{lst}{firstnumber}{\def\lst@firstline{#1}}
\define@key{lst}{advancelabel}{\def\lst@firstline{\lst@saved@lastline+#1+1}}
\lst@AddToHook{Init}{\setcounter{lstlabel}{\lst@firstline-1}}
\lst@AddToHook{DeInit}
  {\setcounter{lstlastline}{\value{lstlabel}}%
   \@ifundefined{lst@intname}{}
     {\@ifundefined{thelst@saved@\lst@intname}
       {\newcounter{lst@saved@\lst@intname}%
       \gdef\csname thelst@saved@\lst@intname\endcsname{\arabic{lst@saved@\lst@intname}}}
       {}%
     \setcounter{lst@saved@\lst@intname}{\value{lstlabel}}}}
\define@key{lst}{labelstyle}{\def\lst@labelstyle##1{{#1{##1}}}}
\define@key{lst}{numberstyle}{\def\lst@labelstyle##1{{#1{##1}}}}
\newcommand{\lst@labelstyle}[1]{#1}
\define@key{lst}{labelstep}{\def\lst@labelstep{#1}}
\define@key{lst}{stepnumber}{\def\lst@labelstep{#1}}
\lst@AddToHook{InlineUnsave}{\def\lst@labelstep{1}}
\lst@AddToHookExe{EmptyStyle}{\def\lst@labelstep{1}}
\define@key{lst}{labelsep}{\def{\lst@labelsep}{\hspace{#1}}}
\newcommand{\lst@labelsep}{\hspace{1ex}}
\newcommand{\lst@pad}[1]{\@pad{\@print{ }}{4}{#1}}
\newcommand{\lst@linenumber@}[1]
  {\lst@labelstyle{\lst@pad{#1}}\lst@labelsep}
\newboolean{lst@numbers}
\newcommand{\lst@linenumber}
  {\ifthenelse{\boolean{lst@numbers}}
     {\ifthenelse{\lst@labelstep=0}
       {}
       {\ifthenelse
         {0=\value{lstlabel}-\(\value{lstlabel}/\lst@labelstep\)*\lst@labelstep}
         {\lst@linenumber@{\thelstlabel}}
         {\lst@linenumber@{}}}}
     {}}
\lst@AddToHook{InitVarEOL}{\stepcounter{lstlabel}}
\newsavebox{\@prevlines}
\newsavebox{\@curline}
\ifhtml
\ifu\lst@@br\def\lst@@br{
}\fi
\def\lst@@@br{\global\let\lst@br\lst@@br}%To avoid output of first line skip
\else
\ifu\lst@@br\let\lst@@br\@br\fi
\let\lst@@@br\lst@@br
\fi
\lst@AddToHook{Init}
{\def\lst@br{}}
\newcommand{\lst@forget@lastline}
{\sbox{\@curline}{}\setcounter{lst@spaces}{0}\addtocounter{lstlabel}{-1}}
\newcommand{\@NewLine@}
{\usebox{\@prevlines}\usebox{\@curline}\lst@spaces%
\sbox{\@prevlines}{}\sbox{\@curline}{}\global\let\@NewLine\lst@spaces}
\lst@AddToHook{EveryLine}{\setcounter{lst@spaces}{0}%
\sbox{\@prevlines}{\usebox{\@prevlines}\usebox{\@curline}}%
\sbox{\@curline}{\lst@br\@rawchars{\lst@doindent}\lst@linenumber}%
\global\let\@NewLine\@NewLine@}
\lst@AddToHook{LastLine}{\setcounter{lst@spaces}{0}%
\sbox{\@prevlines}{\usebox{\@prevlines}\usebox{\@curline}}%
\iflst@showlines\usebox{\@prevlines}\fi\sbox{\@prevlines}{}\sbox{\@curline}{}%
\global\let\@NewLine\@NewLine@}
\lst@AddToHook{Init}{\sbox{\@prevlines}{}\sbox{\@curline}{}\global\let\@NewLine\lst@spaces}
\def\lst@numbers@none{\setboolean{lst@numbers}{false}}
\def\lst@numbers@left{\setboolean{lst@numbers}{true}}
\let\lst@numbers@right\lst@numbers@left
\def\lst@numbers#1{\csname lst@numbers@#1\endcsname}
\define@key{lst}{numbers}{\lst@numbers{#1}}
\lst@AddToHookExe{EmptyStyle}{\lst@numbers{none}}
%%%%%% TeX escapes
\define@key{lst}{texcl}[true]{\def\lst@texcl{#1}}
\setboolean{lst@texcl}{false}
\lst@AddToHook{Init}
  {\@ifundefined{lst@texcl}{}
     {\setboolean{lst@texcl}{\lst@boolean{\lst@texcl}}}}
\lst@AddToHook{DeInit}{\setboolean{lst@texcl}{false}}
\define@key{lst}{mathescape}[true]{\def\lst@mathescape{#1}}
\setboolean{lst@mathescape}{false}
\lst@AddToHook{Init}
  {\@ifundefined{lst@mathescape}{}
     {\setboolean{lst@mathescape}{\lst@boolean{\lst@mathescape}}}}
\lst@AddToHook{DeInit}{\setboolean{lst@mathescape}{false}}
\def\lst@BET{}\def\lst@EET{}
\newcommand{\lst@Esc@key}[2]
  {\def\lst@BET{\@rawchars{#1}}\def\lst@EET{\@rawchars{#2}}}
\define@key{lst}{escapeinside}{\@callsubst{\lst@Esc@key}{#1{}}}
\define@key{lst}{escapechar}[{}]{\lst@Esc@key#1#1}
\def\lst@escapebegin{}
\define@key{lst}{escapebegin}[]{\def\lst@escapebegin{#1}}
\def\lst@escapeend{}
\define@key{lst}{escapeend}[]{\def\lst@escapeend{#1}}
%%%%%% Ignore input
\newcommand{\lst@gobble}{0}
\define@key{lst}{gobble}{\def\lst@gobble{#1}}
\define@key{lst}{first}{\def\lst@first{#1}}
\define@key{lst}{firstline}{\def\lst@first{#1}}
\define@key{lst}{last}{\def\lst@last{#1}}
\define@key{lst}{lastline}{\def\lst@last{#1}}
\define@key{lst}{linerange}{\def\lst@linerange{#1}}
\lst@AddToHook{PreSet}
{\newcommand{\lst@first}{1}\newcommand{\lst@last}{99999}%
\let\lst@linerange\@empty}
\lst@AddToHook{Init}
{\ifx\lst@linerange\@empty
\prim@def\lst@linerange{{\lst@first}-{\lst@last}}\fi}
% boolean includerangemarker key
\define@key{lst}{includerangemarker}[true]{\def\lst@includerangemarker{#1}}
\lst@AddToHook{Init}
{\@ifundefined{lst@includerangemarker}
  {\setboolean{lst@includerangemarker}{true}}
  {\setboolean{lst@includerangemarker}{\lst@boolean{\lst@includerangemarker}}}}
%prefix/suffix of markers, one prefix/suffix only
\newtokens{\lst@rangebeginprefix}\newtokens{\lst@rangeendprefix}%
\newtokens{\lst@rangebeginsuffix}\newtokens{\lst@rangeendsuffix}%
\addtokens{\lst@rangebeginprefix}{}\addtokens{\lst@rangeendprefix}{}%
\addtokens{\lst@rangebeginsuffix}{}\addtokens{\lst@rangeendsuffix}{}%
\define@key{lst}{rangebeginprefix}{\addtokens{\lst@rangebeginprefix}{#1}}
\define@key{lst}{rangebeginsuffix}{\addtokens{\lst@rangebeginsuffix}{#1}}
\define@key{lst}{rangeendprefix}{\addtokens{\lst@rangeendprefix}{#1}}
\define@key{lst}{rangeendsuffix}{\addtokens{\lst@rangeendbeginsuffix}{#1}}
\define@key{lst}{rangeprefix}
  {\addtokens{\lst@rangebeginprefix}{#1}\addtokens{\lst@rangeendprefix}{#1}}
\define@key{lst}{rangesuffix}
  {\addtokens{\lst@rangebeginsuffix}{#1}\addtokens{\lst@rangeendsuffix}{#1}}
% boolean print key
\define@key{lst}{print}[true]{\def\lst@print{#1}}
\lst@AddToHook{Init}
  {\@ifundefined{lst@print}{\setboolean{lst@print}{true}}
     {\setboolean{lst@print}{\lst@boolean{\lst@print}}}}
% showlines
\define@key{lst}{showlines}[true]{\def\lst@showlines{#1}}
\lst@AddToHook{Init}
{\@ifundefined{lst@showlines}
  {\setboolean{lst@showlines}{false}}
  {\setboolean{lst@showlines}{\lst@boolean{\lst@showlines}}}}
%%%%%%% Strings
\newcommand{\lst@inactivate}[1]{\let#1\@empty}
\newcommand{\lst@activate}[1]{#1}
\lst@Hook{String}
\lst@AddToHook{Init}{\let\lst@do\lst@activate\lst@UseHook{String}}
\lst@AddToHook{SetLanguage}
{\let\lst@do\lst@inactivate\lst@UseHook{String}}
%\newcommand{\lst@stringizer@key@add}[2][]
%  {\lst@AddToHook{String}{\lst@def@stringizer{#1}{#2}}}
%\newcommand{\lst@stringizer@key}[2][]
%  {\lst@ResetHook{String}\lst@AddToHook{String}{\lst@def@stringizer{#1}{#2}}}
%\newcommand{\lst@stringizer@key@delete}[2][]{\lst@ResetHook{String}}
%\lst@define@key@opt{stringizer}{\lst@stringizer@key}{}
%\lst@define@key@opt{string}{\lst@stringizer@key}{}
%\lst@define@key@opt{morestring}{\lst@stringizer@key@add}{}
%\lst@define@key@opt{deletestring}{\lst@stringizer@key@delete}{}
%%Real keys, that take all three arguments
\newcommand{\lst@DS@key}[3]
{\def\csname @is@string@#3\endcsname{}}%
\newcommand{\lst@SS@key}[3]
{\let\lst@do\lst@inactivate\lst@UseHook{String}%
\lst@MS@key{#1}{#2}{#3}}%
\newcommand{\lst@MS@key}[3]
{\ifu\csname @recorded@string@#3\endcsname
\let\csname @recorded@string@#3\endcsname\@empty
\lst@AddToHook{String}
{\lst@do{\csname @is@string@#3\endcsname}}\fi
\def\csname @is@string@#3\endcsname{\lst@def@stringizer{#1}{#2}{#3}}}%
%%Parse the  [][]{} arguments
\newcommand{\lst@stringarg@one}[1][b]{\def\lst@arg@one{#1}\lst@stringarg@two}
\newcommand{\lst@stringarg@two}[1][stringstyle]
{\def\lst@arg@two{#1}\lst@stringarg@three}
\newcommand{\lst@stringarg@three}[1]
{\lst@cmd{\lst@arg@one}{\lst@arg@two}{#1}}
%%%Real key handlers
\newcommand{\lst@deletetring@key}{\let\lst@cmd\lst@DS@key\lst@stringarg@one}
\newcommand{\lst@string@key}{\let\lst@cmd\lst@SS@key\lst@stringarg@one}
\newcommand{\lst@morestring@key}{\let\lst@cmd\lst@MS@key\lst@stringarg@one}
\define@key{lst}{deletestring}{\@callsubst{\lst@deletestring@key}{#1}}
\define@key{lst}{stringizer}{\@callsubst{\lst@string@key}{#1}}
\define@key{lst}{string}{\@callsubst{\lst@string@key}{#1}}
\define@key{lst}{morestring}{\@callsubst{\lst@morestring@key}{#1}}
% stringspaces
\define@key{lst}{stringspaces}[true]{\def\lst@stringspaces{#1}}
\define@key{lst}{showstringspaces}[false]{\def\lst@stringspaces{#1}}
\setboolean{lst@stringspaces}{false}
\lst@AddToHook{Init}
  {\@ifundefined{lst@stringspaces}{}
     {\setboolean{lst@stringspaces}{\lst@boolean{\lst@stringspaces}}}}
\lst@AddToHook{DeInit}{\setboolean{lst@stringspaces}{false}}
%%%%%% Comments
% Line comments
\newcommand{\lst@LC@key}[3]
  {\lst@lExtend\lst@DefineComments@LC
    {\lst@line@comment{#1}{#2}{\@rawchars{#3}}}}
\define@key{lst}{commentline}{\lst@LC@key{commentstyle}{#1}}
\lst@AddToHookExe{SetLanguage}{\let\lst@DefineComments@LC\@empty}
\lst@AddToHook{SelectCharTable}{\lst@DefineComments@LC}
% Nested comments
\newcommand{\lst@NC@key}[3]
  {\lst@lExtend\lst@DefineComments@NC
     {\lst@nested@comment{#1}{\@rawchars{#2}}{\@rawchars{#3}}}}
\define@key[2]{lst}{nestedcomment}{\lst@NC@key{commentstyle}{#1}{#2}}
\lst@AddToHookExe{SetLanguage}{\let\lst@DefineComments@NC\@empty}
\lst@AddToHook{SelectCharTable}{\lst@DefineComments@NC}
% Balanced comments
\newcommand{\lst@SC@key}[3]
   {\lst@lExtend\lst@DefineComments@SC
     {\lst@balanced@comment{#1}{\@rawchars{#2}}{\@rawchars{#3}}}}
\define@key[2]{lst}{singlecomment}{\lst@SC@key{commentstyle}{#1}{#2}}
\lst@AddToHookExe{SetLanguage}{\let\lst@DefineComments@SC\@empty}
\lst@AddToHook{SelectCharTable}{\lst@DefineComments@SC}
% Double comments: more or less single comment twice
\newcommand{\lst@DC@key}[5]
   {\lst@lExtend\lst@DefineComments@DC
      {\lst@balanced@comment{#1}{\@rawchars{#2}}{\@rawchars{#3}}}%
   \lst@lExtend\lst@DefineComments@DC
      {\lst@balanced@comment{#1}{\@rawchars{#4}}{\@rawchars{#5}}}}
\define@key[4]{lst}{doublecomment}{\lst@DC@key{commentstyle}{#1}{#2}{#3}{#4}}
\lst@AddToHookExe{SetLanguage}{\let\lst@DefineComments@DC\@empty}
\lst@AddToHook{SelectCharTable}{\lst@DefineComments@DC}
%%%%%% New interface for comments grr...
\newcommand{\lst@morecomment@d}[5][commentstyle]
{\lst@DC@key{#1}{#2}{#3}{#4}{#5}}
\newcommand{\lst@morecomment@s}[3][commentstyle]{\lst@SC@key{#1}{#2}{#3}}
\newcommand{\lst@morecomment@is}[3][commentstyle]
{\hva@warn{Invisible comments not implemented}%
\lst@SC@key{#1}{#2}{#3}}
\newcommand{\lst@morecomment@n}[3][commentstyle]{\lst@NC@key{#1}{#2}{#3}}
%Notice the horible trick to parse morecomment=[l]\\
\def\lst@LC@parse#1#2#3\@empty{\lst@LC@key{#1}{#2}{#3}}
\newcommand{\lst@morecomment@l}[1][commentstyle]{\lst@LC@parse{visible}{#1}}
\newcommand{\lst@morecomment@il}[1][commentstyle]
{\hva@warn{Invisible comments not implemented}%
\lst@LC@parse{visible}{#1}}
%%%
\def\lst@deletecomment@d{\let\lst@DefineComments@DC\@empty}
\def\lst@deletecomment@s{\let\lst@DefineComments@SC\@empty}
\def\lst@deletecomment@n{\let\lst@DefineComments@NC\@empty}
\def\lst@deletecomment@l{\let\lst@DefineComments@LC\@empty}
\def\lst@deletecomment@all{%
\lst@deletecomment@d\lst@deletecomment@s%
\lst@deletecomment@n\lst@deletecomment@l}
\def\lst@till@empty#1\@empty{}
\newcommand{\lst@morecomment@key}[1][]
  {\ifu\csname lst@morecomment@#1\endcsname
    \hva@warn{Unknown comment type '#1'}%
    \let\lst@next\lst@till@empty\else
    \let\lst@next\csname lst@morecomment@#1\endcsname\fi
    \lst@next}
\newcommand{\lst@deletecomment@key}[1][]
  {\ifu\csname lst@deletecomment@#1\endcsname
   \hva@warn{Unknown comment type '#1'}\else
   \csname lst@deletecomment@#1\endcsname\fi
   \lst@till@empty}
\newcommand{\lst@comment@key}[1][]
  {\ifu\csname lst@morecomment@#1\endcsname
    \hva@warn{Unknown comment type '#1'}%
    \let\lst@next\lst@till@empty\else
    \csname lst@deletecomment@all\endcsname
    \let\lst@next\csname lst@morecomment@#1\endcsname\fi
    \lst@next}
\define@key{lst}{deletecomment}{\@callsubst{\lst@deletecomment@key}{#1\@empty}}
\define@key{lst}{comment}{\@callsubst{\lst@comment@key}{#1\@empty}}
\define@key{lst}{morecomment}{\@callsubst{\lst@morecomment@key}{#1\@empty}}
%%%%%%% General delimiters
%Line delimiters
\newcommand{\lst@LD@key}[3]{
\lst@lExtend\lst@Define@LD{\lst@line@delim{#1}{#2}{\@rawchars{#3}}}}
\lst@AddToHookExe{SetLanguage}{\let\lst@Define@LD\@empty}
\lst@AddToHook{SelectCharTable}{\lst@Define@LD}
%Single delimiters
\newcommand{\lst@SD@key}[4]
{\lst@lExtend\lst@Define@SD
{\lst@single@delim{#1}{#2}{\@rawchars{#3}}{\@rawchars{#4}}}}
\lst@AddToHookExe{SetLanguage}{\let\lst@Define@SD\@empty}
\lst@AddToHook{SelectCharTable}{\lst@Define@SD}
%%General interface
\def\lst@deletedelim@l{\let\lst@Define@LC\@empty}
\def\lst@deletedelim@s{\let\lst@Define@SD\@empty}
\def\lst@deletedelim@all{\lst@deletedelim@l\lst@deletedelim@s}
\newcommand{\lst@deletedelim@key}[1][]
{\ifu\csname lst@deletedelim@#1\endcsname
\hva@warn{Unknown delimiter type '#1'}\else
\csname lst@deletedelim@#1\endcsname\fi
\lst@till@empty}
%Trick to parse delim=[l]\\
\def\lst@LD@parse#1#2#3\@empty{\lst@LD@key{#1}{#2}{#3}}
\newcommand{\lst@moredelim@l}[1][\@empty]{\lst@LD@parse{visible}{#1}}
\newcommand{\lst@moredelim@il}[1][\@empty]{\lst@LD@parse{invisible}{#1}}
\newcommand{\lst@moredelim@s}[3][\@empty]{\lst@SD@key{visible}{#1}{#2}{#3}}
\newcommand{\lst@moredelim@is}[3][\@empty]{\lst@SD@key{invisible}{#1}{#2}{#3}}
%Awfull hack to ignore moredelim=*[directive]\# present in lstlang?.sty files
%In that case the argument of \lst@moredelim@key is empty
%and \lst@moredelim@ is called to eat all arguments up to the final \@empty
\newcommand{\lst@moredelim@}{\let\lst@next\lst@till@empty\lst@next}
\newcommand{\lst@moredelim@key}[1][]
{\ifu\csname lst@moredelim@#1\endcsname
\hva@warn{Unknown delimiter type '#1'}%
\let\lst@next\lst@till@empty\else
\let\lst@next\csname lst@moredelim@#1\endcsname\fi
\lst@next}
\newcommand{\lst@delim@key}[1][]
{\ifu\csname lst@moredelim@#1\endcsname
\hva@warn{Unknown delimiter type '#1'}%
\let\lst@next\lst@till@empty\else
\csname lst@deletedelim@all\endcsname
\let\lst@next\csname lst@moredelim@#1\endcsname\fi
\lst@next}
\define@key{lst}{deletedelim}{\@callsubst{\lst@deletedelim@key}{#1\@empty}}
\define@key{lst}{delim}{\@callsubst{\lst@delim@key}{#1\@empty}}
\define@key{lst}{moredelim}{\@callsubst{\lst@moredelim@key}{#1\@empty}}
%%%%%%% Extended chars and others
\def\lst@extendedchars{true}
\lst@AddToHook{DeInit}{\setboolean{lst@extendedchars}{true}}
\lst@AddToHookExe{SetLanguage}{\setboolean{lst@extendedchars}{true}}
\lst@AddToHook{SelectCharTable}
{\setboolean{lst@extendedchars}{\lst@boolean{\lst@extendedchars}}}
\define@key{lst}{extendedchars}[true]{\def\lst@extendedchars{#1}}
\define@key{lst}{showspaces}[true]{\def\lst@showspaces{#1}}
\setboolean{lst@showspaces}{false}
\lst@AddToHook{Init}
  {\@ifundefined{lst@showspaces}{}
     {\setboolean{lst@showspaces}{\lst@boolean{\lst@showspaces}}}}
%Tabs...
\define@key{lst}{showtabs}[true]{\def\lst@showtabs{#1}}
\setboolean{lst@showtabs}{false}
\lst@AddToHook{Init}
  {\@ifundefined{lst@showtabs}{}
     {\setboolean{lst@showtabs}{\lst@boolean{\lst@showtabs}}}}
\lst@AddToHook{DeInit}{\setboolean{lst@showtabs}{false}}
\newcommand{\lst@tabsize}{8}
\define@key{lst}{tabsize}[8]{\def\lst@tabsize{#1}}
\define@key{lst}{tab}{\def\lst@tab{#1}}
\lst@iter{\ignore@key}{inputencoding,formfeed}
%%%%%%% Alter char categories
\def\lst@alsoletter{}
\define@key{lst}{alsoletter}{\def\lst@alsoletter{#1}}
%%%%%%% Sensitive keywords
\lst@AddToHookExe{DeInit}{\setboolean{lst@sensitive}{true}}
\lst@AddToHook{SetLanguage}{\setboolean{lst@sensitive}{true}}
\define@key{lst}{sensitive}[true]{\setboolean{lst@sensitive}{\lst@boolean{#1}}}
%%%%%%% Captions
%\lst@AddToHookExe{InlineUnsave}
%{\let\lst@caption\@empty%
%\let\lst@title\@empty\let\lst@label\@empty}
%listing counter
\@ifundefined{thechapter}
{\newcounter{lstlisting}}
{\newcounter{lstlisting}[chapter]
\renewcommand\thelstlisting
{\ifthenelse{\value{chapter} > 0}{\thechapter.}{}\arabic{lstlisting}}}
\lst@AddToHook{InitVar}{\ifu\lst@caption\else\refstepcounter{lstlisting}\fi}
%%Keys
\define@key{lst}{label}{\def\lst@label{#1}}
\define@key{lst}{title}{\def\lst@title{#1}}
%\lst@define@key@opt{caption}{\lst@caption@key}{}
\define@key{lst}{caption}{\@calloptsimple{\lst@caption@key}{#1}}
\newcommand{\lst@caption@key}[2][]{\def\lst@caption{#2}}
%%% Output Caption
\def\listingname{Listing}
\def\@lst@caption
{\ifu\lst@caption%
\ifu\lst@title\else\begin{center}\lst@title\end{center}\fi\else
\begin{center}\listingname~\thelstlisting: %
\ifu\lst@label\else\label{\lst@label}\fi\lst@caption\end{center}\\\fi}
%%%%%%% Output
% Treating spaces with a counter (and not with boxes) enables
% optimized style managment
\newcounter{lst@spaces}
\def\lst@output@space{\stepcounter{lst@spaces}}
\lst@AddToHook{Init}{\setcounter{lst@spaces}{0}}
\newsavebox{\normalize@box}
\def\lst@normalize#1{%
  \sbox{\normalize@box}
  {\iflst@sensitive\@rawchars{#1}\else\@rawchars{\MakeLowercase{#1}}\fi}%
  \usebox{\normalize@box}}
\def\lst@output#1#2{%
\@NewLine%\lstinfo{OUT: '#1' '#2'}%
{\ifu\csname lstk@\lst@normalize{#1}\endcsname
  \lst@identifierstyle{#2}\else
  \csname lstk@\lst@normalize{#1}\endcsname{#2}\fi}}
\def\lst@output@other#1#2{%
\@NewLine%\lstinfo{OTHER: '#1' '#2'}%
\ifu\csname lstk@\lst@normalize{#1}\endcsname
  #2\else
  {\csname lstk@\lst@normalize{#1}\endcsname{#2}}\fi}
\newcounter{lst@save@spaces}
\def\save@spaces
  {\setcounter{lst@save@spaces}{\value{lst@spaces}}\setcounter{lst@spaces}{0}}
\def\restore@spaces
  {\setcounter{lst@spaces}{\value{lst@save@spaces}}}
\def\lst@output@directive#1#2{%
%\lstinfo{DIRECTIVE: '#1' '#2' '\thelst@spaces'}%
\save@spaces\@NewLine\restore@spaces%
\ifu\csname lstd@\lst@normalize{#1}\endcsname
  {\lst@directivestyle{\@print{#}}}%
  \lst@spaces\lst@output{#1}{#2}\else
  {\csname lstd@\lst@normalize{#1}\endcsname{\@print{#}\lst@spaces#2}}\fi}
%%%%%% Pre and post (already obsolete)
\let\lst@pre\@empty
\let\lst@post\@empty
\define@key{lst}{pre}{\def\lst@pre{#1}}
\define@key{lst}{post}{\def\lst@post{#1}}
%%% inputencodings (for compatibility only ?)
\def\lst@inputencoding@key#1{\inputencoding{#1}}
\define@key{lst}{inputencoding}{\lst@inputencoding@key{#1}}
%%%%% Column allignement None !
\lst@iter{\ignore@key}
  {columns,flexiblecolumns,keepspaces,basewidth,fontadjust,aboveskip,belowskip,lineskip,emptylines}
%%%%%%% User interface
\newcommand{\lstinputlisting}[2][]
  {\@scaninput{\begin{lstlisting}[#1]{}
}{#2}{\end{lstlisting}}}
\let\lstdefinelanguage\lst@definelanguage
\newcommand{\lstloadlanguage}[1]{}
%%%%%% Driver files are loaded directly!
\lstset{defaultdialect=[95]Ada,
        defaultdialect=[68]Algol,
        defaultdialect=[ANSI]C,
        defaultdialect=[Objective]Caml,
        defaultdialect=[1985]Cobol,
        defaultdialect=[ANSI]C++,
        defaultdialect=[95]Fortran,
        defaultdialect=[3.0]Mathematica,
        defaultdialect=[Standard]Pascal,
        defaultdialect=[67]Simula,
        defaultdialect=[plain]TeX}
\input{lstlang1.hva}
\input{lstlang2.hva}
\input{lstlang3.hva}
%%%%%%%%%%% Inits
\lst@UseHook{SetStyle}\lst@UseHook{EmptyStyle}
\lst@UseHook{SetLanguage}\lst@UseHook{EmptyLanguage}
\newsavebox{\lst@box}
%%%%%% Alternative style, with explicit <BR> (for copy/paste)
\newcommand{\lstavoidwhitepre}
{\let\lst@@br\@br%
\newstyle{.mouselstlisting}{font-family:monospace;margin-right:auto;margin-left:0pt;text-align:left}%
\setenvclass{lstlisting}{mouselstlisting}%
\let\lst@whitepre\@empty%
\renewcommand{\lst@pad}[1]{\@pad{~}{4}{##1}}}
