aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames McCoy <jamessan@jamessan.com>2016-10-03 23:36:47 -0400
committerJames McCoy <jamessan@jamessan.com>2016-10-04 14:34:35 -0400
commit1ebb75b1ec5b22d7c7dba1c7fdc9e7969b81ad4d (patch)
tree9b2202052b77173d73efcdd7b34a273f25ff5caa
parentb1edc8abb7e0f11064fde9d4b208fb06f0c16a35 (diff)
downloadrneovim-1ebb75b1ec5b22d7c7dba1c7fdc9e7969b81ad4d.tar.gz
rneovim-1ebb75b1ec5b22d7c7dba1c7fdc9e7969b81ad4d.tar.bz2
rneovim-1ebb75b1ec5b22d7c7dba1c7fdc9e7969b81ad4d.zip
api: Support getting the number of a window/tabpage
In order to provide better compatibility with the classic bindings, the API needs to provide the ability to query the number (really index) of the window/tabpage. This is needed for neovim/python-client#87, as discussed in neovim/neovim#1898. Signed-off-by: James McCoy <jamessan@jamessan.com>
-rw-r--r--src/nvim/api/tabpage.c17
-rw-r--r--src/nvim/api/window.c20
-rw-r--r--src/nvim/window.c51
-rw-r--r--test/functional/api/tabpage_spec.lua16
-rw-r--r--test/functional/api/window_spec.lua24
5 files changed, 103 insertions, 25 deletions
diff --git a/src/nvim/api/tabpage.c b/src/nvim/api/tabpage.c
index 8b1fb041e2..fa00988ae3 100644
--- a/src/nvim/api/tabpage.c
+++ b/src/nvim/api/tabpage.c
@@ -159,6 +159,23 @@ Window nvim_tabpage_get_win(Tabpage tabpage, Error *err)
}
}
+/// Gets the tab page number
+///
+/// @param tabpage The tabpage handle
+/// @param[out] err Details of an error that may have occurred
+/// @return The tabpage number
+Integer nvim_tabpage_get_number(Tabpage tabpage, Error *err)
+{
+ Integer rv = 0;
+ tabpage_T *tab = find_tab_by_handle(tabpage, err);
+
+ if (!tab) {
+ return rv;
+ }
+
+ return tabpage_index(tab);
+}
+
/// Checks if a tab page is valid
///
/// @param tabpage The tab page handle
diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c
index 166e43f698..382f65e7d9 100644
--- a/src/nvim/api/window.c
+++ b/src/nvim/api/window.c
@@ -341,6 +341,26 @@ Tabpage nvim_win_get_tabpage(Window window, Error *err)
return rv;
}
+/// Gets the window number
+///
+/// @param window The window handle
+/// @param[out] err Details of an error that may have occurred
+/// @return The window number
+Integer nvim_win_get_number(Window window, Error *err)
+{
+ Integer rv = 0;
+ win_T *win = find_window_by_handle(window, err);
+
+ if (!win) {
+ return rv;
+ }
+
+ int tabnr;
+ win_get_tabwin(window, &tabnr, (int *)&rv);
+
+ return rv;
+}
+
/// Checks if a window is valid
///
/// @param window The window handle
diff --git a/src/nvim/window.c b/src/nvim/window.c
index e9a66ad907..2cc99b8dfa 100644
--- a/src/nvim/window.c
+++ b/src/nvim/window.c
@@ -5716,45 +5716,46 @@ int win_getid(typval_T *argvars)
int win_gotoid(typval_T *argvars)
{
- win_T *wp;
- tabpage_T *tp;
int id = 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->handle == id) {
- goto_tabpage_win(tp, wp);
- return 1;
- }
+ FOR_ALL_TAB_WINDOWS(tp, wp) {
+ if (wp->handle == id) {
+ goto_tabpage_win(tp, wp);
+ return 1;
}
}
return 0;
}
-void win_id2tabwin(typval_T *argvars, list_T *list)
+void win_get_tabwin(handle_T id, int *tabnr, int *winnr)
{
- win_T *wp;
- tabpage_T *tp;
- int winnr = 1;
- int tabnr = 1;
- int id = get_tv_number(&argvars[0]);
+ *tabnr = 0;
+ *winnr = 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) {
+ int tnum = 1, wnum = 1;
+ FOR_ALL_TABS(tp) {
+ FOR_ALL_WINDOWS_IN_TAB(wp, tp) {
if (wp->handle == id) {
- list_append_number(list, tabnr);
- list_append_number(list, winnr);
+ *winnr = wnum;
+ *tabnr = tnum;
return;
}
- winnr++;
+ wnum++;
}
- tabnr++;
- winnr = 1;
+ tnum++;
+ wnum = 1;
}
- list_append_number(list, 0);
- list_append_number(list, 0);
+}
+
+void win_id2tabwin(typval_T *argvars, list_T *list)
+{
+ int winnr = 1;
+ int tabnr = 1;
+ int id = get_tv_number(&argvars[0]);
+
+ win_get_tabwin(id, &tabnr, &winnr);
+ list_append_number(list, tabnr);
+ list_append_number(list, winnr);
}
int win_id2win(typval_T *argvars)
diff --git a/test/functional/api/tabpage_spec.lua b/test/functional/api/tabpage_spec.lua
index 47dede8b44..90940e9577 100644
--- a/test/functional/api/tabpage_spec.lua
+++ b/test/functional/api/tabpage_spec.lua
@@ -51,6 +51,22 @@ describe('tabpage_* functions', function()
end)
end)
+ describe('get_number', function()
+ it('works', function()
+ local tabs = nvim('list_tabpages')
+ eq(1, tabpage('get_number', tabs[1]))
+
+ nvim('command', 'tabnew')
+ local tab1, tab2 = unpack(nvim('list_tabpages'))
+ eq(1, tabpage('get_number', tab1))
+ eq(2, tabpage('get_number', tab2))
+
+ nvim('command', '-tabmove')
+ eq(2, tabpage('get_number', tab1))
+ eq(1, tabpage('get_number', tab2))
+ end)
+ end)
+
describe('is_valid', function()
it('works', function()
nvim('command', 'tabnew')
diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua
index 2c43e4db2c..bf2bf55fb3 100644
--- a/test/functional/api/window_spec.lua
+++ b/test/functional/api/window_spec.lua
@@ -200,6 +200,30 @@ describe('window_* functions', function()
end)
end)
+ describe('get_number', function()
+ it('works', function()
+ local wins = nvim('list_wins')
+ eq(1, window('get_number', wins[1]))
+
+ nvim('command', 'split')
+ local win1, win2 = unpack(nvim('list_wins'))
+ eq(1, window('get_number', win1))
+ eq(2, window('get_number', win2))
+
+ nvim('command', 'wincmd J')
+ eq(2, window('get_number', win1))
+ eq(1, window('get_number', win2))
+
+ nvim('command', 'tabnew')
+ local win3 = nvim('list_wins')[3]
+ -- First tab page
+ eq(2, window('get_number', win1))
+ eq(1, window('get_number', win2))
+ -- Second tab page
+ eq(1, window('get_number', win3))
+ end)
+ end)
+
describe('is_valid', function()
it('works', function()
nvim('command', 'split')