410e2f9
To: vim-dev@vim.org
410e2f9
Subject: Patch 7.1.214
410e2f9
Fcc: outbox
410e2f9
From: Bram Moolenaar <Bram@moolenaar.net>
410e2f9
Mime-Version: 1.0
410e2f9
Content-Type: text/plain; charset=ISO-8859-1
410e2f9
Content-Transfer-Encoding: 8bit
410e2f9
------------
410e2f9
410e2f9
Patch 7.1.214
410e2f9
Problem:    ":1s/g\n\zs1//" deletes characters from the first line. (A Politz)
410e2f9
Solution:   Start replacing in the line where the match starts.
410e2f9
Files:	    src/ex_cmds.c
410e2f9
410e2f9
410e2f9
*** ../vim-7.1.213/src/ex_cmds.c	Fri Jan  4 14:52:14 2008
410e2f9
--- src/ex_cmds.c	Wed Jan  9 22:32:26 2008
410e2f9
***************
410e2f9
*** 4200,4206 ****
410e2f9
      linenr_T	old_line_count = curbuf->b_ml.ml_line_count;
410e2f9
      linenr_T	line2;
410e2f9
      long	nmatch;			/* number of lines in match */
410e2f9
-     linenr_T	sub_firstlnum;		/* nr of first sub line */
410e2f9
      char_u	*sub_firstline;		/* allocated copy of first sub line */
410e2f9
      int		endcolumn = FALSE;	/* cursor in last column when done */
410e2f9
      pos_T	old_cursor = curwin->w_cursor;
410e2f9
--- 4200,4205 ----
410e2f9
***************
410e2f9
*** 4447,4453 ****
410e2f9
  #endif
410e2f9
  		); ++lnum)
410e2f9
      {
410e2f9
- 	sub_firstlnum = lnum;
410e2f9
  	nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0);
410e2f9
  	if (nmatch)
