diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2019-06-20 00:16:36 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2019-06-20 00:16:36 +1200 |
commit | 6a8fda5f3dd282bddf4c9353fee08c17aaf63396 (patch) | |
tree | 184336c84f28314db5916e0e1e267dd087b37943 | |
parent | First version (diff) | |
parent | Bump VERSION (diff) | |
download | vim-paste-insert-6a8fda5f3dd282bddf4c9353fee08c17aaf63396.tar.gz vim-paste-insert-6a8fda5f3dd282bddf4c9353fee08c17aaf63396.zip |
Merge branch 'release/v0.2.0'v0.2.0
* release/v0.2.0:
Add comments to plugin file
Add better messages, customisable stop key
Apply a little more structure to events
-rw-r--r-- | VERSION | 2 | ||||
-rw-r--r-- | autoload/paste_insert.vim | 69 | ||||
-rw-r--r-- | doc/paste_insert.txt | 8 | ||||
-rw-r--r-- | plugin/paste_insert.vim | 13 |
4 files changed, 85 insertions, 7 deletions
@@ -1 +1 @@ -0.1.0 +0.2.0 diff --git a/autoload/paste_insert.vim b/autoload/paste_insert.vim index 675fa5a..277f333 100644 --- a/autoload/paste_insert.vim +++ b/autoload/paste_insert.vim @@ -1,12 +1,71 @@ +" Autoloaded entry point function function! paste_insert#() abort + + " Set up an event table augroup paste_insert autocmd! - autocmd CursorHold,CursorMoved,User * - \ set nopaste paste? - \|autocmd! paste_insert + + " 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 + \ 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 - set paste paste? + + " Trigger the starting actions + doautocmd paste_insert User Start + +endfunction + +" Key that cancels the pending paste in normal mode, defaults to CTRL-C +let s:cancel = get(g:, 'paste_insert_cancel', '<C-C>') + +" Start the paste: save 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 + \,':<C-U>doautocmd paste_insert User Cancel<CR>' + \]) + execute command + 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 endfunction diff --git a/doc/paste_insert.txt b/doc/paste_insert.txt index 9b63079..73036e4 100644 --- a/doc/paste_insert.txt +++ b/doc/paste_insert.txt @@ -8,7 +8,7 @@ after the insert ends, to avoid the annoyances caused by forgetting to do so. It includes a timeout if insert mode isn't entered within 'updatetime' seconds, or if the user navigates away from the buffer or window. It can also -be cancelled with CTRL-C in normal mode. +be cancelled with a key in normal mode, by default CTRL-C. REQUIREMENTS *paste_insert-requirements* @@ -25,6 +25,12 @@ MAPPINGS *paste_insert-mappings* *<Plug>(PasteInsert)* The `<Plug>(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 '<C-C>', for CTRL-C. + AUTHOR *paste_insert-author* Written and maintained by Tom Ryder <tom@sanctum.geek.nz>. diff --git a/plugin/paste_insert.vim b/plugin/paste_insert.vim index 2c17f80..a792a50 100644 --- a/plugin/paste_insert.vim +++ b/plugin/paste_insert.vim @@ -1,8 +1,21 @@ +" +" This small plugin provides a simple "one shot paste" method, with a command +" or mapping to prefix opening an insert, with the 'paste' option +" automatically set after the insert ends, to avoid the annoyances caused by +" forgetting to do so. +" +" Author: Tom Ryder <tom@sanctum.geek.nz> +" License: Same as Vim itself +" if exists('loaded_paste_insert') || &compatible finish endif let loaded_paste_insert = 1 + +" Command interface command! -bar PasteInsert \ call paste_insert#() + +" Normal mode mapping interface nnoremap <Plug>PasteInsert \ :<C-U>PasteInsert<CR> |