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