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--eliminate-newlines t
132 "*Non-nil means dabbrev should not insert newlines.
133 Instead it converts them to spaces."
137 (defcustom dabbrev-case-fold-search
'case-fold-search
138 "*Control whether dabbrev searches should ignore case.
139 A value of nil means case is significant.
140 A value of `case-fold-search' means case is significant
141 if `case-fold-search' is nil.
142 Any other non-nil version means case is not significant."
143 :type
'(choice (const :tag
"off" nil
)
144 (const :tag
"like search" case-fold-search
)
148 (defcustom dabbrev-upcase-means-case-search nil
149 "*The significance of an uppercase character in an abbreviation.
150 nil means case fold search, non-nil means case sensitive search.
152 This variable has an effect only when the value of
153 `dabbrev-case-fold-search' says to ignore case."
157 (defcustom dabbrev-case-replace
'case-replace
158 "*Controls whether dabbrev preserves case when expanding the abbreviation.
159 A value of nil means preserve case.
160 A value of `case-replace' means preserve case if `case-replace' is nil.
161 Any other non-nil version means do not preserve case.
163 This variable has an effect only when the value of
164 `dabbrev-case-fold-search' specifies to ignore case."
165 :type
'(choice (const :tag
"off" nil
)
166 (const :tag
"like M-x query-replace" case-replace
)
170 (defcustom dabbrev-abbrev-char-regexp nil
171 "*Regexp to recognize a character in an abbreviation or expansion.
172 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
174 Set this variable to \"\\\\sw\" if you want ordinary words or
175 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
176 syntax is \"symbol\" as well as those whose syntax is \"word\".
178 The value nil has a special meaning: the abbreviation is from point to
179 previous word-start, but the search is for symbols.
181 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
182 while `yes', `or', `no' and `p' are considered words. If this
183 variable is nil, then expanding `yes-or-no-' looks for a symbol
184 starting with or containing `no-'. If you set this variable to
185 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
186 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
187 expanding `yes-or-no-' signals an error because `-' is not part of a word;
188 but expanding `yes-or-no' looks for a word starting with `no'.
190 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
191 :type
'(choice (const nil
)
195 (defcustom dabbrev-check-all-buffers t
196 "*Non-nil means dabbrev package should search *all* buffers.
198 Dabbrev always searches the current buffer first. Then, if
199 `dabbrev-check-other-buffers' says so, it searches the buffers
200 designated by `dabbrev-select-buffers-function'.
202 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
203 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
204 or matched by `dabbrev-ignored-regexps'."
208 (defcustom dabbrev-ignored-buffer-names
'("*Messages*" "*Buffer List*")
209 "*List of buffer names that dabbrev should not check.
210 See also `dabbrev-ignored-buffer-regexps'."
211 :type
'(repeat (string :tag
"Buffer name"))
215 (defcustom dabbrev-ignored-buffer-regexps nil
216 "*List of regexps matching names of buffers that dabbrev should not check.
217 See also `dabbrev-ignored-buffer-names'."
218 :type
'(repeat regexp
)
222 (defcustom dabbrev-check-other-buffers t
223 "*Should \\[dabbrev-expand] look in other buffers?\
225 nil: Don't look in other buffers.
226 t: Also look for expansions in the buffers pointed out by
227 `dabbrev-select-buffers-function'.
228 Anything else: When we can't find any more expansions in
229 the current buffer, then ask the user whether to look in other
232 The default value is t."
233 :type
'(choice (const :tag
"off" nil
)
235 (other :tag
"ask" other
))
238 ;; I guess setting this to a function that selects all C- or C++-
239 ;; mode buffers would be a good choice for a debugging buffer,
240 ;; when debugging C- or C++-code.
241 (defvar dabbrev-select-buffers-function
'dabbrev--select-buffers
242 "A function that selects buffers that should be searched by dabbrev.
243 The function should take no arguments and return a list of buffers to
244 search for expansions. See the source of `dabbrev--select-buffers'
247 A mode setting this variable should make it buffer local.")
249 (defcustom dabbrev-friend-buffer-function
'dabbrev--same-major-mode-p
250 "*A function to decide whether dabbrev should search OTHER-BUFFER.
251 The function should take one argument, OTHER-BUFFER, and return
252 non-nil if that buffer should be searched. Have a look at
253 `dabbrev--same-major-mode-p' for an example.
255 The value of `dabbrev-friend-buffer-function' has an effect only if
256 the value of `dabbrev-select-buffers-function' uses it. The function
257 `dabbrev--select-buffers' is one function you can use here.
259 A mode setting this variable should make it buffer local."
263 (defcustom dabbrev-search-these-buffers-only nil
264 "If non-nil, a list of buffers which dabbrev should search.
265 If this variable is non-nil, dabbrev will only look in these buffers.
266 It will not even look in the current buffer if it is not a member of
269 ;;----------------------------------------------------------------
270 ;; Internal variables
271 ;;----------------------------------------------------------------
273 ;; Last obarray of completions in `dabbrev-completion'
274 (defvar dabbrev--last-obarray nil
)
276 ;; Table of expansions seen so far
277 (defvar dabbrev--last-table nil
)
279 ;; Last string we tried to expand.
280 (defvar dabbrev--last-abbreviation nil
)
282 ;; Location last abbreviation began
283 (defvar dabbrev--last-abbrev-location nil
)
285 ;; Direction of last dabbrevs search
286 (defvar dabbrev--last-direction
0)
288 ;; Last expansion of an abbreviation.
289 (defvar dabbrev--last-expansion nil
)
291 ;; Location the last expansion was found.
292 (defvar dabbrev--last-expansion-location nil
)
294 ;; The list of remaining buffers with the same mode as current buffer.
295 (defvar dabbrev--friend-buffer-list nil
)
297 ;; The buffer we looked in last, not counting the current buffer.
298 (defvar dabbrev--last-buffer nil
)
300 ;; The buffer we found the expansion last time.
301 (defvar dabbrev--last-buffer-found nil
)
303 ;; The buffer we last did a completion in.
304 (defvar dabbrev--last-completion-buffer nil
)
306 ;; If non-nil, a function to use when copying successive words.
307 ;; It should be `upcase' or `downcase'.
308 (defvar dabbrev--last-case-pattern nil
)
310 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
311 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers
)
313 ;; The regexp for recognizing a character in an abbreviation.
314 (defvar dabbrev--abbrev-char-regexp nil
)
316 ;;----------------------------------------------------------------
318 ;;----------------------------------------------------------------
320 ;;; Get the buffer that mini-buffer was activated from
321 (defsubst dabbrev--minibuffer-origin
()
322 (car (cdr (buffer-list))))
324 ;; Make a list of some of the elements of LIST.
325 ;; Check each element of LIST, storing it temporarily in the
326 ;; variable ELEMENT, and include it in the result
327 ;; if CONDITION evaluates non-nil.
328 (defmacro dabbrev-filter-elements
(element list condition
)
329 `(let (dabbrev-result dabbrev-tail
,element
)
330 (setq dabbrev-tail
,list
)
332 (setq ,element
(car dabbrev-tail
))
334 (setq dabbrev-result
(cons ,element dabbrev-result
)))
335 (setq dabbrev-tail
(cdr dabbrev-tail
)))
336 (nreverse dabbrev-result
)))
338 ;;----------------------------------------------------------------
339 ;; Exported functions
340 ;;----------------------------------------------------------------
343 (define-key esc-map
"/" 'dabbrev-expand
)
344 ;;;??? Do we want this?
346 (define-key esc-map
[?\C-
/] 'dabbrev-completion
)
349 (defun dabbrev-completion (&optional arg
)
350 "Completion on current word.
351 Like \\[dabbrev-expand] but finds all expansions in the current buffer
352 and presents suggestions for completion.
354 With a prefix argument, it searches all buffers accepted by the
355 function pointed out by `dabbrev-friend-buffer-function' to find the
358 If the prefix argument is 16 (which comes from C-u C-u),
359 then it searches *all* buffers.
361 With no prefix argument, it reuses an old completion list
362 if there is a suitable one already."
365 (dabbrev--reset-global-variables)
366 (let* ((dabbrev-check-other-buffers (and arg t
))
367 (dabbrev-check-all-buffers
368 (and arg
(= (prefix-numeric-value arg
) 16)))
369 (abbrev (dabbrev--abbrev-at-point))
370 (ignore-case-p (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
372 dabbrev-case-fold-search
)
373 (or (not dabbrev-upcase-means-case-search
)
374 (string= abbrev
(downcase abbrev
)))))
375 (my-obarray dabbrev--last-obarray
)
380 (or (eq dabbrev--last-completion-buffer
(current-buffer))
381 (and (window-minibuffer-p (selected-window))
382 (eq dabbrev--last-completion-buffer
383 (dabbrev--minibuffer-origin))))
384 dabbrev--last-abbreviation
385 (>= (length abbrev
) (length dabbrev--last-abbreviation
))
386 (string= dabbrev--last-abbreviation
388 (length dabbrev--last-abbreviation
)))
389 (setq init
(try-completion abbrev my-obarray
)))
390 ;; We can reuse the existing completion list.
392 ;;--------------------------------
393 ;; New abbreviation to expand.
394 ;;--------------------------------
395 (setq dabbrev--last-abbreviation abbrev
)
396 ;; Find all expansion
397 (let ((completion-list
398 (dabbrev--find-all-expansions abbrev ignore-case-p
))
399 (completion-ignore-case ignore-case-p
))
400 ;; Make an obarray with all expansions
401 (setq my-obarray
(make-vector (length completion-list
) 0))
402 (or (> (length my-obarray
) 0)
403 (error "No dynamic expansion for \"%s\" found%s"
405 (if dabbrev--check-other-buffers
"" " in this-buffer")))
407 ((or (not ignore-case-p
)
408 (not dabbrev-case-replace
))
409 (mapc (function (lambda (string)
410 (intern string my-obarray
)))
412 ((string= abbrev
(upcase abbrev
))
413 (mapc (function (lambda (string)
414 (intern (upcase string
) my-obarray
)))
416 ((string= (substring abbrev
0 1)
417 (upcase (substring abbrev
0 1)))
418 (mapc (function (lambda (string)
419 (intern (capitalize string
) my-obarray
)))
422 (mapc (function (lambda (string)
423 (intern (downcase string
) my-obarray
)))
425 (setq dabbrev--last-obarray my-obarray
)
426 (setq dabbrev--last-completion-buffer
(current-buffer))
427 ;; Find the longest common string.
428 (setq init
(try-completion abbrev my-obarray
)))))
429 ;;--------------------------------
430 ;; Let the user choose between the expansions
431 ;;--------------------------------
435 ;; * Replace string fragment with matched common substring completion.
436 ((and (not (string-equal init
""))
437 (not (string-equal (downcase init
) (downcase abbrev
))))
438 (if (> (length (all-completions init my-obarray
)) 1)
439 (message "Repeat `%s' to see all completions"
440 (key-description (this-command-keys)))
441 (message "The only possible completion"))
442 (dabbrev--substitute-expansion nil abbrev init nil
))
444 ;; * String is a common substring completion already. Make list.
445 (message "Making completion list...")
446 (with-output-to-temp-buffer " *Completions*"
447 (display-completion-list (all-completions init my-obarray
)))
448 (message "Making completion list...done")))
449 (and (window-minibuffer-p (selected-window))
453 (defun dabbrev-expand (arg)
454 "Expand previous word \"dynamically\".
456 Expands to the most recent, preceding word for which this is a prefix.
457 If no suitable preceding word is found, words following point are
458 considered. If still no suitable word is found, then look in the
459 buffers accepted by the function pointed out by variable
460 `dabbrev-friend-buffer-function'.
462 A positive prefix argument, N, says to take the Nth backward *distinct*
463 possibility. A negative argument says search forward.
465 If the cursor has not moved from the end of the previous expansion and
466 no argument is given, replace the previously-made expansion
467 with the next possible expansion not yet tried.
469 The variable `dabbrev-backward-only' may be used to limit the
470 direction of search to backward if set non-nil.
472 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
474 (let (abbrev record-case-pattern
475 expansion old direction
(orig-point (point)))
476 ;; abbrev -- the abbrev to expand
477 ;; expansion -- the expansion found (eventually) or nil until then
478 ;; old -- the text currently in the buffer
479 ;; (the abbrev, or the previously-made expansion)
482 (markerp dabbrev--last-abbrev-location
)
483 (marker-position dabbrev--last-abbrev-location
)
484 (or (eq last-command this-command
)
485 (and (window-minibuffer-p (selected-window))
486 (= dabbrev--last-abbrev-location
488 ;; Find a different expansion for the same abbrev as last time.
490 (setq abbrev dabbrev--last-abbreviation
)
491 (setq old dabbrev--last-expansion
)
492 (setq direction dabbrev--last-direction
))
493 ;; If the user inserts a space after expanding
494 ;; and then asks to expand again, always fetch the next word.
495 (if (and (eq (preceding-char) ?\
)
496 (markerp dabbrev--last-abbrev-location
)
497 (marker-position dabbrev--last-abbrev-location
)
498 (= (point) (1+ dabbrev--last-abbrev-location
)))
500 ;; The "abbrev" to expand is just the space.
503 (if dabbrev--last-buffer
504 (set-buffer dabbrev--last-buffer
))
505 ;; Find the end of the last "expansion" word.
506 (if (or (eq dabbrev--last-direction
1)
507 (and (eq dabbrev--last-direction
0)
508 (< dabbrev--last-expansion-location
(point))))
509 (setq dabbrev--last-expansion-location
510 (+ dabbrev--last-expansion-location
511 (length dabbrev--last-expansion
))))
512 (goto-char dabbrev--last-expansion-location
)
513 ;; Take the following word, with intermediate separators,
514 ;; as our expansion this time.
516 (concat "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
517 (setq expansion
(buffer-substring-no-properties
518 dabbrev--last-expansion-location
(point)))
520 ;; Record the end of this expansion, in case we repeat this.
521 (setq dabbrev--last-expansion-location
(point)))
522 ;; Indicate that dabbrev--last-expansion-location is
523 ;; at the end of the expansion.
524 (setq dabbrev--last-direction -
1))
526 ;; We have a different abbrev to expand.
527 (dabbrev--reset-global-variables)
528 (setq direction
(if (null arg
)
529 (if dabbrev-backward-only
1 0)
530 (prefix-numeric-value arg
)))
531 (setq abbrev
(dabbrev--abbrev-at-point))
532 (setq record-case-pattern t
)
535 ;;--------------------------------
536 ;; Find the expansion
537 ;;--------------------------------
540 (dabbrev--find-expansion abbrev direction
541 (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
543 dabbrev-case-fold-search
)
544 (or (not dabbrev-upcase-means-case-search
)
545 (string= abbrev
(downcase abbrev
))))))))
548 (dabbrev--reset-global-variables)
551 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
552 ;; Put back the original abbrev with its original case pattern.
553 (search-backward old
)
555 (delete-region (point) (+ (point) (length old
)))))
556 (error "No%s dynamic expansion for `%s' found"
557 (if old
" further" "") abbrev
))
559 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found
)
560 (minibuffer-window-active-p (selected-window))))
562 (message "Expansion found in '%s'"
563 (buffer-name dabbrev--last-buffer
))
564 (setq dabbrev--last-buffer-found dabbrev--last-buffer
))
566 (if (and (or (eq (current-buffer) dabbrev--last-buffer
)
567 (null dabbrev--last-buffer
))
568 (numberp dabbrev--last-expansion-location
)
569 (and (> dabbrev--last-expansion-location
(point))))
570 (setq dabbrev--last-expansion-location
571 (copy-marker dabbrev--last-expansion-location
)))
572 ;; Success: stick it in and return.
573 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
574 (dabbrev--substitute-expansion old abbrev expansion
577 ;; Save state for re-expand.
578 (setq dabbrev--last-expansion expansion
)
579 (setq dabbrev--last-abbreviation abbrev
)
580 (setq dabbrev--last-abbrev-location
(point-marker))))))
582 ;;----------------------------------------------------------------
584 ;;----------------------------------------------------------------
586 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
587 (defun dabbrev--same-major-mode-p (other-buffer)
590 (set-buffer other-buffer
)
593 ;;; Back over all abbrev type characters and then moves forward over
594 ;;; all skip characters.
595 (defun dabbrev--goto-start-of-abbrev ()
596 ;; Move backwards over abbrev chars
601 (while (and (looking-at dabbrev--abbrev-char-regexp
)
604 (or (looking-at dabbrev--abbrev-char-regexp
)
606 (and dabbrev-abbrev-skip-leading-regexp
607 (while (looking-at dabbrev-abbrev-skip-leading-regexp
)
610 ;;; Extract the symbol at point to serve as abbreviation.
611 (defun dabbrev--abbrev-at-point ()
614 (error "No possible abbreviation preceding point"))
615 ;; Return abbrev at point
617 ;; Record the end of the abbreviation.
618 (setq dabbrev--last-abbrev-location
(point))
619 ;; If we aren't right after an abbreviation,
620 ;; move point back to just after one.
621 ;; This is so the user can get successive words
622 ;; by typing the punctuation followed by M-/.
626 (not (looking-at (concat "\\("
627 (or dabbrev-abbrev-char-regexp
630 (if (re-search-backward (or dabbrev-abbrev-char-regexp
634 (error "No possible abbreviation preceding point"))))
635 ;; Now find the beginning of that one.
636 (dabbrev--goto-start-of-abbrev)
637 (buffer-substring-no-properties
638 dabbrev--last-abbrev-location
(point))))
640 ;;; Initializes all global variables
641 (defun dabbrev--reset-global-variables ()
642 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
643 ;; must not be reset here.
644 (setq dabbrev--last-table nil
645 dabbrev--last-abbreviation nil
646 dabbrev--last-abbrev-location nil
647 dabbrev--last-direction nil
648 dabbrev--last-expansion nil
649 dabbrev--last-expansion-location nil
650 dabbrev--friend-buffer-list nil
651 dabbrev--last-buffer nil
652 dabbrev--last-buffer-found nil
653 dabbrev--abbrev-char-regexp
(or dabbrev-abbrev-char-regexp
655 dabbrev--check-other-buffers dabbrev-check-other-buffers
))
657 (defun dabbrev--select-buffers ()
658 "Return a list of other buffers to search for a possible abbrev.
659 The current buffer is not included in the list.
661 This function makes a list of all the buffers returned by `buffer-list',
662 then discards buffers whose names match `dabbrev-ignored-buffer-names'
663 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
664 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
665 with the buffer as argument.
666 It returns the list of the buffers that are not discarded."
667 (dabbrev-filter-elements
669 (and (not (eq (current-buffer) buffer
))
670 (not (dabbrev--ignore-buffer-p buffer
))
671 (boundp 'dabbrev-friend-buffer-function
)
672 (funcall dabbrev-friend-buffer-function buffer
))))
674 (defun dabbrev--try-find (abbrev reverse n ignore-case
)
675 "Search for ABBREV, backwards if REVERSE, N times.
676 If IGNORE-CASE is non-nil, ignore case while searching.
677 Return the expansion found, and save the location of the start
678 of the expansion in `dabbrev--last-expansion-location'."
682 (let ((expansion nil
))
683 (and dabbrev--last-expansion-location
684 (goto-char dabbrev--last-expansion-location
))
685 (let ((case-fold-search ignore-case
)
687 (while (and (> count
0)
688 (setq expansion
(dabbrev--search abbrev
691 (setq count
(1- count
))))
693 (setq dabbrev--last-expansion-location
(point)))
696 (defun dabbrev--find-all-expansions (abbrev ignore-case
)
697 "Return a list of all possible expansions of ABBREV.
698 If IGNORE-CASE is non-nil, accept matches which differ in case."
699 (let ((all-expansions nil
)
702 (goto-char (point-min))
703 (while (setq expansion
(dabbrev--find-expansion abbrev -
1 ignore-case
))
704 (setq all-expansions
(cons expansion all-expansions
))))
707 (defun dabbrev--scanning-message ()
708 (unless (window-minibuffer-p (selected-window))
709 (message "Scanning `%s'" (buffer-name (current-buffer)))))
711 (defun dabbrev--ignore-buffer-p (buffer)
712 "Return non-nil if BUFFER should be ignored by dabbrev."
713 (let ((bn (buffer-name buffer
)))
714 (or (member bn dabbrev-ignored-buffer-names
)
715 (let ((tail dabbrev-ignored-buffer-regexps
)
717 (while (and tail
(not match
))
718 (setq match
(string-match (car tail
) bn
)
722 (defun dabbrev--find-expansion (abbrev direction ignore-case
)
723 "Find one occurrence of ABBREV, and return the expansion.
724 DIRECTION > 0 means look that many times backwards.
725 DIRECTION < 0 means look that many times forward.
726 DIRECTION = 0 means try both backward and forward.
727 IGNORE-CASE non-nil means ignore case when searching.
728 This sets `dabbrev--last-direction' to 1 or -1 according
729 to the direction in which the occurrence was actually found.
730 It sets `dabbrev--last-expansion-location' to the location
731 of the start of the occurrence."
733 ;; If we were scanning something other than the current buffer,
734 ;; continue scanning there.
735 (when dabbrev--last-buffer
736 (set-buffer dabbrev--last-buffer
)
737 (dabbrev--scanning-message))
739 ;; ------------------------------------------
740 ;; Look backward in current buffer.
741 ;; ------------------------------------------
742 (and (not dabbrev-search-these-buffers-only
)
744 (setq dabbrev--last-direction
(min 1 direction
))
745 (dabbrev--try-find abbrev t
748 ;; ------------------------------------------
749 ;; Look forward in current buffer
750 ;; or whatever buffer we were last scanning.
751 ;; ------------------------------------------
752 (and (or (not dabbrev-search-these-buffers-only
)
753 dabbrev--last-buffer
)
755 (setq dabbrev--last-direction -
1)
756 (dabbrev--try-find abbrev nil
757 (max 1 (- direction
))
759 ;; ------------------------------------------
760 ;; Look in other buffers.
761 ;; Always start at (point-min) and look forward.
762 ;; ------------------------------------------
764 (setq dabbrev--last-direction -
1)
765 (unless dabbrev--last-buffer
766 ;; If we have just now begun to search other buffers,
767 ;; determine which other buffers we should check.
768 ;; Put that list in dabbrev--friend-buffer-list.
769 (or dabbrev--friend-buffer-list
770 (setq dabbrev--friend-buffer-list
771 (dabbrev--make-friend-buffer-list))))
772 ;; Walk through the buffers till we find a match.
774 (while (and (not expansion
) dabbrev--friend-buffer-list
)
775 (setq dabbrev--last-buffer
776 (car dabbrev--friend-buffer-list
))
777 (setq dabbrev--friend-buffer-list
778 (cdr dabbrev--friend-buffer-list
))
779 (set-buffer dabbrev--last-buffer
)
780 (dabbrev--scanning-message)
781 (setq dabbrev--last-expansion-location
(point-min))
782 (setq expansion
(dabbrev--try-find abbrev nil
1 ignore-case
)))
785 ;; Compute the list of buffers to scan.
786 ;; If dabbrev-search-these-buffers-only, then the current buffer
787 ;; is included in this list if it should be searched.
788 ;; Otherwise, the current buffer is searched first specially.,
789 ;; and it is not included in this list.
790 (defun dabbrev--make-friend-buffer-list ()
791 (let ((list (mapcar (function get-buffer
)
792 dabbrev-search-these-buffers-only
)))
793 (when (and (null dabbrev-search-these-buffers-only
)
794 dabbrev--check-other-buffers
795 (or (eq dabbrev--check-other-buffers t
)
796 (setq dabbrev--check-other-buffers
797 (y-or-n-p "Scan other buffers also? "))))
798 (setq list
(funcall dabbrev-select-buffers-function
))
799 ;; If dabbrev-check-all-buffers, tack on all the other
800 ;; buffers at the end of the list, except those which are
801 ;; specifically to be ignored.
802 (if dabbrev-check-all-buffers
805 (dabbrev-filter-elements
807 (and (not (memq buffer list
))
808 (not (dabbrev--ignore-buffer-p buffer
)))))))
809 ;; Remove the current buffer.
810 (setq list
(delq (current-buffer) list
)))
811 ;; Move buffers in the list that are visible on the screen
812 ;; to the front of the list, but don't add anything to the list.
814 (walk-windows (lambda (w)
815 (unless (eq w
(selected-window))
816 (if (memq (window-buffer w
) list
)
818 (cons (window-buffer w
)
819 (delq (window-buffer w
)
821 ;; In a minibuffer, search the buffer it was activated from,
822 ;; first after the minibuffer itself. Unless we aren't supposed
823 ;; to search the current buffer either.
824 (if (and (window-minibuffer-p (selected-window))
825 (not dabbrev-search-these-buffers-only
))
827 (cons (dabbrev--minibuffer-origin)
828 (delq (dabbrev--minibuffer-origin) list
))))
831 (defun dabbrev--safe-replace-match (string &optional fixedcase literal
)
832 (if (eq major-mode
'picture-mode
)
833 (picture-replace-match string fixedcase literal
)
834 (replace-match string fixedcase literal
)))
836 ;;;----------------------------------------------------------------
837 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern
)
838 "Replace OLD with EXPANSION in the buffer.
839 OLD is text currently in the buffer, perhaps the abbreviation
840 or perhaps another expansion that was tried previously.
841 ABBREV is the abbreviation we are expanding.
842 It is \" \" if we are copying subsequent words.
843 EXPANSION is the expansion substring to be used this time.
844 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
845 to record whether we upcased the expansion, downcased it, or did neither."
847 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
849 dabbrev-case-fold-search
)
850 (or (not dabbrev-upcase-means-case-search
)
851 (string= abbrev
(downcase abbrev
)))
852 (if (eq dabbrev-case-replace
'case-replace
)
854 dabbrev-case-replace
))))
856 ;; If we upcased or downcased the original expansion,
857 ;; do likewise for the subsequent words when we copy them.
858 ;; Don't do any of the usual case processing, though.
859 (when (equal abbrev
" ")
860 (if dabbrev--last-case-pattern
862 (funcall dabbrev--last-case-pattern expansion
)))
863 (setq use-case-replace nil
))
865 ;; If the expansion has mixed case
866 ;; and it is not simply a capitalized word,
867 ;; or if the abbrev has mixed case,
868 ;; and if the given abbrev's case pattern
869 ;; matches the start of the expansion,
870 ;; copy the expansion's case
871 ;; instead of downcasing all the rest.
872 ;; Treat a one-capital-letter abbrev as "not all upper case",
873 ;; so as to force preservation of the expansion's pattern
874 ;; if the expansion starts with a capital letter.
875 (let ((expansion-rest (substring expansion
1)))
876 (if (and (not (and (or (string= expansion-rest
(downcase expansion-rest
))
877 (string= expansion-rest
(upcase expansion-rest
)))
878 (or (string= abbrev
(downcase abbrev
))
879 (and (string= abbrev
(upcase abbrev
))
880 (> (length abbrev
) 1)))))
882 (substring expansion
0 (length abbrev
))))
883 (setq use-case-replace nil
)))
885 ;; If the abbrev and the expansion are both all-lower-case
886 ;; then don't do any conversion. The conversion would be a no-op
887 ;; for this replacement, but it would carry forward to subsequent words.
888 ;; The goal of this is to preven that carrying forward.
889 (if (and (string= expansion
(downcase expansion
))
890 (string= abbrev
(downcase abbrev
)))
891 (setq use-case-replace nil
))
894 (setq expansion
(downcase expansion
)))
896 ;; In case we insert subsequent words,
897 ;; record if we upcased or downcased the first word,
898 ;; in order to do likewise for subsequent words.
899 (and record-case-pattern
900 (setq dabbrev--last-case-pattern
901 (and use-case-replace
902 (cond ((equal abbrev
(upcase abbrev
)) 'upcase
)
903 ((equal abbrev
(downcase abbrev
)) 'downcase
)))))
905 ;; Convert newlines to spaces.
906 (if dabbrev--eliminate-newlines
907 (while (string-match "\n" expansion
)
908 (setq expansion
(replace-match " " nil nil expansion
))))
912 (search-backward old
))
913 ;;(set-match-data (list (point-marker) (point-marker)))
914 (search-backward abbrev
)
915 (search-forward abbrev
))
917 ;; Make case of replacement conform to case of abbreviation
918 ;; provided (1) that kind of thing is enabled in this buffer
919 ;; and (2) the replacement itself is all lower case.
920 (dabbrev--safe-replace-match expansion
921 (not use-case-replace
)
925 ;;;----------------------------------------------------------------
926 ;;; Search function used by dabbrevs library.
929 (defun dabbrev--search (abbrev reverse ignore-case
)
930 "Search for something that could be used to expand ABBREV.
932 Second arg, REVERSE, is t for reverse search, nil for forward.
933 The variable `dabbrev-limit' controls the maximum search region size.
934 Third argument IGNORE-CASE non-nil means treat case as insignificant while
935 looking for a match and when comparing with previous matches. Also if
936 that's non-nil and the match is found at the beginning of a sentence
937 and is in lower case except for the initial then it is converted to
938 all lower case for return.
940 Table of expansions already seen is examined in buffer
941 `dabbrev--last-table' so that only distinct possibilities are found
942 by dabbrev-re-expand.
944 Returns the expansion found, or nil if not found.
945 Leaves point at the location of the start of the expansion."
947 (let ((pattern1 (concat (regexp-quote abbrev
)
948 "\\(" dabbrev--abbrev-char-regexp
"\\)"))
949 (pattern2 (concat (regexp-quote abbrev
)
950 "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
955 (narrow-to-region dabbrev--last-expansion-location
957 (if reverse
(- dabbrev-limit
) dabbrev-limit
))))
958 ;;--------------------------------
959 ;; Look for a distinct expansion, using dabbrev--last-table.
960 ;;--------------------------------
961 (while (and (not found-string
)
963 (re-search-backward pattern1 nil t
)
964 (re-search-forward pattern1 nil t
)))
965 (goto-char (match-beginning 0))
966 ;; In case we matched in the middle of a word,
967 ;; back up to start of word and verify we still match.
968 (dabbrev--goto-start-of-abbrev)
970 (if (not (looking-at pattern1
))
972 ;; We have a truly valid match. Find the end.
973 (re-search-forward pattern2
)
974 (setq found-string
(buffer-substring-no-properties
975 (match-beginning 1) (match-end 1)))
976 (and ignore-case
(setq found-string
(downcase found-string
)))
977 ;; Ignore this match if it's already in the table.
978 (if (dabbrev-filter-elements
979 table-string dabbrev--last-table
980 (string= found-string table-string
))
981 (setq found-string nil
)))
982 ;; Prepare to continue searching.
984 (goto-char (match-beginning 0))
985 (goto-char (match-end 0))))
986 ;; If we found something, use it.
988 ;; Put it into `dabbrev--last-table'
989 ;; and return it (either downcased, or as is).
990 (let ((result (buffer-substring-no-properties
991 (match-beginning 0) (match-end 0))))
992 (setq dabbrev--last-table
993 (cons found-string dabbrev--last-table
))
994 (if (and ignore-case
(eval dabbrev-case-replace
))
998 (dolist (mess '("^No dynamic expansion for .* found$"
999 "^No further dynamic expansion for .* found$"
1000 "^No possible abbreviation preceding point$"))
1001 (add-to-list 'debug-ignored-errors mess
))
1005 ;;; dabbrev.el ends here