1
0
forked from aniani/vim

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)
This commit is contained in:
Bram Moolenaar
2018-02-09 20:53:59 +01:00
parent 0d20737732
commit 5459129af2
4 changed files with 26 additions and 7 deletions

View File

@@ -4484,10 +4484,13 @@ getcwd([{winnr} [, {tabnr}]])
Without arguments, for the current window. Without arguments, for the current window.
With {winnr} return the local current directory of this window With {winnr} return the local current directory of this window
in the current tab page. in the current tab page. {winnr} can be the window number or
the |window-ID|.
If {winnr} is -1 return the name of the global working
directory. See also |haslocaldir()|.
With {winnr} and {tabnr} return the local current directory of With {winnr} and {tabnr} return the local current directory of
the window in the specified tab page. the window in the specified tab page.
{winnr} can be the window number or the |window-ID|.
Return an empty string if the arguments are invalid. Return an empty string if the arguments are invalid.
getfsize({fname}) *getfsize()* getfsize({fname}) *getfsize()*

View File

@@ -4613,16 +4613,21 @@ f_getcwd(typval_T *argvars, typval_T *rettv)
{ {
win_T *wp = NULL; win_T *wp = NULL;
char_u *cwd; char_u *cwd;
int global = FALSE;
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL; rettv->vval.v_string = NULL;
wp = find_tabwin(&argvars[0], &argvars[1]); if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
if (wp != NULL) global = TRUE;
else
wp = find_tabwin(&argvars[0], &argvars[1]);
if (wp != NULL && wp->w_localdir != NULL)
rettv->vval.v_string = vim_strsave(wp->w_localdir);
else if (wp != NULL || global)
{ {
if (wp->w_localdir != NULL) if (globaldir != NULL)
rettv->vval.v_string = vim_strsave(wp->w_localdir);
else if (globaldir != NULL)
rettv->vval.v_string = vim_strsave(globaldir); rettv->vval.v_string = vim_strsave(globaldir);
else else
{ {

View File

@@ -37,6 +37,7 @@ function SetUp()
new new
call mkdir('Xtopdir') call mkdir('Xtopdir')
cd Xtopdir cd Xtopdir
let g:topdir = getcwd()
call mkdir('Xdir1') call mkdir('Xdir1')
call mkdir('Xdir2') call mkdir('Xdir2')
call mkdir('Xdir3') call mkdir('Xdir3')
@@ -56,36 +57,44 @@ function Test_GetCwd()
3wincmd w 3wincmd w
lcd Xdir1 lcd Xdir1
call assert_equal("a Xdir1 1", GetCwdInfo(0, 0)) call assert_equal("a Xdir1 1", GetCwdInfo(0, 0))
call assert_equal(g:topdir, getcwd(-1))
wincmd W wincmd W
call assert_equal("b Xtopdir 0", GetCwdInfo(0, 0)) call assert_equal("b Xtopdir 0", GetCwdInfo(0, 0))
call assert_equal(g:topdir, getcwd(-1))
wincmd W wincmd W
lcd Xdir3 lcd Xdir3
call assert_equal("c Xdir3 1", GetCwdInfo(0, 0)) call assert_equal("c Xdir3 1", GetCwdInfo(0, 0))
call assert_equal("a Xdir1 1", GetCwdInfo(bufwinnr("a"), 0)) call assert_equal("a Xdir1 1", GetCwdInfo(bufwinnr("a"), 0))
call assert_equal("b Xtopdir 0", GetCwdInfo(bufwinnr("b"), 0)) call assert_equal("b Xtopdir 0", GetCwdInfo(bufwinnr("b"), 0))
call assert_equal("c Xdir3 1", GetCwdInfo(bufwinnr("c"), 0)) call assert_equal("c Xdir3 1", GetCwdInfo(bufwinnr("c"), 0))
call assert_equal(g:topdir, getcwd(-1))
wincmd W wincmd W
call assert_equal("a Xdir1 1", GetCwdInfo(bufwinnr("a"), tabpagenr())) call assert_equal("a Xdir1 1", GetCwdInfo(bufwinnr("a"), tabpagenr()))
call assert_equal("b Xtopdir 0", GetCwdInfo(bufwinnr("b"), tabpagenr())) call assert_equal("b Xtopdir 0", GetCwdInfo(bufwinnr("b"), tabpagenr()))
call assert_equal("c Xdir3 1", GetCwdInfo(bufwinnr("c"), tabpagenr())) call assert_equal("c Xdir3 1", GetCwdInfo(bufwinnr("c"), tabpagenr()))
call assert_equal(g:topdir, getcwd(-1))
tabnew x tabnew x
new y new y
new z new z
3wincmd w 3wincmd w
call assert_equal("x Xtopdir 0", GetCwdInfo(0, 0)) call assert_equal("x Xtopdir 0", GetCwdInfo(0, 0))
call assert_equal(g:topdir, getcwd(-1))
wincmd W wincmd W
lcd Xdir2 lcd Xdir2
call assert_equal("y Xdir2 1", GetCwdInfo(0, 0)) call assert_equal("y Xdir2 1", GetCwdInfo(0, 0))
call assert_equal(g:topdir, getcwd(-1))
wincmd W wincmd W
lcd Xdir3 lcd Xdir3
call assert_equal("z Xdir3 1", GetCwdInfo(0, 0)) call assert_equal("z Xdir3 1", GetCwdInfo(0, 0))
call assert_equal("x Xtopdir 0", GetCwdInfo(bufwinnr("x"), 0)) call assert_equal("x Xtopdir 0", GetCwdInfo(bufwinnr("x"), 0))
call assert_equal("y Xdir2 1", GetCwdInfo(bufwinnr("y"), 0)) call assert_equal("y Xdir2 1", GetCwdInfo(bufwinnr("y"), 0))
call assert_equal("z Xdir3 1", GetCwdInfo(bufwinnr("z"), 0)) call assert_equal("z Xdir3 1", GetCwdInfo(bufwinnr("z"), 0))
call assert_equal(g:topdir, getcwd(-1))
let tp_nr = tabpagenr() let tp_nr = tabpagenr()
tabrewind tabrewind
call assert_equal("x Xtopdir 0", GetCwdInfo(3, tp_nr)) call assert_equal("x Xtopdir 0", GetCwdInfo(3, tp_nr))
call assert_equal("y Xdir2 1", GetCwdInfo(2, tp_nr)) call assert_equal("y Xdir2 1", GetCwdInfo(2, tp_nr))
call assert_equal("z Xdir3 1", GetCwdInfo(1, tp_nr)) call assert_equal("z Xdir3 1", GetCwdInfo(1, tp_nr))
call assert_equal(g:topdir, getcwd(-1))
endfunc endfunc

View File

@@ -771,6 +771,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1489,
/**/ /**/
1488, 1488,
/**/ /**/