# HG changeset patch # User Christian Brabandt # Date 1468961105 -7200 # Node ID 0190d5de215f6beffcfe2c4afb68ec3750212ee7 # Parent 054a7764b8eaee1653b6f80e865ec22c7c9b801d commit https://github.com/vim/vim/commit/c917da4b3e8801a255dbefea8e4ed19c1c716dd8 Author: Bram Moolenaar Date: Tue Jul 19 22:31:36 2016 +0200 patch 7.4.2075 Problem: No autocommand event to initialize a window or tab page. Solution: Add WinNew and TabNew events. (partly by Felipe Morales) diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt --- a/runtime/doc/autocmd.txt +++ b/runtime/doc/autocmd.txt @@ -293,6 +293,8 @@ Name triggered by ~ |CursorMoved| the cursor was moved in Normal mode |CursorMovedI| the cursor was moved in Insert mode +|WinNew| after creating a new window +|TabNew| after creating a new window |WinEnter| after entering another window |WinLeave| before leaving a window |TabEnter| after entering another tab page @@ -309,6 +311,9 @@ Name triggered by ~ |TextChanged| after a change was made to the text in Normal mode |TextChangedI| after a change was made to the text in Insert mode +|TextChanged| after a change was made to the text in Normal mode +|TextChangedI| after a change was made to the text in Insert mode + |ColorScheme| after loading a color scheme |RemoteReply| a reply from a server Vim was received @@ -882,6 +887,10 @@ TabEnter Just after entering a tab pag TabLeave Just before leaving a tab page. |tab-page| A WinLeave event will have been triggered first. + *TabNew* +TabNew When a tab page was created. |tab-page| + A WinEnter event will have been triggered + first, TabEnter follows. *TermChanged* TermChanged After the value of 'term' has changed. Useful for re-loading the syntax file to update the diff --git a/src/fileio.c b/src/fileio.c --- a/src/fileio.c +++ b/src/fileio.c @@ -7706,6 +7706,7 @@ static struct event_name {"StdinReadPre", EVENT_STDINREADPRE}, {"SwapExists", EVENT_SWAPEXISTS}, {"Syntax", EVENT_SYNTAX}, + {"TabNew", EVENT_TABNEW}, {"TabEnter", EVENT_TABENTER}, {"TabLeave", EVENT_TABLEAVE}, {"TermChanged", EVENT_TERMCHANGED}, @@ -7716,6 +7717,7 @@ static struct event_name {"VimEnter", EVENT_VIMENTER}, {"VimLeave", EVENT_VIMLEAVE}, {"VimLeavePre", EVENT_VIMLEAVEPRE}, + {"WinNew", EVENT_WINNEW}, {"WinEnter", EVENT_WINENTER}, {"WinLeave", EVENT_WINLEAVE}, {"VimResized", EVENT_VIMRESIZED}, diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim --- a/src/testdir/test_autocmd.vim +++ b/src/testdir/test_autocmd.vim @@ -78,3 +78,33 @@ function Test_autocmd_bufunload_with_tab tablast quit endfunc + +func Test_win_tab_autocmd() + let g:record = [] + + augroup testing + au WinNew * call add(g:record, 'WinNew') + au WinEnter * call add(g:record, 'WinEnter') + au WinLeave * call add(g:record, 'WinLeave') + au TabNew * call add(g:record, 'TabNew') + au TabEnter * call add(g:record, 'TabEnter') + au TabLeave * call add(g:record, 'TabLeave') + augroup END + + split + tabnew + close + close + + call assert_equal([ + \ 'WinLeave', 'WinNew', 'WinEnter', + \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', + \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', + \ 'WinLeave', 'WinEnter' + \ ], g:record) + + augroup testing + au! + augroup END + unlet g:record +endfunc diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -759,6 +759,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2075, +/**/ 2074, /**/ 2073, diff --git a/src/vim.h b/src/vim.h --- a/src/vim.h +++ b/src/vim.h @@ -1315,6 +1315,7 @@ enum auto_event EVENT_VIMRESIZED, /* after Vim window was resized */ EVENT_WINENTER, /* after entering a window */ EVENT_WINLEAVE, /* before leaving a window */ + EVENT_WINNEW, /* when entering a new window */ EVENT_ENCODINGCHANGED, /* after changing the 'encoding' option */ EVENT_INSERTCHARPRE, /* before inserting a char */ EVENT_CURSORHOLD, /* cursor in same position for a while */ @@ -1327,8 +1328,9 @@ enum auto_event EVENT_SPELLFILEMISSING, /* spell file missing */ EVENT_CURSORMOVED, /* cursor was moved */ EVENT_CURSORMOVEDI, /* cursor was moved in Insert mode */ + EVENT_TABENTER, /* after entering a tab page */ EVENT_TABLEAVE, /* before leaving a tab page */ - EVENT_TABENTER, /* after entering a tab page */ + EVENT_TABNEW, /* when entering a new tab page */ EVENT_SHELLCMDPOST, /* after ":!cmd" */ EVENT_SHELLFILTERPOST, /* after ":1,2!cmd", ":w !cmd", ":r !cmd". */ EVENT_TEXTCHANGED, /* text was modified */ diff --git a/src/window.c b/src/window.c --- a/src/window.c +++ b/src/window.c @@ -45,7 +45,7 @@ static int leave_tabpage(buf_T *new_curb static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds); static void frame_fix_height(win_T *wp); static int frame_minheight(frame_T *topfrp, win_T *next_curwin); -static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_enter_autocmds, int trigger_leave_autocmds); +static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_new_autocmds, int trigger_enter_autocmds, int trigger_leave_autocmds); static void win_free(win_T *wp, tabpage_T *tp); static void frame_append(frame_T *after, frame_T *frp); static void frame_insert(frame_T *before, frame_T *frp); @@ -1259,7 +1259,7 @@ win_split_ins( /* * make the new window the current window */ - win_enter(wp, FALSE); + win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE); if (flags & WSP_VERT) p_wiw = i; else @@ -2416,7 +2416,7 @@ win_close(win_T *win, int free_buf) win_comp_pos(); if (close_curwin) { - win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE); + win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE); #ifdef FEAT_AUTOCMD if (other_buffer) /* careful: after this wp and win may be invalid! */ @@ -3629,7 +3629,9 @@ win_new_tabpage(int after) redraw_all_later(CLEAR); #ifdef FEAT_AUTOCMD + apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf); apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf); + apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf); apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf); #endif return OK; @@ -3808,7 +3810,7 @@ enter_tabpage( /* We would like doing the TabEnter event first, but we don't have a * valid current window yet, which may break some commands. * This triggers autocommands, thus may make "tp" invalid. */ - win_enter_ext(tp->tp_curwin, FALSE, TRUE, + win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE, trigger_enter_autocmds, trigger_leave_autocmds); prevwin = next_prevwin; @@ -4242,7 +4244,7 @@ end: void win_enter(win_T *wp, int undo_sync) { - win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE); + win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE); } /* @@ -4255,6 +4257,7 @@ win_enter_ext( win_T *wp, int undo_sync, int curwin_invalid, + int trigger_new_autocmds UNUSED, int trigger_enter_autocmds UNUSED, int trigger_leave_autocmds UNUSED) { @@ -4340,6 +4343,8 @@ win_enter_ext( } #ifdef FEAT_AUTOCMD + if (trigger_new_autocmds) + apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf); if (trigger_enter_autocmds) { apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);