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