%%
%% This is file `lstpatch.sty', generated manually :-)
%%
%% Please read the software license in listings.dtx or listings.dvi.
%%
%% (w)(c) 2000-2002 Carsten Heinz
%%
%% This file is distributed under the terms of the LaTeX Project Public
%% License from CTAN archives in directory  macros/latex/base/lppl.txt.
%% Either version 1.0 or, at your option, any later version.
%%
%% Permission is granted to change this file. You are not allowed to
%% distribute any changed version of this file, neither under the same
%% name nor under a different one.
%%
%% Send comments and ideas on the package, error reports and additional
%% programming languages to <cheinz@gmx.de>.
%%
%%
%% This patch file does *not* include any experimental material I sent to
%% various people. These people will have to copy and paste the definitions.
%%
%% This patch file will remove the following bugs from the listings package.
%% Each item contains the bug finder with date of report and first bug fix
%% version, a short description of the problem, and the reason for the bug
%% in parenthesis.
%%
%%  1) Andreas Deininger, 2000/08/25, 0.21a
%%
%%     `breaklines,first>1' leads to ``undefined control sequence'' error
%%     (\lst@breakshape undefined in this case)
%%
%%  2) Vespe Savikko, 2000/09/27, 0.21a (2000/11/02)
%%
%%     stringstyle used also on previous other characters
%%     (missing \lst@PrintToken in \lst@BeginString)
%%
%%  3) Richard Hoefter, 2000/11/03, 0.21b (2000/11/04)
%%
%%     no keyword detection in case of language=HTML,language=Java
%%     (missing deactivation of `keywordsinside' in hook `SetLanguage')
%%
%%  4) Vespe Savikko, 2000/11/06, 0.21c (2000/11/06)
%%
%%     bad output of doc-strings if HTML and Python are loaded
%%     (missing group braces around mode argument in \lst@CommentB causes
%%      a number `0' appearing in the output)
%%
%%  5) Christian Gudrian, 2000/11/13, 0.21d (2000/11/16)
%%
%%     \ref{lst:line} jumps to top of listing and not to the line
%%     (\refstepcounter not used on \c@lstlabel)
%%
%%  6) Dr. Jobst Hoffmann, 2000/11/17, 0.21e (2000/11/20)
%%
%%     incompatibility with typehtml package
%%     (bad lccode of /)
%%
%%  7) Kalle Tuulos, 2001/01/14, 0.21f (2001/01/17)
%%
%%     formfeed characters gobbles next output unit
%%     (\lst@newlines contains \lst@EOLUpdate, which calls \lst@ResetToken)
%%
%%  8) Adam Prugel-Bennett, 2001/02/19, 0.21g
%%
%%     \abovecaptionskip undefined in slides.cls
%%
%%  9) Gabriel Tauro, 2001/04/18, 0.21g
%%
%%     unexpected `)' if the character appears before first printed line
%%     (\lst@breakProcessOther is active)
%%
%% 10) Jens Schwarzer, 2001/05/29, 0.21g
%%
%%     wrong line numbering of lstlisting with first>1
%%     (\lst@firstline not taken into accout in this case)
%%
%% 11) Jens Schwarzer, 2001/05/29, 0.21g (2001/06/21)
%%
%%     caption and title font are not equal
%%     (\lst@maketitle not suitable for koma script)
%%
%% 12) Thorsten Vitt, 2001/06/25, 0.21h
%%
%%     fancyhdr + listings crossing pages ==> ~ in header, not space
%%     (old problem with \output: explicitly redefine ~)
%%
%% 13) Martin Steffen, 2001/04/07, 0.21h
%%
%%     mathescape drops following space
%%     (bad \lst@ifwhitespace)
%%
%% 14) Walter E. Brown, 2001/05/22, 0.21h (2001/06/27)
%%
%%     pdftex 3.14159-14f warning: destination with the same identifier
%%     (bad default of \theHlstlabel)
%%
%% 15) Bradford Chamberlain, 2001/07/07, 0.21i
%%
%%     otherkeywords={@,@^} does not work
%%     (argument treated as one long keyword)
%%
%% 16) GP Gores, 2001/07/11, 0.21i (2001/07/11)
%%
%%     a) inserts \par after each \lstinline
%%     (\lst@Init has been modified, but I missed \lst@DeInit)
%%
%% 17) Carsten Heinz, 2001/07/16, 0.21j (2001/07/22)
%%
%%     16) makes first line of lstlisting textstyle
%%     (bad modification of \lst@DeInit)
%%
%% 18) Olivier Lecarme, 2001/07/30, 0.21k
%%
%%     inconsistent `break' when \lstinline is used inside caption
%%     (remove \let\lst@newlines\@empty from \lstinline)
%%
%% 19) Frank Mittelbach, 2001/09/02, 0.21l
%%
%%     spacing in version 0.21 differs from 0.20 spacing
%%     (bug in `columns' key selected flexible column format)
%%
%% 20) Carsten Heinz, 0.21l (2001/09/03)
%%
%%     default `commentstyle' is not \itshape
%%     (typo `commenstyle')
%%
%% 21) Michael Niedermair, 0.21m (2001/09/18)
%%
%%     strange numbering of listings
%%     (tabularx environment in a heading takes value of \c@lstlisting and
%%      assigns it globally, so a lstlisting can cause negative numbers)
%%
%% 22) Chris Edwards, 2002/02/15, 0.21n (2002/02/20)
%%
%%     Tabulators show up with first>1
%%     (\lst@Let{9}... must be \lst@Def{9}{...})
%%
%% 23) Chris Edwards, 2002/02/21, 0.21o
%%
%%     shifting bottom rule inside list environments
%%     (bad parshape)
%%
%% 24) Gunther Schmidl, 2002/02/20, 0.21o
%%
%%     collapsing empty lines don't work with printpod=false
%%     (\lst@ifnewline not restored)
%%
%% 25) Michael Weber, 2002/03/26, 0.21o,p (2002/03/27)
%%
%%     escapes in lines<first don't work
%%     (escapes not deactivated)
%%
%%
%% Moreover the following functionality is added.
%%
%%  a) allow line breaks in \lstinline (experimental)
%%
%%  b) everydisplay=<token string>
%%
%%  c) nolol=<true|false> is mainly equivalent to the continue key, but
%%     it will also drop lol-entries for lstlisting in the next version
%%
%%  d) allow \lstinline{<code>} (experimental)
%%     (but inside arguments *all* spaces in {<code>} must be protected)
%%
%%  e) option "draft"
%%
%%  f) keys "fvcmdparams" and "morefvcmdparams" (Denis Girou)
%%
\ProvidesFile{lstpatch}[2002/06/02 v0.21q (Carsten Heinz)]
\def\lst@topatch{0.21}
\ifx\lst@version\lst@topatch\else
    \typeout{^^J%
    ***^^J%
    *** Patch file `lstpatch.sty' for version \lst@topatch^^J%
    *** is not suitable for `listings.sty' version \lst@version.^^J%
    ***^^J
    *** Patch file not loaded.^^J%
    ***^^J}%
    \let\lst@topatch\lst@version
    \expandafter\endinput
