Skip to content
Myr

[vim] Restore working directory even when new window is opened

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

Close #612

authored by Junegunn Choi · committed by Junegunn Choi · parent f941012 · Browse files · 2 files+73 added−47 removed
This commit

State

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

Diff stats

2 files+73 added−47 removed

  • tests:+5 added−0 removed
  • other:+68 added−47 removed

2 files+73 added−47 removed

modified:plugin/fzf.vim+68 added-47 removed
145 unmodified lines
146 if has('nvim') && !tmux147 return s:execute_term(dict, command, temps)148 endif149150 let ret = tmux ? s:execute_tmux(dict, command, temps) : s:execute(dict, command, temps)151 call s:popd(dict, ret)152 return ret150 let lines = tmux ? s:execute_tmux(dict, command, temps) : s:execute(dict, command, temps)151 call s:callback(dict, lines)152 return lines153finally154 let &shell = oshell155endtry156endfunction
42 unmodified lines
199 endif200 return 0201endfunction202203function! s:popd(dict, lines)204 " Since anything can be done in the sink function, there is no telling that205 " the change of the working directory was made by &autochdir setting.206 "207 " We use the following heuristic to determine whether to restore CWD:208 " - Always restore the current directory when &autochdir is disabled.209 " FIXME This makes it impossible to change directory from inside the sink210 " function when &autochdir is not used.211 " - In case of an error or an interrupt, a:lines will be empty.212 " And it will be an array of a single empty string when fzf was finished213 " without a match. In these cases, we presume that the change of the214 " directory is not expected and should be undone.215 if has_key(a:dict, 'prev_dir') &&216 \ (!&autochdir || (empty(a:lines) || len(a:lines) == 1 && empty(a:lines[0])))217 execute 'lcd' s:escape(remove(a:dict, 'prev_dir'))203augroup fzf_popd204 autocmd!205 autocmd WinEnter * call s:dopopd()206augroup END207208function! s:dopopd()209 if !exists('w:fzf_prev_dir') || exists('*haslocaldir') && !haslocaldir()210 return211 endif212 execute 'lcd' s:escape(w:fzf_prev_dir)213 unlet w:fzf_prev_dir214endfunction215216function! s:xterm_launcher()217 let fmt = 'xterm -T "[fzf]" -bg "\%s" -fg "\%s" -geometry %dx%d+%d+%d -e bash -ic %%s'
32 unmodified lines
250 let command = escaped251 endif252 execute 'silent !'.command253 redraw!259 return s:exit_handler(v:shell_error, command) ? s:callback(a:dict, a:temps) : []254 return s:exit_handler(v:shell_error, command) ? s:collect(a:temps) : []255endfunction256257function! s:execute_tmux(dict, command, temps) abort258 let command = a:command
3 unmodified lines
262 endif263264 call system(command)265 redraw!271 return s:exit_handler(v:shell_error, command) ? s:callback(a:dict, a:temps) : []266 return s:exit_handler(v:shell_error, command) ? s:collect(a:temps) : []267endfunction268269function! s:calc_size(max, val, dict)270 if a:val =~ '%$'
84 unmodified lines
355 return356 endif357358 call s:pushd(self.dict)364 let ret = []365 try366 let ret = s:callback(self.dict, self.temps)367 call self.switch_back(s:getpos() == self.ppos)368 finally369 call s:popd(self.dict, ret)370 endtry359 let lines = s:collect(self.temps)360 call s:callback(self.dict, lines)361 call self.switch_back(s:getpos() == self.ppos)362 endfunction363373 call s:pushd(a:dict)374 call termopen(a:command, fzf)375 call s:popd(a:dict, [])364 try365 if s:present(a:dict, 'dir')366 execute 'lcd' s:escape(a:dict.dir)367 endif368 call termopen(a:command, fzf)369 finally370 if s:present(a:dict, 'dir')371 lcd -372 endif373 endtry374 setlocal nospell bufhidden=wipe nobuflisted375 setf fzf376 startinsert377 return []378endfunction379382function! s:callback(dict, temps) abort383let lines = []384try385 if filereadable(a:temps.result)386 let lines = readfile(a:temps.result)380function! s:collect(temps) abort381 try382 return filereadable(a:temps.result) ? readfile(a:temps.result) : []383 finally384 for tf in values(a:temps)385 silent! call delete(tf)386 endfor387 endtry388endfunction389390function! s:callback(dict, lines) abort391 " Since anything can be done in the sink function, there is no telling that392 " the change of the working directory was made by &autochdir setting.393 "394 " We use the following heuristic to determine whether to restore CWD:395 " - Always restore the current directory when &autochdir is disabled.396 " FIXME This makes it impossible to change directory from inside the sink397 " function when &autochdir is not used.398 " - In case of an error or an interrupt, a:lines will be empty.399 " And it will be an array of a single empty string when fzf was finished400 " without a match. In these cases, we presume that the change of the401 " directory is not expected and should be undone.402 let popd = has_key(a:dict, 'prev_dir') &&403 \ (!&autochdir || (empty(a:lines) || len(a:lines) == 1 && empty(a:lines[0])))404 if popd405 let w:fzf_prev_dir = a:dict.prev_dir406 endif407408 try409 if has_key(a:dict, 'sink')388 for line in lines410 for line in a:lines411 if type(a:dict.sink) == 2412 call a:dict.sink(line)413 else414 execute a:dict.sink s:escape(line)415 endif416 endfor417 endif418 if has_key(a:dict, 'sink*')397 call a:dict['sink*'](lines)419 call a:dict['sink*'](a:lines)420 endif421 catch422 if stridx(v:exception, ':E325:') < 0423 echoerr v:exception399 endif424 endif425 endtry426401 for tf in values(a:temps)402 silent! call delete(tf)403 endfor404catch405 if stridx(v:exception, ':E325:') < 0406 echoerr v:exception427 " We may have opened a new window or tab428 if popd429 let w:fzf_prev_dir = a:dict.prev_dir430 call s:dopopd()431 endif408finally409 return lines410endtry432endfunction433434let s:default_action = {435 \ 'ctrl-m': 'e',
55 unmodified lines
modified:test/fzf.vader+5 added
41 unmodified lines
42 call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/tmp', 'options': '-1'})43 " No change in working directory44 AssertEqual cwd, getcwd()4546 call fzf#run({'source': ['/foobar'], 'sink': 'tabe', 'dir': '/tmp', 'options': '-1'})47 AssertEqual cwd, getcwd()48 tabclose49 AssertEqual cwd, getcwd()5051Execute (Incomplete fzf#run with dir option and autochdir):52 set acd53 let cwd = getcwd()54 call fzf#run({'source': [], 'sink': 'e', 'dir': '/tmp', 'options': '-0'})
20 unmodified lines