]> code.delx.au - gnu-emacs/blobdiff - src/regex.c
Merge from emacs--devo--0
[gnu-emacs] / src / regex.c
index 48baacd81e207dd044748ec2f84834e0e4c4ec01..a0d6b945cf1afdfd2c698bbafe74a2f439366841 100644 (file)
@@ -3,22 +3,23 @@
    internationalization features.)
 
    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
-                 2002, 2003, 2004, 2005, 2006  Free Software Foundation, Inc.
+                 2002, 2003, 2004, 2005, 2006, 2007
+                 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
+   the Free Software Foundation; either version 3, or (at your option)
    any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-   USA.         */
+   USA.  */
 
 /* TODO:
    - structure the opcode space into opcode+flag.
@@ -67,8 +68,8 @@
 # define regfree(preg) __regfree (preg)
 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
-# define regerror(errcode, preg, errbuf, errbuf_size) \
-       __regerror(errcode, preg, errbuf, errbuf_size)
+# define regerror(err_code, preg, errbuf, errbuf_size) \
+       __regerror(err_code, preg, errbuf, errbuf_size)
 # define re_set_registers(bu, re, nu, st, en) \
        __re_set_registers (bu, re, nu, st, en)
 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
 # endif
 # define free xfree
 
-/* Converts the pointer to the char to BEG-based offset from the start.         */
+/* Converts the pointer to the char to BEG-based offset from the start.  */
 # define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
 # define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
 
 # define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
 # define RE_TARGET_MULTIBYTE_P(bufp) ((bufp)->target_multibyte)
-# define RE_STRING_CHAR(p, s) \
+# define RE_STRING_CHAR(p, s, multibyte) \
   (multibyte ? (STRING_CHAR (p, s)) : (*(p)))
-# define RE_STRING_CHAR_AND_LENGTH(p, s, len) \
+# define RE_STRING_CHAR_AND_LENGTH(p, s, len, multibyte) \
   (multibyte ? (STRING_CHAR_AND_LENGTH (p, s, len)) : ((len) = 1, *(p)))
 
