From 248bbeed2e6c226a053fda19e02aa688c0a86c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Thu, 21 Jan 2016 22:39:35 +0100 Subject: [PATCH] Refactored: extracted text_obj.vim from motion.vim --- autoload/vimtex.vim | 3 + autoload/vimtex/motion.vim | 183 +----------------------------- autoload/vimtex/text_obj.vim | 211 +++++++++++++++++++++++++++++++++++ doc/vimtex.txt | 5 + 4 files changed, 220 insertions(+), 182 deletions(-) create mode 100644 autoload/vimtex/text_obj.vim diff --git a/autoload/vimtex.vim b/autoload/vimtex.vim index 3187c86..2f2c406 100644 --- a/autoload/vimtex.vim +++ b/autoload/vimtex.vim @@ -385,6 +385,9 @@ function! s:init_mappings() " {{{1 call s:map('o', '][', '(vimtex-][)') call s:map('o', '[]', '(vimtex-[])') call s:map('o', '[[', '(vimtex-[[)') + endif + + if g:vimtex_text_obj_enabled call s:map('x', 'ie', '(vimtex-ie)') call s:map('x', 'ae', '(vimtex-ae)') call s:map('o', 'ie', '(vimtex-ie)') diff --git a/autoload/vimtex/motion.vim b/autoload/vimtex/motion.vim index ee5cd9d..bad0110 100644 --- a/autoload/vimtex/motion.vim +++ b/autoload/vimtex/motion.vim @@ -92,7 +92,7 @@ function! vimtex#motion#init_buffer() " {{{1 xmap (vimtex-%) (vimtex-%) onoremap (vimtex-%) :execute "normal \(v)\(vimtex-%)" - " Paragraph motion + " Paragraphs nnoremap (vimtex-}) :call vimtex#motion#next_paragraph(0,0) nnoremap (vimtex-{) :call vimtex#motion#next_paragraph(1,0) xnoremap (vimtex-}) :call vimtex#motion#next_paragraph(0,1) @@ -101,12 +101,6 @@ function! vimtex#motion#init_buffer() " {{{1 xmap (vimtex-{) (vimtex-{) onoremap (vimtex-}) :execute "normal \(v)\(vimtex-})" onoremap (vimtex-{) :execute "normal \(v)\(vimtex-{)" - xnoremap (vimtex-ip) :call vimtex#motion#sel_paragraph(1) - xnoremap (vimtex-ap) :call vimtex#motion#sel_paragraph() - 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)" " Sections nnoremap (vimtex-]]) :call vimtex#motion#next_section(0,0,0) @@ -125,30 +119,6 @@ function! vimtex#motion#init_buffer() " {{{1 onoremap (vimtex-][) :execute "normal \(v)\(vimtex-][)" onoremap (vimtex-[]) :execute "normal \(v)\(vimtex-[])" onoremap (vimtex-[[) :execute "normal \(v)\(vimtex-[[)" - - " Text object for environments - xnoremap (vimtex-ie) :call vimtex#motion#sel_environment(1) - xnoremap (vimtex-ae) :call vimtex#motion#sel_environment() - 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)" - - " Text object for inline math - xnoremap (vimtex-i$) :call vimtex#motion#sel_inline_math(1) - xnoremap (vimtex-a$) :call vimtex#motion#sel_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$)" - - " Text object for delimiters - xnoremap (vimtex-id) :call vimtex#motion#sel_delimiter(1) - xnoremap (vimtex-ad) :call vimtex#motion#sel_delimiter() - 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)" endfunction " }}}1 @@ -280,127 +250,6 @@ function! vimtex#motion#next_section(type, backwards, visual) " {{{1 endif endfunction -" }}}1 -function! vimtex#motion#sel_delimiter(...) " {{{1 - let inner = a:0 > 0 - - let [d1, l1, c1, d2, l2, c2] = vimtex#util#get_delim() - - if inner - let c1 += len(d1) - if c1 != len(getline(l1)) - let l1 += 1 - let c1 = 1 - endif - endif - - if inner - let c2 -= 1 - if c2 < 1 - let l2 -= 1 - let c2 = len(getline(l2)) - endif - else - let c2 += len(d2) - 1 - endif - - if l1 < l2 || (l1 == l2 && c1 < c2) - call cursor(l1,c1) - if visualmode() ==# 'V' - normal! V - else - normal! v - endif - call cursor(l2,c2) - endif -endfunction - -" }}}1 -function! vimtex#motion#sel_environment(...) " {{{1 - let inner = a:0 > 0 - - let [env, lnum, cnum, lnum2, cnum2] = vimtex#util#get_env(1) - call cursor(lnum, cnum) - if inner - if env =~# '^\' - call search('\\.\_\s*\S', 'eW') - else - call search('}\(\_\s*\(\[\_[^]]*\]\|{\_\S\{-}}\)\)\?\_\s*\S', 'eW') - endif - endif - if visualmode() ==# 'V' - normal! V - else - normal! v - endif - call cursor(lnum2, cnum2) - if inner - call search('\S\_\s*', 'bW') - else - if env =~# '^\' - normal! l - else - call search('}', 'eW') - endif - endif -endfunction - -" }}}1 -function! vimtex#motion#sel_inline_math(...) " {{{1 - let l:inner = a:0 > 0 - - let l:flags = 'bW' - let l:dollar = 0 - let l:dollar_pat = '\\\@ 0 - - " Define selection - normal! 0j - call vimtex#motion#next_paragraph(1,0) - normal! jV - call vimtex#motion#next_paragraph(0,0) - - " Go back one line for inner objects - if inner - normal! k - endif -endfunction - " }}}1 function! s:highlight_matching_pair(...) " {{{1 @@ -469,36 +318,6 @@ function! s:highlight_matching_pair(...) " {{{1 endif endfunction -" }}}1 -function! s:search_and_skip_comments(pat, ...) " {{{1 - " Usage: s:search_and_skip_comments(pat, [flags, stopline]) - let flags = a:0 >= 1 ? a:1 : '' - let stopline = a:0 >= 2 ? a:2 : 0 - let saved_pos = getpos('.') - - " search once - let ret = search(a:pat, flags, stopline) - - if ret - " do not match at current position if inside comment - let flags = substitute(flags, 'c', '', 'g') - - " keep searching while in comment - while vimtex#util#in_comment() - let ret = search(a:pat, flags, stopline) - if !ret - break - endif - endwhile - endif - - if !ret - " if no match found, restore position - call setpos('.', saved_pos) - endif - - return ret -endfunction " }}}1 " vim: fdm=marker sw=2 diff --git a/autoload/vimtex/text_obj.vim b/autoload/vimtex/text_obj.vim new file mode 100644 index 0000000..ea0330a --- /dev/null +++ b/autoload/vimtex/text_obj.vim @@ -0,0 +1,211 @@ +" vimtex - LaTeX plugin for Vim +" +" Maintainer: Karl Yngve LervÄg +" Email: karl.yngve@gmail.com +" + +function! vimtex#text_obj#init_options() " {{{1 + call vimtex#util#set_default('g:vimtex_text_obj_enabled', 1) +endfunction + +" }}}1 +function! vimtex#text_obj#init_script() " {{{1 +endfunction + +" }}}1 +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 + + " 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)" + + " 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$)" + + " 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)" +endfunction + +" }}}1 + +function! vimtex#text_obj#delimiters(...) " {{{1 + let inner = a:0 > 0 + + let [d1, l1, c1, d2, l2, c2] = vimtex#delim#get_surrounding() + + if inner + let c1 += len(d1) + if c1 != len(getline(l1)) + let l1 += 1 + let c1 = 1 + endif + endif + + if inner + let c2 -= 1 + if c2 < 1 + let l2 -= 1 + let c2 = len(getline(l2)) + endif + else + let c2 += len(d2) - 1 + endif + + if l1 < l2 || (l1 == l2 && c1 < c2) + call cursor(l1,c1) + if visualmode() ==# 'V' + normal! V + else + normal! v + endif + call cursor(l2,c2) + endif +endfunction + +" }}}1 +function! vimtex#text_obj#environments(...) " {{{1 + let inner = a:0 > 0 + + let [env, lnum, cnum, lnum2, cnum2] = vimtex#util#get_env(1) + call cursor(lnum, cnum) + if inner + if env =~# '^\' + call search('\\.\_\s*\S', 'eW') + else + call search('}\(\_\s*\(\[\_[^]]*\]\|{\_\S\{-}}\)\)\?\_\s*\S', 'eW') + endif + endif + if visualmode() ==# 'V' + normal! V + else + normal! v + endif + call cursor(lnum2, cnum2) + if inner + call search('\S\_\s*', 'bW') + else + if env =~# '^\' + normal! l + else + call search('}', 'eW') + endif + endif +endfunction + +" }}}1 +function! vimtex#text_obj#inline_math(...) " {{{1 + let l:inner = a:0 > 0 + + let l:flags = 'bW' + let l:dollar = 0 + let l:dollar_pat = '\\\@ 0 + + " Define selection + normal! 0j + call vimtex#motion#next_paragraph(1,0) + normal! jV + call vimtex#motion#next_paragraph(0,0) + + " Go back one line for inner objects + if inner + normal! k + endif +endfunction + +" }}}1 + +function! s:search_and_skip_comments(pat, ...) " {{{1 + " Usage: s:search_and_skip_comments(pat, [flags, stopline]) + let flags = a:0 >= 1 ? a:1 : '' + let stopline = a:0 >= 2 ? a:2 : 0 + let saved_pos = getpos('.') + + " search once + let ret = search(a:pat, flags, stopline) + + if ret + " do not match at current position if inside comment + let flags = substitute(flags, 'c', '', 'g') + + " keep searching while in comment + while vimtex#util#in_comment() + let ret = search(a:pat, flags, stopline) + if !ret + break + endif + endwhile + endif + + if !ret + " if no match found, restore position + call setpos('.', saved_pos) + endif + + return ret +endfunction +" }}}1 + +" vim: fdm=marker sw=2 diff --git a/doc/vimtex.txt b/doc/vimtex.txt index e6eea70..76b53a5 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -736,6 +736,11 @@ Options~ Default value: 1 +*g:vimtex_text_obj_enabled* + Use this option to disable the text object mappings. + + Default value: 1 + *g:vimtex_view_enabled* Use this option to disable/enable the |vimtex| viewer interface.