(Reverting): Add anchor.
[emacs.git] / lisp / dabbrev.el
blob47ffba9873dbac5378174d3324f82b0cf604ce9b
1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 86, 92, 94, 96, 1997, 2000, 01, 03, 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Don Morrison
7 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
8 ;; Created: 16 Mars 1992
9 ;; Lindberg's last update version: 5.7
10 ;; Keywords: abbrev expand completion convenience
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
29 ;;; Commentary:
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
33 ;; them.
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)
63 ;; (lambda (buffer)
64 ;; (save-excursion
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
71 ;; minibuffer.
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.
96 ;;; Code:
98 ;;----------------------------------------------------------------
99 ;; Customization variables
100 ;;----------------------------------------------------------------
102 (defgroup dabbrev nil
103 "Dynamic Abbreviations"
104 :tag "Dynamic Abbreviations"
105 :group 'abbrev
106 :group 'convenience)
108 (defcustom dabbrev-backward-only nil
109 "*If non-nil, `dabbrev-expand' only looks backwards."
110 :type 'boolean
111 :group 'dabbrev)
113 (defcustom dabbrev-limit nil
114 "*Limits region searched by `dabbrev-expand' to this many chars away."
115 :type '(choice (const :tag "off" nil)
116 integer)
117 :group 'dabbrev)
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))
129 :group 'dabbrev)
131 (defcustom dabbrev--eliminate-newlines t
132 "*Non-nil means dabbrev should not insert newlines.
133 Instead it converts them to spaces."
134 :type 'boolean
135 :group 'dabbrev)
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)
145 (other :tag "on" t))
146 :group 'dabbrev)
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."
155 :type 'boolean
156 :group 'dabbrev)
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)
169 (other :tag "on" t))
170 :group 'dabbrev
171 :version "21.4")
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)
185 (other :tag "on" t))
186 :group 'dabbrev)
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)
210 regexp)
211 :group 'dabbrev)
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'."
223 :type 'boolean
224 :group 'dabbrev)
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"))
230 :group 'dabbrev
231 :version "20.3")
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)
237 :group 'dabbrev
238 :version "21.1")
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
248 buffers too.
250 The default value is t."
251 :type '(choice (const :tag "off" nil)
252 (const :tag "on" t)
253 (other :tag "ask" other))
254 :group 'dabbrev)
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'
263 for an example.
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."
278 :type 'function
279 :group 'dabbrev)
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
285 this list.")
287 ;;----------------------------------------------------------------
288 ;; Internal variables
289 ;;----------------------------------------------------------------
291 ;; Last obarray of completions in `dabbrev-completion'
292 (defvar dabbrev--last-obarray nil)
294 ;; Table of expansions seen so far
295 (defvar dabbrev--last-table nil)
297 ;; Last string we tried to expand.
298 (defvar dabbrev--last-abbreviation nil)
300 ;; Location last abbreviation began
301 (defvar dabbrev--last-abbrev-location nil)
303 ;; Direction of last dabbrevs search
304 (defvar dabbrev--last-direction 0)
306 ;; Last expansion of an abbreviation.
307 (defvar dabbrev--last-expansion nil)
309 ;; Location the last expansion was found.
310 (defvar dabbrev--last-expansion-location nil)
312 ;; The list of remaining buffers with the same mode as current buffer.
313 (defvar dabbrev--friend-buffer-list nil)
315 ;; The buffer we looked in last, not counting the current buffer.
316 (defvar dabbrev--last-buffer nil)
318 ;; The buffer we found the expansion last time.
319 (defvar dabbrev--last-buffer-found nil)
321 ;; The buffer we last did a completion in.
322 (defvar dabbrev--last-completion-buffer nil)
324 ;; If non-nil, a function to use when copying successive words.
325 ;; It should be `upcase' or `downcase'.
326 (defvar dabbrev--last-case-pattern nil)
328 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
329 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
331 ;; The regexp for recognizing a character in an abbreviation.
332 (defvar dabbrev--abbrev-char-regexp nil)
334 ;;----------------------------------------------------------------
335 ;; Macros
336 ;;----------------------------------------------------------------
338 ;;; Get the buffer that mini-buffer was activated from
339 (defsubst dabbrev--minibuffer-origin ()
340 (car (cdr (buffer-list))))
342 ;; Make a list of some of the elements of LIST.
343 ;; Check each element of LIST, storing it temporarily in the
344 ;; variable ELEMENT, and include it in the result
345 ;; if CONDITION evaluates non-nil.
346 (defmacro dabbrev-filter-elements (element list condition)
347 `(let (dabbrev-result dabbrev-tail ,element)
348 (setq dabbrev-tail ,list)
349 (while dabbrev-tail
350 (setq ,element (car dabbrev-tail))
351 (if ,condition
352 (setq dabbrev-result (cons ,element dabbrev-result)))
353 (setq dabbrev-tail (cdr dabbrev-tail)))
354 (nreverse dabbrev-result)))
356 ;;----------------------------------------------------------------
357 ;; Exported functions
358 ;;----------------------------------------------------------------
360 ;;;###autoload (define-key esc-map "/" 'dabbrev-expand)
361 ;;;??? Do we want this?
362 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion)
364 ;;;###autoload
365 (defun dabbrev-completion (&optional arg)
366 "Completion on current word.
367 Like \\[dabbrev-expand] but finds all expansions in the current buffer
368 and presents suggestions for completion.
370 With a prefix argument, it searches all buffers accepted by the
371 function pointed out by `dabbrev-friend-buffer-function' to find the
372 completions.
374 If the prefix argument is 16 (which comes from C-u C-u),
375 then it searches *all* buffers.
377 With no prefix argument, it reuses an old completion list
378 if there is a suitable one already."
380 (interactive "*P")
381 (dabbrev--reset-global-variables)
382 (let* ((dabbrev-check-other-buffers (and arg t))
383 (dabbrev-check-all-buffers
384 (and arg (= (prefix-numeric-value arg) 16)))
385 (abbrev (dabbrev--abbrev-at-point))
386 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
387 case-fold-search
388 dabbrev-case-fold-search)
389 (or (not dabbrev-upcase-means-case-search)
390 (string= abbrev (downcase abbrev)))))
391 (my-obarray dabbrev--last-obarray)
392 init)
393 (save-excursion
394 (if (and (null arg)
395 my-obarray
396 (or (eq dabbrev--last-completion-buffer (current-buffer))
397 (and (window-minibuffer-p (selected-window))
398 (eq dabbrev--last-completion-buffer
399 (dabbrev--minibuffer-origin))))
400 dabbrev--last-abbreviation
401 (>= (length abbrev) (length dabbrev--last-abbreviation))
402 (string= dabbrev--last-abbreviation
403 (substring abbrev 0
404 (length dabbrev--last-abbreviation)))
405 (setq init (try-completion abbrev my-obarray)))
406 ;; We can reuse the existing completion list.
408 ;;--------------------------------
409 ;; New abbreviation to expand.
410 ;;--------------------------------
411 (setq dabbrev--last-abbreviation abbrev)
412 ;; Find all expansion
413 (let ((completion-list
414 (dabbrev--find-all-expansions abbrev ignore-case-p))
415 (completion-ignore-case ignore-case-p))
416 ;; Make an obarray with all expansions
417 (setq my-obarray (make-vector (length completion-list) 0))
418 (or (> (length my-obarray) 0)
419 (error "No dynamic expansion for \"%s\" found%s"
420 abbrev
421 (if dabbrev--check-other-buffers "" " in this-buffer")))
422 (cond
423 ((or (not ignore-case-p)
424 (not dabbrev-case-replace))
425 (mapc (function (lambda (string)
426 (intern string my-obarray)))
427 completion-list))
428 ((string= abbrev (upcase abbrev))
429 (mapc (function (lambda (string)
430 (intern (upcase string) my-obarray)))
431 completion-list))
432 ((string= (substring abbrev 0 1)
433 (upcase (substring abbrev 0 1)))
434 (mapc (function (lambda (string)
435 (intern (capitalize string) my-obarray)))
436 completion-list))
438 (mapc (function (lambda (string)
439 (intern (downcase string) my-obarray)))
440 completion-list)))
441 (setq dabbrev--last-obarray my-obarray)
442 (setq dabbrev--last-completion-buffer (current-buffer))
443 ;; Find the longest common string.
444 (setq init (try-completion abbrev my-obarray)))))
445 ;;--------------------------------
446 ;; Let the user choose between the expansions
447 ;;--------------------------------
448 (or (stringp init)
449 (setq init abbrev))
450 (cond
451 ;; * Replace string fragment with matched common substring completion.
452 ((and (not (string-equal init ""))
453 (not (string-equal (downcase init) (downcase abbrev))))
454 (if (> (length (all-completions init my-obarray)) 1)
455 (message "Repeat `%s' to see all completions"
456 (key-description (this-command-keys)))
457 (message "The only possible completion"))
458 (dabbrev--substitute-expansion nil abbrev init nil))
460 ;; * String is a common substring completion already. Make list.
461 (message "Making completion list...")
462 (with-output-to-temp-buffer "*Completions*"
463 (display-completion-list (all-completions init my-obarray)))
464 (message "Making completion list...done")))
465 (and (window-minibuffer-p (selected-window))
466 (message nil))))
468 ;;;###autoload
469 (defun dabbrev-expand (arg)
470 "Expand previous word \"dynamically\".
472 Expands to the most recent, preceding word for which this is a prefix.
473 If no suitable preceding word is found, words following point are
474 considered. If still no suitable word is found, then look in the
475 buffers accepted by the function pointed out by variable
476 `dabbrev-friend-buffer-function'.
478 A positive prefix argument, N, says to take the Nth backward *distinct*
479 possibility. A negative argument says search forward.
481 If the cursor has not moved from the end of the previous expansion and
482 no argument is given, replace the previously-made expansion
483 with the next possible expansion not yet tried.
485 The variable `dabbrev-backward-only' may be used to limit the
486 direction of search to backward if set non-nil.
488 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
489 (interactive "*P")
490 (let (abbrev record-case-pattern
491 expansion old direction (orig-point (point)))
492 ;; abbrev -- the abbrev to expand
493 ;; expansion -- the expansion found (eventually) or nil until then
494 ;; old -- the text currently in the buffer
495 ;; (the abbrev, or the previously-made expansion)
496 (save-excursion
497 (if (and (null arg)
498 (markerp dabbrev--last-abbrev-location)
499 (marker-position dabbrev--last-abbrev-location)
500 (or (eq last-command this-command)
501 (and (window-minibuffer-p (selected-window))
502 (= dabbrev--last-abbrev-location
503 (point)))))
504 ;; Find a different expansion for the same abbrev as last time.
505 (progn
506 (setq abbrev dabbrev--last-abbreviation)
507 (setq old dabbrev--last-expansion)
508 (setq direction dabbrev--last-direction))
509 ;; If the user inserts a space after expanding
510 ;; and then asks to expand again, always fetch the next word.
511 (if (and (eq (preceding-char) ?\ )
512 (markerp dabbrev--last-abbrev-location)
513 (marker-position dabbrev--last-abbrev-location)
514 (= (point) (1+ dabbrev--last-abbrev-location)))
515 (progn
516 ;; The "abbrev" to expand is just the space.
517 (setq abbrev " ")
518 (save-excursion
519 (if dabbrev--last-buffer
520 (set-buffer dabbrev--last-buffer))
521 ;; Find the end of the last "expansion" word.
522 (if (or (eq dabbrev--last-direction 1)
523 (and (eq dabbrev--last-direction 0)
524 (< dabbrev--last-expansion-location (point))))
525 (setq dabbrev--last-expansion-location
526 (+ dabbrev--last-expansion-location
527 (length dabbrev--last-expansion))))
528 (goto-char dabbrev--last-expansion-location)
529 ;; Take the following word, with intermediate separators,
530 ;; as our expansion this time.
531 (re-search-forward
532 (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
533 (setq expansion (buffer-substring-no-properties
534 dabbrev--last-expansion-location (point)))
536 ;; Record the end of this expansion, in case we repeat this.
537 (setq dabbrev--last-expansion-location (point)))
538 ;; Indicate that dabbrev--last-expansion-location is
539 ;; at the end of the expansion.
540 (setq dabbrev--last-direction -1))
542 ;; We have a different abbrev to expand.
543 (dabbrev--reset-global-variables)
544 (setq direction (if (null arg)
545 (if dabbrev-backward-only 1 0)
546 (prefix-numeric-value arg)))
547 (setq abbrev (dabbrev--abbrev-at-point))
548 (setq record-case-pattern t)
549 (setq old nil)))
551 ;;--------------------------------
552 ;; Find the expansion
553 ;;--------------------------------
554 (or expansion
555 (setq expansion
556 (dabbrev--find-expansion abbrev direction
557 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
558 case-fold-search
559 dabbrev-case-fold-search)
560 (or (not dabbrev-upcase-means-case-search)
561 (string= abbrev (downcase abbrev))))))))
562 (cond
563 ((not expansion)
564 (dabbrev--reset-global-variables)
565 (if old
566 (save-excursion
567 (setq buffer-undo-list (cons orig-point buffer-undo-list))
568 ;; Put back the original abbrev with its original case pattern.
569 (search-backward old)
570 (insert abbrev)
571 (delete-region (point) (+ (point) (length old)))))
572 (error "No%s dynamic expansion for `%s' found"
573 (if old " further" "") abbrev))
575 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
576 (minibuffer-window-active-p (selected-window))))
577 (progn
578 (message "Expansion found in '%s'"
579 (buffer-name dabbrev--last-buffer))
580 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
581 (message nil))
582 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
583 (null dabbrev--last-buffer))
584 (numberp dabbrev--last-expansion-location)
585 (and (> dabbrev--last-expansion-location (point))))
586 (setq dabbrev--last-expansion-location
587 (copy-marker dabbrev--last-expansion-location)))
588 ;; Success: stick it in and return.
589 (setq buffer-undo-list (cons orig-point buffer-undo-list))
590 (dabbrev--substitute-expansion old abbrev expansion
591 record-case-pattern)
593 ;; Save state for re-expand.
594 (setq dabbrev--last-expansion expansion)
595 (setq dabbrev--last-abbreviation abbrev)
596 (setq dabbrev--last-abbrev-location (point-marker))))))
598 ;;----------------------------------------------------------------
599 ;; Local functions
600 ;;----------------------------------------------------------------
602 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
603 (defun dabbrev--same-major-mode-p (other-buffer)
604 (eq major-mode
605 (save-excursion
606 (set-buffer other-buffer)
607 major-mode)))
609 ;;; Back over all abbrev type characters and then moves forward over
610 ;;; all skip characters.
611 (defun dabbrev--goto-start-of-abbrev ()
612 ;; Move backwards over abbrev chars
613 (save-match-data
614 (when (> (point) (minibuffer-prompt-end))
615 (forward-char -1)
616 (while (and (looking-at dabbrev--abbrev-char-regexp)
617 (> (point) (minibuffer-prompt-end))
618 (not (= (point) (field-beginning (point) nil
619 (1- (point))))))
620 (forward-char -1))
621 (or (looking-at dabbrev--abbrev-char-regexp)
622 (forward-char 1)))
623 (and dabbrev-abbrev-skip-leading-regexp
624 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
625 (forward-char 1)))))
627 ;;; Extract the symbol at point to serve as abbreviation.
628 (defun dabbrev--abbrev-at-point ()
629 ;; Check for error
630 (if (bobp)
631 (error "No possible abbreviation preceding point"))
632 ;; Return abbrev at point
633 (save-excursion
634 ;; Record the end of the abbreviation.
635 (setq dabbrev--last-abbrev-location (point))
636 ;; If we aren't right after an abbreviation,
637 ;; move point back to just after one.
638 ;; This is so the user can get successive words
639 ;; by typing the punctuation followed by M-/.
640 (save-match-data
641 (if (save-excursion
642 (forward-char -1)
643 (not (looking-at (concat "\\("
644 (or dabbrev-abbrev-char-regexp
645 "\\sw\\|\\s_")
646 "\\)+"))))
647 (if (re-search-backward (or dabbrev-abbrev-char-regexp
648 "\\sw\\|\\s_")
649 nil t)
650 (forward-char 1)
651 (error "No possible abbreviation preceding point"))))
652 ;; Now find the beginning of that one.
653 (dabbrev--goto-start-of-abbrev)
654 (buffer-substring-no-properties
655 dabbrev--last-abbrev-location (point))))
657 ;;; Initializes all global variables
658 (defun dabbrev--reset-global-variables ()
659 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
660 ;; must not be reset here.
661 (setq dabbrev--last-table nil
662 dabbrev--last-abbreviation nil
663 dabbrev--last-abbrev-location nil
664 dabbrev--last-direction nil
665 dabbrev--last-expansion nil
666 dabbrev--last-expansion-location nil
667 dabbrev--friend-buffer-list nil
668 dabbrev--last-buffer nil
669 dabbrev--last-buffer-found nil
670 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
671 "\\sw\\|\\s_")
672 dabbrev--check-other-buffers dabbrev-check-other-buffers))
674 (defun dabbrev--select-buffers ()
675 "Return a list of other buffers to search for a possible abbrev.
676 The current buffer is not included in the list.
678 This function makes a list of all the buffers returned by `buffer-list',
679 then discards buffers whose names match `dabbrev-ignored-buffer-names'
680 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
681 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
682 with the buffer as argument.
683 It returns the list of the buffers that are not discarded."
684 (dabbrev-filter-elements
685 buffer (buffer-list)
686 (and (not (eq (current-buffer) buffer))
687 (not (dabbrev--ignore-buffer-p buffer))
688 (boundp 'dabbrev-friend-buffer-function)
689 (funcall dabbrev-friend-buffer-function buffer))))
691 (defun dabbrev--try-find (abbrev reverse n ignore-case)
692 "Search for ABBREV, backwards if REVERSE, N times.
693 If IGNORE-CASE is non-nil, ignore case while searching.
694 Return the expansion found, and save the location of the start
695 of the expansion in `dabbrev--last-expansion-location'."
696 (save-excursion
697 (save-restriction
698 (widen)
699 (let ((expansion nil))
700 (and dabbrev--last-expansion-location
701 (goto-char dabbrev--last-expansion-location))
702 (let ((case-fold-search ignore-case)
703 (count n))
704 (while (and (> count 0)
705 (setq expansion (dabbrev--search abbrev
706 reverse
707 (and ignore-case
708 (if (eq dabbrev-case-distinction 'case-replace)
709 case-replace
710 dabbrev-case-distinction))
712 (setq count (1- count))))
713 (and expansion
714 (setq dabbrev--last-expansion-location (point)))
715 expansion))))
717 (defun dabbrev--find-all-expansions (abbrev ignore-case)
718 "Return a list of all possible expansions of ABBREV.
719 If IGNORE-CASE is non-nil, accept matches which differ in case."
720 (let ((all-expansions nil)
721 expansion)
722 (save-excursion
723 (goto-char (point-min))
724 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
725 (setq all-expansions (cons expansion all-expansions))))
726 all-expansions))
728 (defun dabbrev--scanning-message ()
729 (unless (window-minibuffer-p (selected-window))
730 (message "Scanning `%s'" (buffer-name (current-buffer)))))
732 (defun dabbrev--ignore-buffer-p (buffer)
733 "Return non-nil if BUFFER should be ignored by dabbrev."
734 (let ((bn (buffer-name buffer)))
735 (or (member bn dabbrev-ignored-buffer-names)
736 (let ((tail dabbrev-ignored-buffer-regexps)
737 (match nil))
738 (while (and tail (not match))
739 (setq match (string-match (car tail) bn)
740 tail (cdr tail)))
741 match))))
743 (defun dabbrev--find-expansion (abbrev direction ignore-case)
744 "Find one occurrence of ABBREV, and return the expansion.
745 DIRECTION > 0 means look that many times backwards.
746 DIRECTION < 0 means look that many times forward.
747 DIRECTION = 0 means try both backward and forward.
748 IGNORE-CASE non-nil means ignore case when searching.
749 This sets `dabbrev--last-direction' to 1 or -1 according
750 to the direction in which the occurrence was actually found.
751 It sets `dabbrev--last-expansion-location' to the location
752 of the start of the occurrence."
753 (save-excursion
754 ;; If we were scanning something other than the current buffer,
755 ;; continue scanning there.
756 (when dabbrev--last-buffer
757 (set-buffer dabbrev--last-buffer)
758 (dabbrev--scanning-message))
760 ;; ------------------------------------------
761 ;; Look backward in current buffer.
762 ;; ------------------------------------------
763 (and (not dabbrev-search-these-buffers-only)
764 (>= direction 0)
765 (setq dabbrev--last-direction (min 1 direction))
766 (dabbrev--try-find abbrev t
767 (max 1 direction)
768 ignore-case))
769 ;; ------------------------------------------
770 ;; Look forward in current buffer
771 ;; or whatever buffer we were last scanning.
772 ;; ------------------------------------------
773 (and (or (not dabbrev-search-these-buffers-only)
774 dabbrev--last-buffer)
775 (<= direction 0)
776 (setq dabbrev--last-direction -1)
777 (dabbrev--try-find abbrev nil
778 (max 1 (- direction))
779 ignore-case))
780 ;; ------------------------------------------
781 ;; Look in other buffers.
782 ;; Always start at (point-min) and look forward.
783 ;; ------------------------------------------
784 (progn
785 (setq dabbrev--last-direction -1)
786 (unless dabbrev--last-buffer
787 ;; If we have just now begun to search other buffers,
788 ;; determine which other buffers we should check.
789 ;; Put that list in dabbrev--friend-buffer-list.
790 (or dabbrev--friend-buffer-list
791 (setq dabbrev--friend-buffer-list
792 (dabbrev--make-friend-buffer-list))))
793 ;; Walk through the buffers till we find a match.
794 (let (expansion)
795 (while (and (not expansion) dabbrev--friend-buffer-list)
796 (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
797 (set-buffer dabbrev--last-buffer)
798 (dabbrev--scanning-message)
799 (setq dabbrev--last-expansion-location (point-min))
800 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
801 expansion)))))
803 ;; Compute the list of buffers to scan.
804 ;; If dabbrev-search-these-buffers-only, then the current buffer
805 ;; is included in this list if it should be searched.
806 ;; Otherwise, the current buffer is searched first specially.,
807 ;; and it is not included in this list.
808 (defun dabbrev--make-friend-buffer-list ()
809 (let ((list (mapcar (function get-buffer)
810 dabbrev-search-these-buffers-only)))
811 (when (and (null dabbrev-search-these-buffers-only)
812 dabbrev--check-other-buffers
813 (or (eq dabbrev--check-other-buffers t)
814 (setq dabbrev--check-other-buffers
815 (y-or-n-p "Scan other buffers also? "))))
816 (setq list (funcall dabbrev-select-buffers-function))
817 ;; If dabbrev-check-all-buffers, tack on all the other
818 ;; buffers at the end of the list, except those which are
819 ;; specifically to be ignored.
820 (if dabbrev-check-all-buffers
821 (setq list
822 (append list
823 (dabbrev-filter-elements
824 buffer (buffer-list)
825 (and (not (memq buffer list))
826 (not (dabbrev--ignore-buffer-p buffer)))))))
827 ;; Remove the current buffer.
828 (setq list (delq (current-buffer) list)))
829 ;; Move buffers in the list that are visible on the screen
830 ;; to the front of the list, but don't add anything to the list.
831 (if list
832 (walk-windows (lambda (w)
833 (unless (eq w (selected-window))
834 (if (memq (window-buffer w) list)
835 (setq list
836 (cons (window-buffer w)
837 (delq (window-buffer w)
838 list))))))))
839 ;; In a minibuffer, search the buffer it was activated from,
840 ;; first after the minibuffer itself. Unless we aren't supposed
841 ;; to search the current buffer either.
842 (if (and (window-minibuffer-p (selected-window))
843 (not dabbrev-search-these-buffers-only))
844 (setq list
845 (cons (dabbrev--minibuffer-origin)
846 (delq (dabbrev--minibuffer-origin) list))))
847 list))
849 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
850 (if (eq major-mode 'picture-mode)
851 (with-no-warnings
852 (picture-replace-match string fixedcase literal))
853 (replace-match string fixedcase literal)))
855 ;;;----------------------------------------------------------------
856 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
857 "Replace OLD with EXPANSION in the buffer.
858 OLD is text currently in the buffer, perhaps the abbreviation
859 or perhaps another expansion that was tried previously.
860 ABBREV is the abbreviation we are expanding.
861 It is \" \" if we are copying subsequent words.
862 EXPANSION is the expansion substring to be used this time.
863 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
864 to record whether we upcased the expansion, downcased it, or did neither."
865 ;;(undo-boundary)
866 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
867 case-fold-search
868 dabbrev-case-fold-search)
869 (or (not dabbrev-upcase-means-case-search)
870 (string= abbrev (downcase abbrev)))
871 (if (eq dabbrev-case-replace 'case-replace)
872 case-replace
873 dabbrev-case-replace))))
875 ;; If we upcased or downcased the original expansion,
876 ;; do likewise for the subsequent words when we copy them.
877 ;; Don't do any of the usual case processing, though.
878 (when (equal abbrev " ")
879 (if dabbrev--last-case-pattern
880 (setq expansion
881 (funcall dabbrev--last-case-pattern expansion)))
882 (setq use-case-replace nil))
884 ;; If the expansion has mixed case
885 ;; and it is not simply a capitalized word,
886 ;; or if the abbrev has mixed case,
887 ;; and if the given abbrev's case pattern
888 ;; matches the start of the expansion,
889 ;; copy the expansion's case
890 ;; instead of downcasing all the rest.
892 ;; Treat a one-capital-letter (possibly with preceding non-letter
893 ;; characters) abbrev as "not all upper case", so as to force
894 ;; preservation of the expansion's pattern if the expansion starts
895 ;; with a capital letter.
896 (let ((expansion-rest (substring expansion 1))
897 (first-letter-position (string-match "[[:alpha:]]" abbrev)))
898 (if (or (null first-letter-position)
899 (and (not (and (or (string= expansion-rest (downcase expansion-rest))
900 (string= expansion-rest (upcase expansion-rest)))
901 (or (string= abbrev (downcase abbrev))
902 (and (string= abbrev (upcase abbrev))
903 (> (- (length abbrev) first-letter-position)
904 1)))))
905 (string= abbrev
906 (substring expansion 0 (length abbrev)))))
907 (setq use-case-replace nil)))
909 ;; If the abbrev and the expansion are both all-lower-case
910 ;; then don't do any conversion. The conversion would be a no-op
911 ;; for this replacement, but it would carry forward to subsequent words.
912 ;; The goal of this is to prevent that carrying forward.
913 (if (and (string= expansion (downcase expansion))
914 (string= abbrev (downcase abbrev)))
915 (setq use-case-replace nil))
917 (if use-case-replace
918 (setq expansion (downcase expansion)))
920 ;; In case we insert subsequent words,
921 ;; record if we upcased or downcased the first word,
922 ;; in order to do likewise for subsequent words.
923 (and record-case-pattern
924 (setq dabbrev--last-case-pattern
925 (and use-case-replace
926 (cond ((equal abbrev (upcase abbrev)) 'upcase)
927 ((equal abbrev (downcase abbrev)) 'downcase)))))
929 ;; Convert whitespace to single spaces.
930 (if dabbrev--eliminate-newlines
931 ;; Start searching at end of ABBREV so that any whitespace
932 ;; carried over from the existing text is not changed.
933 (let ((pos (length abbrev)))
934 (while (string-match "[\n \t]+" expansion pos)
935 (setq pos (1+ (match-beginning 0)))
936 (setq expansion (replace-match " " nil nil expansion)))))
938 (if old
939 (save-excursion
940 (search-backward old))
941 ;;(set-match-data (list (point-marker) (point-marker)))
942 (search-backward abbrev)
943 (search-forward abbrev))
945 ;; Make case of replacement conform to case of abbreviation
946 ;; provided (1) that kind of thing is enabled in this buffer
947 ;; and (2) the replacement itself is all lower case.
948 (dabbrev--safe-replace-match expansion
949 (not use-case-replace)
950 t)))
953 ;;;----------------------------------------------------------------
954 ;;; Search function used by dabbrevs library.
957 (defun dabbrev--search (abbrev reverse ignore-case)
958 "Search for something that could be used to expand ABBREV.
960 Second arg, REVERSE, is t for reverse search, nil for forward.
961 The variable `dabbrev-limit' controls the maximum search region size.
962 Third argument IGNORE-CASE non-nil means treat case as insignificant while
963 looking for a match and when comparing with previous matches. Also if
964 that's non-nil and the match is found at the beginning of a sentence
965 and is in lower case except for the initial then it is converted to
966 all lower case for return.
968 Table of expansions already seen is examined in buffer
969 `dabbrev--last-table' so that only distinct possibilities are found
970 by dabbrev-re-expand.
972 Returns the expansion found, or nil if not found.
973 Leaves point at the location of the start of the expansion."
974 (save-match-data
975 (let ((pattern1 (concat (regexp-quote abbrev)
976 "\\(" dabbrev--abbrev-char-regexp "\\)"))
977 (pattern2 (concat (regexp-quote abbrev)
978 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
979 ;; This makes it possible to find matches in minibuffer prompts
980 ;; even when they are "inviolable".
981 (inhibit-point-motion-hooks t)
982 found-string result)
983 ;; Limited search.
984 (save-restriction
985 (and dabbrev-limit
986 (narrow-to-region dabbrev--last-expansion-location
987 (+ (point)
988 (if reverse (- dabbrev-limit) dabbrev-limit))))
989 ;;--------------------------------
990 ;; Look for a distinct expansion, using dabbrev--last-table.
991 ;;--------------------------------
992 (while (and (not found-string)
993 (if reverse
994 (re-search-backward pattern1 nil t)
995 (re-search-forward pattern1 nil t)))
996 (goto-char (match-beginning 0))
997 ;; In case we matched in the middle of a word,
998 ;; back up to start of word and verify we still match.
999 (dabbrev--goto-start-of-abbrev)
1001 (if (not (looking-at pattern1))
1003 ;; We have a truly valid match. Find the end.
1004 (re-search-forward pattern2)
1005 (setq found-string (match-string-no-properties 0))
1006 (setq result found-string)
1007 (and ignore-case (setq found-string (downcase found-string)))
1008 ;; Ignore this match if it's already in the table.
1009 (if (dabbrev-filter-elements
1010 table-string dabbrev--last-table
1011 (string= found-string table-string))
1012 (setq found-string nil)))
1013 ;; Prepare to continue searching.
1014 (goto-char (if reverse (match-beginning 0) (match-end 0))))
1015 ;; If we found something, use it.
1016 (when found-string
1017 ;; Put it into `dabbrev--last-table'
1018 ;; and return it (either downcased, or as is).
1019 (setq dabbrev--last-table
1020 (cons found-string dabbrev--last-table))
1021 result)))))
1023 (dolist (mess '("^No dynamic expansion for .* found$"
1024 "^No further dynamic expansion for .* found$"
1025 "^No possible abbreviation preceding point$"))
1026 (add-to-list 'debug-ignored-errors mess))
1028 (provide 'dabbrev)
1030 ;;; arch-tag: 29e58596-f080-4306-a409-70296cf9d46f
1031 ;;; dabbrev.el ends here