diff src/ex_docmd.c @ 20711:d91b8d1e5198 v8.2.0909

patch 8.2.0909: cannot go back to the previous local directory Commit: https://github.com/vim/vim/commit/002bc79991286934a9593b80635c27d4238cdfc4 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Jun 5 22:33:42 2020 +0200 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 https://github.com/vim/vim/issues/4362)
author Bram Moolenaar <Bram@vim.org>
date Fri, 05 Jun 2020 22:45:04 +0200
parents 3437bf2ce2d4
children 213fb059e02e
line wrap: on
line diff
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -6582,6 +6582,19 @@ free_cd_dir(void)
 #endif
 
 /*
+ * Get the previous directory for the given chdir scope.
+ */
+    static char_u *
+get_prevdir(cdscope_T scope)
+{
+    if (scope == CDSCOPE_WINDOW)
+	return curwin->w_prevdir;
+    else if (scope == CDSCOPE_TABPAGE)
+	return curtab->tp_prevdir;
+    return prev_dir;
+}
+
+/*
  * Deal with the side effects of changing the current directory.
  * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
  * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
@@ -6595,10 +6608,13 @@ post_chdir(cdscope_T scope)
     VIM_CLEAR(curwin->w_localdir);
     if (scope != CDSCOPE_GLOBAL)
     {
-	// If still in global directory, need to remember current
-	// directory as global directory.
-	if (globaldir == NULL && prev_dir != NULL)
-	    globaldir = vim_strsave(prev_dir);
+	char_u	*pdir = get_prevdir(scope);
+
+	// If still in the global directory, need to remember current
+	// directory as the global directory.
+	if (globaldir == NULL && pdir != NULL)
+	    globaldir = vim_strsave(pdir);
+
 	// Remember this local directory for the window.
 	if (mch_dirname(NameBuff, MAXPATHL) == OK)
 	{
@@ -6610,8 +6626,7 @@ post_chdir(cdscope_T scope)
     }
     else
     {
-	// We are now in the global directory, no need to remember its
-	// name.
+	// We are now in the global directory, no need to remember its name.
 	VIM_CLEAR(globaldir);
     }
 
@@ -6633,6 +6648,7 @@ changedir_func(
 	cdscope_T	scope)
 {
     char_u	*tofree;
+    char_u	*pdir = NULL;
     int		dir_differs;
     int		retval = FALSE;
 
@@ -6648,20 +6664,29 @@ changedir_func(
     // ":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;
+    }
+
+    // Free the previous directory
+    tofree = get_prevdir(scope);
 
     // Save current directory for next ":cd -"
-    tofree = prev_dir;
     if (mch_dirname(NameBuff, MAXPATHL) == OK)
-	prev_dir = vim_strsave(NameBuff);
+	pdir = vim_strsave(NameBuff);
     else
-	prev_dir = NULL;
+	pdir = NULL;
+    if (scope == CDSCOPE_WINDOW)
+	curwin->w_prevdir = pdir;
+    else if (scope == CDSCOPE_TABPAGE)
+	curtab->tp_prevdir = pdir;
+    else
+	prev_dir = pdir;
 
 #if defined(UNIX) || defined(VMS)
     // for UNIX ":cd" means: go to home directory
@@ -6682,8 +6707,8 @@ changedir_func(
 	new_dir = NameBuff;
     }
 #endif
-    dir_differs = new_dir == NULL || prev_dir == NULL
-	|| pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
+    dir_differs = new_dir == NULL || pdir == NULL
+	|| pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
     if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
 	emsg(_(e_failed));
     else