File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -450,6 +450,11 @@ close_buffer(
450450 int is_curbuf ;
451451 int nwindows ;
452452 bufref_T bufref ;
453+ # ifdef FEAT_WINDOWS
454+ int is_curwin = (curwin != NULL && curwin -> w_buffer == buf );
455+ win_T * the_curwin = curwin ;
456+ tabpage_T * the_curtab = curtab ;
457+ # endif
453458#endif
454459 int unload_buf = (action != 0 );
455460 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE );
@@ -544,6 +549,19 @@ close_buffer(
544549 return ;
545550# endif
546551 }
552+
553+ # ifdef FEAT_WINDOWS
554+ /* If the buffer was in curwin and the window has changed, go back to that
555+ * window, if it still exists. This avoids that ":edit x" triggering a
556+ * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
557+ if (is_curwin && curwin != the_curwin && win_valid_any_tab (the_curwin ))
558+ {
559+ block_autocmds ();
560+ goto_tabpage_win (the_curtab , the_curwin );
561+ unblock_autocmds ();
562+ }
563+ # endif
564+
547565 nwindows = buf -> b_nwindows ;
548566#endif
549567
Original file line number Diff line number Diff line change @@ -83,6 +83,16 @@ function Test_autocmd_bufunload_with_tabnext()
8383 quit
8484endfunc
8585
86+ function Test_autocmd_bufwinleave_with_tabfirst ()
87+ tabedit
88+ augroup sample
89+ autocmd !
90+ autocmd BufWinLeave <buffer> tabfirst
91+ augroup END
92+ call setline (1 , [' a' , ' b' , ' c' ])
93+ edit ! a .txt
94+ endfunc
95+
8696" SEGV occurs in older versions. (At least 7.4.2321 or older)
8797function Test_autocmd_bufunload_avoiding_SEGV_01 ()
8898 split aa.txt
Original file line number Diff line number Diff line change @@ -763,6 +763,8 @@ static char *(features[]) =
763763
764764static int included_patches [] =
765765{ /* Add new patch number below this line */
766+ /**/
767+ 2328 ,
766768/**/
767769 2327 ,
768770/**/
You can’t perform that action at this time.
0 commit comments