feat(tabline): middle mouse button now closes tab (#27522)

This commit is contained in:
Nacho Nieva 2024-02-18 23:49:33 -03:00 committed by GitHub
parent 8f1f2a1d9f
commit fa131de9ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 30 deletions

View File

@ -316,6 +316,8 @@ The following new APIs and features were added.
• Added |nvim_tabpage_set_win()| to set the current window of a tabpage.
• Clicking on a tabpage in the tabline with the middle mouse button closes it.
==============================================================================
CHANGED FEATURES *news-changed*

View File

@ -136,6 +136,26 @@ static void move_tab_to_mouse(void)
tabpage_move(tabnr);
}
}
/// Close the current or specified tab page.
///
/// @param c1 tabpage number, or 999 for the current tabpage
static void mouse_tab_close(int c1)
{
tabpage_T *tp;
if (c1 == 999) {
tp = curtab;
} else {
tp = find_tabpage(c1);
}
if (tp == curtab) {
if (first_tabpage->tp_next != NULL) {
tabpage_close(false);
}
} else if (tp != NULL) {
tabpage_close_other(tp, false);
}
}
static bool got_click = false; // got a click some time back
@ -487,28 +507,12 @@ bool do_mouse(oparg_T *oap, int c, int dir, int count, bool fixindent)
if (is_click && cmdwin_type == 0 && mouse_col < Columns) {
in_tab_line = true;
c1 = tab_page_click_defs[mouse_col].tabnr;
switch (tab_page_click_defs[mouse_col].type) {
case kStlClickDisabled:
break;
case kStlClickTabClose: {
tabpage_T *tp;
// Close the current or specified tab page.
if (c1 == 999) {
tp = curtab;
} else {
tp = find_tabpage(c1);
}
if (tp == curtab) {
if (first_tabpage->tp_next != NULL) {
tabpage_close(false);
}
} else if (tp != NULL) {
tabpage_close_other(tp, false);
}
break;
}
case kStlClickTabSwitch:
if (which_button != MOUSE_MIDDLE) {
if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) {
// double click opens new page
end_visual_mode();
@ -525,6 +529,11 @@ bool do_mouse(oparg_T *oap, int c, int dir, int count, bool fixindent)
}
}
break;
}
FALLTHROUGH;
case kStlClickTabClose:
mouse_tab_close(c1);
break;
case kStlClickFuncRun:
call_click_def_func(tab_page_click_defs, mouse_col, which_button);
break;

View File

@ -184,4 +184,18 @@ describe('tabline', function()
]],
}
end)
it('middle-click closes tab', function()
command('tabnew')
command('tabnew')
command('tabnew')
command('tabprev')
eq({ 3, 4 }, api.nvim_eval('[tabpagenr(), tabpagenr("$")]'))
api.nvim_input_mouse('middle', 'press', '', 0, 0, 1)
eq({ 2, 3 }, api.nvim_eval('[tabpagenr(), tabpagenr("$")]'))
api.nvim_input_mouse('middle', 'press', '', 0, 0, 20)
eq({ 2, 2 }, api.nvim_eval('[tabpagenr(), tabpagenr("$")]'))
api.nvim_input_mouse('middle', 'press', '', 0, 0, 1)
eq({ 1, 1 }, api.nvim_eval('[tabpagenr(), tabpagenr("$")]'))
end)
end)