Hi,
This patch adds an optional second argument to the bufwinnr function
which if present returns all windows associated with the given buffer
across all tabs. This is useful in order to determine if a buffer is
present in several windows. I'm currently using this patch in order to
get backspace to behave accordingly: if the current buffer is present in
other windows than the current one, close the window, otherwise wipe the
current buffer.
nnoremap <BS> :call BufferClose()<CR>
func BufferClose()
if len(bufwinnr('%', 1)) > 1
wincmd c
else
bwipe
endif
endfunc
I didn't end up using the FOR_ALL_TAB_WINDOWS macro since I needed to
reset winnr and increment tabnr in the outer loop.
--
:wq
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index 9ec893f..3bcac94 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -2436,7 +2436,7 @@ bufnr({expr} [, {create}])
*last_buffer_nr()*
Obsolete name for bufnr("$"): last_buffer_nr().
-bufwinnr({expr}) *bufwinnr()*
+bufwinnr({expr} [, {all}]) *bufwinnr()*
The result is a Number, which is the number of the first
window associated with buffer {expr}. For the use of {expr},
see |bufname()| above. If buffer {expr} doesn't exist or
@@ -2446,7 +2446,16 @@ bufwinnr({expr}) *bufwinnr()*
< The number can be used with |CTRL-W_w| and ":wincmd w"
|:wincmd|.
- Only deals with the current tab page.
+ If the {all} argument is present and not zero a list including
+ all windows associated with buffer {expr} across all tabs is
+ returned. Each list item is a dictionary with these entries:
+ tabnr number of the tab the window is associated
+ with
+ winnr number of the window associated with buffer
+ {expr}
+
+ If buffer {expr} doesn't exist or there is no such window, a
+ empty list is returned.
byte2line({byte}) *byte2line()*
Return the line number that contains the character at byte
diff --git a/src/eval.c b/src/eval.c
index 781cd3d..3e330aa 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -8115,7 +8115,7 @@ static struct fst
{"bufloaded", 1, 1, f_bufloaded},
{"bufname", 1, 1, f_bufname},
{"bufnr", 1, 2, f_bufnr},
- {"bufwinnr", 1, 1, f_bufwinnr},
+ {"bufwinnr", 1, 2, f_bufwinnr},
{"byte2line", 1, 1, f_byte2line},
{"byteidx", 2, 2, f_byteidx},
{"byteidxcomp", 2, 2, f_byteidxcomp},
@@ -9666,7 +9666,11 @@ f_bufnr(typval_T *argvars, typval_T *rettv)
f_bufwinnr(typval_T *argvars, typval_T *rettv)
{
#ifdef FEAT_WINDOWS
+ tabpage_T *tp;
win_T *wp;
+ dict_T *dict;
+ int error = FALSE;
+ int tabnr = 0;
int winnr = 0;
#endif
buf_T *buf;
@@ -9675,13 +9679,40 @@ f_bufwinnr(typval_T *argvars, typval_T *rettv)
++emsg_off;
buf = get_buf_tv(&argvars[0], TRUE);
#ifdef FEAT_WINDOWS
- for (wp = firstwin; wp; wp = wp->w_next)
+ if (argvars[1].v_type != VAR_UNKNOWN
+ && get_tv_number_chk(&argvars[1], &error) != 0
+ && !error)
{
- ++winnr;
- if (wp->w_buffer == buf)
- break;
+ if (rettv_list_alloc(rettv) == FAIL)
+ return;
+ for (tp = first_tabpage; tp; tp = tp->tp_next) {
+ ++tabnr;
+ winnr = 0;
+ for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
+ wp;
+ wp = wp->w_next)
+ {
+ ++winnr;
+ if (wp->w_buffer == buf)
+ if ((dict = dict_alloc()) == NULL
+ || list_append_dict(rettv->vval.v_list,
+ dict) == FAIL
+ || dict_add_nr_str(dict, "tabnr", (long)tabnr,
+ NULL) == FAIL
+ || dict_add_nr_str(dict, "winnr", (long)winnr,
+ NULL) == FAIL)
+ return;
+ }
+ }
+ } else {
+ for (wp = firstwin; wp; wp = wp->w_next)
+ {
+ ++winnr;
+ if (wp->w_buffer == buf)
+ break;
+ }
+ rettv->vval.v_number = (wp != NULL ? winnr : -1);
}
- rettv->vval.v_number = (wp != NULL ? winnr : -1);
#else
rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
#endif