Add TabClosed event

TabClosed is triggered when a tab page closes.
This commit is contained in:
Felipe Morales
2015-01-05 01:03:22 -03:00
committed by Justin M. Keyes
parent 66d94869a0
commit 3ffc5d81c3
7 changed files with 51 additions and 2 deletions

View File

@@ -298,6 +298,7 @@ Name triggered by ~
|TabLeave| before leaving a tab page |TabLeave| before leaving a tab page
|TabNew| when creating a new tab page |TabNew| when creating a new tab page
|TabNewEntered| after entering a new tab page |TabNewEntered| after entering a new tab page
|TabClosed| after closing a tab page
|CmdwinEnter| after entering the command-line window |CmdwinEnter| after entering the command-line window
|CmdwinLeave| before leaving the command-line window |CmdwinLeave| before leaving the command-line window
@@ -867,6 +868,9 @@ TabNew When creating a new tab page. |tab-page|
*TabNewEntered* *TabNewEntered*
TabNewEntered After entering a new tab page. |tab-page| TabNewEntered After entering a new tab page. |tab-page|
After BufEnter. After BufEnter.
*TabClosed*
TabClosed After closing a tab page. <afile> can be used
for the tab page number.
*TermChanged* *TermChanged*
TermChanged After the value of 'term' has changed. Useful TermChanged After the value of 'term' has changed. Useful
for re-loading the syntax file to update the for re-loading the syntax file to update the

View File

