| 1 |
% \iffalse |
| 2 |
% jvlisting.dtx - docstrip archive for the jvlisting macro package |
| 3 |
%<*gobble> |
| 4 |
\iffalse |
| 5 |
%</gobble> |
| 6 |
%<*install> |
| 7 |
\input docstrip.tex |
| 8 |
|
| 9 |
\preamble |
| 10 |
|
| 11 |
Copyright (C) 2011 Jochen Voss <[email protected]> |
| 12 |
|
| 13 |
This work may be distributed and/or modified under the conditions of |
| 14 |
the LaTeX Project Public License, either version 1.3 of this license |
| 15 |
or (at your option) any later version. The latest version of this |
| 16 |
license is in |
| 17 |
|
| 18 |
http://www.latex-project.org/lppl.txt |
| 19 |
|
| 20 |
and version 1.3 or later is part of all distributions of LaTeX version |
| 21 |
2005/12/01 or later. |
| 22 |
|
| 23 |
This work has the LPPL maintenance status `maintained' and the current |
| 24 |
Maintainer of this work is Jochen Voss <[email protected]>. |
| 25 |
|
| 26 |
This work consists of the files jvlisting.dtx, test.tex and the |
| 27 |
derived files jvlisting.sty, jvlisting.ins and examples.tex. |
| 28 |
\endpreamble |
| 29 |
%</install> |
| 30 |
%<*gobble> |
| 31 |
\fi |
| 32 |
%</gobble> |
| 33 |
%<*driver> |
| 34 |
\documentclass{ltxdoc} |
| 35 |
\usepackage[text={6in,9in},centering]{geometry} |
| 36 |
\usepackage{hyperref} |
| 37 |
\usepackage{jvlisting} |
| 38 |
\setlength{\parskip}{0pt plus.5ex} |
| 39 |
\setlength{\listingskipamount}{.6ex plus.5ex minus.1ex} |
| 40 |
\PageIndex |
| 41 |
\NewListingEnvironment{xlist}{0}{\prelistingskip}% |
| 42 |
{\listingfont\small\renewcommand{\baselinestretch}{0.95}}% |
| 43 |
{\ListingTypesetLine}{\postlistingskip} |
| 44 |
\begin{document} |
| 45 |
\DocInput{jvlisting.dtx} |
| 46 |
\end{document} |
| 47 |
%</driver> |
| 48 |
%<*examples> |
| 49 |
|
| 50 |
\documentclass{article} |
| 51 |
|
| 52 |
\usepackage[text={6in,9in},centering]{geometry} |
| 53 |
\usepackage{jvlisting} |
| 54 |
\setlength{\listingskipamount}{2\baselineskip} |
| 55 |
|
| 56 |
\begin{document} |
| 57 |
%</examples> |
| 58 |
%<*usage> |
| 59 |
% \fi |
| 60 |
\GetFileInfo{jvlisting.sty} |
| 61 |
\title{The \textsf{jvlisting} package} |
| 62 |
\date{\filedate\ \fileversion} |
| 63 |
\author{Jochen Voss\\ \texttt{[email protected]}\\ \texttt{http://seehuhn.de/}} |
| 64 |
\maketitle |
| 65 |
|
| 66 |
\begin{abstract} |
| 67 |
This package provides the \LaTeX\ environment \verb|listing|, an |
| 68 |
alternative to the built-in \verb|verbatim| environment. The |
| 69 |
\verb|listing| environment is specially tailored for including |
| 70 |
listings of computer program source code into documents. The main |
| 71 |
advantages over the original \verb|verbatim| environment are that |
| 72 |
\verb|listing| environments automatically fix leading white-space |
| 73 |
so that the environment and program listing can be indented with |
| 74 |
the rest of the document source and that \verb|listing| |
| 75 |
environments can easily be customised and extended. |
| 76 |
\end{abstract} |
| 77 |
|
| 78 |
\tableofcontents |
| 79 |
|
| 80 |
\section{Basic Usage} |
| 81 |
|
| 82 |
The \verb|listing| environment allows to include source code of |
| 83 |
computer programs in \LaTeX\ documents by including the code between |
| 84 |
\verb|\begin{listing}| and \verb|\end{listing}|. |
| 85 |
|
| 86 |
\medskip |
| 87 |
|
| 88 |
\textbf{Example.} In order to typeset the listing of a simple Python |
| 89 |
function (computing the cumulative sum of a list) you can use the |
| 90 |
following code in your \LaTeX\ document. |
| 91 |
\begin{xlist} |
| 92 |
\begin{listing} |
| 93 |
def cumsum(iterable): |
| 94 |
return reduce(lambda res, x: res+[res[-1]+x], iterable, [0])[1:] |
| 95 |
\end{listing} |
| 96 |
\end{xlist} |
| 97 |
\noindent |
| 98 |
These commands result in the following output: |
| 99 |
% \iffalse |
| 100 |
%</usage> |
| 101 |
%<*examples|usage> |
| 102 |
% \fi |
| 103 |
|
| 104 |
\begin{listing} |
| 105 |
def cumsum(iterable): |
| 106 |
return reduce(lambda res, x: res+[res[-1]+x], iterable, [0])[1:] |
| 107 |
\end{listing} |
| 108 |
% \iffalse |
| 109 |
%</examples|usage> |
| 110 |
%<*usage> |
| 111 |
% \fi |
| 112 |
|
| 113 |
\medbreak |
| 114 |
|
| 115 |
Differently from the \LaTeX\ \verb|verbatim| environment, the |
| 116 |
\verb|listing| environment can be indented to match the surrounding |
| 117 |
document source. The following will work as expected: |
| 118 |
\begin{xlist} |
| 119 |
\begin{figure}[h] |
| 120 |
\begin{listing} |
| 121 |
def cumsum(iterable): |
| 122 |
return reduce(lambda res, x: res+[res[-1]+x], iterable, [0])[1:] |
| 123 |
\end{listing} |
| 124 |
\end{figure} |
| 125 |
\end{xlist} |
| 126 |
\noindent |
| 127 |
The Python code will be typeset using the same indentation as in the |
| 128 |
previous example, despite the fact that the code in the two |
| 129 |
\verb|listing| environments has different indentation in the |
| 130 |
\LaTeX\ source code. |
| 131 |
|
| 132 |
To make it easier to copy real program source code directly into your |
| 133 |
\LaTeX\ file, the code in listings can be indented by TAB characters |
| 134 |
instead of spaces. The TAB spacing is assumed to be 8 character |
| 135 |
columns. |
| 136 |
|
| 137 |
In addition to the \LaTeX\ environment described above, there is also |
| 138 |
a command \verb|\filelisting| to typeset source code contained in a |
| 139 |
file. The command takes one argument, the name of the file to |
| 140 |
include, and behaves very similar to the the \verb|listing| |
| 141 |
environment. |
| 142 |
|
| 143 |
\medskip |
| 144 |
|
| 145 |
\textbf{Example.} In order to typeset the contents of a file |
| 146 |
\verb|cumsum.py|, we could use the following command. |
| 147 |
\begin{xlist} |
| 148 |
\filelisting{cumsum.py} |
| 149 |
\end{xlist} |
| 150 |
|
| 151 |
\bigbreak |
| 152 |
|
| 153 |
There are various parameters to customise how listings are typeset. |
| 154 |
These parameters apply both to the \verb|listing| environment and |
| 155 |
to the \verb|\filelisting| command. |
| 156 |
\begin{itemize} |
| 157 |
\item The amount of space inserted before and after a listing is |
| 158 |
given by \verb|\listingskipamount|. The default value is |
| 159 |
\verb|1ex|. Commands like the following can be used to adjust the |
| 160 |
amount of space. |
| 161 |
\begin{xlist} |
| 162 |
\setlength{\listingskipamount}{1\baselineskip} |
| 163 |
\end{xlist} |
| 164 |
\item The indentation of the left margin of the typeset code is |
| 165 |
determined by \verb|\listingindent|. The default value is |
| 166 |
\verb|2em|. Commands like the following can be used to adjust |
| 167 |
indentation. |
| 168 |
\begin{xlist} |
| 169 |
\setlength{\listingindent}{2cm} |
| 170 |
\end{xlist} |
| 171 |
\item The font used in the listing is controlled by the macro |
| 172 |
\verb|\listingfont|. The default value is \verb|\normallistingfont| |
| 173 |
which sets up a typewriter-like font. Example: The following |
| 174 |
command can be used to obtain more compact listings by slightly |
| 175 |
reducing the font size and the line spacing. |
| 176 |
\begin{xlist} |
| 177 |
\renewcommand{\listingfont}% |
| 178 |
{\normallistingfont\small\renewcommand{\baselinestretch}{0.95}} |
| 179 |
\end{xlist} |
| 180 |
\item The penalty for page breaks inside a listing is given by |
| 181 |
\verb|\listingpenalty|. The higher this value, the less attractive |
| 182 |
page breaks inside the listing are to \LaTeX's page breaking |
| 183 |
algorithm. The default value is 500. The following command can be |
| 184 |
used to completely disable page breaks inside listings. |
| 185 |
\begin{xlist} |
| 186 |
\listingpenalty=10000 |
| 187 |
\end{xlist} |
| 188 |
\item The penalty for page breaks just before or after a listing is |
| 189 |
given by \verb|\prelistingpenalty| (default value 100) and |
| 190 |
\verb|\postlistingpenalty| (default value -50), respectively. |
| 191 |
\end{itemize} |
| 192 |
|
| 193 |
\section{Extending the \textsf{listing} Environment} |
| 194 |
|
| 195 |
New \verb|listing|-like environments can be defined using the |
| 196 |
\verb|\NewListingEnvironment| macro. This macro takes six arguments |
| 197 |
and uses them to define a new \LaTeX\ environment. The arguments, in |
| 198 |
order, are |
| 199 |
\begin{enumerate} |
| 200 |
\item The name of the new environment. |
| 201 |
\item The number of extra arguments for the new environment |
| 202 |
(normally~0). These extra arguments of the environment, if any, are |
| 203 |
substituted into the initialisation commands (\textit{i.e.}\ into |
| 204 |
the fourth argument of \verb|\NewListingEnvironment|); see the |
| 205 |
\verb|copylisting| environment, below, for an example. |
| 206 |
\item Commands to execute before the environment is entered |
| 207 |
(\textit{e.g.}\ to add vertical white space). |
| 208 |
\item Initialisation commands, executed inside the environment |
| 209 |
(\textit{e.g.}\ font/margin setup). |
| 210 |
\item The name of a macro (receiving one argument) which will be used |
| 211 |
to typeset each line of input. |
| 212 |
\item Commands to execute after the environment is completed. |
| 213 |
\end{enumerate} |
| 214 |
|
| 215 |
\medskip |
| 216 |
|
| 217 |
\textbf{Example.} The \verb|listing| environment provided by the |
| 218 |
\verb|jvlisting| package is defined using the following commands: |
| 219 |
% \iffalse |
| 220 |
%</usage> |
| 221 |
%<*listingdef|usage> |
| 222 |
% \fi |
| 223 |
|
| 224 |
% \begin{macrocode} |
| 225 |
\let\listingfont=\normallistingfont |
| 226 |
\newcommand{\ListingTypesetLine}[1]{\ifvmode\penalty\listingpenalty\noindent\fi |
| 227 |
\hskip\listingindent\strut#1\par} |
| 228 |
\newcommand{\prelistingskip}{\endgraf\ifdim\lastskip>\listingskipamount\else |
| 229 |
\removelastskip\penalty\prelistingpenalty\vskip\listingskipamount\fi} |
| 230 |
\newcommand{\postlistingskip}{\endgraf\penalty\postlistingpenalty |
| 231 |
\vskip-\parskip\nobreak\vskip\listingskipamount\noindent} |
| 232 |
\NewListingEnvironment{listing}{0}{\prelistingskip}% |
| 233 |
{\listingfont\let\ListingStartHook\listingstarthook}{\ListingTypesetLine}% |
| 234 |
{\postlistingskip\ignorespacesafterend} |
| 235 |
% \end{macrocode} |
| 236 |
% \iffalse |
| 237 |
%</listingdef|usage> |
| 238 |
%<*usage> |
| 239 |
% \fi |
| 240 |
|
| 241 |
\medbreak |
| 242 |
|
| 243 |
\textbf{Example.} An \verb|nlisting| environment which generates |
| 244 |
listings with additional line numbers can be defined as follows: |
| 245 |
% \iffalse |
| 246 |
%</usage> |
| 247 |
%<*examples|usage> |
| 248 |
% \fi |
| 249 |
|
| 250 |
% \begin{macrocode} |
| 251 |
\newcounter{lineno} |
| 252 |
\newcommand{\typesetnline}[1]{\addtocounter{lineno}{1}% |
| 253 |
\noindent\hskip\listingindent\llap{{\it\scriptsize\arabic{lineno}: }}% |
| 254 |
\strut #1\par\penalty\listingpenalty} |
| 255 |
\NewListingEnvironment{nlisting}{0}{\prelistingskip}% |
| 256 |
{\setcounter{lineno}{0}\listingfont}{\typesetnline}{\postlistingskip} |
| 257 |
% \end{macrocode} |
| 258 |
% \iffalse |
| 259 |
%</examples|usage>> |
| 260 |
%<*examples> |
| 261 |
\begin{nlisting} |
| 262 |
for i in [1, 2, 3]: |
| 263 |
for j in ['a', 'b', 'c']: |
| 264 |
print str(i)+j |
| 265 |
\end{nlisting} |
| 266 |
%</examples> |
| 267 |
%<*usage> |
| 268 |
% \fi |
| 269 |
|
| 270 |
\medbreak |
| 271 |
|
| 272 |
\textbf{Example.} The following code defines a new \verb|copylisting| |
| 273 |
environment which does not only typeset the source code, but also |
| 274 |
saves a copy in an external file. Here we need to be careful in case |
| 275 |
the quote character \verb|`| can be present in the listing: by |
| 276 |
default, \verb|\listingfont| makes \verb|`| an active character in |
| 277 |
order to prevent problems with ligatures, but this fix causes damage |
| 278 |
to the output of the \verb|\write| command. To work around this |
| 279 |
problem, we can use \verb|\DisableLigatureFix| (defined by the |
| 280 |
\verb|jvlisting| package for just this purpose) to temporarily disable |
| 281 |
the ligature fix. |
| 282 |
% \iffalse |
| 283 |
%</usage> |
| 284 |
%<*examples|usage> |
| 285 |
% \fi |
| 286 |
|
| 287 |
% \begin{macrocode} |
| 288 |
\newwrite\outfile |
| 289 |
\newcommand{\copytypeset}[1]{\ListingTypesetLine{#1}% |
| 290 |
{\DisableLigatureFix\immediate\write\outfile{#1}}} |
| 291 |
\NewListingEnvironment{copylisting}{1}{\prelistingskip}% |
| 292 |
{\immediate\openout\outfile=#1\listingfont}{\copytypeset}% |
| 293 |
{\immediate\closeout\outfile\postlistingskip} |
| 294 |
% \end{macrocode} |
| 295 |
% \iffalse |
| 296 |
%</examples|usage>> |
| 297 |
%<*usage> |
| 298 |
% \fi |
| 299 |
Here we used the second argument to \verb|\NewListingEnvironment| to |
| 300 |
indicate that the \verb|copylisting| environment should take an |
| 301 |
additional parameter (the output file name). The new environment is |
| 302 |
used as follows: |
| 303 |
% \iffalse |
| 304 |
%</usage> |
| 305 |
%<*examples|usage> |
| 306 |
% \fi |
| 307 |
|
| 308 |
% \begin{macrocode} |
| 309 |
\begin{copylisting}{listing1.c} |
| 310 |
#include <stdio.h> |
| 311 |
|
| 312 |
int |
| 313 |
main() |
| 314 |
{ |
| 315 |
puts("hello, world!"); |
| 316 |
return 0; |
| 317 |
} |
| 318 |
\end{copylisting} |
| 319 |
% \end{macrocode} |
| 320 |
% \iffalse |
| 321 |
%</examples|usage>> |
| 322 |
%<*usage> |
| 323 |
% \fi |
| 324 |
|
| 325 |
\section{Extending the \textsf{filelisting} Command} |
| 326 |
|
| 327 |
New \verb|filelisting|-like commands can be defined using the |
| 328 |
\verb|\NewFileListingCommand| macro. This macro takes six arguments |
| 329 |
and uses them to define a new macro. The arguments, in order, are |
| 330 |
\begin{enumerate} |
| 331 |
\item The name of the new command including the leading backslash. |
| 332 |
\item The number of arguments for the new command (normally~1). These |
| 333 |
arguments, including the first one which gives the file name, are |
| 334 |
substituted into the third, fourth and sixth argument of |
| 335 |
\verb|\NewFileListingCommand|); see the \verb|\prefixfilelisting| |
| 336 |
command, below, for an example. |
| 337 |
\item Commands to execute before the listing is started |
| 338 |
(\textit{e.g.}\ to add vertical white space). |
| 339 |
\item Initialisation commands, executed inside the scope of the |
| 340 |
listing (\textit{e.g.}\ font/margin setup). |
| 341 |
\item The name of a macro (receiving one argument) which will be used |
| 342 |
to typeset each line of input. |
| 343 |
\item Commands to execute after the listing is completed. |
| 344 |
\end{enumerate} |
| 345 |
The first argument of the newly defined macro always denotes the name |
| 346 |
of the file to include. |
| 347 |
|
| 348 |
\medskip |
| 349 |
|
| 350 |
\textbf{Example.} Using the auxiliary functions for the |
| 351 |
\verb|listing| environment, the built-in \verb|filelisting| command |
| 352 |
can be defined as follows: |
| 353 |
% \iffalse |
| 354 |
%</usage> |
| 355 |
%<*listingdef|usage> |
| 356 |
% \fi |
| 357 |
|
| 358 |
% \begin{macrocode} |
| 359 |
\NewFileListingCommand{\filelisting}{1}{\prelistingskip}% |
| 360 |
{\listingfont}{\ListingTypesetLine}{\postlistingskip} |
| 361 |
% \end{macrocode} |
| 362 |
% \iffalse |
| 363 |
%</listingdef|usage> |
| 364 |
%<*usage> |
| 365 |
% \fi |
| 366 |
|
| 367 |
\medbreak |
| 368 |
|
| 369 |
\textbf{Example.} The following command defines a macro to read a |
| 370 |
file and to prefix every line of the resulting listing with a given |
| 371 |
string. |
| 372 |
% \iffalse |
| 373 |
%</usage> |
| 374 |
%<*examples|usage> |
| 375 |
% \fi |
| 376 |
|
| 377 |
% \begin{macrocode} |
| 378 |
\newcommand{\pfxtypeset}[1]{\noindent\hskip\listingindent\strut |
| 379 |
\pfx#1\par\penalty\listingpenalty} |
| 380 |
\NewFileListingCommand{\prefixfilelisting}{2}{\prelistingskip}% |
| 381 |
{\listingfont\def\pfx{#2}}{\pfxtypeset}{\postlistingskip} |
| 382 |
% \end{macrocode} |
| 383 |
% \iffalse |
| 384 |
%</examples|usage>> |
| 385 |
%<*examples> |
| 386 |
\prefixfilelisting{listing1.c}{c> } |
| 387 |
%</examples> |
| 388 |
%<*package> |
| 389 |
% \fi |
| 390 |
|
| 391 |
% \StopEventually{} |
| 392 |
% \section{Implementation} |
| 393 |
% |
| 394 |
% This section describes the internal implementation of the |
| 395 |
% \verb|jvlisting| package. In order to avoid name clashes with other |
| 396 |
% packages, the names of all internal macros defined in this package |
| 397 |
% start with the prefix \verb|jvl@|. The following is the preamble |
| 398 |
% for the package file. |
| 399 |
% \begin{macrocode} |
| 400 |
\NeedsTeXFormat{LaTeX2e} |
| 401 |
\ProvidesPackage{jvlisting}[2011/11/17 v0.7 Formatted Program Listings] |
| 402 |
% \end{macrocode} |
| 403 |
|
| 404 |
% \subsection{Processing the Lines of Input} |
| 405 |
% |
| 406 |
% We start by provinding a macro \verb|\jvl@iterlines| which can be |
| 407 |
% used to iterate over all lines of input until a line containing some |
| 408 |
% marker is found. The marker is given as argument \verb|#1| to the |
| 409 |
% \verb|\jvl@iterlines| macro. Each line is processed by prepending |
| 410 |
% \verb|\jvl@dropempty| (for the first and last line) or |
| 411 |
% \verb|\jvl@tryhook| (for all other lines). |
| 412 |
% \begin{macrocode} |
| 413 |
\def\jvl@iterlines#1{\expandafter\jvl@iterla\expandafter{#1}{\jvl@dropempty}} |
| 414 |
{\obeylines\gdef\jvl@iterla#1#2#3 |
| 415 |
{\def\jvl@testmarker##1#1{}% |
| 416 |
\expandafter\def\expandafter\w\expandafter{\jvl@testmarker#3#1}% |
| 417 |
\ifx\w\empty% |
| 418 |
\def\next{#2{\jvl@iterla{#1}{\jvl@tryhook}}#3 |
| 419 |
}% |
| 420 |
\else% |
| 421 |
% special treatment for the last line |
| 422 |
\def\y##1#1##2#1{\jvl@dropempty{\jvl@end##2}##1 |
| 423 |
}% |
| 424 |
\def\next{\y#3#1}% |
| 425 |
\fi\next}} |
| 426 |
% \end{macrocode} |
| 427 |
|
| 428 |
% The next step in processing is to drop the first line (whatever |
| 429 |
% comes directly after the opening \verb|begin| statement) and the |
| 430 |
% last line (whatever comes directly before the closing \verb|end| |
| 431 |
% statement), if these lines consist only of white space. |
| 432 |
% \begin{macrocode} |
| 433 |
\def\jvl@dropempty#1{\jvl@dropa{#1}{}} |
| 434 |
{\obeylines\gdef\jvl@dropa#1#2#3{\ifx |
| 435 |
#3\def\next{#1}% |
| 436 |
\else% |
| 437 |
\if#3 \def\next{\jvl@dropa{#1}{#2#3}}\else% |
| 438 |
\def\next{\jvl@tryhook{#1}#2#3}\fi% |
| 439 |
\fi\next}} |
| 440 |
% \end{macrocode} |
| 441 |
|
| 442 |
% Next we try to apply the \verb|\ListingStartHook|. |
| 443 |
% \begin{macrocode} |
| 444 |
\def\jvl@tryhook#1{\ifx\ListingStartHook\undefined |
| 445 |
\def\next{\jvl@countspaces{#1}}% |
| 446 |
\else |
| 447 |
\def\jvl@trya{\let\ListingStartHook\undefined\jvl@countspaces{#1}}% |
| 448 |
\def\next{\ListingStartHook{\jvl@trya}}% |
| 449 |
\fi |
| 450 |
\next} |
| 451 |
% \end{macrocode} |
| 452 |
|
| 453 |
% Next we determine the indentation level of the current line by |
| 454 |
% expanding TAB characters and then counting spaces. The result is |
| 455 |
% stored in the scratch counter \verb|@tempcnta|. |
| 456 |
% \begin{macrocode} |
| 457 |
\def\jvl@countspaces#1{\@tempcnta=0\jvl@counta{#1}} |
| 458 |
{\catcode`\^^I=12 |
| 459 |
\gdef\jvl@counta#1#2{\if^^I#2% |
| 460 |
\advance\@tempcnta by8\divide\@tempcnta by8\multiply\@tempcnta by8 |
| 461 |
\def\next{\jvl@counta{#1}}% |
| 462 |
\else |
| 463 |
\expandafter\if\noexpand#2 % |
| 464 |
\advance\@tempcnta by1 |
| 465 |
\def\next{\jvl@counta{#1}}% |
| 466 |
\else |
| 467 |
\def\next{\jvl@fixspaces{#1}#2}% |
| 468 |
\fi |
| 469 |
\fi\next}} |
| 470 |
% \end{macrocode} |
| 471 |
|
| 472 |
% Using the value in \verb|@tempcnta|, we fix the indentation by first |
| 473 |
% subtracting the common indentation level (stored in \verb|\jvl@idt|) |
| 474 |
% and then inserting the required number of spaces (using |
| 475 |
% \verb|\space|). |
| 476 |
% \begin{macrocode} |
| 477 |
\newcount\jvl@idt \jvl@idt=\m@ne |
| 478 |
{\obeylines\gdef\jvl@fixspaces#1#2{\ifx |
| 479 |
#2\else% |
| 480 |
\ifnum\jvl@idt<0% |
| 481 |
\jvl@idt=\@tempcnta% |
| 482 |
\else% |
| 483 |
\ifnum\@tempcnta<\jvl@idt\jvl@idt=\@tempcnta\fi% |
| 484 |
\fi\fi\jvl@fixa{#1}#2}} |
| 485 |
\def\jvl@fixa#1{\ifnum\@tempcnta>\jvl@idt |
| 486 |
\advance\@tempcnta by\m@ne |
| 487 |
\def\next{\jvl@fixa{#1}\space}% |
| 488 |
\else |
| 489 |
\def\next{\jvl@output{#1}}% |
| 490 |
\fi\next} |
| 491 |
% \end{macrocode} |
| 492 |
|
| 493 |
% Finally, we apply the output function \verb|\jvl@typeset| to the |
| 494 |
% processed line and starting processing of the next line. |
| 495 |
% \begin{macrocode} |
| 496 |
{\obeylines\gdef\jvl@output#1#2 |
| 497 |
{\jvl@typeset{#2}#1}} |
| 498 |
% \end{macrocode} |
| 499 |
% The symbol \verb|\jvl@typeset| will be defined inside the |
| 500 |
% \verb|\begin{listing}| command; since neested listings are not |
| 501 |
% possible, using a global name for the output function is no |
| 502 |
% problem. |
| 503 |
|
| 504 |
% \subsection{Defining Listing Environments} |
| 505 |
% |
| 506 |
% In order to allow for verbatim printing, the macro |
| 507 |
% \verb|jvl@fixparsing| switches off the special meaning of most |
| 508 |
% characters. Some care is needed to avoid problems with spaces and |
| 509 |
% hyphenation. |
| 510 |
% \begin{macrocode} |
| 511 |
{\catcode`\ =\active% |
| 512 |
\gdef\jvl@obeyspaces{\frenchspacing\catcode`\ =\active\let \space}} |
| 513 |
\def\jvl@fixparsing{\let\do\@makeother\dospecials\catcode`\^^I=12 |
| 514 |
\jvl@obeyspaces\hyphenchar\font\m@ne} |
| 515 |
% \end{macrocode} |
| 516 |
|
| 517 |
% Since the \verb|\hyphenchar| setting is global, we save save the |
| 518 |
% value of \verb|hyphenchar| before entering the listing environment |
| 519 |
% end restore the original setting at the end. |
| 520 |
% \begin{macrocode} |
| 521 |
\newcount\jvl@tmphyphenchar |
| 522 |
\def\jvl@begingroup{\jvl@tmphyphenchar=\hyphenchar\font |
| 523 |
\begingroup \parskip0pt \advance\leftskip by\@totalleftmargin} |
| 524 |
\def\jvl@endgroup{\endgroup \hyphenchar\font=\jvl@tmphyphenchar} |
| 525 |
% \end{macrocode} |
| 526 |
|
| 527 |
% Given the code above, we can now define the |
| 528 |
% \verb|\NewListingEnvironment| macro. Some care is needed when |
| 529 |
% constructing the marker for use in the \verb|\jvl@iterlines| macro, |
| 530 |
% because the text we need to match uses category code 12 (``other'') |
| 531 |
% for the characters \verb|\|, \verb|{| and~\verb|}|. |
| 532 |
% \begin{macrocode} |
| 533 |
\begingroup |
| 534 |
\catcode`|=0 \catcode`[=1 \catcode`]=2 |
| 535 |
\catcode`\{=12 \catcode`\}=12 \catcode`\\=12 |
| 536 |
|gdef|jvl@makemarker#1[% |
| 537 |
|expandafter|gdef|csname jvl@@#1marker|endcsname[\end{#1}]] |
| 538 |
|endgroup |
| 539 |
\def\NewListingEnvironment#1#2#3#4#5#6{\jvl@makemarker{#1}% |
| 540 |
\expandafter\newcommand\csname #1\endcsname[#2]{#3\jvl@begingroup |
| 541 |
#4\def\jvl@end{\end{#1}}\let\jvl@typeset=#5% |
| 542 |
\jvl@fixparsing\obeylines |
| 543 |
\expandafter\def\expandafter\jvl@start\expandafter{% |
| 544 |
\expandafter\jvl@iterlines\csname jvl@@#1marker\endcsname}% |
| 545 |
\let\next\jvl@start |
| 546 |
\next}% |
| 547 |
\expandafter\gdef\csname end#1\endcsname{\jvl@endgroup #6}} |
| 548 |
% \end{macrocode} |
| 549 |
|
| 550 |
% \subsection{Reading Listings from File} |
| 551 |
% |
| 552 |
% When reading listings from a file, we get the lines terminated by |
| 553 |
% \verb|^^M| characters. The following function is a replacement for |
| 554 |
% \verb|\jvl@iterlines|, used to read lines from a file and to strip |
| 555 |
% the trailing \verb|^^M| characters: |
| 556 |
% \begin{macrocode} |
| 557 |
\newread\jvl@fileinput |
| 558 |
\def\jvl@iterfile{\read\jvl@fileinput to\l |
| 559 |
\ifeof\jvl@fileinput |
| 560 |
\let\next\relax |
| 561 |
\else |
| 562 |
\expandafter\jvl@iterfa\l |
| 563 |
\let\next\jvl@iterfile |
| 564 |
\fi\next} |
| 565 |
{\catcode`\^^M=12 |
| 566 |
\gdef\jvl@iterfa#1^^M{\jvl@iterfb{#1}}} |
| 567 |
{\obeylines\gdef\jvl@iterfb#1{\jvl@tryhook{\relax}#1 |
| 568 |
}} |
| 569 |
% \end{macrocode} |
| 570 |
% |
| 571 |
% In analogy to \verb|\NewListingEnvironment|, the following macro is |
| 572 |
% used to define new functions for listing file contents. |
| 573 |
% \begin{macrocode} |
| 574 |
\def\NewFileListingCommand#1#2#3#4#5#6{% |
| 575 |
\ifnum#2<1 |
| 576 |
\PackageError{jvlisting}{% |
| 577 |
Invalid number of arguments: `#2' |
| 578 |
}{% |
| 579 |
The second argument to \protect\NewFileListingCommand\space must be at |
| 580 |
least 1. |
| 581 |
}% |
| 582 |
\fi |
| 583 |
\newcommand{#1}[#2]{#3\jvl@begingroup |
| 584 |
#4\let\jvl@typeset=#5% |
| 585 |
\jvl@fixparsing\catcode`\^^M=12 |
| 586 |
\openin\jvl@fileinput=##1\jvl@iterfile\closein\jvl@fileinput |
| 587 |
\jvl@endgroup#6% |
| 588 |
\ignorespaces |
| 589 |
}% |
| 590 |
} |
| 591 |
% \end{macrocode} |
| 592 |
|
| 593 |
% \subsection{Finishing Touches} |
| 594 |
% |
| 595 |
% The following definitions provide default values for the |
| 596 |
% customisable parameters of the \verb|listing| environment. |
| 597 |
% \begin{macrocode} |
| 598 |
\newskip\listingskipamount \listingskipamount=1ex |
| 599 |
\newdimen\listingindent \listingindent=2em |
| 600 |
\newcount\prelistingpenalty \prelistingpenalty=100 |
| 601 |
\newcount\listingpenalty \listingpenalty=500 |
| 602 |
\newcount\postlistingpenalty \postlistingpenalty=-50 |
| 603 |
% \end{macrocode} |
| 604 |
% Finally, we define the default font for use in listings. Some care |
| 605 |
% is needed to avoid problems with ligatures like \verb|?`|. We break |
| 606 |
% such ligatures by making \verb|`| and active character. For use in |
| 607 |
% file listings we also provide \verb|\DisableLigatureFix| to |
| 608 |
% (temporarily) disable this fix. |
| 609 |
% \begin{macrocode} |
| 610 |
{\catcode`\`=\active\gdef`{\relax\lq}% |
| 611 |
\gdef\DisableLigatureFix{\def`{\lq}}} |
| 612 |
\def\normallistingfont{\normalfont\ttfamily\catcode`\`=\active} |
| 613 |
% \end{macrocode} |
| 614 |
% \iffalse |
| 615 |
%</package> |
| 616 |
%<*examples> |
| 617 |
|
| 618 |
\filelisting{listing1.c} |
| 619 |
|
| 620 |
\end{document} |
| 621 |
%</examples> |
| 622 |
%<*install> |
| 623 |
|
| 624 |
\keepsilent |
| 625 |
\askforoverwritefalse |
| 626 |
|
| 627 |
\generate{% |
| 628 |
\file{jvlisting.ins}{\from{jvlisting.dtx}{install}}% |
| 629 |
\usedir{tex/latex/jvlisting}% |
| 630 |
\file{jvlisting.sty}{\from{jvlisting.dtx}{package}% |
| 631 |
\from{jvlisting.dtx}{listingdef}}% |
| 632 |
\file{examples.tex}{\from{jvlisting.dtx}{examples}}% |
| 633 |
} |
| 634 |
\endbatchfile |
| 635 |
%</install> |
| 636 |
% \fi |
| 637 |
% \endinput |
| 638 |
% |
| 639 |
% Local Variables: |
| 640 |
% whitespace-style: (face trailing tabs lines-tail empty space-before-tab) |
| 641 |
% whitespace-line-column: 78 |
| 642 |
% End: |