Skip to content

Commit 0c81d1b

Browse files
committed
patch 8.2.0302: setting 'term' may cause error in TermChanged autocommand
Problem: Setting 'term' may cause error in TermChanged autocommand. Solution: Use aucmd_prepbuf() to switch to the buffer where the autocommand is to be executed. (closes #5682)
1 parent b654103 commit 0c81d1b

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/term.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,21 +2071,24 @@ set_termname(char_u *term)
20712071
check_map_keycodes(); // check mappings for terminal codes used
20722072

20732073
{
2074-
bufref_T old_curbuf;
2074+
buf_T *buf;
2075+
aco_save_T aco;
20752076

20762077
/*
20772078
* Execute the TermChanged autocommands for each buffer that is
20782079
* loaded.
20792080
*/
2080-
set_bufref(&old_curbuf, curbuf);
2081-
FOR_ALL_BUFFERS(curbuf)
2081+
FOR_ALL_BUFFERS(buf)
20822082
{
20832083
if (curbuf->b_ml.ml_mfp != NULL)
2084+
{
2085+
aucmd_prepbuf(&aco, buf);
20842086
apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
20852087
curbuf);
2088+
// restore curwin/curbuf and a few other things
2089+
aucmd_restbuf(&aco);
2090+
}
20862091
}
2087-
if (bufref_valid(&old_curbuf))
2088-
curbuf = old_curbuf.br_buf;
20892092
}
20902093
}
20912094

src/testdir/test_autocmd.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,4 +2382,24 @@ func Test_FileChangedRO_winclose()
23822382
augroup! FileChangedROTest
23832383
endfunc
23842384

2385+
func LogACmd()
2386+
call add(g:logged, line('$'))
2387+
endfunc
2388+
2389+
func Test_TermChanged()
2390+
enew!
2391+
tabnew
2392+
call setline(1, ['a', 'b', 'c', 'd'])
2393+
$
2394+
au TermChanged * call LogACmd()
2395+
let g:logged = []
2396+
let term_save = &term
2397+
set term=xterm
2398+
call assert_equal([1, 4], g:logged)
2399+
2400+
au! TermChanged
2401+
let &term = term_save
2402+
bwipe!
2403+
endfunc
2404+
23852405
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

739739
static int included_patches[] =
740740
{ /* Add new patch number below this line */
741+
/**/
742+
302,
741743
/**/
742744
301,
743745
/**/

0 commit comments

Comments
 (0)