fzf.vim
This file
Path
plugin/fzf.vim
811 lines · 23.0 KiB
Last changed by
[vim] Fix directory switching around sink functionJunegunn Choibefore adoption8c6fcee
History of this fileTake it
1" Copyright (c) 2017 Junegunn Choi2"3" MIT License4"5" Permission is hereby granted, free of charge, to any person obtaining6" a copy of this software and associated documentation files (the7" "Software"), to deal in the Software without restriction, including8" without limitation the rights to use, copy, modify, merge, publish,9" distribute, sublicense, and/or sell copies of the Software, and to10" permit persons to whom the Software is furnished to do so, subject to11" the following conditions:12"13" The above copyright notice and this permission notice shall be14" included in all copies or substantial portions of the Software.15"16" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND19" NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE20" LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.2324if exists('g:loaded_fzf')25 finish26endif27let g:loaded_fzf = 12829let s:is_win = has('win32') || has('win64')30if s:is_win && &shellslash31 set noshellslash32 let s:base_dir = expand('<sfile>:h:h')33 set shellslash34else35 let s:base_dir = expand('<sfile>:h:h')36endif37if s:is_win38 let s:term_marker = '&::FZF'3940 function! s:fzf_call(fn, ...)41 let shellslash = &shellslash42 try43 set noshellslash44 return call(a:fn, a:000)45 finally46 let &shellslash = shellslash47 endtry48 endfunction4950 " Use utf-8 for fzf.vim commands51 " Return array of shell commands for cmd.exe52 function! s:wrap_cmds(cmds)53 return map(['@echo off', 'for /f "tokens=4" %%a in (''chcp'') do set origchcp=%%a', 'chcp 65001 > nul'] +54 \ (type(a:cmds) == type([]) ? a:cmds : [a:cmds]) +55 \ ['chcp %origchcp% > nul'], 'v:val."\r"')56 endfunction57else58 let s:term_marker = ";#FZF"5960 function! s:fzf_call(fn, ...)61 return call(a:fn, a:000)62 endfunction6364 function! s:wrap_cmds(cmds)65 return a:cmds66 endfunction67endif6869function! s:shellesc_cmd(arg)70 let escaped = substitute(a:arg, '[&|<>()@^]', '^&', 'g')71 let escaped = substitute(escaped, '%', '%%', 'g')72 let escaped = substitute(escaped, '"', '\\^&', 'g')73 let escaped = substitute(escaped, '\(\\\+\)\(\\^\)', '\1\1\2', 'g')74 return '^"'.substitute(escaped, '\(\\\+\)$', '\1\1', '').'^"'75endfunction7677function! fzf#shellescape(arg, ...)78 let shell = get(a:000, 0, &shell)79 if shell =~# 'cmd.exe$'80 return s:shellesc_cmd(a:arg)81 endif82 return s:fzf_call('shellescape', a:arg)83endfunction8485function! s:fzf_getcwd()86 return s:fzf_call('getcwd')87endfunction8889function! s:fzf_fnamemodify(fname, mods)90 return s:fzf_call('fnamemodify', a:fname, a:mods)91endfunction9293function! s:fzf_expand(fmt)94 return s:fzf_call('expand', a:fmt, 1)95endfunction9697function! s:fzf_tempname()98 return s:fzf_call('tempname')99endfunction100101let s:default_layout = { 'down': '~40%' }102let s:layout_keys = ['window', 'up', 'down', 'left', 'right']103let s:fzf_go = s:base_dir.'/bin/fzf'104let s:fzf_tmux = s:base_dir.'/bin/fzf-tmux'105let s:install = s:base_dir.'/install'106let s:installed = 0107108let s:cpo_save = &cpo109set cpo&vim110111function! s:fzf_exec()112 if !exists('s:exec')113 if executable(s:fzf_go)114 let s:exec = s:fzf_go115 elseif executable('fzf')116 let s:exec = 'fzf'117 elseif s:is_win && !has('win32unix')118 call s:warn('fzf executable not found.')119 call s:warn('Download fzf binary for Windows from https://github.com/junegunn/fzf-bin/releases/')120 call s:warn('and place it as '.s:base_dir.'\bin\fzf.exe')121 throw 'fzf executable not found'122 elseif !s:installed && executable(s:install) &&123 \ input('fzf executable not found. Download binary? (y/n) ') =~? '^y'124 redraw125 echo126 call s:warn('Downloading fzf binary. Please wait ...')127 let s:installed = 1128 call system(s:install.' --bin')129 return s:fzf_exec()130 else131 redraw132 throw 'fzf executable not found'133 endif134 endif135 return fzf#shellescape(s:exec)136endfunction137138function! s:tmux_enabled()139 if has('gui_running')140 return 0141 endif142143 if exists('s:tmux')144 return s:tmux145 endif146147 let s:tmux = 0148 if exists('$TMUX') && executable(s:fzf_tmux)149 let output = system('tmux -V')150 let s:tmux = !v:shell_error && output >= 'tmux 1.7'151 endif152 return s:tmux153endfunction154155function! s:escape(path)156 let path = fnameescape(a:path)157 return s:is_win ? escape(path, '$') : path158endfunction159160" Upgrade legacy options161function! s:upgrade(dict)162 let copy = copy(a:dict)163 if has_key(copy, 'tmux')164 let copy.down = remove(copy, 'tmux')165 endif166 if has_key(copy, 'tmux_height')167 let copy.down = remove(copy, 'tmux_height')168 endif169 if has_key(copy, 'tmux_width')170 let copy.right = remove(copy, 'tmux_width')171 endif172 return copy173endfunction174175function! s:error(msg)176 echohl ErrorMsg177 echom a:msg178 echohl None179endfunction180181function! s:warn(msg)182 echohl WarningMsg183 echom a:msg184 echohl None185endfunction186187function! s:has_any(dict, keys)188 for key in a:keys189 if has_key(a:dict, key)190 return 1191 endif192 endfor193 return 0194endfunction195196function! s:open(cmd, target)197 if stridx('edit', a:cmd) == 0 && s:fzf_fnamemodify(a:target, ':p') ==# s:fzf_expand('%:p')198 return199 endif200 execute a:cmd s:escape(a:target)201endfunction202203function! s:common_sink(action, lines) abort204 if len(a:lines) < 2205 return206 endif207 let key = remove(a:lines, 0)208 let Cmd = get(a:action, key, 'e')209 if type(Cmd) == type(function('call'))210 return Cmd(a:lines)211 endif212 if len(a:lines) > 1213 augroup fzf_swap214 autocmd SwapExists * let v:swapchoice='o'215 \| call s:warn('fzf: E325: swap file exists: '.s:fzf_expand('<afile>'))216 augroup END217 endif218 try219 let empty = empty(s:fzf_expand('%')) && line('$') == 1 && empty(getline(1)) && !&modified220 let autochdir = &autochdir221 set noautochdir222 for item in a:lines223 if empty224 execute 'e' s:escape(item)225 let empty = 0226 else227 call s:open(Cmd, item)228 endif229 if !has('patch-8.0.0177') && !has('nvim-0.2') && exists('#BufEnter')230 \ && isdirectory(item)231 doautocmd BufEnter232 endif233 endfor234 catch /^Vim:Interrupt$/235 finally236 let &autochdir = autochdir237 silent! autocmd! fzf_swap238 endtry239endfunction240241function! s:get_color(attr, ...)242 let gui = has('termguicolors') && &termguicolors243 let fam = gui ? 'gui' : 'cterm'244 let pat = gui ? '^#[a-f0-9]\+' : '^[0-9]\+$'245 for group in a:000246 let code = synIDattr(synIDtrans(hlID(group)), a:attr, fam)247 if code =~? pat248 return code249 endif250 endfor251 return ''252endfunction253254function! s:defaults()255 let rules = copy(get(g:, 'fzf_colors', {}))256 let colors = join(map(items(filter(map(rules, 'call("s:get_color", v:val)'), '!empty(v:val)')), 'join(v:val, ":")'), ',')257 return empty(colors) ? '' : ('--color='.colors)258endfunction259260function! s:validate_layout(layout)261 for key in keys(a:layout)262 if index(s:layout_keys, key) < 0263 throw printf('Invalid entry in g:fzf_layout: %s (allowed: %s)%s',264 \ key, join(s:layout_keys, ', '), key == 'options' ? '. Use $FZF_DEFAULT_OPTS.' : '')265 endif266 endfor267 return a:layout268endfunction269270function! s:evaluate_opts(options)271 return type(a:options) == type([]) ?272 \ join(map(copy(a:options), 'fzf#shellescape(v:val)')) : a:options273endfunction274275" [name string,] [opts dict,] [fullscreen boolean]276function! fzf#wrap(...)277 let args = ['', {}, 0]278 let expects = map(copy(args), 'type(v:val)')279 let tidx = 0280 for arg in copy(a:000)281 let tidx = index(expects, type(arg), tidx)282 if tidx < 0283 throw 'Invalid arguments (expected: [name string] [opts dict] [fullscreen boolean])'284 endif285 let args[tidx] = arg286 let tidx += 1287 unlet arg288 endfor289 let [name, opts, bang] = args290291 if len(name)292 let opts.name = name293 end294295 " Layout: g:fzf_layout (and deprecated g:fzf_height)296 if bang297 for key in s:layout_keys298 if has_key(opts, key)299 call remove(opts, key)300 endif301 endfor302 elseif !s:has_any(opts, s:layout_keys)303 if !exists('g:fzf_layout') && exists('g:fzf_height')304 let opts.down = g:fzf_height305 else306 let opts = extend(opts, s:validate_layout(get(g:, 'fzf_layout', s:default_layout)))307 endif308 endif309310 " Colors: g:fzf_colors311 let opts.options = s:defaults() .' '. s:evaluate_opts(get(opts, 'options', ''))312313 " History: g:fzf_history_dir314 if len(name) && len(get(g:, 'fzf_history_dir', ''))315 let dir = s:fzf_expand(g:fzf_history_dir)316 if !isdirectory(dir)317 call mkdir(dir, 'p')318 endif319 let history = fzf#shellescape(dir.'/'.name)320 let opts.options = join(['--history', history, opts.options])321 endif322323 " Action: g:fzf_action324 if !s:has_any(opts, ['sink', 'sink*'])325 let opts._action = get(g:, 'fzf_action', s:default_action)326 let opts.options .= ' --expect='.join(keys(opts._action), ',')327 function! opts.sink(lines) abort328 return s:common_sink(self._action, a:lines)329 endfunction330 let opts['sink*'] = remove(opts, 'sink')331 endif332333 return opts334endfunction335336function! s:use_sh()337 let [shell, shellslash] = [&shell, &shellslash]338 if s:is_win339 set shell=cmd.exe340 set noshellslash341 else342 set shell=sh343 endif344 return [shell, shellslash]345endfunction346347function! fzf#run(...) abort348try349 let [shell, shellslash] = s:use_sh()350351 let dict = exists('a:1') ? s:upgrade(a:1) : {}352 let temps = { 'result': s:fzf_tempname() }353 let optstr = s:evaluate_opts(get(dict, 'options', ''))354 try355 let fzf_exec = s:fzf_exec()356 catch357 throw v:exception358 endtry359360 if !has_key(dict, 'dir')361 let dict.dir = s:fzf_getcwd()362 endif363 if has('win32unix') && has_key(dict, 'dir')364 let dict.dir = fnamemodify(dict.dir, ':p')365 endif366367 if !has_key(dict, 'source') && !empty($FZF_DEFAULT_COMMAND) && !s:is_win368 let temps.source = s:fzf_tempname()369 call writefile(s:wrap_cmds(split($FZF_DEFAULT_COMMAND, "\n")), temps.source)370 let dict.source = (empty($SHELL) ? &shell : $SHELL).' '.fzf#shellescape(temps.source)371 endif372373 if has_key(dict, 'source')374 let source = dict.source375 let type = type(source)376 if type == 1377 let prefix = '( '.source.' )|'378 elseif type == 3379 let temps.input = s:fzf_tempname()380 call writefile(source, temps.input)381 let prefix = (s:is_win ? 'type ' : 'cat ').fzf#shellescape(temps.input).'|'382 else383 throw 'Invalid source type'384 endif385 else386 let prefix = ''387 endif388389 let prefer_tmux = get(g:, 'fzf_prefer_tmux', 0)390 let use_height = has_key(dict, 'down') && !has('gui_running') &&391 \ !(has('nvim') || s:is_win || has('win32unix') || s:present(dict, 'up', 'left', 'right', 'window')) &&392 \ executable('tput') && filereadable('/dev/tty')393 let has_vim8_term = has('terminal') && has('patch-8.0.995')394 let has_nvim_term = has('nvim-0.2.1') || has('nvim') && !s:is_win395 let use_term = has_nvim_term ||396 \ has_vim8_term && !has('win32unix') && (has('gui_running') || s:is_win || !use_height && s:present(dict, 'down', 'up', 'left', 'right', 'window'))397 let use_tmux = (!use_height && !use_term || prefer_tmux) && !has('win32unix') && s:tmux_enabled() && s:splittable(dict)398 if prefer_tmux && use_tmux399 let use_height = 0400 let use_term = 0401 endif402 if use_height403 let height = s:calc_size(&lines, dict.down, dict)404 let optstr .= ' --height='.height405 elseif use_term406 let optstr .= ' --no-height'407 endif408 let command = prefix.(use_tmux ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result409410 if use_term411 return s:execute_term(dict, command, temps)412 endif413414 let lines = use_tmux ? s:execute_tmux(dict, command, temps)415 \ : s:execute(dict, command, use_height, temps)416 call s:callback(dict, lines)417 return lines418finally419 let [&shell, &shellslash] = [shell, shellslash]420endtry421endfunction422423function! s:present(dict, ...)424 for key in a:000425 if !empty(get(a:dict, key, ''))426 return 1427 endif428 endfor429 return 0430endfunction431432function! s:fzf_tmux(dict)433 let size = ''434 for o in ['up', 'down', 'left', 'right']435 if s:present(a:dict, o)436 let spec = a:dict[o]437 if (o == 'up' || o == 'down') && spec[0] == '~'438 let size = '-'.o[0].s:calc_size(&lines, spec, a:dict)439 else440 " Legacy boolean option441 let size = '-'.o[0].(spec == 1 ? '' : substitute(spec, '^\~', '', ''))442 endif443 break444 endif445 endfor446 return printf('LINES=%d COLUMNS=%d %s %s %s --',447 \ &lines, &columns, fzf#shellescape(s:fzf_tmux), size, (has_key(a:dict, 'source') ? '' : '-'))448endfunction449450function! s:splittable(dict)451 return s:present(a:dict, 'up', 'down') && &lines > 15 ||452 \ s:present(a:dict, 'left', 'right') && &columns > 40453endfunction454455function! s:pushd(dict)456 if s:present(a:dict, 'dir')457 let cwd = s:fzf_getcwd()458 let w:fzf_pushd = {459 \ 'command': haslocaldir() ? 'lcd' : (exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'),460 \ 'origin': cwd461 \ }462 execute 'lcd' s:escape(a:dict.dir)463 let cwd = s:fzf_getcwd()464 let w:fzf_pushd.dir = cwd465 let a:dict.pushd = w:fzf_pushd466 return cwd467 endif468 return ''469endfunction470471augroup fzf_popd472 autocmd!473 autocmd WinEnter * call s:dopopd()474augroup END475476function! s:dopopd()477 if !exists('w:fzf_pushd')478 return479 endif480481 " FIXME: We temporarily change the working directory to 'dir' entry482 " of options dictionary (set to the current working directory if not given)483 " before running fzf.484 "485 " e.g. call fzf#run({'dir': '/tmp', 'source': 'ls', 'sink': 'e'})486 "487 " After processing the sink function, we have to restore the current working488 " directory. But doing so may not be desirable if the function changed the489 " working directory on purpose.490 "491 " So how can we tell if we should do it or not? A simple heuristic we use492 " here is that we change directory only if the current working directory493 " matches 'dir' entry. However, it is possible that the sink function did494 " change the directory to 'dir'. In that case, the user will have an495 " unexpected result.496 if s:fzf_getcwd() ==# w:fzf_pushd.dir497 execute w:fzf_pushd.command s:escape(w:fzf_pushd.origin)498 endif499 unlet w:fzf_pushd500endfunction501502function! s:xterm_launcher()503 let fmt = 'xterm -T "[fzf]" -bg "\%s" -fg "\%s" -geometry %dx%d+%d+%d -e bash -ic %%s'504 if has('gui_macvim')505 let fmt .= '&& osascript -e "tell application \"MacVim\" to activate"'506 endif507 return printf(fmt,508 \ synIDattr(hlID("Normal"), "bg"), synIDattr(hlID("Normal"), "fg"),509 \ &columns, &lines/2, getwinposx(), getwinposy())510endfunction511unlet! s:launcher512if s:is_win || has('win32unix')513 let s:launcher = '%s'514else515 let s:launcher = function('s:xterm_launcher')516endif517518function! s:exit_handler(code, command, ...)519 if a:code == 130520 return 0521 elseif a:code > 1522 call s:error('Error running ' . a:command)523 if !empty(a:000)524 sleep525 endif526 return 0527 endif528 return 1529endfunction530531function! s:execute(dict, command, use_height, temps) abort532 call s:pushd(a:dict)533 if has('unix') && !a:use_height534 silent! !clear 2> /dev/null535 endif536 let escaped = (a:use_height || s:is_win) ? a:command : escape(substitute(a:command, '\n', '\\n', 'g'), '%#!')537 if has('gui_running')538 let Launcher = get(a:dict, 'launcher', get(g:, 'Fzf_launcher', get(g:, 'fzf_launcher', s:launcher)))539 let fmt = type(Launcher) == 2 ? call(Launcher, []) : Launcher540 if has('unix')541 let escaped = "'".substitute(escaped, "'", "'\"'\"'", 'g')."'"542 endif543 let command = printf(fmt, escaped)544 else545 let command = escaped546 endif547 if s:is_win548 let batchfile = s:fzf_tempname().'.bat'549 call writefile(s:wrap_cmds(command), batchfile)550 let command = batchfile551 let a:temps.batchfile = batchfile552 if has('nvim')553 let fzf = {}554 let fzf.dict = a:dict555 let fzf.temps = a:temps556 function! fzf.on_exit(job_id, exit_status, event) dict557 call s:pushd(self.dict)558 let lines = s:collect(self.temps)559 call s:callback(self.dict, lines)560 endfunction561 let cmd = 'start /wait cmd /c '.command562 call jobstart(cmd, fzf)563 return []564 endif565 elseif has('win32unix') && $TERM !=# 'cygwin'566 let shellscript = s:fzf_tempname()567 call writefile([command], shellscript)568 let command = 'cmd.exe /C '.fzf#shellescape('set "TERM=" & start /WAIT sh -c '.shellscript)569 let a:temps.shellscript = shellscript570 endif571 if a:use_height572 let stdin = has_key(a:dict, 'source') ? '' : '< /dev/tty'573 call system(printf('tput cup %d > /dev/tty; tput cnorm > /dev/tty; %s %s 2> /dev/tty', &lines, command, stdin))574 else575 execute 'silent !'.command576 endif577 let exit_status = v:shell_error578 redraw!579 return s:exit_handler(exit_status, command) ? s:collect(a:temps) : []580endfunction581582function! s:execute_tmux(dict, command, temps) abort583 let command = a:command584 let cwd = s:pushd(a:dict)585 if len(cwd)586 " -c '#{pane_current_path}' is only available on tmux 1.9 or above587 let command = join(['cd', fzf#shellescape(cwd), '&&', command])588 endif589590 call system(command)591 let exit_status = v:shell_error592 redraw!593 return s:exit_handler(exit_status, command) ? s:collect(a:temps) : []594endfunction595596function! s:calc_size(max, val, dict)597 let val = substitute(a:val, '^\~', '', '')598 if val =~ '%$'599 let size = a:max * str2nr(val[:-2]) / 100600 else601 let size = min([a:max, str2nr(val)])602 endif603604 let srcsz = -1605 if type(get(a:dict, 'source', 0)) == type([])606 let srcsz = len(a:dict.source)607 endif608609 let opts = s:evaluate_opts(get(a:dict, 'options', '')).$FZF_DEFAULT_OPTS610 let margin = stridx(opts, '--inline-info') > stridx(opts, '--no-inline-info') ? 1 : 2611 let margin += stridx(opts, '--header') > stridx(opts, '--no-header')612 return srcsz >= 0 ? min([srcsz + margin, size]) : size613endfunction614615function! s:getpos()616 return {'tab': tabpagenr(), 'win': winnr(), 'cnt': winnr('$'), 'tcnt': tabpagenr('$')}617endfunction618619function! s:split(dict)620 let directions = {621 \ 'up': ['topleft', 'resize', &lines],622 \ 'down': ['botright', 'resize', &lines],623 \ 'left': ['vertical topleft', 'vertical resize', &columns],624 \ 'right': ['vertical botright', 'vertical resize', &columns] }625 let ppos = s:getpos()626 try627 if s:present(a:dict, 'window')628 execute 'keepalt' a:dict.window629 elseif !s:splittable(a:dict)630 execute (tabpagenr()-1).'tabnew'631 else632 for [dir, triple] in items(directions)633 let val = get(a:dict, dir, '')634 if !empty(val)635 let [cmd, resz, max] = triple636 if (dir == 'up' || dir == 'down') && val[0] == '~'637 let sz = s:calc_size(max, val, a:dict)638 else639 let sz = s:calc_size(max, val, {})640 endif641 execute cmd sz.'new'642 execute resz sz643 return [ppos, {}]644 endif645 endfor646 endif647 return [ppos, { '&l:wfw': &l:wfw, '&l:wfh': &l:wfh }]648 finally649 setlocal winfixwidth winfixheight650 endtry651endfunction652653function! s:execute_term(dict, command, temps) abort654 let winrest = winrestcmd()655 let pbuf = bufnr('')656 let [ppos, winopts] = s:split(a:dict)657 call s:use_sh()658 let b:fzf = a:dict659 let fzf = { 'buf': bufnr(''), 'pbuf': pbuf, 'ppos': ppos, 'dict': a:dict, 'temps': a:temps,660 \ 'winopts': winopts, 'winrest': winrest, 'lines': &lines,661 \ 'columns': &columns, 'command': a:command }662 function! fzf.switch_back(inplace)663 if a:inplace && bufnr('') == self.buf664 if bufexists(self.pbuf)665 execute 'keepalt b' self.pbuf666 endif667 " No other listed buffer668 if bufnr('') == self.buf669 enew670 endif671 endif672 endfunction673 function! fzf.on_exit(id, code, ...)674 if s:getpos() == self.ppos " {'window': 'enew'}675 for [opt, val] in items(self.winopts)676 execute 'let' opt '=' val677 endfor678 call self.switch_back(1)679 else680 if bufnr('') == self.buf681 " We use close instead of bd! since Vim does not close the split when682 " there's no other listed buffer (nvim +'set nobuflisted')683 close684 endif685 execute 'tabnext' self.ppos.tab686 execute self.ppos.win.'wincmd w'687 endif688689 if bufexists(self.buf)690 execute 'bd!' self.buf691 endif692693 if &lines == self.lines && &columns == self.columns && s:getpos() == self.ppos694 execute self.winrest695 endif696697 if !s:exit_handler(a:code, self.command, 1)698 return699 endif700701 call s:pushd(self.dict)702 let lines = s:collect(self.temps)703 call s:callback(self.dict, lines)704 call self.switch_back(s:getpos() == self.ppos)705 endfunction706707 try708 call s:pushd(a:dict)709 if s:is_win710 let fzf.temps.batchfile = s:fzf_tempname().'.bat'711 call writefile(s:wrap_cmds(a:command), fzf.temps.batchfile)712 let command = fzf.temps.batchfile713 else714 let command = a:command715 endif716 let command .= s:term_marker717 if has('nvim')718 call termopen(command, fzf)719 else720 let fzf.buf = term_start([&shell, &shellcmdflag, command], {'curwin': 1, 'exit_cb': function(fzf.on_exit)})721 if !has('patch-8.0.1261') && !has('nvim') && !s:is_win722 call term_wait(fzf.buf, 20)723 endif724 endif725 finally726 call s:dopopd()727 endtry728 setlocal nospell bufhidden=wipe nobuflisted nonumber729 setf fzf730 startinsert731 return []732endfunction733734function! s:collect(temps) abort735 try736 return filereadable(a:temps.result) ? readfile(a:temps.result) : []737 finally738 for tf in values(a:temps)739 silent! call delete(tf)740 endfor741 endtry742endfunction743744function! s:callback(dict, lines) abort745 let popd = has_key(a:dict, 'pushd')746 if popd747 let w:fzf_pushd = a:dict.pushd748 endif749750 try751 if has_key(a:dict, 'sink')752 for line in a:lines753 if type(a:dict.sink) == 2754 call a:dict.sink(line)755 else756 execute a:dict.sink s:escape(line)757 endif758 endfor759 endif760 if has_key(a:dict, 'sink*')761 call a:dict['sink*'](a:lines)762 endif763 catch764 if stridx(v:exception, ':E325:') < 0765 echoerr v:exception766 endif767 endtry768769 " We may have opened a new window or tab770 if popd771 let w:fzf_pushd = a:dict.pushd772 call s:dopopd()773 endif774endfunction775776let s:default_action = {777 \ 'ctrl-t': 'tab split',778 \ 'ctrl-x': 'split',779 \ 'ctrl-v': 'vsplit' }780781function! s:shortpath()782 let short = fnamemodify(getcwd(), ':~:.')783 if !has('win32unix')784 let short = pathshorten(short)785 endif786 let slash = (s:is_win && !&shellslash) ? '\' : '/'787 return empty(short) ? '~'.slash : short . (short =~ escape(slash, '\').'$' ? '' : slash)788endfunction789790function! s:cmd(bang, ...) abort791 let args = copy(a:000)792 let opts = { 'options': ['--multi'] }793 if len(args) && isdirectory(expand(args[-1]))794 let opts.dir = substitute(substitute(remove(args, -1), '\\\(["'']\)', '\1', 'g'), '[/\\]*$', '/', '')795 if s:is_win && !&shellslash796 let opts.dir = substitute(opts.dir, '/', '\\', 'g')797 endif798 let prompt = opts.dir799 else800 let prompt = s:shortpath()801 endif802 let prompt = strwidth(prompt) < &columns - 20 ? prompt : '> '803 call extend(opts.options, ['--prompt', prompt])804 call extend(opts.options, args)805 call fzf#run(fzf#wrap('FZF', opts, a:bang))806endfunction807808command! -nargs=* -complete=dir -bang FZF call s:cmd(<bang>0, <f-args>)809810let &cpo = s:cpo_save811unlet s:cpo_save