]> code.delx.au - gnu-emacs/blob - doc/lispref/abbrevs.texi
Add 2012 to FSF copyright years for Emacs files
[gnu-emacs] / doc / lispref / abbrevs.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1994, 1999, 2001-2012 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../../info/abbrevs
6 @node Abbrevs, Processes, Syntax Tables, Top
7 @chapter Abbrevs and Abbrev Expansion
8 @cindex abbrev
9 @c @cindex abbrev table Redundant with "abbrev".
10
11 An abbreviation or @dfn{abbrev} is a string of characters that may be
12 expanded to a longer string. The user can insert the abbrev string and
13 find it replaced automatically with the expansion of the abbrev. This
14 saves typing.
15
16 The set of abbrevs currently in effect is recorded in an @dfn{abbrev
17 table}. Each buffer has a local abbrev table, but normally all buffers
18 in the same major mode share one abbrev table. There is also a global
19 abbrev table. Normally both are used.
20
21 An abbrev table is represented as an obarray. @xref{Creating
22 Symbols}, for information about obarrays. Each abbreviation is
23 represented by a symbol in the obarray. The symbol's name is the
24 abbreviation; its value is the expansion; its function definition is
25 the hook function for performing the expansion (@pxref{Defining
26 Abbrevs}); and its property list cell contains various additional
27 properties, including the use count and the number of times the
28 abbreviation has been expanded (@pxref{Abbrev Properties}).
29
30 @cindex system abbrev
31 Certain abbrevs, called @dfn{system abbrevs}, are defined by a major
32 mode instead of the user. A system abbrev is identified by its
33 non-@code{nil} @code{:system} property (@pxref{Abbrev Properties}).
34 When abbrevs are saved to an abbrev file, system abbrevs are omitted.
35 @xref{Abbrev Files}.
36
37 Because the symbols used for abbrevs are not interned in the usual
38 obarray, they will never appear as the result of reading a Lisp
39 expression; in fact, normally they are never used except by the code
40 that handles abbrevs. Therefore, it is safe to use them in an
41 extremely nonstandard way.
42
43 For the user-level commands for abbrevs, see @ref{Abbrevs,, Abbrev
44 Mode, emacs, The GNU Emacs Manual}.
45
46 @menu
47 * Abbrev Mode:: Setting up Emacs for abbreviation.
48 * Tables: Abbrev Tables. Creating and working with abbrev tables.
49 * Defining Abbrevs:: Specifying abbreviations and their expansions.
50 * Files: Abbrev Files. Saving abbrevs in files.
51 * Expansion: Abbrev Expansion. Controlling expansion; expansion subroutines.
52 * Standard Abbrev Tables:: Abbrev tables used by various major modes.
53 * Abbrev Properties:: How to read and set abbrev properties.
54 Which properties have which effect.
55 * Abbrev Table Properties:: How to read and set abbrev table properties.
56 Which properties have which effect.
57 @end menu
58
59 @node Abbrev Mode, Abbrev Tables, Abbrevs, Abbrevs
60 @comment node-name, next, previous, up
61 @section Setting Up Abbrev Mode
62
63 Abbrev mode is a minor mode controlled by the variable
64 @code{abbrev-mode}.
65
66 @defopt abbrev-mode
67 If this variable is non-@code{nil}, abbrevs are automatically expanded
68 in the buffer. If the value is @code{nil}, abbrevs may be defined,
69 but they are not expanded automatically.
70
71 This variable automatically becomes buffer-local when set in any fashion.
72 @end defopt
73
74 @node Abbrev Tables, Defining Abbrevs, Abbrev Mode, Abbrevs
75 @section Abbrev Tables
76
77 This section describes how to create and manipulate abbrev tables.
78
79 @defun make-abbrev-table &optional props
80 This function creates and returns a new, empty abbrev table---an
81 obarray containing no symbols. It is a vector filled with zeros.
82 @var{props} is a property list that is applied to the new table
83 (@pxref{Abbrev Table Properties}).
84 @end defun
85
86 @defun abbrev-table-p object
87 This function returns a non-@code{nil} value if @var{object} is an
88 abbrev table.
89 @end defun
90
91 @defun clear-abbrev-table abbrev-table
92 This function undefines all the abbrevs in @var{abbrev-table}, leaving
93 it empty. It always returns @code{nil}.
94 @end defun
95
96 @defun copy-abbrev-table abbrev-table
97 This function returns a copy of @var{abbrev-table}---a new abbrev
98 table containing the same abbrev definitions. There is one difference
99 between the contents of @var{abbrev-table} and the returned copy: all
100 abbrevs in the latter have their property lists set to @code{nil}.
101 @end defun
102
103 @defun define-abbrev-table tabname definitions &optional docstring &rest props
104 This function defines @var{tabname} (a symbol) as an abbrev table
105 name, i.e., as a variable whose value is an abbrev table. It defines
106 abbrevs in the table according to @var{definitions}, a list of
107 elements of the form @code{(@var{abbrevname} @var{expansion}
108 [@var{hook}] [@var{props}...])}. These elements are passed as
109 arguments to @code{define-abbrev}. The return value is always
110 @code{nil}.
111
112 The optional string @var{docstring} is the documentation string of the
113 variable @var{tabname}. The property list @var{props} is applied to
114 the abbrev table (@pxref{Abbrev Table Properties}).
115
116 If this function is called more than once for the same @var{tabname},
117 subsequent calls add the definitions in @var{definitions} to
118 @var{tabname}, rather than overriding the entire original contents.
119 (A subsequent call only overrides abbrevs explicitly redefined or
120 undefined in @var{definitions}.)
121 @end defun
122
123 @defvar abbrev-table-name-list
124 This is a list of symbols whose values are abbrev tables.
125 @code{define-abbrev-table} adds the new abbrev table name to this list.
126 @end defvar
127
128 @defun insert-abbrev-table-description name &optional human
129 This function inserts before point a description of the abbrev table
130 named @var{name}. The argument @var{name} is a symbol whose value is an
131 abbrev table. The return value is always @code{nil}.
132
133 If @var{human} is non-@code{nil}, the description is human-oriented.
134 System abbrevs are listed and identified as such. Otherwise the
135 description is a Lisp expression---a call to @code{define-abbrev-table}
136 that would define @var{name} as it is currently defined, but without
137 the system abbrevs. (The mode or package using @var{name} is supposed
138 to add these to @var{name} separately.)
139 @end defun
140
141 @node Defining Abbrevs, Abbrev Files, Abbrev Tables, Abbrevs
142 @comment node-name, next, previous, up
143 @section Defining Abbrevs
144
145 @code{define-abbrev} is the low-level basic function for defining an
146 abbrev in an abbrev table.
147
148 When a major mode defines a system abbrev, it should call
149 @code{define-abbrev} and specify a @code{t} for the @code{:system}
150 property. Be aware that any saved non-``system'' abbrevs are restored
151 at startup, i.e. before some major modes are loaded. Therefore, major
152 modes should not assume that their abbrev tables are empty when they
153 are first loaded.
154
155 @defun define-abbrev abbrev-table name expansion &optional hook &rest props
156 This function defines an abbrev named @var{name}, in
157 @var{abbrev-table}, to expand to @var{expansion} and call @var{hook},
158 with properties @var{props} (@pxref{Abbrev Properties}). The return
159 value is @var{name}. The @code{:system} property in @var{props} is
160 treated specially here: if it has the value @code{force}, then it will
161 overwrite an existing definition even for a non-``system'' abbrev of
162 the same name.
163
164 @var{name} should be a string. The argument @var{expansion} is
165 normally the desired expansion (a string), or @code{nil} to undefine
166 the abbrev. If it is anything but a string or @code{nil}, then the
167 abbreviation ``expands'' solely by running @var{hook}.
168
169 The argument @var{hook} is a function or @code{nil}. If @var{hook} is
170 non-@code{nil}, then it is called with no arguments after the abbrev is
171 replaced with @var{expansion}; point is located at the end of
172 @var{expansion} when @var{hook} is called.
173
174 @cindex @code{no-self-insert} property
175 If @var{hook} is a non-@code{nil} symbol whose @code{no-self-insert}
176 property is non-@code{nil}, @var{hook} can explicitly control whether
177 to insert the self-inserting input character that triggered the
178 expansion. If @var{hook} returns non-@code{nil} in this case, that
179 inhibits insertion of the character. By contrast, if @var{hook}
180 returns @code{nil}, @code{expand-abbrev} also returns @code{nil}, as
181 if expansion had not really occurred.
182
183 Normally, @code{define-abbrev} sets the variable
184 @code{abbrevs-changed} to @code{t}, if it actually changes the abbrev.
185 (This is so that some commands will offer to save the abbrevs.) It
186 does not do this for a system abbrev, since those aren't saved anyway.
187 @end defun
188
189 @defopt only-global-abbrevs
190 If this variable is non-@code{nil}, it means that the user plans to use
191 global abbrevs only. This tells the commands that define mode-specific
192 abbrevs to define global ones instead. This variable does not alter the
193 behavior of the functions in this section; it is examined by their
194 callers.
195 @end defopt
196
197 @node Abbrev Files, Abbrev Expansion, Defining Abbrevs, Abbrevs
198 @section Saving Abbrevs in Files
199
200 A file of saved abbrev definitions is actually a file of Lisp code.
201 The abbrevs are saved in the form of a Lisp program to define the same
202 abbrev tables with the same contents. Therefore, you can load the file
203 with @code{load} (@pxref{How Programs Do Loading}). However, the
204 function @code{quietly-read-abbrev-file} is provided as a more
205 convenient interface.
206
207 User-level facilities such as @code{save-some-buffers} can save
208 abbrevs in a file automatically, under the control of variables
209 described here.
210
211 @defopt abbrev-file-name
212 This is the default file name for reading and saving abbrevs.
213 @end defopt
214
215 @defun quietly-read-abbrev-file &optional filename
216 This function reads abbrev definitions from a file named @var{filename},
217 previously written with @code{write-abbrev-file}. If @var{filename} is
218 omitted or @code{nil}, the file specified in @code{abbrev-file-name} is
219 used. @code{save-abbrevs} is set to @code{t} so that changes will be
220 saved.
221
222 This function does not display any messages. It returns @code{nil}.
223 @end defun
224
225 @defopt save-abbrevs
226 A non-@code{nil} value for @code{save-abbrevs} means that Emacs should
227 offer the user to save abbrevs when files are saved. If the value is
228 @code{silently}, Emacs saves the abbrevs without asking the user.
229 @code{abbrev-file-name} specifies the file to save the abbrevs in.
230 @end defopt
231
232 @defvar abbrevs-changed
233 This variable is set non-@code{nil} by defining or altering any
234 abbrevs (except system abbrevs). This serves as a flag for various
235 Emacs commands to offer to save your abbrevs.
236 @end defvar
237
238 @deffn Command write-abbrev-file &optional filename
239 Save all abbrev definitions (except system abbrevs), for all abbrev
240 tables listed in @code{abbrev-table-name-list}, in the file
241 @var{filename}, in the form of a Lisp program that when loaded will
242 define the same abbrevs. If @var{filename} is @code{nil} or omitted,
243 @code{abbrev-file-name} is used. This function returns @code{nil}.
244 @end deffn
245
246 @node Abbrev Expansion, Standard Abbrev Tables, Abbrev Files, Abbrevs
247 @comment node-name, next, previous, up
248 @section Looking Up and Expanding Abbreviations
249
250 Abbrevs are usually expanded by certain interactive commands,
251 including @code{self-insert-command}. This section describes the
252 subroutines used in writing such commands, as well as the variables they
253 use for communication.
254
255 @defun abbrev-symbol abbrev &optional table
256 This function returns the symbol representing the abbrev named
257 @var{abbrev}. The value returned is @code{nil} if that abbrev is not
258 defined. The optional second argument @var{table} is the abbrev table
259 in which to look it up. If @var{table} is @code{nil}, this function
260 tries first the current buffer's local abbrev table, and second the
261 global abbrev table.
262 @end defun
263
264 @defun abbrev-expansion abbrev &optional table
265 This function returns the string that @var{abbrev} would expand into (as
266 defined by the abbrev tables used for the current buffer). If
267 @var{abbrev} is not a valid abbrev, the function returns @code{nil}.
268 The optional argument @var{table} specifies the abbrev table to use,
269 as in @code{abbrev-symbol}.
270 @end defun
271
272 @deffn Command expand-abbrev
273 This command expands the abbrev before point, if any. If point does not
274 follow an abbrev, this command does nothing. The command returns the
275 abbrev symbol if it did expansion, @code{nil} otherwise.
276
277 If the abbrev symbol has a hook function which is a symbol whose
278 @code{no-self-insert} property is non-@code{nil}, and if the hook
279 function returns @code{nil} as its value, then @code{expand-abbrev}
280 returns @code{nil} even though expansion did occur.
281 @end deffn
282
283 @defun abbrev-insert abbrev &optional name start end
284 This function inserts the abbrev expansion of @code{abbrev}, replacing
285 the text between @code{start} and @code{end}. If @code{start} is
286 omitted, it defaults to point. @code{name}, if non-@code{nil}, should
287 be the name by which this abbrev was found (a string); it is used to
288 figure out whether to adjust the capitalization of the expansion. The
289 function returns @code{abbrev} if the abbrev was successfully
290 inserted.
291 @end defun
292
293 @deffn Command abbrev-prefix-mark &optional arg
294 This command marks the current location of point as the beginning of
295 an abbrev. The next call to @code{expand-abbrev} will use the text
296 from here to point (where it is then) as the abbrev to expand, rather
297 than using the previous word as usual.
298
299 First, this command expands any abbrev before point, unless @var{arg}
300 is non-@code{nil}. (Interactively, @var{arg} is the prefix argument.)
301 Then it inserts a hyphen before point, to indicate the start of the
302 next abbrev to be expanded. The actual expansion removes the hyphen.
303 @end deffn
304
305 @defopt abbrev-all-caps
306 When this is set non-@code{nil}, an abbrev entered entirely in upper
307 case is expanded using all upper case. Otherwise, an abbrev entered
308 entirely in upper case is expanded by capitalizing each word of the
309 expansion.
310 @end defopt
311
312 @defvar abbrev-start-location
313 The value of this variable is a buffer position (an integer or a marker)
314 for @code{expand-abbrev} to use as the start of the next abbrev to be
315 expanded. The value can also be @code{nil}, which means to use the
316 word before point instead. @code{abbrev-start-location} is set to
317 @code{nil} each time @code{expand-abbrev} is called. This variable is
318 also set by @code{abbrev-prefix-mark}.
319 @end defvar
320
321 @defvar abbrev-start-location-buffer
322 The value of this variable is the buffer for which
323 @code{abbrev-start-location} has been set. Trying to expand an abbrev
324 in any other buffer clears @code{abbrev-start-location}. This variable
325 is set by @code{abbrev-prefix-mark}.
326 @end defvar
327
328 @defvar last-abbrev
329 This is the @code{abbrev-symbol} of the most recent abbrev expanded. This
330 information is left by @code{expand-abbrev} for the sake of the
331 @code{unexpand-abbrev} command (@pxref{Expanding Abbrevs,, Expanding
332 Abbrevs, emacs, The GNU Emacs Manual}).
333 @end defvar
334
335 @defvar last-abbrev-location
336 This is the location of the most recent abbrev expanded. This contains
337 information left by @code{expand-abbrev} for the sake of the
338 @code{unexpand-abbrev} command.
339 @end defvar
340
341 @defvar last-abbrev-text
342 This is the exact expansion text of the most recent abbrev expanded,
343 after case conversion (if any). Its value is @code{nil} if the abbrev
344 has already been unexpanded. This contains information left by
345 @code{expand-abbrev} for the sake of the @code{unexpand-abbrev} command.
346 @end defvar
347
348 @defvar abbrev-expand-functions
349 This is a special hook run @emph{around} the @code{expand-abbrev}
350 function. Each function on this hook is called with a single
351 argument: a function that performs the normal abbrev expansion. The
352 hook function can hence do anything it wants before and after
353 performing the expansion. It can also choose not to call its
354 argument, thus overriding the default behavior; or it may even call it
355 several times. The function should return the abbrev symbol if
356 expansion took place.
357 @end defvar
358
359 The following sample code shows a simple use of
360 @code{abbrev-expand-functions}. It assumes that @code{foo-mode} is a
361 mode for editing certain files in which lines that start with @samp{#}
362 are comments. You want to use Text mode abbrevs for those lines. The
363 regular local abbrev table, @code{foo-mode-abbrev-table} is
364 appropriate for all other lines. Then you can put the following code
365 in your @file{.emacs} file. @xref{Standard Abbrev Tables}, for the
366 definitions of @code{local-abbrev-table} and @code{text-mode-abbrev-table}.
367
368 @smallexample
369 (defun foo-mode-abbrev-expand-function (expand)
370 (if (not (save-excursion (forward-line 0) (eq (char-after) ?#)))
371 ;; Performs normal expansion.
372 (funcall expand)
373 ;; We're inside a comment: use the text-mode abbrevs.
374 (let ((local-abbrev-table text-mode-abbrev-table))
375 (funcall expand))))
376
377 (add-hook 'foo-mode-hook
378 #'(lambda ()
379 (add-hook 'abbrev-expand-functions
380 'foo-mode-abbrev-expand-function
381 nil t)))
382 @end smallexample
383
384 @node Standard Abbrev Tables, Abbrev Properties, Abbrev Expansion, Abbrevs
385 @comment node-name, next, previous, up
386 @section Standard Abbrev Tables
387
388 Here we list the variables that hold the abbrev tables for the
389 preloaded major modes of Emacs.
390
391 @defvar global-abbrev-table
392 This is the abbrev table for mode-independent abbrevs. The abbrevs
393 defined in it apply to all buffers. Each buffer may also have a local
394 abbrev table, whose abbrev definitions take precedence over those in the
395 global table.
396 @end defvar
397
398 @defvar local-abbrev-table
399 The value of this buffer-local variable is the (mode-specific)
400 abbreviation table of the current buffer. It can also be a list of
401 such tables.
402 @end defvar
403
404 @defvar abbrev-minor-mode-table-alist
405 The value of this variable is a list of elements of the form
406 @code{(@var{mode} . @var{abbrev-table})} where @var{mode} is the name
407 of a variable: if the variable is bound to a non-@code{nil} value,
408 then the @var{abbrev-table} is active, otherwise it is ignored.
409 @var{abbrev-table} can also be a list of abbrev tables.
410 @end defvar
411
412 @defvar fundamental-mode-abbrev-table
413 This is the local abbrev table used in Fundamental mode; in other words,
414 it is the local abbrev table in all buffers in Fundamental mode.
415 @end defvar
416
417 @defvar text-mode-abbrev-table
418 This is the local abbrev table used in Text mode.
419 @end defvar
420
421 @defvar lisp-mode-abbrev-table
422 This is the local abbrev table used in Lisp mode and Emacs Lisp mode.
423 @end defvar
424
425 @node Abbrev Properties, Abbrev Table Properties, Standard Abbrev Tables, Abbrevs
426 @section Abbrev Properties
427
428 Abbrevs have properties, some of which influence the way they work.
429 You can provide them as arguments to @code{define-abbrev} and you can
430 manipulate them with the following functions:
431
432 @defun abbrev-put abbrev prop val
433 Set the property @var{prop} of @var{abbrev} to value @var{val}.
434 @end defun
435
436 @defun abbrev-get abbrev prop
437 Return the property @var{prop} of @var{abbrev}, or @code{nil} if the
438 abbrev has no such property.
439 @end defun
440
441 The following properties have special meanings:
442
443 @table @code
444 @item :count
445 This property counts the number of times the abbrev has
446 been expanded. If not explicitly set, it is initialized to 0 by
447 @code{define-abbrev}.
448
449 @item :system
450 If non-@code{nil}, this property marks the abbrev as a system abbrev.
451 Such abbrevs are not saved (@pxref{Abbrev Files}).
452
453 @item :enable-function
454 If non-@code{nil}, this property should be a function of no
455 arguments which returns @code{nil} if the abbrev should not be used
456 and @code{t} otherwise.
457
458 @item :case-fixed
459 If non-@code{nil}, this property indicates that the case of the
460 abbrev's name is significant and should only match a text with the
461 same pattern of capitalization. It also disables the code that
462 modifies the capitalization of the expansion.
463 @end table
464
465 @node Abbrev Table Properties, , Abbrev Properties, Abbrevs
466 @section Abbrev Table Properties
467
468 Like abbrevs, abbrev tables have properties, some of which influence
469 the way they work. You can provide them as arguments to
470 @code{define-abbrev-table} and you can manipulate them with the
471 functions:
472
473 @defun abbrev-table-put table prop val
474 Set the property @var{prop} of abbrev table @var{table} to value @var{val}.
475 @end defun
476
477 @defun abbrev-table-get table prop
478 Return the property @var{prop} of abbrev table @var{table}, or @code{nil}
479 if the abbrev has no such property.
480 @end defun
481
482 The following properties have special meaning:
483
484 @table @code
485 @item :enable-function
486 This is like the @code{:enable-function} abbrev property except that
487 it applies to all abbrevs in the table and is used even before trying
488 to find the abbrev before point so it can dynamically modify the
489 abbrev table.
490
491 @item :case-fixed
492 This is like the @code{:case-fixed} abbrev property except that it
493 applies to all abbrevs in the table.
494
495 @item :regexp
496 If non-@code{nil}, this property is a regular expression that
497 indicates how to extract the name of the abbrev before point before
498 looking it up in the table. When the regular expression matches
499 before point, the abbrev name is expected to be in submatch 1.
500 If this property is @code{nil}, @code{expand-function} defaults to
501 @code{"\\<\\(\\w+\\)\\W"}. This property allows the use of abbrevs
502 whose name contains characters of non-word syntax.
503
504 @item :parents
505 This property holds the list of tables from which to inherit
506 other abbrevs.
507
508 @item :abbrev-table-modiff
509 This property holds a counter incremented each time a new abbrev is
510 added to the table.
511
512 @end table