Add a `provide' call.
[bpt/emacs.git] / lisp / dabbrev.el
CommitLineData
7313ccdb 1;;; dabbrev.el --- dynamic abbreviation package
b578f267 2
7313ccdb 3;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
2f790b20 4
6163b3ba
RS
5;; Author: Don Morrison
6;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
7;; Created: 16 Mars 1992
df6eb420 8;; Lindberg's last update version: 5.7
6163b3ba 9;; Keywords: abbrev expand completion
3a801d0c 10
b578f267
EN
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
2f790b20 14;; it under the terms of the GNU General Public License as published by
b578f267
EN
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
2f790b20
JB
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
b578f267 22
2f790b20 23;; You should have received a copy of the GNU General Public License
b578f267
EN
24;; along with GNU Emacs; see the file COPYING. If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
2f790b20 27
e5167999 28;;; Commentary:
2f790b20 29
6163b3ba
RS
30;; The purpose with this package is to let you write just a few
31;; characters of words you've written earlier to be able to expand
32;; them.
33;;
34;; To expand a word, just put the point right after the word and press
35;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
36;;
6163b3ba
RS
37;; Check out the customizable variables below to learn about all the
38;; features of this package.
39
40;;; Hints and tips for major modes writers:
41
42;; Recommended values C/Lisp etc text
43;; dabbrev-case-fold-search nil t
44;; dabbrev-case-replace nil t
45;;
46;; Set the variables you want special for your mode like this:
47;; (set (make-local-variable 'dabbrev-case-replace) nil)
a7acbbe4 48;; Then you don't interfere with other modes.
6163b3ba
RS
49;;
50;; If your mode handles buffers that refers to other buffers
51;; (i.e. compilation-mode, gud-mode), then try to set
52;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
53;; to a function that point out those buffers.
54
55;; Same goes for major-modes that are connected to other modes. There
56;; are for instance a number of mail-modes. One for reading, one for
57;; creating a new mail etc. Maybe those should be connected.
58
59;; Example for GNUS (when we write a reply, we want dabbrev to look in
60;; the article for expansion):
61;; (set (make-local-variable 'dabbrev-friend-buffer-function)
62;; (lambda (buffer)
63;; (save-excursion
64;; (set-buffer buffer)
65;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
66
6163b3ba
RS
67
68;; Known bugs and limitations.
69;; - Possible to do several levels of `dabbrev-completion' in the
70;; minibuffer.
71;; - dabbrev-completion doesn't handle resetting the globals variables
72;; right. It resets them after finding the abbrev.
73
74;; Future enhancements
75;; - Check the tags-files? Like tags-complete?
76;; - Add the possibility of searching both forward and backward to
77;; the nearest expansion.
7313ccdb
RS
78;; - Check the kill-ring when everything else fails. (Maybe something
79;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
6163b3ba 80
df6eb420 81;;; These people gave suggestions:
6163b3ba
RS
82;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
83;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
84;; [jules] Julian Gosnell <jules@x.co.uk>
85;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
86;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
87;; [alon] Alon Albert <al%imercury@uunet.uu.net>
88;; [tromey] Tom Tromey <tromey@busco.lanl.gov>
89;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
90;; [Petri] Petri Raitio <per@tekla.fi>
91;; [ejb] Jay Berkenbilt <ejb@ERA.COM>
92;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
93;; ... and to all the people who have participated in the beta tests.
2f790b20 94
e5167999 95;;; Code:
6163b3ba 96
b578f267
EN
97;;----------------------------------------------------------------
98;; Customization variables
99;;----------------------------------------------------------------
6163b3ba 100
bbf5eb28
RS
101(defgroup dabbrev nil
102 "Dynamic Abbreviations"
103 :tag "Dynamic Abbreviations"
104 :group 'abbrev)
6163b3ba 105
bbf5eb28
RS
106(defcustom dabbrev-backward-only nil
107 "*If non-nil, `dabbrev-expand' only looks backwards."
108 :type 'boolean
109 :group 'dabbrev)
110
111(defcustom dabbrev-limit nil
112 "*Limits region searched by `dabbrev-expand' to this many chars away."
113 :type '(choice (const :tag "off" nil)
114 integer)
115 :group 'dabbrev)
116
117(defcustom dabbrev-abbrev-skip-leading-regexp nil
6163b3ba
RS
118 "*Regexp for skipping leading characters of an abbreviation.
119
7313ccdb
RS
120Example: Set this to \"\\\\$\" for programming languages
121in which variable names may appear with or without a leading `$'.
bbf5eb28
RS
122\(For example, in Makefiles.
123 :type 'regexp
124 :group 'dabbrev))
6163b3ba 125
bbf5eb28
RS
126Set this to nil if no characters should be skipped."
127 :type '(choice regexp
128 (const :tag "off" nil))
129 :group 'dabbrev)
6163b3ba
RS
130
131;; I recommend that you set this to nil.
bbf5eb28 132(defcustom dabbrev-case-fold-search 'case-fold-search
7313ccdb
RS
133 "*Non-nil if dabbrev searches should ignore case.
134A value of nil means case is significant.
6163b3ba 135
7313ccdb
RS
136The value of this variable is an expression; it is evaluated
137and the resulting value determines the decision.
138For example: setting this to `case-fold-search' means evaluate that
bbf5eb28
RS
139variable to see whether its value is nil."
140 :type 'sexp
141 :group 'dabbrev)
6163b3ba 142
bbf5eb28 143(defcustom dabbrev-upcase-means-case-search nil
6163b3ba 144 "*The significance of an uppercase character in an abbreviation.
7313ccdb 145nil means case fold search, non-nil means case sensitive search.
6163b3ba 146
7313ccdb 147This variable has an effect only when the value of
bbf5eb28
RS
148`dabbrev-case-fold-search' evaluates to t."
149 :type 'boolean
150 :group 'dabbrev)
6163b3ba
RS
151
152;; I recommend that you set this to nil.
bbf5eb28 153(defcustom dabbrev-case-replace 'case-replace
7313ccdb 154 "*Non-nil means dabbrev should preserve case when expanding the abbreviation.
83f80458
RS
155More precisely, it preserves the case pattern of the abbreviation as you
156typed it--as opposed to the case pattern of the expansion that is copied.
7313ccdb
RS
157The value of this variable is an expression; it is evaluated
158and the resulting value determines the decision.
159For example, setting this to `case-replace' means evaluate that
160variable to see if its value is t or nil.
6163b3ba 161
7313ccdb 162This variable has an effect only when the value of
bbf5eb28
RS
163`dabbrev-case-fold-search' evaluates to t."
164 :type 'sexp
165 :group 'dabbrev)
6163b3ba 166
bbf5eb28 167(defcustom dabbrev-abbrev-char-regexp nil
7313ccdb
RS
168 "*Regexp to recognize a character in an abbreviation or expansion.
169This regexp will be surrounded with \\\\( ... \\\\) when actually used.
6163b3ba 170
7313ccdb 171Set this variable to \"\\\\sw\" if you want ordinary words or
df6eb420
RS
172\"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
173syntax is \"symbol\" as well as those whose syntax is \"word\".
6163b3ba 174
df6eb420
RS
175The value nil has a special meaning: the abbreviation is from point to
176previous word-start, but the search is for symbols.
6163b3ba 177
7313ccdb 178For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
df6eb420
RS
179while `yes', `or', `no' and `p' are considered words. If this
180variable is nil, then expanding `yes-or-no-' looks for a symbol
7313ccdb
RS
181starting with or containing `no-'. If you set this variable to
182\"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
183`yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
184expanding `yes-or-no-' signals an error because `-' is not part of a word;
185but expanding `yes-or-no' looks for a word starting with `no'.
6163b3ba 186
bbf5eb28
RS
187The recommended value is \"\\\\sw\\\\|\\\\s_\"."
188 :type '(choice (const nil)
189 regexp)
190 :group 'dabbrev)
6163b3ba 191
bbf5eb28 192(defcustom dabbrev-check-all-buffers t
df6eb420 193 "*Non-nil means dabbrev package should search *all* buffers.
6163b3ba 194
df6eb420
RS
195Dabbrev always searches the current buffer first. Then, if
196`dabbrev-check-other-buffers' says so, it searches the buffers
197designated by `dabbrev-select-buffers-function'.
6163b3ba 198
df6eb420 199Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
bbf5eb28
RS
200all the other buffers."
201 :type 'boolean
202 :group 'dabbrev)
df6eb420 203
bbf5eb28 204(defcustom dabbrev-check-other-buffers t
6163b3ba 205 "*Should \\[dabbrev-expand] look in other buffers?\
6163b3ba 206
df6eb420
RS
207nil: Don't look in other buffers.
208t: Also look for expansions in the buffers pointed out by
209 `dabbrev-select-buffers-function'.
210Anything else: When we can't find any more expansions in
211the current buffer, then ask the user whether to look in other
212buffers too.
213
bbf5eb28
RS
214The default value is t."
215 :type '(choice (const :tag "off" nil)
216 (const :tag "on" t)
217 (const :tag "ask" other))
218 :group 'dabbrev)
6163b3ba
RS
219
220;; I guess setting this to a function that selects all C- or C++-
221;; mode buffers would be a good choice for a debugging buffer,
222;; when debugging C- or C++-code.
223(defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
224 "A function that selects buffers that should be searched by dabbrev.
6163b3ba
RS
225The function should take no arguments and return a list of buffers to
226search for expansions. Have a look at `dabbrev--select-buffers' for
227an example.
228
229A mode setting this variable should make it buffer local.")
230
bbf5eb28 231(defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
df6eb420 232 "*A function to decide whether dabbrev should search OTHER-BUFFER.
6163b3ba
RS
233The function should take one argument, OTHER-BUFFER, and return
234non-nil if that buffer should be searched. Have a look at
235`dabbrev--same-major-mode-p' for an example.
236
7313ccdb
RS
237The value of `dabbrev-friend-buffer-function' has an effect only if
238the value of `dabbrev-select-buffers-function' uses it. The function
239`dabbrev--select-buffers' is one function you can use here.
6163b3ba 240
bbf5eb28
RS
241A mode setting this variable should make it buffer local."
242 :type 'function
243 :group 'dabbrev)
6163b3ba 244
bbf5eb28 245(defcustom dabbrev-search-these-buffers-only nil
7313ccdb
RS
246 "If non-nil, a list of buffers which dabbrev should search.
247If this variable is non-nil, dabbrev will only look in these buffers.
248It will not even look in the current buffer if it is not a member of
249this list.")
e5167999 250
b578f267
EN
251;;----------------------------------------------------------------
252;; Internal variables
253;;----------------------------------------------------------------
2f790b20 254
6163b3ba
RS
255;; Last obarray of completions in `dabbrev-completion'
256(defvar dabbrev--last-obarray nil)
2f790b20 257
6163b3ba
RS
258;; Table of expansions seen so far
259(defvar dabbrev--last-table nil)
2f790b20 260
6163b3ba
RS
261;; Last string we tried to expand.
262(defvar dabbrev--last-abbreviation nil)
2f790b20 263
6163b3ba
RS
264;; Location last abbreviation began
265(defvar dabbrev--last-abbrev-location nil)
2f790b20 266
6163b3ba
RS
267;; Direction of last dabbrevs search
268(defvar dabbrev--last-direction 0)
2f790b20 269
6163b3ba
RS
270;; Last expansion of an abbreviation.
271(defvar dabbrev--last-expansion nil)
2f790b20 272
6163b3ba
RS
273;; Location the last expansion was found.
274(defvar dabbrev--last-expansion-location nil)
275
276;; The list of remaining buffers with the same mode as current buffer.
277(defvar dabbrev--friend-buffer-list nil)
278
279;; The buffer we looked in last.
280(defvar dabbrev--last-buffer nil)
281
282;; The buffer we found the expansion last time.
283(defvar dabbrev--last-buffer-found nil)
284
285;; The buffer we last did a completion in.
286(defvar dabbrev--last-completion-buffer nil)
287
dea5efcb
RS
288;; Non-nil means we should upcase
289;; when copying successive words.
290(defvar dabbrev--last-case-pattern nil)
291
df6eb420
RS
292;; Same as dabbrev-check-other-buffers, but is set for every expand.
293(defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
6163b3ba
RS
294
295;; The regexp for recognizing a character in an abbreviation.
296(defvar dabbrev--abbrev-char-regexp nil)
297
b578f267
EN
298;;----------------------------------------------------------------
299;; Macros
300;;----------------------------------------------------------------
6163b3ba
RS
301
302;;; Get the buffer that mini-buffer was activated from
303(defsubst dabbrev--minibuffer-origin ()
304 (car (cdr (buffer-list))))
305
7313ccdb
RS
306;; Make a list of some of the elements of LIST.
307;; Check each element of LIST, storing it temporarily in the
308;; variable ELEMENT, and include it in the result
309;; if CONDITION evaluates non-nil.
310(defmacro dabbrev-filter-elements (element list condition)
311 (` (let (dabbrev-result dabbrev-tail (, element))
312 (setq dabbrev-tail (, list))
313 (while dabbrev-tail
314 (setq (, element) (car dabbrev-tail))
315 (if (, condition)
316 (setq dabbrev-result (cons (, element) dabbrev-result)))
317 (setq dabbrev-tail (cdr dabbrev-tail)))
318 (nreverse dabbrev-result))))
319
b578f267
EN
320;;----------------------------------------------------------------
321;; Exported functions
322;;----------------------------------------------------------------
6163b3ba
RS
323
324;;;###autoload
325(define-key esc-map "/" 'dabbrev-expand)
7313ccdb 326;;;??? Do we want this?
6163b3ba 327;;;###autoload
f8b581fa 328(define-key esc-map [?\C-/] 'dabbrev-completion)
6163b3ba
RS
329
330;;;###autoload
331(defun dabbrev-completion (&optional arg)
332 "Completion on current word.
6163b3ba
RS
333Like \\[dabbrev-expand] but finds all expansions in the current buffer
334and presents suggestions for completion.
335
7313ccdb
RS
336With a prefix argument, it searches all buffers accepted by the
337function pointed out by `dabbrev-friend-buffer-function' to find the
dd1ae355
RS
338completions.
339
340If the prefix argument is 16 (which comes from C-u C-u),
341then it searches *all* buffers.
6163b3ba 342
7313ccdb
RS
343With no prefix argument, it reuses an old completion list
344if there is a suitable one already."
6163b3ba
RS
345
346 (interactive "*P")
df6eb420
RS
347 (dabbrev--reset-global-variables)
348 (let* ((dabbrev-check-other-buffers (and arg t))
349 (dabbrev-check-all-buffers
dd1ae355 350 (and arg (= (prefix-numeric-value arg) 16)))
6163b3ba
RS
351 (abbrev (dabbrev--abbrev-at-point))
352 (ignore-case-p (and (eval dabbrev-case-fold-search)
dea5efcb
RS
353 (or (not dabbrev-upcase-means-case-search)
354 (string= abbrev (downcase abbrev)))))
6163b3ba
RS
355 (my-obarray dabbrev--last-obarray)
356 init)
357 (save-excursion
358 (if (and (null arg)
359 my-obarray
360 (or (eq dabbrev--last-completion-buffer (current-buffer))
361 (and (window-minibuffer-p (selected-window))
362 (eq dabbrev--last-completion-buffer
363 (dabbrev--minibuffer-origin))))
364 dabbrev--last-abbreviation
365 (>= (length abbrev) (length dabbrev--last-abbreviation))
366 (string= dabbrev--last-abbreviation
367 (substring abbrev 0
368 (length dabbrev--last-abbreviation)))
369 (setq init (try-completion abbrev my-obarray)))
7313ccdb
RS
370 ;; We can reuse the existing completion list.
371 nil
6163b3ba
RS
372 ;;--------------------------------
373 ;; New abbreviation to expand.
374 ;;--------------------------------
6163b3ba
RS
375 (setq dabbrev--last-abbreviation abbrev)
376 ;; Find all expansion
377 (let ((completion-list
66db4b5b
RS
378 (dabbrev--find-all-expansions abbrev ignore-case-p))
379 (completion-ignore-case ignore-case-p))
6163b3ba
RS
380 ;; Make an obarray with all expansions
381 (setq my-obarray (make-vector (length completion-list) 0))
382 (or (> (length my-obarray) 0)
7313ccdb 383 (error "No dynamic expansion for \"%s\" found%s"
6163b3ba
RS
384 abbrev
385 (if dabbrev--check-other-buffers "" " in this-buffer")))
386 (cond
387 ((or (not ignore-case-p)
388 (not dabbrev-case-replace))
df6eb420
RS
389 (mapcar (function (lambda (string)
390 (intern string my-obarray)))
391 completion-list))
6163b3ba 392 ((string= abbrev (upcase abbrev))
df6eb420
RS
393 (mapcar (function (lambda (string)
394 (intern (upcase string) my-obarray)))
395 completion-list))
6163b3ba
RS
396 ((string= (substring abbrev 0 1)
397 (upcase (substring abbrev 0 1)))
df6eb420
RS
398 (mapcar (function (lambda (string)
399 (intern (capitalize string) my-obarray)))
400 completion-list))
6163b3ba 401 (t
df6eb420
RS
402 (mapcar (function (lambda (string)
403 (intern (downcase string) my-obarray)))
404 completion-list)))
6163b3ba
RS
405 (setq dabbrev--last-obarray my-obarray)
406 (setq dabbrev--last-completion-buffer (current-buffer))
407 ;; Find the longest common string.
408 (setq init (try-completion abbrev my-obarray)))))
409 ;;--------------------------------
410 ;; Let the user choose between the expansions
411 ;;--------------------------------
412 (or (stringp init)
413 (setq init abbrev))
414 (cond
415 ;; * Replace string fragment with matched common substring completion.
416 ((and (not (string-equal init ""))
417 (not (string-equal (downcase init) (downcase abbrev))))
418 (if (> (length (all-completions init my-obarray)) 1)
7313ccdb
RS
419 (message "Repeat `%s' to see all completions"
420 (key-description (this-command-keys)))
6163b3ba
RS
421 (message "The only possible completion"))
422 (dabbrev--substitute-expansion nil abbrev init))
423 (t
424 ;; * String is a common substring completion already. Make list.
425 (message "Making completion list...")
426 (with-output-to-temp-buffer " *Completions*"
427 (display-completion-list (all-completions init my-obarray)))
7313ccdb 428 (message "Making completion list...done")))
6163b3ba
RS
429 (and (window-minibuffer-p (selected-window))
430 (message nil))))
2f790b20
JB
431
432;;;###autoload
433(defun dabbrev-expand (arg)
434 "Expand previous word \"dynamically\".
2f790b20 435
6163b3ba
RS
436Expands to the most recent, preceding word for which this is a prefix.
437If no suitable preceding word is found, words following point are
438considered. If still no suitable word is found, then look in the
439buffers accepted by the function pointed out by variable
440`dabbrev-friend-buffer-function'.
2f790b20 441
7313ccdb 442A positive prefix argument, N, says to take the Nth backward *distinct*
6163b3ba 443possibility. A negative argument says search forward.
2f790b20
JB
444
445If the cursor has not moved from the end of the previous expansion and
446no argument is given, replace the previously-made expansion
6163b3ba
RS
447with the next possible expansion not yet tried.
448
449The variable `dabbrev-backward-only' may be used to limit the
450direction of search to backward if set non-nil.
451
7313ccdb 452See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
2f790b20 453 (interactive "*P")
dea5efcb
RS
454 (let (abbrev record-case-pattern
455 expansion old direction (orig-point (point)))
2f790b20
JB
456 ;; abbrev -- the abbrev to expand
457 ;; expansion -- the expansion found (eventually) or nil until then
458 ;; old -- the text currently in the buffer
459 ;; (the abbrev, or the previously-made expansion)
2f790b20
JB
460 (save-excursion
461 (if (and (null arg)
df6eb420
RS
462 (markerp dabbrev--last-abbrev-location)
463 (marker-position dabbrev--last-abbrev-location)
6163b3ba
RS
464 (or (eq last-command this-command)
465 (and (window-minibuffer-p (selected-window))
466 (= dabbrev--last-abbrev-location
467 (point)))))
7313ccdb 468 ;; Find a different expansion for the same abbrev as last time.
6163b3ba
RS
469 (progn
470 (setq abbrev dabbrev--last-abbreviation)
471 (setq old dabbrev--last-expansion)
472 (setq direction dabbrev--last-direction))
a8a2d6ca
KH
473 ;; If the user inserts a space after expanding
474 ;; and then asks to expand again, always fetch the next word.
475 (if (and (eq (preceding-char) ?\ )
476 (markerp dabbrev--last-abbrev-location)
477 (marker-position dabbrev--last-abbrev-location)
478 (= (point) (1+ dabbrev--last-abbrev-location)))
dea5efcb 479 (progn
a8a2d6ca
KH
480 ;; The "abbrev" to expand is just the space.
481 (setq abbrev " ")
482 (save-excursion
483 (if dabbrev--last-buffer
484 (set-buffer dabbrev--last-buffer))
485 ;; Find the end of the last "expansion" word.
486 (if (or (eq dabbrev--last-direction 1)
487 (and (eq dabbrev--last-direction 0)
488 (< dabbrev--last-expansion-location (point))))
489 (setq dabbrev--last-expansion-location
490 (+ dabbrev--last-expansion-location
491 (length dabbrev--last-expansion))))
492 (goto-char dabbrev--last-expansion-location)
493 ;; Take the following word, with intermediate separators,
494 ;; as our expansion this time.
495 (re-search-forward
496 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
497 (setq expansion
498 (buffer-substring dabbrev--last-expansion-location
499 (point)))
dea5efcb 500 (if dabbrev--last-case-pattern
07e47d0b 501 (setq expansion (upcase expansion)))
a8a2d6ca
KH
502
503 ;; Record the end of this expansion, in case we repeat this.
504 (setq dabbrev--last-expansion-location (point)))
505 ;; Indicate that dabbrev--last-expansion-location is
506 ;; at the end of the expansion.
507 (setq dabbrev--last-direction -1))
508
509 ;; We have a different abbrev to expand.
510 (dabbrev--reset-global-variables)
511 (setq direction (if (null arg)
512 (if dabbrev-backward-only 1 0)
513 (prefix-numeric-value arg)))
514 (setq abbrev (dabbrev--abbrev-at-point))
dea5efcb 515 (setq record-case-pattern t)
a8a2d6ca 516 (setq old nil)))
6163b3ba
RS
517
518 ;;--------------------------------
519 ;; Find the expansion
520 ;;--------------------------------
a8a2d6ca
KH
521 (or expansion
522 (setq expansion
523 (dabbrev--find-expansion abbrev direction
524 (and (eval dabbrev-case-fold-search)
525 (or (not dabbrev-upcase-means-case-search)
526 (string= abbrev (downcase abbrev))))))))
6163b3ba
RS
527 (cond
528 ((not expansion)
529 (dabbrev--reset-global-variables)
530 (if old
531 (save-excursion
4209f479 532 (setq buffer-undo-list (cons orig-point buffer-undo-list))
3132e115
RS
533 ;; Put back the original abbrev with its original case pattern.
534 (search-backward old)
535 (insert abbrev)
536 (delete-region (point) (+ (point) (length old)))))
7313ccdb 537 (error "No%s dynamic expansion for `%s' found"
6163b3ba
RS
538 (if old " further" "") abbrev))
539 (t
540 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found))
2f790b20 541 (progn
6163b3ba
RS
542 (message "Expansion found in '%s'"
543 (buffer-name dabbrev--last-buffer))
544 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
545 (message nil))
a8a2d6ca
KH
546 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
547 (null dabbrev--last-buffer))
548 (numberp dabbrev--last-expansion-location)
549 (and (> dabbrev--last-expansion-location (point))))
550 (setq dabbrev--last-expansion-location
551 (copy-marker dabbrev--last-expansion-location)))
2f790b20 552 ;; Success: stick it in and return.
4209f479 553 (setq buffer-undo-list (cons orig-point buffer-undo-list))
6163b3ba 554 (dabbrev--substitute-expansion old abbrev expansion)
dea5efcb
RS
555
556 ;; If we are not copying successive words now,
557 ;; set dabbrev--last-case-pattern.
558 (and record-case-pattern
559 (setq dabbrev--last-case-pattern
560 (and (eval dabbrev-case-fold-search)
561 (not dabbrev-upcase-means-case-search)
562 (equal abbrev (upcase abbrev)))))
563
2f790b20 564 ;; Save state for re-expand.
6163b3ba
RS
565 (setq dabbrev--last-expansion expansion)
566 (setq dabbrev--last-abbreviation abbrev)
567 (setq dabbrev--last-abbrev-location (point-marker))))))
568
b578f267
EN
569;;----------------------------------------------------------------
570;; Local functions
571;;----------------------------------------------------------------
6163b3ba 572
6163b3ba
RS
573;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
574(defun dabbrev--same-major-mode-p (other-buffer)
7313ccdb
RS
575 (eq major-mode
576 (save-excursion
577 (set-buffer other-buffer)
578 major-mode)))
6163b3ba
RS
579
580;;; Back over all abbrev type characters and then moves forward over
581;;; all skip characters.
582(defun dabbrev--goto-start-of-abbrev ()
583 ;; Move backwards over abbrev chars
584 (save-match-data
7313ccdb
RS
585 (if (not (bobp))
586 (progn
587 (forward-char -1)
588 (while (and (looking-at dabbrev--abbrev-char-regexp)
589 (not (bobp)))
590 (forward-char -1))
591 (or (looking-at dabbrev--abbrev-char-regexp)
592 (forward-char 1))))
6163b3ba
RS
593 (and dabbrev-abbrev-skip-leading-regexp
594 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
595 (forward-char 1)))))
596
7313ccdb 597;;; Extract the symbol at point to serve as abbreviation.
6163b3ba
RS
598(defun dabbrev--abbrev-at-point ()
599 ;; Check for error
a8a2d6ca
KH
600 (if (bobp)
601 (error "No possible abbreviation preceding point"))
6163b3ba
RS
602 ;; Return abbrev at point
603 (save-excursion
a8a2d6ca 604 ;; Record the end of the abbreviation.
6163b3ba 605 (setq dabbrev--last-abbrev-location (point))
a8a2d6ca
KH
606 ;; If we aren't right after an abbreviation,
607 ;; move point back to just after one.
608 ;; This is so the user can get successive words
609 ;; by typing the punctuation followed by M-/.
610 (save-match-data
611 (if (save-excursion
612 (forward-char -1)
613 (not (looking-at (concat "\\("
614 (or dabbrev-abbrev-char-regexp
615 "\\sw\\|\\s_")
616 "\\)+"))))
617 (if (re-search-backward (or dabbrev-abbrev-char-regexp
618 "\\sw\\|\\s_")
619 nil t)
620 (forward-char 1)
621 (error "No possible abbreviation preceding point"))))
622 ;; Now find the beginning of that one.
623 (dabbrev--goto-start-of-abbrev)
624 (buffer-substring dabbrev--last-abbrev-location
625 (point))))
6163b3ba
RS
626
627;;; Initializes all global variables
628(defun dabbrev--reset-global-variables ()
629 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
630 ;; must not be reset here.
631 (setq dabbrev--last-table nil
632 dabbrev--last-abbreviation nil
633 dabbrev--last-abbrev-location nil
634 dabbrev--last-direction nil
635 dabbrev--last-expansion nil
636 dabbrev--last-expansion-location nil
637 dabbrev--friend-buffer-list nil
638 dabbrev--last-buffer nil
639 dabbrev--last-buffer-found nil
640 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
641 "\\sw\\|\\s_")
df6eb420 642 dabbrev--check-other-buffers dabbrev-check-other-buffers))
6163b3ba
RS
643
644;;; Find all buffers that are considered "friends" according to the
645;;; function pointed out by dabbrev-friend-buffer-function.
646(defun dabbrev--select-buffers ()
647 (save-excursion
648 (and (window-minibuffer-p (selected-window))
649 (set-buffer (dabbrev--minibuffer-origin)))
650 (let ((orig-buffer (current-buffer)))
7313ccdb
RS
651 (dabbrev-filter-elements
652 buffer (buffer-list)
653 (and (not (eq orig-buffer buffer))
654 (boundp 'dabbrev-friend-buffer-function)
655 (funcall dabbrev-friend-buffer-function buffer))))))
656
657;;; Search for ABBREV, N times, normally looking forward,
658;;; but looking in reverse instead if REVERSE is non-nil.
6163b3ba
RS
659(defun dabbrev--try-find (abbrev reverse n ignore-case)
660 (save-excursion
df6eb420
RS
661 (save-restriction
662 (widen)
663 (let ((expansion nil))
664 (and dabbrev--last-expansion-location
665 (goto-char dabbrev--last-expansion-location))
666 (let ((case-fold-search ignore-case)
667 (count n))
668 (while (and (> count 0)
669 (setq expansion (dabbrev--search abbrev
670 reverse
671 ignore-case)))
672 (setq count (1- count))))
673 (and expansion
674 (setq dabbrev--last-expansion-location (point)))
675 expansion))))
6163b3ba
RS
676
677;;; Find all expansions of ABBREV
678(defun dabbrev--find-all-expansions (abbrev ignore-case)
679 (let ((all-expansions nil)
680 expansion)
681 (save-excursion
682 (goto-char (point-min))
683 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
df6eb420 684 (setq all-expansions (cons expansion all-expansions))))
6163b3ba
RS
685 all-expansions))
686
687(defun dabbrev--scanning-message ()
7313ccdb 688 (message "Scanning `%s'" (buffer-name (current-buffer))))
6163b3ba
RS
689
690;;; Find one occasion of ABBREV.
691;;; DIRECTION > 0 means look that many times backwards.
692;;; DIRECTION < 0 means look that many times forward.
693;;; DIRECTION = 0 means try both backward and forward.
694;;; IGNORE-CASE non-nil means ignore case when searching.
695(defun dabbrev--find-expansion (abbrev direction ignore-case)
696 (let (expansion)
697 (save-excursion
698 (cond
699 (dabbrev--last-buffer
700 (set-buffer dabbrev--last-buffer)
701 (dabbrev--scanning-message))
702 ((and (not dabbrev-search-these-buffers-only)
703 (window-minibuffer-p (selected-window)))
704 (set-buffer (dabbrev--minibuffer-origin))
705 ;; In the minibuffer-origin buffer we will only search from
706 ;; the top and down.
707 (goto-char (point-min))
708 (setq direction -1)
709 (dabbrev--scanning-message)))
710 (cond
711 ;; ------------------------------------------
712 ;; Look backwards
713 ;; ------------------------------------------
714 ((and (not dabbrev-search-these-buffers-only)
715 (>= direction 0)
716 (setq dabbrev--last-direction (min 1 direction))
717 (setq expansion (dabbrev--try-find abbrev t
718 (max 1 direction)
719 ignore-case)))
720 expansion)
721 ;; ------------------------------------------
722 ;; Look forward
723 ;; ------------------------------------------
724 ((and (or (not dabbrev-search-these-buffers-only)
725 dabbrev--last-buffer)
726 (<= direction 0)
727 (setq dabbrev--last-direction -1)
728 (setq expansion (dabbrev--try-find abbrev nil
729 (max 1 (- direction))
730 ignore-case)))
731 expansion)
732 ;; ------------------------------------------
733 ;; Look in other buffers.
734 ;; Start at (point-min) and look forward.
735 ;; ------------------------------------------
736 (t
737 (setq dabbrev--last-direction -1)
738 ;; Make sure that we should check other buffers
739 (or dabbrev--friend-buffer-list
740 dabbrev--last-buffer
741 (setq dabbrev--friend-buffer-list
742 (mapcar (function get-buffer)
743 dabbrev-search-these-buffers-only))
744 (not dabbrev--check-other-buffers)
745 (not (or (eq dabbrev--check-other-buffers t)
746 (progn
747 (setq dabbrev--check-other-buffers
7313ccdb 748 (y-or-n-p "Scan other buffers also? ")))))
6163b3ba
RS
749 (let* (friend-buffer-list non-friend-buffer-list)
750 (setq dabbrev--friend-buffer-list
751 (funcall dabbrev-select-buffers-function))
df6eb420 752 (if dabbrev-check-all-buffers
7313ccdb
RS
753 (setq non-friend-buffer-list
754 (nreverse
755 (dabbrev-filter-elements
756 buffer (buffer-list)
757 (not (memq buffer dabbrev--friend-buffer-list))))
758 dabbrev--friend-buffer-list
759 (append dabbrev--friend-buffer-list
760 non-friend-buffer-list)))))
d8f7e85a
RS
761 ;; Move buffers that are visible on the screen
762 ;; to the front of the list.
763 (if dabbrev--friend-buffer-list
764 (let ((w (next-window (selected-window))))
765 (while (not (eq w (selected-window)))
766 (setq dabbrev--friend-buffer-list
767 (cons (window-buffer w)
768 (delq (window-buffer w) dabbrev--friend-buffer-list)))
769 (setq w (next-window w)))))
6163b3ba
RS
770 ;; Walk through the buffers
771 (while (and (not expansion) dabbrev--friend-buffer-list)
772 (setq dabbrev--last-buffer
773 (car dabbrev--friend-buffer-list))
774 (setq dabbrev--friend-buffer-list
775 (cdr dabbrev--friend-buffer-list))
776 (set-buffer dabbrev--last-buffer)
777 (dabbrev--scanning-message)
778 (setq dabbrev--last-expansion-location (point-min))
779 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
780 expansion)))))
781
6163b3ba
RS
782(defun dabbrev--safe-replace-match (string &optional fixedcase literal)
783 (if (eq major-mode 'picture-mode)
784 (picture-replace-match string fixedcase literal)
785 (replace-match string fixedcase literal)))
786
787;;;----------------------------------------------------------------
788;;; Substitute the current string in buffer with the expansion
789;;; OLD is nil or the last expansion substring.
790;;; ABBREV is the abbreviation we are working with.
791;;; EXPANSION is the expansion substring.
792(defun dabbrev--substitute-expansion (old abbrev expansion)
793 ;;(undo-boundary)
794 (let ((use-case-replace (and (eval dabbrev-case-fold-search)
795 (or (not dabbrev-upcase-means-case-search)
796 (string= abbrev (downcase abbrev)))
797 (eval dabbrev-case-replace))))
798 (and nil use-case-replace
799 (setq old (concat abbrev (or old "")))
800 (setq expansion (concat abbrev expansion)))
65411629
RS
801 ;; If the expansion has mixed case
802 ;; and it is not simply a capitalized word,
803 ;; or if the abbrev has mixed case,
804 ;; and if the given abbrev's case pattern
66db4b5b
RS
805 ;; matches the start of the expansion,
806 ;; copy the expansion's case
807 ;; instead of downcasing all the rest.
65411629
RS
808 (let ((expansion-rest (substring expansion 1)))
809 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
810 (string= expansion-rest (upcase expansion-rest)))
811 (or (string= abbrev (downcase abbrev))
812 (string= abbrev (upcase abbrev)))))
813 (string= abbrev
814 (substring expansion 0 (length abbrev))))
815 (setq use-case-replace nil)))
07e47d0b
RS
816 (if (equal abbrev " ")
817 (setq use-case-replace nil))
818 (if use-case-replace
819 (setq expansion (downcase expansion)))
6163b3ba
RS
820 (if old
821 (save-excursion
822 (search-backward old))
823 ;;(store-match-data (list (point-marker) (point-marker)))
824 (search-backward abbrev))
825 ;; Make case of replacement conform to case of abbreviation
826 ;; provided (1) that kind of thing is enabled in this buffer
827 ;; and (2) the replacement itself is all lower case.
828 (dabbrev--safe-replace-match expansion
829 (not use-case-replace)
830 t)))
831
832
833;;;----------------------------------------------------------------
834;;; Search function used by dabbrevs library.
835
7313ccdb 836;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
6163b3ba 837;;; is t for reverse search, nil for forward. Variable dabbrev-limit
a7acbbe4 838;;; controls the maximum search region size. Third argument IGNORE-CASE
6163b3ba
RS
839;;; non-nil means treat case as insignificant while looking for a match
840;;; and when comparing with previous matches. Also if that's non-nil
841;;; and the match is found at the beginning of a sentence and is in
842;;; lower case except for the initial then it is converted to all lower
843;;; case for return.
844
845;;; Table of expansions already seen is examined in buffer
846;;; `dabbrev--last-table' so that only distinct possibilities are found
847;;; by dabbrev-re-expand.
848
849;;; Value is the expansion, or nil if not found.
850
851(defun dabbrev--search (abbrev reverse ignore-case)
852 (save-match-data
853 (let ((pattern1 (concat (regexp-quote abbrev)
854 "\\(" dabbrev--abbrev-char-regexp "\\)"))
855 (pattern2 (concat (regexp-quote abbrev)
856 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
857 (found-string nil))
858 ;; Limited search.
859 (save-restriction
860 (and dabbrev-limit
861 (narrow-to-region dabbrev--last-expansion-location
862 (+ (point)
863 (if reverse (- dabbrev-limit) dabbrev-limit))))
864 ;;--------------------------------
865 ;; Look for a distinct expansion, using dabbrev--last-table.
866 ;;--------------------------------
867 (while (and (not found-string)
868 (if reverse
869 (re-search-backward pattern1 nil t)
870 (re-search-forward pattern1 nil t)))
63d991d6
KH
871 (goto-char (match-beginning 0))
872 ;; In case we matched in the middle of a word,
873 ;; back up to start of word and verify we still match.
874 (dabbrev--goto-start-of-abbrev)
875
876 (if (not (looking-at pattern1))
877 nil
878 ;; We have a truly valid match. Find the end.
6163b3ba
RS
879 (re-search-forward pattern2)
880 (setq found-string
881 (buffer-substring (match-beginning 1) (match-end 1)))
882 (and ignore-case (setq found-string (downcase found-string)))
63d991d6 883 ;; Ignore this match if it's already in the table.
7313ccdb
RS
884 (if (dabbrev-filter-elements
885 table-string dabbrev--last-table
886 (string= found-string table-string))
63d991d6
KH
887 (setq found-string nil)))
888 ;; Prepare to continue searching.
6163b3ba
RS
889 (if reverse
890 (goto-char (match-beginning 0))
891 (goto-char (match-end 0))))
63d991d6
KH
892 ;; If we found something, use it.
893 (if found-string
894 ;; Put it into `dabbrev--last-table'
895 ;; and return it (either downcased, or as is).
896 (let ((result
897 (buffer-substring (match-beginning 0) (match-end 0))))
898 (setq dabbrev--last-table
899 (cons found-string dabbrev--last-table))
900 (if (and ignore-case (eval dabbrev-case-replace))
66db4b5b 901 result
63d991d6 902 result)))))))
6163b3ba 903
01987a6b 904(provide 'dabbrev)
7313ccdb 905
b578f267 906;;; dabbrev.el ends here