Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1f47db7

Browse files
noscriptchrisbra
authored andcommitted
patch 9.1.0059: No event triggered before creating a window
Problem: No event is triggered before creating a window. (Sergey Vlasov) Solution: Add the WinNewPre event (Sergey Vlasov) fixes: #10635 closes: #12761 Signed-off-by: Sergey Vlasov <sergey@vlasov.me> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 92e90a1 commit 1f47db7

File tree

9 files changed

+119
-7
lines changed

9 files changed

+119
-7
lines changed

runtime/doc/autocmd.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*autocmd.txt* For Vim version 9.1. Last change: 2024 Jan 23
1+
*autocmd.txt* For Vim version 9.1. Last change: 2024 Jan 25
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -381,6 +381,7 @@ Name triggered by ~
381381
|CursorMoved| the cursor was moved in Normal mode
382382
|CursorMovedI| the cursor was moved in Insert mode
383383

384+
|WinNewPre| before creating a new window
384385
|WinNew| after creating a new window
385386
|TabNew| after creating a new tab page
386387
|WinClosed| after closing a window
@@ -1390,6 +1391,18 @@ WinLeave Before leaving a window. If the window to be
13901391
WinLeave autocommands (but not for ":new").
13911392
Not used for ":qa" or ":q" when exiting Vim.
13921393

