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 +++++++++++++++++++++++++++----------------- doc/paste_insert.txt | 5 +++-- 2 files changed, 30 insertions(+), 19 deletions(-) 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 diff --git a/doc/paste_insert.txt b/doc/paste_insert.txt index 73036e4..ff17437 100644 --- a/doc/paste_insert.txt +++ b/doc/paste_insert.txt @@ -28,8 +28,9 @@ The `(PasteInsert)` map in normal mode just does `:PasteInsert`. OPTIONS *paste_insert-options* *g:paste_insert_cancel* -Set `g:paste_insert_cancel` to the key you want to cancel the pending paste in -normal mode. This defaults to '', for CTRL-C. +Set `g:paste_insert_cancel` to a |List| of the keys you want to cancel the +pending paste in normal mode. This defaults to `['', '']`, for +CTRL-C and Escape. AUTHOR *paste_insert-author* -- cgit v1.2.3