2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1999
4 @c Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/abbrevs
7 @node Abbrevs, Processes, Syntax Tables, Top
8 @chapter Abbrevs and Abbrev Expansion
12 An abbreviation or @dfn{abbrev} is a string of characters that may be
13 expanded to a longer string. The user can insert the abbrev string and
14 find it replaced automatically with the expansion of the abbrev. This
17 The set of abbrevs currently in effect is recorded in an @dfn{abbrev
18 table}. Each buffer has a local abbrev table, but normally all buffers
19 in the same major mode share one abbrev table. There is also a global
20 abbrev table. Normally both are used.
22 An abbrev table is represented as an obarray containing a symbol for
23 each abbreviation. The symbol's name is the abbreviation; its value is
24 the expansion; its function definition is the hook function to do the
25 expansion (@pxref{Defining Abbrevs}); its property list cell contains
26 the use count, the number of times the abbreviation has been expanded.
27 Because these symbols are not interned in the usual obarray, they will
28 never appear as the result of reading a Lisp expression; in fact,
29 normally they are never used except by the code that handles abbrevs.
30 Therefore, it is safe to use them in an extremely nonstandard way.
31 @xref{Creating Symbols}.
33 For the user-level commands for abbrevs, see @ref{Abbrevs,, Abbrev
34 Mode, emacs, The GNU Emacs Manual}.
37 * Abbrev Mode:: Setting up Emacs for abbreviation.
38 * Tables: Abbrev Tables. Creating and working with abbrev tables.
39 * Defining Abbrevs:: Specifying abbreviations and their expansions.
40 * Files: Abbrev Files. Saving abbrevs in files.
41 * Expansion: Abbrev Expansion. Controlling expansion; expansion subroutines.
42 * Standard Abbrev Tables:: Abbrev tables used by various major modes.
45 @node Abbrev Mode, Abbrev Tables, Abbrevs, Abbrevs
46 @comment node-name, next, previous, up
47 @section Setting Up Abbrev Mode
49 Abbrev mode is a minor mode controlled by the value of the variable
53 A non-@code{nil} value of this variable turns on the automatic expansion
54 of abbrevs when their abbreviations are inserted into a buffer.
55 If the value is @code{nil}, abbrevs may be defined, but they are not
56 expanded automatically.
58 This variable automatically becomes buffer-local when set in any fashion.
61 @defvar default-abbrev-mode
62 This is the value of @code{abbrev-mode} for buffers that do not override it.
63 This is the same as @code{(default-value 'abbrev-mode)}.
66 @node Abbrev Tables, Defining Abbrevs, Abbrev Mode, Abbrevs
67 @section Abbrev Tables
69 This section describes how to create and manipulate abbrev tables.
71 @defun make-abbrev-table
72 This function creates and returns a new, empty abbrev table---an obarray
73 containing no symbols. It is a vector filled with zeros.
76 @defun clear-abbrev-table table
77 This function undefines all the abbrevs in abbrev table @var{table},
78 leaving it empty. It always returns @code{nil}.
81 @defun define-abbrev-table tabname definitions
82 This function defines @var{tabname} (a symbol) as an abbrev table name,
83 i.e., as a variable whose value is an abbrev table. It defines abbrevs
84 in the table according to @var{definitions}, a list of elements of the
85 form @code{(@var{abbrevname} @var{expansion} @var{hook}
86 @var{usecount})}. The return value is always @code{nil}.
89 @defvar abbrev-table-name-list
90 This is a list of symbols whose values are abbrev tables.
91 @code{define-abbrev-table} adds the new abbrev table name to this list.
94 @defun insert-abbrev-table-description name &optional human
95 This function inserts before point a description of the abbrev table
96 named @var{name}. The argument @var{name} is a symbol whose value is an
97 abbrev table. The return value is always @code{nil}.
99 If @var{human} is non-@code{nil}, the description is human-oriented.
100 Otherwise the description is a Lisp expression---a call to
101 @code{define-abbrev-table} that would define @var{name} exactly as it
102 is currently defined.
105 @node Defining Abbrevs, Abbrev Files, Abbrev Tables, Abbrevs
106 @comment node-name, next, previous, up
107 @section Defining Abbrevs
109 These functions define an abbrev in a specified abbrev table.
110 @code{define-abbrev} is the low-level basic function, while
111 @code{add-abbrev} is used by commands that ask for information from the
114 @defun add-abbrev table type arg
115 This function adds an abbreviation to abbrev table @var{table} based on
116 information from the user. The argument @var{type} is a string
117 describing in English the kind of abbrev this will be (typically,
118 @code{"global"} or @code{"mode-specific"}); this is used in prompting
119 the user. The argument @var{arg} is the number of words in the
122 The return value is the symbol that internally represents the new
123 abbrev, or @code{nil} if the user declines to confirm redefining an
127 @defun define-abbrev table name expansion &optional hook count
128 This function defines an abbrev named @var{name}, in @var{table}, to
129 expand to @var{expansion} and call @var{hook}. The value of
130 @var{count}, if specified, initializes the abbrev's usage-count. If
131 @var{count} is not specified or @code{nil}, the use count is initialized
132 to zero. The return value is a symbol that represents the abbrev inside
133 Emacs; its name is @var{name}.
135 The argument @var{name} should be a string. The argument
136 @var{expansion} is normally the desired expansion (a string), or
137 @code{nil} to undefine the abbrev. If it is anything but a string or
138 @code{nil}, then the abbreviation ``expands'' solely by running
141 The argument @var{hook} is a function or @code{nil}. If @var{hook} is
142 non-@code{nil}, then it is called with no arguments after the abbrev is
143 replaced with @var{expansion}; point is located at the end of
144 @var{expansion} when @var{hook} is called.
146 If @var{hook} is a non-nil symbol whose @code{no-self-insert} property
147 is non-@code{nil}, @var{hook} can explicitly control whether to insert
148 the self-inserting input character that triggered the expansion. If
149 @var{hook} returns non-@code{nil} in this case, that inhibits insertion
150 of the character. By contrast, if @var{hook} returns @code{nil},
151 @code{expand-abbrev} also returns @code{nil}, as if expansion had not
155 @defopt only-global-abbrevs
156 If this variable is non-@code{nil}, it means that the user plans to use
157 global abbrevs only. This tells the commands that define mode-specific
158 abbrevs to define global ones instead. This variable does not alter the
159 behavior of the functions in this section; it is examined by their
163 @node Abbrev Files, Abbrev Expansion, Defining Abbrevs, Abbrevs
164 @section Saving Abbrevs in Files
166 A file of saved abbrev definitions is actually a file of Lisp code.
167 The abbrevs are saved in the form of a Lisp program to define the same
168 abbrev tables with the same contents. Therefore, you can load the file
169 with @code{load} (@pxref{How Programs Do Loading}). However, the
170 function @code{quietly-read-abbrev-file} is provided as a more
171 convenient interface.
173 User-level facilities such as @code{save-some-buffers} can save
174 abbrevs in a file automatically, under the control of variables
177 @defopt abbrev-file-name
178 This is the default file name for reading and saving abbrevs.
181 @defun quietly-read-abbrev-file &optional filename
182 This function reads abbrev definitions from a file named @var{filename},
183 previously written with @code{write-abbrev-file}. If @var{filename} is
184 omitted or @code{nil}, the file specified in @code{abbrev-file-name} is
185 used. @code{save-abbrevs} is set to @code{t} so that changes will be
188 This function does not display any messages. It returns @code{nil}.
192 A non-@code{nil} value for @code{save-abbrev} means that Emacs should
193 save abbrevs when files are saved. @code{abbrev-file-name} specifies
194 the file to save the abbrevs in.
197 @defvar abbrevs-changed
198 This variable is set non-@code{nil} by defining or altering any
199 abbrevs. This serves as a flag for various Emacs commands to offer to
203 @deffn Command write-abbrev-file &optional filename
204 Save all abbrev definitions, in all abbrev tables, in the file
205 @var{filename}, in the form of a Lisp program that when loaded will
206 define the same abbrevs. If @var{filename} is @code{nil} or omitted,
207 @code{abbrev-file-name} is used. This function returns @code{nil}.
210 @node Abbrev Expansion, Standard Abbrev Tables, Abbrev Files, Abbrevs
211 @comment node-name, next, previous, up
212 @section Looking Up and Expanding Abbreviations
214 Abbrevs are usually expanded by certain interactive commands,
215 including @code{self-insert-command}. This section describes the
216 subroutines used in writing such commands, as well as the variables they
217 use for communication.
219 @defun abbrev-symbol abbrev &optional table
220 This function returns the symbol representing the abbrev named
221 @var{abbrev}. The value returned is @code{nil} if that abbrev is not
222 defined. The optional second argument @var{table} is the abbrev table
223 to look it up in. If @var{table} is @code{nil}, this function tries
224 first the current buffer's local abbrev table, and second the global
228 @defun abbrev-expansion abbrev &optional table
229 This function returns the string that @var{abbrev} would expand into (as
230 defined by the abbrev tables used for the current buffer). The optional
231 argument @var{table} specifies the abbrev table to use, as in
232 @code{abbrev-symbol}.
235 @deffn Command expand-abbrev
236 This command expands the abbrev before point, if any. If point does not
237 follow an abbrev, this command does nothing. The command returns the
238 abbrev symbol if it did expansion, @code{nil} otherwise.
240 If the abbrev symbol has a hook function which is a symbol whose
241 @code{no-self-insert} property is non-@code{nil}, and if the hook
242 function returns @code{nil} as its value, then @code{expand-abbrev}
243 returns @code{nil} even though expansion did occur.
246 @deffn Command abbrev-prefix-mark &optional arg
247 Mark current point as the beginning of an abbrev. The next call to
248 @code{expand-abbrev} will use the text from here to point (where it is
249 then) as the abbrev to expand, rather than using the previous word as
253 @defopt abbrev-all-caps
254 When this is set non-@code{nil}, an abbrev entered entirely in upper
255 case is expanded using all upper case. Otherwise, an abbrev entered
256 entirely in upper case is expanded by capitalizing each word of the
260 @defvar abbrev-start-location
261 This is the buffer position for @code{expand-abbrev} to use as the start
262 of the next abbrev to be expanded. (@code{nil} means use the word
263 before point instead.) @code{abbrev-start-location} is set to
264 @code{nil} each time @code{expand-abbrev} is called. This variable is
265 also set by @code{abbrev-prefix-mark}.
268 @defvar abbrev-start-location-buffer
269 The value of this variable is the buffer for which
270 @code{abbrev-start-location} has been set. Trying to expand an abbrev
271 in any other buffer clears @code{abbrev-start-location}. This variable
272 is set by @code{abbrev-prefix-mark}.
276 This is the @code{abbrev-symbol} of the most recent abbrev expanded. This
277 information is left by @code{expand-abbrev} for the sake of the
278 @code{unexpand-abbrev} command (@pxref{Expanding Abbrevs,, Expanding
279 Abbrevs, emacs, The GNU Emacs Manual}).
282 @defvar last-abbrev-location
283 This is the location of the most recent abbrev expanded. This contains
284 information left by @code{expand-abbrev} for the sake of the
285 @code{unexpand-abbrev} command.
288 @defvar last-abbrev-text
289 This is the exact expansion text of the most recent abbrev expanded,
290 after case conversion (if any). Its value is @code{nil} if the abbrev
291 has already been unexpanded. This contains information left by
292 @code{expand-abbrev} for the sake of the @code{unexpand-abbrev} command.
296 @defvar pre-abbrev-expand-hook
297 This is a normal hook whose functions are executed, in sequence, just
298 before any expansion of an abbrev. @xref{Hooks}. Since it is a normal
299 hook, the hook functions receive no arguments. However, they can find
300 the abbrev to be expanded by looking in the buffer before point.
301 Running the hook is the first thing that @code{expand-abbrev} does, and
302 so a hook function can be used to change the current abbrev table before
303 abbrev lookup happens.
306 The following sample code shows a simple use of
307 @code{pre-abbrev-expand-hook}. If the user terminates an abbrev with a
308 punctuation character, the hook function asks for confirmation. Thus,
309 this hook allows the user to decide whether to expand the abbrev, and
310 aborts expansion if it is not confirmed.
313 (add-hook 'pre-abbrev-expand-hook 'query-if-not-space)
315 ;; @r{This is the function invoked by @code{pre-abbrev-expand-hook}.}
317 ;; @r{If the user terminated the abbrev with a space, the function does}
318 ;; @r{nothing (that is, it returns so that the abbrev can expand). If the}
319 ;; @r{user entered some other character, this function asks whether}
320 ;; @r{expansion should continue.}
322 ;; @r{If the user answers the prompt with @kbd{y}, the function returns}
323 ;; @r{@code{nil} (because of the @code{not} function), but that is}
324 ;; @r{acceptable; the return value has no effect on expansion.}
326 (defun query-if-not-space ()
327 (if (/= ?\ (preceding-char))
328 (if (not (y-or-n-p "Do you want to expand this abbrev? "))
329 (error "Not expanding this abbrev"))))
332 @node Standard Abbrev Tables, , Abbrev Expansion, Abbrevs
333 @comment node-name, next, previous, up
334 @section Standard Abbrev Tables
336 Here we list the variables that hold the abbrev tables for the
337 preloaded major modes of Emacs.
339 @defvar global-abbrev-table
340 This is the abbrev table for mode-independent abbrevs. The abbrevs
341 defined in it apply to all buffers. Each buffer may also have a local
342 abbrev table, whose abbrev definitions take precedence over those in the
346 @defvar local-abbrev-table
347 The value of this buffer-local variable is the (mode-specific)
348 abbreviation table of the current buffer.
351 @defvar fundamental-mode-abbrev-table
352 This is the local abbrev table used in Fundamental mode; in other words,
353 it is the local abbrev table in all buffers in Fundamental mode.
356 @defvar text-mode-abbrev-table
357 This is the local abbrev table used in Text mode.
360 @defvar lisp-mode-abbrev-table
361 This is the local abbrev table used in Lisp mode and Emacs Lisp mode.