1394+
*WinNewPre*
1395+
WinNewPre Before creating a new window. Triggered
1396+
before commands that modify window layout by
1397+
creating a split or new tab page. Not done for
1398+
the first window, when Vim has just started.
1399+
It is not allowed to modify window layout
1400+
while executing commands for the WinNewPre
1401+
event.
1402+
Most useful to store current window layout
1403+
and compare it with the new layout after the
1404+
Window has been created.
1405+
13931406
*WinNew*
13941407
WinNew When a new window was created. Not done for
13951408
the first window, when Vim has just started.

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5785,6 +5785,7 @@ WinClosed autocmd.txt /*WinClosed*
57855785
WinEnter autocmd.txt /*WinEnter*
57865786
WinLeave autocmd.txt /*WinLeave*
57875787
WinNew autocmd.txt /*WinNew*
5788+
WinNewPre autocmd.txt /*WinNewPre*
57885789
WinResized autocmd.txt /*WinResized*
57895790
WinResized-event windows.txt /*WinResized-event*
57905791
WinScrolled autocmd.txt /*WinScrolled*

runtime/doc/todo.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4667,7 +4667,6 @@ GUI:
46674667

46684668

46694669
Autocommands:
4670-
9 Add WinNewPre - before creating a new window. #10635
46714670
9 When triggering WinNew provide the window ID somehow. #10633
46724671
9 Rework the code from FEAT_OSFILETYPE for autocmd-osfiletypes to use
46734672
'filetype'. Only for when the current buffer is known.

runtime/syntax/vim.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
" 2024 Jan 14 by Vim Project (TermResponseAll autocommand)
1212
" 2024 Jan 15 by Vim Project (:hi ctermfont attribute)
1313
" 2024 Jan 23 by Vim Project (add :[23]match commands)
14+
" 2024 Jan 25 by Vim Project (WinNewPre autocommand)
1415
" Version: 9.0-25
1516
" URL: http://www.drchip.org/astronaut/vim/index.html#SYNTAX_VIM
1617
" Automatically generated keyword lists: {{{1
@@ -76,7 +77,7 @@ syn keyword vimErrSetting contained bioskey biosk conskey consk autoprint beauti
7677

7778
" AutoCmd Events {{{2
7879
syn case ignore
79-
syn keyword vimAutoEvent contained BufAdd BufDelete BufFilePost BufHidden BufNew BufRead BufReadPost BufUnload BufWinLeave BufWrite BufWritePost CmdlineChanged CmdlineLeave CmdwinEnter ColorScheme CompleteChanged CompleteDonePre CursorHoldI CursorMovedI DiffUpdated DirChanged DirChangedPre EncodingChanged ExitPre FileAppendCmd FileAppendPost FileAppendPre FileChangedRO FileChangedShell FileChangedShellPost FileEncoding FileExplorer FileReadCmd FileReadPost FileReadPre FileType FileWriteCmd FileWritePost FileWritePre FilterReadPost FilterReadPre FilterWritePost FilterWritePre FocusGained FocusLost FuncUndefined GUIEnter GUIFailed InsertChange InsertCharPre InsertEnter InsertLeave InsertLeavePre MenuPopup ModeChanged OptionSet QuickFixCmdPost QuickFixCmdPre QuitPre RemoteReply SafeState SafeStateAgain SessionLoadPost ShellCmdPost ShellFilterPost SigUSR1 SourceCmd SourcePost SourcePre SpellFileMissing StdinReadPost StdinReadPre SwapExists Syntax TabClosed TabEnter TabLeave TabNew TermChanged TerminalOpen TerminalWinOpen TermResponse TermResponseAll TextChanged TextChangedI TextChangedP TextChangedT TextYankPost User VimEnter VimLeave VimLeavePre VimResized VimResume VimSuspend WinClosed WinEnter WinLeave WinNew WinResized WinScrolled
80+
syn keyword vimAutoEvent contained BufAdd BufDelete BufFilePost BufHidden BufNew BufRead BufReadPost BufUnload BufWinLeave BufWrite BufWritePost CmdlineChanged CmdlineLeave CmdwinEnter ColorScheme CompleteChanged CompleteDonePre CursorHoldI CursorMovedI DiffUpdated DirChanged DirChangedPre EncodingChanged ExitPre FileAppendCmd FileAppendPost FileAppendPre FileChangedRO FileChangedShell FileChangedShellPost FileEncoding FileExplorer FileReadCmd FileReadPost FileReadPre FileType FileWriteCmd FileWritePost FileWritePre FilterReadPost FilterReadPre FilterWritePost FilterWritePre FocusGained FocusLost FuncUndefined GUIEnter GUIFailed InsertChange InsertCharPre InsertEnter InsertLeave InsertLeavePre MenuPopup ModeChanged OptionSet QuickFixCmdPost QuickFixCmdPre QuitPre RemoteReply SafeState SafeStateAgain SessionLoadPost ShellCmdPost ShellFilterPost SigUSR1 SourceCmd SourcePost SourcePre SpellFileMissing StdinReadPost StdinReadPre SwapExists Syntax TabClosed TabEnter TabLeave TabNew TermChanged TerminalOpen TerminalWinOpen TermResponse TermResponseAll TextChanged TextChangedI TextChangedP TextChangedT TextYankPost User VimEnter VimLeave VimLeavePre VimResized VimResume VimSuspend WinClosed WinEnter WinLeave WinNewPre WinNew WinResized WinScrolled
8081
syn keyword vimAutoEvent contained BufCreate BufEnter BufFilePre BufLeave BufNewFile BufReadCmd BufReadPre BufWinEnter BufWipeout BufWriteCmd BufWritePre CmdlineEnter CmdUndefined CmdwinLeave ColorSchemePre CompleteDone CursorHold CursorMoved
8182

8283
" Highlight commonly used Groupnames {{{2

src/autocmd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static struct event_name
188188
{"VimEnter", EVENT_VIMENTER},
189189
{"VimLeave", EVENT_VIMLEAVE},
190190
{"VimLeavePre", EVENT_VIMLEAVEPRE},
191+
{"WinNewPre", EVENT_WINNEWPRE},
191192
{"WinNew", EVENT_WINNEW},
192193
{"WinClosed", EVENT_WINCLOSED},
193194
{"WinEnter", EVENT_WINENTER},

src/testdir/test_autocmd.vim

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ func Test_win_tab_autocmd()
270270
let g:record = []
271271

272272
augroup testing
273+
au WinNewPre * call add(g:record, 'WinNewPre')
273274
au WinNew * call add(g:record, 'WinNew')
274275
au WinClosed * call add(g:record, 'WinClosed')
275276
au WinEnter * call add(g:record, 'WinEnter')
@@ -286,8 +287,8 @@ func Test_win_tab_autocmd()
286287
close
287288

288289
call assert_equal([
289-
\ 'WinLeave', 'WinNew', 'WinEnter',
290-
\ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
290+
\ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
291+
\ 'WinLeave', 'TabLeave', 'WinNewPre', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
291292
\ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
292293
\ 'WinLeave', 'WinClosed', 'WinEnter'
293294
\ ], g:record)
@@ -298,17 +299,96 @@ func Test_win_tab_autocmd()
298299
bwipe somefile
299300

300301
call assert_equal([
301-
\ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
302+
\ 'WinLeave', 'TabLeave', 'WinNewPre', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
302303
\ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
303304
\ 'WinClosed', 'TabClosed'
304305
\ ], g:record)
305306

307+
let g:record = []
308+
copen
309+
help
310+
tabnext
311+
vnew
312+
313+
call assert_equal([
314+
\ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
315+
\ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
316+
\ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter'
317+
\ ], g:record)
318+
306319
augroup testing
307320
au!
308321
augroup END
309322
unlet g:record
310323
endfunc
311324

325+
func Test_WinNewPre()
326+
" Test that the old window layout can be accessed before a new window is created.
327+
let g:layouts_pre = []
328+
let g:layouts_post = []
329+
augroup testing
330+
au WinNewPre * call add(g:layouts_pre, winlayout())
331+
au WinNew * call add(g:layouts_post, winlayout())
332+
augroup END
333+
split
334+
call assert_notequal(g:layouts_pre[0], g:layouts_post[0])
335+
split
336+
call assert_equal(g:layouts_pre[1], g:layouts_post[0])
337+
call assert_notequal(g:layouts_pre[1], g:layouts_post[1])
338+
tabnew
339+
call assert_notequal(g:layouts_pre[2], g:layouts_post[1])
340+
call assert_notequal(g:layouts_pre[2], g:layouts_post[2])
341+
augroup testing
342+
au!
343+
augroup END
344+
unlet g:layouts_pre
345+
unlet g:layouts_post
346+
347+
" Test modifying window layout during WinNewPre throws.
348+
let g:caught = 0
349+
augroup testing
350+
au!
351+
au WinNewPre * split
352+
augroup END
353+
try
354+
vnew
355+
catch
356+
let g:caught += 1
357+
endtry
358+
augroup testing
359+
au!
360+
au WinNewPre * tabnew
361+
augroup END
362+
try
363+
vnew
364+
catch
365+
let g:caught += 1
366+
endtry
367+
augroup testing
368+
au!
369+
au WinNewPre * close
370+
augroup END
371+
try
372+
vnew
373+
catch
374+
let g:caught += 1
375+
endtry
376+
augroup testing
377+
au!
378+
au WinNewPre * tabclose
379+
augroup END
380+
try
381+
vnew
382+
catch
383+
let g:caught += 1
384+
endtry
385+
call assert_equal(4, g:caught)
386+
augroup testing
387+
au!
388+
augroup END
389+
unlet g:caught
390+
endfunc
391+
312392
func Test_WinResized()
313393
CheckRunVimInTerminal
314394

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
59,
707709
/**/
708710
58,
709711
/**/

