]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.2037: undo: cursor position not correctly restored v9.1.2037
authoraltermo <107814000+altermo@users.noreply.github.com>
Thu, 1 Jan 2026 14:32:50 +0000 (14:32 +0000)
committerChristian Brabandt <cb@256bit.org>
Thu, 1 Jan 2026 14:32:50 +0000 (14:32 +0000)
Problem:  undo: cursor position not correctly restored
Solution: Do not override the saved cursor position (altermo)

closes: #19052

Signed-off-by: altermo <107814000+altermo@users.noreply.github.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/testdir/test_undo.vim
src/undo.c
src/version.c

index c69cf09bfcc65af776cb55438db7f92015450e30..ad724aa04d01c5e1672bc4a084b709276c93b843 100644 (file)
@@ -913,5 +913,16 @@ func Test_load_existing_undofile()
   bw!
 endfunc
 
+func Test_restore_cursor_position_after_undo()
+  CheckFeature persistent_undo
+  sp samples/test_undo.txt
+
+  3 | exe "norm! gqk" | undojoin | 1 delete
+  call assert_equal(1, line('.'))
+  norm! u
+  call assert_equal(3, line('.'))
+  bw!
+endfunc
+
 
 " vim: shiftwidth=2 sts=2 expandtab
index f40aca66052f0aa5bccdb45855f31e3b2f620320..54fdd227150e3a4fa7447f59d39b8283e5f1067c 100644 (file)
@@ -2721,7 +2721,6 @@ u_undoredo(int undo)
 
        // Decide about the cursor position, depending on what text changed.
        // Don't set it yet, it may be invalid if lines are going to be added.
-       if (top < newlnum)
        {
            // If the saved cursor is somewhere in this undo block, move it to
            // the remembered position.  Makes "gwap" put the cursor back
@@ -2730,9 +2729,12 @@ u_undoredo(int undo)
            if (lnum >= top && lnum <= top + newsize + 1)
            {
                new_curpos = curhead->uh_cursor;
-               newlnum = new_curpos.lnum - 1;
+
+               // We don't want other entries to override saved cursor
+               // position.
+               newlnum = -1;
            }
-           else
+           else if (top < newlnum)
            {
                // Use the first line that actually changed.  Avoids that
                // undoing auto-formatting puts the cursor in the previous
index 77eed6f88799141b29dac08276d679fec0fc4b30..95992bcef7c45679b50132787fec4c3dcaa276bf 100644 (file)
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2037,
 /**/
     2036,
 /**/