]> code.delx.au - gnu-emacs/blobdiff - src/syntax.c
Backport revisions 2011-04-24T05:30:24Z!eggert@cs.ucla.edu..2011-04-25T19:40:22Z...
[gnu-emacs] / src / syntax.c
index 494bd16f8c0f575b686f6e5f4abbece6f24f1f9e..bfdf0e5ee6d6c65f412326cb3d426bf72d32530d 100644 (file)
@@ -1,6 +1,6 @@
 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
    Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
-                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -21,6 +21,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 #include <ctype.h>
+#include <setjmp.h>
 #include "lisp.h"
 #include "commands.h"
 #include "buffer.h"
@@ -341,7 +342,7 @@ dec_bytepos (bytepos)
   return bytepos;
 }
 \f
-/* Return a defun-start position before before POS and not too far before.
+/* Return a defun-start position before POS and not too far before.
    It should be the last one before POS, or nearly the last.
 
    When open_paren_in_column_0_is_defun_start is nonzero,
@@ -511,6 +512,7 @@ back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_p
     {
       int temp_byte, prev_syntax;
       int com2start, com2end;
+      int comstart;
 
       /* Move back and examine a character.  */
       DEC_BOTH (from, from_byte);
@@ -529,7 +531,8 @@ back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_p
                       || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
       com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
                 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
