[vim] Neovim compatibility (#137)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use terminal emulator of Neovim to open fzf
authored by Junegunn Choi · committed by Junegunn Choi · parent 68503d3 · Browse files · 2 files·+97 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+97 added−24 removed
- other:+97 added−24 removed
Provenance
- Commit
622e69f- Tree
bb64b4a- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
68503d3
2 files changed+97 added−24 removed
modified:plugin/fzf.vim+92 added-24 removed
120 unmodified lines121 endif122 else123 let prefix = ''124 endif125- let split = s:tmux_enabled() && s:tmux_splittable(dict)126- let command = prefix.(split ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result125+ let tmux = !has('nvim') && s:tmux_enabled() && s:splittable(dict)126+ let command = prefix.(tmux ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result127 128 try129- if split129+ if tmux130 return s:execute_tmux(dict, command, temps)131+ elseif has('nvim')132+ return s:execute_term(dict, command, temps)133 else134 return s:execute(dict, command, temps)135 endif136 finally14 unmodified lines151 let size = ''152 for o in ['up', 'down', 'left', 'right']153 if s:present(a:dict, o)154 let size = '-'.o[0].(a:dict[o] == 1 ? '' : a:dict[o])155+ break156 endif157 endfor158 return printf('LINES=%d COLUMNS=%d %s %s %s --',159 \ &lines, &columns, s:fzf_tmux, size, (has_key(a:dict, 'source') ? '' : '-'))160 endfunction161 159-function! s:tmux_splittable(dict)162+function! s:splittable(dict)163 return s:present(a:dict, 'up', 'down', 'left', 'right')164 endfunction165 166 function! s:pushd(dict)167 if s:present(a:dict, 'dir')165- let a:dict.prev_dir = getcwd()168+ let cwd = getcwd()169+ if get(a:dict, 'prev_dir', '') ==# cwd170+ return 1171+ endif172+ let a:dict.prev_dir = cwd173 execute 'chdir '.s:escape(a:dict.dir)174 let a:dict.dir = getcwd()175 return 1176 endif39 unmodified lines216 call system(command)217 return s:callback(a:dict, a:temps)218 endfunction219 220+function! s:calc_size(max, val)221+ if a:val =~ '%$'222+ return a:max * str2nr(a:val[:-2]) / 100223+ else224+ return min([a:max, a:val])225+ endif226+endfunction227+228+function! s:split(dict)229+ let directions = {230+ \ 'up': ['topleft', &lines],231+ \ 'down': ['botright', &lines],232+ \ 'left': ['vertical topleft', &columns],233+ \ 'right': ['vertical botright', &columns] }234+ try235+ for [dir, pair] in items(directions)236+ let val = get(a:dict, dir, '')237+ if !empty(val)238+ let [cmd, max] = pair239+ execute cmd s:calc_size(max, val).'new'240+ return241+ endif242+ endfor243+ if s:present(a:dict, 'window')244+ execute a:dict.window245+ else246+ tabnew247+ endif248+ finally249+ setlocal winfixwidth winfixheight250+ endtry251+endfunction252+253+function! s:execute_term(dict, command, temps)254+ call s:pushd(a:dict)255+ call s:split(a:dict)256+257+ let fzf = { 'buf': bufnr('%'), 'dict': a:dict, 'temps': a:temps }258+ function! fzf.on_exit(id, code)259+ execute 'bd!' self.buf260+ call s:pushd(self.dict)261+ try262+ call s:callback(self.dict, self.temps)263+ finally264+ call s:popd(self.dict)265+ endtry266+ endfunction267+268+ call termopen(a:command, fzf)269+ file [FZF]270+ startinsert271+ return []272+endfunction273+274 function! s:callback(dict, temps)275 if !filereadable(a:temps.result)276 let lines = []277 else6 unmodified lines284 execute a:dict.sink s:escape(line)285 endif286 endfor287 endif288+ if has_key(a:dict, 'sink*')289+ call a:dict['sink*'](lines)290+ endif291 endif292 293 for tf in values(a:temps)294 silent! call delete(tf)1 unmodified line296 297 return lines298 endfunction299 300+function! s:cmd_callback(lines) abort301+ if empty(a:lines)302+ return303+ endif304+ let key = remove(a:lines, 0)305+ if key == 'ctrl-t' | let cmd = 'tabedit'306+ elseif key == 'ctrl-x' | let cmd = 'split'307+ elseif key == 'ctrl-v' | let cmd = 'vsplit'308+ else | let cmd = 'e'309+ endif310+ call s:pushd(s:opts)311+ try312+ for item in a:lines313+ execute cmd s:escape(item)314+ endfor315+ finally316+ call s:popd(s:opts)317+ endtry318+endfunction319+320 function! s:cmd(bang, ...) abort321 let args = copy(a:000)322 if !s:legacy323 let args = insert(args, '--expect=ctrl-t,ctrl-x,ctrl-v', 0)6 unmodified lines330 let opts.down = get(g:, 'fzf_tmux_height', s:default_tmux_height)331 endif332 333 if s:legacy250- call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))334+ call fzf#run(extend({ 'options': join(args), 'sink': 'e' }, opts))335 else252- let output = fzf#run(extend({ 'options': join(args) }, opts))253- if empty(output)254- return255- endif256- let key = remove(output, 0)257- if key == 'ctrl-t' | let cmd = 'tabedit'258- elseif key == 'ctrl-x' | let cmd = 'split'259- elseif key == 'ctrl-v' | let cmd = 'vsplit'260- else | let cmd = 'e'261- endif262- try263- call s:pushd(opts)264- for item in output265- execute cmd s:escape(item)266- endfor267- finally268- call s:popd(opts)269- endtry336+ let s:opts = opts337+ call fzf#run(extend({ 'options': join(args), 'sink*': function('<sid>cmd_callback') }, opts))338 endif339 endfunction340 341 command! -nargs=* -complete=dir -bang FZF call s:cmd('<bang>' == '!', <f-args>)4 unmodified linesmodified:README.md+5 added
315 unmodified lines316 | `source` | string | External command to generate input to fzf (e.g. `find .`) |317 | `source` | list | Vim list as input to fzf |318 | `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |319 | `sink` | funcref | Reference to function to process each selected item |320+| `sink*` | funcref | Similar to `sink`, but takes the list of output lines at once |321 | `options` | string | Options to fzf |322 | `dir` | string | Working directory |323 | `up`/`down`/`left`/`right` | number/string | Use tmux pane with the given size (e.g. `20`, `50%`) |324+| `window` (*Neovim only*) | string | Command to open fzf window (e.g. `vertical aboveleft 30new`) |325 | `launcher` | string | External terminal emulator to start fzf with (Only used in GVim) |326 327+*However on Neovim `fzf#run` is asynchronous and does not return values so you328+should use `sink` or `sink+` to process the output from fzf.*329+330 ##### Examples331 332 If `sink` option is not given, `fzf#run` will simply return the list.333 178 unmodified lines