diff --git a/plugin/fzf.vim b/plugin/fzf.vim index 5458896..2c9f33a 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -367,19 +367,44 @@ command! -bang Snippets call s:snippets(0) " ---------------------------------------------------------------------------- " Completion helper " ---------------------------------------------------------------------------- -function! s:complete_insert(data) - execute 'normal!' (empty(s:query) ? 'a' : 'ciW')."\=a:data\" - startinsert! -endfunction +inoremap (-fzf-complete-trigger) :call complete_trigger() -function! fzf#complete(source, ...) - let s:query = get(a:, 1, expand('')) +function! s:complete_trigger() call s:fzf({ - \ 'source': a:source, + \ 'source': s:source, \ 'sink': function('s:complete_insert'), \ 'options': '+m -q '.shellescape(s:query)}, 0) endfunction +function! s:complete_insert(data) + let chars = strchars(s:query) + if chars == 0 | let del = '' + elseif chars == 1 | let del = '"_x' + else | let del = (chars - 1).'"_dvh' + endif + execute 'normal!' (s:eol ? '' : 'h').del.(s:eol ? 'a': 'i').a:data + if has('nvim') + call feedkeys('a') + else + execute "normal! \la" + endif +endfunction + +function! fzf#complete(source) + let s:source = a:source + + let eol = col('$') + let ve = &ve + set ve=all + let s:eol = col('.') == eol + let &ve = ve + + let s:query = col('.') == 1 ? '' : + \ matchstr(getline('.')[0 : col('.')-2], '\k*$') + call feedkeys("\(-fzf-complete-trigger)") + return '' +endfunction + " ------------------------------------------------------------------ let &cpo = s:cpo_save unlet s:cpo_save