diff --git a/autoload/vimtex/text_obj.vim b/autoload/vimtex/text_obj.vim index f3ec91b..f13f895 100644 --- a/autoload/vimtex/text_obj.vim +++ b/autoload/vimtex/text_obj.vim @@ -16,61 +16,32 @@ endfunction function! vimtex#text_obj#init_buffer() " {{{1 if !g:vimtex_text_obj_enabled | return | endif - " Utility maps to avoid conflict with "normal" command - nnoremap (v) v - nnoremap (V) V - - " Commands - xnoremap (vimtex-ic) :call vimtex#text_obj#commands(1) - xnoremap (vimtex-ac) :call vimtex#text_obj#commands() - xmap (vimtex-ic) (vimtex-ic) - xmap (vimtex-ac) (vimtex-ac) - onoremap (vimtex-ic) :execute "normal \(v)\(vimtex-ic)" - onoremap (vimtex-ac) :execute "normal \(v)\(vimtex-ac)" - - " Delimiters - xnoremap (vimtex-id) :call vimtex#text_obj#delimiters(1) - xnoremap (vimtex-ad) :call vimtex#text_obj#delimiters() - xmap (vimtex-id) (vimtex-id) - xmap (vimtex-ad) (vimtex-ad) - onoremap (vimtex-id) :execute "normal \(v)\(vimtex-id)" - onoremap (vimtex-ad) :execute "normal \(v)\(vimtex-ad)" - - " Environments - xnoremap (vimtex-ie) :call vimtex#text_obj#environments(1) - xnoremap (vimtex-ae) :call vimtex#text_obj#environments() - xmap (vimtex-ie) (vimtex-ie) - xmap (vimtex-ae) (vimtex-ae) - onoremap (vimtex-ie) :execute "normal \(v)\(vimtex-ie)" - onoremap (vimtex-ae) :execute "normal \(v)\(vimtex-ae)" - - " Inline math - xnoremap (vimtex-i$) :call vimtex#text_obj#inline_math(1) - xnoremap (vimtex-a$) :call vimtex#text_obj#inline_math() - xmap (vimtex-i$) (vimtex-i$) - xmap (vimtex-a$) (vimtex-a$) - onoremap (vimtex-i$) :execute "normal \(v)\(vimtex-i$)" - onoremap (vimtex-a$) :execute "normal \(v)\(vimtex-a$)" - - " Paragraphs - xnoremap (vimtex-ip) :call vimtex#text_obj#paragraphs(1) - xnoremap (vimtex-ap) :call vimtex#text_obj#paragraphs() - xmap (vimtex-ip) (vimtex-ip) - xmap (vimtex-ap) (vimtex-ap) - onoremap (vimtex-ip) :execute "normal \(V)\(vimtex-ip)" - onoremap (vimtex-ap) :execute "normal \(V)\(vimtex-ap)" + for [l:map, l:name] in [ + \ ['c', 'commands'], + \ ['d', 'delimiters'], + \ ['e', 'environments'], + \ ['$', 'inline_math'], + \ ['p', 'paragraphs'], + \] + let l:p1 = 'noremap (vimtex-' + let l:p2 = l:map . ') :call vimtex#text_obj#' . l:name + execute 'x' . l:p1 . 'i' . l:p2 . '(1)' + execute 'x' . l:p1 . 'a' . l:p2 . '(0)' + execute 'o' . l:p1 . 'i' . l:p2 . '(1)' + execute 'o' . l:p1 . 'a' . l:p2 . '(0)' + endfor endfunction " }}}1 -function! vimtex#text_obj#commands(...) " {{{1 +function! vimtex#text_obj#commands(is_inner) " {{{1 let l:cmd = vimtex#cmd#get_current() if empty(l:cmd) | return | endif let [l1, c1] = [l:cmd.pos_start.lnum, l:cmd.pos_start.cnum] let [l2, c2] = [l:cmd.pos_end.lnum, l:cmd.pos_end.cnum] - if a:0 > 0 + if a:is_inner let l2 = l1 let c2 = c1 + strlen(l:cmd.name) - 1 let c1 += 1 @@ -82,14 +53,14 @@ function! vimtex#text_obj#commands(...) " {{{1 endfunction " }}}1 -function! vimtex#text_obj#delimiters(...) " {{{1 +function! vimtex#text_obj#delimiters(is_inner) " {{{1 let [l:open, l:close] = vimtex#delim#get_surrounding('delim_all') if empty(l:open) | return | endif - call s:text_obj_delim(l:open, l:close, a:0 > 0) + call s:text_obj_delim(l:open, l:close, a:is_inner) endfunction " }}}1 -function! vimtex#text_obj#environments(...) " {{{1 +function! vimtex#text_obj#environments(is_inner) " {{{1 let [l:open, l:close] = vimtex#delim#get_surrounding('env') if empty(l:open) | return | endif @@ -101,19 +72,17 @@ function! vimtex#text_obj#environments(...) " {{{1 \ '^\v%(\s*\[[^]]*\])?%(\s*\{[^}]*\})*', \ l:open.cnum + strlen(l:open.match) - 1) - call s:text_obj_delim(l:open, l:close, a:0 > 0) + call s:text_obj_delim(l:open, l:close, a:is_inner) endfunction " }}}1 -function! vimtex#text_obj#inline_math(...) " {{{1 +function! vimtex#text_obj#inline_math(is_inner) " {{{1 let [l:open, l:close] = vimtex#delim#get_surrounding('env_math') if empty(l:open) | return | endif - call s:text_obj_delim(l:open, l:close, a:0 > 0) + call s:text_obj_delim(l:open, l:close, a:is_inner) endfunction " }}}1 -function! vimtex#text_obj#paragraphs(...) " {{{1 - let inner = a:0 > 0 - +function! vimtex#text_obj#paragraphs(is_inner) " {{{1 " Define selection normal! 0j call vimtex#motion#next_paragraph(1,0) @@ -121,17 +90,17 @@ function! vimtex#text_obj#paragraphs(...) " {{{1 call vimtex#motion#next_paragraph(0,0) " Go back one line for inner objects - if inner + if a:is_inner normal! k endif endfunction " }}}1 -function! s:text_obj_delim(open, close, inner) " {{{1 +function! s:text_obj_delim(open, close, is_inner) " {{{1 let [l1, c1, l2, c2] = [a:open.lnum, a:open.cnum, a:close.lnum, a:close.cnum] - if a:inner + if a:is_inner let c1 += len(a:open.match) let c2 -= 1