*** empty log message ***
[bpt/emacs.git] / lispref / abbrevs.texi
CommitLineData
7015aca4
RS
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4@c See the file elisp.texi for copying conditions.
5@setfilename ../info/abbrevs
6@node Abbrevs, Processes, Syntax Tables, Top
8241495d 7@chapter Abbrevs and Abbrev Expansion
7015aca4
RS
8@cindex abbrev
9@cindex abbrev table
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
21 An abbrev table is represented as an obarray containing a symbol for
bea169e9 22each abbreviation. The symbol's name is the abbreviation; its value is
7015aca4 23the expansion; its function definition is the hook function to do the
bea169e9
RS
24expansion (@pxref{Defining Abbrevs}); its property list cell contains
25the use count, the number of times the abbreviation has been expanded.
26Because these symbols are not interned in the usual obarray, they will
27never appear as the result of reading a Lisp expression; in fact,
28normally they are never used except by the code that handles abbrevs.
29Therefore, it is safe to use them in an extremely nonstandard way.
30@xref{Creating Symbols}.
7015aca4
RS
31
32 For the user-level commands for abbrevs, see @ref{Abbrevs,, Abbrev
33Mode, emacs, The GNU Emacs Manual}.
34
35@menu
36* Abbrev Mode:: Setting up Emacs for abbreviation.
37* Tables: Abbrev Tables. Creating and working with abbrev tables.
38* Defining Abbrevs:: Specifying abbreviations and their expansions.
39* Files: Abbrev Files. Saving abbrevs in files.
40* Expansion: Abbrev Expansion. Controlling expansion; expansion subroutines.
41* Standard Abbrev Tables:: Abbrev tables used by various major modes.
42@end menu
43
44@node Abbrev Mode, Abbrev Tables, Abbrevs, Abbrevs
45@comment node-name, next, previous, up
46@section Setting Up Abbrev Mode
47
48 Abbrev mode is a minor mode controlled by the value of the variable
49@code{abbrev-mode}.
50
51@defvar abbrev-mode
52A non-@code{nil} value of this variable turns on the automatic expansion
53of abbrevs when their abbreviations are inserted into a buffer.
54If the value is @code{nil}, abbrevs may be defined, but they are not
55expanded automatically.
56
969fe9b5 57This variable automatically becomes buffer-local when set in any fashion.
7015aca4
RS
58@end defvar
59
60@defvar default-abbrev-mode
bea169e9 61This is the value of @code{abbrev-mode} for buffers that do not override it.
7015aca4
RS
62This is the same as @code{(default-value 'abbrev-mode)}.
63@end defvar
64
65@node Abbrev Tables, Defining Abbrevs, Abbrev Mode, Abbrevs
66@section Abbrev Tables
67
68 This section describes how to create and manipulate abbrev tables.
69
70@defun make-abbrev-table
71This function creates and returns a new, empty abbrev table---an obarray
72containing no symbols. It is a vector filled with zeros.
73@end defun
74
75@defun clear-abbrev-table table
76This function undefines all the abbrevs in abbrev table @var{table},
77leaving it empty. The function returns @code{nil}.
78@end defun
79
80@defun define-abbrev-table tabname definitions
81This function defines @var{tabname} (a symbol) as an abbrev table name,
82i.e., as a variable whose value is an abbrev table. It defines abbrevs
83in the table according to @var{definitions}, a list of elements of the
84form @code{(@var{abbrevname} @var{expansion} @var{hook}
f9f59935 85@var{usecount})}. The return value is always @code{nil}.
7015aca4
RS
86@end defun
87
88@defvar abbrev-table-name-list
89This is a list of symbols whose values are abbrev tables.
90@code{define-abbrev-table} adds the new abbrev table name to this list.
91@end defvar
92
93@defun insert-abbrev-table-description name &optional human
94This function inserts before point a description of the abbrev table
95named @var{name}. The argument @var{name} is a symbol whose value is an
f9f59935 96abbrev table. The return value is always @code{nil}.
7015aca4
RS
97
98If @var{human} is non-@code{nil}, the description is human-oriented.
99Otherwise the description is a Lisp expression---a call to
bea169e9 100@code{define-abbrev-table} that would define @var{name} exactly as it
7015aca4
RS
101is currently defined.
102@end defun
103
104@node Defining Abbrevs, Abbrev Files, Abbrev Tables, Abbrevs
105@comment node-name, next, previous, up
106@section Defining Abbrevs
107
108 These functions define an abbrev in a specified abbrev table.
109@code{define-abbrev} is the low-level basic function, while
110@code{add-abbrev} is used by commands that ask for information from the
111user.
112
113@defun add-abbrev table type arg
bea169e9
RS
114This function adds an abbreviation to abbrev table @var{table} based on
115information from the user. The argument @var{type} is a string
116describing in English the kind of abbrev this will be (typically,
117@code{"global"} or @code{"mode-specific"}); this is used in prompting
118the user. The argument @var{arg} is the number of words in the
119expansion.
7015aca4 120
bea169e9 121The return value is the symbol that internally represents the new
7015aca4
RS
122abbrev, or @code{nil} if the user declines to confirm redefining an
123existing abbrev.
124@end defun
125
126@defun define-abbrev table name expansion hook
f9f59935
RS
127This function defines an abbrev named @var{name}, in @var{table}, to
128expand to @var{expansion} and call @var{hook}. The return value is a
129symbol that represents the abbrev inside Emacs; its name is @var{name}.
7015aca4
RS
130
131The argument @var{name} should be a string. The argument
f9f59935
RS
132@var{expansion} is normally the desired expansion (a string), or
133@code{nil} to undefine the abbrev. If it is anything but a string or
134@code{nil}, then the abbreviation ``expands'' solely by running
135@var{hook}.
7015aca4
RS
136
137The argument @var{hook} is a function or @code{nil}. If @var{hook} is
138non-@code{nil}, then it is called with no arguments after the abbrev is
139replaced with @var{expansion}; point is located at the end of
bea169e9 140@var{expansion} when @var{hook} is called.
7015aca4
RS
141
142The use count of the abbrev is initialized to zero.
143@end defun
144
145@defopt only-global-abbrevs
146If this variable is non-@code{nil}, it means that the user plans to use
147global abbrevs only. This tells the commands that define mode-specific
148abbrevs to define global ones instead. This variable does not alter the
bea169e9 149behavior of the functions in this section; it is examined by their
7015aca4
RS
150callers.
151@end defopt
152
153@node Abbrev Files, Abbrev Expansion, Defining Abbrevs, Abbrevs
154@section Saving Abbrevs in Files
155
156 A file of saved abbrev definitions is actually a file of Lisp code.
157The abbrevs are saved in the form of a Lisp program to define the same
158abbrev tables with the same contents. Therefore, you can load the file
159with @code{load} (@pxref{How Programs Do Loading}). However, the
160function @code{quietly-read-abbrev-file} is provided as a more
161convenient interface.
162
163 User-level facilities such as @code{save-some-buffers} can save
164abbrevs in a file automatically, under the control of variables
165described here.
166
167@defopt abbrev-file-name
168This is the default file name for reading and saving abbrevs.
169@end defopt
170
171@defun quietly-read-abbrev-file filename
172This function reads abbrev definitions from a file named @var{filename},
173previously written with @code{write-abbrev-file}. If @var{filename} is
174@code{nil}, the file specified in @code{abbrev-file-name} is used.
175@code{save-abbrevs} is set to @code{t} so that changes will be saved.
176
177This function does not display any messages. It returns @code{nil}.
178@end defun
179
180@defopt save-abbrevs
181A non-@code{nil} value for @code{save-abbrev} means that Emacs should
182save abbrevs when files are saved. @code{abbrev-file-name} specifies
183the file to save the abbrevs in.
184@end defopt
185
186@defvar abbrevs-changed
187This variable is set non-@code{nil} by defining or altering any
188abbrevs. This serves as a flag for various Emacs commands to offer to
189save your abbrevs.
190@end defvar
191
192@deffn Command write-abbrev-file filename
193Save all abbrev definitions, in all abbrev tables, in the file
bea169e9 194@var{filename}, in the form of a Lisp program that when loaded will
7015aca4
RS
195define the same abbrevs. This function returns @code{nil}.
196@end deffn
197
198@node Abbrev Expansion, Standard Abbrev Tables, Abbrev Files, Abbrevs
199@comment node-name, next, previous, up
200@section Looking Up and Expanding Abbreviations
201
f9f59935 202 Abbrevs are usually expanded by certain interactive commands,
7015aca4 203including @code{self-insert-command}. This section describes the
f9f59935
RS
204subroutines used in writing such commands, as well as the variables they
205use for communication.
7015aca4
RS
206
207@defun abbrev-symbol abbrev &optional table
208This function returns the symbol representing the abbrev named
209@var{abbrev}. The value returned is @code{nil} if that abbrev is not
210defined. The optional second argument @var{table} is the abbrev table
211to look it up in. If @var{table} is @code{nil}, this function tries
212first the current buffer's local abbrev table, and second the global
213abbrev table.
214@end defun
215
bea169e9
RS
216@defun abbrev-expansion abbrev &optional table
217This function returns the string that @var{abbrev} would expand into (as
218defined by the abbrev tables used for the current buffer). The optional
219argument @var{table} specifies the abbrev table to use, as in
220@code{abbrev-symbol}.
221@end defun
222
223@deffn Command expand-abbrev
224This command expands the abbrev before point, if any.
225If point does not follow an abbrev, this command does nothing.
226The command returns @code{t} if it did expansion, @code{nil} otherwise.
227@end deffn
228
229@deffn Command abbrev-prefix-mark &optional arg
230Mark current point as the beginning of an abbrev. The next call to
231@code{expand-abbrev} will use the text from here to point (where it is
232then) as the abbrev to expand, rather than using the previous word as
233usual.
234@end deffn
235
7015aca4
RS
236@defopt abbrev-all-caps
237When this is set non-@code{nil}, an abbrev entered entirely in upper
238case is expanded using all upper case. Otherwise, an abbrev entered
239entirely in upper case is expanded by capitalizing each word of the
240expansion.
241@end defopt
242
7015aca4
RS
243@defvar abbrev-start-location
244This is the buffer position for @code{expand-abbrev} to use as the start
245of the next abbrev to be expanded. (@code{nil} means use the word
246before point instead.) @code{abbrev-start-location} is set to
247@code{nil} each time @code{expand-abbrev} is called. This variable is
248also set by @code{abbrev-prefix-mark}.
249@end defvar
250
251@defvar abbrev-start-location-buffer
252The value of this variable is the buffer for which
253@code{abbrev-start-location} has been set. Trying to expand an abbrev
254in any other buffer clears @code{abbrev-start-location}. This variable
255is set by @code{abbrev-prefix-mark}.
256@end defvar
257
258@defvar last-abbrev
f9f59935 259This is the @code{abbrev-symbol} of the most recent abbrev expanded. This
7015aca4 260information is left by @code{expand-abbrev} for the sake of the
f9f59935
RS
261@code{unexpand-abbrev} command (@pxref{Expanding Abbrevs,, Expanding
262Abbrevs, emacs, The GNU Emacs Manual}).
7015aca4
RS
263@end defvar
264
265@defvar last-abbrev-location
f9f59935 266This is the location of the most recent abbrev expanded. This contains
7015aca4
RS
267information left by @code{expand-abbrev} for the sake of the
268@code{unexpand-abbrev} command.
269@end defvar
270
271@defvar last-abbrev-text
f9f59935
RS
272This is the exact expansion text of the most recent abbrev expanded,
273after case conversion (if any). Its value is @code{nil} if the abbrev
274has already been unexpanded. This contains information left by
275@code{expand-abbrev} for the sake of the @code{unexpand-abbrev} command.
7015aca4
RS
276@end defvar
277
278@c Emacs 19 feature
279@defvar pre-abbrev-expand-hook
280This is a normal hook whose functions are executed, in sequence, just
281before any expansion of an abbrev. @xref{Hooks}. Since it is a normal
282hook, the hook functions receive no arguments. However, they can find
283the abbrev to be expanded by looking in the buffer before point.
284@end defvar
285
286 The following sample code shows a simple use of
287@code{pre-abbrev-expand-hook}. If the user terminates an abbrev with a
288punctuation character, the hook function asks for confirmation. Thus,
289this hook allows the user to decide whether to expand the abbrev, and
290aborts expansion if it is not confirmed.
291
292@smallexample
293(add-hook 'pre-abbrev-expand-hook 'query-if-not-space)
294
295;; @r{This is the function invoked by @code{pre-abbrev-expand-hook}.}
296
297;; @r{If the user terminated the abbrev with a space, the function does}
298;; @r{nothing (that is, it returns so that the abbrev can expand). If the}
299;; @r{user entered some other character, this function asks whether}
300;; @r{expansion should continue.}
301
bea169e9 302;; @r{If the user answers the prompt with @kbd{y}, the function returns}
7015aca4
RS
303;; @r{@code{nil} (because of the @code{not} function), but that is}
304;; @r{acceptable; the return value has no effect on expansion.}
305
306(defun query-if-not-space ()
307 (if (/= ?\ (preceding-char))
308 (if (not (y-or-n-p "Do you want to expand this abbrev? "))
309 (error "Not expanding this abbrev"))))
310@end smallexample
311
312@node Standard Abbrev Tables, , Abbrev Expansion, Abbrevs
313@comment node-name, next, previous, up
314@section Standard Abbrev Tables
315
316 Here we list the variables that hold the abbrev tables for the
317preloaded major modes of Emacs.
318
319@defvar global-abbrev-table
320This is the abbrev table for mode-independent abbrevs. The abbrevs
321defined in it apply to all buffers. Each buffer may also have a local
322abbrev table, whose abbrev definitions take precedence over those in the
323global table.
324@end defvar
325
326@defvar local-abbrev-table
327The value of this buffer-local variable is the (mode-specific)
328abbreviation table of the current buffer.
329@end defvar
330
331@defvar fundamental-mode-abbrev-table
bea169e9
RS
332This is the local abbrev table used in Fundamental mode; in other words,
333it is the local abbrev table in all buffers in Fundamental mode.
7015aca4
RS
334@end defvar
335
336@defvar text-mode-abbrev-table
337This is the local abbrev table used in Text mode.
338@end defvar
339
7015aca4
RS
340@defvar lisp-mode-abbrev-table
341This is the local abbrev table used in Lisp mode and Emacs Lisp mode.
342@end defvar