\fi
%%
%%  1) Just define \lst@breakshape.
\gdef\lst@breakshape{%
    \ifodd \c@page -\lst@innerspread \else -\lst@outerspread \fi
    \linewidth}
%%
%%  2) Add \lst@PrintToken after \lst@TrackNewLines.
\def\lstpatch@strings{%
\gdef\lst@BeginString##1##2{%
    \lst@TrackNewLines \lst@PrintToken
    \lst@ifexstrings ##2\lst@PrintToken \fi
    \lst@EnterMode{##1}%
        {\lst@modetrue \let\lst@currstyle\lst@stringstyle}%
    \lst@ifexstrings\else ##2\lst@PrintToken \fi
    \lst@ifstringspaces
        \lst@keepspacestrue
        \let\lst@outputspace\lst@visiblespace
    \fi}
}
%%
%%  3) Insert deactivation.
\def\lstpatch@html{%
   \lst@AddToHook{SetLanguage}{\let\lst@DefInside\@empty}%
}
%%
%%  4) Insert argument braces around ##5.
\def\lstpatch@comments{%
\gdef\lst@CommentB##1##2##3##4##5##6##7##8##9{%
    \lst@CDef##1##2{##3}##4%
        {##6}%
        {\let\lst@bnext\lst@CArgEmpty
         \lst@ifmode ##7\else
             ##8%
             \def\lst@bnext{\lst@BeginComment{##5}##9}%
         \fi
         \lst@bnext}%
        \@empty}%
}
%%
%%  5) Use `lstlabel' with \refstepcounter.
\def\lstpatch@labels{%
\lst@AddToHook{EveryPar}
    {\global\advance\c@lstlabel\m@ne \refstepcounter{lstlabel}}%
%%
%% 14) Define appropriate \theHlstlabel.
\AtBeginDocument{\def\theHlstlabel{\thelstlisting.\thelstlabel}}%
}
%%
%%  6) Restore lccode.
\lccode`\/=`\/
%%
%%  7) Don't add \lst@EOLUpdate to \lst@newlines. Since I don't know the
%%     reason for adding it a long time ago :-(, it's probably better to
%%     remove \lst@ResetToken (v0.22) from `InitVarsBOL' hook. Well, I'll
%%     just wait for other bug reports ...
\def\lst@ProcessFormFeed{%
    \lst@PrintToken
    \ifx\lst@newlines\@empty
        \lst@EOLUpdate \lst@formfeed
    \else
%        \lst@lAddTo\lst@newlines{\lst@EOLUpdate \lst@formfeed}%
        \lst@lAddTo\lst@newlines{\lst@formfeed}%
    \fi
    \lst@whitespacetrue}
%%
%%  8) Allocate \abovecaptionskip and \belowcaptionskip if necessary.
%%     Additionally define \@makecaption (exact copy from report.cls).
\@ifundefined{abovecaptionskip}
{\newskip\abovecaptionskip
 \newskip\belowcaptionskip
 \long\def\@makecaption#1#2{%
   \vskip\abovecaptionskip
   \sbox\@tempboxa{#1: #2}%
   \ifdim \wd\@tempboxa >\hsize
     #1: #2\par
   \else
     \global \@minipagefalse
     \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
   \fi
   \vskip\belowcaptionskip}%
}{}
%%
%%  9) Also deactivate \lst@breakProcessOther.
\def\lst@BeginDropInput#1{%
    \lst@EnterMode{#1}%
    {\lst@modetrue
     \let\lst@ProcessLetter\@gobble
     \let\lst@ProcessDigit\@gobble
     \let\lst@ProcessOther\@gobble
     \let\lst@breakProcessOther\@gobble
     \let\lst@ProcessSpace\@empty
     \let\lst@ProcessTabulator\@empty
     \let\lst@ProcessFormFeed\@empty}}
