Patch 9.0.0061
Problem:    ml_get error with nested autocommand.
Solution:   Also check line numbers for a nested autocommand. (closes #10761)
Files:      src/window.c, src/proto/window.pro, src/autocmd.c,
            src/testdir/test_autocmd.vim


*** ../vim-9.0.0060/src/window.c        2022-07-01 15:26:09.294541275 +0100
--- src/window.c        2022-07-23 09:03:37.532879149 +0100
***************
*** 6770,6781 ****
  }
  
  /*
!  * 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)
  {
      win_T     *wp;
      tabpage_T *tp;
--- 6770,6779 ----
  }
  
  /*
!  * Implementation of check_lnums() and check_lnums_nested().
   */
!     static void
! check_lnums_both(int do_curwin, int nested)
  {
      win_T     *wp;
      tabpage_T *tp;
***************
*** 6783,6804 ****
      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 (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
            wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
            wp->w_save_cursor.w_topline_corr = wp->w_topline;
        }
  }
  
  /*
   * Reset cursor and topline to its stored values from check_lnums().
   * check_lnums() must have been called first!
   */
--- 6781,6825 ----
      FOR_ALL_TAB_WINDOWS(tp, wp)
        if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
        {
!           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
            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!
   */
*** ../vim-9.0.0060/src/proto/window.pro        2022-06-27 23:15:31.000000000 
+0100
--- src/proto/window.pro        2022-07-23 09:03:41.724881565 +0100
***************
*** 77,82 ****
--- 77,83 ----
  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);
*** ../vim-9.0.0060/src/autocmd.c       2022-06-14 13:41:08.000000000 +0100
--- src/autocmd.c       2022-07-23 09:03:20.104869054 +0100
***************
*** 2209,2217 ****
            ap->last = FALSE;
        ap->last = TRUE;
  
        if (nesting == 1)
-           // make sure cursor and topline are valid
            check_lnums(TRUE);
  
        save_did_emsg = did_emsg;
  
--- 2209,2221 ----
            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)
            check_lnums(TRUE);
+       else
+           check_lnums_nested(TRUE);
  
        save_did_emsg = did_emsg;
  
*** ../vim-9.0.0060/src/testdir/test_autocmd.vim        2022-06-12 
23:23:27.000000000 +0100
--- src/testdir/test_autocmd.vim        2022-07-23 09:01:06.532787863 +0100
***************
*** 2301,2306 ****
--- 2301,2325 ----
    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
*** ../vim-9.0.0060/src/version.c       2022-07-23 06:53:01.097648234 +0100
--- src/version.c       2022-07-23 08:57:37.912643929 +0100
***************
*** 737,738 ****
--- 737,740 ----
  {   /* Add new patch number below this line */
+ /**/
+     61,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
100. The most exciting sporting events you noticed during summer 1996
    was Netscape vs. Microsoft.

 /// Bram Moolenaar -- b...@moolenaar.net -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20220723080738.507F51C0909%40moolenaar.net.

Raspunde prin e-mail lui