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