Skip to content

Instantly share code, notes, and snippets.

@thinca
Last active November 15, 2015 13:17
Show Gist options
  • Save thinca/9e705a090a56649e7160 to your computer and use it in GitHub Desktop.
Save thinca/9e705a090a56649e7160 to your computer and use it in GitHub Desktop.
Improve getcwd() and haslocaldir()
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index 6e7039c..e36b6b6 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -1828,7 +1828,7 @@ getcmdpos() Number return cursor position in command-line
getcmdtype() String return current command-line type
getcmdwintype() String return current command-line window type
getcurpos() List position of the cursor
-getcwd() String the current working directory
+getcwd( [{winnr} [, {tabnr}]]) String get the working directory
getfontname( [{name}]) String name of font being used
getfperm( {fname}) String file permissions of file {fname}
getfsize( {fname}) Number size in bytes of file {fname}
@@ -1859,7 +1859,8 @@ globpath( {path}, {expr} [, {nosuf} [, {list} [, {alllinks}]]])
String do glob({expr}) for all dirs in {path}
has( {feature}) Number TRUE if feature {feature} supported
has_key( {dict}, {key}) Number TRUE if {dict} has entry {key}
-haslocaldir() Number TRUE if current window executed |:lcd|
+haslocaldir( [{winnr} [, {tabnr}]])
+ Number TRUE if window executed |:lcd|
hasmapto( {what} [, {mode} [, {abbr}]])
Number TRUE if mapping to {what} exists
histadd( {history},{item}) String add an item to a history
@@ -3431,9 +3432,16 @@ getcurpos() Get the position of the cursor. This is like getpos('.'), but
call setpos('.', save_cursor)
<
*getcwd()*
-getcwd() The result is a String, which is the name of the current
+getcwd([{winnr} [, {tabnr}]])
+ The result is a String, which is the name of the current
working directory.
+ With {winnr} only return local current directory of this
+ window in the current tab page.
+ With {winnr} and {tabnr} return local current directory of the
+ window in the specified tab page.
+ Return an empty string if the arguments are invalid.
+
getfsize({fname}) *getfsize()*
The result is a Number, which is the size in bytes of the
given file {fname}.
@@ -3768,9 +3776,15 @@ has_key({dict}, {key}) *has_key()*
The result is a Number, which is 1 if |Dictionary| {dict} has
an entry with key {key}. Zero otherwise.
-haslocaldir() *haslocaldir()*
- The result is a Number, which is 1 when the current
- window has set a local path via |:lcd|, and 0 otherwise.
+haslocaldir([{winnr} [, {tabnr}]]) *haslocaldir()*
+ The result is a Number, which is 1 when the window has set a
+ local path via |:lcd|, and 0 otherwise.
+
+ Without arguments use the current window.
+ With {winnr} only use this window in the current tab page.
+ With {winnr} and {tabnr} use the window in the specified tab
+ page.
+ Return 0 if the arguments are invalid.
hasmapto({what} [, {mode} [, {abbr}]]) *hasmapto()*
The result is a Number, which is 1 if there is a mapping that
diff --git a/src/eval.c b/src/eval.c
index e12813c..913d6ef 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -847,6 +847,7 @@ static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
static void free_funccal __ARGS((funccall_T *fc, int free_val));
static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
+static win_T *find_tabwin __ARGS((typval_T *wvp, typval_T *tvp));
static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
@@ -8160,7 +8161,7 @@ static struct fst
{"getcmdtype", 0, 0, f_getcmdtype},
{"getcmdwintype", 0, 0, f_getcmdwintype},
{"getcurpos", 0, 0, f_getcurpos},
- {"getcwd", 0, 0, f_getcwd},
+ {"getcwd", 0, 2, f_getcwd},
{"getfontname", 0, 1, f_getfontname},
{"getfperm", 1, 1, f_getfperm},
{"getfsize", 1, 1, f_getfsize},
@@ -8184,7 +8185,7 @@ static struct fst
{"globpath", 2, 5, f_globpath},
{"has", 1, 1, f_has},
{"has_key", 2, 2, f_has_key},
- {"haslocaldir", 0, 0, f_haslocaldir},
+ {"haslocaldir", 0, 2, f_haslocaldir},
{"hasmapto", 1, 3, f_hasmapto},
{"highlightID", 1, 1, f_hlID}, /* obsolete */
{"highlight_exists",1, 1, f_hlexists}, /* obsolete */
@@ -9073,30 +9074,11 @@ f_arglistid(argvars, rettv)
typval_T *rettv;
{
win_T *wp;
- tabpage_T *tp = NULL;
- long n;
rettv->vval.v_number = -1;
- if (argvars[0].v_type != VAR_UNKNOWN)
- {
- if (argvars[1].v_type != VAR_UNKNOWN)
- {
- n = get_tv_number(&argvars[1]);
- if (n >= 0)
- tp = find_tabpage(n);
- }
- else
- tp = curtab;
-
- if (tp != NULL)
- {
- wp = find_win_by_nr(&argvars[0], tp);
- if (wp != NULL)
- rettv->vval.v_number = wp->w_alist->id;
- }
- }
- else
- rettv->vval.v_number = curwin->w_alist->id;
+ wp = find_tabwin(&argvars[0], &argvars[1]);
+ if (wp != NULL)
+ rettv->vval.v_number = wp->w_alist->id;
}
/*
@@ -11753,25 +11735,36 @@ f_getcmdwintype(argvars, rettv)
*/
static void
f_getcwd(argvars, rettv)
- typval_T *argvars UNUSED;
+ typval_T *argvars;
typval_T *rettv;
{
+ win_T *wp = NULL;
char_u *cwd;
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
- cwd = alloc(MAXPATHL);
- if (cwd != NULL)
+
+ wp = find_tabwin(&argvars[0], &argvars[1]);
+ if (wp != NULL)
{
- if (mch_dirname(cwd, MAXPATHL) != FAIL)
+ if (wp->w_localdir != NULL)
+ rettv->vval.v_string = vim_strsave(wp->w_localdir);
+ else if(globaldir != NULL)
+ rettv->vval.v_string = vim_strsave(globaldir);
+ else
{
- rettv->vval.v_string = vim_strsave(cwd);
-#ifdef BACKSLASH_IN_FILENAME
- if (rettv->vval.v_string != NULL)
- slash_adjust(rettv->vval.v_string);
-#endif
+ cwd = alloc(MAXPATHL);
+ if (cwd != NULL)
+ {
+ if (mch_dirname(cwd, MAXPATHL) != FAIL)
+ rettv->vval.v_string = vim_strsave(cwd);
+ vim_free(cwd);
+ }
}
- vim_free(cwd);
+#ifdef BACKSLASH_IN_FILENAME
+ if (rettv->vval.v_string != NULL)
+ slash_adjust(rettv->vval.v_string);
+#endif
}
}
@@ -12401,6 +12394,38 @@ find_win_by_nr(vp, tp)
}
/*
+ * Find window specified by "wvp" in tabpage "tvp".
+ */
+ static win_T *
+find_tabwin(wvp, tvp)
+ typval_T *wvp; /* VAR_UNKNOWN for current window */
+ typval_T *tvp; /* VAR_UNKNOWN for current tab page */
+{
+ win_T *wp = NULL;
+ tabpage_T *tp = NULL;
+ long n;
+
+ if (wvp->v_type != VAR_UNKNOWN)
+ {
+ if (tvp->v_type != VAR_UNKNOWN)
+ {
+ n = get_tv_number(tvp);
+ if (n >= 0)
+ tp = find_tabpage(n);
+ }
+ else
+ tp = curtab;
+
+ if (tp != NULL)
+ wp = find_win_by_nr(wvp, tp);
+ }
+ else
+ wp = curwin;
+
+ return wp;
+}
+
+/*
* "getwinvar()" function
*/
static void
@@ -13236,10 +13261,13 @@ f_has_key(argvars, rettv)
*/
static void
f_haslocaldir(argvars, rettv)
- typval_T *argvars UNUSED;
+ typval_T *argvars;
typval_T *rettv;
{
- rettv->vval.v_number = (curwin->w_localdir != NULL);
+ win_T *wp = NULL;
+
+ wp = find_tabwin(&argvars[0], &argvars[1]);
+ rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
}
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment