diff options
author | zeertzjq <zeertzjq@outlook.com> | 2021-10-17 22:04:53 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2021-10-17 22:04:53 +0800 |
commit | 60584c0245a55d72422686aa702132814145b0c1 (patch) | |
tree | 0bb558f11c5bd9e5bb0b4a51ea77530899767dd7 /src/nvim/ex_docmd.c | |
parent | 34cfe745681189bbd8ec2543971a49e013bfebf9 (diff) | |
download | rneovim-60584c0245a55d72422686aa702132814145b0c1.tar.gz rneovim-60584c0245a55d72422686aa702132814145b0c1.tar.bz2 rneovim-60584c0245a55d72422686aa702132814145b0c1.zip |
vim-patch:8.2.0909: cannot go back to the previous local directory
Problem: Cannot go back to the previous local directory.
Solution: Add "tcd -" and "lcd -". (Yegappan Lakshmanan, closes vim/vim#4362)
https://github.com/vim/vim/commit/002bc79991286934a9593b80635c27d4238cdfc4
Diffstat (limited to 'src/nvim/ex_docmd.c')
-rw-r--r-- | src/nvim/ex_docmd.c | 56 |
1 files changed, 44 insertions, 12 deletions
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 06e94fea99..26c06b2675 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -7707,6 +7707,21 @@ void free_cd_dir(void) #endif +// Get the previous directory for the given chdir scope. +static char_u *get_prevdir(CdScope scope) +{ + switch (scope) { + case kCdScopeTab: + return curtab->tp_prevdir; + break; + case kCdScopeWindow: + return curwin->w_prevdir; + break; + default: + return prev_dir; + } +} + /// Deal with the side effects of changing the current directory. /// /// @param scope Scope of the function call (global, tab or window). @@ -7721,9 +7736,10 @@ void post_chdir(CdScope scope, bool trigger_dirchanged) } if (scope < kCdScopeGlobal) { + char_u *pdir = get_prevdir(scope); // If still in global directory, set CWD as the global directory. - if (globaldir == NULL && prev_dir != NULL) { - globaldir = vim_strsave(prev_dir); + if (globaldir == NULL && pdir != NULL) { + globaldir = vim_strsave(pdir); } } @@ -7754,10 +7770,13 @@ void post_chdir(CdScope scope, bool trigger_dirchanged) } /// Change directory function used by :cd/:tcd/:lcd Ex commands and the chdir() function. +/// @param new_dir The directory to change to. +/// @param scope Scope of the function call (global, tab or window). /// @return true if the directory is successfully changed. bool changedir_func(char_u *new_dir, CdScope scope) { char_u *tofree; + char_u *pdir = NULL; bool retval = false; if (new_dir == NULL || allbuf_locked()) { @@ -7766,19 +7785,32 @@ bool changedir_func(char_u *new_dir, CdScope scope) // ":cd -": Change to previous directory if (STRCMP(new_dir, "-") == 0) { - if (prev_dir == NULL) { + pdir = get_prevdir(scope); + if (pdir == NULL) { EMSG(_("E186: No previous directory")); return false; } - new_dir = prev_dir; + new_dir = pdir; } - // Save current directory for next ":cd -" - tofree = prev_dir; + // Free the previous directory + tofree = get_prevdir(scope); + if (os_dirname(NameBuff, MAXPATHL) == OK) { - prev_dir = vim_strsave(NameBuff); + pdir = vim_strsave(NameBuff); } else { - prev_dir = NULL; + pdir = NULL; + } + + switch (scope) { + case kCdScopeTab: + curtab->tp_prevdir = pdir; + break; + case kCdScopeWindow: + curwin->w_prevdir = pdir; + break; + default: + prev_dir = pdir; } #if defined(UNIX) @@ -7790,12 +7822,12 @@ bool changedir_func(char_u *new_dir, CdScope scope) } #endif - bool dir_differs = prev_dir == NULL || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0; - if (dir_differs && vim_chdir(new_dir)) { - EMSG(_(e_failed)); - } else { + if (vim_chdir(new_dir) == 0) { + bool dir_differs = pdir == NULL || pathcmp((char *)pdir, (char *)new_dir, -1) != 0; post_chdir(scope, dir_differs); retval = true; + } else { + EMSG(_(e_failed)); } xfree(tofree); |