vim-unimpaired icon indicating copy to clipboard operation
vim-unimpaired copied to clipboard

Possible optimizations for s:putline (affects ]p, [p, ...)

Open aktau opened this issue 10 years ago • 3 comments

Currently s:putline reads like this:

function! s:putline(how, map) abort
  let [body, type] = [getreg(v:register), getregtype(v:register)]
  call setreg(v:register, body, 'l')
  exe 'normal! "'.v:register.a:how
  call setreg(v:register, body, type)
  if type !=# 'V'
    silent! call repeat#set("\<Plug>unimpairedPut".a:map)
  endif
endfunction

I was wondering if it would also yield good results to replace call setreg(v:register, body, 'l') with call setreg(v:register, '', 'al') and call setreg(v:register, body, type) with call setreg(v:register, '', 'a' . type). I read about this approach on this vim_dev thread: https://groups.google.com/forum/#!msg/vim_dev/SgjFRJemgKM/ztt0q3bkR10J. In it, Tony Mechelynk says something interesting:

Well, what if I yank (in Firefox, say, or in Thunderbird) a multiline sentence from the middle of a paragraph? As in the example below, I mean? With this newfangled system, I won't be able to insert it in the middle of a different paragraph in Vim, except by calling setreg('+', '', 'ac') to force it to characterwise.

The reason I'm looking at this is that when interaction with the system clipboard is enabled in Neovim, it invokes the Vim interpreter which in turn invokes xsel/xlip/pbcopy (whatever's installed). So this means calling into the interpreter, starting up a binary and reading its output into a buffer (or writing it to the application). In s:putline this happens a few times, causing a noticeable slowdown. The bug (which is otherwise irrelevant) where I first posted some findings is https://github.com/neovim/neovim/issues/1690, should you have an interest.

Perhaps with some changes here and there, I can make it acceptable again.

Thanks for the great plugin!

aktau avatar Jan 20 '15 19:01 aktau

I don't see a harm in trying, if you want to make a pull request.

tpope avatar Jan 21 '15 00:01 tpope

It would also avoid the need to retrieve the body variable, which saves another system register retrieval. I can see how this might end up being fast enough even when delegating to system binaries.

Of course the "optimal" solution would be to be able to paste with an extra modifier, overriding the MCHAR/MLINE/... mode of the register. It would be easy to implement, but incompatible with vanilla Vim, so let's not go there for the time being.

I'll see if I can rustle up a PR sometime soon.

aktau avatar Jan 21 '15 09:01 aktau

Unfortunately, it seems that a bug (or inclarity in the docs or user error on my part) is preventing this from working properly: https://code.google.com/p/vim/issues/detail?id=323&thanks=323&ts=1422321366

Current test code:

function! s:putline(how, map) abort
  let type = getregtype(v:register)
  call setreg(v:register, '', 'aV')
  exe 'normal! "'.v:register.a:how
  call setreg(v:register, '', 'a' . type)
  if type !=# 'V'
    silent! call repeat#set("\<Plug>unimpairedPut".a:map)
  endif
endfunction

aktau avatar Jan 27 '15 01:01 aktau