" Autoloaded entry point function function! paste_insert#() abort " Set up an event table augroup paste_insert autocmd! " Set up the paste and tell the user autocmd User Start \ call s:Start() | echo 'Paste ready' " When starting insert mode, add completion hook for when we leave autocmd InsertEnter * \ autocmd paste_insert InsertLeave * \ doautocmd paste_insert User Complete " User waits too long in normal mode, timeout autocmd CursorHold * \ doautocmd paste_insert User Timeout " User leaves the buffer or window, abort autocmd BufLeave,WinLeave * \ doautocmd paste_insert User Abort " Exit condition reporting autocmd User Abort \ echo 'Paste aborted' autocmd User Cancel \ echo 'Paste cancelled' autocmd User Complete \ echo 'Paste completed' autocmd User Timeout \ echo 'Paste timeout' " End the paste and clear the events table autocmd User Abort,Cancel,Complete,Timeout \ call s:Stop() | autocmd! paste_insert augroup END " Trigger the starting actions doautocmd paste_insert User Start endfunction " Keys that cancel the pending paste in normal mode, defaults to Ctrl-C and " Escape let s:cancel = get(g:, 'paste_insert_cancel', ['', '']) " Cache for the prior functions of those keys, in case the user has already " mapped them let s:maparg = {} " Start the paste: save each cancel key's prior function, remap it, set " 'paste' function! s:Start() abort for key in s:cancel let s:maparg[key] = maparg(key, 'n') let command = join([ \ 'nnoremap' \,key \,':doautocmd paste_insert User Cancel' \]) execute command endfor set paste endfunction " Stop the paste: unset 'paste', restore prior function of cancel key function! s:Stop() abort set nopaste for key in s:cancel let command = join( \ s:maparg[key] !=# '' \ ? ['nnoremap', key, s:maparg[key]] \ : ['nunmap', key] \) execute command unlet s:maparg[key] endfor endfunction