diff --git a/autoload/latex/change.vim b/autoload/latex/change.vim index 98ef420..6775fb3 100644 --- a/autoload/latex/change.vim +++ b/autoload/latex/change.vim @@ -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) diff --git a/autoload/latex/util.vim b/autoload/latex/util.vim index 19dcde7..2b60c30 100644 --- a/autoload/latex/util.vim +++ b/autoload/latex/util.vim @@ -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*{[^}]*}\|\\\@ 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*{[^}]*}\|\\\@