# HG changeset patch # User Christian Brabandt # Date 1533662105 -7200 # Node ID 3b87daa5c37a73f2778eb1951e16c37e076f04bf # Parent 1b1e12886c9ed1df103a9bbe475e1612924b71f4 patch 8.1.0245: calling setline() in TextChangedI autocmd breaks undo commit https://github.com/vim/vim/commit/91d2e783b41ca900bc603b3cb5e083c8a4a33170 Author: Bram Moolenaar Date: Tue Aug 7 19:05:01 2018 +0200 patch 8.1.0245: calling setline() in TextChangedI autocmd breaks undo Problem: Calling setline() in TextChangedI autocmd breaks undo. (Jason Felice) Solution: Don't save lines for undo when already saved. (closes #3291) diff --git a/src/edit.c b/src/edit.c --- a/src/edit.c +++ b/src/edit.c @@ -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 diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim --- a/src/testdir/test_autocmd.vim +++ b/src/testdir/test_autocmd.vim @@ -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 call SetLineOne() + call feedkeys("i(\\", '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 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -795,6 +795,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 245, +/**/ 244, /**/ 243,