@@ -76,7 +76,7 @@ syn keyword vimErrSetting contained hardtabs ht w1200 w300 w9600
" AutoCmd Events {{{2 " AutoCmd Events {{{2
syn case ignore syn case ignore
syn keyword vimAutoEvent contained BufAdd BufCreate BufDelete BufEnter BufFilePost BufFilePre BufHidden BufLeave BufNew BufNewFile BufRead BufReadCmd BufReadPost BufReadPre BufUnload BufWinEnter BufWinLeave BufWipeout BufWrite BufWriteCmd BufWritePost BufWritePre Cmd-event CmdwinEnter CmdwinLeave ColorScheme CompleteDone CursorHold CursorHoldI CursorMoved CursorMovedI EncodingChanged FileAppendCmd FileAppendPost FileAppendPre FileChangedRO FileChangedShell FileChangedShellPost FileEncoding FileReadCmd FileReadPost FileReadPre FileType FileWriteCmd FileWritePost FileWritePre FilterReadPost FilterReadPre FilterWritePost FilterWritePre FocusGained FocusLost FuncUndefined GUIEnter GUIFailed InsertChange InsertCharPre InsertEnter InsertLeave JobActivity MenuPopup QuickFixCmdPost QuickFixCmdPre QuitPre RemoteReply SessionLoadPost ShellCmdPost ShellFilterPost SourceCmd SourcePre SpellFileMissing StdinReadPost StdinReadPre SwapExists Syntax TabEnter TabLeave TabNew TabNewEntered TermChanged TermResponse TextChanged TextChangedI User UserGettingBored VimEnter VimLeave VimLeavePre VimResized WinEnter WinLeave syn keyword vimAutoEvent contained BufAdd BufCreate BufDelete BufEnter BufFilePost BufFilePre BufHidden BufLeave BufNew BufNewFile BufRead BufReadCmd BufReadPost BufReadPre BufUnload BufWinEnter BufWinLeave BufWipeout BufWrite BufWriteCmd BufWritePost BufWritePre Cmd-event CmdwinEnter CmdwinLeave ColorScheme CompleteDone CursorHold CursorHoldI CursorMoved CursorMovedI EncodingChanged FileAppendCmd FileAppendPost FileAppendPre FileChangedRO FileChangedShell FileChangedShellPost FileEncoding FileReadCmd FileReadPost FileReadPre FileType FileWriteCmd FileWritePost FileWritePre FilterReadPost FilterReadPre FilterWritePost FilterWritePre FocusGained FocusLost FuncUndefined GUIEnter GUIFailed InsertChange InsertCharPre InsertEnter InsertLeave JobActivity MenuPopup QuickFixCmdPost QuickFixCmdPre QuitPre RemoteReply SessionLoadPost ShellCmdPost ShellFilterPost SourceCmd SourcePre SpellFileMissing StdinReadPost StdinReadPre SwapExists Syntax TabEnter TabLeave TabNew TabNewEntered TabClosed TermChanged TermResponse TextChanged TextChangedI User UserGettingBored VimEnter VimLeave VimLeavePre VimResized WinEnter WinLeave
" Highlight commonly used Groupnames {{{2 " Highlight commonly used Groupnames {{{2
syn keyword vimGroup contained Comment Constant String Character Number Boolean Float Identifier Function Statement Conditional Repeat Label Operator Keyword Exception PreProc Include Define Macro PreCondit Type StorageClass Structure Typedef Special SpecialChar Tag Delimiter SpecialComment Debug Underlined Ignore Error Todo syn keyword vimGroup contained Comment Constant String Character Number Boolean Float Identifier Function Statement Conditional Repeat Label Operator Keyword Exception PreProc Include Define Macro PreCondit Type StorageClass Structure Typedef Special SpecialChar Tag Delimiter SpecialComment Debug Underlined Ignore Error Todo

View File

@@ -5328,10 +5328,12 @@ void tabpage_close_other(tabpage_T *tp, int forceit)
int done = 0; int done = 0;
win_T *wp; win_T *wp;
int h = tabline_height(); int h = tabline_height();
char_u prev_idx[NUMBUFLEN];
/* Limit to 1000 windows, autocommands may add a window while we close /* Limit to 1000 windows, autocommands may add a window while we close
* one. OK, so I'm paranoid... */ * one. OK, so I'm paranoid... */
while (++done < 1000) { while (++done < 1000) {
sprintf((char *)prev_idx, "%i", tabpage_index(tp));
wp = tp->tp_firstwin; wp = tp->tp_firstwin;
ex_win_close(forceit, wp, tp); ex_win_close(forceit, wp, tp);
@@ -5340,6 +5342,7 @@ void tabpage_close_other(tabpage_T *tp, int forceit)
if (!valid_tabpage(tp) || tp->tp_firstwin == wp) if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
break; break;
} }
apply_autocmds(EVENT_TABCLOSED, prev_idx, prev_idx, FALSE, curbuf);
redraw_tabline = TRUE; redraw_tabline = TRUE;
if (h != tabline_height()) if (h != tabline_height())

View File

@@ -5245,6 +5245,7 @@ static struct event_name {
{"StdinReadPre", EVENT_STDINREADPRE}, {"StdinReadPre", EVENT_STDINREADPRE},
{"SwapExists", EVENT_SWAPEXISTS}, {"SwapExists", EVENT_SWAPEXISTS},
{"Syntax", EVENT_SYNTAX}, {"Syntax", EVENT_SYNTAX},
{"TabClosed", EVENT_TABCLOSED},
{"TabEnter", EVENT_TABENTER}, {"TabEnter", EVENT_TABENTER},
{"TabLeave", EVENT_TABLEAVE}, {"TabLeave", EVENT_TABLEAVE},
{"TabNew", EVENT_TABNEW}, {"TabNew", EVENT_TABNEW},
@@ -6634,7 +6635,8 @@ apply_autocmds_group (
|| event == EVENT_QUICKFIXCMDPRE || event == EVENT_QUICKFIXCMDPRE
|| event == EVENT_COLORSCHEME || event == EVENT_COLORSCHEME
|| event == EVENT_QUICKFIXCMDPOST || event == EVENT_QUICKFIXCMDPOST
|| event == EVENT_JOBACTIVITY) || event == EVENT_JOBACTIVITY
|| event == EVENT_TABCLOSED)
fname = vim_strsave(fname); fname = vim_strsave(fname);
else else
fname = FullName_save(fname, FALSE); fname = FullName_save(fname, FALSE);

View File

@@ -93,6 +93,7 @@ typedef enum auto_event {
EVENT_SPELLFILEMISSING, /* spell file missing */ EVENT_SPELLFILEMISSING, /* spell file missing */
EVENT_CURSORMOVED, /* cursor was moved */ EVENT_CURSORMOVED, /* cursor was moved */
EVENT_CURSORMOVEDI, /* cursor was moved in Insert mode */ EVENT_CURSORMOVEDI, /* cursor was moved in Insert mode */
EVENT_TABCLOSED, /* a tab has closed */
EVENT_TABLEAVE, /* before leaving a tab page */ EVENT_TABLEAVE, /* before leaving a tab page */
EVENT_TABENTER, /* after entering a tab page */ EVENT_TABENTER, /* after entering a tab page */
EVENT_TABNEW, /* when creating a new tab */ EVENT_TABNEW, /* when creating a new tab */

View File

@@ -1791,6 +1791,9 @@ static int close_last_window_tabpage(win_T *win, int free_buf, tabpage_T *prev_c
} }
/* Since goto_tabpage_tp above did not trigger *Enter autocommands, do /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
* that now. */ * that now. */
char_u prev_idx[NUMBUFLEN];
sprintf((char *)prev_idx, "%i", tabpage_index(prev_curtab));
apply_autocmds(EVENT_TABCLOSED, prev_idx, prev_idx, FALSE, curbuf);
apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf); apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf); apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
if (old_curbuf != curbuf) if (old_curbuf != curbuf)

View File

@@ -0,0 +1,36 @@
local helpers = require('test.functional.helpers')
local clear, nvim, buffer, curbuf, curwin, eq, neq, ok =
helpers.clear, helpers.nvim, helpers.buffer, helpers.curbuf, helpers.curwin,
helpers.eq, helpers.neq, helpers.ok
describe('TabClosed', function()
describe('au TabClosed', function()
describe('with * as <afile>', function()
it('matches when closing any tab', function()
clear()
nvim('command', 'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()')
repeat
nvim('command', 'tabnew')
until nvim('eval', 'tabpagenr()') == 6 -- current tab is now 6
eq("\ntabclosed:6:6:5", nvim('command_output', 'tabclose')) -- close last 6, current tab is now 5
eq("\ntabclosed:5:5:4", nvim('command_output', 'close')) -- close last window on tab, closes tab
eq("\ntabclosed:2:2:3", nvim('command_output', '2tabclose')) -- close tab 2, current tab is now 3
eq("\ntabclosed:1:1:2\ntabclosed:1:1:1", nvim('command_output', 'tabonly')) -- close tabs 1 and 2
end)
end)
describe('with NR as <afile>', function()
it('matches when closing a tab whose index is NR', function()
tmp_path = nvim('eval', 'tempname()')
nvim('command', 'au! TabClosed 2 echom "tabclosed:match"')
repeat
nvim('command', 'tabnew')
until nvim('eval', 'tabpagenr()') == 5 -- current tab is now 5
-- sanity check, we shouldn't match on tabs with numbers other than 2
eq("\ntabclosed:5:5:4", nvim('command_output', 'tabclose'))
-- close tab page 2, current tab is now 3
eq("\ntabclosed:2:2:3\ntabclosed:match", nvim('command_output', '2tabclose'))
end)
end)
end)
end)