From 70d6c335b17cf166760457e3673aa61098ef0e66 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 9 Dec 2022 06:35:29 +0800 Subject: vim-patch:9.0.1036: undo misbehaves when writing from an insert mode mapping Problem: Undo misbehaves when writing from an insert mode mapping. Solution: Sync undo when writing. (closes vim/vim#11674) https://github.com/vim/vim/commit/3f8f82772313af9f2417b06651f30988b63e1c96 Co-authored-by: Bram Moolenaar --- src/nvim/edit.c | 6 ++++++ src/nvim/testdir/test_undo.vim | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) (limited to 'src') diff --git a/src/nvim/edit.c b/src/nvim/edit.c index ac2973ba68..09836e747f 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -908,6 +908,12 @@ check_pum: } pum_want.active = false; } + + if (curbuf->b_u_synced) { + // The K_EVENT, K_COMMAND, or K_LUA caused undo to be synced. + // Need to save the line for undo before inserting the next char. + ins_need_undo = true; + } break; case K_HOME: // diff --git a/src/nvim/testdir/test_undo.vim b/src/nvim/testdir/test_undo.vim index eb47af08d7..ee8b52caaf 100644 --- a/src/nvim/testdir/test_undo.vim +++ b/src/nvim/testdir/test_undo.vim @@ -3,6 +3,9 @@ " undo-able pieces. Do that by setting 'undolevels'. " Also tests :earlier and :later. +source check.vim +source screendump.vim + func Test_undotree() new @@ -773,4 +776,30 @@ func Test_undo_mark() bwipe! endfunc +func Test_undo_after_write() + " use a terminal to make undo work like when text is typed + CheckRunVimInTerminal + + let lines =<< trim END + edit Xtestfile.txt + set undolevels=100 undofile + imap . write + write + END + call writefile(lines, 'Xtest_undo_after_write', 'D') + let buf = RunVimInTerminal('-S Xtest_undo_after_write', #{rows: 6}) + + call term_sendkeys(buf, "Otest.\boo!!!\") + sleep 100m + call term_sendkeys(buf, "u") + call VerifyScreenDump(buf, 'Test_undo_after_write_1', {}) + + call term_sendkeys(buf, "u") + call VerifyScreenDump(buf, 'Test_undo_after_write_2', {}) + + call StopVimInTerminal(buf) + call delete('Xtestfile.txt') +endfunc + + " vim: shiftwidth=2 sts=2 expandtab -- cgit