+# define RE_CHAR_TO_MULTIBYTE(c) unibyte_to_multibyte_table[(c)]
+
+# define RE_CHAR_TO_UNIBYTE(c)                 \
+  (ASCII_CHAR_P (c) ? (c)                      \
+   : CHAR_BYTE8_P (c) ? CHAR_TO_BYTE8 (c)      \
+   : multibyte_char_to_unibyte_safe (c))
+
 /* Set C a (possibly converted to multibyte) character before P.  P
    points into a string which is the virtual concatenation of STR1
    (which ends at END1) or STR2 (which ends at END2).  */
 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2)                    \
   do {                                                                      \
-    if (multibyte)                                                          \
+    if (target_multibyte)                                                   \
       {                                                                             \
        re_char *dtemp = (p) == (str2) ? (end1) : (p);                       \
        re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
     else                                                                    \
       {                                                                             \
        (c = ((p) == (str2) ? (end1) : (p))[-1]);                            \
-       MAKE_CHAR_MULTIBYTE (c);                                             \
+       (c) = RE_CHAR_TO_MULTIBYTE (c);                                      \
       }                                                                             \
   } while (0)
 
    LEN to the byte length of that character.  */
 # define GET_CHAR_AFTER(c, p, len)             \
   do {                                         \
-    if (multibyte)                             \
-      c = STRING_CHAR_AND_LENGTH (p, 0, len);  \
+    if (target_multibyte)                      \
+      (c) = STRING_CHAR_AND_LENGTH (p, 0, len);        \
     else                                       \
       {                                                \
-       c = *p;                                 \
+       (c) = *p;                               \
        len = 1;                                \
-       MAKE_CHAR_MULTIBYTE (c);                \
+       (c) = RE_CHAR_TO_MULTIBYTE (c);         \
       }                                                \
    } while (0)
 
@@ -301,10 +309,12 @@ enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
 # define MULTIBYTE_FORM_LENGTH(p, s) (1)
 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
 # define STRING_CHAR(p, s) (*(p))
-# define RE_STRING_CHAR STRING_CHAR
+# define RE_STRING_CHAR(p, s, multibyte) STRING_CHAR ((p), (s))
 # define CHAR_STRING(c, s) (*(s) = (c), 1)
 # define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
-# define RE_STRING_CHAR_AND_LENGTH STRING_CHAR_AND_LENGTH
+# define RE_STRING_CHAR_AND_LENGTH(p, s, len, multibyte) STRING_CHAR_AND_LENGTH ((p), (s), (len))
+# define RE_CHAR_TO_MULTIBYTE(c) (c)
+# define RE_CHAR_TO_UNIBYTE(c) (c)
 # define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
   (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
 # define GET_CHAR_AFTER(c, p, len)     \
@@ -312,8 +322,6 @@ enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
 # define MAKE_CHAR(charset, c1, c2) (c1)
 # define BYTE8_TO_CHAR(c) (c)
 # define CHAR_BYTE8_P(c) (0)
-# define MAKE_CHAR_MULTIBYTE(c) (c)
-# define MAKE_CHAR_UNIBYTE(c) (c)
 # define CHAR_LEADING_CODE(c) (c)
 
 #endif /* not emacs */
@@ -565,7 +573,7 @@ init_syntax_once ()
 
 #  define REGEX_REALLOCATE_STACK(source, osize, nsize)                 \
    REGEX_REALLOCATE (source, osize, nsize)
-/* No need to explicitly free anything.         */
+/* No need to explicitly free anything.  */
 #  define REGEX_FREE_STACK(arg) ((void)0)
 
 # endif /* not REGEX_MALLOC */
@@ -679,7 +687,7 @@ typedef enum
   jump,
 
        /* Followed by two-byte relative address of place to resume at
-          in case of failure.  */
+          in case of failure.  */
   on_failure_jump,
 
        /* Like on_failure_jump, but pushes a placeholder instead of the
@@ -716,7 +724,7 @@ typedef enum
   jump_n,
 
        /* Set the following two-byte relative address to the
-          subsequent two-byte number.  The address *includes* the two
+          subsequent two-byte number.  The address *includes* the two
           bytes of number.  */
   set_number_at,
 
@@ -724,7 +732,7 @@ typedef enum
   wordend,     /* Succeeds if at word end.  */
 
   wordbound,   /* Succeeds if at a word boundary.  */
-  notwordbound,        /* Succeeds if not at a word boundary.  */
+  notwordbound,        /* Succeeds if not at a word boundary.  */
 
   symbeg,       /* Succeeds if at symbol beginning.  */
   symend,       /* Succeeds if at symbol end.  */
@@ -742,8 +750,8 @@ typedef enum
   after_dot,   /* Succeeds if after point.  */
 
   /* Matches any character whose category-set contains the specified
-     category. The operator is followed by a byte which contains a
-     category code (mnemonic ASCII character). */
+     category.  The operator is followed by a byte which contains a
+     category code (mnemonic ASCII character).  */
   categoryspec,
 
   /* Matches any character whose category-set does not contain the
@@ -832,7 +840,7 @@ extract_number_and_incr (destination, source)
 \f
 /* Store a multibyte character in three contiguous bytes starting
    DESTINATION, and increment DESTINATION to the byte after where the
-   character is stored.         Therefore, DESTINATION must be an lvalue.  */
+   character is stored.  Therefore, DESTINATION must be an lvalue.  */
 
 #define STORE_CHARACTER_AND_INCR(destination, character)       \
   do {                                                         \
@@ -843,7 +851,7 @@ extract_number_and_incr (destination, source)
   } while (0)
 
 /* Put into DESTINATION a character stored in three contiguous bytes
-   starting at SOURCE. */
+   starting at SOURCE.  */
 
 #define EXTRACT_CHARACTER(destination, source) \
   do {                                         \
@@ -879,13 +887,13 @@ extract_number_and_incr (destination, source)
    && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
 
 /* Return the address of end of RANGE_TABLE.  COUNT is number of
-   ranges (which is a pair of (start, end)) in the RANGE_TABLE.         `* 2'
-   is start of range and end of range. `* 3' is size of each start
+   ranges (which is a pair of (start, end)) in the RANGE_TABLE.  `* 2'
+   is start of range and end of range.  `* 3' is size of each start
    and end.  */
 #define CHARSET_RANGE_TABLE_END(range_table, count)    \
   ((range_table) + (count) * 2 * 3)
 
-/* Test if C is in RANGE_TABLE.         A flag NOT is negated if C is in.
+/* Test if C is in RANGE_TABLE.  A flag NOT is negated if C is in.
    COUNT is number of ranges in RANGE_TABLE.  */
 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count)     \
   do                                                                   \
@@ -1390,18 +1398,12 @@ static const char *re_error_msgid[] =
 /* Normally, this is fine.  */
 #define MATCH_MAY_ALLOCATE
 
-/* When using GNU C, we are not REALLY using the C alloca, no matter
-   what config.h may say.  So don't take precautions for it.  */
-#ifdef __GNUC__
-# undef C_ALLOCA
-#endif
-
 /* The match routines may not allocate if (1) they would do it with malloc
    and (2) it's not safe for them to use malloc.
    Note that if REL_ALLOC is defined, matching would not use malloc for the
    failure stack, but we would still use it for the register vectors;
    so REL_ALLOC should not affect this.  */
-#if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
+#if defined REGEX_MALLOC && defined emacs
 # undef MATCH_MAY_ALLOCATE
 #endif
 
@@ -1692,7 +1694,7 @@ do {                                                                      \
      REGSTART, REGEND -- arrays of string positions.
 
    Also assumes the variables `fail_stack' and (if debugging), `bufp',
-   `pend', `string1', `size1', `string2', and `size2'. */
+   `pend', `string1', `size1', `string2', and `size2'.  */
 
 #define POP_FAILURE_POINT(str, pat)                                     \
 do {                                                                   \
@@ -1761,7 +1763,7 @@ static int analyse_first _RE_ARGS ((re_char *p, re_char *pend,
   do {                                                                 \
     int len;                                                           \
     if (p == pend) return REG_EEND;                                    \
-    c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len);                  \
+    c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len, multibyte);       \
     p += len;                                                          \
   } while (0)
 
@@ -1961,7 +1963,7 @@ struct range_table_work_area
 {
   int *table;                  /* actual work area.  */
   int allocated;               /* allocated size for work area in bytes.  */
-  int used;                    /* actually used size in words.  */
+  int used;                    /* actually used size in words.  */
   int bits;                    /* flag to record character classes */
 };
 
@@ -2000,7 +2002,7 @@ struct range_table_work_area
     (work_area).table[(work_area).used++] = (range_end);               \
   } while (0)
 
-/* Free allocated memory for WORK_AREA.         */
+/* Free allocated memory for WORK_AREA.  */
 #define FREE_RANGE_TABLE_WORK_AREA(work_area)  \
   do {                                         \
     if ((work_area).table)                     \
@@ -2019,32 +2021,107 @@ struct range_table_work_area
 
 #ifdef emacs
 
-/* Store characters in the rage range C0 to C1 in WORK_AREA while
-   translating them and paying attention to the continuity of
-   translated characters.
+/* Store characters in the range FROM to TO in the bitmap at B (for
+   ASCII and unibyte characters) and WORK_AREA (for multibyte
+   characters) while translating them and paying attention to the
+   continuity of translated characters.
 
-   Implementation note: It is better to implement this fairly big
-   macro by a function, but it's not that easy because macros called
+   Implementation note: It is better to implement these fairly big
+   macros by a function, but it's not that easy because macros called
    in this macro assume various local variables already declared.  */
 
-#define SETUP_MULTIBYTE_RANGE(work_area, c0, c1)                             \
-  do {                                                                       \
-    re_wchar_t c, t, t_last;                                                 \
-    int n;                                                                   \
-                                                                             \
-    c = (c0);                                                                \
-    t_last = multibyte ? TRANSLATE (c) : TRANSLATE (MAKE_CHAR_MULTIBYTE (c)); \
-    for (c++, n = 1; c <= (c1); c++, n++)                                    \
-      {                                                                              \
-       t = multibyte ? TRANSLATE (c) : TRANSLATE (MAKE_CHAR_MULTIBYTE (c));  \
-       if (t_last + n == t)                                                  \
-         continue;                                                           \
-       SET_RANGE_TABLE_WORK_AREA ((work_area), t_last, t_last + n - 1);      \
-       t_last = t;                                                           \
-       n = 0;                                                                \
-      }                                                                              \
-    if (n > 0)                                                               \
-      SET_RANGE_TABLE_WORK_AREA ((work_area), t_last, t_last + n - 1);       \
+/* Both FROM and TO are ASCII characters.  */
+
+#define SETUP_ASCII_RANGE(work_area, FROM, TO)                 \
+  do {                                                         \
+    int C0, C1;                                                        \
+                                                               \
+    for (C0 = (FROM); C0 <= (TO); C0++)                                \
+      {                                                                \
+       C1 = TRANSLATE (C0);                                    \
+       if (! ASCII_CHAR_P (C1))                                \
+         {                                                     \
+           SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1);    \
+           if ((C1 = RE_CHAR_TO_UNIBYTE (C1)) < 0)             \
+             C1 = C0;                                          \
+         }                                                     \
+       SET_LIST_BIT (C1);                                      \
+      }                                                                \
+  } while (0)
+
+
+/* Both FROM and TO are unibyte characters (0x80..0xFF).  */
+
+#define SETUP_UNIBYTE_RANGE(work_area, FROM, TO)                              \
+  do {                                                                        \
+    int C0, C1, C2, I;                                                        \
+    int USED = RANGE_TABLE_WORK_USED (work_area);                             \
+                                                                              \
+    for (C0 = (FROM); C0 <= (TO); C0++)                                               \
+      {                                                                               \
+       C1 = RE_CHAR_TO_MULTIBYTE (C0);                                        \
+       if (CHAR_BYTE8_P (C1))                                                 \
+         SET_LIST_BIT (C0);                                                   \
+       else                                                                   \
+         {                                                                    \
+           C2 = TRANSLATE (C1);                                               \
+           if (C2 == C1                                                       \
+               || (C1 = RE_CHAR_TO_UNIBYTE (C2)) < 0)                         \
+             C1 = C0;                                                         \
+           SET_LIST_BIT (C1);                                                 \
+           for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
+             {                                                                \
+               int from = RANGE_TABLE_WORK_ELT (work_area, I);                \
+               int to = RANGE_TABLE_WORK_ELT (work_area, I + 1);              \
+                                                                              \
+               if (C2 >= from - 1 && C2 <= to + 1)                            \
+                 {                                                            \
+                   if (C2 == from - 1)                                        \
+                     RANGE_TABLE_WORK_ELT (work_area, I)--;                   \
+                   else if (C2 == to + 1)                                     \
+                     RANGE_TABLE_WORK_ELT (work_area, I + 1)++;               \
+                   break;                                                     \
+                 }                                                            \
+             }                                                                \
+           if (I < USED)                                                      \
+             SET_RANGE_TABLE_WORK_AREA ((work_area), C2, C2);                 \
+         }                                                                    \
+      }                                                                               \
+  } while (0)
+
+
+/* Both FROM and TO are mulitbyte characters.  */
+
+#define SETUP_MULTIBYTE_RANGE(work_area, FROM, TO)                        \
+  do {                                                                    \
+    int C0, C1, C2, I, USED = RANGE_TABLE_WORK_USED (work_area);          \
+                                                                          \
+    SET_RANGE_TABLE_WORK_AREA ((work_area), (FROM), (TO));                \
+    for (C0 = (FROM); C0 <= (TO); C0++)                                           \
+      {                                                                           \
+       C1 = TRANSLATE (C0);                                               \
+       if ((C2 = RE_CHAR_TO_UNIBYTE (C1)) >= 0                            \
+           || (C1 != C0 && (C2 = RE_CHAR_TO_UNIBYTE (C0)) >= 0))          \
+         SET_LIST_BIT (C2);                                               \
+       if (C1 >= (FROM) && C1 <= (TO))                                    \
+         continue;                                                        \
+       for (I = RANGE_TABLE_WORK_USED (work_area) - 2; I >= USED; I -= 2) \
+         {                                                                \
+           int from = RANGE_TABLE_WORK_ELT (work_area, I);                \
+           int to = RANGE_TABLE_WORK_ELT (work_area, I + 1);              \
+                                                                          \
+           if (C1 >= from - 1 && C1 <= to + 1)                            \
+             {                                                            \
+               if (C1 == from - 1)                                        \
+                 RANGE_TABLE_WORK_ELT (work_area, I)--;                   \
+               else if (C1 == to + 1)                                     \
+                 RANGE_TABLE_WORK_ELT (work_area, I + 1)++;               \
+               break;                                                     \
+             }                                                            \
+         }                                                                \
+       if (I < USED)                                                      \
+         SET_RANGE_TABLE_WORK_AREA ((work_area), C1, C1);                 \
+      }                                                                           \
   } while (0)
 
 #endif /* emacs */
@@ -2101,7 +2178,7 @@ re_wctype (str)
   else return 0;
 }
 
-/* True iff CH is in the char class CC.  */
+/* True if CH is in the char class CC.  */
 boolean
 re_iswctype (ch, cc)
      int ch;
@@ -2534,15 +2611,10 @@ regex_compile (pattern, size, syntax, bufp)
   re_char *beg_interval;
 
   /* Address of the place where a forward jump should go to the end of
-     the containing expression.         Each alternative of an `or' -- except the
+     the containing expression.  Each alternative of an `or' -- except the
      last -- ends with a forward jump of this sort.  */
   unsigned char *fixup_alt_jump = 0;
 
-  /* Counts open-groups as they are encountered.  Remembered for the
-     matching close-group on the compile stack, so the same register
-     number is put in the stop_memory as the start_memory.  */
-  regnum_t regnum = 0;
-
   /* Work area for range table of charset.  */
   struct range_table_work_area range_table_work;
 
@@ -2589,6 +2661,7 @@ regex_compile (pattern, size, syntax, bufp)
   bufp->syntax = syntax;
   bufp->fastmap_accurate = 0;
   bufp->not_bol = bufp->not_eol = 0;
+  bufp->used_syntax = 0;
 
   /* Set `used' to zero, so that if we return an error, the pattern
      printer (for debugging) will think there's no pattern.  We reset it
@@ -2608,11 +2681,11 @@ regex_compile (pattern, size, syntax, bufp)
       if (bufp->buffer)
        { /* If zero allocated, but buffer is non-null, try to realloc
             enough space.  This loses if buffer's address is bogus, but
-            that is the user's responsibility.  */
+            that is the user's responsibility.  */
          RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
        }
       else
-       { /* Caller did not allocate a buffer.  Do it for them.  */
+       { /* Caller did not allocate a buffer.  Do it for them.  */
          bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
        }
       if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
@@ -2676,15 +2749,15 @@ regex_compile (pattern, size, syntax, bufp)
            p = pattern = whitespace_regexp;
            pend = p + strlen (p);
            break;
-         }    
+         }
 
        case '^':
          {
-           if (   /* If at start of pattern, it's an operator.  */
+           if (   /* If at start of pattern, it's an operator.  */
                   p == pattern + 1
-                  /* If context independent, it's an operator.  */
+                  /* If context independent, it's an operator.  */
                || syntax & RE_CONTEXT_INDEP_ANCHORS
-                  /* Otherwise, depends on what's come before.  */
+                  /* Otherwise, depends on what's come before.  */
                || at_begline_loc_p (pattern, p, syntax))
              BUF_PUSH ((syntax & RE_NO_NEWLINE_ANCHOR) ? begbuf : begline);
            else
@@ -2697,7 +2770,7 @@ regex_compile (pattern, size, syntax, bufp)
          {
            if (   /* If at end of pattern, it's an operator.  */
                   p == pend
-                  /* If context independent, it's an operator.  */
+                  /* If context independent, it's an operator.  */
                || syntax & RE_CONTEXT_INDEP_ANCHORS
                   /* Otherwise, depends on what's next.  */
                || at_endline_loc_p (p, pend, syntax))
@@ -2725,14 +2798,14 @@ regex_compile (pattern, size, syntax, bufp)
            }
 
          {
-           /* 1 means zero (many) matches is allowed.  */
+           /* 1 means zero (many) matches is allowed.  */
            boolean zero_times_ok = 0, many_times_ok = 0;
            boolean greedy = 1;
 
            /* If there is a sequence of repetition chars, collapse it
               down to just one (the right one).  We can't combine
               interval operators with these because of, e.g., `a{2}*',
-              which should only match an even number of `a's.  */
+              which should only match an even number of `a's.  */
 
            for (;;)
              {
@@ -2772,7 +2845,7 @@ regex_compile (pattern, size, syntax, bufp)
              break;
 
            /* Now we know whether or not zero matches is allowed
-              and also whether or not two or more matches is allowed.  */
+              and also whether or not two or more matches is allowed.  */
            if (greedy)
              {
                if (many_times_ok)
@@ -2879,7 +2952,7 @@ regex_compile (pattern, size, syntax, bufp)
            laststart = b;
 
            /* We test `*p == '^' twice, instead of using an if
-              statement, so we only need one BUF_PUSH.  */
+              statement, so we only need one BUF_PUSH.  */
            BUF_PUSH (*p == '^' ? charset_not : charset);
            if (*p == '^')
              p++;
@@ -2887,7 +2960,7 @@ regex_compile (pattern, size, syntax, bufp)
            /* Remember the first position in the bracket expression.  */
            p1 = p;
 
-           /* Push the number of bytes in the bitmap.  */
+           /* Push the number of bytes in the bitmap.  */
            BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
 
            /* Clear the whole map.  */
@@ -2898,11 +2971,12 @@ regex_compile (pattern, size, syntax, bufp)
                && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
              SET_LIST_BIT ('\n');
 
-           /* Read in characters and ranges, setting map bits.  */
+           /* Read in characters and ranges, setting map bits.  */
            for (;;)
              {
                boolean escaped_char = false;
                const unsigned char *p2 = p;
+               re_wchar_t ch, c2;
 
                if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
 
@@ -2922,7 +2996,7 @@ regex_compile (pattern, size, syntax, bufp)
                  }
                else
                  {
-                   /* Could be the end of the bracket expression.      If it's
+                   /* Could be the end of the bracket expression.  If it's
                       not (i.e., when the bracket expression is `[]' so
                       far), the ']' character bit gets set way below.  */
                    if (c == ']' && p2 != p1)
@@ -2935,7 +3009,7 @@ regex_compile (pattern, size, syntax, bufp)
                if (!escaped_char &&
                    syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
                  {
-                   /* Leave room for the null.  */
+                   /* Leave room for the null.  */
                    unsigned char str[CHAR_CLASS_MAX_LENGTH + 1];
                    const unsigned char *class_beg;
 
@@ -2965,7 +3039,6 @@ regex_compile (pattern, size, syntax, bufp)
                       them).  */
                    if (c == ':' && *p == ']')
                      {
-                       re_wchar_t ch;
                        re_wctype_t cc;
                        int limit;
 
@@ -2980,6 +3053,15 @@ regex_compile (pattern, size, syntax, bufp)
 
                         if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
 
+#ifndef emacs
+                       for (ch = 0; ch < (1 << BYTEWIDTH); ++ch)
+                         if (re_iswctype (btowc (ch), cc))
+                           {
+                             c = TRANSLATE (ch);
+                             if (c < (1 << BYTEWIDTH))
+                               SET_LIST_BIT (c);
+                           }
+#else  /* emacs */
                        /* Most character classes in a multibyte match
                           just set a flag.  Exceptions are is_blank,
                           is_digit, is_cntrl, and is_xdigit, since
@@ -2987,32 +3069,32 @@ regex_compile (pattern, size, syntax, bufp)
                           don't need to handle them for multibyte.
                           They are distinguished by a negative wctype.  */
 
-                       for (ch = 0; ch < 128; ++ch)
-                         if (re_iswctype (btowc (ch), cc))
-                           {
-                             c = TRANSLATE (ch);
-                             SET_LIST_BIT (c);
-                           }
-
-                       if (target_multibyte)
-                         {
-                           SET_RANGE_TABLE_WORK_AREA_BIT
-                             (range_table_work, re_wctype_to_bit (cc));
-                         }
-                       else
+                       for (ch = 0; ch < 256; ++ch)
                          {
-                           for (ch = 0; ch < (1 << BYTEWIDTH); ++ch)
+                           c = RE_CHAR_TO_MULTIBYTE (ch);
+                           if (! CHAR_BYTE8_P (c)
+                               && re_iswctype (c, cc))
                              {
-                               c = ch;
-                               MAKE_CHAR_MULTIBYTE (c);
-                               if (re_iswctype (btowc (c), cc))
-                                 {
-                                   c = TRANSLATE (c);
-                                   MAKE_CHAR_UNIBYTE (c);
-                                   SET_LIST_BIT (c);
-                                 }
+                               SET_LIST_BIT (ch);
+                               c1 = TRANSLATE (c);
+                               if (c1 == c)
+                                 continue;
+                               if (ASCII_CHAR_P (c1))
+                                 SET_LIST_BIT (c1);
+                               else if ((c1 = RE_CHAR_TO_UNIBYTE (c1)) >= 0)
+                                 SET_LIST_BIT (c1);
                              }
                          }
+                       SET_RANGE_TABLE_WORK_AREA_BIT
+                         (range_table_work, re_wctype_to_bit (cc));
+#endif /* emacs */
+                       /* In most cases the matching rule for char classes
+                          only uses the syntax table for multibyte chars,
+                          so that the content of the syntax-table it is not
+                          hardcoded in the range_table.  SPACE and WORD are
+                          the two exceptions.  */
+                       if ((1 << cc) & ((1 << RECC_SPACE) | (1 << RECC_WORD)))
+                         bufp->used_syntax = 1;
 
                        /* Repeat the loop. */
                        continue;
@@ -3025,7 +3107,7 @@ regex_compile (pattern, size, syntax, bufp)
 
                        /* Because the `:' may starts the range, we
                           can't simply set bit and repeat the loop.
-                          Instead, just set it to C and handle below.  */
+                          Instead, just set it to C and handle below.  */
                        c = ':';
                      }
                  }
@@ -3038,55 +3120,67 @@ regex_compile (pattern, size, syntax, bufp)
 
                    /* Fetch the character which ends the range. */
                    PATFETCH (c1);
-                   if (c > c1)
-                     {
-                       if (syntax & RE_NO_EMPTY_RANGES)
-                         FREE_STACK_RETURN (REG_ERANGEX);
-                       /* Else, repeat the loop.  */
-                     }
+#ifdef emacs
+                   if (CHAR_BYTE8_P (c1)
+                       && ! ASCII_CHAR_P (c) && ! CHAR_BYTE8_P (c))
+                     /* Treat the range from a multibyte character to
+                        raw-byte character as empty.  */
+                     c = c1 + 1;
+#endif /* emacs */
                  }
                else
                  /* Range from C to C. */
                  c1 = c;
 
-#ifndef emacs
-               c = TRANSLATE (c);
-               c1 = TRANSLATE (c1);
-               /* Set the range into bitmap */
-               for (; c <= c1; c++)
-                 SET_LIST_BIT (TRANSLATE (c));
-#else  /* not emacs */
-               if (target_multibyte)
+               if (c > c1)
                  {
-                   if (c1 >= 128)
-                     {
-                       re_wchar_t c0 = MAX (c, 128);
-
-                       SETUP_MULTIBYTE_RANGE (range_table_work, c0, c1);
-                       c1 = 127;
-                     }
-                   for (; c <= c1; c++)
-                     SET_LIST_BIT (TRANSLATE (c));
+                   if (syntax & RE_NO_EMPTY_RANGES)
+                     FREE_STACK_RETURN (REG_ERANGEX);
+                   /* Else, repeat the loop.  */
                  }
                else
                  {
-                   re_wchar_t c0;
-
+#ifndef emacs
+                   /* Set the range into bitmap */
                    for (; c <= c1; c++)
                      {
-                       c0 = c;
-                       if (! multibyte)
-                         MAKE_CHAR_MULTIBYTE (c0);
-                       c0 = TRANSLATE (c0);
-                       MAKE_CHAR_UNIBYTE (c0);
-                       SET_LIST_BIT (c0);
+                       ch = TRANSLATE (c);
+                       if (ch < (1 << BYTEWIDTH))
+                         SET_LIST_BIT (ch);
+                     }
+#else  /* emacs */
+                   if (c < 128)
+                     {
+                       ch = MIN (127, c1);
+                       SETUP_ASCII_RANGE (range_table_work, c, ch);
+                       c = ch + 1;
+                       if (CHAR_BYTE8_P (c1))
+                         c = BYTE8_TO_CHAR (128);
+                     }
+                   if (c <= c1)
+                     {
+                       if (CHAR_BYTE8_P (c))
+                         {
+                           c = CHAR_TO_BYTE8 (c);
+                           c1 = CHAR_TO_BYTE8 (c1);
+                           for (; c <= c1; c++)
+                             SET_LIST_BIT (c);
+                         }
+                       else if (multibyte)
+                         {
+                           SETUP_MULTIBYTE_RANGE (range_table_work, c, c1);
+                         }
+                       else
+                         {
+                           SETUP_UNIBYTE_RANGE (range_table_work, c, c1);
+                         }
                      }
+#endif /* emacs */
                  }
-#endif /* not emacs */
              }
 
            /* Discard any (non)matching list bytes that are all 0 at the
-              end of the map.  Decrease the map-length byte too.  */
+              end of the map.  Decrease the map-length byte too.  */
            while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
              b[-1]--;
            b += b[-1];
@@ -3172,28 +3266,54 @@ regex_compile (pattern, size, syntax, bufp)
            handle_open:
              {
                int shy = 0;
+               regnum_t regnum = 0;
                if (p+1 < pend)
                  {
                    /* Look for a special (?...) construct */
                    if ((syntax & RE_SHY_GROUPS) && *p == '?')
                      {
                        PATFETCH (c); /* Gobble up the '?'.  */
-                       PATFETCH (c);
-                       switch (c)
+                       while (!shy)
                          {
-                         case ':': shy = 1; break;
-                         default:
-                           /* Only (?:...) is supported right now. */
-                           FREE_STACK_RETURN (REG_BADPAT);
+                           PATFETCH (c);
+                           switch (c)
+                             {
+                             case ':': shy = 1; break;
+                             case '0':
+                               /* An explicitly specified regnum must start
+                                  with non-0. */
+                               if (regnum == 0)
+                                 FREE_STACK_RETURN (REG_BADPAT);
+                             case '1': case '2': case '3': case '4':
+                             case '5': case '6': case '7': case '8': case '9':
+                               regnum = 10*regnum + (c - '0'); break;
+                             default:
+                               /* Only (?:...) is supported right now. */
+                               FREE_STACK_RETURN (REG_BADPAT);
+                             }
                          }
                      }
                  }
 
                if (!shy)
-                 {
-                   bufp->re_nsub++;
-                   regnum++;
+                 regnum = ++bufp->re_nsub;
+               else if (regnum)
+                 { /* It's actually not shy, but explicitly numbered.  */
+                   shy = 0;
+                   if (regnum > bufp->re_nsub)
+                     bufp->re_nsub = regnum;
+                   else if (regnum > bufp->re_nsub
+                            /* Ideally, we'd want to check that the specified
+                               group can't have matched (i.e. all subgroups
+                               using the same regnum are in other branches of
+                               OR patterns), but we don't currently keep track
+                               of enough info to do that easily.  */
+                            || group_in_compile_stack (compile_stack, regnum))
+                     FREE_STACK_RETURN (REG_BADPAT);
                  }
+               else
+                 /* It's really shy.  */
+                 regnum = - bufp->re_nsub;
 
                if (COMPILE_STACK_FULL)
                  {
@@ -3205,19 +3325,18 @@ regex_compile (pattern, size, syntax, bufp)
                  }
 
                /* These are the values to restore when we hit end of this
-                  group.        They are all relative offsets, so that if the
+                  group.  They are all relative offsets, so that if the
                   whole pattern moves because of realloc, they will still
                   be valid.  */
                COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
                COMPILE_STACK_TOP.fixup_alt_jump
                  = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
                COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
-               COMPILE_STACK_TOP.regnum = shy ? -regnum : regnum;
+               COMPILE_STACK_TOP.regnum = regnum;
 
-               /* Do not push a
-                  start_memory for groups beyond the last one we can
-                  represent in the compiled pattern.  */
-               if (regnum <= MAX_REGNUM && !shy)
+               /* Do not push a start_memory for groups beyond the last one
+                  we can represent in the compiled pattern.  */
+               if (regnum <= MAX_REGNUM && regnum > 0)
                  BUF_PUSH_2 (start_memory, regnum);
 
                compile_stack.avail++;
@@ -3261,8 +3380,8 @@ regex_compile (pattern, size, syntax, bufp)
              {
                /* We don't just want to restore into `regnum', because
                   later groups should continue to be numbered higher,
-                  as in `(ab)c(de)' -- the second group is #2.  */
-               regnum_t this_group_regnum;
+                  as in `(ab)c(de)' -- the second group is #2.  */
+               regnum_t regnum;
 
                compile_stack.avail--;
                begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
@@ -3271,16 +3390,16 @@ regex_compile (pattern, size, syntax, bufp)
                    ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
                    : 0;
                laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
-               this_group_regnum = COMPILE_STACK_TOP.regnum;
+               regnum = COMPILE_STACK_TOP.regnum;
                /* If we've reached MAX_REGNUM groups, then this open
                   won't actually generate any code, so we'll have to
                   clear pending_exact explicitly.  */
                pending_exact = 0;
 
                /* We're at the end of the group, so now we know how many
-                  groups were inside this one.  */
-               if (this_group_regnum <= MAX_REGNUM && this_group_regnum > 0)
-                 BUF_PUSH_2 (stop_memory, this_group_regnum);
+                  groups were inside this one.  */
+               if (regnum <= MAX_REGNUM && regnum > 0)
+                 BUF_PUSH_2 (stop_memory, regnum);
              }
              break;
 
@@ -3293,7 +3412,7 @@ regex_compile (pattern, size, syntax, bufp)
                goto normal_char;
 
              /* Insert before the previous alternative a jump which
-                jumps to this alternative if the former fails.  */
+                jumps to this alternative if the former fails.  */
              GET_BUFFER_SPACE (3);
              INSERT_JUMP (on_failure_jump, begalt, b + 6);
              pending_exact = 0;
@@ -3430,7 +3549,7 @@ regex_compile (pattern, size, syntax, bufp)
                        b += 5;
 
                        /* Code to initialize the lower bound.  Insert
-                          before the `succeed_n'.       The `5' is the last two
+                          before the `succeed_n'.  The `5' is the last two
                           bytes of this `set_number_at', plus 3 bytes of
                           the following `succeed_n'.  */
                        insert_op2 (set_number_at, laststart, 5, lower_bound, b);
@@ -3500,7 +3619,7 @@ regex_compile (pattern, size, syntax, bufp)
 
 #ifdef emacs
            /* There is no way to specify the before_dot and after_dot
-              operators.  rms says this is ok.  --karl  */
+              operators.  rms says this is ok.  --karl  */
            case '=':
              BUF_PUSH (at_dot);
              break;
@@ -3606,8 +3725,9 @@ regex_compile (pattern, size, syntax, bufp)
 
                reg = c - '0';
 
-               /* Can't back reference to a subexpression before its end.  */
-               if (reg > regnum || group_in_compile_stack (compile_stack, reg))
+               if (reg > bufp->re_nsub || reg < 1
+                   /* Can't back reference to a subexp before its end.  */
+                   || group_in_compile_stack (compile_stack, reg))
                  FREE_STACK_RETURN (REG_ESUBREG);
 
                laststart = b;
@@ -3645,7 +3765,7 @@ regex_compile (pattern, size, syntax, bufp)
              /* We have only one byte following the exactn for the count.  */
              || *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
 
-             /* If followed by a repetition operator.  */
+             /* If followed by a repetition operator.  */
              || (p != pend && (*p == '*' || *p == '^'))
              || ((syntax & RE_BK_PLUS_QM)
                  ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
@@ -3667,17 +3787,22 @@ regex_compile (pattern, size, syntax, bufp)
          {
            int len;
 
-           if (! multibyte)
-             MAKE_CHAR_MULTIBYTE (c);
-           c = TRANSLATE (c);
-           if (target_multibyte)
+           if (multibyte)
              {
+               c = TRANSLATE (c);
                len = CHAR_STRING (c, b);
                b += len;
              }
            else
              {
-               MAKE_CHAR_UNIBYTE (c);
+               c1 = RE_CHAR_TO_MULTIBYTE (c);
+               if (! CHAR_BYTE8_P (c1))
+                 {
+                   re_wchar_t c2 = TRANSLATE (c1);
+
+                   if (c1 != c2 && (c1 = RE_CHAR_TO_UNIBYTE (c2)) >= 0)
+                     c = c1;
+                 }                   
                *b++ = c;
                len = 1;
              }
@@ -3704,11 +3829,6 @@ regex_compile (pattern, size, syntax, bufp)
   /* We have succeeded; set the length of the buffer.  */
   bufp->used = b - bufp->buffer;
 
-#ifdef emacs
-  /* Now the buffer is adjusted for the multibyteness of a target.  */
-  bufp->multibyte = bufp->target_multibyte;
-#endif
-
 #ifdef DEBUG
   if (debug > 0)
     {
@@ -3750,7 +3870,7 @@ regex_compile (pattern, size, syntax, bufp)
 \f
 /* Subroutines for `regex_compile'.  */
 
-/* Store OP at LOC followed by two-byte integer parameter ARG. */
+/* Store OP at LOC followed by two-byte integer parameter ARG.  */
 
 static void
 store_op1 (op, loc, arg)
@@ -3902,7 +4022,7 @@ analyse_first (p, pend, fastmap, multibyte)
   boolean not;
 
   /* If all elements for base leading-codes in fastmap is set, this
-     flag is set true. */
+     flag is set true.  */
   boolean match_any_multibyte_characters = false;
 
   assert (p);
@@ -3950,15 +4070,27 @@ analyse_first (p, pend, fastmap, multibyte)
 
 
       /* Following are the cases which match a character.  These end
-        with `break'.  */
+        with `break'.  */
 
        case exactn:
          if (fastmap)
-           /* If multibyte is nonzero, the first byte of each
-              character is an ASCII or a leading code.  Otherwise,
-              each byte is a character.  Thus, this works in both
-              cases. */
-           fastmap[p[1]] = 1;
+           {
+             /* If multibyte is nonzero, the first byte of each
+                character is an ASCII or a leading code.  Otherwise,
+                each byte is a character.  Thus, this works in both
+                cases. */
+             fastmap[p[1]] = 1;
+             if (! multibyte)
+               {
+                 /* For the case of matching this unibyte regex
+                    against multibyte, we must set a leading code of
+                    the corresponding multibyte character.  */
+                 int c = RE_CHAR_TO_MULTIBYTE (p[1]);
+
+                 if (! CHAR_BYTE8_P (c))
+                   fastmap[CHAR_LEADING_CODE (c)] = 1;
+               }
+           }
          break;
 
 
@@ -3973,12 +4105,8 @@ analyse_first (p, pend, fastmap, multibyte)
          if (!fastmap) break;
          {
            /* Chars beyond end of bitmap are possible matches.  */
-           /* In a multibyte case, the bitmap is used only for ASCII
-              characters.  */
-           int limit = multibyte ? 128 : (1 << BYTEWIDTH);
-
            for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
-                j < limit; j++)
+                j < (1 << BYTEWIDTH); j++)
              fastmap[j] = 1;
          }
 
@@ -3991,17 +4119,21 @@ analyse_first (p, pend, fastmap, multibyte)
            if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
              fastmap[j] = 1;
 
-         if ((not && multibyte)
-             /* Any leading code can possibly start a character
+#ifdef emacs
+         if (/* Any leading code can possibly start a character
                 which doesn't match the specified set of characters.  */
-             || (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
-                 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
-           /* If we can match a character class, we can match
-              any multibyte characters.  */
+             not
+             || 
+             /* If we can match a character class, we can match any
+                multibyte characters.  */
+             (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
+              && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
+
            {
              if (match_any_multibyte_characters == false)
                {
-                 for (j = 0x80; j < (1 << BYTEWIDTH); j++)
+                 for (j = MIN_MULTIBYTE_LEADING_CODE;
+                      j <= MAX_MULTIBYTE_LEADING_CODE; j++)
                    fastmap[j] = 1;
                  match_any_multibyte_characters = true;
                }
@@ -4021,7 +4153,7 @@ analyse_first (p, pend, fastmap, multibyte)
 
              /* Extract the number of ranges in range table into COUNT.  */
              EXTRACT_NUMBER_AND_INCR (count, p);
-             for (; count > 0; count--, p += 2 * 3) /* XXX */
+             for (; count > 0; count--, p += 3)
                {
                  /* Extract the start and end of each range.  */
                  EXTRACT_CHARACTER (c, p);
@@ -4033,6 +4165,7 @@ analyse_first (p, pend, fastmap, multibyte)
                    fastmap[j] = 1;
                }
            }
+#endif
          break;
 
        case syntaxspec:
@@ -4055,20 +4188,18 @@ analyse_first (p, pend, fastmap, multibyte)
          if (!fastmap) break;
          not = (re_opcode_t)p[-1] == notcategoryspec;
          k = *p++;
-         for (j = (multibyte ? 127 : (1 << BYTEWIDTH)); j >= 0; j--)
+         for (j = (1 << BYTEWIDTH); j >= 0; j--)
            if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
              fastmap[j] = 1;
 
-         if (multibyte)
+         /* Any leading code can possibly start a character which
+            has or doesn't has the specified category.  */
+         if (match_any_multibyte_characters == false)
            {
-             /* Any character set can possibly contain a character
-                whose category is K (or not).  */
-             if (match_any_multibyte_characters == false)
-               {
-                 for (j = 0x80; j < (1 << BYTEWIDTH); j++)
-                   fastmap[j] = 1;
-                 match_any_multibyte_characters = true;
-               }
+             for (j = MIN_MULTIBYTE_LEADING_CODE;
+                  j <= MAX_MULTIBYTE_LEADING_CODE; j++)
+               fastmap[j] = 1;
+             match_any_multibyte_characters = true;
            }
          break;
 
@@ -4171,7 +4302,7 @@ analyse_first (p, pend, fastmap, multibyte)
 
       /* Getting here means we have found the possible starting
         characters for one path of the pattern -- and that the empty
-        string does not match.  We need not follow this path further.  */
+        string does not match.  We need not follow this path further.  */
       return 0;
     } /* while p */
 
@@ -4206,7 +4337,7 @@ re_compile_fastmap (bufp)
 
   assert (fastmap && bufp->buffer);
 
-  bzero (fastmap, 1 << BYTEWIDTH);  /* Assume nothing's valid. */
+  bzero (fastmap, 1 << BYTEWIDTH);  /* Assume nothing's valid.  */
   bufp->fastmap_accurate = 1;      /* It will be when we're done.  */
 
   analysis = analyse_first (bufp->buffer, bufp->buffer + bufp->used,
@@ -4251,7 +4382,7 @@ re_set_registers (bufp, regs, num_regs, starts, ends)
 }
 WEAK_ALIAS (__re_set_registers, re_set_registers)
 \f
-/* Searching routines. */
+/* Searching routines.  */
 
 /* Like re_search_2, below, but only one string is specified, and
    doesn't let you say where to stop matching. */
@@ -4319,9 +4450,8 @@ re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
   int total_size = size1 + size2;
   int endpos = startpos + range;
   boolean anchored_start;
-  /* Nonzero if BUFP is setup for multibyte characters.  We are sure
-     that it is the same as RE_TARGET_MULTIBYTE_P (bufp).  */
-  const boolean multibyte = RE_MULTIBYTE_P (bufp);
+  /* Nonzero if we are searching multibyte string.  */
+  const boolean multibyte = RE_TARGET_MULTIBYTE_P (bufp);
 
   /* Check for out-of-range STARTPOS.  */
   if (startpos < 0 || startpos > total_size)
@@ -4390,7 +4520,7 @@ re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
       /* If a fastmap is supplied, skip quickly over characters that
         cannot be the start of a match.  If the pattern can match the
         null string, however, we don't need to skip characters; we want
-        the first null string.  */
+        the first null string.  */
       if (fastmap && startpos < total_size && !bufp->can_be_null)
        {
          register re_char *d;
@@ -4398,7 +4528,7 @@ re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
 
          d = POS_ADDR_VSTRING (startpos);
 
-         if (range > 0)        /* Searching forwards.  */
+         if (range > 0)        /* Searching forwards.  */
            {
              register int lim = 0;
              int irange = range;
@@ -4427,10 +4557,14 @@ re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
                  else
                    while (range > lim)
                      {
+                       register re_wchar_t ch, translated;
+
                        buf_ch = *d;
-                       MAKE_CHAR_MULTIBYTE (buf_ch);
-                       buf_ch = RE_TRANSLATE (translate, buf_ch);
-                       MAKE_CHAR_UNIBYTE (buf_ch);
+                       ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
+                       translated = RE_TRANSLATE (translate, ch);
+                       if (translated != ch
+                           && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
+                         buf_ch = ch;
                        if (fastmap[buf_ch])
                          break;
                        d++;
@@ -4460,7 +4594,7 @@ re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
                }
              startpos += irange - range;
            }
-         else                          /* Searching backwards.  */
+         else                          /* Searching backwards.  */
            {
              int room = (startpos >= size1
                          ? size2 + size1 - startpos
@@ -4474,7 +4608,15 @@ re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
                }
              else
                {
-                 if (! fastmap[TRANSLATE (*d)])
+                 register re_wchar_t ch, translated;
+
+                 buf_ch = *d;
+                 ch = RE_CHAR_TO_MULTIBYTE (buf_ch);
+                 translated = TRANSLATE (ch);
+                 if (translated != ch
+                     && (ch = RE_CHAR_TO_UNIBYTE (translated)) >= 0)
+                   buf_ch = ch;
+                 if (! fastmap[TRANSLATE (buf_ch)])
                    goto advance;
                }
            }
@@ -4487,11 +4629,6 @@ re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
 
       val = re_match_2_internal (bufp, string1, size1, string2, size2,
                                 startpos, regs, stop);
-#ifndef REGEX_MALLOC
-# ifdef C_ALLOCA
-      alloca (0);
-# endif
-#endif
 
       if (val >= 0)
        return startpos;
@@ -4589,7 +4726,7 @@ static int bcmp_translate _RE_ARGS((re_char *s1, re_char *s2,
      }                                                                 \
 
 /* Test if at very beginning or at very end of the virtual concatenation
-   of `string1' and `string2'. If only one string, it's `string2'.  */
+   of `string1' and `string2'.  If only one string, it's `string2'.  */
 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
 #define AT_STRINGS_END(d) ((d) == end2)
 
@@ -4610,7 +4747,7 @@ static int bcmp_translate _RE_ARGS((re_char *s1, re_char *s2,
 
    The DEC Alpha C compiler 3.x generates incorrect code for the
    test         WORDCHAR_P (d - 1) != WORDCHAR_P (d)  in the expansion of
-   AT_WORD_BOUNDARY, so this code is disabled. Expanding the
+   AT_WORD_BOUNDARY, so this code is disabled.  Expanding the
    macro and introducing temporary variables works around the bug.  */
 
 #if 0
@@ -4755,11 +4892,11 @@ mutually_exclusive_p (bufp, p1, p2)
       {
        register re_wchar_t c
          = (re_opcode_t) *p2 == endline ? '\n'
-         : RE_STRING_CHAR (p2 + 2, pend - p2 - 2);
+         : RE_STRING_CHAR (p2 + 2, pend - p2 - 2, multibyte);
 
        if ((re_opcode_t) *p1 == exactn)
          {
-           if (c != RE_STRING_CHAR (p1 + 2, pend - p1 - 2))
+           if (c != RE_STRING_CHAR (p1 + 2, pend - p1 - 2, multibyte))
              {
                DEBUG_PRINT3 ("  '%c' != '%c' => fast loop.\n", c, p1[2]);
                return 1;
@@ -4812,7 +4949,7 @@ mutually_exclusive_p (bufp, p1, p2)
        {
          /* Now, we are sure that P2 has no range table.
             So, for the size of bitmap in P2, `p2[1]' is
-            enough.    But P1 may have range table, so the
+            enough.  But P1 may have range table, so the
             size of bitmap table of P1 is extracted by
             using macro `CHARSET_BITMAP_SIZE'.
 
@@ -4844,7 +4981,7 @@ mutually_exclusive_p (bufp, p1, p2)
            {
              int idx;
              /* We win if the charset_not inside the loop lists
-                every character listed in the charset after.    */
+                every character listed in the charset after.  */
              for (idx = 0; idx < (int) p2[1]; idx++)
                if (! (p2[2 + idx] == 0
                       || (idx < CHARSET_BITMAP_SIZE (p1)
@@ -4928,9 +5065,6 @@ re_match (bufp, string, size, pos, regs)
 {
   int result = re_match_2_internal (bufp, NULL, 0, (re_char*) string, size,
                                    pos, regs, size);
-# if defined C_ALLOCA && !defined REGEX_MALLOC
-  alloca (0);
-# endif
   return result;
 }
 WEAK_ALIAS (__re_match, re_match)
@@ -4938,7 +5072,7 @@ WEAK_ALIAS (__re_match, re_match)
 
 #ifdef emacs
 /* In Emacs, this is the string or buffer in which we
-   are matching.  It is used for looking up syntax properties. */
+   are matching.  It is used for looking up syntax properties.  */
 Lisp_Object re_match_object;
 #endif
 
@@ -4948,11 +5082,11 @@ Lisp_Object re_match_object;
    matching at STOP.
 
    If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
-   store offsets for the substring each group matched in REGS. See the
+   store offsets for the substring each group matched in REGS.  See the
    documentation for exactly how many groups we fill.
 
    We return -1 if no match, -2 if an internal error (such as the
-   failure stack overflowing). Otherwise, we return the length of the
+   failure stack overflowing).  Otherwise, we return the length of the
    matched substring.  */
 
 int
@@ -4976,33 +5110,13 @@ re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
   result = re_match_2_internal (bufp, (re_char*) string1, size1,
                                (re_char*) string2, size2,
                                pos, regs, stop);
-#if defined C_ALLOCA && !defined REGEX_MALLOC
-  alloca (0);
-#endif
   return result;
 }
 WEAK_ALIAS (__re_match_2, re_match_2)
 
-#ifdef emacs
-#define TRANSLATE_VIA_MULTIBYTE(c)     \
-  do {                                 \
-    if (multibyte)                     \
-      (c) = TRANSLATE (c);             \
-    else                               \
-      {                                        \
-       MAKE_CHAR_MULTIBYTE (c);        \
-       (c) = TRANSLATE (c);            \
-       MAKE_CHAR_UNIBYTE (c);          \
-      }                                        \
-  } while (0)
-
-#else
-#define TRANSLATE_VIA_MULTIBYTE(c) ((c) = TRANSLATE (c))
-#endif
-
 
 /* This is a separate function so that we can force an alloca cleanup
-   afterwards. */
+   afterwards.  */
 static int
 re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
      struct re_pattern_buffer *bufp;
@@ -5021,7 +5135,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
   re_char *end1, *end2;
 
   /* Pointers into string1 and string2, just past the last characters in
-     each to consider matching.         */
+     each to consider matching.  */
   re_char *end_match_1, *end_match_2;
 
   /* Where we are in the data, and the end of the current string.  */
@@ -5040,18 +5154,20 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
   /* We use this to map every character in the string. */
   RE_TRANSLATE_TYPE translate = bufp->translate;
 
-  /* Nonzero if BUFP is setup for multibyte characters.  We are sure
-     that it is the same as RE_TARGET_MULTIBYTE_P (bufp).  */
+  /* Nonzero if BUFP is setup from a multibyte regex.  */
   const boolean multibyte = RE_MULTIBYTE_P (bufp);
 
+  /* Nonzero if STRING1/STRING2 are multibyte.  */
+  const boolean target_multibyte = RE_TARGET_MULTIBYTE_P (bufp);
+
   /* Failure point stack.  Each place that can handle a failure further
      down the line pushes a failure point on this stack.  It consists of
      regstart, and regend for all registers corresponding to
      the subexpressions we're currently inside, plus the number of such
      registers, and, finally, two char *'s.  The first char * is where
      to resume scanning the pattern; the second one is where to resume
-     scanning the strings.     */
-#ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global.         */
+     scanning the strings.  */
+#ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global.  */
   fail_stack_type fail_stack;
 #endif
 #ifdef DEBUG
@@ -5065,7 +5181,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
 #endif
 
   /* We fill all the registers internally, independent of what we
-     return, for use in backreferences.         The number here includes
+     return, for use in backreferences.  The number here includes
      an element for register zero.  */
   size_t num_regs = bufp->re_nsub + 1;
 
@@ -5091,7 +5207,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
 
   /* Logically, this is `best_regend[0]'.  But we don't want to have to
      allocate space for that if we're not allocating space for anything
-     else (see below). Also, we never need info about register 0 for
+     else (see below).  Also, we never need info about register 0 for
      any of the other register vectors, and it seems rather a kludge to
      treat `best_regend' differently than the rest.  So we keep track of
      the end of the best match so far in a separate variable.  We
@@ -5149,7 +5265,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
     regstart[reg] = regend[reg] = NULL;
 
   /* We move `string1' into `string2' if the latter's empty -- but not if
-     `string1' is null.         */
+     `string1' is null.  */
   if (size2 == 0 && string1 != NULL)
     {
       string2 = string1;
@@ -5206,7 +5322,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
   DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
   DEBUG_PRINT1 ("'\n");
 
-  /* This loops over pattern commands. It exits by returning from the
+  /* This loops over pattern commands.  It exits by returning from the
      function if the match is complete, or it drops through if the match
      fails at this starting point in the input data.  */
   for (;;)
@@ -5229,7 +5345,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
              boolean best_match_p;
 
              /* AIX compiler got confused when this was combined
-                with the previous declaration.  */
+                with the previous declaration.  */
              if (same_str_p)
                best_match_p = d > match_end;
              else
@@ -5267,7 +5383,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
                     end_match_1' while the restored d is in string2.
                     For example, the pattern `x.*y.*z' against the
                     strings `x-' and `y-z-', if the two strings are
-                    not consecutive in memory.  */
+                    not consecutive in memory.  */
                  DEBUG_PRINT1 ("Restoring best registers.\n");
 
                  d = match_end;
@@ -5290,7 +5406,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
            {
              /* Have the register data arrays been allocated?  */
              if (bufp->regs_allocated == REGS_UNALLOCATED)
-               { /* No.  So allocate them with malloc.  We need one
+               { /* No.  So allocate them with malloc.  We need one
                     extra element beyond `num_regs' for the `-1' marker
                     GNU code uses.  */
                  regs->num_regs = MAX (RE_NREGS, num_regs + 1);
@@ -5322,7 +5438,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
              else
                {
                  /* These braces fend off a "empty body in an else-statement"
-                    warning under GCC when assert expands to nothing.  */
+                    warning under GCC when assert expands to nothing.  */
                  assert (bufp->regs_allocated == REGS_FIXED);
                }
 
@@ -5354,7 +5470,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
                 were in the pattern, set the extra elements to -1.  If
                 we (re)allocated the registers, this is the case,
                 because we always allocate enough to have at least one
-                -1 at the end.  */
+                -1 at the end.  */
              for (reg = num_regs; reg < regs->num_regs; reg++)
                regs->start[reg] = regs->end[reg] = -1;
            } /* regs && !bufp->no_sub */
@@ -5372,7 +5488,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
          return mcnt;
        }
 
-      /* Otherwise match next pattern command. */
+      /* Otherwise match next pattern command.  */
       switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
        {
        /* Ignore these.  Used to ignore the n of succeed_n's which
@@ -5385,9 +5501,9 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
          DEBUG_PRINT1 ("EXECUTING succeed.\n");
          goto succeed_label;
 
-       /* Match the next n pattern characters exactly.  The following
+       /* Match the next n pattern characters exactly.  The following
           byte in the pattern defines n, and the n bytes after that
-          are the characters to match.  */
+          are the characters to match.  */
        case exactn:
          mcnt = *p++;
          DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
@@ -5423,14 +5539,20 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
            while (--mcnt);
 #else  /* emacs */
          /* The cost of testing `translate' is comparatively small.  */
-         if (multibyte)
+         if (target_multibyte)
            do
              {
                int pat_charlen, buf_charlen;
-               unsigned int pat_ch, buf_ch;
+               int pat_ch, buf_ch;
 
                PREFETCH ();
-               pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
+               if (multibyte)
+                 pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
+               else
+                 {
+                   pat_ch = RE_CHAR_TO_MULTIBYTE (*p);
+                   pat_charlen = 1;
+                 }
                buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
 
                if (TRANSLATE (buf_ch) != pat_ch)
@@ -5447,16 +5569,38 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
          else
            do
              {
-               unsigned int buf_ch;
+               int pat_charlen, buf_charlen;
+               int pat_ch, buf_ch;
 
                PREFETCH ();
-               buf_ch = *d++;
-               TRANSLATE_VIA_MULTIBYTE (buf_ch);
-               if (buf_ch != *p++)
+               if (multibyte)
+                 {
+                   pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
+                   if (CHAR_BYTE8_P (pat_ch))
+                     pat_ch = CHAR_TO_BYTE8 (pat_ch);
+                   else
+                     pat_ch = RE_CHAR_TO_UNIBYTE (pat_ch);
+                 }
+               else
+                 {
+                   pat_ch = *p;
+                   pat_charlen = 1;
+                 }
+               buf_ch = RE_CHAR_TO_MULTIBYTE (*d);
+               if (! CHAR_BYTE8_P (buf_ch))
+                 {
+                   buf_ch = TRANSLATE (buf_ch);
+                   buf_ch = RE_CHAR_TO_UNIBYTE (buf_ch);
+                   if (buf_ch < 0)
+                     buf_ch = *d;
+                 }
+               if (buf_ch != pat_ch)
                  {
                    d = dfail;
                    goto fail;
                  }
+               p += pat_charlen;
+               d++;
              }
            while (--mcnt);
 #endif
@@ -5472,7 +5616,8 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
            DEBUG_PRINT1 ("EXECUTING anychar.\n");
 
            PREFETCH ();
-           buf_ch = RE_STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
+           buf_ch = RE_STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen,
+                                               target_multibyte);
            buf_ch = TRANSLATE (buf_ch);
 
            if ((!(bufp->syntax & RE_DOT_NEWLINE)
@@ -5516,10 +5661,30 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
              }
 
            PREFETCH ();
-           c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
-           TRANSLATE_VIA_MULTIBYTE (c); /* The character to match.  */
+           c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len, target_multibyte);
+           if (target_multibyte)
+             {
+               int c1;
 
-           if (! multibyte || IS_REAL_ASCII (c))
+               c = TRANSLATE (c);
+               c1 = RE_CHAR_TO_UNIBYTE (c);
+               if (c1 >= 0)
+                 c = c1;
+             }
+           else
+             {
+               int c1 = RE_CHAR_TO_MULTIBYTE (c);
+
+               if (! CHAR_BYTE8_P (c1))
+                 {
+                   c1 = TRANSLATE (c1);
+                   c1 = RE_CHAR_TO_UNIBYTE (c1);
+                   if (c1 >= 0)
+                     c = c1;
+                 }
+             }
+
+           if (c < (1 << BYTEWIDTH))
              {                 /* Lookup bitmap.  */
                /* Cast to `unsigned' instead of `unsigned char' in
                   case the bit list is a full 32 bytes long.  */
@@ -5559,7 +5724,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
        /* The beginning of a group is represented by start_memory.
           The argument is the register number.  The text
           matched within the group is recorded (in the internal
-          registers data structure) under the register number.  */
+          registers data structure) under the register number.  */
        case start_memory:
          DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p);
 
@@ -5608,14 +5773,14 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
        case duplicate:
          {
            register re_char *d2, *dend2;
-           int regno = *p++;   /* Get which register to match against.  */
+           int regno = *p++;   /* Get which register to match against.  */
            DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
 
-           /* Can't back reference a group which we've never matched.  */
+           /* Can't back reference a group which we've never matched.  */
            if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
              goto fail;
 
-           /* Where in input to try to start matching.  */
+           /* Where in input to try to start matching.  */
            d2 = regstart[regno];
 
            /* Remember the start point to rollback upon failure.  */
@@ -5659,7 +5824,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
                /* Compare that many; failure if mismatch, else move
                   past them.  */
                if (RE_TRANSLATE_P (translate)
-                   ? bcmp_translate (d, d2, mcnt, translate, multibyte)
+                   ? bcmp_translate (d, d2, mcnt, translate, target_multibyte)
                    : memcmp (d, d2, mcnt))
                  {
                    d = dfail;
@@ -5728,7 +5893,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
           pushes NULL as the value for the string on the stack.  Then
           `POP_FAILURE_POINT' will keep the current value for the
           string, instead of restoring it.  To see why, consider
-          matching `foo\nbar' against `.*\n'.  The .* matches the foo;
+          matching `foo\nbar' against `.*\n'.  The .* matches the foo;
           then the . fails against the \n.  But the next thing we want
           to do is match the \n against the \n; if we restored the
           string value, we would be back at the foo.
@@ -5873,7 +6038,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
          IMMEDIATE_QUIT_CHECK;
          EXTRACT_NUMBER_AND_INCR (mcnt, p);    /* Get the amount to jump.  */
          DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
-         p += mcnt;                            /* Do the jump.  */
+         p += mcnt;                            /* Do the jump.  */
          DEBUG_PRINT2 ("(to %p).\n", p);
          break;
 
@@ -5967,7 +6132,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
              if (/* Case 2: Only one of S1 and S2 is Sword.  */
                  ((s1 == Sword) != (s2 == Sword))
                  /* Case 3: Both of S1 and S2 are Sword, and macro
-                    WORD_BOUNDARY_P (C1, C2) returns nonzero.  */
+                    WORD_BOUNDARY_P (C1, C2) returns nonzero.  */
                  || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
                not = !not;
            }
@@ -5981,7 +6146,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
 
          /* We FAIL in one of the following cases: */
 
-         /* Case 1: D is at the end of string.  */
+         /* Case 1: D is at the end of string.  */
          if (AT_STRINGS_END (d))
            goto fail;
          else
@@ -6014,7 +6179,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
                  s1 = SYNTAX (c1);
 
                  /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
-                    returns 0.  */
+                    returns 0.  */
                  if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
                    goto fail;
                }
@@ -6059,7 +6224,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
                  s2 = SYNTAX (c2);
 
                  /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
-                    returns 0.  */
+                    returns 0.  */
                  if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
          goto fail;
                }
@@ -6071,7 +6236,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
 
          /* We FAIL in one of the following cases: */
 
-         /* Case 1: D is at the end of string.  */
+         /* Case 1: D is at the end of string.  */
          if (AT_STRINGS_END (d))
            goto fail;
          else
@@ -6086,9 +6251,9 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
              UPDATE_SYNTAX_TABLE (charpos);
 #endif
              PREFETCH ();
-             c2 = RE_STRING_CHAR (d, dend - d);
+             c2 = RE_STRING_CHAR (d, dend - d, target_multibyte);
              s2 = SYNTAX (c2);
-       
+
              /* Case 2: S2 is neither Sword nor Ssymbol. */
              if (s2 != Sword && s2 != Ssymbol)
                goto fail;
@@ -6139,7 +6304,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
              if (!AT_STRINGS_END (d))
                {
                  PREFETCH_NOLIMIT ();
-                 c2 = RE_STRING_CHAR (d, dend - d);
+                 c2 = RE_STRING_CHAR (d, dend - d, target_multibyte);
 #ifdef emacs
                  UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
 #endif
@@ -6280,11 +6445,11 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
    bytes; nonzero otherwise.  */
 
 static int
-bcmp_translate (s1, s2, len, translate, multibyte)
+bcmp_translate (s1, s2, len, translate, target_multibyte)
      re_char *s1, *s2;
      register int len;
      RE_TRANSLATE_TYPE translate;
-     const int multibyte;
+     const int target_multibyte;
 {
   register re_char *p1 = s1, *p2 = s2;
   re_char *p1_end = s1 + len;
@@ -6332,6 +6497,10 @@ re_compile_pattern (pattern, length, bufp)
 {
   reg_errcode_t ret;
 
+#ifdef emacs
+  gl_state.current_syntax_table = current_buffer->syntax_table;
+#endif
+
   /* GNU code is written to assume at least RE_NREGS registers will be set
      (and at least one extra will be -1).  */
   bufp->regs_allocated = REGS_UNALLOCATED;
@@ -6611,12 +6780,15 @@ regexec (preg, string, nmatch, pmatch, eflags)
 WEAK_ALIAS (__regexec, regexec)
 
 
-/* Returns a message corresponding to an error code, ERRCODE, returned
-   from either regcomp or regexec.   We don't use PREG here.  */
+/* Returns a message corresponding to an error code, ERR_CODE, returned
+   from either regcomp or regexec.   We don't use PREG here.
+
+   ERR_CODE was previously called ERRCODE, but that name causes an
+   error with msvc8 compiler.  */
 
 size_t
-regerror (errcode, preg, errbuf, errbuf_size)
-    int errcode;
+regerror (err_code, preg, errbuf, errbuf_size)
+    int err_code;
     const regex_t *preg;
     char *errbuf;
     size_t errbuf_size;
@@ -6624,15 +6796,15 @@ regerror (errcode, preg, errbuf, errbuf_size)
   const char *msg;
   size_t msg_size;
 
-  if (errcode < 0
-      || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
+  if (err_code < 0
+      || err_code >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
     /* Only error codes returned by the rest of the code should be passed
        to this routine.  If we are given anything else, or if other regex
        code generates an invalid error code, then the program has a bug.
        Dump core so we can fix it.  */
     abort ();
 
-  msg = gettext (re_error_msgid[errcode]);
+  msg = gettext (re_error_msgid[err_code]);
 
   msg_size = strlen (msg) + 1; /* Includes the null.  */