(abbrev_check_chars): Use CHAR_TABLE_REF, not
[bpt/emacs.git] / src / abbrev.c
index f47a83b..dfde3cc 100644 (file)
@@ -1,6 +1,6 @@
 /* Primitives for word-abbrev mode.
    Copyright (C) 1985, 1986, 1993, 1996, 1998, 2001, 2002, 2003, 2004,
-                 2005, 2006 Free Software Foundation, Inc.
+                 2005, 2006, 2007 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -83,7 +83,7 @@ EMACS_INT last_abbrev_point;
 
 Lisp_Object Vpre_abbrev_expand_hook, Qpre_abbrev_expand_hook;
 
-Lisp_Object Qsystem_type, Qcount;
+Lisp_Object Qsystem_type, Qcount, Qforce;
 \f
 DEFUN ("make-abbrev-table", Fmake_abbrev_table, Smake_abbrev_table, 0, 0, 0,
        doc: /* Create a new, empty abbrev table object.  */)
@@ -107,10 +107,10 @@ DEFUN ("clear-abbrev-table", Fclear_abbrev_table, Sclear_abbrev_table, 1, 1, 0,
     XVECTOR (table)->contents[i] = make_number (0);
   return Qnil;
 }
-\f
+
 DEFUN ("define-abbrev", Fdefine_abbrev, Sdefine_abbrev, 3, 6, 0,
        doc: /* Define an abbrev in TABLE named NAME, to expand to EXPANSION and call HOOK.
-NAME must be a string.
+NAME must be a string, and should be lower-case.
 EXPANSION should usually be a string.
 To undefine an abbrev, define it with EXPANSION = nil.
 If HOOK is non-nil, it should be a function of no arguments;
@@ -123,7 +123,9 @@ usage-count, which is incremented each time the abbrev is used.
 \(The default is zero.)
 
 SYSTEM-FLAG, if non-nil, says that this is a "system" abbreviation
-which should not be saved in the user's abbreviation file.  */)
+which should not be saved in the user's abbreviation file.
+Unless SYSTEM-FLAG is `force', a system abbreviation will not
+overwrite a non-system abbreviation of the same name.  */)
      (table, name, expansion, hook, count, system_flag)
      Lisp_Object table, name, expansion, hook, count, system_flag;
 {
@@ -131,6 +133,16 @@ which should not be saved in the user's abbreviation file.  */)
   CHECK_VECTOR (table);
   CHECK_STRING (name);
 
+  /* If defining a system abbrev, do not overwrite a non-system abbrev
+     of the same name, unless 'force is used. */
+  if (!NILP (system_flag) && !EQ (system_flag, Qforce))
+    {
+      sym = Fintern_soft (name, table);
+
+      if (!NILP (SYMBOL_VALUE (sym)) &&
+          NILP (Fplist_get (XSYMBOL (sym)->plist, Qsystem_type))) return Qnil;
+    }
+
   if (NILP (count))
     count = make_number (0);
   else
@@ -160,12 +172,79 @@ which should not be saved in the user's abbreviation file.  */)
   return name;
 }
 
+/* Check if the characters in ABBREV have word syntax in either the
+ * current (if global == 0) or standard syntax table. */
+static void
+abbrev_check_chars (abbrev, global)
+     Lisp_Object abbrev;
+     int global;
+{
+  int i, i_byte, len, nbad = 0;
+  int j, found, nuniq = 0;
+  char *badchars, *baduniq;
+
+  CHECK_STRING (abbrev);
+  len = SCHARS (abbrev);
+
+  badchars = (char *) alloca (len + 1);
+
+  for (i = 0, i_byte = 0; i < len; )
+    {
+      int c;
+
+      FETCH_STRING_CHAR_ADVANCE (c, abbrev, i, i_byte);
+
+      if (global)
+        {
+          /* Copied from SYNTAX in syntax.h, except using FOLLOW_PARENT. */
+          Lisp_Object syntax_temp
+            = CHAR_TABLE_REF (Vstandard_syntax_table, c);
+          if ( (CONSP (syntax_temp)
+                ? (enum syntaxcode) (XINT (XCAR (syntax_temp)) & 0xff)
+                : Swhitespace) != Sword ) badchars[nbad++] = c;
+        }
+      else if (SYNTAX (c) != Sword)
+        badchars[nbad++] = c;
+    }
+
+  if (nbad == 0) return;
+
+  baduniq = (char *) alloca (nbad + 1);
+
+  for (i = 0; i < nbad; i++)
+    {
+      found = 0;
+
+      for (j = 0; j < nuniq; j++)
+        {
+          if (badchars[i] == baduniq[j])
+            {
+              found = 1;
+              break;
+            }
+        }
+
+      if (found) continue ;
+
+      baduniq[nuniq++] = badchars[i];
+    }
+
+  baduniq[nuniq] = '\0';
+
+  error ("Some abbrev characters (%s) are not word constituents %s",
+         baduniq, global ? "in the standard syntax" : "in this mode" );
+}
+
 DEFUN ("define-global-abbrev", Fdefine_global_abbrev, Sdefine_global_abbrev, 2, 2,
        "sDefine global abbrev: \nsExpansion for %s: ",
-       doc: /* Define ABBREV as a global abbreviation for EXPANSION.  */)
+       doc: /* Define ABBREV as a global abbreviation for EXPANSION.
+The characters in ABBREV must all be word constituents in the standard
+syntax table.  */)
      (abbrev, expansion)
      Lisp_Object abbrev, expansion;
 {
+  abbrev_check_chars (abbrev, 1);
+
   Fdefine_abbrev (Vglobal_abbrev_table, Fdowncase (abbrev),
                  expansion, Qnil, make_number (0), Qnil);
   return abbrev;
@@ -173,13 +252,16 @@ DEFUN ("define-global-abbrev", Fdefine_global_abbrev, Sdefine_global_abbrev, 2,
 
 DEFUN ("define-mode-abbrev", Fdefine_mode_abbrev, Sdefine_mode_abbrev, 2, 2,
        "sDefine mode abbrev: \nsExpansion for %s: ",
-       doc: /* Define ABBREV as a mode-specific abbreviation for EXPANSION.  */)
+       doc: /* Define ABBREV as a mode-specific abbreviation for EXPANSION.
+The characters in ABBREV must all be word-constituents in the current mode.  */)
      (abbrev, expansion)
      Lisp_Object abbrev, expansion;
 {
   if (NILP (current_buffer->abbrev_table))
     error ("Major mode has no abbrev table");
 
+  abbrev_check_chars (abbrev, 0);
+
   Fdefine_abbrev (current_buffer->abbrev_table, Fdowncase (abbrev),
                  expansion, Qnil, make_number (0), Qnil);
   return abbrev;
@@ -646,6 +728,9 @@ syms_of_abbrev ()
   Qcount = intern ("count");
   staticpro (&Qcount);
 
+  Qforce = intern ("force");
+  staticpro (&Qforce);
+
   DEFVAR_LISP ("abbrev-table-name-list", &Vabbrev_table_name_list,
               doc: /* List of symbols whose values are abbrev tables.  */);
   Vabbrev_table_name_list = Fcons (intern ("fundamental-mode-abbrev-table"),
@@ -670,7 +755,7 @@ for any particular abbrev defined in both.  */);
 
   DEFVAR_LISP ("last-abbrev-text", &Vlast_abbrev_text,
               doc: /* The exact text of the last abbrev expanded.
-nil if the abbrev has already been unexpanded.  */);
+A value of nil means the abbrev has already been unexpanded.  */);
 
   DEFVAR_INT ("last-abbrev-location", &last_abbrev_point,
              doc: /* The location of the start of the last abbrev expanded.  */);