bfredl / nvim-miniyank

killring-alike plugin for neovim and vim 8 with no default mappings
MIT License
233 stars 15 forks source link

FZF integration #19

Open dkiyatkin opened 6 years ago

dkiyatkin commented 6 years ago

Hello, i created simple FZF integration! Maybe you can improve my code and add to this awesome plugin =)

function FZFYankList() abort
  function! KeyValue(key, val)
    let line = join(a:val[0], '\n')
    if (a:val[1] ==# 'V')
      let line = '\n'.line
    endif
    return a:key.' '.line
  endfunction
  return map(miniyank#read(), function('KeyValue'))
endfunction

function FZFYankHandler(opt, line) abort
  let key = substitute(a:line, ' .*', '', '')
  if !empty(a:line)
    let yanks = miniyank#read()[key]
    call miniyank#drop(yanks, a:opt)
  endif
endfunction

command YanksAfter call fzf#run(fzf#wrap('YanksAfter', {
\ 'source':  FZFYankList(),
\ 'sink':    function('FZFYankHandler', ['p']),
\ 'options': '--no-sort --prompt="Yanks-p> "',
\ }))

command YanksBefore call fzf#run(fzf#wrap('YanksBefore', {
\ 'source':  FZFYankList(),
\ 'sink':    function('FZFYankHandler', ['P']),
\ 'options': '--no-sort --prompt="Yanks-P> "',
\ }))

map <A-p> :YanksAfter<CR>
map <A-P> :YanksBefore<CR>
comfortablynick commented 4 years ago

Just finding this plugin. I also think the fzf integration is a great idea. I was able to simplify it to one function and it seems to work great for me.

function! s:fzf_miniyank(put_before, fullscreen) abort
    function! Sink(opt, line) abort
        let l:key = substitute(a:line, ' .*', '', '')
        if empty(a:line) | return | endif
        let l:yanks = miniyank#read()[l:key]
        call miniyank#drop(l:yanks, a:opt)
    endfunction

    let l:put_action = a:put_before ? 'P' : 'p'
    let l:name = a:put_before ? 'YanksBefore' : 'YanksAfter'
    let l:spec = {}
    let l:spec['source'] = map(miniyank#read(), {k,v -> k.' '.join(v[0], '\n')})
    let l:spec['sink'] = {val -> Sink(l:put_action, val)}
    let l:spec['options'] = '--no-sort --prompt="Yanks-'.l:put_action.'> "'
    call fzf#run(fzf#wrap(l:name, l:spec, a:fullscreen))
endfunction

command! -bang YanksBefore call s:fzf_miniyank(1, <bang>0)
command! -bang YanksAfter call s:fzf_miniyank(0, <bang>0)

map <A-p> :YanksAfter<CR>
map <A-P> :YanksBefore<CR>
bdlangton commented 2 years ago

First of all, thanks for the code snippets! I've used this for awhile now, but I noticed one issue that started happening. If I used either :YanksAfter or :YanksBefore, then tried using :Buffers (provided by the fzf plugin), I'd get the following error: E705: Variable name conflicts with existing function: Sink.

Simply changing Sink to something else in the two places it exists in the code above fixes it.