ad84649
To: vim-dev@vim.org
ad84649
Subject: Patch 7.1.207
ad84649
Fcc: outbox
ad84649
From: Bram Moolenaar <Bram@moolenaar.net>
ad84649
Mime-Version: 1.0
ad84649
Content-Type: text/plain; charset=ISO-8859-1
ad84649
Content-Transfer-Encoding: 8bit
ad84649
------------
ad84649
ad84649
Patch 7.1.207
ad84649
Problem:    Netbeans: "remove" cannot delete one line. 
ad84649
Solution:   Remove partial lines and whole lines properly.  Avoid a memory
ad84649
	    leak.  (Xavier de Gaye)
ad84649
Files:	    src/netbeans.c
ad84649
ad84649
ad84649
*** ../vim-7.1.206/src/netbeans.c	Thu May 10 18:40:48 2007
ad84649
--- src/netbeans.c	Sat Jan  5 18:03:24 2008
ad84649
***************
ad84649
*** 1204,1209 ****
ad84649
--- 1204,1257 ----
ad84649
      return result;
ad84649
  }
ad84649
  
ad84649
+ /*
ad84649
+  * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
ad84649
+  * current buffer.  Remove to end of line when "last" is MAXCOL.
ad84649
+  */
ad84649
+     static void
ad84649
+ nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
ad84649
+ {
ad84649
+     char_u *oldtext, *newtext;
ad84649
+     int oldlen;
ad84649
+     int lastbyte = last;
ad84649
+ 
ad84649
+     oldtext = ml_get(lnum);
ad84649
+     oldlen = STRLEN(oldtext);
ad84649
+     if (first >= oldlen || oldlen == 0)  /* just in case */
ad84649
+ 	return;
ad84649
+     if (lastbyte >= oldlen)
ad84649
+ 	lastbyte = oldlen - 1;
ad84649
+     newtext = alloc(oldlen - (int)(lastbyte - first));
ad84649
+     if (newtext != NULL)
ad84649
+     {
ad84649
+ 	mch_memmove(newtext, oldtext, first);
ad84649
+ 	mch_memmove(newtext + first, oldtext + lastbyte + 1, STRLEN(oldtext + lastbyte + 1) + 1);
ad84649
+ 	nbdebug(("    NEW LINE %d: %s\n", lnum, newtext));
ad84649
+ 	ml_replace(lnum, newtext, FALSE);
ad84649
+     }
ad84649
+ }
ad84649
+ 
ad84649
+ /*
ad84649
+  * Replace the "first" line with the concatenation of the "first" and
ad84649
+  * the "other" line. The "other" line is not removed.
ad84649
+  */
ad84649
+     static void
ad84649
+ nb_joinlines(linenr_T first, linenr_T other)
ad84649
+ {
ad84649
+     int len_first, len_other;
ad84649
+     char_u *p;
ad84649
+ 
ad84649
+     len_first = STRLEN(ml_get(first));
ad84649
+     len_other = STRLEN(ml_get(other));
ad84649
+     p = alloc((unsigned)(len_first + len_other + 1));
ad84649
+     if (p != NULL)
ad84649
+     {
ad84649
+       mch_memmove(p, ml_get(first), len_first);
ad84649
+       mch_memmove(p + len_first, ml_get(other), len_other + 1);
ad84649
+       ml_replace(first, p, FALSE);
ad84649
+     }
ad84649
+ }
ad84649
+ 
ad84649
  #define SKIP_STOP 2
ad84649
  #define streq(a,b) (strcmp(a,b) == 0)
ad84649
  static int needupdate = 0;
ad84649
***************
ad84649
*** 1371,1376 ****
ad84649
--- 1419,1426 ----
ad84649
  	    long count;
ad84649
  	    pos_T first, last;
ad84649
  	    pos_T *pos;
ad84649
+ 	    pos_T *next;
ad84649
+ 	    linenr_T del_from_lnum, del_to_lnum;  /* lines to be deleted as a whole */
ad84649
  	    int oldFire = netbeansFireChanges;
ad84649
  	    int oldSuppress = netbeansSuppressNoLines;
ad84649
  	    int wasChanged;
