(comint-word): store-match-data => set-match-data.
[bpt/emacs.git] / lisp / dabbrev.el
CommitLineData
7313ccdb 1;;; dabbrev.el --- dynamic abbreviation package
b578f267 2
8b64df46 3;; Copyright (C) 1985, 86, 92, 94, 96, 1997 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>
7bd9ba70 91;; [ejb] Jay Berkenbilt <ejb@ql.org>
6163b3ba
RS
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 `$'.
79bb4872 122\(For example, in Makefiles.\)
6163b3ba 123
bbf5eb28
RS
124Set this to nil if no characters should be skipped."
125 :type '(choice regexp
126 (const :tag "off" nil))
127 :group 'dabbrev)
6163b3ba 128
bbf5eb28 129(defcustom dabbrev-case-fold-search 'case-fold-search
543abb4a 130 "*Control whether dabbrev searches should ignore case.
7313ccdb 131A value of nil means case is significant.
543abb4a
RS
132A value of `case-fold-search' means case is significant
133 if `case-fold-search' is nil.
134Any other non-nil version means case is not significant."
135 :type '(choice (const :tag "off" nil)
136 (const :tag "on" t)
fec1279b 137 (const :tag "like search" case-fold-search))
bbf5eb28 138 :group 'dabbrev)
6163b3ba 139
bbf5eb28 140(defcustom dabbrev-upcase-means-case-search nil
6163b3ba 141 "*The significance of an uppercase character in an abbreviation.
7313ccdb 142nil means case fold search, non-nil means case sensitive search.
6163b3ba 143
7313ccdb 144This variable has an effect only when the value of
543abb4a 145`dabbrev-case-fold-search' says to ignore case."
bbf5eb28
RS
146 :type 'boolean
147 :group 'dabbrev)
6163b3ba 148
bbf5eb28 149(defcustom dabbrev-case-replace 'case-replace
543abb4a
RS
150 "*Controls whether dabbrev preserves case when expanding the abbreviation.
151A value of nil means preserve case.
152A value of `case-replace' means preserve case if `case-replace' is nil.
153Any other non-nil version means do not preserve case.
6163b3ba 154
7313ccdb 155This variable has an effect only when the value of
543abb4a
RS
156`dabbrev-case-fold-search' specifies to ignore case."
157 :type '(choice (const :tag "off" nil)
158 (const :tag "on" t)
fec1279b 159 (const :tag "like M-x query-replace" case-replace))
bbf5eb28 160 :group 'dabbrev)
6163b3ba 161
bbf5eb28 162(defcustom dabbrev-abbrev-char-regexp nil
7313ccdb
RS
163 "*Regexp to recognize a character in an abbreviation or expansion.
164This regexp will be surrounded with \\\\( ... \\\\) when actually used.
6163b3ba 165
7313ccdb 166Set this variable to \"\\\\sw\" if you want ordinary words or
df6eb420
RS
167\"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
168syntax is \"symbol\" as well as those whose syntax is \"word\".
6163b3ba 169
df6eb420
RS
170The value nil has a special meaning: the abbreviation is from point to
171previous word-start, but the search is for symbols.
6163b3ba 172
7313ccdb 173For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
df6eb420
RS
174while `yes', `or', `no' and `p' are considered words. If this
175variable is nil, then expanding `yes-or-no-' looks for a symbol
7313ccdb
RS
176starting with or containing `no-'. If you set this variable to
177\"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
178`yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
179expanding `yes-or-no-' signals an error because `-' is not part of a word;
180but expanding `yes-or-no' looks for a word starting with `no'.
6163b3ba 181
bbf5eb28
RS
182The recommended value is \"\\\\sw\\\\|\\\\s_\"."
183 :type '(choice (const nil)
184 regexp)
185 :group 'dabbrev)
6163b3ba 186
bbf5eb28 187(defcustom dabbrev-check-all-buffers t
df6eb420 188 "*Non-nil means dabbrev package should search *all* buffers.
6163b3ba 189
df6eb420
RS
190Dabbrev always searches the current buffer first. Then, if
191`dabbrev-check-other-buffers' says so, it searches the buffers
192designated by `dabbrev-select-buffers-function'.
6163b3ba 193
df6eb420 194Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
7bd9ba70 195all the other buffers, except those named in `dabbrev-ignored-buffer-names'."
bbf5eb28
RS
196 :type 'boolean
197 :group 'dabbrev)
df6eb420 198
7bd9ba70
RS
199(defcustom dabbrev-ignored-buffer-names '("*Messages")
200 "*List of buffer names that dabbrev should not check."
201 :type '(repeat (string :tag "Buffer name"))
202 :group 'dabbrev)
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 351 (abbrev (dabbrev--abbrev-at-point))
543abb4a
RS
352 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
353 case-fold-search
354 dabbrev-case-fold-search)
355 (or (not dabbrev-upcase-means-case-search)
356 (string= abbrev (downcase abbrev)))))
6163b3ba
RS
357 (my-obarray dabbrev--last-obarray)
358 init)
359 (save-excursion
360 (if (and (null arg)
361 my-obarray
362 (or (eq dabbrev--last-completion-buffer (current-buffer))
363 (and (window-minibuffer-p (selected-window))
364 (eq dabbrev--last-completion-buffer
365 (dabbrev--minibuffer-origin))))
366 dabbrev--last-abbreviation
367 (>= (length abbrev) (length dabbrev--last-abbreviation))
368 (string= dabbrev--last-abbreviation
369 (substring abbrev 0
370 (length dabbrev--last-abbreviation)))
371 (setq init (try-completion abbrev my-obarray)))
7313ccdb
RS
372 ;; We can reuse the existing completion list.
373 nil
6163b3ba
RS
374 ;;--------------------------------
375 ;; New abbreviation to expand.
376 ;;--------------------------------
6163b3ba
RS
377 (setq dabbrev--last-abbreviation abbrev)
378 ;; Find all expansion
379 (let ((completion-list
66db4b5b
RS
380 (dabbrev--find-all-expansions abbrev ignore-case-p))
381 (completion-ignore-case ignore-case-p))
6163b3ba
RS
382 ;; Make an obarray with all expansions
383 (setq my-obarray (make-vector (length completion-list) 0))
384 (or (> (length my-obarray) 0)
7313ccdb 385 (error "No dynamic expansion for \"%s\" found%s"
6163b3ba
RS
386 abbrev
387 (if dabbrev--check-other-buffers "" " in this-buffer")))
388 (cond
389 ((or (not ignore-case-p)
390 (not dabbrev-case-replace))
df6eb420
RS
391 (mapcar (function (lambda (string)
392 (intern string my-obarray)))
393 completion-list))
6163b3ba 394 ((string= abbrev (upcase abbrev))
df6eb420
RS
395 (mapcar (function (lambda (string)
396 (intern (upcase string) my-obarray)))
397 completion-list))
6163b3ba
RS
398 ((string= (substring abbrev 0 1)
399 (upcase (substring abbrev 0 1)))
df6eb420
RS
400 (mapcar (function (lambda (string)
401 (intern (capitalize string) my-obarray)))
402 completion-list))
6163b3ba 403 (t
df6eb420
RS
404 (mapcar (function (lambda (string)
405 (intern (downcase string) my-obarray)))
406 completion-list)))
6163b3ba
RS
407 (setq dabbrev--last-obarray my-obarray)
408 (setq dabbrev--last-completion-buffer (current-buffer))
409 ;; Find the longest common string.
410 (setq init (try-completion abbrev my-obarray)))))
411 ;;--------------------------------
412 ;; Let the user choose between the expansions
413 ;;--------------------------------
414 (or (stringp init)
415 (setq init abbrev))
416 (cond
417 ;; * Replace string fragment with matched common substring completion.
418 ((and (not (string-equal init ""))
419 (not (string-equal (downcase init) (downcase abbrev))))
420 (if (> (length (all-completions init my-obarray)) 1)
7313ccdb
RS
421 (message "Repeat `%s' to see all completions"
422 (key-description (this-command-keys)))
6163b3ba
RS
423 (message "The only possible completion"))
424 (dabbrev--substitute-expansion nil abbrev init))
425 (t
426 ;; * String is a common substring completion already. Make list.
427 (message "Making completion list...")
428 (with-output-to-temp-buffer " *Completions*"
429 (display-completion-list (all-completions init my-obarray)))
7313ccdb 430 (message "Making completion list...done")))
6163b3ba
RS
431 (and (window-minibuffer-p (selected-window))
432 (message nil))))
2f790b20
JB
433
434;;;###autoload
435(defun dabbrev-expand (arg)
436 "Expand previous word \"dynamically\".
2f790b20 437
6163b3ba
RS
438Expands to the most recent, preceding word for which this is a prefix.
439If no suitable preceding word is found, words following point are
440considered. If still no suitable word is found, then look in the
441buffers accepted by the function pointed out by variable
442`dabbrev-friend-buffer-function'.
2f790b20 443
7313ccdb 444A positive prefix argument, N, says to take the Nth backward *distinct*
6163b3ba 445possibility. A negative argument says search forward.
2f790b20
JB
446
447If the cursor has not moved from the end of the previous expansion and
448no argument is given, replace the previously-made expansion
6163b3ba
RS
449with the next possible expansion not yet tried.
450
451The variable `dabbrev-backward-only' may be used to limit the
452direction of search to backward if set non-nil.
453
7313ccdb 454See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
2f790b20 455 (interactive "*P")
dea5efcb
RS
456 (let (abbrev record-case-pattern
457 expansion old direction (orig-point (point)))
2f790b20
JB
458 ;; abbrev -- the abbrev to expand
459 ;; expansion -- the expansion found (eventually) or nil until then
460 ;; old -- the text currently in the buffer
461 ;; (the abbrev, or the previously-made expansion)
2f790b20
JB
462 (save-excursion
463 (if (and (null arg)
df6eb420
RS
464 (markerp dabbrev--last-abbrev-location)
465 (marker-position dabbrev--last-abbrev-location)
6163b3ba
RS
466 (or (eq last-command this-command)
467 (and (window-minibuffer-p (selected-window))
468 (= dabbrev--last-abbrev-location
469 (point)))))
7313ccdb 470 ;; Find a different expansion for the same abbrev as last time.
6163b3ba
RS
471 (progn
472 (setq abbrev dabbrev--last-abbreviation)
473 (setq old dabbrev--last-expansion)
474 (setq direction dabbrev--last-direction))
a8a2d6ca
KH
475 ;; If the user inserts a space after expanding
476 ;; and then asks to expand again, always fetch the next word.
477 (if (and (eq (preceding-char) ?\ )
478 (markerp dabbrev--last-abbrev-location)
479 (marker-position dabbrev--last-abbrev-location)
480 (= (point) (1+ dabbrev--last-abbrev-location)))
dea5efcb 481 (progn
a8a2d6ca
KH
482 ;; The "abbrev" to expand is just the space.
483 (setq abbrev " ")
484 (save-excursion
485 (if dabbrev--last-buffer
486 (set-buffer dabbrev--last-buffer))
487 ;; Find the end of the last "expansion" word.
488 (if (or (eq dabbrev--last-direction 1)
489 (and (eq dabbrev--last-direction 0)
490 (< dabbrev--last-expansion-location (point))))
491 (setq dabbrev--last-expansion-location
492 (+ dabbrev--last-expansion-location
493 (length dabbrev--last-expansion))))
494 (goto-char dabbrev--last-expansion-location)
495 ;; Take the following word, with intermediate separators,
496 ;; as our expansion this time.
497 (re-search-forward
498 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
6f0b000c
RS
499 (setq expansion (buffer-substring-no-properties
500 dabbrev--last-expansion-location (point)))
dea5efcb 501 (if dabbrev--last-case-pattern
07e47d0b 502 (setq expansion (upcase expansion)))
a8a2d6ca
KH
503
504 ;; Record the end of this expansion, in case we repeat this.
505 (setq dabbrev--last-expansion-location (point)))
506 ;; Indicate that dabbrev--last-expansion-location is
507 ;; at the end of the expansion.
508 (setq dabbrev--last-direction -1))
509
510 ;; We have a different abbrev to expand.
511 (dabbrev--reset-global-variables)
512 (setq direction (if (null arg)
513 (if dabbrev-backward-only 1 0)
514 (prefix-numeric-value arg)))
515 (setq abbrev (dabbrev--abbrev-at-point))
dea5efcb 516 (setq record-case-pattern t)
a8a2d6ca 517 (setq old nil)))
6163b3ba
RS
518
519 ;;--------------------------------
520 ;; Find the expansion
521 ;;--------------------------------
a8a2d6ca
KH
522 (or expansion
523 (setq expansion
524 (dabbrev--find-expansion abbrev direction
543abb4a
RS
525 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
526 case-fold-search
527 dabbrev-case-fold-search)
a8a2d6ca
KH
528 (or (not dabbrev-upcase-means-case-search)
529 (string= abbrev (downcase abbrev))))))))
6163b3ba
RS
530 (cond
531 ((not expansion)
532 (dabbrev--reset-global-variables)
533 (if old
534 (save-excursion
4209f479 535 (setq buffer-undo-list (cons orig-point buffer-undo-list))
3132e115
RS
536 ;; Put back the original abbrev with its original case pattern.
537 (search-backward old)
538 (insert abbrev)
539 (delete-region (point) (+ (point) (length old)))))
7313ccdb 540 (error "No%s dynamic expansion for `%s' found"
6163b3ba
RS
541 (if old " further" "") abbrev))
542 (t
543 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found))
2f790b20 544 (progn
6163b3ba
RS
545 (message "Expansion found in '%s'"
546 (buffer-name dabbrev--last-buffer))
547 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
548 (message nil))
a8a2d6ca
KH
549 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
550 (null dabbrev--last-buffer))
551 (numberp dabbrev--last-expansion-location)
552 (and (> dabbrev--last-expansion-location (point))))
553 (setq dabbrev--last-expansion-location
554 (copy-marker dabbrev--last-expansion-location)))
2f790b20 555 ;; Success: stick it in and return.
4209f479 556 (setq buffer-undo-list (cons orig-point buffer-undo-list))
6163b3ba 557 (dabbrev--substitute-expansion old abbrev expansion)
dea5efcb
RS
558
559 ;; If we are not copying successive words now,
560 ;; set dabbrev--last-case-pattern.
561 (and record-case-pattern
562 (setq dabbrev--last-case-pattern
543abb4a
RS
563 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
564 case-fold-search
565 dabbrev-case-fold-search)
dea5efcb
RS
566 (not dabbrev-upcase-means-case-search)
567 (equal abbrev (upcase abbrev)))))
568
2f790b20 569 ;; Save state for re-expand.
6163b3ba
RS
570 (setq dabbrev--last-expansion expansion)
571 (setq dabbrev--last-abbreviation abbrev)
572 (setq dabbrev--last-abbrev-location (point-marker))))))
573
b578f267
EN
574;;----------------------------------------------------------------
575;; Local functions
576;;----------------------------------------------------------------
6163b3ba 577
6163b3ba
RS
578;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
579(defun dabbrev--same-major-mode-p (other-buffer)
7313ccdb
RS
580 (eq major-mode
581 (save-excursion
582 (set-buffer other-buffer)
583 major-mode)))
6163b3ba
RS
584
585;;; Back over all abbrev type characters and then moves forward over
586;;; all skip characters.
587(defun dabbrev--goto-start-of-abbrev ()
588 ;; Move backwards over abbrev chars
589 (save-match-data
7313ccdb
RS
590 (if (not (bobp))
591 (progn
592 (forward-char -1)
593 (while (and (looking-at dabbrev--abbrev-char-regexp)
594 (not (bobp)))
595 (forward-char -1))
596 (or (looking-at dabbrev--abbrev-char-regexp)
597 (forward-char 1))))
6163b3ba
RS
598 (and dabbrev-abbrev-skip-leading-regexp
599 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
600 (forward-char 1)))))
601
7313ccdb 602;;; Extract the symbol at point to serve as abbreviation.
6163b3ba
RS
603(defun dabbrev--abbrev-at-point ()
604 ;; Check for error
a8a2d6ca
KH
605 (if (bobp)
606 (error "No possible abbreviation preceding point"))
6163b3ba
RS
607 ;; Return abbrev at point
608 (save-excursion
a8a2d6ca 609 ;; Record the end of the abbreviation.
6163b3ba 610 (setq dabbrev--last-abbrev-location (point))
a8a2d6ca
KH
611 ;; If we aren't right after an abbreviation,
612 ;; move point back to just after one.
613 ;; This is so the user can get successive words
614 ;; by typing the punctuation followed by M-/.
615 (save-match-data
616 (if (save-excursion
617 (forward-char -1)
618 (not (looking-at (concat "\\("
619 (or dabbrev-abbrev-char-regexp
620 "\\sw\\|\\s_")
621 "\\)+"))))
622 (if (re-search-backward (or dabbrev-abbrev-char-regexp
623 "\\sw\\|\\s_")
624 nil t)
625 (forward-char 1)
626 (error "No possible abbreviation preceding point"))))
627 ;; Now find the beginning of that one.
628 (dabbrev--goto-start-of-abbrev)
6f0b000c
RS
629 (buffer-substring-no-properties
630 dabbrev--last-abbrev-location (point))))
6163b3ba
RS
631
632;;; Initializes all global variables
633(defun dabbrev--reset-global-variables ()
634 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
635 ;; must not be reset here.
636 (setq dabbrev--last-table nil
637 dabbrev--last-abbreviation nil
638 dabbrev--last-abbrev-location nil
639 dabbrev--last-direction nil
640 dabbrev--last-expansion nil
641 dabbrev--last-expansion-location nil
642 dabbrev--friend-buffer-list nil
643 dabbrev--last-buffer nil
644 dabbrev--last-buffer-found nil
645 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
646 "\\sw\\|\\s_")
df6eb420 647 dabbrev--check-other-buffers dabbrev-check-other-buffers))
6163b3ba
RS
648
649;;; Find all buffers that are considered "friends" according to the
650;;; function pointed out by dabbrev-friend-buffer-function.
651(defun dabbrev--select-buffers ()
652 (save-excursion
653 (and (window-minibuffer-p (selected-window))
654 (set-buffer (dabbrev--minibuffer-origin)))
655 (let ((orig-buffer (current-buffer)))
7313ccdb
RS
656 (dabbrev-filter-elements
657 buffer (buffer-list)
658 (and (not (eq orig-buffer buffer))
659 (boundp 'dabbrev-friend-buffer-function)
660 (funcall dabbrev-friend-buffer-function buffer))))))
661
662;;; Search for ABBREV, N times, normally looking forward,
663;;; but looking in reverse instead if REVERSE is non-nil.
6163b3ba
RS
664(defun dabbrev--try-find (abbrev reverse n ignore-case)
665 (save-excursion
df6eb420
RS
666 (save-restriction
667 (widen)
668 (let ((expansion nil))
669 (and dabbrev--last-expansion-location
670 (goto-char dabbrev--last-expansion-location))
671 (let ((case-fold-search ignore-case)
672 (count n))
673 (while (and (> count 0)
674 (setq expansion (dabbrev--search abbrev
675 reverse
676 ignore-case)))
677 (setq count (1- count))))
678 (and expansion
679 (setq dabbrev--last-expansion-location (point)))
680 expansion))))
6163b3ba
RS
681
682;;; Find all expansions of ABBREV
683(defun dabbrev--find-all-expansions (abbrev ignore-case)
684 (let ((all-expansions nil)
685 expansion)
686 (save-excursion
687 (goto-char (point-min))
688 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
df6eb420 689 (setq all-expansions (cons expansion all-expansions))))
6163b3ba
RS
690 all-expansions))
691
692(defun dabbrev--scanning-message ()
7313ccdb 693 (message "Scanning `%s'" (buffer-name (current-buffer))))
6163b3ba
RS
694
695;;; Find one occasion of ABBREV.
696;;; DIRECTION > 0 means look that many times backwards.
697;;; DIRECTION < 0 means look that many times forward.
698;;; DIRECTION = 0 means try both backward and forward.
699;;; IGNORE-CASE non-nil means ignore case when searching.
700(defun dabbrev--find-expansion (abbrev direction ignore-case)
701 (let (expansion)
702 (save-excursion
703 (cond
704 (dabbrev--last-buffer
705 (set-buffer dabbrev--last-buffer)
706 (dabbrev--scanning-message))
707 ((and (not dabbrev-search-these-buffers-only)
708 (window-minibuffer-p (selected-window)))
709 (set-buffer (dabbrev--minibuffer-origin))
710 ;; In the minibuffer-origin buffer we will only search from
711 ;; the top and down.
712 (goto-char (point-min))
713 (setq direction -1)
714 (dabbrev--scanning-message)))
715 (cond
716 ;; ------------------------------------------
717 ;; Look backwards
718 ;; ------------------------------------------
719 ((and (not dabbrev-search-these-buffers-only)
720 (>= direction 0)
721 (setq dabbrev--last-direction (min 1 direction))
722 (setq expansion (dabbrev--try-find abbrev t
723 (max 1 direction)
724 ignore-case)))
725 expansion)
726 ;; ------------------------------------------
727 ;; Look forward
728 ;; ------------------------------------------
729 ((and (or (not dabbrev-search-these-buffers-only)
730 dabbrev--last-buffer)
731 (<= direction 0)
732 (setq dabbrev--last-direction -1)
733 (setq expansion (dabbrev--try-find abbrev nil
734 (max 1 (- direction))
735 ignore-case)))
736 expansion)
737 ;; ------------------------------------------
738 ;; Look in other buffers.
739 ;; Start at (point-min) and look forward.
740 ;; ------------------------------------------
741 (t
742 (setq dabbrev--last-direction -1)
743 ;; Make sure that we should check other buffers
744 (or dabbrev--friend-buffer-list
745 dabbrev--last-buffer
746 (setq dabbrev--friend-buffer-list
747 (mapcar (function get-buffer)
748 dabbrev-search-these-buffers-only))
749 (not dabbrev--check-other-buffers)
750 (not (or (eq dabbrev--check-other-buffers t)
751 (progn
752 (setq dabbrev--check-other-buffers
7313ccdb 753 (y-or-n-p "Scan other buffers also? ")))))
6163b3ba
RS
754 (let* (friend-buffer-list non-friend-buffer-list)
755 (setq dabbrev--friend-buffer-list
756 (funcall dabbrev-select-buffers-function))
df6eb420 757 (if dabbrev-check-all-buffers
7313ccdb
RS
758 (setq non-friend-buffer-list
759 (nreverse
760 (dabbrev-filter-elements
761 buffer (buffer-list)
7bd9ba70
RS
762 (and (not (member (buffer-name buffer)
763 dabbrev-ignored-buffer-names))
764 (not (memq buffer dabbrev--friend-buffer-list)))))
7313ccdb
RS
765 dabbrev--friend-buffer-list
766 (append dabbrev--friend-buffer-list
767 non-friend-buffer-list)))))
d8f7e85a
RS
768 ;; Move buffers that are visible on the screen
769 ;; to the front of the list.
770 (if dabbrev--friend-buffer-list
771 (let ((w (next-window (selected-window))))
772 (while (not (eq w (selected-window)))
773 (setq dabbrev--friend-buffer-list
774 (cons (window-buffer w)
775 (delq (window-buffer w) dabbrev--friend-buffer-list)))
776 (setq w (next-window w)))))
6163b3ba
RS
777 ;; Walk through the buffers
778 (while (and (not expansion) dabbrev--friend-buffer-list)
779 (setq dabbrev--last-buffer
780 (car dabbrev--friend-buffer-list))
781 (setq dabbrev--friend-buffer-list
782 (cdr dabbrev--friend-buffer-list))
783 (set-buffer dabbrev--last-buffer)
784 (dabbrev--scanning-message)
785 (setq dabbrev--last-expansion-location (point-min))
786 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
787 expansion)))))
788
6163b3ba
RS
789(defun dabbrev--safe-replace-match (string &optional fixedcase literal)
790 (if (eq major-mode 'picture-mode)
791 (picture-replace-match string fixedcase literal)
792 (replace-match string fixedcase literal)))
793
794;;;----------------------------------------------------------------
795;;; Substitute the current string in buffer with the expansion
796;;; OLD is nil or the last expansion substring.
797;;; ABBREV is the abbreviation we are working with.
798;;; EXPANSION is the expansion substring.
799(defun dabbrev--substitute-expansion (old abbrev expansion)
800 ;;(undo-boundary)
543abb4a
RS
801 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
802 case-fold-search
803 dabbrev-case-fold-search)
6163b3ba
RS
804 (or (not dabbrev-upcase-means-case-search)
805 (string= abbrev (downcase abbrev)))
543abb4a
RS
806 (if (eq dabbrev-case-replace 'case-replace)
807 case-replace
808 dabbrev-case-replace))))
6163b3ba
RS
809 (and nil use-case-replace
810 (setq old (concat abbrev (or old "")))
811 (setq expansion (concat abbrev expansion)))
65411629
RS
812 ;; If the expansion has mixed case
813 ;; and it is not simply a capitalized word,
814 ;; or if the abbrev has mixed case,
815 ;; and if the given abbrev's case pattern
66db4b5b
RS
816 ;; matches the start of the expansion,
817 ;; copy the expansion's case
818 ;; instead of downcasing all the rest.
65411629
RS
819 (let ((expansion-rest (substring expansion 1)))
820 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
821 (string= expansion-rest (upcase expansion-rest)))
822 (or (string= abbrev (downcase abbrev))
823 (string= abbrev (upcase abbrev)))))
824 (string= abbrev
825 (substring expansion 0 (length abbrev))))
826 (setq use-case-replace nil)))
07e47d0b
RS
827 (if (equal abbrev " ")
828 (setq use-case-replace nil))
829 (if use-case-replace
830 (setq expansion (downcase expansion)))
6163b3ba
RS
831 (if old
832 (save-excursion
833 (search-backward old))
834 ;;(store-match-data (list (point-marker) (point-marker)))
835 (search-backward abbrev))
836 ;; Make case of replacement conform to case of abbreviation
837 ;; provided (1) that kind of thing is enabled in this buffer
838 ;; and (2) the replacement itself is all lower case.
839 (dabbrev--safe-replace-match expansion
840 (not use-case-replace)
841 t)))
842
843
844;;;----------------------------------------------------------------
845;;; Search function used by dabbrevs library.
846
7313ccdb 847;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
6163b3ba 848;;; is t for reverse search, nil for forward. Variable dabbrev-limit
a7acbbe4 849;;; controls the maximum search region size. Third argument IGNORE-CASE
6163b3ba
RS
850;;; non-nil means treat case as insignificant while looking for a match
851;;; and when comparing with previous matches. Also if that's non-nil
852;;; and the match is found at the beginning of a sentence and is in
853;;; lower case except for the initial then it is converted to all lower
854;;; case for return.
855
856;;; Table of expansions already seen is examined in buffer
857;;; `dabbrev--last-table' so that only distinct possibilities are found
858;;; by dabbrev-re-expand.
859
860;;; Value is the expansion, or nil if not found.
861
862(defun dabbrev--search (abbrev reverse ignore-case)
863 (save-match-data
864 (let ((pattern1 (concat (regexp-quote abbrev)
865 "\\(" dabbrev--abbrev-char-regexp "\\)"))
866 (pattern2 (concat (regexp-quote abbrev)
867 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
868 (found-string nil))
869 ;; Limited search.
870 (save-restriction
871 (and dabbrev-limit
872 (narrow-to-region dabbrev--last-expansion-location
873 (+ (point)
874 (if reverse (- dabbrev-limit) dabbrev-limit))))
875 ;;--------------------------------
876 ;; Look for a distinct expansion, using dabbrev--last-table.
877 ;;--------------------------------
878 (while (and (not found-string)
879 (if reverse
880 (re-search-backward pattern1 nil t)
881 (re-search-forward pattern1 nil t)))
63d991d6
KH
882 (goto-char (match-beginning 0))
883 ;; In case we matched in the middle of a word,
884 ;; back up to start of word and verify we still match.
885 (dabbrev--goto-start-of-abbrev)
886
887 (if (not (looking-at pattern1))
888 nil
889 ;; We have a truly valid match. Find the end.
6163b3ba 890 (re-search-forward pattern2)
6f0b000c
RS
891 (setq found-string (buffer-substring-no-properties
892 (match-beginning 1) (match-end 1)))
6163b3ba 893 (and ignore-case (setq found-string (downcase found-string)))
63d991d6 894 ;; Ignore this match if it's already in the table.
7313ccdb
RS
895 (if (dabbrev-filter-elements
896 table-string dabbrev--last-table
897 (string= found-string table-string))
63d991d6
KH
898 (setq found-string nil)))
899 ;; Prepare to continue searching.
6163b3ba
RS
900 (if reverse
901 (goto-char (match-beginning 0))
902 (goto-char (match-end 0))))
63d991d6
KH
903 ;; If we found something, use it.
904 (if found-string
905 ;; Put it into `dabbrev--last-table'
906 ;; and return it (either downcased, or as is).
6f0b000c
RS
907 (let ((result (buffer-substring-no-properties
908 (match-beginning 0) (match-end 0))))
63d991d6
KH
909 (setq dabbrev--last-table
910 (cons found-string dabbrev--last-table))
911 (if (and ignore-case (eval dabbrev-case-replace))
66db4b5b 912 result
63d991d6 913 result)))))))
6163b3ba 914
01987a6b 915(provide 'dabbrev)
7313ccdb 916
b578f267 917;;; dabbrev.el ends here