From 92a70f73b5cbbf86cb3d239684a97711ac1302fe Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 19:49:44 +1200 Subject: Move functions into autoload --- autoload/insert_cancel.vim | 22 ++++++++++++++++++++++ plugin/insert_cancel.vim | 29 +++-------------------------- 2 files changed, 25 insertions(+), 26 deletions(-) create mode 100644 autoload/insert_cancel.vim diff --git a/autoload/insert_cancel.vim b/autoload/insert_cancel.vim new file mode 100644 index 0000000..41e6c60 --- /dev/null +++ b/autoload/insert_cancel.vim @@ -0,0 +1,22 @@ +" On leaving insert mode, whether normally or via (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() + if changenr() > b:insert_cancel_changenr + let b:insert_cancel_changed = 1 + endif +endfunction + +" 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() + let b:insert_cancel_changed = 0 + call insert_cancel#Check() +endfunction + +" On cancelling insert mode, if we think we made a change, undo it +function! insert_cancel#Cancel() + if get(b:, 'insert_cancel_changed', 0) + silent undo + endif +endfunction diff --git a/plugin/insert_cancel.vim b/plugin/insert_cancel.vim index a4595f9..4221ba4 100644 --- a/plugin/insert_cancel.vim +++ b/plugin/insert_cancel.vim @@ -16,29 +16,6 @@ if v:version < 700 endif let loaded_insert_cancel = 1 -" On leaving insert mode, whether normally or via (InsertCancel), check -" if changenr() exceeds the last time we cached it, and flag that a change has -" taken place if it did -function! s:Check() - if changenr() > b:insert_cancel_changenr - let b:insert_cancel_changed = 1 - endif -endfunction - -" On entering insert mode, reset the changed flag and check for a new round of -" changes since insert mode was opened -function! s:Enter() - let b:insert_cancel_changed = 0 - call s:Check() -endfunction - -" On cancelling insert mode, if we think we made a change, undo it -function! s:Cancel() - if get(b:, 'insert_cancel_changed', 0) - silent undo - endif -endfunction - " Set up the hooks described for the functions above, if Vim is new enough to " support all the hooks required augroup insert_cancel @@ -49,11 +26,11 @@ augroup insert_cancel \ let b:insert_cancel_changenr = changenr() " Function wrappers for entering and leaving insert mode - autocmd InsertEnter * call s:Enter() - autocmd InsertLeave * call s:Check() + 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 (InsertCancel) - \ :call Cancel() + \ :call insert_cancel#Cancel() -- cgit v1.2.3