Skip to content

Commit c917da4

Browse files
committed
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)
1 parent 0a0f641 commit c917da4

6 files changed

Lines changed: 56 additions & 6 deletions

File tree

runtime/doc/autocmd.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ Name triggered by ~
293293
|CursorMoved| the cursor was moved in Normal mode
294294
|CursorMovedI| the cursor was moved in Insert mode
295295

296+
|WinNew| after creating a new window
297+
|TabNew| after creating a new window
296298
|WinEnter| after entering another window
297299
|WinLeave| before leaving a window
298300
|TabEnter| after entering another tab page
@@ -309,6 +311,9 @@ Name triggered by ~
309311
|TextChanged| after a change was made to the text in Normal mode
310312
|TextChangedI| after a change was made to the text in Insert mode
311313

314+
|TextChanged| after a change was made to the text in Normal mode
315+
|TextChangedI| after a change was made to the text in Insert mode
316+
312317
|ColorScheme| after loading a color scheme
313318

314319
|RemoteReply| a reply from a server Vim was received
@@ -882,6 +887,10 @@ TabEnter Just after entering a tab page. |tab-page|
882887
TabLeave Just before leaving a tab page. |tab-page|
883888
A WinLeave event will have been triggered
884889
first.
890+
*TabNew*
891+
TabNew When a tab page was created. |tab-page|
892+
A WinEnter event will have been triggered
893+
first, TabEnter follows.
885894
*TermChanged*
886895
TermChanged After the value of 'term' has changed. Useful
887896
for re-loading the syntax file to update the

src/fileio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7706,6 +7706,7 @@ static struct event_name
77067706
{"StdinReadPre", EVENT_STDINREADPRE},
77077707
{"SwapExists", EVENT_SWAPEXISTS},
77087708
{"Syntax", EVENT_SYNTAX},
7709+
{"TabNew", EVENT_TABNEW},
77097710
{"TabEnter", EVENT_TABENTER},
77107711
{"TabLeave", EVENT_TABLEAVE},
77117712
{"TermChanged", EVENT_TERMCHANGED},
@@ -7716,6 +7717,7 @@ static struct event_name
77167717
{"VimEnter", EVENT_VIMENTER},
77177718
{"VimLeave", EVENT_VIMLEAVE},
77187719
{"VimLeavePre", EVENT_VIMLEAVEPRE},
7720+
{"WinNew", EVENT_WINNEW},
77197721
{"WinEnter", EVENT_WINENTER},
77207722
{"WinLeave", EVENT_WINLEAVE},
77217723
{"VimResized", EVENT_VIMRESIZED},

src/testdir/test_autocmd.vim

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,33 @@ function Test_autocmd_bufunload_with_tabnext()
7878
tablast
7979
quit
8080
endfunc
81+
82+
func Test_win_tab_autocmd()
83+
let g:record = []
84+
85+
augroup testing
86+
au WinNew * call add(g:record, 'WinNew')
87+
au WinEnter * call add(g:record, 'WinEnter')
88+
au WinLeave * call add(g:record, 'WinLeave')
89+
au TabNew * call add(g:record, 'TabNew')
90+
au TabEnter * call add(g:record, 'TabEnter')
91+
au TabLeave * call add(g:record, 'TabLeave')
92+
augroup END
93+
94+
split
95+
tabnew
96+
close
97+
close
98+
99+
call assert_equal([
100+
\ 'WinLeave', 'WinNew', 'WinEnter',
101+
\ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
102+
\ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
103+
\ 'WinLeave', 'WinEnter'
104+
\ ], g:record)
105+
106+
augroup testing
107+
au!
108+
augroup END
109+
unlet g:record
110+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,8 @@ static char *(features[]) =
758758

