aboutsummaryrefslogtreecommitdiff
path: root/autoload/paste_insert.vim
blob: 92fdc544c8d381fb1d2fabf012c5e8a66b319e43 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
" Autoloaded entry point function
function! paste_insert#() abort

  " Set up an event table
  augroup paste_insert
    autocmd!

    " Set up the paste and tell the user
    autocmd User Start
          \ call s:Start() | echo 'Paste ready'

    " When starting insert mode, add completion hook for when we leave
    autocmd InsertEnter *
          \ autocmd paste_insert InsertLeave *
                \ doautocmd paste_insert User Complete

    " User waits too long in normal mode, timeout
    autocmd CursorHold *
          \ doautocmd paste_insert User Timeout

    " User leaves the buffer or window, abort
    autocmd BufLeave,WinLeave *
          \ doautocmd paste_insert User Abort

    " Exit condition reporting
    autocmd User Abort
          \ echo 'Paste aborted'
    autocmd User Cancel
          \ echo 'Paste cancelled'
    autocmd User Complete
          \ echo 'Paste completed'
    autocmd User Timeout
          \ echo 'Paste timeout'

    " End the paste and clear the events table
    autocmd User Abort,Cancel,Complete,Timeout
          \ call s:Stop() | autocmd! paste_insert

  augroup END

  " Trigger the starting actions
  doautocmd paste_insert User Start

endfunction

" Keys that cancel the pending paste in normal mode, defaults to Ctrl-C and
" Escape
let s:cancel = get(g:, 'paste_insert_cancel', ['<C-C>', '<Esc>'])

" Cache for the prior functions of those keys, in case the user has already
" mapped them
let s:maps = {}

function! s:MapArg(key, mode) abort
  if v:version > 703 || v:version == 703 && has('patch-32')
    return maparg(a:key, a:mode, 0, 1)
  else
    let rhs = maparg(a:key, a:mode)
    if rhs ==# ''
      return {}
    else
      return { 'rhs': rhs }
    endif
  endif
endfunction

" Start the paste: save each cancel key's prior function, remap it, set
" 'paste'
function! s:Start() abort
  for key in s:cancel
    let maps = []
    try
      let map = s:MapArg(key, 'n')
      if !empty(map)
        call add(maps, s:MapArg(key, 'n'))
      endif
      execute 'nunmap <buffer> '.key
      let map = s:MapArg(key, 'n')
      if !empty(map)
        call add(maps, s:MapArg(key, 'n'))
      endif
      execute 'nunmap '.key
    catch
    endtry
    if len(maps) == 2
      let maps[0]['buffer'] = 1
    endif
    let s:maps[key] = maps
    execute 'nnoremap '.key
          \.' :<C-U>doautocmd paste_insert User Cancel<CR>'
  endfor
  set paste
endfunction

" Stop the paste: unset 'paste', restore prior function of cancel key
function! s:Stop() abort
  set nopaste
  for key in s:cancel
    execute 'nunmap '.key
    for map in reverse(s:maps[key])
      let command = !has_key(map, 'noremap') || map['noremap']
              \ ? ['nnoremap']
              \ : ['nmap']
      for flag in ['buffer', 'expr', 'nowait', 'silent']
        if has_key(map, flag) && map[flag]
          call add(command, '<'.flag.'>')
        endif
      endfor
      call extend(command, [key, map['rhs']])
      execute join(command)
    endfor
    unlet s:maps[key]
  endfor
endfunction