Skip to content

Commit 3f8f827

Browse files
committed
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 #11674)
1 parent d28d7b9 commit 3f8f827

6 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/edit.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,12 +1049,19 @@ edit(
10491049

10501050
case K_COMMAND: // <Cmd>command<CR>
10511051
case K_SCRIPT_COMMAND: // <ScriptCmd>command<CR>
1052-
do_cmdkey_command(c, 0);
1052+
{
1053+
do_cmdkey_command(c, 0);
1054+
10531055
#ifdef FEAT_TERMINAL
1054-
if (term_use_loop())
1055-
// Started a terminal that gets the input, exit Insert mode.
1056-
goto doESCkey;
1056+
if (term_use_loop())
1057+
// Started a terminal that gets the input, exit Insert mode.
1058+
goto doESCkey;
10571059
#endif
1060+
if (curbuf->b_u_synced)
1061+
// The command caused undo to be synced. Need to save the
1062+
// line for undo before inserting the next char.
1063+
ins_need_undo = TRUE;
1064+
}
10581065
break;
10591066

10601067
case K_CURSORHOLD: // Didn't type something for a while.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
|t+0&#ffffff0|e|s>t| @70
2+
@75
3+
|~+0#4040ff13&| @73
4+
|~| @73
5+
|~| @73
6+
|1+0#0000000&| |l|i|n|e| |l|e|s@1|;| |b|e|f|o|r|e| |#|2| @1|0| |s|e|c|o|n|d|s| |a|g|o| @19|1|,|4| @10|A|l@1|
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
> +0&#ffffff0@74
2+
|~+0#4040ff13&| @73
3+
|~| @73
4+
|~| @73
5+
|~| @73
6+
|2+0#0000000&| |f|e|w|e|r| |l|i|n|e|s|;| |b|e|f|o|r|e| |#|1| @1|1| |s|e|c|o|n|d| |a|g|o| @18|0|,|0|-|1| @8|A|l@1|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
" Filter that changes the "1 second ago" message to "0 seconds ago".
2+
6s+|1| |s|e|c|o|n|d| |a|g|o| @18|+|0| |s|e|c|o|n|d|s| |a|g|o| @17|+e

src/testdir/test_undo.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
" undo-able pieces. Do that by setting 'undolevels'.
44
" Also tests :earlier and :later.
55

6+
source check.vim
7+
source screendump.vim
8+
69
func Test_undotree()
710
new
811

@@ -775,4 +778,30 @@ func Test_undo_mark()
775778
bwipe!
776779
endfunc
777780

781+
func Test_undo_after_write()
782+
" use a terminal to make undo work like when text is typed
783+
CheckRunVimInTerminal
784+
785+
let lines =<< trim END
786+
edit Xtestfile.txt
787+
set undolevels=100 undofile
788+
imap . <Cmd>write<CR>
789+
write
790+
END
791+
call writefile(lines, 'Xtest_undo_after_write', 'D')
792+
let buf = RunVimInTerminal('-S Xtest_undo_after_write', #{rows: 6})
793+
794+
call term_sendkeys(buf, "Otest.\<CR>boo!!!\<Esc>")
795+
sleep 100m
796+
call term_sendkeys(buf, "u")
797+
call VerifyScreenDump(buf, 'Test_undo_after_write_1', {})
798+
799+
call term_sendkeys(buf, "u")
800+
call VerifyScreenDump(buf, 'Test_undo_after_write_2', {})
801+
802+
call StopVimInTerminal(buf)
803+
call delete('Xtestfile.txt')
804+
endfunc
805+
806+
778807
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1036,
698700
/**/
699701
1035,
700702
/**/

0 commit comments

Comments
 (0)