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