src/vim.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,8 @@ enum auto_event
14351435
EVENT_VIMRESIZED, // after Vim window was resized
14361436
EVENT_WINENTER, // after entering a window
14371437
EVENT_WINLEAVE, // before leaving a window
1438-
EVENT_WINNEW, // when entering a new window
1438+
EVENT_WINNEWPRE, // before creating a new window
1439+
EVENT_WINNEW, // after creating a new window
14391440
EVENT_WINCLOSED, // after closing a window
14401441
EVENT_VIMSUSPEND, // before Vim is suspended
14411442
EVENT_VIMRESUME, // after Vim is resumed

src/window.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static void win_exchange(long);
1919
static void win_rotate(int, int);
2020
static void win_totop(int size, int flags);
2121
static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
22+
static void trigger_winnewpre(void);
2223
static void trigger_winclosed(win_T *win);
2324
static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
2425
static frame_T *win_altframe(win_T *win, tabpage_T *tp);
@@ -955,6 +956,8 @@ win_split_ins(
955956
// Do not redraw here, curwin->w_buffer may be invalid.
956957
++RedrawingDisabled;
957958

959+
trigger_winnewpre();
960+
958961
if (flags & WSP_TOP)
959962
oldwin = firstwin;
960963
else if (flags & WSP_BOT)
@@ -2886,6 +2889,14 @@ win_close(win_T *win, int free_buf)
28862889
return OK;
28872890
}
28882891

2892+
static void
2893+
trigger_winnewpre(void)
2894+
{
2895+
window_layout_lock();
2896+
apply_autocmds(EVENT_WINNEWPRE, NULL, NULL, FALSE, NULL);
2897+
window_layout_unlock();
2898+
}
2899+
28892900
static void
28902901
trigger_winclosed(win_T *win)
28912902
{
@@ -4477,6 +4488,9 @@ win_new_tabpage(int after)
44774488

44784489
newtp->tp_localdir = (tp->tp_localdir == NULL)
44794490
? NULL : vim_strsave(tp->tp_localdir);
4491+
4492+
trigger_winnewpre();
4493+
44804494
// Create a new empty window.
44814495
if (win_alloc_firstwin(tp->tp_curwin) == OK)
44824496
{

0 commit comments

Comments
 (0)