Permalink
Browse files

Replace empty region properly

  • Loading branch information...
1 parent 7a8431c commit 37ab7316edb761239ee278e04f29e69a666a5f97 @kana committed Nov 29, 2012
Showing with 47 additions and 1 deletion.
  1. +14 −1 autoload/operator/replace.vim
  2. +33 −0 t/basics.vim
@@ -38,7 +38,9 @@ function! operator#replace#do(motion_wise) "{{{2
\ ? 'p'
\ : 'P')
- execute 'normal!' '`['.visual_command.'`]"_d'
+ if !s:is_empty_region(getpos("'["), getpos("']"))
+ execute 'normal!' '`['.visual_command.'`]"_d'
+ end
execute 'normal!' '"'.register.put_command
return
endfunction
@@ -80,6 +82,17 @@ endfunction
+function! s:is_empty_region(begin, end) "{{{2
+ " Whenever 'operatorfunc' is called, '[ is always placed before '] even if
+ " a backward motion is given to g@. But there is the only one exception.
+ " If an empty region is given to g@, '[ and '] are set to the same line, but
+ " '[ is placed after '].
+ return a:begin[1] == a:end[1] && a:end[2] < a:begin[2]
+endfunction
+
+
+
+
function! s:visual_command_from_wise_name(wise_name) "{{{2()
if a:wise_name ==# 'char'
return 'v'
View
@@ -184,3 +184,36 @@ describe '<Plug>(operator-replace) with line {motion}'
Expect [getreg('s'), getregtype('s')] ==# ["waldo\n", 'V']
end
end
+
+
+
+
+describe '<Plug>(operator-replace) with empty region'
+ before
+ tabnew
+ end
+
+ after
+ tabclose!
+ end
+
+ it 'replaces a given empty region with a register content'
+ let @" = 'foo'
+
+ normal! o<div>---</div>
+ execute 'normal' "\<Plug>(operator-replace)it"
+ Expect getline('.') ==# '<div>foo</div>'
+
+ normal! o<div>-</div>
+ execute 'normal' "\<Plug>(operator-replace)it"
+ Expect getline('.') ==# '<div>foo</div>'
+
+ normal! o<div></div>
+ execute 'normal' "\<Plug>(operator-replace)it"
+ Expect getline('.') ==# '<div>foo</div>'
+
+ normal! o<div>xyz</div>
+ execute 'normal' "0fz\<Plug>(operator-replace)Fx"
+ Expect getline('.') ==# '<div>fooz</div>'
+ end
+end

0 comments on commit 37ab731

Please sign in to comment.