gpoore / minted

minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.
1.73k stars 125 forks source link

Passing additional flags to `pygmentize` / Using local lexers with the `-x` flag #350

Closed schtandard closed 9 months ago

schtandard commented 1 year ago

I sometimes want to use a custom pygments lexer residing in the document directory (i.e. not installed system-wide). This can be achieved using pygmentize's -x option. Of course, minted is calling pygmentize for me, so I have to get it to pass that flag for me. In the past, I could just enter it as part of the "language name". For example, if I have a file named pygmentsmod.py in the document directory in which I define a pygments lexer MyTexLexer, I could say

\mintinline{./pygmentsmod.py:MyTexLexer -x}{\relax}

This no longer works, as \minted@opt@quote now quotes my "language", including the -x. I can still trick minted by using

\mintinline{./pygmentsmod.py:MyTexLexer" -x"}{\relax}

instead (on Windows), but while while I found the previous hack to be somewhat acceptable, this is just ugly.

So, I would like to request

jeeger commented 1 year ago

This used to work, but I can't get it to work with current versions of minted.

jeeger commented 1 year ago

I've successfully modified the old version to work again.

\makeatletter
\newcommand{\minted@addto@optlistclswitch}[2]{%
  \expandafter\def\expandafter#1\expandafter{#1%
    \detokenize{#2}\space}}
\newcommand{\minted@addto@optlistcl@langswitch}[2]{%
  \expandafter\let\expandafter\minted@tmp\csname #1\endcsname
  \expandafter\def\expandafter\minted@tmp\expandafter{\minted@tmp%
    \detokenize{#2}\space}%
  \expandafter\let\csname #1\endcsname\minted@tmp}
\newcommand{\minted@def@optcl@filterswitch}[2]{%
  \define@booleankey{minted@opt@g}{#1}%
  {\minted@addto@optlistclswitch{\minted@optlistcl@g}{#2}{}%
    \@namedef{minted@opt@g:#1}{true}}
  {\minted@addto@optlistclswitch{\minted@optlistcl@g}{}{}%
    \@namedef{minted@opt@g:#1}{false}}
  \define@booleankey{minted@opt@g@i}{#1}%
  {\minted@addto@optlistclswitch{\minted@optlistcl@g@i}{#2}{}%
    \@namedef{minted@opt@g@i:#1}{true}}
  {\minted@addto@optlistclswitch{\minted@optlistcl@g@i}{}{}%
    \@namedef{minted@opt@g@i:#1}{false}}
  \define@booleankey{minted@opt@lang}{#1}%
  {\minted@addto@optlistcl@langswitch{minted@optlistcl@lang\minted@lang}{#2}{}%
    \@namedef{minted@opt@lang\minted@lang:#1}{true}}
  {\minted@addto@optlistcl@langswitch{minted@optlistcl@lang\minted@lang}{}{}%
    \@namedef{minted@opt@lang\minted@lang:#1}{false}}
  \define@booleankey{minted@opt@lang@i}{#1}%
  {\minted@addto@optlistcl@langswitch{minted@optlistcl@lang\minted@lang@i}{#2}{}%
    \@namedef{minted@opt@lang\minted@lang@i:#1}{true}}
  {\minted@addto@optlistcl@langswitch{minted@optlistcl@lang\minted@lang@i}{}{}%
    \@namedef{minted@opt@lang\minted@lang@i:#1}{false}}
  \define@booleankey{minted@opt@cmd}{#1}%
  {\minted@addto@optlistclswitch{\minted@optlistcl@cmd}{#2}{}%
    \@namedef{minted@opt@cmd:#1}{true}}
  {\minted@addto@optlistclswitch{\minted@optlistcl@cmd}{}{}%
    \@namedef{minted@opt@cmd:#1}{false}}
}
\minted@def@optcl@filterswitch{allowlocal}{-x}
\makeatother
\setminted{allowlocal}
gpoore commented 9 months ago

minted version 3.0 is now under development, thanks to a grant from the TeX Users Group. It will include official support for custom lexers. It will also be able to be extended using Python, not just LaTeX macro programming, which will make possible many new lexer-related features. Progress on custom lexers will be tracked in #372. Initial beta releases of minted version 3.0 are expected by early 2024.