]> granicus.if.org Git - vim/commitdiff
patch 8.2.0302: setting 'term' may cause error in TermChanged autocommand v8.2.0302
authorBram Moolenaar <Bram@vim.org>
Sat, 22 Feb 2020 21:45:55 +0000 (22:45 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 22 Feb 2020 21:45:55 +0000 (22:45 +0100)
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)

src/term.c
src/testdir/test_autocmd.vim
src/version.c

index d269eb83c04ea694f8a2bb4adede866d9e344491..e9a9004a2dcb657334f439ba9b00ba9bdb714fe7 100644 (file)
@@ -2071,21 +2071,24 @@ set_termname(char_u *term)
        check_map_keycodes();   // check mappings for terminal codes used
 
        {
-           bufref_T    old_curbuf;
+           buf_T       *buf;
+           aco_save_T  aco;
 
            /*
             * Execute the TermChanged autocommands for each buffer that is
             * loaded.
             */
-           set_bufref(&old_curbuf, curbuf);
-           FOR_ALL_BUFFERS(curbuf)
+           FOR_ALL_BUFFERS(buf)
            {
                if (curbuf->b_ml.ml_mfp != NULL)
+               {
+                   aucmd_prepbuf(&aco, buf);
                    apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
                                                                      curbuf);
+                   // restore curwin/curbuf and a few other things
+                   aucmd_restbuf(&aco);
+               }
            }
-           if (bufref_valid(&old_curbuf))
-               curbuf = old_curbuf.br_buf;
        }
     }
 
index a16329b8cd72328b6111e678a021223b1426c89f..70e96087cf389483ab63f9e2cf23bed8061d1bbe 100644 (file)
@@ -2382,4 +2382,24 @@ func Test_FileChangedRO_winclose()
   augroup! FileChangedROTest
 endfunc
 
+func LogACmd()
+  call add(g:logged, line('$'))
+endfunc
+
+func Test_TermChanged()
+  enew!
+  tabnew
+  call setline(1, ['a', 'b', 'c', 'd'])
+  $
+  au TermChanged * call LogACmd()
+  let g:logged = []
+  let term_save = &term
+  set term=xterm
+  call assert_equal([1, 4], g:logged)
+
+  au! TermChanged
+  let &term = term_save
+  bwipe!
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index cda4a816495df812bc46d5794769e3484ad1bcc5..96ef2c1037cc17400cfcf577f39b2c50bfda9cc4 100644 (file)
@@ -738,6 +738,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    302,
 /**/
     301,
 /**/