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