]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.0094: cursor restored unexpected with nested autocommand v9.0.0094
authorBram Moolenaar <Bram@vim.org>
Wed, 27 Jul 2022 14:23:35 +0000 (15:23 +0100)
committerBram Moolenaar <Bram@vim.org>
Wed, 27 Jul 2022 14:23:35 +0000 (15:23 +0100)
Problem:    Cursor restored unexpected with nested autocommand.
Solution:   Do not restore the cursor when it was moved intentionally.
            (closes #10780)

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

index f7763e6bd1cf5d0cfac816cffcea94b48627f164..6bbe48c0f2fbd225041a0e99199be801399bb398 100644 (file)
@@ -2321,6 +2321,17 @@ func Test_autocmd_nested_cursor_invalid()
   bwipe!
 endfunc
 
+func Test_autocmd_nested_keeps_cursor_pos()
+  enew
+  call setline(1, 'foo')
+  autocmd User foo ++nested normal! $a
+  autocmd InsertLeave * :
+  doautocmd User foo
+  call assert_equal([0, 1, 3, 0], getpos('.'))
+
+  bwipe!
+endfunc
+
 func Test_autocmd_nested_switch_window()
   " run this in a separate Vim so that SafeState works
   CheckRunVimInTerminal
index fde548bbb3f619221e267aaa35a1dc8867f074a6..8232111feb3bf5e855d8438560ea32fce951dfe8 100644 (file)
@@ -735,6 +735,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    94,
 /**/
     93,
 /**/
index 83cc24e23fad392424fd7b7fa6b53d6ab6e7776f..6be736945bcca250e17763c4b8dab448f482006c 100644 (file)
@@ -6781,6 +6781,8 @@ check_lnums_both(int do_curwin, int nested)
     FOR_ALL_TAB_WINDOWS(tp, wp)
        if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
        {
+           int need_adjust;
+
            if (!nested)
            {
                // save the original cursor position and topline
@@ -6788,14 +6790,19 @@ check_lnums_both(int do_curwin, int nested)
                wp->w_save_cursor.w_topline_save = wp->w_topline;
            }
 
-           if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
+           need_adjust = wp->w_cursor.lnum > curbuf->b_ml.ml_line_count;
+           if (need_adjust)
                wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
-           if (wp->w_topline > curbuf->b_ml.ml_line_count)
-               wp->w_topline = curbuf->b_ml.ml_line_count;
+           if (need_adjust || !nested)
+               // save the (corrected) cursor position
+               wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
 
-           // save the (corrected) cursor position and topline
-           wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
-           wp->w_save_cursor.w_topline_corr = wp->w_topline;
+           need_adjust = wp->w_topline > curbuf->b_ml.ml_line_count;
+           if (need_adjust)
+               wp->w_topline = curbuf->b_ml.ml_line_count;
+           if (need_adjust || !nested)
+               // save the (corrected) topline
+               wp->w_save_cursor.w_topline_corr = wp->w_topline;
        }
 }