*** empty log message ***
[bpt/emacs.git] / src / abbrev.c
index dabc03b..bdb8dc6 100644 (file)
@@ -1,12 +1,12 @@
 /* Primitives for word-abbrev mode.
-   Copyright (C) 1985, 1986, 1993, 1996, 1998, 2001
-   Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1993, 1996, 1998, 2001, 2002, 2003, 2004,
+                 2005, 2006, 2007 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs 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.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -16,8 +16,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 
 #include <config.h>
@@ -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
@@ -356,10 +368,13 @@ Returns the abbrev symbol, if expansion took place.  */)
     {
       SET_PT (wordstart);
 
-      del_range_both (wordstart, wordstart_byte, wordend, wordend_byte, 1);
-
       insert_from_string (expansion, 0, 0, SCHARS (expansion),
                          SBYTES (expansion), 1);
+      del_range_both (PT, PT_BYTE,
+                     wordend + (PT - wordstart),
+                     wordend_byte + (PT_BYTE - wordstart_byte),
+                     1);
+
       SET_PT (PT + whitecnt);
 
       if (uccount && !lccount)
@@ -439,7 +454,7 @@ is not undone.  */)
 
       val = SYMBOL_VALUE (Vlast_abbrev);
       if (!STRINGP (val))
-       error ("value of abbrev-symbol must be a string");
+       error ("Value of `abbrev-symbol' must be a string");
       zv_before = ZV;
       del_range_byte (PT_BYTE, PT_BYTE + SBYTES (val), 1);
       /* Don't inherit properties here; just copy from old contents.  */
@@ -528,6 +543,13 @@ describe_abbrev (sym, stream)
   Fterpri (stream);
 }
 
+static void
+record_symbol (sym, list)
+     Lisp_Object sym, list;
+{
+  XSETCDR (list, Fcons (sym, XCDR (list)));
+}
+
 DEFUN ("insert-abbrev-table-description", Finsert_abbrev_table_description,
        Sinsert_abbrev_table_description, 1, 2, 0,
        doc: /* Insert before point a full description of abbrev table named NAME.
@@ -537,11 +559,13 @@ is inserted.  Otherwise the description is an expression,
 a call to `define-abbrev-table', which would
 define the abbrev table NAME exactly as it is currently defined.
 
-Abbrevs marked as "system abbrevs" are omitted.  */)
+Abbrevs marked as "system abbrevs" are normally omitted.  However, if
+READABLE is non-nil, they are listed.  */)
      (name, readable)
      Lisp_Object name, readable;
 {
   Lisp_Object table;
+  Lisp_Object symbols;
   Lisp_Object stream;
 
   CHECK_SYMBOL (name);
@@ -550,12 +574,22 @@ Abbrevs marked as "system abbrevs" are omitted.  */)
 
   XSETBUFFER (stream, current_buffer);
 
+  symbols = Fcons (Qnil, Qnil);
+  map_obarray (table, record_symbol, symbols);
+  symbols = XCDR (symbols);
+  symbols = Fsort (symbols, Qstring_lessp);
+
   if (!NILP (readable))
     {
       insert_string ("(");
       Fprin1 (name, stream);
       insert_string (")\n\n");
-      map_obarray (table, describe_abbrev, stream);
+      while (! NILP (symbols))
+       {
+         describe_abbrev (XCAR (symbols), stream);
+         symbols = XCDR (symbols);
+       }
+
       insert_string ("\n\n");
     }
   else
@@ -563,7 +597,11 @@ Abbrevs marked as "system abbrevs" are omitted.  */)
       insert_string ("(define-abbrev-table '");
       Fprin1 (name, stream);
       insert_string (" '(\n");
-      map_obarray (table, write_abbrev, stream);
+      while (! NILP (symbols))
+       {
+         write_abbrev (XCAR (symbols), stream);
+         symbols = XCDR (symbols);
+       }
       insert_string ("    ))\n\n");
     }
 
@@ -614,6 +652,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"),
@@ -638,7 +679,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.  */);
@@ -690,3 +731,6 @@ the current abbrev table before abbrev lookup happens.  */);
   defsubr (&Sinsert_abbrev_table_description);
   defsubr (&Sdefine_abbrev_table);
 }
+
+/* arch-tag: b721db69-f633-44a8-a361-c275acbdad7d
+   (do not change this comment) */