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