Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to default vim behavior after first visual selection #8

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 28 additions & 9 deletions plugin/textobjectify.vim
Expand Up @@ -2,7 +2,7 @@
" Maintainer: Daniel Thau (paradigm@bedrocklinux.org)
" Version: 0.1
" Description: TextObjectify is a Vim plugin which improves text objects
" Last Change: 2013-04-04
" Last Change: 2013-08-28
" Location: plugin/textobjectify.vim
" Website: https://github.com/paradigm/textobjectfy
"
Expand Down Expand Up @@ -67,6 +67,7 @@ function! TextObjectify(mode,ia)

" get object
let l:object = nr2char(getchar())
let l:is_obj_vim_default = count(s:defaults, l:object) > 0

" figure out how to treat object. four possible situations. use the
" first we run into.
Expand All @@ -85,9 +86,17 @@ function! TextObjectify(mode,ia)
if g:textobjectify[l:object]['line'] == 1
let s:mode = "V"
endif
elseif count(s:defaults, l:object) > 0
elseif l:is_obj_vim_default
if s:IsVisual(s:mode)
" this works because even if the user started out in normal mode, the act
" of pressing 'v' will start a new 1-char selection, so '<esc>gv' simply
" re-selects that 1-char selection.
execute "normal! \<esc>gv".a:ia.l:object
else
" recurse into TextObjectify (! is intentionally omitted)
execute "normal v".a:ia.l:object
end
" object is vim default - no need for any more plugin
execute "normal! v".a:ia. l:object
return
elseif !exists("g:textobjectify_onthefly") || g:textobjectify_onthefly == 1
" create object on-the-fly
Expand Down Expand Up @@ -123,13 +132,19 @@ function! TextObjectify(mode,ia)
let s:realorigcol = col('.')

" note whether or not a region is already visually selected
if (s:mode ==# "v" || s:mode ==# "V" || s:mode ==# "\<c-v>") &&
\(col("'<") != col("'>") || line("'<") != line("'>"))
if s:IsVisual(s:mode) && (col("'<") != col("'>") || line("'<") != line("'>"))
let s:invisual = 1
" if there is one and 'i' is used, expand selected area so we can
" re-select a larger area
if s:ia == 'i'
if s:seek == 2
if l:is_obj_vim_default
" already in a selection; let vim default behavior take over.
let l:saved_ve = &virtualedit
set virtualedit=
execute "normal! \<esc>gvi".l:object
let &virtualedit=l:saved_ve
return
elseif s:seek == 2
if col(".") < col("$")-1
execute "normal! \<right>"
else
Expand Down Expand Up @@ -285,12 +300,12 @@ function! s:SelectRange()
let s:c2 = virtcol('.')

" select range
if s:mode == "v" || s:mode == "V" || s:mode == "\<c-v>"
if s:IsVisual(s:mode)
" if already in a visual mode, use that same mode
execute 'normal! '.s:l1.'G'.s:c1.'|'.s:mode.s:l2.'G'.s:c2.'|'
execute 'keepjumps normal! '.s:l1.'G'.s:c1.'|'.s:mode.s:l2.'G'.s:c2.'|'
else
" visually select range - operator will apply to this range
execute 'normal! '.s:l1.'G'.s:c1.'|v'.s:l2.'G'.s:c2.'|'
execute 'keepjumps normal! '.s:l1.'G'.s:c1.'|v'.s:l2.'G'.s:c2.'|'
endif
return 1
endfunction
Expand Down Expand Up @@ -337,4 +352,8 @@ function! s:SearchAnyLine()
endwhile
endfunction

function! s:IsVisual(mode)
return (a:mode ==# "v" || a:mode ==# "V" || a:mode ==# "\<c-v>")
endfunction

" vim: noet sw=8 sts=8