1 |
@c -*-texinfo-*- |
@c -*-texinfo-*- |
2 |
@c This is part of the GNU Emacs Lisp Reference Manual. |
@c This is part of the GNU Emacs Lisp Reference Manual. |
3 |
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1999 |
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1999, 2004 |
4 |
@c Free Software Foundation, Inc. |
@c Free Software Foundation, Inc. |
5 |
@c See the file elisp.texi for copying conditions. |
@c See the file elisp.texi for copying conditions. |
6 |
@setfilename ../info/abbrevs |
@setfilename ../info/abbrevs |
288 |
@end defopt |
@end defopt |
289 |
|
|
290 |
@defvar abbrev-start-location |
@defvar abbrev-start-location |
291 |
The value of this variable is a marker pointing to the buffer position |
The value of this variable is a buffer position (an integer or a marker) |
292 |
for @code{expand-abbrev} to use as the start of the next abbrev to be |
for @code{expand-abbrev} to use as the start of the next abbrev to be |
293 |
expanded. The value can also be @code{nil}, which means to use the |
expanded. The value can also be @code{nil}, which means to use the |
294 |
word before point instead. @code{abbrev-start-location} is set to |
word before point instead. @code{abbrev-start-location} is set to |
331 |
the abbrev to be expanded by looking in the buffer before point. |
the abbrev to be expanded by looking in the buffer before point. |
332 |
Running the hook is the first thing that @code{expand-abbrev} does, and |
Running the hook is the first thing that @code{expand-abbrev} does, and |
333 |
so a hook function can be used to change the current abbrev table before |
so a hook function can be used to change the current abbrev table before |
334 |
abbrev lookup happens. |
abbrev lookup happens. (Although you have to do this carefully. See |
335 |
|
the example below.) |
336 |
@end defvar |
@end defvar |
337 |
|
|
338 |
The following sample code shows a simple use of |
The following sample code shows a simple use of |
339 |
@code{pre-abbrev-expand-hook}. If the user terminates an abbrev with |
@code{pre-abbrev-expand-hook}. It assumes that @code{foo-mode} is a |
340 |
a punctuation character, the hook function asks for confirmation. It |
mode for editing certain files in which lines that start with @samp{#} |
341 |
aborts expansion if the user does not confirm. |
are comments. You want to use Text mode abbrevs for those lines. The |
342 |
|
regular local abbrev table, @code{foo-mode-abbrev-table} is |
343 |
|
appropriate for all other lines. Then you can put the following code |
344 |
|
in your @file{.emacs} file. @xref{Standard Abbrev Tables}, for the |
345 |
|
definitions of @code{local-abbrev-table} and @code{text-mode-abbrev-table}. |
346 |
|
|
347 |
@smallexample |
@smallexample |
348 |
(add-hook 'pre-abbrev-expand-hook 'query-if-not-space) |
(defun foo-mode-pre-abbrev-expand () |
349 |
|
(when (save-excursion (forward-line 0) (eq (char-after) ?#)) |
350 |
;; @r{This is the function invoked by @code{pre-abbrev-expand-hook}.} |
(let ((local-abbrev-table text-mode-abbrev-table) |
351 |
|
;; Avoid infinite loop. |
352 |
;; @r{If the user terminated the abbrev with a space, the function does} |
(pre-abbrev-expand-hook nil)) |
353 |
;; @r{nothing (that is, it returns so that the abbrev can expand). If the} |
(expand-abbrev)) |
354 |
;; @r{user entered some other character, this function asks whether} |
;; We have already called `expand-abbrev' in this hook. |
355 |
;; @r{expansion should continue.} |
;; Hence we want the "actual" call following this hook to be a no-op. |
356 |
|
(setq abbrev-start-location (point-max) |
357 |
;; @r{The function's return value makes no difference.} |
abbrev-start-location-buffer (current-buffer)))) |
358 |
|
|
359 |
(defun query-if-not-space () |
(add-hook 'foo-mode-hook |
360 |
(if (/= ?\s last-command-char) |
#'(lambda () |
361 |
(if (not (y-or-n-p "Do you want to expand this abbrev? ")) |
(add-hook 'pre-abbrev-expand-hook |
362 |
(error "Not expanding this abbrev")))) |
'foo-mode-pre-abbrev-expand |
363 |
|
nil t))) |
364 |
@end smallexample |
@end smallexample |
365 |
|
|
366 |
|
Note that @code{foo-mode-pre-abbrex-expand} just returns @code{nil} |
367 |
|
without doing anything for lines not starting with @samp{#}. Hence |
368 |
|
abbrevs expand normally using @code{foo-mode-abbrev-table} as local |
369 |
|
abbrev table for such lines. |
370 |
|
|
371 |
@node Standard Abbrev Tables, , Abbrev Expansion, Abbrevs |
@node Standard Abbrev Tables, , Abbrev Expansion, Abbrevs |
372 |
@comment node-name, next, previous, up |
@comment node-name, next, previous, up |
373 |
@section Standard Abbrev Tables |
@section Standard Abbrev Tables |