comparison 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
comparison
equal deleted inserted replaced
20710:24fa685e72e9 20711:d91b8d1e5198
6580 VIM_CLEAR(globaldir); 6580 VIM_CLEAR(globaldir);
6581 } 6581 }
6582 #endif 6582 #endif
6583 6583
6584 /* 6584 /*
6585 * Get the previous directory for the given chdir scope.
6586 */
6587 static char_u *
6588 get_prevdir(cdscope_T scope)
6589 {
6590 if (scope == CDSCOPE_WINDOW)
6591 return curwin->w_prevdir;
6592 else if (scope == CDSCOPE_TABPAGE)
6593 return curtab->tp_prevdir;
6594 return prev_dir;
6595 }
6596
6597 /*
6585 * Deal with the side effects of changing the current directory. 6598 * Deal with the side effects of changing the current directory.
6586 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command. 6599 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6587 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command. 6600 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
6588 */ 6601 */
6589 void 6602 void
6593 // Clear tab local directory for both :cd and :tcd 6606 // Clear tab local directory for both :cd and :tcd
6594 VIM_CLEAR(curtab->tp_localdir); 6607 VIM_CLEAR(curtab->tp_localdir);
6595 VIM_CLEAR(curwin->w_localdir); 6608 VIM_CLEAR(curwin->w_localdir);
6596 if (scope != CDSCOPE_GLOBAL) 6609 if (scope != CDSCOPE_GLOBAL)
6597 { 6610 {
6598 // If still in global directory, need to remember current 6611 char_u *pdir = get_prevdir(scope);
6599 // directory as global directory. 6612
6600 if (globaldir == NULL && prev_dir != NULL) 6613 // If still in the global directory, need to remember current
6601 globaldir = vim_strsave(prev_dir); 6614 // directory as the global directory.
6615 if (globaldir == NULL && pdir != NULL)
6616 globaldir = vim_strsave(pdir);
6617
6602 // Remember this local directory for the window. 6618 // Remember this local directory for the window.
6603 if (mch_dirname(NameBuff, MAXPATHL) == OK) 6619 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6604 { 6620 {
6605 if (scope == CDSCOPE_TABPAGE) 6621 if (scope == CDSCOPE_TABPAGE)
6606 curtab->tp_localdir = vim_strsave(NameBuff); 6622 curtab->tp_localdir = vim_strsave(NameBuff);
6608 curwin->w_localdir = vim_strsave(NameBuff); 6624 curwin->w_localdir = vim_strsave(NameBuff);
6609 } 6625 }
6610 } 6626 }
6611 else 6627 else
6612 { 6628 {
6613 // We are now in the global directory, no need to remember its 6629 // We are now in the global directory, no need to remember its name.
6614 // name.
6615 VIM_CLEAR(globaldir); 6630 VIM_CLEAR(globaldir);
6616 } 6631 }
6617 6632
6618 shorten_fnames(TRUE); 6633 shorten_fnames(TRUE);
6619 } 6634 }
6631 char_u *new_dir, 6646 char_u *new_dir,
6632 int forceit, 6647 int forceit,
6633 cdscope_T scope) 6648 cdscope_T scope)
6634 { 6649 {
6635 char_u *tofree; 6650 char_u *tofree;
6651 char_u *pdir = NULL;
6636 int dir_differs; 6652 int dir_differs;
6637 int retval = FALSE; 6653 int retval = FALSE;
6638 6654
6639 if (new_dir == NULL || allbuf_locked()) 6655 if (new_dir == NULL || allbuf_locked())
6640 return FALSE; 6656 return FALSE;
6646 } 6662 }
6647 6663
6648 // ":cd -": Change to previous directory 6664 // ":cd -": Change to previous directory
6649 if (STRCMP(new_dir, "-") == 0) 6665 if (STRCMP(new_dir, "-") == 0)
6650 { 6666 {
6651 if (prev_dir == NULL) 6667 pdir = get_prevdir(scope);
6668 if (pdir == NULL)
6652 { 6669 {
6653 emsg(_("E186: No previous directory")); 6670 emsg(_("E186: No previous directory"));
6654 return FALSE; 6671 return FALSE;
6655 } 6672 }
6656 new_dir = prev_dir; 6673 new_dir = pdir;
6657 } 6674 }
6675
6676 // Free the previous directory
6677 tofree = get_prevdir(scope);
6658 6678
6659 // Save current directory for next ":cd -" 6679 // Save current directory for next ":cd -"
6660 tofree = prev_dir;
6661 if (mch_dirname(NameBuff, MAXPATHL) == OK) 6680 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6662 prev_dir = vim_strsave(NameBuff); 6681 pdir = vim_strsave(NameBuff);
6663 else 6682 else
6664 prev_dir = NULL; 6683 pdir = NULL;
6684 if (scope == CDSCOPE_WINDOW)
6685 curwin->w_prevdir = pdir;
6686 else if (scope == CDSCOPE_TABPAGE)
6687 curtab->tp_prevdir = pdir;
6688 else
6689 prev_dir = pdir;
6665 6690
6666 #if defined(UNIX) || defined(VMS) 6691 #if defined(UNIX) || defined(VMS)
6667 // for UNIX ":cd" means: go to home directory 6692 // for UNIX ":cd" means: go to home directory
6668 if (*new_dir == NUL) 6693 if (*new_dir == NUL)
6669 { 6694 {
6680 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL); 6705 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6681 # endif 6706 # endif
6682 new_dir = NameBuff; 6707 new_dir = NameBuff;
6683 } 6708 }
6684 #endif 6709 #endif
6685 dir_differs = new_dir == NULL || prev_dir == NULL 6710 dir_differs = new_dir == NULL || pdir == NULL
6686 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0; 6711 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
6687 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir))) 6712 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6688 emsg(_(e_failed)); 6713 emsg(_(e_failed));
6689 else 6714 else
6690 { 6715 {
6691 char_u *acmd_fname; 6716 char_u *acmd_fname;