fzf.vim
This file
Path
plugin/fzf.vim
778 lines · 21.9 KiB
Last changed by
[neovim] Allow running FZF in multiple windowsJunegunn Choibefore adoptione825b07
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 function! s:fzf_call(fn, ...)39 let shellslash = &shellslash40 try41 set noshellslash42 return call(a:fn, a:000)43 finally44 let &shellslash = shellslash45 endtry46 endfunction4748 " Use utf-8 for fzf.vim commands49 " Return array of shell commands for cmd.exe50 function! s:wrap_cmds(cmds)51 return ['@echo off', 'for /f "tokens=4" %%a in (''chcp'') do set origchcp=%%a', 'chcp 65001 > nul'] +52 \ (type(a:cmds) == type([]) ? a:cmds : [a:cmds]) +53 \ ['chcp %origchcp% > nul']54 endfunction55else56 function! s:fzf_call(fn, ...)57 return call(a:fn, a:000)58 endfunction5960 function! s:wrap_cmds(cmds)61 return a:cmds62 endfunction63endif6465function! s:shellesc_cmd(arg)66 let escaped = substitute(a:arg, '[&|<>()@^]', '^&', 'g')67 let escaped = substitute(escaped, '%', '%%', 'g')68 let escaped = substitute(escaped, '"', '\\^&', 'g')69 let escaped = substitute(escaped, '\(\\\+\)\(\\^\)', '\1\1\2', 'g')70 return '^"'.substitute(escaped, '\(\\\+\)$', '\1\1', '').'^"'71endfunction7273function! fzf#shellescape(arg, ...)74 let shell = get(a:000, 0, &shell)75 if shell =~# 'cmd.exe$'76 return s:shellesc_cmd(a:arg)77 endif78 return s:fzf_call('shellescape', a:arg)79endfunction8081function! s:fzf_getcwd()82 return s:fzf_call('getcwd')83endfunction8485function! s:fzf_fnamemodify(fname, mods)86 return s:fzf_call('fnamemodify', a:fname, a:mods)87endfunction8889function! s:fzf_expand(fmt)90 return s:fzf_call('expand', a:fmt, 1)91endfunction9293function! s:fzf_tempname()94 return s:fzf_call('tempname')95endfunction9697let s:default_layout = { 'down': '~40%' }98let s:layout_keys = ['window', 'up', 'down', 'left', 'right']99let s:fzf_go = s:base_dir.'/bin/fzf'100let s:fzf_tmux = s:base_dir.'/bin/fzf-tmux'101let s:install = s:base_dir.'/install'102let s:installed = 0103104let s:cpo_save = &cpo105set cpo&vim106107function! s:fzf_exec()108 if !exists('s:exec')109 if executable(s:fzf_go)110 let s:exec = s:fzf_go111 elseif executable('fzf')112 let s:exec = 'fzf'113 elseif s:is_win && !has('win32unix')114 call s:warn('fzf executable not found.')115 call s:warn('Download fzf binary for Windows from https://github.com/junegunn/fzf-bin/releases/')116 call s:warn('and place it as '.s:base_dir.'\bin\fzf.exe')117 throw 'fzf executable not found'118 elseif !s:installed && executable(s:install) &&119 \ input('fzf executable not found. Download binary? (y/n) ') =~? '^y'120 redraw121 echo122 call s:warn('Downloading fzf binary. Please wait ...')123 let s:installed = 1124 call system(s:install.' --bin')125 return s:fzf_exec()126 else127 redraw128 throw 'fzf executable not found'129 endif130 endif131 return fzf#shellescape(s:exec)132endfunction133134function! s:tmux_enabled()135 if has('gui_running')136 return 0137 endif138139 if exists('s:tmux')140 return s:tmux141 endif142143 let s:tmux = 0144 if exists('$TMUX') && executable(s:fzf_tmux)145 let output = system('tmux -V')146 let s:tmux = !v:shell_error && output >= 'tmux 1.7'147 endif148 return s:tmux149endfunction150151function! s:escape(path)152 let path = fnameescape(a:path)153 return s:is_win ? escape(path, '$') : path154endfunction155156" Upgrade legacy options157function! s:upgrade(dict)158 let copy = copy(a:dict)159 if has_key(copy, 'tmux')160 let copy.down = remove(copy, 'tmux')161 endif162 if has_key(copy, 'tmux_height')163 let copy.down = remove(copy, 'tmux_height')164 endif165 if has_key(copy, 'tmux_width')166 let copy.right = remove(copy, 'tmux_width')167 endif168 return copy169endfunction170171function! s:error(msg)172 echohl ErrorMsg173 echom a:msg174 echohl None175endfunction176177function! s:warn(msg)178 echohl WarningMsg179 echom a:msg180 echohl None181endfunction182183function! s:has_any(dict, keys)184 for key in a:keys185 if has_key(a:dict, key)186 return 1187 endif188 endfor189 return 0190endfunction191192function! s:open(cmd, target)193 if stridx('edit', a:cmd) == 0 && s:fzf_fnamemodify(a:target, ':p') ==# s:fzf_expand('%:p')194 return195 endif196 execute a:cmd s:escape(a:target)197endfunction198199function! s:common_sink(action, lines) abort200 if len(a:lines) < 2201 return202 endif203 let key = remove(a:lines, 0)204 let Cmd = get(a:action, key, 'e')205 if type(Cmd) == type(function('call'))206 return Cmd(a:lines)207 endif208 if len(a:lines) > 1209 augroup fzf_swap210 autocmd SwapExists * let v:swapchoice='o'211 \| call s:warn('fzf: E325: swap file exists: '.s:fzf_expand('<afile>'))212 augroup END213 endif214 try215 let empty = empty(s:fzf_expand('%')) && line('$') == 1 && empty(getline(1)) && !&modified216 let autochdir = &autochdir217 set noautochdir218 for item in a:lines219 if empty220 execute 'e' s:escape(item)221 let empty = 0222 else223 call s:open(Cmd, item)224 endif225 if !has('patch-8.0.0177') && !has('nvim-0.2') && exists('#BufEnter')226 \ && isdirectory(item)227 doautocmd BufEnter228 endif229 endfor230 finally231 let &autochdir = autochdir232 silent! autocmd! fzf_swap233 endtry234endfunction235236function! s:get_color(attr, ...)237 let gui = has('termguicolors') && &termguicolors238 let fam = gui ? 'gui' : 'cterm'239 let pat = gui ? '^#[a-f0-9]\+' : '^[0-9]\+$'240 for group in a:000241 let code = synIDattr(synIDtrans(hlID(group)), a:attr, fam)242 if code =~? pat243 return code244 endif245 endfor246 return ''247endfunction248249function! s:defaults()250 let rules = copy(get(g:, 'fzf_colors', {}))251 let colors = join(map(items(filter(map(rules, 'call("s:get_color", v:val)'), '!empty(v:val)')), 'join(v:val, ":")'), ',')252 return empty(colors) ? '' : ('--color='.colors)253endfunction254255function! s:validate_layout(layout)256 for key in keys(a:layout)257 if index(s:layout_keys, key) < 0258 throw printf('Invalid entry in g:fzf_layout: %s (allowed: %s)%s',259 \ key, join(s:layout_keys, ', '), key == 'options' ? '. Use $FZF_DEFAULT_OPTS.' : '')260 endif261 endfor262 return a:layout263endfunction264265function! s:evaluate_opts(options)266 return type(a:options) == type([]) ?267 \ join(map(copy(a:options), 'fzf#shellescape(v:val)')) : a:options268endfunction269270" [name string,] [opts dict,] [fullscreen boolean]271function! fzf#wrap(...)272 let args = ['', {}, 0]273 let expects = map(copy(args), 'type(v:val)')274 let tidx = 0275 for arg in copy(a:000)276 let tidx = index(expects, type(arg), tidx)277 if tidx < 0278 throw 'Invalid arguments (expected: [name string] [opts dict] [fullscreen boolean])'279 endif280 let args[tidx] = arg281 let tidx += 1282 unlet arg283 endfor284 let [name, opts, bang] = args285286 if len(name)287 let opts.name = name288 end289290 " Layout: g:fzf_layout (and deprecated g:fzf_height)291 if bang292 for key in s:layout_keys293 if has_key(opts, key)294 call remove(opts, key)295 endif296 endfor297 elseif !s:has_any(opts, s:layout_keys)298 if !exists('g:fzf_layout') && exists('g:fzf_height')299 let opts.down = g:fzf_height300 else301 let opts = extend(opts, s:validate_layout(get(g:, 'fzf_layout', s:default_layout)))302 endif303 endif304305 " Colors: g:fzf_colors306 let opts.options = s:defaults() .' '. s:evaluate_opts(get(opts, 'options', ''))307308 " History: g:fzf_history_dir309 if len(name) && len(get(g:, 'fzf_history_dir', ''))310 let dir = s:fzf_expand(g:fzf_history_dir)311 if !isdirectory(dir)312 call mkdir(dir, 'p')313 endif314 let history = fzf#shellescape(dir.'/'.name)315 let opts.options = join(['--history', history, opts.options])316 endif317318 " Action: g:fzf_action319 if !s:has_any(opts, ['sink', 'sink*'])320 let opts._action = get(g:, 'fzf_action', s:default_action)321 let opts.options .= ' --expect='.join(keys(opts._action), ',')322 function! opts.sink(lines) abort323 return s:common_sink(self._action, a:lines)324 endfunction325 let opts['sink*'] = remove(opts, 'sink')326 endif327328 return opts329endfunction330331function! fzf#run(...) abort332try333 let oshell = &shell334 let useshellslash = &shellslash335336 if s:is_win337 set shell=cmd.exe338 set noshellslash339 else340 set shell=sh341 endif342343 let dict = exists('a:1') ? s:upgrade(a:1) : {}344 let temps = { 'result': s:fzf_tempname() }345 let optstr = s:evaluate_opts(get(dict, 'options', ''))346 try347 let fzf_exec = s:fzf_exec()348 catch349 throw v:exception350 endtry351352 if has('nvim') && !has_key(dict, 'dir')353 let dict.dir = s:fzf_getcwd()354 endif355 if has('win32unix') && has_key(dict, 'dir')356 let dict.dir = fnamemodify(dict.dir, ':p')357 endif358359 if !has_key(dict, 'source') && !empty($FZF_DEFAULT_COMMAND) && !s:is_win360 let temps.source = s:fzf_tempname()361 call writefile(s:wrap_cmds(split($FZF_DEFAULT_COMMAND, "\n")), temps.source)362 let dict.source = (empty($SHELL) ? &shell : $SHELL).' '.fzf#shellescape(temps.source)363 endif364365 if has_key(dict, 'source')366 let source = dict.source367 let type = type(source)368 if type == 1369 let prefix = source.'|'370 elseif type == 3371 let temps.input = s:fzf_tempname()372 call writefile(source, temps.input)373 let prefix = (s:is_win ? 'type ' : 'cat ').fzf#shellescape(temps.input).'|'374 else375 throw 'Invalid source type'376 endif377 else378 let prefix = ''379 endif380381 let prefer_tmux = get(g:, 'fzf_prefer_tmux', 0)382 let use_height = has_key(dict, 'down') &&383 \ !(has('nvim') || s:is_win || has('win32unix') || s:present(dict, 'up', 'left', 'right')) &&384 \ executable('tput') && filereadable('/dev/tty')385 let use_term = has('nvim') && !s:is_win386 let use_tmux = (!use_height && !use_term || prefer_tmux) && !has('win32unix') && s:tmux_enabled() && s:splittable(dict)387 if prefer_tmux && use_tmux388 let use_height = 0389 let use_term = 0390 endif391 if use_height392 let height = s:calc_size(&lines, dict.down, dict)393 let optstr .= ' --height='.height394 elseif use_term395 let optstr .= ' --no-height'396 endif397 let command = prefix.(use_tmux ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result398399 if use_term400 return s:execute_term(dict, command, temps)401 endif402403 let lines = use_tmux ? s:execute_tmux(dict, command, temps)404 \ : s:execute(dict, command, use_height, temps)405 call s:callback(dict, lines)406 return lines407finally408 let &shell = oshell409 let &shellslash = useshellslash410endtry411endfunction412413function! s:present(dict, ...)414 for key in a:000415 if !empty(get(a:dict, key, ''))416 return 1417 endif418 endfor419 return 0420endfunction421422function! s:fzf_tmux(dict)423 let size = ''424 for o in ['up', 'down', 'left', 'right']425 if s:present(a:dict, o)426 let spec = a:dict[o]427 if (o == 'up' || o == 'down') && spec[0] == '~'428 let size = '-'.o[0].s:calc_size(&lines, spec, a:dict)429 else430 " Legacy boolean option431 let size = '-'.o[0].(spec == 1 ? '' : substitute(spec, '^\~', '', ''))432 endif433 break434 endif435 endfor436 return printf('LINES=%d COLUMNS=%d %s %s %s --',437 \ &lines, &columns, fzf#shellescape(s:fzf_tmux), size, (has_key(a:dict, 'source') ? '' : '-'))438endfunction439440function! s:splittable(dict)441 return s:present(a:dict, 'up', 'down') && &lines > 15 ||442 \ s:present(a:dict, 'left', 'right') && &columns > 40443endfunction444445function! s:pushd(dict)446 if s:present(a:dict, 'dir')447 let cwd = s:fzf_getcwd()448 if get(a:dict, 'prev_dir', '') ==# cwd449 return 1450 endif451 let a:dict.prev_dir = cwd452 execute 'lcd' s:escape(a:dict.dir)453 let a:dict.dir = s:fzf_getcwd()454 return 1455 endif456 return 0457endfunction458459augroup fzf_popd460 autocmd!461 autocmd WinEnter * call s:dopopd()462augroup END463464function! s:dopopd()465 if !exists('w:fzf_dir') || s:fzf_getcwd() != w:fzf_dir[1]466 return467 endif468 execute 'lcd' s:escape(w:fzf_dir[0])469 unlet w:fzf_dir470endfunction471472function! s:xterm_launcher()473 let fmt = 'xterm -T "[fzf]" -bg "\%s" -fg "\%s" -geometry %dx%d+%d+%d -e bash -ic %%s'474 if has('gui_macvim')475 let fmt .= '&& osascript -e "tell application \"MacVim\" to activate"'476 endif477 return printf(fmt,478 \ synIDattr(hlID("Normal"), "bg"), synIDattr(hlID("Normal"), "fg"),479 \ &columns, &lines/2, getwinposx(), getwinposy())480endfunction481unlet! s:launcher482if s:is_win || has('win32unix')483 let s:launcher = '%s'484else485 let s:launcher = function('s:xterm_launcher')486endif487488function! s:exit_handler(code, command, ...)489 if a:code == 130490 return 0491 elseif a:code > 1492 call s:error('Error running ' . a:command)493 if !empty(a:000)494 sleep495 endif496 return 0497 endif498 return 1499endfunction500501function! s:execute(dict, command, use_height, temps) abort502 call s:pushd(a:dict)503 if has('unix') && !a:use_height504 silent! !clear 2> /dev/null505 endif506 let escaped = (a:use_height || s:is_win) ? a:command : escape(substitute(a:command, '\n', '\\n', 'g'), '%#!')507 if has('gui_running')508 let Launcher = get(a:dict, 'launcher', get(g:, 'Fzf_launcher', get(g:, 'fzf_launcher', s:launcher)))509 let fmt = type(Launcher) == 2 ? call(Launcher, []) : Launcher510 if has('unix')511 let escaped = "'".substitute(escaped, "'", "'\"'\"'", 'g')."'"512 endif513 let command = printf(fmt, escaped)514 else515 let command = escaped516 endif517 if s:is_win518 let batchfile = s:fzf_tempname().'.bat'519 call writefile(s:wrap_cmds(command), batchfile)520 let command = batchfile521 let a:temps.batchfile = batchfile522 if has('nvim')523 let fzf = {}524 let fzf.dict = a:dict525 let fzf.temps = a:temps526 function! fzf.on_exit(job_id, exit_status, event) dict527 if s:present(self.dict, 'dir')528 execute 'lcd' s:escape(self.dict.dir)529 endif530 let lines = s:collect(self.temps)531 call s:callback(self.dict, lines)532 endfunction533 let cmd = 'start /wait cmd /c '.command534 call jobstart(cmd, fzf)535 return []536 endif537 elseif has('win32unix') && $TERM !=# 'cygwin'538 let shellscript = s:fzf_tempname()539 call writefile([command], shellscript)540 let command = 'cmd.exe /C '.fzf#shellescape('set "TERM=" & start /WAIT sh -c '.shellscript)541 let a:temps.shellscript = shellscript542 endif543 if a:use_height544 let stdin = has_key(a:dict, 'source') ? '' : '< /dev/tty'545 call system(printf('tput cup %d > /dev/tty; tput cnorm > /dev/tty; %s %s 2> /dev/tty', &lines, command, stdin))546 else547 execute 'silent !'.command548 endif549 let exit_status = v:shell_error550 redraw!551 return s:exit_handler(exit_status, command) ? s:collect(a:temps) : []552endfunction553554function! s:execute_tmux(dict, command, temps) abort555 let command = a:command556 if s:pushd(a:dict)557 " -c '#{pane_current_path}' is only available on tmux 1.9 or above558 let command = join(['cd', fzf#shellescape(a:dict.dir), '&&', command])559 endif560561 call system(command)562 let exit_status = v:shell_error563 redraw!564 return s:exit_handler(exit_status, command) ? s:collect(a:temps) : []565endfunction566567function! s:calc_size(max, val, dict)568 let val = substitute(a:val, '^\~', '', '')569 if val =~ '%$'570 let size = a:max * str2nr(val[:-2]) / 100571 else572 let size = min([a:max, str2nr(val)])573 endif574575 let srcsz = -1576 if type(get(a:dict, 'source', 0)) == type([])577 let srcsz = len(a:dict.source)578 endif579580 let opts = get(a:dict, 'options', '').$FZF_DEFAULT_OPTS581 let margin = stridx(opts, '--inline-info') > stridx(opts, '--no-inline-info') ? 1 : 2582 let margin += stridx(opts, '--header') > stridx(opts, '--no-header')583 return srcsz >= 0 ? min([srcsz + margin, size]) : size584endfunction585586function! s:getpos()587 return {'tab': tabpagenr(), 'win': winnr(), 'cnt': winnr('$'), 'tcnt': tabpagenr('$')}588endfunction589590function! s:split(dict)591 let directions = {592 \ 'up': ['topleft', 'resize', &lines],593 \ 'down': ['botright', 'resize', &lines],594 \ 'left': ['vertical topleft', 'vertical resize', &columns],595 \ 'right': ['vertical botright', 'vertical resize', &columns] }596 let ppos = s:getpos()597 try598 if s:present(a:dict, 'window')599 execute 'keepalt' a:dict.window600 elseif !s:splittable(a:dict)601 execute (tabpagenr()-1).'tabnew'602 else603 for [dir, triple] in items(directions)604 let val = get(a:dict, dir, '')605 if !empty(val)606 let [cmd, resz, max] = triple607 if (dir == 'up' || dir == 'down') && val[0] == '~'608 let sz = s:calc_size(max, val, a:dict)609 else610 let sz = s:calc_size(max, val, {})611 endif612 execute cmd sz.'new'613 execute resz sz614 return [ppos, {}]615 endif616 endfor617 endif618 return [ppos, { '&l:wfw': &l:wfw, '&l:wfh': &l:wfh }]619 finally620 setlocal winfixwidth winfixheight621 endtry622endfunction623624function! s:execute_term(dict, command, temps) abort625 let winrest = winrestcmd()626 let pbuf = bufnr('')627 let [ppos, winopts] = s:split(a:dict)628 let b:fzf = a:dict629 let fzf = { 'buf': bufnr(''), 'pbuf': pbuf, 'ppos': ppos, 'dict': a:dict, 'temps': a:temps,630 \ 'winopts': winopts, 'winrest': winrest, 'lines': &lines,631 \ 'columns': &columns, 'command': a:command }632 function! fzf.switch_back(inplace)633 if a:inplace && bufnr('') == self.buf634 if bufexists(self.pbuf)635 execute 'keepalt b' self.pbuf636 endif637 " No other listed buffer638 if bufnr('') == self.buf639 enew640 endif641 endif642 endfunction643 function! fzf.on_exit(id, code, _event)644 if s:getpos() == self.ppos " {'window': 'enew'}645 for [opt, val] in items(self.winopts)646 execute 'let' opt '=' val647 endfor648 call self.switch_back(1)649 else650 if bufnr('') == self.buf651 " We use close instead of bd! since Vim does not close the split when652 " there's no other listed buffer (nvim +'set nobuflisted')653 close654 endif655 execute 'tabnext' self.ppos.tab656 execute self.ppos.win.'wincmd w'657 endif658659 if bufexists(self.buf)660 execute 'bd!' self.buf661 endif662663 if &lines == self.lines && &columns == self.columns && s:getpos() == self.ppos664 execute self.winrest665 endif666667 if !s:exit_handler(a:code, self.command, 1)668 return669 endif670671 call s:pushd(self.dict)672 let lines = s:collect(self.temps)673 call s:callback(self.dict, lines)674 call self.switch_back(s:getpos() == self.ppos)675 endfunction676677 try678 if s:present(a:dict, 'dir')679 execute 'lcd' s:escape(a:dict.dir)680 endif681 call termopen(a:command . ';#FZF', fzf)682 finally683 if s:present(a:dict, 'dir')684 lcd -685 endif686 endtry687 setlocal nospell bufhidden=wipe nobuflisted nonumber688 setf fzf689 startinsert690 return []691endfunction692693function! s:collect(temps) abort694 try695 return filereadable(a:temps.result) ? readfile(a:temps.result) : []696 finally697 for tf in values(a:temps)698 silent! call delete(tf)699 endfor700 endtry701endfunction702703function! s:callback(dict, lines) abort704 " Since anything can be done in the sink function, there is no telling that705 " the change of the working directory was made by &autochdir setting.706 "707 " We use the following heuristic to determine whether to restore CWD:708 " - Always restore the current directory when &autochdir is disabled.709 " FIXME This makes it impossible to change directory from inside the sink710 " function when &autochdir is not used.711 " - In case of an error or an interrupt, a:lines will be empty.712 " And it will be an array of a single empty string when fzf was finished713 " without a match. In these cases, we presume that the change of the714 " directory is not expected and should be undone.715 let popd = has_key(a:dict, 'prev_dir') &&716 \ (!&autochdir || (empty(a:lines) || len(a:lines) == 1 && empty(a:lines[0])))717 if popd718 let w:fzf_dir = [a:dict.prev_dir, a:dict.dir]719 endif720721 try722 if has_key(a:dict, 'sink')723 for line in a:lines724 if type(a:dict.sink) == 2725 call a:dict.sink(line)726 else727 execute a:dict.sink s:escape(line)728 endif729 endfor730 endif731 if has_key(a:dict, 'sink*')732 call a:dict['sink*'](a:lines)733 endif734 catch735 if stridx(v:exception, ':E325:') < 0736 echoerr v:exception737 endif738 endtry739740 " We may have opened a new window or tab741 if popd742 let w:fzf_dir = [a:dict.prev_dir, a:dict.dir]743 call s:dopopd()744 endif745endfunction746747let s:default_action = {748 \ 'ctrl-t': 'tab split',749 \ 'ctrl-x': 'split',750 \ 'ctrl-v': 'vsplit' }751752function! s:shortpath()753 let short = pathshorten(fnamemodify(getcwd(), ':~:.'))754 let slash = (s:is_win && !&shellslash) ? '\' : '/'755 return empty(short) ? '~'.slash : short . (short =~ escape(slash, '\').'$' ? '' : slash)756endfunction757758function! s:cmd(bang, ...) abort759 let args = copy(a:000)760 let opts = { 'options': ['--multi'] }761 if len(args) && isdirectory(expand(args[-1]))762 let opts.dir = substitute(substitute(remove(args, -1), '\\\(["'']\)', '\1', 'g'), '[/\\]*$', '/', '')763 if s:is_win && !&shellslash764 let opts.dir = substitute(opts.dir, '/', '\\', 'g')765 endif766 let prompt = opts.dir767 else768 let prompt = s:shortpath()769 endif770 call extend(opts.options, ['--prompt', prompt])771 call extend(opts.options, args)772 call fzf#run(fzf#wrap('FZF', opts, a:bang))773endfunction774775command! -nargs=* -complete=dir -bang FZF call s:cmd(<bang>0, <f-args>)776777let &cpo = s:cpo_save778unlet s:cpo_save