This commit was generated by cvs2svn to compensate for changes in r5670,
[chise/xemacs-chise.git.1] / src / search.c
index 7738cde..82e27ca 100644 (file)
@@ -29,6 +29,7 @@ Boston, MA 02111-1307, USA.  */
 #include "lisp.h"
 
 #include "buffer.h"
+#include "commands.h"
 #include "insdel.h"
 #include "opaque.h"
 #ifdef REGION_CACHE_NEEDS_WORK
@@ -54,10 +55,10 @@ struct regexp_cache {
 };
 
 /* The instances of that struct.  */
-static struct regexp_cache searchbufs[REGEXP_CACHE_SIZE];
+struct regexp_cache searchbufs[REGEXP_CACHE_SIZE];
 
 /* The head of the linked list; points to the most recently used buffer.  */
-static struct regexp_cache *searchbuf_head;
+struct regexp_cache *searchbuf_head;
 
 
 /* Every call to re_match, etc., must pass &search_regs as the regs
@@ -131,7 +132,7 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
                   char *translate, struct re_registers *regp, int posix,
                   Error_behavior errb)
 {
-  const char *val;
+  CONST char *val;
   reg_syntax_t old;
 
   cp->regexp = Qnil;
@@ -139,7 +140,7 @@ compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
   cp->posix = posix;
   old = re_set_syntax (RE_SYNTAX_EMACS
                       | (posix ? 0 : RE_NO_POSIX_BACKTRACKING));
-  val = (const char *)
+  val = (CONST char *)
     re_compile_pattern ((char *) XSTRING_DATA (pattern),
                        XSTRING_LENGTH (pattern), &cp->buf);
   re_set_syntax (old);
@@ -301,7 +302,6 @@ looking_at_1 (Lisp_Object string, struct buffer *buf, int posix)
   s2 = BI_BUF_ZV (buf) - p2;
 
   regex_emacs_buffer = buf;
-  regex_emacs_buffer_p = 1;
   i = re_match_2 (bufp, (char *) BI_BUF_BYTE_ADDRESS (buf, p1),
                  s1, (char *) BI_BUF_BYTE_ADDRESS (buf, p2), s2,
                  BI_BUF_PT (buf) - BI_BUF_BEGV (buf), &search_regs,
@@ -392,7 +392,6 @@ string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start,
   {
     Bytecount bis = charcount_to_bytecount (XSTRING_DATA (string), s);
     regex_emacs_buffer = buf;
-    regex_emacs_buffer_p = 0;
     val = re_search (bufp, (char *) XSTRING_DATA (string),
                     XSTRING_LENGTH (string), bis,
                     XSTRING_LENGTH (string) - bis,
@@ -444,7 +443,7 @@ tables) and defaults to the current buffer.
    This does not clobber the match data. */
 
 Bytecount
