Skip to content

Commit

Permalink
Version 1.20
Browse files Browse the repository at this point in the history
BUG: Repeat always used the unnamed register. Add register registration to enhanced repeat.vim plugin. This also handles repetition when used together with the expression register "=. Requires a so far inofficial update to repeat.vim version 1.0 (that hopefully makes it into upstream), which is available at https://github.com/inkarkat/vim-repeat/zipball/1.0ENH1
  • Loading branch information
Ingo Karkat authored and vim-scripts committed Dec 9, 2011
1 parent 8a24b52 commit 0799772
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
7 changes: 7 additions & 0 deletions doc/UnconditionalPaste.txt
Expand Up @@ -77,6 +77,13 @@ IDEAS *UnconditionalPaste-ideas*
==============================================================================
HISTORY *UnconditionalPaste-history*

1.20 30-Sep-2011
BUG: Repeat always used the unnamed register. Add register registration to
enhanced repeat.vim plugin. This also handles repetition when used together
with the expression register "=. Requires a so far inofficial update to
repeat.vim version 1.0 (that hopefully makes it into upstream), which is
available at https://github.com/inkarkat/vim-repeat/zipball/1.0ENH1

1.11 06-Jun-2011
ENH: Support repetition of mappings through repeat.vim.

Expand Down
57 changes: 49 additions & 8 deletions plugin/UnconditionalPaste.vim
Expand Up @@ -13,6 +13,12 @@
" http://vim.wikia.com/wiki/Unconditional_linewise_or_characterwise_paste
"
" REVISION DATE REMARKS
" 1.20.012 29-Sep-2011 BUG: Repeat always used the unnamed register.
" Add register registration to enhanced repeat.vim
" plugin, which also handles repetition when used
" together with the expression register "=.
" BUG: Move <silent> maparg to <Plug> mapping to
" silence command repeat.
" 1.11.010 06-Jun-2011 ENH: Support repetition of mappings through
" repeat.vim.
" 1.10.009 12-Jan-2011 Incorporated suggestions by Peter Rincker
Expand Down Expand Up @@ -53,26 +59,53 @@ let g:loaded_UnconditionalPaste = 1
let s:save_cpo = &cpo
set cpo&vim

function! s:HandleExprReg( exprResult )
let s:exprResult = a:exprResult
endfunction

function! s:Flatten( text )
" Remove newline characters at the end of the text, convert all other
" newlines to a single space.
return substitute(substitute(a:text, "\n\\+$", '', 'g'), "\n\\+", ' ', 'g')
endfunction

function! s:Paste( regName, pasteType, pasteCmd )
let l:regType = getregtype(a:regName)
let l:regContent = getreg(a:regName, 1) " Expression evaluation inside function context may cause errors, therefore get unevaluated expression when a:regName ==# '='.

if a:regName ==# '='
" Cannot evaluate the expression register within a function; unscoped
" variables do not refer to the global scope. Therefore, evaluation
" happened earlier in the mappings, and stored this in s:exprResult.
" To get the expression result into the buffer, use the unnamed
" register, and restore it later.
let l:regName = '"'
let l:regContent = s:exprResult

let l:save_clipboard = &clipboard
set clipboard= " Avoid clobbering the selection and clipboard registers.
let l:save_reg = getreg(l:regName)
let l:save_regmode = getregtype(l:regName)
else
let l:regName = a:regName
endif

try
let l:regType = getregtype(a:regName)
let l:regContent = getreg(a:regName)
call setreg(a:regName, (a:pasteType ==# 'c' ? s:Flatten(l:regContent) : l:regContent), a:pasteType)
execute 'normal! "' . a:regName . (v:count ? v:count : '') . a:pasteCmd
call setreg(a:regName, l:regContent, l:regType)
call setreg(l:regName, (a:pasteType ==# 'c' ? s:Flatten(l:regContent) : l:regContent), a:pasteType)
execute 'normal! "' . l:regName . (v:count ? v:count : '') . a:pasteCmd
call setreg(l:regName, l:regContent, l:regType)
catch /^Vim\%((\a\+)\)\=:E/
" v:exception contains what is normally in v:errmsg, but with extra
" exception source info prepended, which we cut away.
let v:errmsg = substitute(v:exception, '^Vim\%((\a\+)\)\=:', '', '')
echohl ErrorMsg
echomsg v:errmsg
echohl None
finally
if a:regName ==# '='
call setreg('"', l:save_reg, l:save_regmode)
let &clipboard = l:save_clipboard
endif
endtry
endfunction

Expand All @@ -81,14 +114,22 @@ function! s:CreateMappings()
for [l:direction, l:pasteCmd] in [['After', 'p'], ['Before', 'P']]
let l:mappingName = 'UnconditionalPaste' . l:pasteName . l:direction
let l:plugMappingName = '<Plug>' . l:mappingName
execute printf('nnoremap %s :<C-u>call <SID>Paste(v:register, %s, %s)<Bar>silent! call repeat#set("\<lt>Plug>%s")<CR>',
execute printf('nnoremap <silent> %s :<C-u>' .
\ 'silent! call repeat#setreg("\<lt>Plug>%s", v:register)<Bar>' .
\ 'if v:register ==# "="<Bar>' .
\ ' call <SID>HandleExprReg(getreg("="))<Bar>' .
\ 'endif<Bar>' .
\ 'call <SID>Paste(v:register, %s, %s)<Bar>' .
\ 'silent! call repeat#set("\<lt>Plug>%s")<CR>',
\
\ l:plugMappingName,
\ l:mappingName,
\ string(l:pasteType),
\ string(l:pasteCmd),
\ l:mappingName
\)
if ! hasmapto(l:plugMappingName, 'n')
execute printf('nmap <silent> g%s%s %s',
execute printf('nmap g%s%s %s',
\ l:pasteType,
\ l:pasteCmd,
\ l:plugMappingName
Expand All @@ -102,4 +143,4 @@ delfunction s:CreateMappings

let &cpo = s:save_cpo
unlet s:save_cpo
" vim: set sts=4 sw=4 noexpandtab ff=unix fdm=syntax :
" vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax :

0 comments on commit 0799772

Please sign in to comment.