Include GFDL in org.texi
[emacs.git] / lisp / dabbrev.el
blobab886edac25de810798572bc9fb7b40228fa8ae6
1 ;;; dabbrev.el --- dynamic abbreviation package -*- lexical-binding: t -*-
3 ;; Copyright (C) 1985-1986, 1992, 1994, 1996-1997, 2000-2012
4 ;; Free 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 :group 'dabbrev)
290 ;;----------------------------------------------------------------
291 ;; Internal variables
292 ;;----------------------------------------------------------------
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 ;; If non-nil, a function to use when copying successive words.
322 ;; It should be `upcase' or `downcase'.
323 (defvar dabbrev--last-case-pattern nil)
325 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
326 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
328 ;; The regexp for recognizing a character in an abbreviation.
329 (defvar dabbrev--abbrev-char-regexp nil)
331 ;; The progress reporter for buffer-scanning progress.
332 (defvar dabbrev--progress-reporter nil)
334 ;;----------------------------------------------------------------
335 ;; Macros
336 ;;----------------------------------------------------------------
338 (defsubst dabbrev--minibuffer-origin ()
339 "Get the buffer from which mini-buffer."
340 (window-buffer (minibuffer-selected-window)))
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 (defun dabbrev--ignore-case-p (abbrev)
365 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
366 case-fold-search
367 dabbrev-case-fold-search)
368 (or (not dabbrev-upcase-means-case-search)
369 (string= abbrev (downcase abbrev)))))
371 ;;;###autoload
372 (defun dabbrev-completion (&optional arg)
373 "Completion on current word.
374 Like \\[dabbrev-expand] but finds all expansions in the current buffer
375 and presents suggestions for completion.
377 With a prefix argument ARG, it searches all buffers accepted by the
378 function pointed out by `dabbrev-friend-buffer-function' to find the
379 completions.
381 If the prefix argument is 16 (which comes from \\[universal-argument] \\[universal-argument]),
382 then it searches *all* buffers."
383 (interactive "*P")
384 (dabbrev--reset-global-variables)
385 (let* ((dabbrev-check-other-buffers (and arg t))
386 (dabbrev-check-all-buffers
387 (and arg (= (prefix-numeric-value arg) 16)))
388 (abbrev (dabbrev--abbrev-at-point))
389 (beg (progn (search-backward abbrev) (point)))
390 (end (progn (search-forward abbrev) (point)))
391 (ignore-case-p (dabbrev--ignore-case-p abbrev))
392 (list 'uninitialized)
393 (table
394 (lambda (s p a)
395 (if (eq a 'metadata)
396 `(metadata (cycle-sort-function . ,#'identity)
397 (category . dabbrev))
398 (when (eq list 'uninitialized)
399 (save-excursion
400 ;;--------------------------------
401 ;; New abbreviation to expand.
402 ;;--------------------------------
403 (setq dabbrev--last-abbreviation abbrev)
404 ;; Find all expansion
405 (let ((completion-list
406 (dabbrev--find-all-expansions abbrev ignore-case-p))
407 (completion-ignore-case ignore-case-p))
408 (or (consp completion-list)
409 (user-error "No dynamic expansion for \"%s\" found%s"
410 abbrev
411 (if dabbrev--check-other-buffers
412 "" " in this-buffer")))
413 (setq list
414 (cond
415 ((not (and ignore-case-p dabbrev-case-replace))
416 completion-list)
417 ((string= abbrev (upcase abbrev))
418 (mapcar #'upcase completion-list))
419 ((string= (substring abbrev 0 1)
420 (upcase (substring abbrev 0 1)))
421 (mapcar #'capitalize completion-list))
423 (mapcar #'downcase completion-list)))))))
424 (complete-with-action a list s p)))))
425 (completion-in-region beg end table)))
427 ;;;###autoload
428 (defun dabbrev-expand (arg)
429 "Expand previous word \"dynamically\".
431 Expands to the most recent, preceding word for which this is a prefix.
432 If no suitable preceding word is found, words following point are
433 considered. If still no suitable word is found, then look in the
434 buffers accepted by the function pointed out by variable
435 `dabbrev-friend-buffer-function'.
437 A positive prefix argument, N, says to take the Nth backward *distinct*
438 possibility. A negative argument says search forward.
440 If the cursor has not moved from the end of the previous expansion and
441 no argument is given, replace the previously-made expansion
442 with the next possible expansion not yet tried.
444 The variable `dabbrev-backward-only' may be used to limit the
445 direction of search to backward if set non-nil.
447 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
448 (interactive "*P")
449 (let (abbrev record-case-pattern
450 expansion old direction (orig-point (point)))
451 ;; abbrev -- the abbrev to expand
452 ;; expansion -- the expansion found (eventually) or nil until then
453 ;; old -- the text currently in the buffer
454 ;; (the abbrev, or the previously-made expansion)
455 (save-excursion
456 (if (and (null arg)
457 (markerp dabbrev--last-abbrev-location)
458 (marker-position dabbrev--last-abbrev-location)
459 (or (eq last-command this-command)
460 (and (window-minibuffer-p (selected-window))
461 (= dabbrev--last-abbrev-location
462 (point)))))
463 ;; Find a different expansion for the same abbrev as last time.
464 (progn
465 (setq abbrev dabbrev--last-abbreviation)
466 (setq old dabbrev--last-expansion)
467 (setq direction dabbrev--last-direction))
468 ;; If the user inserts a space after expanding
469 ;; and then asks to expand again, always fetch the next word.
470 (if (and (eq (preceding-char) ?\s)
471 (markerp dabbrev--last-abbrev-location)
472 (marker-position dabbrev--last-abbrev-location)
473 (= (point) (1+ dabbrev--last-abbrev-location)))
474 (progn
475 ;; The "abbrev" to expand is just the space.
476 (setq abbrev " ")
477 (save-excursion
478 (save-restriction
479 (widen)
480 (if dabbrev--last-buffer
481 (set-buffer dabbrev--last-buffer))
482 ;; Find the end of the last "expansion" word.
483 (if (or (eq dabbrev--last-direction 1)
484 (and (eq dabbrev--last-direction 0)
485 (< dabbrev--last-expansion-location (point))))
486 (setq dabbrev--last-expansion-location
487 (+ dabbrev--last-expansion-location
488 (length dabbrev--last-expansion))))
489 (goto-char dabbrev--last-expansion-location)
490 ;; Take the following word, with intermediate separators,
491 ;; as our expansion this time.
492 (re-search-forward
493 (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
494 (setq expansion (buffer-substring-no-properties
495 dabbrev--last-expansion-location (point)))
497 ;; Record the end of this expansion, in case we repeat this.
498 (setq dabbrev--last-expansion-location (point))))
499 ;; Indicate that dabbrev--last-expansion-location is
500 ;; at the end of the expansion.
501 (setq dabbrev--last-direction -1))
503 ;; We have a different abbrev to expand.
504 (dabbrev--reset-global-variables)
505 (setq direction (if (null arg)
506 (if dabbrev-backward-only 1 0)
507 (prefix-numeric-value arg)))
508 (setq abbrev (dabbrev--abbrev-at-point))
509 (setq record-case-pattern t)
510 (setq old nil)))
512 ;;--------------------------------
513 ;; Find the expansion
514 ;;--------------------------------
515 (or expansion
516 (setq expansion
517 (dabbrev--find-expansion
518 abbrev direction
519 (dabbrev--ignore-case-p abbrev)))))
520 (cond
521 ((not expansion)
522 (dabbrev--reset-global-variables)
523 (if old
524 (save-excursion
525 (setq buffer-undo-list (cons orig-point buffer-undo-list))
526 ;; Put back the original abbrev with its original case pattern.
527 (search-backward old)
528 (insert abbrev)
529 (delete-region (point) (+ (point) (length old)))))
530 (user-error "No%s dynamic expansion for `%s' found"
531 (if old " further" "") abbrev))
533 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
534 (minibuffer-window-active-p (selected-window))))
535 (progn
536 (message "Expansion found in '%s'"
537 (buffer-name dabbrev--last-buffer))
538 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
539 (message nil))
540 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
541 (null dabbrev--last-buffer))
542 (numberp dabbrev--last-expansion-location)
543 (and (> dabbrev--last-expansion-location (point))))
544 (setq dabbrev--last-expansion-location
545 (copy-marker dabbrev--last-expansion-location)))
546 ;; Success: stick it in and return.
547 (setq buffer-undo-list (cons orig-point buffer-undo-list))
548 (dabbrev--substitute-expansion old abbrev expansion
549 record-case-pattern)
551 ;; Save state for re-expand.
552 (setq dabbrev--last-expansion expansion)
553 (setq dabbrev--last-abbreviation abbrev)
554 (setq dabbrev--last-abbrev-location (point-marker))))))
556 ;;----------------------------------------------------------------
557 ;; Local functions
558 ;;----------------------------------------------------------------
560 (defun dabbrev--same-major-mode-p (other-buffer)
561 "Check if OTHER-BUFFER has the same major mode as current buffer."
562 (eq major-mode
563 (with-current-buffer other-buffer
564 major-mode)))
566 (defun dabbrev--goto-start-of-abbrev ()
567 "Back over all abbrev type characters and then moves forward over
568 all skip characters."
569 ;; Move backwards over abbrev chars
570 (save-match-data
571 (when (> (point) (minibuffer-prompt-end))
572 (forward-char -1)
573 (while (and (looking-at dabbrev--abbrev-char-regexp)
574 (> (point) (minibuffer-prompt-end))
575 (not (= (point) (field-beginning (point) nil
576 (1- (point))))))
577 (forward-char -1))
578 (or (looking-at dabbrev--abbrev-char-regexp)
579 (forward-char 1)))
580 (and dabbrev-abbrev-skip-leading-regexp
581 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
582 (forward-char 1)))))
584 (defun dabbrev--abbrev-at-point ()
585 "Extract the symbol at point to serve as abbreviation."
586 ;; Check for error
587 (if (bobp)
588 (user-error "No possible abbreviation preceding point"))
589 ;; Return abbrev at point
590 (save-excursion
591 ;; Record the end of the abbreviation.
592 (setq dabbrev--last-abbrev-location (point))
593 ;; If we aren't right after an abbreviation,
594 ;; move point back to just after one.
595 ;; This is so the user can get successive words
596 ;; by typing the punctuation followed by M-/.
597 (save-match-data
598 (if (save-excursion
599 (forward-char -1)
600 (not (looking-at (or dabbrev-abbrev-char-regexp
601 "\\sw\\|\\s_"))))
602 (if (re-search-backward (or dabbrev-abbrev-char-regexp
603 "\\sw\\|\\s_")
604 nil t)
605 (forward-char 1)
606 (user-error "No possible abbreviation preceding point"))))
607 ;; Now find the beginning of that one.
608 (dabbrev--goto-start-of-abbrev)
609 (buffer-substring-no-properties
610 dabbrev--last-abbrev-location (point))))
612 (defun dabbrev--reset-global-variables ()
613 "Initialize all global variables."
614 (setq dabbrev--last-table nil
615 dabbrev--last-abbreviation nil
616 dabbrev--last-abbrev-location nil
617 dabbrev--last-direction nil
618 dabbrev--last-expansion nil
619 dabbrev--last-expansion-location nil
620 dabbrev--friend-buffer-list nil
621 dabbrev--last-buffer nil
622 dabbrev--last-buffer-found nil
623 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
624 "\\sw\\|\\s_")
625 dabbrev--check-other-buffers dabbrev-check-other-buffers))
627 (defun dabbrev--select-buffers ()
628 "Return a list of other buffers to search for a possible abbrev.
629 The current buffer is not included in the list.
631 This function makes a list of all the buffers returned by `buffer-list',
632 then discards buffers whose names match `dabbrev-ignored-buffer-names'
633 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
634 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
635 with the buffer as argument.
636 It returns the list of the buffers that are not discarded."
637 (dabbrev-filter-elements
638 buffer (buffer-list)
639 (and (not (eq (current-buffer) buffer))
640 (not (dabbrev--ignore-buffer-p buffer))
641 (boundp 'dabbrev-friend-buffer-function)
642 (funcall dabbrev-friend-buffer-function buffer))))
644 (defun dabbrev--try-find (abbrev reverse n ignore-case)
645 "Search for ABBREV, backwards if REVERSE, N times.
646 If IGNORE-CASE is non-nil, ignore case while searching.
647 Return the expansion found, and save the location of the start
648 of the expansion in `dabbrev--last-expansion-location'."
649 (save-excursion
650 (save-restriction
651 (widen)
652 (let ((expansion nil))
653 (and dabbrev--last-expansion-location
654 (goto-char dabbrev--last-expansion-location))
655 (let ((case-fold-search ignore-case)
656 (count n))
657 (while (and (> count 0)
658 (setq expansion (dabbrev--search
659 abbrev reverse
660 (and ignore-case
661 (if (eq dabbrev-case-distinction
662 'case-replace)
663 case-replace
664 dabbrev-case-distinction)))))
665 (setq count (1- count))))
666 (and expansion
667 (setq dabbrev--last-expansion-location (point)))
668 expansion))))
670 (defun dabbrev--find-all-expansions (abbrev ignore-case)
671 "Return a list of all possible expansions of ABBREV.
672 If IGNORE-CASE is non-nil, accept matches which differ in case."
673 (let ((all-expansions nil)
674 expansion)
675 (save-excursion
676 (goto-char (point-min))
677 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
678 (setq all-expansions (cons expansion all-expansions))))
679 all-expansions))
681 (defun dabbrev--ignore-buffer-p (buffer)
682 "Return non-nil if BUFFER should be ignored by dabbrev."
683 (let ((bn (buffer-name buffer)))
684 (or (member bn dabbrev-ignored-buffer-names)
685 (let ((tail dabbrev-ignored-buffer-regexps)
686 (match nil))
687 (while (and tail (not match))
688 (setq match (string-match (car tail) bn)
689 tail (cdr tail)))
690 match))))
692 (defun dabbrev--find-expansion (abbrev direction ignore-case)
693 "Find one occurrence of ABBREV, and return the expansion.
694 DIRECTION > 0 means look that many times backwards.
695 DIRECTION < 0 means look that many times forward.
696 DIRECTION = 0 means try both backward and forward.
697 IGNORE-CASE non-nil means ignore case when searching.
698 This sets `dabbrev--last-direction' to 1 or -1 according
699 to the direction in which the occurrence was actually found.
700 It sets `dabbrev--last-expansion-location' to the location
701 of the start of the occurrence."
702 (save-excursion
703 ;; If we were scanning something other than the current buffer,
704 ;; continue scanning there.
705 (when dabbrev--last-buffer
706 (set-buffer dabbrev--last-buffer))
708 ;; ------------------------------------------
709 ;; Look backward in current buffer.
710 ;; ------------------------------------------
711 (and (not dabbrev-search-these-buffers-only)
712 (>= direction 0)
713 (setq dabbrev--last-direction (min 1 direction))
714 (dabbrev--try-find abbrev t
715 (max 1 direction)
716 ignore-case))
717 ;; ------------------------------------------
718 ;; Look forward in current buffer
719 ;; or whatever buffer we were last scanning.
720 ;; ------------------------------------------
721 (and (or (not dabbrev-search-these-buffers-only)
722 dabbrev--last-buffer)
723 (<= direction 0)
724 (setq dabbrev--last-direction -1)
725 (dabbrev--try-find abbrev nil
726 (max 1 (- direction))
727 ignore-case))
728 ;; ------------------------------------------
729 ;; Look in other buffers.
730 ;; Always start at (point-min) and look forward.
731 ;; ------------------------------------------
732 (progn
733 (setq dabbrev--last-direction -1)
734 (unless dabbrev--last-buffer
735 ;; If we have just now begun to search other buffers,
736 ;; determine which other buffers we should check.
737 ;; Put that list in dabbrev--friend-buffer-list.
738 (unless dabbrev--friend-buffer-list
739 (setq dabbrev--friend-buffer-list
740 (dabbrev--make-friend-buffer-list))
741 (setq dabbrev--progress-reporter
742 (make-progress-reporter
743 "Scanning for dabbrevs..."
744 (- (length dabbrev--friend-buffer-list)) 0 0 1 1.5))))
745 ;; Walk through the buffers till we find a match.
746 (let (expansion)
747 (while (and (not expansion) dabbrev--friend-buffer-list)
748 (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
749 (set-buffer dabbrev--last-buffer)
750 (progress-reporter-update dabbrev--progress-reporter
751 (- (length dabbrev--friend-buffer-list)))
752 (setq dabbrev--last-expansion-location (point-min))
753 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
754 (progress-reporter-done dabbrev--progress-reporter)
755 expansion)))))
757 ;; Compute the list of buffers to scan.
758 ;; If dabbrev-search-these-buffers-only, then the current buffer
759 ;; is included in this list if it should be searched.
760 ;; Otherwise, the current buffer is searched first specially.,
761 ;; and it is not included in this list.
762 (defun dabbrev--make-friend-buffer-list ()
763 (let ((list (mapcar (function get-buffer)
764 dabbrev-search-these-buffers-only)))
765 (when (and (null dabbrev-search-these-buffers-only)
766 dabbrev--check-other-buffers
767 (or (eq dabbrev--check-other-buffers t)
768 (setq dabbrev--check-other-buffers
769 (y-or-n-p "Scan other buffers also? "))))
770 (setq list (funcall dabbrev-select-buffers-function))
771 ;; If dabbrev-check-all-buffers, tack on all the other
772 ;; buffers at the end of the list, except those which are
773 ;; specifically to be ignored.
774 (if dabbrev-check-all-buffers
775 (setq list
776 (append list
777 (dabbrev-filter-elements
778 buffer (buffer-list)
779 (and (not (memq buffer list))
780 (not (dabbrev--ignore-buffer-p buffer)))))))
781 ;; Remove the current buffer.
782 (setq list (delq (current-buffer) list)))
783 ;; Move buffers in the list that are visible on the screen
784 ;; to the front of the list, but don't add anything to the list.
785 (if list
786 (walk-windows (lambda (w)
787 (unless (eq w (selected-window))
788 (if (memq (window-buffer w) list)
789 (setq list
790 (cons (window-buffer w)
791 (delq (window-buffer w)
792 list))))))))
793 ;; In a minibuffer, search the buffer it was activated from,
794 ;; first after the minibuffer itself. Unless we aren't supposed
795 ;; to search the current buffer either.
796 (if (and (window-minibuffer-p (selected-window))
797 (not dabbrev-search-these-buffers-only))
798 (setq list
799 (cons (dabbrev--minibuffer-origin)
800 (delq (dabbrev--minibuffer-origin) list))))
801 list))
803 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
804 (if (eq major-mode 'picture-mode)
805 (with-no-warnings
806 (picture-replace-match string fixedcase literal))
807 (replace-match string fixedcase literal)))
809 ;;;----------------------------------------------------------------
810 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
811 "Replace OLD with EXPANSION in the buffer.
812 OLD is text currently in the buffer, perhaps the abbreviation
813 or perhaps another expansion that was tried previously.
814 ABBREV is the abbreviation we are expanding.
815 It is \" \" if we are copying subsequent words.
816 EXPANSION is the expansion substring to be used this time.
817 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
818 to record whether we upcased the expansion, downcased it, or did neither."
819 ;;(undo-boundary)
820 (let ((use-case-replace
821 (and (dabbrev--ignore-case-p abbrev)
822 (if (eq dabbrev-case-replace 'case-replace)
823 case-replace
824 dabbrev-case-replace))))
826 ;; If we upcased or downcased the original expansion,
827 ;; do likewise for the subsequent words when we copy them.
828 ;; Don't do any of the usual case processing, though.
829 (when (equal abbrev " ")
830 (if dabbrev--last-case-pattern
831 (setq expansion
832 (funcall dabbrev--last-case-pattern expansion)))
833 (setq use-case-replace nil))
835 ;; If the expansion has mixed case
836 ;; and it is not simply a capitalized word,
837 ;; or if the abbrev has mixed case,
838 ;; and if the given abbrev's case pattern
839 ;; matches the start of the expansion,
840 ;; copy the expansion's case
841 ;; instead of downcasing all the rest.
843 ;; Treat a one-capital-letter (possibly with preceding non-letter
844 ;; characters) abbrev as "not all upper case", so as to force
845 ;; preservation of the expansion's pattern if the expansion starts
846 ;; with a capital letter.
847 (let ((expansion-rest (substring expansion 1))
848 (first-letter-position (string-match "[[:alpha:]]" abbrev)))
849 (if (or (null first-letter-position)
850 (and (not
851 (and (or (string= expansion-rest (downcase expansion-rest))
852 (string= expansion-rest (upcase expansion-rest)))
853 (or (string= abbrev (downcase abbrev))
854 (and (string= abbrev (upcase abbrev))
855 (> (- (length abbrev) first-letter-position)
856 1)))))
857 (string= abbrev
858 (substring expansion 0 (length abbrev)))))
859 (setq use-case-replace nil)))
861 ;; If the abbrev and the expansion are both all-lower-case
862 ;; then don't do any conversion. The conversion would be a no-op
863 ;; for this replacement, but it would carry forward to subsequent words.
864 ;; The goal of this is to prevent that carrying forward.
865 (if (and (string= expansion (downcase expansion))
866 (string= abbrev (downcase abbrev)))
867 (setq use-case-replace nil))
869 (if use-case-replace
870 (setq expansion (downcase expansion)))
872 ;; In case we insert subsequent words,
873 ;; record if we upcased or downcased the first word,
874 ;; in order to do likewise for subsequent words.
875 (and record-case-pattern
876 (setq dabbrev--last-case-pattern
877 (and use-case-replace
878 (cond ((equal abbrev (upcase abbrev)) 'upcase)
879 ((equal abbrev (downcase abbrev)) 'downcase)))))
881 ;; Convert whitespace to single spaces.
882 (if dabbrev-eliminate-newlines
883 (let ((pos
884 (if (equal abbrev " ") 0 (length abbrev))))
885 ;; If ABBREV is real, search after the end of it.
886 ;; If ABBREV is space and we are copying successive words,
887 ;; search starting at the front.
888 (while (string-match "[\n \t]+" expansion pos)
889 (setq pos (1+ (match-beginning 0)))
890 (setq expansion (replace-match " " nil nil expansion)))))
892 (if old
893 (save-excursion
894 (search-backward old))
895 ;;(set-match-data (list (point-marker) (point-marker)))
896 (search-backward abbrev)
897 (search-forward abbrev))
899 ;; Make case of replacement conform to case of abbreviation
900 ;; provided (1) that kind of thing is enabled in this buffer
901 ;; and (2) the replacement itself is all lower case.
902 (dabbrev--safe-replace-match expansion
903 (not use-case-replace)
904 t)))
907 ;;;----------------------------------------------------------------
908 ;;; Search function used by dabbrevs library.
911 (defun dabbrev--search (abbrev reverse ignore-case)
912 "Search for something that could be used to expand ABBREV.
914 Second arg, REVERSE, is t for reverse search, nil for forward.
915 The variable `dabbrev-limit' controls the maximum search region size.
916 Third argument IGNORE-CASE non-nil means treat case as insignificant while
917 looking for a match and when comparing with previous matches. Also if
918 that's non-nil and the match is found at the beginning of a sentence
919 and is in lower case except for the initial then it is converted to
920 all lower case for return.
922 Table of expansions already seen is examined in buffer
923 `dabbrev--last-table' so that only distinct possibilities are found
924 by dabbrev-re-expand.
926 Returns the expansion found, or nil if not found.
927 Leaves point at the location of the start of the expansion."
928 (save-match-data
929 (let ((pattern1 (concat (regexp-quote abbrev)
930 "\\(" dabbrev--abbrev-char-regexp "\\)"))
931 (pattern2 (concat (regexp-quote abbrev)
932 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
933 ;; This makes it possible to find matches in minibuffer prompts
934 ;; even when they are "inviolable".
935 (inhibit-point-motion-hooks t)
936 found-string result)
937 ;; Limited search.
938 (save-restriction
939 (and dabbrev-limit
940 (narrow-to-region
941 dabbrev--last-expansion-location
942 (+ (point) (if reverse (- dabbrev-limit) dabbrev-limit))))
943 ;;--------------------------------
944 ;; Look for a distinct expansion, using dabbrev--last-table.
945 ;;--------------------------------
946 (while (and (not found-string)
947 (if reverse
948 (re-search-backward pattern1 nil t)
949 (re-search-forward pattern1 nil t)))
950 (goto-char (match-beginning 0))
951 ;; In case we matched in the middle of a word,
952 ;; back up to start of word and verify we still match.
953 (dabbrev--goto-start-of-abbrev)
955 (if (not (looking-at pattern1))
957 ;; We have a truly valid match. Find the end.
958 (re-search-forward pattern2)
959 (setq found-string (match-string-no-properties 0))
960 (setq result found-string)
961 (and ignore-case (setq found-string (downcase found-string)))
962 ;; Ignore this match if it's already in the table.
963 (if (dabbrev-filter-elements
964 table-string dabbrev--last-table
965 (string= found-string table-string))
966 (setq found-string nil)))
967 ;; Prepare to continue searching.
968 (goto-char (if reverse (match-beginning 0) (match-end 0))))
969 ;; If we found something, use it.
970 (when found-string
971 ;; Put it into `dabbrev--last-table'
972 ;; and return it (either downcased, or as is).
973 (setq dabbrev--last-table
974 (cons found-string dabbrev--last-table))
975 result)))))
977 (provide 'dabbrev)
979 ;;; dabbrev.el ends here