aboutsummaryrefslogtreecommitdiff
path: root/autoload/replace_operator.vim
blob: 96e7c902af57491355fa92b619009e016982881b (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
48
49
50
51
" Replace the operated text with the contents of a register
function! replace_operator#Operatorfunc(type) abort

  " 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 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

  " Build normal mode keystrokes to select the operated text in visual mode
  if a:type ==# 'line'
    let select = "'[V']"
  elseif a:type ==# 'block'
    let select = "`[\<C-V>`]"
  else
    let select = '`[v`]'
  endif

  " Build normal mode keystrokes to paste from the selected register; only add
  " a register prefix if it's not the default unnamed register, because Vim
  " before 7.4 gets ""p wrong in visual mode
  let paste = 'p'
  if s:register !=# '"'
    let paste = '"'.s:register.paste
  endif
  silent execute 'normal! '.select.paste

  " Restore contents of the unnamed register and the previous values of the
  " 'clipboard' and 'selection' options
  let @@ = save['unnamed']
  let &clipboard = save['clipboard']
  let &selection = save['selection']

endfunction

" Helper function for normal mode map
function! replace_operator#(register) abort
  let s:register = a:register
  set operatorfunc=replace_operator#Operatorfunc
  return 'g@'
endfunction