Skip to content

Commit 5794223

Browse files
committed
patch 8.2.3287: channel events not handled in BufEnter autocommand
Problem: Channel events not handled in BufEnter autocommand. Solution: Decrement dont_parse_messages earlier. (Tim Pope, closes #8697)
1 parent d61f2f7 commit 5794223

3 files changed

Lines changed: 65 additions & 14 deletions

File tree

src/testdir/test_channel.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,4 +2330,33 @@ func Test_cb_with_input()
23302330
unlet g:wait_exit_cb
23312331
endfunc
23322332

2333+
function s:HandleBufEnter() abort
2334+
let queue = []
2335+
let job = job_start(['date'], {'callback': { j, d -> add(queue, d) }})
2336+
while empty(queue)
2337+
sleep! 10m
2338+
endwhile
2339+
endfunction
2340+
2341+
func Test_parse_messages_in_autocmd()
2342+
CheckUnix
2343+
2344+
" Check that in the BufEnter autocommand events are being handled
2345+
augroup bufenterjob
2346+
autocmd!
2347+
autocmd BufEnter Xbufenterjob call s:HandleBufEnter()
2348+
augroup END
2349+
2350+
only
2351+
split Xbufenterjob
2352+
wincmd p
2353+
redraw
2354+
2355+
close
2356+
augroup bufenterjob
2357+
autocmd!
2358+
augroup END
2359+
endfunc
2360+
2361+
23332362
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3287,
758760
/**/
759761
3286,
760762
/**/

src/window.c

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_au
4040
static void frame_fix_height(win_T *wp);
4141
static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
4242
static int may_open_tabpage(void);
43-
static void win_enter_ext(win_T *wp, int flags);
43+
static int win_enter_ext(win_T *wp, int flags);
4444
static void win_free(win_T *wp, tabpage_T *tp);
4545
static int win_unlisted(win_T *wp);
4646
static void win_append(win_T *after, win_T *wp);
@@ -73,6 +73,7 @@ static win_T *win_alloc(win_T *after, int hidden);
7373
#define WEE_TRIGGER_NEW_AUTOCMDS 0x04
7474
#define WEE_TRIGGER_ENTER_AUTOCMDS 0x08
7575
#define WEE_TRIGGER_LEAVE_AUTOCMDS 0x10
76+
#define WEE_ALLOW_PARSE_MESSAGES 0x20
7677

7778
static char *m_onlyone = N_("Already only one window");
7879

@@ -1338,8 +1339,8 @@ win_split_ins(
13381339
/*
13391340
* make the new window the current window
13401341
*/
1341-
win_enter_ext(wp, WEE_TRIGGER_NEW_AUTOCMDS | WEE_TRIGGER_ENTER_AUTOCMDS
1342-
| WEE_TRIGGER_LEAVE_AUTOCMDS);
1342+
(void)win_enter_ext(wp, WEE_TRIGGER_NEW_AUTOCMDS
1343+
| WEE_TRIGGER_ENTER_AUTOCMDS | WEE_TRIGGER_LEAVE_AUTOCMDS);
13431344
if (flags & WSP_VERT)
13441345
p_wiw = i;
13451346
else
@@ -2483,6 +2484,7 @@ win_close(win_T *win, int free_buf)
24832484
#ifdef FEAT_DIFF
24842485
int had_diffmode = win->w_p_diff;
24852486
#endif
2487+
int did_decrement = FALSE;
24862488

24872489
#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
24882490
// Can close a popup window with a terminal if the job has finished.
@@ -2661,16 +2663,20 @@ win_close(win_T *win, int free_buf)
26612663
win_comp_pos();
26622664
if (close_curwin)
26632665
{
2664-
win_enter_ext(wp, WEE_CURWIN_INVALID | WEE_TRIGGER_ENTER_AUTOCMDS
2665-
| WEE_TRIGGER_LEAVE_AUTOCMDS);
2666+
// Pass WEE_ALLOW_PARSE_MESSAGES to decrement dont_parse_messages
2667+
// before autocommands.
2668+
did_decrement = win_enter_ext(wp,
2669+
WEE_CURWIN_INVALID | WEE_TRIGGER_ENTER_AUTOCMDS
2670+
| WEE_TRIGGER_LEAVE_AUTOCMDS | WEE_ALLOW_PARSE_MESSAGES);
26662671
if (other_buffer)
26672672
// careful: after this wp and win may be invalid!
26682673
apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
26692674
}
26702675

26712676
--split_disallowed;
26722677
#ifdef MESSAGE_QUEUE
2673-
--dont_parse_messages;
2678+
if (!did_decrement)
2679+
--dont_parse_messages;
26742680
#endif
26752681

26762682
/*
@@ -4188,7 +4194,7 @@ enter_tabpage(
41884194
// We would like doing the TabEnter event first, but we don't have a
41894195
// valid current window yet, which may break some commands.
41904196
// This triggers autocommands, thus may make "tp" invalid.
4191-
win_enter_ext(tp->tp_curwin, WEE_CURWIN_INVALID
4197+
(void)win_enter_ext(tp->tp_curwin, WEE_CURWIN_INVALID
41924198
| (trigger_enter_autocmds ? WEE_TRIGGER_ENTER_AUTOCMDS : 0)
41934199
| (trigger_leave_autocmds ? WEE_TRIGGER_LEAVE_AUTOCMDS : 0));
41944200
prevwin = next_prevwin;
@@ -4476,7 +4482,7 @@ win_goto(win_T *wp)
44764482
#endif
44774483
}
44784484

4479-
#if defined(FEAT_PERL) || defined(PROTO)
4485+
#if defined(FEAT_PERL) || defined(FEAT_LUA) || defined(PROTO)
44804486
/*
44814487
* Find window number "winnr" (counting top to bottom).
44824488
*/
@@ -4689,23 +4695,25 @@ win_goto_hor(
46894695
void
46904696
win_enter(win_T *wp, int undo_sync)
46914697
{
4692-
win_enter_ext(wp, (undo_sync ? WEE_UNDO_SYNC : 0)
4698+
(void)win_enter_ext(wp, (undo_sync ? WEE_UNDO_SYNC : 0)
46934699
| WEE_TRIGGER_ENTER_AUTOCMDS | WEE_TRIGGER_LEAVE_AUTOCMDS);
46944700
}
46954701

46964702
/*
46974703
* Make window "wp" the current window.
46984704
* Can be called with "flags" containing WEE_CURWIN_INVALID, which means that
46994705
* curwin has just been closed and isn't valid.
4706+
* Returns TRUE when dont_parse_messages was decremented.
47004707
*/
4701-
static void
4708+
static int
47024709
win_enter_ext(win_T *wp, int flags)
47034710
{
47044711
int other_buffer = FALSE;
47054712
int curwin_invalid = (flags & WEE_CURWIN_INVALID);
4713+
int did_decrement = FALSE;
47064714

47074715
if (wp == curwin && !curwin_invalid) // nothing to do
4708-
return;
4716+
return FALSE;
47094717

47104718
#ifdef FEAT_JOB_CHANNEL
47114719
if (!curwin_invalid)
@@ -4722,15 +4730,15 @@ win_enter_ext(win_T *wp, int flags)
47224730
apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
47234731
other_buffer = TRUE;
47244732
if (!win_valid(wp))
4725-
return;
4733+
return FALSE;
47264734
}
47274735
apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
47284736
if (!win_valid(wp))
4729-
return;
4737+
return FALSE;
47304738
#ifdef FEAT_EVAL
47314739
// autocmds may abort script processing
47324740
if (aborting())
4733-
return;
4741+
return FALSE;
47344742
#endif
47354743
}
47364744

@@ -4757,6 +4765,16 @@ win_enter_ext(win_T *wp, int flags)
47574765
curwin->w_cursor.coladd = 0;
47584766
changed_line_abv_curs(); // assume cursor position needs updating
47594767

4768+
// Now it is OK to parse messages again, which may be needed in
4769+
// autocommands.
4770+
#ifdef MESSAGE_QUEUE
4771+
if (flags & WEE_ALLOW_PARSE_MESSAGES)
4772+
{
4773+
--dont_parse_messages;
4774+
did_decrement = TRUE;
4775+
}
4776+
#endif
4777+
47604778
if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
47614779
{
47624780
char_u *dirname;
@@ -4832,6 +4850,8 @@ win_enter_ext(win_T *wp, int flags)
48324850

48334851
// Change directories when the 'acd' option is set.
48344852
DO_AUTOCHDIR;
4853+
4854+
return did_decrement;
48354855
}
48364856

48374857

0 commit comments

Comments
 (0)