Add the latest changes to etags behaviour.
[bpt/emacs.git] / src / abbrev.c
index bcf974f..f30986d 100644 (file)
@@ -25,6 +25,7 @@ Boston, MA 02111-1307, USA.  */
 #include "commands.h"
 #include "buffer.h"
 #include "window.h"
+#include "charset.h"
 #include "syntax.h"
 
 /* An abbrev table is an obarray.
@@ -110,7 +111,9 @@ To undefine an abbrev, define it with EXPANSION = nil.\n\
 If HOOK is non-nil, it should be a function of no arguments;\n\
 it is called after EXPANSION is inserted.\n\
 If EXPANSION is not a string, the abbrev is a special one,\n\
- which does not expand in the usual way but only runs HOOK.")
+ which does not expand in the usual way but only runs HOOK.\n\
+COUNT, if specified, initializes the abbrev's usage-count\n\
+which is incremented each time the abbrev is used.")
   (table, name, expansion, hook, count)
      Lisp_Object table, name, expansion, hook, count;
 {
@@ -224,7 +227,6 @@ Returns the abbrev symbol, if expansion took place.")
   int uccount = 0, lccount = 0;
   register Lisp_Object sym;
   Lisp_Object expansion, hook, tem;
-  int oldmodiff = MODIFF;
   Lisp_Object value;
 
   value = Qnil;
@@ -321,10 +323,10 @@ Returns the abbrev symbol, if expansion took place.")
     {
       SET_PT (wordstart);
 
-      del_range_both (wordstart, wordend, wordstart_byte, wordend_byte, 1);
+      del_range_both (wordstart, wordstart_byte, wordend, wordend_byte, 1);
 
       insert_from_string (expansion, 0, 0, XSTRING (expansion)->size,
-                         XSTRING (expansion)->size_byte, 1);
+                         STRING_BYTES (XSTRING (expansion)), 1);
       SET_PT (PT + whitecnt);
 
       if (uccount && !lccount)
@@ -362,7 +364,23 @@ Returns the abbrev symbol, if expansion took place.")
 
   hook = XSYMBOL (sym)->function;
   if (!NILP (hook))
-    call0 (hook);
+    {
+      Lisp_Object expanded, prop;
+
+      /* If the abbrev has a hook function, run it.  */
+      expanded = call0 (hook);
+
+      /* In addition, if the hook function is a symbol with a a
+        non-nil `no-self-insert' property, let the value it returned
+        specify whether we consider that an expansion took place.  If
+        it returns nil, no expansion has been done.  */
+
+      if (SYMBOLP (hook)
+         && NILP (expanded)
+         && (prop = Fget (hook, intern ("no-self-insert")),
+             !NILP (prop)))
+       value = Qnil;
+    }
 
   return value;
 }
@@ -390,16 +408,16 @@ is not undone.")
       if (!STRINGP (val))
        error ("value of abbrev-symbol must be a string");
       zv_before = ZV;
-      del_range_byte (PT_BYTE, PT_BYTE + XSTRING (val)->size_byte, 1);
+      del_range_byte (PT_BYTE, PT_BYTE + STRING_BYTES (XSTRING (val)), 1);
       /* Don't inherit properties here; just copy from old contents.  */
       insert_from_string (Vlast_abbrev_text, 0, 0,
                          XSTRING (Vlast_abbrev_text)->size,
-                         XSTRING (Vlast_abbrev_text)->size_byte, 0);
+                         STRING_BYTES (XSTRING (Vlast_abbrev_text)), 0);
       Vlast_abbrev_text = Qnil;
       /* Total number of characters deleted.  */
       adjust = ZV - zv_before;
     }
-  SET_PT (last_abbrev_point < opoint ? opoint - adjust : opoint);
+  SET_PT (last_abbrev_point < opoint ? opoint + adjust : opoint);
   return Qnil;
 }
 \f
@@ -517,6 +535,7 @@ of the form (ABBREVNAME EXPANSION HOOK USECOUNT).")
   return Qnil;
 }
 \f
+void
 syms_of_abbrev ()
 {
   DEFVAR_LISP ("abbrev-table-name-list", &Vabbrev_table_name_list,
@@ -536,6 +555,7 @@ for any particular abbrev defined in both.");
     "The abbrev table of mode-specific abbrevs for Fundamental Mode.");
   Vfundamental_mode_abbrev_table = Fmake_abbrev_table ();
   current_buffer->abbrev_table = Vfundamental_mode_abbrev_table;
+  buffer_defaults.abbrev_table = Vfundamental_mode_abbrev_table;
 
   DEFVAR_LISP ("last-abbrev", &Vlast_abbrev,
     "The abbrev-symbol of the last abbrev expanded.  See `abbrev-symbol'.");