ad84649
***************
ad84649
*** 1420,1444 ****
ad84649
  		}
ad84649
  		last = *pos;
ad84649
  		nbdebug(("    LAST POS: line %d, col %d\n", last.lnum, last.col));
ad84649
! 		curwin->w_cursor = first;
ad84649
  		doupdate = 1;
ad84649
  
ad84649
! 		/* keep part of first line */
ad84649
! 		if (first.lnum == last.lnum && first.col != last.col)
ad84649
  		{
ad84649
! 		    /* deletion is within one line */
ad84649
! 		    char_u *p = ml_get(first.lnum);
ad84649
! 		    mch_memmove(p + first.col, p + last.col + 1, STRLEN(p + last.col) + 1);
ad84649
! 		    nbdebug(("    NEW LINE %d: %s\n", first.lnum, p));
ad84649
! 		    ml_replace(first.lnum, p, TRUE);
ad84649
  		}
ad84649
  
ad84649
! 		if (first.lnum < last.lnum)
ad84649
  		{
ad84649
  		    int i;
ad84649
  
ad84649
  		    /* delete signs from the lines being deleted */
ad84649
! 		    for (i = first.lnum; i <= last.lnum; i++)
ad84649
  		    {
ad84649
  			int id = buf_findsign_id(buf->bufp, (linenr_T)i);
ad84649
  			if (id > 0)
ad84649
--- 1470,1544 ----
ad84649
  		}
ad84649
  		last = *pos;
ad84649
  		nbdebug(("    LAST POS: line %d, col %d\n", last.lnum, last.col));
ad84649
! 		del_from_lnum = first.lnum;
ad84649
! 		del_to_lnum = last.lnum;
ad84649
  		doupdate = 1;
ad84649
  
ad84649
! 		/* Get the position of the first byte after the deleted
ad84649
! 		 * section.  "next" is NULL when deleting to the end of the
ad84649
! 		 * file. */
ad84649
! 		next = off2pos(buf->bufp, off + count);
ad84649
! 
ad84649
! 		/* Remove part of the first line. */
ad84649
! 		if (first.col != 0 || (next != NULL && first.lnum == next->lnum))
ad84649
  		{
ad84649
! 		    if (first.lnum != last.lnum
ad84649
! 			    || (next != NULL && first.lnum != next->lnum))
ad84649
! 		    {
ad84649
! 			/* remove to the end of the first line */
ad84649
! 			nb_partialremove(first.lnum, first.col,
ad84649
! 							     (colnr_T)MAXCOL);
ad84649
! 			if (first.lnum == last.lnum)
ad84649
! 			{
ad84649
! 			    /* Partial line to remove includes the end of
ad84649
! 			     * line.  Join the line with the next one, have
ad84649
! 			     * the next line deleted below. */
ad84649
! 			    nb_joinlines(first.lnum, next->lnum);
ad84649
! 			    del_to_lnum = next->lnum;
ad84649
! 			}
ad84649
! 		    }
ad84649
! 		    else
ad84649
! 		    {
ad84649
! 			/* remove within one line */
ad84649
! 			nb_partialremove(first.lnum, first.col, last.col);
ad84649
! 		    }
ad84649
! 		    ++del_from_lnum;  /* don't delete the first line */
ad84649
! 		}
ad84649
! 
ad84649
! 		/* Remove part of the last line. */
ad84649
! 		if (first.lnum != last.lnum && next != NULL
ad84649
! 			&& next->col != 0 && last.lnum == next->lnum)
ad84649
! 		{
ad84649
! 		    nb_partialremove(last.lnum, 0, last.col);
ad84649
! 		    if (del_from_lnum > first.lnum)
ad84649
! 		    {
ad84649
! 			/* Join end of last line to start of first line; last
ad84649
! 			 * line is deleted below. */
ad84649
! 			nb_joinlines(first.lnum, last.lnum);
ad84649
! 		    }
ad84649
! 		    else
ad84649
! 			/* First line is deleted as a whole, keep the last
ad84649
! 			 * line. */
ad84649
! 			--del_to_lnum;
ad84649
  		}
ad84649
  
ad84649
! 		/* First is partial line; last line to remove includes
ad84649
! 		 * the end of line; join first line to line following last
ad84649
! 		 * line; line following last line is deleted below. */
ad84649
! 		if (first.lnum != last.lnum && del_from_lnum > first.lnum
ad84649
! 			&& next != NULL && last.lnum != next->lnum)
ad84649
! 		{
ad84649
! 		    nb_joinlines(first.lnum, next->lnum);
ad84649
! 		    del_to_lnum = next->lnum;
ad84649
! 		}
ad84649
! 
ad84649
! 		/* Delete whole lines if there are any. */
ad84649
! 		if (del_to_lnum >= del_from_lnum)
ad84649
  		{
ad84649
  		    int i;
ad84649
  
ad84649
  		    /* delete signs from the lines being deleted */
ad84649
! 		    for (i = del_from_lnum; i <= del_to_lnum; i++)
ad84649
  		    {
ad84649
  			int id = buf_findsign_id(buf->bufp, (linenr_T)i);
ad84649
  			if (id > 0)
ad84649
***************
ad84649
*** 1450,1459 ****
ad84649
  			    nbdebug(("    No sign on line %d\n", i));
ad84649
  		    }
ad84649
  
ad84649
! 		    /* delete whole lines */
ad84649
! 		    nbdebug(("    Deleting lines %d through %d\n", first.lnum, last.lnum));
ad84649
! 		    del_lines(last.lnum - first.lnum + 1, FALSE);
ad84649
  		}
ad84649
  		buf->bufp->b_changed = wasChanged; /* logically unchanged */
ad84649
  		netbeansFireChanges = oldFire;
ad84649
  		netbeansSuppressNoLines = oldSuppress;
ad84649
--- 1550,1564 ----
ad84649
  			    nbdebug(("    No sign on line %d\n", i));
ad84649
  		    }
