3476 }{\def\lst@next{\@ifstar{\let\lst@beginfloat\@dblfloat
3477 \let\lst@endfloat\end@dblfloat
3478 \lst@KFloat}%
3479 {\let\lst@beginfloat\@float
3480 \let\lst@endfloat\end@float
3481 \lst@KFloat}}
3482 \edef\lst@float{#1}%
3483 \expandafter\lst@next\lst@float\relax}}
. . . and define\lst@float.
3484\def\lst@KFloat#1\relax{%
3485 \ifx\@empty#1\@empty
3486 \let\lst@float\lst@floatplacement
3487 \else
3488 \def\lst@float{#1}%
3489 \fi}
The setting\lst@AddToHook{PreSet}{\let\lst@float\relax}has been changed on request of Tanguy Fautr´e. This also led to some adjustments above.
3490\lst@Key{floatplacement}{tbp}{\def\lst@floatplacement{#1}}
3491\lst@AddToHook{PreSet}{\let\lst@float\lst@floatdefault}
3492\lst@AddToHook{TextStyle}{\let\lst@float\relax}
3493\let\lst@floatdefault\relax % init
\lst@doendpe is set according to\lst@float– thanks to Andreas Schmidt and Heiko Oberdiek.
3494\lst@AddToHook{DeInit}{%
3495 \ifx\lst@float\relax
3496 \global\let\lst@doendpe\@doendpe
3497 \else
3498 \global\let\lst@doendpe\@empty
3499 \fi}
The float type\ftype@lstlisting is set according to whether the floatpackage is loaded and whether figure and table floats are defined. This is done at
\begin{document}to make the code independent of the order of package loading.
3500\AtBeginDocument{%
3501\@ifundefined{c@float@type}%
3502 {\edef\ftype@lstlisting{\ifx\c@figure\@undefined 1\else 4\fi}}
3503 {\edef\ftype@lstlisting{\the\c@float@type}%
3504 \addtocounter{float@type}{\value{float@type}}}%
3505}
3512\lst@AddToHook{DisplayStyle}{\let\lst@ifdisplaystyle\iftrue}
3513\let\lst@ifdisplaystyle\iffalse
\lst@Init Begin a float or multicolumn environment if requested.
3514\def\lst@Init#1{%
3515 \begingroup
3516 \ifx\lst@float\relax\else
3517 \edef\@tempa{\noexpand\lst@beginfloat{lstlisting}[\lst@float]}%
3518 \expandafter\@tempa
3519 \fi
3520 \ifx\lst@multicols\@empty\else
3521 \edef\lst@next{\noexpand\multicols{\lst@multicols}}
3522 \expandafter\lst@next
3523 \fi
In restricted horizontal TEX mode we switch to \lst@boxtrue. In that case we make appropriate box(es) around the listing.
3524 \ifhmode\ifinner \lst@boxtrue \fi\fi
3525 \lst@ifbox
3526 \lsthk@BoxUnsafe
3527 \hbox to\z@\bgroup
3528 $\if t\lst@boxpos \vtop
3529 \else \if b\lst@boxpos \vbox
3530 \else \vcenter \fi\fi
3531 \bgroup \par\noindent
3532 \else
3533 \lst@ifdisplaystyle
3534 \lst@EveryDisplay
3535 \par\penalty-50\relax
3536 \vspace\lst@aboveskip
3537 \fi
3538 \fi
Moved \vspace after \par—or we can get an empty line atop listings. Bug re- ported by Jim Hefferon.
Now make the top caption.
3539 \normalbaselines
3540 \abovecaptionskip\lst@abovecaption\relax
3541 \belowcaptionskip\lst@belowcaption\relax
3542 \lst@MakeCaption t%
Some initialization. I removed \par\nointerlineskip \normalbaselines af- ter bug report from Jim Hefferon. He reported the same problem as Aidan Philip Heerdegen (see below), but I immediately saw the bug here since Jim used
\parskip6= 0.
3543 \lsthk@PreInit \lsthk@Init
3544 \lst@ifdisplaystyle
3545 \global\let\lst@ltxlabel\@empty
3546 \if@inlabel
3547 \lst@ifresetmargins
3548 \leavevmode
3549 \else
3550 \xdef\lst@ltxlabel{\the\everypar}%
3551 \lst@AddTo\lst@ltxlabel{%
3552 \global\let\lst@ltxlabel\@empty
3553 \everypar{\lsthk@EveryLine\lsthk@EveryPar}}%
3554 \fi
3555 \fi
3556 \everypar\expandafter{\lst@ltxlabel
3557 \lsthk@EveryLine\lsthk@EveryPar}%
3558 \else
3559 \everypar{}\let\lst@NewLine\@empty
3560 \fi
3561 \lsthk@InitVars \lsthk@InitVarsBOL
The end of line character chr(13)=^^M controls the processing, see the defini- tion of \lst@MProcessListing below. The argument #1 is either \relax or
\lstenv@backslash.
3562 \lst@Let{13}\lst@MProcessListing
3563 \let\lst@Backslash#1%
3564 \lst@EnterMode{\lst@Pmode}{\lst@SelectCharTable}%
3565 \lst@InitFinalize}
Note: From version 0.19 on ‘listing processing’ is implemented as an internal mode, namely a mode with special character table. Since a bug report from Fermin Reig
\rightskip and the others are reset viaPreInitand not via InitVars.
3566\let\lst@InitFinalize\@empty % init
3567\lst@AddToHook{PreInit}
3568 {\rightskip\z@ \leftskip\z@ \parfillskip=\z@ plus 1fil
3569 \let\par\@@par}
3570\lst@AddToHook{EveryLine}{}% init
3571\lst@AddToHook{EveryPar}{}% init
showlines lets the user control whether empty lines at the end of a listing are printed. But you know that if you’ve read the User’s guide.
3572\lst@Key{showlines}f[t]{\lstKV@SetIf{#1}\lst@ifshowlines}
\lst@DeInit Output the remaining characters and update all things. First I missed to to use \lst@ifdisplaystyle here, but then KP Gores reported a problem. The
\everyparhas been put behind\lsthk@ExitVarsafter a bug report by Michael Niedermair and I’ve added\normalbaselinesafter a bug report by Georg Rehm and\normalcolorafter a report by Walter E. Brown.
3573\def\lst@DeInit{%
3574 \lst@XPrintToken \lst@EOLUpdate
3575 \global\advance\lst@newlines\m@ne
3576 \lst@ifshowlines
3577 \lst@DoNewLines
3578 \else
3579 \setbox\@tempboxa\vbox{\lst@DoNewLines}%
3580 \fi
3581 \lst@ifdisplaystyle \par\removelastskip \fi
3582 \lsthk@ExitVars\everypar{}\lsthk@DeInit\normalbaselines\normalcolor Place the bottom caption.
3583 \lst@MakeCaption b%
Close the boxes if necessary and make a rule to get the right width. I added the \par\nointerlineskip (and removed \nointerlineskip later again) after receiving a bug report from Aidan Philip Heerdegen. \everypar{} is due to a bug report from Sonja Weidmann.
3584 \lst@ifbox
3585 \egroup $\hss \egroup
3586 \vrule\@width\lst@maxwidth\@height\z@\@depth\z@
3587 \else
3588 \lst@ifdisplaystyle
3589 \par\penalty-50\vspace\lst@belowskip
3590 \fi
3591 \fi
End the multicolumn environment and/or float if necessary.
3592 \ifx\lst@multicols\@empty\else
3593 \def\lst@next{\global\let\@checkend\@gobble
3594 \endmulticols
3595 \global\let\@checkend\lst@@checkend}
3596 \expandafter\lst@next
3597 \fi
3598 \ifx\lst@float\relax\else
3599 \expandafter\lst@endfloat
3600 \fi
3601 \endgroup}
3602\let\lst@@checkend\@checkend
\lst@maxwidth is to be allocated, initialized and updated.
3603\newdimen\lst@maxwidth % \global
3604\lst@AddToHook{InitVars}{\global\lst@maxwidth\z@}
3605\lst@AddToHook{InitVarsEOL}
3606 {\ifdim\lst@currlwidth>\lst@maxwidth
3607 \global\lst@maxwidth\lst@currlwidth
3608 \fi}
\lst@EOLUpdate What do you think this macro does?
3609\def\lst@EOLUpdate{\lsthk@EOL \lsthk@InitVarsEOL}
\lst@MProcessListing This is what we have to do at EOL while processing a listing. We output all remaining characters and update the variables. If we’ve reached the last line, we check whether there is a next line interval to input or not.
3610\def\lst@MProcessListing{%
3611 \lst@XPrintToken \lst@EOLUpdate \lsthk@InitVarsBOL
3612 \global\advance\lst@lineno\@ne
3613 \ifnum \lst@lineno>\lst@lastline
3614 \lst@ifdropinput \lst@LeaveMode \fi
3615 \ifx\lst@linerange\@empty
3616 \expandafter\expandafter\expandafter\lst@EndProcessListing
3617 \else
3618 \lst@interrange
3619 \lst@GetLineInterval
3620 \expandafter\expandafter\expandafter\lst@SkipToFirst
3621 \fi
3622 \else
3623 \expandafter\lst@BOLGobble
3624 \fi}
\lst@EndProcessListing Default definition is\endinput. This works for \lstinputlisting.
3625\let\lst@EndProcessListing\endinput
gobble The key sets the number of characters to gobble each line.
3626\lst@Key{gobble}{0}{\def\lst@gobble{#1}}
\lst@BOLGobble If the number is positive, we set a temporary counter and start a loop.
3627\def\lst@BOLGobble{%
3628 \ifnum\lst@gobble>\z@
3629 \@tempcnta\lst@gobble\relax
3630 \expandafter\lst@BOLGobble@
3631\fi}
A nonpositive number terminates the loop (by not continuing). Note: This is not the macro just used in\lst@BOLGobble.
3632\def\lst@BOLGobble@@{%
3633 \ifnum\@tempcnta>\z@
3634 \expandafter\lst@BOLGobble@
3635 \fi}
If we gobble a backslash, we have to look whether this backslash ends an environ- ment. Whether the coming characters equal e.g.end{lstlisting}, we either end the environment or insert all just eaten characters after the ‘continue loop’ macro.
3636\def\lstenv@BOLGobble@@{%
3637 \lst@IfNextChars\lstenv@endstring{\lstenv@End}%
3638 {\advance\@tempcnta\m@ne \expandafter\lst@BOLGobble@@\lst@eaten}}
Now comes the loop: if we read\relax, EOL or FF, the next operation is exactly the same token. Note that for FF (and tabs below) we test against a macro which contains\lst@ProcessFormFeed. This was a bug analyzed by Heiko Oberdiek.
3639\def\lst@BOLGobble@#1{%
3640 \let\lst@next#1%
3641 \ifx \lst@next\relax\else
3642 \ifx \lst@next\lst@MProcessListing\else
3643 \ifx \lst@next\lst@processformfeed\else Otherwise we use one of the two submacros.
3644 \ifx \lst@next\lstenv@backslash
3645 \let\lst@next\lstenv@BOLGobble@@
3646 \else
3647 \let\lst@next\lst@BOLGobble@@
Now we really gobble characters. A tabulator decreases the temporary counter by
\lst@tabsize(and deals with remaining amounts, if necessary), . . .
3648 \ifx #1\lst@processtabulator
3649 \advance\@tempcnta-\lst@tabsize\relax
3650 \ifnum\@tempcnta<\z@
3651 \lst@length-\@tempcnta \lst@PreGotoTabStop
3652 \fi
. . . whereas any other character decreases the counter by one.
3653 \else
3654 \advance\@tempcnta\m@ne
3655 \fi
3656 \fi \fi \fi \fi
3657 \lst@next}
3658\def\lst@processformfeed{\lst@ProcessFormFeed}
3659\def\lst@processtabulator{\lst@ProcessTabulator}