From 7a7299c2e80a4990cbb1a8e3607ec9697db84546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 13 Feb 2016 11:39:18 +0100 Subject: [PATCH] Added text object for commands (#244) --- README.md | 4 ++- autoload/vimtex.vim | 12 +++++--- autoload/vimtex/text_obj.vim | 55 +++++++++++++++++++++++++++--------- doc/vimtex.txt | 36 ++++++++++++++--------- 4 files changed, 75 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 0c66b2c..9fea65a 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,11 @@ disabled if desired. - Move between sections with `[[`, `[]`, `][`, `]]` - Move between matching delimiters with `%` - Text objects + - `ic ac` Commands + - `id ad` Delimiters - `ie ae` LaTeX environments - `i$ a$` Inline math structures - - `id ad` Delimiters + - `ip ap` Paragraphs - Other mappings - Delete the surrounding command or environment with `dsc`/`dse` - Change the surrounding command or environment with `csc`/`cse` diff --git a/autoload/vimtex.vim b/autoload/vimtex.vim index e4b7f66..e63a238 100644 --- a/autoload/vimtex.vim +++ b/autoload/vimtex.vim @@ -393,6 +393,14 @@ function! s:init_mappings() " {{{1 endif if g:vimtex_text_obj_enabled + call s:map('x', 'ic', '(vimtex-ic)') + call s:map('x', 'ac', '(vimtex-ac)') + call s:map('o', 'ic', '(vimtex-ic)') + call s:map('o', 'ac', '(vimtex-ac)') + call s:map('x', 'id', '(vimtex-id)') + call s:map('x', 'ad', '(vimtex-ad)') + call s:map('o', 'id', '(vimtex-id)') + call s:map('o', 'ad', '(vimtex-ad)') call s:map('x', 'ie', '(vimtex-ie)') call s:map('x', 'ae', '(vimtex-ae)') call s:map('o', 'ie', '(vimtex-ie)') @@ -401,10 +409,6 @@ function! s:init_mappings() " {{{1 call s:map('x', 'a$', '(vimtex-a$)') call s:map('o', 'i$', '(vimtex-i$)') call s:map('o', 'a$', '(vimtex-a$)') - call s:map('x', 'id', '(vimtex-id)') - call s:map('x', 'ad', '(vimtex-ad)') - call s:map('o', 'id', '(vimtex-id)') - call s:map('o', 'ad', '(vimtex-ad)') call s:map('x', 'ip', '(vimtex-ip)') call s:map('x', 'ap', '(vimtex-ap)') call s:map('o', 'ip', '(vimtex-ip)') diff --git a/autoload/vimtex/text_obj.vim b/autoload/vimtex/text_obj.vim index 1dd5b99..3425761 100644 --- a/autoload/vimtex/text_obj.vim +++ b/autoload/vimtex/text_obj.vim @@ -20,13 +20,21 @@ function! vimtex#text_obj#init_buffer() " {{{1 nnoremap (v) v nnoremap (V) V - " 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)" + " 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) @@ -44,17 +52,36 @@ function! vimtex#text_obj#init_buffer() " {{{1 onoremap (vimtex-i$) :execute "normal \(v)\(vimtex-i$)" onoremap (vimtex-a$) :execute "normal \(v)\(vimtex-a$)" - " 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)" + " 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)" endfunction " }}}1 +function! vimtex#text_obj#commands(...) " {{{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 + let l2 = l1 + let c2 = c1 + strlen(l:cmd.name) - 1 + let c1 += 1 + endif + + call cursor(l1, c1) + normal! v + call cursor(l2, c2) +endfunction + +" }}}1 function! vimtex#text_obj#delimiters(...) " {{{1 let [l:open, l:close] = vimtex#delim#get_surrounding('delim_all') if empty(l:open) | return | endif diff --git a/doc/vimtex.txt b/doc/vimtex.txt index c2fafc4..51e2f93 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -95,9 +95,11 @@ Feature overview~ - Move between sections with `[[`, `[]`, `][`, `]]` - Move between matching delimiters with `%` - Text objects + - `ic` `ac` Commands + - `id` `ad` Delimiters - `ie` `ae` LaTeX environments - `i$` `a$` Inline math structures - - `id` `ad` Delimiters + - `ip` `ap` Paragraphs - Other mappings - Delete the surrounding command or environment with `dsc`/`dse` - Change the surrounding command or environment with `csc`/`cse` @@ -307,12 +309,14 @@ This feature is explained in more detail later, see |vimtex-imaps|. tsd |(vimtex-delim-toggle-modifier)| `n` |(vimtex-cmd-create)| `ni` ]] |(vimtex-delim-close)| `i` - a$ |(vimtex-a$)| `nxo` - i$ |(vimtex-i$)| `nxo` - ae |(vimtex-ae)| `nxo` - ie |(vimtex-ie)| `nxo` + ac |(vimtex-ac)| `nxo` + ic |(vimtex-ic)| `nxo` ad |(vimtex-ad)| `nxo` id |(vimtex-id)| `nxo` + ae |(vimtex-ae)| `nxo` + ie |(vimtex-ie)| `nxo` + a$ |(vimtex-a$)| `nxo` + i$ |(vimtex-i$)| `nxo` ap |(vimtex-ap)| `nxo` ip |(vimtex-ip)| `nxo` % |(vimtex-%)| `nxo` @@ -1118,11 +1122,17 @@ Map definitions~ *(vimtex-delim-close)* Close the current environment or delimiter (insert mode). -*(vimtex-a$)* - Text object for inline math (inclusive). +*(vimtex-ac)* + Text object for commands (inclusive). -*(vimtex-i$)* - Text object for inline math (exclusive). +*(vimtex-ic)* + Text object for commands (exclusive). + +*(vimtex-ad)* + Text object for delimiters (inclusive). + +*(vimtex-id)* + Text object for delimiters (exclusive). *(vimtex-ae)* Text object for environments (inclusive). @@ -1130,11 +1140,11 @@ Map definitions~ *(vimtex-ie)* Text object for environments (exclusive). -*(vimtex-ad)* - Text object for delimiters (inclusive). +*(vimtex-a$)* + Text object for inline math (inclusive). -*(vimtex-id)* - Text object for delimiters (exclusive). +*(vimtex-i$)* + Text object for inline math (exclusive). *(vimtex-ap)* Text object for paragraphs (inclusive).