Promote SSDATA macro from gtkutil.c and xsmfns.c to lisp.h.
[bpt/emacs.git] / src / search.c
index 7bdb4ec..570bbe7 100644 (file)
@@ -1,6 +1,6 @@
 /* String search routines for GNU Emacs.
    Copyright (C) 1985, 1986, 1987, 1993, 1994, 1997, 1998, 1999, 2001, 2002,
-                 2003, 2004, 2005, 2006, 2007, 2008
+                 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -20,6 +20,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
 #include <config.h>
+#include <setjmp.h>
 #include "lisp.h"
 #include "syntax.h"
 #include "category.h"
@@ -90,22 +91,22 @@ Lisp_Object Qinvalid_regexp;
 /* Error condition used for failing searches */
 Lisp_Object Qsearch_failed;
 
-Lisp_Object Vsearch_spaces_regexp;
-
-/* If non-nil, the match data will not be changed during call to
-   searching or matching functions.  This variable is for internal use
-   only.  */
-Lisp_Object Vinhibit_changing_match_data;
-
-static void set_search_regs ();
-static void save_search_regs ();
-static int simple_search ();
-static int boyer_moore ();
-static int search_buffer ();
-static void matcher_overflow () NO_RETURN;
+static void set_search_regs (EMACS_INT, EMACS_INT);
+static void save_search_regs (void);
+static EMACS_INT simple_search (EMACS_INT, unsigned char *, EMACS_INT,
+                               EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT,
+                                EMACS_INT, EMACS_INT);
+static EMACS_INT boyer_moore (EMACS_INT, unsigned char *, EMACS_INT, EMACS_INT,
+                              Lisp_Object, Lisp_Object,
+                              EMACS_INT, EMACS_INT,
+                              EMACS_INT, EMACS_INT, int);
+static EMACS_INT search_buffer (Lisp_Object, EMACS_INT, EMACS_INT,
+                                EMACS_INT, EMACS_INT, EMACS_INT, int,
+                                Lisp_Object, Lisp_Object, int);
+static void matcher_overflow (void) NO_RETURN;
 
 static void
-matcher_overflow ()
+matcher_overflow (void)
 {
   error ("Stack overflow in regexp matcher");
 }
@@ -124,12 +125,7 @@ matcher_overflow ()
    The behavior also depends on Vsearch_spaces_regexp.  */
 
 static void
