From 5a4664ff4161183be93192803f6aa1b0a9e2fa4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Wed, 17 Jun 2015 21:05:40 +0200 Subject: [PATCH] Add support for minted package Supports c, csharp and python out of the box. More languages may be added to the list `g:vimtex_syntax_minted` (see help for more info). Resolves: #167 --- after/syntax/tex.vim | 48 +++++++++++++++++++++++++++ doc/vimtex.txt | 20 ++++++++++- test/test-syntax/test-syntax.tex | 57 ++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 test/test-syntax/test-syntax.tex diff --git a/after/syntax/tex.vim b/after/syntax/tex.vim index a0915fc..ea24bf6 100644 --- a/after/syntax/tex.vim +++ b/after/syntax/tex.vim @@ -61,6 +61,54 @@ syntax region texZone \ contains=texBeginEnd,@DOT let b:current_syntax = 'tex' +" }}}1 +" {{{1 Nested syntax highlighting for minted +let s:minted = get(g:, 'vimtex_syntax_minted', [ + \ { + \ 'lang' : 'c', + \ }, + \ { + \ 'lang' : 'csharp', + \ 'syntax' : 'cs' + \ }, + \ { + \ 'lang' : 'python', + \ 'ignore' : [ + \ 'pythonEscape', + \ 'pythonBEscape', + \ ], + \ } + \]) + +for entry in s:minted + let lang = entry.lang + let syntax = get(entry, 'syntax', lang) + + unlet b:current_syntax + execute 'syntax include @' . toupper(lang) 'syntax/' . syntax . '.vim' + + if has_key(entry, 'ignore') + execute 'syntax cluster' toupper(lang) + \ 'remove=' . join(entry.ignore, ',') + endif + + execute 'syntax region texZone' + \ 'start="\\begin{minted}\_[^}]\{-}{' . lang . '}"rs=s' + \ 'end="\\end{minted}"re=e' + \ 'keepend' + \ 'transparent' + \ 'contains=texMinted,@' . toupper(lang) +endfor +let b:current_syntax = 'tex' + +syntax match texMinted "\\begin{minted}\_[^}]\{-}{\w\+}" + \ contains=texBeginEnd,texMintedName +syntax match texMinted "\\end{minted}" + \ contains=texBeginEnd +syntax match texMintedName "{\w\+}" + +highlight link texMintedName texBeginEndName + " }}}1 " vim: fdm=marker sw=2 diff --git a/doc/vimtex.txt b/doc/vimtex.txt index d400927..e994945 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -659,6 +659,23 @@ Options~ Default value: '' +*g:vimtex_syntax_minted* + A list that contains dictionaries for each supported language. A language + dictionary should be something similar to the following: > + + { + 'lang' : NAME, + 'syntax' : SYNTAX-FILE-NAME, + 'ignored' : IGNORED-GROUPS + } +< + If the `syntax` entry is not present, then the `lang` entry is used as name of + the syntax file. The `ignored` entry is also optional, and is used to name + syntax groups to ignore in the minted region. + + Default value: + Please see `VIMTEX/after/syntax/tex.vim` for the default value. + ------------------------------------------------------------------------------ Commands~ *vimtex-commands* @@ -998,8 +1015,9 @@ highlighting. However, |vimtex| does add some minor improvements: - Support for `biblatex` and `natbib` package - Support for `cleveref` package -- Support for `listings` package +- Improved highlighting of `listings` package - Nested syntax highlighting for `dot2tex` +- Nested syntax highlighting for `minted` (see |g:vimtex_syntax_minted|) ============================================================================== NAVIGATION *vimtex-navigation* diff --git a/test/test-syntax/test-syntax.tex b/test/test-syntax/test-syntax.tex new file mode 100644 index 0000000..500f5bb --- /dev/null +++ b/test/test-syntax/test-syntax.tex @@ -0,0 +1,57 @@ +\documentclass{article} +\usepackage{minted} +\begin{document} + +\begin{lstlisting} +testing +\end{lstlisting} + +\begin{align} + f(x) = 0 +\end{align} + +\begin{equation} + f(x) = 0 +\end{equation} + +\begin{quote} + test +\end{quote} + +\begin{dot2tex} + graph graphname { + a -- b; + b -- c; + b -- d; + d -- a; + } +\end{dot2tex} + +\begin{minted}{python} +def function(arg): + pass +\end{minted} + +\begin{minted}{c} +int main() { + printf("hello, world"); + return 0; +} +\end{minted} + +\begin{minted}[mathescape, + linenos, + numbersep=5pt, + gobble=2, + frame=lines, + framesep=2mm]{csharp} +string title = "This is a Unicode π in the sky" +/* +Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeter +of an $n$-sided regular polygon circumscribing a +circle of diameter $d$. +*/ +const double pi = 3.1415926535 +\end{minted} + +\end{document}