2102f06
To: vim-dev@vim.org
2102f06
Subject: Patch 7.2.308
2102f06
Fcc: outbox
2102f06
From: Bram Moolenaar <Bram@moolenaar.net>
2102f06
Mime-Version: 1.0
2102f06
Content-Type: text/plain; charset=UTF-8
2102f06
Content-Transfer-Encoding: 8bit
2102f06
------------
2102f06
2102f06
Patch 7.2.308
2102f06
Problem:    When using a regexp in the "\=" expression of a substitute
2102f06
	    command, submatch() returns empty strings for further lines.
2102f06
	    (Clockwork Jam)
2102f06
Solution:   Save and restore the line number and line count when calling
2102f06
	    reg_getline().
2102f06
Files:	    src/regexp.c
2102f06
2102f06
2102f06
*** ../vim-7.2.307/src/regexp.c	2009-11-25 18:21:48.000000000 +0100
2102f06
--- src/regexp.c	2009-11-25 19:45:07.000000000 +0100
2102f06
***************
2102f06
*** 6828,6833 ****
2102f06
--- 6828,6835 ----
2102f06
   * that contains a call to substitute() and submatch(). */
2102f06
  static regmatch_T	*submatch_match;
2102f06
  static regmmatch_T	*submatch_mmatch;
2102f06
+ static linenr_T		submatch_firstlnum;
2102f06
+ static linenr_T		submatch_maxline;
2102f06
  #endif
2102f06
  
2102f06
  #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
2102f06
***************
2102f06
*** 6941,6947 ****
2102f06
  	}
2102f06
  	else
2102f06
  	{
2102f06
- 	    linenr_T	save_reg_maxline;
2102f06
  	    win_T	*save_reg_win;
2102f06
  	    int		save_ireg_ic;
2102f06
  
2102f06
--- 6943,6948 ----
2102f06
***************
2102f06
*** 6953,6959 ****
2102f06
  	     * vim_regexec_multi() can't be called recursively. */
2102f06
  	    submatch_match = reg_match;
2102f06
  	    submatch_mmatch = reg_mmatch;
2102f06
! 	    save_reg_maxline = reg_maxline;
2102f06
  	    save_reg_win = reg_win;
2102f06
  	    save_ireg_ic = ireg_ic;
2102f06
  	    can_f_submatch = TRUE;
2102f06
--- 6954,6961 ----
2102f06
  	     * vim_regexec_multi() can't be called recursively. */
2102f06
  	    submatch_match = reg_match;
2102f06
  	    submatch_mmatch = reg_mmatch;
2102f06
! 	    submatch_firstlnum = reg_firstlnum;
2102f06
! 	    submatch_maxline = reg_maxline;
2102f06
  	    save_reg_win = reg_win;
2102f06
  	    save_ireg_ic = ireg_ic;
2102f06
  	    can_f_submatch = TRUE;
2102f06
***************
2102f06
*** 6976,6982 ****
2102f06
  
2102f06
  	    reg_match = submatch_match;
2102f06
  	    reg_mmatch = submatch_mmatch;
2102f06
! 	    reg_maxline = save_reg_maxline;
2102f06
  	    reg_win = save_reg_win;
2102f06
  	    ireg_ic = save_ireg_ic;
2102f06
  	    can_f_submatch = FALSE;
2102f06
--- 6978,6985 ----
2102f06
  
2102f06
  	    reg_match = submatch_match;
2102f06
  	    reg_mmatch = submatch_mmatch;
2102f06
! 	    reg_firstlnum = submatch_firstlnum;
2102f06
! 	    reg_maxline = submatch_maxline;
2102f06
  	    reg_win = save_reg_win;
2102f06
  	    ireg_ic = save_ireg_ic;
2102f06
  	    can_f_submatch = FALSE;
2102f06
***************
2102f06
*** 7212,7217 ****
2102f06
--- 7215,7243 ----
2102f06
  
2102f06
  #ifdef FEAT_EVAL
2102f06
  /*
2102f06
+  * Call reg_getline() with the line numbers from the submatch.  If a
2102f06
+  * substitute() was used the reg_maxline and other values have been
2102f06
+  * overwritten.
2102f06
+  */
2102f06
+     static char_u *
2102f06
+ reg_getline_submatch(lnum)
2102f06
+     linenr_T	lnum;
2102f06
+ {
2102f06
+     char_u *s;
2102f06
+     linenr_T save_first = reg_firstlnum;
2102f06
+     linenr_T save_max = reg_maxline;
2102f06
+ 
2102f06
+     reg_firstlnum = submatch_firstlnum;
2102f06
+     reg_maxline = submatch_maxline;
2102f06
+ 
2102f06
+     s = reg_getline(lnum);
2102f06
+ 
2102f06
+     reg_firstlnum = save_first;
2102f06
+     reg_maxline = save_max;
2102f06
+     return s;
2102f06
+ }
2102f06
+ 
2102f06
+ /*
2102f06
   * Used for the submatch() function: get the string from the n'th submatch in
2102f06
   * allocated memory.
2102f06
   * Returns NULL when not in a ":s" command and for a non-existing submatch.
2102f06
***************
2102f06
*** 7241,7247 ****
2102f06
  	    if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
2102f06
  		return NULL;
2102f06
  
2102f06
! 	    s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
2102f06
  	    if (s == NULL)  /* anti-crash check, cannot happen? */
2102f06
  		break;
2102f06
  	    if (submatch_mmatch->endpos[no].lnum == lnum)
2102f06
--- 7267,7273 ----
2102f06
  	    if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
2102f06
  		return NULL;
2102f06
  
2102f06
! 	    s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col;
2102f06
  	    if (s == NULL)  /* anti-crash check, cannot happen? */
2102f06
  		break;
2102f06
  	    if (submatch_mmatch->endpos[no].lnum == lnum)
2102f06
***************
2102f06
*** 7267,7273 ****
2102f06
  		++lnum;
2102f06
  		while (lnum < submatch_mmatch->endpos[no].lnum)
2102f06
  		{
2102f06
! 		    s = reg_getline(lnum++);
2102f06
  		    if (round == 2)
2102f06
  			STRCPY(retval + len, s);
2102f06
  		    len += (int)STRLEN(s);
2102f06
--- 7293,7299 ----
2102f06
  		++lnum;
2102f06
  		while (lnum < submatch_mmatch->endpos[no].lnum)
2102f06
  		{
2102f06
! 		    s = reg_getline_submatch(lnum++);
2102f06
  		    if (round == 2)
2102f06
  			STRCPY(retval + len, s);
2102f06
  		    len += (int)STRLEN(s);
2102f06
***************
2102f06
*** 7276,7282 ****
2102f06
  		    ++len;
2102f06
  		}
2102f06
  		if (round == 2)
2102f06
! 		    STRNCPY(retval + len, reg_getline(lnum),
2102f06
  					     submatch_mmatch->endpos[no].col);
2102f06
  		len += submatch_mmatch->endpos[no].col;
2102f06
  		if (round == 2)
2102f06
--- 7302,7308 ----
2102f06
  		    ++len;
2102f06
  		}
2102f06
  		if (round == 2)
2102f06
! 		    STRNCPY(retval + len, reg_getline_submatch(lnum),
2102f06
  					     submatch_mmatch->endpos[no].col);
2102f06
  		len += submatch_mmatch->endpos[no].col;
2102f06
  		if (round == 2)
2102f06
*** ../vim-7.2.307/src/version.c	2009-11-25 18:21:48.000000000 +0100
2102f06
--- src/version.c	2009-11-25 19:50:16.000000000 +0100
2102f06
***************
2102f06
*** 683,684 ****
2102f06
--- 683,686 ----
2102f06
  {   /* Add new patch number below this line */
2102f06
+ /**/
2102f06
+     308,
2102f06
  /**/
2102f06
2102f06
-- 
2102f06
Engineers are always delighted to share wisdom, even in areas in which they
2102f06
have no experience whatsoever.  Their logic provides them with inherent
2102f06
insight into any field of expertise.  This can be a problem when dealing with
2102f06
the illogical people who believe that knowledge can only be derived through
2102f06
experience.
2102f06
				(Scott Adams - The Dilbert principle)
2102f06
2102f06
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
2102f06
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
2102f06
\\\        download, build and distribute -- http://www.A-A-P.org        ///
2102f06
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///