Updated syntax parser utilities

This commit is contained in:
Karl Yngve Lervåg 2016-01-21 21:11:11 +01:00
parent 1aedd5a746
commit 45733b79af
2 changed files with 26 additions and 12 deletions

View File

@ -184,7 +184,7 @@ function! vimtex#motion#find_matching_pair(...) " {{{1
if delim =~# '^\$'
let inline = s:notcomment . s:notbslash . '\$'
let [lnum0, cnum0] = searchpos('.', 'nW')
if lnum0 && vimtex#util#has_syntax('texMathZoneX', lnum0, cnum0)
if lnum0 && vimtex#util#in_syntax('texMathZoneX', lnum0, cnum0)
let [lnum2, cnum2] = searchpos(inline, 'nW', 0, 200)
else
let [lnum2, cnum2] = searchpos(not_cursor . inline, 'bnW', 0, 200)
@ -353,14 +353,14 @@ function! vimtex#motion#sel_inline_math(...) " {{{1
let l:dollar = 0
let l:dollar_pat = '\\\@<!\$'
if vimtex#util#has_syntax('texMathZoneX')
if vimtex#util#in_syntax('texMathZoneX')
let l:dollar = 1
let l:pattern = [l:dollar_pat, l:dollar_pat]
let l:flags .= 'c'
elseif getline('.')[col('.') - 1] ==# '$'
let l:dollar = 1
let l:pattern = [l:dollar_pat, l:dollar_pat]
elseif vimtex#util#has_syntax('texMathZoneV')
elseif vimtex#util#in_syntax('texMathZoneV')
let l:pattern = ['\\(', '\\)']
let l:flags .= 'c'
elseif getline('.')[col('.') - 2:col('.') - 1] ==# '\)'
@ -428,7 +428,7 @@ function! s:highlight_matching_pair(...) " {{{1
" Match inline math
"
let [lnum0, cnum0] = searchpos('.', 'nW')
if lnum0 && vimtex#util#has_syntax('texMathZoneX', lnum0, cnum0)
if lnum0 && vimtex#util#in_syntax('texMathZoneX', lnum0, cnum0)
let [lnum2, cnum2] = searchpos(s:notcomment . s:notbslash . '\$',
\ 'nW', line('w$'), 200)
else

View File

@ -282,17 +282,31 @@ function! vimtex#util#get_os() " {{{1
endfunction
" }}}1
function! vimtex#util#has_syntax(name, ...) " {{{1
" Usage: vimtex#util#has_syntax(name, [line], [col])
let line = a:0 >= 1 ? a:1 : line('.')
let col = a:0 >= 2 ? a:2 : col('.')
return 0 <= index(map(synstack(line, col),
\ 'synIDattr(v:val, "name") == "' . a:name . '"'), 1)
function! vimtex#util#in_comment(...) " {{{1
return call('vimtex#util#in_syntax', ['texComment'] + a:000)
endfunction
" }}}1
function! vimtex#util#in_comment(...) " {{{1
return synIDattr(synID(line('.'), col('.'), 0), 'name') =~# '^texComment'
function! vimtex#util#in_mathzone(...) " {{{1
return call('vimtex#util#in_syntax', ['texMathZone'] + a:000)
endfunction
" }}}1
function! vimtex#util#in_syntax(name, ...) " {{{1
" Usage: vimtex#util#in_syntax(name, [line, col])
" Get position and correct it if necessary
let l:pos = a:0 > 0 ? [a:1, a:2] : [line('.'), col('.')]
if mode() ==# 'i'
let l:pos[1] -= 1
endif
call map(l:pos, 'max([v:val, 1])')
" Check syntax at position
return match(map(synstack(l:pos[0], l:pos[1]),
\ "synIDattr(v:val, 'name')"),
\ '^' . a:name) >= 0
endfunction
" }}}1