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