4 From: Bram Moolenaar <Bram@moolenaar.net>
6 Content-Type: text/plain; charset=ISO-8859-1
7 Content-Transfer-Encoding: 8bit
11 Problem: Netbeans: "remove" cannot delete one line.
12 Solution: Remove partial lines and whole lines properly. Avoid a memory
13 leak. (Xavier de Gaye)
17 *** ../vim-7.1.206/src/netbeans.c Thu May 10 18:40:48 2007
18 --- src/netbeans.c Sat Jan 5 18:03:24 2008
26 + * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
27 + * current buffer. Remove to end of line when "last" is MAXCOL.
30 + nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
32 + char_u *oldtext, *newtext;
34 + int lastbyte = last;
36 + oldtext = ml_get(lnum);
37 + oldlen = STRLEN(oldtext);
38 + if (first >= oldlen || oldlen == 0) /* just in case */
40 + if (lastbyte >= oldlen)
41 + lastbyte = oldlen - 1;
42 + newtext = alloc(oldlen - (int)(lastbyte - first));
43 + if (newtext != NULL)
45 + mch_memmove(newtext, oldtext, first);
46 + mch_memmove(newtext + first, oldtext + lastbyte + 1, STRLEN(oldtext + lastbyte + 1) + 1);
47 + nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
48 + ml_replace(lnum, newtext, FALSE);
53 + * Replace the "first" line with the concatenation of the "first" and
54 + * the "other" line. The "other" line is not removed.
57 + nb_joinlines(linenr_T first, linenr_T other)
59 + int len_first, len_other;
62 + len_first = STRLEN(ml_get(first));
63 + len_other = STRLEN(ml_get(other));
64 + p = alloc((unsigned)(len_first + len_other + 1));
67 + mch_memmove(p, ml_get(first), len_first);
68 + mch_memmove(p + len_first, ml_get(other), len_other + 1);
69 + ml_replace(first, p, FALSE);
74 #define streq(a,b) (strcmp(a,b) == 0)
75 static int needupdate = 0;
83 + linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
84 int oldFire = netbeansFireChanges;
85 int oldSuppress = netbeansSuppressNoLines;
91 nbdebug((" LAST POS: line %d, col %d\n", last.lnum, last.col));
92 ! curwin->w_cursor = first;
95 ! /* keep part of first line */
96 ! if (first.lnum == last.lnum && first.col != last.col)
98 ! /* deletion is within one line */
99 ! char_u *p = ml_get(first.lnum);
100 ! mch_memmove(p + first.col, p + last.col + 1, STRLEN(p + last.col) + 1);
101 ! nbdebug((" NEW LINE %d: %s\n", first.lnum, p));
102 ! ml_replace(first.lnum, p, TRUE);
105 ! if (first.lnum < last.lnum)
109 /* delete signs from the lines being deleted */
110 ! for (i = first.lnum; i <= last.lnum; i++)
112 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
117 nbdebug((" LAST POS: line %d, col %d\n", last.lnum, last.col));
118 ! del_from_lnum = first.lnum;
119 ! del_to_lnum = last.lnum;
122 ! /* Get the position of the first byte after the deleted
123 ! * section. "next" is NULL when deleting to the end of the
125 ! next = off2pos(buf->bufp, off + count);
127 ! /* Remove part of the first line. */
128 ! if (first.col != 0 || (next != NULL && first.lnum == next->lnum))
130 ! if (first.lnum != last.lnum
131 ! || (next != NULL && first.lnum != next->lnum))
133 ! /* remove to the end of the first line */
134 ! nb_partialremove(first.lnum, first.col,
136 ! if (first.lnum == last.lnum)
138 ! /* Partial line to remove includes the end of
139 ! * line. Join the line with the next one, have
140 ! * the next line deleted below. */
141 ! nb_joinlines(first.lnum, next->lnum);
142 ! del_to_lnum = next->lnum;
147 ! /* remove within one line */
148 ! nb_partialremove(first.lnum, first.col, last.col);
150 ! ++del_from_lnum; /* don't delete the first line */
153 ! /* Remove part of the last line. */
154 ! if (first.lnum != last.lnum && next != NULL
155 ! && next->col != 0 && last.lnum == next->lnum)
157 ! nb_partialremove(last.lnum, 0, last.col);
158 ! if (del_from_lnum > first.lnum)
160 ! /* Join end of last line to start of first line; last
161 ! * line is deleted below. */
162 ! nb_joinlines(first.lnum, last.lnum);
165 ! /* First line is deleted as a whole, keep the last
170 ! /* First is partial line; last line to remove includes
171 ! * the end of line; join first line to line following last
172 ! * line; line following last line is deleted below. */
173 ! if (first.lnum != last.lnum && del_from_lnum > first.lnum
174 ! && next != NULL && last.lnum != next->lnum)
176 ! nb_joinlines(first.lnum, next->lnum);
177 ! del_to_lnum = next->lnum;
180 ! /* Delete whole lines if there are any. */
181 ! if (del_to_lnum >= del_from_lnum)
185 /* delete signs from the lines being deleted */
186 ! for (i = del_from_lnum; i <= del_to_lnum; i++)
188 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
192 nbdebug((" No sign on line %d\n", i));
195 ! /* delete whole lines */
196 ! nbdebug((" Deleting lines %d through %d\n", first.lnum, last.lnum));
197 ! del_lines(last.lnum - first.lnum + 1, FALSE);
199 buf->bufp->b_changed = wasChanged; /* logically unchanged */
200 netbeansFireChanges = oldFire;
201 netbeansSuppressNoLines = oldSuppress;
203 nbdebug((" No sign on line %d\n", i));
206 ! nbdebug((" Deleting lines %d through %d\n", del_from_lnum, del_to_lnum));
207 ! curwin->w_cursor.lnum = del_from_lnum;
208 ! curwin->w_cursor.col = 0;
209 ! del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
212 + /* Leave cursor at first deleted byte. */
213 + curwin->w_cursor = first;
214 + check_cursor_lnum();
215 buf->bufp->b_changed = wasChanged; /* logically unchanged */
216 netbeansFireChanges = oldFire;
217 netbeansSuppressNoLines = oldSuppress;
220 * the current buffer as "buf".
226 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
227 set_curbuf(buf, DOBUF_GOTO);
229 * the current buffer as "buf".
232 ! nb_set_curbuf(buf_T *buf)
234 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
235 set_curbuf(buf, DOBUF_GOTO);
236 *** ../vim-7.1.206/src/version.c Sat Jan 5 13:58:48 2008
237 --- src/version.c Sat Jan 5 18:06:04 2008
241 { /* Add new patch number below this line */
247 Q: How many hardware engineers does it take to change a lightbulb?
248 A: None. We'll fix it in software.
250 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
251 /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
252 \\\ download, build and distribute -- http://www.A-A-P.org ///
253 \\\ help me help AIDS victims -- http://ICCF-Holland.org ///