]> code.delx.au - gnu-emacs/blobdiff - src/abbrev.c
(LIBS_MACHINE): Add -lelf.
[gnu-emacs] / src / abbrev.c
index 240f668d0908f8338973a11be11fe3b4d6b6bbb8..8c28e1c3ff01a027f5cb2af6fb004cddb647fb61 100644 (file)
@@ -104,18 +104,20 @@ DEFUN ("clear-abbrev-table", Fclear_abbrev_table, Sclear_abbrev_table, 1, 1, 0,
 \f
 DEFUN ("define-abbrev", Fdefine_abbrev, Sdefine_abbrev, 3, 5, 0,
   "Define an abbrev in TABLE named NAME, to expand to EXPANSION and call HOOK.\n\
-NAME and EXPANSION are strings.\n\
+NAME must be a string.\n\
+EXPANSION should usually be a string.\n\
 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.")
+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.")
   (table, name, expansion, hook, count)
      Lisp_Object table, name, expansion, hook, count;
 {
   Lisp_Object sym, oexp, ohook, tem;
   CHECK_VECTOR (table, 0);
   CHECK_STRING (name, 1);
-  if (!NILP (expansion))
-    CHECK_STRING (expansion, 2);
+
   if (NILP (count))
     count = make_number (0);
   else
@@ -212,7 +214,7 @@ then ABBREV is looked up in that table only.")
 DEFUN ("expand-abbrev", Fexpand_abbrev, Sexpand_abbrev, 0, 0, "",
   "Expand the abbrev before point, if there is an abbrev there.\n\
 Effective when explicitly called even when `abbrev-mode' is nil.\n\
-Returns t if expansion took place.")
+Returns the abbrev symbol, if expansion took place.")
   ()
 {
   register char *buffer, *p;
@@ -224,15 +226,14 @@ Returns t if expansion took place.")
   int oldmodiff = MODIFF;
   Lisp_Object value;
 
+  value = Qnil;
+
   if (!NILP (Vrun_hooks))
     call1 (Vrun_hooks, Qpre_abbrev_expand_hook);
-  /* If the hook changes the buffer, treat that as having "done an
-     expansion".  */
-  value = (MODIFF != oldmodiff ? Qt : Qnil);
 
   wordstart = 0;
-  if (!(BUFFERP (Vabbrev_start_location_buffer) &&
-       XBUFFER (Vabbrev_start_location_buffer) == current_buffer))
+  if (!(BUFFERP (Vabbrev_start_location_buffer)
+       && XBUFFER (Vabbrev_start_location_buffer) == current_buffer))
     Vabbrev_start_location = Qnil;
   if (!NILP (Vabbrev_start_location))
     {
@@ -242,7 +243,7 @@ Returns t if expansion took place.")
       Vabbrev_start_location = Qnil;
       if (wordstart < BEGV || wordstart > ZV)
        wordstart = 0;
-      if (wordstart && wordstart != ZV && FETCH_CHAR (wordstart) == '-')
+      if (wordstart && wordstart != ZV && FETCH_BYTE (wordstart) == '-')
        del_range (wordstart, wordstart + 1);
     }
   if (!wordstart)
@@ -265,7 +266,7 @@ Returns t if expansion took place.")
 
   for (idx = wordstart; idx < wordend; idx++)
     {
-      register int c = FETCH_CHAR (idx);
+      register int c = FETCH_BYTE (idx);
       if (UPPERCASEP (c))
        c = DOWNCASE (c), uccount++;
       else if (! NOCASEP (c))
@@ -289,59 +290,68 @@ Returns t if expansion took place.")
       SET_PT (wordend);
       Fundo_boundary ();
     }
-  SET_PT (wordstart);
+
   Vlast_abbrev_text
     = Fbuffer_substring (make_number (wordstart), make_number (wordend));
-  del_range (wordstart, wordend);
 
-  /* Now sym is the abbrev symbol. */
+  /* Now sym is the abbrev symbol.  */
   Vlast_abbrev = sym;
+  value = sym;
   last_abbrev_point = wordstart;
 
   if (INTEGERP (XSYMBOL (sym)->plist))
     XSETINT (XSYMBOL (sym)->plist,
             XINT (XSYMBOL (sym)->plist) + 1);  /* Increment use count */
 
+  /* If this abbrev has an expansion, delete the abbrev
+     and insert the expansion.  */
   expansion = XSYMBOL (sym)->value;
-  insert_from_string (expansion, 0, XSTRING (expansion)->size, 1);
-  SET_PT (PT + whitecnt);
-
-  if (uccount && !lccount)
+  if (STRINGP (expansion))
     {
-      /* Abbrev was all caps */
-      /* If expansion is multiple words, normally capitalize each word */
-      /* This used to be if (!... && ... >= ...) Fcapitalize; else Fupcase
-        but Megatest 68000 compiler can't handle that */
-      if (!abbrev_all_caps)
-       if (scan_words (PT, -1) > scan_words (wordstart, 1))
-         {
-           Fupcase_initials_region (make_number (wordstart),
-                                    make_number (PT));
-           goto caped;
-         }
-      /* If expansion is one word, or if user says so, upcase it all. */
-      Fupcase_region (make_number (wordstart), make_number (PT));
-    caped: ;
-    }
-  else if (uccount)
-    {
-      /* Abbrev included some caps.  Cap first initial of expansion */
-      int pos = wordstart;
-
-      /* Find the initial.  */
-      while (pos < PT
-            && SYNTAX (*BUF_CHAR_ADDRESS (current_buffer, pos)) != Sword)
-       pos++;
-
-      /* Change just that.  */
-      Fupcase_initials_region (make_number (pos), make_number (pos + 1));
+      SET_PT (wordstart);
+
+      del_range (wordstart, wordend);
+
+      insert_from_string (expansion, 0, XSTRING (expansion)->size, 1);
+      SET_PT (PT + whitecnt);
+
+      if (uccount && !lccount)
+       {
+         /* Abbrev was all caps */
+         /* If expansion is multiple words, normally capitalize each word */
+         /* This used to be if (!... && ... >= ...) Fcapitalize; else Fupcase
+            but Megatest 68000 compiler can't handle that */
+         if (!abbrev_all_caps)
+           if (scan_words (PT, -1) > scan_words (wordstart, 1))
+             {
+               Fupcase_initials_region (make_number (wordstart),
+                                        make_number (PT));
+               goto caped;
+             }
+         /* If expansion is one word, or if user says so, upcase it all. */
+         Fupcase_region (make_number (wordstart), make_number (PT));
+       caped: ;
+       }
+      else if (uccount)
+       {
+         /* Abbrev included some caps.  Cap first initial of expansion */
+         int pos = wordstart;
+
+         /* Find the initial.  */
+         while (pos < PT
+                && SYNTAX (*BUF_CHAR_ADDRESS (current_buffer, pos)) != Sword)
+           pos++;
+
+         /* Change just that.  */
+         Fupcase_initials_region (make_number (pos), make_number (pos + 1));
+       }
     }
 
   hook = XSYMBOL (sym)->function;
   if (!NILP (hook))
     call0 (hook);
 
-  return Qt;
+  return value;
 }
 
 DEFUN ("unexpand-abbrev", Funexpand_abbrev, Sunexpand_abbrev, 0, 0, "",