This commit was generated by cvs2svn to compensate for changes in r5670,
[chise/xemacs-chise.git.1] / src / search.c
index 7f92765..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] :
@@ -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);
@@ -1848,7 +1801,7 @@ 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;
@@ -1884,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))
@@ -2010,7 +1963,7 @@ and you do not need to specify it.)
              Charcount subend   = -1;
 
              c = string_char (XSTRING (newtext), strpos);
-             if (c == '\\' && strpos < stlen - 1)
+             if (c == '\\')
                {
                  c = string_char (XSTRING (newtext), ++strpos);
                  if (c == '&')
@@ -2156,7 +2109,7 @@ and you do not need to specify it.)
          Charcount offset = BUF_PT (buf) - search_regs.start[0];
 
          c = string_char (XSTRING (newtext), strpos);
-         if (c == '\\' && strpos < stlen - 1)
+         if (c == '\\')
            {
              c = string_char (XSTRING (newtext), ++strpos);
              if (c == '&')
@@ -2266,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]);
@@ -2357,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];
@@ -2548,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);
@@ -2577,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)
     {
@@ -2590,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'.