%%
%% 10) Replace \@tempcnta\@ne by \@tempcnta\lst@firstline.
\def\lstpatch@labels{%
\gdef\lst@SetFirstLabel{%
    \ifx\lst@firstlabel\@undefined
        \@tempcnta 0\csname lstno@\lst@intname\endcsname\relax
        \ifnum\@tempcnta=\z@ \@tempcnta\lst@firstline
                       \else \lst@continuetrue \fi
        \advance\@tempcnta\lst@advancelabel
        \edef\lst@firstlabel{\the\@tempcnta\relax}%
    \fi}%
}
%%
%% 11) Define \lst@maketitle in terms of \@makecaption. Note
%%     thet this need not to work for all packages and classes.
%%     The \let command here is especially for koma script.
\def\lst@maketitle#1#2{%
    {\let\captionformat\@empty
     \@makecaption{}\lst@title}}
%%
%% 12) Redefine ~ in output routine.
\lst@AddToHook{Init}
    {\let\lsts@OldOutput\lst@OldOutput
     \def\lst@OldOutput{\def~{\nobreakspace{}}%
                        \lsts@OldOutput}%
    }
%%
%% 13) Use \lst@whitespacefalse at end of escape.
\def\lstpatch@escape{%
    \lst@Key{escapeend}{}{\def\lst@escapeend{##1\lst@whitespacefalse}}%
}
%%
%% 15) Make `other keywords' active iteratively.
\def\lstpatch@keywords{%
\gdef\lst@AddOtherKey##1,{%
    \ifx\relax##1\@empty\else
        \ifx\@empty##1\@empty\else
            \lst@MakeActive{##1}%
            \lst@lExtend\lst@otherkeywords{\expandafter,\lst@temp}%
        \fi
        \expandafter\lst@AddOtherKey
    \fi}%
\lst@Key{otherkeywords}{}%
    {\let\lst@otherkeywords\@empty
     \lst@AddOtherKey##1,\relax,}
}
%%
%% 19) Interchange \lst@flexibletrue/false.
\lst@Key{columns}{fixed}{\def\lst@arg{#1}%
    \def\lst@temp{fixed}%
    \ifx\lst@arg\lst@temp
        \lst@flexiblefalse
    \else
        \def\lst@temp{flexible}%
        \ifx\lst@arg\lst@temp
            \lst@flexibletrue
        \else
            \PackageWarning{Listings}{Unknown column format `#1'}%
        \fi
    \fi}
