From b2d05a24bd541ca32ad77b41c07c13c25528dd6e Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 22 Jun 2019 22:37:23 +1200 Subject: Support mapping cancel keys, add Esc as default --- autoload/paste_insert.vim | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'autoload') diff --git a/autoload/paste_insert.vim b/autoload/paste_insert.vim index 277f333..6068b51 100644 --- a/autoload/paste_insert.vim +++ b/autoload/paste_insert.vim @@ -43,29 +43,39 @@ function! paste_insert#() abort endfunction -" Key that cancels the pending paste in normal mode, defaults to CTRL-C -let s:cancel = get(g:, 'paste_insert_cancel', '') +" Keys that cancel the pending paste in normal mode, defaults to Ctrl-C and +" Escape +let s:cancel = get(g:, 'paste_insert_cancel', ['', '']) -" Start the paste: save cancel key's prior function, remap it, set 'paste' +" 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 - let s:maparg = maparg(s:cancel) - let command = join([ - \ 'nnoremap' - \,s:cancel - \,':doautocmd paste_insert User Cancel' - \]) - execute command + for key in s:cancel + let s:maparg[key] = maparg(key) + 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 - let command = join( - \ s:maparg !=# '' - \ ? ['nnoremap', s:cancel, s:maparg] - \ : ['nunmap', s:cancel] - \) - execute command - unlet s:maparg + 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 -- cgit v1.2.3