# HG changeset patch # User Bram Moolenaar # Date 1639225803 -3600 # Node ID 8088fa133b933bb6db6160b6aea2821e91462361 # Parent 44215e355c69f99d214c2561209b14ddb45811ce patch 8.2.3780: ":cd" works differently on MS-Windows Commit: https://github.com/vim/vim/commit/29f3a4591528130fded3fe1d63d74bcf22ab4f6c Author: Bakudankun Date: Sat Dec 11 12:28:08 2021 +0000 patch 8.2.3780: ":cd" works differently on MS-Windows Problem: ":cd" works differently on MS-Windows. Solution: Add the 'cdhome' option. (closes https://github.com/vim/vim/issues/9324) diff --git a/runtime/doc/editing.txt b/runtime/doc/editing.txt --- a/runtime/doc/editing.txt +++ b/runtime/doc/editing.txt @@ -1298,11 +1298,12 @@ Changing directory fails when the curren present in 'cpoptions' and "!" is not used in the command. *:cd* *E747* *E472* -:cd[!] On non-Unix systems: Print the current directory - name. On Unix systems: Change the current directory - to the home directory. Use |:pwd| to print the - current directory on all systems. - On Unix systems: clear any window-local directory. +:cd[!] On non-Unix systems when 'cdhome' is off: Print the + current directory name. + Otherwise: Change the current directory to the home + directory. Clear any window-local directory. + Use |:pwd| to print the current directory on all + systems. :cd[!] {path} Change the current directory to {path}. If {path} is relative, it is searched for in the diff --git a/runtime/doc/quickref.txt b/runtime/doc/quickref.txt --- a/runtime/doc/quickref.txt +++ b/runtime/doc/quickref.txt @@ -635,6 +635,7 @@ Short explanation of each option: *opti 'buflisted' 'bl' whether the buffer shows up in the buffer list 'buftype' 'bt' special type of buffer 'casemap' 'cmp' specifies how case of letters is changed +'cdhome' 'cdh' change directory to the home directory by ":cd" 'cdpath' 'cd' list of directories searched with ":cd" 'cedit' key used to open the command-line window 'charconvert' 'ccv' expression for character encoding conversion diff --git a/runtime/optwin.vim b/runtime/optwin.vim --- a/runtime/optwin.vim +++ b/runtime/optwin.vim @@ -260,6 +260,10 @@ call OptionG("sect", §) call AddOption("path", gettext("list of directory names used for file searching")) call append("$", "\t" .. s:global_or_local) call OptionG("pa", &pa) +if exists("+cdhome") + call AddOption("cdhome", gettext("change directory to the home directory by :cd")) + call BinOptionG("cdh", &cdh) +endif call AddOption("cdpath", gettext("list of directory names used for :cd")) call OptionG("cd", &cd) if exists("+autochdir") diff --git a/src/ex_docmd.c b/src/ex_docmd.c --- a/src/ex_docmd.c +++ b/src/ex_docmd.c @@ -7402,9 +7402,13 @@ changedir_func( else prev_dir = pdir; + // For UNIX ":cd" means: go to home directory. + // On other systems too if 'cdhome' is set. #if defined(UNIX) || defined(VMS) - // for UNIX ":cd" means: go to home directory if (*new_dir == NUL) +#else + if (*new_dir == NUL && p_cdh) +#endif { // use NameBuff for home directory name # ifdef VMS @@ -7420,7 +7424,6 @@ changedir_func( # endif new_dir = NameBuff; } -#endif 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))) @@ -7459,8 +7462,8 @@ ex_cd(exarg_T *eap) new_dir = eap->arg; #if !defined(UNIX) && !defined(VMS) - // for non-UNIX ":cd" means: print current directory - if (*new_dir == NUL) + // for non-UNIX ":cd" means: print current directory unless 'cdhome' is set + if (*new_dir == NUL && !p_cdh) ex_pwd(NULL); else #endif diff --git a/src/option.h b/src/option.h --- a/src/option.h +++ b/src/option.h @@ -1094,6 +1094,7 @@ EXTERN int p_write; // 'write' EXTERN int p_wa; // 'writeany' EXTERN int p_wb; // 'writebackup' EXTERN long p_wd; // 'writedelay' +EXTERN int p_cdh; // 'cdhome' /* * "indir" values for buffer-local options. diff --git a/src/optiondefs.h b/src/optiondefs.h --- a/src/optiondefs.h +++ b/src/optiondefs.h @@ -549,6 +549,10 @@ static struct vimoption options[] = (char_u *)&p_cmp, PV_NONE, {(char_u *)"internal,keepascii", (char_u *)0L} SCTX_INIT}, + {"cdhome", "cdh", P_BOOL|P_VI_DEF|P_VIM|P_SECURE, + (char_u *)&p_cdh, PV_NONE, + {(char_u *)FALSE, (char_u *)0L} + SCTX_INIT}, {"cdpath", "cd", P_STRING|P_EXPAND|P_VI_DEF|P_SECURE|P_COMMA|P_NODUP, #ifdef FEAT_SEARCHPATH (char_u *)&p_cdpath, PV_NONE, diff --git a/src/testdir/runtest.vim b/src/testdir/runtest.vim --- a/src/testdir/runtest.vim +++ b/src/testdir/runtest.vim @@ -77,6 +77,9 @@ if has('reltime') let s:start_time = reltime() endif +" Always use forward slashes. +set shellslash + " Common with all tests on all systems. source setup.vim @@ -128,9 +131,6 @@ if has('gui_running') && exists('did_ins source $VIMRUNTIME/menu.vim endif -" Always use forward slashes. -set shellslash - let s:srcdir = expand('%:p:h:h') if has('win32') diff --git a/src/testdir/test_options.vim b/src/testdir/test_options.vim --- a/src/testdir/test_options.vim +++ b/src/testdir/test_options.vim @@ -1199,4 +1199,25 @@ func Test_opt_scrolljump() bw endfunc +" Test for the 'cdhome' option +func Test_opt_cdhome() + if has('unix') || has('vms') + throw 'Skipped: only works on non-Unix' + endif + + set cdhome& + call assert_equal(0, &cdhome) + set cdhome + + " This paragraph is copied from Test_cd_no_arg(). + let path = getcwd() + cd + call assert_equal($HOME, getcwd()) + call assert_notequal(path, getcwd()) + exe 'cd ' .. fnameescape(path) + call assert_equal(path, getcwd()) + + set cdhome& +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -754,6 +754,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 3780, +/**/ 3779, /**/ 3778,