aboutsummaryrefslogblamecommitdiff
path: root/plugin/insert_cancel.vim
blob: f722ac8fb7dea6b58bc49cfddf86c195f6e7ed72 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                                                                            
                                            
 

                                                    


                                         
                                                                   

        
                            
 

                                                                             

                     



                                    
                       
                                                   
           

                                                                         
                                      
                                                  
"
" 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 to do something useful.
"
" This was *way* harder to figure out than it looks.
"
" Author: Tom Ryder <tom@sanctum.geek.nz>
" License: Same as Vim itself
"
if exists('loaded_insert_cancel') || &compatible || v:version < 700
  finish
endif
let loaded_insert_cancel = 1

" Set up the hooks described for the functions above, if Vim is new enough to
" support all the hooks required
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 insert_cancel#Cancel()<CR>