Protect against an infloop in python-mode
[emacs.git] / lisp / dabbrev.el
blob3550d75c46a43cce07a4e05de3592fd52646b5ee
1 ;;; dabbrev.el --- dynamic abbreviation package -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1992, 1994, 1996-1997, 2000-2017 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', if `dabbrev-check-other-buffers'
437 says so. Then, if `dabbrev-check-all-buffers' is non-nil, look in
438 all the other buffers, subject to constraints specified
439 by `dabbrev-ignored-buffer-names' and `dabbrev-ignored-regexps'.
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]."
452 (interactive "*P")
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)
459 (save-excursion
460 (if (and (null arg)
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)
465 (= dabbrev--last-abbrev-location
466 (point)))))
467 ;; Find a different expansion for the same abbrev as last time.
468 (progn
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) ?\s)
475 (markerp dabbrev--last-abbrev-location)
476 (marker-position dabbrev--last-abbrev-location)
477 (= (point) (1+ dabbrev--last-abbrev-location)))
478 (progn
479 ;; The "abbrev" to expand is just the space.
480 (setq abbrev " ")
481 (save-excursion
482 (save-restriction
483 (widen)
484 (if dabbrev--last-buffer
485 (set-buffer dabbrev--last-buffer))
486 ;; Find the end of the last "expansion" word.
487 (if (or (eq dabbrev--last-direction 1)
488 (and (eq dabbrev--last-direction 0)
489 (< dabbrev--last-expansion-location (point))))
490 (setq dabbrev--last-expansion-location
491 (+ dabbrev--last-expansion-location
492 (length dabbrev--last-expansion))))
493 (goto-char dabbrev--last-expansion-location)
494 ;; Take the following word, with intermediate separators,
495 ;; as our expansion this time.
496 (re-search-forward
497 (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
498 (setq expansion (buffer-substring-no-properties
499 dabbrev--last-expansion-location (point)))
501 ;; Record the end of this expansion, in case we repeat this.
502 (setq dabbrev--last-expansion-location (point))))
503 ;; Indicate that dabbrev--last-expansion-location is
504 ;; at the end of the expansion.
505 (setq dabbrev--last-direction -1))
507 ;; We have a different abbrev to expand.
508 (dabbrev--reset-global-variables)
509 (setq direction (if (null arg)
510 (if dabbrev-backward-only 1 0)
511 (prefix-numeric-value arg)))
512 (setq abbrev (dabbrev--abbrev-at-point))
513 (setq record-case-pattern t)
514 (setq old nil)))
516 ;;--------------------------------
517 ;; Find the expansion
518 ;;--------------------------------
519 (or expansion
520 (setq expansion
521 (dabbrev--find-expansion
522 abbrev direction
523 (dabbrev--ignore-case-p abbrev)))))
524 (cond
525 ((not expansion)
526 (dabbrev--reset-global-variables)
527 (if old
528 (save-excursion
529 (setq buffer-undo-list (cons orig-point buffer-undo-list))
530 ;; Put back the original abbrev with its original case pattern.
531 (search-backward old)
532 (insert abbrev)
533 (delete-region (point) (+ (point) (length old)))))
534 (user-error "No%s dynamic expansion for `%s' found"
535 (if old " further" "") abbrev))
537 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
538 (minibuffer-window-active-p (selected-window))))
539 (progn
540 (message "Expansion found in `%s'"
541 (buffer-name dabbrev--last-buffer))
542 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
543 (message nil))
544 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
545 (null dabbrev--last-buffer))
546 (numberp dabbrev--last-expansion-location)
547 (and (> dabbrev--last-expansion-location (point))))
548 (setq dabbrev--last-expansion-location
549 (copy-marker dabbrev--last-expansion-location)))
550 ;; Success: stick it in and return.
551 (setq buffer-undo-list (cons orig-point buffer-undo-list))
552 (setq expansion (dabbrev--substitute-expansion old abbrev expansion
553 record-case-pattern))
555 ;; Save state for re-expand.
556 (setq dabbrev--last-expansion expansion)
557 (setq dabbrev--last-abbreviation abbrev)
558 (setq dabbrev--last-abbrev-location (point-marker))))))
560 ;;----------------------------------------------------------------
561 ;; Local functions
562 ;;----------------------------------------------------------------
564 (defun dabbrev--same-major-mode-p (other-buffer)
565 "Check if OTHER-BUFFER has the same major mode as current buffer."
566 (eq major-mode
567 (with-current-buffer other-buffer
568 major-mode)))
570 (defun dabbrev--goto-start-of-abbrev ()
571 "Back over all abbrev type characters and then moves forward over
572 all skip characters."
573 ;; Move backwards over abbrev chars
574 (save-match-data
575 (when (> (point) (minibuffer-prompt-end))
576 (forward-char -1)
577 (while (and (looking-at dabbrev--abbrev-char-regexp)
578 (> (point) (minibuffer-prompt-end))
579 (not (= (point) (field-beginning (point) nil
580 (1- (point))))))
581 (forward-char -1))
582 (or (looking-at dabbrev--abbrev-char-regexp)
583 (forward-char 1)))
584 (and dabbrev-abbrev-skip-leading-regexp
585 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
586 (forward-char 1)))))
588 (defun dabbrev--abbrev-at-point ()
589 "Extract the symbol at point to serve as abbreviation."
590 ;; Check for error
591 (if (bobp)
592 (user-error "No possible abbreviation preceding point"))
593 ;; Return abbrev at point
594 (save-excursion
595 ;; Record the end of the abbreviation.
596 (setq dabbrev--last-abbrev-location (point))
597 ;; If we aren't right after an abbreviation,
598 ;; move point back to just after one.
599 ;; This is so the user can get successive words
600 ;; by typing the punctuation followed by M-/.
601 (save-match-data
602 (if (save-excursion
603 (forward-char -1)
604 (not (looking-at (or dabbrev-abbrev-char-regexp
605 "\\sw\\|\\s_"))))
606 (if (re-search-backward (or dabbrev-abbrev-char-regexp
607 "\\sw\\|\\s_")
608 nil t)
609 (forward-char 1)
610 (user-error "No possible abbreviation preceding point"))))
611 ;; Now find the beginning of that one.
612 (dabbrev--goto-start-of-abbrev)
613 (buffer-substring-no-properties
614 dabbrev--last-abbrev-location (point))))
616 (defun dabbrev--reset-global-variables ()
617 "Initialize all global variables."
618 (setq dabbrev--last-table nil
619 dabbrev--last-abbreviation nil
620 dabbrev--last-abbrev-location nil
621 dabbrev--last-direction nil
622 dabbrev--last-expansion nil
623 dabbrev--last-expansion-location nil
624 dabbrev--friend-buffer-list nil
625 dabbrev--last-buffer nil
626 dabbrev--last-buffer-found nil
627 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
628 "\\sw\\|\\s_")
629 dabbrev--check-other-buffers dabbrev-check-other-buffers))
631 (defun dabbrev--select-buffers ()
632 "Return a list of other buffers to search for a possible abbrev.
633 The current buffer is not included in the list.
635 This function makes a list of all the buffers returned by `buffer-list',
636 then discards buffers whose names match `dabbrev-ignored-buffer-names'
637 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
638 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
639 with the buffer as argument.
640 It returns the list of the buffers that are not discarded."
641 (dabbrev-filter-elements
642 buffer (buffer-list)
643 (and (not (eq (current-buffer) buffer))
644 (not (dabbrev--ignore-buffer-p buffer))
645 (boundp 'dabbrev-friend-buffer-function)
646 (funcall dabbrev-friend-buffer-function buffer))))
648 (defun dabbrev--try-find (abbrev reverse n ignore-case)
649 "Search for ABBREV, backwards if REVERSE, N times.
650 If IGNORE-CASE is non-nil, ignore case while searching.
651 Return the expansion found, and save the location of the start
652 of the expansion in `dabbrev--last-expansion-location'."
653 (save-excursion
654 (save-restriction
655 (widen)
656 (let ((expansion nil))
657 (and dabbrev--last-expansion-location
658 (goto-char dabbrev--last-expansion-location))
659 (let ((case-fold-search ignore-case)
660 (count n))
661 (while (and (> count 0)
662 (setq expansion (dabbrev--search
663 abbrev reverse
664 (and ignore-case
665 (if (eq dabbrev-case-distinction
666 'case-replace)
667 case-replace
668 dabbrev-case-distinction)))))
669 (setq count (1- count))))
670 (and expansion
671 (setq dabbrev--last-expansion-location (point)))
672 expansion))))
674 (defun dabbrev--find-all-expansions (abbrev ignore-case)
675 "Return a list of all possible expansions of ABBREV.
676 If IGNORE-CASE is non-nil, accept matches which differ in case."
677 (let ((all-expansions nil)
678 expansion)
679 (save-excursion
680 (goto-char (point-min))
681 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
682 (setq all-expansions (cons expansion all-expansions))))
683 all-expansions))
685 (defun dabbrev--ignore-buffer-p (buffer)
686 "Return non-nil if BUFFER should be ignored by dabbrev."
687 (let ((bn (buffer-name buffer)))
688 (or (member bn dabbrev-ignored-buffer-names)
689 (let ((tail dabbrev-ignored-buffer-regexps)
690 (match nil))
691 (while (and tail (not match))
692 (setq match (string-match (car tail) bn)
693 tail (cdr tail)))
694 match))))
696 (defun dabbrev--find-expansion (abbrev direction ignore-case)
697 "Find one occurrence of ABBREV, and return the expansion.
698 DIRECTION > 0 means look that many times backwards.
699 DIRECTION < 0 means look that many times forward.
700 DIRECTION = 0 means try both backward and forward.
701 IGNORE-CASE non-nil means ignore case when searching.
702 This sets `dabbrev--last-direction' to 1 or -1 according
703 to the direction in which the occurrence was actually found.
704 It sets `dabbrev--last-expansion-location' to the location
705 of the start of the occurrence."
706 (save-excursion
707 ;; If we were scanning something other than the current buffer,
708 ;; continue scanning there.
709 (when dabbrev--last-buffer
710 (set-buffer dabbrev--last-buffer))
712 ;; ------------------------------------------
713 ;; Look backward in current buffer.
714 ;; ------------------------------------------
715 (and (not dabbrev-search-these-buffers-only)
716 (>= direction 0)
717 (setq dabbrev--last-direction (min 1 direction))
718 (dabbrev--try-find abbrev t
719 (max 1 direction)
720 ignore-case))
721 ;; ------------------------------------------
722 ;; Look forward in current buffer
723 ;; or whatever buffer we were last scanning.
724 ;; ------------------------------------------
725 (and (or (not dabbrev-search-these-buffers-only)
726 dabbrev--last-buffer)
727 (<= direction 0)
728 (setq dabbrev--last-direction -1)
729 (dabbrev--try-find abbrev nil
730 (max 1 (- direction))
731 ignore-case))
732 ;; ------------------------------------------
733 ;; Look in other buffers.
734 ;; Always start at (point-min) and look forward.
735 ;; ------------------------------------------
736 (progn
737 (setq dabbrev--last-direction -1)
738 (unless dabbrev--last-buffer
739 ;; If we have just now begun to search other buffers,
740 ;; determine which other buffers we should check.
741 ;; Put that list in dabbrev--friend-buffer-list.
742 (unless dabbrev--friend-buffer-list
743 (setq dabbrev--friend-buffer-list
744 (dabbrev--make-friend-buffer-list))
745 (setq dabbrev--progress-reporter
746 (make-progress-reporter
747 "Scanning for dabbrevs..."
748 (- (length dabbrev--friend-buffer-list)) 0 0 1 1.5))))
749 ;; Walk through the buffers till we find a match.
750 (let (expansion)
751 (while (and (not expansion) dabbrev--friend-buffer-list)
752 (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
753 (set-buffer dabbrev--last-buffer)
754 (progress-reporter-update dabbrev--progress-reporter
755 (- (length dabbrev--friend-buffer-list)))
756 (setq dabbrev--last-expansion-location (point-min))
757 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
758 (progress-reporter-done dabbrev--progress-reporter)
759 expansion)))))
761 ;; Compute the list of buffers to scan.
762 ;; If dabbrev-search-these-buffers-only, then the current buffer
763 ;; is included in this list if it should be searched.
764 ;; Otherwise, the current buffer is searched first specially.,
765 ;; and it is not included in this list.
766 (defun dabbrev--make-friend-buffer-list ()
767 (let ((list (mapcar (function get-buffer)
768 dabbrev-search-these-buffers-only)))
769 (when (and (null dabbrev-search-these-buffers-only)
770 dabbrev--check-other-buffers
771 (or (eq dabbrev--check-other-buffers t)
772 (setq dabbrev--check-other-buffers
773 (y-or-n-p "Scan other buffers also? "))))
774 (setq list (funcall dabbrev-select-buffers-function))
775 ;; If dabbrev-check-all-buffers, tack on all the other
776 ;; buffers at the end of the list, except those which are
777 ;; specifically to be ignored.
778 (if dabbrev-check-all-buffers
779 (setq list
780 (append list
781 (dabbrev-filter-elements
782 buffer (buffer-list)
783 (and (not (memq buffer list))
784 (not (dabbrev--ignore-buffer-p buffer)))))))
785 ;; Remove the current buffer.
786 (setq list (delq (current-buffer) list)))
787 ;; Move buffers in the list that are visible on the screen
788 ;; to the front of the list, but don't add anything to the list.
789 (if list
790 (walk-windows (lambda (w)
791 (unless (eq w (selected-window))
792 (if (memq (window-buffer w) list)
793 (setq list
794 (cons (window-buffer w)
795 (delq (window-buffer w)
796 list))))))))
797 ;; In a minibuffer, search the buffer it was activated from,
798 ;; first after the minibuffer itself. Unless we aren't supposed
799 ;; to search the current buffer either.
800 (if (and (window-minibuffer-p)
801 (not dabbrev-search-these-buffers-only))
802 (setq list
803 (cons (dabbrev--minibuffer-origin)
804 (delq (dabbrev--minibuffer-origin) list))))
805 list))
807 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
808 (if (eq major-mode 'picture-mode)
809 (with-no-warnings
810 (picture-replace-match string fixedcase literal))
811 (replace-match string fixedcase literal)))
813 ;;;----------------------------------------------------------------
814 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
815 "Replace OLD with EXPANSION in the buffer.
816 OLD is text currently in the buffer, perhaps the abbreviation
817 or perhaps another expansion that was tried previously.
818 ABBREV is the abbreviation we are expanding.
819 It is \" \" if we are copying subsequent words.
820 EXPANSION is the expansion substring to be used this time.
821 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
822 to record whether we upcased the expansion, downcased it, or did neither."
823 ;;(undo-boundary)
824 (let ((use-case-replace
825 (and (dabbrev--ignore-case-p abbrev)
826 (if (eq dabbrev-case-replace 'case-replace)
827 case-replace
828 dabbrev-case-replace))))
830 ;; If we upcased or downcased the original expansion,
831 ;; do likewise for the subsequent words when we copy them.
832 ;; Don't do any of the usual case processing, though.
833 (when (equal abbrev " ")
834 (if dabbrev--last-case-pattern
835 (setq expansion
836 (funcall dabbrev--last-case-pattern expansion)))
837 (setq use-case-replace nil))
839 ;; If the expansion has mixed case
840 ;; and it is not simply a capitalized word,
841 ;; or if the abbrev has mixed case,
842 ;; and if the given abbrev's case pattern
843 ;; matches the start of the expansion,
844 ;; copy the expansion's case
845 ;; instead of downcasing all the rest.
847 ;; Treat a one-capital-letter (possibly with preceding non-letter
848 ;; characters) abbrev as "not all upper case", so as to force
849 ;; preservation of the expansion's pattern if the expansion starts
850 ;; with a capital letter.
851 (let ((expansion-rest (substring expansion 1))
852 (first-letter-position (string-match "[[:alpha:]]" abbrev)))
853 (if (or (null first-letter-position)
854 (and (not
855 (and (or (string= expansion-rest (downcase expansion-rest))
856 (string= expansion-rest (upcase expansion-rest)))
857 (or (string= abbrev (downcase abbrev))
858 (and (string= abbrev (upcase abbrev))
859 (> (- (length abbrev) first-letter-position)
860 1)))))
861 (string= abbrev
862 (substring expansion 0 (length abbrev)))))
863 (setq use-case-replace nil)))
865 ;; If the abbrev and the expansion are both all-lower-case
866 ;; then don't do any conversion. The conversion would be a no-op
867 ;; for this replacement, but it would carry forward to subsequent words.
868 ;; The goal of this is to prevent that carrying forward.
869 (if (and (string= expansion (downcase expansion))
870 (string= abbrev (downcase abbrev)))
871 (setq use-case-replace nil))
873 (if use-case-replace
874 (setq expansion (downcase expansion)))
876 ;; In case we insert subsequent words,
877 ;; record if we upcased or downcased the first word,
878 ;; in order to do likewise for subsequent words.
879 (and record-case-pattern
880 (setq dabbrev--last-case-pattern
881 (and use-case-replace
882 (cond ((equal abbrev (upcase abbrev)) 'upcase)
883 ((equal abbrev (downcase abbrev)) 'downcase)))))
885 ;; Convert whitespace to single spaces.
886 (if dabbrev-eliminate-newlines
887 (let ((pos
888 (if (equal abbrev " ") 0 (length abbrev))))
889 ;; If ABBREV is real, search after the end of it.
890 ;; If ABBREV is space and we are copying successive words,
891 ;; search starting at the front.
892 (while (string-match "[\n \t]+" expansion pos)
893 (setq pos (1+ (match-beginning 0)))
894 (setq expansion (replace-match " " nil nil expansion)))))
896 (if old
897 (save-excursion
898 (search-backward old))
899 ;;(set-match-data (list (point-marker) (point-marker)))
900 (search-backward abbrev)
901 (search-forward abbrev))
903 ;; Make case of replacement conform to case of abbreviation
904 ;; provided (1) that kind of thing is enabled in this buffer
905 ;; and (2) the replacement itself is all lower case.
906 (dabbrev--safe-replace-match expansion
907 (not use-case-replace)
909 ;; Return the expansion actually used.
910 expansion)
913 ;;;----------------------------------------------------------------
914 ;;; Search function used by dabbrevs library.
917 (defun dabbrev--search (abbrev reverse ignore-case)
918 "Search for something that could be used to expand ABBREV.
920 Second arg, REVERSE, is t for reverse search, nil for forward.
921 The variable `dabbrev-limit' controls the maximum search region size.
922 Third argument IGNORE-CASE non-nil means treat case as insignificant while
923 looking for a match and when comparing with previous matches. Also if
924 that's non-nil and the match is found at the beginning of a sentence
925 and is in lower case except for the initial then it is converted to
926 all lower case for return.
928 Table of expansions already seen is examined in buffer
929 `dabbrev--last-table' so that only distinct possibilities are found
930 by dabbrev-re-expand.
932 Returns the expansion found, or nil if not found.
933 Leaves point at the location of the start of the expansion."
934 (save-match-data
935 (let ((pattern1 (concat (regexp-quote abbrev)
936 "\\(" dabbrev--abbrev-char-regexp "\\)"))
937 (pattern2 (concat (regexp-quote abbrev)
938 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
939 ;; This makes it possible to find matches in minibuffer prompts
940 ;; even when they are "inviolable".
941 (inhibit-point-motion-hooks t)
942 found-string result)
943 ;; Limited search.
944 (save-restriction
945 (and dabbrev-limit
946 (narrow-to-region
947 dabbrev--last-expansion-location
948 (+ (point) (if reverse (- dabbrev-limit) dabbrev-limit))))
949 ;;--------------------------------
950 ;; Look for a distinct expansion, using dabbrev--last-table.
951 ;;--------------------------------
952 (while (and (not found-string)
953 (if reverse
954 (re-search-backward pattern1 nil t)
955 (re-search-forward pattern1 nil t)))
956 (goto-char (match-beginning 0))
957 ;; In case we matched in the middle of a word,
958 ;; back up to start of word and verify we still match.
959 (dabbrev--goto-start-of-abbrev)
961 (if (not (looking-at pattern1))
963 ;; We have a truly valid match. Find the end.
964 (re-search-forward pattern2)
965 (setq found-string (match-string-no-properties 0))
966 (setq result found-string)
967 (and ignore-case (setq found-string (downcase found-string)))
968 ;; Ignore this match if it's already in the table.
969 (if (dabbrev-filter-elements
970 table-string dabbrev--last-table
971 (string= found-string table-string))
972 (setq found-string nil)))
973 ;; Prepare to continue searching.
974 (goto-char (if reverse (match-beginning 0) (match-end 0))))
975 ;; If we found something, use it.
976 (when found-string
977 ;; Put it into `dabbrev--last-table'
978 ;; and return it (either downcased, or as is).
979 (setq dabbrev--last-table
980 (cons found-string dabbrev--last-table))
981 result)))))
983 (provide 'dabbrev)
985 ;;; dabbrev.el ends here