-compile_pattern_1 (cp, pattern, translate, regp, posix)
-     struct regexp_cache *cp;
-     Lisp_Object pattern;
-     Lisp_Object translate;
-     struct re_registers *regp;
-     int posix;
+compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern, Lisp_Object translate, struct re_registers *regp, int posix)
 {
   char *val;
   reg_syntax_t old;
@@ -157,7 +153,7 @@ compile_pattern_1 (cp, pattern, translate, regp, posix)
   else
     re_set_whitespace_regexp (NULL);
 
-  val = (char *) re_compile_pattern ((char *) SDATA (pattern),
+  val = (char *) re_compile_pattern (SSDATA (pattern),
                                     SBYTES (pattern), &cp->buf);
 
   /* If the compiled pattern hard codes some of the contents of the
@@ -179,7 +175,7 @@ compile_pattern_1 (cp, pattern, translate, regp, posix)
    This is called from garbage collection.  */
 
 void
-shrink_regexp_cache ()
+shrink_regexp_cache (void)
 {
   struct regexp_cache *cp;
 
@@ -197,13 +193,13 @@ shrink_regexp_cache ()
    automagically manages the memory in each re_pattern_buffer struct,
    based on its `allocated' and `buffer' values.  */
 void
-clear_regexp_cache ()
+clear_regexp_cache (void)
 {
   int i;
 
   for (i = 0; i < REGEXP_CACHE_SIZE; ++i)
-    /* It's tempting to compare with the syntax-table we've actually changd,
-       but it's not sufficient because char-table inheritance mewans that
+    /* It's tempting to compare with the syntax-table we've actually changed,
+       but it's not sufficient because char-table inheritance means that
        modifying one syntax-table can change others at the same time.  */
     if (!EQ (searchbufs[i].syntax_table, Qt))
       searchbufs[i].regexp = Qnil;
@@ -221,11 +217,7 @@ clear_regexp_cache ()
    for this pattern.  0 means backtrack only enough to get a valid match.  */
 
 struct re_pattern_buffer *
-compile_pattern (pattern, regp, translate, posix, multibyte)
-     Lisp_Object pattern;
-     struct re_registers *regp;
-     Lisp_Object translate;
-     int posix, multibyte;
+compile_pattern (Lisp_Object pattern, struct re_registers *regp, Lisp_Object translate, int posix, int multibyte)
 {
   struct regexp_cache *cp, **cpp;
 
@@ -273,7 +265,7 @@ compile_pattern (pattern, regp, translate, posix, multibyte)
   if (regp)
     re_set_registers (&cp->buf, regp, regp->num_regs, regp->start, regp->end);
 
-  /* The compiled pattern can be used both for mulitbyte and unibyte
+  /* The compiled pattern can be used both for multibyte and unibyte
      target.  But, we have to tell which the pattern is used for. */
   cp->buf.target_multibyte = multibyte;
 
@@ -282,14 +274,12 @@ compile_pattern (pattern, regp, translate, posix, multibyte)
 
 \f
 static Lisp_Object
-looking_at_1 (string, posix)
-     Lisp_Object string;
-     int posix;
+looking_at_1 (Lisp_Object string, int posix)
 {
   Lisp_Object val;
   unsigned char *p1, *p2;
-  int s1, s2;
-  register int i;
+  EMACS_INT s1, s2;
+  register EMACS_INT i;
   struct re_pattern_buffer *bufp;
 
   if (running_asynch_code)
@@ -365,8 +355,7 @@ DEFUN ("looking-at", Flooking_at, Slooking_at, 1, 1, 0,
 This function modifies the match data that `match-beginning',
 `match-end' and `match-data' access; save and restore the match
 data if you want to preserve them.  */)
-     (regexp)
-     Lisp_Object regexp;
+  (Lisp_Object regexp)
 {
   return looking_at_1 (regexp, 0);
 }
@@ -377,20 +366,17 @@ Find the longest match, in accord with Posix regular expression rules.
 This function modifies the match data that `match-beginning',
 `match-end' and `match-data' access; save and restore the match
 data if you want to preserve them.  */)
-     (regexp)
-     Lisp_Object regexp;
+  (Lisp_Object regexp)
 {
   return looking_at_1 (regexp, 1);
 }
 \f
 static Lisp_Object
-string_match_1 (regexp, string, start, posix)
-     Lisp_Object regexp, string, start;
-     int posix;
+string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start, int posix)
 {
   int val;
   struct re_pattern_buffer *bufp;
-  int pos, pos_byte;
+  EMACS_INT pos, pos_byte;
   int i;
 
   if (running_asynch_code)
@@ -403,7 +389,7 @@ string_match_1 (regexp, string, start, posix)
     pos = 0, pos_byte = 0;
   else
     {
-      int len = SCHARS (string);
+      EMACS_INT len = SCHARS (string);
 
       CHECK_NUMBER (start);
       pos = XINT (start);
@@ -428,7 +414,7 @@ string_match_1 (regexp, string, start, posix)
   immediate_quit = 1;
   re_match_object = string;
 
-  val = re_search (bufp, (char *) SDATA (string),
+  val = re_search (bufp, SSDATA (string),
                   SBYTES (string), pos_byte,
                   SBYTES (string) - pos_byte,
                   (NILP (Vinhibit_changing_match_data)
@@ -466,8 +452,7 @@ matched by parenthesis constructs in the pattern.
 
 You can use the function `match-string' to extract the substrings
 matched by the parenthesis constructions in REGEXP. */)
-     (regexp, string, start)
-     Lisp_Object regexp, string, start;
+  (Lisp_Object regexp, Lisp_Object string, Lisp_Object start)
 {
   return string_match_1 (regexp, string, start, 0);
 }
@@ -480,8 +465,7 @@ If third arg START is non-nil, start search at that index in STRING.
 For index of first char beyond the match, do (match-end 0).
 `match-end' and `match-beginning' also give indices of substrings
 matched by parenthesis constructs in the pattern.  */)
-     (regexp, string, start)
-     Lisp_Object regexp, string, start;
+  (Lisp_Object regexp, Lisp_Object string, Lisp_Object start)
 {
   return string_match_1 (regexp, string, start, 1);
 }
@@ -491,8 +475,7 @@ matched by parenthesis constructs in the pattern.  */)
    This does not clobber the match data.  */
 
 int
-fast_string_match (regexp, string)
-     Lisp_Object regexp, string;
+fast_string_match (Lisp_Object regexp, Lisp_Object string)
 {
   int val;
   struct re_pattern_buffer *bufp;
@@ -502,7 +485,7 @@ fast_string_match (regexp, string)
   immediate_quit = 1;
   re_match_object = string;
 
-  val = re_search (bufp, (char *) SDATA (string),
+  val = re_search (bufp, SSDATA (string),
                   SBYTES (string), 0,
                   SBYTES (string), 0);
   immediate_quit = 0;
@@ -514,16 +497,12 @@ fast_string_match (regexp, string)
    This does not clobber the match data.
    We assume that STRING contains single-byte characters.  */
 
-extern Lisp_Object Vascii_downcase_table;
-
 int
-fast_c_string_match_ignore_case (regexp, string)
-     Lisp_Object regexp;
-     const char *string;
+fast_c_string_match_ignore_case (Lisp_Object regexp, const char *string)
 {
   int val;
   struct re_pattern_buffer *bufp;
-  int len = strlen (string);
+  size_t len = strlen (string);
 
   regexp = string_make_unibyte (regexp);
   re_match_object = Qt;
@@ -539,8 +518,7 @@ fast_c_string_match_ignore_case (regexp, string)
 /* Like fast_string_match but ignore case.  */
 
 int
-fast_string_match_ignore_case (regexp, string)
-     Lisp_Object regexp, string;
+fast_string_match_ignore_case (Lisp_Object regexp, Lisp_Object string)
 {
   int val;
   struct re_pattern_buffer *bufp;
@@ -550,13 +528,78 @@ fast_string_match_ignore_case (regexp, string)
   immediate_quit = 1;
   re_match_object = string;
 
-  val = re_search (bufp, (char *) SDATA (string),
+  val = re_search (bufp, SSDATA (string),
                   SBYTES (string), 0,
                   SBYTES (string), 0);
   immediate_quit = 0;
   return val;
 }
 \f
+/* Match REGEXP against the characters after POS to LIMIT, and return
+   the number of matched characters.  If STRING is non-nil, match
+   against the characters in it.  In that case, POS and LIMIT are
+   indices into the string.  This function doesn't modify the match
+   data.  */
+
+EMACS_INT
+fast_looking_at (Lisp_Object regexp, EMACS_INT pos, EMACS_INT pos_byte, EMACS_INT limit, EMACS_INT limit_byte, Lisp_Object string)
+{
+  int multibyte;
+  struct re_pattern_buffer *buf;
+  unsigned char *p1, *p2;
+  EMACS_INT s1, s2;
+  EMACS_INT len;
+
+  if (STRINGP (string))
+    {
+      if (pos_byte < 0)
+       pos_byte = string_char_to_byte (string, pos);
+      if (limit_byte < 0)
+       limit_byte = string_char_to_byte (string, limit);
+      p1 = NULL;
+      s1 = 0;
+      p2 = SDATA (string);
+      s2 = SBYTES (string);
+      re_match_object = string;
+      multibyte = STRING_MULTIBYTE (string);
+    }
+  else
+    {
+      if (pos_byte < 0)
+       pos_byte = CHAR_TO_BYTE (pos);
+      if (limit_byte < 0)
+       limit_byte = CHAR_TO_BYTE (limit);
+      pos_byte -= BEGV_BYTE;
+      limit_byte -= BEGV_BYTE;
+      p1 = BEGV_ADDR;
+      s1 = GPT_BYTE - BEGV_BYTE;
+      p2 = GAP_END_ADDR;
+      s2 = ZV_BYTE - GPT_BYTE;
+      if (s1 < 0)
+       {
+         p2 = p1;
+         s2 = ZV_BYTE - BEGV_BYTE;
+         s1 = 0;
+       }
+      if (s2 < 0)
+       {
+         s1 = ZV_BYTE - BEGV_BYTE;
+         s2 = 0;
+       }
+      re_match_object = Qnil;
+      multibyte = ! NILP (current_buffer->enable_multibyte_characters);
+    }
+
+  buf = compile_pattern (regexp, 0, Qnil, 0, multibyte);
+  immediate_quit = 1;
+  len = re_match_2 (buf, (char *) p1, s1, (char *) p2, s2,
+                   pos_byte, NULL, limit_byte);
+  immediate_quit = 0;
+
+  return len;
+}
+
+\f
 /* The newline cache: remembering which sections of text have no newlines.  */
 
 /* If the user has requested newline caching, make sure it's on.
@@ -564,8 +607,7 @@ fast_string_match_ignore_case (regexp, string)
    This is our cheezy way of associating an action with the change of
    state of a buffer-local variable.  */
 static void
-newline_cache_on_off (buf)
-     struct buffer *buf;
+newline_cache_on_off (struct buffer *buf)
 {
   if (NILP (buf->cache_long_line_scans))
     {
@@ -605,13 +647,9 @@ newline_cache_on_off (buf)
    If ALLOW_QUIT is non-zero, set immediate_quit.  That's good to do
    except when inside redisplay.  */
 
-int
-scan_buffer (target, start, end, count, shortage, allow_quit)
-     register int target;
-     int start, end;
-     int count;
-     int *shortage;
-     int allow_quit;
+EMACS_INT
+scan_buffer (register int target, EMACS_INT start, EMACS_INT end,
+            EMACS_INT count, int *shortage, int allow_quit)
 {
   struct region_cache *newline_cache;
   int direction;
@@ -643,15 +681,15 @@ scan_buffer (target, start, end, count, shortage, allow_quit)
            the position of the last character before the next such
            obstacle --- the last character the dumb search loop should
            examine.  */
-       int ceiling_byte = CHAR_TO_BYTE (end) - 1;
-       int start_byte = CHAR_TO_BYTE (start);
-       int tem;
+       EMACS_INT ceiling_byte = CHAR_TO_BYTE (end) - 1;
+       EMACS_INT start_byte = CHAR_TO_BYTE (start);
+       EMACS_INT tem;
 
         /* If we're looking for a newline, consult the newline cache
            to see where we can avoid some scanning.  */
         if (target == '\n' && newline_cache)
           {
-            int next_change;
+            EMACS_INT next_change;
             immediate_quit = 0;
             while (region_cache_forward
                    (current_buffer, newline_cache, start_byte, &next_change))
@@ -716,14 +754,14 @@ scan_buffer (target, start, end, count, shortage, allow_quit)
     while (start > end)
       {
         /* The last character to check before the next obstacle.  */
-       int ceiling_byte = CHAR_TO_BYTE (end);
-       int start_byte = CHAR_TO_BYTE (start);
-       int tem;
+       EMACS_INT ceiling_byte = CHAR_TO_BYTE (end);
+       EMACS_INT start_byte = CHAR_TO_BYTE (start);
+       EMACS_INT tem;
 
         /* Consult the newline cache, if appropriate.  */
         if (target == '\n' && newline_cache)
           {
-            int next_change;
+            EMACS_INT next_change;
             immediate_quit = 0;
             while (region_cache_backward
                    (current_buffer, newline_cache, start_byte, &next_change))
@@ -802,19 +840,17 @@ scan_buffer (target, start, end, count, shortage, allow_quit)
    If ALLOW_QUIT is non-zero, set immediate_quit.  That's good to do
    except in special cases.  */
 
-int
-scan_newline (start, start_byte, limit, limit_byte, count, allow_quit)
-     int start, start_byte;
-     int limit, limit_byte;
-     register int count;
-     int allow_quit;
+EMACS_INT
+scan_newline (EMACS_INT start, EMACS_INT start_byte,
+             EMACS_INT limit, EMACS_INT limit_byte,
+             register EMACS_INT count, int allow_quit)
 {
   int direction = ((count > 0) ? 1 : -1);
 
   register unsigned char *cursor;
   unsigned char *base;
 
-  register int ceiling;
+  EMACS_INT ceiling;
   register unsigned char *ceiling_addr;
 
   int old_immediate_quit = immediate_quit;
@@ -900,9 +936,8 @@ scan_newline (start, start_byte, limit, limit_byte, count, allow_quit)
   return count * direction;
 }
 
-int
-find_next_newline_no_quit (from, cnt)
-     register int from, cnt;
+EMACS_INT
+find_next_newline_no_quit (EMACS_INT from, EMACS_INT cnt)
 {
   return scan_buffer ('\n', from, 0, cnt, (int *) 0, 0);
 }
@@ -911,12 +946,11 @@ find_next_newline_no_quit (from, cnt)
    not after, and only search up to TO.  This isn't just
    find_next_newline (...)-1, because you might hit TO.  */
 
-int
-find_before_next_newline (from, to, cnt)
-     int from, to, cnt;
+EMACS_INT
+find_before_next_newline (EMACS_INT from, EMACS_INT to, EMACS_INT cnt)
 {
   int shortage;
-  int pos = scan_buffer ('\n', from, to, cnt, &shortage, 1);
+  EMACS_INT pos = scan_buffer ('\n', from, to, cnt, &shortage, 1);
 
   if (shortage == 0)
     pos--;
@@ -927,14 +961,11 @@ find_before_next_newline (from, to, cnt)
 /* Subroutines of Lisp buffer search functions. */
 
 static Lisp_Object
-search_command (string, bound, noerror, count, direction, RE, posix)
-     Lisp_Object string, bound, noerror, count;
-     int direction;
-     int RE;
-     int posix;
+search_command (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror,
+               Lisp_Object count, int direction, int RE, int posix)
 {
   register int np;
-  int lim, lim_byte;
+  EMACS_INT lim, lim_byte;
   int n = direction;
 
   if (!NILP (count))
@@ -1008,10 +1039,9 @@ search_command (string, bound, noerror, count, direction, RE, posix)
 /* Return 1 if REGEXP it matches just one constant string.  */
 
 static int
-trivial_regexp_p (regexp)
-     Lisp_Object regexp;
+trivial_regexp_p (Lisp_Object regexp)
 {
-  int len = SBYTES (regexp);
+  EMACS_INT len = SBYTES (regexp);
   unsigned char *s = SDATA (regexp);
   while (--len >= 0)
     {
@@ -1074,22 +1104,13 @@ while (0)
    (i.e. Vinhibit_changing_match_data is non-nil).  */
 static struct re_registers search_regs_1;
 
-static int
-search_buffer (string, pos, pos_byte, lim, lim_byte, n,
-              RE, trt, inverse_trt, posix)
-     Lisp_Object string;
-     int pos;
-     int pos_byte;
-     int lim;
-     int lim_byte;
-     int n;
-     int RE;
-     Lisp_Object trt;
-     Lisp_Object inverse_trt;
-     int posix;
+static EMACS_INT
+search_buffer (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte,
+              EMACS_INT lim, EMACS_INT lim_byte, EMACS_INT n,
+              int RE, Lisp_Object trt, Lisp_Object inverse_trt, int posix)
 {
-  int len = SCHARS (string);
-  int len_byte = SBYTES (string);
+  EMACS_INT len = SCHARS (string);
+  EMACS_INT len_byte = SBYTES (string);
   register int i;
 
   if (running_asynch_code)
@@ -1106,7 +1127,7 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
   if (RE && !(trivial_regexp_p (string) && NILP (Vsearch_spaces_regexp)))
     {
       unsigned char *p1, *p2;
-      int s1, s2;
+      EMACS_INT s1, s2;
       struct re_pattern_buffer *bufp;
 
       bufp = compile_pattern (string,
@@ -1142,7 +1163,7 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
 
       while (n < 0)
        {
-         int val;
+         EMACS_INT val;
          val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2,
                             pos_byte - BEGV_BYTE, lim_byte - pos_byte,
                             (NILP (Vinhibit_changing_match_data)
@@ -1186,7 +1207,7 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
        }
       while (n > 0)
        {
-         int val;
+         EMACS_INT val;
          val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2,
                             pos_byte - BEGV_BYTE, lim_byte - pos_byte,
                             (NILP (Vinhibit_changing_match_data)
@@ -1231,8 +1252,8 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
   else                         /* non-RE case */
     {
       unsigned char *raw_pattern, *pat;
-      int raw_pattern_size;
-      int raw_pattern_size_byte;
+      EMACS_INT raw_pattern_size;
+      EMACS_INT raw_pattern_size_byte;
       unsigned char *patbuf;
       int multibyte = !NILP (current_buffer->enable_multibyte_characters);
       unsigned char *base_pat;
@@ -1247,7 +1268,7 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
 
       if (multibyte == STRING_MULTIBYTE (string))
        {
-         raw_pattern = (unsigned char *) SDATA (string);
+         raw_pattern = SDATA (string);
          raw_pattern_size = SCHARS (string);
          raw_pattern_size_byte = SBYTES (string);
        }
@@ -1309,7 +1330,7 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
                  base_pat++;
                }
 
-             c = STRING_CHAR_AND_LENGTH (base_pat, len_byte, in_charlen);
+             c = STRING_CHAR_AND_LENGTH (base_pat, in_charlen);
 
              if (NILP (trt))
                {
@@ -1340,11 +1361,7 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
                              if (this_char_base > 0)
                                boyer_moore_ok = 0;
                              else
-                               {
-                                 this_char_base = 0;
-                                 if (char_base < 0)
-                                   char_base = this_char_base;
-                               }
+                               this_char_base = 0;
                            }
                          else if (CHAR_BYTE8_P (inverse))
                            /* Boyer-moore search can't handle a
@@ -1356,8 +1373,7 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
                              this_char_base = inverse & ~0x3F;
                              if (char_base < 0)
                                char_base = this_char_base;
-                             else if (char_base > 0
-                                      && this_char_base != char_base)
+                             else if (this_char_base != char_base)
                                boyer_moore_ok = 0;
                            }
                          else if ((inverse & ~0x3F) != this_char_base)
@@ -1368,15 +1384,18 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
                        }
                    }
                }
-             if (char_base < 0)
-               char_base = 0;
 
              /* Store this character into the translated pattern.  */
-             bcopy (str, pat, charlen);
+             memcpy (pat, str, charlen);
              pat += charlen;
              base_pat += in_charlen;
              len_byte -= in_charlen;
            }
+
+         /* If char_base is still negative we didn't find any translated
+            non-ASCII characters.  */
+         if (char_base < 0)
+           char_base = 0;
        }
       else
        {
@@ -1427,20 +1446,17 @@ search_buffer (string, pos, pos_byte, lim, lim_byte, n,
    regardless of what is in TRT.  It is used in cases where
    boyer_moore cannot work.  */
 
-static int
-simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
-     int n;
-     unsigned char *pat;
-     int len, len_byte;
-     Lisp_Object trt;
-     int pos, pos_byte;
-     int lim, lim_byte;
+static EMACS_INT
+simple_search (EMACS_INT n, unsigned char *pat,
+              EMACS_INT len, EMACS_INT len_byte, Lisp_Object trt,
+              EMACS_INT pos, EMACS_INT pos_byte,
+              EMACS_INT lim, EMACS_INT lim_byte)
 {
   int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
   int forward = n > 0;
   /* Number of buffer bytes matched.  Note that this may be different
      from len_byte in a multibyte buffer.  */
-  int match_byte;
+  EMACS_INT match_byte;
 
   if (lim > pos && multibyte)
     while (n > 0)
@@ -1448,10 +1464,9 @@ simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
        while (1)
          {
            /* Try matching at position POS.  */
-           int this_pos = pos;
-           int this_pos_byte = pos_byte;
-           int this_len = len;
-           int this_len_byte = len_byte;
+           EMACS_INT this_pos = pos;
+           EMACS_INT this_pos_byte = pos_byte;
+           EMACS_INT this_len = len;
            unsigned char *p = pat;
            if (pos + len > lim || pos_byte + len_byte > lim_byte)
              goto stop;
@@ -1461,16 +1476,14 @@ simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
                int charlen, buf_charlen;
                int pat_ch, buf_ch;
 
-               pat_ch = STRING_CHAR_AND_LENGTH (p, this_len_byte, charlen);
+               pat_ch = STRING_CHAR_AND_LENGTH (p, charlen);
                buf_ch = STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte),
-                                                ZV_BYTE - this_pos_byte,
                                                 buf_charlen);
                TRANSLATE (buf_ch, trt, buf_ch);
 
                if (buf_ch != pat_ch)
                  break;
 
-               this_len_byte -= charlen;
                this_len--;
                p += charlen;
 
@@ -1497,8 +1510,8 @@ simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
        while (1)
          {
            /* Try matching at position POS.  */
-           int this_pos = pos;
-           int this_len = len;
+           EMACS_INT this_pos = pos;
+           EMACS_INT this_len = len;
            unsigned char *p = pat;
 
            if (pos + len > lim)
@@ -1536,42 +1549,36 @@ simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
        while (1)
          {
            /* Try matching at position POS.  */
-           int this_pos = pos - len;
-           int this_pos_byte;
-           int this_len = len;
-           int this_len_byte = len_byte;
-           unsigned char *p = pat;
+           EMACS_INT this_pos = pos;
+           EMACS_INT this_pos_byte = pos_byte;
+           EMACS_INT this_len = len;
+           const unsigned char *p = pat + len_byte;
 
-           if (this_pos < lim || (pos_byte - len_byte) < lim_byte)
+           if (this_pos - len < lim || (pos_byte - len_byte) < lim_byte)
              goto stop;
-           this_pos_byte = CHAR_TO_BYTE (this_pos);
-           match_byte = pos_byte - this_pos_byte;
 
            while (this_len > 0)
              {
-               int charlen, buf_charlen;
+               int charlen;
                int pat_ch, buf_ch;
 
-               pat_ch = STRING_CHAR_AND_LENGTH (p, this_len_byte, charlen);
-               buf_ch = STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte),
-                                                ZV_BYTE - this_pos_byte,
-                                                buf_charlen);
+               DEC_BOTH (this_pos, this_pos_byte);
+               PREV_CHAR_BOUNDARY (p, pat);
+               pat_ch = STRING_CHAR (p);
+               buf_ch = STRING_CHAR (BYTE_POS_ADDR (this_pos_byte));
                TRANSLATE (buf_ch, trt, buf_ch);
 
                if (buf_ch != pat_ch)
                  break;
 
-               this_len_byte -= charlen;
                this_len--;
-               p += charlen;
-               this_pos_byte += buf_charlen;
-               this_pos++;
              }
 
            if (this_len == 0)
              {
-               pos -= len;
-               pos_byte -= match_byte;
+               match_byte = pos_byte - this_pos_byte;
+               pos = this_pos;
+               pos_byte = this_pos_byte;
                break;
              }
 
@@ -1586,8 +1593,8 @@ simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
        while (1)
          {
            /* Try matching at position POS.  */
-           int this_pos = pos - len;
-           int this_len = len;
+           EMACS_INT this_pos = pos - len;
+           EMACS_INT this_len = len;
            unsigned char *p = pat;
 
            if (this_pos < lim)
@@ -1649,25 +1656,21 @@ simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
 
    If that criterion is not satisfied, do not call this function.  */
 
-static int
-boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
-            pos, pos_byte, lim, lim_byte, char_base)
-     int n;
-     unsigned char *base_pat;
-     int len, len_byte;
-     Lisp_Object trt;
-     Lisp_Object inverse_trt;
-     int pos, pos_byte;
-     int lim, lim_byte;
-     int char_base;
+static EMACS_INT
+boyer_moore (EMACS_INT n, unsigned char *base_pat,
+            EMACS_INT len, EMACS_INT len_byte,
+            Lisp_Object trt, Lisp_Object inverse_trt,
+            EMACS_INT pos, EMACS_INT pos_byte,
+            EMACS_INT lim, EMACS_INT lim_byte, int char_base)
 {
   int direction = ((n > 0) ? 1 : -1);
-  register int dirlen;
-  int infinity, limit, stride_for_teases = 0;
-  register int *BM_tab;
-  int *BM_tab_base;
+  register EMACS_INT dirlen;
+  EMACS_INT limit;
+  int stride_for_teases = 0;
+  int BM_tab[0400];
   register unsigned char *cursor, *p_limit;
-  register int i, j;
+  register EMACS_INT i;
+  register int j;
   unsigned char *pat, *pat_end;
   int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
 
@@ -1681,37 +1684,28 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
   int translate_prev_byte3 = 0;
   int translate_prev_byte4 = 0;
 
-  BM_tab = (int *) alloca (0400 * sizeof (int));
-
-  /* The general approach is that we are going to maintain that we know */
-  /* the first (closest to the present position, in whatever direction */
-  /* we're searching) character that could possibly be the last */
-  /* (furthest from present position) character of a valid match.  We */
-  /* advance the state of our knowledge by looking at that character */
-  /* and seeing whether it indeed matches the last character of the */
-  /* pattern.  If it does, we take a closer look.  If it does not, we */
-  /* move our pointer (to putative last characters) as far as is */
-  /* logically possible.  This amount of movement, which I call a */
-  /* stride, will be the length of the pattern if the actual character */
-  /* appears nowhere in the pattern, otherwise it will be the distance */
-  /* from the last occurrence of that character to the end of the */
-  /* pattern. */
-  /* As a coding trick, an enormous stride is coded into the table for */
-  /* characters that match the last character.  This allows use of only */
-  /* a single test, a test for having gone past the end of the */
-  /* permissible match region, to test for both possible matches (when */
-  /* the stride goes past the end immediately) and failure to */
-  /* match (where you get nudged past the end one stride at a time). */
-
-  /* Here we make a "mickey mouse" BM table.  The stride of the search */
-  /* is determined only by the last character of the putative match. */
-  /* If that character does not match, we will stride the proper */
-  /* distance to propose a match that superimposes it on the last */
-  /* instance of a character that matches it (per trt), or misses */
-  /* it entirely if there is none. */
+  /* The general approach is that we are going to maintain that we know
+     the first (closest to the present position, in whatever direction
+     we're searching) character that could possibly be the last
+     (furthest from present position) character of a valid match.  We
+     advance the state of our knowledge by looking at that character
+     and seeing whether it indeed matches the last character of the
+     pattern.  If it does, we take a closer look.  If it does not, we
+     move our pointer (to putative last characters) as far as is
+     logically possible.  This amount of movement, which I call a
+     stride, will be the length of the pattern if the actual character
+     appears nowhere in the pattern, otherwise it will be the distance
+     from the last occurrence of that character to the end of the
+     pattern.  If the amount is zero we have a possible match.  */
+
+  /* Here we make a "mickey mouse" BM table.  The stride of the search
+     is determined only by the last character of the putative match.
+     If that character does not match, we will stride the proper
+     distance to propose a match that superimposes it on the last
+     instance of a character that matches it (per trt), or misses
+     it entirely if there is none. */
 
   dirlen = len_byte * direction;
-  infinity = dirlen - (lim_byte + pos_byte + len_byte + len_byte) * direction;
 
   /* Record position after the end of the pattern.  */
   pat_end = base_pat + len_byte;
@@ -1721,23 +1715,14 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
   if (direction < 0)
     base_pat = pat_end - 1;
 
-  BM_tab_base = BM_tab;
-  BM_tab += 0400;
-  j = dirlen;          /* to get it in a register */
-  /* A character that does not appear in the pattern induces a */
-  /* stride equal to the pattern length. */
-  while (BM_tab_base != BM_tab)
-    {
-      *--BM_tab = j;
-      *--BM_tab = j;
-      *--BM_tab = j;
-      *--BM_tab = j;
-    }
+  /* A character that does not appear in the pattern induces a
+     stride equal to the pattern length.  */
+  for (i = 0; i < 0400; i++)
+    BM_tab[i] = dirlen;
 
   /* We use this for translation, instead of TRT itself.
      We fill this in to handle the characters that actually
      occur in the pattern.  Others don't matter anyway!  */
-  bzero (simple_translate, sizeof simple_translate);
   for (i = 0; i < 0400; i++)
     simple_translate[i] = i;
 
@@ -1762,12 +1747,10 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
     }
 
   i = 0;
-  while (i != infinity)
+  while (i != dirlen)
     {
       unsigned char *ptr = base_pat + i;
       i += direction;
-      if (i == dirlen)
-       i = infinity;
       if (! NILP (trt))
        {
          /* If the byte currently looking at is the last of a
@@ -1785,7 +1768,7 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
 
              while (! (CHAR_HEAD_P (*charstart)))
                charstart--;
-             ch = STRING_CHAR (charstart, ptr - charstart + 1);
+             ch = STRING_CHAR (charstart);
              if (char_base != (ch & ~0x3F))
                ch = -1;
            }
@@ -1795,7 +1778,7 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
          else
            j = *ptr;
 
-         if (i == infinity)
+         if (i == dirlen)
            stride_for_teases = BM_tab[j];
 
          BM_tab[j] = dirlen - i;
@@ -1828,23 +1811,22 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
        {
          j = *ptr;
 
-         if (i == infinity)
+         if (i == dirlen)
            stride_for_teases = BM_tab[j];
          BM_tab[j] = dirlen - i;
        }
-      /* stride_for_teases tells how much to stride if we get a */
-      /* match on the far character but are subsequently */
-      /* disappointed, by recording what the stride would have been */
-      /* for that character if the last character had been */
-      /* different. */
+      /* stride_for_teases tells how much to stride if we get a
+        match on the far character but are subsequently
+        disappointed, by recording what the stride would have been
+        for that character if the last character had been
+        different.  */
     }
-  infinity = dirlen - infinity;
   pos_byte += dirlen - ((direction > 0) ? direction : 0);
   /* loop invariant - POS_BYTE points at where last char (first
      char if reverse) of pattern would align in a possible match.  */
   while (n != 0)
     {
-      int tail_end;
+      EMACS_INT tail_end;
       unsigned char *tail_end_ptr;
 
       /* It's been reported that some (broken) compiler thinks that
@@ -1882,43 +1864,34 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
 
          p_limit = BYTE_POS_ADDR (limit);
          p2 = (cursor = BYTE_POS_ADDR (pos_byte));
-         /* In this loop, pos + cursor - p2 is the surrogate for pos */
+         /* In this loop, pos + cursor - p2 is the surrogate for pos */
          while (1)             /* use one cursor setting as long as i can */
            {
              if (direction > 0) /* worth duplicating */
                {
-                 /* Use signed comparison if appropriate
-                    to make cursor+infinity sure to be > p_limit.
-                    Assuming that the buffer lies in a range of addresses
-                    that are all "positive" (as ints) or all "negative",
-                    either kind of comparison will work as long
-                    as we don't step by infinity.  So pick the kind
-                    that works when we do step by infinity.  */
-                 if ((EMACS_INT) (p_limit + infinity) > (EMACS_INT) p_limit)
-                   while ((EMACS_INT) cursor <= (EMACS_INT) p_limit)
-                     cursor += BM_tab[*cursor];
-                 else
-                   while ((EMACS_UINT) cursor <= (EMACS_UINT) p_limit)
+                 while (cursor <= p_limit)
+                   {
+                     if (BM_tab[*cursor] == 0)
+                       goto hit;
                      cursor += BM_tab[*cursor];
+                   }
                }
              else
                {
-                 if ((EMACS_INT) (p_limit + infinity) < (EMACS_INT) p_limit)
-                   while ((EMACS_INT) cursor >= (EMACS_INT) p_limit)
-                     cursor += BM_tab[*cursor];
-                 else
-                   while ((EMACS_UINT) cursor >= (EMACS_UINT) p_limit)
+                 while (cursor >= p_limit)
+                   {
+                     if (BM_tab[*cursor] == 0)
+                       goto hit;
                      cursor += BM_tab[*cursor];
+                   }
                }
-/* 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 */
-/* happen if you fail to match within the permitted region and would */
-/* otherwise try a character beyond that region */
-             if ((cursor - p_limit) * direction <= len_byte)
-               break;  /* a small overrun is genuine */
-             cursor -= infinity; /* large overrun = hit */
+             /* If you are here, cursor is beyond the end of the
+                searched region.  You fail to match within the
+                permitted region and would otherwise try a character
+                beyond that region.  */
+             break;
+
+           hit:
              i = dirlen - direction;
              if (! NILP (trt))
                {
@@ -1957,7 +1930,7 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
              cursor += dirlen - i - direction; /* fix cursor */
              if (i + direction == 0)
                {
-                 int position, start, end;
+                 EMACS_INT position, start, end;
 
                  cursor -= direction;
 
@@ -1990,8 +1963,8 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
          pos_byte += cursor - p2;
        }
       else
-       /* Now we'll pick up a clump that has to be done the hard */
-       /* way because it covers a discontinuity */
+       /* Now we'll pick up a clump that has to be done the hard
+          way because it covers a discontinuity.  */
        {
          limit = ((direction > 0)
                   ? BUFFER_CEILING_OF (pos_byte - dirlen + 1)
@@ -2003,19 +1976,21 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
             and still be valid for a possible match.  */
          while (1)
            {
-             /* This loop can be coded for space rather than */
-             /* speed because it will usually run only once. */
-             /* (the reach is at most len + 21, and typically */
-             /* does not exceed len) */
+             /* This loop can be coded for space rather than
+                speed because it will usually run only once.
+                (the reach is at most len + 21, and typically
+                does not exceed len).  */
              while ((limit - pos_byte) * direction >= 0)
-               pos_byte += BM_tab[FETCH_BYTE (pos_byte)];
-             /* now run the same tests to distinguish going off the */
-             /* end, a match or a phony match. */
-             if ((pos_byte - limit) * direction <= len_byte)
-               break;  /* ran off the end */
-             /* Found what might be a match.
-                Set POS_BYTE back to last (first if reverse) pos.  */
-             pos_byte -= infinity;
+               {
+                 int ch = FETCH_BYTE (pos_byte);
+                 if (BM_tab[ch] == 0)
+                   goto hit2;
+                 pos_byte += BM_tab[ch];
+               }
+             break;    /* ran off the end */
+
+           hit2:
+             /* Found what might be a match.  */
              i = dirlen - direction;
              while ((i -= direction) + direction != 0)
                {
@@ -2044,10 +2019,10 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
              /* Above loop has moved POS_BYTE part or all the way
                 back to the first pos (last pos if reverse).
                 Set it once again at the last (first if reverse) char.  */
-             pos_byte += dirlen - i- direction;
+             pos_byte += dirlen - i - direction;
              if (i + direction == 0)
                {
-                 int position, start, end;
+                 EMACS_INT position, start, end;
                  pos_byte -= direction;
 
                  position = pos_byte + ((direction > 0) ? 1 - len_byte : 0);
@@ -2088,8 +2063,7 @@ boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
    Also clear out the match data for registers 1 and up.  */
 
 static void
-set_search_regs (beg_byte, nbytes)
-     int beg_byte, nbytes;
+set_search_regs (EMACS_INT beg_byte, EMACS_INT nbytes)
 {
   int i;
 
@@ -2117,19 +2091,20 @@ set_search_regs (beg_byte, nbytes)
   XSETBUFFER (last_thing_searched, current_buffer);
 }
 \f
-/* Given a string of words separated by word delimiters,
-  compute a regexp that matches those exact words
-  separated by arbitrary punctuation.  */
+/* Given STRING, a string of words separated by word delimiters,
+   compute a regexp that matches those exact words separated by
+   arbitrary punctuation.  If LAX is nonzero, the end of the string
+   need not match a word boundary unless it ends in whitespace.  */
 
 static Lisp_Object
-wordify (string)
-     Lisp_Object string;
+wordify (Lisp_Object string, int lax)
 {
   register unsigned char *p, *o;
-  register int i, i_byte, len, punct_count = 0, word_count = 0;
+  register EMACS_INT i, i_byte, len, punct_count = 0, word_count = 0;
   Lisp_Object val;
   int prev_c = 0;
-  int adjust;
+  EMACS_INT adjust;
+  int whitespace_at_end;
 
   CHECK_STRING (string);
   p = SDATA (string);
@@ -2152,11 +2127,18 @@ wordify (string)
     }
 
   if (SYNTAX (prev_c) == Sword)
-    word_count++;
+    {
+      word_count++;
+      whitespace_at_end = 0;
+    }
+  else
+    whitespace_at_end = 1;
+
   if (!word_count)
     return empty_unibyte_string;
 
-  adjust = - punct_count + 5 * (word_count - 1) + 4;
+  adjust = - punct_count + 5 * (word_count - 1)
+    + ((lax && !whitespace_at_end) ? 2 : 4);
   if (STRING_MULTIBYTE (string))
     val = make_uninit_multibyte_string (len + adjust,
                                        SBYTES (string)
@@ -2172,14 +2154,13 @@ wordify (string)
   for (i = 0, i_byte = 0; i < len; )
     {
       int c;
-      int i_byte_orig = i_byte;
+      EMACS_INT i_byte_orig = i_byte;
 
       FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c, string, i, i_byte);
 
       if (SYNTAX (c) == Sword)
        {
-         bcopy (SDATA (string) + i_byte_orig, o,
-                i_byte - i_byte_orig);
+         memcpy (o, SDATA (string) + i_byte_orig, i_byte - i_byte_orig);
          o += i_byte - i_byte_orig;
        }
       else if (i > 0 && SYNTAX (prev_c) == Sword && --word_count)
@@ -2194,8 +2175,11 @@ wordify (string)
       prev_c = c;
     }
 
-  *o++ = '\\';
-  *o++ = 'b';
+  if (!lax || whitespace_at_end)
+    {
+      *o++ = '\\';
+      *o++ = 'b';
+    }
 
   return val;
 }
@@ -2214,8 +2198,7 @@ Search case-sensitivity is determined by the value of the variable
 `case-fold-search', which see.
 
 See also the functions `match-beginning', `match-end' and `replace-match'.  */)
-     (string, bound, noerror, count)
-     Lisp_Object string, bound, noerror, count;
+  (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
   return search_command (string, bound, noerror, count, -1, 0, 0);
 }
@@ -2234,8 +2217,7 @@ Search case-sensitivity is determined by the value of the variable
 `case-fold-search', which see.
 
 See also the functions `match-beginning', `match-end' and `replace-match'.  */)
-     (string, bound, noerror, count)
-     Lisp_Object string, bound, noerror, count;
+  (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
   return search_command (string, bound, noerror, count, 1, 0, 0);
 }
@@ -2249,10 +2231,9 @@ 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.  */)
-     (string, bound, noerror, count)
-     Lisp_Object string, bound, noerror, count;
+  (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
-  return search_command (wordify (string), bound, noerror, count, -1, 1, 0);
+  return search_command (wordify (string, 0), bound, noerror, count, -1, 1, 0);
 }
 
 DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4,
@@ -2264,10 +2245,45 @@ 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.  */)
-     (string, bound, noerror, count)
-     Lisp_Object string, bound, noerror, count;
+  (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
+{
+  return search_command (wordify (string, 0), bound, noerror, count, 1, 1, 0);
+}
+
+DEFUN ("word-search-backward-lax", Fword_search_backward_lax, Sword_search_backward_lax, 1, 4,
+       "sWord search backward: ",
+       doc: /* Search backward from point for STRING, ignoring differences in punctuation.
+Set point to the beginning of the occurrence found, and return point.
+
+Unlike `word-search-backward', the end of STRING need not match a word
+boundary unless it ends in whitespace.
+
+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.  */)
+  (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
+{
+  return search_command (wordify (string, 1), bound, noerror, count, -1, 1, 0);
+}
+
+DEFUN ("word-search-forward-lax", Fword_search_forward_lax, Sword_search_forward_lax, 1, 4,
+       "sWord search: ",
+       doc: /* Search forward from point for STRING, ignoring differences in punctuation.
+Set point to the end of the occurrence found, and return point.
+
+Unlike `word-search-forward', the end of STRING need not match a word
+boundary unless it ends in whitespace.
+
+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.  */)
+  (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
-  return search_command (wordify (string), bound, noerror, count, 1, 1, 0);
+  return search_command (wordify (string, 1), bound, noerror, count, 1, 1, 0);
 }
 
 DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4,
@@ -2283,8 +2299,7 @@ Optional third argument, if t, means if fail just return nil (no error).
 Optional fourth argument is repeat count--search for successive occurrences.
 See also the functions `match-beginning', `match-end', `match-string',
 and `replace-match'.  */)
-     (regexp, bound, noerror, count)
-     Lisp_Object regexp, bound, noerror, count;
+  (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
   return search_command (regexp, bound, noerror, count, -1, 1, 0);
 }
@@ -2300,8 +2315,7 @@ Optional third argument, if t, means if fail just return nil (no error).
 Optional fourth argument is repeat count--search for successive occurrences.
 See also the functions `match-beginning', `match-end', `match-string',
 and `replace-match'.  */)
-     (regexp, bound, noerror, count)
-     Lisp_Object regexp, bound, noerror, count;
+  (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
   return search_command (regexp, bound, noerror, count, 1, 1, 0);
 }
@@ -2320,8 +2334,7 @@ Optional third argument, if t, means if fail just return nil (no error).
 Optional fourth argument is repeat count--search for successive occurrences.
 See also the functions `match-beginning', `match-end', `match-string',
 and `replace-match'.  */)
-     (regexp, bound, noerror, count)
-     Lisp_Object regexp, bound, noerror, count;
+  (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
   return search_command (regexp, bound, noerror, count, -1, 1, 1);
 }
@@ -2338,8 +2351,7 @@ Optional third argument, if t, means if fail just return nil (no error).
 Optional fourth argument is repeat count--search for successive occurrences.
 See also the functions `match-beginning', `match-end', `match-string',
 and `replace-match'.  */)
-     (regexp, bound, noerror, count)
-     Lisp_Object regexp, bound, noerror, count;
+  (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
 {
   return search_command (regexp, bound, noerror, count, 1, 1, 1);
 }
@@ -2380,18 +2392,17 @@ This is, in a vague sense, the inverse of using `\\N' in NEWTEXT;
 NEWTEXT in place of subexp N.
 This is useful only after a regular expression search or match,
 since only regular expressions have distinguished subexpressions.  */)
-     (newtext, fixedcase, literal, string, subexp)
-     Lisp_Object newtext, fixedcase, literal, string, subexp;
+  (Lisp_Object newtext, Lisp_Object fixedcase, Lisp_Object literal, Lisp_Object string, Lisp_Object subexp)
 {
   enum { nochange, all_caps, cap_initial } case_action;
-  register int pos, pos_byte;
+  register EMACS_INT pos, pos_byte;
   int some_multiletter_word;
   int some_lowercase;
   int some_uppercase;
   int some_nonuppercase_initial;
   register int c, prevc;
   int sub;
-  int opoint, newpoint;
+  EMACS_INT opoint, newpoint;
 
   CHECK_STRING (newtext);
 
@@ -2434,7 +2445,7 @@ since only regular expressions have distinguished subexpressions.  */)
   if (NILP (fixedcase))
     {
       /* Decide how to casify by examining the matched text. */
-      int last;
+      EMACS_INT last;
 
       pos = search_regs.start[sub];
       last = search_regs.end[sub];
@@ -2521,8 +2532,8 @@ since only regular expressions have distinguished subexpressions.  */)
         if desired.  */
       if (NILP (literal))
        {
-         int lastpos = 0;
-         int lastpos_byte = 0;
+         EMACS_INT lastpos = 0;
+         EMACS_INT lastpos_byte = 0;
          /* We build up the substituted string in ACCUM.  */
          Lisp_Object accum;
          Lisp_Object middle;
@@ -2532,8 +2543,8 @@ since only regular expressions have distinguished subexpressions.  */)
 
          for (pos_byte = 0, pos = 0; pos_byte < length;)
            {
-             int substart = -1;
-             int subend = 0;
+             EMACS_INT substart = -1;
+             EMACS_INT subend = 0;
              int delbackslash = 0;
 
              FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
@@ -2626,9 +2637,9 @@ since only regular expressions have distinguished subexpressions.  */)
      perform substitution on the replacement string.  */
   if (NILP (literal))
     {
-      int length = SBYTES (newtext);
+      EMACS_INT length = SBYTES (newtext);
       unsigned char *substed;
-      int substed_alloc_size, substed_len;
+      EMACS_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;
@@ -2647,8 +2658,8 @@ since only regular expressions have distinguished subexpressions.  */)
       for (pos_byte = 0, pos = 0; pos_byte < length;)
        {
          unsigned char str[MAX_MULTIBYTE_LENGTH];
-         unsigned char *add_stuff = NULL;
-         int add_len = 0;
+         const unsigned char *add_stuff = NULL;
+         EMACS_INT add_len = 0;
          int idx = -1;
 
          if (str_multibyte)
@@ -2662,7 +2673,7 @@ since only regular expressions have distinguished subexpressions.  */)
              /* Note that we don't have to increment POS.  */
              c = SREF (newtext, pos_byte++);
              if (buf_multibyte)
-               c = unibyte_char_to_multibyte (c);
+               MAKE_CHAR_MULTIBYTE (c);
            }
 
          /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
@@ -2684,7 +2695,7 @@ since only regular expressions have distinguished subexpressions.  */)
                {
                  c = SREF (newtext, pos_byte++);
                  if (buf_multibyte)
-                   c = unibyte_char_to_multibyte (c);
+                   MAKE_CHAR_MULTIBYTE (c);
                }
 
              if (c == '&')
@@ -2712,7 +2723,7 @@ since only regular expressions have distinguished subexpressions.  */)
             set up ADD_STUFF and ADD_LEN to point to it.  */
          if (idx >= 0)
            {
-             int begbyte = CHAR_TO_BYTE (search_regs.start[idx]);
+             EMACS_INT begbyte = CHAR_TO_BYTE (search_regs.start[idx]);
              add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte;
              if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx])
                move_gap (search_regs.start[idx]);
@@ -2733,7 +2744,7 @@ since only regular expressions have distinguished subexpressions.  */)
          /* Now add to the end of SUBSTED.  */
          if (add_stuff)
            {
-             bcopy (add_stuff, substed + substed_len, add_len);
+             memcpy (substed + substed_len, add_stuff, add_len);
              substed_len += add_len;
            }
        }
@@ -2742,7 +2753,8 @@ since only regular expressions have distinguished subexpressions.  */)
        {
          if (buf_multibyte)
            {
-             int nchars = multibyte_chars_in_text (substed, substed_len);
+             EMACS_INT nchars =
+               multibyte_chars_in_text (substed, substed_len);
 
              newtext = make_multibyte_string (substed, nchars, substed_len);
            }
@@ -2766,9 +2778,9 @@ since only regular expressions have distinguished subexpressions.  */)
 
   /* Adjust search data for this change.  */
   {
-    int oldend = search_regs.end[sub];
-    int oldstart = search_regs.start[sub];
-    int change = newpoint - search_regs.end[sub];
+    EMACS_INT oldend = search_regs.end[sub];
+    EMACS_INT oldstart = search_regs.start[sub];
+    EMACS_INT change = newpoint - search_regs.end[sub];
     int i;
 
     for (i = 0; i < search_regs.num_regs; i++)
@@ -2797,9 +2809,7 @@ since only regular expressions have distinguished subexpressions.  */)
 }
 \f
 static Lisp_Object
-match_limit (num, beginningp)
-     Lisp_Object num;
-     int beginningp;
+match_limit (Lisp_Object num, int beginningp)
 {
   register int n;
 
@@ -2823,8 +2833,7 @@ SUBEXP, a number, specifies which parenthesized expression in the last
 Value is nil if SUBEXPth pair didn't match, or there were less than
   SUBEXP pairs.
 Zero means the entire text matched by the whole regexp or whole string.  */)
-     (subexp)
-     Lisp_Object subexp;
+  (Lisp_Object subexp)
 {
   return match_limit (subexp, 1);
 }
@@ -2836,8 +2845,7 @@ SUBEXP, a number, specifies which parenthesized expression in the last
 Value is nil if SUBEXPth pair didn't match, or there were less than
   SUBEXP pairs.
 Zero means the entire text matched by the whole regexp or whole string.  */)
-     (subexp)
-     Lisp_Object subexp;
+  (Lisp_Object subexp)
 {
   return match_limit (subexp, 0);
 }
@@ -2847,7 +2855,7 @@ DEFUN ("match-data", Fmatch_data, Smatch_data, 0, 3, 0,
 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.
+Use `set-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.  In
@@ -2862,8 +2870,7 @@ If optional third arg RESEAT is non-nil, any previous markers on the
 REUSE list will be modified to point to nowhere.
 
 Return value is undefined if the last search failed.  */)
-  (integers, reuse, reseat)
-     Lisp_Object integers, reuse, reseat;
+  (Lisp_Object integers, Lisp_Object reuse, Lisp_Object reseat)
 {
   Lisp_Object tail, prev;
   Lisp_Object *data;
@@ -2963,8 +2970,7 @@ DEFUN ("set-match-data", Fset_match_data, Sset_match_data, 1, 2, 0,
 LIST should have been created by calling `match-data' previously.
 
 If optional arg RESEAT is non-nil, make markers on LIST point nowhere.  */)
-    (list, reseat)
-     register Lisp_Object list, reseat;
+  (register Lisp_Object list, Lisp_Object reseat)
 {
   register int i;
   register Lisp_Object marker;
@@ -3024,7 +3030,7 @@ If optional arg RESEAT is non-nil, make markers on LIST point nowhere.  */)
          }
        else
          {
-           int from;
+           EMACS_INT from;
            Lisp_Object m;
 
            m = marker;
@@ -3082,7 +3088,7 @@ static Lisp_Object saved_last_thing_searched;
 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
    if asynchronous code (filter or sentinel) is running. */
 static void
-save_search_regs ()
+save_search_regs (void)
 {
   if (!search_regs_saved)
     {
@@ -3101,7 +3107,7 @@ save_search_regs ()
 
 /* Called upon exit from filters and sentinels. */
 void
-restore_search_regs ()
+restore_search_regs (void)
 {
   if (search_regs_saved)
     {
@@ -3120,8 +3126,7 @@ restore_search_regs ()
 }
 
 static Lisp_Object
-unwind_set_match_data (list)
-     Lisp_Object list;
+unwind_set_match_data (Lisp_Object list)
 {
   /* It is NOT ALWAYS safe to free (evaporate) the markers immediately.  */
   return Fset_match_data (list, Qt);
@@ -3129,7 +3134,7 @@ unwind_set_match_data (list)
 
 /* Called to unwind protect the match data.  */
 void
-record_unwind_save_match_data ()
+record_unwind_save_match_data (void)
 {
   record_unwind_protect (unwind_set_match_data,
                         Fmatch_data (Qnil, Qnil, Qnil));
@@ -3139,8 +3144,7 @@ record_unwind_save_match_data ()
 
 DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0,
        doc: /* Return a regexp string which matches exactly STRING and nothing else.  */)
-     (string)
-     Lisp_Object string;
+  (Lisp_Object string)
 {
   register unsigned char *in, *out, *end;
   register unsigned char *temp;
@@ -3173,7 +3177,7 @@ DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0,
 }
 \f
 void
-syms_of_search ()
+syms_of_search (void)
 {
   register int i;
 
@@ -3192,20 +3196,20 @@ syms_of_search ()
     }
   searchbuf_head = &searchbufs[0];
 
-  Qsearch_failed = intern ("search-failed");
+  Qsearch_failed = intern_c_string ("search-failed");
   staticpro (&Qsearch_failed);
-  Qinvalid_regexp = intern ("invalid-regexp");
+  Qinvalid_regexp = intern_c_string ("invalid-regexp");
   staticpro (&Qinvalid_regexp);
 
   Fput (Qsearch_failed, Qerror_conditions,
-       Fcons (Qsearch_failed, Fcons (Qerror, Qnil)));
+       pure_cons (Qsearch_failed, pure_cons (Qerror, Qnil)));
   Fput (Qsearch_failed, Qerror_message,
-       build_string ("Search failed"));
+       make_pure_c_string ("Search failed"));
 
   Fput (Qinvalid_regexp, Qerror_conditions,
-       Fcons (Qinvalid_regexp, Fcons (Qerror, Qnil)));
+       pure_cons (Qinvalid_regexp, pure_cons (Qerror, Qnil)));
   Fput (Qinvalid_regexp, Qerror_message,
-       build_string ("Invalid regexp"));
+       make_pure_c_string ("Invalid regexp"));
 
   last_thing_searched = Qnil;
   staticpro (&last_thing_searched);
@@ -3213,7 +3217,7 @@ syms_of_search ()
   saved_last_thing_searched = Qnil;
   staticpro (&saved_last_thing_searched);
 
-  DEFVAR_LISP ("search-spaces-regexp", &Vsearch_spaces_regexp,
+  DEFVAR_LISP ("search-spaces-regexp", Vsearch_spaces_regexp,
       doc: /* Regexp to substitute for bunches of spaces in regexp search.
 Some commands use this for user-specified regexps.
 Spaces that occur inside character classes or repetition operators
@@ -3221,7 +3225,7 @@ or other such regexp constructs are not replaced with this.
 A value of nil (which is the normal value) means treat spaces literally.  */);
   Vsearch_spaces_regexp = Qnil;
 
-  DEFVAR_LISP ("inhibit-changing-match-data", &Vinhibit_changing_match_data,
+  DEFVAR_LISP ("inhibit-changing-match-data", Vinhibit_changing_match_data,
       doc: /* Internal use only.
 If non-nil, the primitive searching and matching functions
 such as `looking-at', `string-match', `re-search-forward', etc.,
@@ -3237,6 +3241,8 @@ is to bind it with `let' around a small expression.  */);
   defsubr (&Ssearch_backward);
   defsubr (&Sword_search_forward);
   defsubr (&Sword_search_backward);
+  defsubr (&Sword_search_forward_lax);
+  defsubr (&Sword_search_backward_lax);
   defsubr (&Sre_search_forward);
   defsubr (&Sre_search_backward);
   defsubr (&Sposix_search_forward);
@@ -3248,6 +3254,3 @@ is to bind it with `let' around a small expression.  */);
   defsubr (&Sset_match_data);
   defsubr (&Sregexp_quote);
 }
-
-/* arch-tag: a6059d79-0552-4f14-a2cb-d379a4e3c78f
-   (do not change this comment) */