759759
static int included_patches[] =
760760
{ /* Add new patch number below this line */
761+
/**/
762+
2075,
761763
/**/
762764
2074,
763765
/**/

src/vim.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ enum auto_event
13151315
EVENT_VIMRESIZED, /* after Vim window was resized */
13161316
EVENT_WINENTER, /* after entering a window */
13171317
EVENT_WINLEAVE, /* before leaving a window */
1318+
EVENT_WINNEW, /* when entering a new window */
13181319
EVENT_ENCODINGCHANGED, /* after changing the 'encoding' option */
13191320
EVENT_INSERTCHARPRE, /* before inserting a char */
13201321
EVENT_CURSORHOLD, /* cursor in same position for a while */
@@ -1327,8 +1328,9 @@ enum auto_event
13271328
EVENT_SPELLFILEMISSING, /* spell file missing */
13281329
EVENT_CURSORMOVED, /* cursor was moved */
13291330
EVENT_CURSORMOVEDI, /* cursor was moved in Insert mode */
1330-
EVENT_TABLEAVE, /* before leaving a tab page */
13311331
EVENT_TABENTER, /* after entering a tab page */
1332+
EVENT_TABLEAVE, /* before leaving a tab page */
1333+
EVENT_TABNEW, /* when entering a new tab page */
13321334
EVENT_SHELLCMDPOST, /* after ":!cmd" */
13331335
EVENT_SHELLFILTERPOST, /* after ":1,2!cmd", ":w !cmd", ":r !cmd". */
13341336
EVENT_TEXTCHANGED, /* text was modified */

src/window.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
4545
static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
4646
static void frame_fix_height(win_T *wp);
4747
static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
48-
static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_enter_autocmds, int trigger_leave_autocmds);
48+
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);
4949
static void win_free(win_T *wp, tabpage_T *tp);
5050
static void frame_append(frame_T *after, frame_T *frp);
5151
static void frame_insert(frame_T *before, frame_T *frp);
@@ -1259,7 +1259,7 @@ win_split_ins(
12591259
/*
12601260
* make the new window the current window
12611261
*/
1262-
win_enter(wp, FALSE);
1262+
win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
12631263
if (flags & WSP_VERT)
12641264
p_wiw = i;
12651265
else
@@ -2416,7 +2416,7 @@ win_close(win_T *win, int free_buf)
24162416
win_comp_pos();
24172417
if (close_curwin)
24182418
{
2419-
win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
2419+
win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
24202420
#ifdef FEAT_AUTOCMD
24212421
if (other_buffer)
24222422
/* careful: after this wp and win may be invalid! */
@@ -3629,7 +3629,9 @@ win_new_tabpage(int after)
36293629

36303630
redraw_all_later(CLEAR);
36313631
#ifdef FEAT_AUTOCMD
3632+
apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
36323633
apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3634+
apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
36333635
apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
36343636
#endif
36353637
return OK;
@@ -3808,7 +3810,7 @@ enter_tabpage(
38083810
/* We would like doing the TabEnter event first, but we don't have a
38093811
* valid current window yet, which may break some commands.
38103812
* This triggers autocommands, thus may make "tp" invalid. */
3811-
win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3813+
win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
38123814
trigger_enter_autocmds, trigger_leave_autocmds);
38133815
prevwin = next_prevwin;
38143816

@@ -4242,7 +4244,7 @@ win_goto_hor(
42424244
void
42434245
win_enter(win_T *wp, int undo_sync)
42444246
{
4245-
win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
4247+
win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
42464248
}
42474249

42484250
/*
@@ -4255,6 +4257,7 @@ win_enter_ext(
42554257
win_T *wp,
42564258
int undo_sync,
42574259
int curwin_invalid,
4260+
int trigger_new_autocmds UNUSED,
42584261
int trigger_enter_autocmds UNUSED,
42594262
int trigger_leave_autocmds UNUSED)
42604263
{
@@ -4340,6 +4343,8 @@ win_enter_ext(
43404343
}
43414344

43424345
#ifdef FEAT_AUTOCMD
4346+
if (trigger_new_autocmds)
4347+
apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
43434348
if (trigger_enter_autocmds)
43444349
{
43454350
apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);

0 commit comments

Comments
 (0)