From 0f58ba10e264697e4bb330a17b41cd9a0ceb8b1e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 19 Nov 2021 20:07:04 +0800 Subject: vim-patch:8.2.3468: problem with :cd when editing file in non-existent directory Problem: Problem with :cd when editing file in non-existent directory. (Yee Cheng Chin) Solution: Prepend the current directory to get the full path. (closes vim/vim#8903) https://github.com/vim/vim/commit/c6376c798433bcb9ee38a8664299d11454546950 --- src/nvim/path.c | 16 +++++++++++----- src/nvim/testdir/test_cd.vim | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/nvim/path.c b/src/nvim/path.c index c28d848683..ffe7fb636f 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -2245,11 +2245,17 @@ int path_full_dir_name(char *directory, char *buffer, size_t len) } if (os_chdir(directory) != SUCCESS) { - // Do not return immediately since we may be in the wrong directory. - retval = FAIL; - } - - if (retval == FAIL || os_dirname((char_u *)buffer, len) == FAIL) { + // Path does not exist (yet). For a full path fail, + // will use the path as-is. For a relative path use + // the current directory and append the file name. + if (path_is_absolute((const char_u *)directory)) { + // Do not return immediately since we may be in the wrong directory. + retval = FAIL; + } else { + xstrlcpy(buffer, old_dir, len); + append_path(buffer, directory, len); + } + } else if (os_dirname((char_u *)buffer, len) == FAIL) { // Do not return immediately since we are in the wrong directory. retval = FAIL; } diff --git a/src/nvim/testdir/test_cd.vim b/src/nvim/testdir/test_cd.vim index 0bba321ee2..0502791c88 100644 --- a/src/nvim/testdir/test_cd.vim +++ b/src/nvim/testdir/test_cd.vim @@ -215,3 +215,22 @@ func Test_cd_from_non_existing_dir() cd - call assert_equal(saveddir, getcwd()) endfunc + +func Test_cd_unknown_dir() + call mkdir('Xa') + cd Xa + call writefile(['text'], 'Xb.txt') + edit Xa/Xb.txt + let first_buf = bufnr() + cd .. + edit + call assert_equal(first_buf, bufnr()) + edit Xa/Xb.txt + call assert_notequal(first_buf, bufnr()) + + bwipe! + exe "bwipe! " .. first_buf + call delete('Xa', 'rf') +endfunc + +" vim: shiftwidth=2 sts=2 expandtab -- cgit From 4785cad8ee808d166a9cbc9d7ab5761da70d0563 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 19 Nov 2021 20:07:04 +0800 Subject: vim-patch:8.2.3617: ":verbose pwd" does not mention 'autochdir' was applied Problem: ":verbose pwd" does not mention 'autochdir' was applied. Solution: Remember the last chdir was done by 'autochdir'. (issue vim/vim#9142) https://github.com/vim/vim/commit/0526815c15170a5926e1008600ec29d42d8b64c2 --- src/nvim/buffer.c | 1 + src/nvim/ex_docmd.c | 5 ++++- src/nvim/globals.h | 2 ++ src/nvim/testdir/test_autochdir.vim | 30 ++++++++++++++++++++++++++++++ src/nvim/window.c | 2 ++ 5 files changed, 39 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 88938d5099..89e237a517 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -1588,6 +1588,7 @@ void do_autochdir(void) && curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname, kCdCauseAuto) == OK) { post_chdir(kCdScopeGlobal, false); + last_chdir_reason = "autochdir"; shorten_fnames(true); } } diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index e622a2a68f..ae868bd1cf 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -7753,6 +7753,7 @@ void post_chdir(CdScope scope, bool trigger_dirchanged) abort(); } + last_chdir_reason = NULL; shorten_fnames(true); if (trigger_dirchanged) { @@ -7870,7 +7871,9 @@ static void ex_pwd(exarg_T *eap) #endif if (p_verbose > 0) { char *context = "global"; - if (curwin->w_localdir != NULL) { + if (last_chdir_reason != NULL) { + context = last_chdir_reason; + } else if (curwin->w_localdir != NULL) { context = "window"; } else if (curtab->tp_localdir != NULL) { context = "tabpage"; diff --git a/src/nvim/globals.h b/src/nvim/globals.h index e2d3378402..8a273e37f6 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -789,6 +789,8 @@ extern char_u *compiled_sys; // directory is not a local directory, globaldir is NULL. EXTERN char_u *globaldir INIT(= NULL); +EXTERN char *last_chdir_reason INIT(= NULL); + // Whether 'keymodel' contains "stopsel" and "startsel". EXTERN bool km_stopsel INIT(= false); EXTERN bool km_startsel INIT(= false); diff --git a/src/nvim/testdir/test_autochdir.vim b/src/nvim/testdir/test_autochdir.vim index 0b76828dd7..e172d0cb9a 100644 --- a/src/nvim/testdir/test_autochdir.vim +++ b/src/nvim/testdir/test_autochdir.vim @@ -26,4 +26,34 @@ func Test_set_filename() call delete('samples/Xtest') endfunc +func Test_verbose_pwd() + CheckFunction test_autochdir + let cwd = getcwd() + call test_autochdir() + + edit global.txt + call assert_match('\[global\].*testdir$', execute('verbose pwd')) + + call mkdir('Xautodir') + split Xautodir/local.txt + lcd Xautodir + call assert_match('\[window\].*testdir[/\\]Xautodir', execute('verbose pwd')) + + set acd + wincmd w + call assert_match('\[autochdir\].*testdir$', execute('verbose pwd')) + wincmd w + call assert_match('\[autochdir\].*testdir[/\\]Xautodir', execute('verbose pwd')) + set noacd + call assert_match('\[autochdir\].*testdir[/\\]Xautodir', execute('verbose pwd')) + wincmd w + call assert_match('\[global\].*testdir', execute('verbose pwd')) + wincmd w + call assert_match('\[window\].*testdir[/\\]Xautodir', execute('verbose pwd')) + + bwipe! + call chdir(cwd) + call delete('Xautodir', 'rf') +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/window.c b/src/nvim/window.c index 9f6481f5de..c6e32827e7 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -4592,6 +4592,7 @@ void fix_current_dir(void) do_autocmd_dirchanged(new_dir, curwin->w_localdir ? kCdScopeWindow : kCdScopeTabpage, kCdCauseWindow); } + last_chdir_reason = NULL; shorten_fnames(true); } } else if (globaldir != NULL) { @@ -4603,6 +4604,7 @@ void fix_current_dir(void) } } XFREE_CLEAR(globaldir); + last_chdir_reason = NULL; shorten_fnames(true); } } -- cgit From 54e9cce61274c89e4d8cd80832faecf67724e834 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 19 Nov 2021 20:07:04 +0800 Subject: revert: partially revert #9894 Vim has a solution to #9892 with fewer side-effects in patch 8.2.3618 --- src/nvim/buffer.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 89e237a517..07c775e6d7 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -1587,7 +1587,6 @@ void do_autochdir(void) if (starting == 0 && curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname, kCdCauseAuto) == OK) { - post_chdir(kCdScopeGlobal, false); last_chdir_reason = "autochdir"; shorten_fnames(true); } -- cgit From 3d504f27a002b3ce6acf75398789a37611b35c44 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 19 Nov 2021 20:07:04 +0800 Subject: vim-patch:8.2.3618: getcwd() is unclear about how 'autochdir' is used Problem: getcwd() is unclear about how 'autochdir' is used. Solution: Update the help for getcwd(). Without any arguments always return the actual current directory. (closes vim/vim#9142) https://github.com/vim/vim/commit/851c7a699ae00bdc14a4db874cf722b7b7393b53 --- src/nvim/eval/funcs.c | 14 +++++--------- src/nvim/testdir/test_cd.vim | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 7549ec7ac8..3cc5b49f8f 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -3485,11 +3485,6 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } - // If the user didn't specify anything, default to window scope - if (scope == kCdScopeInvalid) { - scope = MIN_CD_SCOPE; - } - // Find the tabpage by number if (scope_number[kCdScopeTabpage] > 0) { tp = find_tabpage(scope_number[kCdScopeTabpage]); @@ -3535,12 +3530,13 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv, FunPtr fptr) case kCdScopeGlobal: if (globaldir) { // `globaldir` is not always set. from = globaldir; - } else if (os_dirname(cwd, MAXPATHL) == FAIL) { // Get the OS CWD. + break; + } + FALLTHROUGH; // In global directory, just need to get OS CWD. + case kCdScopeInvalid: // If called without any arguments, get OS CWD. + if (os_dirname(cwd, MAXPATHL) == FAIL) { from = (char_u *)""; // Return empty string on failure. } - break; - case kCdScopeInvalid: // We should never get here - abort(); } if (from) { diff --git a/src/nvim/testdir/test_cd.vim b/src/nvim/testdir/test_cd.vim index 0502791c88..57db0a2544 100644 --- a/src/nvim/testdir/test_cd.vim +++ b/src/nvim/testdir/test_cd.vim @@ -233,4 +233,24 @@ func Test_cd_unknown_dir() call delete('Xa', 'rf') endfunc +func Test_getcwd_actual_dir() + CheckFunction test_autochdir + let startdir = getcwd() + call mkdir('Xactual') + call test_autochdir() + set autochdir + edit Xactual/file.txt + call assert_match('testdir.Xactual$', getcwd()) + lcd .. + call assert_match('testdir$', getcwd()) + edit + call assert_match('testdir.Xactual$', getcwd()) + call assert_match('testdir$', getcwd(win_getid())) + + set noautochdir + bwipe! + call chdir(startdir) + call delete('Xactual', 'rf') +endfunc + " vim: shiftwidth=2 sts=2 expandtab -- cgit From 349b9ce9df0c7f48ce236216ab0d2c507f483425 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 19 Nov 2021 20:07:04 +0800 Subject: vim-patch:8.2.3622: "verbose pwd" shows confusing info Problem: "verbose pwd" shows confusing info when :lcd does not change directory. Solution: Clear last_chdir_reason also when the directory does not change. (closes vim/vim#9160) https://github.com/vim/vim/commit/64be6aa3a54ecfe355d4a03e1200650c301e7f29 This only ports the tests, as this is already Nvim's behavior. --- src/nvim/testdir/test_autochdir.vim | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/nvim/testdir/test_autochdir.vim b/src/nvim/testdir/test_autochdir.vim index e172d0cb9a..9ad727241e 100644 --- a/src/nvim/testdir/test_autochdir.vim +++ b/src/nvim/testdir/test_autochdir.vim @@ -42,6 +42,14 @@ func Test_verbose_pwd() set acd wincmd w call assert_match('\[autochdir\].*testdir$', execute('verbose pwd')) + execute 'lcd' cwd + call assert_match('\[window\].*testdir$', execute('verbose pwd')) + execute 'tcd' cwd + call assert_match('\[tabpage\].*testdir$', execute('verbose pwd')) + execute 'cd' cwd + call assert_match('\[global\].*testdir$', execute('verbose pwd')) + edit + call assert_match('\[autochdir\].*testdir$', execute('verbose pwd')) wincmd w call assert_match('\[autochdir\].*testdir[/\\]Xautodir', execute('verbose pwd')) set noacd -- cgit