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