-
+      comstart = (com2start || code == Scomment);
+      
       /* Nasty cases with overlapping 2-char comment markers:
         - snmp-mode: -- c -- foo -- c --
                      --- c --
@@ -540,15 +543,16 @@ back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_p
                      ///   */
 
       /* If a 2-char comment sequence partly overlaps with another,
-        we don't try to be clever.  */
-      if (from > stop && (com2end || com2start))
+        we don't try to be clever.  E.g. |*| in C, or }% in modes that
+        have %..\n and %{..}%.  */
+      if (from > stop && (com2end || comstart))
        {
          int next = from, next_byte = from_byte, next_c, next_syntax;
          DEC_BOTH (next, next_byte);
          UPDATE_SYNTAX_TABLE_BACKWARD (next);
          next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
          next_syntax = SYNTAX_WITH_FLAGS (next_c);
-         if (((com2start || comnested)
+         if (((comstart || comnested)
               && SYNTAX_FLAGS_COMEND_SECOND (syntax)
               && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
              || ((com2end || comnested)
@@ -851,8 +855,8 @@ static Lisp_Object Vsyntax_code_object;
 \f
 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
        doc: /* Return the syntax code of CHARACTER, described by a character.
-For example, if CHARACTER is a word constituent,
-the character `w' is returned.
+For example, if CHARACTER is a word constituent, the
+character `w' (119) is returned.
 The characters that correspond to various syntax codes
 are listed in the documentation of `modify-syntax-entry'.  */)
      (character)
@@ -910,8 +914,7 @@ text property.  */)
   if (*p)
     {
       int len;
-      int character = (STRING_CHAR_AND_LENGTH
-                      (p, SBYTES (string) - 1, len));
+      int character = STRING_CHAR_AND_LENGTH (p, len);
       XSETINT (match, character);
       if (XFASTINT (match) == ' ')
        match = Qnil;
@@ -953,7 +956,7 @@ text property.  */)
        break;
       }
 
-  if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
+  if (val < XVECTOR_SIZE (Vsyntax_code_object) && NILP (match))
     return XVECTOR (Vsyntax_code_object)->contents[val];
   else
     /* Since we can't use a shared object, let's make a new one.  */
@@ -1555,14 +1558,14 @@ skip_chars (forwardp, string, lim, handle_iso_classes)
          bzero (fastmap + 0200, 0200);
          /* We are sure that this loop stops.  */
          for (i = 0200; ! fastmap2[i]; i++);
-         c = unibyte_char_to_multibyte (i);
+         c = BYTE8_TO_CHAR (i);
          fastmap[CHAR_LEADING_CODE (c)] = 1;
          range_start_byte = i;
          range_start_char = c;
          char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
          for (i = 129; i < 0400; i++)
            {
-             c = unibyte_char_to_multibyte (i);
+             c = BYTE8_TO_CHAR (i);
              fastmap[CHAR_LEADING_CODE (c)] = 1;
              if (i - range_start_byte != c - range_start_char)
                {
@@ -1587,12 +1590,12 @@ skip_chars (forwardp, string, lim, handle_iso_classes)
          unsigned char leading_code;
 
          leading_code = str[i_byte];
-         c = STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
+         c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
          i_byte += len;
 
          if (handle_iso_classes && c == '['
              && i_byte < size_byte
-             && STRING_CHAR (str + i_byte, size_byte - i_byte) == ':')
+             && STRING_CHAR (str + i_byte) == ':')
            {
              const unsigned char *class_beg = str + i_byte + 1;
              const unsigned char *class_end = class_beg;
@@ -1632,8 +1635,7 @@ skip_chars (forwardp, string, lim, handle_iso_classes)
                break;
 
              leading_code = str[i_byte];
-             c = STRING_CHAR_AND_LENGTH (str + i_byte,
-                                         size_byte - i_byte, len);
+             c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
              i_byte += len;
            }
          /* Treat `-' as range character only if another character
@@ -1649,15 +1651,14 @@ skip_chars (forwardp, string, lim, handle_iso_classes)
 
              /* Get the end of the range.  */
              leading_code2 = str[i_byte];
-             c2 = STRING_CHAR_AND_LENGTH (str + i_byte,
-                                          size_byte - i_byte, len);
+             c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
              i_byte += len;
 
              if (c2 == '\\'
                  && i_byte < size_byte)
                {
                  leading_code2 = str[i_byte];
-                 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
+                 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
                  i_byte += len;
                }
 
@@ -1749,6 +1750,12 @@ skip_chars (forwardp, string, lim, handle_iso_classes)
       }
 
     immediate_quit = 1;
+    /* This code may look up syntax tables using macros that rely on the
+       gl_state object.  To make sure this object is not out of date,
+       let's initialize it manually.
+       We ignore syntax-table text-properties for now, since that's
+       what we've done in the past.  */
+    SETUP_SYNTAX_TABLE (BEGV, 0);
     if (forwardp)
       {
        if (multibyte)
@@ -1763,7 +1770,7 @@ skip_chars (forwardp, string, lim, handle_iso_classes)
                  p = GAP_END_ADDR;
                  stop = endp;
                }
-             c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
+             c = STRING_CHAR_AND_LENGTH (p, nbytes);
              if (! NILP (iso_classes) && in_classes (c, iso_classes))
                {
                  if (negate)
@@ -1834,7 +1841,7 @@ skip_chars (forwardp, string, lim, handle_iso_classes)
                }
              prev_p = p;
              while (--p >= stop && ! CHAR_HEAD_P (*p));
-             c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
+             c = STRING_CHAR (p);
 
              if (! NILP (iso_classes) && in_classes (c, iso_classes))
                {
@@ -1988,7 +1995,7 @@ skip_syntaxes (forwardp, string, lim)
                    p = GAP_END_ADDR;
                    stop = endp;
                  }
-               c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
+               c = STRING_CHAR_AND_LENGTH (p, nbytes);
                if (! fastmap[(int) SYNTAX (c)])
                  break;
                p += nbytes, pos++, pos_byte += nbytes;
@@ -2031,7 +2038,7 @@ skip_syntaxes (forwardp, string, lim)
                UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
                prev_p = p;
                while (--p >= stop && ! CHAR_HEAD_P (*p));
-               c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
+               c = STRING_CHAR (p);
                if (! fastmap[(int) SYNTAX (c)])
                  break;
                pos--, pos_byte -= prev_p - p;
@@ -2074,7 +2081,7 @@ in_classes (c, iso_classes)
 {
   int fits_class = 0;
 
-  while (! NILP (iso_classes))
+  while (CONSP (iso_classes))
     {
       Lisp_Object elt;
       elt = XCAR (iso_classes);
@@ -3331,17 +3338,17 @@ init_syntax_once ()
   Lisp_Object temp;
 
   /* This has to be done here, before we call Fmake_char_table.  */
-  Qsyntax_table = intern ("syntax-table");
+  Qsyntax_table = intern_c_string ("syntax-table");
   staticpro (&Qsyntax_table);
 
-  /* Intern this now in case it isn't already done.
+  /* Intern_C_String this now in case it isn't already done.
      Setting this variable twice is harmless.
      But don't staticpro it here--that is done in alloc.c.  */
-  Qchar_table_extra_slots = intern ("char-table-extra-slots");
+  Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
 
   /* Create objects which can be shared among syntax tables.  */
   Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
-  for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
+  for (i = 0; i < XVECTOR_SIZE (Vsyntax_code_object); i++)
     XVECTOR (Vsyntax_code_object)->contents[i]
       = Fcons (make_number (i), Qnil);
 
@@ -3417,7 +3424,7 @@ init_syntax_once ()
 void
 syms_of_syntax ()
 {
-  Qsyntax_table_p = intern ("syntax-table-p");
+  Qsyntax_table_p = intern_c_string ("syntax-table-p");
   staticpro (&Qsyntax_table_p);
 
   staticpro (&Vsyntax_code_object);
@@ -3430,12 +3437,12 @@ syms_of_syntax ()
   /* Defined in regex.c */
   staticpro (&re_match_object);
 
-  Qscan_error = intern ("scan-error");
+  Qscan_error = intern_c_string ("scan-error");
   staticpro (&Qscan_error);
   Fput (Qscan_error, Qerror_conditions,
-       Fcons (Qscan_error, Fcons (Qerror, Qnil)));
+       pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
   Fput (Qscan_error, Qerror_message,
-       build_string ("Scan error"));
+       make_pure_c_string ("Scan error"));
 
   DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
               doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace.  */);