4 From: Bram Moolenaar <Bram@moolenaar.net>
6 Content-Type: text/plain; charset=ISO-8859-1
7 Content-Transfer-Encoding: 8bit
11 Problem: ":1s/g\n\zs1//" deletes characters from the first line. (A Politz)
12 Solution: Start replacing in the line where the match starts.
16 *** ../vim-7.1.213/src/ex_cmds.c Fri Jan 4 14:52:14 2008
17 --- src/ex_cmds.c Wed Jan 9 22:32:26 2008
20 linenr_T old_line_count = curbuf->b_ml.ml_line_count;
22 long nmatch; /* number of lines in match */
23 - linenr_T sub_firstlnum; /* nr of first sub line */
24 char_u *sub_firstline; /* allocated copy of first sub line */
25 int endcolumn = FALSE; /* cursor in last column when done */
26 pos_T old_cursor = curwin->w_cursor;
33 - sub_firstlnum = lnum;
34 nmatch = vim_regexec_multi(®match, curwin, curbuf, lnum, (colnr_T)0);
41 long nmatch_tl = 0; /* nr of lines matched below lnum */
42 int do_again; /* do it again after joining lines */
43 int skip_match = FALSE;
44 + linenr_T sub_firstlnum; /* nr of first sub line */
47 * The new text is build up step by step, to avoid too much
51 * new_end The new text, where to append new text.
53 ! * lnum The line number where we were looking for the
54 ! * first match in the old line.
55 * sub_firstlnum The line number in the buffer where to look
56 * for a match. Can be different from "lnum"
57 * when the pattern or substitute string contains
60 * new_end The new text, where to append new text.
62 ! * lnum The line number where we found the start of
63 ! * the match. Can be below the line we searched
64 ! * when there is a \n before a \zs in the
66 * sub_firstlnum The line number in the buffer where to look
67 * for a match. Can be different from "lnum"
68 * when the pattern or substitute string contains
71 * updating the screen or handling a multi-line match. The "old_"
72 * pointers point into this copy.
74 ! sub_firstline = vim_strsave(ml_get(sub_firstlnum));
75 ! if (sub_firstline == NULL)
77 ! vim_free(new_start);
84 * updating the screen or handling a multi-line match. The "old_"
85 * pointers point into this copy.
87 ! sub_firstlnum = lnum;
97 + /* Advance "lnum" to the line where the match starts. The
98 + * match does not start in the first line when there is a line
99 + * break before \zs. */
100 + if (regmatch.startpos[0].lnum > 0)
102 + lnum += regmatch.startpos[0].lnum;
103 + sub_firstlnum += regmatch.startpos[0].lnum;
104 + nmatch -= regmatch.startpos[0].lnum;
105 + vim_free(sub_firstline);
106 + sub_firstline = NULL;
109 + if (sub_firstline == NULL)
111 + sub_firstline = vim_strsave(ml_get(sub_firstlnum));
112 + if (sub_firstline == NULL)
114 + vim_free(new_start);
119 /* Save the line number of the last change for the final
120 * cursor position (just like Vi). */
121 curwin->w_cursor.lnum = lnum;
124 temp = RedrawingDisabled;
125 RedrawingDisabled = 0;
127 ! search_match_lines = regmatch.endpos[0].lnum;
128 search_match_endcol = regmatch.endpos[0].col;
129 highlight_match = TRUE;
132 temp = RedrawingDisabled;
133 RedrawingDisabled = 0;
135 ! search_match_lines = regmatch.endpos[0].lnum
136 ! - regmatch.startpos[0].lnum;
137 search_match_endcol = regmatch.endpos[0].col;
138 highlight_match = TRUE;
142 * 3. substitute the string.
144 /* get length of substitution part */
145 ! sublen = vim_regsub_multi(®match, sub_firstlnum,
146 sub, sub_firstline, FALSE, p_magic, TRUE);
148 /* When the match included the "$" of the last line it may
150 * 3. substitute the string.
152 /* get length of substitution part */
153 ! sublen = vim_regsub_multi(®match,
154 ! sub_firstlnum - regmatch.startpos[0].lnum,
155 sub, sub_firstline, FALSE, p_magic, TRUE);
157 /* When the match included the "$" of the last line it may
160 mch_memmove(new_end, sub_firstline + copycol, (size_t)i);
163 ! (void)vim_regsub_multi(®match, sub_firstlnum,
164 sub, new_end, TRUE, p_magic, TRUE);
168 mch_memmove(new_end, sub_firstline + copycol, (size_t)i);
171 ! (void)vim_regsub_multi(®match,
172 ! sub_firstlnum - regmatch.startpos[0].lnum,
173 sub, new_end, TRUE, p_magic, TRUE);
179 /* We already know that we did the last subst when we are at
180 * the end of the line, except that a pattern like
181 ! * "bar\|\nfoo" may match at the NUL. */
182 lastone = (skip_match
185 || !(do_all || do_again)
186 || (sub_firstline[matchcol] == NUL && nmatch <= 1
187 && !re_multiline(regmatch.regprog)));
190 /* We already know that we did the last subst when we are at
191 * the end of the line, except that a pattern like
192 ! * "bar\|\nfoo" may match at the NUL. "lnum" can be below
193 ! * "line2" when there is a \zs in the pattern after a line
195 lastone = (skip_match
199 || !(do_all || do_again)
200 || (sub_firstline[matchcol] == NUL && nmatch <= 1
201 && !re_multiline(regmatch.regprog)));
204 * When asking the user we like to show the already replaced
205 * text, but don't do it when "\<@=" or "\<@!" is used, it
206 * changes what matches.
209 || (do_ask && !re_lookbehind(regmatch.regprog))
211 || (nmatch = vim_regexec_multi(®match, curwin,
212 ! curbuf, sub_firstlnum, matchcol)) == 0)
214 if (new_start != NULL)
217 * When asking the user we like to show the already replaced
218 * text, but don't do it when "\<@=" or "\<@!" is used, it
219 * changes what matches.
220 + * When the match starts below where we start searching also
221 + * need to replace the line first (using \zs after \n).
224 || (do_ask && !re_lookbehind(regmatch.regprog))
226 || (nmatch = vim_regexec_multi(®match, curwin,
227 ! curbuf, sub_firstlnum, matchcol)) == 0
228 ! || regmatch.startpos[0].lnum > 0)
230 if (new_start != NULL)
235 * 5. break if there isn't another match in this line
239 + /* If the match found didn't start where we were
240 + * searching, do the next search in the line where we
241 + * found the match. */
243 + lnum -= regmatch.startpos[0].lnum;
249 *** ../vim-7.1.213/src/version.c Wed Jan 9 20:29:51 2008
250 --- src/version.c Wed Jan 9 22:37:47 2008
254 { /* Add new patch number below this line */
260 Q: What's orange and sounds like a parrot?
263 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
264 /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
265 \\\ download, build and distribute -- http://www.A-A-P.org ///
266 \\\ help me help AIDS victims -- http://ICCF-Holland.org ///