410e2f9
  	{
410e2f9
--- 4446,4451 ----
410e2f9
***************
410e2f9
*** 4463,4468 ****
410e2f9
--- 4461,4467 ----
410e2f9
  	    long	nmatch_tl = 0;	/* nr of lines matched below lnum */
410e2f9
  	    int		do_again;	/* do it again after joining lines */
410e2f9
  	    int		skip_match = FALSE;
410e2f9
+ 	    linenr_T	sub_firstlnum;	/* nr of first sub line */
410e2f9
  
410e2f9
  	    /*
410e2f9
  	     * The new text is build up step by step, to avoid too much
410e2f9
***************
410e2f9
*** 4482,4489 ****
410e2f9
  	     *			far.
410e2f9
  	     * new_end		The new text, where to append new text.
410e2f9
  	     *
410e2f9
! 	     * lnum		The line number where we were looking for the
410e2f9
! 	     *			first match in the old line.
410e2f9
  	     * sub_firstlnum	The line number in the buffer where to look
410e2f9
  	     *			for a match.  Can be different from "lnum"
410e2f9
  	     *			when the pattern or substitute string contains
410e2f9
--- 4481,4490 ----
410e2f9
  	     *			far.
410e2f9
  	     * new_end		The new text, where to append new text.
410e2f9
  	     *
410e2f9
! 	     * lnum		The line number where we found the start of
410e2f9
! 	     *			the match.  Can be below the line we searched
410e2f9
! 	     *			when there is a \n before a \zs in the
410e2f9
! 	     *			pattern.
410e2f9
  	     * sub_firstlnum	The line number in the buffer where to look
410e2f9
  	     *			for a match.  Can be different from "lnum"
410e2f9
  	     *			when the pattern or substitute string contains
410e2f9
***************
410e2f9
*** 4507,4518 ****
410e2f9
  	     * updating the screen or handling a multi-line match.  The "old_"
410e2f9
  	     * pointers point into this copy.
410e2f9
  	     */
410e2f9
! 	    sub_firstline = vim_strsave(ml_get(sub_firstlnum));
410e2f9
! 	    if (sub_firstline == NULL)
410e2f9
! 	    {
410e2f9
! 		vim_free(new_start);
410e2f9
! 		goto outofmem;
410e2f9
! 	    }
410e2f9
  	    copycol = 0;
410e2f9
  	    matchcol = 0;
410e2f9
  
410e2f9
--- 4508,4514 ----
410e2f9
  	     * updating the screen or handling a multi-line match.  The "old_"
410e2f9
  	     * pointers point into this copy.
410e2f9
  	     */
410e2f9
! 	    sub_firstlnum = lnum;
410e2f9
  	    copycol = 0;
410e2f9
  	    matchcol = 0;
410e2f9
  
410e2f9
***************
410e2f9
*** 4533,4538 ****
410e2f9
--- 4529,4556 ----
410e2f9
  	     */
410e2f9
  	    for (;;)
410e2f9
  	    {
410e2f9
+ 		/* Advance "lnum" to the line where the match starts.  The
410e2f9
+ 		 * match does not start in the first line when there is a line
410e2f9
+ 		 * break before \zs. */
410e2f9
+ 		if (regmatch.startpos[0].lnum > 0)
410e2f9
+ 		{
410e2f9
+ 		    lnum += regmatch.startpos[0].lnum;
410e2f9
+ 		    sub_firstlnum += regmatch.startpos[0].lnum;
410e2f9
+ 		    nmatch -= regmatch.startpos[0].lnum;
410e2f9
+ 		    vim_free(sub_firstline);
410e2f9
+ 		    sub_firstline = NULL;
410e2f9
+ 		}
410e2f9
+ 
410e2f9
+ 		if (sub_firstline == NULL)
410e2f9
+ 		{
410e2f9
+ 		    sub_firstline = vim_strsave(ml_get(sub_firstlnum));
410e2f9
+ 		    if (sub_firstline == NULL)
410e2f9
+ 		    {
410e2f9
+ 			vim_free(new_start);
410e2f9
+ 			goto outofmem;
410e2f9
+ 		    }
410e2f9
+ 		}
410e2f9
+ 
410e2f9
  		/* Save the line number of the last change for the final
410e2f9
  		 * cursor position (just like Vi). */
410e2f9
  		curwin->w_cursor.lnum = lnum;
410e2f9
***************
410e2f9
*** 4638,4644 ****
410e2f9
  			    temp = RedrawingDisabled;
410e2f9
  			    RedrawingDisabled = 0;
410e2f9
  
410e2f9
! 			    search_match_lines = regmatch.endpos[0].lnum;
410e2f9
  			    search_match_endcol = regmatch.endpos[0].col;
410e2f9
  			    highlight_match = TRUE;
410e2f9
  
410e2f9
--- 4656,4663 ----
410e2f9
  			    temp = RedrawingDisabled;
410e2f9
  			    RedrawingDisabled = 0;
410e2f9
  
410e2f9
! 			    search_match_lines = regmatch.endpos[0].lnum
410e2f9
! 						  - regmatch.startpos[0].lnum;
410e2f9
  			    search_match_endcol = regmatch.endpos[0].col;
410e2f9
  			    highlight_match = TRUE;
410e2f9
  
410e2f9
***************
410e2f9
*** 4749,4755 ****
410e2f9
  		 * 3. substitute the string.
410e2f9
  		 */
410e2f9
  		/* get length of substitution part */
410e2f9
! 		sublen = vim_regsub_multi(&regmatch, sub_firstlnum,
410e2f9
  				    sub, sub_firstline, FALSE, p_magic, TRUE);
410e2f9
  
410e2f9
  		/* When the match included the "$" of the last line it may
410e2f9
--- 4768,4775 ----
410e2f9
  		 * 3. substitute the string.
410e2f9
  		 */
410e2f9
  		/* get length of substitution part */
410e2f9
! 		sublen = vim_regsub_multi(&regmatch,
410e2f9
! 				    sub_firstlnum - regmatch.startpos[0].lnum,
410e2f9
  				    sub, sub_firstline, FALSE, p_magic, TRUE);
410e2f9
  
410e2f9
  		/* When the match included the "$" of the last line it may
410e2f9
***************
410e2f9
*** 4819,4825 ****
410e2f9
  		mch_memmove(new_end, sub_firstline + copycol, (size_t)i);
410e2f9
  		new_end += i;
410e2f9
  
410e2f9
! 		(void)vim_regsub_multi(&regmatch, sub_firstlnum,
410e2f9
  					   sub, new_end, TRUE, p_magic, TRUE);
410e2f9
  		sub_nsubs++;
410e2f9
  		did_sub = TRUE;
410e2f9
--- 4839,4846 ----
410e2f9
  		mch_memmove(new_end, sub_firstline + copycol, (size_t)i);
410e2f9
  		new_end += i;
410e2f9
  
410e2f9
! 		(void)vim_regsub_multi(&regmatch,
410e2f9
! 				    sub_firstlnum - regmatch.startpos[0].lnum,
410e2f9
  					   sub, new_end, TRUE, p_magic, TRUE);
410e2f9
  		sub_nsubs++;
410e2f9
  		did_sub = TRUE;
410e2f9
***************
410e2f9
*** 4908,4917 ****
410e2f9
  skip:
410e2f9
  		/* We already know that we did the last subst when we are at
410e2f9
  		 * the end of the line, except that a pattern like
410e2f9
! 		 * "bar\|\nfoo" may match at the NUL. */
410e2f9
  		lastone = (skip_match
410e2f9
  			|| got_int
410e2f9
  			|| got_quit
410e2f9
  			|| !(do_all || do_again)
410e2f9
  			|| (sub_firstline[matchcol] == NUL && nmatch <= 1
410e2f9
  					 && !re_multiline(regmatch.regprog)));
410e2f9
--- 4929,4941 ----
410e2f9
  skip:
410e2f9
  		/* We already know that we did the last subst when we are at
410e2f9
  		 * the end of the line, except that a pattern like
410e2f9
! 		 * "bar\|\nfoo" may match at the NUL.  "lnum" can be below
410e2f9
! 		 * "line2" when there is a \zs in the pattern after a line
410e2f9
! 		 * break. */
410e2f9
  		lastone = (skip_match
410e2f9
  			|| got_int
410e2f9
  			|| got_quit
410e2f9
+ 			|| lnum > line2
410e2f9
  			|| !(do_all || do_again)
410e2f9
  			|| (sub_firstline[matchcol] == NUL && nmatch <= 1
410e2f9
  					 && !re_multiline(regmatch.regprog)));
410e2f9
***************
410e2f9
*** 4926,4937 ****
410e2f9
  		 * When asking the user we like to show the already replaced
410e2f9
  		 * text, but don't do it when "\<@=" or "\<@!" is used, it
410e2f9
  		 * changes what matches.
410e2f9
  		 */
410e2f9
  		if (lastone
410e2f9
  			|| (do_ask && !re_lookbehind(regmatch.regprog))
410e2f9
  			|| nmatch_tl > 0
410e2f9
  			|| (nmatch = vim_regexec_multi(&regmatch, curwin,
410e2f9
! 				       curbuf, sub_firstlnum, matchcol)) == 0)
410e2f9
  		{
410e2f9
  		    if (new_start != NULL)
410e2f9
  		    {
410e2f9
--- 4950,4964 ----
410e2f9
  		 * When asking the user we like to show the already replaced
410e2f9
  		 * text, but don't do it when "\<@=" or "\<@!" is used, it
410e2f9
  		 * changes what matches.
410e2f9
+ 		 * When the match starts below where we start searching also
410e2f9
+ 		 * need to replace the line first (using \zs after \n).
410e2f9
  		 */
410e2f9
  		if (lastone
410e2f9
  			|| (do_ask && !re_lookbehind(regmatch.regprog))
410e2f9
  			|| nmatch_tl > 0
410e2f9
  			|| (nmatch = vim_regexec_multi(&regmatch, curwin,
410e2f9
! 				       curbuf, sub_firstlnum, matchcol)) == 0
410e2f9
! 			|| regmatch.startpos[0].lnum > 0)
410e2f9
  		{
410e2f9
  		    if (new_start != NULL)
410e2f9
  		    {
410e2f9
***************
410e2f9
*** 5001,5007 ****
410e2f9
--- 5028,5041 ----
410e2f9
  		     * 5. break if there isn't another match in this line
410e2f9
  		     */
410e2f9
  		    if (nmatch <= 0)
410e2f9
+ 		    {
410e2f9
+ 			/* If the match found didn't start where we were
410e2f9
+ 			 * searching, do the next search in the line where we
410e2f9
+ 			 * found the match. */
410e2f9
+ 			if (nmatch == -1)
410e2f9
+ 			    lnum -= regmatch.startpos[0].lnum;
410e2f9
  			break;
410e2f9
+ 		    }
410e2f9
  		}
410e2f9
  
410e2f9
  		line_breakcheck();
410e2f9
*** ../vim-7.1.213/src/version.c	Wed Jan  9 20:29:51 2008
410e2f9
--- src/version.c	Wed Jan  9 22:37:47 2008
410e2f9
***************
410e2f9
*** 668,669 ****
410e2f9
--- 668,671 ----
410e2f9
  {   /* Add new patch number below this line */
410e2f9
+ /**/
410e2f9
+     214,
410e2f9
  /**/
410e2f9
410e2f9
-- 
410e2f9
Q: What's orange and sounds like a parrot?
410e2f9
A: A carrot
410e2f9
410e2f9
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
410e2f9
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
410e2f9
\\\        download, build and distribute -- http://www.A-A-P.org        ///
410e2f9
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///