1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 86, 92, 94, 96, 1997, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Don Morrison
7 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
8 ;; Created: 16 Mars 1992
9 ;; Lindberg's last update version: 5.7
10 ;; Keywords: abbrev expand completion convenience
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; The purpose with this package is to let you write just a few
32 ;; characters of words you've written earlier to be able to expand
35 ;; To expand a word, just put the point right after the word and press
36 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
38 ;; Check out the customizable variables below to learn about all the
39 ;; features of this package.
41 ;;; Hints and tips for major modes writers:
43 ;; Recommended values C/Lisp etc text
44 ;; dabbrev-case-fold-search nil t
45 ;; dabbrev-case-replace nil t
47 ;; Set the variables you want special for your mode like this:
48 ;; (set (make-local-variable 'dabbrev-case-replace) nil)
49 ;; Then you don't interfere with other modes.
51 ;; If your mode handles buffers that refers to other buffers
52 ;; (i.e. compilation-mode, gud-mode), then try to set
53 ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
54 ;; to a function that point out those buffers.
56 ;; Same goes for major-modes that are connected to other modes. There
57 ;; are for instance a number of mail-modes. One for reading, one for
58 ;; creating a new mail etc. Maybe those should be connected.
60 ;; Example for GNUS (when we write a reply, we want dabbrev to look in
61 ;; the article for expansion):
62 ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
65 ;; (set-buffer buffer)
66 ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
69 ;; Known bugs and limitations.
70 ;; - Possible to do several levels of `dabbrev-completion' in the
72 ;; - dabbrev-completion doesn't handle resetting the globals variables
73 ;; right. It resets them after finding the abbrev.
75 ;; Future enhancements
76 ;; - Check the tags-files? Like tags-complete?
77 ;; - Add the possibility of searching both forward and backward to
78 ;; the nearest expansion.
79 ;; - Check the kill-ring when everything else fails. (Maybe something
80 ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
82 ;;; These people gave suggestions:
83 ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
84 ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
85 ;; [jules] Julian Gosnell <jules@x.co.uk>
86 ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
87 ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
88 ;; [alon] Alon Albert <al%imercury@uunet.uu.net>
89 ;; [tromey] Tom Tromey <tromey@busco.lanl.gov>
90 ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
91 ;; [Petri] Petri Raitio <per@tekla.fi>
92 ;; [ejb] Jay Berkenbilt <ejb@ql.org>
93 ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
94 ;; ... and to all the people who have participated in the beta tests.
98 ;;----------------------------------------------------------------
99 ;; Customization variables
100 ;;----------------------------------------------------------------
102 (defgroup dabbrev nil
103 "Dynamic Abbreviations"
104 :tag
"Dynamic Abbreviations"
108 (defcustom dabbrev-backward-only nil
109 "*If non-nil, `dabbrev-expand' only looks backwards."
113 (defcustom dabbrev-limit nil
114 "*Limits region searched by `dabbrev-expand' to this many chars away."
115 :type
'(choice (const :tag
"off" nil
)
119 (defcustom dabbrev-abbrev-skip-leading-regexp nil
120 "*Regexp for skipping leading characters of an abbreviation.
122 Example: Set this to \"\\\\$\" for programming languages
123 in which variable names may appear with or without a leading `$'.
124 \(For example, in Makefiles.\)
126 Set this to nil if no characters should be skipped."
127 :type
'(choice regexp
128 (const :tag
"off" nil
))
131 (defcustom dabbrev-case-fold-search
'case-fold-search
132 "*Control whether dabbrev searches should ignore case.
133 A value of nil means case is significant.
134 A value of `case-fold-search' means case is significant
135 if `case-fold-search' is nil.
136 Any other non-nil version means case is not significant."
137 :type
'(choice (const :tag
"off" nil
)
138 (const :tag
"like search" case-fold-search
)
142 (defcustom dabbrev-upcase-means-case-search nil
143 "*The significance of an uppercase character in an abbreviation.
144 nil means case fold search, non-nil means case sensitive search.
146 This variable has an effect only when the value of
147 `dabbrev-case-fold-search' says to ignore case."
151 (defcustom dabbrev-case-replace
'case-replace
152 "*Controls whether dabbrev preserves case when expanding the abbreviation.
153 A value of nil means preserve case.
154 A value of `case-replace' means preserve case if `case-replace' is nil.
155 Any other non-nil version means do not preserve case.
157 This variable has an effect only when the value of
158 `dabbrev-case-fold-search' specifies to ignore case."
159 :type
'(choice (const :tag
"off" nil
)
160 (const :tag
"like M-x query-replace" case-replace
)
164 (defcustom dabbrev-abbrev-char-regexp nil
165 "*Regexp to recognize a character in an abbreviation or expansion.
166 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
168 Set this variable to \"\\\\sw\" if you want ordinary words or
169 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
170 syntax is \"symbol\" as well as those whose syntax is \"word\".
172 The value nil has a special meaning: the abbreviation is from point to
173 previous word-start, but the search is for symbols.
175 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
176 while `yes', `or', `no' and `p' are considered words. If this
177 variable is nil, then expanding `yes-or-no-' looks for a symbol
178 starting with or containing `no-'. If you set this variable to
179 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
180 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
181 expanding `yes-or-no-' signals an error because `-' is not part of a word;
182 but expanding `yes-or-no' looks for a word starting with `no'.
184 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
185 :type
'(choice (const nil
)
189 (defcustom dabbrev-check-all-buffers t
190 "*Non-nil means dabbrev package should search *all* buffers.
192 Dabbrev always searches the current buffer first. Then, if
193 `dabbrev-check-other-buffers' says so, it searches the buffers
194 designated by `dabbrev-select-buffers-function'.
196 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
197 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
198 or matched by `dabbrev-ignored-regexps'."
202 (defcustom dabbrev-ignored-buffer-names
'("*Messages*" "*Buffer List*")
203 "*List of buffer names that dabbrev should not check.
204 See also `dabbrev-ignored-buffer-regexps'."
205 :type
'(repeat (string :tag
"Buffer name"))
209 (defcustom dabbrev-ignored-buffer-regexps nil
210 "*List of regexps matching names of buffers that dabbrev should not check.
211 See also `dabbrev-ignored-buffer-names'."
212 :type
'(repeat regexp
)
216 (defcustom dabbrev-check-other-buffers t
217 "*Should \\[dabbrev-expand] look in other buffers?\
219 nil: Don't look in other buffers.
220 t: Also look for expansions in the buffers pointed out by
221 `dabbrev-select-buffers-function'.
222 Anything else: When we can't find any more expansions in
223 the current buffer, then ask the user whether to look in other
226 The default value is t."
227 :type
'(choice (const :tag
"off" nil
)
229 (other :tag
"ask" other
))
232 ;; I guess setting this to a function that selects all C- or C++-
233 ;; mode buffers would be a good choice for a debugging buffer,
234 ;; when debugging C- or C++-code.
235 (defvar dabbrev-select-buffers-function
'dabbrev--select-buffers
236 "A function that selects buffers that should be searched by dabbrev.
237 The function should take no arguments and return a list of buffers to
238 search for expansions. Have a look at `dabbrev--select-buffers' for
241 A mode setting this variable should make it buffer local.")
243 (defcustom dabbrev-friend-buffer-function
'dabbrev--same-major-mode-p
244 "*A function to decide whether dabbrev should search OTHER-BUFFER.
245 The function should take one argument, OTHER-BUFFER, and return
246 non-nil if that buffer should be searched. Have a look at
247 `dabbrev--same-major-mode-p' for an example.
249 The value of `dabbrev-friend-buffer-function' has an effect only if
250 the value of `dabbrev-select-buffers-function' uses it. The function
251 `dabbrev--select-buffers' is one function you can use here.
253 A mode setting this variable should make it buffer local."
257 (defcustom dabbrev-search-these-buffers-only nil
258 "If non-nil, a list of buffers which dabbrev should search.
259 If this variable is non-nil, dabbrev will only look in these buffers.
260 It will not even look in the current buffer if it is not a member of
263 ;;----------------------------------------------------------------
264 ;; Internal variables
265 ;;----------------------------------------------------------------
267 ;; Last obarray of completions in `dabbrev-completion'
268 (defvar dabbrev--last-obarray nil
)
270 ;; Table of expansions seen so far
271 (defvar dabbrev--last-table nil
)
273 ;; Last string we tried to expand.
274 (defvar dabbrev--last-abbreviation nil
)
276 ;; Location last abbreviation began
277 (defvar dabbrev--last-abbrev-location nil
)
279 ;; Direction of last dabbrevs search
280 (defvar dabbrev--last-direction
0)
282 ;; Last expansion of an abbreviation.
283 (defvar dabbrev--last-expansion nil
)
285 ;; Location the last expansion was found.
286 (defvar dabbrev--last-expansion-location nil
)
288 ;; The list of remaining buffers with the same mode as current buffer.
289 (defvar dabbrev--friend-buffer-list nil
)
291 ;; The buffer we looked in last.
292 (defvar dabbrev--last-buffer nil
)
294 ;; The buffer we found the expansion last time.
295 (defvar dabbrev--last-buffer-found nil
)
297 ;; The buffer we last did a completion in.
298 (defvar dabbrev--last-completion-buffer nil
)
300 ;; If non-nil, a function to use when copying successive words.
301 ;; It should be `upcase' or `downcase'.
302 (defvar dabbrev--last-case-pattern nil
)
304 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
305 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers
)
307 ;; The regexp for recognizing a character in an abbreviation.
308 (defvar dabbrev--abbrev-char-regexp nil
)
310 ;;----------------------------------------------------------------
312 ;;----------------------------------------------------------------
314 ;;; Get the buffer that mini-buffer was activated from
315 (defsubst dabbrev--minibuffer-origin
()
316 (car (cdr (buffer-list))))
318 ;; Make a list of some of the elements of LIST.
319 ;; Check each element of LIST, storing it temporarily in the
320 ;; variable ELEMENT, and include it in the result
321 ;; if CONDITION evaluates non-nil.
322 (defmacro dabbrev-filter-elements
(element list condition
)
323 `(let (dabbrev-result dabbrev-tail
,element
)
324 (setq dabbrev-tail
,list
)
326 (setq ,element
(car dabbrev-tail
))
328 (setq dabbrev-result
(cons ,element dabbrev-result
)))
329 (setq dabbrev-tail
(cdr dabbrev-tail
)))
330 (nreverse dabbrev-result
)))
332 ;;----------------------------------------------------------------
333 ;; Exported functions
334 ;;----------------------------------------------------------------
337 (define-key esc-map
"/" 'dabbrev-expand
)
338 ;;;??? Do we want this?
340 (define-key esc-map
[?\C-
/] 'dabbrev-completion
)
343 (defun dabbrev-completion (&optional arg
)
344 "Completion on current word.
345 Like \\[dabbrev-expand] but finds all expansions in the current buffer
346 and presents suggestions for completion.
348 With a prefix argument, it searches all buffers accepted by the
349 function pointed out by `dabbrev-friend-buffer-function' to find the
352 If the prefix argument is 16 (which comes from C-u C-u),
353 then it searches *all* buffers.
355 With no prefix argument, it reuses an old completion list
356 if there is a suitable one already."
359 (dabbrev--reset-global-variables)
360 (let* ((dabbrev-check-other-buffers (and arg t
))
361 (dabbrev-check-all-buffers
362 (and arg
(= (prefix-numeric-value arg
) 16)))
363 (abbrev (dabbrev--abbrev-at-point))
364 (ignore-case-p (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
366 dabbrev-case-fold-search
)
367 (or (not dabbrev-upcase-means-case-search
)
368 (string= abbrev
(downcase abbrev
)))))
369 (my-obarray dabbrev--last-obarray
)
374 (or (eq dabbrev--last-completion-buffer
(current-buffer))
375 (and (window-minibuffer-p (selected-window))
376 (eq dabbrev--last-completion-buffer
377 (dabbrev--minibuffer-origin))))
378 dabbrev--last-abbreviation
379 (>= (length abbrev
) (length dabbrev--last-abbreviation
))
380 (string= dabbrev--last-abbreviation
382 (length dabbrev--last-abbreviation
)))
383 (setq init
(try-completion abbrev my-obarray
)))
384 ;; We can reuse the existing completion list.
386 ;;--------------------------------
387 ;; New abbreviation to expand.
388 ;;--------------------------------
389 (setq dabbrev--last-abbreviation abbrev
)
390 ;; Find all expansion
391 (let ((completion-list
392 (dabbrev--find-all-expansions abbrev ignore-case-p
))
393 (completion-ignore-case ignore-case-p
))
394 ;; Make an obarray with all expansions
395 (setq my-obarray
(make-vector (length completion-list
) 0))
396 (or (> (length my-obarray
) 0)
397 (error "No dynamic expansion for \"%s\" found%s"
399 (if dabbrev--check-other-buffers
"" " in this-buffer")))
401 ((or (not ignore-case-p
)
402 (not dabbrev-case-replace
))
403 (mapc (function (lambda (string)
404 (intern string my-obarray
)))
406 ((string= abbrev
(upcase abbrev
))
407 (mapc (function (lambda (string)
408 (intern (upcase string
) my-obarray
)))
410 ((string= (substring abbrev
0 1)
411 (upcase (substring abbrev
0 1)))
412 (mapc (function (lambda (string)
413 (intern (capitalize string
) my-obarray
)))
416 (mapc (function (lambda (string)
417 (intern (downcase string
) my-obarray
)))
419 (setq dabbrev--last-obarray my-obarray
)
420 (setq dabbrev--last-completion-buffer
(current-buffer))
421 ;; Find the longest common string.
422 (setq init
(try-completion abbrev my-obarray
)))))
423 ;;--------------------------------
424 ;; Let the user choose between the expansions
425 ;;--------------------------------
429 ;; * Replace string fragment with matched common substring completion.
430 ((and (not (string-equal init
""))
431 (not (string-equal (downcase init
) (downcase abbrev
))))
432 (if (> (length (all-completions init my-obarray
)) 1)
433 (message "Repeat `%s' to see all completions"
434 (key-description (this-command-keys)))
435 (message "The only possible completion"))
436 (dabbrev--substitute-expansion nil abbrev init nil
))
438 ;; * String is a common substring completion already. Make list.
439 (message "Making completion list...")
440 (with-output-to-temp-buffer " *Completions*"
441 (display-completion-list (all-completions init my-obarray
)))
442 (message "Making completion list...done")))
443 (and (window-minibuffer-p (selected-window))
447 (defun dabbrev-expand (arg)
448 "Expand previous word \"dynamically\".
450 Expands to the most recent, preceding word for which this is a prefix.
451 If no suitable preceding word is found, words following point are
452 considered. If still no suitable word is found, then look in the
453 buffers accepted by the function pointed out by variable
454 `dabbrev-friend-buffer-function'.
456 A positive prefix argument, N, says to take the Nth backward *distinct*
457 possibility. A negative argument says search forward.
459 If the cursor has not moved from the end of the previous expansion and
460 no argument is given, replace the previously-made expansion
461 with the next possible expansion not yet tried.
463 The variable `dabbrev-backward-only' may be used to limit the
464 direction of search to backward if set non-nil.
466 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
468 (let (abbrev record-case-pattern
469 expansion old direction
(orig-point (point)))
470 ;; abbrev -- the abbrev to expand
471 ;; expansion -- the expansion found (eventually) or nil until then
472 ;; old -- the text currently in the buffer
473 ;; (the abbrev, or the previously-made expansion)
476 (markerp dabbrev--last-abbrev-location
)
477 (marker-position dabbrev--last-abbrev-location
)
478 (or (eq last-command this-command
)
479 (and (window-minibuffer-p (selected-window))
480 (= dabbrev--last-abbrev-location
482 ;; Find a different expansion for the same abbrev as last time.
484 (setq abbrev dabbrev--last-abbreviation
)
485 (setq old dabbrev--last-expansion
)
486 (setq direction dabbrev--last-direction
))
487 ;; If the user inserts a space after expanding
488 ;; and then asks to expand again, always fetch the next word.
489 (if (and (eq (preceding-char) ?\
)
490 (markerp dabbrev--last-abbrev-location
)
491 (marker-position dabbrev--last-abbrev-location
)
492 (= (point) (1+ dabbrev--last-abbrev-location
)))
494 ;; The "abbrev" to expand is just the space.
497 (if dabbrev--last-buffer
498 (set-buffer dabbrev--last-buffer
))
499 ;; Find the end of the last "expansion" word.
500 (if (or (eq dabbrev--last-direction
1)
501 (and (eq dabbrev--last-direction
0)
502 (< dabbrev--last-expansion-location
(point))))
503 (setq dabbrev--last-expansion-location
504 (+ dabbrev--last-expansion-location
505 (length dabbrev--last-expansion
))))
506 (goto-char dabbrev--last-expansion-location
)
507 ;; Take the following word, with intermediate separators,
508 ;; as our expansion this time.
510 (concat "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
511 (setq expansion
(buffer-substring-no-properties
512 dabbrev--last-expansion-location
(point)))
514 ;; Record the end of this expansion, in case we repeat this.
515 (setq dabbrev--last-expansion-location
(point)))
516 ;; Indicate that dabbrev--last-expansion-location is
517 ;; at the end of the expansion.
518 (setq dabbrev--last-direction -
1))
520 ;; We have a different abbrev to expand.
521 (dabbrev--reset-global-variables)
522 (setq direction
(if (null arg
)
523 (if dabbrev-backward-only
1 0)
524 (prefix-numeric-value arg
)))
525 (setq abbrev
(dabbrev--abbrev-at-point))
526 (setq record-case-pattern t
)
529 ;;--------------------------------
530 ;; Find the expansion
531 ;;--------------------------------
534 (dabbrev--find-expansion abbrev direction
535 (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
537 dabbrev-case-fold-search
)
538 (or (not dabbrev-upcase-means-case-search
)
539 (string= abbrev
(downcase abbrev
))))))))
542 (dabbrev--reset-global-variables)
545 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
546 ;; Put back the original abbrev with its original case pattern.
547 (search-backward old
)
549 (delete-region (point) (+ (point) (length old
)))))
550 (error "No%s dynamic expansion for `%s' found"
551 (if old
" further" "") abbrev
))
553 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found
)
554 (minibuffer-window-active-p (selected-window))))
556 (message "Expansion found in '%s'"
557 (buffer-name dabbrev--last-buffer
))
558 (setq dabbrev--last-buffer-found dabbrev--last-buffer
))
560 (if (and (or (eq (current-buffer) dabbrev--last-buffer
)
561 (null dabbrev--last-buffer
))
562 (numberp dabbrev--last-expansion-location
)
563 (and (> dabbrev--last-expansion-location
(point))))
564 (setq dabbrev--last-expansion-location
565 (copy-marker dabbrev--last-expansion-location
)))
566 ;; Success: stick it in and return.
567 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
568 (dabbrev--substitute-expansion old abbrev expansion
571 ;; Save state for re-expand.
572 (setq dabbrev--last-expansion expansion
)
573 (setq dabbrev--last-abbreviation abbrev
)
574 (setq dabbrev--last-abbrev-location
(point-marker))))))
576 ;;----------------------------------------------------------------
578 ;;----------------------------------------------------------------
580 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
581 (defun dabbrev--same-major-mode-p (other-buffer)
584 (set-buffer other-buffer
)
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
595 (while (and (looking-at dabbrev--abbrev-char-regexp
)
598 (or (looking-at dabbrev--abbrev-char-regexp
)
600 (and dabbrev-abbrev-skip-leading-regexp
601 (while (looking-at dabbrev-abbrev-skip-leading-regexp
)
604 ;;; Extract the symbol at point to serve as abbreviation.
605 (defun dabbrev--abbrev-at-point ()
608 (error "No possible abbreviation preceding point"))
609 ;; Return abbrev at point
611 ;; Record the end of the abbreviation.
612 (setq dabbrev--last-abbrev-location
(point))
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-/.
620 (not (looking-at (concat "\\("
621 (or dabbrev-abbrev-char-regexp
624 (if (re-search-backward (or dabbrev-abbrev-char-regexp
628 (error "No possible abbreviation preceding point"))))
629 ;; Now find the beginning of that one.
630 (dabbrev--goto-start-of-abbrev)
631 (buffer-substring-no-properties
632 dabbrev--last-abbrev-location
(point))))
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
649 dabbrev--check-other-buffers dabbrev-check-other-buffers
))
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 ()
655 (and (window-minibuffer-p (selected-window))
656 (set-buffer (dabbrev--minibuffer-origin)))
657 (let ((orig-buffer (current-buffer)))
658 (dabbrev-filter-elements
660 (and (not (eq orig-buffer buffer
))
661 (boundp 'dabbrev-friend-buffer-function
)
662 (funcall dabbrev-friend-buffer-function buffer
))))))
664 ;;; Search for ABBREV, N times, normally looking forward,
665 ;;; but looking in reverse instead if REVERSE is non-nil.
666 (defun dabbrev--try-find (abbrev reverse n ignore-case
)
670 (let ((expansion nil
))
671 (and dabbrev--last-expansion-location
672 (goto-char dabbrev--last-expansion-location
))
673 (let ((case-fold-search ignore-case
)
675 (while (and (> count
0)
676 (setq expansion
(dabbrev--search abbrev
679 (setq count
(1- count
))))
681 (setq dabbrev--last-expansion-location
(point)))
684 ;;; Find all expansions of ABBREV
685 (defun dabbrev--find-all-expansions (abbrev ignore-case
)
686 (let ((all-expansions nil
)
689 (goto-char (point-min))
690 (while (setq expansion
(dabbrev--find-expansion abbrev -
1 ignore-case
))
691 (setq all-expansions
(cons expansion all-expansions
))))
694 (defun dabbrev--scanning-message ()
695 (message "Scanning `%s'" (buffer-name (current-buffer))))
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
)
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
714 (goto-char (point-min))
716 (dabbrev--scanning-message)))
718 ;; ------------------------------------------
720 ;; ------------------------------------------
721 ((and (not dabbrev-search-these-buffers-only
)
723 (setq dabbrev--last-direction
(min 1 direction
))
724 (setq expansion
(dabbrev--try-find abbrev t
728 ;; ------------------------------------------
730 ;; ------------------------------------------
731 ((and (or (not dabbrev-search-these-buffers-only
)
732 dabbrev--last-buffer
)
734 (setq dabbrev--last-direction -
1)
735 (setq expansion
(dabbrev--try-find abbrev nil
736 (max 1 (- direction
))
739 ;; ------------------------------------------
740 ;; Look in other buffers.
741 ;; Start at (point-min) and look forward.
742 ;; ------------------------------------------
744 (setq dabbrev--last-direction -
1)
745 ;; Make sure that we should check other buffers
746 (or dabbrev--friend-buffer-list
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
)
754 (setq dabbrev--check-other-buffers
755 (y-or-n-p "Scan other buffers also? ")))))
756 (let* (friend-buffer-list non-friend-buffer-list
)
757 (setq dabbrev--friend-buffer-list
758 (funcall dabbrev-select-buffers-function
))
759 (if dabbrev-check-all-buffers
760 (setq non-friend-buffer-list
761 (dabbrev-filter-elements
763 (let ((bn (buffer-name buffer
)))
764 (and (not (member bn dabbrev-ignored-buffer-names
))
765 (not (memq buffer dabbrev--friend-buffer-list
))
767 (let ((tail dabbrev-ignored-buffer-regexps
)
769 (while (and tail
(not match
))
770 (setq match
(string-match (car tail
) bn
)
773 dabbrev--friend-buffer-list
774 (append dabbrev--friend-buffer-list
775 non-friend-buffer-list
)))))
776 ;; Move buffers that are visible on the screen
777 ;; to the front of the list. Remove the current buffer.
778 (when dabbrev--friend-buffer-list
779 (walk-windows (lambda (w)
780 (unless (eq w
(selected-window))
781 (setq dabbrev--friend-buffer-list
782 (cons (window-buffer w
)
783 (delq (window-buffer w
)
784 dabbrev--friend-buffer-list
))))))
785 (setq dabbrev--friend-buffer-list
786 (delq (current-buffer) dabbrev--friend-buffer-list
)))
787 ;; Walk through the buffers
788 (while (and (not expansion
) dabbrev--friend-buffer-list
)
789 (setq dabbrev--last-buffer
790 (car dabbrev--friend-buffer-list
))
791 (setq dabbrev--friend-buffer-list
792 (cdr dabbrev--friend-buffer-list
))
793 (set-buffer dabbrev--last-buffer
)
794 (dabbrev--scanning-message)
795 (setq dabbrev--last-expansion-location
(point-min))
796 (setq expansion
(dabbrev--try-find abbrev nil
1 ignore-case
)))
799 (defun dabbrev--safe-replace-match (string &optional fixedcase literal
)
800 (if (eq major-mode
'picture-mode
)
801 (picture-replace-match string fixedcase literal
)
802 (replace-match string fixedcase literal
)))
804 ;;;----------------------------------------------------------------
805 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern
)
806 "Replace OLD with EXPANSION in the buffer.
807 OLD is text currently in the buffer, perhaps the abbreviation
808 or perhaps another expansion that was tried previously.
809 ABBREV is the abbreviation we are expanding.
810 It is \" \" if we are copying subsequent words.
811 EXPANSION is the expansion substring to be used this time.
812 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
813 to record whether we upcased the expansion, downcased it, or did neither."
815 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
817 dabbrev-case-fold-search
)
818 (or (not dabbrev-upcase-means-case-search
)
819 (string= abbrev
(downcase abbrev
)))
820 (if (eq dabbrev-case-replace
'case-replace
)
822 dabbrev-case-replace
))))
824 ;; If we upcased or downcased the original expansion,
825 ;; do likewise for the subsequent words when we copy them.
826 (and (equal abbrev
" ")
827 dabbrev--last-case-pattern
829 (funcall dabbrev--last-case-pattern expansion
)))
831 ;; If the expansion has mixed case
832 ;; and it is not simply a capitalized word,
833 ;; or if the abbrev has mixed case,
834 ;; and if the given abbrev's case pattern
835 ;; matches the start of the expansion,
836 ;; copy the expansion's case
837 ;; instead of downcasing all the rest.
838 ;; Treat a one-capital-letter abbrev as "not all upper case",
839 ;; so as to force preservation of the expansion's pattern
840 ;; if the expansion starts with a capital letter.
841 (let ((expansion-rest (substring expansion
1)))
842 (if (and (not (and (or (string= expansion-rest
(downcase expansion-rest
))
843 (string= expansion-rest
(upcase expansion-rest
)))
844 (or (string= abbrev
(downcase abbrev
))
845 (and (string= abbrev
(upcase abbrev
))
846 (> (length abbrev
) 1)))))
848 (substring expansion
0 (length abbrev
))))
849 (setq use-case-replace nil
)))
850 (if (equal abbrev
" ")
851 (setq use-case-replace nil
))
853 (setq expansion
(downcase expansion
)))
855 ;; In case we insert subsequent words,
856 ;; record if we upcased or downcased the first word,
857 ;; in order to do likewise for subsequent words.
858 (and record-case-pattern
859 (setq dabbrev--last-case-pattern
860 (and use-case-replace
861 (cond ((equal abbrev
(upcase abbrev
)) 'upcase
)
862 ((equal abbrev
(downcase abbrev
)) 'downcase
)))))
866 (search-backward old
))
867 ;;(set-match-data (list (point-marker) (point-marker)))
868 (search-backward abbrev
))
869 ;; Make case of replacement conform to case of abbreviation
870 ;; provided (1) that kind of thing is enabled in this buffer
871 ;; and (2) the replacement itself is all lower case.
872 (dabbrev--safe-replace-match expansion
873 (not use-case-replace
)
877 ;;;----------------------------------------------------------------
878 ;;; Search function used by dabbrevs library.
880 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
881 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
882 ;;; controls the maximum search region size. Third argument IGNORE-CASE
883 ;;; non-nil means treat case as insignificant while looking for a match
884 ;;; and when comparing with previous matches. Also if that's non-nil
885 ;;; and the match is found at the beginning of a sentence and is in
886 ;;; lower case except for the initial then it is converted to all lower
889 ;;; Table of expansions already seen is examined in buffer
890 ;;; `dabbrev--last-table' so that only distinct possibilities are found
891 ;;; by dabbrev-re-expand.
893 ;;; Value is the expansion, or nil if not found.
895 (defun dabbrev--search (abbrev reverse ignore-case
)
897 (let ((pattern1 (concat (regexp-quote abbrev
)
898 "\\(" dabbrev--abbrev-char-regexp
"\\)"))
899 (pattern2 (concat (regexp-quote abbrev
)
900 "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
905 (narrow-to-region dabbrev--last-expansion-location
907 (if reverse
(- dabbrev-limit
) dabbrev-limit
))))
908 ;;--------------------------------
909 ;; Look for a distinct expansion, using dabbrev--last-table.
910 ;;--------------------------------
911 (while (and (not found-string
)
913 (re-search-backward pattern1 nil t
)
914 (re-search-forward pattern1 nil t
)))
915 (goto-char (match-beginning 0))
916 ;; In case we matched in the middle of a word,
917 ;; back up to start of word and verify we still match.
918 (dabbrev--goto-start-of-abbrev)
920 (if (not (looking-at pattern1
))
922 ;; We have a truly valid match. Find the end.
923 (re-search-forward pattern2
)
924 (setq found-string
(buffer-substring-no-properties
925 (match-beginning 1) (match-end 1)))
926 (and ignore-case
(setq found-string
(downcase found-string
)))
927 ;; Ignore this match if it's already in the table.
928 (if (dabbrev-filter-elements
929 table-string dabbrev--last-table
930 (string= found-string table-string
))
931 (setq found-string nil
)))
932 ;; Prepare to continue searching.
934 (goto-char (match-beginning 0))
935 (goto-char (match-end 0))))
936 ;; If we found something, use it.
938 ;; Put it into `dabbrev--last-table'
939 ;; and return it (either downcased, or as is).
940 (let ((result (buffer-substring-no-properties
941 (match-beginning 0) (match-end 0))))
942 (setq dabbrev--last-table
943 (cons found-string dabbrev--last-table
))
944 (if (and ignore-case
(eval dabbrev-case-replace
))
948 (dolist (mess '("^No dynamic expansion for .* found$"
949 "^No further dynamic expansion for .* found$"
950 "^No possible abbreviation preceding point$"))
951 (add-to-list 'debug-ignored-errors mess
))
955 ;;; dabbrev.el ends here