Don't pass un-encoded file name to mkstemp.
[bpt/emacs.git] / doc / lispref / abbrevs.texi
CommitLineData
b8d4c8d0
GM
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
ea16568d 3@c Copyright (C) 1990-1994, 1999, 2001-2012 Free Software Foundation, Inc.
b8d4c8d0 4@c See the file elisp.texi for copying conditions.
ecc6530d 5@node Abbrevs
b8d4c8d0
GM
6@chapter Abbrevs and Abbrev Expansion
7@cindex abbrev
8@c @cindex abbrev table Redundant with "abbrev".
9
10 An abbreviation or @dfn{abbrev} is a string of characters that may be
11expanded to a longer string. The user can insert the abbrev string and
12find it replaced automatically with the expansion of the abbrev. This
13saves typing.
14
15 The set of abbrevs currently in effect is recorded in an @dfn{abbrev
16table}. Each buffer has a local abbrev table, but normally all buffers
17in the same major mode share one abbrev table. There is also a global
18abbrev table. Normally both are used.
19
925672cd
CY
20 An abbrev table is represented as an obarray. @xref{Creating
21Symbols}, for information about obarrays. Each abbreviation is
22represented by a symbol in the obarray. The symbol's name is the
23abbreviation; its value is the expansion; its function definition is
24the hook function for performing the expansion (@pxref{Defining
25Abbrevs}); and its property list cell contains various additional
26properties, including the use count and the number of times the
27abbreviation has been expanded (@pxref{Abbrev Properties}).
28
29@cindex system abbrev
30 Certain abbrevs, called @dfn{system abbrevs}, are defined by a major
31mode instead of the user. A system abbrev is identified by its
32non-@code{nil} @code{:system} property (@pxref{Abbrev Properties}).
33When abbrevs are saved to an abbrev file, system abbrevs are omitted.
34@xref{Abbrev Files}.
35
36 Because the symbols used for abbrevs are not interned in the usual
b8d4c8d0
GM
37obarray, they will never appear as the result of reading a Lisp
38expression; in fact, normally they are never used except by the code
ea16568d
GM
39that handles abbrevs. Therefore, it is safe to use them in a
40nonstandard way.
b8d4c8d0 41
ea16568d
GM
42 If the minor mode Abbrev mode is enabled, the buffer-local variable
43@code{abbrev-mode} is non-@code{nil}, and abbrevs are automatically
44expanded in the buffer. For the user-level commands for abbrevs, see
45@ref{Abbrevs,, Abbrev Mode, emacs, The GNU Emacs Manual}.
b8d4c8d0
GM
46
47@menu
b8d4c8d0
GM
48* Tables: Abbrev Tables. Creating and working with abbrev tables.
49* Defining Abbrevs:: Specifying abbreviations and their expansions.
50* Files: Abbrev Files. Saving abbrevs in files.
51* Expansion: Abbrev Expansion. Controlling expansion; expansion subroutines.
52* Standard Abbrev Tables:: Abbrev tables used by various major modes.
e047f448
SM
53* Abbrev Properties:: How to read and set abbrev properties.
54 Which properties have which effect.
55* Abbrev Table Properties:: How to read and set abbrev table properties.
56 Which properties have which effect.
b8d4c8d0
GM
57@end menu
58
ecc6530d 59@node Abbrev Tables
b8d4c8d0
GM
60@section Abbrev Tables
61
62 This section describes how to create and manipulate abbrev tables.
63
106e6894 64@defun make-abbrev-table &optional props
925672cd
CY
65This function creates and returns a new, empty abbrev table---an
66obarray containing no symbols. It is a vector filled with zeros.
67@var{props} is a property list that is applied to the new table
79415279 68(@pxref{Abbrev Table Properties}).
e047f448
SM
69@end defun
70
925672cd
CY
71@defun abbrev-table-p object
72This function returns a non-@code{nil} value if @var{object} is an
73abbrev table.
b8d4c8d0
GM
74@end defun
75
925672cd
CY
76@defun clear-abbrev-table abbrev-table
77This function undefines all the abbrevs in @var{abbrev-table}, leaving
ea16568d
GM
78it empty.
79@c Don't see why this needs saying.
80@c It always returns @code{nil}.
b8d4c8d0
GM
81@end defun
82
925672cd
CY
83@defun copy-abbrev-table abbrev-table
84This function returns a copy of @var{abbrev-table}---a new abbrev
ea16568d
GM
85table containing the same abbrev definitions. It does @emph{not} copy
86any property lists; only the names, values, and functions.
b8d4c8d0
GM
87@end defun
88
e047f448 89@defun define-abbrev-table tabname definitions &optional docstring &rest props
b8d4c8d0
GM
90This function defines @var{tabname} (a symbol) as an abbrev table
91name, i.e., as a variable whose value is an abbrev table. It defines
92abbrevs in the table according to @var{definitions}, a list of
93elements of the form @code{(@var{abbrevname} @var{expansion}
e047f448 94[@var{hook}] [@var{props}...])}. These elements are passed as
ea16568d 95arguments to @code{define-abbrev}. @c The return value is always @code{nil}.
e047f448
SM
96
97The optional string @var{docstring} is the documentation string of the
98variable @var{tabname}. The property list @var{props} is applied to
99the abbrev table (@pxref{Abbrev Table Properties}).
b8d4c8d0
GM
100
101If this function is called more than once for the same @var{tabname},
102subsequent calls add the definitions in @var{definitions} to
ea16568d 103@var{tabname}, rather than overwriting the entire original contents.
b8d4c8d0
GM
104(A subsequent call only overrides abbrevs explicitly redefined or
105undefined in @var{definitions}.)
106@end defun
107
108@defvar abbrev-table-name-list
109This is a list of symbols whose values are abbrev tables.
110@code{define-abbrev-table} adds the new abbrev table name to this list.
111@end defvar
112
113@defun insert-abbrev-table-description name &optional human
114This function inserts before point a description of the abbrev table
115named @var{name}. The argument @var{name} is a symbol whose value is an
ea16568d 116abbrev table. @c The return value is always @code{nil}.
b8d4c8d0
GM
117
118If @var{human} is non-@code{nil}, the description is human-oriented.
119System abbrevs are listed and identified as such. Otherwise the
120description is a Lisp expression---a call to @code{define-abbrev-table}
121that would define @var{name} as it is currently defined, but without
122the system abbrevs. (The mode or package using @var{name} is supposed
123to add these to @var{name} separately.)
124@end defun
125
ecc6530d 126@node Defining Abbrevs
b8d4c8d0 127@section Defining Abbrevs
925672cd 128
b8d4c8d0 129 @code{define-abbrev} is the low-level basic function for defining an
925672cd
CY
130abbrev in an abbrev table.
131
132 When a major mode defines a system abbrev, it should call
ea16568d 133@code{define-abbrev} and specify @code{t} for the @code{:system}
925672cd
CY
134property. Be aware that any saved non-``system'' abbrevs are restored
135at startup, i.e. before some major modes are loaded. Therefore, major
136modes should not assume that their abbrev tables are empty when they
137are first loaded.
138
139@defun define-abbrev abbrev-table name expansion &optional hook &rest props
140This function defines an abbrev named @var{name}, in
141@var{abbrev-table}, to expand to @var{expansion} and call @var{hook},
142with properties @var{props} (@pxref{Abbrev Properties}). The return
143value is @var{name}. The @code{:system} property in @var{props} is
144treated specially here: if it has the value @code{force}, then it will
145overwrite an existing definition even for a non-``system'' abbrev of
146the same name.
147
148@var{name} should be a string. The argument @var{expansion} is
149normally the desired expansion (a string), or @code{nil} to undefine
150the abbrev. If it is anything but a string or @code{nil}, then the
151abbreviation ``expands'' solely by running @var{hook}.
b8d4c8d0
GM
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
156@var{expansion} when @var{hook} is called.
157
158@cindex @code{no-self-insert} property
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}
ea16568d
GM
164returns @code{nil}, @code{expand-abbrev} (or @code{abbrev-insert})
165also returns @code{nil}, as if expansion had not really occurred.
b8d4c8d0 166
925672cd 167Normally, @code{define-abbrev} sets the variable
b8d4c8d0 168@code{abbrevs-changed} to @code{t}, if it actually changes the abbrev.
ea16568d 169This is so that some commands will offer to save the abbrevs. It
925672cd 170does not do this for a system abbrev, since those aren't saved anyway.
b8d4c8d0
GM
171@end defun
172
173@defopt only-global-abbrevs
174If this variable is non-@code{nil}, it means that the user plans to use
175global abbrevs only. This tells the commands that define mode-specific
176abbrevs to define global ones instead. This variable does not alter the
177behavior of the functions in this section; it is examined by their
178callers.
179@end defopt
180
ecc6530d 181@node Abbrev Files
b8d4c8d0
GM
182@section Saving Abbrevs in Files
183
184 A file of saved abbrev definitions is actually a file of Lisp code.
185The abbrevs are saved in the form of a Lisp program to define the same
186abbrev tables with the same contents. Therefore, you can load the file
187with @code{load} (@pxref{How Programs Do Loading}). However, the
188function @code{quietly-read-abbrev-file} is provided as a more
ea16568d
GM
189convenient interface. Emacs automatically calls this function at
190startup.
b8d4c8d0
GM
191
192 User-level facilities such as @code{save-some-buffers} can save
193abbrevs in a file automatically, under the control of variables
194described here.
195
196@defopt abbrev-file-name
197This is the default file name for reading and saving abbrevs.
198@end defopt
199
200@defun quietly-read-abbrev-file &optional filename
201This function reads abbrev definitions from a file named @var{filename},
202previously written with @code{write-abbrev-file}. If @var{filename} is
203omitted or @code{nil}, the file specified in @code{abbrev-file-name} is
ea16568d 204used.
b8d4c8d0 205
ea16568d
GM
206As the name implies, this function does not display any messages.
207@c It returns @code{nil}.
b8d4c8d0
GM
208@end defun
209
210@defopt save-abbrevs
211A non-@code{nil} value for @code{save-abbrevs} means that Emacs should
ea16568d
GM
212offer to save abbrevs (if any have changed) when files are saved. If
213the value is @code{silently}, Emacs saves the abbrevs without asking
214the user. @code{abbrev-file-name} specifies the file to save the
215abbrevs in.
b8d4c8d0
GM
216@end defopt
217
218@defvar abbrevs-changed
219This variable is set non-@code{nil} by defining or altering any
925672cd
CY
220abbrevs (except system abbrevs). This serves as a flag for various
221Emacs commands to offer to save your abbrevs.
b8d4c8d0
GM
222@end defvar
223
224@deffn Command write-abbrev-file &optional filename
925672cd 225Save all abbrev definitions (except system abbrevs), for all abbrev
b8d4c8d0
GM
226tables listed in @code{abbrev-table-name-list}, in the file
227@var{filename}, in the form of a Lisp program that when loaded will
228define the same abbrevs. If @var{filename} is @code{nil} or omitted,
229@code{abbrev-file-name} is used. This function returns @code{nil}.
230@end deffn
231
ecc6530d 232@node Abbrev Expansion
b8d4c8d0
GM
233@section Looking Up and Expanding Abbreviations
234
235 Abbrevs are usually expanded by certain interactive commands,
236including @code{self-insert-command}. This section describes the
237subroutines used in writing such commands, as well as the variables they
238use for communication.
239
240@defun abbrev-symbol abbrev &optional table
241This function returns the symbol representing the abbrev named
ea16568d 242@var{abbrev}. It returns @code{nil} if that abbrev is not
b8d4c8d0 243defined. The optional second argument @var{table} is the abbrev table
925672cd
CY
244in which to look it up. If @var{table} is @code{nil}, this function
245tries first the current buffer's local abbrev table, and second the
246global abbrev table.
b8d4c8d0
GM
247@end defun
248
249@defun abbrev-expansion abbrev &optional table
250This function returns the string that @var{abbrev} would expand into (as
ea16568d
GM
251defined by the abbrev tables used for the current buffer). It returns
252@code{nil} if @var{abbrev} is not a valid abbrev.
b8d4c8d0
GM
253The optional argument @var{table} specifies the abbrev table to use,
254as in @code{abbrev-symbol}.
255@end defun
256
257@deffn Command expand-abbrev
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.
261
ea16568d 262If the abbrev symbol has a hook function that is a symbol whose
b8d4c8d0
GM
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.
266@end deffn
267
5854c267 268@defun abbrev-insert abbrev &optional name start end
925672cd
CY
269This function inserts the abbrev expansion of @code{abbrev}, replacing
270the text between @code{start} and @code{end}. If @code{start} is
271omitted, it defaults to point. @code{name}, if non-@code{nil}, should
272be the name by which this abbrev was found (a string); it is used to
273figure out whether to adjust the capitalization of the expansion. The
274function returns @code{abbrev} if the abbrev was successfully
275inserted.
5854c267 276@end defun
925672cd 277
b8d4c8d0
GM
278@deffn Command abbrev-prefix-mark &optional arg
279This command marks the current location of point as the beginning of
280an abbrev. The next call to @code{expand-abbrev} will use the text
281from here to point (where it is then) as the abbrev to expand, rather
282than using the previous word as usual.
283
284First, this command expands any abbrev before point, unless @var{arg}
285is non-@code{nil}. (Interactively, @var{arg} is the prefix argument.)
286Then it inserts a hyphen before point, to indicate the start of the
287next abbrev to be expanded. The actual expansion removes the hyphen.
288@end deffn
289
290@defopt abbrev-all-caps
291When this is set non-@code{nil}, an abbrev entered entirely in upper
292case is expanded using all upper case. Otherwise, an abbrev entered
293entirely in upper case is expanded by capitalizing each word of the
294expansion.
295@end defopt
296
297@defvar abbrev-start-location
298The value of this variable is a buffer position (an integer or a marker)
299for @code{expand-abbrev} to use as the start of the next abbrev to be
300expanded. The value can also be @code{nil}, which means to use the
301word before point instead. @code{abbrev-start-location} is set to
302@code{nil} each time @code{expand-abbrev} is called. This variable is
303also set by @code{abbrev-prefix-mark}.
304@end defvar
305
306@defvar abbrev-start-location-buffer
307The value of this variable is the buffer for which
308@code{abbrev-start-location} has been set. Trying to expand an abbrev
309in any other buffer clears @code{abbrev-start-location}. This variable
310is set by @code{abbrev-prefix-mark}.
311@end defvar
312
313@defvar last-abbrev
314This is the @code{abbrev-symbol} of the most recent abbrev expanded. This
315information is left by @code{expand-abbrev} for the sake of the
316@code{unexpand-abbrev} command (@pxref{Expanding Abbrevs,, Expanding
317Abbrevs, emacs, The GNU Emacs Manual}).
318@end defvar
319
320@defvar last-abbrev-location
321This is the location of the most recent abbrev expanded. This contains
322information left by @code{expand-abbrev} for the sake of the
323@code{unexpand-abbrev} command.
324@end defvar
325
326@defvar last-abbrev-text
327This is the exact expansion text of the most recent abbrev expanded,
328after case conversion (if any). Its value is @code{nil} if the abbrev
329has already been unexpanded. This contains information left by
330@code{expand-abbrev} for the sake of the @code{unexpand-abbrev} command.
331@end defvar
332
e047f448 333@defvar abbrev-expand-functions
ea16568d
GM
334This is a wrapper hook (@pxref{Running Hooks}) run around the
335@code{expand-abbrev} function. Each function on this hook is called
336with a single argument: a function that performs the normal abbrev
337expansion. The hook function can hence do anything it wants before
338and after performing the expansion. It can also choose not to call
339its argument, thus overriding the default behavior; or it may even
340call it several times. The function should return the abbrev symbol
341if expansion took place.
b8d4c8d0
GM
342@end defvar
343
344 The following sample code shows a simple use of
e047f448 345@code{abbrev-expand-functions}. It assumes that @code{foo-mode} is a
b8d4c8d0
GM
346mode for editing certain files in which lines that start with @samp{#}
347are comments. You want to use Text mode abbrevs for those lines. The
348regular local abbrev table, @code{foo-mode-abbrev-table} is
ea16568d 349appropriate for all other lines. @xref{Standard Abbrev Tables}, for the
b8d4c8d0
GM
350definitions of @code{local-abbrev-table} and @code{text-mode-abbrev-table}.
351
352@smallexample
e047f448
SM
353(defun foo-mode-abbrev-expand-function (expand)
354 (if (not (save-excursion (forward-line 0) (eq (char-after) ?#)))
355 ;; Performs normal expansion.
356 (funcall expand)
357 ;; We're inside a comment: use the text-mode abbrevs.
358 (let ((local-abbrev-table text-mode-abbrev-table))
359 (funcall expand))))
b8d4c8d0
GM
360
361(add-hook 'foo-mode-hook
d24880de
GM
362 #'(lambda ()
363 (add-hook 'abbrev-expand-functions
364 'foo-mode-abbrev-expand-function
365 nil t)))
b8d4c8d0
GM
366@end smallexample
367
ecc6530d 368@node Standard Abbrev Tables
b8d4c8d0
GM
369@section Standard Abbrev Tables
370
371 Here we list the variables that hold the abbrev tables for the
372preloaded major modes of Emacs.
373
374@defvar global-abbrev-table
375This is the abbrev table for mode-independent abbrevs. The abbrevs
376defined in it apply to all buffers. Each buffer may also have a local
377abbrev table, whose abbrev definitions take precedence over those in the
378global table.
379@end defvar
380
381@defvar local-abbrev-table
382The value of this buffer-local variable is the (mode-specific)
e047f448
SM
383abbreviation table of the current buffer. It can also be a list of
384such tables.
385@end defvar
386
387@defvar abbrev-minor-mode-table-alist
388The value of this variable is a list of elements of the form
389@code{(@var{mode} . @var{abbrev-table})} where @var{mode} is the name
390of a variable: if the variable is bound to a non-@code{nil} value,
391then the @var{abbrev-table} is active, otherwise it is ignored.
392@var{abbrev-table} can also be a list of abbrev tables.
b8d4c8d0
GM
393@end defvar
394
395@defvar fundamental-mode-abbrev-table
396This is the local abbrev table used in Fundamental mode; in other words,
397it is the local abbrev table in all buffers in Fundamental mode.
398@end defvar
399
400@defvar text-mode-abbrev-table
401This is the local abbrev table used in Text mode.
402@end defvar
403
404@defvar lisp-mode-abbrev-table
ea16568d
GM
405This is the local abbrev table used in Lisp mode. It is the parent
406of the local abbrev table used in Emacs Lisp mode. @xref{Abbrev Table
407Properties}.
b8d4c8d0
GM
408@end defvar
409
ecc6530d 410@node Abbrev Properties
e047f448
SM
411@section Abbrev Properties
412
413Abbrevs have properties, some of which influence the way they work.
ea16568d 414You can provide them as arguments to @code{define-abbrev}, and
925672cd 415manipulate them with the following functions:
e047f448
SM
416
417@defun abbrev-put abbrev prop val
925672cd 418Set the property @var{prop} of @var{abbrev} to value @var{val}.
e047f448
SM
419@end defun
420
421@defun abbrev-get abbrev prop
925672cd
CY
422Return the property @var{prop} of @var{abbrev}, or @code{nil} if the
423abbrev has no such property.
e047f448
SM
424@end defun
425
925672cd 426The following properties have special meanings:
e047f448
SM
427
428@table @code
79415279 429@item :count
e047f448
SM
430This property counts the number of times the abbrev has
431been expanded. If not explicitly set, it is initialized to 0 by
432@code{define-abbrev}.
433
79415279 434@item :system
925672cd
CY
435If non-@code{nil}, this property marks the abbrev as a system abbrev.
436Such abbrevs are not saved (@pxref{Abbrev Files}).
e047f448
SM
437
438@item :enable-function
79415279 439If non-@code{nil}, this property should be a function of no
e047f448
SM
440arguments which returns @code{nil} if the abbrev should not be used
441and @code{t} otherwise.
442
443@item :case-fixed
444If non-@code{nil}, this property indicates that the case of the
445abbrev's name is significant and should only match a text with the
79415279
SM
446same pattern of capitalization. It also disables the code that
447modifies the capitalization of the expansion.
e047f448
SM
448@end table
449
ecc6530d 450@node Abbrev Table Properties
e047f448
SM
451@section Abbrev Table Properties
452
bc5cde4b 453Like abbrevs, abbrev tables have properties, some of which influence
79415279 454the way they work. You can provide them as arguments to
ea16568d 455@code{define-abbrev-table}, and manipulate them with the functions:
e047f448
SM
456
457@defun abbrev-table-put table prop val
458Set the property @var{prop} of abbrev table @var{table} to value @var{val}.
459@end defun
460
461@defun abbrev-table-get table prop
462Return the property @var{prop} of abbrev table @var{table}, or @code{nil}
463if the abbrev has no such property.
464@end defun
465
466The following properties have special meaning:
467
468@table @code
469@item :enable-function
79415279 470This is like the @code{:enable-function} abbrev property except that
ea16568d
GM
471it applies to all abbrevs in the table. It is used before even trying
472to find the abbrev before point, so it can dynamically modify the
79415279 473abbrev table.
e047f448
SM
474
475@item :case-fixed
79415279
SM
476This is like the @code{:case-fixed} abbrev property except that it
477applies to all abbrevs in the table.
e047f448
SM
478
479@item :regexp
480If non-@code{nil}, this property is a regular expression that
ea16568d 481indicates how to extract the name of the abbrev before point, before
e047f448
SM
482looking it up in the table. When the regular expression matches
483before point, the abbrev name is expected to be in submatch 1.
ea16568d
GM
484If this property is @code{nil}, the default is to use
485@code{backward-word} and @code{forward-word} to find the name. This
486property allows the use of abbrevs whose name contains characters of
487non-word syntax.
e047f448
SM
488
489@item :parents
ea16568d 490This property holds a list of tables from which to inherit
e047f448
SM
491other abbrevs.
492
493@item :abbrev-table-modiff
494This property holds a counter incremented each time a new abbrev is
495added to the table.
496
497@end table