Skip to content
Myr

[vim] Fix directory switching around sink function

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

Close #1356 Related: - #612 - https://github.com/junegunn/fzf.vim/issues/308

authored by Junegunn Choi · committed by Junegunn Choi · parent 13803d0 · Browse files · 1 file+21 added−15 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+21 added−15 removed

  • other:+21 added−15 removed

1 file+21 added−15 removed

modified:plugin/fzf.vim+21 added-15 removed
355 unmodified lines
356 catch357 throw v:exception358 endtry359360 if has('nvim') && !has_key(dict, 'dir')360 if !has_key(dict, 'dir')361 let dict.dir = s:fzf_getcwd()362 endif363 if has('win32unix') && has_key(dict, 'dir')364 let dict.dir = fnamemodify(dict.dir, ':p')
111 unmodified lines
476function! s:dopopd()477 if !exists('w:fzf_pushd')478 return479 endif480481 " FIXME: We temporarily change the working directory to 'dir' entry482 " of options dictionary (set to the current working directory if not given)483 " before running fzf.484 "485 " e.g. call fzf#run({'dir': '/tmp', 'source': 'ls', 'sink': 'e'})486 "487 " After processing the sink function, we have to restore the current working488 " directory. But doing so may not be desirable if the function changed the489 " working directory on purpose.490 "491 " So how can we tell if we should do it or not? A simple heuristic we use492 " here is that we change directory only if the current working directory493 " matches 'dir' entry. However, it is possible that the sink function did494 " change the directory to 'dir'. In that case, the user will have an495 " unexpected result.496 if s:fzf_getcwd() ==# w:fzf_pushd.dir480 execute w:fzf_pushd.command s:escape(w:fzf_pushd.origin)497 execute w:fzf_pushd.command s:escape(w:fzf_pushd.origin)498 endif499 unlet w:fzf_pushd500endfunction501502function! s:xterm_launcher()
238 unmodified lines
741 endtry742endfunction743744function! s:callback(dict, lines) abort727 " Since anything can be done in the sink function, there is no telling that728 " the change of the working directory was made by &autochdir setting.729 "730 " We use the following heuristic to determine whether to restore CWD:731 " - Always restore the current directory when &autochdir is disabled.732 " FIXME This makes it impossible to change directory from inside the sink733 " function when &autochdir is not used.734 " - In case of an error or an interrupt, a:lines will be empty.735 " And it will be an array of a single empty string when fzf was finished736 " without a match. In these cases, we presume that the change of the737 " directory is not expected and should be undone.738 let popd = has_key(a:dict, 'pushd') &&745 let popd = has_key(a:dict, 'pushd')739 \ (!&autochdir || (empty(a:lines) || len(a:lines) == 1 && empty(a:lines[0])))746 if popd747 let w:fzf_pushd = a:dict.pushd748 endif749
62 unmodified lines