Replace uses of in-string-p; make it obsolete
[emacs.git] / lisp / dabbrev.el
blob50c52071b08ecd7f089083756eecfd35192cb69e
1 ;;; dabbrev.el --- dynamic abbreviation package -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1992, 1994, 1996-1997, 2000-2015 Free
4 ;; Software Foundation, Inc.
6 ;; Author: Don Morrison
7 ;; Lars Lindberg
8 ;; (according to ack.texi)
9 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
10 ;; Created: 16 Mars 1992
11 ;; Lindberg's last update version: 5.7
12 ;; Keywords: abbrev expand completion convenience
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 ;; (with-current-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
70 ;; minibuffer.
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@ql.org>
92 ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
93 ;; ... and to all the people who have participated in the beta tests.
95 ;;; Code:
97 ;;----------------------------------------------------------------
98 ;; Customization variables
99 ;;----------------------------------------------------------------
101 (defgroup dabbrev nil
102 "Dynamic Abbreviations."
103 :tag "Dynamic Abbreviations"
104 :group 'abbrev
105 :group 'convenience)
107 (defcustom dabbrev-backward-only nil
108 "If non-nil, `dabbrev-expand' only looks backwards."
109 :type 'boolean
110 :group 'dabbrev)
112 (defcustom dabbrev-limit nil
113 "Limits region searched by `dabbrev-expand' to this many chars away."
114 :type '(choice (const :tag "off" nil)
115 integer)
116 :group 'dabbrev)
118 (defcustom dabbrev-abbrev-skip-leading-regexp nil
119 "Regexp for skipping leading characters of an abbreviation.
121 Example: Set this to \"\\\\$\" for programming languages
122 in which variable names may appear with or without a leading `$'.
123 \(For example, in Makefiles.\)
125 Set this to nil if no characters should be skipped."
126 :type '(choice regexp
127 (const :tag "off" nil))
128 :group 'dabbrev)
130 (defcustom dabbrev-eliminate-newlines t
131 "Non-nil means dabbrev should not insert newlines.
132 Instead it converts them to spaces."
133 :type 'boolean
134 :group 'dabbrev)
136 (defcustom dabbrev-case-fold-search 'case-fold-search
137 "Control whether dabbrev searches should ignore case.
138 A value of nil means case is significant.
139 A value of `case-fold-search' means case is significant
140 if `case-fold-search' is nil.
141 Any other non-nil version means case is not significant."
142 :type '(choice (const :tag "off" nil)
143 (const :tag "like search" case-fold-search)
144 (other :tag "on" t))
145 :group 'dabbrev)
146 ;;;###autoload(put 'dabbrev-case-fold-search 'risky-local-variable t)
148 (defcustom dabbrev-upcase-means-case-search nil
149 "The significance of an uppercase character in an abbreviation.
150 A nil value 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 "22.1")
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)
187 ;;;###autoload(put 'dabbrev-case-replace 'risky-local-variable t)
189 (defcustom dabbrev-abbrev-char-regexp nil
190 "Regexp to recognize a character in an abbreviation or expansion.
191 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
193 Set this variable to \"\\\\sw\" if you want ordinary words or
194 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
195 syntax is \"symbol\" as well as those whose syntax is \"word\".
197 The value nil has a special meaning: the abbreviation is from point to
198 previous word-start, but the search is for symbols.
200 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
201 while `yes', `or', `no' and `p' are considered words. If this
202 variable is nil, then expanding `yes-or-no-' looks for a symbol
203 starting with or containing `no-'. If you set this variable to
204 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
205 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
206 expanding `yes-or-no-' signals an error because `-' is not part of a word;
207 but expanding `yes-or-no' looks for a word starting with `no'.
209 The recommended value is nil, which will make dabbrev default to
210 using \"\\\\sw\\\\|\\\\s_\"."
211 :type '(choice (const nil)
212 regexp)
213 :group 'dabbrev)
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'."
225 :type 'boolean
226 :group 'dabbrev)
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"))
232 :group 'dabbrev
233 :version "20.3")
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)
239 :group 'dabbrev
240 :version "21.1")
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
250 buffers too.
252 The default value is t."
253 :type '(choice (const :tag "off" nil)
254 (const :tag "on" t)
255 (other :tag "ask" other))
256 :group 'dabbrev)
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'
265 for an example.
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."
280 :type 'function
281 :group 'dabbrev)
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
287 this list."
288 :type '(choice (const nil) (repeat :tag "List of buffers" string))
289 :group 'dabbrev)
291 ;;----------------------------------------------------------------
292 ;; Internal variables
293 ;;----------------------------------------------------------------
295 ;; Table of expansions seen so far
296 (defvar dabbrev--last-table nil)
298 ;; Last string we tried to expand.
299 (defvar dabbrev--last-abbreviation nil)
301 ;; Location last abbreviation began
302 (defvar dabbrev--last-abbrev-location nil)
304 ;; Direction of last dabbrevs search
305 (defvar dabbrev--last-direction 0)
307 ;; Last expansion of an abbreviation.
308 (defvar dabbrev--last-expansion nil)
310 ;; Location the last expansion was found.
311 (defvar dabbrev--last-expansion-location nil)
313 ;; The list of remaining buffers with the same mode as current buffer.
314 (defvar dabbrev--friend-buffer-list nil)
316 ;; The buffer we looked in last, not counting the current buffer.
317 (defvar dabbrev--last-buffer nil)
319 ;; The buffer we found the expansion last time.
320 (defvar dabbrev--last-buffer-found nil)
322 ;; If non-nil, a function to use when copying successive words.
323 ;; It should be `upcase' or `downcase'.
324 (defvar dabbrev--last-case-pattern nil)
326 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
327 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
329 ;; The regexp for recognizing a character in an abbreviation.
330 (defvar dabbrev--abbrev-char-regexp nil)
332 ;; The progress reporter for buffer-scanning progress.
333 (defvar dabbrev--progress-reporter nil)
335 ;;----------------------------------------------------------------
336 ;; Macros
337 ;;----------------------------------------------------------------
339 (defsubst dabbrev--minibuffer-origin ()
340 "Get the buffer from which mini-buffer."
341 (window-buffer (minibuffer-selected-window)))
343 ;; Make a list of some of the elements of LIST.
344 ;; Check each element of LIST, storing it temporarily in the
345 ;; variable ELEMENT, and include it in the result
346 ;; if CONDITION evaluates non-nil.
347 (defmacro dabbrev-filter-elements (element list condition)
348 `(let (dabbrev-result dabbrev-tail ,element)
349 (setq dabbrev-tail ,list)
350 (while dabbrev-tail
351 (setq ,element (car dabbrev-tail))
352 (if ,condition
353 (setq dabbrev-result (cons ,element dabbrev-result)))
354 (setq dabbrev-tail (cdr dabbrev-tail)))
355 (nreverse dabbrev-result)))
357 ;;----------------------------------------------------------------
358 ;; Exported functions
359 ;;----------------------------------------------------------------
361 ;;;###autoload (define-key esc-map "/" 'dabbrev-expand)
362 ;;??? Do we want this?
363 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion)
365 (defun dabbrev--ignore-case-p (abbrev)
366 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
367 case-fold-search
368 dabbrev-case-fold-search)
369 (or (not dabbrev-upcase-means-case-search)
370 (string= abbrev (downcase abbrev)))))
372 ;;;###autoload
373 (defun dabbrev-completion (&optional arg)
374 "Completion on current word.
375 Like \\[dabbrev-expand] but finds all expansions in the current buffer
376 and presents suggestions for completion.
378 With a prefix argument ARG, it searches all buffers accepted by the
379 function pointed out by `dabbrev-friend-buffer-function' to find the
380 completions.
382 If the prefix argument is 16 (which comes from \\[universal-argument] \\[universal-argument]),
383 then it searches *all* buffers."
384 (interactive "*P")
385 (dabbrev--reset-global-variables)
386 (let* ((dabbrev-check-other-buffers (and arg t))
387 (dabbrev-check-all-buffers
388 (and arg (= (prefix-numeric-value arg) 16)))
389 (abbrev (dabbrev--abbrev-at-point))
390 (beg (progn (search-backward abbrev) (point)))
391 (end (progn (search-forward abbrev) (point)))
392 (ignore-case-p (dabbrev--ignore-case-p abbrev))
393 (list 'uninitialized)
394 (table
395 (lambda (s p a)
396 (if (eq a 'metadata)
397 `(metadata (cycle-sort-function . ,#'identity)
398 (category . dabbrev))
399 (when (eq list 'uninitialized)
400 (save-excursion
401 ;;--------------------------------
402 ;; New abbreviation to expand.
403 ;;--------------------------------
404 (setq dabbrev--last-abbreviation abbrev)
405 ;; Find all expansion
406 (let ((completion-list
407 (dabbrev--find-all-expansions abbrev ignore-case-p))
408 (completion-ignore-case ignore-case-p))
409 (or (consp completion-list)
410 (user-error "No dynamic expansion for \"%s\" found%s"
411 abbrev
412 (if dabbrev--check-other-buffers
413 "" " in this-buffer")))
414 (setq list
415 (cond
416 ((not (and ignore-case-p dabbrev-case-replace))
417 completion-list)
418 ((string= abbrev (upcase abbrev))
419 (mapcar #'upcase completion-list))
420 ((string= (substring abbrev 0 1)
421 (upcase (substring abbrev 0 1)))
422 (mapcar #'capitalize completion-list))
424 (mapcar #'downcase completion-list)))))))
425 (complete-with-action a list s p)))))
426 (completion-in-region beg end table)))
428 ;;;###autoload
429 (defun dabbrev-expand (arg)
430 "Expand previous word \"dynamically\".
432 Expands to the most recent, preceding word for which this is a prefix.
433 If no suitable preceding word is found, words following point are
434 considered. If still no suitable word is found, then look in the
435 buffers accepted by the function pointed out by variable
436 `dabbrev-friend-buffer-function'.
438 A positive prefix argument, N, says to take the Nth backward *distinct*
439 possibility. A negative argument says search forward.
441 If the cursor has not moved from the end of the previous expansion and
442 no argument is given, replace the previously-made expansion
443 with the next possible expansion not yet tried.
445 The variable `dabbrev-backward-only' may be used to limit the
446 direction of search to backward if set non-nil.
448 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
449 (interactive "*P")
450 (let (abbrev record-case-pattern
451 expansion old direction (orig-point (point)))
452 ;; abbrev -- the abbrev to expand
453 ;; expansion -- the expansion found (eventually) or nil until then
454 ;; old -- the text currently in the buffer
455 ;; (the abbrev, or the previously-made expansion)
456 (save-excursion
457 (if (and (null arg)
458 (markerp dabbrev--last-abbrev-location)
459 (marker-position dabbrev--last-abbrev-location)
460 (or (eq last-command this-command)
461 (and (window-minibuffer-p)
462 (= dabbrev--last-abbrev-location
463 (point)))))
464 ;; Find a different expansion for the same abbrev as last time.
465 (progn
466 (setq abbrev dabbrev--last-abbreviation)
467 (setq old dabbrev--last-expansion)
468 (setq direction dabbrev--last-direction))
469 ;; If the user inserts a space after expanding
470 ;; and then asks to expand again, always fetch the next word.
471 (if (and (eq (preceding-char) ?\s)
472 (markerp dabbrev--last-abbrev-location)
473 (marker-position dabbrev--last-abbrev-location)
474 (= (point) (1+ dabbrev--last-abbrev-location)))
475 (progn
476 ;; The "abbrev" to expand is just the space.
477 (setq abbrev " ")
478 (save-excursion
479 (save-restriction
480 (widen)
481 (if dabbrev--last-buffer
482 (set-buffer dabbrev--last-buffer))
483 ;; Find the end of the last "expansion" word.
484 (if (or (eq dabbrev--last-direction 1)
485 (and (eq dabbrev--last-direction 0)
486 (< dabbrev--last-expansion-location (point))))
487 (setq dabbrev--last-expansion-location
488 (+ dabbrev--last-expansion-location
489 (length dabbrev--last-expansion))))
490 (goto-char dabbrev--last-expansion-location)
491 ;; Take the following word, with intermediate separators,
492 ;; as our expansion this time.
493 (re-search-forward
494 (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
495 (setq expansion (buffer-substring-no-properties
496 dabbrev--last-expansion-location (point)))
498 ;; Record the end of this expansion, in case we repeat this.
499 (setq dabbrev--last-expansion-location (point))))
500 ;; Indicate that dabbrev--last-expansion-location is
501 ;; at the end of the expansion.
502 (setq dabbrev--last-direction -1))
504 ;; We have a different abbrev to expand.
505 (dabbrev--reset-global-variables)
506 (setq direction (if (null arg)
507 (if dabbrev-backward-only 1 0)
508 (prefix-numeric-value arg)))
509 (setq abbrev (dabbrev--abbrev-at-point))
510 (setq record-case-pattern t)
511 (setq old nil)))
513 ;;--------------------------------
514 ;; Find the expansion
515 ;;--------------------------------
516 (or expansion
517 (setq expansion
518 (dabbrev--find-expansion
519 abbrev direction
520 (dabbrev--ignore-case-p abbrev)))))
521 (cond
522 ((not expansion)
523 (dabbrev--reset-global-variables)
524 (if old
525 (save-excursion
526 (setq buffer-undo-list (cons orig-point buffer-undo-list))
527 ;; Put back the original abbrev with its original case pattern.
528 (search-backward old)
529 (insert abbrev)
530 (delete-region (point) (+ (point) (length old)))))
531 (user-error "No%s dynamic expansion for `%s' found"
532 (if old " further" "") abbrev))
534 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
535 (minibuffer-window-active-p (selected-window))))
536 (progn
537 (message "Expansion found in '%s'"
538 (buffer-name dabbrev--last-buffer))
539 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
540 (message nil))
541 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
542 (null dabbrev--last-buffer))
543 (numberp dabbrev--last-expansion-location)
544 (and (> dabbrev--last-expansion-location (point))))
545 (setq dabbrev--last-expansion-location
546 (copy-marker dabbrev--last-expansion-location)))
547 ;; Success: stick it in and return.
548 (setq buffer-undo-list (cons orig-point buffer-undo-list))
549 (dabbrev--substitute-expansion old abbrev expansion
550 record-case-pattern)
552 ;; Save state for re-expand.
553 (setq dabbrev--last-expansion expansion)
554 (setq dabbrev--last-abbreviation abbrev)
555 (setq dabbrev--last-abbrev-location (point-marker))))))
557 ;;----------------------------------------------------------------
558 ;; Local functions
559 ;;----------------------------------------------------------------
561 (defun dabbrev--same-major-mode-p (other-buffer)
562 "Check if OTHER-BUFFER has the same major mode as current buffer."
563 (eq major-mode
564 (with-current-buffer other-buffer
565 major-mode)))
567 (defun dabbrev--goto-start-of-abbrev ()
568 "Back over all abbrev type characters and then moves forward over
569 all skip characters."
570 ;; Move backwards over abbrev chars
571 (save-match-data
572 (when (> (point) (minibuffer-prompt-end))
573 (forward-char -1)
574 (while (and (looking-at dabbrev--abbrev-char-regexp)
575 (> (point) (minibuffer-prompt-end))
576 (not (= (point) (field-beginning (point) nil
577 (1- (point))))))
578 (forward-char -1))
579 (or (looking-at dabbrev--abbrev-char-regexp)
580 (forward-char 1)))
581 (and dabbrev-abbrev-skip-leading-regexp
582 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
583 (forward-char 1)))))
585 (defun dabbrev--abbrev-at-point ()
586 "Extract the symbol at point to serve as abbreviation."
587 ;; Check for error
588 (if (bobp)
589 (user-error "No possible abbreviation preceding point"))
590 ;; Return abbrev at point
591 (save-excursion
592 ;; Record the end of the abbreviation.
593 (setq dabbrev--last-abbrev-location (point))
594 ;; If we aren't right after an abbreviation,
595 ;; move point back to just after one.
596 ;; This is so the user can get successive words
597 ;; by typing the punctuation followed by M-/.
598 (save-match-data
599 (if (save-excursion
600 (forward-char -1)
601 (not (looking-at (or dabbrev-abbrev-char-regexp
602 "\\sw\\|\\s_"))))
603 (if (re-search-backward (or dabbrev-abbrev-char-regexp
604 "\\sw\\|\\s_")
605 nil t)
606 (forward-char 1)
607 (user-error "No possible abbreviation preceding point"))))
608 ;; Now find the beginning of that one.
609 (dabbrev--goto-start-of-abbrev)
610 (buffer-substring-no-properties
611 dabbrev--last-abbrev-location (point))))
613 (defun dabbrev--reset-global-variables ()
614 "Initialize all global variables."
615 (setq dabbrev--last-table nil
616 dabbrev--last-abbreviation nil
617 dabbrev--last-abbrev-location nil
618 dabbrev--last-direction nil
619 dabbrev--last-expansion nil
620 dabbrev--last-expansion-location nil
621 dabbrev--friend-buffer-list nil
622 dabbrev--last-buffer nil
623 dabbrev--last-buffer-found nil
624 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
625 "\\sw\\|\\s_")
626 dabbrev--check-other-buffers dabbrev-check-other-buffers))
628 (defun dabbrev--select-buffers ()
629 "Return a list of other buffers to search for a possible abbrev.
630 The current buffer is not included in the list.
632 This function makes a list of all the buffers returned by `buffer-list',
633 then discards buffers whose names match `dabbrev-ignored-buffer-names'
634 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
635 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
636 with the buffer as argument.
637 It returns the list of the buffers that are not discarded."
638 (dabbrev-filter-elements
639 buffer (buffer-list)
640 (and (not (eq (current-buffer) buffer))
641 (not (dabbrev--ignore-buffer-p buffer))
642 (boundp 'dabbrev-friend-buffer-function)
643 (funcall dabbrev-friend-buffer-function buffer))))
645 (defun dabbrev--try-find (abbrev reverse n ignore-case)
646 "Search for ABBREV, backwards if REVERSE, N times.
647 If IGNORE-CASE is non-nil, ignore case while searching.
648 Return the expansion found, and save the location of the start
649 of the expansion in `dabbrev--last-expansion-location'."
650 (save-excursion
651 (save-restriction
652 (widen)
653 (let ((expansion nil))
654 (and dabbrev--last-expansion-location
655 (goto-char dabbrev--last-expansion-location))
656 (let ((case-fold-search ignore-case)
657 (count n))
658 (while (and (> count 0)
659 (setq expansion (dabbrev--search
660 abbrev reverse
661 (and ignore-case
662 (if (eq dabbrev-case-distinction
663 'case-replace)
664 case-replace
665 dabbrev-case-distinction)))))
666 (setq count (1- count))))
667 (and expansion
668 (setq dabbrev--last-expansion-location (point)))
669 expansion))))
671 (defun dabbrev--find-all-expansions (abbrev ignore-case)
672 "Return a list of all possible expansions of ABBREV.
673 If IGNORE-CASE is non-nil, accept matches which differ in case."
674 (let ((all-expansions nil)
675 expansion)
676 (save-excursion
677 (goto-char (point-min))
678 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
679 (setq all-expansions (cons expansion all-expansions))))
680 all-expansions))
682 (defun dabbrev--ignore-buffer-p (buffer)
683 "Return non-nil if BUFFER should be ignored by dabbrev."
684 (let ((bn (buffer-name buffer)))
685 (or (member bn dabbrev-ignored-buffer-names)
686 (let ((tail dabbrev-ignored-buffer-regexps)
687 (match nil))
688 (while (and tail (not match))
689 (setq match (string-match (car tail) bn)
690 tail (cdr tail)))
691 match))))
693 (defun dabbrev--find-expansion (abbrev direction ignore-case)
694 "Find one occurrence of ABBREV, and return the expansion.
695 DIRECTION > 0 means look that many times backwards.
696 DIRECTION < 0 means look that many times forward.
697 DIRECTION = 0 means try both backward and forward.
698 IGNORE-CASE non-nil means ignore case when searching.
699 This sets `dabbrev--last-direction' to 1 or -1 according
700 to the direction in which the occurrence was actually found.
701 It sets `dabbrev--last-expansion-location' to the location
702 of the start of the occurrence."
703 (save-excursion
704 ;; If we were scanning something other than the current buffer,
705 ;; continue scanning there.
706 (when dabbrev--last-buffer
707 (set-buffer dabbrev--last-buffer))
709 ;; ------------------------------------------
710 ;; Look backward in current buffer.
711 ;; ------------------------------------------
712 (and (not dabbrev-search-these-buffers-only)
713 (>= direction 0)
714 (setq dabbrev--last-direction (min 1 direction))
715 (dabbrev--try-find abbrev t
716 (max 1 direction)
717 ignore-case))
718 ;; ------------------------------------------
719 ;; Look forward in current buffer
720 ;; or whatever buffer we were last scanning.
721 ;; ------------------------------------------
722 (and (or (not dabbrev-search-these-buffers-only)
723 dabbrev--last-buffer)
724 (<= direction 0)
725 (setq dabbrev--last-direction -1)
726 (dabbrev--try-find abbrev nil
727 (max 1 (- direction))
728 ignore-case))
729 ;; ------------------------------------------
730 ;; Look in other buffers.
731 ;; Always start at (point-min) and look forward.
732 ;; ------------------------------------------
733 (progn
734 (setq dabbrev--last-direction -1)
735 (unless dabbrev--last-buffer
736 ;; If we have just now begun to search other buffers,
737 ;; determine which other buffers we should check.
738 ;; Put that list in dabbrev--friend-buffer-list.
739 (unless dabbrev--friend-buffer-list
740 (setq dabbrev--friend-buffer-list
741 (dabbrev--make-friend-buffer-list))
742 (setq dabbrev--progress-reporter
743 (make-progress-reporter
744 "Scanning for dabbrevs..."
745 (- (length dabbrev--friend-buffer-list)) 0 0 1 1.5))))
746 ;; Walk through the buffers till we find a match.
747 (let (expansion)
748 (while (and (not expansion) dabbrev--friend-buffer-list)
749 (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
750 (set-buffer dabbrev--last-buffer)
751 (progress-reporter-update dabbrev--progress-reporter
752 (- (length dabbrev--friend-buffer-list)))
753 (setq dabbrev--last-expansion-location (point-min))
754 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
755 (progress-reporter-done dabbrev--progress-reporter)
756 expansion)))))
758 ;; Compute the list of buffers to scan.
759 ;; If dabbrev-search-these-buffers-only, then the current buffer
760 ;; is included in this list if it should be searched.
761 ;; Otherwise, the current buffer is searched first specially.,
762 ;; and it is not included in this list.
763 (defun dabbrev--make-friend-buffer-list ()
764 (let ((list (mapcar (function get-buffer)
765 dabbrev-search-these-buffers-only)))
766 (when (and (null dabbrev-search-these-buffers-only)
767 dabbrev--check-other-buffers
768 (or (eq dabbrev--check-other-buffers t)
769 (setq dabbrev--check-other-buffers
770 (y-or-n-p "Scan other buffers also? "))))
771 (setq list (funcall dabbrev-select-buffers-function))
772 ;; If dabbrev-check-all-buffers, tack on all the other
773 ;; buffers at the end of the list, except those which are
774 ;; specifically to be ignored.
775 (if dabbrev-check-all-buffers
776 (setq list
777 (append list
778 (dabbrev-filter-elements
779 buffer (buffer-list)
780 (and (not (memq buffer list))
781 (not (dabbrev--ignore-buffer-p buffer)))))))
782 ;; Remove the current buffer.
783 (setq list (delq (current-buffer) list)))
784 ;; Move buffers in the list that are visible on the screen
785 ;; to the front of the list, but don't add anything to the list.
786 (if list
787 (walk-windows (lambda (w)
788 (unless (eq w (selected-window))
789 (if (memq (window-buffer w) list)
790 (setq list
791 (cons (window-buffer w)
792 (delq (window-buffer w)
793 list))))))))
794 ;; In a minibuffer, search the buffer it was activated from,
795 ;; first after the minibuffer itself. Unless we aren't supposed
796 ;; to search the current buffer either.
797 (if (and (window-minibuffer-p)
798 (not dabbrev-search-these-buffers-only))
799 (setq list
800 (cons (dabbrev--minibuffer-origin)
801 (delq (dabbrev--minibuffer-origin) list))))
802 list))
804 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
805 (if (eq major-mode 'picture-mode)
806 (with-no-warnings
807 (picture-replace-match string fixedcase literal))
808 (replace-match string fixedcase literal)))
810 ;;;----------------------------------------------------------------
811 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
812 "Replace OLD with EXPANSION in the buffer.
813 OLD is text currently in the buffer, perhaps the abbreviation
814 or perhaps another expansion that was tried previously.
815 ABBREV is the abbreviation we are expanding.
816 It is \" \" if we are copying subsequent words.
817 EXPANSION is the expansion substring to be used this time.
818 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
819 to record whether we upcased the expansion, downcased it, or did neither."
820 ;;(undo-boundary)
821 (let ((use-case-replace
822 (and (dabbrev--ignore-case-p abbrev)
823 (if (eq dabbrev-case-replace 'case-replace)
824 case-replace
825 dabbrev-case-replace))))
827 ;; If we upcased or downcased the original expansion,
828 ;; do likewise for the subsequent words when we copy them.
829 ;; Don't do any of the usual case processing, though.
830 (when (equal abbrev " ")
831 (if dabbrev--last-case-pattern
832 (setq expansion
833 (funcall dabbrev--last-case-pattern expansion)))
834 (setq use-case-replace nil))
836 ;; If the expansion has mixed case
837 ;; and it is not simply a capitalized word,
838 ;; or if the abbrev has mixed case,
839 ;; and if the given abbrev's case pattern
840 ;; matches the start of the expansion,
841 ;; copy the expansion's case
842 ;; instead of downcasing all the rest.
844 ;; Treat a one-capital-letter (possibly with preceding non-letter
845 ;; characters) abbrev as "not all upper case", so as to force
846 ;; preservation of the expansion's pattern if the expansion starts
847 ;; with a capital letter.
848 (let ((expansion-rest (substring expansion 1))
849 (first-letter-position (string-match "[[:alpha:]]" abbrev)))
850 (if (or (null first-letter-position)
851 (and (not
852 (and (or (string= expansion-rest (downcase expansion-rest))
853 (string= expansion-rest (upcase expansion-rest)))
854 (or (string= abbrev (downcase abbrev))
855 (and (string= abbrev (upcase abbrev))
856 (> (- (length abbrev) first-letter-position)
857 1)))))
858 (string= abbrev
859 (substring expansion 0 (length abbrev)))))
860 (setq use-case-replace nil)))
862 ;; If the abbrev and the expansion are both all-lower-case
863 ;; then don't do any conversion. The conversion would be a no-op
864 ;; for this replacement, but it would carry forward to subsequent words.
865 ;; The goal of this is to prevent that carrying forward.
866 (if (and (string= expansion (downcase expansion))
867 (string= abbrev (downcase abbrev)))
868 (setq use-case-replace nil))
870 (if use-case-replace
871 (setq expansion (downcase expansion)))
873 ;; In case we insert subsequent words,
874 ;; record if we upcased or downcased the first word,
875 ;; in order to do likewise for subsequent words.
876 (and record-case-pattern
877 (setq dabbrev--last-case-pattern
878 (and use-case-replace
879 (cond ((equal abbrev (upcase abbrev)) 'upcase)
880 ((equal abbrev (downcase abbrev)) 'downcase)))))
882 ;; Convert whitespace to single spaces.
883 (if dabbrev-eliminate-newlines
884 (let ((pos
885 (if (equal abbrev " ") 0 (length abbrev))))
886 ;; If ABBREV is real, search after the end of it.
887 ;; If ABBREV is space and we are copying successive words,
888 ;; search starting at the front.
889 (while (string-match "[\n \t]+" expansion pos)
890 (setq pos (1+ (match-beginning 0)))
891 (setq expansion (replace-match " " nil nil expansion)))))
893 (if old
894 (save-excursion
895 (search-backward old))
896 ;;(set-match-data (list (point-marker) (point-marker)))
897 (search-backward abbrev)
898 (search-forward abbrev))
900 ;; Make case of replacement conform to case of abbreviation
901 ;; provided (1) that kind of thing is enabled in this buffer
902 ;; and (2) the replacement itself is all lower case.
903 (dabbrev--safe-replace-match expansion
904 (not use-case-replace)
905 t)))
908 ;;;----------------------------------------------------------------
909 ;;; Search function used by dabbrevs library.
912 (defun dabbrev--search (abbrev reverse ignore-case)
913 "Search for something that could be used to expand ABBREV.
915 Second arg, REVERSE, is t for reverse search, nil for forward.
916 The variable `dabbrev-limit' controls the maximum search region size.
917 Third argument IGNORE-CASE non-nil means treat case as insignificant while
918 looking for a match and when comparing with previous matches. Also if
919 that's non-nil and the match is found at the beginning of a sentence
920 and is in lower case except for the initial then it is converted to
921 all lower case for return.
923 Table of expansions already seen is examined in buffer
924 `dabbrev--last-table' so that only distinct possibilities are found
925 by dabbrev-re-expand.
927 Returns the expansion found, or nil if not found.
928 Leaves point at the location of the start of the expansion."
929 (save-match-data
930 (let ((pattern1 (concat (regexp-quote abbrev)
931 "\\(" dabbrev--abbrev-char-regexp "\\)"))
932 (pattern2 (concat (regexp-quote abbrev)
933 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
934 ;; This makes it possible to find matches in minibuffer prompts
935 ;; even when they are "inviolable".
936 (inhibit-point-motion-hooks t)
937 found-string result)
938 ;; Limited search.
939 (save-restriction
940 (and dabbrev-limit
941 (narrow-to-region
942 dabbrev--last-expansion-location
943 (+ (point) (if reverse (- dabbrev-limit) dabbrev-limit))))
944 ;;--------------------------------
945 ;; Look for a distinct expansion, using dabbrev--last-table.
946 ;;--------------------------------
947 (while (and (not found-string)
948 (if reverse
949 (re-search-backward pattern1 nil t)
950 (re-search-forward pattern1 nil t)))
951 (goto-char (match-beginning 0))
952 ;; In case we matched in the middle of a word,
953 ;; back up to start of word and verify we still match.
954 (dabbrev--goto-start-of-abbrev)
956 (if (not (looking-at pattern1))
958 ;; We have a truly valid match. Find the end.
959 (re-search-forward pattern2)
960 (setq found-string (match-string-no-properties 0))
961 (setq result found-string)
962 (and ignore-case (setq found-string (downcase found-string)))
963 ;; Ignore this match if it's already in the table.
964 (if (dabbrev-filter-elements
965 table-string dabbrev--last-table
966 (string= found-string table-string))
967 (setq found-string nil)))
968 ;; Prepare to continue searching.
969 (goto-char (if reverse (match-beginning 0) (match-end 0))))
970 ;; If we found something, use it.
971 (when found-string
972 ;; Put it into `dabbrev--last-table'
973 ;; and return it (either downcased, or as is).
974 (setq dabbrev--last-table
975 (cons found-string dabbrev--last-table))
976 result)))))
978 (provide 'dabbrev)
980 ;;; dabbrev.el ends here