Skip to content

Commit f9e687e

Browse files
committed
patch 7.4.2328
Problem: Crash when BufWinLeave autocmd goes to another tab page. (Hirohito Higashi) Solution: Make close_buffer() go back to the right window.
1 parent c8fe338 commit f9e687e

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/buffer.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/testdir/test_autocmd.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ function Test_autocmd_bufunload_with_tabnext()
8383
quit
8484
endfunc
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)
8797
function Test_autocmd_bufunload_avoiding_SEGV_01()
8898
split aa.txt

src/version.c

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

764764
static int included_patches[] =
765765
{ /* Add new patch number below this line */
766+
/**/
767+
2328,
766768
/**/
767769
2327,
768770
/**/

0 commit comments

Comments
 (0)