diff options
author | Karl Berry <karl@freefriends.org> | 2016-07-14 21:40:44 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2016-07-14 21:40:44 +0000 |
commit | d3fb5eda09daee697bb9d6f9a0779c9d385ced62 (patch) | |
tree | ef665c5e31b28d47e220c5117453cb37ec536cc8 | |
parent | f6512b3997a8ac7dd05b2bd15a0b70be17e8cab6 (diff) |
fvextra (14jul16)
git-svn-id: svn://tug.org/texlive/trunk@41697 c570f23f-e606-0410-a88d-b1316a301751
-rw-r--r-- | Master/texmf-dist/doc/latex/fvextra/fvextra.pdf | bin | 638123 -> 683423 bytes | |||
-rw-r--r-- | Master/texmf-dist/source/latex/fvextra/fvextra.dtx | 1114 | ||||
-rw-r--r-- | Master/texmf-dist/tex/latex/fvextra/fvextra.sty | 575 |
3 files changed, 1559 insertions, 130 deletions
diff --git a/Master/texmf-dist/doc/latex/fvextra/fvextra.pdf b/Master/texmf-dist/doc/latex/fvextra/fvextra.pdf Binary files differindex b6cbc272d59..23ca2af590a 100644 --- a/Master/texmf-dist/doc/latex/fvextra/fvextra.pdf +++ b/Master/texmf-dist/doc/latex/fvextra/fvextra.pdf diff --git a/Master/texmf-dist/source/latex/fvextra/fvextra.dtx b/Master/texmf-dist/source/latex/fvextra/fvextra.dtx index 759cba6c14a..97ab6ba06bd 100644 --- a/Master/texmf-dist/source/latex/fvextra/fvextra.dtx +++ b/Master/texmf-dist/source/latex/fvextra/fvextra.dtx @@ -26,7 +26,7 @@ %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01] %<package>\ProvidesPackage{fvextra} %<*package> - [2016/06/28 v1.0 fvextra - extensions and patches for fancyvrb] + [2016/07/14 v1.1 fvextra - extensions and patches for fancyvrb] %</package> % %<*driver> @@ -60,6 +60,36 @@ } \usepackage{cleveref} +% Create a short verbatim pipe that handles quotation marks properly +\begingroup +\catcode`\|=\active +\gdef\pipe@active@verbatim{% + \begingroup + \let\do\@makeother\dospecials + \catcode`\|=\active + \catcode`\`=\active + \catcode`\'=\active + \catcode`\<=\active + \catcode`\>=\active + \catcode`\-=\active + \catcode`\,=\active + \catcode`\ =\active + \pipe@active@verbatim@i} +\gdef\pipe@active@verbatim@i#1|{% + \endgroup + \begingroup + \def\FV@SV@pipe@active@verbatim{% + \FV@Gobble + \expandafter\FV@ProcessLine\expandafter{#1}}% + %\let\FV@BeginVBox\relax + %\let\FV@EndVBox\relax + %\def\FV@BProcessLine##1{\FancyVerbFormatLine{##1}}% + \BUseVerbatim{pipe@active@verbatim}% + \endgroup} +\AtBeginDocument{\let|\pipe@active@verbatim} +\endgroup + + \newcommand{\todo}[1]{} %\newcommand{\todo}[1]{\textcolor{red}{TO~DO: \scantokens{#1}}} @@ -186,7 +216,7 @@ %</driver> % \fi % -% \CheckSum{1562} +% \CheckSum{2566} % % \CharacterTable % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z @@ -205,6 +235,16 @@ % Right brace \} Tilde \~} % % +% \begin{changelog}{v1.1}{2016/07/14} +% \item The options \texttt{rulecolor} and \texttt{fillcolor} now accept color names directly; using \texttt{\string\color\{<color\_name>\}} is no longer necessary, though it still works. +% \item Added \texttt{tabcolor} and \texttt{spacecolor} options for use with \texttt{showtabs} and \texttt{showspaces}. +% \item Added \texttt{highlightlines} option that takes a line number or range of line numbers and highlights the corresponding lines. Added \texttt{highlightcolor} option that controls hightlighting color. +% \item \texttt{obeytabs} no longer causes lines to vanish when tabs are inside macro arguments. Tabs and spaces inside a macro argument but otherwise at the beginning of a line are expanded correctly. Tabs inside a macro argument that are preceded by non-whitespace characters (not spaces or tabs) are expanded based on the starting position of the run of whitespace in which they occur. +% \item The line breaking options \texttt{breakanywhere}, \texttt{breakbefore}, and \texttt{breakafter} now work with multi-byte UTF-8 code points under pdfTeX with \texttt{inputenc}. They were already fully functional under XeTeX and LuaTeX. +% \item Added \texttt{curlyquotes} option, which essentially disables the \texttt{uquote} package. +% \end{changelog} +% +% % \begin{changelog}{v1.0}{2016/06/28} % \item Initial release. % \end{changelog} @@ -246,7 +286,7 @@ % % \fvextra\ is an add-on package that gives \fancyvrb\ several additional features, including automatic line breaking. Because \fvextra\ patches and overwrites some of the \fancyvrb\ internals, it may not be suitable for documents that rely on the details of the original \fancyvrb\ implementation. \fvextra\ tries to maintain the default \fancyvrb\ behavior in most cases. All patches (\cref{sec:patch}) and modifications to \fancyvrb\ defaults (\cref{sec:modifications}) are documented. % -% Some features of \fvextra\ were originally created as part of the \pkg{pythontex} and \pkg{minted} packages. \fancyvrb-related patches and extensions that currently exist in those packages will gradually be migrated into \fvextra, and both packages will eventually require \fvextra. +% Some features of \fvextra\ were originally created as part of the \pkg{pythontex} and \pkg{minted} packages. \fancyvrb-related patches and extensions that currently exist in those packages will gradually be migrated into \fvextra, and both packages will require \fvextra\ in the future. % % % @@ -255,12 +295,10 @@ % % \fvextra\ may be used as a drop-in replacement for \fancyvrb. It will load \fancyvrb\ if it has not yet been loaded, and then proceeds to patch \fancyvrb\ and define additional features. % -% The \pkg{upquote} package is loaded to give correct backticks (\texttt{\textasciigrave}) and single quotation marks (\texttt{\textquotesingle}). \fvextra\ modifies the behavior of these and other symbols in typeset math within verbatim, so that they will behave as expected (\cref{sec:patch:math}). \fvextra\ uses the \pkg{lineno} package for working with automatic line breaks. \pkg{lineno} gives a warning when the \pkg{csquotes} package is loaded before it, so \fvextra\ should be loaded before \pkg{csquotes}. The \pkg{ifthen} and \pkg{etoolbox} packages are required. \pkg{color} or \pkg{xcolor} should be loaded manually to use color-dependent features. +% The \pkg{upquote} package is loaded to give correct backticks (\texttt{\textasciigrave}) and typewriter single quotation marks (\texttt{\textquotesingle}). When this is not desirable within a given environment, use the option |curlyquotes|. \fvextra\ modifies the behavior of these and other symbols in typeset math within verbatim, so that they will behave as expected (\cref{sec:patch:math}). \fvextra\ uses the \pkg{lineno} package for working with automatic line breaks. \pkg{lineno} gives a warning when the \pkg{csquotes} package is loaded before it, so \fvextra\ should be loaded before \pkg{csquotes}. The \pkg{ifthen} and \pkg{etoolbox} packages are required. \pkg{color} or \pkg{xcolor} should be loaded manually to use color-dependent features. % % While \fvextra\ attempts to minimize changes to the \fancyvrb\ internals, in some cases it completely overwrites \fancyvrb\ macros with new definitions. New definitions typically follow the original definitions as much as possible, but code that depends on the details of the original \fancyvrb\ implementation may be incompatible with \fvextra. % -% \fvextra\ must be loaded before \pkg{pythontex} and \pkg{minted}, so that it will not clash with the \fancyvrb\ patches that currently exist in those packages. Those patches will eventually be migrated to \fvextra. -% % % % @@ -271,6 +309,59 @@ % % \begin{optionlist} % +% \item[curlyquotes (boolean) (false)] +% Unlike \fancyvrb, \pkg{fvextra} requires the \pkg{upquote} package, so the backtick (\texttt{\textasciigrave}) and typewriter single quotation mark (\texttt{\textquotesingle}) always appear literally by default, instead of becoming the left and right curly single quotation marks (\texttt{`'}). This option allows these characters to be replaced by the curly quotation marks when that is desirable. +% +% \begin{example} +% \begin{Verbatim} +% `quoted text' +% \end{Verbatim} +% \end{example} +% +% \begin{example} +% \begin{Verbatim}[curlyquotes] +% `quoted text' +% \end{Verbatim} +% \end{example} +% +% +% \item[highlightcolor (string) (LightCyan)] +% Set the color used for |highlightlines|, using a predefined color name from \pkg{color} or \pkg{xcolor}, or a color defined via |\definecolor|. +% +% +% \item[highlightlines (string) (\meta{none})] +% This highlights a single line or a range of lines based on line numbers. The line numbers refer to the line numbers that \fancyvrb\ would show if |numbers=left|, etc. They do not refer to original or actual line numbers before adjustment by |firstnumber|. +% +% The highlighting color can be customized with |highlightcolor|. +% +% \begingroup +% \fvset{xleftmargin=2em} +% \begin{longexample} +% \begin{Verbatim}[numbers=left, highlightlines={1, 3-4}] +% First line +% Second line +% Third line +% Fourth line +% Fifth line +% \end{Verbatim} +% \end{longexample} +% \endgroup +% +% The actual highlighting is performed by a set of commands. These may be customized for additional fine-tuning of highlighting. See the default definition of |\FancyVerbHighlightLineFirst| as a starting point. +% +% \vspace{0.1in} +% $\bullet$ |\FancyVerbHighlightLineFirst|: First line in a range. \\ +% \indent $\bullet$ |\FancyVerbHighlightLineMiddle|: Inner lines in a range. \\ +% \indent $\bullet$ |\FancyVerbHighlightLineLast|: Last line in a range. \\ +% \indent $\bullet$ |\FancyVerbHighlightLineSingle|: Single highlighted lines. \\ +% \indent $\bullet$ |\FancyVerbHighlightLineNormal|: Normal lines without highlighting. \\ +% +% \vspace{-0.1in} +% \noindent If these are customized in such a way that indentation or inter-line spacing is changed, then |\FancyVerbHighlightLineNormal| may be modified as well to make all lines uniform. When working with the |First|, |Last|, and |Single| commands, keep in mind that \fvextra\ merges all numbers ranges, so that |{1, 2-3, 3-5}| is treated the same as |{1-5}|. +% +% Highlighting is applied after |\FancyVerbFormatText|, so any text formatting defined via that command will work with highlighting. Highlighting is applied before |\FancyVerbFormatLine|, so if |\FancyVerbFormatLine| puts a line in a box, the box will be behind whatever is created by highlighting. This prevents highlighting from vanishing due to user-defined customization. +% +% % \item[linenos (boolean) (false)] % \fancyvrb\ allows line numbers via the options |numbers=|\meta{position}. This is essentially an alias for |numbers=left|. It primarily exists for better compatibility with the \pkg{minted} package. % @@ -300,7 +391,7 @@ % \endgroup % % -% \item[numbers (none\|left\|right\|both) (none)] +% \item[numbers (none \| left \| right \| both) (none)] % \fvextra\ adds the |both| option for line numbering. % % \begingroup @@ -317,7 +408,18 @@ % % % \item[space (macro) (\string\textvisiblespace, \textvisiblespace)] -% Redefine the visible space character. Note that this is only used if |showspaces=true|. +% Redefine the visible space character. Note that this is only used if |showspaces=true|. The color of the character may be set with |spacecolor|. +% +% +% \item[spacecolor (string) (none)] +% Set the color of visible spaces. By default (|none|), they take the color of their surroundings. +% +% \begin{longexample} +% \color{gray} +% \begin{Verbatim}[showspaces, spacecolor=red] +% One two three +% \end{Verbatim} +% \end{longexample} % % % \item[stepnumberfromfirst (boolean) (false)] @@ -357,27 +459,33 @@ % % % \item[tab (macro) ({\rmfamily\fancyvrb's} \string\FancyVerbTab, \FancyVerbTab)] -% Redefine the visible tab character. Note that this is only used if |showtabs=true|. +% Redefine the visible tab character. Note that this is only used if |showtabs=true|. The color of the character may be set with |tabcolor|. +% +% When redefining the tab, you should include the font family, font shape, and text color in the definition. Otherwise these may be inherited from the surrounding text. This is particularly important when using the tab with syntax highlighting, such as with the \pkg{minted} or \pkg{pythontex} packages. % % \fvextra\ patches \fancyvrb\ tab expansion so that variable-width symbols such as |\rightarrowfill| may be used as tabs. For example, % % \begingroup -% \fvset{frame=single, rulecolor=\color{DarkGreen}, gobble=4} +% \fvset{frame=single, rulecolor=DarkGreen, gobble=4} % \begin{VerbatimVerbatim}[breaklines, obeytabs, showtabs] % \begin{Verbatim}[obeytabs, showtabs, breaklines, -% tab=\textcolor{orange}{\rightarrowfill}] +% tab=\rightarrowfill, tabcolor=orange] % First Second Third And more text that goes on for a while until wrapping is needed % First Second Third Forth % \end{Verbatim} % \end{VerbatimVerbatim} -% \begin{Verbatim}[obeytabs, showtabs, breaklines, -% tab=\textcolor{orange}{\rightarrowfill}] +% \begin{Verbatim}[obeytabs, showtabs, breaklines, +% tab=\rightarrowfill, tabcolor=orange] % First Second Third And more text that goes on for a while until wrapping is needed % First Second Third Forth % \end{Verbatim} % \endgroup % % +% \item[tabcolor (string) (none)] +% Set the color of visible tabs. By default (|none|), they take the color of their surroundings. +% +% % \end{optionlist} % % @@ -400,8 +508,7 @@ % \begin{longexample} % \renewcommand{\FancyVerbFormatLine}[1]{% % \fcolorbox{DarkBlue}{LightGray}{#1}} -% \renewcommand{\FancyVerbFormatText}[1]{% -% \textcolor{DarkViolet}{#1}} +% \renewcommand{\FancyVerbFormatText}[1]{\textcolor{Green}{#1}} % % \begin{Verbatim}[breaklines] % Some text that proceeds for a while and finally wraps onto another line @@ -425,7 +532,7 @@ % \begin{optionlist} % % \item[breakafter (string) (\meta{none})] -% Break lines after specified characters, not just at spaces, when |breaklines=true|. For example, |breakafter=-/| would allow breaks after any hyphens or slashes. Special characters given to |breakafter| should be backslash-escaped (usually |#|, |{|, |}|, |%|, |[|, |]|; the backslash |\| may be obtained via |\\| and the space via |\space|).\footnote{|breakafter| expands each token it is given once, so when it is given a macro like |\%|, the macro should expand to a literal character that will appear in the text to be typeset. \fvextra\ defines special character escapes that are activated for |breakafter| so that this will work with common escapes. |breakafter| is not catcode-sensitive.} +% Break lines after specified characters, not just at spaces, when |breaklines=true|. For example, |breakafter=-/| would allow breaks after any hyphens or slashes. Special characters given to |breakafter| should be backslash-escaped (usually |#|, |{|, |}|, |%|, |[|, |]|; the backslash |\| may be obtained via |\\| and the space via |\space|).\footnote{|breakafter| expands each token it is given once, so when it is given a macro like |\%|, the macro should expand to a literal character that will appear in the text to be typeset. \fvextra\ defines special character escapes that are activated for |breakafter| so that this will work with common escapes. The only exception to token expansion is non-ASCII characters under pdfTeX; these should appear literally. |breakafter| is not catcode-sensitive.} % % For an alternative, see |breakbefore|. When |breakbefore| and |breakafter| are used for the same character, |breakbeforegroup| and |breakaftergroup| must both have the same setting. % @@ -475,7 +582,7 @@ % % % \item[breakbefore (string) (\meta{none})] -% Break lines before specified characters, not just at spaces, when |breaklines=true|. For example, |breakbefore=A| would allow breaks before capital A's. Special characters given to |breakbefore| should be backslash-escaped (usually |#|, |{|, |}|, |%|, |[|, |]|; the backslash |\| may be obtained via |\\| and the space via |\space|).\footnote{|breakbefore| expands each token it is given once, so when it is given a macro like |\%|, the macro should expand to a literal character that will appear in the text to be typeset. \fvextra\ defines special character escapes that are activated for |breakbefore| so that this will work with common escapes. |breakbefore| is not catcode-sensitive.} +% Break lines before specified characters, not just at spaces, when |breaklines=true|. For example, |breakbefore=A| would allow breaks before capital A's. Special characters given to |breakbefore| should be backslash-escaped (usually |#|, |{|, |}|, |%|, |[|, |]|; the backslash |\| may be obtained via |\\| and the space via |\space|).\footnote{|breakbefore| expands each token it is given once, so when it is given a macro like |\%|, the macro should expand to a literal character that will appear in the text to be typeset. \fvextra\ defines special character escapes that are activated for |breakbefore| so that this will work with common escapes. The only exception to token expansion is non-ASCII characters under pdfTeX; these should appear literally. |breakbefore| is not catcode-sensitive.} % % For an alternative, see |breakafter|. When |breakbefore| and |breakafter| are used for the same character, |breakbeforegroup| and |breakaftergroup| must both have the same setting. % @@ -736,12 +843,50 @@ % \subsection{Visible spaces} % \label{sec:patch:visible-space} % -% The command |\FancyVerbSpace| defines the visible space when |showspaces=true|. The default \fancyvrb\ definition allows a font command to escape, so that all following text is forced to be teletype font. The command is redefined to use |\textvisiblespace|. +% The command |\FancyVerbSpace| defines the visible space when |showspaces=true|. The default \fancyvrb\ definition allows a font command to escape under some circumstances, so that all following text is forced to be teletype font. The command is redefined to use |\textvisiblespace|. % % -% \subsection{Visible tabs} +% \subsection{\texttt{obeytabs} with visible tabs and with tabs inside macro arguments} % -% The default treatment of visible tabs when |showtabs=true| does not allow variable-width tab symbols such as |\rightarrowfill| to function correctly. This is fixed through a redefinition of |\FV@TrueTab|. +% The original \fancyvrb\ treatment of visible tabs when |showtabs=true| and |obeytabs=true| did not allow variable-width tab symbols such as |\rightarrowfill| to function correctly. This is fixed through a redefinition of |\FV@TrueTab|. +% +% Various macros associated with |obeytabs=true| are also redefined so that tabs may be expanded regardless of whether they are within a group (within |{...}| with the normal \LaTeX\ meaning due to |commandchars|, etc.). In the \fancyvrb\ implementation, using |obeytabs=true| when a tab is inside a group typically causes the entire line to vanish. \fvextra\ patches this so that the tab is expanded and will be visible if |showtabs=true|. Note, though, that the tab expansion in these cases is only guaranteed to be correct for leading whitespace that is inside a group. The start of each run of whitespace that is inside a group is treated as a tab stop, whether or not it actually is, due to limitations of the tab expansion algorithm. A more detailed discussion is provided in the implementation. +% +% The example below shows correct tab expansion of leading whitespace within a macro argument. With \fancyvrb, the line of text would simply vanish in this case. +% +% \begingroup +% \fvset{frame=single, rulecolor=DarkGreen, gobble=4} +% \begin{VerbatimVerbatim}[showtabs] +% \begin{Verbatim}[obeytabs, showtabs, showspaces, tabsize=4, +% commandchars=\\\{\}, tab=\textcolor{orange}{\rightarrowfill}] +% \textcolor{blue}{ Text after 1 space + 2 tabs} +% \end{Verbatim} +% \end{VerbatimVerbatim} +% +% \begin{Verbatim}[obeytabs, showtabs, showspaces, tabsize=4, +% commandchars=\\\{\}, tab=\textcolor{orange}{\rightarrowfill}] +% \textcolor{blue}{ Text after 1 space + 2 tabs} +% \end{Verbatim} +% \endgroup +% +% The next example shows that tab expansion inside macros in the midst of text typically does not match up with the correct tab stops, since in such circumstances the beginning of the run of whitespace must be treated as a tab stop. +% +% \begingroup +% \fvset{frame=single, rulecolor=DarkGreen, gobble=4, obeytabs} +% \begin{VerbatimVerbatim}[showtabs] +% \begin{Verbatim}[obeytabs, showtabs, commandchars=\\\{\}, +% tab=\textcolor{orange}{\rightarrowfill}] +% \textcolor{blue}{ 2 leading tabs} +% \textcolor{blue}{Text then 2 tabs} +% \end{Verbatim} +% \end{VerbatimVerbatim} +% +% \begin{Verbatim}[obeytabs, showtabs, commandchars=\\\{\}, +% tab=\textcolor{orange}{\rightarrowfill}] +% \textcolor{blue}{ 2 leading tabs} +% \textcolor{blue}{Text then 2 tabs} +% \end{Verbatim} +% \endgroup % % % \subsection{Math mode} @@ -794,6 +939,12 @@ % % % +% \subsection{\texttt{rulecolor} and \texttt{fillcolor}} +% +% The |rulecolor| and |fillcolor| options are redefined so that they accept color names directly, rather than requiring |\color{|\meta{color\_name}|}|. The definitions still allow the old usage. +% +% +% % % % \section{Additional modifications to \fancyvrb} @@ -801,6 +952,12 @@ % % \fvextra\ modifies some \fancyvrb\ behavior with the intention of improving logical consistency or providing better defaults. % +% +% \subsection{Backtick and single quotation mark} +% +% With \fancyvrb, the backtick \texttt{\textasciigrave} and typewriter single quotation mark \texttt{\textquotesingle} are typeset as the left and right curly single quotation marks \texttt{`'}. \pkg{fvextra} loads the \pkg{upquote} package so that these characters will appear literally by default. The original \fancyvrb\ behavior can be restored with the \fvextra\ option |curlyquotes| (\cref{sec:general-options}). +% +% % \subsection{Line numbering} % \label{sec:modifications:line-numbering} % @@ -834,7 +991,7 @@ % % \DescribeMacro{\FancyVerbTab} % -% This defines the visible tab character (\FancyVerbTab) that is used when |showtabs=true|. The default definition in \fancyvrb\ is +% This defines the visible tab character (\FancyVerbTab) that is used when |showtabs=true|. The default definition is %\begin{verbatim} %\def\FancyVerbTab{% % \valign{% @@ -881,19 +1038,31 @@ \@ifpackageloaded{csquotes}% {\PackageWarning{fvextra}{csquotes should be loaded after fvextra, % to avoid a warning from the lineno package}}{} -\@ifpackageloaded{minted}% - {\PackageError{fvextra}% - {fvextra must be loaded before minted}% - {fvextra must be loaded before minted}} -\@ifpackageloaded{pythontex}% - {\PackageError{fvextra}% - {fvextra must be loaded before pythontex}% - {fvextra must be loaded before pythontex}} % \end{macrocode} % % % % +% \subsection{Utility macros} +% +% \begin{macro}{\FV@Space@ifx} +% Macro for testing if a |\let| token is |\FV@Space| with |\ifx|. The space will be active and defined as |\FV@Space|. +% \begin{macrocode} +\def\FV@Space@ifx{\FV@Space} +% \end{macrocode} +% \end{macro} +% +% +% \begin{macro}{\FV@Tab@ifx} +% Macro for testing if a |\let| token is |\FV@Tab| with |\ifx|. The tab will be active and defined as |\FV@Tab|. +% \begin{macrocode} +\def\FV@Tab@ifx{\FV@Tab} +% \end{macrocode} +% \end{macro} +% +% +% +% % \subsection{Hooks} % % @@ -943,23 +1112,89 @@ % \subsubsection{Visible spaces} % % \begin{macro}{\FancyVerbSpace} -% The default definition of visible spaces (|showspaces=true|) allows font commands to escape: +% The default definition of visible spaces (|showspaces=true|) could allow font commands to escape under some circumstances, depending on how it is used: %\begin{verbatim} %{\catcode`\ =12 \gdef\FancyVerbSpace{\tt }} %\end{verbatim} -% The command is redefined in more standard \LaTeX\ form. +% The command is redefined in more robust and standard \LaTeX\ form. % \begin{macrocode} \def\FancyVerbSpace{\textvisiblespace} % \end{macrocode} % \end{macro} % % -% \subsubsection{Visible tabs} +% \subsubsection{\texttt{obeytabs} with visible tabs and with tabs inside macro arguments} +% \label{sec:patch:obeytabs} +% +% |\FV@TrueTab| governs tab appearance when |obeytabs=true| and |showtabs=true|. It is redefined so that symbols with flexible width, such as |\rightarrowfill|, will work as expected. In the original \fancyvrb\ definition, |\kern\@tempdima\hbox to\z@{...}|. The |\kern| is removed and instead the |\hbox| is given the width |\@tempdima|. +% +% |\FV@TrueTab| and related macros are also modified so that they function for tabs inside macro arguments when |obeytabs=true| (inside curly braces |{}| with their normal meaning, when using |commandchars|, etc.). The \fancyvrb\ implementation of tab expansion assumes that tabs are never inside a group; when a group that contains a tab is present, the entire line typically vanishes. The new implementation keeps the \fancyvrb\ behavior exactly for tabs outside groups; they are perfectly expanded to tab stops. Tabs inside groups cannot be perfectly expanded to tab stops, at least not using the \fancyvrb\ approach. Instead, when \fvextra\ encounters a run of whitespace characters (tabs and possibly spaces), it makes the assumption that the nearest tab stop was at the beginning of the run. This gives the correct behavior if the whitespace characters are leading indentation that happens to be within a macro. Otherwise, it will typically not give correct tab expansion---but at least the entire line will not be discarded, and the run of whitespace will be represented, even if imperfectly. +% +% A general solution to tab expansion may be possible, but will almost certainly require multiple compiles, perhaps even one compile (or more) per tab. The \pkg{zref} package provides a |\zsaveposx| macro that stores the current $x$ position on the page for subsequent compiles. This macro, or a similar macro from another package, could be used to establish a reference point at the beginning of each line. Then each run of whitespace that contains a tab could have a reference point established at its start, and tabs could be expanded based on the distance between the start of the run and the start of the line. Such an approach would allow the first run of whitespace to measure its distance from the start of the line on the 2nd compile (once both reference points were established), so it would be able expand the first run of whitespace correctly on the 3rd compile. That would allow a second run of whitespace to definitely establish its starting point on the 3rd compile, which would allow it to expand correctly on the 4th compile. And so on. Thus, while it should be possible to perform completely correct tab expansion with such an approach, it will in general require at least 4 compiles to do better than the current approach. Furthermore, the sketch of the algorithm provided so far does not include any complications introduced by line breaking. In the current approach, it is necessary to determine how each tab would be expanded in the absence of line breaking, save all tab widths, and then expand using saved widths during the actual typesetting with line breaking. +% +% +% \begin{macro}{FV@TrueTabGroupLevel} +% Counter for keeping track of the group level (|\currentgrouplevel|) at the very beginning of a line, inside |\FancyVerbFormatLine| but outside |\FancyVerbFormatText|, which is where the tab expansion macro is invoked. This allows us to determine whether we are in a group, and expand tabs accordingly. +% \begin{macrocode} +\newcounter{FV@TrueTabGroupLevel} +% \end{macrocode} +% \end{macro} +% +% +% \begin{macro}{\FV@@ObeyTabs} +% The \fancyvrb\ macro responsible for tab expansion is modified so that it can handle tabs inside groups, even if imperfectly. We need to use a special version of the space, |\FV@Space@ObeyTabs|, that within a group will capture all following spaces or tabs and then insert them with tab expansion based on the beginning of the run of whitespace. We need to record the current group level, but then increment it by $1$ because all comparisons will be performed within the |\hbox{...}|. +% \begin{macrocode} +\def\FV@@ObeyTabs#1{% + \let\FV@Space@Orig\FV@Space + \let\FV@Space\FV@Space@ObeyTabs + \setcounter{FV@TrueTabGroupLevel}{\the\currentgrouplevel}% + \addtocounter{FV@TrueTabGroupLevel}{1}% + \setbox\FV@TabBox=\hbox{#1}\box\FV@TabBox + \let\FV@Space\FV@Space@Orig} +% \end{macrocode} +% \end{macro} +% % % \begin{macro}{\FV@TrueTab} -% Redefine |\FV@TrueTab| so that symbols with flexible width, such as |\rightarrowfill|, will work as expected. In the original \fancyvrb\ definition, |\kern\@tempdima\hbox to\z@{...}|. The |\kern| is removed and instead the |\hbox| is given the width |\@tempdima|. +% Version that follows \fancyvrb\ if not in a group and takes another approach otherwise. % \begin{macrocode} \def\FV@TrueTab{% + \ifnum\value{FV@TrueTabGroupLevel}=\the\currentgrouplevel\relax + \expandafter\FV@TrueTab@NoGroup + \else + \expandafter\FV@TrueTab@Group + \fi} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FV@TrueTabSaveWidth} +% When linebreaking is in use, the \fancyvrb\ tab expansion algorithm cannot be used directly, since it involves |\hbox|, which doesn't allow for line breaks. In those cases, tab widths will be calculated for the case without breaks and saved, and then saved widths will be used in the actual typesetting. This macro is |\let| to width-saving code in those cases. +% \begin{macrocode} +\let\FV@TrueTabSaveWidth\relax +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{FV@TrueTabCounter} +% Counter for tracking saved tabs. +% \begin{macrocode} +\newcounter{FV@TrueTabCounter} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FV@TrueTabSaveWidth@Save} +% Save the current tab width, then increment the tab counter. |\@tempdima| will hold the current tab width. +% \begin{macrocode} +\def\FV@TrueTabSaveWidth@Save{% + \expandafter\xdef\csname FV@TrueTab:Width\arabic{FV@TrueTabCounter}\endcsname{% + \number\@tempdima}% + \stepcounter{FV@TrueTabCounter}} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FV@TrueTab@NoGroup} +% This follows the \fancyvrb\ approach exactly, except for the |\hbox to\@tempdima| adjustment and the addition of |\FV@TrueTabSaveWidth|. +% \begin{macrocode} +\def\FV@TrueTab@NoGroup{% \egroup \@tempdima=\FV@ObeyTabSize sp\relax \@tempcnta=\wd\FV@TabBox @@ -967,11 +1202,116 @@ \divide\@tempcnta\@tempdima \multiply\@tempdima\@tempcnta \advance\@tempdima-\wd\FV@TabBox + \FV@TrueTabSaveWidth \setbox\FV@TabBox=\hbox\bgroup \unhbox\FV@TabBox\hbox to\@tempdima{\hss\FV@TabChar}} % \end{macrocode} % \end{macro} % +% \begin{macro}{FV@ObeyTabs@Whitespace@Tab} +% In a group where runs of whitespace characters are collected, we need to keep track of whether a tab has been found, so we can avoid expansion and the associated |\hbox| for spaces without tabs. +% \begin{macrocode} +\newboolean{FV@ObeyTabs@Whitespace@Tab} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FV@TrueTab@Group} +% If in a group, a tab should start collecting whitespace characters for later tab expansion, beginning with itself. The collected whitespace will use |\FV@Tab@ifx| and |\FV@Space@ifx| so that any |\ifx| comparisons performed later will behave as expected. This shouldn't be strictly necessary, because |\FancyVerbBreakStart| operates with saved tab widths rather than using the tab expansion code directly. But it is safer in case any other unanticipated scanning is going on. +% \begin{macrocode} +\def\FV@TrueTab@Group{% + \booltrue{FV@ObeyTabs@Whitespace@Tab}% + \gdef\FV@TmpWhitespace{\FV@Tab@ifx}% + \FV@ObeyTabs@ScanWhitespace} +% \end{macrocode} +% \end{macro} +% +% +% \begin{macro}{\FV@Space@ObeyTabs} +% Space treatment, like tab treatment, now depends on whether we are in a group, because in a group we want to collect all runs of whitespace and then expand any tabs. +% \begin{macrocode} +\def\FV@Space@ObeyTabs{% + \ifnum\value{FV@TrueTabGroupLevel}=\the\currentgrouplevel\relax + \expandafter\FV@Space@ObeyTabs@NoGroup + \else + \expandafter\FV@Space@ObeyTabs@Group + \fi} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FV@Space@ObeyTabs@NoGroup} +% Fall back to normal space. +% \begin{macrocode} +\def\FV@Space@ObeyTabs@NoGroup{\FV@Space@Orig} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FV@Space@ObeyTabs@Group} +% Make a note that no tabs have yet been encountered, store the current space, then scan for following whitespace. +% \begin{macrocode} +\def\FV@Space@ObeyTabs@Group{% + \boolfalse{FV@ObeyTabs@Whitespace@Tab}% + \gdef\FV@TmpWhitespace{\FV@Space@ifx}% + \FV@ObeyTabs@ScanWhitespace} +% \end{macrocode} +% \end{macro} +% +% +% \begin{macro}{\FV@ObeyTabs@ScanWhitespace} +% Collect whitespace until the end of the run, then process it. Proper lookahead comparison requires |\FV@Space@ifx| and |\FV@Tab@ifx|. +% \begin{macrocode} +\def\FV@ObeyTabs@ScanWhitespace{% + \@ifnextchar\FV@Space@ifx% + {\FV@TrueTab@CaptureWhitespace@Space}% + {\ifx\@let@token\FV@Tab@ifx + \expandafter\FV@TrueTab@CaptureWhitespace@Tab + \else + \expandafter\FV@ObeyTabs@ResolveWhitespace + \fi}} +\def\FV@TrueTab@CaptureWhitespace@Space#1{% + \g@addto@macro\FV@TmpWhitespace{\FV@Space@ifx}% + \FV@ObeyTabs@ScanWhitespace} +\def\FV@TrueTab@CaptureWhitespace@Tab#1{% + \booltrue{FV@ObeyTabs@Whitespace@Tab}% + \g@addto@macro\FV@TmpWhitespace{\FV@Tab@ifx}% + \FV@ObeyTabs@ScanWhitespace} +% \end{macrocode} +% \end{macro} +% +% +% \begin{macro}{\FV@TrueTab@Group@Expand} +% Yet another tab definition, this one for use in the actual expansion of tabs in whitespace. This uses the \fancyvrb\ algorithm, but only over a restricted region known to contain no groups. +% \begin{macrocode} +\newbox\FV@TabBox@Group +\def\FV@TrueTab@Group@Expand{% + \egroup + \@tempdima=\FV@ObeyTabSize sp\relax + \@tempcnta=\wd\FV@TabBox@Group + \advance\@tempcnta\FV@@ObeyTabSize\relax + \divide\@tempcnta\@tempdima + \multiply\@tempdima\@tempcnta + \advance\@tempdima-\wd\FV@TabBox@Group + \FV@TrueTabSaveWidth + \setbox\FV@TabBox@Group=\hbox\bgroup + \unhbox\FV@TabBox@Group\hbox to\@tempdima{\hss\FV@TabChar}} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FV@ObeyTabs@ResolveWhitespace} +% Need to make sure the right definitions of the space and tab are in play here. Only do tab expansion, with the associated |\hbox|, if a tab is indeed present. +% \begin{macrocode} +\def\FV@ObeyTabs@ResolveWhitespace{% + \let\FV@Space\FV@Space@Orig + \let\FV@Tab\FV@TrueTab@Group@Expand + \expandafter\FV@ObeyTabs@ResolveWhitespace@i\expandafter{\FV@TmpWhitespace}% + \let\FV@Space\FV@Space@ObeyTabs + \let\FV@Tab\FV@TrueTab} +\def\FV@ObeyTabs@ResolveWhitespace@i#1{% + \ifbool{FV@ObeyTabs@Whitespace@Tab}% + {\setbox\FV@TabBox@Group=\hbox{#1}\box\FV@TabBox@Group}% + {#1}} +% \end{macrocode} +% \end{macro} +% % % \subsubsection{Spacing in math mode} % @@ -1071,6 +1411,53 @@ % % % +% \subsubsection{\texttt{rulecolor} and \texttt{fillcolor}} +% +% The |rulecolor| and |fillcolor| options are redefined so that they accept color names directly, rather than requiring |\color{|\meta{color\_name}|}|. The definitions still allow the old usage. +% +% \begin{macro}{rulecolor} +% \begin{macrocode} +\define@key{FV}{rulecolor}{% + \ifstrempty{#1}% + {\let\FancyVerbRuleColor\relax}% + {\ifstrequal{#1}{none}% + {\let\FancyVerbRuleColor\relax}% + {\def\@tempa{#1}% + \FV@KVProcess@RuleColor#1\FV@Undefined}}} +\def\FV@KVProcess@RuleColor#1#2\FV@Undefined{% + \ifx#1\color + \else + \expandafter\def\expandafter\@tempa\expandafter{% + \expandafter\color\expandafter{\@tempa}}% + \fi + \let\FancyVerbRuleColor\@tempa} +\fvset{rulecolor=none} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{fillcolor} +% \begin{macrocode} +\define@key{FV}{fillcolor}{% + \ifstrempty{#1}% + {\let\FancyVerbFillColor\relax}% + {\ifstrequal{#1}{none}% + {\let\FancyVerbFillColor\relax}% + {\def\@tempa{#1}% + \FV@KVProcess@FillColor#1\FV@Undefined}}} +\def\FV@KVProcess@FillColor#1#2\FV@Undefined{% + \ifx#1\color + \else + \expandafter\def\expandafter\@tempa\expandafter{% + \expandafter\color\expandafter{\@tempa}}% + \fi + \let\FancyVerbFillColor\@tempa} +\fvset{fillcolor=none} +% \end{macrocode} +% \end{macro} +% +% +% +% % \subsection{Extensions} % % @@ -1094,6 +1481,23 @@ % \end{macro} % % +% \begin{macro}{tabcolor} +% Set tab color, or allow it to adjust to surroundings (the default \fancyvrb\ behavior). This involves re-creating the |showtabs| option to add |\FV@TabColor|. +% \begin{macrocode} +\define@key{FV}{tabcolor}% + {\ifstrempty{#1}% + {\let\FV@TabColor\relax}% + {\ifstrequal{#1}{none}% + {\let\FV@TabColor\relax}% + {\def\FV@TabColor{\textcolor{#1}}}}} +\define@booleankey{FV}{showtabs}% + {\def\FV@TabChar{\FV@TabColor{\FancyVerbTab}}}% + {\let\FV@TabChar\relax} +\fvset{tabcolor=none, showtabs=false} +% \end{macrocode} +% \end{macro} +% +% % \begin{macro}{space} % Redefine |\FancyVerbSpace|. % \begin{macrocode} @@ -1102,25 +1506,64 @@ % \end{macro} % % +% \begin{macro}{spacecolor} +% Set space color, or allow it to adjust to surroundings (the default \fancyvrb\ behavior). This involves re-creating the |showspaces| option to add |\FV@SpaceColor|. +% \begin{macrocode} +\define@key{FV}{spacecolor}% + {\ifstrempty{#1}% + {\let\FV@SpaceColor\relax}% + {\ifstrequal{#1}{none}% + {\let\FV@SpaceColor\relax}% + {\def\FV@SpaceColor{\textcolor{#1}}}}} +\define@booleankey{FV}{showspaces}% + {\def\FV@Space{\FV@SpaceColor{\FancyVerbSpace}}}% + {\def\FV@Space{\ }} +\fvset{spacecolor=none, showspaces=false} +% \end{macrocode} +% \end{macro} +% +% % \begin{macro}{mathescape} % Give |$|, |^|, and |_| their normal catcodes to allow normal typeset math. % \begin{macrocode} \define@booleankey{FV}{mathescape}% {\let\FancyVerbMathEscape\FV@MathEscape}% {\let\FancyVerbMathEscape\relax} -\FV@AddToHook\FV@CatCodesHook\FancyVerbMathEscape \def\FV@MathEscape{\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax} +\FV@AddToHook\FV@CatCodesHook\FancyVerbMathEscape \fvset{mathescape=false} % \end{macrocode} % \end{macro} % % -% \subsubsection{\texttt{\textbackslash FancyVerbFormatLine} and \texttt{\textbackslash FancyVerbFormatText}} +% \begin{macro}{curlyquotes} +% Let \texttt{\textasciigrave} and \texttt{\textquotesingle} produce curly quotation marks \texttt{`} and \texttt{'} rather than the backtick and typewriter single quotation mark produced by default via \pkg{upquote}. +% \begin{macrocode} +\newbool{FV@CurlyQuotes} +\define@booleankey{FV}{curlyquotes}% + {\booltrue{FV@CurlyQuotes}}% + {\boolfalse{FV@CurlyQuotes}} +\def\FancyVerbCurlyQuotes{% + \ifbool{FV@CurlyQuotes}% + {\expandafter\def\expandafter\@noligs\expandafter{\@noligs + \begingroup\lccode`\~=`\`\lowercase{\endgroup\def~}{`}% + \begingroup\lccode`\~=`\'\lowercase{\endgroup\def~}{'}}}% + {}} +\g@addto@macro\FV@FormattingPrepHook{\FancyVerbCurlyQuotes} +\fvset{curlyquotes=false} +% \end{macrocode} +% \end{macro} +% +% +% +% \subsubsection{Formatting with \texttt{\textbackslash FancyVerbFormatLine}, \texttt{\textbackslash FancyVerbFormatText}, and \texttt{\textbackslash FancyVerbHighlightLine}} % % \fancyvrb\ defines |\FancyVerbFormatLine|, which defines the formatting for each line. The introduction of line breaks introduces an issue for |\FancyVerbFormatLine|. Does it format the entire line, including any whitespace in the margins or behind line break symbols (that is, is it outside the |\parbox| in which the entire line is wrapped when breaking is active)? Or does it only format the text part of the line, only affecting the actual characters (inside the |\parbox|)? Since both might be desirable, |\FancyVerbFormatLine| is assigned to the entire line, and a new macro |\FancyVerbFormatText| is assigned to the text, within the |\parbox|. % % An additional complication is that the \fancyvrb\ documentation says that the default value is |\def\FancyVerbFormatLine#1{#1}|. But the actual default is |\def\FancyVerbFormatLine#1{\FV@ObeyTabs{#1}}|. That is, |\FV@ObeyTabs| needs to operate directly on the line to handle tabs. As a result, \emph{all} \fancyvrb\ commands that involve |\FancyVerbFormatLine| are patched, so that |\def\FancyVerbFormatLine#1{#1}|. % +% An additional macro |\FancyVerbHighlightLine| is added between |\FancyVerbFormatLine| and |\FancyVerbFormatText|. This is used to highlight selected lines (\cref{sec:impl:extensions:highlighting}). It is inside |\FancyVerbHighlightLine| so that if |\FancyVerbHighlightLine| is used to provide a background color, |\FancyVerbHighlightLine| can override it. +% % \begin{macro}{\FancyVerbFormatLine} % Format the entire line, following the definition given in the \fancyvrb\ documentation. Because this is formatting the entire line, using boxes works with line breaking. % \begin{macrocode} @@ -1146,7 +1589,9 @@ \hbox to \linewidth{% \FV@LeftListNumber \FV@LeftListFrame - \FancyVerbFormatLine{\FV@ObeyTabs{\FancyVerbFormatText{#1}}}\hss + \FancyVerbFormatLine{% + \FancyVerbHighlightLine{% + \FV@ObeyTabs{\FancyVerbFormatText{#1}}}}\hss \FV@RightListFrame \FV@RightListNumber}% \hss}} @@ -1157,7 +1602,10 @@ % \begin{macro}{\FV@BProcessLine} % Redefined |\FV@BProcessLine| in which |\FancyVerbFormatText| is added and tab handling is explicit. % \begin{macrocode} -\def\FV@BProcessLine#1{\hbox{\FancyVerbFormatLine{\FV@ObeyTabs{\FancyVerbFormatText{#1}}}}} +\def\FV@BProcessLine#1{% + \hbox{\FancyVerbFormatLine{% + \FancyVerbHighlightLine{% + \FV@ObeyTabs{\FancyVerbFormatText{#1}}}}}} % \end{macrocode} % \end{macro} % @@ -1383,6 +1831,171 @@ % % % +% \subsubsection{Line highlighting or emphasis} +% \label{sec:impl:extensions:highlighting} +% +% This adds an option |highlightlines| that allows specific lines, or lines within a range, to be highlighted or otherwise emphasized. +% +% \begin{macro}{highlightlines}\begin{macro}{\FV@HighlightLinesList} +% \begin{macrocode} +\define@key{FV}{highlightlines}{\def\FV@HighlightLinesList{#1}}% +\fvset{highlightlines=} +% \end{macrocode} +% \end{macro}\end{macro} +% +% \begin{macro}{highlightcolor}\begin{macro}{\FV@HighlightColor} +% Define color for highlighting. The default is LightCyan. A good alternative for a brighter color would be LemonChiffon. +% \begin{macrocode} +\define@key{FV}{highlightcolor}{\def\FancyVerbHighlightColor{#1}}% +\let\FancyVerbHighlightColor\@empty +\ifcsname definecolor\endcsname +\ifx\definecolor\relax +\else + \definecolor{FancyVerbHighlightColor}{HTML}{E0FFFF} + \fvset{highlightcolor=FancyVerbHighlightColor} +\fi\fi +\AtBeginDocument{% + \ifx\FancyVerbHighlightColor\@empty + \ifcsname definecolor\endcsname + \ifx\definecolor\relax + \else + \definecolor{FancyVerbHighlightColor}{rgb}{0,1,1} + \fvset{highlightcolor=FancyVerbHighlightColor} + \fi\fi + \fi} +% \end{macrocode} +% \end{macro}\end{macro} +% +% +% \begin{macro}{\FancyVerbHighlightLine} +% This is the entry macro into line highlighting. By default it should do nothing. It is always invoked between |\FancyVerbFormatLine| and |\FancyVerbFormatText|, so that it can provide a background color (won't interfere with line breaking) and can override any formatting provided by |\FancyVerbFormatLine|. It is |\let| to |\FV@HighlightLine| when highlighting is active. +% \begin{macrocode} +\def\FancyVerbHighlightLine#1{#1} +% \end{macrocode} +% \end{macro} +% +% +% \begin{macro}{\FV@HighlightLine} +% This determines whether highlighting should be performed, and if so, which macro should be invoked. +% \begin{macrocode} +\def\FV@HighlightLine#1{% + \@tempcnta=\c@FancyVerbLine + \@tempcntb=\c@FancyVerbLine + \ifcsname FV@HighlightLine:\number\@tempcnta\endcsname + \advance\@tempcntb\m@ne + \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname + \advance\@tempcntb\tw@ + \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname + \let\FV@HighlightLine@Next\FancyVerbHighlightLineMiddle + \else + \let\FV@HighlightLine@Next\FancyVerbHighlightLineLast + \fi + \else + \advance\@tempcntb\tw@ + \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname + \let\FV@HighlightLine@Next\FancyVerbHighlightLineFirst + \else + \let\FV@HighlightLine@Next\FancyVerbHighlightLineSingle + \fi + \fi + \else + \let\FV@HighlightLine@Next\FancyVerbHighlightLineNormal + \fi + \FV@HighlightLine@Next{#1}% +} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FancyVerbHighlightLineNormal} +% A normal line that is not highlighted or otherwise emphasized. This could be redefined to de-emphasize the line. +% \begin{macrocode} +\def\FancyVerbHighlightLineNormal#1{#1} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FV@TmpLength} +% \begin{macrocode} +\newlength{\FV@TmpLength} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FancyVerbHighlightLineFirst} +% The first line in a multi-line range. +% +% |\fboxsep| is set to zero so as to avoid indenting the line or changing inter-line spacing. It is restored to its original value inside to prevent any undesired effects. The |\strut| is needed to get the highlighting to be the appropriate height. The |\rlap| and |\hspace| make the |\colorbox| expand to the full |\linewidth|. Note that if |\fboxsep| $\ne0$, then we would want to use |\dimexpr\linewidth-2\fboxsep| or add |\hspace{-2\fboxsep}| at the end. +% +% If this macro is customized so that the text cannot take up the full |\linewidth|, then adjustments may need to be made here or in the line breaking code to make sure that line breaking takes place at the appropriate location. +% \begin{macrocode} +\def\FancyVerbHighlightLineFirst#1{% + \setlength{\FV@TmpLength}{\fboxsep}% + \setlength{\fboxsep}{0pt}% + \colorbox{\FancyVerbHighlightColor}{% + \setlength{\fboxsep}{\FV@TmpLength}% + \rlap{\strut#1}% + \hspace{\linewidth}}} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FancyVerbHighlightLineMiddle} +% A middle line in a multi-line range. +% \begin{macrocode} +\let\FancyVerbHighlightLineMiddle\FancyVerbHighlightLineFirst +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FancyVerbHighlightLineLast} +% The last line in a multi-line range. +% \begin{macrocode} +\let\FancyVerbHighlightLineLast\FancyVerbHighlightLineFirst +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\FancyVerbHighlightLineSingle} +% A single line not in a multi-line range. +% \begin{macrocode} +\let\FancyVerbHighlightLineSingle\FancyVerbHighlightLineFirst +% \end{macrocode} +% \end{macro} +% +% +% \begin{macro}{\FV@HighlightLinesPrep} +% Process the list of lines to highlight (if any). A macro is created for each line to be highlighted. During highlighting, a line is highlighted if the corresponding macro exists. All of the macro creating is ultimately within the current environment group so it stays local. |\FancyVerbHighlightLine| is |\let| to a version that will invoke the necessary logic. +% \begin{macrocode} +\def\FV@HighlightLinesPrep{% + \ifx\FV@HighlightLinesList\@empty + \else + \let\FancyVerbHighlightLine\FV@HighlightLine + \expandafter\FV@HighlightLinesPrep@i + \fi} +\def\FV@HighlightLinesPrep@i{% + \renewcommand{\do}[1]{% + \ifstrempty{##1}{}{\FV@HighlightLinesParse##1-\FV@Undefined}}% + \expandafter\docsvlist\expandafter{\FV@HighlightLinesList}} +\def\FV@HighlightLinesParse#1-#2\FV@Undefined{% + \ifstrempty{#2}% + {\FV@HighlightLinesParse@Single{#1}}% + {\FV@HighlightLinesParse@Range{#1}#2\relax}} +\def\FV@HighlightLinesParse@Single#1{% + \expandafter\let\csname FV@HighlightLine:\detokenize{#1}\endcsname\relax} +\newcounter{FV@HighlightLinesStart} +\newcounter{FV@HighlightLinesStop} +\def\FV@HighlightLinesParse@Range#1#2-{% + \setcounter{FV@HighlightLinesStart}{#1}% + \setcounter{FV@HighlightLinesStop}{#2}% + \stepcounter{FV@HighlightLinesStop}% + \FV@HighlightLinesParse@Range@Loop} +\def\FV@HighlightLinesParse@Range@Loop{% + \ifnum\value{FV@HighlightLinesStart}<\value{FV@HighlightLinesStop}\relax + \expandafter\let\csname FV@HighlightLine:\arabic{FV@HighlightLinesStart}\endcsname\relax + \stepcounter{FV@HighlightLinesStart}% + \expandafter\FV@HighlightLinesParse@Range@Loop + \fi} +\g@addto@macro\FV@FormattingPrepHook{\FV@HighlightLinesPrep} +% \end{macrocode} +% \end{macro} +% +% +% % % \subsection{Line breaking} % @@ -1584,6 +2197,8 @@ % We need a way to break before characters if and only if they have been specified as breaking characters. It would be possible to do that via a nested conditional, but that would be messy. It is much simpler to create an empty macro whose name contains the character, and test for the existence of this macro. This needs to be done inside a |\begingroup...\endgroup| so that the macros do not have to be cleaned up manually. A good place to do this is in |\FV@FormattingPrep|, which is inside a group and before processing starts. The macro is added to |\FV@FormattingPrepHook|, which contains \fvextra\ exntensions to |\FV@FormattingPrep|, after |\FV@BreakAfterPrep| is defined below. % % The procedure here is a bit roundabout. We need to use |\FV@EscChars| to handle character escapes, but the character redefinitions need to be kept local, requiring that we work within a |\begingroup...\endgroup|. So we loop through the breaking tokens and assemble a macro that will itself define character macros. Only this defining macro is declared global, and it contains \emph{expanded} characters so that there is no longer any dependence on |\FV@EscChars|. +% +% A pdfTeX-compatible version for working with UTF-8 is defined later, and |\FV@BreakBeforePrep| is |\let| to it under pdfTeX as necessary. % \begin{macrocode} \def\FV@BreakBeforePrep{% \ifx\FV@BreakBefore\@empty\relax @@ -1640,6 +2255,8 @@ % % \begin{macro}{\FV@BreakAfterPrep} % This is the |breakafter| equivalent of |\FV@BreakBeforePrep|. It is also used within |\FV@FormattingPrep|. The order of |\FV@BreakBeforePrep| and |\FV@BreakAfterPrep| is important; |\FV@BreakAfterPrep| must always be second, because it checks for conflicts with |breakbefore|. +% +% A pdfTeX-compatible version for working with UTF-8 is defined later, and |\FV@BreakAfterPrep| is |\let| to it under pdfTeX as necessary. % \begin{macrocode} \def\FV@BreakAfterPrep{% \ifx\FV@BreakAfter\@empty\relax @@ -1666,7 +2283,6 @@ {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}% {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}}% {}}% - \else \fi \g@addto@macro\FV@BreakAfter@Def{% \@namedef{FV@BreakAfter@Token\detokenize{##1}}{}}% @@ -1680,9 +2296,22 @@ % \end{macrocode} % \end{macro} % -% Now that |\FV@BreakBeforePrep| and |\FV@BreakAfterPrep| are defined, add them to |\FV@FormattingPrepHook|, which is the \fvextra\ extension to |\FV@FormattingPrep|. The ordering here is important, since |\FV@BreakAfterPrep| contains compatibility checks with |\FV@BreakBeforePrep|, and thus must be used after it. +% Now that |\FV@BreakBeforePrep| and |\FV@BreakAfterPrep| are defined, add them to |\FV@FormattingPrepHook|, which is the \fvextra\ extension to |\FV@FormattingPrep|. The ordering here is important, since |\FV@BreakAfterPrep| contains compatibility checks with |\FV@BreakBeforePrep|, and thus must be used after it. Also, we have to check for the pdfTeX engine with \pkg{inputenc} using UTF-8, and use the |UTF| macros instead when that is the case. % \begin{macrocode} -\g@addto@macro\FV@FormattingPrepHook{\FV@BreakBeforePrep\FV@BreakAfterPrep} +\g@addto@macro\FV@FormattingPrepHook{% + \ifcsname pdfmatch\endcsname + \ifx\pdfmatch\relax + \else + \ifcsname inputencodingname\endcsname + \ifx\inputencodingname\relax + \else + \ifdefstring{\inputencodingname}{utf8}% + {\let\FV@BreakBeforePrep\FV@BreakBeforePrep@UTF + \let\FV@BreakAfterPrep\FV@BreakAfterPrep@UTF}% + {}% + \fi\fi + \fi\fi + \FV@BreakBeforePrep\FV@BreakAfterPrep} % \end{macrocode} % % @@ -1815,12 +2444,12 @@ \ifx\FV@NextChar\FV@Undefined\relax \let\FV@Next=\relax \else - \expandafter\ifx\FV@NextChar\FV@Space\relax - \g@addto@macro{\FV@LineIndentChars}{\FV@Space}% + \ifx\FV@NextChar\FV@Space@ifx\relax + \g@addto@macro{\FV@LineIndentChars}{\FV@Space@ifx}% \let\FV@Next=\FV@GetLineIndent \else - \expandafter\ifx\FV@NextChar\FV@Tab\relax - \g@addto@macro{\FV@LineIndentChars}{\FV@Tab}% + \ifx\FV@NextChar\FV@Tab@ifx\relax + \g@addto@macro{\FV@LineIndentChars}{\FV@Tab@ifx}% \let\FV@Next=\FV@GetLineIndent \else \let\FV@Next=\FV@CleanRemainingChars @@ -1835,46 +2464,18 @@ % % \paragraph{Tab expansion}\hfill\\ % -% \noindent The \fancyvrb\ option |obeytabs| uses a clever algorithm involving boxing and unboxing to expand tabs based on tab stops rather than a fixed number of equivalent space characters. (See the definitions of |\FV@@ObeyTabs| and |\FV@TrueTab|.) Unfortunately, since this involves |\hbox|, it interferes with the line breaking algorithm, and an alternative is required. +% \noindent The \fancyvrb\ option |obeytabs| uses a clever algorithm involving boxing and unboxing to expand tabs based on tab stops rather than a fixed number of equivalent space characters. (See the definitions of |\FV@@ObeyTabs| and |\FV@TrueTab| in \cref{sec:patch:obeytabs}.) Unfortunately, since this involves |\hbox|, it interferes with the line breaking algorithm, and an alternative is required. % -% There are probably many ways tab expansion could be performed while still allowing line breaks. The current approach has been chosen because it is relatively straightforward and yields identical results to the case without line breaks. Line breaking involves saving a line in a box, and determining whether the box is too wide. During this process, if |obeytabs=true|, |\FV@TrueTab| is |\let| to a version that saves the width of every tab in a macro. When a line is broken, all tabs within it will then use another variant of |\FV@TrueTab| that sequentially retrieves the saved widths. This maintains the exact behavior of the case without line breaks. -% -% Note that the variants of |\FV@TrueTab| are based on the \fvextra\ patched version of |\FV@TrueTab|, not on the original |\FV@TrueTab| defined in \fancyvrb. -% -% -% \begin{macro}{FV@TrueTabCounter} -% Counter for tabs, for creating uniquely named macros containing tab widths. -% \begin{macrocode} -\newcounter{FV@TrueTabCounter} -% \end{macrocode} -% \end{macro} +% There are probably many ways tab expansion could be performed while still allowing line breaks. The current approach has been chosen because it is relatively straightforward and yields identical results to the case without line breaks. Line breaking involves saving a line in a box, and determining whether the box is too wide. During this process, if |obeytabs=true|, |\FV@TrueTabSaveWidth|, which is inside |\FV@TrueTab|, is |\let| to a version that saves the width of every tab in a macro. When a line is broken, all tabs within it will then use a variant of |\FV@TrueTab| that sequentially retrieves the saved widths. This maintains the exact behavior of the case without line breaks. % +% Note that the special version of |\FV@TrueTab| is based on the \fvextra\ patched version of |\FV@TrueTab|, not on the original |\FV@TrueTab| defined in \fancyvrb. % -% \begin{macro}{\FV@TrueTab@SaveWidths} -% Version of |\FV@TrueTab| that also saves the width of each tab in sequentially numbered macros. -% \begin{macrocode} -\def\FV@TrueTab@SaveWidths{% - \egroup - \@tempdima=\FV@ObeyTabSize sp\relax - \@tempcnta=\wd\FV@TabBox - \advance\@tempcnta\FV@@ObeyTabSize\relax - \divide\@tempcnta\@tempdima - \multiply\@tempdima\@tempcnta - \advance\@tempdima-\wd\FV@TabBox - \expandafter\xdef\csname FV@TrueTab@Width\arabic{FV@TrueTabCounter}\endcsname{% - \number\@tempdima}% - \stepcounter{FV@TrueTabCounter}% - \setbox\FV@TabBox=\hbox\bgroup - \unhbox\FV@TabBox\hbox to\@tempdima{\hss\FV@TabChar}} -% \end{macrocode} -% \end{macro} % -% -% \begin{macro}{\FV@TrueTab@UseWidths} +% \begin{macro}{\FV@TrueTab@UseWidth} % Version of |\FV@TrueTab| that uses pre-computed tab widths. % \begin{macrocode} -\def\FV@TrueTab@UseWidths{% - \@tempdima=\csname FV@TrueTab@Width\arabic{FV@TrueTabCounter}\endcsname sp\relax +\def\FV@TrueTab@UseWidth{% + \@tempdima=\csname FV@TrueTab:Width\arabic{FV@TrueTabCounter}\endcsname sp\relax \stepcounter{FV@TrueTabCounter}% \hbox to\@tempdima{\hss\FV@TabChar}} % \end{macrocode} @@ -1888,10 +2489,29 @@ % % \begin{macro}{\FV@Break} % The entry macro for breaking lines, either anywhere or before/after specified characters. The current line (or argument) will be scanned token by token/group by group, and accumulated (with added potential breaks) in |\FV@TmpLine|. After scanning is complete, |\FV@TmpLine| will be inserted. It would be possible to insert each token/group into the document immediately after it is scanned, instead of accumulating them in a ``buffer.'' But that would interfere with macros. Even in the current approach, macros that take optional arguments are problematic.\footnote{Through a suitable definition that tracks the current state and looks for square brackets, this might be circumvented. Then again, in verbatim contexts, macro use should be minimal, so the restriction to macros without optional arguments should generally not be an issue.} The last token is tracked with |\FV@LastToken|, to allow lookbehind when breaking by groups of identical characters. |\FV@LastToken| is |\let| to |\FV@Undefined| any time the last token was something that shouldn't be compared against (for example, a non-empty group), and it is not reset whenever the last token may be ignored (for example, |{}|). When setting |\FV@LastToken|, it is vital always to use |\let\FV@LastToken=...| so that |\let\FV@LastToken==| will work (so that the equals sign |=| won't break things). +% +% The current definition of |\FV@Break@Token| is swapped for a UTF-8 compatible one under pdfTeX when necessary. The standard macros are defined next, since they make the algorithms simpler to understand. The more complex |UTF| variants are defined later. When swapping for the |UTF| macros, it is important to make sure that pdfTeX is indeed in use, that \pkg{inputenc} is indeed in use, and that the current encoding is UTF-8. The checks take into account the possibility of an errant |\ifx| test creating a previously non-existent macro and then |\let|ting it to |\relax|. % \begin{macrocode} \def\FV@Break{% \def\FV@TmpLine{}% \let\FV@LastToken=\FV@Undefined + \ifcsname pdfmatch\endcsname + \ifx\pdfmatch\relax + \else + \ifcsname inputencodingname\endcsname + \ifx\inputencodingname\relax + \else + \ifdefstring{\inputencodingname}{utf8}% + {\ifx\FV@Break@Token\FV@Break@AnyToken + \let\FV@Break@Token\FV@Break@AnyToken@UTF + \else + \ifx\FV@Break@Token\FV@Break@BeforeAfterToken + \let\FV@Break@Token\FV@Break@BeforeAfterToken@UTF + \fi + \fi}% + {}% + \fi\fi + \fi\fi \FV@Break@Scan } % \end{macrocode} @@ -2036,7 +2656,7 @@ \expandafter\FV@Break@AfterTokenBreak\FV@RescanToken} \def\FV@Break@AfterTokenBreak#1{% \let\FV@LastToken=#1% - \@ifnextchar\FV@Space% + \@ifnextchar\FV@Space@ifx% {\g@addto@macro{\FV@TmpLine}{#1}\FV@Break@Scan}% {\ifthenelse{\boolean{FV@BreakAfterGroup}}% {\ifx\@let@token#1\relax @@ -2070,6 +2690,320 @@ % \end{macro} % % +% \paragraph{Line scanning and break insertion macros for pdfTeX with UTF-8}\hfill\\ +% The macros above work with the XeTeX and LuaTeX engines and are also fine for pdfTeX with 8-bit character encodings. Unfortunately, pdfTeX works with multi-byte UTF-8 code points at the byte level, making things significantly trickier. The code below re-implements the macros in a manner compatible with the \pkg{inputenc} package with option |utf8|. Note that there is no attempt for compatibility with |utf8x|; |utf8| has been significantly improved in recent years and should be sufficient in the vast majority of cases. And implementing variants for |utf8| was already sufficiently painful. +% +% All of the |UTF| macros are only needed with pdfTeX, so they are created conditionally, inspired by the approach of the \pkg{iftex} package. The pdfTeX test deals with the possibility that a previous test using |\ifx| rather than the cleaner |\ifcsname| has already been performed. +% \begin{macrocode} +\ifcsname pdfmatch\endcsname +\ifx\pdfmatch\relax +\else +% \end{macrocode} +% +% +% \begin{macro}{\FV@UTF@two@octets}% +% \begin{macro}{\FV@UTF@three@octets}% +% \begin{macro}{\FV@UTF@four@octets} +% These are variants of the |utf8.def| macros that capture all bytes of a multi-byte code point and then pass them on as a single argument for further processing. The current |\FV@Break| (or other invoking macro) will have |\let| |\FV@Break@NextNext| to an appropriate macro that performs further processing. All code points are checked for validity here so as to raise errors as early as possible. Otherwise an invalid terminal byte sequence might gobble |\FV@EndBreak|, |\FV@Undefined|, or another delimiting macro, potentially making debugging much more difficult. It would be possible to use |\UTFviii@defined{|\meta{bytes}|}| to trigger an error directly, but the current approach is to attempt to typeset invalid code points, which should trigger errors without relying on the details of the |utf8.def| implementation. +% \begin{macrocode} +\def\FV@UTF@two@octets#1#2{% + \ifcsname u8:\detokenize{#1#2}\endcsname + \else + #1#2% + \fi + \FV@Break@NextNext{#1#2}} +\def\FV@UTF@three@octets#1#2#3{% + \ifcsname u8:\detokenize{#1#2#3}\endcsname + \else + #1#2#3% + \fi + \FV@Break@NextNext{#1#2#3}} +\def\FV@UTF@four@octets#1#2#3#4{% + \ifcsname u8:\detokenize{#1#2#3#4}\endcsname + \else + #1#2#3#4% + \fi + \FV@Break@NextNext{#1#2#3#4}} +% \end{macrocode} +% \end{macro}\end{macro}\end{macro} +% +% \begin{macro}{\FV@U8:<byte>} +% Define macros for each active byte. These are used for determining whether the current token is the first byte in a multi-byte sequence, and if so, invoking the necessary macro to capture the remaining bytes. The code is adapted from the beginning of |utf8.def|. Completely capitalized macro names are used to avoid having to worry about |\uppercase|. +% \begin{macrocode} +\begingroup +\catcode`\~=13 +\catcode`\"=12 +\def\FV@UTFviii@loop{% + \uccode`\~\count@ + \uppercase\expandafter{\FV@UTFviii@Tmp}% + \advance\count@\@ne + \ifnum\count@<\@tempcnta + \expandafter\FV@UTFviii@loop + \fi} +% \end{macrocode} +% Setting up 2-byte UTF-8: +% \begin{macrocode} +\count@"C2 +\@tempcnta"E0 +\def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{% + \FV@UTF@two@octets}} +\FV@UTFviii@loop +% \end{macrocode} +% Setting up 3-byte UTF-8: +% \begin{macrocode} +\count@"E0 +\@tempcnta"F0 +\def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{% + \FV@UTF@three@octets}} +\FV@UTFviii@loop +% \end{macrocode} +% Setting up 4-byte UTF-8: +% \begin{macrocode} +\count@"F0 +\@tempcnta"F4 +\def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{% + \FV@UTF@four@octets}} +\FV@UTFviii@loop +\endgroup +% \end{macrocode} +% \end{macro} +% +% +% +% \begin{macro}{\FV@BreakBeforePrep@UTF} +% We need |UTF| variants of the |breakbefore| and |breakafter| prep macros. These are only ever used with \pkg{inputenc} with UTF-8. There is no need for encoding checks here; checks are performed in |\FV@FormattingPrepHook| (checks are inserted into it after the non-|UTF| macro definitions). +% \begin{macrocode} +\def\FV@BreakBeforePrep@UTF{% + \ifx\FV@BreakBefore\@empty\relax + \else + \gdef\FV@BreakBefore@Def{}% + \begingroup + \def\FV@BreakBefore@Process##1{% + \ifcsname FV@U8:\detokenize{##1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{##1}\endcsname + \let\FV@Break@NextNext\FV@BreakBefore@Process@ii + \else + \ifx##1\FV@Undefined + \let\FV@Break@Next\@gobble + \else + \let\FV@Break@Next\FV@BreakBefore@Process@i + \fi + \fi + \FV@Break@Next##1% + }% + \def\FV@BreakBefore@Process@i##1{% + \expandafter\FV@BreakBefore@Process@ii\expandafter{##1}}% + \def\FV@BreakBefore@Process@ii##1{% + \g@addto@macro\FV@BreakBefore@Def{% + \@namedef{FV@BreakBefore@Token\detokenize{##1}}{}}% + \FV@BreakBefore@Process + }% + \FV@EscChars + \expandafter\FV@BreakBefore@Process\FV@BreakBefore\FV@Undefined + \endgroup + \FV@BreakBefore@Def + \fi +} +% \end{macrocode} +% \end{macro} +% +% +% \begin{macro}{\FV@BreakAfterPrep@UTF} +% \begin{macrocode} +\def\FV@BreakAfterPrep@UTF{% + \ifx\FV@BreakAfter\@empty\relax + \else + \gdef\FV@BreakAfter@Def{}% + \begingroup + \def\FV@BreakAfter@Process##1{% + \ifcsname FV@U8:\detokenize{##1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{##1}\endcsname + \let\FV@Break@NextNext\FV@BreakAfter@Process@ii + \else + \ifx##1\FV@Undefined + \let\FV@Break@Next\@gobble + \else + \let\FV@Break@Next\FV@BreakAfter@Process@i + \fi + \fi + \FV@Break@Next##1% + }% + \def\FV@BreakAfter@Process@i##1{% + \expandafter\FV@BreakAfter@Process@ii\expandafter{##1}}% + \def\FV@BreakAfter@Process@ii##1{% + \ifcsname FV@BreakBefore@Token\detokenize{##1}\endcsname + \ifthenelse{\boolean{FV@BreakBeforeGroup}}% + {\ifthenelse{\boolean{FV@BreakAfterGroup}}% + {}% + {\PackageError{fvextra}% + {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}% + {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}}}% + {\ifthenelse{\boolean{FV@BreakAfterGroup}}% + {\PackageError{fvextra}% + {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}% + {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}}% + {}}% + \fi + \g@addto@macro\FV@BreakAfter@Def{% + \@namedef{FV@BreakAfter@Token\detokenize{##1}}{}}% + \FV@BreakAfter@Process + }% + \FV@EscChars + \expandafter\FV@BreakAfter@Process\FV@BreakAfter\FV@Undefined + \endgroup + \FV@BreakAfter@Def + \fi +} +% \end{macrocode} +% \end{macro} +% +% +% +% \begin{macro}{\FV@Break@AnyToken@UTF} +% Instead of just adding each token to |\FV@TmpLine| with a preceding break, also check for multi-byte code points and capture the remaining bytes when they are encountered. +% \begin{macrocode} +\def\FV@Break@AnyToken@UTF#1{% + \ifcsname FV@U8:\detokenize{#1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname + \let\FV@Break@NextNext\FV@Break@AnyToken@UTF@i + \else + \let\FV@Break@Next\FV@Break@AnyToken@UTF@i + \fi + \FV@Break@Next{#1}% +} +\def\FV@Break@AnyToken@UTF@i#1{% + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakAnywhereBreak#1}% + \FV@Break@Scan} +% \end{macrocode} +% \end{macro} +% +% +% +% \begin{macro}{\FV@Break@BeforeAfterToken@UTF} +% Due to the way that the flow works, |#1| will sometimes be a single byte and sometimes be a multi-byte UTF-8 code point. As a result, it is vital use use |\detokenize| in the UTF-8 leading byte checks; |\string| would only deal with the first byte. It is also important to keep track of the distinction between |\FV@Break@Next#1| and |\FV@Break@Next{#1}|. In some cases, a multi-byte sequence is being passed on as a single argument, so it must be enclosed in curly braces; in other cases, it is being re-inserted into the scanning stream and curly braces must be avoided lest they be interpreted as part of the original text. +% \begin{macrocode} +\def\FV@Break@BeforeAfterToken@UTF#1{% + \ifcsname FV@U8:\detokenize{#1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname + \let\FV@Break@NextNext\FV@Break@BeforeAfterToken@UTF@i + \else + \let\FV@Break@Next\FV@Break@BeforeAfterToken@UTF@i + \fi + \FV@Break@Next{#1}% +} +\def\FV@Break@BeforeAfterToken@UTF@i#1{% + \ifcsname FV@BreakBefore@Token\detokenize{#1}\endcsname + \let\FV@Break@Next\FV@Break@BeforeTokenBreak@UTF + \else + \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname + \let\FV@Break@Next\FV@Break@AfterTokenBreak@UTF + \else + \let\FV@Break@Next\FV@Break@BeforeAfterTokenNoBreak@UTF + \fi + \fi + \FV@Break@Next{#1}% +} +\def\FV@Break@BeforeAfterTokenNoBreak@UTF#1{% + \g@addto@macro{\FV@TmpLine}{#1}% + \def\FV@LastToken{#1}% + \FV@Break@Scan} +\def\FV@Break@BeforeTokenBreak@UTF#1{% + \def\FV@CurrentToken{#1}% + \ifthenelse{\boolean{FV@BreakBeforeGroup}}% + {\ifx\FV@CurrentToken\FV@LastToken\relax + \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname + \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF + \def\FV@RescanToken{#1}% + \else + \g@addto@macro{\FV@TmpLine}{#1}% + \let\FV@Break@Next\FV@Break@Scan + \def\FV@LastToken{#1}% + \fi + \else + \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakBeforeBreak}% + \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF + \def\FV@RescanToken{#1}% + \else + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakBeforeBreak#1}% + \let\FV@Break@Next\FV@Break@Scan + \def\FV@LastToken{#1}% + \fi + \fi}% + {\ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakBeforeBreak}% + \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF + \def\FV@RescanToken{#1}% + \else + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakBeforeBreak#1}% + \let\FV@Break@Next\FV@Break@Scan + \def\FV@LastToken{#1}% + \fi}% + \FV@Break@Next} +\def\FV@Break@BeforeTokenBreak@AfterRescan@UTF{% + \expandafter\FV@Break@AfterTokenBreak@UTF\expandafter{\FV@RescanToken}} +\def\FV@Break@AfterTokenBreak@UTF#1{% + \def\FV@LastToken{#1}% + \@ifnextchar\FV@Space@ifx% + {\g@addto@macro{\FV@TmpLine}{#1}\FV@Break@Scan}% + {\ifthenelse{\boolean{FV@BreakAfterGroup}}% + {\g@addto@macro{\FV@TmpLine}{#1}% + \ifx\@let@token\bgroup\relax + \let\FV@Break@Next\FV@Break@AfterTokenBreak@Group@UTF + \else + \let\FV@Break@Next\FV@Break@AfterTokenBreak@UTF@i + \fi}% + {\g@addto@macro{\FV@TmpLine}{#1\FancyVerbBreakAfterBreak}% + \let\FV@Break@Next\FV@Break@Scan}% + \FV@Break@Next}% +} +\def\FV@Break@AfterTokenBreak@UTF@i#1{% + \ifcsname FV@U8:\detokenize{#1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname + \let\FV@Break@NextNext\FV@Break@AfterTokenBreak@UTF@i + \else + \def\FV@NextToken{#1}% + \ifx\FV@LastToken\FV@NextToken + \else + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakAfterBreak}% + \fi + \let\FV@Break@Next\FV@Break@Scan + \fi + \FV@Break@Next#1} +\def\FV@Break@AfterTokenBreak@Group@UTF#1{% + \g@addto@macro{\FV@TmpLine}{{#1}}% + \ifstrempty{#1}% + {\let\FV@Break@Next\FV@Break@AfterTokenBreak@Group@UTF@i}% + {\let\FV@Break@Next\FV@Break@Scan\let\FV@LastToken=\FV@Undefined}% + \FV@Break@Next} +\def\FV@Break@AfterTokenBreak@Group@UTF@i{% + \@ifnextchar\bgroup% + {\FV@Break@Scan}% + {\FV@Break@AfterTokenBreak@Group@UTF@ii}} +\def\FV@Break@AfterTokenBreak@Group@UTF@ii#1{% + \ifcsname FV@U8:\detokenize{#1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname + \let\FV@Break@NextNext\FV@Break@AfterTokenBreak@Group@UTF@ii + \else + \def\FV@NextToken{#1}% + \ifx\FV@LastToken\FV@NextToken + \else + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakAfterBreak}% + \fi + \let\FV@Break@Next\FV@Break@Scan + \fi + \FV@Break@Next#1} +% \end{macrocode} +% \end{macro} +% +% +% End the conditional creation of the pdfTeX |UTF| macros: +% \begin{macrocode} +\fi\fi +% \end{macrocode} +% +% +% % \paragraph{Line processing before scanning} % % \begin{macro}{\FV@makeLineNumber} @@ -2151,17 +3085,20 @@ \advance\linewidth by -\FV@FrameRule \fi \ifx\FV@Tab\FV@TrueTab - \let\FV@Tab\FV@TrueTab@SaveWidths + \let\FV@TrueTabSaveWidth\FV@TrueTabSaveWidth@Save \setcounter{FV@TrueTabCounter}{0}% \fi - \sbox{\FV@LineBox}{\FancyVerbFormatLine{\FV@ObeyTabs{\FancyVerbFormatText{#1}}}}% - \ifx\FV@Tab\FV@TrueTab@SaveWidths - \let\FV@Tab\FV@TrueTab + \sbox{\FV@LineBox}{% + \FancyVerbFormatLine{% + %\FancyVerbHighlightLine %<-- Default definition using \rlap breaks breaking + {\FV@ObeyTabs{\FancyVerbFormatText{#1}}}}}% + \ifx\FV@Tab\FV@TrueTab + \let\FV@TrueTabSaveWidth\relax \fi \ifdim\wd\FV@LineBox>\linewidth \setcounter{FancyVerbLineBreakLast}{0}% \ifx\FV@Tab\FV@TrueTab - \let\FV@Tab\FV@TrueTab@UseWidths + \let\FV@Tab\FV@TrueTab@UseWidth \setcounter{FV@TrueTabCounter}{0}% \fi \FV@SaveLineBox{#1}% @@ -2171,17 +3108,20 @@ \FV@SaveLineBox{#1}}% \FV@LeftListNumber \FV@LeftListFrame - \FancyVerbFormatLine{\usebox{\FV@LineBox}}% + \FancyVerbFormatLine{% + \FancyVerbHighlightLine{\usebox{\FV@LineBox}}}% \FV@RightListFrame \FV@RightListNumber - \ifx\FV@Tab\FV@TrueTab@UseWidths + \ifx\FV@Tab\FV@TrueTab@UseWidth \let\FV@Tab\FV@TrueTab \fi \else \FV@LeftListNumber \FV@LeftListFrame \FancyVerbFormatLine{% - \parbox[t]{\linewidth}{\noindent\strut\FV@ObeyTabs{\FancyVerbFormatText{#1}}\strut}}% + \FancyVerbHighlightLine{% + \parbox[t]{\linewidth}{% + \noindent\strut\FV@ObeyTabs{\FancyVerbFormatText{#1}}\strut}}}% \FV@RightListFrame \FV@RightListNumber \fi}% diff --git a/Master/texmf-dist/tex/latex/fvextra/fvextra.sty b/Master/texmf-dist/tex/latex/fvextra/fvextra.sty index 00e5bbed774..45388f31463 100644 --- a/Master/texmf-dist/tex/latex/fvextra/fvextra.sty +++ b/Master/texmf-dist/tex/latex/fvextra/fvextra.sty @@ -20,7 +20,7 @@ %% \NeedsTeXFormat{LaTeX2e}[1999/12/01] \ProvidesPackage{fvextra} - [2016/06/28 v1.0 fvextra - extensions and patches for fancyvrb] + [2016/07/14 v1.1 fvextra - extensions and patches for fancyvrb] \RequirePackage{ifthen} \RequirePackage{etoolbox} \RequirePackage{fancyvrb} @@ -35,14 +35,8 @@ \@ifpackageloaded{csquotes}% {\PackageWarning{fvextra}{csquotes should be loaded after fvextra, % to avoid a warning from the lineno package}}{} -\@ifpackageloaded{minted}% - {\PackageError{fvextra}% - {fvextra must be loaded before minted}% - {fvextra must be loaded before minted}} -\@ifpackageloaded{pythontex}% - {\PackageError{fvextra}% - {fvextra must be loaded before pythontex}% - {fvextra must be loaded before pythontex}} +\def\FV@Space@ifx{\FV@Space} +\def\FV@Tab@ifx{\FV@Tab} \let\FV@FormattingPrepHook\@empty \expandafter\def\expandafter\FV@FormattingPrep\expandafter{% \expandafter\FV@FormattingPrepHook\FV@FormattingPrep} @@ -68,7 +62,27 @@ \let\]\FV@rightsquarebracket } %$ <- highlighting \def\FancyVerbSpace{\textvisiblespace} +\newcounter{FV@TrueTabGroupLevel} +\def\FV@@ObeyTabs#1{% + \let\FV@Space@Orig\FV@Space + \let\FV@Space\FV@Space@ObeyTabs + \setcounter{FV@TrueTabGroupLevel}{\the\currentgrouplevel}% + \addtocounter{FV@TrueTabGroupLevel}{1}% + \setbox\FV@TabBox=\hbox{#1}\box\FV@TabBox + \let\FV@Space\FV@Space@Orig} \def\FV@TrueTab{% + \ifnum\value{FV@TrueTabGroupLevel}=\the\currentgrouplevel\relax + \expandafter\FV@TrueTab@NoGroup + \else + \expandafter\FV@TrueTab@Group + \fi} +\let\FV@TrueTabSaveWidth\relax +\newcounter{FV@TrueTabCounter} +\def\FV@TrueTabSaveWidth@Save{% + \expandafter\xdef\csname FV@TrueTab:Width\arabic{FV@TrueTabCounter}\endcsname{% + \number\@tempdima}% + \stepcounter{FV@TrueTabCounter}} +\def\FV@TrueTab@NoGroup{% \egroup \@tempdima=\FV@ObeyTabSize sp\relax \@tempcnta=\wd\FV@TabBox @@ -76,8 +90,62 @@ \divide\@tempcnta\@tempdima \multiply\@tempdima\@tempcnta \advance\@tempdima-\wd\FV@TabBox + \FV@TrueTabSaveWidth \setbox\FV@TabBox=\hbox\bgroup \unhbox\FV@TabBox\hbox to\@tempdima{\hss\FV@TabChar}} +\newboolean{FV@ObeyTabs@Whitespace@Tab} +\def\FV@TrueTab@Group{% + \booltrue{FV@ObeyTabs@Whitespace@Tab}% + \gdef\FV@TmpWhitespace{\FV@Tab@ifx}% + \FV@ObeyTabs@ScanWhitespace} +\def\FV@Space@ObeyTabs{% + \ifnum\value{FV@TrueTabGroupLevel}=\the\currentgrouplevel\relax + \expandafter\FV@Space@ObeyTabs@NoGroup + \else + \expandafter\FV@Space@ObeyTabs@Group + \fi} +\def\FV@Space@ObeyTabs@NoGroup{\FV@Space@Orig} +\def\FV@Space@ObeyTabs@Group{% + \boolfalse{FV@ObeyTabs@Whitespace@Tab}% + \gdef\FV@TmpWhitespace{\FV@Space@ifx}% + \FV@ObeyTabs@ScanWhitespace} +\def\FV@ObeyTabs@ScanWhitespace{% + \@ifnextchar\FV@Space@ifx% + {\FV@TrueTab@CaptureWhitespace@Space}% + {\ifx\@let@token\FV@Tab@ifx + \expandafter\FV@TrueTab@CaptureWhitespace@Tab + \else + \expandafter\FV@ObeyTabs@ResolveWhitespace + \fi}} +\def\FV@TrueTab@CaptureWhitespace@Space#1{% + \g@addto@macro\FV@TmpWhitespace{\FV@Space@ifx}% + \FV@ObeyTabs@ScanWhitespace} +\def\FV@TrueTab@CaptureWhitespace@Tab#1{% + \booltrue{FV@ObeyTabs@Whitespace@Tab}% + \g@addto@macro\FV@TmpWhitespace{\FV@Tab@ifx}% + \FV@ObeyTabs@ScanWhitespace} +\newbox\FV@TabBox@Group +\def\FV@TrueTab@Group@Expand{% + \egroup + \@tempdima=\FV@ObeyTabSize sp\relax + \@tempcnta=\wd\FV@TabBox@Group + \advance\@tempcnta\FV@@ObeyTabSize\relax + \divide\@tempcnta\@tempdima + \multiply\@tempdima\@tempcnta + \advance\@tempdima-\wd\FV@TabBox@Group + \FV@TrueTabSaveWidth + \setbox\FV@TabBox@Group=\hbox\bgroup + \unhbox\FV@TabBox@Group\hbox to\@tempdima{\hss\FV@TabChar}} +\def\FV@ObeyTabs@ResolveWhitespace{% + \let\FV@Space\FV@Space@Orig + \let\FV@Tab\FV@TrueTab@Group@Expand + \expandafter\FV@ObeyTabs@ResolveWhitespace@i\expandafter{\FV@TmpWhitespace}% + \let\FV@Space\FV@Space@ObeyTabs + \let\FV@Tab\FV@TrueTab} +\def\FV@ObeyTabs@ResolveWhitespace@i#1{% + \ifbool{FV@ObeyTabs@Whitespace@Tab}% + {\setbox\FV@TabBox@Group=\hbox{#1}\box\FV@TabBox@Group}% + {#1}} \def\FancyVerbMathSpace{ } \def\FV@SetupMathSpace{% \everymath\expandafter{\the\everymath\let\FV@Space\FancyVerbMathSpace}} @@ -122,16 +190,78 @@ \kern\FV@FrameSep\relax \penalty\@M \endgroup} +\define@key{FV}{rulecolor}{% + \ifstrempty{#1}% + {\let\FancyVerbRuleColor\relax}% + {\ifstrequal{#1}{none}% + {\let\FancyVerbRuleColor\relax}% + {\def\@tempa{#1}% + \FV@KVProcess@RuleColor#1\FV@Undefined}}} +\def\FV@KVProcess@RuleColor#1#2\FV@Undefined{% + \ifx#1\color + \else + \expandafter\def\expandafter\@tempa\expandafter{% + \expandafter\color\expandafter{\@tempa}}% + \fi + \let\FancyVerbRuleColor\@tempa} +\fvset{rulecolor=none} +\define@key{FV}{fillcolor}{% + \ifstrempty{#1}% + {\let\FancyVerbFillColor\relax}% + {\ifstrequal{#1}{none}% + {\let\FancyVerbFillColor\relax}% + {\def\@tempa{#1}% + \FV@KVProcess@FillColor#1\FV@Undefined}}} +\def\FV@KVProcess@FillColor#1#2\FV@Undefined{% + \ifx#1\color + \else + \expandafter\def\expandafter\@tempa\expandafter{% + \expandafter\color\expandafter{\@tempa}}% + \fi + \let\FancyVerbFillColor\@tempa} +\fvset{fillcolor=none} \define@booleankey{FV}{linenos}% {\@nameuse{FV@Numbers@left}}{\@nameuse{FV@Numbers@none}} \define@key{FV}{tab}{\def\FancyVerbTab{#1}} +\define@key{FV}{tabcolor}% + {\ifstrempty{#1}% + {\let\FV@TabColor\relax}% + {\ifstrequal{#1}{none}% + {\let\FV@TabColor\relax}% + {\def\FV@TabColor{\textcolor{#1}}}}} +\define@booleankey{FV}{showtabs}% + {\def\FV@TabChar{\FV@TabColor{\FancyVerbTab}}}% + {\let\FV@TabChar\relax} +\fvset{tabcolor=none, showtabs=false} \define@key{FV}{space}{\def\FancyVerbSpace{#1}} +\define@key{FV}{spacecolor}% + {\ifstrempty{#1}% + {\let\FV@SpaceColor\relax}% + {\ifstrequal{#1}{none}% + {\let\FV@SpaceColor\relax}% + {\def\FV@SpaceColor{\textcolor{#1}}}}} +\define@booleankey{FV}{showspaces}% + {\def\FV@Space{\FV@SpaceColor{\FancyVerbSpace}}}% + {\def\FV@Space{\ }} +\fvset{spacecolor=none, showspaces=false} \define@booleankey{FV}{mathescape}% {\let\FancyVerbMathEscape\FV@MathEscape}% {\let\FancyVerbMathEscape\relax} -\FV@AddToHook\FV@CatCodesHook\FancyVerbMathEscape \def\FV@MathEscape{\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax} +\FV@AddToHook\FV@CatCodesHook\FancyVerbMathEscape \fvset{mathescape=false} +\newbool{FV@CurlyQuotes} +\define@booleankey{FV}{curlyquotes}% + {\booltrue{FV@CurlyQuotes}}% + {\boolfalse{FV@CurlyQuotes}} +\def\FancyVerbCurlyQuotes{% + \ifbool{FV@CurlyQuotes}% + {\expandafter\def\expandafter\@noligs\expandafter{\@noligs + \begingroup\lccode`\~=`\`\lowercase{\endgroup\def~}{`}% + \begingroup\lccode`\~=`\'\lowercase{\endgroup\def~}{'}}}% + {}} +\g@addto@macro\FV@FormattingPrepHook{\FancyVerbCurlyQuotes} +\fvset{curlyquotes=false} \def\FancyVerbFormatLine#1{#1} \def\FancyVerbFormatText#1{#1} \def\FV@ListProcessLine@NoBreak#1{% @@ -140,11 +270,16 @@ \hbox to \linewidth{% \FV@LeftListNumber \FV@LeftListFrame - \FancyVerbFormatLine{\FV@ObeyTabs{\FancyVerbFormatText{#1}}}\hss + \FancyVerbFormatLine{% + \FancyVerbHighlightLine{% + \FV@ObeyTabs{\FancyVerbFormatText{#1}}}}\hss \FV@RightListFrame \FV@RightListNumber}% \hss}} -\def\FV@BProcessLine#1{\hbox{\FancyVerbFormatLine{\FV@ObeyTabs{\FancyVerbFormatText{#1}}}}} +\def\FV@BProcessLine#1{% + \hbox{\FancyVerbFormatLine{% + \FancyVerbHighlightLine{% + \FV@ObeyTabs{\FancyVerbFormatText{#1}}}}}} \newbool{FV@NumberFirstLine} \define@booleankey{FV}{numberfirstline}% {\booltrue{FV@NumberFirstLine}}% @@ -308,6 +443,93 @@ \fi}{}% \fi}% } +\define@key{FV}{highlightlines}{\def\FV@HighlightLinesList{#1}}% +\fvset{highlightlines=} +\define@key{FV}{highlightcolor}{\def\FancyVerbHighlightColor{#1}}% +\let\FancyVerbHighlightColor\@empty +\ifcsname definecolor\endcsname +\ifx\definecolor\relax +\else + \definecolor{FancyVerbHighlightColor}{HTML}{E0FFFF} + \fvset{highlightcolor=FancyVerbHighlightColor} +\fi\fi +\AtBeginDocument{% + \ifx\FancyVerbHighlightColor\@empty + \ifcsname definecolor\endcsname + \ifx\definecolor\relax + \else + \definecolor{FancyVerbHighlightColor}{rgb}{0,1,1} + \fvset{highlightcolor=FancyVerbHighlightColor} + \fi\fi + \fi} +\def\FancyVerbHighlightLine#1{#1} +\def\FV@HighlightLine#1{% + \@tempcnta=\c@FancyVerbLine + \@tempcntb=\c@FancyVerbLine + \ifcsname FV@HighlightLine:\number\@tempcnta\endcsname + \advance\@tempcntb\m@ne + \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname + \advance\@tempcntb\tw@ + \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname + \let\FV@HighlightLine@Next\FancyVerbHighlightLineMiddle + \else + \let\FV@HighlightLine@Next\FancyVerbHighlightLineLast + \fi + \else + \advance\@tempcntb\tw@ + \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname + \let\FV@HighlightLine@Next\FancyVerbHighlightLineFirst + \else + \let\FV@HighlightLine@Next\FancyVerbHighlightLineSingle + \fi + \fi + \else + \let\FV@HighlightLine@Next\FancyVerbHighlightLineNormal + \fi + \FV@HighlightLine@Next{#1}% +} +\def\FancyVerbHighlightLineNormal#1{#1} +\newlength{\FV@TmpLength} +\def\FancyVerbHighlightLineFirst#1{% + \setlength{\FV@TmpLength}{\fboxsep}% + \setlength{\fboxsep}{0pt}% + \colorbox{\FancyVerbHighlightColor}{% + \setlength{\fboxsep}{\FV@TmpLength}% + \rlap{\strut#1}% + \hspace{\linewidth}}} +\let\FancyVerbHighlightLineMiddle\FancyVerbHighlightLineFirst +\let\FancyVerbHighlightLineLast\FancyVerbHighlightLineFirst +\let\FancyVerbHighlightLineSingle\FancyVerbHighlightLineFirst +\def\FV@HighlightLinesPrep{% + \ifx\FV@HighlightLinesList\@empty + \else + \let\FancyVerbHighlightLine\FV@HighlightLine + \expandafter\FV@HighlightLinesPrep@i + \fi} +\def\FV@HighlightLinesPrep@i{% + \renewcommand{\do}[1]{% + \ifstrempty{##1}{}{\FV@HighlightLinesParse##1-\FV@Undefined}}% + \expandafter\docsvlist\expandafter{\FV@HighlightLinesList}} +\def\FV@HighlightLinesParse#1-#2\FV@Undefined{% + \ifstrempty{#2}% + {\FV@HighlightLinesParse@Single{#1}}% + {\FV@HighlightLinesParse@Range{#1}#2\relax}} +\def\FV@HighlightLinesParse@Single#1{% + \expandafter\let\csname FV@HighlightLine:\detokenize{#1}\endcsname\relax} +\newcounter{FV@HighlightLinesStart} +\newcounter{FV@HighlightLinesStop} +\def\FV@HighlightLinesParse@Range#1#2-{% + \setcounter{FV@HighlightLinesStart}{#1}% + \setcounter{FV@HighlightLinesStop}{#2}% + \stepcounter{FV@HighlightLinesStop}% + \FV@HighlightLinesParse@Range@Loop} +\def\FV@HighlightLinesParse@Range@Loop{% + \ifnum\value{FV@HighlightLinesStart}<\value{FV@HighlightLinesStop}\relax + \expandafter\let\csname FV@HighlightLine:\arabic{FV@HighlightLinesStart}\endcsname\relax + \stepcounter{FV@HighlightLinesStart}% + \expandafter\FV@HighlightLinesParse@Range@Loop + \fi} +\g@addto@macro\FV@FormattingPrepHook{\FV@HighlightLinesPrep} \newboolean{FV@BreakLines} \define@booleankey{FV}{breaklines}% {\FV@BreakLinestrue @@ -440,7 +662,6 @@ {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}% {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}}% {}}% - \else \fi \g@addto@macro\FV@BreakAfter@Def{% \@namedef{FV@BreakAfter@Token\detokenize{##1}}{}}% @@ -451,7 +672,20 @@ \FV@BreakAfter@Def \fi } -\g@addto@macro\FV@FormattingPrepHook{\FV@BreakBeforePrep\FV@BreakAfterPrep} +\g@addto@macro\FV@FormattingPrepHook{% + \ifcsname pdfmatch\endcsname + \ifx\pdfmatch\relax + \else + \ifcsname inputencodingname\endcsname + \ifx\inputencodingname\relax + \else + \ifdefstring{\inputencodingname}{utf8}% + {\let\FV@BreakBeforePrep\FV@BreakBeforePrep@UTF + \let\FV@BreakAfterPrep\FV@BreakAfterPrep@UTF}% + {}% + \fi\fi + \fi\fi + \FV@BreakBeforePrep\FV@BreakAfterPrep} \define@key{FV}{breakanywheresymbolpre}{% \ifstrempty{#1}% {\def\FancyVerbBreakAnywhereSymbolPre{}}% @@ -500,12 +734,12 @@ \ifx\FV@NextChar\FV@Undefined\relax \let\FV@Next=\relax \else - \expandafter\ifx\FV@NextChar\FV@Space\relax - \g@addto@macro{\FV@LineIndentChars}{\FV@Space}% + \ifx\FV@NextChar\FV@Space@ifx\relax + \g@addto@macro{\FV@LineIndentChars}{\FV@Space@ifx}% \let\FV@Next=\FV@GetLineIndent \else - \expandafter\ifx\FV@NextChar\FV@Tab\relax - \g@addto@macro{\FV@LineIndentChars}{\FV@Tab}% + \ifx\FV@NextChar\FV@Tab@ifx\relax + \g@addto@macro{\FV@LineIndentChars}{\FV@Tab@ifx}% \let\FV@Next=\FV@GetLineIndent \else \let\FV@Next=\FV@CleanRemainingChars @@ -514,27 +748,30 @@ \fi \FV@Next } -\newcounter{FV@TrueTabCounter} -\def\FV@TrueTab@SaveWidths{% - \egroup - \@tempdima=\FV@ObeyTabSize sp\relax - \@tempcnta=\wd\FV@TabBox - \advance\@tempcnta\FV@@ObeyTabSize\relax - \divide\@tempcnta\@tempdima - \multiply\@tempdima\@tempcnta - \advance\@tempdima-\wd\FV@TabBox - \expandafter\xdef\csname FV@TrueTab@Width\arabic{FV@TrueTabCounter}\endcsname{% - \number\@tempdima}% - \stepcounter{FV@TrueTabCounter}% - \setbox\FV@TabBox=\hbox\bgroup - \unhbox\FV@TabBox\hbox to\@tempdima{\hss\FV@TabChar}} -\def\FV@TrueTab@UseWidths{% - \@tempdima=\csname FV@TrueTab@Width\arabic{FV@TrueTabCounter}\endcsname sp\relax +\def\FV@TrueTab@UseWidth{% + \@tempdima=\csname FV@TrueTab:Width\arabic{FV@TrueTabCounter}\endcsname sp\relax \stepcounter{FV@TrueTabCounter}% \hbox to\@tempdima{\hss\FV@TabChar}} \def\FV@Break{% \def\FV@TmpLine{}% \let\FV@LastToken=\FV@Undefined + \ifcsname pdfmatch\endcsname + \ifx\pdfmatch\relax + \else + \ifcsname inputencodingname\endcsname + \ifx\inputencodingname\relax + \else + \ifdefstring{\inputencodingname}{utf8}% + {\ifx\FV@Break@Token\FV@Break@AnyToken + \let\FV@Break@Token\FV@Break@AnyToken@UTF + \else + \ifx\FV@Break@Token\FV@Break@BeforeAfterToken + \let\FV@Break@Token\FV@Break@BeforeAfterToken@UTF + \fi + \fi}% + {}% + \fi\fi + \fi\fi \FV@Break@Scan } \def\FV@EndBreak{\FV@TmpLine} @@ -621,7 +858,7 @@ \expandafter\FV@Break@AfterTokenBreak\FV@RescanToken} \def\FV@Break@AfterTokenBreak#1{% \let\FV@LastToken=#1% - \@ifnextchar\FV@Space% + \@ifnextchar\FV@Space@ifx% {\g@addto@macro{\FV@TmpLine}{#1}\FV@Break@Scan}% {\ifthenelse{\boolean{FV@BreakAfterGroup}}% {\ifx\@let@token#1\relax @@ -651,6 +888,252 @@ {\FV@Break@Scan}% {\g@addto@macro{\FV@TmpLine}{\FancyVerbBreakAfterBreak}% \FV@Break@Scan}} +\ifcsname pdfmatch\endcsname +\ifx\pdfmatch\relax +\else +\def\FV@UTF@two@octets#1#2{% + \ifcsname u8:\detokenize{#1#2}\endcsname + \else + #1#2% + \fi + \FV@Break@NextNext{#1#2}} +\def\FV@UTF@three@octets#1#2#3{% + \ifcsname u8:\detokenize{#1#2#3}\endcsname + \else + #1#2#3% + \fi + \FV@Break@NextNext{#1#2#3}} +\def\FV@UTF@four@octets#1#2#3#4{% + \ifcsname u8:\detokenize{#1#2#3#4}\endcsname + \else + #1#2#3#4% + \fi + \FV@Break@NextNext{#1#2#3#4}} +\begingroup +\catcode`\~=13 +\catcode`\"=12 +\def\FV@UTFviii@loop{% + \uccode`\~\count@ + \uppercase\expandafter{\FV@UTFviii@Tmp}% + \advance\count@\@ne + \ifnum\count@<\@tempcnta + \expandafter\FV@UTFviii@loop + \fi} +\count@"C2 +\@tempcnta"E0 +\def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{% + \FV@UTF@two@octets}} +\FV@UTFviii@loop +\count@"E0 +\@tempcnta"F0 +\def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{% + \FV@UTF@three@octets}} +\FV@UTFviii@loop +\count@"F0 +\@tempcnta"F4 +\def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{% + \FV@UTF@four@octets}} +\FV@UTFviii@loop +\endgroup +\def\FV@BreakBeforePrep@UTF{% + \ifx\FV@BreakBefore\@empty\relax + \else + \gdef\FV@BreakBefore@Def{}% + \begingroup + \def\FV@BreakBefore@Process##1{% + \ifcsname FV@U8:\detokenize{##1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{##1}\endcsname + \let\FV@Break@NextNext\FV@BreakBefore@Process@ii + \else + \ifx##1\FV@Undefined + \let\FV@Break@Next\@gobble + \else + \let\FV@Break@Next\FV@BreakBefore@Process@i + \fi + \fi + \FV@Break@Next##1% + }% + \def\FV@BreakBefore@Process@i##1{% + \expandafter\FV@BreakBefore@Process@ii\expandafter{##1}}% + \def\FV@BreakBefore@Process@ii##1{% + \g@addto@macro\FV@BreakBefore@Def{% + \@namedef{FV@BreakBefore@Token\detokenize{##1}}{}}% + \FV@BreakBefore@Process + }% + \FV@EscChars + \expandafter\FV@BreakBefore@Process\FV@BreakBefore\FV@Undefined + \endgroup + \FV@BreakBefore@Def + \fi +} +\def\FV@BreakAfterPrep@UTF{% + \ifx\FV@BreakAfter\@empty\relax + \else + \gdef\FV@BreakAfter@Def{}% + \begingroup + \def\FV@BreakAfter@Process##1{% + \ifcsname FV@U8:\detokenize{##1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{##1}\endcsname + \let\FV@Break@NextNext\FV@BreakAfter@Process@ii + \else + \ifx##1\FV@Undefined + \let\FV@Break@Next\@gobble + \else + \let\FV@Break@Next\FV@BreakAfter@Process@i + \fi + \fi + \FV@Break@Next##1% + }% + \def\FV@BreakAfter@Process@i##1{% + \expandafter\FV@BreakAfter@Process@ii\expandafter{##1}}% + \def\FV@BreakAfter@Process@ii##1{% + \ifcsname FV@BreakBefore@Token\detokenize{##1}\endcsname + \ifthenelse{\boolean{FV@BreakBeforeGroup}}% + {\ifthenelse{\boolean{FV@BreakAfterGroup}}% + {}% + {\PackageError{fvextra}% + {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}% + {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}}}% + {\ifthenelse{\boolean{FV@BreakAfterGroup}}% + {\PackageError{fvextra}% + {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}% + {Conflicting breakbeforegroup and breakaftergroup for "\detokenize{##1}"}}% + {}}% + \fi + \g@addto@macro\FV@BreakAfter@Def{% + \@namedef{FV@BreakAfter@Token\detokenize{##1}}{}}% + \FV@BreakAfter@Process + }% + \FV@EscChars + \expandafter\FV@BreakAfter@Process\FV@BreakAfter\FV@Undefined + \endgroup + \FV@BreakAfter@Def + \fi +} +\def\FV@Break@AnyToken@UTF#1{% + \ifcsname FV@U8:\detokenize{#1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname + \let\FV@Break@NextNext\FV@Break@AnyToken@UTF@i + \else + \let\FV@Break@Next\FV@Break@AnyToken@UTF@i + \fi + \FV@Break@Next{#1}% +} +\def\FV@Break@AnyToken@UTF@i#1{% + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakAnywhereBreak#1}% + \FV@Break@Scan} +\def\FV@Break@BeforeAfterToken@UTF#1{% + \ifcsname FV@U8:\detokenize{#1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname + \let\FV@Break@NextNext\FV@Break@BeforeAfterToken@UTF@i + \else + \let\FV@Break@Next\FV@Break@BeforeAfterToken@UTF@i + \fi + \FV@Break@Next{#1}% +} +\def\FV@Break@BeforeAfterToken@UTF@i#1{% + \ifcsname FV@BreakBefore@Token\detokenize{#1}\endcsname + \let\FV@Break@Next\FV@Break@BeforeTokenBreak@UTF + \else + \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname + \let\FV@Break@Next\FV@Break@AfterTokenBreak@UTF + \else + \let\FV@Break@Next\FV@Break@BeforeAfterTokenNoBreak@UTF + \fi + \fi + \FV@Break@Next{#1}% +} +\def\FV@Break@BeforeAfterTokenNoBreak@UTF#1{% + \g@addto@macro{\FV@TmpLine}{#1}% + \def\FV@LastToken{#1}% + \FV@Break@Scan} +\def\FV@Break@BeforeTokenBreak@UTF#1{% + \def\FV@CurrentToken{#1}% + \ifthenelse{\boolean{FV@BreakBeforeGroup}}% + {\ifx\FV@CurrentToken\FV@LastToken\relax + \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname + \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF + \def\FV@RescanToken{#1}% + \else + \g@addto@macro{\FV@TmpLine}{#1}% + \let\FV@Break@Next\FV@Break@Scan + \def\FV@LastToken{#1}% + \fi + \else + \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakBeforeBreak}% + \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF + \def\FV@RescanToken{#1}% + \else + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakBeforeBreak#1}% + \let\FV@Break@Next\FV@Break@Scan + \def\FV@LastToken{#1}% + \fi + \fi}% + {\ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakBeforeBreak}% + \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF + \def\FV@RescanToken{#1}% + \else + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakBeforeBreak#1}% + \let\FV@Break@Next\FV@Break@Scan + \def\FV@LastToken{#1}% + \fi}% + \FV@Break@Next} +\def\FV@Break@BeforeTokenBreak@AfterRescan@UTF{% + \expandafter\FV@Break@AfterTokenBreak@UTF\expandafter{\FV@RescanToken}} +\def\FV@Break@AfterTokenBreak@UTF#1{% + \def\FV@LastToken{#1}% + \@ifnextchar\FV@Space@ifx% + {\g@addto@macro{\FV@TmpLine}{#1}\FV@Break@Scan}% + {\ifthenelse{\boolean{FV@BreakAfterGroup}}% + {\g@addto@macro{\FV@TmpLine}{#1}% + \ifx\@let@token\bgroup\relax + \let\FV@Break@Next\FV@Break@AfterTokenBreak@Group@UTF + \else + \let\FV@Break@Next\FV@Break@AfterTokenBreak@UTF@i + \fi}% + {\g@addto@macro{\FV@TmpLine}{#1\FancyVerbBreakAfterBreak}% + \let\FV@Break@Next\FV@Break@Scan}% + \FV@Break@Next}% +} +\def\FV@Break@AfterTokenBreak@UTF@i#1{% + \ifcsname FV@U8:\detokenize{#1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname + \let\FV@Break@NextNext\FV@Break@AfterTokenBreak@UTF@i + \else + \def\FV@NextToken{#1}% + \ifx\FV@LastToken\FV@NextToken + \else + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakAfterBreak}% + \fi + \let\FV@Break@Next\FV@Break@Scan + \fi + \FV@Break@Next#1} +\def\FV@Break@AfterTokenBreak@Group@UTF#1{% + \g@addto@macro{\FV@TmpLine}{{#1}}% + \ifstrempty{#1}% + {\let\FV@Break@Next\FV@Break@AfterTokenBreak@Group@UTF@i}% + {\let\FV@Break@Next\FV@Break@Scan\let\FV@LastToken=\FV@Undefined}% + \FV@Break@Next} +\def\FV@Break@AfterTokenBreak@Group@UTF@i{% + \@ifnextchar\bgroup% + {\FV@Break@Scan}% + {\FV@Break@AfterTokenBreak@Group@UTF@ii}} +\def\FV@Break@AfterTokenBreak@Group@UTF@ii#1{% + \ifcsname FV@U8:\detokenize{#1}\endcsname + \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname + \let\FV@Break@NextNext\FV@Break@AfterTokenBreak@Group@UTF@ii + \else + \def\FV@NextToken{#1}% + \ifx\FV@LastToken\FV@NextToken + \else + \g@addto@macro{\FV@TmpLine}{\FancyVerbBreakAfterBreak}% + \fi + \let\FV@Break@Next\FV@Break@Scan + \fi + \FV@Break@Next#1} +\fi\fi \def\FV@makeLineNumber{% \hss \FancyVerbBreakSymbolLeftLogic{\FancyVerbBreakSymbolLeft}% @@ -710,17 +1193,20 @@ \advance\linewidth by -\FV@FrameRule \fi \ifx\FV@Tab\FV@TrueTab - \let\FV@Tab\FV@TrueTab@SaveWidths + \let\FV@TrueTabSaveWidth\FV@TrueTabSaveWidth@Save \setcounter{FV@TrueTabCounter}{0}% \fi - \sbox{\FV@LineBox}{\FancyVerbFormatLine{\FV@ObeyTabs{\FancyVerbFormatText{#1}}}}% - \ifx\FV@Tab\FV@TrueTab@SaveWidths - \let\FV@Tab\FV@TrueTab + \sbox{\FV@LineBox}{% + \FancyVerbFormatLine{% + %\FancyVerbHighlightLine %<-- Default definition using \rlap breaks breaking + {\FV@ObeyTabs{\FancyVerbFormatText{#1}}}}}% + \ifx\FV@Tab\FV@TrueTab + \let\FV@TrueTabSaveWidth\relax \fi \ifdim\wd\FV@LineBox>\linewidth \setcounter{FancyVerbLineBreakLast}{0}% \ifx\FV@Tab\FV@TrueTab - \let\FV@Tab\FV@TrueTab@UseWidths + \let\FV@Tab\FV@TrueTab@UseWidth \setcounter{FV@TrueTabCounter}{0}% \fi \FV@SaveLineBox{#1}% @@ -730,17 +1216,20 @@ \FV@SaveLineBox{#1}}% \FV@LeftListNumber \FV@LeftListFrame - \FancyVerbFormatLine{\usebox{\FV@LineBox}}% + \FancyVerbFormatLine{% + \FancyVerbHighlightLine{\usebox{\FV@LineBox}}}% \FV@RightListFrame \FV@RightListNumber - \ifx\FV@Tab\FV@TrueTab@UseWidths + \ifx\FV@Tab\FV@TrueTab@UseWidth \let\FV@Tab\FV@TrueTab \fi \else \FV@LeftListNumber \FV@LeftListFrame \FancyVerbFormatLine{% - \parbox[t]{\linewidth}{\noindent\strut\FV@ObeyTabs{\FancyVerbFormatText{#1}}\strut}}% + \FancyVerbHighlightLine{% + \parbox[t]{\linewidth}{% + \noindent\strut\FV@ObeyTabs{\FancyVerbFormatText{#1}}\strut}}}% \FV@RightListFrame \FV@RightListNumber \fi}% |