/* Primitives for word-abbrev mode.
- Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
+ Copyright (C) 1985, 1986, 1993, 1996, 1998 Free Software Foundation, Inc.
This file is part of GNU Emacs.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA. */
-#include "config.h"
+#include <config.h>
#include <stdio.h>
#include "lisp.h"
#include "commands.h"
#include "buffer.h"
#include "window.h"
+#include "charset.h"
+#include "syntax.h"
/* An abbrev table is an obarray.
Each defined abbrev is represented by a symbol in that obarray
\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.\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;
{
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
oexp = XSYMBOL (sym)->value;
ohook = XSYMBOL (sym)->function;
if (!((EQ (oexp, expansion)
- || (XTYPE (oexp) == Lisp_String && XTYPE (expansion) == Lisp_String
+ || (STRINGP (oexp) && STRINGP (expansion)
&& (tem = Fstring_equal (oexp, expansion), !NILP (tem))))
&&
(EQ (ohook, hook)
DEFUN ("define-global-abbrev", Fdefine_global_abbrev, Sdefine_global_abbrev, 2, 2,
"sDefine global abbrev: \nsExpansion for %s: ",
"Define ABBREV as a global abbreviation for EXPANSION.")
- (name, expansion)
- Lisp_Object name, expansion;
+ (abbrev, expansion)
+ Lisp_Object abbrev, expansion;
{
- Fdefine_abbrev (Vglobal_abbrev_table, Fdowncase (name),
+ Fdefine_abbrev (Vglobal_abbrev_table, Fdowncase (abbrev),
expansion, Qnil, make_number (0));
- return name;
+ return abbrev;
}
DEFUN ("define-mode-abbrev", Fdefine_mode_abbrev, Sdefine_mode_abbrev, 2, 2,
"sDefine mode abbrev: \nsExpansion for %s: ",
"Define ABBREV as a mode-specific abbreviation for EXPANSION.")
- (name, expansion)
- Lisp_Object name, expansion;
+ (abbrev, expansion)
+ Lisp_Object abbrev, expansion;
{
if (NILP (current_buffer->abbrev_table))
error ("Major mode has no abbrev table");
- Fdefine_abbrev (current_buffer->abbrev_table, Fdowncase (name),
+ Fdefine_abbrev (current_buffer->abbrev_table, Fdowncase (abbrev),
expansion, Qnil, make_number (0));
- return name;
+ return abbrev;
}
DEFUN ("abbrev-symbol", Fabbrev_symbol, Sabbrev_symbol, 1, 2, 0,
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;
- register int wordstart, wordend, idx;
+ int wordstart, wordend;
+ register int wordstart_byte, wordend_byte, idx;
int whitecnt;
int uccount = 0, lccount = 0;
register Lisp_Object sym;
Lisp_Object expansion, hook, tem;
- 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);
- if (XBUFFER (Vabbrev_start_location_buffer) != current_buffer)
+ wordstart = 0;
+ if (!(BUFFERP (Vabbrev_start_location_buffer)
+ && XBUFFER (Vabbrev_start_location_buffer) == current_buffer))
Vabbrev_start_location = Qnil;
if (!NILP (Vabbrev_start_location))
{
CHECK_NUMBER_COERCE_MARKER (tem, 0);
wordstart = XINT (tem);
Vabbrev_start_location = Qnil;
- if (FETCH_CHAR (wordstart) == '-')
- del_range (wordstart, wordstart + 1);
+ if (wordstart < BEGV || wordstart > ZV)
+ wordstart = 0;
+ if (wordstart && wordstart != ZV)
+ {
+ wordstart_byte = CHAR_TO_BYTE (wordstart);
+ if (FETCH_BYTE (wordstart_byte) == '-')
+ del_range (wordstart, wordstart + 1);
+ }
}
- else
- wordstart = scan_words (point, -1);
+ if (!wordstart)
+ wordstart = scan_words (PT, -1);
if (!wordstart)
return value;
+ wordstart_byte = CHAR_TO_BYTE (wordstart);
wordend = scan_words (wordstart, 1);
if (!wordend)
return value;
- if (wordend > point)
- wordend = point;
- whitecnt = point - wordend;
+ if (wordend > PT)
+ wordend = PT;
+
+ wordend_byte = CHAR_TO_BYTE (wordend);
+ whitecnt = PT - wordend;
if (wordend <= wordstart)
return value;
- p = buffer = (char *) alloca (wordend - wordstart);
+ p = buffer = (char *) alloca (wordend_byte - wordstart_byte);
- for (idx = wordstart; idx < wordend; idx++)
+ for (idx = wordstart_byte; idx < wordend_byte; idx++)
{
- register int c = FETCH_CHAR (idx);
+ /* ??? This loop needs to go by characters! */
+ register int c = FETCH_BYTE (idx);
if (UPPERCASEP (c))
c = DOWNCASE (c), uccount++;
else if (! NOCASEP (c))
*p++ = c;
}
- if (XTYPE (current_buffer->abbrev_table) == Lisp_Vector)
- sym = oblookup (current_buffer->abbrev_table, buffer, p - buffer);
+ if (VECTORP (current_buffer->abbrev_table))
+ sym = oblookup (current_buffer->abbrev_table, buffer,
+ wordend - wordstart, wordend_byte - wordstart_byte);
else
- XFASTINT (sym) = 0;
- if (XTYPE (sym) == Lisp_Int || NILP (XSYMBOL (sym)->value))
- sym = oblookup (Vglobal_abbrev_table, buffer, p - buffer);
- if (XTYPE (sym) == Lisp_Int || NILP (XSYMBOL (sym)->value))
+ XSETFASTINT (sym, 0);
+ if (INTEGERP (sym) || NILP (XSYMBOL (sym)->value))
+ sym = oblookup (Vglobal_abbrev_table, buffer,
+ wordend - wordstart, wordend_byte - wordstart_byte);
+ if (INTEGERP (sym) || NILP (XSYMBOL (sym)->value))
return value;
if (INTERACTIVE && !EQ (minibuf_window, selected_window))
{
+ /* Add an undo boundary, in case we are doing this for
+ a self-inserting command which has avoided making one so far. */
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 (XTYPE (XSYMBOL (sym)->plist) == Lisp_Int)
+ 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);
- SET_PT (point + 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 (point, -1) > scan_words (wordstart, 1))
- {
- upcase_initials_region (make_number (wordstart),
- make_number (point));
- goto caped;
- }
- /* If expansion is one word, or if user says so, upcase it all. */
- Fupcase_region (make_number (wordstart), make_number (point));
- caped: ;
- }
- else if (uccount)
- {
- /* Abbrev included some caps. Cap first initial of expansion */
- int old_zv = ZV;
- int old_pt = point;
-
- /* Don't let Fcapitalize_word operate on text after point. */
- ZV = point;
SET_PT (wordstart);
- Fcapitalize_word (make_number (1));
- SET_PT (old_pt);
- ZV = old_zv;
+ del_range_both (wordstart, wordstart_byte, wordend, wordend_byte, 1);
+
+ insert_from_string (expansion, 0, 0, XSTRING (expansion)->size,
+ STRING_BYTES (XSTRING (expansion)), 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_byte;
+
+ /* Find the initial. */
+ while (pos < PT_BYTE
+ && SYNTAX (*BUF_BYTE_ADDRESS (current_buffer, pos)) != Sword)
+ pos++;
+
+ /* Change just that. */
+ pos = BYTE_TO_CHAR (pos);
+ Fupcase_initials_region (make_number (pos), make_number (pos + 1));
+ }
}
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);
- return Qt;
+ /* 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;
}
DEFUN ("unexpand-abbrev", Funexpand_abbrev, Sunexpand_abbrev, 0, 0, "",
is not undone.")
()
{
- int opoint = point;
+ int opoint = PT;
int adjust = 0;
if (last_abbrev_point < BEGV
|| last_abbrev_point > ZV)
return Qnil;
SET_PT (last_abbrev_point);
- if (XTYPE (Vlast_abbrev_text) == Lisp_String)
+ if (STRINGP (Vlast_abbrev_text))
{
/* This isn't correct if Vlast_abbrev->function was used
to do the expansion */
Lisp_Object val;
+ int zv_before;
+
val = XSYMBOL (Vlast_abbrev)->value;
- if (XTYPE (val) != Lisp_String)
+ if (!STRINGP (val))
error ("value of abbrev-symbol must be a string");
- adjust = XSTRING (val)->size;
- del_range (point, point + adjust);
- insert_from_string (Vlast_abbrev_text, 0,
- XSTRING (Vlast_abbrev_text)->size);
- adjust -= XSTRING (Vlast_abbrev_text)->size;
+ zv_before = ZV;
+ 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,
+ 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
-static
+static void
write_abbrev (sym, stream)
Lisp_Object sym, stream;
{
if (NILP (XSYMBOL (sym)->value))
return;
insert (" (", 5);
- XSET (name, Lisp_String, XSYMBOL (sym)->name);
+ XSETSTRING (name, XSYMBOL (sym)->name);
Fprin1 (name, stream);
insert (" ", 1);
Fprin1 (XSYMBOL (sym)->value, stream);
insert (")\n", 2);
}
-static
+static void
describe_abbrev (sym, stream)
Lisp_Object sym, stream;
{
Fterpri (stream);
}
-DEFUN ("insert-abbrev-table-description",
- Finsert_abbrev_table_description, Sinsert_abbrev_table_description,
- 1, 2, 0,
+DEFUN ("insert-abbrev-table-description", Finsert_abbrev_table_description,
+ Sinsert_abbrev_table_description, 1, 2, 0,
"Insert before point a full description of abbrev table named NAME.\n\
NAME is a symbol whose value is an abbrev table.\n\
-If optional 2nd arg HUMAN is non-nil, a human-readable description is inserted.\n\
-Otherwise the description is an expression,\n\
+If optional 2nd arg READABLE is non-nil, a human-readable description\n\
+is inserted. Otherwise the description is an expression,\n\
a call to `define-abbrev-table', which would\n\
define the abbrev table NAME exactly as it is currently defined.")
(name, readable)
table = Fsymbol_value (name);
CHECK_VECTOR (table, 0);
- XSET (stream, Lisp_Buffer, current_buffer);
+ XSETBUFFER (stream, current_buffer);
if (!NILP (readable))
{
\f
DEFUN ("define-abbrev-table", Fdefine_abbrev_table, Sdefine_abbrev_table,
2, 2, 0,
- "Define TABNAME (a symbol) as an abbrev table name.\n\
+ "Define TABLENAME (a symbol) as an abbrev table name.\n\
Define abbrevs in it according to DEFINITIONS, which is a list of elements\n\
of the form (ABBREVNAME EXPANSION HOOK USECOUNT).")
- (tabname, defns)
- Lisp_Object tabname, defns;
+ (tablename, definitions)
+ Lisp_Object tablename, definitions;
{
Lisp_Object name, exp, hook, count;
Lisp_Object table, elt;
- CHECK_SYMBOL (tabname, 0);
- table = Fboundp (tabname);
- if (NILP (table) || (table = Fsymbol_value (tabname), NILP (table)))
+ CHECK_SYMBOL (tablename, 0);
+ table = Fboundp (tablename);
+ if (NILP (table) || (table = Fsymbol_value (tablename), NILP (table)))
{
table = Fmake_abbrev_table ();
- Fset (tabname, table);
- Vabbrev_table_name_list =
- Fcons (tabname, Vabbrev_table_name_list);
+ Fset (tablename, table);
+ Vabbrev_table_name_list = Fcons (tablename, Vabbrev_table_name_list);
}
CHECK_VECTOR (table, 0);
- for (;!NILP (defns); defns = Fcdr (defns))
+ for (; !NILP (definitions); definitions = Fcdr (definitions))
{
- elt = Fcar (defns);
+ elt = Fcar (definitions);
name = Fcar (elt); elt = Fcdr (elt);
exp = Fcar (elt); elt = Fcdr (elt);
hook = Fcar (elt); elt = Fcdr (elt);
return Qnil;
}
\f
+void
syms_of_abbrev ()
{
DEFVAR_LISP ("abbrev-table-name-list", &Vabbrev_table_name_list,
"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'.");