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