Skip to content

Commit

Permalink
[latex] temporary files with common file extensions are now automatic…
Browse files Browse the repository at this point in the history
…ally detected and processed correctly regardless of highlightmode (#401)
  • Loading branch information
gpoore committed Oct 1, 2024
1 parent c1cafea commit 68f6eca
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 5 deletions.
7 changes: 7 additions & 0 deletions latex/CHANGELOG_MINTED_LATEX_PACKAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## v3.1.0 (dev)

* Temporary files with common file extensions are now automatically detected
and processed correctly regardless of `highlightmode` (#401). Previously,
`highlightmode=immediate` was needed for working with temp files that are
overwritten or deleted during compilation; the default
`highlightmode=fastfirst` gave an error message during the first compile
but worked correctly during subsequent compiles.

* Fixed bug when `cache=false`. When caching is disabled, `highlightmode` is
now set to `immediate`.

Expand Down
115 changes: 112 additions & 3 deletions latex/minted/minted.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{minted}
%<*package>
[2024/10/01 v3.1.0dev1 Yet another Pygments shim for LaTeX]
[2024/10/01 v3.1.0dev2 Yet another Pygments shim for LaTeX]
%</package>
%<*driver>
\documentclass{ltxdoc}
Expand Down Expand Up @@ -760,7 +760,9 @@
%
% The default is |fastfirst|. If a cache for the document exists, then code is highlighted immediately. If a cache for the document does not exist, then typeset a placeholder instead of code and highlight all code at the end of the document. This will require a second compile before code is typeset, but because all code is highlighted at once, there is less overhead and the total time required can be significantly less for documents that include many code snippets.
%
% The alternatives are |fast| (always highlight at end of document, requiring a second compile) and |immediate| (always highlight immediately, so no second compile is needed). |immediate| should be used when typesetting code in external temp files that are overwritten during compilation.
% The alternatives are |fast| (always highlight at end of document, requiring a second compile) and |immediate| (always highlight immediately, so no second compile is needed).
%
% Temporary files with the following file extensions are automatically detected and processed correctly, regardless of |highlightmode|: |.listing|, |.out|, |.outfile|, |.output|, |.temp|, |.tempfile|, |.tmp|, |.verb|, and |.vrb|. For temp files with other file extensions, |highlightmode=immediate| is needed if the files are overwritten or deleted during compilation. |fastfirst| can work in such cases, but it will give an error message about modified or missing files during the first compile, and then will work correctly during subsequent compiles when it switches to |immediate| mode.
%
% When code is highlighted at the end of the document with |fast| or |fastfirst|, any error and warning messages will refer to a location at the end of the document rather than the original code location, since highlighting occurred at the end of the document. In this case, messages are supplemented with original \LaTeX\ source file names and line numbers to aid in debugging.
%
Expand Down Expand Up @@ -3666,7 +3668,8 @@
\input{\minted@highlightfilepath}}%
\minted@addcachefilename{\minted@highlightfilename}}%
{\ifbool{minted@canexec}%
{\minted@highlight@create}%
{\minted@iffasthighlightmode@buffertempfile
\minted@highlight@create}%
{\minted@error{Cannot highlight code (minted executable is unavailable or
disabled); attempting to typeset without highlighting}%
\minted@highlight@fallback}}}%
Expand Down Expand Up @@ -3736,6 +3739,112 @@
% \end{macro}
%
%
% \begin{macro}{\minted@iffasthighlightmode@buffertempfile}
%
% With caching, when |fasthighlightmode=true| and |\minted@inputfilepath| has a file extension that has been designated for temp files, read the file into a temp buffer, and then if that succeeds copy the code into the highlighting buffer. This can avoid errors with temp files that are modified or deleted before highlighting occurs when |fasthighlightmode=true|, since that delays highlighting until the end of the document.
%
% This is not done for all highlighted external files because it adds overhead and complexity. When files are read, it is not possible to determine the newline sequence (|\n| versus |\r\n|), and trailing whitespace is discarded by \TeX\ during the reading process, so it is not possible to reconstruct the original file bytes within \TeX, only an (essentially equivalent) approximation. As a result, files that are read are hashed a second time after reading to reduce the chance that they were modified after initial hashing but before reading.
%
% \begin{macrocode}
\begingroup
\def\minted@set@tempfileextension#1{%
\if\relax\detokenize{#1}\relax
\else
\expandafter\global\expandafter
\let\csname minted@buffertempfileextension@#1\endcsname\relax
\fi}
\minted@forcsvlist{\minted@set@tempfileextension}{
listing,
out,
outfile,
output,
temp,
tempfile,
tmp,
verb,
vrb,
}
\endgroup
\begingroup
\catcode`\/=12
\catcode`\.=12
\gdef\minted@iffasthighlightmode@buffertempfile{%
\ifbool{minted@fasthighlightmode}%
{\ifcsname minted@inputfilepath\endcsname
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{\expandafter
\minted@iffasthighlightmode@buffertempfile@i\minted@inputfilepath/\FV@Sentinel}}%
{}}
\gdef\minted@iffasthighlightmode@buffertempfile@i#1/#2\FV@Sentinel{%
\if\relax\detokenize{#2}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\minted@iffasthighlightmode@buffertempfile@ii#1.\FV@Sentinel}%
{\minted@iffasthighlightmode@buffertempfile@i#2\FV@Sentinel}}
\gdef\minted@iffasthighlightmode@buffertempfile@ii#1.#2\FV@Sentinel{%
\if\relax\detokenize{#2}\relax
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\minted@iffasthighlightmode@buffertempfile@iii#2\FV@Sentinel}}
\gdef\minted@iffasthighlightmode@buffertempfile@iii#1.\FV@Sentinel{%
\ifcsname minted@buffertempfileextension@#1\endcsname
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{\minted@iffasthighlightmode@buffertempfile@iv}}
\endgroup
\def\minted@iffasthighlightmode@buffertempfile@iv{%
\begingroup
\setcounter{minted@tmpcodebufferlength}{0}%
\newread\minted@intempfile
\openin\minted@intempfile=\minted@inputfilepath
\let\do\@makeother\FVExtraDoSpecials
\loop\unless\ifeof\minted@intempfile
\read\minted@intempfile to\minted@intempfileline
\stepnewcounter{minted@tmpcodebufferlength}%
\expandafter\global\expandafter\let\csname
minted@tmpcodebufferline\arabic{minted@tmpcodebufferlength}%
\endcsname\minted@intempfileline
\repeat
\closein\minted@intempfile
\endgroup
\edef\minted@inputfilemdfivesum@check{\pdf@filemdfivesum{\minted@inputfilepath}}%
\ifx\minted@inputfilemdfivesum@check\minted@inputfilemdfivesum
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\VerbatimClearBuffer[buffername=minted@tmpcodebuffer]}%
\ifnum\value{minted@tmpcodebufferlength}>0\relax
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{\minted@iffasthighlightmode@buffertempfile@v}}
\def\minted@iffasthighlightmode@buffertempfile@v{%
\pydatabufferkey{code}%
\pydatabuffermlvaluestart
\setcounter{minted@tmpcnt}{1}%
\loop\unless\ifnum\value{minted@tmpcnt}>\value{minted@tmpcodebufferlength}\relax
\expandafter\let\expandafter
\minted@tmp\csname minted@tmpcodebufferline\arabic{minted@tmpcnt}\endcsname
\expandafter\pydatabuffermlvalueline\expandafter{\minted@tmp}%
\stepcounter{minted@tmpcnt}%
\repeat
\pydatabuffermlvalueend
\VerbatimClearBuffer[buffername=minted@tmpcodebuffer]}
% \end{macrocode}
% \end{macro}
%
%
%
% \subsection{Public API}
%
Expand Down
100 changes: 98 additions & 2 deletions latex/minted/minted.sty
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{minted}
[2024/10/01 v3.1.0dev1 Yet another Pygments shim for LaTeX]
[2024/10/01 v3.1.0dev2 Yet another Pygments shim for LaTeX]
\RequirePackage{catchfile}
\RequirePackage{etoolbox}
\RequirePackage{fvextra}[2024/09/05]
Expand Down Expand Up @@ -1342,7 +1342,8 @@
\input{\minted@highlightfilepath}}%
\minted@addcachefilename{\minted@highlightfilename}}%
{\ifbool{minted@canexec}%
{\minted@highlight@create}%
{\minted@iffasthighlightmode@buffertempfile
\minted@highlight@create}%
{\minted@error{Cannot highlight code (minted executable is unavailable or
disabled); attempting to typeset without highlighting}%
\minted@highlight@fallback}}}%
Expand Down Expand Up @@ -1408,6 +1409,101 @@
\minted@highlight@fallback}}}
\def\minted@highlight@fallback{%
\minted@insertplaceholder}
\begingroup
\def\minted@set@tempfileextension#1{%
\if\relax\detokenize{#1}\relax
\else
\expandafter\global\expandafter
\let\csname minted@buffertempfileextension@#1\endcsname\relax
\fi}
\minted@forcsvlist{\minted@set@tempfileextension}{
listing,
out,
outfile,
output,
temp,
tempfile,
tmp,
verb,
vrb,
}
\endgroup
\begingroup
\catcode`\/=12
\catcode`\.=12
\gdef\minted@iffasthighlightmode@buffertempfile{%
\ifbool{minted@fasthighlightmode}%
{\ifcsname minted@inputfilepath\endcsname
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{\expandafter
\minted@iffasthighlightmode@buffertempfile@i\minted@inputfilepath/\FV@Sentinel}}%
{}}
\gdef\minted@iffasthighlightmode@buffertempfile@i#1/#2\FV@Sentinel{%
\if\relax\detokenize{#2}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\minted@iffasthighlightmode@buffertempfile@ii#1.\FV@Sentinel}%
{\minted@iffasthighlightmode@buffertempfile@i#2\FV@Sentinel}}
\gdef\minted@iffasthighlightmode@buffertempfile@ii#1.#2\FV@Sentinel{%
\if\relax\detokenize{#2}\relax
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\minted@iffasthighlightmode@buffertempfile@iii#2\FV@Sentinel}}
\gdef\minted@iffasthighlightmode@buffertempfile@iii#1.\FV@Sentinel{%
\ifcsname minted@buffertempfileextension@#1\endcsname
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{\minted@iffasthighlightmode@buffertempfile@iv}}
\endgroup
\def\minted@iffasthighlightmode@buffertempfile@iv{%
\begingroup
\setcounter{minted@tmpcodebufferlength}{0}%
\newread\minted@intempfile
\openin\minted@intempfile=\minted@inputfilepath
\let\do\@makeother\FVExtraDoSpecials
\loop\unless\ifeof\minted@intempfile
\read\minted@intempfile to\minted@intempfileline
\stepnewcounter{minted@tmpcodebufferlength}%
\expandafter\global\expandafter\let\csname
minted@tmpcodebufferline\arabic{minted@tmpcodebufferlength}%
\endcsname\minted@intempfileline
\repeat
\closein\minted@intempfile
\endgroup
\edef\minted@inputfilemdfivesum@check{\pdf@filemdfivesum{\minted@inputfilepath}}%
\ifx\minted@inputfilemdfivesum@check\minted@inputfilemdfivesum
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\VerbatimClearBuffer[buffername=minted@tmpcodebuffer]}%
\ifnum\value{minted@tmpcodebufferlength}>0\relax
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{\minted@iffasthighlightmode@buffertempfile@v}}
\def\minted@iffasthighlightmode@buffertempfile@v{%
\pydatabufferkey{code}%
\pydatabuffermlvaluestart
\setcounter{minted@tmpcnt}{1}%
\loop\unless\ifnum\value{minted@tmpcnt}>\value{minted@tmpcodebufferlength}\relax
\expandafter\let\expandafter
\minted@tmp\csname minted@tmpcodebufferline\arabic{minted@tmpcnt}\endcsname
\expandafter\pydatabuffermlvalueline\expandafter{\minted@tmp}%
\stepcounter{minted@tmpcnt}%
\repeat
\pydatabuffermlvalueend
\VerbatimClearBuffer[buffername=minted@tmpcodebuffer]}
\newcommand{\setminted}[2][]{%
\ifstrempty{#1}%
{\pgfkeys{/minted/global/.cd,#2}}%
Expand Down

0 comments on commit 68f6eca

Please sign in to comment.