diff options
Diffstat (limited to 'autoload/insert_cancel.vim')
-rw-r--r-- | autoload/insert_cancel.vim | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/autoload/insert_cancel.vim b/autoload/insert_cancel.vim new file mode 100644 index 0000000..74af060 --- /dev/null +++ b/autoload/insert_cancel.vim @@ -0,0 +1,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 |