1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1996, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007 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., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, 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 when searching for possible expansions;
151 non-nil means case sensitive search.
153 This variable has an effect only when the value of
154 `dabbrev-case-fold-search' says to ignore case."
158 (defcustom dabbrev-case-distinction
'case-replace
159 "*Whether dabbrev treats expansions as the same if they differ in case.
161 A value of nil means treat them as different.
162 A value of `case-replace' means distinguish them if `case-replace' is nil.
163 Any other non-nil value means to treat them as the same.
165 This variable has an effect only when the value of
166 `dabbrev-case-fold-search' specifies to ignore case."
167 :type
'(choice (const :tag
"off" nil
)
168 (const :tag
"based on `case-replace'" case-replace
)
173 (defcustom dabbrev-case-replace
'case-replace
174 "*Whether dabbrev applies the abbreviations's case pattern to the expansion.
176 A value of nil means preserve the expansion's case pattern.
177 A value of `case-replace' means preserve it if `case-replace' is nil.
178 Any other non-nil value means modify the expansion
179 by applying the abbreviation's case pattern to it.
181 This variable has an effect only when the value of
182 `dabbrev-case-fold-search' specifies to ignore case."
183 :type
'(choice (const :tag
"off" nil
)
184 (const :tag
"based on `case-replace'" case-replace
)
188 (defcustom dabbrev-abbrev-char-regexp nil
189 "*Regexp to recognize a character in an abbreviation or expansion.
190 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
192 Set this variable to \"\\\\sw\" if you want ordinary words or
193 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
194 syntax is \"symbol\" as well as those whose syntax is \"word\".
196 The value nil has a special meaning: the abbreviation is from point to
197 previous word-start, but the search is for symbols.
199 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
200 while `yes', `or', `no' and `p' are considered words. If this
201 variable is nil, then expanding `yes-or-no-' looks for a symbol
202 starting with or containing `no-'. If you set this variable to
203 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
204 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
205 expanding `yes-or-no-' signals an error because `-' is not part of a word;
206 but expanding `yes-or-no' looks for a word starting with `no'.
208 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
209 :type
'(choice (const nil
)
213 (defcustom dabbrev-check-all-buffers t
214 "*Non-nil means dabbrev package should search *all* buffers.
216 Dabbrev always searches the current buffer first. Then, if
217 `dabbrev-check-other-buffers' says so, it searches the buffers
218 designated by `dabbrev-select-buffers-function'.
220 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
221 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
222 or matched by `dabbrev-ignored-regexps'."
226 (defcustom dabbrev-ignored-buffer-names
'("*Messages*" "*Buffer List*")
227 "*List of buffer names that dabbrev should not check.
228 See also `dabbrev-ignored-buffer-regexps'."
229 :type
'(repeat (string :tag
"Buffer name"))
233 (defcustom dabbrev-ignored-buffer-regexps nil
234 "*List of regexps matching names of buffers that dabbrev should not check.
235 See also `dabbrev-ignored-buffer-names'."
236 :type
'(repeat regexp
)
240 (defcustom dabbrev-check-other-buffers t
241 "*Should \\[dabbrev-expand] look in other buffers?\
243 nil: Don't look in other buffers.
244 t: Also look for expansions in the buffers pointed out by
245 `dabbrev-select-buffers-function'.
246 Anything else: When we can't find any more expansions in
247 the current buffer, then ask the user whether to look in other
250 The default value is t."
251 :type
'(choice (const :tag
"off" nil
)
253 (other :tag
"ask" other
))
256 ;; I guess setting this to a function that selects all C- or C++-
257 ;; mode buffers would be a good choice for a debugging buffer,
258 ;; when debugging C- or C++-code.
259 (defvar dabbrev-select-buffers-function
'dabbrev--select-buffers
260 "A function that selects buffers that should be searched by dabbrev.
261 The function should take no arguments and return a list of buffers to
262 search for expansions. See the source of `dabbrev--select-buffers'
265 A mode setting this variable should make it buffer local.")
267 (defcustom dabbrev-friend-buffer-function
'dabbrev--same-major-mode-p
268 "*A function to decide whether dabbrev should search OTHER-BUFFER.
269 The function should take one argument, OTHER-BUFFER, and return
270 non-nil if that buffer should be searched. Have a look at
271 `dabbrev--same-major-mode-p' for an example.
273 The value of `dabbrev-friend-buffer-function' has an effect only if
274 the value of `dabbrev-select-buffers-function' uses it. The function
275 `dabbrev--select-buffers' is one function you can use here.
277 A mode setting this variable should make it buffer local."
281 (defcustom dabbrev-search-these-buffers-only nil
282 "If non-nil, a list of buffers which dabbrev should search.
283 If this variable is non-nil, dabbrev will only look in these buffers.
284 It will not even look in the current buffer if it is not a member of
288 ;;----------------------------------------------------------------
289 ;; Internal variables
290 ;;----------------------------------------------------------------
292 ;; Last obarray of completions in `dabbrev-completion'
293 (defvar dabbrev--last-obarray nil
)
295 ;; Table of expansions seen so far
296 (defvar dabbrev--last-table nil
)
298 ;; Last string we tried to expand.
299 (defvar dabbrev--last-abbreviation nil
)
301 ;; Location last abbreviation began
302 (defvar dabbrev--last-abbrev-location nil
)
304 ;; Direction of last dabbrevs search
305 (defvar dabbrev--last-direction
0)
307 ;; Last expansion of an abbreviation.
308 (defvar dabbrev--last-expansion nil
)
310 ;; Location the last expansion was found.
311 (defvar dabbrev--last-expansion-location nil
)
313 ;; The list of remaining buffers with the same mode as current buffer.
314 (defvar dabbrev--friend-buffer-list nil
)
316 ;; The buffer we looked in last, not counting the current buffer.
317 (defvar dabbrev--last-buffer nil
)
319 ;; The buffer we found the expansion last time.
320 (defvar dabbrev--last-buffer-found nil
)
322 ;; The buffer we last did a completion in.
323 (defvar dabbrev--last-completion-buffer nil
)
325 ;; If non-nil, a function to use when copying successive words.
326 ;; It should be `upcase' or `downcase'.
327 (defvar dabbrev--last-case-pattern nil
)
329 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
330 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers
)
332 ;; The regexp for recognizing a character in an abbreviation.
333 (defvar dabbrev--abbrev-char-regexp nil
)
335 ;;----------------------------------------------------------------
337 ;;----------------------------------------------------------------
339 ;;; Get the buffer that mini-buffer was activated from
340 (defsubst dabbrev--minibuffer-origin
()
341 (car (cdr (buffer-list))))
343 ;; Make a list of some of the elements of LIST.
344 ;; Check each element of LIST, storing it temporarily in the
345 ;; variable ELEMENT, and include it in the result
346 ;; if CONDITION evaluates non-nil.
347 (defmacro dabbrev-filter-elements
(element list condition
)
348 `(let (dabbrev-result dabbrev-tail
,element
)
349 (setq dabbrev-tail
,list
)
351 (setq ,element
(car dabbrev-tail
))
353 (setq dabbrev-result
(cons ,element dabbrev-result
)))
354 (setq dabbrev-tail
(cdr dabbrev-tail
)))
355 (nreverse dabbrev-result
)))
357 ;;----------------------------------------------------------------
358 ;; Exported functions
359 ;;----------------------------------------------------------------
361 ;;;###autoload (define-key esc-map "/" 'dabbrev-expand)
362 ;;;??? Do we want this?
363 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion)
366 (defun dabbrev-completion (&optional arg
)
367 "Completion on current word.
368 Like \\[dabbrev-expand] but finds all expansions in the current buffer
369 and presents suggestions for completion.
371 With a prefix argument, it searches all buffers accepted by the
372 function pointed out by `dabbrev-friend-buffer-function' to find the
375 If the prefix argument is 16 (which comes from C-u C-u),
376 then it searches *all* buffers."
378 (dabbrev--reset-global-variables)
379 (let* ((dabbrev-check-other-buffers (and arg t
))
380 (dabbrev-check-all-buffers
381 (and arg
(= (prefix-numeric-value arg
) 16)))
382 (abbrev (dabbrev--abbrev-at-point))
383 (ignore-case-p (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
385 dabbrev-case-fold-search
)
386 (or (not dabbrev-upcase-means-case-search
)
387 (string= abbrev
(downcase abbrev
)))))
388 (my-obarray dabbrev--last-obarray
)
391 ;;--------------------------------
392 ;; New abbreviation to expand.
393 ;;--------------------------------
394 (setq dabbrev--last-abbreviation abbrev
)
395 ;; Find all expansion
396 (let ((completion-list
397 (dabbrev--find-all-expansions abbrev ignore-case-p
))
398 (completion-ignore-case ignore-case-p
))
399 ;; Make an obarray with all expansions
400 (setq my-obarray
(make-vector (length completion-list
) 0))
401 (or (> (length my-obarray
) 0)
402 (error "No dynamic expansion for \"%s\" found%s"
404 (if dabbrev--check-other-buffers
"" " in this-buffer")))
406 ((or (not ignore-case-p
)
407 (not dabbrev-case-replace
))
408 (mapc (function (lambda (string)
409 (intern string my-obarray
)))
411 ((string= abbrev
(upcase abbrev
))
412 (mapc (function (lambda (string)
413 (intern (upcase string
) my-obarray
)))
415 ((string= (substring abbrev
0 1)
416 (upcase (substring abbrev
0 1)))
417 (mapc (function (lambda (string)
418 (intern (capitalize string
) my-obarray
)))
421 (mapc (function (lambda (string)
422 (intern (downcase string
) my-obarray
)))
424 (setq dabbrev--last-obarray my-obarray
)
425 (setq dabbrev--last-completion-buffer
(current-buffer))
426 ;; Find the longest common string.
427 (setq init
(try-completion abbrev my-obarray
))))
428 ;;--------------------------------
429 ;; Let the user choose between the expansions
430 ;;--------------------------------
434 ;; * Replace string fragment with matched common substring completion.
435 ((and (not (string-equal init
""))
436 (not (string-equal (downcase init
) (downcase abbrev
))))
437 (if (> (length (all-completions init my-obarray
)) 1)
438 (message "Repeat `%s' to see all completions"
439 (key-description (this-command-keys)))
440 (message "The only possible completion"))
441 (dabbrev--substitute-expansion nil abbrev init nil
))
443 ;; * String is a common substring completion already. Make list.
444 (message "Making completion list...")
445 (with-output-to-temp-buffer "*Completions*"
446 (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) ?\s
)
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.
505 (if dabbrev--last-buffer
506 (set-buffer dabbrev--last-buffer
))
507 ;; Find the end of the last "expansion" word.
508 (if (or (eq dabbrev--last-direction
1)
509 (and (eq dabbrev--last-direction
0)
510 (< dabbrev--last-expansion-location
(point))))
511 (setq dabbrev--last-expansion-location
512 (+ dabbrev--last-expansion-location
513 (length dabbrev--last-expansion
))))
514 (goto-char dabbrev--last-expansion-location
)
515 ;; Take the following word, with intermediate separators,
516 ;; as our expansion this time.
518 (concat "\\(?:" dabbrev--abbrev-char-regexp
"\\)+"))
519 (setq expansion
(buffer-substring-no-properties
520 dabbrev--last-expansion-location
(point)))
522 ;; Record the end of this expansion, in case we repeat this.
523 (setq dabbrev--last-expansion-location
(point))))
524 ;; Indicate that dabbrev--last-expansion-location is
525 ;; at the end of the expansion.
526 (setq dabbrev--last-direction -
1))
528 ;; We have a different abbrev to expand.
529 (dabbrev--reset-global-variables)
530 (setq direction
(if (null arg
)
531 (if dabbrev-backward-only
1 0)
532 (prefix-numeric-value arg
)))
533 (setq abbrev
(dabbrev--abbrev-at-point))
534 (setq record-case-pattern t
)
537 ;;--------------------------------
538 ;; Find the expansion
539 ;;--------------------------------
542 (dabbrev--find-expansion abbrev direction
543 (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
545 dabbrev-case-fold-search
)
546 (or (not dabbrev-upcase-means-case-search
)
547 (string= abbrev
(downcase abbrev
))))))))
550 (dabbrev--reset-global-variables)
553 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
554 ;; Put back the original abbrev with its original case pattern.
555 (search-backward old
)
557 (delete-region (point) (+ (point) (length old
)))))
558 (error "No%s dynamic expansion for `%s' found"
559 (if old
" further" "") abbrev
))
561 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found
)
562 (minibuffer-window-active-p (selected-window))))
564 (message "Expansion found in '%s'"
565 (buffer-name dabbrev--last-buffer
))
566 (setq dabbrev--last-buffer-found dabbrev--last-buffer
))
568 (if (and (or (eq (current-buffer) dabbrev--last-buffer
)
569 (null dabbrev--last-buffer
))
570 (numberp dabbrev--last-expansion-location
)
571 (and (> dabbrev--last-expansion-location
(point))))
572 (setq dabbrev--last-expansion-location
573 (copy-marker dabbrev--last-expansion-location
)))
574 ;; Success: stick it in and return.
575 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
576 (dabbrev--substitute-expansion old abbrev expansion
579 ;; Save state for re-expand.
580 (setq dabbrev--last-expansion expansion
)
581 (setq dabbrev--last-abbreviation abbrev
)
582 (setq dabbrev--last-abbrev-location
(point-marker))))))
584 ;;----------------------------------------------------------------
586 ;;----------------------------------------------------------------
588 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
589 (defun dabbrev--same-major-mode-p (other-buffer)
592 (set-buffer other-buffer
)
595 ;;; Back over all abbrev type characters and then moves forward over
596 ;;; all skip characters.
597 (defun dabbrev--goto-start-of-abbrev ()
598 ;; Move backwards over abbrev chars
600 (when (> (point) (minibuffer-prompt-end))
602 (while (and (looking-at dabbrev--abbrev-char-regexp
)
603 (> (point) (minibuffer-prompt-end))
604 (not (= (point) (field-beginning (point) nil
607 (or (looking-at dabbrev--abbrev-char-regexp
)
609 (and dabbrev-abbrev-skip-leading-regexp
610 (while (looking-at dabbrev-abbrev-skip-leading-regexp
)
613 ;;; Extract the symbol at point to serve as abbreviation.
614 (defun dabbrev--abbrev-at-point ()
617 (error "No possible abbreviation preceding point"))
618 ;; Return abbrev at point
620 ;; Record the end of the abbreviation.
621 (setq dabbrev--last-abbrev-location
(point))
622 ;; If we aren't right after an abbreviation,
623 ;; move point back to just after one.
624 ;; This is so the user can get successive words
625 ;; by typing the punctuation followed by M-/.
629 (not (looking-at (concat "\\("
630 (or dabbrev-abbrev-char-regexp
633 (if (re-search-backward (or dabbrev-abbrev-char-regexp
637 (error "No possible abbreviation preceding point"))))
638 ;; Now find the beginning of that one.
639 (dabbrev--goto-start-of-abbrev)
640 (buffer-substring-no-properties
641 dabbrev--last-abbrev-location
(point))))
643 ;;; Initializes all global variables
644 (defun dabbrev--reset-global-variables ()
645 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
646 ;; must not be reset here.
647 (setq dabbrev--last-table nil
648 dabbrev--last-abbreviation nil
649 dabbrev--last-abbrev-location nil
650 dabbrev--last-direction nil
651 dabbrev--last-expansion nil
652 dabbrev--last-expansion-location nil
653 dabbrev--friend-buffer-list nil
654 dabbrev--last-buffer nil
655 dabbrev--last-buffer-found nil
656 dabbrev--abbrev-char-regexp
(or dabbrev-abbrev-char-regexp
658 dabbrev--check-other-buffers dabbrev-check-other-buffers
))
660 (defun dabbrev--select-buffers ()
661 "Return a list of other buffers to search for a possible abbrev.
662 The current buffer is not included in the list.
664 This function makes a list of all the buffers returned by `buffer-list',
665 then discards buffers whose names match `dabbrev-ignored-buffer-names'
666 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
667 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
668 with the buffer as argument.
669 It returns the list of the buffers that are not discarded."
670 (dabbrev-filter-elements
672 (and (not (eq (current-buffer) buffer
))
673 (not (dabbrev--ignore-buffer-p buffer
))
674 (boundp 'dabbrev-friend-buffer-function
)
675 (funcall dabbrev-friend-buffer-function buffer
))))
677 (defun dabbrev--try-find (abbrev reverse n ignore-case
)
678 "Search for ABBREV, backwards if REVERSE, N times.
679 If IGNORE-CASE is non-nil, ignore case while searching.
680 Return the expansion found, and save the location of the start
681 of the expansion in `dabbrev--last-expansion-location'."
685 (let ((expansion nil
))
686 (and dabbrev--last-expansion-location
687 (goto-char dabbrev--last-expansion-location
))
688 (let ((case-fold-search ignore-case
)
690 (while (and (> count
0)
691 (setq expansion
(dabbrev--search abbrev
694 (if (eq dabbrev-case-distinction
'case-replace
)
696 dabbrev-case-distinction
))
698 (setq count
(1- count
))))
700 (setq dabbrev--last-expansion-location
(point)))
703 (defun dabbrev--find-all-expansions (abbrev ignore-case
)
704 "Return a list of all possible expansions of ABBREV.
705 If IGNORE-CASE is non-nil, accept matches which differ in case."
706 (let ((all-expansions nil
)
709 (goto-char (point-min))
710 (while (setq expansion
(dabbrev--find-expansion abbrev -
1 ignore-case
))
711 (setq all-expansions
(cons expansion all-expansions
))))
714 (defun dabbrev--scanning-message ()
715 (unless (window-minibuffer-p (selected-window))
716 (message "Scanning `%s'" (buffer-name (current-buffer)))))
718 (defun dabbrev--ignore-buffer-p (buffer)
719 "Return non-nil if BUFFER should be ignored by dabbrev."
720 (let ((bn (buffer-name buffer
)))
721 (or (member bn dabbrev-ignored-buffer-names
)
722 (let ((tail dabbrev-ignored-buffer-regexps
)
724 (while (and tail
(not match
))
725 (setq match
(string-match (car tail
) bn
)
729 (defun dabbrev--find-expansion (abbrev direction ignore-case
)
730 "Find one occurrence of ABBREV, and return the expansion.
731 DIRECTION > 0 means look that many times backwards.
732 DIRECTION < 0 means look that many times forward.
733 DIRECTION = 0 means try both backward and forward.
734 IGNORE-CASE non-nil means ignore case when searching.
735 This sets `dabbrev--last-direction' to 1 or -1 according
736 to the direction in which the occurrence was actually found.
737 It sets `dabbrev--last-expansion-location' to the location
738 of the start of the occurrence."
740 ;; If we were scanning something other than the current buffer,
741 ;; continue scanning there.
742 (when dabbrev--last-buffer
743 (set-buffer dabbrev--last-buffer
)
744 (dabbrev--scanning-message))
746 ;; ------------------------------------------
747 ;; Look backward in current buffer.
748 ;; ------------------------------------------
749 (and (not dabbrev-search-these-buffers-only
)
751 (setq dabbrev--last-direction
(min 1 direction
))
752 (dabbrev--try-find abbrev t
755 ;; ------------------------------------------
756 ;; Look forward in current buffer
757 ;; or whatever buffer we were last scanning.
758 ;; ------------------------------------------
759 (and (or (not dabbrev-search-these-buffers-only
)
760 dabbrev--last-buffer
)
762 (setq dabbrev--last-direction -
1)
763 (dabbrev--try-find abbrev nil
764 (max 1 (- direction
))
766 ;; ------------------------------------------
767 ;; Look in other buffers.
768 ;; Always start at (point-min) and look forward.
769 ;; ------------------------------------------
771 (setq dabbrev--last-direction -
1)
772 (unless dabbrev--last-buffer
773 ;; If we have just now begun to search other buffers,
774 ;; determine which other buffers we should check.
775 ;; Put that list in dabbrev--friend-buffer-list.
776 (or dabbrev--friend-buffer-list
777 (setq dabbrev--friend-buffer-list
778 (dabbrev--make-friend-buffer-list))))
779 ;; Walk through the buffers till we find a match.
781 (while (and (not expansion
) dabbrev--friend-buffer-list
)
782 (setq dabbrev--last-buffer
(pop dabbrev--friend-buffer-list
))
783 (set-buffer dabbrev--last-buffer
)
784 (dabbrev--scanning-message)
785 (setq dabbrev--last-expansion-location
(point-min))
786 (setq expansion
(dabbrev--try-find abbrev nil
1 ignore-case
)))
789 ;; Compute the list of buffers to scan.
790 ;; If dabbrev-search-these-buffers-only, then the current buffer
791 ;; is included in this list if it should be searched.
792 ;; Otherwise, the current buffer is searched first specially.,
793 ;; and it is not included in this list.
794 (defun dabbrev--make-friend-buffer-list ()
795 (let ((list (mapcar (function get-buffer
)
796 dabbrev-search-these-buffers-only
)))
797 (when (and (null dabbrev-search-these-buffers-only
)
798 dabbrev--check-other-buffers
799 (or (eq dabbrev--check-other-buffers t
)
800 (setq dabbrev--check-other-buffers
801 (y-or-n-p "Scan other buffers also? "))))
802 (setq list
(funcall dabbrev-select-buffers-function
))
803 ;; If dabbrev-check-all-buffers, tack on all the other
804 ;; buffers at the end of the list, except those which are
805 ;; specifically to be ignored.
806 (if dabbrev-check-all-buffers
809 (dabbrev-filter-elements
811 (and (not (memq buffer list
))
812 (not (dabbrev--ignore-buffer-p buffer
)))))))
813 ;; Remove the current buffer.
814 (setq list
(delq (current-buffer) list
)))
815 ;; Move buffers in the list that are visible on the screen
816 ;; to the front of the list, but don't add anything to the list.
818 (walk-windows (lambda (w)
819 (unless (eq w
(selected-window))
820 (if (memq (window-buffer w
) list
)
822 (cons (window-buffer w
)
823 (delq (window-buffer w
)
825 ;; In a minibuffer, search the buffer it was activated from,
826 ;; first after the minibuffer itself. Unless we aren't supposed
827 ;; to search the current buffer either.
828 (if (and (window-minibuffer-p (selected-window))
829 (not dabbrev-search-these-buffers-only
))
831 (cons (dabbrev--minibuffer-origin)
832 (delq (dabbrev--minibuffer-origin) list
))))
835 (defun dabbrev--safe-replace-match (string &optional fixedcase literal
)
836 (if (eq major-mode
'picture-mode
)
838 (picture-replace-match string fixedcase literal
))
839 (replace-match string fixedcase literal
)))
841 ;;;----------------------------------------------------------------
842 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern
)
843 "Replace OLD with EXPANSION in the buffer.
844 OLD is text currently in the buffer, perhaps the abbreviation
845 or perhaps another expansion that was tried previously.
846 ABBREV is the abbreviation we are expanding.
847 It is \" \" if we are copying subsequent words.
848 EXPANSION is the expansion substring to be used this time.
849 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
850 to record whether we upcased the expansion, downcased it, or did neither."
852 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
854 dabbrev-case-fold-search
)
855 (or (not dabbrev-upcase-means-case-search
)
856 (string= abbrev
(downcase abbrev
)))
857 (if (eq dabbrev-case-replace
'case-replace
)
859 dabbrev-case-replace
))))
861 ;; If we upcased or downcased the original expansion,
862 ;; do likewise for the subsequent words when we copy them.
863 ;; Don't do any of the usual case processing, though.
864 (when (equal abbrev
" ")
865 (if dabbrev--last-case-pattern
867 (funcall dabbrev--last-case-pattern expansion
)))
868 (setq use-case-replace nil
))
870 ;; If the expansion has mixed case
871 ;; and it is not simply a capitalized word,
872 ;; or if the abbrev has mixed case,
873 ;; and if the given abbrev's case pattern
874 ;; matches the start of the expansion,
875 ;; copy the expansion's case
876 ;; instead of downcasing all the rest.
878 ;; Treat a one-capital-letter (possibly with preceding non-letter
879 ;; characters) abbrev as "not all upper case", so as to force
880 ;; preservation of the expansion's pattern if the expansion starts
881 ;; with a capital letter.
882 (let ((expansion-rest (substring expansion
1))
883 (first-letter-position (string-match "[[:alpha:]]" abbrev
)))
884 (if (or (null first-letter-position
)
885 (and (not (and (or (string= expansion-rest
(downcase expansion-rest
))
886 (string= expansion-rest
(upcase expansion-rest
)))
887 (or (string= abbrev
(downcase abbrev
))
888 (and (string= abbrev
(upcase abbrev
))
889 (> (- (length abbrev
) first-letter-position
)
892 (substring expansion
0 (length abbrev
)))))
893 (setq use-case-replace nil
)))
895 ;; If the abbrev and the expansion are both all-lower-case
896 ;; then don't do any conversion. The conversion would be a no-op
897 ;; for this replacement, but it would carry forward to subsequent words.
898 ;; The goal of this is to prevent that carrying forward.
899 (if (and (string= expansion
(downcase expansion
))
900 (string= abbrev
(downcase abbrev
)))
901 (setq use-case-replace nil
))
904 (setq expansion
(downcase expansion
)))
906 ;; In case we insert subsequent words,
907 ;; record if we upcased or downcased the first word,
908 ;; in order to do likewise for subsequent words.
909 (and record-case-pattern
910 (setq dabbrev--last-case-pattern
911 (and use-case-replace
912 (cond ((equal abbrev
(upcase abbrev
)) 'upcase
)
913 ((equal abbrev
(downcase abbrev
)) 'downcase
)))))
915 ;; Convert whitespace to single spaces.
916 (if dabbrev--eliminate-newlines
917 ;; Start searching at end of ABBREV so that any whitespace
918 ;; carried over from the existing text is not changed.
919 (let ((pos (length abbrev
)))
920 (while (string-match "[\n \t]+" expansion pos
)
921 (setq pos
(1+ (match-beginning 0)))
922 (setq expansion
(replace-match " " nil nil expansion
)))))
926 (search-backward old
))
927 ;;(set-match-data (list (point-marker) (point-marker)))
928 (search-backward abbrev
)
929 (search-forward abbrev
))
931 ;; Make case of replacement conform to case of abbreviation
932 ;; provided (1) that kind of thing is enabled in this buffer
933 ;; and (2) the replacement itself is all lower case.
934 (dabbrev--safe-replace-match expansion
935 (not use-case-replace
)
939 ;;;----------------------------------------------------------------
940 ;;; Search function used by dabbrevs library.
943 (defun dabbrev--search (abbrev reverse ignore-case
)
944 "Search for something that could be used to expand ABBREV.
946 Second arg, REVERSE, is t for reverse search, nil for forward.
947 The variable `dabbrev-limit' controls the maximum search region size.
948 Third argument IGNORE-CASE non-nil means treat case as insignificant while
949 looking for a match and when comparing with previous matches. Also if
950 that's non-nil and the match is found at the beginning of a sentence
951 and is in lower case except for the initial then it is converted to
952 all lower case for return.
954 Table of expansions already seen is examined in buffer
955 `dabbrev--last-table' so that only distinct possibilities are found
956 by dabbrev-re-expand.
958 Returns the expansion found, or nil if not found.
959 Leaves point at the location of the start of the expansion."
961 (let ((pattern1 (concat (regexp-quote abbrev
)
962 "\\(" dabbrev--abbrev-char-regexp
"\\)"))
963 (pattern2 (concat (regexp-quote abbrev
)
964 "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
965 ;; This makes it possible to find matches in minibuffer prompts
966 ;; even when they are "inviolable".
967 (inhibit-point-motion-hooks t
)
972 (narrow-to-region dabbrev--last-expansion-location
974 (if reverse
(- dabbrev-limit
) dabbrev-limit
))))
975 ;;--------------------------------
976 ;; Look for a distinct expansion, using dabbrev--last-table.
977 ;;--------------------------------
978 (while (and (not found-string
)
980 (re-search-backward pattern1 nil t
)
981 (re-search-forward pattern1 nil t
)))
982 (goto-char (match-beginning 0))
983 ;; In case we matched in the middle of a word,
984 ;; back up to start of word and verify we still match.
985 (dabbrev--goto-start-of-abbrev)
987 (if (not (looking-at pattern1
))
989 ;; We have a truly valid match. Find the end.
990 (re-search-forward pattern2
)
991 (setq found-string
(match-string-no-properties 0))
992 (setq result found-string
)
993 (and ignore-case
(setq found-string
(downcase found-string
)))
994 ;; Ignore this match if it's already in the table.
995 (if (dabbrev-filter-elements
996 table-string dabbrev--last-table
997 (string= found-string table-string
))
998 (setq found-string nil
)))
999 ;; Prepare to continue searching.
1000 (goto-char (if reverse
(match-beginning 0) (match-end 0))))
1001 ;; If we found something, use it.
1003 ;; Put it into `dabbrev--last-table'
1004 ;; and return it (either downcased, or as is).
1005 (setq dabbrev--last-table
1006 (cons found-string dabbrev--last-table
))
1009 (dolist (mess '("^No dynamic expansion for .* found$"
1010 "^No further dynamic expansion for .* found$"
1011 "^No possible abbreviation preceding point$"))
1012 (add-to-list 'debug-ignored-errors mess
))
1016 ;;; arch-tag: 29e58596-f080-4306-a409-70296cf9d46f
1017 ;;; dabbrev.el ends here