\usepackage{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{\@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}
%%%%%%%%%%%%
% Keywords %
%%%%%%%%%%%%
\newcommand{\lst@allkeywords}{}
\def\lst@record@kwds#1{\lst@AddTo,{\lst@allkeywords}{\lst@normalize{#1}}}
\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}
  {\@callprim{\lst@delete@kwds}{\{\lst@allkeywords\}}%
  \@callprim{\lst@delete@okwds}{\{\@rawchars{\lst@allotherkeywords}\}}%
  \@callprim{\lst@delete@directives}{\{\lst@alldirectives\}}}
\def\lst@install@kwd#1#2{\def\csname lstk@\lst@normalize{#2}\endcsname{#1}}
\def\lst@install@kwds
  #1#2{%
    \lst@record@kwds{#2}%
    \lst@iter{\lst@install@kwd{#1}}{#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{\def\csname lstk@\@rawchars{#1}\endcsname{\lst@identifier@style}}
\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{\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@delete#1{\lst@AddToHook{AfterSetLanguage}{\lst@delete@kwds{#1}}}
\define@key{lst}{keywords}{\lst@delay@install\lst@kwd@style#1}
\define@key{lst}{morekeywords}{\lst@delay@install\lst@kwd@style#1}
\define@key{lst}{deletekeywords}{\lst@delay@delete#1}
\define@key{lst}{ndkeywords}{\lst@delay@install\lst@ndkwd@style#1}
\define@key{lst}{morendkeywords}{\lst@delay@install\lst@ndkwd@style#1}
\define@key{lst}{deletendkeywords}{\lst@delay@delete#1}
\define@key{lst}{rdkeywords}{\lst@delay@install\lst@rdkwd@style#1}
\define@key{lst}{morerdkeywords}{\lst@delay@install\lst@rdkwd@style#1}
\define@key{lst}{deleterdkeywords}{\lst@delay@delete#1}
%%%%%%%% Other keywords
\def\lst@delay@oinstall#1#2{\lst@AddToHook{AfterSetLanguage}{\lst@install@okwds{#1}{#2}}}
\def\lst@delay@odelete#1{\lst@AddToHook{AfterSetLanguage}{\lst@delete@okwds{#1}}}
\define@key{lst}{otherkeywords}{\lst@delay@oinstall\lst@okwd@style#1}
\define@key{lst}{morekeothersywords}{\lst@delay@oinstall\lst@okwd@style#1}
\define@key{lst}{deleteotherskeywords}{\lst@delay@odelete#1}
%%% 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@directive@style#1}
\define@key{lst}{moredirectives}{\lst@delay@dinstall\lst@directive@style#1}
%%%%%%% Styles applied to keywords, strings etc %%%%%%
\define@key{lst}{keywordstyle}{\def\lst@kwd@style##1{#1{##1}}}
\define@key{lst}{ndkeywordstyle}{\def\lst@ndkwd@style##1{#1{##1}}}
\define@key{lst}{rdkeywordstyle}{\def\lst@rdkwd@style##1{#1{##1}}}
\define@key{lst}{otherkeywordstyle}{\def\lst@okwd@style##1{#1{##1}}}
\define@key{lst}{directivestyle}{\def\lst@directive@style##1{#1{##1}}}
\define@key{lst}{identifierstyle}[]{\def\lst@identifier@style##1{#1{##1}}}
\define@key{lst}{basicstyle}{\def\lst@basic@style{#1}}
\define@key{lst}{commentstyle}{\def\lst@comment@style{#1}}
\define@key{lst}{stringstyle}{\def\lst@string@style{#1}}
\lst@AddToHook{EmptyStyle}{\let\lst@string@style\@empty}
\lst@AddToHook{EmptyStyle}{\let\lst@comment@style\@empty}
\lst@AddToHook{EmptyStyle}{\let\lst@identifier@style\@empty}
\lst@AddToHook{EmptyStyle}{\let\lst@basic@style\@empty}
\lst@AddToHook{EmptyStyle}{\let\lst@kwd@style\bfseries}
\lst@AddToHook{EmptyStyle}{\def\lst@ndkwd@style{\lst@kwd@style}}
\lst@AddToHook{EmptyStyle}{\def\lst@rdkwd@style{\lst@kwd@style}}
\lst@AddToHook{EmptyStyle}{\def\lst@okwd@style{\lst@kwd@style}}
\lst@AddToHook{EmptyStyle}{\def\lst@directive@style{\lst@kwd@style}}
\lst@AddToHook{EmptyStyle}{\def\lst@directive@style{\lst@kwd@style}}
%%%%%% 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}}
\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@define@key@opt{language}{\lst@language@key}{}
\newcommand{\lst@checkdd}[2]
  {\@ifundefined{lstdd@\MakeLowercase{#1}}{\lst@define@dd[#2]{#1}}{}}
\newcommand{\lst@definelanguage@}[3]
  {\lst@checkdd{#1}{#2}\def\csname\lst@mklang[#2]{#1}\endcsname{\lstset{#3}}}
\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}
\def\lst@color@in@temp{%
   \let\old@color\color%
   \renewcommand{\color}[2][!*!]{\@getstylecolor[##1]{##2}}%
   \prim@def\lst@temp{\lst@bgcolor}%
   \let\color\old@color%
}
\lst@AddToHook{InitVar}
  {\ifu\lst@bgcolor
   \def\@@open@lstbox{\@open{DIV}{\envclass@attr{lstlisting}}}%
   \def\@close@lstbox{\@close{DIV}}%
   \else
   \lst@color@in@temp%
   \def\@@open@lstbox{\@opencell{STYLE="padding:1ex;background-color:\lst@temp"}{}{\envclass@attr{lstlisting}}}%
   \def\@close@lstbox{\@closecell}%
   \fi
   \def\@open@lstbox{\@@open@lstbox\whitepretrue}}
%\newcommand{\lst@define@bgcolor}[2][!*!]
%  {\def\lst@tmp{#2}%
%  \ifx\lst@tmp\@empty\let\lst@bgcolor\@empty
%  \else\def\lst@bgcolor{\@getcolor[#1]{#2}}\fi}
%\lst@define@key@opt{backgroundcolor}{\lst@define@bgcolor}{}
\def\colorwarning{\warning{Colors in listings and 'color' package not loaded}}
\define@key{lst}{backgroundcolor}
{\ifu\color\colorwarning\else\def\lst@bgcolor{#1}\fi}
%%%%%%% 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,breaklines,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@label@style##1{{#1{##1}}}}
\define@key{lst}{numberstyle}{\def\lst@label@style##1{{#1{##1}}}}
\newcommand{\lst@label@style}[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}{ }
\newcommand{\lst@pad}[1]{\@pad{\@print{ }}{4}{#1}}
\newcommand{\lst@linenumber@}[1]
  {\lst@label@style{\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}
\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\else\sbox{\@prevlines}{}\fi\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}}}}
% 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
\lst@Hook{String}
\lst@AddToHook{Init}{\lst@UseHook{String}}
\lst@AddToHook{SetLanguage}{\lst@ResetHook{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}{}
% 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}[1]
  {\lst@lExtend\lst@DefineComments@LC
    {\lst@line@comment{\@rawchars{#1}}}}
\define@key{lst}{commentline}{\lst@LC@key{#1}}
\lst@AddToHookExe{SetLanguage}{\let\lst@DefineComments@LC\@empty}
\lst@AddToHook{SelectCharTable}{\lst@DefineComments@LC}
% Nested comments
\newcommand{\lst@NC@key}[2]
  {\lst@lExtend\lst@DefineComments@NC
     {\lst@nested@comment{\@rawchars{#1}}{\@rawchars{#2}}}}
\define@key[2]{lst}{nestedcomment}{\lst@NC@key{#1}{#2}}
\lst@AddToHookExe{SetLanguage}{\let\lst@DefineComments@NC\@empty}
\lst@AddToHook{SelectCharTable}{\lst@DefineComments@NC}
% Balanced comments
\newcommand{\lst@SC@key}[2]
   {\lst@lExtend\lst@DefineComments@SC
     {\lst@balanced@comment{\@rawchars{#1}}{\@rawchars{#2}}}}
\define@key[2]{lst}{singlecomment}{\lst@SC@key{#1}{#2}}
\lst@AddToHookExe{SetLanguage}{\let\lst@DefineComments@SC\@empty}
\lst@AddToHook{SelectCharTable}{\lst@DefineComments@SC}
\newcommand{\lst@DC@key}[4]
   {\lst@lExtend\lst@DefineComments@DC
      {\lst@balanced@comment{\@rawchars{#1}}{\@rawchars{#2}}}%
   \lst@lExtend\lst@DefineComments@DC
      {\lst@balanced@comment{\@rawchars{#3}}{\@rawchars{#4}}}}
\define@key[4]{lst}{doublecomment}{\lst@DC@key{#1}{#2}{#3}{#4}}
\lst@AddToHookExe{SetLanguage}{\let\lst@DefineComments@DC\@empty}
\lst@AddToHook{SelectCharTable}{\lst@DefineComments@DC}
%%%%%% New interface for comments grr...
\def\lst@morecomment@d#1#2#3#4{\lst@DC@key{#1}{#2}{#3}{#4}}
\def\lst@morecomment@s#1#2{\lst@SC@key{#1}{#2}}
\def\lst@morecomment@n#1#2{\lst@NC@key{#1}{#2}}
\def\lst@morecomment@l#1#2#3#4\@empty{\lst@LC@key{#1#2#3#4}}
\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@till@empty#1\@empty{}
\newcommand{\lst@morecomment@key}[1][]
  {\ifu\csname lst@morecomment@#1\endcsname
    \warning{Unkown 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
   \warning{Unkown comment type ``#1''}\else
   \csname lst@deletecomment@#1\endcsname\fi
   \lst@till@empty}
\newcommand{\lst@comment@key}[1][]
  {\ifu\csname lst@morecomment@#1\endcsname
    \warning{Unkown comment type ``#1''}%
    \let\lst@next\lst@till@empty\else
    \csname lst@deletecomment@#1\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\@empty\@empty}}
\define@key{lst}{morecomment}{\@callsubst{\lst@morecomment@key}{#1\@empty\@empty\@empty}}
%%%%%%% Extended chars and others
\def\lst@extendedchars{false}
\lst@AddToHook{DeInit}{\setboolean{lst@extendedchars}{false}}
\lst@AddToHookExe{SetLanguage}{\setboolean{lst@extendedchars}{false}}
\lst@AddToHook{SelectCharTable}
{\setboolean{lst@extendedchars}{\lst@boolean{\lst@extendedchars}}}
\define@key{lst}{extendedchars}[true]{\def\lst@extendedchars{#1}}
\lst@iter{\ignore@key}{inputencoding,showspaces,formfeed}
\newcommand{\lst@warnkey}[2]{\define@key{lst}{#2}{\warning{#1}}}
\lst@iter{\lst@warnkey{No more tabulations in our
century}}{tabsize,showtabs,tab}
%%%%%%% 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@identifier@style{#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@directive@style{\@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}}
%%%%% Column allignement None !
\lst@iter{\ignore@key}
  {columns,flexiblecolumns,keepspaces,basewidth,fontadjust,aboveskip,belowskip,lineskip}
%%%%%%% 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}