diff --git a/autoload/vimtex/syntax.vim b/autoload/vimtex/syntax.vim index 68229b879a..b66e699cdd 100644 --- a/autoload/vimtex/syntax.vim +++ b/autoload/vimtex/syntax.vim @@ -26,13 +26,29 @@ endfunction " }}}1 function! vimtex#syntax#in_mathzone(...) abort " {{{1 - " The following checks if we are inside a texMathZone environment. The - " arguments to \label{...}, the texRefArg group, and \text{...} like - " commands, the texMathTextArg group, are actively ignored as these should - " not be considered to be math environments. let l:groups = reverse(call('vimtex#syntax#stack', a:000)) - let l:group = matchstr(l:groups, '\v^tex%(Math%(Zone|Text|Tag)|RefArg)') + let l:group = matchstr(l:groups, s:__mathzone_regex) return l:group =~# '^texMathZone' endfunction +" This specifies matchers that are combined for finding the group used to +" determine if we are in a mathzone. The first entry is `texMathZone`, which +" indicates that we are in a mathzone. The other entries are groups that +" indicate specifically that we are NOT in a mathzone. The entries here are +" part of the core spec. Extensions can register more groups that should be +" ignored with vimtex#syntax#register_mathzone_ignore. +let s:__mathzone_matchers = [ + \ 'texMathZone', + \ 'texMathText', + \ 'texMathTag', + \ 'texRefArg', + \] +let s:__mathzone_regex = '^\%(' . join(s:__mathzone_matchers, '\|') . '\)' + +" }}}1 +function! vimtex#syntax#add_to_mathzone_ignore(regex) abort " {{{1 + let s:__mathzone_matchers += [a:regex] + let s:__mathzone_regex = '^\%(' . join(s:__mathzone_matchers, '\|') . '\)' +endfunction + " }}}1 diff --git a/test/test-syntax/test-custom.tex b/test/test-syntax/test-custom.tex index 625dc6e48e..dbeb09aa1b 100644 --- a/test/test-syntax/test-custom.tex +++ b/test/test-syntax/test-custom.tex @@ -28,4 +28,6 @@ \Ac*{arg} \iacsp{arg} +$\mather{some text}$ + \end{document} diff --git a/test/test-syntax/test-custom.vim b/test/test-syntax/test-custom.vim index a6858dde47..0858aff76e 100644 --- a/test/test-syntax/test-custom.vim +++ b/test/test-syntax/test-custom.vim @@ -17,10 +17,21 @@ let g:vimtex_syntax_custom_cmds = [ \ {'name': 'mygls', 'argspell': 0}, \ {'name': 'slurp', 'argspell': 0, 'arggreedy': v:true}, \ {'name': 'regex', 'cmdre': '[iI]?[aA]c[slaf]?p?\*?', 'conceal': 1}, + \ {'name': 'mather', 'mathmode': 1, + \ 'nextgroup': 'texMatherArg', 'hlgroup': 'texOpt'}, \] +call vimtex#syntax#add_to_mathzone_ignore('texMatherArg') + EditConcealed test-custom.tex +call vimtex#syntax#core#new_arg('texMatherArg', { + \ 'opts': 'contained keepend' + \}) + if empty($INMAKE) | finish | endif +call assert_true(vimtex#syntax#in_mathzone(31, 5)) +call assert_false(vimtex#syntax#in_mathzone(31, 15)) + call vimtex#test#finished()