aboutsummaryrefslogtreecommitdiff
path: root/autoload/insert_cancel.vim
blob: 74af0604b325acec45c2e2606911812a205e86ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
" On entering insert mode, reset the changed flag and check for a new round of
" changes since insert mode was opened
function! insert_cancel#Enter() abort
  let b:insert_cancel_changed = 0
  call insert_cancel#Check()
endfunction

" On leaving insert mode, whether normally or via <Plug>(InsertCancel), check
" if changenr() exceeds the last time we cached it, and flag that a change has
" taken place if it did
function! insert_cancel#Check() abort
  if changenr() > b:insert_cancel_changenr
    let b:insert_cancel_changed = 1
  endif
endfunction

" On cancelling insert mode, if we think we made a change, undo it
function! insert_cancel#Cancel() abort
  if get(b:, 'insert_cancel_changed', 0)
    silent undo
  endif
endfunction