From cb3e382f94443a259dd49f575326975e7e289f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 20 Apr 2024 23:48:14 +0200 Subject: [PATCH 1/2] feat: add vimtex#syntax#add_to_mathzone_ignore refer: #2929 --- autoload/vimtex/syntax.vim | 26 +++++++++++++++++++++----- test/test-syntax/test-custom.tex | 2 ++ test/test-syntax/test-custom.vim | 11 +++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) 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() From 268dd8d311adebb121fa0a4b31cdabcb98072403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 20 Apr 2024 23:55:52 +0200 Subject: [PATCH 2/2] doc: document vimtex#syntax#in refer: #2929 --- doc/vimtex.txt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/vimtex.txt b/doc/vimtex.txt index c32566315c..05a3db93ae 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -6248,10 +6248,18 @@ Note: This API is currently a work in progress! the |VimtexEventCompiling|, |VimtexEventCompileFailed| and |VimtexEventCompileSuccess| events. +*vimtex#syntax#in* + `vimtex#syntax#in(name)` -> |Boolean| + `vimtex#syntax#in(name, line, column)` -> |Boolean| + Returns |v:true| if the cursor position or the specified position is inside + the `name`d group. `name` is a regex that is used to matched against the + syntax group stack. + *vimtex#syntax#in_mathzone* - Returns 1 if the position is inside a math zone. If called without - arguments, the position refers to the cursor position. Else must be called - with two arguments: the line number and column number. + `vimtex#syntax#in_mathzone()` -> |Boolean| + `vimtex#syntax#in_mathzone(line, column)` -> |Boolean| + Returns |v:true| if the cursor position or the specified position is inside + a math zone. *vimtex#view#inverse_search* Utility function for reverse search from pdf viewer. Takes two arguments: