Skip to content
Myr

[vim] Try to make 'dir' option compatible with &autochdir

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

When 'dir' option is passed to fzf#run(), the current working directory is temporarily changed to the given directory, and restored at the end. However, this behavior is not compatible with &autochdir. This commit introduces a heuristic to determine whether or not to restore the previous working directory.

authored by Junegunn Choi · committed by Junegunn Choi · parent b18db47 · Browse files · 2 files+57 added−19 removed
This commit

State

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

Trailers

  • Related: https://github.com/junegunn/fzf.vim/issues/70

Diff stats

2 files+57 added−19 removed

  • tests:+29 added−0 removed
  • other:+28 added−19 removed

2 files+57 added−19 removed

modified:plugin/fzf.vim+28 added-19 removed
1" Copyright (c) 2015 Junegunn Choi1" Copyright (c) 2016 Junegunn Choi2"3" MIT License4"5" Permission is hereby granted, free of charge, to any person obtaining
132 unmodified lines
138 endif139 let tmux = !has('nvim') && s:tmux_enabled() && s:splittable(dict)140 let command = prefix.(tmux ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result141142 try143 if tmux144 return s:execute_tmux(dict, command, temps)145 elseif has('nvim')146 return s:execute_term(dict, command, temps)147 else148 return s:execute(dict, command, temps)149 endif150 finally151 call s:popd(dict)152 endtry142 if has('nvim')143 return s:execute_term(dict, command, temps)144 endif145146 let ret = tmux ? s:execute_tmux(dict, command, temps) : s:execute(dict, command, temps)147 call s:popd(dict, ret)148 return ret149finally150 let &shell = oshell151endtry152endfunction
35 unmodified lines
188 if get(a:dict, 'prev_dir', '') ==# cwd189 return 1190 endif191 let a:dict.prev_dir = cwd196 execute 'chdir '.s:escape(a:dict.dir)192 execute 'chdir' s:escape(a:dict.dir)193 let a:dict.dir = getcwd()194 return 1195 endif196 return 0197endfunction198203function! s:popd(dict)204 if has_key(a:dict, 'prev_dir') && getcwd() ==# a:dict.dir205 execute 'chdir '.s:escape(remove(a:dict, 'prev_dir'))199function! s:popd(dict, lines)200 " Since anything can be done in the sink function, there is no telling that201 " the change of the working directory was made by &autochdir setting.202 "203 " We use the following heuristic to determine whether to restore CWD:204 " - Always restore the current directory when &autochdir is disabled.205 " FIXME This makes it impossible to change directory from inside the sink206 " function when &autochdir is not used.207 " - In case of an error or an interrupt, a:lines will be empty.208 " And it will be an array of a single empty string when fzf was finished209 " without a match. In these cases, we presume that the change of the210 " directory is not expected and should be undone.211 if has_key(a:dict, 'prev_dir') &&212 \ (!&autochdir || (empty(a:lines) || len(a:lines) == 1 && empty(a:lines[0])))213 execute 'chdir' s:escape(remove(a:dict, 'prev_dir'))214 endif215endfunction216217function! s:xterm_launcher()
103 unmodified lines
321endfunction322323function! s:execute_term(dict, command, temps)324 call s:split(a:dict)317 call s:pushd(a:dict)325326 let fzf = { 'buf': bufnr('%'), 'dict': a:dict, 'temps': a:temps, 'name': 'FZF' }327 let s:command = a:command328 function! fzf.on_exit(id, code)
15 unmodified lines
344 endif345346 call s:pushd(self.dict)347 try341 call s:callback(self.dict, self.temps)348 let ret = s:callback(self.dict, self.temps)349350 if inplace && bufnr('') == self.buf351 execute "normal! \<c-^>"352 " No other listed buffer
1 unmodified line
354 bd!355 endif356 endif357 finally351 call s:popd(self.dict)358 call s:popd(self.dict, ret)359 endtry360 endfunction361362 call s:pushd(a:dict)363 call termopen(a:command, fzf)364 call s:popd(a:dict, [])365 setlocal nospell366 setf fzf367 startinsert368 return []
91 unmodified lines
modified:test/fzf.vader+29 added
1Execute (Setup):2 let g:dir = fnamemodify(g:vader_file, ':p:h')3 Log 'Test directory: ' . g:dir4 Save &acd56Execute (fzf#run with dir option):7 let cwd = getcwd()8 let result = fzf#run({ 'options': '--filter=vdr', 'dir': g:dir })
26 unmodified lines
35Execute (fzf#run with string source):36 let result = sort(fzf#run({ 'source': 'echo hi', 'options': '-f i' }))37 AssertEqual ['hi'], result3839Execute (fzf#run with dir option and noautochdir):40 set noacd41 let cwd = getcwd()42 call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'})43 " No change in working directory44 AssertEqual cwd, getcwd()4546Execute (Incomplete fzf#run with dir option and autochdir):47 set acd48 let cwd = getcwd()49 call fzf#run({'source': [], 'sink': 'e', 'dir': '/tmp', 'options': '-0'})50 " No change in working directory even if &acd is set51 AssertEqual cwd, getcwd()5253Execute (fzf#run with dir option and autochdir):54 set acd55 let cwd = getcwd()56 call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'})57 " Working directory changed due to &acd58 AssertEqual '/', getcwd()5960Execute (fzf#run with dir option and autochdir when final cwd is same as dir):61 set acd62 cd /tmp63 call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/', 'options': '-1'})64 " Working directory changed due to &acd65 AssertEqual '/', getcwd()6667Execute (Cleanup):68 unlet g:dir69 Restore