%%
%% 20) Use \lst@commentstyle instead of \lst@commenstyle.
\lstpatch@comments{%
    \lst@AddToHook{EmptyStyle}{\let\lst@commentstyle\itshape}%
}
%%
%% 21) Negative value is present only in \lst@HRefStepCounter.
\def\lst@HRefStepCounter#1{%
    \begingroup
    \c@lstlisting\lst@neglisting
    \advance\c@lstlisting\m@ne \xdef\lst@neglisting{\the\c@lstlisting}%
    \ifx\hyper@refstepcounter\@undefined\else
        \hyper@refstepcounter{#1}%
    \fi
    \endgroup}
%%
%% 22) Use \lst@Def instead of \lst@Let.
\lst@lAddTo\lst@SelectStdCharTable{%
    \lst@Def{9}{\lst@ProcessTabulator}%
    \lst@Def{12}{\lst@ProcessFormFeed}%
    \lst@Def{32}{\lst@ProcessSpace}}
%%
%% 23) Replace \noindent by \parshape\z@.
\def\lstpatch@frames{%
\gdef\lst@frameExit{%
    \lst@frameSpreadV
    \lst@frameH B\lst@framebshape
    \ifvoid\z@\else
        \everypar{}\par\nointerlineskip\parshape\z@\box\z@
    \fi}%
}
%%
%% 24) Save \lst@ifnewline (and use \relax after \lst@newline in
%%     \lst@TrackNewlines as in listings 1.0).
\def\lst@BeginDropOutput#1{%
    \global\let\lst@BDOnewlines\lst@newlines
    \global\let\lst@BDOifnewline\lst@ifnewline
    \lst@EnterMode{#1}%
        {\lst@modetrue
         \let\lst@OutputBox\@gobble
         \aftergroup\lst@BDORestore}}
\def\lst@BDORestore{%
    \global\let\lst@newlines\lst@BDOnewlines
    \global\let\lst@ifnewline\lst@BDOifnewline}
\def\lst@TrackNewLines{%
    \ifx\lst@newlines\@empty\else
        \lsthk@OnNewLine
        \lst@newlines\relax
        \global\let\lst@newlines\@empty
    \fi}
%%
%% 25) Deactivate escapes by checking \lst@ProcessLetter.
\def\lstpatch@escape{%
\gdef\lst@Escape##1##2##3##4{%
    \lst@CArgX ##1\relax\lst@CDefX
        {}%
        {\ifx\lst@ProcessLetter\@gobble\else
            \lst@TrackNewLines\lst@UseLostSpace \lst@PrintToken
            \lst@InterruptModes
            \lst@EnterMode{\lst@TeXmode}{\lst@modetrue}%
            \ifx\^^M##2%
                \lst@CArg ##2\relax\lst@ActiveCDefX
                    {}%
                    {\lst@escapeend ##4\lst@LeaveAllModes\lst@ReenterModes}%
                    {\lst@MProcessListing}%
            \else
                \lst@CArg ##2\relax\lst@ActiveCDefX
                    {}%
                    {\lst@escapeend ##4\lst@LeaveAllModes\lst@ReenterModes
                     \lst@whitespacefalse}%
                    {}%
            \fi
            ##3\lst@escapebegin
         \fi}%
        {}}%
}
%%
%% a) Remove \hbox in front of \bgroup and ...
\renewcommand\lstinline[1][]{%
    \leavevmode\hbox\bgroup
        \def\lst@boxpos{b}\let\lst@ifflexible\iftrue
        \lsthk@PreSet\lstset{#1}%
        \lsthk@TextStyle
%% d) Check for \bgroup ...
        \@ifnextchar\bgroup{\afterassignment\lst@InlineG \let\lst@trash}%
                           \lstinline@}
