Merge pull request #5424 from jamessan/win-tab-nr

api: Support getting the number of a window/tabpage
This commit is contained in:
James McCoy 2016-10-15 15:29:53 -04:00 committed by GitHub
commit b268ba353a
5 changed files with 104 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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_get_tabwin(handle_T id, int *tabnr, int *winnr)
{
*tabnr = 0;
*winnr = 0;
int tnum = 1, wnum = 1;
FOR_ALL_TABS(tp) {
FOR_ALL_WINDOWS_IN_TAB(wp, tp) {
if (wp->handle == id) {
*winnr = wnum;
*tabnr = tnum;
return;
}
wnum++;
}
tnum++;
wnum = 1;
}
}
void win_id2tabwin(typval_T *argvars, list_T *list)
{
win_T *wp;
tabpage_T *tp;
int winnr = 1;
int tabnr = 1;
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) {
list_append_number(list, tabnr);
list_append_number(list, winnr);
return;
}
winnr++;
}
tabnr++;
winnr = 1;
}
list_append_number(list, 0);
list_append_number(list, 0);
win_get_tabwin(id, &tabnr, &winnr);
list_append_number(list, tabnr);
list_append_number(list, winnr);
}
int win_id2win(typval_T *argvars)

View File

@ -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')

View File

@ -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')