1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 86, 92, 94, 96, 1997 Free Software Foundation, Inc.
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
11 ;; This file is part of GNU Emacs.
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)
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.
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.
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
34 ;; To expand a word, just put the point right after the word and press
35 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
37 ;; Check out the customizable variables below to learn about all the
38 ;; features of this package.
40 ;;; Hints and tips for major modes writers:
42 ;; Recommended values C/Lisp etc text
43 ;; dabbrev-case-fold-search nil t
44 ;; dabbrev-case-replace nil t
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.
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.
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.
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)
64 ;; (set-buffer buffer)
65 ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
68 ;; Known bugs and limitations.
69 ;; - Possible to do several levels of `dabbrev-completion' in the
71 ;; - dabbrev-completion doesn't handle resetting the globals variables
72 ;; right. It resets them after finding the abbrev.
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>
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@ql.org>
92 ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
93 ;; ... and to all the people who have participated in the beta tests.
97 ;;----------------------------------------------------------------
98 ;; Customization variables
99 ;;----------------------------------------------------------------
101 (defgroup dabbrev nil
102 "Dynamic Abbreviations"
103 :tag
"Dynamic Abbreviations"
106 (defcustom dabbrev-backward-only nil
107 "*If non-nil, `dabbrev-expand' only looks backwards."
111 (defcustom dabbrev-limit nil
112 "*Limits region searched by `dabbrev-expand' to this many chars away."
113 :type
'(choice (const :tag
"off" nil
)
117 (defcustom dabbrev-abbrev-skip-leading-regexp nil
118 "*Regexp for skipping leading characters of an abbreviation.
120 Example: Set this to \"\\\\$\" for programming languages
121 in which variable names may appear with or without a leading `$'.
122 \(For example, in Makefiles.\)
124 Set this to nil if no characters should be skipped."
125 :type
'(choice regexp
126 (const :tag
"off" nil
))
129 (defcustom dabbrev-case-fold-search
'case-fold-search
130 "*Control whether dabbrev searches should ignore case.
131 A value of nil means case is significant.
132 A value of `case-fold-search' means case is significant
133 if `case-fold-search' is nil.
134 Any other non-nil version means case is not significant."
135 :type
'(choice (const :tag
"off" nil
)
137 (const :tag
"like search" case-fold-search
))
140 (defcustom dabbrev-upcase-means-case-search nil
141 "*The significance of an uppercase character in an abbreviation.
142 nil means case fold search, non-nil means case sensitive search.
144 This variable has an effect only when the value of
145 `dabbrev-case-fold-search' says to ignore case."
149 (defcustom dabbrev-case-replace
'case-replace
150 "*Controls whether dabbrev preserves case when expanding the abbreviation.
151 A value of nil means preserve case.
152 A value of `case-replace' means preserve case if `case-replace' is nil.
153 Any other non-nil version means do not preserve case.
155 This variable has an effect only when the value of
156 `dabbrev-case-fold-search' specifies to ignore case."
157 :type
'(choice (const :tag
"off" nil
)
159 (const :tag
"like M-x query-replace" case-replace
))
162 (defcustom dabbrev-abbrev-char-regexp nil
163 "*Regexp to recognize a character in an abbreviation or expansion.
164 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
166 Set this variable to \"\\\\sw\" if you want ordinary words or
167 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
168 syntax is \"symbol\" as well as those whose syntax is \"word\".
170 The value nil has a special meaning: the abbreviation is from point to
171 previous word-start, but the search is for symbols.
173 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
174 while `yes', `or', `no' and `p' are considered words. If this
175 variable is nil, then expanding `yes-or-no-' looks for a symbol
176 starting 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
179 expanding `yes-or-no-' signals an error because `-' is not part of a word;
180 but expanding `yes-or-no' looks for a word starting with `no'.
182 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
183 :type
'(choice (const nil
)
187 (defcustom dabbrev-check-all-buffers t
188 "*Non-nil means dabbrev package should search *all* buffers.
190 Dabbrev always searches the current buffer first. Then, if
191 `dabbrev-check-other-buffers' says so, it searches the buffers
192 designated by `dabbrev-select-buffers-function'.
194 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
195 all the other buffers, except those named in `dabbrev-ignored-buffer-names'."
199 (defcustom dabbrev-ignored-buffer-names
'("*Messages")
200 "*List of buffer names that dabbrev should not check."
201 :type
'(repeat (string :tag
"Buffer name"))
205 (defcustom dabbrev-check-other-buffers t
206 "*Should \\[dabbrev-expand] look in other buffers?\
208 nil: Don't look in other buffers.
209 t: Also look for expansions in the buffers pointed out by
210 `dabbrev-select-buffers-function'.
211 Anything else: When we can't find any more expansions in
212 the current buffer, then ask the user whether to look in other
215 The default value is t."
216 :type
'(choice (const :tag
"off" nil
)
218 (const :tag
"ask" other
))
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.
226 The function should take no arguments and return a list of buffers to
227 search for expansions. Have a look at `dabbrev--select-buffers' for
230 A mode setting this variable should make it buffer local.")
232 (defcustom dabbrev-friend-buffer-function
'dabbrev--same-major-mode-p
233 "*A function to decide whether dabbrev should search OTHER-BUFFER.
234 The function should take one argument, OTHER-BUFFER, and return
235 non-nil if that buffer should be searched. Have a look at
236 `dabbrev--same-major-mode-p' for an example.
238 The value of `dabbrev-friend-buffer-function' has an effect only if
239 the value of `dabbrev-select-buffers-function' uses it. The function
240 `dabbrev--select-buffers' is one function you can use here.
242 A mode setting this variable should make it buffer local."
246 (defcustom dabbrev-search-these-buffers-only nil
247 "If non-nil, a list of buffers which dabbrev should search.
248 If this variable is non-nil, dabbrev will only look in these buffers.
249 It will not even look in the current buffer if it is not a member of
252 ;;----------------------------------------------------------------
253 ;; Internal variables
254 ;;----------------------------------------------------------------
256 ;; Last obarray of completions in `dabbrev-completion'
257 (defvar dabbrev--last-obarray nil
)
259 ;; Table of expansions seen so far
260 (defvar dabbrev--last-table nil
)
262 ;; Last string we tried to expand.
263 (defvar dabbrev--last-abbreviation nil
)
265 ;; Location last abbreviation began
266 (defvar dabbrev--last-abbrev-location nil
)
268 ;; Direction of last dabbrevs search
269 (defvar dabbrev--last-direction
0)
271 ;; Last expansion of an abbreviation.
272 (defvar dabbrev--last-expansion nil
)
274 ;; Location the last expansion was found.
275 (defvar dabbrev--last-expansion-location nil
)
277 ;; The list of remaining buffers with the same mode as current buffer.
278 (defvar dabbrev--friend-buffer-list nil
)
280 ;; The buffer we looked in last.
281 (defvar dabbrev--last-buffer nil
)
283 ;; The buffer we found the expansion last time.
284 (defvar dabbrev--last-buffer-found nil
)
286 ;; The buffer we last did a completion in.
287 (defvar dabbrev--last-completion-buffer nil
)
289 ;; Non-nil means we should upcase
290 ;; when copying successive words.
291 (defvar dabbrev--last-case-pattern nil
)
293 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
294 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers
)
296 ;; The regexp for recognizing a character in an abbreviation.
297 (defvar dabbrev--abbrev-char-regexp nil
)
299 ;;----------------------------------------------------------------
301 ;;----------------------------------------------------------------
303 ;;; Get the buffer that mini-buffer was activated from
304 (defsubst dabbrev--minibuffer-origin
()
305 (car (cdr (buffer-list))))
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
))
315 (setq (, element
) (car dabbrev-tail
))
317 (setq dabbrev-result
(cons (, element
) dabbrev-result
)))
318 (setq dabbrev-tail
(cdr dabbrev-tail
)))
319 (nreverse dabbrev-result
))))
321 ;;----------------------------------------------------------------
322 ;; Exported functions
323 ;;----------------------------------------------------------------
326 (define-key esc-map
"/" 'dabbrev-expand
)
327 ;;;??? Do we want this?
329 (define-key esc-map
[?\C-
/] 'dabbrev-completion
)
332 (defun dabbrev-completion (&optional arg
)
333 "Completion on current word.
334 Like \\[dabbrev-expand] but finds all expansions in the current buffer
335 and presents suggestions for completion.
337 With a prefix argument, it searches all buffers accepted by the
338 function pointed out by `dabbrev-friend-buffer-function' to find the
341 If the prefix argument is 16 (which comes from C-u C-u),
342 then it searches *all* buffers.
344 With no prefix argument, it reuses an old completion list
345 if there is a suitable one already."
348 (dabbrev--reset-global-variables)
349 (let* ((dabbrev-check-other-buffers (and arg t
))
350 (dabbrev-check-all-buffers
351 (and arg
(= (prefix-numeric-value arg
) 16)))
352 (abbrev (dabbrev--abbrev-at-point))
353 (ignore-case-p (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
355 dabbrev-case-fold-search
)
356 (or (not dabbrev-upcase-means-case-search
)
357 (string= abbrev
(downcase abbrev
)))))
358 (my-obarray dabbrev--last-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
371 (length dabbrev--last-abbreviation
)))
372 (setq init
(try-completion abbrev my-obarray
)))
373 ;; We can reuse the existing completion list.
375 ;;--------------------------------
376 ;; New abbreviation to expand.
377 ;;--------------------------------
378 (setq dabbrev--last-abbreviation abbrev
)
379 ;; Find all expansion
380 (let ((completion-list
381 (dabbrev--find-all-expansions abbrev ignore-case-p
))
382 (completion-ignore-case ignore-case-p
))
383 ;; Make an obarray with all expansions
384 (setq my-obarray
(make-vector (length completion-list
) 0))
385 (or (> (length my-obarray
) 0)
386 (error "No dynamic expansion for \"%s\" found%s"
388 (if dabbrev--check-other-buffers
"" " in this-buffer")))
390 ((or (not ignore-case-p
)
391 (not dabbrev-case-replace
))
392 (mapcar (function (lambda (string)
393 (intern string my-obarray
)))
395 ((string= abbrev
(upcase abbrev
))
396 (mapcar (function (lambda (string)
397 (intern (upcase string
) my-obarray
)))
399 ((string= (substring abbrev
0 1)
400 (upcase (substring abbrev
0 1)))
401 (mapcar (function (lambda (string)
402 (intern (capitalize string
) my-obarray
)))
405 (mapcar (function (lambda (string)
406 (intern (downcase string
) my-obarray
)))
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 ;;--------------------------------
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)
422 (message "Repeat `%s' to see all completions"
423 (key-description (this-command-keys)))
424 (message "The only possible completion"))
425 (dabbrev--substitute-expansion nil abbrev init
))
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
)))
431 (message "Making completion list...done")))
432 (and (window-minibuffer-p (selected-window))
436 (defun dabbrev-expand (arg)
437 "Expand previous word \"dynamically\".
439 Expands to the most recent, preceding word for which this is a prefix.
440 If no suitable preceding word is found, words following point are
441 considered. If still no suitable word is found, then look in the
442 buffers accepted by the function pointed out by variable
443 `dabbrev-friend-buffer-function'.
445 A positive prefix argument, N, says to take the Nth backward *distinct*
446 possibility. A negative argument says search forward.
448 If the cursor has not moved from the end of the previous expansion and
449 no argument is given, replace the previously-made expansion
450 with the next possible expansion not yet tried.
452 The variable `dabbrev-backward-only' may be used to limit the
453 direction of search to backward if set non-nil.
455 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
457 (let (abbrev record-case-pattern
458 expansion old direction
(orig-point (point)))
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)
465 (markerp dabbrev--last-abbrev-location
)
466 (marker-position dabbrev--last-abbrev-location
)
467 (or (eq last-command this-command
)
468 (and (window-minibuffer-p (selected-window))
469 (= dabbrev--last-abbrev-location
471 ;; Find a different expansion for the same abbrev as last time.
473 (setq abbrev dabbrev--last-abbreviation
)
474 (setq old dabbrev--last-expansion
)
475 (setq direction dabbrev--last-direction
))
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
)))
483 ;; The "abbrev" to expand is just the space.
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.
499 (concat "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
500 (setq expansion
(buffer-substring-no-properties
501 dabbrev--last-expansion-location
(point)))
502 (if dabbrev--last-case-pattern
503 (setq expansion
(upcase expansion
)))
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))
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))
517 (setq record-case-pattern t
)
520 ;;--------------------------------
521 ;; Find the expansion
522 ;;--------------------------------
525 (dabbrev--find-expansion abbrev direction
526 (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
528 dabbrev-case-fold-search
)
529 (or (not dabbrev-upcase-means-case-search
)
530 (string= abbrev
(downcase abbrev
))))))))
533 (dabbrev--reset-global-variables)
536 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
537 ;; Put back the original abbrev with its original case pattern.
538 (search-backward old
)
540 (delete-region (point) (+ (point) (length old
)))))
541 (error "No%s dynamic expansion for `%s' found"
542 (if old
" further" "") abbrev
))
544 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found
))
546 (message "Expansion found in '%s'"
547 (buffer-name dabbrev--last-buffer
))
548 (setq dabbrev--last-buffer-found dabbrev--last-buffer
))
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
)))
556 ;; Success: stick it in and return.
557 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
558 (dabbrev--substitute-expansion old abbrev expansion
)
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
564 (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
566 dabbrev-case-fold-search
)
567 (not dabbrev-upcase-means-case-search
)
568 (equal abbrev
(upcase abbrev
)))))
570 ;; Save state for re-expand.
571 (setq dabbrev--last-expansion expansion
)
572 (setq dabbrev--last-abbreviation abbrev
)
573 (setq dabbrev--last-abbrev-location
(point-marker))))))
575 ;;----------------------------------------------------------------
577 ;;----------------------------------------------------------------
579 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
580 (defun dabbrev--same-major-mode-p (other-buffer)
583 (set-buffer other-buffer
)
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
594 (while (and (looking-at dabbrev--abbrev-char-regexp
)
597 (or (looking-at dabbrev--abbrev-char-regexp
)
599 (and dabbrev-abbrev-skip-leading-regexp
600 (while (looking-at dabbrev-abbrev-skip-leading-regexp
)
603 ;;; Extract the symbol at point to serve as abbreviation.
604 (defun dabbrev--abbrev-at-point ()
607 (error "No possible abbreviation preceding point"))
608 ;; Return abbrev at point
610 ;; Record the end of the abbreviation.
611 (setq dabbrev--last-abbrev-location
(point))
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-/.
619 (not (looking-at (concat "\\("
620 (or dabbrev-abbrev-char-regexp
623 (if (re-search-backward (or dabbrev-abbrev-char-regexp
627 (error "No possible abbreviation preceding point"))))
628 ;; Now find the beginning of that one.
629 (dabbrev--goto-start-of-abbrev)
630 (buffer-substring-no-properties
631 dabbrev--last-abbrev-location
(point))))
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
648 dabbrev--check-other-buffers dabbrev-check-other-buffers
))
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 ()
654 (and (window-minibuffer-p (selected-window))
655 (set-buffer (dabbrev--minibuffer-origin)))
656 (let ((orig-buffer (current-buffer)))
657 (dabbrev-filter-elements
659 (and (not (eq orig-buffer buffer
))
660 (boundp 'dabbrev-friend-buffer-function
)
661 (funcall dabbrev-friend-buffer-function buffer
))))))
663 ;;; Search for ABBREV, N times, normally looking forward,
664 ;;; but looking in reverse instead if REVERSE is non-nil.
665 (defun dabbrev--try-find (abbrev reverse n ignore-case
)
669 (let ((expansion nil
))
670 (and dabbrev--last-expansion-location
671 (goto-char dabbrev--last-expansion-location
))
672 (let ((case-fold-search ignore-case
)
674 (while (and (> count
0)
675 (setq expansion
(dabbrev--search abbrev
678 (setq count
(1- count
))))
680 (setq dabbrev--last-expansion-location
(point)))
683 ;;; Find all expansions of ABBREV
684 (defun dabbrev--find-all-expansions (abbrev ignore-case
)
685 (let ((all-expansions nil
)
688 (goto-char (point-min))
689 (while (setq expansion
(dabbrev--find-expansion abbrev -
1 ignore-case
))
690 (setq all-expansions
(cons expansion all-expansions
))))
693 (defun dabbrev--scanning-message ()
694 (message "Scanning `%s'" (buffer-name (current-buffer))))
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
)
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
713 (goto-char (point-min))
715 (dabbrev--scanning-message)))
717 ;; ------------------------------------------
719 ;; ------------------------------------------
720 ((and (not dabbrev-search-these-buffers-only
)
722 (setq dabbrev--last-direction
(min 1 direction
))
723 (setq expansion
(dabbrev--try-find abbrev t
727 ;; ------------------------------------------
729 ;; ------------------------------------------
730 ((and (or (not dabbrev-search-these-buffers-only
)
731 dabbrev--last-buffer
)
733 (setq dabbrev--last-direction -
1)
734 (setq expansion
(dabbrev--try-find abbrev nil
735 (max 1 (- direction
))
738 ;; ------------------------------------------
739 ;; Look in other buffers.
740 ;; Start at (point-min) and look forward.
741 ;; ------------------------------------------
743 (setq dabbrev--last-direction -
1)
744 ;; Make sure that we should check other buffers
745 (or dabbrev--friend-buffer-list
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
)
753 (setq dabbrev--check-other-buffers
754 (y-or-n-p "Scan other buffers also? ")))))
755 (let* (friend-buffer-list non-friend-buffer-list
)
756 (setq dabbrev--friend-buffer-list
757 (funcall dabbrev-select-buffers-function
))
758 (if dabbrev-check-all-buffers
759 (setq non-friend-buffer-list
761 (dabbrev-filter-elements
763 (and (not (member (buffer-name buffer
)
764 dabbrev-ignored-buffer-names
))
765 (not (memq buffer dabbrev--friend-buffer-list
)))))
766 dabbrev--friend-buffer-list
767 (append dabbrev--friend-buffer-list
768 non-friend-buffer-list
)))))
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
)))))
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
)))
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
)))
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
)
802 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
804 dabbrev-case-fold-search
)
805 (or (not dabbrev-upcase-means-case-search
)
806 (string= abbrev
(downcase abbrev
)))
807 (if (eq dabbrev-case-replace
'case-replace
)
809 dabbrev-case-replace
))))
810 (and nil use-case-replace
811 (setq old
(concat abbrev
(or old
"")))
812 (setq expansion
(concat abbrev expansion
)))
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
817 ;; matches the start of the expansion,
818 ;; copy the expansion's case
819 ;; instead of downcasing all the rest.
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
)))))
826 (substring expansion
0 (length abbrev
))))
827 (setq use-case-replace nil
)))
828 (if (equal abbrev
" ")
829 (setq use-case-replace nil
))
831 (setq expansion
(downcase expansion
)))
834 (search-backward old
))
835 ;;(set-match-data (list (point-marker) (point-marker)))
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
)
845 ;;;----------------------------------------------------------------
846 ;;; Search function used by dabbrevs library.
848 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
849 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
850 ;;; controls the maximum search region size. Third argument IGNORE-CASE
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
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.
861 ;;; Value is the expansion, or nil if not found.
863 (defun dabbrev--search (abbrev reverse ignore-case
)
865 (let ((pattern1 (concat (regexp-quote abbrev
)
866 "\\(" dabbrev--abbrev-char-regexp
"\\)"))
867 (pattern2 (concat (regexp-quote abbrev
)
868 "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
873 (narrow-to-region dabbrev--last-expansion-location
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
)
881 (re-search-backward pattern1 nil t
)
882 (re-search-forward pattern1 nil t
)))
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)
888 (if (not (looking-at pattern1
))
890 ;; We have a truly valid match. Find the end.
891 (re-search-forward pattern2
)
892 (setq found-string
(buffer-substring-no-properties
893 (match-beginning 1) (match-end 1)))
894 (and ignore-case
(setq found-string
(downcase found-string
)))
895 ;; Ignore this match if it's already in the table.
896 (if (dabbrev-filter-elements
897 table-string dabbrev--last-table
898 (string= found-string table-string
))
899 (setq found-string nil
)))
900 ;; Prepare to continue searching.
902 (goto-char (match-beginning 0))
903 (goto-char (match-end 0))))
904 ;; If we found something, use it.
906 ;; Put it into `dabbrev--last-table'
907 ;; and return it (either downcased, or as is).
908 (let ((result (buffer-substring-no-properties
909 (match-beginning 0) (match-end 0))))
910 (setq dabbrev--last-table
911 (cons found-string dabbrev--last-table
))
912 (if (and ignore-case
(eval dabbrev-case-replace
))
918 ;;; dabbrev.el ends here