.
[emacs.git] / src / abbrev.c
blobe3ee9e039b2256a1ea607f07d65b92bd946cdb5f
1 /* Primitives for word-abbrev mode.
2 Copyright (C) 1985, 1986, 1993, 1996, 1998 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 2, or (at your option)
9 any later version.
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <stdio.h>
24 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "window.h"
28 #include "charset.h"
29 #include "syntax.h"
31 /* An abbrev table is an obarray.
32 Each defined abbrev is represented by a symbol in that obarray
33 whose print name is the abbreviation.
34 The symbol's value is a string which is the expansion.
35 If its function definition is non-nil, it is called
36 after the expansion is done.
37 The plist slot of the abbrev symbol is its usage count. */
39 /* List of all abbrev-table name symbols:
40 symbols whose values are abbrev tables. */
42 Lisp_Object Vabbrev_table_name_list;
44 /* The table of global abbrevs. These are in effect
45 in any buffer in which abbrev mode is turned on. */
47 Lisp_Object Vglobal_abbrev_table;
49 /* The local abbrev table used by default (in Fundamental Mode buffers) */
51 Lisp_Object Vfundamental_mode_abbrev_table;
53 /* Set nonzero when an abbrev definition is changed */
55 int abbrevs_changed;
57 int abbrev_all_caps;
59 /* Non-nil => use this location as the start of abbrev to expand
60 (rather than taking the word before point as the abbrev) */
62 Lisp_Object Vabbrev_start_location;
64 /* Buffer that Vabbrev_start_location applies to */
65 Lisp_Object Vabbrev_start_location_buffer;
67 /* The symbol representing the abbrev most recently expanded */
69 Lisp_Object Vlast_abbrev;
71 /* A string for the actual text of the abbrev most recently expanded.
72 This has more info than Vlast_abbrev since case is significant. */
74 Lisp_Object Vlast_abbrev_text;
76 /* Character address of start of last abbrev expanded */
78 int last_abbrev_point;
80 /* Hook to run before expanding any abbrev. */
82 Lisp_Object Vpre_abbrev_expand_hook, Qpre_abbrev_expand_hook;
84 DEFUN ("make-abbrev-table", Fmake_abbrev_table, Smake_abbrev_table, 0, 0, 0,
85 "Create a new, empty abbrev table object.")
88 return Fmake_vector (make_number (59), make_number (0));
91 DEFUN ("clear-abbrev-table", Fclear_abbrev_table, Sclear_abbrev_table, 1, 1, 0,
92 "Undefine all abbrevs in abbrev table TABLE, leaving it empty.")
93 (table)
94 Lisp_Object table;
96 int i, size;
98 CHECK_VECTOR (table, 0);
99 size = XVECTOR (table)->size;
100 abbrevs_changed = 1;
101 for (i = 0; i < size; i++)
102 XVECTOR (table)->contents[i] = make_number (0);
103 return Qnil;
106 DEFUN ("define-abbrev", Fdefine_abbrev, Sdefine_abbrev, 3, 5, 0,
107 "Define an abbrev in TABLE named NAME, to expand to EXPANSION and call HOOK.\n\
108 NAME must be a string.\n\
109 EXPANSION should usually be a string.\n\
110 To undefine an abbrev, define it with EXPANSION = nil.\n\
111 If HOOK is non-nil, it should be a function of no arguments;\n\
112 it is called after EXPANSION is inserted.\n\
113 If EXPANSION is not a string, the abbrev is a special one,\n\
114 which does not expand in the usual way but only runs HOOK.\n\
115 COUNT, if specified, initializes the abbrev's usage-count\n\
116 which is incremented each time the abbrev is used.")
117 (table, name, expansion, hook, count)
118 Lisp_Object table, name, expansion, hook, count;
120 Lisp_Object sym, oexp, ohook, tem;
121 CHECK_VECTOR (table, 0);
122 CHECK_STRING (name, 1);
124 if (NILP (count))
125 count = make_number (0);
126 else
127 CHECK_NUMBER (count, 0);
129 sym = Fintern (name, table);
131 oexp = XSYMBOL (sym)->value;
132 ohook = XSYMBOL (sym)->function;
133 if (!((EQ (oexp, expansion)
134 || (STRINGP (oexp) && STRINGP (expansion)
135 && (tem = Fstring_equal (oexp, expansion), !NILP (tem))))
137 (EQ (ohook, hook)
138 || (tem = Fequal (ohook, hook), !NILP (tem)))))
139 abbrevs_changed = 1;
141 Fset (sym, expansion);
142 Ffset (sym, hook);
143 Fsetplist (sym, count);
145 return name;
148 DEFUN ("define-global-abbrev", Fdefine_global_abbrev, Sdefine_global_abbrev, 2, 2,
149 "sDefine global abbrev: \nsExpansion for %s: ",
150 "Define ABBREV as a global abbreviation for EXPANSION.")
151 (abbrev, expansion)
152 Lisp_Object abbrev, expansion;
154 Fdefine_abbrev (Vglobal_abbrev_table, Fdowncase (abbrev),
155 expansion, Qnil, make_number (0));
156 return abbrev;
159 DEFUN ("define-mode-abbrev", Fdefine_mode_abbrev, Sdefine_mode_abbrev, 2, 2,
160 "sDefine mode abbrev: \nsExpansion for %s: ",
161 "Define ABBREV as a mode-specific abbreviation for EXPANSION.")
162 (abbrev, expansion)
163 Lisp_Object abbrev, expansion;
165 if (NILP (current_buffer->abbrev_table))
166 error ("Major mode has no abbrev table");
168 Fdefine_abbrev (current_buffer->abbrev_table, Fdowncase (abbrev),
169 expansion, Qnil, make_number (0));
170 return abbrev;
173 DEFUN ("abbrev-symbol", Fabbrev_symbol, Sabbrev_symbol, 1, 2, 0,
174 "Return the symbol representing abbrev named ABBREV.\n\
175 This symbol's name is ABBREV, but it is not the canonical symbol of that name;\n\
176 it is interned in an abbrev-table rather than the normal obarray.\n\
177 The value is nil if that abbrev is not defined.\n\
178 Optional second arg TABLE is abbrev table to look it up in.\n\
179 The default is to try buffer's mode-specific abbrev table, then global table.")
180 (abbrev, table)
181 Lisp_Object abbrev, table;
183 Lisp_Object sym;
184 CHECK_STRING (abbrev, 0);
185 if (!NILP (table))
186 sym = Fintern_soft (abbrev, table);
187 else
189 sym = Qnil;
190 if (!NILP (current_buffer->abbrev_table))
191 sym = Fintern_soft (abbrev, current_buffer->abbrev_table);
192 if (NILP (XSYMBOL (sym)->value))
193 sym = Qnil;
194 if (NILP (sym))
195 sym = Fintern_soft (abbrev, Vglobal_abbrev_table);
197 if (NILP (XSYMBOL (sym)->value)) return Qnil;
198 return sym;
201 DEFUN ("abbrev-expansion", Fabbrev_expansion, Sabbrev_expansion, 1, 2, 0,
202 "Return the string that ABBREV expands into in the current buffer.\n\
203 Optionally specify an abbrev table as second arg;\n\
204 then ABBREV is looked up in that table only.")
205 (abbrev, table)
206 Lisp_Object abbrev, table;
208 Lisp_Object sym;
209 sym = Fabbrev_symbol (abbrev, table);
210 if (NILP (sym)) return sym;
211 return Fsymbol_value (sym);
214 /* Expand the word before point, if it is an abbrev.
215 Returns 1 if an expansion is done. */
217 DEFUN ("expand-abbrev", Fexpand_abbrev, Sexpand_abbrev, 0, 0, "",
218 "Expand the abbrev before point, if there is an abbrev there.\n\
219 Effective when explicitly called even when `abbrev-mode' is nil.\n\
220 Returns the abbrev symbol, if expansion took place.")
223 register char *buffer, *p;
224 int wordstart, wordend;
225 register int wordstart_byte, wordend_byte, idx;
226 int whitecnt;
227 int uccount = 0, lccount = 0;
228 register Lisp_Object sym;
229 Lisp_Object expansion, hook, tem;
230 Lisp_Object value;
232 value = Qnil;
234 if (!NILP (Vrun_hooks))
235 call1 (Vrun_hooks, Qpre_abbrev_expand_hook);
237 wordstart = 0;
238 if (!(BUFFERP (Vabbrev_start_location_buffer)
239 && XBUFFER (Vabbrev_start_location_buffer) == current_buffer))
240 Vabbrev_start_location = Qnil;
241 if (!NILP (Vabbrev_start_location))
243 tem = Vabbrev_start_location;
244 CHECK_NUMBER_COERCE_MARKER (tem, 0);
245 wordstart = XINT (tem);
246 Vabbrev_start_location = Qnil;
247 if (wordstart < BEGV || wordstart > ZV)
248 wordstart = 0;
249 if (wordstart && wordstart != ZV)
251 wordstart_byte = CHAR_TO_BYTE (wordstart);
252 if (FETCH_BYTE (wordstart_byte) == '-')
253 del_range (wordstart, wordstart + 1);
256 if (!wordstart)
257 wordstart = scan_words (PT, -1);
259 if (!wordstart)
260 return value;
262 wordstart_byte = CHAR_TO_BYTE (wordstart);
263 wordend = scan_words (wordstart, 1);
264 if (!wordend)
265 return value;
267 if (wordend > PT)
268 wordend = PT;
270 wordend_byte = CHAR_TO_BYTE (wordend);
271 whitecnt = PT - wordend;
272 if (wordend <= wordstart)
273 return value;
275 p = buffer = (char *) alloca (wordend_byte - wordstart_byte);
277 for (idx = wordstart_byte; idx < wordend_byte; idx++)
279 /* ??? This loop needs to go by characters! */
280 register int c = FETCH_BYTE (idx);
281 if (UPPERCASEP (c))
282 c = DOWNCASE (c), uccount++;
283 else if (! NOCASEP (c))
284 lccount++;
285 *p++ = c;
288 if (VECTORP (current_buffer->abbrev_table))
289 sym = oblookup (current_buffer->abbrev_table, buffer,
290 wordend - wordstart, wordend_byte - wordstart_byte);
291 else
292 XSETFASTINT (sym, 0);
293 if (INTEGERP (sym) || NILP (XSYMBOL (sym)->value))
294 sym = oblookup (Vglobal_abbrev_table, buffer,
295 wordend - wordstart, wordend_byte - wordstart_byte);
296 if (INTEGERP (sym) || NILP (XSYMBOL (sym)->value))
297 return value;
299 if (INTERACTIVE && !EQ (minibuf_window, selected_window))
301 /* Add an undo boundary, in case we are doing this for
302 a self-inserting command which has avoided making one so far. */
303 SET_PT (wordend);
304 Fundo_boundary ();
307 Vlast_abbrev_text
308 = Fbuffer_substring (make_number (wordstart), make_number (wordend));
310 /* Now sym is the abbrev symbol. */
311 Vlast_abbrev = sym;
312 value = sym;
313 last_abbrev_point = wordstart;
315 if (INTEGERP (XSYMBOL (sym)->plist))
316 XSETINT (XSYMBOL (sym)->plist,
317 XINT (XSYMBOL (sym)->plist) + 1); /* Increment use count */
319 /* If this abbrev has an expansion, delete the abbrev
320 and insert the expansion. */
321 expansion = XSYMBOL (sym)->value;
322 if (STRINGP (expansion))
324 SET_PT (wordstart);
326 del_range_both (wordstart, wordstart_byte, wordend, wordend_byte, 1);
328 insert_from_string (expansion, 0, 0, XSTRING (expansion)->size,
329 STRING_BYTES (XSTRING (expansion)), 1);
330 SET_PT (PT + whitecnt);
332 if (uccount && !lccount)
334 /* Abbrev was all caps */
335 /* If expansion is multiple words, normally capitalize each word */
336 /* This used to be if (!... && ... >= ...) Fcapitalize; else Fupcase
337 but Megatest 68000 compiler can't handle that */
338 if (!abbrev_all_caps)
339 if (scan_words (PT, -1) > scan_words (wordstart, 1))
341 Fupcase_initials_region (make_number (wordstart),
342 make_number (PT));
343 goto caped;
345 /* If expansion is one word, or if user says so, upcase it all. */
346 Fupcase_region (make_number (wordstart), make_number (PT));
347 caped: ;
349 else if (uccount)
351 /* Abbrev included some caps. Cap first initial of expansion */
352 int pos = wordstart_byte;
354 /* Find the initial. */
355 while (pos < PT_BYTE
356 && SYNTAX (*BUF_BYTE_ADDRESS (current_buffer, pos)) != Sword)
357 pos++;
359 /* Change just that. */
360 pos = BYTE_TO_CHAR (pos);
361 Fupcase_initials_region (make_number (pos), make_number (pos + 1));
365 hook = XSYMBOL (sym)->function;
366 if (!NILP (hook))
367 call0 (hook);
369 return value;
372 DEFUN ("unexpand-abbrev", Funexpand_abbrev, Sunexpand_abbrev, 0, 0, "",
373 "Undo the expansion of the last abbrev that expanded.\n\
374 This differs from ordinary undo in that other editing done since then\n\
375 is not undone.")
378 int opoint = PT;
379 int adjust = 0;
380 if (last_abbrev_point < BEGV
381 || last_abbrev_point > ZV)
382 return Qnil;
383 SET_PT (last_abbrev_point);
384 if (STRINGP (Vlast_abbrev_text))
386 /* This isn't correct if Vlast_abbrev->function was used
387 to do the expansion */
388 Lisp_Object val;
389 int zv_before;
391 val = XSYMBOL (Vlast_abbrev)->value;
392 if (!STRINGP (val))
393 error ("value of abbrev-symbol must be a string");
394 zv_before = ZV;
395 del_range_byte (PT_BYTE, PT_BYTE + STRING_BYTES (XSTRING (val)), 1);
396 /* Don't inherit properties here; just copy from old contents. */
397 insert_from_string (Vlast_abbrev_text, 0, 0,
398 XSTRING (Vlast_abbrev_text)->size,
399 STRING_BYTES (XSTRING (Vlast_abbrev_text)), 0);
400 Vlast_abbrev_text = Qnil;
401 /* Total number of characters deleted. */
402 adjust = ZV - zv_before;
404 SET_PT (last_abbrev_point < opoint ? opoint + adjust : opoint);
405 return Qnil;
408 static void
409 write_abbrev (sym, stream)
410 Lisp_Object sym, stream;
412 Lisp_Object name;
413 if (NILP (XSYMBOL (sym)->value))
414 return;
415 insert (" (", 5);
416 XSETSTRING (name, XSYMBOL (sym)->name);
417 Fprin1 (name, stream);
418 insert (" ", 1);
419 Fprin1 (XSYMBOL (sym)->value, stream);
420 insert (" ", 1);
421 Fprin1 (XSYMBOL (sym)->function, stream);
422 insert (" ", 1);
423 Fprin1 (XSYMBOL (sym)->plist, stream);
424 insert (")\n", 2);
427 static void
428 describe_abbrev (sym, stream)
429 Lisp_Object sym, stream;
431 Lisp_Object one;
433 if (NILP (XSYMBOL (sym)->value))
434 return;
435 one = make_number (1);
436 Fprin1 (Fsymbol_name (sym), stream);
437 Findent_to (make_number (15), one);
438 Fprin1 (XSYMBOL (sym)->plist, stream);
439 Findent_to (make_number (20), one);
440 Fprin1 (XSYMBOL (sym)->value, stream);
441 if (!NILP (XSYMBOL (sym)->function))
443 Findent_to (make_number (45), one);
444 Fprin1 (XSYMBOL (sym)->function, stream);
446 Fterpri (stream);
449 DEFUN ("insert-abbrev-table-description", Finsert_abbrev_table_description,
450 Sinsert_abbrev_table_description, 1, 2, 0,
451 "Insert before point a full description of abbrev table named NAME.\n\
452 NAME is a symbol whose value is an abbrev table.\n\
453 If optional 2nd arg READABLE is non-nil, a human-readable description\n\
454 is inserted. Otherwise the description is an expression,\n\
455 a call to `define-abbrev-table', which would\n\
456 define the abbrev table NAME exactly as it is currently defined.")
457 (name, readable)
458 Lisp_Object name, readable;
460 Lisp_Object table;
461 Lisp_Object stream;
463 CHECK_SYMBOL (name, 0);
464 table = Fsymbol_value (name);
465 CHECK_VECTOR (table, 0);
467 XSETBUFFER (stream, current_buffer);
469 if (!NILP (readable))
471 insert_string ("(");
472 Fprin1 (name, stream);
473 insert_string (")\n\n");
474 map_obarray (table, describe_abbrev, stream);
475 insert_string ("\n\n");
477 else
479 insert_string ("(define-abbrev-table '");
480 Fprin1 (name, stream);
481 insert_string (" '(\n");
482 map_obarray (table, write_abbrev, stream);
483 insert_string (" ))\n\n");
486 return Qnil;
489 DEFUN ("define-abbrev-table", Fdefine_abbrev_table, Sdefine_abbrev_table,
490 2, 2, 0,
491 "Define TABLENAME (a symbol) as an abbrev table name.\n\
492 Define abbrevs in it according to DEFINITIONS, which is a list of elements\n\
493 of the form (ABBREVNAME EXPANSION HOOK USECOUNT).")
494 (tablename, definitions)
495 Lisp_Object tablename, definitions;
497 Lisp_Object name, exp, hook, count;
498 Lisp_Object table, elt;
500 CHECK_SYMBOL (tablename, 0);
501 table = Fboundp (tablename);
502 if (NILP (table) || (table = Fsymbol_value (tablename), NILP (table)))
504 table = Fmake_abbrev_table ();
505 Fset (tablename, table);
506 Vabbrev_table_name_list = Fcons (tablename, Vabbrev_table_name_list);
508 CHECK_VECTOR (table, 0);
510 for (; !NILP (definitions); definitions = Fcdr (definitions))
512 elt = Fcar (definitions);
513 name = Fcar (elt); elt = Fcdr (elt);
514 exp = Fcar (elt); elt = Fcdr (elt);
515 hook = Fcar (elt); elt = Fcdr (elt);
516 count = Fcar (elt);
517 Fdefine_abbrev (table, name, exp, hook, count);
519 return Qnil;
522 void
523 syms_of_abbrev ()
525 DEFVAR_LISP ("abbrev-table-name-list", &Vabbrev_table_name_list,
526 "List of symbols whose values are abbrev tables.");
527 Vabbrev_table_name_list = Fcons (intern ("fundamental-mode-abbrev-table"),
528 Fcons (intern ("global-abbrev-table"),
529 Qnil));
531 DEFVAR_LISP ("global-abbrev-table", &Vglobal_abbrev_table,
532 "The abbrev table whose abbrevs affect all buffers.\n\
533 Each buffer may also have a local abbrev table.\n\
534 If it does, the local table overrides the global one\n\
535 for any particular abbrev defined in both.");
536 Vglobal_abbrev_table = Fmake_abbrev_table ();
538 DEFVAR_LISP ("fundamental-mode-abbrev-table", &Vfundamental_mode_abbrev_table,
539 "The abbrev table of mode-specific abbrevs for Fundamental Mode.");
540 Vfundamental_mode_abbrev_table = Fmake_abbrev_table ();
541 current_buffer->abbrev_table = Vfundamental_mode_abbrev_table;
543 DEFVAR_LISP ("last-abbrev", &Vlast_abbrev,
544 "The abbrev-symbol of the last abbrev expanded. See `abbrev-symbol'.");
546 DEFVAR_LISP ("last-abbrev-text", &Vlast_abbrev_text,
547 "The exact text of the last abbrev expanded.\n\
548 nil if the abbrev has already been unexpanded.");
550 DEFVAR_INT ("last-abbrev-location", &last_abbrev_point,
551 "The location of the start of the last abbrev expanded.");
553 Vlast_abbrev = Qnil;
554 Vlast_abbrev_text = Qnil;
555 last_abbrev_point = 0;
557 DEFVAR_LISP ("abbrev-start-location", &Vabbrev_start_location,
558 "Buffer position for `expand-abbrev' to use as the start of the abbrev.\n\
559 nil means use the word before point as the abbrev.\n\
560 Calling `expand-abbrev' sets this to nil.");
561 Vabbrev_start_location = Qnil;
563 DEFVAR_LISP ("abbrev-start-location-buffer", &Vabbrev_start_location_buffer,
564 "Buffer that `abbrev-start-location' has been set for.\n\
565 Trying to expand an abbrev in any other buffer clears `abbrev-start-location'.");
566 Vabbrev_start_location_buffer = Qnil;
568 DEFVAR_PER_BUFFER ("local-abbrev-table", &current_buffer->abbrev_table, Qnil,
569 "Local (mode-specific) abbrev table of current buffer.");
571 DEFVAR_BOOL ("abbrevs-changed", &abbrevs_changed,
572 "Set non-nil by defining or altering any word abbrevs.\n\
573 This causes `save-some-buffers' to offer to save the abbrevs.");
574 abbrevs_changed = 0;
576 DEFVAR_BOOL ("abbrev-all-caps", &abbrev_all_caps,
577 "*Set non-nil means expand multi-word abbrevs all caps if abbrev was so.");
578 abbrev_all_caps = 0;
580 DEFVAR_LISP ("pre-abbrev-expand-hook", &Vpre_abbrev_expand_hook,
581 "Function or functions to be called before abbrev expansion is done.\n\
582 This is the first thing that `expand-abbrev' does, and so this may change\n\
583 the current abbrev table before abbrev lookup happens.");
584 Vpre_abbrev_expand_hook = Qnil;
585 Qpre_abbrev_expand_hook = intern ("pre-abbrev-expand-hook");
586 staticpro (&Qpre_abbrev_expand_hook);
588 defsubr (&Smake_abbrev_table);
589 defsubr (&Sclear_abbrev_table);
590 defsubr (&Sdefine_abbrev);
591 defsubr (&Sdefine_global_abbrev);
592 defsubr (&Sdefine_mode_abbrev);
593 defsubr (&Sabbrev_expansion);
594 defsubr (&Sabbrev_symbol);
595 defsubr (&Sexpand_abbrev);
596 defsubr (&Sunexpand_abbrev);
597 defsubr (&Sinsert_abbrev_table_description);
598 defsubr (&Sdefine_abbrev_table);