diff options
author | Karl Berry <karl@freefriends.org> | 2011-10-18 22:26:36 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2011-10-18 22:26:36 +0000 |
commit | 9570de045eef234423490cbec0cf6b72cf808d4e (patch) | |
tree | afce9ffbd434cf42158257b8b068c8db226b89b6 /Master/texmf-dist/source/latex/jvlisting | |
parent | 60d7a3cc1d113df4777e1fb10b0a68bab06ece5f (diff) |
jvlisting 0.5 (17oct11)
git-svn-id: svn://tug.org/texlive/trunk@24315 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/source/latex/jvlisting')
-rw-r--r-- | Master/texmf-dist/source/latex/jvlisting/jvlisting.dtx | 179 |
1 files changed, 105 insertions, 74 deletions
diff --git a/Master/texmf-dist/source/latex/jvlisting/jvlisting.dtx b/Master/texmf-dist/source/latex/jvlisting/jvlisting.dtx index 3796931f94c..162eee6be56 100644 --- a/Master/texmf-dist/source/latex/jvlisting/jvlisting.dtx +++ b/Master/texmf-dist/source/latex/jvlisting/jvlisting.dtx @@ -54,12 +54,12 @@ \begin{abstract} This package provides the \LaTeX\ environment \verb|listing|, an alternative to the built-in \verb|verbatim| environment. The - \verb|listing| environment is specially taylored for including + \verb|listing| environment is specially tailored for including listings of computer program source code into documents. The main advantages over the original \verb|verbatim| environment are that \verb|listing| environments automatically fix leading white-space so that the environment and program listing can be indented with - the rest of the \LaTeX\ source code and that \verb|listing| + the rest of the document source and that \verb|listing| environments can easily be customised and extended. \end{abstract} @@ -159,7 +159,7 @@ the \verb|\filelisting| command. \verb|\listingfont|. The default value is \verb|\normallistingfont| which sets up a typewriter-like font. Example: The following command can be used to obtain more compact listings by slightly - reducing the fontsize and the line spacing. + reducing the font size and the line spacing. \begin{xlist} \renewcommand{\listingfont}% {\normallistingfont\small\renewcommand{\baselinestretch}{0.95}} @@ -191,7 +191,7 @@ order, are the fourth argument of \verb|\NewListingEnvironment|); see the \verb|copylisting| environment, below, for an example. \item Commands to execute before the environment is entered - (\textit{e.g.}\ to add vertical whitespace). + (\textit{e.g.}\ to add vertical white space). \item Initialisation commands, executed inside the environment (\textit{e.g.}\ font/margin setup). \item The name of a macro (receiving one argument) which will be used @@ -210,14 +210,15 @@ order, are % \begin{macrocode} \let\listingfont=\normallistingfont -\newcommand{\ListingTypesetLine}[1]{\noindent\hskip\listingindent\strut - #1\par\penalty\listingpenalty} -\newcommand{\prelistingskip}{\endgraf\ifdim\lastskip<\listingskipamount +\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\listingskipamount} + \vskip-\parskip\nobreak\vskip\listingskipamount\noindent} \NewListingEnvironment{listing}{0}{\prelistingskip}% - {\listingfont}{\ListingTypesetLine}{\postlistingskip} + {\listingfont\let\ListingStartHook\listingstarthook}{\ListingTypesetLine}% + {\postlistingskip\ignorespacesafterend} % \end{macrocode} % \iffalse %</listingdef|usage> @@ -314,7 +315,7 @@ and uses them to define a new macro. The arguments, in order, are \verb|\NewFileListingCommand|); see the \verb|\prefixfilelisting| command, below, for an example. \item Commands to execute before the listing is started - (\textit{e.g.}\ to add vertical whitespace). + (\textit{e.g.}\ to add vertical white space). \item Initialisation commands, executed inside the scope of the listing (\textit{e.g.}\ font/margin setup). \item The name of a macro (receiving one argument) which will be used @@ -376,62 +377,73 @@ string. % for the package file. % \begin{macrocode} \NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{jvlisting}[2011/06/27 v0.1 Formatted Program Listings] +\ProvidesPackage{jvlisting}[2011/10/17 v0.5 Formatted Program Listings] % \end{macrocode} % \subsection{Processing the Lines of Input} % % We start by provinding a macro \verb|\jvl@iterlines| which can be % used to iterate over all lines of input until a line containing some -% marker is found. The marker is given as the argument \verb|#1| to -% the \verb|\jvl@iterlines| macro. Each line is processed by -% appending the token \verb|\jvl@eol| (used later to recognise the end -% of line) and by prepending \verb|\jvl@dropempty| (for the first and -% last line) or \verb|\jvl@countspaces| (for all other lines). +% marker is found. The marker is given as argument \verb|#1| to the +% \verb|\jvl@iterlines| macro. Each line is processed by prepending +% \verb|\jvl@dropempty| (for the first and last line) or +% \verb|\jvl@tryhook| (for all other lines). % \begin{macrocode} -\def\jvl@iterlines#1{\obeylines - \expandafter\jvl@iterla\expandafter{#1}{\jvl@dropempty}} -{\obeylines -\gdef\jvl@iterla#1#2#3 +\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#3\jvl@eol\jvl@iterla{#1}{\jvl@countspaces}}% + \def\next{#2{\jvl@iterla{#1}{\jvl@tryhook}}#3 + }% \else% - \def\y##1#1##2#1{\jvl@dropempty ##1\jvl@eol\jvl@end##2}% + % 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}} % \end{macrocode} % The next step in processing is to drop the first line (whatever % comes directly after the opening \verb|begin| statement) and the -% last line (whatever comes directly after the closing \verb|end| -% statement), if they consist only of white space. +% last line (whatever comes directly before the closing \verb|end| +% statement), if these lines consist only of white space. +% \begin{macrocode} +\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}} +% \end{macrocode} + +% Next we try to apply the \verb|\ListingStartHook|. % \begin{macrocode} -\def\jvl@dropempty{\jvl@dropa{}} -\def\jvl@dropa#1#2{\ifx\jvl@eol#2% - \let\next=\relax +\def\jvl@tryhook#1{\ifx\ListingStartHook\undefined + \def\next{\jvl@countspaces{#1}}% \else - \if#2 \def\next{\jvl@dropa{#1#2}}\else - \def\next{\jvl@countspaces #1#2}\fi - \fi\next} + \def\jvl@trya{\let\ListingStartHook\undefined\jvl@countspaces{#1}}% + \def\next{\ListingStartHook{\jvl@trya}}% + \fi + \next} % \end{macrocode} % Next we determine the indentation level of the current line by % expanding TAB characters and then counting spaces. The result is % stored in the scratch counter \verb|@tempcnta|. % \begin{macrocode} -\def\jvl@countspaces{\@tempcnta=0\jvl@counta} +\def\jvl@countspaces#1{\@tempcnta=0\jvl@counta{#1}} {\catcode`\^^I=12 -\gdef\jvl@counta#1{\expandafter\if\noexpand#1^^I% +\gdef\jvl@counta#1#2{\if^^I#2% \advance\@tempcnta by8\divide\@tempcnta by8\multiply\@tempcnta by8 - \let\next=\jvl@counta + \def\next{\jvl@counta{#1}}% \else - \expandafter\if\noexpand#1 % + \expandafter\if\noexpand#2 % \advance\@tempcnta by1 - \let\next=\jvl@counta + \def\next{\jvl@counta{#1}}% \else - \def\next{\jvl@fixspaces #1} + \def\next{\jvl@fixspaces{#1}#2}% \fi \fi\next}} % \end{macrocode} @@ -441,26 +453,27 @@ string. % and then inserting the required number of spaces (using % \verb|\space|). % \begin{macrocode} -\newcount\jvl@idt \jvl@idt=-1 -\def\jvl@fixspaces#1{\ifx\jvl@eol#1\else - \ifnum\jvl@idt<0 - \jvl@idt=\@tempcnta - \else - \ifnum\@tempcnta<\jvl@idt\jvl@idt=\@tempcnta\fi - \fi\fi\jvl@fixa#1} -\def\jvl@fixa{\ifnum\@tempcnta>\jvl@idt +\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\space}% + \def\next{\jvl@fixa{#1}\space}% \else - \let\next\jvl@output + \def\next{\jvl@output{#1}}% \fi\next} % \end{macrocode} -% Finally, by matching the \verb|jvl@eol| inserted by the -% \verb|\jvl@iterlines| function above, we can apply the output -% function \verb|\jvl@typeset| to the processed line. +% Finally, we apply the output function \verb|\jvl@typeset| to the +% processed line and starting processing of the next line. % \begin{macrocode} -\def\jvl@output#1\jvl@eol{\jvl@typeset{#1}} +{\obeylines\gdef\jvl@output#1#2 + {\jvl@typeset{#2}#1}} % \end{macrocode} % The symbol \verb|\jvl@typeset| will be defined inside the % \verb|\begin{listing}| command; since neested listings are not @@ -469,11 +482,34 @@ string. % \subsection{Defining Listing Environments} % +% In order to allow for verbatim printing, the macro +% \verb|jvl@fixparsing| switches off the special meaning of most +% characters. Some care is needed to avoid problems with spaces and +% ligatures like \verb|?`|. +% \begin{macrocode} +{\catcode`\ =\active% +\gdef\jvl@obeyspaces{\frenchspacing\catcode`\ =\active\let \space}} +{\catcode`\`=\active\gdef`{\relax\lq}} +\gdef\jvl@noligs{\catcode`\`=\active} +\def\jvl@fixparsing{\let\do\@makeother\dospecials\catcode`\^^I=12 + \jvl@obeyspaces\jvl@noligs\hyphenchar\font\m@ne} +% \end{macrocode} + +% Since the \verb|\hyphenchar| setting is global, we save save the +% value of \verb|hyphenchar| before entering the listing environment +% end restore the original setting at the end. +% \begin{macrocode} +\newcount\jvl@tmphyphenchar +\def\jvl@begingroup{\jvl@tmphyphenchar=\hyphenchar\font + \begingroup \parskip0pt \advance\leftskip by\@totalleftmargin} +\def\jvl@endgroup{\endgroup \hyphenchar\font=\jvl@tmphyphenchar} +% \end{macrocode} + % Given the code above, we can now define the % \verb|\NewListingEnvironment| macro. Some care is needed when -% constructing the marker for use in the \verb|\jvl@iterlines| method, +% constructing the marker for use in the \verb|\jvl@iterlines| macro, % because the text we need to match uses category code 12 (``other'') -% for all characters. +% for the characters \verb|\|, \verb|{| and~\verb|}|. % \begin{macrocode} \begingroup \catcode`|=0 \catcode`[=1 \catcode`]=2 @@ -481,14 +517,15 @@ string. |gdef|jvl@makemarker#1[% |expandafter|gdef|csname jvl@@#1marker|endcsname[\end{#1}]] |endgroup -\def\jvl@setup{\begingroup - \parskip0pt \advance\leftskip by\@totalleftmargin - \let\do\@makeother\dospecials \catcode`\^^I=12} \def\NewListingEnvironment#1#2#3#4#5#6{\jvl@makemarker{#1}% - \expandafter\newcommand\csname #1\endcsname[#2]{#3\jvl@setup + \expandafter\newcommand\csname #1\endcsname[#2]{#3\jvl@begingroup #4\def\jvl@end{\end{#1}}\let\jvl@typeset=#5% - \expandafter\jvl@iterlines\csname jvl@@#1marker\endcsname}% - \expandafter\gdef\csname end#1\endcsname{\endgroup #6}} + \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}} % \end{macrocode} % \subsection{Reading Listings from File} @@ -496,7 +533,7 @@ string. % When reading listings from a file, we get the lines terminated by % \verb|^^M| characters. The following function is a replacement for % \verb|\jvl@iterlines|, used to read lines from a file and to strip -% of the trailing \verb|^^M| characters: +% the trailing \verb|^^M| characters: % \begin{macrocode} \newread\jvl@fileinput \def\jvl@iterfile{\read\jvl@fileinput to\l @@ -507,7 +544,9 @@ string. \let\next\jvl@iterfile \fi\next} {\catcode`\^^M=12 -\gdef\jvl@iterfa#1^^M{\jvl@countspaces#1\jvl@eol}} +\gdef\jvl@iterfa#1^^M{\jvl@iterfb{#1}}} +{\obeylines\gdef\jvl@iterfb#1{\jvl@tryhook{\relax}#1 + }} % \end{macrocode} % % In analogy to \verb|\NewListingEnvironment|, the following macro is @@ -522,11 +561,13 @@ string. least 1. }% \fi - \newcommand{#1}[#2]{#3\jvl@setup\catcode`\^^M=12 + \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 - \endgroup #6 - } + \jvl@endgroup#6% + \ignorespaces + }% } % \end{macrocode} @@ -540,17 +581,7 @@ string. \newcount\prelistingpenalty \prelistingpenalty=100 \newcount\listingpenalty \listingpenalty=500 \newcount\postlistingpenalty \postlistingpenalty=-50 -% \end{macrocode} - -% Finally, we need to define the font for use in listing environments. -% Some care is needed to avoid problems with spaces and ligatures. -% \begin{macrocode} -{\catcode`\ =\active% -\gdef\jvl@obeyspaces{\frenchspacing\catcode`\ =\active\let \space}} -{\catcode`\`=\active\gdef`{\relax\lq}} -\gdef\jvl@noligs{\catcode`\`=\active} -\def\normallistingfont{\normalfont\ttfamily - \jvl@obeyspaces\jvl@noligs\hyphenchar\font-1} +\def\normallistingfont{\normalfont\ttfamily} % \end{macrocode} % \iffalse %</package> |