(Fmouse_position) [MULTI_FRAME]: Use pixel_to_glyph, not glyph_to_pixel. Skip
[emacs.git] / lispref / abbrevs.texi
blob59e1995a4d58b1504ef2b0560e3d4ee19231d078
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
7 @chapter Abbrevs And Abbrev Expansion
8 @cindex abbrev
9 @cindex abbrev table
11   An abbreviation or @dfn{abbrev} is a string of characters that may be
12 expanded to a longer string.  The user can insert the abbrev string and
13 find it replaced automatically with the expansion of the abbrev.  This
14 saves typing.
16   The set of abbrevs currently in effect is recorded in an @dfn{abbrev
17 table}.  Each buffer has a local abbrev table, but normally all buffers
18 in the same major mode share one abbrev table.  There is also a global
19 abbrev table.  Normally both are used.
21   An abbrev table is represented as an obarray containing a symbol for
22 each abbreviation.  The symbol's name is the abbreviation.  Its value is
23 the expansion; its function definition is the hook function to do the
24 expansion (if any); its property list cell contains the use count, the
25 number of times the abbreviation has been expanded.  Because these
26 symbols are not interned in the usual obarray, they will never appear as
27 the result of reading a Lisp expression; in fact, normally they are
28 never used except by the code that handles abbrevs.  Therefore, it is
29 safe to use them in an extremely nonstandard way.  @xref{Creating
30 Symbols}.
32   For the user-level commands for abbrevs, see @ref{Abbrevs,, Abbrev
33 Mode, emacs, The GNU Emacs Manual}.
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
44 @node Abbrev Mode, Abbrev Tables, Abbrevs, Abbrevs
45 @comment  node-name,  next,  previous,  up
46 @section Setting Up Abbrev Mode    
48   Abbrev mode is a minor mode controlled by the value of the variable
49 @code{abbrev-mode}.
51 @defvar abbrev-mode
52 A non-@code{nil} value of this variable turns on the automatic expansion
53 of abbrevs when their abbreviations are inserted into a buffer.
54 If the value is @code{nil}, abbrevs may be defined, but they are not
55 expanded automatically.
57 This variable automatically becomes local when set in any fashion.
58 @end defvar
60 @defvar default-abbrev-mode
61 This is the value @code{abbrev-mode} for buffers that do not override it.
62 This is the same as @code{(default-value 'abbrev-mode)}.
63 @end defvar
65 @node Abbrev Tables, Defining Abbrevs, Abbrev Mode, Abbrevs
66 @section Abbrev Tables
68   This section describes how to create and manipulate abbrev tables.
70 @defun make-abbrev-table
71 This function creates and returns a new, empty abbrev table---an obarray
72 containing no symbols.  It is a vector filled with zeros.
73 @end defun
75 @defun clear-abbrev-table table
76 This function undefines all the abbrevs in abbrev table @var{table},
77 leaving it empty.  The function returns @code{nil}.
78 @end defun
80 @defun define-abbrev-table tabname definitions
81 This function defines @var{tabname} (a symbol) as an abbrev table name,
82 i.e., as a variable whose value is an abbrev table.  It defines abbrevs
83 in the table according to @var{definitions}, a list of elements of the
84 form @code{(@var{abbrevname} @var{expansion} @var{hook}
85 @var{usecount})}.  The value is always @code{nil}.
86 @end defun
88 @defvar abbrev-table-name-list
89 This 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
93 @defun insert-abbrev-table-description name &optional human
94 This function inserts before point a description of the abbrev table
95 named @var{name}.  The argument @var{name} is a symbol whose value is an
96 abbrev table.  The value is always @code{nil}.
98 If @var{human} is non-@code{nil}, the description is human-oriented.
99 Otherwise the description is a Lisp expression---a call to
100 @code{define-abbrev-table} which would define @var{name} exactly as it
101 is currently defined.
102 @end defun
104 @node Defining Abbrevs, Abbrev Files, Abbrev Tables, Abbrevs
105 @comment  node-name,  next,  previous,  up
106 @section Defining Abbrevs
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
111 user.
113 @defun add-abbrev table type arg
114 This function adds an abbreviation to abbrev table @var{table}.  The
115 argument @var{type} is a string describing in English the kind of abbrev
116 this will be (typically, @code{"global"} or @code{"mode-specific"});
117 this is used in prompting the user.  The argument @var{arg} is the
118 number of words in the expansion.
120 The return value is the symbol which internally represents the new
121 abbrev, or @code{nil} if the user declines to confirm redefining an
122 existing abbrev.
123 @end defun
125 @defun define-abbrev table name expansion hook
126 This function defines an abbrev in @var{table} named @var{name}, to
127 expand to @var{expansion}, and call @var{hook}.  The return value is an
128 uninterned symbol which represents the abbrev inside Emacs; its name is
129 @var{name}.
131 The argument @var{name} should be a string.  The argument
132 @var{expansion} should be a string, or @code{nil}, to undefine the
133 abbrev.
135 The argument @var{hook} is a function or @code{nil}.  If @var{hook} is
136 non-@code{nil}, then it is called with no arguments after the abbrev is
137 replaced with @var{expansion}; point is located at the end of
138 @var{expansion}.
140 The use count of the abbrev is initialized to zero.
141 @end defun
143 @defopt only-global-abbrevs
144 If this variable is non-@code{nil}, it means that the user plans to use
145 global abbrevs only.  This tells the commands that define mode-specific
146 abbrevs to define global ones instead.  This variable does not alter the
147 functioning of the functions in this section; it is examined by their
148 callers.
149 @end defopt
151 @node Abbrev Files, Abbrev Expansion, Defining Abbrevs, Abbrevs
152 @section Saving Abbrevs in Files
154   A file of saved abbrev definitions is actually a file of Lisp code.
155 The abbrevs are saved in the form of a Lisp program to define the same
156 abbrev tables with the same contents.  Therefore, you can load the file
157 with @code{load} (@pxref{How Programs Do Loading}).  However, the
158 function @code{quietly-read-abbrev-file} is provided as a more
159 convenient interface.
161   User-level facilities such as @code{save-some-buffers} can save
162 abbrevs in a file automatically, under the control of variables
163 described here.
165 @defopt abbrev-file-name
166 This is the default file name for reading and saving abbrevs.
167 @end defopt
169 @defun quietly-read-abbrev-file filename
170 This function reads abbrev definitions from a file named @var{filename},
171 previously written with @code{write-abbrev-file}.  If @var{filename} is
172 @code{nil}, the file specified in @code{abbrev-file-name} is used.
173 @code{save-abbrevs} is set to @code{t} so that changes will be saved.
175 This function does not display any messages.  It returns @code{nil}.
176 @end defun
178 @defopt save-abbrevs
179 A non-@code{nil} value for @code{save-abbrev} means that Emacs should
180 save abbrevs when files are saved.  @code{abbrev-file-name} specifies
181 the file to save the abbrevs in.
182 @end defopt
184 @defvar abbrevs-changed
185 This variable is set non-@code{nil} by defining or altering any 
186 abbrevs.  This serves as a flag for various Emacs commands to offer to
187 save your abbrevs.
188 @end defvar
190 @deffn Command write-abbrev-file filename
191 Save all abbrev definitions, in all abbrev tables, in the file
192 @var{filename}, in the form of a Lisp program which when loaded will
193 define the same abbrevs.  This function returns @code{nil}.
194 @end deffn
196 @node Abbrev Expansion, Standard Abbrev Tables, Abbrev Files, Abbrevs
197 @comment  node-name,  next,  previous,  up
198 @section Looking Up and Expanding Abbreviations
200   Abbrevs are usually expanded by commands for interactive use,
201 including @code{self-insert-command}.  This section describes the
202 subroutines used in writing such functions, as well as the variables
203 they use for communication.
205 @defun abbrev-symbol abbrev &optional table
206 This function returns the symbol representing the abbrev named
207 @var{abbrev}.  The value returned is @code{nil} if that abbrev is not
208 defined.  The optional second argument @var{table} is the abbrev table
209 to look it up in.  If @var{table} is @code{nil}, this function tries
210 first the current buffer's local abbrev table, and second the global
211 abbrev table.
212 @end defun
214 @defopt abbrev-all-caps
215 When this is set non-@code{nil}, an abbrev entered entirely in upper
216 case is expanded using all upper case.  Otherwise, an abbrev entered
217 entirely in upper case is expanded by capitalizing each word of the
218 expansion.
219 @end defopt
221 @defun abbrev-expansion abbrev &optional table
222 This function returns the string that @var{abbrev} would expand into (as
223 defined by the abbrev tables used for the current buffer).  The optional
224 argument @var{table} specifies the abbrev table to use; if it is
225 specified, the abbrev is looked up in that table only.
226 @end defun
228 @defvar abbrev-start-location
229 This is the buffer position for @code{expand-abbrev} to use as the start
230 of the next abbrev to be expanded.  (@code{nil} means use the word
231 before point instead.)  @code{abbrev-start-location} is set to
232 @code{nil} each time @code{expand-abbrev} is called.  This variable is
233 also set by @code{abbrev-prefix-mark}.
234 @end defvar
236 @defvar abbrev-start-location-buffer
237 The value of this variable is the buffer for which
238 @code{abbrev-start-location} has been set.  Trying to expand an abbrev
239 in any other buffer clears @code{abbrev-start-location}.  This variable
240 is set by @code{abbrev-prefix-mark}.
241 @end defvar
243 @defvar last-abbrev
244 This is the @code{abbrev-symbol} of the last abbrev expanded.  This
245 information is left by @code{expand-abbrev} for the sake of the
246 @code{unexpand-abbrev} command.
247 @end defvar
249 @defvar last-abbrev-location
250 This is the location of the last abbrev expanded.  This contains
251 information left by @code{expand-abbrev} for the sake of the
252 @code{unexpand-abbrev} command.
253 @end defvar
255 @defvar last-abbrev-text
256 This is the exact expansion  text of the last abbrev expanded, as
257 results from case conversion.  Its value is
258 @code{nil} if the abbrev has already been unexpanded.  This
259 contains information left by @code{expand-abbrev} for the sake of the
260 @code{unexpand-abbrev} command.
261 @end defvar
263 @c Emacs 19 feature
264 @defvar pre-abbrev-expand-hook
265 This is a normal hook whose functions are executed, in sequence, just
266 before any expansion of an abbrev.  @xref{Hooks}.  Since it is a normal
267 hook, the hook functions receive no arguments.  However, they can find
268 the abbrev to be expanded by looking in the buffer before point.
269 @end defvar
271   The following sample code shows a simple use of
272 @code{pre-abbrev-expand-hook}.  If the user terminates an abbrev with a
273 punctuation character, the hook function asks for confirmation.  Thus,
274 this hook allows the user to decide whether to expand the abbrev, and
275 aborts expansion if it is not confirmed.
277 @smallexample
278 (add-hook 'pre-abbrev-expand-hook 'query-if-not-space)
280 ;; @r{This is the function invoked by @code{pre-abbrev-expand-hook}.}
282 ;; @r{If the user terminated the abbrev with a space, the function does}
283 ;; @r{nothing (that is, it returns so that the abbrev can expand).  If the}
284 ;; @r{user entered some other character, this function asks whether}
285 ;; @r{expansion should continue.}
287 ;; @r{If the user enters the prompt with @kbd{y}, the function returns}
288 ;; @r{@code{nil} (because of the @code{not} function), but that is}
289 ;; @r{acceptable; the return value has no effect on expansion.}
291 (defun query-if-not-space ()
292   (if (/= ?\  (preceding-char))
293       (if (not (y-or-n-p "Do you want to expand this abbrev? "))
294           (error "Not expanding this abbrev"))))
295 @end smallexample
297 @node Standard Abbrev Tables,  , Abbrev Expansion, Abbrevs
298 @comment  node-name,  next,  previous,  up
299 @section Standard Abbrev Tables
301   Here we list the variables that hold the abbrev tables for the
302 preloaded major modes of Emacs.
304 @defvar global-abbrev-table
305 This is the abbrev table for mode-independent abbrevs.  The abbrevs
306 defined in it apply to all buffers.  Each buffer may also have a local
307 abbrev table, whose abbrev definitions take precedence over those in the
308 global table.
309 @end defvar
311 @defvar local-abbrev-table
312 The value of this buffer-local variable is the (mode-specific)
313 abbreviation table of the current buffer.
314 @end defvar
316 @defvar fundamental-mode-abbrev-table
317 This is the local abbrev table used in Fundamental mode.  It is the
318 local abbrev table in all buffers in Fundamental mode.
319 @end defvar
321 @defvar text-mode-abbrev-table
322 This is the local abbrev table used in Text mode.
323 @end defvar
325 @defvar c-mode-abbrev-table
326 This is the local abbrev table used in C mode.
327 @end defvar
329 @defvar lisp-mode-abbrev-table
330 This is the local abbrev table used in Lisp mode and Emacs Lisp mode.
331 @end defvar