]> code.delx.au - gnu-emacs/blobdiff - src/search.c
(UNEXEC): Definition deleted.
[gnu-emacs] / src / search.c
index a7e27c88ecdde4afbeae03588da3a96315e01d92..f795eb40abe58b194766652eb5fc8960813df465 100644 (file)
@@ -128,16 +128,16 @@ compile_pattern_1 (cp, pattern, translate, regp, posix, multibyte)
 
   if (multibyte == STRING_MULTIBYTE (pattern))
     {
-      raw_pattern = (unsigned char *) XSTRING (pattern)->data;
-      raw_pattern_size = STRING_BYTES (XSTRING (pattern));
+      raw_pattern = (unsigned char *) SDATA (pattern);
+      raw_pattern_size = SBYTES (pattern);
     }
   else if (multibyte)
     {
-      raw_pattern_size = count_size_as_multibyte (XSTRING (pattern)->data,
-                                                 XSTRING (pattern)->size);
+      raw_pattern_size = count_size_as_multibyte (SDATA (pattern),
+                                                 SCHARS (pattern));
       raw_pattern = (unsigned char *) alloca (raw_pattern_size + 1);
-      copy_text (XSTRING (pattern)->data, raw_pattern,
-                XSTRING (pattern)->size, 0, 1);
+      copy_text (SDATA (pattern), raw_pattern,
+                SCHARS (pattern), 0, 1);
     }
   else
     {
@@ -147,10 +147,10 @@ compile_pattern_1 (cp, pattern, translate, regp, posix, multibyte)
         by subtracting nonascii-insert-offset from each non-ASCII char,
         so that only the multibyte chars which really correspond to
         the chosen single-byte character set can possibly match.  */
-      raw_pattern_size = XSTRING (pattern)->size;
+      raw_pattern_size = SCHARS (pattern);
       raw_pattern = (unsigned char *) alloca (raw_pattern_size + 1);
-      copy_text (XSTRING (pattern)->data, raw_pattern,
-                STRING_BYTES (XSTRING (pattern)), 1, 0);
+      copy_text (SDATA (pattern), raw_pattern,
+                SBYTES (pattern), 1, 0);
     }
 
   cp->regexp = Qnil;
@@ -212,12 +212,12 @@ compile_pattern (pattern, regp, translate, posix, multibyte)
       cp = *cpp;
       /* Entries are initialized to nil, and may be set to nil by
         compile_pattern_1 if the pattern isn't valid.  Don't apply
-        XSTRING in those cases.  However, compile_pattern_1 is only
-        applied to the cache entry we pick here to reuse.  So nil
-        should never appear before a non-nil entry.  */
+        string accessors in those cases.  However, compile_pattern_1
+        is only applied to the cache entry we pick here to reuse.  So
+        nil should never appear before a non-nil entry.  */
       if (NILP (cp->regexp))
        goto compile_it;
