1
0
forked from aniani/vim

patch 8.1.1291: not easy to change directory and restore

Problem:    Not easy to change directory and restore.
Solution:   Add the chdir() function. (Yegappan Lakshmanan, closes #4358)
This commit is contained in:
Bram Moolenaar
2019-05-07 22:06:52 +02:00
parent fd31e45e4b
commit 1063f3d200
9 changed files with 224 additions and 85 deletions

View File

@@ -107,6 +107,7 @@ static void f_ch_status(typval_T *argvars, typval_T *rettv);
#endif
static void f_changenr(typval_T *argvars, typval_T *rettv);
static void f_char2nr(typval_T *argvars, typval_T *rettv);
static void f_chdir(typval_T *argvars, typval_T *rettv);
static void f_cindent(typval_T *argvars, typval_T *rettv);
static void f_clearmatches(typval_T *argvars, typval_T *rettv);
static void f_col(typval_T *argvars, typval_T *rettv);
@@ -597,6 +598,7 @@ static struct fst
#endif
{"changenr", 0, 0, f_changenr},
{"char2nr", 1, 2, f_char2nr},
{"chdir", 1, 1, f_chdir},
{"cindent", 1, 1, f_cindent},
{"clearmatches", 0, 1, f_clearmatches},
{"col", 1, 1, f_col},
@@ -2490,6 +2492,45 @@ f_char2nr(typval_T *argvars, typval_T *rettv)
rettv->vval.v_number = tv_get_string(&argvars[0])[0];
}
/*
* "chdir(dir)" function
*/
static void
f_chdir(typval_T *argvars, typval_T *rettv)
{
char_u *cwd;
cdscope_T scope = CDSCOPE_GLOBAL;
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
if (argvars[0].v_type != VAR_STRING)
return;
// Return the current directory
cwd = alloc(MAXPATHL);
if (cwd != NULL)
{
if (mch_dirname(cwd, MAXPATHL) != FAIL)
{
#ifdef BACKSLASH_IN_FILENAME
slash_adjust(cwd);
#endif
rettv->vval.v_string = vim_strsave(cwd);
}
vim_free(cwd);
}
if (curwin->w_localdir != NULL)
scope = CDSCOPE_WINDOW;
else if (curtab->tp_localdir != NULL)
scope = CDSCOPE_TABPAGE;
if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
// Directory change failed
VIM_CLEAR(rettv->vval.v_string);
}
/*
* "cindent(lnum)" function
*/