Skip to content

Commit 5459129

Browse files
committed
patch 8.0.1489: there is no easy way to get the global directory
Problem: There is no easy way to get the global directory, esp. if some windows have a local directory. Solution: Make getcwd(-1) return the global directory. (Andy Massimino, closes #2606)
1 parent 0d20737 commit 5459129

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

runtime/doc/eval.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4484,10 +4484,13 @@ getcwd([{winnr} [, {tabnr}]])
44844484
Without arguments, for the current window.
44854485

44864486
With {winnr} return the local current directory of this window
4487-
in the current tab page.
4487+
in the current tab page. {winnr} can be the window number or
4488+
the |window-ID|.
4489+
If {winnr} is -1 return the name of the global working
4490+
directory. See also |haslocaldir()|.
4491+
44884492
With {winnr} and {tabnr} return the local current directory of
44894493
the window in the specified tab page.
4490-
{winnr} can be the window number or the |window-ID|.
44914494
Return an empty string if the arguments are invalid.
44924495

44934496
getfsize({fname}) *getfsize()*

src/evalfunc.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4613,16 +4613,21 @@ f_getcwd(typval_T *argvars, typval_T *rettv)
46134613
{
46144614
win_T *wp = NULL;
46154615
char_u *cwd;
4616+
int global = FALSE;
46164617

46174618
rettv->v_type = VAR_STRING;
46184619
rettv->vval.v_string = NULL;
46194620

4620-
wp = find_tabwin(&argvars[0], &argvars[1]);
4621-
if (wp != NULL)
4621+
if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4622+
global = TRUE;
4623+
else
4624+
wp = find_tabwin(&argvars[0], &argvars[1]);
4625+
4626+
if (wp != NULL && wp->w_localdir != NULL)
4627+
rettv->vval.v_string = vim_strsave(wp->w_localdir);
4628+
else if (wp != NULL || global)
46224629
{
4623-
if (wp->w_localdir != NULL)
4624-
rettv->vval.v_string = vim_strsave(wp->w_localdir);
4625-
else if (globaldir != NULL)
4630+
if (globaldir != NULL)
46264631
rettv->vval.v_string = vim_strsave(globaldir);
46274632
else
46284633
{

src/testdir/test_getcwd.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function SetUp()
3737
new
3838
call mkdir('Xtopdir')
3939
cd Xtopdir
40+
let g:topdir = getcwd()
4041
call mkdir('Xdir1')
4142
call mkdir('Xdir2')
4243
call mkdir('Xdir3')
@@ -56,36 +57,44 @@ function Test_GetCwd()
5657
3wincmd w
5758
lcd Xdir1
5859
call assert_equal("a Xdir1 1", GetCwdInfo(0, 0))
60+
call assert_equal(g:topdir, getcwd(-1))
5961
wincmd W
6062
call assert_equal("b Xtopdir 0", GetCwdInfo(0, 0))
63+
call assert_equal(g:topdir, getcwd(-1))
6164
wincmd W
6265
lcd Xdir3
6366
call assert_equal("c Xdir3 1", GetCwdInfo(0, 0))
6467
call assert_equal("a Xdir1 1", GetCwdInfo(bufwinnr("a"), 0))
6568
call assert_equal("b Xtopdir 0", GetCwdInfo(bufwinnr("b"), 0))
6669
call assert_equal("c Xdir3 1", GetCwdInfo(bufwinnr("c"), 0))
70+
call assert_equal(g:topdir, getcwd(-1))
6771
wincmd W
6872
call assert_equal("a Xdir1 1", GetCwdInfo(bufwinnr("a"), tabpagenr()))
6973
call assert_equal("b Xtopdir 0", GetCwdInfo(bufwinnr("b"), tabpagenr()))
7074
call assert_equal("c Xdir3 1", GetCwdInfo(bufwinnr("c"), tabpagenr()))
75+
call assert_equal(g:topdir, getcwd(-1))
7176

7277
tabnew x
7378
new y
7479
new z
7580
3wincmd w
7681
call assert_equal("x Xtopdir 0", GetCwdInfo(0, 0))
82+
call assert_equal(g:topdir, getcwd(-1))
7783
wincmd W
7884
lcd Xdir2
7985
call assert_equal("y Xdir2 1", GetCwdInfo(0, 0))
86+
call assert_equal(g:topdir, getcwd(-1))
8087
wincmd W
8188
lcd Xdir3
8289
call assert_equal("z Xdir3 1", GetCwdInfo(0, 0))
8390
call assert_equal("x Xtopdir 0", GetCwdInfo(bufwinnr("x"), 0))
8491
call assert_equal("y Xdir2 1", GetCwdInfo(bufwinnr("y"), 0))
8592
call assert_equal("z Xdir3 1", GetCwdInfo(bufwinnr("z"), 0))
93+
call assert_equal(g:topdir, getcwd(-1))
8694
let tp_nr = tabpagenr()
8795
tabrewind
8896
call assert_equal("x Xtopdir 0", GetCwdInfo(3, tp_nr))
8997
call assert_equal("y Xdir2 1", GetCwdInfo(2, tp_nr))
9098
call assert_equal("z Xdir3 1", GetCwdInfo(1, tp_nr))
99+
call assert_equal(g:topdir, getcwd(-1))
91100
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static char *(features[]) =
771771

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1489,
774776
/**/
775777
1488,
776778
/**/

0 commit comments

Comments
 (0)