| 1 |
" Copyright (c) 2013-2026 Junegunn Choi |
| 2 |
" |
| 3 |
" MIT License |
| 4 |
" |
| 5 |
" Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
" a copy of this software and associated documentation files (the |
| 7 |
" "Software"), to deal in the Software without restriction, including |
| 8 |
" without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
" distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
" permit persons to whom the Software is furnished to do so, subject to |
| 11 |
" the following conditions: |
| 12 |
" |
| 13 |
" The above copyright notice and this permission notice shall be |
| 14 |
" 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 OF |
| 18 |
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
" NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 |
" LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 |
" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 |
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
|
| 24 |
if exists('g:loaded_fzf') |
| 25 |
finish |
| 26 |
endif |
| 27 |
" Bumped when the plugin gains behavior a caller may need to detect |
| 28 |
let g:loaded_fzf = 20260821 |
| 29 |
|
| 30 |
let s:is_win = has('win32') || has('win64') |
| 31 |
if s:is_win && &shellslash |
| 32 |
set noshellslash |
| 33 |
let s:base_dir = expand('<sfile>:h:h') |
| 34 |
set shellslash |
| 35 |
else |
| 36 |
let s:base_dir = expand('<sfile>:h:h') |
| 37 |
endif |
| 38 |
if s:is_win |
| 39 |
let s:term_marker = '&::FZF' |
| 40 |
|
| 41 |
function! s:fzf_call(fn, ...) |
| 42 |
let shellslash = &shellslash |
| 43 |
try |
| 44 |
set noshellslash |
| 45 |
return call(a:fn, a:000) |
| 46 |
finally |
| 47 |
let &shellslash = shellslash |
| 48 |
endtry |
| 49 |
endfunction |
| 50 |
|
| 51 |
" Use utf-8 for fzf.vim commands |
| 52 |
" Return array of shell commands for cmd.exe |
| 53 |
function! s:enc_to_cp(str) |
| 54 |
if !has('iconv') |
| 55 |
return a:str |
| 56 |
endif |
| 57 |
if !exists('s:codepage') |
| 58 |
let s:codepage = libcallnr('kernel32.dll', 'GetACP', 0) |
| 59 |
endif |
| 60 |
return iconv(a:str, &encoding, 'cp'.s:codepage) |
| 61 |
endfunction |
| 62 |
function! s:wrap_cmds(cmds) |
| 63 |
return map(['@echo off'] |
| 64 |
\ + (has('gui_running') ? ['set TERM= > nul'] : []) |
| 65 |
\ + (type(a:cmds) == type([]) ? a:cmds : [a:cmds]), |
| 66 |
\ '<SID>enc_to_cp(v:val."\r")') |
| 67 |
endfunction |
| 68 |
else |
| 69 |
let s:term_marker = ";#FZF" |
| 70 |
|
| 71 |
function! s:fzf_call(fn, ...) |
| 72 |
return call(a:fn, a:000) |
| 73 |
endfunction |
| 74 |
|
| 75 |
function! s:wrap_cmds(cmds) |
| 76 |
return a:cmds |
| 77 |
endfunction |
| 78 |
|
| 79 |
function! s:enc_to_cp(str) |
| 80 |
return a:str |
| 81 |
endfunction |
| 82 |
endif |
| 83 |
|
| 84 |
function! s:shellesc_cmd(arg) |
| 85 |
let e = '"' |
| 86 |
let slashes = 0 |
| 87 |
for c in split(a:arg, '\zs') |
| 88 |
if c ==# '\' |
| 89 |
let slashes += 1 |
| 90 |
elseif c ==# '"' |
| 91 |
let e .= repeat('\', slashes + 1) |
| 92 |
let slashes = 0 |
| 93 |
else |
| 94 |
let slashes = 0 |
| 95 |
endif |
| 96 |
let e .= c |
| 97 |
endfor |
| 98 |
let e .= repeat('\', slashes) .'"' |
| 99 |
return substitute(substitute(e, '[&|<>()^!"]', '^&', 'g'), '%', '%%', 'g') |
| 100 |
endfunction |
| 101 |
|
| 102 |
function! fzf#shellescape(arg, ...) |
| 103 |
let shell = get(a:000, 0, s:is_win ? 'cmd.exe' : 'sh') |
| 104 |
if shell =~# 'cmd.exe$' |
| 105 |
return s:shellesc_cmd(a:arg) |
| 106 |
endif |
| 107 |
try |
| 108 |
let [shell, &shell] = [&shell, shell] |
| 109 |
return s:fzf_call('shellescape', a:arg) |
| 110 |
finally |
| 111 |
let [shell, &shell] = [&shell, shell] |
| 112 |
endtry |
| 113 |
endfunction |
| 114 |
|
| 115 |
function! s:fzf_getcwd() |
| 116 |
return s:fzf_call('getcwd') |
| 117 |
endfunction |
| 118 |
|
| 119 |
function! s:fzf_fnamemodify(fname, mods) |
| 120 |
return s:fzf_call('fnamemodify', a:fname, a:mods) |
| 121 |
endfunction |
| 122 |
|
| 123 |
function! s:fzf_expand(fmt) |
| 124 |
return s:fzf_call('expand', a:fmt, 1) |
| 125 |
endfunction |
| 126 |
|
| 127 |
function! s:fzf_tempname() |
| 128 |
return s:fzf_call('tempname') |
| 129 |
endfunction |
| 130 |
|
| 131 |
let s:layout_keys = ['window', 'popup', 'tmux', 'up', 'down', 'left', 'right'] |
| 132 |
let s:fzf_go = s:base_dir.'/bin/fzf' |
| 133 |
let s:fzf_tmux = s:base_dir.'/bin/fzf-tmux' |
| 134 |
|
| 135 |
let s:cpo_save = &cpo |
| 136 |
set cpo&vim |
| 137 |
|
| 138 |
function! s:popup_support() |
| 139 |
return has('nvim') ? has('nvim-0.4') : has('popupwin') && has('patch-8.2.191') |
| 140 |
endfunction |
| 141 |
|
| 142 |
function! s:default_layout() |
| 143 |
" A floating pane leaves the window fzf was started from visible and |
| 144 |
" reachable while fzf is open. A popup covers it, inside Vim or not. |
| 145 |
" Without a job, s:execute_tmux() blocks and freezes Vim. fzf also wants a |
| 146 |
" tmux window of at least 3x3, and Vim's pane is never larger than the |
| 147 |
" window, so asking Vim is free and survives a resize |
| 148 |
if (has('nvim') || has('job')) && &columns >= 3 && &lines >= 3 |
| 149 |
\ && s:tmux_enabled() && get(s:, 'tmux_floating', 0) |
| 150 |
return { 'tmux': '90%,60%' } |
| 151 |
endif |
| 152 |
return s:popup_support() |
| 153 |
\ ? { 'window' : { 'width': 0.9, 'height': 0.6 } } |
| 154 |
\ : { 'down': '~40%' } |
| 155 |
endfunction |
| 156 |
|
| 157 |
function! fzf#install() |
| 158 |
if s:is_win && !has('win32unix') |
| 159 |
let script = s:base_dir.'/install.ps1' |
| 160 |
if !filereadable(script) |
| 161 |
throw script.' not found' |
| 162 |
endif |
| 163 |
let script = 'powershell -ExecutionPolicy Bypass -file ' . shellescape(script) |
| 164 |
else |
| 165 |
let script = s:base_dir.'/install' |
| 166 |
if !executable(script) |
| 167 |
throw script.' not found' |
| 168 |
endif |
| 169 |
let script .= ' --bin' |
| 170 |
endif |
| 171 |
|
| 172 |
call s:warn('Running fzf installer ...') |
| 173 |
call system(script) |
| 174 |
if v:shell_error |
| 175 |
throw 'Failed to download fzf: '.script |
| 176 |
endif |
| 177 |
|
| 178 |
" A new binary invalidates the chosen executable and everything derived from |
| 179 |
" its version, including whether fzf opens a floating pane. fzf#install() is |
| 180 |
" also the vim-plug 'do' hook, so this can run long after the first fzf call |
| 181 |
let [s:versions, s:checked] = [{}, {}] |
| 182 |
unlet! s:exec s:tmux s:tmux_floating |
| 183 |
endfunction |
| 184 |
|
| 185 |
let s:versions = {} |
| 186 |
function s:get_version(bin) |
| 187 |
if has_key(s:versions, a:bin) |
| 188 |
return s:versions[a:bin] |
| 189 |
end |
| 190 |
let command = (&shell =~ 'powershell\|pwsh' ? '&' : '') . s:fzf_call('shellescape', a:bin) . ' --version --no-height' |
| 191 |
let output = systemlist(command) |
| 192 |
if v:shell_error || empty(output) |
| 193 |
return '' |
| 194 |
endif |
| 195 |
let ver = matchstr(output[-1], '[0-9.]\+') |
| 196 |
let s:versions[a:bin] = ver |
| 197 |
return ver |
| 198 |
endfunction |
| 199 |
|
| 200 |
function! s:compare_versions(a, b) |
| 201 |
let a = split(a:a, '\.') |
| 202 |
let b = split(a:b, '\.') |
| 203 |
for idx in range(0, max([len(a), len(b)]) - 1) |
| 204 |
let v1 = str2nr(get(a, idx, 0)) |
| 205 |
let v2 = str2nr(get(b, idx, 0)) |
| 206 |
if v1 < v2 | return -1 |
| 207 |
elseif v1 > v2 | return 1 |
| 208 |
endif |
| 209 |
endfor |
| 210 |
return 0 |
| 211 |
endfunction |
| 212 |
|
| 213 |
function! s:compare_binary_versions(a, b) |
| 214 |
return s:compare_versions(s:get_version(a:a), s:get_version(a:b)) |
| 215 |
endfunction |
| 216 |
|
| 217 |
let s:min_version = '0.53.0' |
| 218 |
let s:checked = {} |
| 219 |
function! fzf#exec(...) |
| 220 |
if !exists('s:exec') |
| 221 |
let binaries = [] |
| 222 |
if executable('fzf') |
| 223 |
call add(binaries, 'fzf') |
| 224 |
endif |
| 225 |
if executable(s:fzf_go) |
| 226 |
call add(binaries, s:fzf_go) |
| 227 |
endif |
| 228 |
|
| 229 |
if empty(binaries) |
| 230 |
if input('fzf executable not found. Download binary? (y/n) ') =~? '^y' |
| 231 |
redraw |
| 232 |
call fzf#install() |
| 233 |
return fzf#exec() |
| 234 |
else |
| 235 |
redraw |
| 236 |
throw 'fzf executable not found' |
| 237 |
endif |
| 238 |
elseif len(binaries) > 1 |
| 239 |
call sort(binaries, 's:compare_binary_versions') |
| 240 |
endif |
| 241 |
|
| 242 |
let s:exec = binaries[-1] |
| 243 |
endif |
| 244 |
|
| 245 |
let min_version = s:min_version |
| 246 |
if a:0 && s:compare_versions(a:1, min_version) > 0 |
| 247 |
let min_version = a:1 |
| 248 |
endif |
| 249 |
if !has_key(s:checked, min_version) |
| 250 |
let fzf_version = s:get_version(s:exec) |
| 251 |
if empty(fzf_version) |
| 252 |
let message = printf('Failed to run "%s --version"', s:exec) |
| 253 |
unlet s:exec |
| 254 |
throw message |
| 255 |
end |
| 256 |
|
| 257 |
if s:compare_versions(fzf_version, min_version) >= 0 |
| 258 |
let s:checked[min_version] = 1 |
| 259 |
return s:exec |
| 260 |
elseif a:0 < 2 && input(printf('You need fzf %s or above. Found: %s. Download binary? (y/n) ', min_version, fzf_version)) =~? '^y' |
| 261 |
let s:versions = {} |
| 262 |
unlet s:exec |
| 263 |
redraw |
| 264 |
call fzf#install() |
| 265 |
return fzf#exec(min_version, 1) |
| 266 |
else |
| 267 |
throw printf('You need to upgrade fzf (required: %s or above)', min_version) |
| 268 |
endif |
| 269 |
endif |
| 270 |
|
| 271 |
return s:exec |
| 272 |
endfunction |
| 273 |
|
| 274 |
" Path to the fzf-tmux script, or an empty string if it is not available. Only |
| 275 |
" the legacy options still need it. --popup is handled by fzf itself. |
| 276 |
function! s:fzf_tmux_script() |
| 277 |
if !executable(s:fzf_tmux) |
| 278 |
if !executable('fzf-tmux') |
| 279 |
return '' |
| 280 |
endif |
| 281 |
let s:fzf_tmux = 'fzf-tmux' |
| 282 |
endif |
| 283 |
return s:fzf_tmux |
| 284 |
endfunction |
| 285 |
|
| 286 |
function! s:tmux_enabled() |
| 287 |
if has('gui_running') |
| 288 |
return 0 |
| 289 |
endif |
| 290 |
|
| 291 |
if empty($TMUX) && empty($ZELLIJ) |
| 292 |
return 0 |
| 293 |
endif |
| 294 |
|
| 295 |
if exists('s:tmux') |
| 296 |
return s:tmux |
| 297 |
endif |
| 298 |
|
| 299 |
let [s:tmux, s:tmux_floating] = [0, 0] |
| 300 |
|
| 301 |
" --popup covers Zellij as well, where the fzf-tmux script and the tmux |
| 302 |
" version are irrelevant. fzf learned it in 0.71.0, and the floating pane |
| 303 |
" options it passes need Zellij 0.44 or above. fzf checks tmux first, so |
| 304 |
" this branch is Zellij without tmux. Both non-empty means tmux wins. |
| 305 |
" empty(), not exists(), to match how fzf reads the two variables |
| 306 |
if empty($TMUX) |
| 307 |
let s:tmux = |
| 308 |
\ s:compare_versions(s:get_version(s:fzf_binary()), '0.71.0') >= 0 |
| 309 |
\ && s:compare_versions(s:zellij_version(), '0.44') >= 0 |
| 310 |
let s:tmux_floating = s:tmux |
| 311 |
return s:tmux |
| 312 |
endif |
| 313 |
|
| 314 |
let output = system('tmux -V') |
| 315 |
if v:shell_error |
| 316 |
return s:tmux |
| 317 |
endif |
| 318 |
" e.g. 'tmux 3.7b', 'tmux next-3.8' |
| 319 |
let ver = matchstr(output, '\d\+\.\d\+') |
| 320 |
|
| 321 |
" --popup requires tmux 3.3 or above, and needs no fzf-tmux script. The |
| 322 |
" default layout wants a floating pane, which also needs fzf 0.74.0 or |
| 323 |
" above. fzf opens a modal popup otherwise. The version here only skips the |
| 324 |
" probe for servers too old to answer it |
| 325 |
if s:compare_versions(ver, '3.3') >= 0 |
| 326 |
let s:tmux = 1 |
| 327 |
let s:tmux_floating = s:compare_versions(ver, '3.7') >= 0 |
| 328 |
\ && s:tmux_floating_pane_support() |
| 329 |
\ && s:compare_versions(s:get_version(s:fzf_binary()), '0.74.0') >= 0 |
| 330 |
return s:tmux |
| 331 |
endif |
| 332 |
|
| 333 |
" Older versions still go through the script |
| 334 |
let s:tmux = !empty(s:fzf_tmux_script()) && s:compare_versions(ver, '1.7') >= 0 |
| 335 |
return s:tmux |
| 336 |
endfunction |
| 337 |
|
| 338 |
function! s:escape(path) |
| 339 |
let path = fnameescape(a:path) |
| 340 |
return s:is_win ? escape(path, '$') : path |
| 341 |
endfunction |
| 342 |
|
| 343 |
function! s:error(msg) |
| 344 |
echohl ErrorMsg |
| 345 |
echom a:msg |
| 346 |
echohl None |
| 347 |
endfunction |
| 348 |
|
| 349 |
function! s:warn(msg) |
| 350 |
echohl WarningMsg |
| 351 |
echom a:msg |
| 352 |
echohl None |
| 353 |
endfunction |
| 354 |
|
| 355 |
function! s:has_any(dict, keys) |
| 356 |
for key in a:keys |
| 357 |
if has_key(a:dict, key) |
| 358 |
return 1 |
| 359 |
endif |
| 360 |
endfor |
| 361 |
return 0 |
| 362 |
endfunction |
| 363 |
|
| 364 |
function! s:open(cmd, target) |
| 365 |
if stridx('edit', a:cmd) == 0 && s:fzf_fnamemodify(a:target, ':p') ==# s:fzf_expand('%:p') |
| 366 |
return |
| 367 |
endif |
| 368 |
execute a:cmd s:escape(a:target) |
| 369 |
endfunction |
| 370 |
|
| 371 |
function! s:common_sink(action, lines) abort |
| 372 |
if len(a:lines) < 2 |
| 373 |
return |
| 374 |
endif |
| 375 |
let key = remove(a:lines, 0) |
| 376 |
let Cmd = get(a:action, key, 'e') |
| 377 |
if type(Cmd) == type(function('call')) |
| 378 |
return Cmd(a:lines) |
| 379 |
endif |
| 380 |
if len(a:lines) > 1 |
| 381 |
augroup fzf_swap |
| 382 |
autocmd SwapExists * let v:swapchoice='o' |
| 383 |
\| call s:warn('fzf: E325: swap file exists: '.s:fzf_expand('<afile>')) |
| 384 |
augroup END |
| 385 |
endif |
| 386 |
try |
| 387 |
let empty = empty(s:fzf_expand('%')) && line('$') == 1 && empty(getline(1)) && !&modified |
| 388 |
" Preserve the current working directory in case it's changed during |
| 389 |
" the execution (e.g. `set autochdir` or `autocmd BufEnter * lcd ...`) |
| 390 |
let cwd = exists('w:fzf_pushd') ? w:fzf_pushd.dir : expand('%:p:h') |
| 391 |
for item in a:lines |
| 392 |
if has('win32unix') && item !~ '/' |
| 393 |
let item = substitute(item, '\', '/', 'g') |
| 394 |
end |
| 395 |
if item[0] != '~' && item !~ (s:is_win ? '^\([A-Z]:\)\?\' : '^/') |
| 396 |
let sep = s:is_win ? '\' : '/' |
| 397 |
let item = join([cwd, item], cwd[len(cwd)-1] == sep ? '' : sep) |
| 398 |
endif |
| 399 |
if empty |
| 400 |
execute 'e' s:escape(item) |
| 401 |
let empty = 0 |
| 402 |
else |
| 403 |
call s:open(Cmd, item) |
| 404 |
endif |
| 405 |
if !has('patch-8.0.0177') && !has('nvim-0.2') && exists('#BufEnter') |
| 406 |
\ && isdirectory(item) |
| 407 |
doautocmd BufEnter |
| 408 |
endif |
| 409 |
endfor |
| 410 |
catch /^Vim:Interrupt$/ |
| 411 |
finally |
| 412 |
silent! autocmd! fzf_swap |
| 413 |
endtry |
| 414 |
endfunction |
| 415 |
|
| 416 |
function! s:get_color(attr, ...) |
| 417 |
" Force 24 bit colors: g:fzf_force_termguicolors (temporary workaround for https://github.com/junegunn/fzf.vim/issues/1152) |
| 418 |
let gui = get(g:, 'fzf_force_termguicolors', 0) || (!s:is_win && !has('win32unix') && (has('gui_running') || has('termguicolors') && &termguicolors)) |
| 419 |
let fam = gui ? 'gui' : 'cterm' |
| 420 |
let pat = gui ? '^#[a-f0-9]\+' : '^[0-9]\+$' |
| 421 |
for group in a:000 |
| 422 |
let code = synIDattr(synIDtrans(hlID(group)), a:attr, fam) |
| 423 |
if code =~? pat |
| 424 |
return code |
| 425 |
endif |
| 426 |
endfor |
| 427 |
return '' |
| 428 |
endfunction |
| 429 |
|
| 430 |
function! s:defaults() |
| 431 |
let rules = copy(get(g:, 'fzf_colors', {})) |
| 432 |
let colors = join(map(items(filter(map(rules, 'call("s:get_color", v:val)'), '!empty(v:val)')), 'join(v:val, ":")'), ',') |
| 433 |
return empty(colors) ? '' : fzf#shellescape('--color='.colors) |
| 434 |
endfunction |
| 435 |
|
| 436 |
function! s:validate_layout(layout) |
| 437 |
for key in keys(a:layout) |
| 438 |
if index(s:layout_keys, key) < 0 |
| 439 |
throw printf('Invalid entry in g:fzf_layout: %s (allowed: %s)%s', |
| 440 |
\ key, join(s:layout_keys, ', '), key == 'options' ? '. Use $FZF_DEFAULT_OPTS.' : '') |
| 441 |
endif |
| 442 |
endfor |
| 443 |
return a:layout |
| 444 |
endfunction |
| 445 |
|
| 446 |
function! s:evaluate_opts(options) |
| 447 |
return type(a:options) == type([]) ? |
| 448 |
\ join(map(copy(a:options), 'fzf#shellescape(v:val)')) : a:options |
| 449 |
endfunction |
| 450 |
|
| 451 |
" [name string,] [opts dict,] [fullscreen boolean] |
| 452 |
function! fzf#wrap(...) |
| 453 |
let args = ['', {}, 0] |
| 454 |
let expects = map(copy(args), 'type(v:val)') |
| 455 |
let tidx = 0 |
| 456 |
for arg in copy(a:000) |
| 457 |
let tidx = index(expects, type(arg) == 6 ? type(0) : type(arg), tidx) |
| 458 |
if tidx < 0 |
| 459 |
throw 'Invalid arguments (expected: [name string] [opts dict] [fullscreen boolean])' |
| 460 |
endif |
| 461 |
let args[tidx] = arg |
| 462 |
let tidx += 1 |
| 463 |
unlet arg |
| 464 |
endfor |
| 465 |
let [name, opts, bang] = args |
| 466 |
|
| 467 |
if len(name) |
| 468 |
let opts.name = name |
| 469 |
end |
| 470 |
|
| 471 |
" Layout: g:fzf_layout (and deprecated g:fzf_height) |
| 472 |
if bang |
| 473 |
for key in s:layout_keys |
| 474 |
if has_key(opts, key) |
| 475 |
call remove(opts, key) |
| 476 |
endif |
| 477 |
endfor |
| 478 |
elseif !s:has_any(opts, s:layout_keys) |
| 479 |
if !exists('g:fzf_layout') && exists('g:fzf_height') |
| 480 |
let opts.down = g:fzf_height |
| 481 |
else |
| 482 |
" Not get(), which would evaluate s:default_layout() and run its version |
| 483 |
" checks even when g:fzf_layout makes the answer irrelevant |
| 484 |
let opts = extend(opts, s:validate_layout( |
| 485 |
\ exists('g:fzf_layout') ? g:fzf_layout : s:default_layout())) |
| 486 |
endif |
| 487 |
endif |
| 488 |
|
| 489 |
" Colors: g:fzf_colors |
| 490 |
let opts.options = s:defaults() .' '. s:evaluate_opts(get(opts, 'options', '')) |
| 491 |
|
| 492 |
" History: g:fzf_history_dir |
| 493 |
if len(name) && len(get(g:, 'fzf_history_dir', '')) |
| 494 |
let dir = s:fzf_expand(g:fzf_history_dir) |
| 495 |
if !isdirectory(dir) |
| 496 |
call mkdir(dir, 'p') |
| 497 |
endif |
| 498 |
let history = fzf#shellescape(dir.'/'.name) |
| 499 |
let opts.options = join(['--history', history, opts.options]) |
| 500 |
endif |
| 501 |
|
| 502 |
" Action: g:fzf_action |
| 503 |
if !s:has_any(opts, ['sink', 'sinklist', 'sink*']) |
| 504 |
let opts._action = get(g:, 'fzf_action', s:default_action) |
| 505 |
let opts.options .= ' --expect='.join(keys(opts._action), ',') |
| 506 |
function! opts.sinklist(lines) abort |
| 507 |
return s:common_sink(self._action, a:lines) |
| 508 |
endfunction |
| 509 |
let opts['sink*'] = opts.sinklist " For backward compatibility |
| 510 |
endif |
| 511 |
|
| 512 |
return opts |
| 513 |
endfunction |
| 514 |
|
| 515 |
function! s:use_sh() |
| 516 |
let [shell, shellslash, shellcmdflag, shellxquote] = [&shell, &shellslash, &shellcmdflag, &shellxquote] |
| 517 |
if s:is_win |
| 518 |
set shell=cmd.exe |
| 519 |
set noshellslash |
| 520 |
let &shellcmdflag = has('nvim') ? '/s /c' : '/c' |
| 521 |
let &shellxquote = has('nvim') ? '"' : '(' |
| 522 |
else |
| 523 |
set shell=sh |
| 524 |
endif |
| 525 |
return [shell, shellslash, shellcmdflag, shellxquote] |
| 526 |
endfunction |
| 527 |
|
| 528 |
function! s:writefile(...) |
| 529 |
if call('writefile', a:000) == -1 |
| 530 |
throw 'Failed to write temporary file. Check if you can write to the path tempname() returns.' |
| 531 |
endif |
| 532 |
endfunction |
| 533 |
|
| 534 |
function! s:extract_option(opts, name) |
| 535 |
let opt = '' |
| 536 |
let expect = 0 |
| 537 |
" There are a few cases where this function doesn't work as expected. |
| 538 |
" Let's just assume such cases are extremely unlikely in real world. |
| 539 |
" e.g. --query --border |
| 540 |
for word in split(a:opts) |
| 541 |
if expect && word !~ '^"\=-' |
| 542 |
let opt = opt . ' ' . word |
| 543 |
let expect = 0 |
| 544 |
elseif word == '--no-'.a:name |
| 545 |
let opt = '' |
| 546 |
elseif word =~ '^--'.a:name.'=' |
| 547 |
let opt = word |
| 548 |
elseif word =~ '^--'.a:name.'$' |
| 549 |
let opt = word |
| 550 |
let expect = 1 |
| 551 |
elseif expect |
| 552 |
let expect = 0 |
| 553 |
endif |
| 554 |
endfor |
| 555 |
return opt |
| 556 |
endfunction |
| 557 |
|
| 558 |
let s:need_cmd_window = has('win32unix') && $TERM_PROGRAM ==# 'mintty' && s:compare_versions($TERM_PROGRAM_VERSION, '3.4.5') < 0 && !executable('winpty') |
| 559 |
|
| 560 |
function! fzf#run(...) abort |
| 561 |
try |
| 562 |
let [shell, shellslash, shellcmdflag, shellxquote] = s:use_sh() |
| 563 |
|
| 564 |
let dict = exists('a:1') ? copy(a:1) : {} |
| 565 |
" 'popup' and 'tmux' are synonyms, as --popup and --tmux are. Normalize here |
| 566 |
" so that the rest of the function only has to know about one of them. |
| 567 |
if has_key(dict, 'popup') |
| 568 |
let dict.tmux = remove(dict, 'popup') |
| 569 |
endif |
| 570 |
let temps = { 'result': s:fzf_tempname() } |
| 571 |
let optstr = s:evaluate_opts(get(dict, 'options', '')) |
| 572 |
try |
| 573 |
let fzf_exec = shellescape(fzf#exec()) |
| 574 |
catch |
| 575 |
throw v:exception |
| 576 |
endtry |
| 577 |
|
| 578 |
if !s:present(dict, 'dir') |
| 579 |
let dict.dir = s:fzf_getcwd() |
| 580 |
endif |
| 581 |
if has('win32unix') && s:present(dict, 'dir') |
| 582 |
let dict.dir = fnamemodify(dict.dir, ':p') |
| 583 |
endif |
| 584 |
|
| 585 |
if has_key(dict, 'source') |
| 586 |
let source = dict.source |
| 587 |
let type = type(source) |
| 588 |
if type == 1 |
| 589 |
let prefix = '('.source.')|' |
| 590 |
elseif type == 3 |
| 591 |
let temps.input = s:fzf_tempname() |
| 592 |
call s:writefile(source, temps.input) |
| 593 |
let prefix = (s:is_win ? 'type ' : 'command cat ').fzf#shellescape(temps.input).'|' |
| 594 |
else |
| 595 |
throw 'Invalid source type' |
| 596 |
endif |
| 597 |
else |
| 598 |
let prefix = '' |
| 599 |
endif |
| 600 |
|
| 601 |
let prefer_tmux = get(g:, 'fzf_prefer_tmux', 0) || has_key(dict, 'tmux') |
| 602 |
let use_height = has_key(dict, 'down') && !has('gui_running') && |
| 603 |
\ !(has('nvim') || s:is_win || has('win32unix') || s:present(dict, 'up', 'left', 'right', 'window')) && |
| 604 |
\ executable('tput') && filereadable('/dev/tty') |
| 605 |
let has_vim8_term = has('terminal') && has('patch-8.0.995') |
| 606 |
let has_nvim_term = has('nvim-0.2.1') || has('nvim') && !s:is_win |
| 607 |
let use_term = (has_nvim_term || has_vim8_term) && !s:need_cmd_window |
| 608 |
let use_tmux = (has_key(dict, 'tmux') || (!use_height && !use_term || prefer_tmux) && !has('win32unix') && s:splittable(dict)) && s:tmux_enabled() |
| 609 |
if prefer_tmux && use_tmux |
| 610 |
let use_height = 0 |
| 611 |
let use_term = 0 |
| 612 |
endif |
| 613 |
if use_term |
| 614 |
let optstr .= ' --no-height' |
| 615 |
" Cancel a --popup from $FZF_DEFAULT_OPTS only when the spec asks for a Vim |
| 616 |
" window. Without a layout option, respect the user's preference. |
| 617 |
if s:present(dict, 'window', 'up', 'down', 'left', 'right') |
| 618 |
let optstr .= ' --no-tmux' |
| 619 |
endif |
| 620 |
elseif use_height |
| 621 |
let height = s:calc_size(&lines, dict.down, dict) |
| 622 |
let optstr .= ' --no-tmux --height='.height |
| 623 |
endif |
| 624 |
|
| 625 |
if exists('&winborder') && &winborder !=# '' && &winborder !=# 'none' |
| 626 |
" Add 1-column horizontal margin |
| 627 |
let optstr = join(['--margin 0,1', optstr]) |
| 628 |
else |
| 629 |
" Respect --border option given in $FZF_DEFAULT_OPTS and 'options' |
| 630 |
let optstr = join([s:border_opt(get(dict, 'window', 0)), s:extract_option($FZF_DEFAULT_OPTS, 'border'), optstr]) |
| 631 |
endif |
| 632 |
|
| 633 |
let command = prefix.(use_tmux ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result |
| 634 |
|
| 635 |
if use_term |
| 636 |
return s:execute_term(dict, command, temps) |
| 637 |
endif |
| 638 |
|
| 639 |
" s:execute_tmux may run fzf asynchronously, so it calls s:callback itself |
| 640 |
if use_tmux |
| 641 |
return s:execute_tmux(dict, command, temps) |
| 642 |
endif |
| 643 |
|
| 644 |
let lines = s:execute(dict, command, use_height, temps) |
| 645 |
call s:callback(dict, lines) |
| 646 |
return lines |
| 647 |
finally |
| 648 |
let [&shell, &shellslash, &shellcmdflag, &shellxquote] = [shell, shellslash, shellcmdflag, shellxquote] |
| 649 |
endtry |
| 650 |
endfunction |
| 651 |
|
| 652 |
function! s:present(dict, ...) |
| 653 |
for key in a:000 |
| 654 |
if !empty(get(a:dict, key, '')) |
| 655 |
return 1 |
| 656 |
endif |
| 657 |
endfor |
| 658 |
return 0 |
| 659 |
endfunction |
| 660 |
|
| 661 |
" The binary fzf#exec() would choose, without its prompting or installing. |
| 662 |
" Layout selection runs before fzf#exec() has resolved one |
| 663 |
function! s:fzf_binary() |
| 664 |
if exists('s:exec') |
| 665 |
return s:exec |
| 666 |
endif |
| 667 |
let bins = filter(['fzf', s:fzf_go], 'executable(v:val)') |
| 668 |
if empty(bins) |
| 669 |
return '' |
| 670 |
endif |
| 671 |
return len(bins) > 1 ? sort(bins, 's:compare_binary_versions')[-1] : bins[0] |
| 672 |
endfunction |
| 673 |
|
| 674 |
function! s:zellij_version() |
| 675 |
if !exists('s:zellij_ver') |
| 676 |
let output = systemlist('zellij --version') |
| 677 |
let s:zellij_ver = v:shell_error || empty(output) |
| 678 |
\ ? '' : matchstr(output[0], '[0-9.]\+') |
| 679 |
endif |
| 680 |
return s:zellij_ver |
| 681 |
endfunction |
| 682 |
|
| 683 |
" Whether the running server can put fzf in a floating pane. fzf decides on |
| 684 |
" the server, not on the version the tmux client reports, so ask it the same |
| 685 |
" question rather than predicting the answer. See tmuxFloatingPaneInfo in |
| 686 |
" src/tmux.go, which also requires the tmux window to be at least 3x3; that |
| 687 |
" one changes with a resize, so s:default_layout() reads it from Vim instead |
| 688 |
" of caching it here |
| 689 |
function! s:tmux_floating_pane_support() |
| 690 |
" fzf does not use a floating pane when it was not started from a pane |
| 691 |
if empty($TMUX_PANE) |
| 692 |
return 0 |
| 693 |
endif |
| 694 |
let out = system('tmux list-commands new-pane') |
| 695 |
" A server that does not know the command exits normally with no output |
| 696 |
return !v:shell_error && out =~# 'new-pane' |
| 697 |
endfunction |
| 698 |
|
| 699 |
function! s:fzf_tmux(dict) |
| 700 |
let size = get(a:dict, 'tmux', '') |
| 701 |
if empty(size) |
| 702 |
for o in ['up', 'down', 'left', 'right'] |
| 703 |
if s:present(a:dict, o) |
| 704 |
let size = o . ',' . a:dict[o] |
| 705 |
break |
| 706 |
endif |
| 707 |
endfor |
| 708 |
endif |
| 709 |
|
| 710 |
" Legacy fzf-tmux options are flags. A --tmux value never starts with a dash, |
| 711 |
" but may contain one, as in '90%,60%,border-native' |
| 712 |
if size =~ '^-' |
| 713 |
let script = s:fzf_tmux_script() |
| 714 |
if empty(script) |
| 715 |
throw 'fzf-tmux not found, required for the legacy option: ' . size |
| 716 |
endif |
| 717 |
return printf('LINES=%d COLUMNS=%d %s %s %s --', |
| 718 |
\ &lines, &columns, fzf#shellescape(script), size, (has_key(a:dict, 'source') ? '' : '-')) |
| 719 |
end |
| 720 |
|
| 721 |
" Using native --tmux option |
| 722 |
let in = (has_key(a:dict, 'source') ? '' : ' --force-tty-in') |
| 723 |
return printf('%s --tmux %s%s', fzf#shellescape(fzf#exec()), size, in) |
| 724 |
endfunction |
| 725 |
|
| 726 |
function! s:splittable(dict) |
| 727 |
return s:present(a:dict, 'up', 'down') && &lines > 15 || |
| 728 |
\ s:present(a:dict, 'left', 'right') && &columns > 40 |
| 729 |
endfunction |
| 730 |
|
| 731 |
function! s:pushd(dict) |
| 732 |
if s:present(a:dict, 'dir') |
| 733 |
let cwd = s:fzf_getcwd() |
| 734 |
let w:fzf_pushd = { |
| 735 |
\ 'command': haslocaldir() ? 'lcd' : (exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'), |
| 736 |
\ 'origin': cwd, |
| 737 |
\ 'bufname': bufname('') |
| 738 |
\ } |
| 739 |
execute 'lcd' s:escape(a:dict.dir) |
| 740 |
let cwd = s:fzf_getcwd() |
| 741 |
let w:fzf_pushd.dir = cwd |
| 742 |
let a:dict.pushd = w:fzf_pushd |
| 743 |
return cwd |
| 744 |
endif |
| 745 |
return '' |
| 746 |
endfunction |
| 747 |
|
| 748 |
augroup fzf_popd |
| 749 |
autocmd! |
| 750 |
autocmd WinEnter * call s:dopopd() |
| 751 |
augroup END |
| 752 |
|
| 753 |
function! s:dopopd() |
| 754 |
if !exists('w:fzf_pushd') |
| 755 |
return |
| 756 |
endif |
| 757 |
|
| 758 |
" FIXME: We temporarily change the working directory to 'dir' entry |
| 759 |
" of options dictionary (set to the current working directory if not given) |
| 760 |
" before running fzf. |
| 761 |
" |
| 762 |
" e.g. call fzf#run({'dir': '/tmp', 'source': 'ls', 'sink': 'e'}) |
| 763 |
" |
| 764 |
" After processing the sink function, we have to restore the current working |
| 765 |
" directory. But doing so may not be desirable if the function changed the |
| 766 |
" working directory on purpose. |
| 767 |
" |
| 768 |
" So how can we tell if we should do it or not? A simple heuristic we use |
| 769 |
" here is that we change directory only if the current working directory |
| 770 |
" matches 'dir' entry. However, it is possible that the sink function did |
| 771 |
" change the directory to 'dir'. In that case, the user will have an |
| 772 |
" unexpected result. |
| 773 |
if s:fzf_getcwd() ==# w:fzf_pushd.dir && (!&autochdir || w:fzf_pushd.bufname ==# bufname('')) |
| 774 |
execute w:fzf_pushd.command s:escape(w:fzf_pushd.origin) |
| 775 |
endif |
| 776 |
unlet! w:fzf_pushd |
| 777 |
endfunction |
| 778 |
|
| 779 |
function! s:xterm_launcher() |
| 780 |
let fmt = 'xterm -T "[fzf]" -bg "%s" -fg "%s" -geometry %dx%d+%d+%d -e bash -ic %%s' |
| 781 |
if has('gui_macvim') |
| 782 |
let fmt .= '&& osascript -e "tell application \"MacVim\" to activate"' |
| 783 |
endif |
| 784 |
return printf(fmt, |
| 785 |
\ escape(synIDattr(hlID("Normal"), "bg"), '#'), escape(synIDattr(hlID("Normal"), "fg"), '#'), |
| 786 |
\ &columns, &lines/2, getwinposx(), getwinposy()) |
| 787 |
endfunction |
| 788 |
unlet! s:launcher |
| 789 |
if s:is_win || has('win32unix') |
| 790 |
let s:launcher = '%s' |
| 791 |
else |
| 792 |
let s:launcher = function('s:xterm_launcher') |
| 793 |
endif |
| 794 |
|
| 795 |
function! s:exit_handler(dict, code, command, ...) |
| 796 |
if has_key(a:dict, 'exit') |
| 797 |
call a:dict.exit(a:code) |
| 798 |
endif |
| 799 |
if a:code == 2 |
| 800 |
call s:error('Error running ' . a:command) |
| 801 |
if !empty(a:000) |
| 802 |
sleep |
| 803 |
endif |
| 804 |
endif |
| 805 |
return a:code |
| 806 |
endfunction |
| 807 |
|
| 808 |
function! s:execute(dict, command, use_height, temps) abort |
| 809 |
call s:pushd(a:dict) |
| 810 |
if has('unix') && !a:use_height |
| 811 |
silent! !clear 2> /dev/null |
| 812 |
endif |
| 813 |
let escaped = (a:use_height || s:is_win) ? a:command : escape(substitute(a:command, '\n', '\\n', 'g'), '%#!') |
| 814 |
if has('gui_running') |
| 815 |
let Launcher = get(a:dict, 'launcher', get(g:, 'Fzf_launcher', get(g:, 'fzf_launcher', s:launcher))) |
| 816 |
let fmt = type(Launcher) == 2 ? call(Launcher, []) : Launcher |
| 817 |
if has('unix') |
| 818 |
let escaped = "'".substitute(escaped, "'", "'\"'\"'", 'g')."'" |
| 819 |
endif |
| 820 |
let command = printf(fmt, escaped) |
| 821 |
else |
| 822 |
let command = escaped |
| 823 |
endif |
| 824 |
if s:is_win |
| 825 |
let batchfile = s:fzf_tempname().'.bat' |
| 826 |
call s:writefile(s:wrap_cmds(command), batchfile) |
| 827 |
let command = batchfile |
| 828 |
let a:temps.batchfile = batchfile |
| 829 |
if has('nvim') |
| 830 |
let fzf = {} |
| 831 |
let fzf.dict = a:dict |
| 832 |
let fzf.temps = a:temps |
| 833 |
function! fzf.on_exit(job_id, exit_status, event) dict |
| 834 |
call s:pushd(self.dict) |
| 835 |
let lines = s:collect(self.temps) |
| 836 |
call s:callback(self.dict, lines) |
| 837 |
endfunction |
| 838 |
let cmd = 'start /wait cmd /c '.command |
| 839 |
call jobstart(cmd, fzf) |
| 840 |
return [] |
| 841 |
endif |
| 842 |
elseif s:need_cmd_window |
| 843 |
let shellscript = s:fzf_tempname() |
| 844 |
call s:writefile([command], shellscript) |
| 845 |
let command = 'start //WAIT sh -c '.shellscript |
| 846 |
let a:temps.shellscript = shellscript |
| 847 |
endif |
| 848 |
if a:use_height |
| 849 |
let stdin = has_key(a:dict, 'source') ? '' : '< /dev/tty' |
| 850 |
call system(printf('tput cup %d > /dev/tty; tput cnorm > /dev/tty; %s %s 2> /dev/tty', &lines, command, stdin)) |
| 851 |
else |
| 852 |
execute 'silent !'.command |
| 853 |
endif |
| 854 |
let exit_status = v:shell_error |
| 855 |
redraw! |
| 856 |
let lines = s:collect(a:temps) |
| 857 |
return s:exit_handler(a:dict, exit_status, command) < 2 ? lines : [] |
| 858 |
endfunction |
| 859 |
|
| 860 |
" Returns 0 if the job could not be started |
| 861 |
function! s:start_popup_job(dict, command, temps) abort |
| 862 |
let fzf = { 'dict': a:dict, 'temps': a:temps, 'command': a:command } |
| 863 |
" Vim passes (job, status) and Nvim (id, status, event) |
| 864 |
function! fzf.on_exit(id, code, ...) abort |
| 865 |
redraw! |
| 866 |
let lines = s:collect(self.temps) |
| 867 |
if s:exit_handler(self.dict, a:code, self.command, 1) >= 2 |
| 868 |
return |
| 869 |
endif |
| 870 |
call s:pushd(self.dict) |
| 871 |
call s:callback(self.dict, lines) |
| 872 |
endfunction |
| 873 |
|
| 874 |
if has('nvim') |
| 875 |
return jobstart([&shell, &shellcmdflag, a:command], fzf) > 0 |
| 876 |
endif |
| 877 |
|
| 878 |
" The command redirects to the result file, so no stream is of interest |
| 879 |
let opts = { 'exit_cb': function(fzf.on_exit), |
| 880 |
\ 'in_io': 'null', 'out_io': 'null', 'err_io': 'null' } |
| 881 |
|
| 882 |
" job_start() gives the child $TERM=dumb, and the popup inherits the |
| 883 |
" environment from here, so fzf would lose its colors. 'env' was added in |
| 884 |
" 8.0.902, so set it in the command itself on older versions. |
| 885 |
let command = a:command |
| 886 |
if has('patch-8.0.902') |
| 887 |
let opts.env = { 'TERM': $TERM } |
| 888 |
elseif !s:is_win |
| 889 |
let command = join(['export TERM=' . fzf#shellescape($TERM) . ';', command]) |
| 890 |
endif |
| 891 |
|
| 892 |
let job = job_start([&shell, &shellcmdflag, command], opts) |
| 893 |
return job_status(job) !=# 'fail' |
| 894 |
endfunction |
| 895 |
|
| 896 |
function! s:execute_tmux(dict, command, temps) abort |
| 897 |
let command = a:command |
| 898 |
let cwd = s:pushd(a:dict) |
| 899 |
if len(cwd) |
| 900 |
" -c '#{pane_current_path}' is only available on tmux 1.9 or above |
| 901 |
let command = join(['cd', fzf#shellescape(cwd), '&&', command]) |
| 902 |
endif |
| 903 |
|
| 904 |
" fzf draws in a pane of its own, so the process we start here only waits for |
| 905 |
" it. Hold it with a job instead of system() so that Vim keeps processing its |
| 906 |
" event loop, and nothing has to be displayed for it. Fall back to the |
| 907 |
" blocking path when the job cannot be started, or the sink would never run |
| 908 |
" and the temporary files would be left behind. |
| 909 |
if (has('nvim') || has('job')) && s:start_popup_job(a:dict, command, a:temps) |
| 910 |
" Restore the working directory while fzf runs. on_exit pushes it again |
| 911 |
call s:dopopd() |
| 912 |
return [] |
| 913 |
endif |
| 914 |
|
| 915 |
call system(command) |
| 916 |
let exit_status = v:shell_error |
| 917 |
redraw! |
| 918 |
let lines = s:collect(a:temps) |
| 919 |
let lines = s:exit_handler(a:dict, exit_status, command) < 2 ? lines : [] |
| 920 |
call s:callback(a:dict, lines) |
| 921 |
return lines |
| 922 |
endfunction |
| 923 |
|
| 924 |
function! s:calc_size(max, val, dict) |
| 925 |
let val = substitute(a:val, '^\~', '', '') |
| 926 |
if val =~ '%$' |
| 927 |
let size = a:max * str2nr(val[:-2]) / 100 |
| 928 |
else |
| 929 |
let size = min([a:max, str2nr(val)]) |
| 930 |
endif |
| 931 |
|
| 932 |
let srcsz = -1 |
| 933 |
if type(get(a:dict, 'source', 0)) == type([]) |
| 934 |
let srcsz = len(a:dict.source) |
| 935 |
endif |
| 936 |
|
| 937 |
let opts = $FZF_DEFAULT_OPTS.' '.s:evaluate_opts(get(a:dict, 'options', '')) |
| 938 |
if opts =~ 'preview' |
| 939 |
return size |
| 940 |
endif |
| 941 |
let margin = match(opts, '--inline-info\|--info[^-]\{-}inline') > match(opts, '--no-inline-info\|--info[^-]\{-}\(default\|hidden\)') ? 1 : 2 |
| 942 |
let margin += match(opts, '--border\([^-]\|$\)') > match(opts, '--no-border\([^-]\|$\)') ? 2 : 0 |
| 943 |
if stridx(opts, '--header') > stridx(opts, '--no-header') |
| 944 |
let margin += len(split(opts, "\n")) |
| 945 |
endif |
| 946 |
return srcsz >= 0 ? min([srcsz + margin, size]) : size |
| 947 |
endfunction |
| 948 |
|
| 949 |
function! s:getpos() |
| 950 |
return {'tab': tabpagenr(), 'win': winnr(), 'winid': win_getid(), 'cnt': winnr('$'), 'tcnt': tabpagenr('$')} |
| 951 |
endfunction |
| 952 |
|
| 953 |
function! s:border_opt(window) |
| 954 |
if type(a:window) != type({}) |
| 955 |
return '' |
| 956 |
endif |
| 957 |
|
| 958 |
" Border style |
| 959 |
let style = tolower(get(a:window, 'border', '')) |
| 960 |
if !has_key(a:window, 'border') && has_key(a:window, 'rounded') |
| 961 |
let style = a:window.rounded ? 'rounded' : 'sharp' |
| 962 |
endif |
| 963 |
if style == 'none' || style == 'no' |
| 964 |
return '' |
| 965 |
endif |
| 966 |
|
| 967 |
" For --border styles, we need fzf 0.24.0 or above |
| 968 |
call fzf#exec('0.24.0') |
| 969 |
let opt = ' --border ' . style |
| 970 |
if has_key(a:window, 'highlight') |
| 971 |
let color = s:get_color('fg', a:window.highlight) |
| 972 |
if len(color) |
| 973 |
let opt .= ' --color=border:' . color |
| 974 |
endif |
| 975 |
endif |
| 976 |
return opt |
| 977 |
endfunction |
| 978 |
|
| 979 |
function! s:split(dict) |
| 980 |
let directions = { |
| 981 |
\ 'up': ['topleft', 'resize', &lines], |
| 982 |
\ 'down': ['botright', 'resize', &lines], |
| 983 |
\ 'left': ['vertical topleft', 'vertical resize', &columns], |
| 984 |
\ 'right': ['vertical botright', 'vertical resize', &columns] } |
| 985 |
let ppos = s:getpos() |
| 986 |
let is_popup = 0 |
| 987 |
try |
| 988 |
if s:present(a:dict, 'window') |
| 989 |
if type(a:dict.window) == type({}) |
| 990 |
if !s:popup_support() |
| 991 |
throw 'Nvim 0.4+ or Vim 8.2.191+ with popupwin feature is required for pop-up window' |
| 992 |
end |
| 993 |
call s:popup(a:dict.window) |
| 994 |
let is_popup = 1 |
| 995 |
else |
| 996 |
execute 'keepalt' a:dict.window |
| 997 |
endif |
| 998 |
elseif !s:splittable(a:dict) |
| 999 |
execute (tabpagenr()-1).'tabnew' |
| 1000 |
else |
| 1001 |
for [dir, triple] in items(directions) |
| 1002 |
let val = get(a:dict, dir, '') |
| 1003 |
if !empty(val) |
| 1004 |
let [cmd, resz, max] = triple |
| 1005 |
if (dir == 'up' || dir == 'down') && val[0] == '~' |
| 1006 |
let sz = s:calc_size(max, val, a:dict) |
| 1007 |
else |
| 1008 |
let sz = s:calc_size(max, val, {}) |
| 1009 |
endif |
| 1010 |
execute cmd sz.'new' |
| 1011 |
execute resz sz |
| 1012 |
return [ppos, {}, is_popup] |
| 1013 |
endif |
| 1014 |
endfor |
| 1015 |
endif |
| 1016 |
return [ppos, is_popup ? {} : { '&l:wfw': &l:wfw, '&l:wfh': &l:wfh }, is_popup] |
| 1017 |
finally |
| 1018 |
if !is_popup |
| 1019 |
setlocal winfixwidth winfixheight |
| 1020 |
endif |
| 1021 |
endtry |
| 1022 |
endfunction |
| 1023 |
|
| 1024 |
nnoremap <silent> <Plug>(fzf-insert) i |
| 1025 |
nnoremap <silent> <Plug>(fzf-normal) <Nop> |
| 1026 |
if exists(':tnoremap') |
| 1027 |
tnoremap <silent> <Plug>(fzf-insert) <C-\><C-n>i |
| 1028 |
tnoremap <silent> <Plug>(fzf-normal) <C-\><C-n> |
| 1029 |
endif |
| 1030 |
|
| 1031 |
let s:warned = 0 |
| 1032 |
function! s:handle_ambidouble(dict) |
| 1033 |
if &ambiwidth == 'double' |
| 1034 |
let a:dict.env = { 'RUNEWIDTH_EASTASIAN': '1' } |
| 1035 |
elseif !s:warned && $RUNEWIDTH_EASTASIAN == '1' && &ambiwidth !=# 'double' |
| 1036 |
call s:warn("$RUNEWIDTH_EASTASIAN is '1' but &ambiwidth is not 'double'") |
| 1037 |
2sleep |
| 1038 |
let s:warned = 1 |
| 1039 |
endif |
| 1040 |
endfunction |
| 1041 |
|
| 1042 |
function! s:execute_term(dict, command, temps) abort |
| 1043 |
let winrest = winrestcmd() |
| 1044 |
let pbuf = bufnr('') |
| 1045 |
let [ppos, winopts, is_popup] = s:split(a:dict) |
| 1046 |
call s:use_sh() |
| 1047 |
let b:fzf = a:dict |
| 1048 |
let fzf = { 'buf': bufnr(''), 'pbuf': pbuf, 'ppos': ppos, 'dict': a:dict, 'temps': a:temps, |
| 1049 |
\ 'winopts': winopts, 'winrest': winrest, 'lines': &lines, |
| 1050 |
\ 'columns': &columns, 'command': a:command } |
| 1051 |
function! fzf.switch_back(inplace) |
| 1052 |
if a:inplace && bufnr('') == self.buf |
| 1053 |
if bufexists(self.pbuf) |
| 1054 |
execute 'keepalt keepjumps b' self.pbuf |
| 1055 |
endif |
| 1056 |
" No other listed buffer |
| 1057 |
if bufnr('') == self.buf |
| 1058 |
enew |
| 1059 |
endif |
| 1060 |
endif |
| 1061 |
endfunction |
| 1062 |
function! fzf.on_exit(id, code, ...) |
| 1063 |
silent! autocmd! fzf_popup_resize |
| 1064 |
if s:getpos() == self.ppos " {'window': 'enew'} |
| 1065 |
for [opt, val] in items(self.winopts) |
| 1066 |
execute 'let' opt '=' val |
| 1067 |
endfor |
| 1068 |
call self.switch_back(1) |
| 1069 |
else |
| 1070 |
if bufnr('') == self.buf |
| 1071 |
" We use close instead of bd! since Vim does not close the split when |
| 1072 |
" there's no other listed buffer (nvim +'set nobuflisted') |
| 1073 |
close |
| 1074 |
endif |
| 1075 |
silent! execute 'tabnext' self.ppos.tab |
| 1076 |
silent! execute self.ppos.win.'wincmd w' |
| 1077 |
endif |
| 1078 |
|
| 1079 |
if bufexists(self.buf) |
| 1080 |
execute 'bd!' self.buf |
| 1081 |
endif |
| 1082 |
|
| 1083 |
if &lines == self.lines && &columns == self.columns && s:getpos() == self.ppos |
| 1084 |
execute self.winrest |
| 1085 |
endif |
| 1086 |
|
| 1087 |
let lines = s:collect(self.temps) |
| 1088 |
if s:exit_handler(self.dict, a:code, self.command, 1) >= 2 |
| 1089 |
return |
| 1090 |
endif |
| 1091 |
|
| 1092 |
call s:pushd(self.dict) |
| 1093 |
call s:callback(self.dict, lines) |
| 1094 |
call self.switch_back(s:getpos() == self.ppos) |
| 1095 |
|
| 1096 |
if &buftype == 'terminal' |
| 1097 |
call feedkeys(&filetype == 'fzf' ? "\<Plug>(fzf-insert)" : "\<Plug>(fzf-normal)") |
| 1098 |
endif |
| 1099 |
endfunction |
| 1100 |
|
| 1101 |
try |
| 1102 |
call s:pushd(a:dict) |
| 1103 |
if s:is_win |
| 1104 |
let fzf.temps.batchfile = s:fzf_tempname().'.bat' |
| 1105 |
call s:writefile(s:wrap_cmds(a:command), fzf.temps.batchfile) |
| 1106 |
let command = fzf.temps.batchfile |
| 1107 |
else |
| 1108 |
let command = a:command |
| 1109 |
endif |
| 1110 |
let command .= s:term_marker |
| 1111 |
if has('nvim') |
| 1112 |
call s:handle_ambidouble(fzf) |
| 1113 |
call termopen(command, fzf) |
| 1114 |
else |
| 1115 |
let term_opts = {'exit_cb': function(fzf.on_exit)} |
| 1116 |
if v:version >= 802 |
| 1117 |
let term_opts.term_kill = 'term' |
| 1118 |
endif |
| 1119 |
if is_popup |
| 1120 |
let term_opts.hidden = 1 |
| 1121 |
else |
| 1122 |
let term_opts.curwin = 1 |
| 1123 |
endif |
| 1124 |
call s:handle_ambidouble(term_opts) |
| 1125 |
keepjumps let fzf.buf = term_start([&shell, &shellcmdflag, command], term_opts) |
| 1126 |
if is_popup && exists('#TerminalWinOpen') |
| 1127 |
doautocmd <nomodeline> TerminalWinOpen |
| 1128 |
endif |
| 1129 |
if !has('patch-8.0.1261') && !s:is_win |
| 1130 |
call term_wait(fzf.buf, 20) |
| 1131 |
endif |
| 1132 |
endif |
| 1133 |
tnoremap <buffer> <c-z> <nop> |
| 1134 |
if exists('&termwinkey') && (empty(&termwinkey) || &termwinkey =~? '<c-w>') |
| 1135 |
tnoremap <buffer> <c-w> <c-w>. |
| 1136 |
endif |
| 1137 |
finally |
| 1138 |
call s:dopopd() |
| 1139 |
endtry |
| 1140 |
setlocal nospell bufhidden=wipe nobuflisted nonumber |
| 1141 |
setf fzf |
| 1142 |
startinsert |
| 1143 |
return [] |
| 1144 |
endfunction |
| 1145 |
|
| 1146 |
function! s:collect(temps) abort |
| 1147 |
try |
| 1148 |
return filereadable(a:temps.result) ? readfile(a:temps.result) : [] |
| 1149 |
finally |
| 1150 |
for tf in values(a:temps) |
| 1151 |
silent! call delete(tf) |
| 1152 |
endfor |
| 1153 |
endtry |
| 1154 |
endfunction |
| 1155 |
|
| 1156 |
function! s:callback(dict, lines) abort |
| 1157 |
let popd = has_key(a:dict, 'pushd') |
| 1158 |
if popd |
| 1159 |
let w:fzf_pushd = a:dict.pushd |
| 1160 |
endif |
| 1161 |
|
| 1162 |
try |
| 1163 |
if has_key(a:dict, 'sink') |
| 1164 |
for line in a:lines |
| 1165 |
if type(a:dict.sink) == 2 |
| 1166 |
call a:dict.sink(line) |
| 1167 |
else |
| 1168 |
execute a:dict.sink s:escape(line) |
| 1169 |
endif |
| 1170 |
endfor |
| 1171 |
endif |
| 1172 |
if has_key(a:dict, 'sink*') |
| 1173 |
call a:dict['sink*'](a:lines) |
| 1174 |
elseif has_key(a:dict, 'sinklist') |
| 1175 |
call a:dict['sinklist'](a:lines) |
| 1176 |
endif |
| 1177 |
catch |
| 1178 |
if stridx(v:exception, ':E325:') < 0 |
| 1179 |
echoerr v:exception |
| 1180 |
endif |
| 1181 |
endtry |
| 1182 |
|
| 1183 |
" We may have opened a new window or tab |
| 1184 |
if popd |
| 1185 |
let w:fzf_pushd = a:dict.pushd |
| 1186 |
call s:dopopd() |
| 1187 |
endif |
| 1188 |
endfunction |
| 1189 |
|
| 1190 |
if has('nvim') |
| 1191 |
function! s:create_popup() abort |
| 1192 |
let opts = s:popup_bounds() |
| 1193 |
let opts = extend({'relative': 'editor', 'style': 'minimal'}, opts) |
| 1194 |
|
| 1195 |
let buf = nvim_create_buf(v:false, v:true) |
| 1196 |
let s:popup_id = nvim_open_win(buf, v:true, opts) |
| 1197 |
call setwinvar(s:popup_id, '&colorcolumn', '') |
| 1198 |
|
| 1199 |
" Colors |
| 1200 |
try |
| 1201 |
call setwinvar(s:popup_id, '&winhighlight', 'Pmenu:,Normal:Normal') |
| 1202 |
let rules = get(g:, 'fzf_colors', {}) |
| 1203 |
if has_key(rules, 'bg') |
| 1204 |
let color = call('s:get_color', rules.bg) |
| 1205 |
if len(color) |
| 1206 |
let ns = nvim_create_namespace('fzf_popup') |
| 1207 |
let hl = nvim_set_hl(ns, 'Normal', |
| 1208 |
\ &termguicolors ? { 'bg': color } : { 'ctermbg': str2nr(color) }) |
| 1209 |
call nvim_win_set_hl_ns(s:popup_id, ns) |
| 1210 |
endif |
| 1211 |
endif |
| 1212 |
catch |
| 1213 |
endtry |
| 1214 |
return buf |
| 1215 |
endfunction |
| 1216 |
|
| 1217 |
function! s:resize_popup() abort |
| 1218 |
if !exists('s:popup_id') || !nvim_win_is_valid(s:popup_id) |
| 1219 |
return |
| 1220 |
endif |
| 1221 |
let opts = s:popup_bounds() |
| 1222 |
let opts = extend({'relative': 'editor'}, opts) |
| 1223 |
call nvim_win_set_config(s:popup_id, opts) |
| 1224 |
endfunction |
| 1225 |
else |
| 1226 |
function! s:create_popup() abort |
| 1227 |
function! s:popup_create(buf) |
| 1228 |
let s:popup_id = popup_create(a:buf, #{zindex: 1000}) |
| 1229 |
call s:resize_popup() |
| 1230 |
endfunction |
| 1231 |
autocmd TerminalOpen * ++once call s:popup_create(str2nr(expand('<abuf>'))) |
| 1232 |
endfunction |
| 1233 |
|
| 1234 |
function! s:resize_popup() abort |
| 1235 |
if !exists('s:popup_id') || empty(popup_getpos(s:popup_id)) |
| 1236 |
return |
| 1237 |
endif |
| 1238 |
let opts = s:popup_bounds() |
| 1239 |
call popup_move(s:popup_id, { |
| 1240 |
\ 'line': opts.row, |
| 1241 |
\ 'col': opts.col, |
| 1242 |
\ 'minwidth': opts.width, |
| 1243 |
\ 'maxwidth': opts.width, |
| 1244 |
\ 'minheight': opts.height, |
| 1245 |
\ 'maxheight': opts.height, |
| 1246 |
\ }) |
| 1247 |
endfunction |
| 1248 |
endif |
| 1249 |
|
| 1250 |
function! s:popup_bounds() abort |
| 1251 |
let opts = s:popup_opts |
| 1252 |
|
| 1253 |
let xoffset = get(opts, 'xoffset', 0.5) |
| 1254 |
let yoffset = get(opts, 'yoffset', 0.5) |
| 1255 |
let relative = get(opts, 'relative', 0) |
| 1256 |
|
| 1257 |
" Use current window size for positioning relatively positioned popups |
| 1258 |
let columns = relative ? winwidth(0) : &columns |
| 1259 |
let lines = relative ? winheight(0) : (&lines - has('nvim')) |
| 1260 |
|
| 1261 |
" Size and position |
| 1262 |
let width = min([max([8, opts.width > 1 ? opts.width : float2nr(columns * opts.width)]), columns]) |
| 1263 |
let height = min([max([4, opts.height > 1 ? opts.height : float2nr(lines * opts.height)]), lines]) |
| 1264 |
let row = float2nr(yoffset * (lines - height)) + (relative ? win_screenpos(0)[0] - 1 : 0) |
| 1265 |
let col = float2nr(xoffset * (columns - width)) + (relative ? win_screenpos(0)[1] - 1 : 0) |
| 1266 |
|
| 1267 |
" Managing the differences |
| 1268 |
let row = min([max([0, row]), &lines - has('nvim') - height]) |
| 1269 |
let col = min([max([0, col]), &columns - width]) |
| 1270 |
let row += !has('nvim') |
| 1271 |
let col += !has('nvim') |
| 1272 |
|
| 1273 |
return { 'row': row, 'col': col, 'width': width, 'height': height } |
| 1274 |
endfunction |
| 1275 |
|
| 1276 |
function! s:popup(opts) abort |
| 1277 |
let s:popup_opts = a:opts |
| 1278 |
call s:create_popup() |
| 1279 |
|
| 1280 |
augroup fzf_popup_resize |
| 1281 |
autocmd! |
| 1282 |
autocmd VimResized * call s:resize_popup() |
| 1283 |
augroup END |
| 1284 |
endfunction |
| 1285 |
|
| 1286 |
let s:default_action = { |
| 1287 |
\ 'ctrl-t': 'tab split', |
| 1288 |
\ 'ctrl-x': 'split', |
| 1289 |
\ 'ctrl-v': 'vsplit' } |
| 1290 |
|
| 1291 |
function! s:shortpath() |
| 1292 |
let short = fnamemodify(getcwd(), ':~:.') |
| 1293 |
if !has('win32unix') |
| 1294 |
let short = pathshorten(short) |
| 1295 |
endif |
| 1296 |
let slash = (s:is_win && !&shellslash) ? '\' : '/' |
| 1297 |
return empty(short) ? '~'.slash : short . (short =~ escape(slash, '\').'$' ? '' : slash) |
| 1298 |
endfunction |
| 1299 |
|
| 1300 |
function! s:cmd(bang, ...) abort |
| 1301 |
let args = copy(a:000) |
| 1302 |
let opts = { 'options': ['--multi', '--scheme', 'path'] } |
| 1303 |
if len(args) && isdirectory(expand(args[-1])) |
| 1304 |
let opts.dir = substitute(substitute(remove(args, -1), '\\\(["'']\)', '\1', 'g'), '[/\\]*$', '/', '') |
| 1305 |
if s:is_win && !&shellslash |
| 1306 |
let opts.dir = substitute(opts.dir, '/', '\\', 'g') |
| 1307 |
endif |
| 1308 |
let prompt = opts.dir |
| 1309 |
else |
| 1310 |
let prompt = s:shortpath() |
| 1311 |
endif |
| 1312 |
let prompt = strwidth(prompt) < &columns - 20 ? prompt : '> ' |
| 1313 |
call extend(opts.options, ['--prompt', prompt]) |
| 1314 |
call extend(opts.options, args) |
| 1315 |
call fzf#run(fzf#wrap('FZF', opts, a:bang)) |
| 1316 |
endfunction |
| 1317 |
|
| 1318 |
command! -nargs=* -complete=dir -bang FZF call s:cmd(<bang>0, <f-args>) |
| 1319 |
|
| 1320 |
let &cpo = s:cpo_save |
| 1321 |
unlet s:cpo_save |