diff src/search.c @ 282:c42ec1d1cded r21-0b39

Import from CVS: tag r21-0b39
author cvs
date Mon, 13 Aug 2007 10:33:18 +0200
parents c5d627a313b1
children 57709be46d1b
line wrap: on
line diff
--- a/src/search.c	Mon Aug 13 10:32:23 2007 +0200
+++ b/src/search.c	Mon Aug 13 10:33:18 2007 +0200
@@ -2247,16 +2247,22 @@
   return match_limit (num, 0);
 }
 
-DEFUN ("match-data", Fmatch_data, 0, 0, 0, /*
+DEFUN ("match-data", Fmatch_data, 0, 2, 0, /*
 Return a list containing all info on what the last regexp search matched.
 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
 All the elements are markers or nil (nil if the Nth pair didn't match)
 if the last match was on a buffer; integers or nil if a string was matched.
 Use `store-match-data' to reinstate the data in this list.
+
+If INTEGERS (the optional first argument) is non-nil, always use integers
+\(rather than markers) to represent buffer positions.
+If REUSE is a list, reuse it as part of the value.  If REUSE is long enough
+to hold all the values, and if INTEGERS is non-nil, no consing is done.
 */
-       ())
+       (integers, reuse))
 {
   /* This function has been Mule-ized. */
+  Lisp_Object tail, prev;
   Lisp_Object *data;
   int i;
   Charcount len;
@@ -2273,7 +2279,8 @@
       Bufpos start = search_regs.start[i];
       if (start >= 0)
 	{
-	  if (EQ (last_thing_searched, Qt))
+	  if (EQ (last_thing_searched, Qt)
+	      || !NILP (integers))
 	    {
 	      data[2 * i] = make_int (start);
 	      data[2 * i + 1] = make_int (search_regs.end[i]);
@@ -2298,7 +2305,26 @@
       else
 	data[2 * i] = data [2 * i + 1] = Qnil;
     }
-  return Flist (2 * len + 2, data);
+  if (!CONSP (reuse))
+    return Flist (2 * len + 2, data);
+
+  /* If REUSE is a list, store as many value elements as will fit
+     into the elements of REUSE.  */
+  for (i = 0, tail = reuse; CONSP (tail); i++, tail = XCDR (tail))
+    {
+      if (i < 2 * len + 2)
+	XCAR (tail) = data[i];
+      else
+	XCAR (tail) = Qnil;
+      prev = tail;
+    }
+
+  /* If we couldn't fit all value elements into REUSE,
+     cons up the rest of them and add them to the end of REUSE.  */
+  if (i < 2 * len + 2)
+    XCDR (prev) = Flist (2 * len + 2 - i, data + i);
+
+  return reuse;
 }
 
 
@@ -2427,7 +2453,6 @@
 */
        (str))
 {
-  /* This function has been Mule-ized. */
   REGISTER Bufbyte *in, *out, *end;
   REGISTER Bufbyte *temp;
 
@@ -2441,14 +2466,17 @@
   end = in + XSTRING_LENGTH (str);
   out = temp;
 
-  for (; in != end; in++)
+  while (in < end)
     {
-      if (*in == '[' || *in == ']'
-	  || *in == '*' || *in == '.' || *in == '\\'
-	  || *in == '?' || *in == '+'
-	  || *in == '^' || *in == '$')
+      Emchar c = charptr_emchar (in);
+
+      if (c == '[' || c == ']'
+	  || c == '*' || c == '.' || c == '\\'
+	  || c == '?' || c == '+'
+	  || c == '^' || c == '$')
 	*out++ = '\\';
-      *out++ = *in;
+      out += set_charptr_emchar (out, c);
+      INC_CHARPTR (in);
     }
 
   return make_string (temp, out - temp);