\def\lstinline@#1{%
    \lst@Init\relax
    \lst@IfNextCharActive{\lst@InlineM#1}{\lst@InlineJ#1}}
%%    ... to do strange conversion here.
\def\lst@InlineG{%
    \lst@Init\relax
    \lst@IfNextCharActive{\lst@InlineM\}}%
                         {\let\lst@arg\@empty \lst@InlineGJ}}
\def\lst@InlineGJ{\futurelet\@let@token\lst@InlineGJTest}
\def\lst@InlineGJTest{%
    \ifx\@let@token\egroup
        \afterassignment\lst@InlineGJEnd
        \expandafter\let\expandafter\lst@trash
    \else
        \expandafter\lst@InlineGJRead
    \fi}
\def\lst@InlineGJEnd{\lst@arg\lst@DeInit\egroup}
\def\lst@InlineGJRead#1{%
    \lccode`\~=`#1\lowercase{\lst@lAddTo\lst@arg~}%
    \lst@InlineGJ}
%%    Old redefinition:
%\renewcommand\lstinline[1][]{%
%    \leavevmode\bgroup
%        \def\lst@boxpos{b}\let\lst@ifflexible\iftrue
%        \lsthk@PreSet\lstset{#1}%
%        \lsthk@TextStyle \lst@Init\relax
%        \everypar{}\let\lsthk@Endgroup\@empty
%% 18) The following line has been removed in the above definition.
%        \let\lst@newlines\@empty \let\lst@breakcurrindent\z@
%        \lstinline@}
%% 17) Add switch to distinguish text and display style listings.
\lst@AddToHook{TextStyle}{\let\lst@iftextstyle\iftrue}
\lst@AddToHook{DisplayStyle}{\let\lst@iftextstyle\iffalse}
\let\lst@iftextstyle\iffalse
% ... back to a):
\def\lst@InputListing#1{%
    \begingroup
    \lsthk@PreSet \gdef\lst@intname{#1}%
    \expandafter\lstset\expandafter{\lst@set}%
    \lst@ifprint
        \par % inserted to switch to vmode
        \lst@Init\relax \let\lst@gobble\z@
        \lst@SkipToFirst \input{#1}\lst@DeInit
    \else
        \begin{center}%
        \footnotesize --- Listing of #1 has been skipped. ---
        \end{center}%
    \fi
    \endgroup}
\def\lst@Init#1{%
    \begingroup
    \ifx\lst@float\relax\else
        \edef\@tempa{\noexpand\@float{lstlisting}[\lst@float]}%
        \expandafter\@tempa
    \fi
    \ifhmode\ifinner \lst@boxtrue \fi\fi
    \lst@ifbox
        \lsthk@BoxUnsafe
        \hbox to\z@\bgroup
             $\if t\lst@boxpos \vtop
        \else \if b\lst@boxpos \vbox
        \else \vcenter \fi\fi
        \bgroup
    \else
%% 17) Don't check for vertical mode, use \lst@iftextstyle.
        \lst@iftextstyle\else
\lst@EveryDisplay
            \par\penalty-50\relax
            \vspace\lst@aboveskip
        \fi
    \fi
    \normalbaselines
    \abovecaptionskip\lst@abovecaption\relax
    \belowcaptionskip\lst@belowcaption\relax
    \lst@MakeCaption t%
    \lsthk@PreInit \lsthk@Init
    \everypar{\lsthk@EveryLine\lsthk@EveryPar}%
    \lsthk@InitVars
    \lsthk@InitVarsBOL
    \lst@Let{13}\lst@MProcessListing
    \let\lst@Backslash#1%
%% d) Reset some variables for textstyle listings.
    \lst@iftextstyle
        \everypar{}\let\lsthk@Endgroup\@empty
        %\let\lst@newlines\@empty \let\lst@breakcurrindent\z@
    \fi
    \lst@EnterMode{\lst@Pmode}{\lst@SelectCharTable}}
