aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-12-29 23:02:18 +1300
committerTom Ryder <tom@sanctum.geek.nz>2018-12-29 23:02:18 +1300
commita66a19354521cdf34e8ed2b0a708b88967ef12d2 (patch)
treef9ab4c3f0efca75c10bd3100b16716f86a8900aa
parentMerge branch 'release/v0.2.0' (diff)
parentBump VERSION (diff)
downloadvim-replace-operator-1.0.0.tar.gz (sig)
vim-replace-operator-1.0.0.zip
Merge branch 'release/v1.0.0'v1.0.0
* release/v1.0.0: Refactor with expression mappings and simpler alg Correct use of plural in documentation Two-space sentences in documentation
-rw-r--r--README.md2
-rw-r--r--VERSION2
-rw-r--r--autoload/replace_operator.vim76
-rw-r--r--doc/replace_operator.txt6
-rw-r--r--plugin/replace_operator.vim14
5 files changed, 43 insertions, 57 deletions
diff --git a/README.md b/README.md
index 1496a8d..5b65c02 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ bounded by a motion with the contents of a register, by default the unnamed
register.
This allows you, for example, to select a set of lines and replace any other
-set of lines with it in one repeatable operation. The text you replace stacks
+set of lines with it in one repeatable operation. The text you replace stacks
up in the numbered registers as normal, if you do end up needing it back.
License
diff --git a/VERSION b/VERSION
index 0ea3a94..3eefcb9 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.2.0
+1.0.0
diff --git a/autoload/replace_operator.vim b/autoload/replace_operator.vim
index 74711fd..a48a04d 100644
--- a/autoload/replace_operator.vim
+++ b/autoload/replace_operator.vim
@@ -1,59 +1,47 @@
" Replace the operated text with the contents of a register
function! replace_operator#Operatorfunc(type) abort
- " If we're using the unnamed register, we'll need to save its current
- " contents, because the deletion we're about to do will overwrite it
- let l:register = g:replace_operator#register
- if l:register ==# '"'
- let l:text = getreg(l:register)
- endif
-
- " Select or re-select text depending on how we were invoked
- if a:type ==# 'v' || a:type ==# 'V'
- normal! gv
- elseif a:type ==# "\<C-V>"
- echoerr 'Visual block mode replace not supported'
- return
- elseif a:type ==# 'line'
+ " Save the active register into a local variable
+ let l:register = v:register
+
+ " Save the current value of the unnamed register and the current value of
+ " the 'clipboard' and 'selection' options into a dictionary for restoring
+ " after this is all done
+ let l:save = {
+ \ 'unnamed': @@,
+ \ 'clipboard': &clipboard,
+ \ 'selection': &selection
+ \ }
+
+ " Don't involve any system clipboard for the duration of this function
+ set clipboard-=unnamed
+ set clipboard-=unnamedplus
+
+ " Ensure that we include end-of-line and final characters in selections
+ set selection=inclusive
+
+ " Select or re-select text, depending on how we were invoked
+ if a:type ==# 'line'
normal! '[V']
+ elseif a:type ==# 'block'
+ execute "normal! `[\<C-V>`]"
else
normal! `[v`]
endif
- " Delete the text normally so it stacks up in the numbered registers, and
- " then restore the active register's initial value if we just clobbered it
- normal! d
- if l:register ==# '"'
- call setreg(l:register, l:text)
- endif
+ " Re-select the text, and replace it with the contents of the register
+ execute 'normal! "'.l:register.'p'
- " Are we working linewise or characterwise?
- let l:linewise = a:type ==# 'V' || a:type ==# 'line'
-
- " If the cursor is before the start of the last changed text, we've deleted
- " to the end of line (characterwise) or the end of buffer (linewise) and
- " have been forced to move back and up respectively. If this is the case,
- " we'll need to paste after the current point, not before it.
- if l:linewise && line('.') < line("'[")
- \ || !l:linewise && col('.') < col("'[")
- let l:paste = 'p'
- else
- let l:paste = 'P'
- endif
-
- " Run the paste
- execute 'normal "'.l:register.l:paste
+ " Restore contents of the unnamed register and the previous values of the
+ " 'clipboard' and 'selection' options
+ let @@ = l:save['unnamed']
+ let &clipboard = l:save['clipboard']
+ let &selection = l:save['selection']
endfunction
" Helper function for normal mode map
-function! replace_operator#MapNormal(register) abort
- let g:replace_operator#register = a:register
+function! replace_operator#Map(register) abort
set operatorfunc=replace_operator#Operatorfunc
-endfunction
-
-" Helper function for visual mode map
-function! replace_operator#MapVisual(register, visualmode) abort
- let g:replace_operator#register = a:register
- call replace_operator#Operatorfunc(a:visualmode)
+ return 'g@'
endfunction
diff --git a/doc/replace_operator.txt b/doc/replace_operator.txt
index d8e6e18..faf247f 100644
--- a/doc/replace_operator.txt
+++ b/doc/replace_operator.txt
@@ -7,7 +7,7 @@ text bounded by a motion with the contents of a register, by default the
unnamed register.
This allows you, for example, to select a set of lines and replace any other
-set of lines with it in one repeatable operation. The text you replace stacks
+set of lines with it in one repeatable operation. The text you replace stacks
up in the numbered registers as normal, if you do end up needing it back.
REQUIREMENTS *replace_operator-requirements*
@@ -18,8 +18,8 @@ MAPPINGS *replace_operator-mappings*
*<Plug>(RepeatOperator)*
The normal and visual mode mapping targets are both named
-|<Plug>(RepeatOperator)|. There is no default key mapping; you should define
-one yourself in your |vimrc|. For example:
+|<Plug>(RepeatOperator)|. There are no default key mappings; you should
+define one yourself in your |vimrc|. For example:
>
nmap <Leader>r <Plug>(ReplaceOperator)
xmap <Leader>r <Plug>(ReplaceOperator)
diff --git a/plugin/replace_operator.vim b/plugin/replace_operator.vim
index a5c4a95..982b573 100644
--- a/plugin/replace_operator.vim
+++ b/plugin/replace_operator.vim
@@ -1,6 +1,6 @@
"
-" replace_operator.vim: Replace text selected with a motion with the
-" contents of a register in a repeatable way.
+" replace_operator.vim: Replace text selected with a motion with the contents
+" of a register in a repeatable way.
"
" Author: Tom Ryder <tom@sanctum.geek.nz>
" License: Same as Vim itself
@@ -14,9 +14,7 @@ endif
let g:loaded_replace_operator = 1
" Set up mapping
-nnoremap <silent> <unique>
- \ <Plug>(ReplaceOperator)
- \ :<C-U>call replace_operator#MapNormal(v:register)<CR>g@
-xnoremap <silent> <unique>
- \ <Plug>(ReplaceOperator)
- \ :<C-U>call replace_operator#MapVisual(v:register, visualmode())<CR>
+nnoremap <expr> <Plug>(ReplaceOperator)
+ \ replace_operator#Map()
+xnoremap <expr> <Plug>(ReplaceOperator)
+ \ replace_operator#Map()