]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.0061: ml_get error with nested autocommand v9.0.0061
authorBram Moolenaar <Bram@vim.org>
Sat, 23 Jul 2022 08:06:48 +0000 (09:06 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 23 Jul 2022 08:06:48 +0000 (09:06 +0100)
Problem:    ml_get error with nested autocommand.
Solution:   Also check line numbers for a nested autocommand. (closes #10761)

src/autocmd.c
src/proto/window.pro
src/testdir/test_autocmd.vim
src/version.c
src/window.c

index 841da146226c071b6eb0bb51176595ed0a1ff00e..c376f20a231469768309831c4ee41cdd1405ff1a 100644 (file)
@@ -2209,9 +2209,13 @@ apply_autocmds_group(
            ap->last = FALSE;
        ap->last = TRUE;
 
+       // Make sure cursor and topline are valid.  The first time the current
+       // values are saved, restored by reset_lnums().  When nested only the
+       // values are corrected when needed.
        if (nesting == 1)
-           // make sure cursor and topline are valid
            check_lnums(TRUE);
+       else
+           check_lnums_nested(TRUE);
 
        save_did_emsg = did_emsg;
 
index 9625942feacac233a0b0b6495e3dfc748d6edcf8..8fa86616855ecf468250f87be9028760585865ca 100644 (file)
@@ -77,6 +77,7 @@ int tabline_height(void);
 int min_rows(void);
 int only_one_window(void);
 void check_lnums(int do_curwin);
+void check_lnums_nested(int do_curwin);
 void reset_lnums(void);
 void make_snapshot(int idx);
 void restore_snapshot(int idx, int close_curwin);
index e9a59c29a6b246c82c017406988c039d0261ad7c..1202c058dadc88e8cc5e226a47599a1d48c70401 100644 (file)
@@ -2301,6 +2301,25 @@ func Test_autocmd_nested()
   call assert_fails('au WinNew * nested nested echo bad', 'E983:')
 endfunc
 
+func Test_autocmd_nested_cursor_invalid()
+  set laststatus=0
+  copen
+  cclose
+  call setline(1, ['foo', 'bar', 'baz'])
+  3
+  augroup nested_inv
+    autocmd User foo ++nested copen
+    autocmd BufAdd * let &laststatus = 2 - &laststatus
+  augroup END
+  doautocmd User foo
+
+  augroup nested_inv
+    au!
+  augroup END
+  set laststatus&
+  bwipe!
+endfunc
+
 func Test_autocmd_once()
   " Without ++once WinNew triggers twice
   let g:did_split = 0
index b6e61f50ef16cd86f0f47850ed0eac57b6f037ef..46d964312fb5e35b666b2c71157669d002a6a4da 100644 (file)
@@ -735,6 +735,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    61,
 /**/
     60,
 /**/
index c91ebbcfe579405602ca514909e17ea0fca7b1a7..e7b44abd16b660f5e0284124d1c470b49ab9f8f7 100644 (file)
@@ -6770,12 +6770,10 @@ only_one_window(void)
 }
 
 /*
- * Correct the cursor line number in other windows.  Used after changing the
- * current buffer, and before applying autocommands.
- * When "do_curwin" is TRUE, also check current window.
+ * Implementation of check_lnums() and check_lnums_nested().
  */
-    void
-check_lnums(int do_curwin)
+    static void
+check_lnums_both(int do_curwin, int nested)
 {
     win_T      *wp;
     tabpage_T  *tp;
@@ -6783,21 +6781,44 @@ check_lnums(int do_curwin)
     FOR_ALL_TAB_WINDOWS(tp, wp)
        if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
        {
-           // save the original cursor position and topline
-           wp->w_save_cursor.w_cursor_save = wp->w_cursor;
-           wp->w_save_cursor.w_topline_save = wp->w_topline;
+           if (!nested)
+           {
+               // save the original cursor position and topline
+               wp->w_save_cursor.w_cursor_save = wp->w_cursor;
+               wp->w_save_cursor.w_topline_save = wp->w_topline;
+           }
 
            if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
                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;
 
-           // save the corrected cursor position and topline
+           // 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;
        }
 }
 
+/*
+ * Correct the cursor line number in other windows.  Used after changing the
+ * current buffer, and before applying autocommands.
+ * When "do_curwin" is TRUE, also check current window.
+ */
+    void
+check_lnums(int do_curwin)
+{
+    check_lnums_both(do_curwin, FALSE);
+}
+
+/*
+ * Like check_lnums() but for when check_lnums() was already called.
+ */
+    void
+check_lnums_nested(int do_curwin)
+{
+    check_lnums_both(do_curwin, TRUE);
+}
+
 /*
  * Reset cursor and topline to its stored values from check_lnums().
  * check_lnums() must have been called first!