patch 7.4.1893
Problem: Cannot easily get the window ID for a buffer.
Solution: Add bufwinid().
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index cb5fa43..b3262b2 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -1862,6 +1862,7 @@
bufloaded({expr}) Number TRUE if buffer {expr} is loaded
bufname({expr}) String Name of the buffer {expr}
bufnr({expr} [, {create}]) Number Number of the buffer {expr}
+bufwinid({expr}) Number window ID of buffer {expr}
bufwinnr({expr}) Number window number of buffer {expr}
byte2line({byte}) Number line number at byte count {byte}
byteidx({expr}, {nr}) Number byte index of {nr}'th char in {expr}
@@ -2557,6 +2558,16 @@
*last_buffer_nr()*
Obsolete name for bufnr("$"): last_buffer_nr().
+bufwinid({expr}) *bufwinid()*
+ The result is a Number, which is the window ID of the first
+ window associated with buffer {expr}. For the use of {expr},
+ see |bufname()| above. If buffer {expr} doesn't exist or
+ there is no such window, -1 is returned. Example: >
+
+ echo "A window containing buffer 1 is " . (bufwinid(1))
+<
+ Only deals with the current tab page.
+
bufwinnr({expr}) *bufwinnr()*
The result is a Number, which is the number of the first
window associated with buffer {expr}. For the use of {expr},
diff --git a/src/eval.c b/src/eval.c
index 2ca6611..66cf3a0 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -500,6 +500,7 @@
static void f_bufloaded(typval_T *argvars, typval_T *rettv);
static void f_bufname(typval_T *argvars, typval_T *rettv);
static void f_bufnr(typval_T *argvars, typval_T *rettv);
+static void f_bufwinid(typval_T *argvars, typval_T *rettv);
static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
static void f_byte2line(typval_T *argvars, typval_T *rettv);
static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
@@ -8488,6 +8489,7 @@
{"bufloaded", 1, 1, f_bufloaded},
{"bufname", 1, 1, f_bufname},
{"bufnr", 1, 2, f_bufnr},
+ {"bufwinid", 1, 1, f_bufwinid},
{"bufwinnr", 1, 1, f_bufwinnr},
{"byte2line", 1, 1, f_byte2line},
{"byteidx", 2, 2, f_byteidx},
@@ -10213,11 +10215,8 @@
rettv->vval.v_number = -1;
}
-/*
- * "bufwinnr(nr)" function
- */
static void
-f_bufwinnr(typval_T *argvars, typval_T *rettv)
+buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
{
#ifdef FEAT_WINDOWS
win_T *wp;
@@ -10235,14 +10234,33 @@
if (wp->w_buffer == buf)
break;
}
- rettv->vval.v_number = (wp != NULL ? winnr : -1);
+ rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
#else
- rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
+ rettv->vval.v_number = (curwin->w_buffer == buf
+ ? (get_nr ? 1 : curwin->w_id) : -1);
#endif
--emsg_off;
}
/*
+ * "bufwinid(nr)" function
+ */
+ static void
+f_bufwinid(typval_T *argvars, typval_T *rettv)
+{
+ buf_win_common(argvars, rettv, FALSE);
+}
+
+/*
+ * "bufwinnr(nr)" function
+ */
+ static void
+f_bufwinnr(typval_T *argvars, typval_T *rettv)
+{
+ buf_win_common(argvars, rettv, TRUE);
+}
+
+/*
* "byte2line(byte)" function
*/
static void
diff --git a/src/version.c b/src/version.c
index 5aa8a2a..36b5ed7 100644
--- a/src/version.c
+++ b/src/version.c
@@ -754,6 +754,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1893,
+/**/
1892,
/**/
1891,