-      if (XSTRING (cp->regexp)->size == XSTRING (pattern)->size
+      if (SCHARS (cp->regexp) == SCHARS (pattern)
          && STRING_MULTIBYTE (cp->regexp) == STRING_MULTIBYTE (pattern)
          && !NILP (Fstring_equal (cp->regexp, pattern))
          && EQ (cp->buf.translate, (! NILP (translate) ? translate : make_number (0)))
@@ -372,7 +372,7 @@ string_match_1 (regexp, string, start, posix)
     pos = 0, pos_byte = 0;
   else
     {
-      int len = XSTRING (string)->size;
+      int len = SCHARS (string);
 
       CHECK_NUMBER (start);
       pos = XINT (start);
@@ -391,9 +391,9 @@ string_match_1 (regexp, string, start, posix)
   immediate_quit = 1;
   re_match_object = string;
   
-  val = re_search (bufp, (char *) XSTRING (string)->data,
-                  STRING_BYTES (XSTRING (string)), pos_byte,
-                  STRING_BYTES (XSTRING (string)) - pos_byte,
+  val = re_search (bufp, (char *) SDATA (string),
+                  SBYTES (string), pos_byte,
+                  SBYTES (string) - pos_byte,
                   &search_regs);
   immediate_quit = 0;
   last_thing_searched = Qt;
@@ -456,9 +456,9 @@ fast_string_match (regexp, string)
   immediate_quit = 1;
   re_match_object = string;
   
-  val = re_search (bufp, (char *) XSTRING (string)->data,
-                  STRING_BYTES (XSTRING (string)), 0,
-                  STRING_BYTES (XSTRING (string)), 0);
+  val = re_search (bufp, (char *) SDATA (string),
+                  SBYTES (string), 0,
+                  SBYTES (string), 0);
   immediate_quit = 0;
   return val;
 }
@@ -473,7 +473,7 @@ extern Lisp_Object Vascii_downcase_table;
 int
 fast_c_string_match_ignore_case (regexp, string)
      Lisp_Object regexp;
-     char *string;
+     const char *string;
 {
   int val;
   struct re_pattern_buffer *bufp;
@@ -939,8 +939,8 @@ static int
 trivial_regexp_p (regexp)
      Lisp_Object regexp;
 {
-  int len = STRING_BYTES (XSTRING (regexp));
-  unsigned char *s = XSTRING (regexp)->data;
+  int len = SBYTES (regexp);
+  unsigned char *s = SDATA (regexp);
   while (--len >= 0)
     {
       switch (*s++)
@@ -1011,8 +1011,8 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
      Lisp_Object inverse_trt;
      int posix;
 {
-  int len = XSTRING (string)->size;
-  int len_byte = STRING_BYTES (XSTRING (string));
+  int len = SCHARS (string);
+  int len_byte = SBYTES (string);
   register int i;
 
   if (running_asynch_code)
@@ -1136,7 +1136,7 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
       int raw_pattern_size_byte;
       unsigned char *patbuf;
       int multibyte = !NILP (current_buffer->enable_multibyte_characters);
-      unsigned char *base_pat = XSTRING (string)->data;
+      unsigned char *base_pat = SDATA (string);
       int charset_base = -1;
       int boyer_moore_ok = 1;
 
@@ -1146,19 +1146,19 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
 
       if (multibyte == STRING_MULTIBYTE (string))
        {
-         raw_pattern = (unsigned char *) XSTRING (string)->data;
-         raw_pattern_size = XSTRING (string)->size;
-         raw_pattern_size_byte = STRING_BYTES (XSTRING (string));
+         raw_pattern = (unsigned char *) SDATA (string);
+         raw_pattern_size = SCHARS (string);
+         raw_pattern_size_byte = SBYTES (string);
        }
       else if (multibyte)
        {
-         raw_pattern_size = XSTRING (string)->size;
+         raw_pattern_size = SCHARS (string);
          raw_pattern_size_byte
-           = count_size_as_multibyte (XSTRING (string)->data,
+           = count_size_as_multibyte (SDATA (string),
                                       raw_pattern_size);
          raw_pattern = (unsigned char *) alloca (raw_pattern_size_byte + 1);
-         copy_text (XSTRING (string)->data, raw_pattern,
-                    XSTRING (string)->size, 0, 1);
+         copy_text (SDATA (string), raw_pattern,
+                    SCHARS (string), 0, 1);
        }
       else
        {
@@ -1168,11 +1168,11 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
             by subtracting nonascii-insert-offset from each non-ASCII char,
             so that only the multibyte chars which really correspond to
             the chosen single-byte character set can possibly match.  */
-         raw_pattern_size = XSTRING (string)->size;
-         raw_pattern_size_byte = XSTRING (string)->size;
+         raw_pattern_size = SCHARS (string);
+         raw_pattern_size_byte = SCHARS (string);
          raw_pattern = (unsigned char *) alloca (raw_pattern_size + 1);
-         copy_text (XSTRING (string)->data, raw_pattern,
-                    STRING_BYTES (XSTRING (string)), 1, 0);
+         copy_text (SDATA (string), raw_pattern,
+                    SBYTES (string), 1, 0);
        }
 
       /* Copy and optionally translate the pattern.  */
@@ -1229,7 +1229,11 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
                  /* Keep track of which character set row
                     contains the characters that need translation.  */
                  int charset_base_code = c & ~CHAR_FIELD3_MASK;
-                 if (charset_base == -1)
+                 int inverse_charset_base = inverse & ~CHAR_FIELD3_MASK;
+
+                 if (charset_base_code != inverse_charset_base)
+                   boyer_moore_ok = 0;
+                 else if (charset_base == -1)
                    charset_base = charset_base_code;
                  else if (charset_base != charset_base_code)
                    /* If two different rows appear, needing translation,
@@ -1944,8 +1948,8 @@ wordify (string)
   int adjust;
 
   CHECK_STRING (string);
-  p = XSTRING (string)->data;
-  len = XSTRING (string)->size;
+  p = SDATA (string);
+  len = SCHARS (string);
 
   for (i = 0, i_byte = 0; i < len; )
     {
@@ -1971,12 +1975,12 @@ wordify (string)
   adjust = - punct_count + 5 * (word_count - 1) + 4;
   if (STRING_MULTIBYTE (string))
     val = make_uninit_multibyte_string (len + adjust,
-                                       STRING_BYTES (XSTRING (string))
+                                       SBYTES (string)
                                        + adjust);
   else
     val = make_uninit_string (len + adjust);
 
-  o = XSTRING (val)->data;
+  o = SDATA (val);
   *o++ = '\\';
   *o++ = 'b';
   prev_c = 0;
@@ -1990,7 +1994,7 @@ wordify (string)
 
       if (SYNTAX (c) == Sword)
        {
-         bcopy (&XSTRING (string)->data[i_byte_orig], o,
+         bcopy (SDATA (string) + i_byte_orig, o,
                 i_byte - i_byte_orig);
          o += i_byte - i_byte_orig;
        }
@@ -2158,13 +2162,15 @@ and `replace-match'.  */)
 \f
 DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 5, 0,
        doc: /* Replace text matched by last search with NEWTEXT.
+Leave point at the end of the replacement text.
+
 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.
 Otherwise maybe capitalize the whole text, or maybe just word initials,
 based on the replaced text.
 If the replaced text has only capital letters
 and has at least one multiletter word, convert NEWTEXT to all caps.
-If the replaced text has at least one word starting with a capital letter,
-then capitalize each word in NEWTEXT.
+Otherwise if all words are capitalized in the replaced text,
+capitalize each word in NEWTEXT.
 
 If third arg LITERAL is non-nil, insert NEWTEXT literally.
 Otherwise treat `\\' as special:
@@ -2172,8 +2178,9 @@ Otherwise treat `\\' as special:
   `\\N' means substitute what matched the Nth `\\(...\\)'.
        If Nth parens didn't match, substitute nothing.
   `\\\\' means insert one `\\'.
+Case conversion does not apply to these substitutions.
+
 FIXEDCASE and LITERAL are optional arguments.
-Leaves point at end of replacement text.
 
 The optional fourth argument STRING can be a string to modify.
 This is meaningful when the previous match was done against STRING,
@@ -2199,7 +2206,6 @@ since only regular expressions have distinguished subexpressions.  */)
   int some_uppercase;
   int some_nonuppercase_initial;
   register int c, prevc;
-  int inslen;
   int sub;
   int opoint, newpoint;
 
@@ -2236,7 +2242,7 @@ since only regular expressions have distinguished subexpressions.  */)
     {
       if (search_regs.start[sub] < 0
          || search_regs.start[sub] > search_regs.end[sub]
-         || search_regs.end[sub] > XSTRING (string)->size)
+         || search_regs.end[sub] > SCHARS (string))
        args_out_of_range (make_number (search_regs.start[sub]),
                           make_number (search_regs.end[sub]));
     }
@@ -2336,7 +2342,7 @@ since only regular expressions have distinguished subexpressions.  */)
          /* We build up the substituted string in ACCUM.  */
          Lisp_Object accum;
          Lisp_Object middle;
-         int length = STRING_BYTES (XSTRING (newtext));
+         int length = SBYTES (newtext);
 
          accum = Qnil;
 
@@ -2424,22 +2430,17 @@ since only regular expressions have distinguished subexpressions.  */)
   else
     opoint = PT;
 
-  TEMP_SET_PT (search_regs.start[sub]);
-
-  /* We insert the replacement text before the old text, and then
-     delete the original text.  This means that markers at the
-     beginning or end of the original will float to the corresponding
-     position in the replacement.  */
-  if (!NILP (literal))
-    Finsert_and_inherit (1, &newtext);
-  else
+  /* If we want non-literal replacement,
+     perform substitution on the replacement string.  */
+  if (NILP (literal))
     {
-      int length = STRING_BYTES (XSTRING (newtext));
+      int length = SBYTES (newtext);
       unsigned char *substed;
       int substed_alloc_size, substed_len;
       int buf_multibyte = !NILP (current_buffer->enable_multibyte_characters);
       int str_multibyte = STRING_MULTIBYTE (newtext);
       Lisp_Object rev_tbl;
+      int really_changed = 0;
 
       rev_tbl= (!buf_multibyte && CHAR_TABLE_P (Vnonascii_translation_table)
                ? Fchar_table_extra_slot (Vnonascii_translation_table,
@@ -2470,7 +2471,7 @@ since only regular expressions have distinguished subexpressions.  */)
          else
            {
              /* Note that we don't have to increment POS.  */
-             c = XSTRING (newtext)->data[pos_byte++];
+             c = SREF (newtext, pos_byte++);
              if (buf_multibyte)
                c = unibyte_char_to_multibyte (c);
            }
@@ -2481,6 +2482,8 @@ since only regular expressions have distinguished subexpressions.  */)
 
          if (c == '\\')
            {
+             really_changed = 1;
+
              if (str_multibyte)
                {
                  FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, newtext,
@@ -2490,7 +2493,7 @@ since only regular expressions have distinguished subexpressions.  */)
                }
              else
                {
-                 c = XSTRING (newtext)->data[pos_byte++];
+                 c = SREF (newtext, pos_byte++);
                  if (buf_multibyte)
                    c = unibyte_char_to_multibyte (c);
                }
@@ -2546,21 +2549,23 @@ since only regular expressions have distinguished subexpressions.  */)
            }
        }
 
