From 81e152d1edeba826bc7b1ee178a8ad264d8ea349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sun, 11 Aug 2024 23:47:10 +0200 Subject: [PATCH] feat: improve robust_externalize support ref: #2977 --- autoload/vimtex/options.vim | 9 +++++ .../vimtex/syntax/p/robust_externalize.vim | 33 ++++++++++--------- doc/vimtex.txt | 13 ++++++++ test/test-syntax/test-robust-externalize.tex | 10 ++++++ 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/autoload/vimtex/options.vim b/autoload/vimtex/options.vim index f0030846fb..a3ab6ef4d8 100644 --- a/autoload/vimtex/options.vim +++ b/autoload/vimtex/options.vim @@ -392,6 +392,15 @@ function! vimtex#options#init() abort " {{{1 \ 'babel': {'conceal': 1}, \ 'hyperref': {'conceal': 1}, \ 'fontawesome5': {'conceal': 1}, + \ 'robust_externalize': { + \ 'presets': [ + \ ['bash', 'bash'], + \ ['python', 'python'], + \ ['gnuplot', 'gnuplot'], + \ ['tikz', '@texClusterTikz'], + \ ['latex', 'TOP'], + \ ], + \ }, \}) " Disable conceals if chosen diff --git a/autoload/vimtex/syntax/p/robust_externalize.vim b/autoload/vimtex/syntax/p/robust_externalize.vim index 1723bf13de..85b10a47e9 100644 --- a/autoload/vimtex/syntax/p/robust_externalize.vim +++ b/autoload/vimtex/syntax/p/robust_externalize.vim @@ -5,6 +5,8 @@ " function! vimtex#syntax#p#robust_externalize#load(cfg) abort " {{{1 + call vimtex#syntax#packages#load("tikz") + " Match environment boundaries syntax match texRobExtEnvBgn contained '\\begin{\%(RobExt\)\?CacheMeCode\|CacheMe}' \ nextgroup=texRobExtEnvOpt,texRobExtEnvArg skipwhite skipnl @@ -30,30 +32,31 @@ function! vimtex#syntax#p#robust_externalize#load(cfg) abort " {{{1 \}) " Add nested syntax support for supported languages - for [l:preset, l:target] in [ - \ ['c', 'c'], - \ ['bash', 'bash'], - \ ['python', 'python'], - \ ['my python', 'python'], - \] - let l:cluster = vimtex#syntax#nested#include(l:target) - - let l:name = toupper(l:target[0]) . l:target[1:] - let l:grp_env = 'texRobExtZone' . l:name - let l:options = 'keepend' - let l:contains = 'contains=texCmdEnv,texRobExtEnvBgn' - - if empty(l:cluster) + for [l:preset, l:target] in get(a:cfg, "presets", []) + if empty(l:target) + let l:name = 'Verb' + let l:contains = 'contains=texCmdEnv,texRobExtEnvBgn' execute 'highlight def link' l:grp_env 'texRobExtZone' + elseif l:target ==# "TOP" + let l:name = 'LaTeX' + let l:contains = 'contains=TOP,texRobExtZone' else + let l:name = toupper(l:target[0]) . l:target[1:] + let l:cluster = l:target[0] == "@" + \ ? l:target + \ : vimtex#syntax#nested#include(l:target) + + let l:contains = 'contains=texCmdEnv,texRobExtEnvBgn' let l:contains .= ',' . l:cluster endif + let l:grp_env = 'texRobExtZone' . l:name + " Match normal robext environments execute 'syntax region' l:grp_env \ 'start="\\begin{\z(\%(RobExt\)\?CacheMeCode\|CacheMe\)}\_s*{' . l:preset . '[ ,}]"' \ 'end="\\end{\z1}"' - \ l:options + \ 'keepend' \ l:contains endfor diff --git a/doc/vimtex.txt b/doc/vimtex.txt index 8179567451..5f941cad9a 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -2926,6 +2926,10 @@ OPTIONS *vimtex-options* hyperref ~ `conceal` whether to enable conceal; enabled by default + robust_externalize ~ + `presets` list of presets and target syntaxes for things like + `\begin{CacheMeCode}{bash} ... \end{CacheMeCode}. + Default: >vim let g:vimtex_syntax_packages = { @@ -2933,6 +2937,15 @@ OPTIONS *vimtex-options* \ 'babel': {'conceal': 1}, \ 'hyperref': {'conceal': 1}, \ 'fontawesome5': {'conceal': 1}, + \ 'robust_externalize': { + \ 'presets': [ + \ ['bash', 'bash'], + \ ['python', 'python'], + \ ['gnuplot', 'gnuplot'], + \ ['tikz', '@texClusterTikz'], + \ ['latex', 'TOP'], + \ ], + \ }, \} *g:vimtex_texcount_custom_arg* diff --git a/test/test-syntax/test-robust-externalize.tex b/test/test-syntax/test-robust-externalize.tex index ee6925dd13..9c06ad8959 100644 --- a/test/test-syntax/test-robust-externalize.tex +++ b/test/test-syntax/test-robust-externalize.tex @@ -27,6 +27,12 @@ return [x + p for x in ('a', 'b')] \end{RobExtCacheMeCode} +\begin{CacheMe}{tikz}[scale=0.6] + \draw[gray, thick] (-1,2) -- (2,-4); + \draw[gray, thick] (-1,-1) -- (2,2); + \filldraw[black] (0,0) circle (2pt) node[anchor=west]{Intersection point}; +\end{CacheMe} + \begin{CacheMe}{tikzpicture}[scale=0.6] \draw[gray, thick] (-1,2) -- (2,-4); \draw[gray, thick] (-1,-1) -- (2,2); @@ -43,4 +49,8 @@ return [x + p for x in ('a', 'b')] \end{PlaceholderPathFromCode} +\begin{CacheMe}{latex} + This is regular \LaTeX commands. +\end{CacheMe} + \end{document}