Began on get_delim and change_delim

This commit is contained in:
Karl Yngve Lervåg 2013-10-09 00:02:06 +02:00
parent d321623bfc
commit 399a340b07
2 changed files with 69 additions and 4 deletions

View File

@ -7,6 +7,18 @@ function! latex#change#init(initialized)
endif
endfunction
" {{{1 latex#change#delim
function! latex#change#delim(open, close)
let [d1, l1, c1, d2, l2, c2] = latex#util#get_delim(1)
let line = getline(l1)
let line = strpart(line, 0, c1 - 1) . a:open . strpart(line, c1 + len(d1))
call setline(l1, line)
let line = getline(l2)
let line = strpart(line, 0, c2 - 1) . a:close . strpart(line, c2 + len(d2))
call setline(l2, line)
endfunction
" {{{1 latex#change#env
function! latex#change#env(new_env)
let [env, l1, c1, l2, c2] = latex#util#get_env(1)

View File

@ -86,11 +86,64 @@ function! latex#util#get_env(...)
" if with_pos is not given
" - [environment, lnum_begin, cnum_begin, lnum_end, cnum_end]
" if with_pos is nonzero
if a:0 > 0
let with_pos = a:1
else
let with_pos = 0
let with_pos = a:0 > 0 ? a:1 : 0
let begin_pat = '\C\\begin\_\s*{[^}]*}\|\\\@<!\\\[\|\\\@<!\\('
let end_pat = '\C\\end\_\s*{[^}]*}\|\\\@<!\\\]\|\\\@<!\\)'
let saved_pos = getpos('.')
" move to the left until on a backslash
let [bufnum, lnum, cnum, off] = 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
let flags = 'bnW'
if strpart(getline('.'), col('.') - 1) =~ '^\%(' . begin_pat . '\)'
let flags .= 'c'
endif
let [lnum1, cnum1] = searchpairpos(begin_pat, '', end_pat, flags,
\ 'latex#util#in_comment()')
let env = ''
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
if with_pos == 1
let flags = 'nW'
if !(lnum1 == lnum && cnum1 == cnum)
let flags .= 'c'
endif
let [lnum2, cnum2] = searchpairpos(begin_pat, '', end_pat, flags,
\ 'latex#util#in_comment()')
call setpos('.', saved_pos)
return [env, lnum1, cnum1, lnum2, cnum2]
else
call setpos('.', saved_pos)
return env
endif
endfunction
" {{{1 latex#util#get_delim
function! latex#util#get_delim(...)
let with_pos = a:0 > 0 ? a:1 : 0
let begin_pat = '\C\\begin\_\s*{[^}]*}\|\\\@<!\\\[\|\\\@<!\\('
let end_pat = '\C\\end\_\s*{[^}]*}\|\\\@<!\\\]\|\\\@<!\\)'