1 /* Primitives for word-abbrev mode.
2 Copyright (C) 1985, 1986 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* An abbrev table is an obarray.
30 Each defined abbrev is represented by a symbol in that obarray
31 whose print name is the abbreviation.
32 The symbol's value is a string which is the expansion.
33 If its function definition is non-nil, it is called
34 after the expansion is done.
35 The plist slot of the abbrev symbol is its usage count. */
37 /* List of all abbrev-table name symbols:
38 symbols whose values are abbrev tables. */
40 Lisp_Object Vabbrev_table_name_list
;
42 /* The table of global abbrevs. These are in effect
43 in any buffer in which abbrev mode is turned on. */
45 Lisp_Object Vglobal_abbrev_table
;
47 /* The local abbrev table used by default (in Fundamental Mode buffers) */
49 Lisp_Object Vfundamental_mode_abbrev_table
;
51 /* Set nonzero when an abbrev definition is changed */
57 /* Non-nil => use this location as the start of abbrev to expand
58 (rather than taking the word before point as the abbrev) */
60 Lisp_Object Vabbrev_start_location
;
62 /* Buffer that Vabbrev_start_location applies to */
63 Lisp_Object Vabbrev_start_location_buffer
;
65 /* The symbol representing the abbrev most recently expanded */
67 Lisp_Object Vlast_abbrev
;
69 /* A string for the actual text of the abbrev most recently expanded.
70 This has more info than Vlast_abbrev since case is significant. */
72 Lisp_Object Vlast_abbrev_text
;
74 /* Character address of start of last abbrev expanded */
76 int last_abbrev_point
;
79 DEFUN ("make-abbrev-table", Fmake_abbrev_table
, Smake_abbrev_table
, 0, 0, 0,
80 "Create a new, empty abbrev table object.")
83 return Fmake_vector (make_number (59), make_number (0));
86 DEFUN ("clear-abbrev-table", Fclear_abbrev_table
, Sclear_abbrev_table
, 1, 1, 0,
87 "Undefine all abbrevs in abbrev table TABLE, leaving it empty.")
93 CHECK_VECTOR (table
, 0);
94 size
= XVECTOR (table
)->size
;
96 for (i
= 0; i
< size
; i
++)
97 XVECTOR (table
)->contents
[i
] = make_number (0);
101 DEFUN ("define-abbrev", Fdefine_abbrev
, Sdefine_abbrev
, 3, 5, 0,
102 "Define an abbrev in TABLE named NAME, to expand to EXPANSION and call HOOK.\n\
103 NAME and EXPANSION are strings.\n\
104 To undefine an abbrev, define it with EXPANSION = nil.\n\
105 If HOOK is non-nil, it should be a function of no arguments;\n\
106 it is called after EXPANSION is inserted.")
107 (table
, name
, expansion
, hook
, count
)
108 Lisp_Object table
, name
, expansion
, hook
, count
;
110 Lisp_Object sym
, oexp
, ohook
, tem
;
111 CHECK_VECTOR (table
, 0);
112 CHECK_STRING (name
, 1);
113 if (!NULL (expansion
))
114 CHECK_STRING (expansion
, 2);
116 count
= make_number (0);
118 CHECK_NUMBER (count
, 0);
120 sym
= Fintern (name
, table
);
122 oexp
= XSYMBOL (sym
)->value
;
123 ohook
= XSYMBOL (sym
)->function
;
124 if (!((EQ (oexp
, expansion
)
125 || (XTYPE (oexp
) == Lisp_String
&& XTYPE (expansion
) == Lisp_String
126 && (tem
= Fstring_equal (oexp
, expansion
), !NULL (tem
))))
129 || (tem
= Fequal (ohook
, hook
), !NULL (tem
)))))
132 Fset (sym
, expansion
);
134 Fsetplist (sym
, count
);
139 DEFUN ("define-global-abbrev", Fdefine_global_abbrev
, Sdefine_global_abbrev
, 2, 2,
140 "sDefine global abbrev: \nsExpansion for %s: ",
141 "Define ABBREV as a global abbreviation for EXPANSION.")
143 Lisp_Object name
, expansion
;
145 Fdefine_abbrev (Vglobal_abbrev_table
, Fdowncase (name
),
146 expansion
, Qnil
, make_number (0));
150 DEFUN ("define-mode-abbrev", Fdefine_mode_abbrev
, Sdefine_mode_abbrev
, 2, 2,
151 "sDefine mode abbrev: \nsExpansion for %s: ",
152 "Define ABBREV as a mode-specific abbreviation for EXPANSION.")
154 Lisp_Object name
, expansion
;
156 if (NULL (current_buffer
->abbrev_table
))
157 error ("Major mode has no abbrev table");
159 Fdefine_abbrev (current_buffer
->abbrev_table
, Fdowncase (name
),
160 expansion
, Qnil
, make_number (0));
164 DEFUN ("abbrev-symbol", Fabbrev_symbol
, Sabbrev_symbol
, 1, 2, 0,
165 "Return the symbol representing abbrev named ABBREV.\n\
166 This symbol's name is ABBREV, but it is not the canonical symbol of that name;\n\
167 it is interned in an abbrev-table rather than the normal obarray.\n\
168 The value is nil if that abbrev is not defined.\n\
169 Optional second arg TABLE is abbrev table to look it up in.\n\
170 The default is to try buffer's mode-specific abbrev table, then global table.")
172 Lisp_Object abbrev
, table
;
175 CHECK_STRING (abbrev
, 0);
177 sym
= Fintern_soft (abbrev
, table
);
181 if (!NULL (current_buffer
->abbrev_table
))
182 sym
= Fintern_soft (abbrev
, current_buffer
->abbrev_table
);
183 if (NULL (XSYMBOL (sym
)->value
))
186 sym
= Fintern_soft (abbrev
, Vglobal_abbrev_table
);
188 if (NULL (XSYMBOL (sym
)->value
)) return Qnil
;
192 DEFUN ("abbrev-expansion", Fabbrev_expansion
, Sabbrev_expansion
, 1, 2, 0,
193 "Return the string that ABBREV expands into in the current buffer.\n\
194 Optionally specify an abbrev table as second arg;\n\
195 then ABBREV is looked up in that table only.")
197 Lisp_Object abbrev
, table
;
200 sym
= Fabbrev_symbol (abbrev
, table
);
201 if (NULL (sym
)) return sym
;
202 return Fsymbol_value (sym
);
205 /* Expand the word before point, if it is an abbrev.
206 Returns 1 if an expansion is done. */
208 DEFUN ("expand-abbrev", Fexpand_abbrev
, Sexpand_abbrev
, 0, 0, "",
209 "Expand the abbrev before point, if there is an abbrev there.\n\
210 Effective when explicitly called even when `abbrev-mode' is nil.\n\
211 Returns t if expansion took place.")
214 register char *buffer
, *p
;
215 register int wordstart
, wordend
, idx
;
217 int uccount
= 0, lccount
= 0;
218 register Lisp_Object sym
;
219 Lisp_Object expansion
, hook
, tem
;
221 if (XBUFFER (Vabbrev_start_location_buffer
) != current_buffer
)
222 Vabbrev_start_location
= Qnil
;
223 if (!NULL (Vabbrev_start_location
))
225 tem
= Vabbrev_start_location
;
226 CHECK_NUMBER_COERCE_MARKER (tem
, 0);
227 wordstart
= XINT (tem
);
228 Vabbrev_start_location
= Qnil
;
229 if (FETCH_CHAR (wordstart
) == '-')
230 del_range (wordstart
, wordstart
+ 1);
233 wordstart
= scan_words (point
, -1);
238 wordend
= scan_words (wordstart
, 1);
244 whitecnt
= point
- wordend
;
245 if (wordend
<= wordstart
)
248 p
= buffer
= (char *) alloca (wordend
- wordstart
);
250 for (idx
= wordstart
; idx
< point
; idx
++)
252 register int c
= FETCH_CHAR (idx
);
254 c
= DOWNCASE (c
), uccount
++;
255 else if (! NOCASEP (c
))
260 if (XTYPE (current_buffer
->abbrev_table
) == Lisp_Vector
)
261 sym
= oblookup (current_buffer
->abbrev_table
, buffer
, p
- buffer
);
264 if (XTYPE (sym
) == Lisp_Int
|| NULL (XSYMBOL (sym
)->value
))
265 sym
= oblookup (Vglobal_abbrev_table
, buffer
, p
- buffer
);
266 if (XTYPE (sym
) == Lisp_Int
|| NULL (XSYMBOL (sym
)->value
))
269 if (INTERACTIVE
&& !EQ (minibuf_window
, selected_window
))
276 = Fbuffer_substring (make_number (wordstart
), make_number (wordend
));
277 del_range (wordstart
, wordend
);
279 /* Now sym is the abbrev symbol. */
281 last_abbrev_point
= wordstart
;
283 if (XTYPE (XSYMBOL (sym
)->plist
) == Lisp_Int
)
284 XSETINT (XSYMBOL (sym
)->plist
,
285 XINT (XSYMBOL (sym
)->plist
) + 1); /* Increment use count */
287 expansion
= XSYMBOL (sym
)->value
;
288 insert_from_string (expansion
, 0, XSTRING (expansion
)->size
);
289 SET_PT (point
+ whitecnt
);
291 if (uccount
&& !lccount
)
293 /* Abbrev was all caps */
294 /* If expansion is multiple words, normally capitalize each word */
295 /* This used to be if (!... && ... >= ...) Fcapitalize; else Fupcase
296 but Megatest 68000 compiler can't handle that */
297 if (!abbrev_all_caps
)
298 if (scan_words (point
, -1) > scan_words (wordstart
, 1))
300 upcase_initials_region (make_number (wordstart
),
301 make_number (point
));
304 /* If expansion is one word, or if user says so, upcase it all. */
305 Fupcase_region (make_number (wordstart
), make_number (point
));
310 /* Abbrev included some caps. Cap first initial of expansion */
313 Fcapitalize_word (make_number (1));
317 hook
= XSYMBOL (sym
)->function
;
324 DEFUN ("unexpand-abbrev", Funexpand_abbrev
, Sunexpand_abbrev
, 0, 0, "",
325 "Undo the expansion of the last abbrev that expanded.\n\
326 This differs from ordinary undo in that other editing done since then\n\
332 if (last_abbrev_point
< BEGV
333 || last_abbrev_point
> ZV
)
335 SET_PT (last_abbrev_point
);
336 if (XTYPE (Vlast_abbrev_text
) == Lisp_String
)
338 /* This isn't correct if Vlast_abbrev->function was used
339 to do the expansion */
341 XSET (val
, Lisp_String
, XSYMBOL (Vlast_abbrev
)->value
);
342 adjust
= XSTRING (val
)->size
;
343 del_range (point
, point
+ adjust
);
344 insert_from_string (Vlast_abbrev_text
, 0,
345 XSTRING (Vlast_abbrev_text
)->size
);
346 adjust
-= XSTRING (Vlast_abbrev_text
)->size
;
347 Vlast_abbrev_text
= Qnil
;
349 SET_PT (last_abbrev_point
< opoint
? opoint
- adjust
: opoint
);
354 write_abbrev (sym
, stream
)
355 Lisp_Object sym
, stream
;
358 if (NULL (XSYMBOL (sym
)->value
))
361 XSET (name
, Lisp_String
, XSYMBOL (sym
)->name
);
362 Fprin1 (name
, stream
);
364 Fprin1 (XSYMBOL (sym
)->value
, stream
);
366 Fprin1 (XSYMBOL (sym
)->function
, stream
);
368 Fprin1 (XSYMBOL (sym
)->plist
, stream
);
373 describe_abbrev (sym
, stream
)
374 Lisp_Object sym
, stream
;
378 if (NULL (XSYMBOL (sym
)->value
))
380 one
= make_number (1);
381 Fprin1 (Fsymbol_name (sym
), stream
);
382 Findent_to (make_number (15), one
);
383 Fprin1 (XSYMBOL (sym
)->plist
, stream
);
384 Findent_to (make_number (20), one
);
385 Fprin1 (XSYMBOL (sym
)->value
, stream
);
386 if (!NULL (XSYMBOL (sym
)->function
))
388 Findent_to (make_number (45), one
);
389 Fprin1 (XSYMBOL (sym
)->function
, stream
);
394 DEFUN ("insert-abbrev-table-description",
395 Finsert_abbrev_table_description
, Sinsert_abbrev_table_description
,
397 "Insert before point a full description of abbrev table named NAME.\n\
398 NAME is a symbol whose value is an abbrev table.\n\
399 If optional 2nd arg HUMAN is non-nil, a human-readable description is inserted.\n\
400 Otherwise the description is an expression,\n\
401 a call to `define-abbrev-table', which would\n\
402 define the abbrev table NAME exactly as it is currently defined.")
404 Lisp_Object name
, readable
;
409 CHECK_SYMBOL (name
, 0);
410 table
= Fsymbol_value (name
);
411 CHECK_VECTOR (table
, 0);
413 XSET (stream
, Lisp_Buffer
, current_buffer
);
415 if (!NULL (readable
))
418 Fprin1 (name
, stream
);
419 insert_string (")\n\n");
420 map_obarray (table
, describe_abbrev
, stream
);
421 insert_string ("\n\n");
425 insert_string ("(define-abbrev-table '");
426 Fprin1 (name
, stream
);
427 insert_string (" '(\n");
428 map_obarray (table
, write_abbrev
, stream
);
429 insert_string (" ))\n\n");
435 DEFUN ("define-abbrev-table", Fdefine_abbrev_table
, Sdefine_abbrev_table
,
437 "Define TABNAME (a symbol) as an abbrev table name.\n\
438 Define abbrevs in it according to DEFINITIONS, which is a list of elements\n\
439 of the form (ABBREVNAME EXPANSION HOOK USECOUNT).")
441 Lisp_Object tabname
, defns
;
443 Lisp_Object name
, exp
, hook
, count
;
444 Lisp_Object table
, elt
;
446 CHECK_SYMBOL (tabname
, 0);
447 table
= Fboundp (tabname
);
448 if (NULL (table
) || (table
= Fsymbol_value (tabname
), NULL (table
)))
450 table
= Fmake_abbrev_table ();
451 Fset (tabname
, table
);
452 Vabbrev_table_name_list
=
453 Fcons (tabname
, Vabbrev_table_name_list
);
455 CHECK_VECTOR (table
, 0);
457 for (;!NULL (defns
); defns
= Fcdr (defns
))
467 Fdefine_abbrev (table
, name
, exp
, hook
, count
);
474 DEFVAR_LISP ("abbrev-table-name-list", &Vabbrev_table_name_list
,
475 "List of symbols whose values are abbrev tables.");
476 Vabbrev_table_name_list
= Fcons (intern ("fundamental-mode-abbrev-table"),
477 Fcons (intern ("global-abbrev-table"),
480 DEFVAR_LISP ("global-abbrev-table", &Vglobal_abbrev_table
,
481 "The abbrev table whose abbrevs affect all buffers.\n\
482 Each buffer may also have a local abbrev table.\n\
483 If it does, the local table overrides the global one\n\
484 for any particular abbrev defined in both.");
485 Vglobal_abbrev_table
= Fmake_abbrev_table ();
487 DEFVAR_LISP ("fundamental-mode-abbrev-table", &Vfundamental_mode_abbrev_table
,
488 "The abbrev table of mode-specific abbrevs for Fundamental Mode.");
489 Vfundamental_mode_abbrev_table
= Fmake_abbrev_table ();
490 current_buffer
->abbrev_table
= Vfundamental_mode_abbrev_table
;
492 DEFVAR_LISP ("last-abbrev", &Vlast_abbrev
,
493 "The abbrev-symbol of the last abbrev expanded. See `abbrev-symbol'.");
495 DEFVAR_LISP ("last-abbrev-text", &Vlast_abbrev_text
,
496 "The exact text of the last abbrev expanded.\n\
497 nil if the abbrev has already been unexpanded.");
499 DEFVAR_INT ("last-abbrev-location", &last_abbrev_point
,
500 "The location of the start of the last abbrev expanded.");
503 Vlast_abbrev_text
= Qnil
;
504 last_abbrev_point
= 0;
506 DEFVAR_LISP ("abbrev-start-location", &Vabbrev_start_location
,
507 "Buffer position for `expand-abbrev' to use as the start of the abbrev.\n\
508 nil means use the word before point as the abbrev.\n\
509 Calling `expand-abbrev' sets this to nil.");
510 Vabbrev_start_location
= Qnil
;
512 DEFVAR_LISP ("abbrev-start-location-buffer", &Vabbrev_start_location_buffer
,
513 "Buffer that `abbrev-start-location' has been set for.\n\
514 Trying to expand an abbrev in any other buffer clears `abbrev-start-location'.");
515 Vabbrev_start_location_buffer
= Qnil
;
517 DEFVAR_PER_BUFFER ("local-abbrev-table", ¤t_buffer
->abbrev_table
,
518 "Local (mode-specific) abbrev table of current buffer.");
520 DEFVAR_BOOL ("abbrevs-changed", &abbrevs_changed
,
521 "Set non-nil by defining or altering any word abbrevs.\n\
522 This causes `save-some-buffers' to offer to save the abbrevs.");
525 DEFVAR_BOOL ("abbrev-all-caps", &abbrev_all_caps
,
526 "*Set non-nil means expand multi-word abbrevs all caps if abbrev was so.");
529 defsubr (&Smake_abbrev_table
);
530 defsubr (&Sclear_abbrev_table
);
531 defsubr (&Sdefine_abbrev
);
532 defsubr (&Sdefine_global_abbrev
);
533 defsubr (&Sdefine_mode_abbrev
);
534 defsubr (&Sabbrev_expansion
);
535 defsubr (&Sabbrev_symbol
);
536 defsubr (&Sexpand_abbrev
);
537 defsubr (&Sunexpand_abbrev
);
538 defsubr (&Sinsert_abbrev_table_description
);
539 defsubr (&Sdefine_abbrev_table
);