]> granicus.if.org Git - vim/commitdiff
patch 8.1.0245: calling setline() in TextChangedI autocmd breaks undo v8.1.0245
authorBram Moolenaar <Bram@vim.org>
Tue, 7 Aug 2018 17:05:01 +0000 (19:05 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 7 Aug 2018 17:05:01 +0000 (19:05 +0200)
Problem:    Calling setline() in TextChangedI autocmd breaks undo. (Jason
            Felice)
Solution:   Don't save lines for undo when already saved. (closes #3291)

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

index 45176d79f497272d56326c39134e6338ba41cd61..b5c129f991b646f27adafec20e325dfd75071a38 100644 (file)
@@ -1722,11 +1722,19 @@ ins_redraw(
     {
        aco_save_T      aco;
 
+       // Sync undo when the autocommand calls setline() or append(), so that
+       // it can be undone separately.
+       u_sync_once = 2;
+
        // save and restore curwin and curbuf, in case the autocmd changes them
        aucmd_prepbuf(&aco, curbuf);
        apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
        aucmd_restbuf(&aco);
        curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
+
+       if (u_sync_once == 1)
+           ins_need_undo = TRUE;
+       u_sync_once = 0;
     }
 
 #ifdef FEAT_INS_EXPAND
index 19c54d1567651e67b09206861fc0a3147c56b95c..b52493fcb49cc6e779740fb27a14ad5d80f191c0 100644 (file)
@@ -587,7 +587,7 @@ func Test_OptionSet()
   " Cleanup
   au! OptionSet
   for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp']
-    exe printf(":set %s&vi", opt)
+    exe printf(":set %s&vim", opt)
   endfor
   call test_override('starting', 0)
   delfunc! AutoCommandOptionSet
@@ -1313,6 +1313,31 @@ func Test_ChangedP()
   bw!
 endfunc
 
+let g:setline_handled = v:false
+func! SetLineOne()
+  if !g:setline_handled
+    call setline(1, "(x)")
+    let g:setline_handled = v:true
+  endif
+endfunc
+
+func Test_TextChangedI_with_setline()
+  new
+  call test_override('char_avail', 1)
+  autocmd TextChangedI <buffer> call SetLineOne()
+  call feedkeys("i(\<CR>\<Esc>", 'tx')
+  call assert_equal('(', getline(1))
+  call assert_equal('x)', getline(2))
+  undo
+  call assert_equal('(', getline(1))
+  call assert_equal('', getline(2))
+  undo
+  call assert_equal('', getline(1))
+
+  call test_override('starting', 0)
+  bwipe!
+endfunc
+
 func Test_Changed_FirstTime()
   if !has('terminal') || has('gui_running')
     return
index 08345408b4a040a3f3b97f8ee1cf363374e57a90..3b437c45ace6f617d03091c213d1405c86867116 100644 (file)
@@ -794,6 +794,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    245,
 /**/
     244,
 /**/