Skip to content
Myr

[vim] Use backslash for Windows filepaths (#896)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Fix shellescaping issues for filepaths - Supports both forward slashes or backslashes - Paths with spaces - Use jobstart for neovim in s:execute (Windows) - https://github.com/neovim/neovim/pull/6497 - Make 2 s:fzf_shellescape functions - (Windows) Substitute \" with \\" to escape the last backslash - (Default) Regular shellescape - Support list 'options' - Add "@echo off" to the batchfile used to execute fzf

authored by Jan Edmund Lazo · committed by Junegunn Choi · parent a5862d4 · Browse files · 1 file+97 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+97 added−36 removed

  • other:+97 added−36 removed

1 file+97 added−36 removed

modified:plugin/fzf.vim+97 added-36 removed
24 unmodified lines
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 function! s:fzf_shellescape(path)49 return substitute(s:fzf_call('shellescape', a:path), '[^\\]\zs\\"$', '\\\\"', '')50 endfunction51else52 function! s:fzf_call(fn, ...)53 return call(a:fn, a:000)54 endfunction5556 function! s:fzf_shellescape(path)57 return shellescape(a:path)58 endfunction59endif6061function! s:fzf_getcwd()62 return s:fzf_call('getcwd')63endfunction6465function! s:fzf_fnamemodify(fname, mods)66 return s:fzf_call('fnamemodify', a:fname, a:mods)67endfunction6869function! s:fzf_expand(fmt)70 return s:fzf_call('expand', a:fmt)71endfunction7273function! s:fzf_tempname()74 return s:fzf_call('tempname')75endfunction7677let s:default_layout = { 'down': '~40%' }78let s:layout_keys = ['window', 'up', 'down', 'left', 'right']31let s:is_win = has('win32') || has('win64')32let s:base_dir = expand('<sfile>:h:h')79let s:fzf_go = s:base_dir.'/bin/fzf'80let s:fzf_tmux = s:base_dir.'/bin/fzf-tmux'81let s:install = s:base_dir.'/install'82let s:installed = 0
3 unmodified lines
8687function! s:fzf_exec()88 if !exists('s:exec')89 if executable(s:fzf_go)44 let s:exec = s:fzf_go90 let s:exec = s:fzf_expand(s:fzf_go)91 elseif executable('fzf')92 let s:exec = 'fzf'93 elseif s:is_win94 call s:warn('fzf executable not found.')
12 unmodified lines
107 redraw108 throw 'fzf executable not found'109 endif110 endif65 return s:shellesc(s:exec)111 return s:is_win ? s:exec : s:shellesc(s:exec)112endfunction113114function! s:tmux_enabled()115 if has('gui_running')
62 unmodified lines
178 return 0179endfunction180181function! s:open(cmd, target)136 if stridx('edit', a:cmd) == 0 && fnamemodify(a:target, ':p') ==# expand('%:p')182 if stridx('edit', a:cmd) == 0 && s:fzf_fnamemodify(a:target, ':p') ==# s:fzf_expand('%:p')183 return184 endif185 execute a:cmd s:escape(a:target)186endfunction
6 unmodified lines
193 let cmd = get(a:action, key, 'e')194 if len(a:lines) > 1195 augroup fzf_swap196 autocmd SwapExists * let v:swapchoice='o'151 \| call s:warn('fzf: E325: swap file exists: '.expand('<afile>'))197 \| call s:warn('fzf: E325: swap file exists: '.s:fzf_expand('<afile>'))198 augroup END199 endif200 try155 let empty = empty(expand('%')) && line('$') == 1 && empty(getline(1)) && !&modified201 let empty = empty(s:fzf_expand('%')) && line('$') == 1 && empty(getline(1)) && !&modified202 let autochdir = &autochdir203 set noautochdir204 for item in a:lines205 if empty
41 unmodified lines
247 endfor248 return a:layout249endfunction250251function! s:evaluate_opts(options)252 return type(a:options) == type([]) ?253 \ join(map(copy(a:options), 's:fzf_shellescape(v:val)')) : a:options254endfunction255256" [name string,] [opts dict,] [fullscreen boolean]257function! fzf#wrap(...)258 let args = ['', {}, 0]259 let expects = map(copy(args), 'type(v:val)')
28 unmodified lines
288 endif289 endif290291 " Colors: g:fzf_colors241 let opts.options = s:defaults() .' '. get(opts, 'options', '')292 let opts.options = s:defaults() .' '. s:evaluate_opts(get(opts, 'options', ''))293294 " History: g:fzf_history_dir295 if len(name) && len(get(g:, 'fzf_history_dir', ''))245 let dir = expand(g:fzf_history_dir)296 let dir = s:fzf_expand(g:fzf_history_dir)297 if !isdirectory(dir)298 call mkdir(dir, 'p')299 endif300 let history = s:is_win ? s:fzf_shellescape(dir.'\'.name) : s:escape(dir.'/'.name)249 let opts.options = join(['--history', s:escape(dir.'/'.name), opts.options])301 let opts.options = join(['--history', history, opts.options])302 endif303304 " Action: g:fzf_action305 if !s:has_any(opts, ['sink', 'sink*'])
7 unmodified lines
313314 return opts315endfunction316265function! fzf#shellescape(path)266 if s:is_win267 let shellslash = &shellslash268 try269 set noshellslash270 return shellescape(a:path)271 finally272 let &shellslash = shellslash273 endtry274 endif275 return shellescape(a:path)276endfunction277317function! fzf#run(...) abort318try319 let oshell = &shell320 let useshellslash = &shellslash
12 unmodified lines
333 return []334 endif335 endif336 let dict = exists('a:1') ? s:upgrade(a:1) : {}298 let temps = { 'result': tempname() }299 let optstr = get(dict, 'options', '')337 let temps = { 'result': s:fzf_tempname() }338 let optstr = s:evaluate_opts(get(dict, 'options', ''))339 try340 let fzf_exec = s:fzf_exec()341 catch342 throw v:exception343 endtry344345 if has('nvim') && !has_key(dict, 'dir')307 let dict.dir = getcwd()346 let dict.dir = s:fzf_getcwd()347 endif348349 if !has_key(dict, 'source') && !empty($FZF_DEFAULT_COMMAND)311 let temps.source = tempname().(s:is_win ? '.bat' : '')350 let temps.source = s:fzf_tempname().(s:is_win ? '.bat' : '')351 call writefile((s:is_win ? ['@echo off'] : []) + split($FZF_DEFAULT_COMMAND, "\n"), temps.source)352 let dict.source = (empty($SHELL) ? &shell : $SHELL) . (s:is_win ? ' /c ' : ' ') . s:shellesc(temps.source)353 endif354
2 unmodified lines
357 let type = type(source)358 if type == 1359 let prefix = source.'|'360 elseif type == 3322 let temps.input = tempname()361 let temps.input = s:fzf_tempname()362 call writefile(source, temps.input)363 let prefix = (s:is_win ? 'type ' : 'cat ').s:shellesc(temps.input).'|'364 else365 throw 'Invalid source type'
5 unmodified lines
371 let prefer_tmux = get(g:, 'fzf_prefer_tmux', 0)372 let use_height = has_key(dict, 'down') &&373 \ !(has('nvim') || s:is_win || s:present(dict, 'up', 'left', 'right')) &&374 \ executable('tput') && filereadable('/dev/tty')336 let use_term = has('nvim')375 let use_term = has('nvim') && !s:is_win376 let use_tmux = (!use_height && !use_term || prefer_tmux) && s:tmux_enabled() && s:splittable(dict)377 if prefer_tmux && use_tmux378 let use_height = 0379 let use_term = 0
52 unmodified lines
432endfunction433434function! s:pushd(dict)435 if s:present(a:dict, 'dir')397 let cwd = getcwd()436 let cwd = s:fzf_getcwd()437 if get(a:dict, 'prev_dir', '') ==# cwd438 return 1439 endif440 let a:dict.prev_dir = cwd441 execute 'lcd' s:escape(a:dict.dir)403 let a:dict.dir = getcwd()442 let a:dict.dir = s:fzf_getcwd()443 return 1444 endif445 return 0446endfunction
55 unmodified lines
502 let command = printf(fmt, escaped)503 else504 let command = a:use_height ? a:command : escaped505 endif506 if s:is_win507 let batchfile = s:fzf_tempname().'.bat'508 call writefile(['@echo off', command], batchfile)509 let command = batchfile510 if has('nvim')511 let s:dict = a:dict512 let s:temps = a:temps513 let fzf = {}514 function! fzf.on_exit(job_id, exit_status, event) dict515 let lines = s:collect(s:temps)516 call s:callback(s:dict, lines)517 endfunction518 let cmd = 'start /wait cmd /c '.command519 call jobstart(cmd, fzf)520 return []521 endif522 endif523 if a:use_height524 let stdin = has_key(a:dict, 'source') ? '' : '< /dev/tty'525 call system(printf('tput cup %d > /dev/tty; tput cnorm > /dev/tty; %s %s 2> /dev/tty', &lines, command, stdin))526 else
203 unmodified lines
730 \ 'ctrl-v': 'vsplit' }731732function! s:shortpath()733 let short = pathshorten(fnamemodify(getcwd(), ':~:.'))678 return empty(short) ? '~/' : short . (short =~ '/$' ? '' : '/')734 let slash = (s:is_win && !&shellslash) ? '\' : '/'735 return empty(short) ? '~'.slash : short . (short =~ slash.'$' ? '' : slash)736endfunction737738function! s:cmd(bang, ...) abort739 let args = copy(a:000)683 let opts = { 'options': '--multi ' }740 let opts = { 'options': ['--multi'] }741 if len(args) && isdirectory(expand(args[-1]))742 let opts.dir = substitute(substitute(remove(args, -1), '\\\(["'']\)', '\1', 'g'), '[/\\]*$', '/', '')686 let opts.options .= ' --prompt '.fzf#shellescape(opts.dir)743 if s:is_win && !&shellslash744 let opts.dir = substitute(opts.dir, '/', '\\', 'g')745 endif746 let prompt = opts.dir747 else688 let opts.options .= ' --prompt '.fzf#shellescape(s:shortpath())748 let prompt = s:shortpath()749 endif690 let opts.options .= ' '.join(args)750 call extend(opts.options, ['--prompt', prompt])751 call extend(opts.options, args)752 call fzf#run(fzf#wrap('FZF', opts, a:bang))753endfunction754755command! -nargs=* -complete=dir -bang FZF call s:cmd(<bang>0, <f-args>)
3 unmodified lines