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