Skip to content

Commit

Permalink
feat: improve robust_externalize support
Browse files Browse the repository at this point in the history
ref: #2977
  • Loading branch information
lervag committed Aug 11, 2024
1 parent 7f6817c commit 81e152d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
9 changes: 9 additions & 0 deletions autoload/vimtex/options.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 18 additions & 15 deletions autoload/vimtex/syntax/p/robust_externalize.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
13 changes: 13 additions & 0 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2926,13 +2926,26 @@ 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 = {
\ 'amsmath': {'conceal': 1, 'load': 2},
\ '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*
Expand Down
10 changes: 10 additions & 0 deletions test/test-syntax/test-robust-externalize.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}

0 comments on commit 81e152d

Please sign in to comment.