summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/jvlisting/jvlisting.sty
blob: 526d3ad1e8cdf333f2be00fbacd22fb1b736c04f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
%%
%% 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'.