%%
%% 16) Adjust \lst@DeInit accordingly.
\def\lst@DeInit{%
    \lst@PrintToken \lst@EOLUpdate
    \lst@RemoveNewLine\@empty
    \lst@RemoveNewLine{%
        \lst@ifshowlines
            \lst@TrackNewLines
        \else
            \setbox\@tempboxa\vbox{\lst@TrackNewLines}%
        \fi}%
%% 17) Don't check for vertical mode, use \lst@iftextstyle.
    \lst@iftextstyle\else
        \par\removelastskip
    \fi
    \everypar{}\lsthk@ExitVars \lsthk@DeInit
    \lst@MakeCaption b%
    \lst@ifbox
        \egroup $\hss \egroup
        \vrule\@width\lst@maxwidth\@height\z@\@depth\z@
    \else
%% check for vertical mode.
        \ifvmode
            \par\penalty-50\vspace\lst@belowskip
        \fi
    \fi
    \ifx\lst@float\relax\else
        \expandafter\end@float
    \fi
    \endgroup}
%%
%% b) Define key, the material is inserted above.
\lst@Key{everydisplay}{}{\def\lst@EveryDisplay{#1}}
%%
%% c) New key sets value of continue key.
\lst@Key{nolol}{false}[t]{\lstKV@SetIf{#1}\lst@ifnolol}
\lst@AddToHook{PreInit}{\lst@ifcontinue\else \lst@SetIfContinue \fi}
\def\lst@SetIfContinue{\let\lst@ifcontinue\lst@ifnolol}
%%
%% e) Declare option and slightly change \lst@InputListing.
\let\lst@ifdraft\iffalse
\DeclareOption{draft}{\let\lst@ifdraft\iftrue}
\def\lst@InputListing#1{%
    \begingroup
    \lsthk@PreSet \gdef\lst@intname{#1}%
    \expandafter\lstset\expandafter{\lst@set}%
    \lst@ifprint
        \lst@Init\relax \let\lst@gobble\z@
        \lst@SkipToFirst
        \lst@ifdraft \let\lst@next\@empty % added
               \else \def\lst@next{\input{#1}}\fi
        \lst@next
        \lst@DeInit
    \else
        \begin{center}%
        \footnotesize --- Listing of #1 has been skipped. ---
        \end{center}%
    \fi
    \endgroup}
%
% f) Refine conversion definitions to either work as usual or to ...
\def\lstpatch@fancyvrb{%
\gdef\lst@FVConvert{\@tempcnta\z@ \lst@FVConvertO@}%
\gdef\lst@FVConvertO@{%
    \ifcase\@tempcnta \expandafter
        \futurelet\expandafter\@let@token\expandafter\lst@FVConvert@@
    \else
% append arguments as they are.
        \expandafter\lst@FVConvertO@a
    \fi}%
\gdef\lst@FVConvertO@a##1{%
    \lst@lAddTo\lst@arg{{##1}}\advance\@tempcnta\m@ne
    \lst@FVConvertO@}%
% Here, ...
\gdef\lst@FVConvert@##1{%
  \ifx \@nil##1\else
    \lst@if
       \if\relax\noexpand##1%
          \lst@lAddTo\lst@arg{\lst@UseLostSpace\lst@PrintToken##1}%
% ... actually here, we check for commands with arguments and set the
% value of \@tempcnta as required.
          \def\lst@temp####1,##1####2,####3####4\relax{%
              \ifx####3\@empty \else \@tempcnta####2\relax \fi}%
          \expandafter\lst@temp\lst@FVcmdparams,##1\z@,\@empty\relax
       \else
          \lccode`\~=`##1\lowercase{\lst@lAddTo\lst@arg~}%
       \fi
    \else
       \lccode`\~=`##1\lowercase{\lst@lAddTo\lst@arg~}%
    \fi
    \expandafter\lst@FVConvertO@
  \fi}
% Eventually we define user keys to adjust \lst@FVcmdparams. The base
% set of commands and parameter numbers was provided by Denis Girou.
\lst@Key{fvcmdparams}%
% D.G. suggestion begin - 2002/05/31
    {\overlay\@ne}%
% D.G. suggestion end
    {\def\lst@FVcmdparams{,##1}}%
\lst@Key{morefvcmdparams}\relax{\lst@lAddTo\lst@FVcmdparams{,##1}}%
}
%%
\endinput
%%
%% End of file `lstpatch.sty'.
