Fix #493: Improved highlight matching delims

This commit is contained in:
Karl Yngve Lervåg 2016-07-18 22:15:43 +02:00
parent 8dd4ed77db
commit a80a2600b0
2 changed files with 20 additions and 37 deletions

View File

@ -408,6 +408,8 @@ function! vimtex#delim#get_matching(delim) " {{{1
let l:matching.corr = a:delim.match
let l:matching.side = a:delim.is_open ? 'close' : 'open'
let l:matching.is_open = !a:delim.is_open
let l:matching.re.corr = a:delim.re.this
let l:matching.re.this = a:delim.re.corr
if l:matching.type ==# 'delim'
let l:matching.corr_delim = a:delim.delim

View File

@ -183,49 +183,30 @@ endfunction
" }}}1
function! s:highlight_matching_pair() " {{{1
if exists('*matchaddpos')
call s:highlight_matching_pair_new()
else
call s:highlight_matching_pair_old()
endif
endfunction
" }}}1
function! s:highlight_matching_pair_new() " {{{1
if exists('s:match_id')
call matchdelete(s:match_id)
unlet s:match_id
if exists('s:match_id1')
call matchdelete(s:match_id1)
call matchdelete(s:match_id2)
unlet s:match_id1
unlet s:match_id2
endif
if vimtex#util#in_comment() | return | endif
let l:d1 = vimtex#delim#get_current('all', 'both')
if empty(l:d1) | return | endif
let l:current = vimtex#delim#get_current('all', 'both')
if empty(l:current) | return | endif
let l:d2 = vimtex#delim#get_matching(l:d1)
if empty(l:d2) | return | endif
let l:corresponding = vimtex#delim#get_matching(l:current)
if empty(l:corresponding) | return | endif
let s:match_id = matchaddpos('MatchParen',
\ [[l:d1.lnum, l:d1.cnum, strlen(l:d1.match)],
\ [l:d2.lnum, l:d2.cnum, strlen(l:d2.match)]])
endfunction
let [l:o, l:c] = l:current.is_open
\ ? [l:current, l:corresponding]
\ : [l:corresponding, l:current]
" }}}1
function! s:highlight_matching_pair_old() " {{{1
2match none
if vimtex#util#in_comment() | return | endif
let l:d1 = vimtex#delim#get_current('all', 'both')
if empty(l:d1) | return | endif
let l:d2 = vimtex#delim#get_matching(l:d1)
if empty(l:d2) | return | endif
let [l1, c1, l2, c2] = l:d1.side ==# 'open'
\ ? [l:d1.lnum, l:d1.cnum, l:d2.lnum, l:d2.cnum]
\ : [l:d2.lnum, l:d2.cnum, l:d1.lnum, l:d1.cnum]
execute '2match MatchParen /'
\ . '\%' . l1 . 'l\%' . c1 . 'c' . l:d1.re.open
\ . '\|\%' . l2 . 'l\%' . c2 . 'c' . l:d1.re.close . '/'
let s:match_id1 = matchadd('MatchParen',
\ '\%' . l:o.lnum . 'l\%' . l:o.cnum . 'c' . l:o.re.this . '\ze\_.*'
\ . '\%' . l:c.lnum . 'l\%' . l:c.cnum . 'c' . l:c.re.this)
let s:match_id2 = matchadd('MatchParen',
\ '\%' . l:o.lnum . 'l\%' . l:o.cnum . 'c' . l:o.re.this . '\_.*\zs'
\ . '\%' . l:c.lnum . 'l\%' . l:c.cnum . 'c' . l:c.re.this)
endfunction
" }}}1