[vim] Clear temporary window-local working directory

Close #1085
Close #1086
Close https://github.com/junegunn/fzf.vim/issues/678
This commit is contained in:
Junegunn Choi 2018-08-10 18:19:29 +09:00
parent 423986996a
commit 13803d0dbb
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 26 additions and 26 deletions

View File

@ -455,15 +455,17 @@ endfunction
function! s:pushd(dict) function! s:pushd(dict)
if s:present(a:dict, 'dir') if s:present(a:dict, 'dir')
let cwd = s:fzf_getcwd() let cwd = s:fzf_getcwd()
if get(a:dict, 'prev_dir', '') ==# cwd let w:fzf_pushd = {
return 1 \ 'command': haslocaldir() ? 'lcd' : (exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'),
endif \ 'origin': cwd
let a:dict.prev_dir = cwd \ }
execute 'lcd' s:escape(a:dict.dir) execute 'lcd' s:escape(a:dict.dir)
let a:dict.dir = s:fzf_getcwd() let cwd = s:fzf_getcwd()
return 1 let w:fzf_pushd.dir = cwd
let a:dict.pushd = w:fzf_pushd
return cwd
endif endif
return 0 return ''
endfunction endfunction
augroup fzf_popd augroup fzf_popd
@ -472,11 +474,11 @@ augroup fzf_popd
augroup END augroup END
function! s:dopopd() function! s:dopopd()
if !exists('w:fzf_dir') || s:fzf_getcwd() != w:fzf_dir[1] if !exists('w:fzf_pushd')
return return
endif endif
execute 'lcd' s:escape(w:fzf_dir[0]) execute w:fzf_pushd.command s:escape(w:fzf_pushd.origin)
unlet w:fzf_dir unlet w:fzf_pushd
endfunction endfunction
function! s:xterm_launcher() function! s:xterm_launcher()
@ -534,9 +536,7 @@ function! s:execute(dict, command, use_height, temps) abort
let fzf.dict = a:dict let fzf.dict = a:dict
let fzf.temps = a:temps let fzf.temps = a:temps
function! fzf.on_exit(job_id, exit_status, event) dict function! fzf.on_exit(job_id, exit_status, event) dict
if s:present(self.dict, 'dir') call s:pushd(self.dict)
execute 'lcd' s:escape(self.dict.dir)
endif
let lines = s:collect(self.temps) let lines = s:collect(self.temps)
call s:callback(self.dict, lines) call s:callback(self.dict, lines)
endfunction endfunction
@ -563,9 +563,10 @@ endfunction
function! s:execute_tmux(dict, command, temps) abort function! s:execute_tmux(dict, command, temps) abort
let command = a:command let command = a:command
if s:pushd(a:dict) let cwd = s:pushd(a:dict)
if len(cwd)
" -c '#{pane_current_path}' is only available on tmux 1.9 or above " -c '#{pane_current_path}' is only available on tmux 1.9 or above
let command = join(['cd', fzf#shellescape(a:dict.dir), '&&', command]) let command = join(['cd', fzf#shellescape(cwd), '&&', command])
endif endif
call system(command) call system(command)
@ -686,9 +687,7 @@ function! s:execute_term(dict, command, temps) abort
endfunction endfunction
try try
if s:present(a:dict, 'dir') call s:pushd(a:dict)
execute 'lcd' s:escape(a:dict.dir)
endif
if s:is_win if s:is_win
let fzf.temps.batchfile = s:fzf_tempname().'.bat' let fzf.temps.batchfile = s:fzf_tempname().'.bat'
call writefile(s:wrap_cmds(a:command), fzf.temps.batchfile) call writefile(s:wrap_cmds(a:command), fzf.temps.batchfile)
@ -706,9 +705,7 @@ function! s:execute_term(dict, command, temps) abort
endif endif
endif endif
finally finally
if s:present(a:dict, 'dir') call s:dopopd()
lcd -
endif
endtry endtry
setlocal nospell bufhidden=wipe nobuflisted nonumber setlocal nospell bufhidden=wipe nobuflisted nonumber
setf fzf setf fzf
@ -738,10 +735,10 @@ function! s:callback(dict, lines) abort
" And it will be an array of a single empty string when fzf was finished " And it will be an array of a single empty string when fzf was finished
" without a match. In these cases, we presume that the change of the " without a match. In these cases, we presume that the change of the
" directory is not expected and should be undone. " directory is not expected and should be undone.
let popd = has_key(a:dict, 'prev_dir') && let popd = has_key(a:dict, 'pushd') &&
\ (!&autochdir || (empty(a:lines) || len(a:lines) == 1 && empty(a:lines[0]))) \ (!&autochdir || (empty(a:lines) || len(a:lines) == 1 && empty(a:lines[0])))
if popd if popd
let w:fzf_dir = [a:dict.prev_dir, a:dict.dir] let w:fzf_pushd = a:dict.pushd
endif endif
try try
@ -765,7 +762,7 @@ function! s:callback(dict, lines) abort
" We may have opened a new window or tab " We may have opened a new window or tab
if popd if popd
let w:fzf_dir = [a:dict.prev_dir, a:dict.dir] let w:fzf_pushd = a:dict.pushd
call s:dopopd() call s:dopopd()
endif endif
endfunction endfunction

View File

@ -8,10 +8,13 @@ Execute (fzf#run with dir option):
let cwd = getcwd() let cwd = getcwd()
let result = fzf#run({ 'source': 'git ls-files', 'options': '--filter=vdr', 'dir': g:dir }) let result = fzf#run({ 'source': 'git ls-files', 'options': '--filter=vdr', 'dir': g:dir })
AssertEqual ['fzf.vader'], result AssertEqual ['fzf.vader'], result
AssertEqual 0, haslocaldir()
AssertEqual getcwd(), cwd AssertEqual getcwd(), cwd
execute 'lcd' fnameescape(cwd)
let result = sort(fzf#run({ 'source': 'git ls-files', 'options': '--filter e', 'dir': g:dir })) let result = sort(fzf#run({ 'source': 'git ls-files', 'options': '--filter e', 'dir': g:dir }))
AssertEqual ['fzf.vader', 'test_go.rb'], result AssertEqual ['fzf.vader', 'test_go.rb'], result
AssertEqual 1, haslocaldir()
AssertEqual getcwd(), cwd AssertEqual getcwd(), cwd
Execute (fzf#run with Funcref command): Execute (fzf#run with Funcref command):
@ -56,11 +59,11 @@ Execute (Incomplete fzf#run with dir option and autochdir):
" No change in working directory even if &acd is set " No change in working directory even if &acd is set
AssertEqual cwd, getcwd() AssertEqual cwd, getcwd()
Execute (fzf#run with dir option and autochdir): Execute (FIXME: fzf#run with dir option and autochdir):
set acd set acd
let cwd = getcwd()
call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'}) call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'})
" Working directory changed due to &acd " Working directory changed due to &acd
AssertEqual '/foobar', expand('%')
AssertEqual '/', getcwd() AssertEqual '/', getcwd()
Execute (fzf#run with dir option and autochdir when final cwd is same as dir): Execute (fzf#run with dir option and autochdir when final cwd is same as dir):