changeset 8536:09041d2fd7d0 v7.4.1558

commit https://github.com/vim/vim/commit/9cdf86b86f5fdb5a45b682f336846f9d9a9c6f1f Author: Bram Moolenaar <Bram@vim.org> Date: Sun Mar 13 19:04:51 2016 +0100 patch 7.4.1558 Problem: It is not easy to find out what windows display a buffer. Solution: Add win_findbuf().
author Christian Brabandt <cb@256bit.org>
date Sun, 13 Mar 2016 19:15:05 +0100
parents 6ec2cb295acd
children cc20abebafa3
files runtime/doc/eval.txt src/eval.c src/proto/window.pro src/testdir/test_window_id.vim src/version.c src/window.c
diffstat 6 files changed, 44 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -2149,6 +2149,7 @@ values( {dict})			List	values in {dict}
 virtcol( {expr})		Number	screen column of cursor or mark
 visualmode( [expr])		String	last visual mode used
 wildmenumode()			Number	whether 'wildmenu' mode is active
+win_findbuf( {bufnr}) 		List	find windows containing {bufnr}
 win_getid( [{win} [, {tab}]]) 	Number	get window ID for {win} in {tab}
 win_gotoid( {expr})		Number	go to window with ID {expr}
 win_id2tabwin( {expr})		List	get tab and window nr from window ID
@@ -7176,6 +7177,10 @@ wildmenumode()					*wildmenumode()*
 		(Note, this needs the 'wildcharm' option set appropriately).
 
 
+win_findbuf({bufnr})					*win_findbuf()*
+		Returns a list with window IDs for windows that contain buffer
+		{bufnr}.  When there is none the list is empty.
+
 win_getid([{win} [, {tab}]])				*win_getid()*
 		Get the window ID for the specified window.
 		When {win} is missing use the current window.
--- a/src/eval.c
+++ b/src/eval.c
@@ -807,6 +807,7 @@ static void f_values(typval_T *argvars, 
 static void f_virtcol(typval_T *argvars, typval_T *rettv);
 static void f_visualmode(typval_T *argvars, typval_T *rettv);
 static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
+static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
 static void f_win_getid(typval_T *argvars, typval_T *rettv);
 static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
 static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
@@ -8388,6 +8389,7 @@ static struct fst
     {"virtcol",		1, 1, f_virtcol},
     {"visualmode",	0, 1, f_visualmode},
     {"wildmenumode",	0, 0, f_wildmenumode},
+    {"win_findbuf",	1, 1, f_win_findbuf},
     {"win_getid",	0, 2, f_win_getid},
     {"win_gotoid",	1, 1, f_win_gotoid},
     {"win_id2tabwin",	1, 1, f_win_id2tabwin},
@@ -12669,6 +12671,16 @@ f_getwinposx(typval_T *argvars UNUSED, t
 }
 
 /*
+ * "win_findbuf()" function
+ */
+    static void
+f_win_findbuf(typval_T *argvars, typval_T *rettv)
+{
+    if (rettv_list_alloc(rettv) != FAIL)
+	win_findbuf(argvars, rettv->vval.v_list);
+}
+
+/*
  * "win_getid()" function
  */
     static void
--- a/src/proto/window.pro
+++ b/src/proto/window.pro
@@ -87,4 +87,5 @@ int win_getid(typval_T *argvars);
 int win_gotoid(typval_T *argvars);
 void win_id2tabwin(typval_T *argvars, list_T *list);
 int win_id2win(typval_T *argvars);
+void win_findbuf(typval_T *argvars, list_T *list);
 /* vim: set ft=c : */
--- a/src/testdir/test_window_id.vim
+++ b/src/testdir/test_window_id.vim
@@ -5,6 +5,7 @@ func Test_win_getid()
   let id1 = win_getid()
   split two
   let id2 = win_getid()
+  let bufnr2 = bufnr('%')
   split three
   let id3 = win_getid()
   tabnew
@@ -12,6 +13,7 @@ func Test_win_getid()
   let id4 = win_getid()
   split five
   let id5 = win_getid()
+  let bufnr5 = bufnr('%')
   tabnext
 
   wincmd w
@@ -67,5 +69,11 @@ func Test_win_getid()
   call assert_equal([1, nr2], win_id2tabwin(id2))
   call assert_equal([2, nr4], win_id2tabwin(id4))
 
+  call assert_equal([], win_findbuf(9999))
+  call assert_equal([id2], win_findbuf(bufnr2))
+  call win_gotoid(id5)
+  split
+  call assert_equal(sort([id5, win_getid()]), sort(win_findbuf(bufnr5)))
+
   only!
 endfunc
--- a/src/version.c
+++ b/src/version.c
@@ -744,6 +744,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1558,
+/**/
     1557,
 /**/
     1556,
--- a/src/window.c
+++ b/src/window.c
@@ -7297,4 +7297,19 @@ win_id2win(typval_T *argvars)
     }
     return 0;
 }
-#endif
+
+    void
+win_findbuf(typval_T *argvars, list_T *list)
+{
+    win_T	*wp;
+    tabpage_T   *tp;
+    int		bufnr = get_tv_number(&argvars[0]);
+
+    for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
+	for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
+						  wp != NULL; wp = wp->w_next)
+	    if (wp->w_buffer->b_fnum == bufnr)
+		list_append_number(list, wp->w_id);
+}
+
+#endif