1%% LITERAL BLOCKS
2%
3% change this info string if making any custom modification
4\ProvidesFile{sphinxlatexliterals.sty}[2021/01/27 code-blocks and parsed literals]
5
6% Provides support for this output mark-up from Sphinx latex writer:
7%
8% - macros:
9%   - \sphinxLiteralBlockLabel
10%   - \sphinxSetupCaptionForVerbatim
11%   - \sphinxSetupCodeBlockInFootnote
12%   - \sphinxhref
13%   - \sphinxnolinkurl
14%   - \sphinxresetverbatimhllines
15%   - \sphinxunactivateextrasandspace
16%   - \sphinxupquote
17%   - \sphinxurl
18%
19% - environments:
20%   - sphinxVerbatim
21%   - sphinxVerbatimintable
22%   - sphinxalltt
23%
24% Dependency:
25%
26% - hyperref (for \phantomsection and \capstart) (loaded later)
27%
28% Executes \RequirePackage for:
29%
30% - framed
31% - fancyvrb
32% - alltt
33% - upquote
34% - needspace
35
36% also in sphinxlatexadmonitions.sty:
37% This is a workaround to a "feature" of French lists, when literal block
38% follows immediately; usable generally (does only \par then), a priori...
39\providecommand*\sphinxvspacefixafterfrenchlists{%
40   \ifvmode\ifdim\lastskip<\z@ \vskip\parskip\fi\else\par\fi
41}
42
43% For framing allowing pagebreaks
44\RequirePackage{framed}
45% For source code
46% MEMO: fancyvrb is used mainly to
47% 1- control horizontal and vertical spacing
48% 2- optional line numbering
49% 3- optional line emphasizing
50% 4- while still allowing expansion of Pygments latex mark-up
51% Other aspects such as framing, caption handling, codeline wrapping are
52% added on top of it. We should stop using fancyvrb and implement
53% 1, 2, 3, 4 by own Sphinx fully native Verbatim. This would allow to solve
54% limitations with wrapped long code line not allowing page break.
55\RequirePackage{fancyvrb}
56% For parsed-literal blocks.
57\RequirePackage{alltt}
58% Display "real" single quotes in literal blocks.
59\RequirePackage{upquote}
60% Skip to next page if not enough space at bottom
61\RequirePackage{needspace}
62
63% Based on use of "fancyvrb.sty"'s Verbatim.
64% - with framing allowing page breaks ("framed.sty")
65% - with breaking of long lines (exploits Pygments mark-up),
66% - with possibly of a top caption, non-separable by pagebreak.
67% - and usable inside tables or footnotes ("sphinxpackagefootnote.sty").
68
69% for emphasizing lines
70\define@key{FV}{hllines}{\def\sphinx@verbatim@checkifhl##1{\in@{, ##1,}{#1}}}
71% sphinxVerbatim must be usable by third party without requiring hllines set-up
72\def\sphinxresetverbatimhllines{\def\sphinx@verbatim@checkifhl##1{\in@false}}
73\sphinxresetverbatimhllines
74
75% Prior to Sphinx 1.5, \Verbatim and \endVerbatim were modified by Sphinx.
76% The aliases defined here are used in sphinxVerbatim environment and can
77% serve as hook-points with no need to modify \Verbatim itself.
78\let\OriginalVerbatim   \Verbatim
79\let\endOriginalVerbatim\endVerbatim
80
81% for captions of literal blocks
82% at start of caption title
83\newcommand*{\fnum@literalblock}{\literalblockname\nobreakspace\theliteralblock}
84% this will be overwritten in document preamble by Babel translation
85\newcommand*{\literalblockname}{Listing }
86% file extension needed for \caption's good functioning, the file is created
87% only if a \listof{literalblock}{foo} command is encountered, which is
88% analogous to \listoffigures, but for the code listings (foo = chosen title.)
89\newcommand*{\ext@literalblock}{lol}
90
91% if forced use of minipage encapsulation is needed (e.g. table cells)
92\newif\ifsphinxverbatimwithminipage   \sphinxverbatimwithminipagefalse
93
94% Framing macro for use with framed.sty's \FrameCommand
95%  - it obeys current indentation,
96%  - frame is \fboxsep separated from the contents,
97%  - the contents use the full available text width,
98%  - #1 = color of frame, #2 = color of background,
99%  - #3 = above frame, #4 = below frame, #5 = within frame,
100%  - #3 and #4 must be already typeset boxes; they must issue \normalcolor
101%    or similar, else, they are under scope of color #1
102\long\def\spx@fcolorbox #1#2#3#4#5{%
103  \hskip\@totalleftmargin
104  \hskip-\fboxsep\hskip-\fboxrule
105  % use of \color@b@x here is compatible with both xcolor.sty and color.sty
106  \color@b@x {\color{#1}\spx@CustomFBox{#3}{#4}}{\color{#2}}{#5}%
107  \hskip-\fboxsep\hskip-\fboxrule
108  \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth
109}%
110% #1 = for material above frame, such as a caption or a "continued" hint
111% #2 = for material below frame, such as a caption or "continues on next page"
112% #3 = actual contents, which will be typeset with a background color
113\long\def\spx@CustomFBox#1#2#3{%
114  \begingroup
115  \setbox\@tempboxa\hbox{{#3}}% inner braces to avoid color leaks
116  \vbox{#1% above frame
117            % draw frame border _latest_ to avoid pdf viewer issue
118            \kern\fboxrule
119            \hbox{\kern\fboxrule
120                  \copy\@tempboxa
121                  \kern-\wd\@tempboxa\kern-\fboxrule
122                  \vrule\@width\fboxrule
123                  \kern\wd\@tempboxa
124                  \vrule\@width\fboxrule}%
125            \kern-\dimexpr\ht\@tempboxa+\dp\@tempboxa+\fboxrule\relax
126            \hrule\@height\fboxrule
127            \kern\dimexpr\ht\@tempboxa+\dp\@tempboxa\relax
128            \hrule\@height\fboxrule
129        #2% below frame
130       }%
131  \endgroup
132}%
133\def\spx@fcolorbox@put@c#1{% hide width from framed.sty measuring
134    \moveright\dimexpr\fboxrule+.5\wd\@tempboxa\hb@xt@\z@{\hss#1\hss}%
135}%
136\def\spx@fcolorbox@put@r#1{% right align with contents, width hidden
137    \moveright\dimexpr\fboxrule+\wd\@tempboxa-\fboxsep\hb@xt@\z@{\hss#1}%
138}%
139\def\spx@fcolorbox@put@l#1{% left align with contents, width hidden
140    \moveright\dimexpr\fboxrule+\fboxsep\hb@xt@\z@{#1\hss}%
141}%
142%
143\def\sphinxVerbatim@Continued
144   {\csname spx@fcolorbox@put@\spx@opt@verbatimcontinuedalign\endcsname
145    {\normalcolor\sphinxstylecodecontinued\literalblockcontinuedname}}%
146\def\sphinxVerbatim@Continues
147   {\csname spx@fcolorbox@put@\spx@opt@verbatimcontinuesalign\endcsname
148    {\normalcolor\sphinxstylecodecontinues\literalblockcontinuesname}}%
149\def\sphinxVerbatim@Title
150   {\spx@fcolorbox@put@c{\unhcopy\sphinxVerbatim@TitleBox}}%
151\let\sphinxVerbatim@Before\@empty
152\let\sphinxVerbatim@After\@empty
153% Defaults are redefined in document preamble according to language
154\newcommand*\literalblockcontinuedname{continued from previous page}%
155\newcommand*\literalblockcontinuesname{continues on next page}%
156%
157\def\spx@verbatimfcolorbox{\spx@fcolorbox{VerbatimBorderColor}{VerbatimColor}}%
158\def\sphinxVerbatim@FrameCommand
159   {\spx@verbatimfcolorbox\sphinxVerbatim@Before\sphinxVerbatim@After}%
160\def\sphinxVerbatim@FirstFrameCommand
161   {\spx@verbatimfcolorbox\sphinxVerbatim@Before\sphinxVerbatim@Continues}%
162\def\sphinxVerbatim@MidFrameCommand
163   {\spx@verbatimfcolorbox\sphinxVerbatim@Continued\sphinxVerbatim@Continues}%
164\def\sphinxVerbatim@LastFrameCommand
165   {\spx@verbatimfcolorbox\sphinxVerbatim@Continued\sphinxVerbatim@After}%
166
167% For linebreaks inside Verbatim environment from package fancyvrb.
168\newbox\sphinxcontinuationbox
169\newbox\sphinxvisiblespacebox
170\newcommand*\sphinxafterbreak {\copy\sphinxcontinuationbox}
171
172% Take advantage of the already applied Pygments mark-up to insert
173% potential linebreaks for TeX processing.
174%        {, <, #, %, $, ' and ": go to next line.
175%        _, }, ^, &, >, -, ~, and \: stay at end of broken line.
176% Use of \textquotesingle for straight quote.
177% FIXME: convert this to package options ?
178\newcommand*\sphinxbreaksbeforelist {%
179    \do\PYGZob\{\do\PYGZlt\<\do\PYGZsh\#\do\PYGZpc\%% {, <, #, %,
180    \do\PYGZdl\$\do\PYGZdq\"% $, "
181    \def\PYGZsq
182     {\discretionary{}{\sphinxafterbreak\textquotesingle}{\textquotesingle}}% '
183}
184\newcommand*\sphinxbreaksafterlist {%
185    \do\PYGZus\_\do\PYGZcb\}\do\PYGZca\^\do\PYGZam\&% _, }, ^, &,
186    \do\PYGZgt\>\do\PYGZhy\-\do\PYGZti\~% >, -, ~
187    \do\PYGZbs\\% \
188}
189\newcommand*\sphinxbreaksatspecials {%
190  \def\do##1##2%
191     {\def##1{\discretionary{}{\sphinxafterbreak\char`##2}{\char`##2}}}%
192  \sphinxbreaksbeforelist
193  \def\do##1##2%
194     {\def##1{\discretionary{\char`##2}{\sphinxafterbreak}{\char`##2}}}%
195  \sphinxbreaksafterlist
196}
197
198\def\sphinx@verbatim@nolig@list {\do \`}%
199% Some characters . , ; ? ! / are neither pygmentized nor "tex-escaped".
200% This macro makes them "active" and they will insert potential linebreaks.
201% Not compatible with math mode (cf \sphinxunactivateextras).
202\newcommand*\sphinxbreaksbeforeactivelist {}% none
203\newcommand*\sphinxbreaksafteractivelist  {\do\.\do\,\do\;\do\?\do\!\do\/}
204\newcommand*\sphinxbreaksviaactive {%
205   \def\do##1{\lccode`\~`##1%
206    \lowercase{\def~}{\discretionary{}{\sphinxafterbreak\char`##1}{\char`##1}}%
207    \catcode`##1\active}%
208   \sphinxbreaksbeforeactivelist
209   \def\do##1{\lccode`\~`##1%
210    \lowercase{\def~}{\discretionary{\char`##1}{\sphinxafterbreak}{\char`##1}}%
211    \catcode`##1\active}%
212   \sphinxbreaksafteractivelist
213   \lccode`\~`\~
214}
215
216% If the linebreak is at a space, the latter will be displayed as visible
217% space at end of first line, and a continuation symbol starts next line.
218\def\spx@verbatim@space {%
219     \nobreak\hskip\z@skip
220     \discretionary{\copy\sphinxvisiblespacebox}{\sphinxafterbreak}
221                   {\kern\fontdimen2\font}%
222}%
223
224% if the available space on page is less than \literalblockneedspace, insert pagebreak
225\newcommand{\sphinxliteralblockneedspace}{5\baselineskip}
226\newcommand{\sphinxliteralblockwithoutcaptionneedspace}{1.5\baselineskip}
227% The title (caption) is specified from outside as macro \sphinxVerbatimTitle.
228% \sphinxVerbatimTitle is reset to empty after each use of Verbatim.
229\newcommand*\sphinxVerbatimTitle {}
230% This box to typeset the caption before framed.sty multiple passes for framing.
231\newbox\sphinxVerbatim@TitleBox
232% This box to measure contents if nested as inner \MakeFramed requires then
233% minipage encapsulation but too long contents then break outer \MakeFramed
234\newbox\sphinxVerbatim@ContentsBox
235% Holder macro for labels of literal blocks. Set-up by LaTeX writer.
236\newcommand*\sphinxLiteralBlockLabel {}
237\newcommand*\sphinxSetupCaptionForVerbatim [1]
238{%
239    \sphinxvspacefixafterfrenchlists
240    \needspace{\sphinxliteralblockneedspace}%
241% insert a \label via \sphinxLiteralBlockLabel
242% reset to normal the color for the literal block caption
243    \def\sphinxVerbatimTitle
244       {\py@NormalColor\sphinxcaption{\sphinxLiteralBlockLabel #1}}%
245}
246\newcommand*\sphinxSetupCodeBlockInFootnote {%
247  \fvset{fontsize=\footnotesize}\let\caption\sphinxfigcaption
248  \sphinxverbatimwithminipagetrue % reduces vertical spaces
249  % we counteract (this is in a group) the \@normalsize from \caption
250  \let\normalsize\footnotesize\let\@parboxrestore\relax
251  \def\spx@abovecaptionskip{\sphinxverbatimsmallskipamount}%
252}
253\newcommand*{\sphinxverbatimsmallskipamount}{\smallskipamount}
254% serves to implement line highlighting and line wrapping
255\newcommand\sphinxFancyVerbFormatLine[1]{%
256  \expandafter\sphinx@verbatim@checkifhl\expandafter{\the\FV@CodeLineNo}%
257  \ifin@
258     \sphinxVerbatimHighlightLine{#1}%
259  \else
260     \sphinxVerbatimFormatLine{#1}%
261  \fi
262}%
263\newcommand\sphinxVerbatimHighlightLine[1]{%
264  \edef\sphinxrestorefboxsep{\fboxsep\the\fboxsep\relax}%
265  \fboxsep0pt\relax % cf LaTeX bug graphics/4524
266  \colorbox{sphinxVerbatimHighlightColor}%
267           {\sphinxrestorefboxsep\sphinxVerbatimFormatLine{#1}}%
268  % no need to restore \fboxsep here, as this ends up in a \hbox from fancyvrb
269}%
270% \sphinxVerbatimFormatLine will be set locally to one of those two:
271\newcommand\sphinxVerbatimFormatLineWrap{%
272  \hsize\linewidth
273  \ifspx@opt@verbatimforcewraps
274       \expandafter\spx@verb@FormatLineForceWrap
275  \else\expandafter\spx@verb@FormatLineWrap
276  \fi
277}%
278\newcommand\sphinxVerbatimFormatLineNoWrap[1]{\hb@xt@\linewidth{\strut #1\hss}}%
279\long\def\spx@verb@FormatLineWrap#1{%
280    \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
281          \doublehyphendemerits\z@\finalhyphendemerits\z@
282          \strut #1\strut}%
283}%
284%
285% The normal line wrapping allows breaks at spaces and ascii non
286% letters, non digits. The \raggedright above means there will be
287% an overfilled line only if some non-breakable "word" was
288% encountered, which is longer than a line (it is moved always to
289% be on its own on a new line).
290%
291% The "forced" line wrapping will parse the tokens to add potential
292% breakpoints at each character. As some strings are highlighted,
293% we have to apply the highlighting character per character, which
294% requires to manipulate the output of the Pygments LaTeXFormatter.
295%
296% Doing this at latex level is complicated. The contents should
297% be as expected: i.e. some active characters from
298% \sphinxbreaksviaactive, some Pygments character escapes such as
299% \PYGZdl{}, and the highlighting \PYG macro with always 2
300% arguments. No other macros should be there, except perhaps
301% zero-parameter macros. In particular:
302% - the texcomments Pygments option must be set to False
303%
304% With pdflatex, Unicode input gives multi-bytes characters
305% where the first byte is active. We support the "utf8" macros
306% only. "utf8x" is not supported.
307%
308% The highlighting macro \PYG will be applied character per
309% character. Highlighting via a colored background gives thus a
310% chain of small colored boxes which may cause some artefact in
311% some pdf viewers. Can't do anything here if we do want the line
312% break to be possible.
313%
314% First a measurement step is done of what would the standard line
315% wrapping give (i.e line breaks only at spaces and non-letter,
316% non-digit ascii characters), cf TeX by Topic for the basic
317% dissecting technique: TeX unfortunately when building a vertical
318% box does not store in an accessible way what was the maximal
319% line-width during paragraph building.
320%
321% Avoid LaTeX 2021 alteration of \@@par which potentially could break our
322% measurement step (typically if the para/after hook is configured to use
323% \vspace).  Of course, breakage could happen only from user or package
324% adding things to basic Sphinx latex.  And perhaps spring LaTeX 2021 will
325% provide a non-hooked \@@par, but this should work anyway and can't be
326% beaten for speed.
327\ltx@ifundefined{tex_par:D}
328% We could use \@ifl@t@r\fmtversion{2020/02/02}{use \tex_par:D}{use \@@par}.
329  {\let\spx@par\@@par}% \@@par is then expected to be TeX's original \par
330  {\expandafter\let\expandafter\spx@par\csname tex_par:D\endcsname}
331% More hesitation for avoiding the at-start-of-par hooks for our
332% measurement : 1. with old LaTeX, we can not avoid hooks from everyhook
333% or similar packages, 2. and perhaps the hooks add stuff which we should
334% actually measure.  Ideally, hooks are for inserting things in margin
335% which do not change spacing.  Most everything else in fact should not be
336% executed in our scratch box for measurement, such as counter stepping.
337\ltx@ifundefined{tex_everypar:D}
338  {\let\spx@everypar\everypar}
339  {\expandafter\let\expandafter\spx@everypar\csname tex_everypar:D\endcsname}
340%
341% If the max width exceeds the linewidth by more than verbatimmaxoverfull
342% character widths, or if the min width plus verbatimmaxunderfull character
343% widths is inferior to linewidth, then we apply the "force wrapping" with
344% potential line break at each character, else we don't.
345\long\def\spx@verb@FormatLineForceWrap#1{%
346    % \spx@image@box is a scratch box register that we can use here
347    \global\let\spx@verb@maxwidth\z@
348    \global\let\spx@verb@minwidth\linewidth
349    \setbox\spx@image@box
350    \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
351          \doublehyphendemerits\z@\finalhyphendemerits\z@
352          \spx@everypar{}\noindent\strut #1\strut\spx@par
353          \spx@verb@getwidths}%
354    \ifdim\spx@verb@maxwidth>
355          \dimexpr\linewidth+\spx@opt@verbatimmaxoverfull\fontcharwd\font`X \relax
356      \spx@verb@FormatLineWrap{\spx@verb@wrapPYG #1\spx@verb@wrapPYG}%
357    \else
358      \ifdim\spx@verb@minwidth<
359            \dimexpr\linewidth-\spx@opt@verbatimmaxunderfull\fontcharwd\font`X \relax
360        \spx@verb@FormatLineWrap{\spx@verb@wrapPYG #1\spx@verb@wrapPYG}%
361      \else
362        \spx@verb@FormatLineWrap{#1}%
363    \fi\fi
364}%
365% auxiliary paragraph dissector to get max and min widths
366% but minwidth must not take into account the last line
367\newbox\spx@scratchbox
368\def\spx@verb@getwidths {%
369    \unskip\unpenalty
370    \setbox\spx@scratchbox\lastbox
371    \ifvoid\spx@scratchbox
372    \else
373       \setbox\spx@scratchbox\hbox{\unhbox\spx@scratchbox}%
374       \ifdim\spx@verb@maxwidth<\wd\spx@scratchbox
375          \xdef\spx@verb@maxwidth{\number\wd\spx@scratchbox sp}%
376       \fi
377       \expandafter\spx@verb@getwidths@loop
378    \fi
379}%
380\def\spx@verb@getwidths@loop {%
381    \unskip\unpenalty
382    \setbox\spx@scratchbox\lastbox
383    \ifvoid\spx@scratchbox
384    \else
385       \setbox\spx@scratchbox\hbox{\unhbox\spx@scratchbox}%
386       \ifdim\spx@verb@maxwidth<\wd\spx@scratchbox
387          \xdef\spx@verb@maxwidth{\number\wd\spx@scratchbox sp}%
388       \fi
389       \ifdim\spx@verb@minwidth>\wd\spx@scratchbox
390          \xdef\spx@verb@minwidth{\number\wd\spx@scratchbox sp}%
391       \fi
392       \expandafter\spx@verb@getwidths@loop
393    \fi
394}%
395% auxiliary macros to implement "cut long line even in middle of word"
396\catcode`Z=3 % safe delimiter
397\def\spx@verb@wrapPYG{%
398    \futurelet\spx@nexttoken\spx@verb@wrapPYG@i
399}%
400\def\spx@verb@wrapPYG@i{%
401    \ifx\spx@nexttoken\spx@verb@wrapPYG\let\next=\@gobble\else
402    \ifx\spx@nexttoken\PYG\let\next=\spx@verb@wrapPYG@PYG@onebyone\else
403      \discretionary{}{\sphinxafterbreak}{}%
404      \let\next\spx@verb@wrapPYG@ii
405    \fi\fi
406    \next
407}%
408% Let's recognize active characters. We don't support utf8x only utf8.
409% And here #1 should not have picked up (non empty) braced contents
410\long\def\spx@verb@wrapPYG@ii#1{%
411    \ifcat\noexpand~\noexpand#1\relax% active character
412      \expandafter\spx@verb@wrapPYG@active
413    \else % non-active character, control sequence such as \PYGZdl, or empty
414      \expandafter\spx@verb@wrapPYG@one
415    \fi {#1}%
416}%
417\long\def\spx@verb@wrapPYG@active#1{%
418% Let's hope expansion of active character does not really require arguments,
419% as we certainly don't want to go into expanding upfront token stream anyway.
420    \expandafter\spx@verb@wrapPYG@iii#1{}{}{}{}{}{}{}{}{}Z#1%
421}%
422\long\def\spx@verb@wrapPYG@iii#1#2Z{%
423    \ifx\UTFviii@four@octets#1\let\next=\spx@verb@wrapPYG@four\else
424    \ifx\UTFviii@three@octets#1\let\next=\spx@verb@wrapPYG@three\else
425    \ifx\UTFviii@two@octets#1\let\next=\spx@verb@wrapPYG@two\else
426    \let\next=\spx@verb@wrapPYG@one
427    \fi\fi\fi
428    \next
429}%
430\long\def\spx@verb@wrapPYG@one   #1{#1\futurelet\spx@nexttoken\spx@verb@wrapPYG@i}%
431\long\def\spx@verb@wrapPYG@two   #1#2{#1#2\futurelet\spx@nexttoken\spx@verb@wrapPYG@i}%
432\long\def\spx@verb@wrapPYG@three #1#2#3{#1#2#3\futurelet\spx@nexttoken\spx@verb@wrapPYG@i}%
433\long\def\spx@verb@wrapPYG@four  #1#2#3#4{#1#2#3#4\futurelet\spx@nexttoken\spx@verb@wrapPYG@i}%
434% Replace \PYG by itself applied one character at a time! This way breakpoints
435% can be inserted.
436\def\spx@verb@wrapPYG@PYG@onebyone#1#2#3{% #1 = \PYG, #2 = highlight spec, #3 = tokens
437    \def\spx@verb@wrapPYG@PYG@spec{{#2}}%
438    \futurelet\spx@nexttoken\spx@verb@wrapPYG@PYG@i#3Z%
439}%
440\def\spx@verb@wrapPYG@PYG@i{%
441    \ifx\spx@nexttokenZ\let\next=\spx@verb@wrapPYG@PYG@done\else
442      \discretionary{}{\sphinxafterbreak}{}%
443      \let\next\spx@verb@wrapPYG@PYG@ii
444    \fi
445    \next
446}%
447\def\spx@verb@wrapPYG@PYG@doneZ{\futurelet\spx@nexttoken\spx@verb@wrapPYG@i}%
448\long\def\spx@verb@wrapPYG@PYG@ii#1{%
449    \ifcat\noexpand~\noexpand#1\relax% active character
450      \expandafter\spx@verb@wrapPYG@PYG@active
451    \else % non-active character, control sequence such as \PYGZdl, or empty
452      \expandafter\spx@verb@wrapPYG@PYG@one
453    \fi {#1}%
454}%
455\long\def\spx@verb@wrapPYG@PYG@active#1{%
456% Let's hope expansion of active character does not really require arguments,
457% as we certainly don't want to go into expanding upfront token stream anyway.
458    \expandafter\spx@verb@wrapPYG@PYG@iii#1{}{}{}{}{}{}{}{}{}Z#1%
459}%
460\long\def\spx@verb@wrapPYG@PYG@iii#1#2Z{%
461    \ifx\UTFviii@four@octets#1\let\next=\spx@verb@wrapPYG@PYG@four\else
462    \ifx\UTFviii@three@octets#1\let\next=\spx@verb@wrapPYG@PYG@three\else
463    \ifx\UTFviii@two@octets#1\let\next=\spx@verb@wrapPYG@PYG@two\else
464    \let\next=\spx@verb@wrapPYG@PYG@one
465    \fi\fi\fi
466    \next
467}%
468\long\def\spx@verb@wrapPYG@PYG@one#1{%
469    \expandafter\PYG\spx@verb@wrapPYG@PYG@spec{#1}%
470    \futurelet\spx@nexttoken\spx@verb@wrapPYG@PYG@i
471}%
472\long\def\spx@verb@wrapPYG@PYG@two#1#2{%
473    \expandafter\PYG\spx@verb@wrapPYG@PYG@spec{#1#2}%
474    \futurelet\spx@nexttoken\spx@verb@wrapPYG@PYG@i
475}%
476\long\def\spx@verb@wrapPYG@PYG@three#1#2#3{%
477    \expandafter\PYG\spx@verb@wrapPYG@PYG@spec{#1#2#3}%
478    \futurelet\spx@nexttoken\spx@verb@wrapPYG@PYG@i
479}%
480\long\def\spx@verb@wrapPYG@PYG@four#1#2#3#4{%
481    \expandafter\PYG\spx@verb@wrapPYG@PYG@spec{#1#2#3#4}%
482    \futurelet\spx@nexttoken\spx@verb@wrapPYG@PYG@i
483}%
484\catcode`Z 11 %
485%
486\g@addto@macro\FV@SetupFont{%
487    \sbox\sphinxcontinuationbox {\spx@opt@verbatimcontinued}%
488    \sbox\sphinxvisiblespacebox {\spx@opt@verbatimvisiblespace}%
489}%
490\newenvironment{sphinxVerbatim}{%
491  % first, let's check if there is a caption
492  \ifx\sphinxVerbatimTitle\empty
493      \sphinxvspacefixafterfrenchlists
494      \parskip\z@skip
495      \vskip\sphinxverbatimsmallskipamount
496      % there was no caption. Check if nevertheless a label was set.
497      \ifx\sphinxLiteralBlockLabel\empty\else
498      % we require some space to be sure hyperlink target from \phantomsection
499      % will not be separated from upcoming verbatim by a page break
500          \needspace{\sphinxliteralblockwithoutcaptionneedspace}%
501          \phantomsection\sphinxLiteralBlockLabel
502      \fi
503  \else
504     \parskip\z@skip
505     \if t\spx@opt@literalblockcappos
506       \vskip\spx@abovecaptionskip
507       \def\sphinxVerbatim@Before
508           {\sphinxVerbatim@Title\nointerlineskip
509            \kern\dimexpr-\dp\strutbox+\sphinxbelowcaptionspace
510                 % if no frame (code-blocks inside table cells), remove
511                 % the "verbatimsep" whitespace from the top (better visually)
512                 \ifspx@opt@verbatimwithframe\else-\sphinxverbatimsep\fi
513                 % caption package adds \abovecaptionskip vspace, remove it
514                 \spx@ifcaptionpackage{-\abovecaptionskip}{}\relax}%
515     \else
516       \vskip\sphinxverbatimsmallskipamount
517       \def\sphinxVerbatim@After
518          {\nointerlineskip\kern\dimexpr\dp\strutbox
519            \ifspx@opt@verbatimwithframe\else-\sphinxverbatimsep\fi
520            \spx@ifcaptionpackage{-\abovecaptionskip}{}\relax
521           \sphinxVerbatim@Title}%
522     \fi
523     \def\@captype{literalblock}%
524     \capstart
525     % \sphinxVerbatimTitle must reset color
526     \setbox\sphinxVerbatim@TitleBox
527            \hbox{\begin{minipage}{\linewidth}%
528     % caption package may detect wrongly if top or bottom, so we help it
529                    \spx@ifcaptionpackage
530                      {\caption@setposition{\spx@opt@literalblockcappos}}{}%
531                    \sphinxVerbatimTitle
532                  \end{minipage}}%
533  \fi
534  \global\let\sphinxLiteralBlockLabel\empty
535  \global\let\sphinxVerbatimTitle\empty
536  \fboxsep\sphinxverbatimsep \fboxrule\sphinxverbatimborder
537  \ifspx@opt@verbatimwithframe\else\fboxrule\z@\fi
538  \let\FrameCommand     \sphinxVerbatim@FrameCommand
539  \let\FirstFrameCommand\sphinxVerbatim@FirstFrameCommand
540  \let\MidFrameCommand  \sphinxVerbatim@MidFrameCommand
541  \let\LastFrameCommand \sphinxVerbatim@LastFrameCommand
542  \ifspx@opt@verbatimhintsturnover\else
543      \let\sphinxVerbatim@Continued\@empty
544      \let\sphinxVerbatim@Continues\@empty
545  \fi
546  \ifspx@opt@verbatimwrapslines
547  % fancyvrb's Verbatim puts each input line in (unbreakable) horizontal boxes.
548  % This customization wraps each line from the input in a \vtop, thus
549  % allowing it to wrap and display on two or more lines in the latex output.
550  %     - The codeline counter will be increased only once.
551  %     - The wrapped material will not break across pages, it is impossible
552  %       to achieve this without extensive rewrite of fancyvrb.
553  %     - The (not used in sphinx) obeytabs option to Verbatim is
554  %       broken by this change (showtabs and tabspace work).
555    \let\sphinxVerbatimFormatLine\sphinxVerbatimFormatLineWrap
556    \let\FV@Space\spx@verbatim@space
557  % Allow breaks at special characters using \PYG... macros.
558    \sphinxbreaksatspecials
559  % Breaks at punctuation characters . , ; ? ! and / (needs catcode activation)
560    \fvset{codes*=\sphinxbreaksviaactive}%
561  \else % end of conditional code for wrapping long code lines
562    \let\sphinxVerbatimFormatLine\sphinxVerbatimFormatLineNoWrap
563  \fi
564  \let\FancyVerbFormatLine\sphinxFancyVerbFormatLine
565  \VerbatimEnvironment
566  % workaround to fancyvrb's check of current list depth
567  \def\@toodeep {\advance\@listdepth\@ne}%
568  % The list environment is needed to control perfectly the vertical space.
569  % Note: \OuterFrameSep used by framed.sty is later set to \topsep hence 0pt.
570  % - if caption: distance from last text baseline to caption baseline is
571  %   A+(B-F)+\ht\strutbox, A = \abovecaptionskip (default 10pt), B =
572  %   \baselineskip, F is the framed.sty \FrameHeightAdjust macro, default 6pt.
573  %   Formula valid for F < 10pt.
574  % - distance of baseline of caption to top of frame is like for tables:
575  %   \sphinxbelowcaptionspace (=0.5\baselineskip)
576  % - if no caption: distance of last text baseline to code frame is S+(B-F),
577  %   with S = \sphinxverbatimtopskip (=\smallskip)
578  % - and distance from bottom of frame to next text baseline is
579  %   \baselineskip+\parskip.
580  % The \trivlist is used to avoid possible "too deeply nested" error.
581  \itemsep   \z@skip
582  \topsep    \z@skip
583  \partopsep \z@skip
584  % trivlist will set \parsep to \parskip (which itself is set to zero above)
585  % \leftmargin will be set to zero by trivlist
586  \rightmargin\z@
587  \parindent  \z@% becomes \itemindent. Default zero, but perhaps overwritten.
588  \trivlist\item\relax
589     \ifspx@inframed\setbox\sphinxVerbatim@ContentsBox\vbox\bgroup
590        \@setminipage\hsize\linewidth
591        % use bulk of minipage paragraph shape restores (this is needed
592        % in indented contexts, at least for some)
593        \textwidth\hsize \columnwidth\hsize \@totalleftmargin\z@
594        \leftskip\z@skip \rightskip\z@skip  \@rightskip\z@skip
595     \else
596       \ifsphinxverbatimwithminipage\noindent\begin{minipage}{\linewidth}\fi
597       \MakeFramed {% adapted over from framed.sty's snugshade environment
598        \advance\hsize-\width\@totalleftmargin\z@\linewidth\hsize\@setminipage
599        }%
600     \fi
601     % For grid placement from \strut's in \FancyVerbFormatLine
602     \lineskip\z@skip
603     % active comma should not be overwritten by \@noligs
604     \ifspx@opt@verbatimwrapslines
605       \let\verbatim@nolig@list \sphinx@verbatim@nolig@list
606     \fi
607     % will fetch its optional arguments if any
608     \OriginalVerbatim
609}
610{%
611  \endOriginalVerbatim
612  \ifspx@inframed
613   \egroup % finish \sphinxVerbatim@ContentsBox vbox
614   \nobreak % update page totals
615   \ifdim\dimexpr\ht\sphinxVerbatim@ContentsBox+
616         \dp\sphinxVerbatim@ContentsBox+
617         \ht\sphinxVerbatim@TitleBox+
618         \dp\sphinxVerbatim@TitleBox+
619         2\fboxsep+2\fboxrule+
620         % try to account for external frame parameters
621         \FrameSep+\FrameRule+
622         % Usage here of 2 baseline distances is empirical.
623         % In border case where code-block fits barely in remaining space,
624         % it gets framed and looks good but the outer frame may continue
625         % on top of next page and give (if no contents after code-block)
626         % an empty framed line, as testing showed.
627         2\baselineskip+
628         % now add all to accumulated page totals and compare to \pagegoal
629         \pagetotal+\pagedepth>\pagegoal
630    % long contents: do not \MakeFramed. Do make a caption (either before or
631    % after) if title exists. Continuation hints across pagebreaks dropped.
632    % FIXME? a bottom caption may end up isolated at top of next page
633    %        (no problem with a top caption, which is default)
634    \spx@opt@verbatimwithframefalse
635    \def\sphinxVerbatim@Title{\noindent\box\sphinxVerbatim@TitleBox\par}%
636    \sphinxVerbatim@Before
637    \noindent\unvbox\sphinxVerbatim@ContentsBox\par
638    \sphinxVerbatim@After
639   \else
640    % short enough contents: use \MakeFramed. As it is nested, this requires
641    % minipage encapsulation.
642    \noindent\begin{minipage}{\linewidth}%
643     \MakeFramed {% Use it now with the fetched contents
644      \advance\hsize-\width\@totalleftmargin\z@\linewidth\hsize\@setminipage
645      }%
646     \unvbox\sphinxVerbatim@ContentsBox
647     % some of this may be superfluous:
648     \par\unskip\@minipagefalse\endMakeFramed
649    \end{minipage}%
650   \fi
651  \else % non-nested \MakeFramed
652    \par\unskip\@minipagefalse\endMakeFramed % from framed.sty snugshade
653    \ifsphinxverbatimwithminipage\end{minipage}\fi
654  \fi
655  \endtrivlist
656}
657\newenvironment {sphinxVerbatimNoFrame}
658  {\spx@opt@verbatimwithframefalse
659   \VerbatimEnvironment
660   \begin{sphinxVerbatim}}
661  {\end{sphinxVerbatim}}
662\newenvironment {sphinxVerbatimintable}
663  {% don't use a frame if in a table cell
664   \spx@opt@verbatimwithframefalse
665   \sphinxverbatimwithminipagetrue
666   % the literal block caption uses \sphinxcaption which is wrapper of \caption,
667   % but \caption must be modified because longtable redefines it to work only
668   % for the own table caption, and tabulary has multiple passes
669   \let\caption\sphinxfigcaption
670   % reduce above caption skip
671   \def\spx@abovecaptionskip{\sphinxverbatimsmallskipamount}%
672   \VerbatimEnvironment
673   \begin{sphinxVerbatim}}
674  {\end{sphinxVerbatim}}
675
676
677%% PARSED LITERALS
678% allow long lines to wrap like they do in code-blocks
679
680% this should be kept in sync with definitions in sphinx.util.texescape
681\newcommand*\sphinxbreaksattexescapedchars{%
682  \def\do##1##2% put potential break point before character
683     {\def##1{\discretionary{}{\sphinxafterbreak\char`##2}{\char`##2}}}%
684  \do\{\{\do\textless\<\do\#\#\do\%\%\do\$\$% {, <, #, %, $
685  \def\do##1##2% put potential break point after character
686     {\def##1{\discretionary{\char`##2}{\sphinxafterbreak}{\char`##2}}}%
687  \do\_\_\do\}\}\do\textasciicircum\^\do\&\&% _, }, ^, &,
688  \do\textgreater\>\do\textasciitilde\~% >, ~
689  \do\textbackslash\\% \
690}
691\newcommand*\sphinxbreaksviaactiveinparsedliteral{%
692  \sphinxbreaksviaactive % by default handles . , ; ? ! /
693  \lccode`\~`\~ %
694  % update \dospecials as it is used by \url
695  % but deactivation will already have been done hence this is unneeded:
696  %  \expandafter\def\expandafter\dospecials\expandafter{\dospecials
697  %   \sphinxbreaksbeforeactivelist\sphinxbreaksafteractivelist\do\-}%
698}
699\newcommand*\sphinxbreaksatspaceinparsedliteral{%
700  \lccode`~32 \lowercase{\let~}\spx@verbatim@space\lccode`\~`\~
701}
702\newcommand*{\sphinxunactivateextras}{\let\do\@makeother
703      \sphinxbreaksbeforeactivelist\sphinxbreaksafteractivelist}%
704% the \catcode13=5\relax (deactivate end of input lines) is left to callers
705\newcommand*{\sphinxunactivateextrasandspace}{\catcode32=10\relax
706      \sphinxunactivateextras}%
707% now for the modified alltt environment
708\newenvironment{sphinxalltt}
709{% at start of next line to workaround Emacs/AUCTeX issue with this file
710\begin{alltt}%
711 \ifspx@opt@parsedliteralwraps
712   \sbox\sphinxcontinuationbox {\spx@opt@verbatimcontinued}%
713   \sbox\sphinxvisiblespacebox {\spx@opt@verbatimvisiblespace}%
714   \sphinxbreaksattexescapedchars
715   \sphinxbreaksviaactiveinparsedliteral
716   \sphinxbreaksatspaceinparsedliteral
717% alltt takes care of the ' as derivative ("prime") in math mode
718   \everymath\expandafter{\the\everymath\sphinxunactivateextrasandspace
719             \catcode`\<=12\catcode`\>=12\catcode`\^=7\catcode`\_=8 }%
720% not sure if displayed math (align,...) can end up in parsed-literal, anyway
721   \everydisplay\expandafter{\the\everydisplay
722             \catcode13=5 \sphinxunactivateextrasandspace
723             \catcode`\<=12\catcode`\>=12\catcode`\^=7\catcode`\_=8 }%
724 \fi }
725{\end{alltt}}
726
727
728%% INLINE MARK-UP
729%
730
731% Protect \href's first argument in contexts such as sphinxalltt (or
732% \sphinxcode). Sphinx uses \#, \%, \& ... always inside \sphinxhref.
733\protected\def\sphinxhref#1#2{{%
734    \sphinxunactivateextrasandspace % never do \scantokens with active space!
735% for the \endlinechar business, https://github.com/latex3/latex2e/issues/286
736    \endlinechar\m@ne\everyeof{{\endlinechar13 #2}}% keep catcode regime for #2
737    \scantokens{\href{#1}}% normalise it for #1 during \href expansion
738}}
739% Same for \url. And also \nolinkurl for coherence.
740\protected\def\sphinxurl#1{{%
741    \sphinxunactivateextrasandspace\everyeof{}% (<- precaution for \scantokens)
742    \endlinechar\m@ne\scantokens{\url{#1}}%
743}}
744\protected\def\sphinxnolinkurl#1{{%
745    \sphinxunactivateextrasandspace\everyeof{}%
746    \endlinechar\m@ne\scantokens{\nolinkurl{#1}}%
747}}
748
749% \sphinxupquote
750% to obtain straight quotes we execute \@noligs as patched by upquote, and
751% \scantokens is needed in cases where it would be too late for the macro to
752% first set catcodes and then fetch its argument. We also make the contents
753% breakable at non-escaped . , ; ? ! / using \sphinxbreaksviaactive,
754% and also at \ character (which is escaped to \textbackslash{}).
755\protected\def\sphinxtextbackslashbreakbefore
756 {\discretionary{}{\sphinxafterbreak\sphinx@textbackslash}{\sphinx@textbackslash}}
757\protected\def\sphinxtextbackslashbreakafter
758 {\discretionary{\sphinx@textbackslash}{\sphinxafterbreak}{\sphinx@textbackslash}}
759\let\sphinxtextbackslash\sphinxtextbackslashbreakafter
760% the macro must be protected if it ends up used in moving arguments,
761% in 'alltt' \@noligs is done already, and the \scantokens must be avoided.
762\protected\def\sphinxupquote#1{{\def\@tempa{alltt}%
763  \ifx\@tempa\@currenvir\else
764   \ifspx@opt@inlineliteralwraps
765      % break at . , ; ? ! /
766      \sphinxbreaksviaactive
767      % break also at \
768      \setbox8=\hbox{\textbackslash}%
769      \def\sphinx@textbackslash{\copy8}%
770      \let\textbackslash\sphinxtextbackslash
771      % by default, no continuation symbol on next line but may be added
772      \let\sphinxafterbreak\sphinxafterbreakofinlineliteral
773      % do not overwrite the comma set-up
774      \let\verbatim@nolig@list\sphinx@literal@nolig@list
775   \fi
776   % fix a space-gobbling issue due to LaTeX's original \do@noligs
777% TODO: using \@noligs as patched by upquote.sty is now unneeded because
778% either ` and ' are escaped (non-unicode engines) or they don't build
779% ligatures (unicode engines). Thus remove this and unify handling of `, <, >,
780% ' and - with the characters . , ; ? ! / as handled via
781% \sphinxbreaksviaactive.
782% Hence \sphinx@do@noligs will be removed, or rather replaced  with code
783% inserting discretionaries, as they allow a continuation symbol on start of
784% next line to achieve common design with code-blocks.
785   \let\do@noligs\sphinx@do@noligs
786   \@noligs\endlinechar\m@ne\everyeof{}% (<- in case inside \sphinxhref)
787   \expandafter\scantokens
788  \fi {{#1}}}}% extra brace pair to fix end-space gobbling issue...
789\def\sphinx@do@noligs #1{\catcode`#1\active\begingroup\lccode`\~`#1\relax
790    \lowercase{\endgroup\def~{\leavevmode\kern\z@\char`#1 }}}
791\def\sphinx@literal@nolig@list {\do\`\do\<\do\>\do\'\do\-}%
792\let\sphinxafterbreakofinlineliteral\empty
793
794
795\endinput
796