Minor refactoring of text objects (#365)

This commit is contained in:
Karl Yngve Lervåg 2016-02-23 23:01:19 +01:00
parent b3b2d2f301
commit ec5e0df360

View File

@ -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 <buffer> <sid>(v) v
nnoremap <buffer> <sid>(V) V
" Commands
xnoremap <silent><buffer> <sid>(vimtex-ic) :<c-u>call vimtex#text_obj#commands(1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-ac) :<c-u>call vimtex#text_obj#commands()<cr>
xmap <silent><buffer> <plug>(vimtex-ic) <sid>(vimtex-ic)
xmap <silent><buffer> <plug>(vimtex-ac) <sid>(vimtex-ac)
onoremap <silent><buffer> <plug>(vimtex-ic) :execute "normal \<sid>(v)\<sid>(vimtex-ic)"<cr>
onoremap <silent><buffer> <plug>(vimtex-ac) :execute "normal \<sid>(v)\<sid>(vimtex-ac)"<cr>
" Delimiters
xnoremap <silent><buffer> <sid>(vimtex-id) :<c-u>call vimtex#text_obj#delimiters(1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-ad) :<c-u>call vimtex#text_obj#delimiters()<cr>
xmap <silent><buffer> <plug>(vimtex-id) <sid>(vimtex-id)
xmap <silent><buffer> <plug>(vimtex-ad) <sid>(vimtex-ad)
onoremap <silent><buffer> <plug>(vimtex-id) :execute "normal \<sid>(v)\<sid>(vimtex-id)"<cr>
onoremap <silent><buffer> <plug>(vimtex-ad) :execute "normal \<sid>(v)\<sid>(vimtex-ad)"<cr>
" Environments
xnoremap <silent><buffer> <sid>(vimtex-ie) :<c-u>call vimtex#text_obj#environments(1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-ae) :<c-u>call vimtex#text_obj#environments()<cr>
xmap <silent><buffer> <plug>(vimtex-ie) <sid>(vimtex-ie)
xmap <silent><buffer> <plug>(vimtex-ae) <sid>(vimtex-ae)
onoremap <silent><buffer> <plug>(vimtex-ie) :execute "normal \<sid>(v)\<sid>(vimtex-ie)"<cr>
onoremap <silent><buffer> <plug>(vimtex-ae) :execute "normal \<sid>(v)\<sid>(vimtex-ae)"<cr>
" Inline math
xnoremap <silent><buffer> <sid>(vimtex-i$) :<c-u>call vimtex#text_obj#inline_math(1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-a$) :<c-u>call vimtex#text_obj#inline_math()<cr>
xmap <silent><buffer> <plug>(vimtex-i$) <sid>(vimtex-i$)
xmap <silent><buffer> <plug>(vimtex-a$) <sid>(vimtex-a$)
onoremap <silent><buffer> <plug>(vimtex-i$) :execute "normal \<sid>(v)\<sid>(vimtex-i$)"<cr>
onoremap <silent><buffer> <plug>(vimtex-a$) :execute "normal \<sid>(v)\<sid>(vimtex-a$)"<cr>
" Paragraphs
xnoremap <silent><buffer> <sid>(vimtex-ip) :<c-u>call vimtex#text_obj#paragraphs(1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-ap) :<c-u>call vimtex#text_obj#paragraphs()<cr>
xmap <silent><buffer> <plug>(vimtex-ip) <sid>(vimtex-ip)
xmap <silent><buffer> <plug>(vimtex-ap) <sid>(vimtex-ap)
onoremap <silent><buffer> <plug>(vimtex-ip) :execute "normal \<sid>(V)\<sid>(vimtex-ip)"<cr>
onoremap <silent><buffer> <plug>(vimtex-ap) :execute "normal \<sid>(V)\<sid>(vimtex-ap)"<cr>
for [l:map, l:name] in [
\ ['c', 'commands'],
\ ['d', 'delimiters'],
\ ['e', 'environments'],
\ ['$', 'inline_math'],
\ ['p', 'paragraphs'],
\]
let l:p1 = 'noremap <silent><buffer> <plug>(vimtex-'
let l:p2 = l:map . ') :<c-u>call vimtex#text_obj#' . l:name
execute 'x' . l:p1 . 'i' . l:p2 . '(1)<cr>'
execute 'x' . l:p1 . 'a' . l:p2 . '(0)<cr>'
execute 'o' . l:p1 . 'i' . l:p2 . '(1)<cr>'
execute 'o' . l:p1 . 'a' . l:p2 . '(0)<cr>'
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