diff options
Diffstat (limited to 'plugin/insert_cancel.vim')
-rw-r--r-- | plugin/insert_cancel.vim | 67 |
1 files changed, 11 insertions, 56 deletions
diff --git a/plugin/insert_cancel.vim b/plugin/insert_cancel.vim index 8ad47e5..f722ac8 100644 --- a/plugin/insert_cancel.vim +++ b/plugin/insert_cancel.vim @@ -8,68 +8,23 @@ " Author: Tom Ryder <tom@sanctum.geek.nz> " License: Same as Vim itself " -if exists('loaded_insert_cancel') || &compatible - finish -endif -if v:version < 600 +if exists('loaded_insert_cancel') || &compatible || v:version < 700 finish endif let loaded_insert_cancel = 1 -" 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! 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() - - " The flag exists, if it's on, undo - if exists('b:insert_cancel_changed') - if b:insert_cancel_changed - silent undo - endif - - " The flag didn't exist, fall back to marks; if the line number or column - " number of the marks for the last changed text aren't exactly equal, that - " suggests we changed something; undo it - elseif line("'[") != line("']") || col("'[") != col("']") - silent undo - endif - - " Redraw the screen to avoid bug with vestigial 'showmode' display - redraw! - -endfunction - " Set up the hooks described for the functions above, if Vim is new enough to " support all the hooks required -if has('autocmd') && v:version >= 700 - 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 s:Enter() - autocmd InsertLeave * call s:Check() - - augroup END -endif +augroup insert_cancel + autocmd! + autocmd InsertEnter * + \ call insert_cancel#Enter() + autocmd InsertLeave * + \ call insert_cancel#Check() + autocmd CursorMoved * + \ let b:insert_cancel_changenr = changenr() +augroup END " Mapping that exits insert mode normally and checks for a change to undo inoremap <silent> <Plug>(InsertCancel) - \ <Esc>:<C-U>call <SID>Cancel()<CR> + \ <Esc>:<C-U>call insert_cancel#Cancel()<CR> |