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
The walk stopped after 200 commits: lines older than faedae7 point at it.
Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 1–37why, one step back1" 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_win[neovim] Fix terminal buffer marker on WindowsMichael Smithbefore adoption5097e56
lines 38–39why, one step back38 let s:term_marker = '&::FZF'39Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 40–52why, one step back40 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)[vim] Use CRLF in batchfile for multibyte codepage (#1289)Jan Edmund Lazobefore adoptioncce17ad
line 53why, one step back53 return map(['@echo off', 'for /f "tokens=4" %%a in (''chcp'') do set origchcp=%%a', 'chcp 65001 > nul'] +Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 54why, one step back54 \ (type(a:cmds) == type([]) ? a:cmds : [a:cmds]) +[vim] Use CRLF in batchfile for multibyte codepage (#1289)Jan Edmund Lazobefore adoptioncce17ad
line 55why, one step back55 \ ['chcp %origchcp% > nul'], 'v:val."\r"')Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 56–57why, one step back56 endfunction57else[neovim] Fix terminal buffer marker on WindowsMichael Smithbefore adoption5097e56
lines 58–59why, one step back58 let s:term_marker = ";#FZF"59Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 60–72why, one step back60 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')[vim] Escape backslashes in fzf#shellescape (#1021)Jan Edmund Lazobefore adoption3399e39
lines 73–74why, one step back73 let escaped = substitute(escaped, '\(\\\+\)\(\\^\)', '\1\1\2', 'g')74 return '^"'.substitute(escaped, '\(\\\+\)$', '\1\1', '').'^"'Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 75–155why, one step back75endfunction7677function! 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)[vim] Use fnameescape to escape command line argumentsJunegunn Choibefore adoption7727ad4
lines 156–157why, one step back156 let path = fnameescape(a:path)157 return s:is_win ? escape(path, '$') : pathFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 158–207why, one step back158endfunction159160" 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)[vim] Allow Funcref in g:fzf_actionJunegunn Choibefore adoption2069bbc
lines 208–211why, one step back208 let Cmd = get(a:action, key, 'e')209 if type(Cmd) == type(function('call'))210 return Cmd(a:lines)211 endifFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 212–226why, one step back212 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 else[vim] Allow Funcref in g:fzf_actionJunegunn Choibefore adoption2069bbc
line 227why, one step back227 call s:open(Cmd, item)Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 228–233why, one step back228 endif229 if !has('patch-8.0.0177') && !has('nvim-0.2') && exists('#BufEnter')230 \ && isdirectory(item)231 doautocmd BufEnter232 endif233 endfor[vim] Ignore Vim:Interrupt when "Abort" selected on E325Junegunn Choibefore adoption6eac4af
line 234why, one step back234 catch /^Vim:Interrupt$/Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 235–335why, one step back235 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 opts334endfunction335[neovim] Set &shell to sh (again) after opening a new windowJunegunn Choibefore adoption48adad5
lines 336–337why, one step back336function! s:use_sh()337 let [shell, shellslash] = [&shell, &shellslash]Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 338–343why, one step back338 if s:is_win339 set shell=cmd.exe340 set noshellslash341 else342 set shell=sh343 endif[neovim] Set &shell to sh (again) after opening a new windowJunegunn Choibefore adoption48adad5
lines 344–345why, one step back344 return [shell, shellslash]345endfunctionFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 346why, one step back346[neovim] Set &shell to sh (again) after opening a new windowJunegunn Choibefore adoption48adad5
lines 347–350why, one step back347function! fzf#run(...) abort348try349 let [shell, shellslash] = s:use_sh()350Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 351–359why, one step back351 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 endtry359[vim] Fix directory switching around sink functionJunegunn Choibefore adoption8c6fcee
line 360why, one step back360 if !has_key(dict, 'dir')Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 361–366why, one step back361 let dict.dir = s:fzf_getcwd()362 endif363 if has('win32unix') && has_key(dict, 'dir')364 let dict.dir = fnamemodify(dict.dir, ':p')365 endif366[vim] Don't pipe FZF_DEFAULT_COMMAND in Windows (#969)Jan Edmund Lazobefore adoption68bd410
lines 367–368why, one step back367 if !has_key(dict, 'source') && !empty($FZF_DEFAULT_COMMAND) && !s:is_win368 let temps.source = s:fzf_tempname()Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 369why, one step back369 call writefile(s:wrap_cmds(split($FZF_DEFAULT_COMMAND, "\n")), temps.source)[vim] Don't pipe FZF_DEFAULT_COMMAND in Windows (#969)Jan Edmund Lazobefore adoption68bd410
line 370why, one step back370 let dict.source = (empty($SHELL) ? &shell : $SHELL).' '.fzf#shellescape(temps.source)Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 371–376why, one step back371 endif372373 if has_key(dict, 'source')374 let source = dict.source375 let type = type(source)376 if type == 1[vim] Add parens around piped source commands (#1029)Jack O'Connorbefore adoptionb27dc3e
line 377why, one step back377 let prefix = '( '.source.' )|'Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 378–389why, one step back378 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)[vim] Use Vim 8 terminal when appropriateJunegunn Choibefore adoption077ae51
lines 390–391why, one step back390 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')) &&Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 392why, one step back392 \ executable('tput') && filereadable('/dev/tty')[neovim] Fix Neovim plugin to use terminal instead of --heightJunegunn Choibefore adoptionc261446
lines 393–395why, one step back393 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 ||[vim] Don't use :terminal on msys2 or Cygwin (#1155)Jan Edmund Lazobefore adoption37d4015
line 396why, one step back396 \ has_vim8_term && !has('win32unix') && (has('gui_running') || s:is_win || !use_height && s:present(dict, 'down', 'up', 'left', 'right', 'window'))Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 397–418why, one step back397 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 lines418finally[neovim] Set &shell to sh (again) after opening a new windowJunegunn Choibefore adoption48adad5
line 419why, one step back419 let [&shell, &shellslash] = [shell, shellslash]Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 420–457why, one step back420endtry421endfunction422423function! 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()[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
lines 458–461why, one step back458 let w:fzf_pushd = {459 \ 'command': haslocaldir() ? 'lcd' : (exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'),460 \ 'origin': cwd461 \ }Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 462why, one step back462 execute 'lcd' s:escape(a:dict.dir)[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
lines 463–466why, one step back463 let cwd = s:fzf_getcwd()464 let w:fzf_pushd.dir = cwd465 let a:dict.pushd = w:fzf_pushd466 return cwdFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 467why, one step back467 endif[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 468why, one step back468 return ''Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 469–476why, one step back469endfunction470471augroup fzf_popd472 autocmd!473 autocmd WinEnter * call s:dopopd()474augroup END475476function! s:dopopd()[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 477why, one step back477 if !exists('w:fzf_pushd')Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 478–479why, one step back478 return479 endif[vim] Fix directory switching around sink functionJunegunn Choibefore adoption8c6fcee
lines 480–498why, one step back480481 " 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 endif[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 499why, one step back499 unlet w:fzf_pushdFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 500–553why, one step back500endfunction501502function! 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 = {}[neovim] Fix lcd when fzf job exits on Windows (#970)Jan Edmund Lazobefore adoption940214a
lines 554–555why, one step back554 let fzf.dict = a:dict555 let fzf.temps = a:tempsFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 556why, one step back556 function! fzf.on_exit(job_id, exit_status, event) dict[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 557why, one step back557 call s:pushd(self.dict)[neovim] Fix lcd when fzf job exits on Windows (#970)Jan Edmund Lazobefore adoption940214a
lines 558–559why, one step back558 let lines = s:collect(self.temps)559 call s:callback(self.dict, lines)Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 560–583why, one step back560 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:command[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
lines 584–585why, one step back584 let cwd = s:pushd(a:dict)585 if len(cwd)Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 586why, one step back586 " -c '#{pane_current_path}' is only available on tmux 1.9 or above[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 587why, one step back587 let command = join(['cd', fzf#shellescape(cwd), '&&', command])Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 588–608why, one step back588 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 endif608[vim] Make list options compatible with layout optionsJunegunn Choibefore adoptioneb3afc0
line 609why, one step back609 let opts = s:evaluate_opts(get(a:dict, 'options', '')).$FZF_DEFAULT_OPTSFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 610–656why, one step back610 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)[neovim] Set &shell to sh (again) after opening a new windowJunegunn Choibefore adoption48adad5
line 657why, one step back657 call s:use_sh()Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 658–672why, one step back658 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 endfunction[vim] s:execute_term works in GVim on WindowsJan Edmund Lazobefore adoption6c41c95
line 673why, one step back673 function! fzf.on_exit(id, code, ...)Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 674–707why, one step back674 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 try[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 708why, one step back708 call s:pushd(a:dict)[neovim] use batchfile for s:execute_term in WindowsJan Edmund Lazobefore adoption446e044
lines 709–715why, one step back709 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 endif[vim] s:execute_term works in GVim on WindowsJan Edmund Lazobefore adoption6c41c95
lines 716–719why, one step back716 let command .= s:term_marker717 if has('nvim')718 call termopen(command, fzf)719 else[vim] Fix terminal buffer cleanup on Vim 8Junegunn Choibefore adoption5c34ab6
line 720why, one step back720 let fzf.buf = term_start([&shell, &shellcmdflag, command], {'curwin': 1, 'exit_cb': function(fzf.on_exit)})[vim] Remove unnecessary term_wait workaroundJunegunn Choibefore adoption54a0265
line 721why, one step back721 if !has('patch-8.0.1261') && !has('nvim') && !s:is_win[vim] Fix terminal buffer cleanup on Vim 8Junegunn Choibefore adoption5c34ab6
line 722why, one step back722 call term_wait(fzf.buf, 20)[vim] Use Vim 8 terminal when appropriateJunegunn Choibefore adoption077ae51
line 723why, one step back723 endif[vim] s:execute_term works in GVim on WindowsJan Edmund Lazobefore adoption6c41c95
line 724why, one step back724 endifFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 725why, one step back725 finally[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 726why, one step back726 call s:dopopd()Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 727why, one step back727 endtry[nvim] Disable number in fzf bufferJunegunn Choibefore adoptiona2604c0
line 728why, one step back728 setlocal nospell bufhidden=wipe nobuflisted nonumberFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 729–744why, one step back729 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) abort[vim] Fix directory switching around sink functionJunegunn Choibefore adoption8c6fcee
line 745why, one step back745 let popd = has_key(a:dict, 'pushd')Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 746why, one step back746 if popd[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 747why, one step back747 let w:fzf_pushd = a:dict.pushdFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 748–770why, one step back748 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 popd[vim] Clear temporary window-local working directoryJunegunn Choibefore adoption13803d0
line 771why, one step back771 let w:fzf_pushd = a:dict.pushdFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 772–781why, one step back772 call s:dopopd()773 endif774endfunction775776let s:default_action = {777 \ 'ctrl-t': 'tab split',778 \ 'ctrl-x': 'split',779 \ 'ctrl-v': 'vsplit' }780781function! s:shortpath()[vim] Do not pathshorten prompt in cygwin (#1043)Jan Edmund Lazobefore adoption79d2ef4
lines 782–785why, one step back782 let short = fnamemodify(getcwd(), ':~:.')783 if !has('win32unix')784 let short = pathshorten(short)785 endifFix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
line 786why, one step back786 let slash = (s:is_win && !&shellslash) ? '\' : '/'[vim] Fix escape of backslash in s:shortpathJan Edmund Lazobefore adoption1fcc07e
line 787why, one step back787 return empty(short) ? '~'.slash : short . (short =~ escape(slash, '\').'$' ? '' : slash)Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 788–801why, one step back788endfunction789790function! 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 endif[vim] Update FZF command not set up lengthy prompt on narrow screenJunegunn Choibefore adoption5edc3f7
line 802why, one step back802 let prompt = strwidth(prompt) < &columns - 20 ? prompt : '> 'Fix FZF_CTRL_T_COMMAND example for fishJunegunn Choibefore adoptionfaedae7
lines 803–811why, one step back803 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