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