1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1996, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: Don Morrison
9 ;; (according to ack.texi)
10 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
11 ;; Created: 16 Mars 1992
12 ;; Lindberg's last update version: 5.7
13 ;; Keywords: abbrev expand completion convenience
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 ;; The purpose with this package is to let you write just a few
33 ;; characters of words you've written earlier to be able to expand
36 ;; To expand a word, just put the point right after the word and press
37 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
39 ;; Check out the customizable variables below to learn about all the
40 ;; features of this package.
42 ;;; Hints and tips for major modes writers:
44 ;; Recommended values C/Lisp etc text
45 ;; dabbrev-case-fold-search nil t
46 ;; dabbrev-case-replace nil t
48 ;; Set the variables you want special for your mode like this:
49 ;; (set (make-local-variable 'dabbrev-case-replace) nil)
50 ;; Then you don't interfere with other modes.
52 ;; If your mode handles buffers that refers to other buffers
53 ;; (i.e. compilation-mode, gud-mode), then try to set
54 ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
55 ;; to a function that point out those buffers.
57 ;; Same goes for major-modes that are connected to other modes. There
58 ;; are for instance a number of mail-modes. One for reading, one for
59 ;; creating a new mail etc. Maybe those should be connected.
61 ;; Example for GNUS (when we write a reply, we want dabbrev to look in
62 ;; the article for expansion):
63 ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
65 ;; (with-current-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
)
147 ;;;###autoload(put 'dabbrev-case-fold-search 'risky-local-variable t)
149 (defcustom dabbrev-upcase-means-case-search nil
150 "The significance of an uppercase character in an abbreviation.
151 A nil value means case fold search when searching for possible expansions;
152 non-nil means case sensitive search.
154 This variable has an effect only when the value of
155 `dabbrev-case-fold-search' says to ignore case."
159 (defcustom dabbrev-case-distinction
'case-replace
160 "Whether dabbrev treats expansions as the same if they differ in case.
162 A value of nil means treat them as different.
163 A value of `case-replace' means distinguish them if `case-replace' is nil.
164 Any other non-nil value means to treat them as the same.
166 This variable has an effect only when the value of
167 `dabbrev-case-fold-search' specifies to ignore case."
168 :type
'(choice (const :tag
"off" nil
)
169 (const :tag
"based on `case-replace'" case-replace
)
174 (defcustom dabbrev-case-replace
'case-replace
175 "Whether dabbrev applies the abbreviations's case pattern to the expansion.
177 A value of nil means preserve the expansion's case pattern.
178 A value of `case-replace' means preserve it if `case-replace' is nil.
179 Any other non-nil value means modify the expansion
180 by applying the abbreviation's case pattern to it.
182 This variable has an effect only when the value of
183 `dabbrev-case-fold-search' specifies to ignore case."
184 :type
'(choice (const :tag
"off" nil
)
185 (const :tag
"based on `case-replace'" case-replace
)
188 ;;;###autoload(put 'dabbrev-case-replace 'risky-local-variable t)
190 (defcustom dabbrev-abbrev-char-regexp nil
191 "Regexp to recognize a character in an abbreviation or expansion.
192 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
194 Set this variable to \"\\\\sw\" if you want ordinary words or
195 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
196 syntax is \"symbol\" as well as those whose syntax is \"word\".
198 The value nil has a special meaning: the abbreviation is from point to
199 previous word-start, but the search is for symbols.
201 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
202 while `yes', `or', `no' and `p' are considered words. If this
203 variable is nil, then expanding `yes-or-no-' looks for a symbol
204 starting with or containing `no-'. If you set this variable to
205 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
206 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
207 expanding `yes-or-no-' signals an error because `-' is not part of a word;
208 but expanding `yes-or-no' looks for a word starting with `no'.
210 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
211 :type
'(choice (const nil
)
215 (defcustom dabbrev-check-all-buffers t
216 "Non-nil means dabbrev package should search *all* buffers.
218 Dabbrev always searches the current buffer first. Then, if
219 `dabbrev-check-other-buffers' says so, it searches the buffers
220 designated by `dabbrev-select-buffers-function'.
222 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
223 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
224 or matched by `dabbrev-ignored-regexps'."
228 (defcustom dabbrev-ignored-buffer-names
'("*Messages*" "*Buffer List*")
229 "List of buffer names that dabbrev should not check.
230 See also `dabbrev-ignored-buffer-regexps'."
231 :type
'(repeat (string :tag
"Buffer name"))
235 (defcustom dabbrev-ignored-buffer-regexps nil
236 "List of regexps matching names of buffers that dabbrev should not check.
237 See also `dabbrev-ignored-buffer-names'."
238 :type
'(repeat regexp
)
242 (defcustom dabbrev-check-other-buffers t
243 "Should \\[dabbrev-expand] look in other buffers?\
245 nil: Don't look in other buffers.
246 t: Also look for expansions in the buffers pointed out by
247 `dabbrev-select-buffers-function'.
248 Anything else: When we can't find any more expansions in
249 the current buffer, then ask the user whether to look in other
252 The default value is t."
253 :type
'(choice (const :tag
"off" nil
)
255 (other :tag
"ask" other
))
258 ;; I guess setting this to a function that selects all C- or C++-
259 ;; mode buffers would be a good choice for a debugging buffer,
260 ;; when debugging C- or C++-code.
261 (defvar dabbrev-select-buffers-function
'dabbrev--select-buffers
262 "A function that selects buffers that should be searched by dabbrev.
263 The function should take no arguments and return a list of buffers to
264 search for expansions. See the source of `dabbrev--select-buffers'
267 A mode setting this variable should make it buffer local.")
269 (defcustom dabbrev-friend-buffer-function
'dabbrev--same-major-mode-p
270 "A function to decide whether dabbrev should search OTHER-BUFFER.
271 The function should take one argument, OTHER-BUFFER, and return
272 non-nil if that buffer should be searched. Have a look at
273 `dabbrev--same-major-mode-p' for an example.
275 The value of `dabbrev-friend-buffer-function' has an effect only if
276 the value of `dabbrev-select-buffers-function' uses it. The function
277 `dabbrev--select-buffers' is one function you can use here.
279 A mode setting this variable should make it buffer local."
283 (defcustom dabbrev-search-these-buffers-only nil
284 "If non-nil, a list of buffers which dabbrev should search.
285 If this variable is non-nil, dabbrev will only look in these buffers.
286 It will not even look in the current buffer if it is not a member of
290 ;;----------------------------------------------------------------
291 ;; Internal variables
292 ;;----------------------------------------------------------------
294 ;; Last obarray of completions in `dabbrev-completion'
295 (defvar dabbrev--last-obarray nil
)
297 ;; Table of expansions seen so far
298 (defvar dabbrev--last-table nil
)
300 ;; Last string we tried to expand.
301 (defvar dabbrev--last-abbreviation nil
)
303 ;; Location last abbreviation began
304 (defvar dabbrev--last-abbrev-location nil
)
306 ;; Direction of last dabbrevs search
307 (defvar dabbrev--last-direction
0)
309 ;; Last expansion of an abbreviation.
310 (defvar dabbrev--last-expansion nil
)
312 ;; Location the last expansion was found.
313 (defvar dabbrev--last-expansion-location nil
)
315 ;; The list of remaining buffers with the same mode as current buffer.
316 (defvar dabbrev--friend-buffer-list nil
)
318 ;; The buffer we looked in last, not counting the current buffer.
319 (defvar dabbrev--last-buffer nil
)
321 ;; The buffer we found the expansion last time.
322 (defvar dabbrev--last-buffer-found nil
)
324 ;; The buffer we last did a completion in.
325 (defvar dabbrev--last-completion-buffer nil
)
327 ;; If non-nil, a function to use when copying successive words.
328 ;; It should be `upcase' or `downcase'.
329 (defvar dabbrev--last-case-pattern nil
)
331 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
332 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers
)
334 ;; The regexp for recognizing a character in an abbreviation.
335 (defvar dabbrev--abbrev-char-regexp nil
)
337 ;; The progress reporter for buffer-scanning progress.
338 (defvar dabbrev--progress-reporter nil
)
340 ;;----------------------------------------------------------------
342 ;;----------------------------------------------------------------
344 (defsubst dabbrev--minibuffer-origin
()
345 "Get the buffer from which mini-buffer."
346 (window-buffer (minibuffer-selected-window)))
348 ;; Make a list of some of the elements of LIST.
349 ;; Check each element of LIST, storing it temporarily in the
350 ;; variable ELEMENT, and include it in the result
351 ;; if CONDITION evaluates non-nil.
352 (defmacro dabbrev-filter-elements
(element list condition
)
353 `(let (dabbrev-result dabbrev-tail
,element
)
354 (setq dabbrev-tail
,list
)
356 (setq ,element
(car dabbrev-tail
))
358 (setq dabbrev-result
(cons ,element dabbrev-result
)))
359 (setq dabbrev-tail
(cdr dabbrev-tail
)))
360 (nreverse dabbrev-result
)))
362 ;;----------------------------------------------------------------
363 ;; Exported functions
364 ;;----------------------------------------------------------------
366 ;;;###autoload (define-key esc-map "/" 'dabbrev-expand)
367 ;;??? Do we want this?
368 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion)
371 (defun dabbrev-completion (&optional arg
)
372 "Completion on current word.
373 Like \\[dabbrev-expand] but finds all expansions in the current buffer
374 and presents suggestions for completion.
376 With a prefix argument ARG, it searches all buffers accepted by the
377 function pointed out by `dabbrev-friend-buffer-function' to find the
380 If the prefix argument is 16 (which comes from \\[universal-argument] \\[universal-argument]),
381 then it searches *all* buffers."
383 (dabbrev--reset-global-variables)
384 (let* ((dabbrev-check-other-buffers (and arg t
))
385 (dabbrev-check-all-buffers
386 (and arg
(= (prefix-numeric-value arg
) 16)))
387 (abbrev (dabbrev--abbrev-at-point))
388 (beg (progn (search-backward abbrev
) (point)))
389 (end (progn (search-forward abbrev
) (point)))
390 (ignore-case-p (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
392 dabbrev-case-fold-search
)
393 (or (not dabbrev-upcase-means-case-search
)
394 (string= abbrev
(downcase abbrev
)))))
395 (my-obarray dabbrev--last-obarray
)
398 ;;--------------------------------
399 ;; New abbreviation to expand.
400 ;;--------------------------------
401 (setq dabbrev--last-abbreviation abbrev
)
402 ;; Find all expansion
403 (let ((completion-list
404 (dabbrev--find-all-expansions abbrev ignore-case-p
))
405 (completion-ignore-case ignore-case-p
))
406 ;; Make an obarray with all expansions
407 (setq my-obarray
(make-vector (length completion-list
) 0))
408 (or (> (length my-obarray
) 0)
409 (error "No dynamic expansion for \"%s\" found%s"
411 (if dabbrev--check-other-buffers
"" " in this-buffer")))
413 ((or (not ignore-case-p
)
414 (not dabbrev-case-replace
))
415 (mapc (function (lambda (string)
416 (intern string my-obarray
)))
418 ((string= abbrev
(upcase abbrev
))
419 (mapc (function (lambda (string)
420 (intern (upcase string
) my-obarray
)))
422 ((string= (substring abbrev
0 1)
423 (upcase (substring abbrev
0 1)))
424 (mapc (function (lambda (string)
425 (intern (capitalize string
) my-obarray
)))
428 (mapc (function (lambda (string)
429 (intern (downcase string
) my-obarray
)))
431 (setq dabbrev--last-obarray my-obarray
)
432 (setq dabbrev--last-completion-buffer
(current-buffer))))
433 (completion-in-region beg end my-obarray
)))
436 (defun dabbrev-expand (arg)
437 "Expand previous word \"dynamically\".
439 Expands to the most recent, preceding word for which this is a prefix.
440 If no suitable preceding word is found, words following point are
441 considered. If still no suitable word is found, then look in the
442 buffers accepted by the function pointed out by variable
443 `dabbrev-friend-buffer-function'.
445 A positive prefix argument, N, says to take the Nth backward *distinct*
446 possibility. A negative argument says search forward.
448 If the cursor has not moved from the end of the previous expansion and
449 no argument is given, replace the previously-made expansion
450 with the next possible expansion not yet tried.
452 The variable `dabbrev-backward-only' may be used to limit the
453 direction of search to backward if set non-nil.
455 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
457 (let (abbrev record-case-pattern
458 expansion old direction
(orig-point (point)))
459 ;; abbrev -- the abbrev to expand
460 ;; expansion -- the expansion found (eventually) or nil until then
461 ;; old -- the text currently in the buffer
462 ;; (the abbrev, or the previously-made expansion)
465 (markerp dabbrev--last-abbrev-location
)
466 (marker-position dabbrev--last-abbrev-location
)
467 (or (eq last-command this-command
)
468 (and (window-minibuffer-p (selected-window))
469 (= dabbrev--last-abbrev-location
471 ;; Find a different expansion for the same abbrev as last time.
473 (setq abbrev dabbrev--last-abbreviation
)
474 (setq old dabbrev--last-expansion
)
475 (setq direction dabbrev--last-direction
))
476 ;; If the user inserts a space after expanding
477 ;; and then asks to expand again, always fetch the next word.
478 (if (and (eq (preceding-char) ?\s
)
479 (markerp dabbrev--last-abbrev-location
)
480 (marker-position dabbrev--last-abbrev-location
)
481 (= (point) (1+ dabbrev--last-abbrev-location
)))
483 ;; The "abbrev" to expand is just the space.
488 (if dabbrev--last-buffer
489 (set-buffer dabbrev--last-buffer
))
490 ;; Find the end of the last "expansion" word.
491 (if (or (eq dabbrev--last-direction
1)
492 (and (eq dabbrev--last-direction
0)
493 (< dabbrev--last-expansion-location
(point))))
494 (setq dabbrev--last-expansion-location
495 (+ dabbrev--last-expansion-location
496 (length dabbrev--last-expansion
))))
497 (goto-char dabbrev--last-expansion-location
)
498 ;; Take the following word, with intermediate separators,
499 ;; as our expansion this time.
501 (concat "\\(?:" dabbrev--abbrev-char-regexp
"\\)+"))
502 (setq expansion
(buffer-substring-no-properties
503 dabbrev--last-expansion-location
(point)))
505 ;; Record the end of this expansion, in case we repeat this.
506 (setq dabbrev--last-expansion-location
(point))))
507 ;; Indicate that dabbrev--last-expansion-location is
508 ;; at the end of the expansion.
509 (setq dabbrev--last-direction -
1))
511 ;; We have a different abbrev to expand.
512 (dabbrev--reset-global-variables)
513 (setq direction
(if (null arg
)
514 (if dabbrev-backward-only
1 0)
515 (prefix-numeric-value arg
)))
516 (setq abbrev
(dabbrev--abbrev-at-point))
517 (setq record-case-pattern t
)
520 ;;--------------------------------
521 ;; Find the expansion
522 ;;--------------------------------
525 (dabbrev--find-expansion abbrev direction
526 (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
528 dabbrev-case-fold-search
)
529 (or (not dabbrev-upcase-means-case-search
)
530 (string= abbrev
(downcase abbrev
))))))))
533 (dabbrev--reset-global-variables)
536 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
537 ;; Put back the original abbrev with its original case pattern.
538 (search-backward old
)
540 (delete-region (point) (+ (point) (length old
)))))
541 (error "No%s dynamic expansion for `%s' found"
542 (if old
" further" "") abbrev
))
544 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found
)
545 (minibuffer-window-active-p (selected-window))))
547 (message "Expansion found in '%s'"
548 (buffer-name dabbrev--last-buffer
))
549 (setq dabbrev--last-buffer-found dabbrev--last-buffer
))
551 (if (and (or (eq (current-buffer) dabbrev--last-buffer
)
552 (null dabbrev--last-buffer
))
553 (numberp dabbrev--last-expansion-location
)
554 (and (> dabbrev--last-expansion-location
(point))))
555 (setq dabbrev--last-expansion-location
556 (copy-marker dabbrev--last-expansion-location
)))
557 ;; Success: stick it in and return.
558 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
559 (dabbrev--substitute-expansion old abbrev expansion
562 ;; Save state for re-expand.
563 (setq dabbrev--last-expansion expansion
)
564 (setq dabbrev--last-abbreviation abbrev
)
565 (setq dabbrev--last-abbrev-location
(point-marker))))))
567 ;;----------------------------------------------------------------
569 ;;----------------------------------------------------------------
571 (defun dabbrev--same-major-mode-p (other-buffer)
572 "Check if OTHER-BUFFER has the same major mode as current buffer."
574 (with-current-buffer other-buffer
577 (defun dabbrev--goto-start-of-abbrev ()
578 "Back over all abbrev type characters and then moves forward over
579 all skip characters."
580 ;; Move backwards over abbrev chars
582 (when (> (point) (minibuffer-prompt-end))
584 (while (and (looking-at dabbrev--abbrev-char-regexp
)
585 (> (point) (minibuffer-prompt-end))
586 (not (= (point) (field-beginning (point) nil
589 (or (looking-at dabbrev--abbrev-char-regexp
)
591 (and dabbrev-abbrev-skip-leading-regexp
592 (while (looking-at dabbrev-abbrev-skip-leading-regexp
)
595 (defun dabbrev--abbrev-at-point ()
596 "Extract the symbol at point to serve as abbreviation."
599 (error "No possible abbreviation preceding point"))
600 ;; Return abbrev at point
602 ;; Record the end of the abbreviation.
603 (setq dabbrev--last-abbrev-location
(point))
604 ;; If we aren't right after an abbreviation,
605 ;; move point back to just after one.
606 ;; This is so the user can get successive words
607 ;; by typing the punctuation followed by M-/.
611 (not (looking-at (or dabbrev-abbrev-char-regexp
613 (if (re-search-backward (or dabbrev-abbrev-char-regexp
617 (error "No possible abbreviation preceding point"))))
618 ;; Now find the beginning of that one.
619 (dabbrev--goto-start-of-abbrev)
620 (buffer-substring-no-properties
621 dabbrev--last-abbrev-location
(point))))
623 (defun dabbrev--reset-global-variables ()
624 "Initialize all global variables."
625 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
626 ;; must not be reset here.
627 (setq dabbrev--last-table nil
628 dabbrev--last-abbreviation nil
629 dabbrev--last-abbrev-location nil
630 dabbrev--last-direction nil
631 dabbrev--last-expansion nil
632 dabbrev--last-expansion-location nil
633 dabbrev--friend-buffer-list nil
634 dabbrev--last-buffer nil
635 dabbrev--last-buffer-found nil
636 dabbrev--abbrev-char-regexp
(or dabbrev-abbrev-char-regexp
638 dabbrev--check-other-buffers dabbrev-check-other-buffers
))
640 (defun dabbrev--select-buffers ()
641 "Return a list of other buffers to search for a possible abbrev.
642 The current buffer is not included in the list.
644 This function makes a list of all the buffers returned by `buffer-list',
645 then discards buffers whose names match `dabbrev-ignored-buffer-names'
646 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
647 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
648 with the buffer as argument.
649 It returns the list of the buffers that are not discarded."
650 (dabbrev-filter-elements
652 (and (not (eq (current-buffer) buffer
))
653 (not (dabbrev--ignore-buffer-p buffer
))
654 (boundp 'dabbrev-friend-buffer-function
)
655 (funcall dabbrev-friend-buffer-function buffer
))))
657 (defun dabbrev--try-find (abbrev reverse n ignore-case
)
658 "Search for ABBREV, backwards if REVERSE, N times.
659 If IGNORE-CASE is non-nil, ignore case while searching.
660 Return the expansion found, and save the location of the start
661 of the expansion in `dabbrev--last-expansion-location'."
665 (let ((expansion nil
))
666 (and dabbrev--last-expansion-location
667 (goto-char dabbrev--last-expansion-location
))
668 (let ((case-fold-search ignore-case
)
670 (while (and (> count
0)
671 (setq expansion
(dabbrev--search abbrev
674 (if (eq dabbrev-case-distinction
'case-replace
)
676 dabbrev-case-distinction
))
678 (setq count
(1- count
))))
680 (setq dabbrev--last-expansion-location
(point)))
683 (defun dabbrev--find-all-expansions (abbrev ignore-case
)
684 "Return a list of all possible expansions of ABBREV.
685 If IGNORE-CASE is non-nil, accept matches which differ in 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--ignore-buffer-p (buffer)
695 "Return non-nil if BUFFER should be ignored by dabbrev."
696 (let ((bn (buffer-name buffer
)))
697 (or (member bn dabbrev-ignored-buffer-names
)
698 (let ((tail dabbrev-ignored-buffer-regexps
)
700 (while (and tail
(not match
))
701 (setq match
(string-match (car tail
) bn
)
705 (defun dabbrev--find-expansion (abbrev direction ignore-case
)
706 "Find one occurrence of ABBREV, and return the expansion.
707 DIRECTION > 0 means look that many times backwards.
708 DIRECTION < 0 means look that many times forward.
709 DIRECTION = 0 means try both backward and forward.
710 IGNORE-CASE non-nil means ignore case when searching.
711 This sets `dabbrev--last-direction' to 1 or -1 according
712 to the direction in which the occurrence was actually found.
713 It sets `dabbrev--last-expansion-location' to the location
714 of the start of the occurrence."
716 ;; If we were scanning something other than the current buffer,
717 ;; continue scanning there.
718 (when dabbrev--last-buffer
719 (set-buffer dabbrev--last-buffer
))
721 ;; ------------------------------------------
722 ;; Look backward in current buffer.
723 ;; ------------------------------------------
724 (and (not dabbrev-search-these-buffers-only
)
726 (setq dabbrev--last-direction
(min 1 direction
))
727 (dabbrev--try-find abbrev t
730 ;; ------------------------------------------
731 ;; Look forward in current buffer
732 ;; or whatever buffer we were last scanning.
733 ;; ------------------------------------------
734 (and (or (not dabbrev-search-these-buffers-only
)
735 dabbrev--last-buffer
)
737 (setq dabbrev--last-direction -
1)
738 (dabbrev--try-find abbrev nil
739 (max 1 (- direction
))
741 ;; ------------------------------------------
742 ;; Look in other buffers.
743 ;; Always start at (point-min) and look forward.
744 ;; ------------------------------------------
746 (setq dabbrev--last-direction -
1)
747 (unless dabbrev--last-buffer
748 ;; If we have just now begun to search other buffers,
749 ;; determine which other buffers we should check.
750 ;; Put that list in dabbrev--friend-buffer-list.
751 (unless dabbrev--friend-buffer-list
752 (setq dabbrev--friend-buffer-list
753 (dabbrev--make-friend-buffer-list))
754 (setq dabbrev--progress-reporter
755 (make-progress-reporter
756 "Scanning for dabbrevs..."
757 (- (length dabbrev--friend-buffer-list
)) 0 0 1 1.5))))
758 ;; Walk through the buffers till we find a match.
760 (while (and (not expansion
) dabbrev--friend-buffer-list
)
761 (setq dabbrev--last-buffer
(pop dabbrev--friend-buffer-list
))
762 (set-buffer dabbrev--last-buffer
)
763 (progress-reporter-update dabbrev--progress-reporter
764 (- (length dabbrev--friend-buffer-list
)))
765 (setq dabbrev--last-expansion-location
(point-min))
766 (setq expansion
(dabbrev--try-find abbrev nil
1 ignore-case
)))
769 ;; Compute the list of buffers to scan.
770 ;; If dabbrev-search-these-buffers-only, then the current buffer
771 ;; is included in this list if it should be searched.
772 ;; Otherwise, the current buffer is searched first specially.,
773 ;; and it is not included in this list.
774 (defun dabbrev--make-friend-buffer-list ()
775 (let ((list (mapcar (function get-buffer
)
776 dabbrev-search-these-buffers-only
)))
777 (when (and (null dabbrev-search-these-buffers-only
)
778 dabbrev--check-other-buffers
779 (or (eq dabbrev--check-other-buffers t
)
780 (setq dabbrev--check-other-buffers
781 (y-or-n-p "Scan other buffers also? "))))
782 (setq list
(funcall dabbrev-select-buffers-function
))
783 ;; If dabbrev-check-all-buffers, tack on all the other
784 ;; buffers at the end of the list, except those which are
785 ;; specifically to be ignored.
786 (if dabbrev-check-all-buffers
789 (dabbrev-filter-elements
791 (and (not (memq buffer list
))
792 (not (dabbrev--ignore-buffer-p buffer
)))))))
793 ;; Remove the current buffer.
794 (setq list
(delq (current-buffer) list
)))
795 ;; Move buffers in the list that are visible on the screen
796 ;; to the front of the list, but don't add anything to the list.
798 (walk-windows (lambda (w)
799 (unless (eq w
(selected-window))
800 (if (memq (window-buffer w
) list
)
802 (cons (window-buffer w
)
803 (delq (window-buffer w
)
805 ;; In a minibuffer, search the buffer it was activated from,
806 ;; first after the minibuffer itself. Unless we aren't supposed
807 ;; to search the current buffer either.
808 (if (and (window-minibuffer-p (selected-window))
809 (not dabbrev-search-these-buffers-only
))
811 (cons (dabbrev--minibuffer-origin)
812 (delq (dabbrev--minibuffer-origin) list
))))
815 (defun dabbrev--safe-replace-match (string &optional fixedcase literal
)
816 (if (eq major-mode
'picture-mode
)
818 (picture-replace-match string fixedcase literal
))
819 (replace-match string fixedcase literal
)))
821 ;;;----------------------------------------------------------------
822 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern
)
823 "Replace OLD with EXPANSION in the buffer.
824 OLD is text currently in the buffer, perhaps the abbreviation
825 or perhaps another expansion that was tried previously.
826 ABBREV is the abbreviation we are expanding.
827 It is \" \" if we are copying subsequent words.
828 EXPANSION is the expansion substring to be used this time.
829 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
830 to record whether we upcased the expansion, downcased it, or did neither."
832 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
834 dabbrev-case-fold-search
)
835 (or (not dabbrev-upcase-means-case-search
)
836 (string= abbrev
(downcase abbrev
)))
837 (if (eq dabbrev-case-replace
'case-replace
)
839 dabbrev-case-replace
))))
841 ;; If we upcased or downcased the original expansion,
842 ;; do likewise for the subsequent words when we copy them.
843 ;; Don't do any of the usual case processing, though.
844 (when (equal abbrev
" ")
845 (if dabbrev--last-case-pattern
847 (funcall dabbrev--last-case-pattern expansion
)))
848 (setq use-case-replace nil
))
850 ;; If the expansion has mixed case
851 ;; and it is not simply a capitalized word,
852 ;; or if the abbrev has mixed case,
853 ;; and if the given abbrev's case pattern
854 ;; matches the start of the expansion,
855 ;; copy the expansion's case
856 ;; instead of downcasing all the rest.
858 ;; Treat a one-capital-letter (possibly with preceding non-letter
859 ;; characters) abbrev as "not all upper case", so as to force
860 ;; preservation of the expansion's pattern if the expansion starts
861 ;; with a capital letter.
862 (let ((expansion-rest (substring expansion
1))
863 (first-letter-position (string-match "[[:alpha:]]" abbrev
)))
864 (if (or (null first-letter-position
)
865 (and (not (and (or (string= expansion-rest
(downcase expansion-rest
))
866 (string= expansion-rest
(upcase expansion-rest
)))
867 (or (string= abbrev
(downcase abbrev
))
868 (and (string= abbrev
(upcase abbrev
))
869 (> (- (length abbrev
) first-letter-position
)
872 (substring expansion
0 (length abbrev
)))))
873 (setq use-case-replace nil
)))
875 ;; If the abbrev and the expansion are both all-lower-case
876 ;; then don't do any conversion. The conversion would be a no-op
877 ;; for this replacement, but it would carry forward to subsequent words.
878 ;; The goal of this is to prevent that carrying forward.
879 (if (and (string= expansion
(downcase expansion
))
880 (string= abbrev
(downcase abbrev
)))
881 (setq use-case-replace nil
))
884 (setq expansion
(downcase expansion
)))
886 ;; In case we insert subsequent words,
887 ;; record if we upcased or downcased the first word,
888 ;; in order to do likewise for subsequent words.
889 (and record-case-pattern
890 (setq dabbrev--last-case-pattern
891 (and use-case-replace
892 (cond ((equal abbrev
(upcase abbrev
)) 'upcase
)
893 ((equal abbrev
(downcase abbrev
)) 'downcase
)))))
895 ;; Convert whitespace to single spaces.
896 (if dabbrev-eliminate-newlines
898 (if (equal abbrev
" ") 0 (length abbrev
))))
899 ;; If ABBREV is real, search after the end of it.
900 ;; If ABBREV is space and we are copying successive words,
901 ;; search starting at the front.
902 (while (string-match "[\n \t]+" expansion pos
)
903 (setq pos
(1+ (match-beginning 0)))
904 (setq expansion
(replace-match " " nil nil expansion
)))))
908 (search-backward old
))
909 ;;(set-match-data (list (point-marker) (point-marker)))
910 (search-backward abbrev
)
911 (search-forward abbrev
))
913 ;; Make case of replacement conform to case of abbreviation
914 ;; provided (1) that kind of thing is enabled in this buffer
915 ;; and (2) the replacement itself is all lower case.
916 (dabbrev--safe-replace-match expansion
917 (not use-case-replace
)
921 ;;;----------------------------------------------------------------
922 ;;; Search function used by dabbrevs library.
925 (defun dabbrev--search (abbrev reverse ignore-case
)
926 "Search for something that could be used to expand ABBREV.
928 Second arg, REVERSE, is t for reverse search, nil for forward.
929 The variable `dabbrev-limit' controls the maximum search region size.
930 Third argument IGNORE-CASE non-nil means treat case as insignificant while
931 looking for a match and when comparing with previous matches. Also if
932 that's non-nil and the match is found at the beginning of a sentence
933 and is in lower case except for the initial then it is converted to
934 all lower case for return.
936 Table of expansions already seen is examined in buffer
937 `dabbrev--last-table' so that only distinct possibilities are found
938 by dabbrev-re-expand.
940 Returns the expansion found, or nil if not found.
941 Leaves point at the location of the start of the expansion."
943 (let ((pattern1 (concat (regexp-quote abbrev
)
944 "\\(" dabbrev--abbrev-char-regexp
"\\)"))
945 (pattern2 (concat (regexp-quote abbrev
)
946 "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
947 ;; This makes it possible to find matches in minibuffer prompts
948 ;; even when they are "inviolable".
949 (inhibit-point-motion-hooks t
)
954 (narrow-to-region dabbrev--last-expansion-location
956 (if reverse
(- dabbrev-limit
) dabbrev-limit
))))
957 ;;--------------------------------
958 ;; Look for a distinct expansion, using dabbrev--last-table.
959 ;;--------------------------------
960 (while (and (not found-string
)
962 (re-search-backward pattern1 nil t
)
963 (re-search-forward pattern1 nil t
)))
964 (goto-char (match-beginning 0))
965 ;; In case we matched in the middle of a word,
966 ;; back up to start of word and verify we still match.
967 (dabbrev--goto-start-of-abbrev)
969 (if (not (looking-at pattern1
))
971 ;; We have a truly valid match. Find the end.
972 (re-search-forward pattern2
)
973 (setq found-string
(match-string-no-properties 0))
974 (setq result found-string
)
975 (and ignore-case
(setq found-string
(downcase found-string
)))
976 ;; Ignore this match if it's already in the table.
977 (if (dabbrev-filter-elements
978 table-string dabbrev--last-table
979 (string= found-string table-string
))
980 (setq found-string nil
)))
981 ;; Prepare to continue searching.
982 (goto-char (if reverse
(match-beginning 0) (match-end 0))))
983 ;; If we found something, use it.
985 ;; Put it into `dabbrev--last-table'
986 ;; and return it (either downcased, or as is).
987 (setq dabbrev--last-table
988 (cons found-string dabbrev--last-table
))
991 (dolist (mess '("^No dynamic expansion for .* found"
992 "^No further dynamic expansion for .* found$"
993 "^No possible abbreviation preceding point$"))
994 (add-to-list 'debug-ignored-errors mess
))
998 ;; arch-tag: 29e58596-f080-4306-a409-70296cf9d46f
999 ;;; dabbrev.el ends here