Skip to content

Commit

Permalink
feat: add vimtex#syntax#add_to_mathzone_ignore
Browse files Browse the repository at this point in the history
refer: #2929
  • Loading branch information
lervag committed Apr 20, 2024
1 parent 4a0ea3f commit cb3e382
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
26 changes: 21 additions & 5 deletions autoload/vimtex/syntax.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions test/test-syntax/test-custom.tex
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@
\Ac*{arg}
\iacsp{arg}

$\mather{some text}$

\end{document}
11 changes: 11 additions & 0 deletions test/test-syntax/test-custom.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit cb3e382

Please sign in to comment.