From d8079e5ab7c9cb029db35f725333ea95d515e337 Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Sat, 27 Aug 2016 17:35:54 +0200 Subject: vim-patch:7.4.1893 Problem: Cannot easily get the window ID for a buffer. Solution: Add bufwinid(). https://github.com/vim/vim/commit/b3619a90eae2702553ff9494ecc4c9b20c13c224 --- src/nvim/eval.c | 29 +++++++++++++++++++---------- src/nvim/eval.lua | 1 + src/nvim/version.c | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 0b1fd6670e..2c09dcd420 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6709,7 +6709,6 @@ static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate) # include "funcs.generated.h" #endif - /* * Function given to ExpandGeneric() to obtain the list of internal * or user defined function names. @@ -7712,26 +7711,36 @@ static void f_bufnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = -1; } -/* - * "bufwinnr(nr)" function - */ -static void f_bufwinnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) +static void buf_win_common(typval_T *argvars, typval_T *rettv, bool get_nr) { - (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ - ++emsg_off; + (void)get_tv_number(&argvars[0]); // issue errmsg if type error + emsg_off++; buf_T *buf = get_buf_tv(&argvars[0], TRUE); int winnr = 0; + int winid; bool found_buf = false; FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { - ++winnr; + winnr++; if (wp->w_buffer == buf) { found_buf = true; + winid = wp->handle; break; } } - rettv->vval.v_number = (found_buf ? winnr : -1); - --emsg_off; + rettv->vval.v_number = (found_buf ? (get_nr ? winnr : winid) : -1); + emsg_off--; +} + +/// "bufwinid(nr)" function +static void f_bufwinid(typval_T *argvars, typval_T *rettv, FunPtr fptr) { + buf_win_common(argvars, rettv, false); +} + +/// "bufwinnr(nr)" function +static void f_bufwinnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + buf_win_common(argvars, rettv, true); } /* diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 3c371fc264..c4a2a9adfc 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -45,6 +45,7 @@ return { bufloaded={args=1}, bufname={args=1}, bufnr={args={1, 2}}, + bufwinid={args=1}, bufwinnr={args=1}, byte2line={args=1}, byteidx={args=2}, diff --git a/src/nvim/version.c b/src/nvim/version.c index bdffa21f46..c46aaf28b5 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -548,7 +548,7 @@ static int included_patches[] = { 1896, // 1895, // 1894 NA - // 1893, + 1893, // 1892 NA // 1891 NA // 1890 NA -- cgit From 50bd8297e67814b3ddbf0f274314fd2d321f63b6 Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Sat, 27 Aug 2016 17:57:05 +0200 Subject: vim-patch:7.4.1895 Problem: Cannot use a window ID where a window number is expected. Solution: Add LOWEST_WIN_ID, so that the window ID can be used where a number is expected. https://github.com/vim/vim/commit/888ccac8902cee186fbd47e971881f6d9b19c068 --- src/nvim/eval.c | 6 +++++- src/nvim/testdir/test_window_id.vim | 15 +++++++++++++++ src/nvim/version.c | 2 +- src/nvim/vim.h | 3 +++ src/nvim/window.c | 2 +- 5 files changed, 25 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 2c09dcd420..f077458c9b 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -10268,7 +10268,11 @@ find_win_by_nr ( } FOR_ALL_WINDOWS_IN_TAB(wp, tp) { - if (--nr <= 0) { + if (nr >= LOWEST_WIN_ID) { + if (wp->handle == nr) { + return wp; + } + } else if (--nr <= 0) { return wp; } } diff --git a/src/nvim/testdir/test_window_id.vim b/src/nvim/testdir/test_window_id.vim index fa3ebd757e..66656e1d0a 100644 --- a/src/nvim/testdir/test_window_id.vim +++ b/src/nvim/testdir/test_window_id.vim @@ -3,17 +3,22 @@ func Test_win_getid() edit one let id1 = win_getid() + let w:one = 'one' split two let id2 = win_getid() let bufnr2 = bufnr('%') + let w:two = 'two' split three let id3 = win_getid() + let w:three = 'three' tabnew edit four let id4 = win_getid() + let w:four = 'four' split five let id5 = win_getid() let bufnr5 = bufnr('%') + let w:five = 'five' tabnext wincmd w @@ -28,6 +33,9 @@ func Test_win_getid() call assert_equal("three", expand("%")) call assert_equal(id3, win_getid()) let nr3 = winnr() + call assert_equal('one', getwinvar(id1, 'one')) + call assert_equal('two', getwinvar(id2, 'two')) + call assert_equal('three', getwinvar(id3, 'three')) tabnext call assert_equal("five", expand("%")) call assert_equal(id5, win_getid()) @@ -36,7 +44,14 @@ func Test_win_getid() call assert_equal("four", expand("%")) call assert_equal(id4, win_getid()) let nr4 = winnr() + call assert_equal('four', getwinvar(id4, 'four')) + call assert_equal('five', getwinvar(id5, 'five')) + call settabwinvar(1, id2, 'two', '2') + call setwinvar(id4, 'four', '4') tabnext + call assert_equal('4', gettabwinvar(2, id4, 'four')) + call assert_equal('five', gettabwinvar(2, id5, 'five')) + call assert_equal('2', getwinvar(id2, 'two')) exe nr1 . "wincmd w" call assert_equal(id1, win_getid()) diff --git a/src/nvim/version.c b/src/nvim/version.c index c46aaf28b5..f56942ac43 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -546,7 +546,7 @@ static int included_patches[] = { 1898, // 1897, 1896, - // 1895, + 1895, // 1894 NA 1893, // 1892 NA diff --git a/src/nvim/vim.h b/src/nvim/vim.h index 09e9e850a7..6bbd9b58ef 100644 --- a/src/nvim/vim.h +++ b/src/nvim/vim.h @@ -316,4 +316,7 @@ enum { #define DIP_OPT 0x10 // also use "opt" directory in 'packpath' #define DIP_NORTP 0x20 // do not use 'runtimepath' +// Lowest number used for window ID. Cannot have this many windows per tab. +#define LOWEST_WIN_ID 1000 + #endif /* NVIM_VIM_H */ diff --git a/src/nvim/window.c b/src/nvim/window.c index e9a66ad907..66f1049b5e 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -3694,7 +3694,7 @@ win_T *buf_jump_open_tab(buf_T *buf) */ static win_T *win_alloc(win_T *after, int hidden) { - static int last_win_id = 0; + static int last_win_id = LOWEST_WIN_ID - 1; // allocate window structure and linesizes arrays win_T *new_wp = xcalloc(1, sizeof(win_T)); -- cgit From 61024fb4a84467d0b09076a537c6c8a9a75ae45a Mon Sep 17 00:00:00 2001 From: Jurica Bradaric Date: Sun, 28 Aug 2016 13:42:18 +0200 Subject: eval: Exit early if argument is invalid. --- src/nvim/eval.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index f077458c9b..a8b6a3ac95 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -7713,10 +7713,20 @@ static void f_bufnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) static void buf_win_common(typval_T *argvars, typval_T *rettv, bool get_nr) { - (void)get_tv_number(&argvars[0]); // issue errmsg if type error + int error = false; + (void)get_tv_number_chk(&argvars[0], &error); // issue errmsg if type error + if (error) { // the argument has an invalid type + rettv->vval.v_number = -1; + return; + } + emsg_off++; + buf_T *buf = get_buf_tv(&argvars[0], true); + if (buf == NULL) { // no need to search if buffer was not found + rettv->vval.v_number = -1; + goto end; + } - buf_T *buf = get_buf_tv(&argvars[0], TRUE); int winnr = 0; int winid; bool found_buf = false; @@ -7729,6 +7739,7 @@ static void buf_win_common(typval_T *argvars, typval_T *rettv, bool get_nr) } } rettv->vval.v_number = (found_buf ? (get_nr ? winnr : winid) : -1); +end: emsg_off--; } -- cgit