Xavier de Gaye wrote:

> After deleting the last line of a buffer, the cursor lnum position is
> invalid and causes the following error message, if a key happens to be
> pressed while Vim has passed control to the gui:
> 
>     "E315: ml_get: invalid lnum: 2"
> 
> See below the test case description and the corresponding gdb
> backtrace. This has been tested with Vim 7.2.
> 
> The problem does not occur anymore when changing the code in
> misc1.c:del_lines() at line 2365, to fix curwin->w_cursor.lnum:
> 
>         /* If we delete the last line in the file, stop */
>         if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
>         {
>             // 3 lines have been added: fix curwin->w_cursor.lnum
>             curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
>             if (curwin->w_cursor.lnum == 0)
>                 curwin->w_cursor.lnum = 1;
>             break;
>         }
>     }
>     /* adjust marks, mark the buffer as changed and prepare for displaying */
>     deleted_lines_mark(curwin->w_cursor.lnum, n);
> 
> 
> This change does not seem to be the correct one, as
> deleted_lines_mark() expect (as far as I understand) the previous
> value of lnum, not the actual one after the last line is deleted.

The stack trace helped pinpointing the problem.  Adjusting the marks
takes a while, and displaying the cursor is triggered there.  Thus we
need to adjust the cursor position before calling deleted_lines_mark().

This patch should fix it:


*** ../vim-7.2.222/src/misc1.c  2009-06-24 16:25:23.000000000 +0200
--- src/misc1.c 2009-07-09 12:50:24.000000000 +0200
***************
*** 2345,2356 ****
      int               undo;           /* if TRUE, prepare for undo */
  {
      long      n;
  
      if (nlines <= 0)
        return;
  
      /* save the deleted lines for undo */
!     if (undo && u_savedel(curwin->w_cursor.lnum, nlines) == FAIL)
        return;
  
      for (n = 0; n < nlines; )
--- 2345,2357 ----
      int               undo;           /* if TRUE, prepare for undo */
  {
      long      n;
+     linenr_T  first = curwin->w_cursor.lnum;
  
      if (nlines <= 0)
        return;
  
      /* save the deleted lines for undo */
!     if (undo && u_savedel(first, nlines) == FAIL)
        return;
  
      for (n = 0; n < nlines; )
***************
*** 2358,2375 ****
        if (curbuf->b_ml.ml_flags & ML_EMPTY)       /* nothing to delete */
            break;
  
!       ml_delete(curwin->w_cursor.lnum, TRUE);
        ++n;
  
        /* If we delete the last line in the file, stop */
!       if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
            break;
      }
-     /* adjust marks, mark the buffer as changed and prepare for displaying */
-     deleted_lines_mark(curwin->w_cursor.lnum, n);
  
      curwin->w_cursor.col = 0;
      check_cursor_lnum();
  }
  
      int
--- 2359,2379 ----
        if (curbuf->b_ml.ml_flags & ML_EMPTY)       /* nothing to delete */
            break;
  
!       ml_delete(first, TRUE);
        ++n;
  
        /* If we delete the last line in the file, stop */
!       if (first > curbuf->b_ml.ml_line_count)
            break;
      }
  
+     /* Correct the cursor position before calling deleted_lines_mark(), it may
+      * trigger a callback to display the cursor. */
      curwin->w_cursor.col = 0;
      check_cursor_lnum();
+ 
+     /* adjust marks, mark the buffer as changed and prepare for displaying */
+     deleted_lines_mark(first, n);
  }
  
      int


-- 
hundred-and-one symptoms of being an internet addict:
72. Somebody at IRC just mentioned a way to obtain full motion video without
    a PC using a wireless protocol called NTSC, you wonder how you never
    heard about it

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Raspunde prin e-mail lui