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