svermeulen / vim-easyclip

Simplified clipboard functionality for Vim
689 stars 21 forks source link

Highlight yanked area #104

Open Taverius opened 7 years ago

Taverius commented 7 years ago

I'm not even sure if its possible, but I would love to have this functionality.

rafaeln commented 7 years ago

Have you checked whether vim-highlightedyank is compatible with vim-easyclip?

vitmy0000 commented 7 years ago

Neither vim-highlightedyank nor vim-operator-flashy works with EasyClip.

Below can be a workaround for Y and yy, however solution for y seems not trivial. Any way combine two operators that accept same motion/text obect?

map y <Plug>(operator-flashy)
nmap Y :EasyClipBeforeYank<cr>y$:EasyClipOnYanksChanged<cr><Plug>(operator-flashy)$
nmap yy <Plug>YankLinePreserveCursorPosition<Plug>(operator-flashy)y
vitmy0000 commented 7 years ago

This may be a temporary solution:

hi HighlightedyankRegion ctermfg=Black ctermbg=Blue
function! s:sallowsleep(ms) abort
  let t = reltime()
  while !getchar(1) && a:ms - str2float(reltimestr(reltime(t))) * 1000.0 > 0
  endwhile
endfunction
function! HighlightYankedLine()
  let l:curline = line('.')
  call matchaddpos("HighlightedyankRegion", [l:curline])
  redraw
  call s:sallowsleep(500)
  call clearmatches()
endfunction
function! HighlightYankedEOL()
  let l:curline = line(".")
  let l:curcol = virtcol(".")
  let l:eofcol = virtcol("$")
  call matchaddpos("HighlightedyankRegion", [[l:curline, l:curcol, l:eofcol - l:curcol]])
  redraw
  call s:sallowsleep(500)
  call clearmatches()
endfunction
function! EasyClipYankMotionHighlithWrapper(type)
  call EasyClip#Yank#YankMotion(a:type)
  let l:startline = line("'[")
  let l:startcol = virtcol("'[")
  let l:endline = line("']")
  let l:endcol = virtcol("']")
  if a:type ==# 'char' && l:startline == l:endline
    call matchaddpos("HighlightedyankRegion", [[l:startline, l:startcol, l:endcol - l:startcol + 1]])
  elseif a:type ==# 'char' && l:startline != l:endline
    execute "normal! '["
    let l:firstlineEndcol = virtcol("$")
    call matchaddpos("HighlightedyankRegion", [[l:startline, l:startcol, l:firstlineEndcol - l:startcol]])
    for l:line in range(l:startline + 1, l:endline - 1)
      call matchaddpos("HighlightedyankRegion", [l:line])
    endfor
    call matchaddpos("HighlightedyankRegion", [[l:endline, 0, l:endcol]])
  elseif a:type ==# 'line'
    for l:line in range(l:startline, l:endline)
      call matchaddpos("HighlightedyankRegion", [l:line])
    endfor
  endif
  redraw
  call s:sallowsleep(500)
  call clearmatches()
endfunction
nnoremap <silent> yy :<c-u>call EasyClip#Yank#PreYankMotion()<cr>:call EasyClip#Yank#YankLine()<cr>:<c-u>call HighlightYankedLine()<cr>
nnoremap <silent> <expr> Y ":<c-u>call EasyClip#Yank#PreYankMotion()<cr>:set opfunc=EasyClip#Yank#YankMotion<cr>" . (v:count > 0 ? v:count : '') . "g@$:<c-u>call HighlightYankedEOL()<cr>"
nnoremap <silent> <expr> y ":<c-u>call EasyClip#Yank#PreYankMotion()<cr>:set opfunc=EasyClipYankMotionHighlithWrapper<cr>" . (v:count > 0 ? v:count : '') . "g@"
coachshea commented 6 years ago

If you are using a fairly recent version of vim or neovim, vim-highlightedyank is perfectly compatible. The TextYankPost event makes the 'y' mapping unnecessary and it now works "out of the box" and therefore is perfectly compatible with easyclip.