aboutsummaryrefslogtreecommitdiff
path: root/autoload/replace_operator.vim
blob: a48a04db3824b28420bb5c69f1c1e7b4351e0a3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
" Replace the operated text with the contents of a register
function! replace_operator#Operatorfunc(type) abort

  " 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

  " Re-select the text, and replace it with the contents of the register
  execute 'normal! "'.l:register.'p'

  " 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#Map(register) abort
  set operatorfunc=replace_operator#Operatorfunc
  return 'g@'
endfunction