*** empty log message ***
[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"))
cd32a7ba
DN
202 :group 'dabbrev
203 :version "20.3")
7bd9ba70 204
bbf5eb28 205(defcustom dabbrev-check-other-buffers t
6163b3ba 206 "*Should \\[dabbrev-expand] look in other buffers?\
6163b3ba 207
df6eb420
RS
208nil: Don't look in other buffers.
209t: Also look for expansions in the buffers pointed out by
210 `dabbrev-select-buffers-function'.
211Anything else: When we can't find any more expansions in
212the current buffer, then ask the user whether to look in other
213buffers too.
214
bbf5eb28
RS
215The default value is t."
216 :type '(choice (const :tag "off" nil)
217 (const :tag "on" t)
218 (const :tag "ask" other))
219 :group 'dabbrev)
6163b3ba
RS
220
221;; I guess setting this to a function that selects all C- or C++-
222;; mode buffers would be a good choice for a debugging buffer,
223;; when debugging C- or C++-code.
224(defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
225 "A function that selects buffers that should be searched by dabbrev.
6163b3ba
RS
226The function should take no arguments and return a list of buffers to
227search for expansions. Have a look at `dabbrev--select-buffers' for
228an example.
229
230A mode setting this variable should make it buffer local.")
231
bbf5eb28 232(defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
df6eb420 233 "*A function to decide whether dabbrev should search OTHER-BUFFER.
6163b3ba
RS
234The function should take one argument, OTHER-BUFFER, and return
235non-nil if that buffer should be searched. Have a look at
236`dabbrev--same-major-mode-p' for an example.
237
7313ccdb
RS
238The value of `dabbrev-friend-buffer-function' has an effect only if
239the value of `dabbrev-select-buffers-function' uses it. The function
240`dabbrev--select-buffers' is one function you can use here.
6163b3ba 241
bbf5eb28
RS
242A mode setting this variable should make it buffer local."
243 :type 'function
244 :group 'dabbrev)
6163b3ba 245
bbf5eb28 246(defcustom dabbrev-search-these-buffers-only nil
7313ccdb
RS
247 "If non-nil, a list of buffers which dabbrev should search.
248If this variable is non-nil, dabbrev will only look in these buffers.
249It will not even look in the current buffer if it is not a member of
250this list.")
e5167999 251
b578f267
EN
252;;----------------------------------------------------------------
253;; Internal variables
254;;----------------------------------------------------------------
2f790b20 255
6163b3ba
RS
256;; Last obarray of completions in `dabbrev-completion'
257(defvar dabbrev--last-obarray nil)
2f790b20 258
6163b3ba
RS
259;; Table of expansions seen so far
260(defvar dabbrev--last-table nil)
2f790b20 261
6163b3ba
RS
262;; Last string we tried to expand.
263(defvar dabbrev--last-abbreviation nil)
2f790b20 264
6163b3ba
RS
265;; Location last abbreviation began
266(defvar dabbrev--last-abbrev-location nil)
2f790b20 267
6163b3ba
RS
268;; Direction of last dabbrevs search
269(defvar dabbrev--last-direction 0)
2f790b20 270
6163b3ba
RS
271;; Last expansion of an abbreviation.
272(defvar dabbrev--last-expansion nil)
2f790b20 273
6163b3ba
RS
274;; Location the last expansion was found.
275(defvar dabbrev--last-expansion-location nil)
276
277;; The list of remaining buffers with the same mode as current buffer.
278(defvar dabbrev--friend-buffer-list nil)
279
280;; The buffer we looked in last.
281(defvar dabbrev--last-buffer nil)
282
283;; The buffer we found the expansion last time.
284(defvar dabbrev--last-buffer-found nil)
285
286;; The buffer we last did a completion in.
287(defvar dabbrev--last-completion-buffer nil)
288
dea5efcb
RS
289;; Non-nil means we should upcase
290;; when copying successive words.
291(defvar dabbrev--last-case-pattern nil)
292
df6eb420
RS
293;; Same as dabbrev-check-other-buffers, but is set for every expand.
294(defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
6163b3ba
RS
295
296;; The regexp for recognizing a character in an abbreviation.
297(defvar dabbrev--abbrev-char-regexp nil)
298
b578f267
EN
299;;----------------------------------------------------------------
300;; Macros
301;;----------------------------------------------------------------
6163b3ba
RS
302
303;;; Get the buffer that mini-buffer was activated from
304(defsubst dabbrev--minibuffer-origin ()
305 (car (cdr (buffer-list))))
306
7313ccdb
RS
307;; Make a list of some of the elements of LIST.
308;; Check each element of LIST, storing it temporarily in the
309;; variable ELEMENT, and include it in the result
310;; if CONDITION evaluates non-nil.
311(defmacro dabbrev-filter-elements (element list condition)
312 (` (let (dabbrev-result dabbrev-tail (, element))
313 (setq dabbrev-tail (, list))
314 (while dabbrev-tail
315 (setq (, element) (car dabbrev-tail))
316 (if (, condition)
317 (setq dabbrev-result (cons (, element) dabbrev-result)))
318 (setq dabbrev-tail (cdr dabbrev-tail)))
319 (nreverse dabbrev-result))))
320
b578f267
EN
321;;----------------------------------------------------------------
322;; Exported functions
323;;----------------------------------------------------------------
6163b3ba
RS
324
325;;;###autoload
326(define-key esc-map "/" 'dabbrev-expand)
7313ccdb 327;;;??? Do we want this?
6163b3ba 328;;;###autoload
f8b581fa 329(define-key esc-map [?\C-/] 'dabbrev-completion)
6163b3ba
RS
330
331;;;###autoload
332(defun dabbrev-completion (&optional arg)
333 "Completion on current word.
6163b3ba
RS
334Like \\[dabbrev-expand] but finds all expansions in the current buffer
335and presents suggestions for completion.
336
7313ccdb
RS
337With a prefix argument, it searches all buffers accepted by the
338function pointed out by `dabbrev-friend-buffer-function' to find the
dd1ae355
RS
339completions.
340
341If the prefix argument is 16 (which comes from C-u C-u),
342then it searches *all* buffers.
6163b3ba 343
7313ccdb
RS
344With no prefix argument, it reuses an old completion list
345if there is a suitable one already."
6163b3ba
RS
346
347 (interactive "*P")
df6eb420
RS
348 (dabbrev--reset-global-variables)
349 (let* ((dabbrev-check-other-buffers (and arg t))
350 (dabbrev-check-all-buffers
dd1ae355 351 (and arg (= (prefix-numeric-value arg) 16)))
6163b3ba 352 (abbrev (dabbrev--abbrev-at-point))
543abb4a
RS
353 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
354 case-fold-search
355 dabbrev-case-fold-search)
356 (or (not dabbrev-upcase-means-case-search)
357 (string= abbrev (downcase abbrev)))))
6163b3ba
RS
358 (my-obarray dabbrev--last-obarray)
359 init)
360 (save-excursion
361 (if (and (null arg)
362 my-obarray
363 (or (eq dabbrev--last-completion-buffer (current-buffer))
364 (and (window-minibuffer-p (selected-window))
365 (eq dabbrev--last-completion-buffer
366 (dabbrev--minibuffer-origin))))
367 dabbrev--last-abbreviation
368 (>= (length abbrev) (length dabbrev--last-abbreviation))
369 (string= dabbrev--last-abbreviation
370 (substring abbrev 0
371 (length dabbrev--last-abbreviation)))
372 (setq init (try-completion abbrev my-obarray)))
7313ccdb
RS
373 ;; We can reuse the existing completion list.
374 nil
6163b3ba
RS
375 ;;--------------------------------
376 ;; New abbreviation to expand.
377 ;;--------------------------------
6163b3ba
RS
378 (setq dabbrev--last-abbreviation abbrev)
379 ;; Find all expansion
380 (let ((completion-list
66db4b5b
RS
381 (dabbrev--find-all-expansions abbrev ignore-case-p))
382 (completion-ignore-case ignore-case-p))
6163b3ba
RS
383 ;; Make an obarray with all expansions
384 (setq my-obarray (make-vector (length completion-list) 0))
385 (or (> (length my-obarray) 0)
7313ccdb 386 (error "No dynamic expansion for \"%s\" found%s"
6163b3ba
RS
387 abbrev
388 (if dabbrev--check-other-buffers "" " in this-buffer")))
389 (cond
390 ((or (not ignore-case-p)
391 (not dabbrev-case-replace))
df6eb420
RS
392 (mapcar (function (lambda (string)
393 (intern string my-obarray)))
394 completion-list))
6163b3ba 395 ((string= abbrev (upcase abbrev))
df6eb420
RS
396 (mapcar (function (lambda (string)
397 (intern (upcase string) my-obarray)))
398 completion-list))
6163b3ba
RS
399 ((string= (substring abbrev 0 1)
400 (upcase (substring abbrev 0 1)))
df6eb420
RS
401 (mapcar (function (lambda (string)
402 (intern (capitalize string) my-obarray)))
403 completion-list))
6163b3ba 404 (t
df6eb420
RS
405 (mapcar (function (lambda (string)
406 (intern (downcase string) my-obarray)))
407 completion-list)))
6163b3ba
RS
408 (setq dabbrev--last-obarray my-obarray)
409 (setq dabbrev--last-completion-buffer (current-buffer))
410 ;; Find the longest common string.
411 (setq init (try-completion abbrev my-obarray)))))
412 ;;--------------------------------
413 ;; Let the user choose between the expansions
414 ;;--------------------------------
415 (or (stringp init)
416 (setq init abbrev))
417 (cond
418 ;; * Replace string fragment with matched common substring completion.
419 ((and (not (string-equal init ""))
420 (not (string-equal (downcase init) (downcase abbrev))))
421 (if (> (length (all-completions init my-obarray)) 1)
7313ccdb
RS
422 (message "Repeat `%s' to see all completions"
423 (key-description (this-command-keys)))
6163b3ba
RS
424 (message "The only possible completion"))
425 (dabbrev--substitute-expansion nil abbrev init))
426 (t
427 ;; * String is a common substring completion already. Make list.
428 (message "Making completion list...")
429 (with-output-to-temp-buffer " *Completions*"
430 (display-completion-list (all-completions init my-obarray)))
7313ccdb 431 (message "Making completion list...done")))
6163b3ba
RS
432 (and (window-minibuffer-p (selected-window))
433 (message nil))))
2f790b20
JB
434
435;;;###autoload
436(defun dabbrev-expand (arg)
437 "Expand previous word \"dynamically\".
2f790b20 438
6163b3ba
RS
439Expands to the most recent, preceding word for which this is a prefix.
440If no suitable preceding word is found, words following point are
441considered. If still no suitable word is found, then look in the
442buffers accepted by the function pointed out by variable
443`dabbrev-friend-buffer-function'.
2f790b20 444
7313ccdb 445A positive prefix argument, N, says to take the Nth backward *distinct*
6163b3ba 446possibility. A negative argument says search forward.
2f790b20
JB
447
448If the cursor has not moved from the end of the previous expansion and
449no argument is given, replace the previously-made expansion
6163b3ba
RS
450with the next possible expansion not yet tried.
451
452The variable `dabbrev-backward-only' may be used to limit the
453direction of search to backward if set non-nil.
454
7313ccdb 455See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
2f790b20 456 (interactive "*P")
dea5efcb
RS
457 (let (abbrev record-case-pattern
458 expansion old direction (orig-point (point)))
2f790b20
JB
459 ;; abbrev -- the abbrev to expand
460 ;; expansion -- the expansion found (eventually) or nil until then
461 ;; old -- the text currently in the buffer
462 ;; (the abbrev, or the previously-made expansion)
2f790b20
JB
463 (save-excursion
464 (if (and (null arg)
df6eb420
RS
465 (markerp dabbrev--last-abbrev-location)
466 (marker-position dabbrev--last-abbrev-location)
6163b3ba
RS
467 (or (eq last-command this-command)
468 (and (window-minibuffer-p (selected-window))
469 (= dabbrev--last-abbrev-location
470 (point)))))
7313ccdb 471 ;; Find a different expansion for the same abbrev as last time.
6163b3ba
RS
472 (progn
473 (setq abbrev dabbrev--last-abbreviation)
474 (setq old dabbrev--last-expansion)
475 (setq direction dabbrev--last-direction))
a8a2d6ca
KH
476 ;; If the user inserts a space after expanding
477 ;; and then asks to expand again, always fetch the next word.
478 (if (and (eq (preceding-char) ?\ )
479 (markerp dabbrev--last-abbrev-location)
480 (marker-position dabbrev--last-abbrev-location)
481 (= (point) (1+ dabbrev--last-abbrev-location)))
dea5efcb 482 (progn
a8a2d6ca
KH
483 ;; The "abbrev" to expand is just the space.
484 (setq abbrev " ")
485 (save-excursion
486 (if dabbrev--last-buffer
487 (set-buffer dabbrev--last-buffer))
488 ;; Find the end of the last "expansion" word.
489 (if (or (eq dabbrev--last-direction 1)
490 (and (eq dabbrev--last-direction 0)
491 (< dabbrev--last-expansion-location (point))))
492 (setq dabbrev--last-expansion-location
493 (+ dabbrev--last-expansion-location
494 (length dabbrev--last-expansion))))
495 (goto-char dabbrev--last-expansion-location)
496 ;; Take the following word, with intermediate separators,
497 ;; as our expansion this time.
498 (re-search-forward
499 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
6f0b000c
RS
500 (setq expansion (buffer-substring-no-properties
501 dabbrev--last-expansion-location (point)))
dea5efcb 502 (if dabbrev--last-case-pattern
07e47d0b 503 (setq expansion (upcase expansion)))
a8a2d6ca
KH
504
505 ;; Record the end of this expansion, in case we repeat this.
506 (setq dabbrev--last-expansion-location (point)))
507 ;; Indicate that dabbrev--last-expansion-location is
508 ;; at the end of the expansion.
509 (setq dabbrev--last-direction -1))
510
511 ;; We have a different abbrev to expand.
512 (dabbrev--reset-global-variables)
513 (setq direction (if (null arg)
514 (if dabbrev-backward-only 1 0)
515 (prefix-numeric-value arg)))
516 (setq abbrev (dabbrev--abbrev-at-point))
dea5efcb 517 (setq record-case-pattern t)
a8a2d6ca 518 (setq old nil)))
6163b3ba
RS
519
520 ;;--------------------------------
521 ;; Find the expansion
522 ;;--------------------------------
a8a2d6ca
KH
523 (or expansion
524 (setq expansion
525 (dabbrev--find-expansion abbrev direction
543abb4a
RS
526 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
527 case-fold-search
528 dabbrev-case-fold-search)
a8a2d6ca
KH
529 (or (not dabbrev-upcase-means-case-search)
530 (string= abbrev (downcase abbrev))))))))
6163b3ba
RS
531 (cond
532 ((not expansion)
533 (dabbrev--reset-global-variables)
534 (if old
535 (save-excursion
4209f479 536 (setq buffer-undo-list (cons orig-point buffer-undo-list))
3132e115
RS
537 ;; Put back the original abbrev with its original case pattern.
538 (search-backward old)
539 (insert abbrev)
540 (delete-region (point) (+ (point) (length old)))))
7313ccdb 541 (error "No%s dynamic expansion for `%s' found"
6163b3ba
RS
542 (if old " further" "") abbrev))
543 (t
544 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found))
2f790b20 545 (progn
6163b3ba
RS
546 (message "Expansion found in '%s'"
547 (buffer-name dabbrev--last-buffer))
548 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
549 (message nil))
a8a2d6ca
KH
550 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
551 (null dabbrev--last-buffer))
552 (numberp dabbrev--last-expansion-location)
553 (and (> dabbrev--last-expansion-location (point))))
554 (setq dabbrev--last-expansion-location
555 (copy-marker dabbrev--last-expansion-location)))
2f790b20 556 ;; Success: stick it in and return.
4209f479 557 (setq buffer-undo-list (cons orig-point buffer-undo-list))
6163b3ba 558 (dabbrev--substitute-expansion old abbrev expansion)
dea5efcb
RS
559
560 ;; If we are not copying successive words now,
561 ;; set dabbrev--last-case-pattern.
562 (and record-case-pattern
563 (setq dabbrev--last-case-pattern
543abb4a
RS
564 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
565 case-fold-search
566 dabbrev-case-fold-search)
dea5efcb
RS
567 (not dabbrev-upcase-means-case-search)
568 (equal abbrev (upcase abbrev)))))
569
2f790b20 570 ;; Save state for re-expand.
6163b3ba
RS
571 (setq dabbrev--last-expansion expansion)
572 (setq dabbrev--last-abbreviation abbrev)
573 (setq dabbrev--last-abbrev-location (point-marker))))))
574
b578f267
EN
575;;----------------------------------------------------------------
576;; Local functions
577;;----------------------------------------------------------------
6163b3ba 578
6163b3ba
RS
579;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
580(defun dabbrev--same-major-mode-p (other-buffer)
7313ccdb
RS
581 (eq major-mode
582 (save-excursion
583 (set-buffer other-buffer)
584 major-mode)))
6163b3ba
RS
585
586;;; Back over all abbrev type characters and then moves forward over
587;;; all skip characters.
588(defun dabbrev--goto-start-of-abbrev ()
589 ;; Move backwards over abbrev chars
590 (save-match-data
7313ccdb
RS
591 (if (not (bobp))
592 (progn
593 (forward-char -1)
594 (while (and (looking-at dabbrev--abbrev-char-regexp)
595 (not (bobp)))
596 (forward-char -1))
597 (or (looking-at dabbrev--abbrev-char-regexp)
598 (forward-char 1))))
6163b3ba
RS
599 (and dabbrev-abbrev-skip-leading-regexp
600 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
601 (forward-char 1)))))
602
7313ccdb 603;;; Extract the symbol at point to serve as abbreviation.
6163b3ba
RS
604(defun dabbrev--abbrev-at-point ()
605 ;; Check for error
a8a2d6ca
KH
606 (if (bobp)
607 (error "No possible abbreviation preceding point"))
6163b3ba
RS
608 ;; Return abbrev at point
609 (save-excursion
a8a2d6ca 610 ;; Record the end of the abbreviation.
6163b3ba 611 (setq dabbrev--last-abbrev-location (point))
a8a2d6ca
KH
612 ;; If we aren't right after an abbreviation,
613 ;; move point back to just after one.
614 ;; This is so the user can get successive words
615 ;; by typing the punctuation followed by M-/.
616 (save-match-data
617 (if (save-excursion
618 (forward-char -1)
619 (not (looking-at (concat "\\("
620 (or dabbrev-abbrev-char-regexp
621 "\\sw\\|\\s_")
622 "\\)+"))))
623 (if (re-search-backward (or dabbrev-abbrev-char-regexp
624 "\\sw\\|\\s_")
625 nil t)
626 (forward-char 1)
627 (error "No possible abbreviation preceding point"))))
628 ;; Now find the beginning of that one.
629 (dabbrev--goto-start-of-abbrev)
6f0b000c
RS
630 (buffer-substring-no-properties
631 dabbrev--last-abbrev-location (point))))
6163b3ba
RS
632
633;;; Initializes all global variables
634(defun dabbrev--reset-global-variables ()
635 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
636 ;; must not be reset here.
637 (setq dabbrev--last-table nil
638 dabbrev--last-abbreviation nil
639 dabbrev--last-abbrev-location nil
640 dabbrev--last-direction nil
641 dabbrev--last-expansion nil
642 dabbrev--last-expansion-location nil
643 dabbrev--friend-buffer-list nil
644 dabbrev--last-buffer nil
645 dabbrev--last-buffer-found nil
646 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
647 "\\sw\\|\\s_")
df6eb420 648 dabbrev--check-other-buffers dabbrev-check-other-buffers))
6163b3ba
RS
649
650;;; Find all buffers that are considered "friends" according to the
651;;; function pointed out by dabbrev-friend-buffer-function.
652(defun dabbrev--select-buffers ()
653 (save-excursion
654 (and (window-minibuffer-p (selected-window))
655 (set-buffer (dabbrev--minibuffer-origin)))
656 (let ((orig-buffer (current-buffer)))
7313ccdb
RS
657 (dabbrev-filter-elements
658 buffer (buffer-list)
659 (and (not (eq orig-buffer buffer))
660 (boundp 'dabbrev-friend-buffer-function)
661 (funcall dabbrev-friend-buffer-function buffer))))))
662
663;;; Search for ABBREV, N times, normally looking forward,
664;;; but looking in reverse instead if REVERSE is non-nil.
6163b3ba
RS
665(defun dabbrev--try-find (abbrev reverse n ignore-case)
666 (save-excursion
df6eb420
RS
667 (save-restriction
668 (widen)
669 (let ((expansion nil))
670 (and dabbrev--last-expansion-location
671 (goto-char dabbrev--last-expansion-location))
672 (let ((case-fold-search ignore-case)
673 (count n))
674 (while (and (> count 0)
675 (setq expansion (dabbrev--search abbrev
676 reverse
677 ignore-case)))
678 (setq count (1- count))))
679 (and expansion
680 (setq dabbrev--last-expansion-location (point)))
681 expansion))))
6163b3ba
RS
682
683;;; Find all expansions of ABBREV
684(defun dabbrev--find-all-expansions (abbrev ignore-case)
685 (let ((all-expansions nil)
686 expansion)
687 (save-excursion
688 (goto-char (point-min))
689 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
df6eb420 690 (setq all-expansions (cons expansion all-expansions))))
6163b3ba
RS
691 all-expansions))
692
693(defun dabbrev--scanning-message ()
7313ccdb 694 (message "Scanning `%s'" (buffer-name (current-buffer))))
6163b3ba
RS
695
696;;; Find one occasion of ABBREV.
697;;; DIRECTION > 0 means look that many times backwards.
698;;; DIRECTION < 0 means look that many times forward.
699;;; DIRECTION = 0 means try both backward and forward.
700;;; IGNORE-CASE non-nil means ignore case when searching.
701(defun dabbrev--find-expansion (abbrev direction ignore-case)
702 (let (expansion)
703 (save-excursion
704 (cond
705 (dabbrev--last-buffer
706 (set-buffer dabbrev--last-buffer)
707 (dabbrev--scanning-message))
708 ((and (not dabbrev-search-these-buffers-only)
709 (window-minibuffer-p (selected-window)))
710 (set-buffer (dabbrev--minibuffer-origin))
711 ;; In the minibuffer-origin buffer we will only search from
712 ;; the top and down.
713 (goto-char (point-min))
714 (setq direction -1)
715 (dabbrev--scanning-message)))
716 (cond
717 ;; ------------------------------------------
718 ;; Look backwards
719 ;; ------------------------------------------
720 ((and (not dabbrev-search-these-buffers-only)
721 (>= direction 0)
722 (setq dabbrev--last-direction (min 1 direction))
723 (setq expansion (dabbrev--try-find abbrev t
724 (max 1 direction)
725 ignore-case)))
726 expansion)
727 ;; ------------------------------------------
728 ;; Look forward
729 ;; ------------------------------------------
730 ((and (or (not dabbrev-search-these-buffers-only)
731 dabbrev--last-buffer)
732 (<= direction 0)
733 (setq dabbrev--last-direction -1)
734 (setq expansion (dabbrev--try-find abbrev nil
735 (max 1 (- direction))
736 ignore-case)))
737 expansion)
738 ;; ------------------------------------------
739 ;; Look in other buffers.
740 ;; Start at (point-min) and look forward.
741 ;; ------------------------------------------
742 (t
743 (setq dabbrev--last-direction -1)
744 ;; Make sure that we should check other buffers
745 (or dabbrev--friend-buffer-list
746 dabbrev--last-buffer
747 (setq dabbrev--friend-buffer-list
748 (mapcar (function get-buffer)
749 dabbrev-search-these-buffers-only))
750 (not dabbrev--check-other-buffers)
751 (not (or (eq dabbrev--check-other-buffers t)
752 (progn
753 (setq dabbrev--check-other-buffers
7313ccdb 754 (y-or-n-p "Scan other buffers also? ")))))
6163b3ba
RS
755 (let* (friend-buffer-list non-friend-buffer-list)
756 (setq dabbrev--friend-buffer-list
757 (funcall dabbrev-select-buffers-function))
df6eb420 758 (if dabbrev-check-all-buffers
7313ccdb
RS
759 (setq non-friend-buffer-list
760 (nreverse
761 (dabbrev-filter-elements
762 buffer (buffer-list)
7bd9ba70
RS
763 (and (not (member (buffer-name buffer)
764 dabbrev-ignored-buffer-names))
765 (not (memq buffer dabbrev--friend-buffer-list)))))
7313ccdb
RS
766 dabbrev--friend-buffer-list
767 (append dabbrev--friend-buffer-list
768 non-friend-buffer-list)))))
d8f7e85a
RS
769 ;; Move buffers that are visible on the screen
770 ;; to the front of the list.
771 (if dabbrev--friend-buffer-list
772 (let ((w (next-window (selected-window))))
773 (while (not (eq w (selected-window)))
774 (setq dabbrev--friend-buffer-list
775 (cons (window-buffer w)
776 (delq (window-buffer w) dabbrev--friend-buffer-list)))
777 (setq w (next-window w)))))
6163b3ba
RS
778 ;; Walk through the buffers
779 (while (and (not expansion) dabbrev--friend-buffer-list)
780 (setq dabbrev--last-buffer
781 (car dabbrev--friend-buffer-list))
782 (setq dabbrev--friend-buffer-list
783 (cdr dabbrev--friend-buffer-list))
784 (set-buffer dabbrev--last-buffer)
785 (dabbrev--scanning-message)
786 (setq dabbrev--last-expansion-location (point-min))
787 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
788 expansion)))))
789
6163b3ba
RS
790(defun dabbrev--safe-replace-match (string &optional fixedcase literal)
791 (if (eq major-mode 'picture-mode)
792 (picture-replace-match string fixedcase literal)
793 (replace-match string fixedcase literal)))
794
795;;;----------------------------------------------------------------
796;;; Substitute the current string in buffer with the expansion
797;;; OLD is nil or the last expansion substring.
798;;; ABBREV is the abbreviation we are working with.
799;;; EXPANSION is the expansion substring.
800(defun dabbrev--substitute-expansion (old abbrev expansion)
801 ;;(undo-boundary)
543abb4a
RS
802 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
803 case-fold-search
804 dabbrev-case-fold-search)
6163b3ba
RS
805 (or (not dabbrev-upcase-means-case-search)
806 (string= abbrev (downcase abbrev)))
543abb4a
RS
807 (if (eq dabbrev-case-replace 'case-replace)
808 case-replace
809 dabbrev-case-replace))))
6163b3ba
RS
810 (and nil use-case-replace
811 (setq old (concat abbrev (or old "")))
812 (setq expansion (concat abbrev expansion)))
65411629
RS
813 ;; If the expansion has mixed case
814 ;; and it is not simply a capitalized word,
815 ;; or if the abbrev has mixed case,
816 ;; and if the given abbrev's case pattern
66db4b5b
RS
817 ;; matches the start of the expansion,
818 ;; copy the expansion's case
819 ;; instead of downcasing all the rest.
65411629
RS
820 (let ((expansion-rest (substring expansion 1)))
821 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
822 (string= expansion-rest (upcase expansion-rest)))
823 (or (string= abbrev (downcase abbrev))
824 (string= abbrev (upcase abbrev)))))
825 (string= abbrev
826 (substring expansion 0 (length abbrev))))
827 (setq use-case-replace nil)))
07e47d0b
RS
828 (if (equal abbrev " ")
829 (setq use-case-replace nil))
830 (if use-case-replace
831 (setq expansion (downcase expansion)))
6163b3ba
RS
832 (if old
833 (save-excursion
834 (search-backward old))
9b7c13e0 835 ;;(set-match-data (list (point-marker) (point-marker)))
6163b3ba
RS
836 (search-backward abbrev))
837 ;; Make case of replacement conform to case of abbreviation
838 ;; provided (1) that kind of thing is enabled in this buffer
839 ;; and (2) the replacement itself is all lower case.
840 (dabbrev--safe-replace-match expansion
841 (not use-case-replace)
842 t)))
843
844
845;;;----------------------------------------------------------------
846;;; Search function used by dabbrevs library.
847
7313ccdb 848;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
6163b3ba 849;;; is t for reverse search, nil for forward. Variable dabbrev-limit
a7acbbe4 850;;; controls the maximum search region size. Third argument IGNORE-CASE
6163b3ba
RS
851;;; non-nil means treat case as insignificant while looking for a match
852;;; and when comparing with previous matches. Also if that's non-nil
853;;; and the match is found at the beginning of a sentence and is in
854;;; lower case except for the initial then it is converted to all lower
855;;; case for return.
856
857;;; Table of expansions already seen is examined in buffer
858;;; `dabbrev--last-table' so that only distinct possibilities are found
859;;; by dabbrev-re-expand.
860
861;;; Value is the expansion, or nil if not found.
862
863(defun dabbrev--search (abbrev reverse ignore-case)
864 (save-match-data
865 (let ((pattern1 (concat (regexp-quote abbrev)
866 "\\(" dabbrev--abbrev-char-regexp "\\)"))
867 (pattern2 (concat (regexp-quote abbrev)
868 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
869 (found-string nil))
870 ;; Limited search.
871 (save-restriction
872 (and dabbrev-limit
873 (narrow-to-region dabbrev--last-expansion-location
874 (+ (point)
875 (if reverse (- dabbrev-limit) dabbrev-limit))))
876 ;;--------------------------------
877 ;; Look for a distinct expansion, using dabbrev--last-table.
878 ;;--------------------------------
879 (while (and (not found-string)
880 (if reverse
881 (re-search-backward pattern1 nil t)
882 (re-search-forward pattern1 nil t)))
63d991d6
KH
883 (goto-char (match-beginning 0))
884 ;; In case we matched in the middle of a word,
885 ;; back up to start of word and verify we still match.
886 (dabbrev--goto-start-of-abbrev)
887
888 (if (not (looking-at pattern1))
889 nil
890 ;; We have a truly valid match. Find the end.
6163b3ba 891 (re-search-forward pattern2)
6f0b000c
RS
892 (setq found-string (buffer-substring-no-properties
893 (match-beginning 1) (match-end 1)))
6163b3ba 894 (and ignore-case (setq found-string (downcase found-string)))
63d991d6 895 ;; Ignore this match if it's already in the table.
7313ccdb
RS
896 (if (dabbrev-filter-elements
897 table-string dabbrev--last-table
898 (string= found-string table-string))
63d991d6
KH
899 (setq found-string nil)))
900 ;; Prepare to continue searching.
6163b3ba
RS
901 (if reverse
902 (goto-char (match-beginning 0))
903 (goto-char (match-end 0))))
63d991d6
KH
904 ;; If we found something, use it.
905 (if found-string
906 ;; Put it into `dabbrev--last-table'
907 ;; and return it (either downcased, or as is).
6f0b000c
RS
908 (let ((result (buffer-substring-no-properties
909 (match-beginning 0) (match-end 0))))
63d991d6
KH
910 (setq dabbrev--last-table
911 (cons found-string dabbrev--last-table))
912 (if (and ignore-case (eval dabbrev-case-replace))
66db4b5b 913 result
63d991d6 914 result)))))))
6163b3ba 915
01987a6b 916(provide 'dabbrev)
7313ccdb 917
b578f267 918;;; dabbrev.el ends here