Fixed get_delim()
This commit is contained in:
parent
aad42917a4
commit
bfd99c8c05
@ -142,60 +142,83 @@ function! latex#util#get_env(...)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" {{{1 latex#util#get_delim
|
" {{{1 latex#util#get_delim
|
||||||
function! latex#util#get_delim(...)
|
let s:latex_delim_open = [
|
||||||
let with_pos = a:0 > 0 ? a:1 : 0
|
\ '(',
|
||||||
|
\ '\[',
|
||||||
|
\ '\\{',
|
||||||
|
\ '\\\[',
|
||||||
|
\ '\\\Cleft\s*\%([^\\]\|\\.\|\\\a*\)',
|
||||||
|
\ '\\\cbigg\?\((\|\[\|\\{\)',
|
||||||
|
\ ]
|
||||||
|
let s:latex_delim_close = [
|
||||||
|
\ ')',
|
||||||
|
\ '\]',
|
||||||
|
\ '\\}',
|
||||||
|
\ '\\\]',
|
||||||
|
\ '\\\Cright\s*\%([^\\]\|\\.\|\\\a*\)',
|
||||||
|
\ '\\\cbigg\?\()\|\]\|\\}\)',
|
||||||
|
\ ]
|
||||||
|
function! latex#util#get_delim()
|
||||||
|
"
|
||||||
|
" Note: This functions has not been optimized
|
||||||
|
"
|
||||||
|
|
||||||
let begin_pat = '\C\\begin\_\s*{[^}]*}\|\\\@<!\\\[\|\\\@<!\\('
|
" Save position in order to restore before finishing
|
||||||
let end_pat = '\C\\end\_\s*{[^}]*}\|\\\@<!\\\]\|\\\@<!\\)'
|
let pos_original = getpos('.')
|
||||||
let saved_pos = getpos('.')
|
|
||||||
|
|
||||||
" move to the left until on a backslash
|
" Save position for more work later
|
||||||
let [bufnum, lnum, cnum, off] = getpos('.')
|
let pos_save = getpos('.')
|
||||||
let line = getline(lnum)
|
|
||||||
while cnum > 1 && line[cnum - 1] != '\'
|
|
||||||
let cnum -= 1
|
|
||||||
endwhile
|
|
||||||
call cursor(lnum, cnum)
|
|
||||||
|
|
||||||
" match begin/end pairs but skip comments
|
" Check if the cursor is on top of a closing delimiter
|
||||||
let flags = 'bnW'
|
let close_pats = '\(' . join(s:latex_delim_close, '\|') . '\)'
|
||||||
if strpart(getline('.'), col('.') - 1) =~ '^\%(' . begin_pat . '\)'
|
let lnum = pos_save[1]
|
||||||
let flags .= 'c'
|
let cnum = pos_save[2]
|
||||||
endif
|
let [lnum, cnum] = searchpos(close_pats, 'cbnW', lnum)
|
||||||
let [lnum1, cnum1] = searchpairpos(begin_pat, '', end_pat, flags,
|
let delim = matchstr(getline(lnum), '^'. close_pats, cnum-1)
|
||||||
\ 'latex#util#in_comment()')
|
if pos_save[2] <= (cnum + len(delim) - 1)
|
||||||
|
let pos_save[1] = lnum
|
||||||
let env = ''
|
let pos_save[2] = cnum
|
||||||
|
call setpos('.', pos_save)
|
||||||
if lnum1
|
|
||||||
let line = strpart(getline(lnum1), cnum1 - 1)
|
|
||||||
|
|
||||||
if empty(env)
|
|
||||||
let env = matchstr(line, '^\C\\begin\_\s*{\zs[^}]*\ze}')
|
|
||||||
endif
|
|
||||||
if empty(env)
|
|
||||||
let env = matchstr(line, '^\\\[')
|
|
||||||
endif
|
|
||||||
if empty(env)
|
|
||||||
let env = matchstr(line, '^\\(')
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if with_pos == 1
|
let d1=''
|
||||||
let flags = 'nW'
|
let d2=''
|
||||||
if !(lnum1 == lnum && cnum1 == cnum)
|
let l1=1000000
|
||||||
|
let l2=1000000
|
||||||
|
let c1=1000000
|
||||||
|
let c2=1000000
|
||||||
|
for i in range(len(s:latex_delim_open))
|
||||||
|
call setpos('.', pos_save)
|
||||||
|
let open = s:latex_delim_open[i]
|
||||||
|
let close = s:latex_delim_close[i]
|
||||||
|
let flags = 'W'
|
||||||
|
|
||||||
|
" Check if the cursor is on top of an opening delimiter. If it is not,
|
||||||
|
" then we want to include matches at cursor position to match closing
|
||||||
|
" delimiters.
|
||||||
|
if searchpos(open, 'cn') != pos_save[1:2]
|
||||||
let flags .= 'c'
|
let flags .= 'c'
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let [lnum2, cnum2] = searchpairpos(begin_pat, '', end_pat, flags,
|
" Search for closing delimiter
|
||||||
\ 'latex#util#in_comment()')
|
let pos = searchpairpos(open, '', close, flags, 'latex#util#in_comment()')
|
||||||
|
|
||||||
call setpos('.', saved_pos)
|
" Check if the current is pair is the closest pair
|
||||||
return [env, lnum1, cnum1, lnum2, cnum2]
|
if pos[0] && pos[0]*1000 + pos[1] < l2*1000 + c2
|
||||||
else
|
let l2=pos[0]
|
||||||
call setpos('.', saved_pos)
|
let c2=pos[1]
|
||||||
return env
|
let d2=matchstr(strpart(getline(l2), c2 - 1), close)
|
||||||
endif
|
|
||||||
|
let pos = searchpairpos(open,'',close,'bW', 'latex#util#in_comment()')
|
||||||
|
let l1=pos[0]
|
||||||
|
let c1=pos[1]
|
||||||
|
let d1=matchstr(strpart(getline(l1), c1 - 1), open)
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
|
||||||
|
" Restore cursor position and return delimiters and positions
|
||||||
|
call setpos('.', pos_original)
|
||||||
|
return [d1,l1,c1,d2,l2,c2]
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" {{{1 latex#util#has_syntax
|
" {{{1 latex#util#has_syntax
|
||||||
|
Loading…
Reference in New Issue
Block a user