aboutsummaryrefslogtreecommitdiff
path: root/plugin/insert_cancel.vim
blob: 4221ba47297d0e00fdd022975ce55f9c4ca76166 (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
"
" insert_cancel.vim: Cancel the current insert operation by undoing the last
" change upon insert exit, if we made a change; intended for remapping
" insert-mode Ctrl-C to do something useful.
"
" This was *way* harder to figure out than it looks.
"
" Author: Tom Ryder <tom@sanctum.geek.nz>
" License: Same as Vim itself
"
if exists('loaded_insert_cancel') || &compatible
  finish
endif
if v:version < 700
  finish
endif
let loaded_insert_cancel = 1

" Set up the hooks described for the functions above, if Vim is new enough to
" support all the hooks required
augroup insert_cancel
  autocmd!

  " On buffer edit and cursor move, cache the current change number
  autocmd BufEnter,CursorMoved *
        \ let b:insert_cancel_changenr = changenr()

  " Function wrappers for entering and leaving insert mode
  autocmd InsertEnter * call insert_cancel#Enter()
  autocmd InsertLeave * call insert_cancel#Check()

augroup END

" Mapping that exits insert mode normally and checks for a change to undo
inoremap <silent> <Plug>(InsertCancel)
      \ <Esc>:<C-U>call insert_cancel#Cancel()<CR>