From 131f57858c20b05706063e675d56f8b1252f78f3 Mon Sep 17 00:00:00 2001 From: Rui Marques Date: Sun, 16 Jun 2019 15:07:08 +0200 Subject: [PATCH] Added nvim function to README docs (Issue #64) --- README.md | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e6676ea..01f6342 100644 --- a/README.md +++ b/README.md @@ -68,22 +68,51 @@ the default is `/usr/local`. fzy is a drop in replacement for [selecta](https://github.com/garybernhardt/selecta), and can be used with its [usage examples](https://github.com/garybernhardt/selecta#usage-examples). -### Use with Vim +### Use with Vim/Neovim -fzy can be easily integrated with vim. +fzy can be easily integrated with vim/neovim. ``` vim -function! FzyCommand(choice_command, vim_command) - try - let output = system(a:choice_command . " | fzy ") - catch /Vim:Interrupt/ - " Swallow errors from ^C, allow redraw! below - endtry - redraw! - if v:shell_error == 0 && !empty(output) - exec a:vim_command . ' ' . output - endif -endfunction +if has('nvim') + function! FzyCommand(choice_command, vim_command) abort + let l:callback = { + \ 'window_id': win_getid(), + \ 'filename': tempname(), + \ 'vim_command': a:vim_command + \ } + + function! l:callback.on_exit(job_id, data, event) abort + bdelete! + call win_gotoid(self.window_id) + if filereadable(self.filename) + try + let l:selected_filename = readfile(self.filename)[0] + exec self.vim_command . l:selected_filename + catch /E684/ + endtry + endif + call delete(self.filename) + endfunction + + botright 10 new + let l:term_command = a:choice_command . ' | fzy > ' . l:callback.filename + silent call termopen(l:term_command, l:callback) + setlocal nonumber norelativenumber + startinsert + endfunction +else + function! FzyCommand(choice_command, vim_command) + try + let output = system(a:choice_command . " | fzy ") + catch /Vim:Interrupt/ + " Swallow errors from ^C, allow redraw! below + endtry + redraw! + if v:shell_error == 0 && !empty(output) + exec a:vim_command . ' ' . output + endif + endfunction +endif nnoremap e :call FzyCommand("find . -type f", ":e") nnoremap v :call FzyCommand("find . -type f", ":vs")