-      /* Now insert what we accumulated.  */
-      insert_and_inherit (substed, substed_len);
+      if (really_changed)
+       newtext = make_string (substed, substed_len);
 
       xfree (substed);
     }
 
-  inslen = PT - (search_regs.start[sub]);
-  del_range (search_regs.start[sub] + inslen, search_regs.end[sub] + inslen);
+  /* Replace the old text with the new in the cleanest possible way.  */
+  replace_range (search_regs.start[sub], search_regs.end[sub],
+                newtext, 1, 0, 1);
+  newpoint = search_regs.start[sub] + SCHARS (newtext);
 
   if (case_action == all_caps)
-    Fupcase_region (make_number (PT - inslen), make_number (PT));
+    Fupcase_region (make_number (search_regs.start[sub]),
+                   make_number (newpoint));
   else if (case_action == cap_initial)
-    Fupcase_initials_region (make_number (PT - inslen), make_number (PT));
-
-  newpoint = PT;
+    Fupcase_initials_region (make_number (search_regs.start[sub]),
+                            make_number (newpoint));
 
   /* Put point back where it was in the text.  */
   if (opoint <= 0)
@@ -2844,12 +2849,12 @@ DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0,
 
   CHECK_STRING (string);
 
-  temp = (unsigned char *) alloca (STRING_BYTES (XSTRING (string)) * 2);
+  temp = (unsigned char *) alloca (SBYTES (string) * 2);
 
   /* Now copy the data into the new string, inserting escapes. */
 
-  in = XSTRING (string)->data;
-  end = in + STRING_BYTES (XSTRING (string));
+  in = SDATA (string);
+  end = in + SBYTES (string);
   out = temp; 
 
   for (; in != end; in++)
@@ -2863,7 +2868,7 @@ DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0,
     }
 
   return make_specified_string (temp,
-                               XSTRING (string)->size + backslashes_added,
+                               SCHARS (string) + backslashes_added,
                                out - temp,
                                STRING_MULTIBYTE (string));
 }