ad84649
  
ad84649
! 		    nbdebug(("    Deleting lines %d through %d\n", del_from_lnum, del_to_lnum));
ad84649
! 		    curwin->w_cursor.lnum = del_from_lnum;
ad84649
! 		    curwin->w_cursor.col = 0;
ad84649
! 		    del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
ad84649
  		}
ad84649
+ 
ad84649
+ 		/* Leave cursor at first deleted byte. */
ad84649
+ 		curwin->w_cursor = first;
ad84649
+ 		check_cursor_lnum();
ad84649
  		buf->bufp->b_changed = wasChanged; /* logically unchanged */
ad84649
  		netbeansFireChanges = oldFire;
ad84649
  		netbeansSuppressNoLines = oldSuppress;
ad84649
***************
ad84649
*** 2374,2381 ****
ad84649
   * the current buffer as "buf".
ad84649
   */
ad84649
      static void
ad84649
! nb_set_curbuf(buf)
ad84649
!     buf_T *buf;
ad84649
  {
ad84649
      if (curbuf != buf && buf_jump_open_win(buf) == NULL)
ad84649
  	set_curbuf(buf, DOBUF_GOTO);
ad84649
--- 2479,2485 ----
ad84649
   * the current buffer as "buf".
ad84649
   */
ad84649
      static void
ad84649
! nb_set_curbuf(buf_T *buf)
ad84649
  {
ad84649
      if (curbuf != buf && buf_jump_open_win(buf) == NULL)
ad84649
  	set_curbuf(buf, DOBUF_GOTO);
ad84649
*** ../vim-7.1.206/src/version.c	Sat Jan  5 13:58:48 2008
ad84649
--- src/version.c	Sat Jan  5 18:06:04 2008
ad84649
***************
ad84649
*** 668,669 ****
ad84649
--- 668,671 ----
ad84649
  {   /* Add new patch number below this line */
ad84649
+ /**/
ad84649
+     207,
ad84649
  /**/
ad84649
ad84649
-- 
ad84649
Q:   How many hardware engineers does it take to change a lightbulb?
ad84649
A:   None.  We'll fix it in software.
ad84649
ad84649
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
ad84649
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
ad84649
\\\        download, build and distribute -- http://www.A-A-P.org        ///
ad84649
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///