2006-10-07 Ralf Angeli <angeli@caeruleus.net>
[bpt/emacs.git] / lispref / abbrevs.texi
CommitLineData
7015aca4
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
651f374c 3@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1999, 2002, 2003, 2004,
ceb4c4d3 4@c 2005, 2006 Free Software Foundation, Inc.
7015aca4
RS
5@c See the file elisp.texi for copying conditions.
6@setfilename ../info/abbrevs
7@node Abbrevs, Processes, Syntax Tables, Top
8241495d 8@chapter Abbrevs and Abbrev Expansion
7015aca4
RS
9@cindex abbrev
10@cindex abbrev table
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
22 An abbrev table is represented as an obarray containing a symbol for
d9cc1d0e
RS
23each abbreviation. The symbol's name is the abbreviation; its value
24is the expansion; its function definition is the hook function to do
25the expansion (@pxref{Defining Abbrevs}); its property list cell
26typically contains the use count, the number of times the abbreviation
6763a405 27has been expanded. Alternatively, the use count is on the
d9cc1d0e 28@code{count} property and the system-abbrev flag is on the
6763a405
LT
29@code{system-type} property. Abbrevs with a non-@code{nil}
30@code{system-type} property are called ``system'' abbrevs. They are
31usually defined by modes or packages, instead of by the user, and are
32treated specially in certain respects.
33
34Because the symbols used for abbrevs are not interned in the usual
35obarray, they will never appear as the result of reading a Lisp
36expression; in fact, normally they are never used except by the code
37that handles abbrevs. Therefore, it is safe to use them in an
d9cc1d0e 38extremely nonstandard way. @xref{Creating Symbols}.
7015aca4
RS
39
40 For the user-level commands for abbrevs, see @ref{Abbrevs,, Abbrev
41Mode, emacs, The GNU Emacs Manual}.
42
43@menu
44* Abbrev Mode:: Setting up Emacs for abbreviation.
45* Tables: Abbrev Tables. Creating and working with abbrev tables.
46* Defining Abbrevs:: Specifying abbreviations and their expansions.
47* Files: Abbrev Files. Saving abbrevs in files.
48* Expansion: Abbrev Expansion. Controlling expansion; expansion subroutines.
49* Standard Abbrev Tables:: Abbrev tables used by various major modes.
50@end menu
51
52@node Abbrev Mode, Abbrev Tables, Abbrevs, Abbrevs
53@comment node-name, next, previous, up
177c0ea7 54@section Setting Up Abbrev Mode
7015aca4
RS
55
56 Abbrev mode is a minor mode controlled by the value of the variable
57@code{abbrev-mode}.
58
59@defvar abbrev-mode
60A non-@code{nil} value of this variable turns on the automatic expansion
61of abbrevs when their abbreviations are inserted into a buffer.
62If the value is @code{nil}, abbrevs may be defined, but they are not
63expanded automatically.
64
969fe9b5 65This variable automatically becomes buffer-local when set in any fashion.
7015aca4
RS
66@end defvar
67
68@defvar default-abbrev-mode
bea169e9 69This is the value of @code{abbrev-mode} for buffers that do not override it.
7015aca4
RS
70This is the same as @code{(default-value 'abbrev-mode)}.
71@end defvar
72
73@node Abbrev Tables, Defining Abbrevs, Abbrev Mode, Abbrevs
74@section Abbrev Tables
75
76 This section describes how to create and manipulate abbrev tables.
77
78@defun make-abbrev-table
79This function creates and returns a new, empty abbrev table---an obarray
80containing no symbols. It is a vector filled with zeros.
81@end defun
82
83@defun clear-abbrev-table table
84This function undefines all the abbrevs in abbrev table @var{table},
1d8c59e9 85leaving it empty. It always returns @code{nil}.
7015aca4
RS
86@end defun
87
57cbdf17
RS
88@defun copy-abbrev-table table
89This function returns a copy of abbrev table @var{table}---a new
6763a405
LT
90abbrev table that contains the same abbrev definitions. The only
91difference between @var{table} and the returned copy is that this
92function sets the property lists of all copied abbrevs to 0.
57cbdf17
RS
93@end defun
94
7015aca4 95@defun define-abbrev-table tabname definitions
d9cc1d0e
RS
96This function defines @var{tabname} (a symbol) as an abbrev table
97name, i.e., as a variable whose value is an abbrev table. It defines
98abbrevs in the table according to @var{definitions}, a list of
99elements of the form @code{(@var{abbrevname} @var{expansion}
6763a405
LT
100@var{hook} @var{usecount} @var{system-flag})}. If an element of
101@var{definitions} has length less than five, omitted elements default
102to @code{nil}. A value of @code{nil} for @var{usecount} is equivalent
103to zero. The return value is always @code{nil}.
104
105If this function is called more than once for the same @var{tabname},
106subsequent calls add the definitions in @var{definitions} to
107@var{tabname}, rather than overriding the entire original contents.
108(A subsequent call only overrides abbrevs explicitly redefined or
109undefined in @var{definitions}.)
7015aca4
RS
110@end defun
111
112@defvar abbrev-table-name-list
113This is a list of symbols whose values are abbrev tables.
114@code{define-abbrev-table} adds the new abbrev table name to this list.
115@end defvar
116
117@defun insert-abbrev-table-description name &optional human
118This function inserts before point a description of the abbrev table
119named @var{name}. The argument @var{name} is a symbol whose value is an
f9f59935 120abbrev table. The return value is always @code{nil}.
7015aca4
RS
121
122If @var{human} is non-@code{nil}, the description is human-oriented.
6763a405
LT
123System abbrevs are listed and identified as such. Otherwise the
124description is a Lisp expression---a call to @code{define-abbrev-table}
125that would define @var{name} as it is currently defined, but without
126the system abbrevs. (The mode or package using @var{name} is supposed
127to add these to @var{name} separately.)
7015aca4
RS
128@end defun
129
130@node Defining Abbrevs, Abbrev Files, Abbrev Tables, Abbrevs
131@comment node-name, next, previous, up
132@section Defining Abbrevs
6763a405
LT
133 @code{define-abbrev} is the low-level basic function for defining an
134abbrev in a specified abbrev table. When major modes predefine
135standard abbrevs, they should call @code{define-abbrev} and specify
136@code{t} for @var{system-flag}.
7015aca4 137
d9cc1d0e 138@defun define-abbrev table name expansion &optional hook count system-flag
f9f59935 139This function defines an abbrev named @var{name}, in @var{table}, to
6763a405 140expand to @var{expansion} and call @var{hook}. The return value is
d9cc1d0e
RS
141@var{name}.
142
143The value of @var{count}, if specified, initializes the abbrev's
144usage-count. If @var{count} is not specified or @code{nil}, the use
145count is initialized to zero.
7015aca4
RS
146
147The argument @var{name} should be a string. The argument
f9f59935
RS
148@var{expansion} is normally the desired expansion (a string), or
149@code{nil} to undefine the abbrev. If it is anything but a string or
150@code{nil}, then the abbreviation ``expands'' solely by running
151@var{hook}.
7015aca4
RS
152
153The argument @var{hook} is a function or @code{nil}. If @var{hook} is
154non-@code{nil}, then it is called with no arguments after the abbrev is
155replaced with @var{expansion}; point is located at the end of
bea169e9 156@var{expansion} when @var{hook} is called.
02b14400 157
259c7ed4 158@cindex @code{no-self-insert} property
15bc2f77
RS
159If @var{hook} is a non-@code{nil} symbol whose @code{no-self-insert}
160property is non-@code{nil}, @var{hook} can explicitly control whether
161to insert the self-inserting input character that triggered the
162expansion. If @var{hook} returns non-@code{nil} in this case, that
163inhibits insertion of the character. By contrast, if @var{hook}
164returns @code{nil}, @code{expand-abbrev} also returns @code{nil}, as
165if expansion had not really occurred.
d9cc1d0e 166
6d96d6eb
RS
167If @var{system-flag} is non-@code{nil}, that marks the abbrev as a
168``system'' abbrev with the @code{system-type} property.
169
d9cc1d0e 170Normally the function @code{define-abbrev} sets the variable
6d96d6eb
RS
171@code{abbrevs-changed} to @code{t}, if it actually changes the abbrev.
172(This is so that some commands will offer to save the abbrevs.) It
173does not do this for a ``system'' abbrev, since those won't be saved
174anyway.
7015aca4
RS
175@end defun
176
177@defopt only-global-abbrevs
178If this variable is non-@code{nil}, it means that the user plans to use
179global abbrevs only. This tells the commands that define mode-specific
180abbrevs to define global ones instead. This variable does not alter the
bea169e9 181behavior of the functions in this section; it is examined by their
7015aca4
RS
182callers.
183@end defopt
184
185@node Abbrev Files, Abbrev Expansion, Defining Abbrevs, Abbrevs
186@section Saving Abbrevs in Files
187
188 A file of saved abbrev definitions is actually a file of Lisp code.
189The abbrevs are saved in the form of a Lisp program to define the same
190abbrev tables with the same contents. Therefore, you can load the file
191with @code{load} (@pxref{How Programs Do Loading}). However, the
192function @code{quietly-read-abbrev-file} is provided as a more
193convenient interface.
194
195 User-level facilities such as @code{save-some-buffers} can save
196abbrevs in a file automatically, under the control of variables
197described here.
198
199@defopt abbrev-file-name
200This is the default file name for reading and saving abbrevs.
201@end defopt
202
7f785b50 203@defun quietly-read-abbrev-file &optional filename
7015aca4
RS
204This function reads abbrev definitions from a file named @var{filename},
205previously written with @code{write-abbrev-file}. If @var{filename} is
7f785b50
GM
206omitted or @code{nil}, the file specified in @code{abbrev-file-name} is
207used. @code{save-abbrevs} is set to @code{t} so that changes will be
208saved.
7015aca4
RS
209
210This function does not display any messages. It returns @code{nil}.
211@end defun
212
213@defopt save-abbrevs
6763a405
LT
214A non-@code{nil} value for @code{save-abbrevs} means that Emacs should
215offer the user to save abbrevs when files are saved. If the value is
216@code{silently}, Emacs saves the abbrevs without asking the user.
217@code{abbrev-file-name} specifies the file to save the abbrevs in.
7015aca4
RS
218@end defopt
219
220@defvar abbrevs-changed
d9cc1d0e
RS
221This variable is set non-@code{nil} by defining or altering any
222abbrevs (except ``system'' abbrevs). This serves as a flag for
223various Emacs commands to offer to save your abbrevs.
7015aca4
RS
224@end defvar
225
7f785b50 226@deffn Command write-abbrev-file &optional filename
6763a405
LT
227Save all abbrev definitions (except ``system'' abbrevs), for all abbrev
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}.
7015aca4
RS
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
f9f59935 238 Abbrevs are usually expanded by certain interactive commands,
7015aca4 239including @code{self-insert-command}. This section describes the
f9f59935
RS
240subroutines used in writing such commands, as well as the variables they
241use for communication.
7015aca4
RS
242
243@defun abbrev-symbol abbrev &optional table
244This function returns the symbol representing the abbrev named
245@var{abbrev}. The value returned is @code{nil} if that abbrev is not
246defined. The optional second argument @var{table} is the abbrev table
247to look it up in. If @var{table} is @code{nil}, this function tries
248first the current buffer's local abbrev table, and second the global
249abbrev table.
250@end defun
251
bea169e9
RS
252@defun abbrev-expansion abbrev &optional table
253This function returns the string that @var{abbrev} would expand into (as
6763a405
LT
254defined by the abbrev tables used for the current buffer). If
255@var{abbrev} is not a valid abbrev, the function returns @code{nil}.
256The optional argument @var{table} specifies the abbrev table to use,
257as in @code{abbrev-symbol}.
bea169e9
RS
258@end defun
259
260@deffn Command expand-abbrev
7f785b50
GM
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.
02b14400
RS
264
265If the abbrev symbol has a hook function which is a symbol whose
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.
bea169e9
RS
269@end deffn
270
271@deffn Command abbrev-prefix-mark &optional arg
a7cc2e20
RS
272This command marks the current location of point as the beginning of
273an abbrev. The next call to @code{expand-abbrev} will use the text
274from here to point (where it is then) as the abbrev to expand, rather
275than using the previous word as usual.
6763a405
LT
276
277First, this command expands any abbrev before point, unless @var{arg}
278is non-@code{nil}. (Interactively, @var{arg} is the prefix argument.)
279Then it inserts a hyphen before point, to indicate the start of the
280next abbrev to be expanded. The actual expansion removes the hyphen.
bea169e9
RS
281@end deffn
282
7015aca4
RS
283@defopt abbrev-all-caps
284When this is set non-@code{nil}, an abbrev entered entirely in upper
285case is expanded using all upper case. Otherwise, an abbrev entered
286entirely in upper case is expanded by capitalizing each word of the
287expansion.
288@end defopt
289
7015aca4 290@defvar abbrev-start-location
73b74087 291The value of this variable is a buffer position (an integer or a marker)
83797c58
LT
292for @code{expand-abbrev} to use as the start of the next abbrev to be
293expanded. The value can also be @code{nil}, which means to use the
294word before point instead. @code{abbrev-start-location} is set to
295@code{nil} each time @code{expand-abbrev} is called. This variable is
296also set by @code{abbrev-prefix-mark}.
7015aca4
RS
297@end defvar
298
299@defvar abbrev-start-location-buffer
300The value of this variable is the buffer for which
301@code{abbrev-start-location} has been set. Trying to expand an abbrev
302in any other buffer clears @code{abbrev-start-location}. This variable
303is set by @code{abbrev-prefix-mark}.
304@end defvar
305
306@defvar last-abbrev
f9f59935 307This is the @code{abbrev-symbol} of the most recent abbrev expanded. This
7015aca4 308information is left by @code{expand-abbrev} for the sake of the
f9f59935
RS
309@code{unexpand-abbrev} command (@pxref{Expanding Abbrevs,, Expanding
310Abbrevs, emacs, The GNU Emacs Manual}).
7015aca4
RS
311@end defvar
312
313@defvar last-abbrev-location
f9f59935 314This is the location of the most recent abbrev expanded. This contains
7015aca4
RS
315information left by @code{expand-abbrev} for the sake of the
316@code{unexpand-abbrev} command.
317@end defvar
318
319@defvar last-abbrev-text
f9f59935
RS
320This is the exact expansion text of the most recent abbrev expanded,
321after case conversion (if any). Its value is @code{nil} if the abbrev
322has already been unexpanded. This contains information left by
323@code{expand-abbrev} for the sake of the @code{unexpand-abbrev} command.
7015aca4
RS
324@end defvar
325
326@c Emacs 19 feature
327@defvar pre-abbrev-expand-hook
328This is a normal hook whose functions are executed, in sequence, just
329before any expansion of an abbrev. @xref{Hooks}. Since it is a normal
330hook, the hook functions receive no arguments. However, they can find
331the abbrev to be expanded by looking in the buffer before point.
7f785b50
GM
332Running the hook is the first thing that @code{expand-abbrev} does, and
333so a hook function can be used to change the current abbrev table before
73b74087
LT
334abbrev lookup happens. (Although you have to do this carefully. See
335the example below.)
7015aca4
RS
336@end defvar
337
338 The following sample code shows a simple use of
73b74087
LT
339@code{pre-abbrev-expand-hook}. It assumes that @code{foo-mode} is a
340mode for editing certain files in which lines that start with @samp{#}
341are comments. You want to use Text mode abbrevs for those lines. The
342regular local abbrev table, @code{foo-mode-abbrev-table} is
343appropriate for all other lines. Then you can put the following code
344in your @file{.emacs} file. @xref{Standard Abbrev Tables}, for the
345definitions of @code{local-abbrev-table} and @code{text-mode-abbrev-table}.
7015aca4
RS
346
347@smallexample
73b74087
LT
348(defun foo-mode-pre-abbrev-expand ()
349 (when (save-excursion (forward-line 0) (eq (char-after) ?#))
350 (let ((local-abbrev-table text-mode-abbrev-table)
351 ;; Avoid infinite loop.
352 (pre-abbrev-expand-hook nil))
353 (expand-abbrev))
354 ;; We have already called `expand-abbrev' in this hook.
355 ;; Hence we want the "actual" call following this hook to be a no-op.
356 (setq abbrev-start-location (point-max)
357 abbrev-start-location-buffer (current-buffer))))
358
359(add-hook 'foo-mode-hook
360 #'(lambda ()
361 (add-hook 'pre-abbrev-expand-hook
362 'foo-mode-pre-abbrev-expand
363 nil t)))
7015aca4
RS
364@end smallexample
365
a7cc2e20 366Note that @code{foo-mode-pre-abbrev-expand} just returns @code{nil}
73b74087
LT
367without doing anything for lines not starting with @samp{#}. Hence
368abbrevs expand normally using @code{foo-mode-abbrev-table} as local
369abbrev table for such lines.
370
7015aca4
RS
371@node Standard Abbrev Tables, , Abbrev Expansion, Abbrevs
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)
387abbreviation table of the current buffer.
388@end defvar
389
390@defvar fundamental-mode-abbrev-table
bea169e9
RS
391This is the local abbrev table used in Fundamental mode; in other words,
392it is the local abbrev table in all buffers in Fundamental mode.
7015aca4
RS
393@end defvar
394
395@defvar text-mode-abbrev-table
396This is the local abbrev table used in Text mode.
397@end defvar
398
7015aca4
RS
399@defvar lisp-mode-abbrev-table
400This is the local abbrev table used in Lisp mode and Emacs Lisp mode.
401@end defvar
7f785b50 402
ab5796a9
MB
403@ignore
404 arch-tag: 5ffdbe08-2cd4-48ec-a5a8-080f95756eec
405@end ignore