%%
%% This is file `jvlisting.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% jvlisting.dtx  (with options: `package')
%% jvlisting.dtx  (with options: `listingdef')
%% 
%% Copyright (C) 2011  Jochen Voss <voss@seehuhn.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 `maintained' and the current
%% Maintainer of this work is Jochen Voss <voss@seehuhn.de>.
%% 
%% This work consists of the files jvlisting.dtx, test.tex and the
%% derived files jvlisting.sty, jvlisting.ins and examples.tex.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{jvlisting}[2011/11/17 v0.7 Formatted Program Listings]

\def\jvl@iterlines#1{\expandafter\jvl@iterla\expandafter{#1}{\jvl@dropempty}}
{\obeylines\gdef\jvl@iterla#1#2#3
  {\def\jvl@testmarker##1#1{}%
  \expandafter\def\expandafter\w\expandafter{\jvl@testmarker#3#1}%
  \ifx\w\empty%
    \def\next{#2{\jvl@iterla{#1}{\jvl@tryhook}}#3
    }%
  \else%
    % special treatment for the last line
    \def\y##1#1##2#1{\jvl@dropempty{\jvl@end##2}##1
    }%
    \def\next{\y#3#1}%
  \fi\next}}

\def\jvl@dropempty#1{\jvl@dropa{#1}{}}
{\obeylines\gdef\jvl@dropa#1#2#3{\ifx
    #3\def\next{#1}%
  \else%
    \if#3 \def\next{\jvl@dropa{#1}{#2#3}}\else%
      \def\next{\jvl@tryhook{#1}#2#3}\fi%
  \fi\next}}

\def\jvl@tryhook#1{\ifx\ListingStartHook\undefined
    \def\next{\jvl@countspaces{#1}}%
  \else
    \def\jvl@trya{\let\ListingStartHook\undefined\jvl@countspaces{#1}}%
    \def\next{\ListingStartHook{\jvl@trya}}%
  \fi
  \next}

\def\jvl@countspaces#1{\@tempcnta=0\jvl@counta{#1}}
{\catcode`\^^I=12
\gdef\jvl@counta#1#2{\if^^I#2%
    \advance\@tempcnta by8\divide\@tempcnta by8\multiply\@tempcnta by8
    \def\next{\jvl@counta{#1}}%
  \else
    \expandafter\if\noexpand#2 %
      \advance\@tempcnta by1
      \def\next{\jvl@counta{#1}}%
    \else
      \def\next{\jvl@fixspaces{#1}#2}%
    \fi
  \fi\next}}

\newcount\jvl@idt \jvl@idt=\m@ne
{\obeylines\gdef\jvl@fixspaces#1#2{\ifx
    #2\else%
  \ifnum\jvl@idt<0%
    \jvl@idt=\@tempcnta%
  \else%
    \ifnum\@tempcnta<\jvl@idt\jvl@idt=\@tempcnta\fi%
  \fi\fi\jvl@fixa{#1}#2}}
\def\jvl@fixa#1{\ifnum\@tempcnta>\jvl@idt
    \advance\@tempcnta by\m@ne
    \def\next{\jvl@fixa{#1}\space}%
  \else
    \def\next{\jvl@output{#1}}%
  \fi\next}

{\obeylines\gdef\jvl@output#1#2
  {\jvl@typeset{#2}#1}}

{\catcode`\ =\active%
\gdef\jvl@obeyspaces{\frenchspacing\catcode`\ =\active\let \space}}
\def\jvl@fixparsing{\let\do\@makeother\dospecials\catcode`\^^I=12
  \jvl@obeyspaces\hyphenchar\font\m@ne}

\newcount\jvl@tmphyphenchar
\def\jvl@begingroup{\jvl@tmphyphenchar=\hyphenchar\font
  \begingroup \parskip0pt \advance\leftskip by\@totalleftmargin}
\def\jvl@endgroup{\endgroup \hyphenchar\font=\jvl@tmphyphenchar}

\begingroup
  \catcode`|=0 \catcode`[=1 \catcode`]=2
  \catcode`\{=12 \catcode`\}=12 \catcode`\\=12
  |gdef|jvl@makemarker#1[%
    |expandafter|gdef|csname jvl@@#1marker|endcsname[\end{#1}]]
|endgroup
\def\NewListingEnvironment#1#2#3#4#5#6{\jvl@makemarker{#1}%
  \expandafter\newcommand\csname #1\endcsname[#2]{#3\jvl@begingroup
    #4\def\jvl@end{\end{#1}}\let\jvl@typeset=#5%
    \jvl@fixparsing\obeylines
    \expandafter\def\expandafter\jvl@start\expandafter{%
      \expandafter\jvl@iterlines\csname jvl@@#1marker\endcsname}%
    \let\next\jvl@start
    \next}%
  \expandafter\gdef\csname end#1\endcsname{\jvl@endgroup #6}}

\newread\jvl@fileinput
\def\jvl@iterfile{\read\jvl@fileinput to\l
  \ifeof\jvl@fileinput
    \let\next\relax
  \else
    \expandafter\jvl@iterfa\l
    \let\next\jvl@iterfile
  \fi\next}
{\catcode`\^^M=12
\gdef\jvl@iterfa#1^^M{\jvl@iterfb{#1}}}
{\obeylines\gdef\jvl@iterfb#1{\jvl@tryhook{\relax}#1
  }}
\def\NewFileListingCommand#1#2#3#4#5#6{%
  \ifnum#2<1
    \PackageError{jvlisting}{%
      Invalid number of arguments: `#2'
    }{%
      The second argument to \protect\NewFileListingCommand\space must be at
      least 1.
    }%
  \fi
  \newcommand{#1}[#2]{#3\jvl@begingroup
    #4\let\jvl@typeset=#5%
    \jvl@fixparsing\catcode`\^^M=12
    \openin\jvl@fileinput=##1\jvl@iterfile\closein\jvl@fileinput
    \jvl@endgroup#6%
    \ignorespaces
  }%
}

\newskip\listingskipamount \listingskipamount=1ex
\newdimen\listingindent \listingindent=2em
\newcount\prelistingpenalty \prelistingpenalty=100
\newcount\listingpenalty \listingpenalty=500
\newcount\postlistingpenalty \postlistingpenalty=-50
{\catcode`\`=\active\gdef`{\relax\lq}%
\gdef\DisableLigatureFix{\def`{\lq}}}
\def\normallistingfont{\normalfont\ttfamily\catcode`\`=\active}

\let\listingfont=\normallistingfont
\newcommand{\ListingTypesetLine}[1]{\ifvmode\penalty\listingpenalty\noindent\fi
  \hskip\listingindent\strut#1\par}
\newcommand{\prelistingskip}{\endgraf\ifdim\lastskip>\listingskipamount\else
  \removelastskip\penalty\prelistingpenalty\vskip\listingskipamount\fi}
\newcommand{\postlistingskip}{\endgraf\penalty\postlistingpenalty
  \vskip-\parskip\nobreak\vskip\listingskipamount\noindent}
\NewListingEnvironment{listing}{0}{\prelistingskip}%
  {\listingfont\let\ListingStartHook\listingstarthook}{\ListingTypesetLine}%
  {\postlistingskip\ignorespacesafterend}

\NewFileListingCommand{\filelisting}{1}{\prelistingskip}%
  {\listingfont}{\ListingTypesetLine}{\postlistingskip}
\endinput
%%
%% End of file `jvlisting.sty'.
