1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
5 ;; Author: Don Morrison
6 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
7 ;; Created: 16 Mars 1992
8 ;; Lindberg's last update version: 5.7
9 ;; Keywords: abbrev expand completion
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; The purpose with this package is to let you write just a few
31 ;; characters of words you've written earlier to be able to expand
34 ;; To expand a word, just put the point right after the word and press
35 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
37 ;; Check out the customizable variables below to learn about all the
38 ;; features of this package.
40 ;;; Hints and tips for major modes writers:
42 ;; Recommended values C/Lisp etc text
43 ;; dabbrev-case-fold-search nil t
44 ;; dabbrev-case-replace nil t
46 ;; Set the variables you want special for your mode like this:
47 ;; (set (make-local-variable 'dabbrev-case-replace) nil)
48 ;; Then you don't interfere with other modes.
50 ;; If your mode handles buffers that refers to other buffers
51 ;; (i.e. compilation-mode, gud-mode), then try to set
52 ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
53 ;; to a function that point out those buffers.
55 ;; Same goes for major-modes that are connected to other modes. There
56 ;; are for instance a number of mail-modes. One for reading, one for
57 ;; creating a new mail etc. Maybe those should be connected.
59 ;; Example for GNUS (when we write a reply, we want dabbrev to look in
60 ;; the article for expansion):
61 ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
64 ;; (set-buffer buffer)
65 ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
68 ;; Known bugs and limitations.
69 ;; - Possible to do several levels of `dabbrev-completion' in the
71 ;; - dabbrev-completion doesn't handle resetting the globals variables
72 ;; right. It resets them after finding the abbrev.
74 ;; Future enhancements
75 ;; - Check the tags-files? Like tags-complete?
76 ;; - Add the possibility of searching both forward and backward to
77 ;; the nearest expansion.
78 ;; - Check the kill-ring when everything else fails. (Maybe something
79 ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
81 ;;; These people gave suggestions:
82 ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
83 ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
84 ;; [jules] Julian Gosnell <jules@x.co.uk>
85 ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
86 ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
87 ;; [alon] Alon Albert <al%imercury@uunet.uu.net>
88 ;; [tromey] Tom Tromey <tromey@busco.lanl.gov>
89 ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
90 ;; [Petri] Petri Raitio <per@tekla.fi>
91 ;; [ejb] Jay Berkenbilt <ejb@ERA.COM>
92 ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
93 ;; ... and to all the people who have participated in the beta tests.
97 ;;----------------------------------------------------------------
98 ;; Customization variables
99 ;;----------------------------------------------------------------
101 (defgroup dabbrev nil
102 "Dynamic Abbreviations"
103 :tag
"Dynamic Abbreviations"
106 (defcustom dabbrev-backward-only nil
107 "*If non-nil, `dabbrev-expand' only looks backwards."
111 (defcustom dabbrev-limit nil
112 "*Limits region searched by `dabbrev-expand' to this many chars away."
113 :type
'(choice (const :tag
"off" nil
)
117 (defcustom dabbrev-abbrev-skip-leading-regexp nil
118 "*Regexp for skipping leading characters of an abbreviation.
120 Example: Set this to \"\\\\$\" for programming languages
121 in which variable names may appear with or without a leading `$'.
122 \(For example, in Makefiles.
126 Set this to nil if no characters should be skipped."
127 :type
'(choice regexp
128 (const :tag
"off" nil
))
131 (defcustom dabbrev-case-fold-search
'case-fold-search
132 "*Control whether dabbrev searches should ignore case.
133 A value of nil means case is significant.
134 A value of `case-fold-search' means case is significant
135 if `case-fold-search' is nil.
136 Any other non-nil version means case is not significant."
137 :type
'(choice (const :tag
"off" nil
)
139 (const :tag
"like search" 'case-fold-search
))
142 (defcustom dabbrev-upcase-means-case-search nil
143 "*The significance of an uppercase character in an abbreviation.
144 nil means case fold search, non-nil means case sensitive search.
146 This variable has an effect only when the value of
147 `dabbrev-case-fold-search' says to ignore case."
151 (defcustom dabbrev-case-replace
'case-replace
152 "*Controls whether dabbrev preserves case when expanding the abbreviation.
153 A value of nil means preserve case.
154 A value of `case-replace' means preserve case if `case-replace' is nil.
155 Any other non-nil version means do not preserve case.
157 This variable has an effect only when the value of
158 `dabbrev-case-fold-search' specifies to ignore case."
159 :type
'(choice (const :tag
"off" nil
)
161 (const :tag
"like M-x query-replace" 'case-replace
))
164 (defcustom dabbrev-abbrev-char-regexp nil
165 "*Regexp to recognize a character in an abbreviation or expansion.
166 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
168 Set this variable to \"\\\\sw\" if you want ordinary words or
169 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
170 syntax is \"symbol\" as well as those whose syntax is \"word\".
172 The value nil has a special meaning: the abbreviation is from point to
173 previous word-start, but the search is for symbols.
175 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
176 while `yes', `or', `no' and `p' are considered words. If this
177 variable is nil, then expanding `yes-or-no-' looks for a symbol
178 starting with or containing `no-'. If you set this variable to
179 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
180 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
181 expanding `yes-or-no-' signals an error because `-' is not part of a word;
182 but expanding `yes-or-no' looks for a word starting with `no'.
184 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
185 :type
'(choice (const nil
)
189 (defcustom dabbrev-check-all-buffers t
190 "*Non-nil means dabbrev package should search *all* buffers.
192 Dabbrev always searches the current buffer first. Then, if
193 `dabbrev-check-other-buffers' says so, it searches the buffers
194 designated by `dabbrev-select-buffers-function'.
196 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
197 all the other buffers."
201 (defcustom dabbrev-check-other-buffers t
202 "*Should \\[dabbrev-expand] look in other buffers?\
204 nil: Don't look in other buffers.
205 t: Also look for expansions in the buffers pointed out by
206 `dabbrev-select-buffers-function'.
207 Anything else: When we can't find any more expansions in
208 the current buffer, then ask the user whether to look in other
211 The default value is t."
212 :type
'(choice (const :tag
"off" nil
)
214 (const :tag
"ask" other
))
217 ;; I guess setting this to a function that selects all C- or C++-
218 ;; mode buffers would be a good choice for a debugging buffer,
219 ;; when debugging C- or C++-code.
220 (defvar dabbrev-select-buffers-function
'dabbrev--select-buffers
221 "A function that selects buffers that should be searched by dabbrev.
222 The function should take no arguments and return a list of buffers to
223 search for expansions. Have a look at `dabbrev--select-buffers' for
226 A mode setting this variable should make it buffer local.")
228 (defcustom dabbrev-friend-buffer-function
'dabbrev--same-major-mode-p
229 "*A function to decide whether dabbrev should search OTHER-BUFFER.
230 The function should take one argument, OTHER-BUFFER, and return
231 non-nil if that buffer should be searched. Have a look at
232 `dabbrev--same-major-mode-p' for an example.
234 The value of `dabbrev-friend-buffer-function' has an effect only if
235 the value of `dabbrev-select-buffers-function' uses it. The function
236 `dabbrev--select-buffers' is one function you can use here.
238 A mode setting this variable should make it buffer local."
242 (defcustom dabbrev-search-these-buffers-only nil
243 "If non-nil, a list of buffers which dabbrev should search.
244 If this variable is non-nil, dabbrev will only look in these buffers.
245 It will not even look in the current buffer if it is not a member of
248 ;;----------------------------------------------------------------
249 ;; Internal variables
250 ;;----------------------------------------------------------------
252 ;; Last obarray of completions in `dabbrev-completion'
253 (defvar dabbrev--last-obarray nil
)
255 ;; Table of expansions seen so far
256 (defvar dabbrev--last-table nil
)
258 ;; Last string we tried to expand.
259 (defvar dabbrev--last-abbreviation nil
)
261 ;; Location last abbreviation began
262 (defvar dabbrev--last-abbrev-location nil
)
264 ;; Direction of last dabbrevs search
265 (defvar dabbrev--last-direction
0)
267 ;; Last expansion of an abbreviation.
268 (defvar dabbrev--last-expansion nil
)
270 ;; Location the last expansion was found.
271 (defvar dabbrev--last-expansion-location nil
)
273 ;; The list of remaining buffers with the same mode as current buffer.
274 (defvar dabbrev--friend-buffer-list nil
)
276 ;; The buffer we looked in last.
277 (defvar dabbrev--last-buffer nil
)
279 ;; The buffer we found the expansion last time.
280 (defvar dabbrev--last-buffer-found nil
)
282 ;; The buffer we last did a completion in.
283 (defvar dabbrev--last-completion-buffer nil
)
285 ;; Non-nil means we should upcase
286 ;; when copying successive words.
287 (defvar dabbrev--last-case-pattern nil
)
289 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
290 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers
)
292 ;; The regexp for recognizing a character in an abbreviation.
293 (defvar dabbrev--abbrev-char-regexp nil
)
295 ;;----------------------------------------------------------------
297 ;;----------------------------------------------------------------
299 ;;; Get the buffer that mini-buffer was activated from
300 (defsubst dabbrev--minibuffer-origin
()
301 (car (cdr (buffer-list))))
303 ;; Make a list of some of the elements of LIST.
304 ;; Check each element of LIST, storing it temporarily in the
305 ;; variable ELEMENT, and include it in the result
306 ;; if CONDITION evaluates non-nil.
307 (defmacro dabbrev-filter-elements
(element list condition
)
308 (` (let (dabbrev-result dabbrev-tail
(, element
))
309 (setq dabbrev-tail
(, list
))
311 (setq (, element
) (car dabbrev-tail
))
313 (setq dabbrev-result
(cons (, element
) dabbrev-result
)))
314 (setq dabbrev-tail
(cdr dabbrev-tail
)))
315 (nreverse dabbrev-result
))))
317 ;;----------------------------------------------------------------
318 ;; Exported functions
319 ;;----------------------------------------------------------------
322 (define-key esc-map
"/" 'dabbrev-expand
)
323 ;;;??? Do we want this?
325 (define-key esc-map
[?\C-
/] 'dabbrev-completion
)
328 (defun dabbrev-completion (&optional arg
)
329 "Completion on current word.
330 Like \\[dabbrev-expand] but finds all expansions in the current buffer
331 and presents suggestions for completion.
333 With a prefix argument, it searches all buffers accepted by the
334 function pointed out by `dabbrev-friend-buffer-function' to find the
337 If the prefix argument is 16 (which comes from C-u C-u),
338 then it searches *all* buffers.
340 With no prefix argument, it reuses an old completion list
341 if there is a suitable one already."
344 (dabbrev--reset-global-variables)
345 (let* ((dabbrev-check-other-buffers (and arg t
))
346 (dabbrev-check-all-buffers
347 (and arg
(= (prefix-numeric-value arg
) 16)))
348 (abbrev (dabbrev--abbrev-at-point))
349 (ignore-case-p (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
351 dabbrev-case-fold-search
)
352 (or (not dabbrev-upcase-means-case-search
)
353 (string= abbrev
(downcase abbrev
)))))
354 (my-obarray dabbrev--last-obarray
)
359 (or (eq dabbrev--last-completion-buffer
(current-buffer))
360 (and (window-minibuffer-p (selected-window))
361 (eq dabbrev--last-completion-buffer
362 (dabbrev--minibuffer-origin))))
363 dabbrev--last-abbreviation
364 (>= (length abbrev
) (length dabbrev--last-abbreviation
))
365 (string= dabbrev--last-abbreviation
367 (length dabbrev--last-abbreviation
)))
368 (setq init
(try-completion abbrev my-obarray
)))
369 ;; We can reuse the existing completion list.
371 ;;--------------------------------
372 ;; New abbreviation to expand.
373 ;;--------------------------------
374 (setq dabbrev--last-abbreviation abbrev
)
375 ;; Find all expansion
376 (let ((completion-list
377 (dabbrev--find-all-expansions abbrev ignore-case-p
))
378 (completion-ignore-case ignore-case-p
))
379 ;; Make an obarray with all expansions
380 (setq my-obarray
(make-vector (length completion-list
) 0))
381 (or (> (length my-obarray
) 0)
382 (error "No dynamic expansion for \"%s\" found%s"
384 (if dabbrev--check-other-buffers
"" " in this-buffer")))
386 ((or (not ignore-case-p
)
387 (not dabbrev-case-replace
))
388 (mapcar (function (lambda (string)
389 (intern string my-obarray
)))
391 ((string= abbrev
(upcase abbrev
))
392 (mapcar (function (lambda (string)
393 (intern (upcase string
) my-obarray
)))
395 ((string= (substring abbrev
0 1)
396 (upcase (substring abbrev
0 1)))
397 (mapcar (function (lambda (string)
398 (intern (capitalize string
) my-obarray
)))
401 (mapcar (function (lambda (string)
402 (intern (downcase string
) my-obarray
)))
404 (setq dabbrev--last-obarray my-obarray
)
405 (setq dabbrev--last-completion-buffer
(current-buffer))
406 ;; Find the longest common string.
407 (setq init
(try-completion abbrev my-obarray
)))))
408 ;;--------------------------------
409 ;; Let the user choose between the expansions
410 ;;--------------------------------
414 ;; * Replace string fragment with matched common substring completion.
415 ((and (not (string-equal init
""))
416 (not (string-equal (downcase init
) (downcase abbrev
))))
417 (if (> (length (all-completions init my-obarray
)) 1)
418 (message "Repeat `%s' to see all completions"
419 (key-description (this-command-keys)))
420 (message "The only possible completion"))
421 (dabbrev--substitute-expansion nil abbrev init
))
423 ;; * String is a common substring completion already. Make list.
424 (message "Making completion list...")
425 (with-output-to-temp-buffer " *Completions*"
426 (display-completion-list (all-completions init my-obarray
)))
427 (message "Making completion list...done")))
428 (and (window-minibuffer-p (selected-window))
432 (defun dabbrev-expand (arg)
433 "Expand previous word \"dynamically\".
435 Expands to the most recent, preceding word for which this is a prefix.
436 If no suitable preceding word is found, words following point are
437 considered. If still no suitable word is found, then look in the
438 buffers accepted by the function pointed out by variable
439 `dabbrev-friend-buffer-function'.
441 A positive prefix argument, N, says to take the Nth backward *distinct*
442 possibility. A negative argument says search forward.
444 If the cursor has not moved from the end of the previous expansion and
445 no argument is given, replace the previously-made expansion
446 with the next possible expansion not yet tried.
448 The variable `dabbrev-backward-only' may be used to limit the
449 direction of search to backward if set non-nil.
451 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
453 (let (abbrev record-case-pattern
454 expansion old direction
(orig-point (point)))
455 ;; abbrev -- the abbrev to expand
456 ;; expansion -- the expansion found (eventually) or nil until then
457 ;; old -- the text currently in the buffer
458 ;; (the abbrev, or the previously-made expansion)
461 (markerp dabbrev--last-abbrev-location
)
462 (marker-position dabbrev--last-abbrev-location
)
463 (or (eq last-command this-command
)
464 (and (window-minibuffer-p (selected-window))
465 (= dabbrev--last-abbrev-location
467 ;; Find a different expansion for the same abbrev as last time.
469 (setq abbrev dabbrev--last-abbreviation
)
470 (setq old dabbrev--last-expansion
)
471 (setq direction dabbrev--last-direction
))
472 ;; If the user inserts a space after expanding
473 ;; and then asks to expand again, always fetch the next word.
474 (if (and (eq (preceding-char) ?\
)
475 (markerp dabbrev--last-abbrev-location
)
476 (marker-position dabbrev--last-abbrev-location
)
477 (= (point) (1+ dabbrev--last-abbrev-location
)))
479 ;; The "abbrev" to expand is just the space.
482 (if dabbrev--last-buffer
483 (set-buffer dabbrev--last-buffer
))
484 ;; Find the end of the last "expansion" word.
485 (if (or (eq dabbrev--last-direction
1)
486 (and (eq dabbrev--last-direction
0)
487 (< dabbrev--last-expansion-location
(point))))
488 (setq dabbrev--last-expansion-location
489 (+ dabbrev--last-expansion-location
490 (length dabbrev--last-expansion
))))
491 (goto-char dabbrev--last-expansion-location
)
492 ;; Take the following word, with intermediate separators,
493 ;; as our expansion this time.
495 (concat "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
497 (buffer-substring dabbrev--last-expansion-location
499 (if dabbrev--last-case-pattern
500 (setq expansion
(upcase expansion
)))
502 ;; Record the end of this expansion, in case we repeat this.
503 (setq dabbrev--last-expansion-location
(point)))
504 ;; Indicate that dabbrev--last-expansion-location is
505 ;; at the end of the expansion.
506 (setq dabbrev--last-direction -
1))
508 ;; We have a different abbrev to expand.
509 (dabbrev--reset-global-variables)
510 (setq direction
(if (null arg
)
511 (if dabbrev-backward-only
1 0)
512 (prefix-numeric-value arg
)))
513 (setq abbrev
(dabbrev--abbrev-at-point))
514 (setq record-case-pattern t
)
517 ;;--------------------------------
518 ;; Find the expansion
519 ;;--------------------------------
522 (dabbrev--find-expansion abbrev direction
523 (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
525 dabbrev-case-fold-search
)
526 (or (not dabbrev-upcase-means-case-search
)
527 (string= abbrev
(downcase abbrev
))))))))
530 (dabbrev--reset-global-variables)
533 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
534 ;; Put back the original abbrev with its original case pattern.
535 (search-backward old
)
537 (delete-region (point) (+ (point) (length old
)))))
538 (error "No%s dynamic expansion for `%s' found"
539 (if old
" further" "") abbrev
))
541 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found
))
543 (message "Expansion found in '%s'"
544 (buffer-name dabbrev--last-buffer
))
545 (setq dabbrev--last-buffer-found dabbrev--last-buffer
))
547 (if (and (or (eq (current-buffer) dabbrev--last-buffer
)
548 (null dabbrev--last-buffer
))
549 (numberp dabbrev--last-expansion-location
)
550 (and (> dabbrev--last-expansion-location
(point))))
551 (setq dabbrev--last-expansion-location
552 (copy-marker dabbrev--last-expansion-location
)))
553 ;; Success: stick it in and return.
554 (setq buffer-undo-list
(cons orig-point buffer-undo-list
))
555 (dabbrev--substitute-expansion old abbrev expansion
)
557 ;; If we are not copying successive words now,
558 ;; set dabbrev--last-case-pattern.
559 (and record-case-pattern
560 (setq dabbrev--last-case-pattern
561 (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
563 dabbrev-case-fold-search
)
564 (not dabbrev-upcase-means-case-search
)
565 (equal abbrev
(upcase abbrev
)))))
567 ;; Save state for re-expand.
568 (setq dabbrev--last-expansion expansion
)
569 (setq dabbrev--last-abbreviation abbrev
)
570 (setq dabbrev--last-abbrev-location
(point-marker))))))
572 ;;----------------------------------------------------------------
574 ;;----------------------------------------------------------------
576 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
577 (defun dabbrev--same-major-mode-p (other-buffer)
580 (set-buffer other-buffer
)
583 ;;; Back over all abbrev type characters and then moves forward over
584 ;;; all skip characters.
585 (defun dabbrev--goto-start-of-abbrev ()
586 ;; Move backwards over abbrev chars
591 (while (and (looking-at dabbrev--abbrev-char-regexp
)
594 (or (looking-at dabbrev--abbrev-char-regexp
)
596 (and dabbrev-abbrev-skip-leading-regexp
597 (while (looking-at dabbrev-abbrev-skip-leading-regexp
)
600 ;;; Extract the symbol at point to serve as abbreviation.
601 (defun dabbrev--abbrev-at-point ()
604 (error "No possible abbreviation preceding point"))
605 ;; Return abbrev at point
607 ;; Record the end of the abbreviation.
608 (setq dabbrev--last-abbrev-location
(point))
609 ;; If we aren't right after an abbreviation,
610 ;; move point back to just after one.
611 ;; This is so the user can get successive words
612 ;; by typing the punctuation followed by M-/.
616 (not (looking-at (concat "\\("
617 (or dabbrev-abbrev-char-regexp
620 (if (re-search-backward (or dabbrev-abbrev-char-regexp
624 (error "No possible abbreviation preceding point"))))
625 ;; Now find the beginning of that one.
626 (dabbrev--goto-start-of-abbrev)
627 (buffer-substring dabbrev--last-abbrev-location
630 ;;; Initializes all global variables
631 (defun dabbrev--reset-global-variables ()
632 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
633 ;; must not be reset here.
634 (setq dabbrev--last-table nil
635 dabbrev--last-abbreviation nil
636 dabbrev--last-abbrev-location nil
637 dabbrev--last-direction nil
638 dabbrev--last-expansion nil
639 dabbrev--last-expansion-location nil
640 dabbrev--friend-buffer-list nil
641 dabbrev--last-buffer nil
642 dabbrev--last-buffer-found nil
643 dabbrev--abbrev-char-regexp
(or dabbrev-abbrev-char-regexp
645 dabbrev--check-other-buffers dabbrev-check-other-buffers
))
647 ;;; Find all buffers that are considered "friends" according to the
648 ;;; function pointed out by dabbrev-friend-buffer-function.
649 (defun dabbrev--select-buffers ()
651 (and (window-minibuffer-p (selected-window))
652 (set-buffer (dabbrev--minibuffer-origin)))
653 (let ((orig-buffer (current-buffer)))
654 (dabbrev-filter-elements
656 (and (not (eq orig-buffer buffer
))
657 (boundp 'dabbrev-friend-buffer-function
)
658 (funcall dabbrev-friend-buffer-function buffer
))))))
660 ;;; Search for ABBREV, N times, normally looking forward,
661 ;;; but looking in reverse instead if REVERSE is non-nil.
662 (defun dabbrev--try-find (abbrev reverse n ignore-case
)
666 (let ((expansion nil
))
667 (and dabbrev--last-expansion-location
668 (goto-char dabbrev--last-expansion-location
))
669 (let ((case-fold-search ignore-case
)
671 (while (and (> count
0)
672 (setq expansion
(dabbrev--search abbrev
675 (setq count
(1- count
))))
677 (setq dabbrev--last-expansion-location
(point)))
680 ;;; Find all expansions of ABBREV
681 (defun dabbrev--find-all-expansions (abbrev ignore-case
)
682 (let ((all-expansions nil
)
685 (goto-char (point-min))
686 (while (setq expansion
(dabbrev--find-expansion abbrev -
1 ignore-case
))
687 (setq all-expansions
(cons expansion all-expansions
))))
690 (defun dabbrev--scanning-message ()
691 (message "Scanning `%s'" (buffer-name (current-buffer))))
693 ;;; Find one occasion of ABBREV.
694 ;;; DIRECTION > 0 means look that many times backwards.
695 ;;; DIRECTION < 0 means look that many times forward.
696 ;;; DIRECTION = 0 means try both backward and forward.
697 ;;; IGNORE-CASE non-nil means ignore case when searching.
698 (defun dabbrev--find-expansion (abbrev direction ignore-case
)
702 (dabbrev--last-buffer
703 (set-buffer dabbrev--last-buffer
)
704 (dabbrev--scanning-message))
705 ((and (not dabbrev-search-these-buffers-only
)
706 (window-minibuffer-p (selected-window)))
707 (set-buffer (dabbrev--minibuffer-origin))
708 ;; In the minibuffer-origin buffer we will only search from
710 (goto-char (point-min))
712 (dabbrev--scanning-message)))
714 ;; ------------------------------------------
716 ;; ------------------------------------------
717 ((and (not dabbrev-search-these-buffers-only
)
719 (setq dabbrev--last-direction
(min 1 direction
))
720 (setq expansion
(dabbrev--try-find abbrev t
724 ;; ------------------------------------------
726 ;; ------------------------------------------
727 ((and (or (not dabbrev-search-these-buffers-only
)
728 dabbrev--last-buffer
)
730 (setq dabbrev--last-direction -
1)
731 (setq expansion
(dabbrev--try-find abbrev nil
732 (max 1 (- direction
))
735 ;; ------------------------------------------
736 ;; Look in other buffers.
737 ;; Start at (point-min) and look forward.
738 ;; ------------------------------------------
740 (setq dabbrev--last-direction -
1)
741 ;; Make sure that we should check other buffers
742 (or dabbrev--friend-buffer-list
744 (setq dabbrev--friend-buffer-list
745 (mapcar (function get-buffer
)
746 dabbrev-search-these-buffers-only
))
747 (not dabbrev--check-other-buffers
)
748 (not (or (eq dabbrev--check-other-buffers t
)
750 (setq dabbrev--check-other-buffers
751 (y-or-n-p "Scan other buffers also? ")))))
752 (let* (friend-buffer-list non-friend-buffer-list
)
753 (setq dabbrev--friend-buffer-list
754 (funcall dabbrev-select-buffers-function
))
755 (if dabbrev-check-all-buffers
756 (setq non-friend-buffer-list
758 (dabbrev-filter-elements
760 (not (memq buffer dabbrev--friend-buffer-list
))))
761 dabbrev--friend-buffer-list
762 (append dabbrev--friend-buffer-list
763 non-friend-buffer-list
)))))
764 ;; Move buffers that are visible on the screen
765 ;; to the front of the list.
766 (if dabbrev--friend-buffer-list
767 (let ((w (next-window (selected-window))))
768 (while (not (eq w
(selected-window)))
769 (setq dabbrev--friend-buffer-list
770 (cons (window-buffer w
)
771 (delq (window-buffer w
) dabbrev--friend-buffer-list
)))
772 (setq w
(next-window w
)))))
773 ;; Walk through the buffers
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 (defun dabbrev--safe-replace-match (string &optional fixedcase literal
)
786 (if (eq major-mode
'picture-mode
)
787 (picture-replace-match string fixedcase literal
)
788 (replace-match string fixedcase literal
)))
790 ;;;----------------------------------------------------------------
791 ;;; Substitute the current string in buffer with the expansion
792 ;;; OLD is nil or the last expansion substring.
793 ;;; ABBREV is the abbreviation we are working with.
794 ;;; EXPANSION is the expansion substring.
795 (defun dabbrev--substitute-expansion (old abbrev expansion
)
797 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search
'case-fold-search
)
799 dabbrev-case-fold-search
)
800 (or (not dabbrev-upcase-means-case-search
)
801 (string= abbrev
(downcase abbrev
)))
802 (if (eq dabbrev-case-replace
'case-replace
)
804 dabbrev-case-replace
))))
805 (and nil use-case-replace
806 (setq old
(concat abbrev
(or old
"")))
807 (setq expansion
(concat abbrev expansion
)))
808 ;; If the expansion has mixed case
809 ;; and it is not simply a capitalized word,
810 ;; or if the abbrev has mixed case,
811 ;; and if the given abbrev's case pattern
812 ;; matches the start of the expansion,
813 ;; copy the expansion's case
814 ;; instead of downcasing all the rest.
815 (let ((expansion-rest (substring expansion
1)))
816 (if (and (not (and (or (string= expansion-rest
(downcase expansion-rest
))
817 (string= expansion-rest
(upcase expansion-rest
)))
818 (or (string= abbrev
(downcase abbrev
))
819 (string= abbrev
(upcase abbrev
)))))
821 (substring expansion
0 (length abbrev
))))
822 (setq use-case-replace nil
)))
823 (if (equal abbrev
" ")
824 (setq use-case-replace nil
))
826 (setq expansion
(downcase expansion
)))
829 (search-backward old
))
830 ;;(store-match-data (list (point-marker) (point-marker)))
831 (search-backward abbrev
))
832 ;; Make case of replacement conform to case of abbreviation
833 ;; provided (1) that kind of thing is enabled in this buffer
834 ;; and (2) the replacement itself is all lower case.
835 (dabbrev--safe-replace-match expansion
836 (not use-case-replace
)
840 ;;;----------------------------------------------------------------
841 ;;; Search function used by dabbrevs library.
843 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
844 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
845 ;;; controls the maximum search region size. Third argument IGNORE-CASE
846 ;;; non-nil means treat case as insignificant while looking for a match
847 ;;; and when comparing with previous matches. Also if that's non-nil
848 ;;; and the match is found at the beginning of a sentence and is in
849 ;;; lower case except for the initial then it is converted to all lower
852 ;;; Table of expansions already seen is examined in buffer
853 ;;; `dabbrev--last-table' so that only distinct possibilities are found
854 ;;; by dabbrev-re-expand.
856 ;;; Value is the expansion, or nil if not found.
858 (defun dabbrev--search (abbrev reverse ignore-case
)
860 (let ((pattern1 (concat (regexp-quote abbrev
)
861 "\\(" dabbrev--abbrev-char-regexp
"\\)"))
862 (pattern2 (concat (regexp-quote abbrev
)
863 "\\(\\(" dabbrev--abbrev-char-regexp
"\\)+\\)"))
868 (narrow-to-region dabbrev--last-expansion-location
870 (if reverse
(- dabbrev-limit
) dabbrev-limit
))))
871 ;;--------------------------------
872 ;; Look for a distinct expansion, using dabbrev--last-table.
873 ;;--------------------------------
874 (while (and (not found-string
)
876 (re-search-backward pattern1 nil t
)
877 (re-search-forward pattern1 nil t
)))
878 (goto-char (match-beginning 0))
879 ;; In case we matched in the middle of a word,
880 ;; back up to start of word and verify we still match.
881 (dabbrev--goto-start-of-abbrev)
883 (if (not (looking-at pattern1
))
885 ;; We have a truly valid match. Find the end.
886 (re-search-forward pattern2
)
888 (buffer-substring (match-beginning 1) (match-end 1)))
889 (and ignore-case
(setq found-string
(downcase found-string
)))
890 ;; Ignore this match if it's already in the table.
891 (if (dabbrev-filter-elements
892 table-string dabbrev--last-table
893 (string= found-string table-string
))
894 (setq found-string nil
)))
895 ;; Prepare to continue searching.
897 (goto-char (match-beginning 0))
898 (goto-char (match-end 0))))
899 ;; If we found something, use it.
901 ;; Put it into `dabbrev--last-table'
902 ;; and return it (either downcased, or as is).
904 (buffer-substring (match-beginning 0) (match-end 0))))
905 (setq dabbrev--last-table
906 (cons found-string dabbrev--last-table
))
907 (if (and ignore-case
(eval dabbrev-case-replace
))
913 ;;; dabbrev.el ends here