-fast_string_match (Lisp_Object regexp,  const Bufbyte *nonreloc,
+fast_string_match (Lisp_Object regexp,  CONST Bufbyte *nonreloc,
                   Lisp_Object reloc, Bytecount offset,
                   Bytecount length, int case_fold_search,
                   Error_behavior errb, int no_quit)
@@ -486,7 +485,6 @@ fast_string_match (Lisp_Object regexp,  const Bufbyte *nonreloc,
 
   /* #### evil current-buffer dependency */
   regex_emacs_buffer = current_buffer;
-  regex_emacs_buffer_p = 0;
   val = re_search (bufp, (char *) newnonreloc + offset, length, 0,
                   length, 0);
 
@@ -686,75 +684,31 @@ scan_buffer (struct buffer *buf, Emchar target, Bufpos start, Bufpos end,
 }
 
 Bytind
-bi_find_next_newline_no_quit (struct buffer *buf, Bytind from, int count)
+bi_find_next_newline_no_quit (struct buffer *buf, Bytind from, int cnt)
 {
-  return bi_scan_buffer (buf, '\n', from, 0, count, 0, 0);
+  return bi_scan_buffer (buf, '\n', from, 0, cnt, 0, 0);
 }
 
 Bufpos
-find_next_newline_no_quit (struct buffer *buf, Bufpos from, int count)
+find_next_newline_no_quit (struct buffer *buf, Bufpos from, int cnt)
 {
-  return scan_buffer (buf, '\n', from, 0, count, 0, 0);
+  return scan_buffer (buf, '\n', from, 0, cnt, 0, 0);
 }
 
 Bufpos
-find_next_newline (struct buffer *buf, Bufpos from, int count)
+find_next_newline (struct buffer *buf, Bufpos from, int cnt)
 {
-  return scan_buffer (buf, '\n', from, 0, count, 0, 1);
-}
-
-Bytind
-bi_find_next_emchar_in_string (Lisp_String* str, Emchar target, Bytind st,
-                              EMACS_INT count)
-{
-  /* This function has been Mule-ized. */
-  Bytind lim = string_length (str) -1;
-  Bufbyte* s = string_data (str);
-
-  assert (count >= 0);
-
-#ifdef MULE
-  /* Due to the Mule representation of characters in a buffer,
-     we can simply search for characters in the range 0 - 127
-     directly.  For other characters, we do it the "hard" way.
-     Note that this way works for all characters but the other
-     way is faster. */
-  if (target >= 0200)
-    {
-      while (st < lim && count > 0)
-       {
-         if (string_char (str, st) == target)
-           count--;
-         INC_CHARBYTIND (s, st);
-       }
-    }
-  else
-#endif
-    {
-      while (st < lim && count > 0)
-       {
-         Bufbyte *bufptr = (Bufbyte *) memchr (charptr_n_addr (s, st),
-                                               (int) target, lim - st);
-         if (bufptr)
-           {
-             count--;
-             st =  (Bytind)(bufptr - s) + 1;
-           }
-         else
-           st = lim;
-       }
-    }
-  return st;
+  return scan_buffer (buf, '\n', from, 0, cnt, 0, 1);
 }
 
 /* Like find_next_newline, but returns position before the newline,
    not after, and only search up to TO.  This isn't just
    find_next_newline (...)-1, because you might hit TO.  */
 Bufpos
-find_before_next_newline (struct buffer *buf, Bufpos from, Bufpos to, int count)
+find_before_next_newline (struct buffer *buf, Bufpos from, Bufpos to, int cnt)
 {
   EMACS_INT shortage;
-  Bufpos pos = scan_buffer (buf, '\n', from, to, count, &shortage, 1);
+  Bufpos pos = scan_buffer (buf, '\n', from, to, cnt, &shortage, 1);
 
   if (shortage == 0)
     pos--;
@@ -774,22 +728,22 @@ skip_chars (struct buffer *buf, int forwardp, int syntaxp,
   unsigned char fastmap[0400];
   int negate = 0;
   REGISTER int i;
-  Lisp_Char_Table *syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
-  Bufpos limit;
+  struct Lisp_Char_Table *syntax_table =
+    XCHAR_TABLE (buf->mirror_syntax_table);
+
+  CHECK_STRING (string);
 
   if (NILP (lim))
-    limit = forwardp ? BUF_ZV (buf) : BUF_BEGV (buf);
+    XSETINT (lim, forwardp ? BUF_ZV (buf) : BUF_BEGV (buf));
   else
-    {
-      CHECK_INT_COERCE_MARKER (lim);
-      limit = XINT (lim);
+    CHECK_INT_COERCE_MARKER (lim);
 
-      /* In any case, don't allow scan outside bounds of buffer.  */
-      if (limit > BUF_ZV   (buf)) limit = BUF_ZV   (buf);
-      if (limit < BUF_BEGV (buf)) limit = BUF_BEGV (buf);
-    }
+  /* In any case, don't allow scan outside bounds of buffer.  */
+  if (XINT (lim) > BUF_ZV (buf))
+    lim = make_int (BUF_ZV (buf));
+  if (XINT (lim) < BUF_BEGV (buf))
+    lim = make_int (BUF_BEGV (buf));
 
-  CHECK_STRING (string);
   p = XSTRING_DATA (string);
   pend = p + XSTRING_LENGTH (string);
   memset (fastmap, 0, sizeof (fastmap));
@@ -874,7 +828,7 @@ skip_chars (struct buffer *buf, int forwardp, int syntaxp,
           to worry about */
        if (forwardp)
          {
-           while (BUF_PT (buf) < limit
+           while (BUF_PT (buf) < XINT (lim)
                   && fastmap[(unsigned char)
                               syntax_code_spec
                              [(int) SYNTAX (syntax_table,
@@ -884,7 +838,7 @@ skip_chars (struct buffer *buf, int forwardp, int syntaxp,
          }
        else
          {
-           while (BUF_PT (buf) > limit
+           while (BUF_PT (buf) > XINT (lim)
                   && fastmap[(unsigned char)
                               syntax_code_spec
                              [(int) SYNTAX (syntax_table,
@@ -897,7 +851,7 @@ skip_chars (struct buffer *buf, int forwardp, int syntaxp,
       {
        if (forwardp)
          {
-           while (BUF_PT (buf) < limit)
+           while (BUF_PT (buf) < XINT (lim))
              {
                Emchar ch = BUF_FETCH_CHAR (buf, BUF_PT (buf));
                if ((ch < 0400) ? fastmap[ch] :
@@ -912,7 +866,7 @@ skip_chars (struct buffer *buf, int forwardp, int syntaxp,
          }
        else
          {
-           while (BUF_PT (buf) > limit)
+           while (BUF_PT (buf) > XINT (lim))
              {
                Emchar ch = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
                if ((ch < 0400) ? fastmap[ch] :
@@ -932,7 +886,7 @@ skip_chars (struct buffer *buf, int forwardp, int syntaxp,
 }
 
 DEFUN ("skip-chars-forward", Fskip_chars_forward, 1, 3, 0, /*
-Move point forward, stopping before a char not in STRING, or at pos LIMIT.
+Move point forward, stopping before a char not in STRING, or at pos LIM.
 STRING is like the inside of a `[...]' in a regular expression
 except that `]' is never special and `\\' quotes `^', `-' or `\\'.
 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
@@ -941,57 +895,57 @@ Returns the distance traveled, either zero or positive.
 
 Optional argument BUFFER defaults to the current buffer.
 */
-       (string, limit, buffer))
+       (string, lim, buffer))
 {
-  return skip_chars (decode_buffer (buffer, 0), 1, 0, string, limit);
+  return skip_chars (decode_buffer (buffer, 0), 1, 0, string, lim);
 }
 
 DEFUN ("skip-chars-backward", Fskip_chars_backward, 1, 3, 0, /*
-Move point backward, stopping after a char not in STRING, or at pos LIMIT.
+Move point backward, stopping after a char not in STRING, or at pos LIM.
 See `skip-chars-forward' for details.
 Returns the distance traveled, either zero or negative.
 
 Optional argument BUFFER defaults to the current buffer.
 */
-       (string, limit, buffer))
+       (string, lim, buffer))
 {
-  return skip_chars (decode_buffer (buffer, 0), 0, 0, string, limit);
+  return skip_chars (decode_buffer (buffer, 0), 0, 0, string, lim);
 }
 
 
 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, 1, 3, 0, /*
 Move point forward across chars in specified syntax classes.
 SYNTAX is a string of syntax code characters.
-Stop before a char whose syntax is not in SYNTAX, or at position LIMIT.
+Stop before a char whose syntax is not in SYNTAX, or at position LIM.
 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
 This function returns the distance traveled, either zero or positive.
 
 Optional argument BUFFER defaults to the current buffer.
 */
-       (syntax, limit, buffer))
+       (syntax, lim, buffer))
 {
-  return skip_chars (decode_buffer (buffer, 0), 1, 1, syntax, limit);
+  return skip_chars (decode_buffer (buffer, 0), 1, 1, syntax, lim);
 }
 
 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, 1, 3, 0, /*
 Move point backward across chars in specified syntax classes.
 SYNTAX is a string of syntax code characters.
-Stop on reaching a char whose syntax is not in SYNTAX, or at position LIMIT.
+Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
 This function returns the distance traveled, either zero or negative.
 
 Optional argument BUFFER defaults to the current buffer.
 */
-       (syntax, limit, buffer))
+       (syntax, lim, buffer))
 {
-  return skip_chars (decode_buffer (buffer, 0), 0, 1, syntax, limit);
+  return skip_chars (decode_buffer (buffer, 0), 0, 1, syntax, lim);
 }
 
 \f
 /* Subroutines of Lisp buffer search functions. */
 
 static Lisp_Object
-search_command (Lisp_Object string, Lisp_Object limit, Lisp_Object noerror,
+search_command (Lisp_Object string, Lisp_Object bound, Lisp_Object no_error,
                Lisp_Object count, Lisp_Object buffer, int direction,
                int RE, int posix)
 {
@@ -1009,14 +963,14 @@ search_command (Lisp_Object string, Lisp_Object limit, Lisp_Object noerror,
 
   buf = decode_buffer (buffer, 0);
   CHECK_STRING (string);
-  if (NILP (limit))
+  if (NILP (bound))
     lim = n > 0 ? BUF_ZV (buf) : BUF_BEGV (buf);
   else
     {
-      CHECK_INT_COERCE_MARKER (limit);
-      lim = XINT (limit);
+      CHECK_INT_COERCE_MARKER (bound);
+      lim = XINT (bound);
       if (n > 0 ? lim < BUF_PT (buf) : lim > BUF_PT (buf))
-       error ("Invalid search limit (wrong side of point)");
+       error ("Invalid search bound (wrong side of point)");
       if (lim > BUF_ZV (buf))
        lim = BUF_ZV (buf);
       if (lim < BUF_BEGV (buf))
@@ -1033,9 +987,9 @@ search_command (Lisp_Object string, Lisp_Object limit, Lisp_Object noerror,
 
   if (np <= 0)
     {
-      if (NILP (noerror))
+      if (NILP (no_error))
        return signal_failure (string);
-      if (!EQ (noerror, Qt))
+      if (!EQ (no_error, Qt))
        {
          if (lim < BUF_BEGV (buf) || lim > BUF_ZV (buf))
            abort ();
@@ -1167,7 +1121,6 @@ search_buffer (struct buffer *buf, Lisp_Object string, Bufpos bufpos,
          Bytecount val;
          QUIT;
           regex_emacs_buffer = buf;
-         regex_emacs_buffer_p = 1;
          val = re_search_2 (bufp,
                             (char *) BI_BUF_BYTE_ADDRESS (buf, p1), s1,
                             (char *) BI_BUF_BYTE_ADDRESS (buf, p2), s2,
@@ -1206,7 +1159,6 @@ search_buffer (struct buffer *buf, Lisp_Object string, Bufpos bufpos,
          Bytecount val;
          QUIT;
           regex_emacs_buffer = buf;
-         regex_emacs_buffer_p = 1;
           val = re_search_2 (bufp,
                             (char *) BI_BUF_BYTE_ADDRESS (buf, p1), s1,
                             (char *) BI_BUF_BYTE_ADDRESS (buf, p2), s2,
@@ -1247,7 +1199,7 @@ search_buffer (struct buffer *buf, Lisp_Object string, Bufpos bufpos,
 
        BTW "BM" stands for Boyer-Moore, which is one of the standard
        string-searching algorithms.  It's the best string-searching
-       algorithm out there, provided that:
+       algorithm out there provided
 
        a) You're not fazed by algorithm complexity. (Rabin-Karp, which
           uses hashing, is much much easier to code but not as fast.)
@@ -1431,7 +1383,7 @@ search_buffer (struct buffer *buf, Lisp_Object string, Bufpos bufpos,
                               (EMACS_UINT) p_limit)
                          cursor += BM_tab[*cursor];
                    }
- /* If you are here, cursor is beyond the end of the searched region. */
+/* If you are here, cursor is beyond the end of the searched region. */
  /* This can happen if you match on the far character of the pattern, */
  /* because the "stride" of that character is infinity, a number able */
  /* to throw you well beyond the end of the search.  It can also */
@@ -1579,8 +1531,8 @@ set_search_regs (struct buffer *buf, Bufpos beg, Charcount len)
 
 \f
 /* Given a string of words separated by word delimiters,
-   compute a regexp that matches those exact words
-   separated by arbitrary punctuation.  */
+  compute a regexp that matches those exact words
+  separated by arbitrary punctuation.  */
 
 static Lisp_Object
 wordify (Lisp_Object buffer, Lisp_Object string)
@@ -1588,7 +1540,8 @@ wordify (Lisp_Object buffer, Lisp_Object string)
   Charcount i, len;
   EMACS_INT punct_count = 0, word_count = 0;
   struct buffer *buf = decode_buffer (buffer, 0);
-  Lisp_Char_Table *syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
+  struct Lisp_Char_Table *syntax_table =
+    XCHAR_TABLE (buf->mirror_syntax_table);
 
   CHECK_STRING (string);
   len = XSTRING_CHAR_LENGTH (string);
@@ -1645,103 +1598,70 @@ wordify (Lisp_Object buffer, Lisp_Object string)
 DEFUN ("search-backward", Fsearch_backward, 1, 5, "sSearch backward: ", /*
 Search backward from point for STRING.
 Set point to the beginning of the occurrence found, and return point.
-
-Optional second argument LIMIT bounds the search; it is a buffer
-position.  The match found must not extend before that position.
-The value nil is equivalent to (point-min).
-
-Optional third argument NOERROR, if t, means just return nil (no
-error) if the search fails.  If neither nil nor t, set point to LIMIT
-and return nil.
-
-Optional fourth argument COUNT is a repeat count--search for
-successive occurrences.
-
+An optional second argument bounds the search; it is a buffer position.
+The match found must not extend before that position.
+Optional third argument, if t, means if fail just return nil (no error).
+ If not nil and not t, position at limit of search and return nil.
+Optional fourth argument is repeat count--search for successive occurrences.
 Optional fifth argument BUFFER specifies the buffer to search in and
-defaults to the current buffer.
-
+ defaults to the current buffer.
 See also the functions `match-beginning', `match-end' and `replace-match'.
 */
-       (string, limit, noerror, count, buffer))
+       (string, bound, no_error, count, buffer))
 {
-  return search_command (string, limit, noerror, count, buffer, -1, 0, 0);
+  return search_command (string, bound, no_error, count, buffer, -1, 0, 0);
 }
 
 DEFUN ("search-forward", Fsearch_forward, 1, 5, "sSearch: ", /*
 Search forward from point for STRING.
 Set point to the end of the occurrence found, and return point.
-
-Optional second argument LIMIT bounds the search; it is a buffer
-position.  The match found must not extend after that position.  The
-value nil is equivalent to (point-max).
-
-Optional third argument NOERROR, if t, means just return nil (no
-error) if the search fails.  If neither nil nor t, set point to LIMIT
-and return nil.
-
-Optional fourth argument COUNT is a repeat count--search for
-successive occurrences.
-
+An optional second argument bounds the search; it is a buffer position.
+The match found must not extend after that position.  nil is equivalent
+  to (point-max).
+Optional third argument, if t, means if fail just return nil (no error).
+  If not nil and not t, move to limit of search and return nil.
+Optional fourth argument is repeat count--search for successive occurrences.
 Optional fifth argument BUFFER specifies the buffer to search in and
-defaults to the current buffer.
-
+ defaults to the current buffer.
 See also the functions `match-beginning', `match-end' and `replace-match'.
 */
-       (string, limit, noerror, count, buffer))
+       (string, bound, no_error, count, buffer))
 {
-  return search_command (string, limit, noerror, count, buffer, 1, 0, 0);
+  return search_command (string, bound, no_error, count, buffer, 1, 0, 0);
 }
 
 DEFUN ("word-search-backward", Fword_search_backward, 1, 5,
        "sWord search backward: ", /*
 Search backward from point for STRING, ignoring differences in punctuation.
 Set point to the beginning of the occurrence found, and return point.
-
-Optional second argument LIMIT bounds the search; it is a buffer
-position.  The match found must not extend before that position.
-The value nil is equivalent to (point-min).
-
-Optional third argument NOERROR, if t, means just return nil (no
-error) if the search fails.  If neither nil nor t, set point to LIMIT
-and return nil.
-
-Optional fourth argument COUNT is a repeat count--search for
-successive occurrences.
-
+An optional second argument bounds the search; it is a buffer position.
+The match found must not extend before that position.
+Optional third argument, if t, means if fail just return nil (no error).
+  If not nil and not t, move to limit of search and return nil.
+Optional fourth argument is repeat count--search for successive occurrences.
 Optional fifth argument BUFFER specifies the buffer to search in and
-defaults to the current buffer.
-
-See also the functions `match-beginning', `match-end' and `replace-match'.
+ defaults to the current buffer.
 */
-       (string, limit, noerror, count, buffer))
+       (string, bound, no_error, count, buffer))
 {
-  return search_command (wordify (buffer, string), limit, noerror, count,
+  return search_command (wordify (buffer, string), bound, no_error, count,
                         buffer, -1, 1, 0);
 }
 
 DEFUN ("word-search-forward", Fword_search_forward, 1, 5, "sWord search: ", /*
 Search forward from point for STRING, ignoring differences in punctuation.
 Set point to the end of the occurrence found, and return point.
-
-Optional second argument LIMIT bounds the search; it is a buffer
-position.  The match found must not extend after that position.  The
-value nil is equivalent to (point-max).
-
-Optional third argument NOERROR, if t, means just return nil (no
-error) if the search fails.  If neither nil nor t, set point to LIMIT
-and return nil.
-
-Optional fourth argument COUNT is a repeat count--search for
-successive occurrences.
-
+An optional second argument bounds the search; it is a buffer position.
+The match found must not extend after that position.
+Optional third argument, if t, means if fail just return nil (no error).
+  If not nil and not t, move to limit of search and return nil.
+Optional fourth argument is repeat count--search for successive occurrences.
 Optional fifth argument BUFFER specifies the buffer to search in and
-defaults to the current buffer.
-
-See also the functions `match-beginning', `match-end' and `replace-match'.
+ defaults to the current buffer.
 */
-       (string, limit, noerror, count, buffer))
+       (string, bound, no_error, count, buffer))
 {
-  return search_command (wordify (buffer, string), limit, noerror, count,
+  return search_command (wordify (buffer, string), bound, no_error, count,
                         buffer, 1, 1, 0);
 }
 
@@ -1751,51 +1671,35 @@ Search backward from point for match for regular expression REGEXP.
 Set point to the beginning of the match, and return point.
 The match found is the one starting last in the buffer
 and yet ending before the origin of the search.
-
-Optional second argument LIMIT bounds the search; it is a buffer
-position.  The match found must not extend before that position.
-The value nil is equivalent to (point-min).
-
-Optional third argument NOERROR, if t, means just return nil (no
-error) if the search fails.  If neither nil nor t, set point to LIMIT
-and return nil.
-
-Optional fourth argument COUNT is a repeat count--search for
-successive occurrences.
-
+An optional second argument bounds the search; it is a buffer position.
+The match found must start at or after that position.
+Optional third argument, if t, means if fail just return nil (no error).
+  If not nil and not t, move to limit of search and return nil.
+Optional fourth argument is repeat count--search for successive occurrences.
 Optional fifth argument BUFFER specifies the buffer to search in and
-defaults to the current buffer.
-
+ defaults to the current buffer.
 See also the functions `match-beginning', `match-end' and `replace-match'.
 */
-       (regexp, limit, noerror, count, buffer))
+       (regexp, bound, no_error, count, buffer))
 {
-  return search_command (regexp, limit, noerror, count, buffer, -1, 1, 0);
+  return search_command (regexp, bound, no_error, count, buffer, -1, 1, 0);
 }
 
 DEFUN ("re-search-forward", Fre_search_forward, 1, 5, "sRE search: ", /*
 Search forward from point for regular expression REGEXP.
 Set point to the end of the occurrence found, and return point.
-
-Optional second argument LIMIT bounds the search; it is a buffer
-position.  The match found must not extend after that position.  The
-value nil is equivalent to (point-max).
-
-Optional third argument NOERROR, if t, means just return nil (no
-error) if the search fails.  If neither nil nor t, set point to LIMIT
-and return nil.
-
-Optional fourth argument COUNT is a repeat count--search for
-successive occurrences.
-
+An optional second argument bounds the search; it is a buffer position.
+The match found must not extend after that position.
+Optional third argument, if t, means if fail just return nil (no error).
+  If not nil and not t, move to limit of search and return nil.
+Optional fourth argument is repeat count--search for successive occurrences.
 Optional fifth argument BUFFER specifies the buffer to search in and
-defaults to the current buffer.
-
+ defaults to the current buffer.
 See also the functions `match-beginning', `match-end' and `replace-match'.
 */
-       (regexp, limit, noerror, count, buffer))
+       (regexp, bound, no_error, count, buffer))
 {
-  return search_command (regexp, limit, noerror, count, buffer, 1, 1, 0);
+  return search_command (regexp, bound, no_error, count, buffer, 1, 1, 0);
 }
 
 DEFUN ("posix-search-backward", Fposix_search_backward, 1, 5,
@@ -1805,52 +1709,36 @@ Find the longest match in accord with Posix regular expression rules.
 Set point to the beginning of the match, and return point.
 The match found is the one starting last in the buffer
 and yet ending before the origin of the search.
-
-Optional second argument LIMIT bounds the search; it is a buffer
-position.  The match found must not extend before that position.
-The value nil is equivalent to (point-min).
-
-Optional third argument NOERROR, if t, means just return nil (no
-error) if the search fails.  If neither nil nor t, set point to LIMIT
-and return nil.
-
-Optional fourth argument COUNT is a repeat count--search for
-successive occurrences.
-
+An optional second argument bounds the search; it is a buffer position.
+The match found must start at or after that position.
+Optional third argument, if t, means if fail just return nil (no error).
+  If not nil and not t, move to limit of search and return nil.
+Optional fourth argument is repeat count--search for successive occurrences.
 Optional fifth argument BUFFER specifies the buffer to search in and
-defaults to the current buffer.
-
+ defaults to the current buffer.
 See also the functions `match-beginning', `match-end' and `replace-match'.
 */
-       (regexp, limit, noerror, count, buffer))
+       (regexp, bound, no_error, count, buffer))
 {
-  return search_command (regexp, limit, noerror, count, buffer, -1, 1, 1);
+  return search_command (regexp, bound, no_error, count, buffer, -1, 1, 1);
 }
 
 DEFUN ("posix-search-forward", Fposix_search_forward, 1, 5, "sPosix search: ", /*
 Search forward from point for regular expression REGEXP.
 Find the longest match in accord with Posix regular expression rules.
 Set point to the end of the occurrence found, and return point.
-
-Optional second argument LIMIT bounds the search; it is a buffer
-position.  The match found must not extend after that position.  The
-value nil is equivalent to (point-max).
-
-Optional third argument NOERROR, if t, means just return nil (no
-error) if the search fails.  If neither nil nor t, set point to LIMIT
-and return nil.
-
-Optional fourth argument COUNT is a repeat count--search for
-successive occurrences.
-
+An optional second argument bounds the search; it is a buffer position.
+The match found must not extend after that position.
+Optional third argument, if t, means if fail just return nil (no error).
+  If not nil and not t, move to limit of search and return nil.
+Optional fourth argument is repeat count--search for successive occurrences.
 Optional fifth argument BUFFER specifies the buffer to search in and
-defaults to the current buffer.
-
+ defaults to the current buffer.
 See also the functions `match-beginning', `match-end' and `replace-match'.
 */
-       (regexp, limit, noerror, count, buffer))
+       (regexp, bound, no_error, count, buffer))
 {
-  return search_command (regexp, limit, noerror, count, buffer, 1, 1, 1);
+  return search_command (regexp, bound, no_error, count, buffer, 1, 1, 1);
 }
 
 \f
@@ -1866,18 +1754,18 @@ free_created_dynarrs (Lisp_Object cons)
 }
 
 DEFUN ("replace-match", Freplace_match, 1, 5, 0, /*
-Replace text matched by last search with REPLACEMENT.
+Replace text matched by last search with NEWTEXT.
 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 REPLACEMENT to all caps.
+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 REPLACEMENT.
+then capitalize each word in NEWTEXT.
 
-If third arg LITERAL is non-nil, insert REPLACEMENT literally.
+If third arg LITERAL is non-nil, insert NEWTEXT literally.
 Otherwise treat `\\' as special:
-  `\\&' in REPLACEMENT means substitute original matched text.
+  `\\&' in NEWTEXT means substitute original matched text.
   `\\N' means substitute what matched the Nth `\\(...\\)'.
        If Nth parens didn't match, substitute nothing.
   `\\\\' means insert one `\\'.
@@ -1896,11 +1784,11 @@ In that case, this function creates and returns a new string
 which is made by replacing the part of STRING that was matched.
 When fourth argument is a string, fifth argument STRBUFFER specifies
 the buffer to be used for syntax-table and case-table lookup and
-defaults to the current buffer.  When fourth argument is not a string,
+defaults to the current buffer. (When fourth argument is not a string,
 the buffer that the match occurred in has automatically been remembered
-and you do not need to specify it.
+and you do not need to specify it.)
 */
-       (replacement, fixedcase, literal, string, strbuffer))
+       (newtext, fixedcase, literal, string, strbuffer))
 {
   /* This function has been Mule-ized. */
   /* This function can GC */
@@ -1913,14 +1801,14 @@ and you do not need to specify it.
   Emchar c, prevc;
   Charcount inslen;
   struct buffer *buf;
-  Lisp_Char_Table *syntax_table;
+  struct Lisp_Char_Table *syntax_table;
   int mc_count;
   Lisp_Object buffer;
   int_dynarr *ul_action_dynarr = 0;
   int_dynarr *ul_pos_dynarr = 0;
   int speccount;
 
-  CHECK_STRING (replacement);
+  CHECK_STRING (newtext);
 
   if (! NILP (string))
     {
@@ -1949,7 +1837,7 @@ and you do not need to specify it.
   case_action = nochange;      /* We tried an initialization */
                                /* but some C compilers blew it */
 
-  if (search_regs.num_regs == 0)
+  if (search_regs.num_regs <= 0)
     error ("replace-match called before any match found");
 
   if (NILP (string))
@@ -2044,10 +1932,10 @@ and you do not need to specify it.
       before = Fsubstring (string, Qzero, make_int (search_regs.start[0]));
       after = Fsubstring (string, make_int (search_regs.end[0]), Qnil);
 
-      /* Do case substitution into REPLACEMENT if desired.  */
+      /* Do case substitution into NEWTEXT if desired.  */
       if (NILP (literal))
        {
-         Charcount stlen = XSTRING_CHAR_LENGTH (replacement);
+         Charcount stlen = XSTRING_CHAR_LENGTH (newtext);
          Charcount strpos;
          /* XEmacs change: rewrote this loop somewhat to make it
             cleaner.  Also added \U, \E, etc. */
@@ -2074,10 +1962,10 @@ and you do not need to specify it.
              Charcount substart = -1;
              Charcount subend   = -1;
 
-             c = string_char (XSTRING (replacement), strpos);
-             if (c == '\\' && strpos < stlen - 1)
+             c = string_char (XSTRING (newtext), strpos);
+             if (c == '\\')
                {
-                 c = string_char (XSTRING (replacement), ++strpos);
+                 c = string_char (XSTRING (newtext), ++strpos);
                  if (c == '&')
                    {
                      literal_end = strpos - 1;
@@ -2126,7 +2014,7 @@ and you do not need to specify it.
                  Lisp_Object literal_text = Qnil;
                  Lisp_Object substring = Qnil;
                  if (literal_end != literal_start)
-                   literal_text = Fsubstring (replacement,
+                   literal_text = Fsubstring (newtext,
                                               make_int (literal_start),
                                               make_int (literal_end));
                  if (substart >= 0 && subend != substart)
@@ -2141,33 +2029,29 @@ and you do not need to specify it.
 
          if (strpos != literal_start)
            /* some literal text at end to be inserted */
-           replacement = concat2 (accum, Fsubstring (replacement,
-                                                     make_int (literal_start),
-                                                     make_int (strpos)));
+           newtext = concat2 (accum, Fsubstring (newtext,
+                                                 make_int (literal_start),
+                                                 make_int (strpos)));
          else
-           replacement = accum;
+           newtext = accum;
        }
 
-      /* replacement can be nil. */
-      if (NILP (replacement))
-       replacement = build_string ("");
-
       if (case_action == all_caps)
-       replacement = Fupcase (replacement, buffer);
+       newtext = Fupcase (newtext, buffer);
       else if (case_action == cap_initial)
-       replacement = Fupcase_initials (replacement, buffer);
+       newtext = Fupcase_initials (newtext, buffer);
 
       /* Now finally, we need to process the \U's, \E's, etc. */
       if (ul_pos_dynarr)
        {
          int i = 0;
          int cur_action = 'E';
-         Charcount stlen = XSTRING_CHAR_LENGTH (replacement);
+         Charcount stlen = XSTRING_CHAR_LENGTH (newtext);
          Charcount strpos;
 
          for (strpos = 0; strpos < stlen; strpos++)
            {
-             Emchar curchar = string_char (XSTRING (replacement), strpos);
+             Emchar curchar = string_char (XSTRING (newtext), strpos);
              Emchar newchar = -1;
              if (i < Dynarr_length (ul_pos_dynarr) &&
                  strpos == Dynarr_at (ul_pos_dynarr, i))
@@ -2191,13 +2075,13 @@ and you do not need to specify it.
                    newchar = curchar;
                }
              if (newchar != curchar)
-               set_string_char (XSTRING (replacement), strpos, newchar);
+               set_string_char (XSTRING (newtext), strpos, newchar);
            }
        }
 
       /* frees the Dynarrs if necessary. */
       unbind_to (speccount, Qnil);
-      return concat3 (before, replacement, after);
+      return concat3 (before, newtext, after);
     }
 
   mc_count = begin_multiple_change (buf, search_regs.start[0],
@@ -2213,21 +2097,21 @@ and you do not need to specify it.
      position in the replacement.  */
   BUF_SET_PT (buf, search_regs.start[0]);
   if (!NILP (literal))
-    Finsert (1, &replacement);
+    Finsert (1, &newtext);
   else
     {
-      Charcount stlen = XSTRING_CHAR_LENGTH (replacement);
+      Charcount stlen = XSTRING_CHAR_LENGTH (newtext);
       Charcount strpos;
       struct gcpro gcpro1;
-      GCPRO1 (replacement);
+      GCPRO1 (newtext);
       for (strpos = 0; strpos < stlen; strpos++)
        {
          Charcount offset = BUF_PT (buf) - search_regs.start[0];
 
-         c = string_char (XSTRING (replacement), strpos);
-         if (c == '\\' && strpos < stlen - 1)
+         c = string_char (XSTRING (newtext), strpos);
+         if (c == '\\')
            {
-             c = string_char (XSTRING (replacement), ++strpos);
+             c = string_char (XSTRING (newtext), ++strpos);
              if (c == '&')
                Finsert_buffer_substring
                   (buffer,
@@ -2335,7 +2219,7 @@ match_limit (Lisp_Object num, int beginningp)
   n = XINT (num);
   if (n < 0 || n >= search_regs.num_regs)
     args_out_of_range (num, make_int (search_regs.num_regs));
-  if (search_regs.num_regs == 0 ||
+  if (search_regs.num_regs <= 0 ||
       search_regs.start[n] < 0)
     return Qnil;
   return make_int (beginningp ? search_regs.start[n] : search_regs.end[n]);
@@ -2426,7 +2310,7 @@ to hold all the values, and if INTEGERS is non-nil, no consing is done.
 
   /* If REUSE is a list, store as many value elements as will fit
      into the elements of REUSE.  */
-  for (prev = Qnil, i = 0, tail = reuse; CONSP (tail); i++, tail = XCDR (tail))
+  for (i = 0, tail = reuse; CONSP (tail); i++, tail = XCDR (tail))
     {
       if (i < 2 * len + 2)
        XCAR (tail) = data[i];
@@ -2567,19 +2451,19 @@ restore_match_data (void)
 DEFUN ("regexp-quote", Fregexp_quote, 1, 1, 0, /*
 Return a regexp string which matches exactly STRING and nothing else.
 */
-       (string))
+       (str))
 {
   REGISTER Bufbyte *in, *out, *end;
   REGISTER Bufbyte *temp;
 
-  CHECK_STRING (string);
+  CHECK_STRING (str);
 
-  temp = (Bufbyte *) alloca (XSTRING_LENGTH (string) * 2);
+  temp = (Bufbyte *) alloca (XSTRING_LENGTH (str) * 2);
 
   /* Now copy the data into the new string, inserting escapes. */
 
-  in = XSTRING_DATA (string);
-  end = in + XSTRING_LENGTH (string);
+  in = XSTRING_DATA (str);
+  end = in + XSTRING_LENGTH (str);
   out = temp;
 
   while (in < end)
@@ -2617,8 +2501,8 @@ void
 syms_of_search (void)
 {
 
-  DEFERROR_STANDARD (Qsearch_failed, Qinvalid_operation);
-  DEFERROR_STANDARD (Qinvalid_regexp, Qsyntax_error);
+  deferror (&Qsearch_failed, "search-failed", "Search failed", Qerror);
+  deferror (&Qinvalid_regexp, "invalid-regexp", "Invalid regexp", Qerror);
 
   DEFSUBR (Flooking_at);
   DEFSUBR (Fposix_looking_at);
@@ -2646,12 +2530,9 @@ syms_of_search (void)
 }
 
 void
-reinit_vars_of_search (void)
+vars_of_search (void)
 {
-  int i;
-
-  last_thing_searched = Qnil;
-  staticpro_nodump (&last_thing_searched);
+  REGISTER int i;
 
   for (i = 0; i < REGEXP_CACHE_SIZE; ++i)
     {
@@ -2659,16 +2540,13 @@ reinit_vars_of_search (void)
       searchbufs[i].buf.buffer = (unsigned char *) xmalloc (100);
       searchbufs[i].buf.fastmap = searchbufs[i].fastmap;
       searchbufs[i].regexp = Qnil;
-      staticpro_nodump (&searchbufs[i].regexp);
+      staticpro (&searchbufs[i].regexp);
       searchbufs[i].next = (i == REGEXP_CACHE_SIZE-1 ? 0 : &searchbufs[i+1]);
     }
   searchbuf_head = &searchbufs[0];
-}
 
-void
-vars_of_search (void)
-{
-  reinit_vars_of_search ();
+  last_thing_searched = Qnil;
+  staticpro (&last_thing_searched);
 
   DEFVAR_LISP ("forward-word-regexp", &Vforward_word_regexp /*
 *Regular expression to be used in `forward-word'.