aboutsummaryrefslogtreecommitdiff
path: root/plugin/insert_cancel.vim
blob: 33735aea00ae756d1a33fc44f2bcef59b08d5c47 (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
"
" 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.
"
" Author: Tom Ryder <tom@sanctum.geek.nz>
" License: Same as Vim itself
"
if exists('g:loaded_insert_cancel') || &compatible
  finish
endif
if v:version < 600
  finish
endif
let g:loaded_insert_cancel = 1

" Initialise s:changedtick so vint understands
let s:changedtick = 0

" InsertEnter is only available from Vim 7
if has('autocmd') && v:version >= 700
  augroup insert_cancel
    autocmd!
    autocmd InsertEnter *
          \ let s:changedtick = b:changedtick
  augroup END
endif

" Try to figure out whether we made a change to undo, undo it if so
function! s:InsertCancel()
  if !&modified
    return
  endif
  if s:changedtick > 0 && b:changedtick > s:changedtick
        \ || line("'[") != line("']")
        \ || col("'[") != col("']")
    silent undo
  endif
endfunction

" Provide plugin mapping
inoremap <silent> <Plug>InsertCancel
      \ <C-C>:<C-U>call <SID>InsertCancel()<CR>