From 295e449db653a432d1cdcea2122e737b7c6ed3dc Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Wed, 18 Jan 2017 17:15:15 +0100 Subject: [PATCH] Add sy#util#chdir() --- autoload/sy.vim | 8 -------- autoload/sy/repo.vim | 26 ++++++++++++++++++-------- autoload/sy/util.vim | 8 ++++++++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/autoload/sy.vim b/autoload/sy.vim index 16a4ff4..8e8a866 100644 --- a/autoload/sy.vim +++ b/autoload/sy.vim @@ -25,16 +25,8 @@ function! sy#start() abort return endif - function! s:chdir() - return haslocaldir() - \ ? 'lcd' - \ : (exists(':tcd') && haslocaldir(-1, 0)) ? 'tcd' : 'cd' - endfunction - " sy_info is used in autoload/sy/repo let b:sy_info = { - \ 'chdir': s:chdir(), - \ 'cwd': fnameescape(getcwd()), \ 'dir': fnamemodify(sy_path, ':p:h'), \ 'path': sy#util#escape(sy_path), \ 'file': sy#util#escape(fnamemodify(sy_path, ':t')), diff --git a/autoload/sy/repo.vim b/autoload/sy/repo.vim index 3747448..aeec448 100644 --- a/autoload/sy/repo.vim +++ b/autoload/sy/repo.vim @@ -48,37 +48,46 @@ endfunction function! sy#repo#get_diff_start(vcs, do_register) abort call sy#verbose('s:get_diff_start()', a:vcs) + " Neovim if has('nvim') if exists('b:job_id_'.a:vcs) silent! call jobstop(b:job_id_{a:vcs}) endif + let [cmd, options] = s:initialize_job(a:vcs, a:do_register) - execute b:sy_info.chdir fnameescape(b:sy_info.dir) + let [cwd, chdir] = sy#util#chdir() + try + execute chdir fnameescape(b:sy_info.dir) let b:job_id_{a:vcs} = jobstart(cmd, extend(options, { \ 'on_stdout': function('s:callback_stdout_nvim'), \ 'on_exit': function('s:callback_exit'), \ })) - call sy#verbose('job_start()', a:vcs) finally - execute b:sy_info.chdir b:sy_info.cwd + execute chdir fnameescape(cwd) endtry + + " Newer Vim elseif v:version > 704 || v:version == 704 && has('patch1967') if exists('b:job_id_'.a:vcs) silent! call job_stop(b:job_id_{a:vcs}) endif + let [cmd, options] = s:initialize_job(a:vcs, a:do_register) - execute b:sy_info.chdir fnameescape(b:sy_info.dir) + let [cwd, chdir] = sy#util#chdir() + try + execute chdir fnameescape(b:sy_info.dir) let b:job_id_{a:vcs} = job_start(cmd, { \ 'in_io': 'null', \ 'out_cb': function('s:callback_stdout_vim', options), \ 'exit_cb': function('s:callback_exit', options), \ }) - call sy#verbose('job_start()', a:vcs) finally - execute b:sy_info.chdir b:sy_info.cwd + execute chdir fnameescape(cwd) endtry + + " Older Vim else let diff = split(s:run(a:vcs), '\n') call sy#repo#get_diff_{a:vcs}(v:shell_error, diff, a:do_register) @@ -238,15 +247,16 @@ endfunction " Function: s:run {{{1 function! s:run(vcs) - execute b:sy_info.chdir fnameescape(b:sy_info.dir) + let [cwd, chdir] = sy#util#chdir() try + execute chdir fnameescape(b:sy_info.dir) let ret = system(s:expand_cmd(a:vcs)) catch " This exception message can be seen via :SignifyDebugUnknown. " E.g. unquoted VCS programs in vcd_cmds can lead to E484. let ret = v:exception .' at '. v:throwpoint finally - execute b:sy_info.chdir b:sy_info.cwd + execute chdir fnameescape(cwd) return ret endtry endfunction diff --git a/autoload/sy/util.vim b/autoload/sy/util.vim index 76eca1a..0501e8d 100644 --- a/autoload/sy/util.vim +++ b/autoload/sy/util.vim @@ -79,3 +79,11 @@ function! sy#util#shell_redirect(path) abort return &shellredir .' '. a:path endif endfunction + +" Function: #chdir {{{1 +function! sy#util#chdir() abort + let chdir = haslocaldir() + \ ? 'lcd' + \ : (exists(':tcd') && haslocaldir(-1, 0)) ? 'tcd' : 'cd' + return [getcwd(), chdir] +endfunction