]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1605: cannot specify scope for chdir() v9.1.1605
authorkuuote <znmxodq1@gmail.com>
Fri, 8 Aug 2025 11:09:25 +0000 (13:09 +0200)
committerChristian Brabandt <cb@256bit.org>
Fri, 8 Aug 2025 11:09:25 +0000 (13:09 +0200)
Problem:  Cannot specify scope for chdir()
Solution: Add optional scope argument (kuuote)

closes: #17888

Signed-off-by: kuuote <znmxodq1@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/builtin.txt
runtime/doc/version9.txt
src/evalfunc.c
src/filepath.c
src/testdir/test_cd.vim
src/testdir/test_vim9_builtin.vim
src/version.c

index 2c18290f91802dd273af76aef23681182991782c..f4409f39ad3ba20255284caeacb7694df53fbe4e 100644 (file)
@@ -1783,16 +1783,23 @@ charidx({string}, {idx} [, {countcc} [, {utf16}]])
                Return type: |Number|
 
 
-chdir({dir})                                           *chdir()*
-               Change the current working directory to {dir}.  The scope of
-               the directory change depends on the directory of the current
-               window:
-                       - If the current window has a window-local directory
-                         (|:lcd|), then changes the window local directory.
-                       - Otherwise, if the current tabpage has a local
-                         directory (|:tcd|) then changes the tabpage local
-                         directory.
-                       - Otherwise, changes the global directory.
+chdir({dir} [, {scope}])                               *chdir()*
+               Changes the current working directory to {dir}.  The scope of
+               the change is determined as follows:
+               If {scope} is not present, the current working directory is
+               changed to the scope of the current directory:
+                   - If the window local directory (|:lcd|) is set, it
+                     changes the current working directory for that scope.
+                   - Otherwise, if the tab page local directory (|:tcd|) is
+                     set, it changes the current directory for that scope.
+                   - Otherwise, changes the global directory for that scope.
+
+               If {scope} is present, changes the current working directory
+               for the specified scope:
+                   "window"    Changes the window local directory.  |:lcd|
+                   "tabpage"   Changes the tab page local directory.  |:tcd|
+                   "global"    Changes the global directory.  |:cd|
+
                {dir} must be a String.
                If successful, returns the previous working directory.  Pass
                this to another chdir() to restore the directory.
index a82676c424ef8b5371d4b0a8a07add263ca252c9..1052e7e51579231e46b484aa2333708197c95ba9 100644 (file)
@@ -1,4 +1,4 @@
-*version9.txt*  For Vim version 9.1.  Last change: 2025 Jul 25
+*version9.txt*  For Vim version 9.1.  Last change: 2025 Aug 08
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -41722,6 +41722,7 @@ Functions: ~
   not finished
 - Add the optional {opts} |Dict| argument to |getchar()| to control: cursor
   behaviour, return type and whether or not to simplify the returned key
+- |chdir()| allows to optionally specify a scope argument
 
 Others: ~
 - the regex engines match correctly case-insensitive multi-byte characters
index c7936238e0870aa475fa3c02197c547d5047dc10..7211048d113671d372ee0805f516920920e90a5b 100644 (file)
@@ -2086,7 +2086,7 @@ static funcentry_T global_functions[] =
                        ret_number,         f_charcol},
     {"charidx",                2, 4, FEARG_1,      arg4_string_number_bool_bool,
                        ret_number,         f_charidx},
-    {"chdir",          1, 1, FEARG_1,      arg1_string,
+    {"chdir",          1, 2, FEARG_1,      arg2_string,
                        ret_string,         f_chdir},
     {"cindent",                1, 1, FEARG_1,      arg1_lnum,
                        ret_number,         f_cindent},
index 15f770f42c8196c049002b578644e67cf507aa4b..0ef77f9d8f9889ffba1b20e6c463d31e96f80bda 100644 (file)
@@ -842,7 +842,22 @@ f_chdir(typval_T *argvars, typval_T *rettv)
        vim_free(cwd);
     }
 
-    if (curwin->w_localdir != NULL)
+    if (argvars[1].v_type != VAR_UNKNOWN)
+    {
+       char_u *s = tv_get_string(&argvars[1]);
+       if (STRCMP(s, "global") == 0)
+           scope = CDSCOPE_GLOBAL;
+       else if (STRCMP(s, "tabpage") == 0)
+           scope = CDSCOPE_TABPAGE;
+       else if (STRCMP(s, "window") == 0)
+           scope = CDSCOPE_WINDOW;
+       else
+       {
+           semsg(_(e_invalid_value_for_argument_str_str), "scope", s);
+           return;
+       }
+    }
+    else if (curwin->w_localdir != NULL)
        scope = CDSCOPE_WINDOW;
     else if (curtab->tp_localdir != NULL)
        scope = CDSCOPE_TABPAGE;
index 878a2aeb5f51d85907311a5aab8bc55d39bfe1c0..1b7777e1b554fa733ab8c7bd2d935cd621fd18e8 100644 (file)
@@ -96,10 +96,20 @@ func Test_chdir_func()
   call assert_equal('y', fnamemodify(getcwd(3, 2), ':t'))
   call assert_equal('testdir', fnamemodify(getcwd(1, 1), ':t'))
 
+  " Forcing scope
+  call chdir('.', 'global')
+  call assert_match('^\[global\]', trim(execute('verbose pwd')))
+  call chdir('.', 'tabpage')
+  call assert_match('^\[tabpage\]', trim(execute('verbose pwd')))
+  call chdir('.', 'window')
+  call assert_match('^\[window\]', trim(execute('verbose pwd')))
+
   " Error case
   call assert_fails("call chdir('dir-abcd')", 'E344:')
   silent! let d = chdir("dir_abcd")
   call assert_equal("", d)
+  call assert_fails("call chdir('.', test_null_string())", 'E475:')
+  call assert_fails("call chdir('.', [])", 'E730:')
   " Should not crash
   call chdir(d)
   call assert_equal('', chdir([]))
index 675dcd00c92e044c1bb51230c43185583d05c64f..40cc15f1a2aeb15aabba9cbb768e6ed1c2cbabc4 100644 (file)
@@ -770,6 +770,8 @@ enddef
 
 def Test_chdir()
   assert_fails('chdir(true)', 'E1174:')
+  assert_fails('chdir(".", test_null_string())', 'E475:')
+  assert_fails('chdir(".", [])', 'E730:')
 enddef
 
 def Test_cindent()
index 2cd99fc78c95d53cf97e7e984e14c582ff4b15b9..e4acba9a5d8df04faec88357300c3b34cffce897 100644 (file)
@@ -719,6 +719,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1605,
 /**/
     1604,
 /**/