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