Unquote lambda functions. Add autoload cookies to functions formerly
[emacs.git] / lisp / dabbrev.el
blobb5ae7ac3b71b66caee8202d27252c95dcb9fdb77
1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1996, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Don Morrison
7 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
8 ;; Created: 16 Mars 1992
9 ;; Lindberg's last update version: 5.7
10 ;; Keywords: abbrev expand completion convenience
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; The purpose with this package is to let you write just a few
32 ;; characters of words you've written earlier to be able to expand
33 ;; them.
35 ;; To expand a word, just put the point right after the word and press
36 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
38 ;; Check out the customizable variables below to learn about all the
39 ;; features of this package.
41 ;;; Hints and tips for major modes writers:
43 ;; Recommended values C/Lisp etc text
44 ;; dabbrev-case-fold-search nil t
45 ;; dabbrev-case-replace nil t
47 ;; Set the variables you want special for your mode like this:
48 ;; (set (make-local-variable 'dabbrev-case-replace) nil)
49 ;; Then you don't interfere with other modes.
51 ;; If your mode handles buffers that refers to other buffers
52 ;; (i.e. compilation-mode, gud-mode), then try to set
53 ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
54 ;; to a function that point out those buffers.
56 ;; Same goes for major-modes that are connected to other modes. There
57 ;; are for instance a number of mail-modes. One for reading, one for
58 ;; creating a new mail etc. Maybe those should be connected.
60 ;; Example for GNUS (when we write a reply, we want dabbrev to look in
61 ;; the article for expansion):
62 ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
63 ;; (lambda (buffer)
64 ;; (save-excursion
65 ;; (set-buffer buffer)
66 ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
69 ;; Known bugs and limitations.
70 ;; - Possible to do several levels of `dabbrev-completion' in the
71 ;; minibuffer.
72 ;; - dabbrev-completion doesn't handle resetting the globals variables
73 ;; right. It resets them after finding the abbrev.
75 ;; Future enhancements
76 ;; - Check the tags-files? Like tags-complete?
77 ;; - Add the possibility of searching both forward and backward to
78 ;; the nearest expansion.
79 ;; - Check the kill-ring when everything else fails. (Maybe something
80 ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
82 ;;; These people gave suggestions:
83 ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
84 ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
85 ;; [jules] Julian Gosnell <jules@x.co.uk>
86 ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
87 ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
88 ;; [alon] Alon Albert <al%imercury@uunet.uu.net>
89 ;; [tromey] Tom Tromey <tromey@busco.lanl.gov>
90 ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
91 ;; [Petri] Petri Raitio <per@tekla.fi>
92 ;; [ejb] Jay Berkenbilt <ejb@ql.org>
93 ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
94 ;; ... and to all the people who have participated in the beta tests.
96 ;;; Code:
98 ;;----------------------------------------------------------------
99 ;; Customization variables
100 ;;----------------------------------------------------------------
102 (defgroup dabbrev nil
103 "Dynamic Abbreviations."
104 :tag "Dynamic Abbreviations"
105 :group 'abbrev
106 :group 'convenience)
108 (defcustom dabbrev-backward-only nil
109 "*If non-nil, `dabbrev-expand' only looks backwards."
110 :type 'boolean
111 :group 'dabbrev)
113 (defcustom dabbrev-limit nil
114 "*Limits region searched by `dabbrev-expand' to this many chars away."
115 :type '(choice (const :tag "off" nil)
116 integer)
117 :group 'dabbrev)
119 (defcustom dabbrev-abbrev-skip-leading-regexp nil
120 "*Regexp for skipping leading characters of an abbreviation.
122 Example: Set this to \"\\\\$\" for programming languages
123 in which variable names may appear with or without a leading `$'.
124 \(For example, in Makefiles.\)
126 Set this to nil if no characters should be skipped."
127 :type '(choice regexp
128 (const :tag "off" nil))
129 :group 'dabbrev)
131 (defcustom dabbrev-eliminate-newlines t
132 "*Non-nil means dabbrev should not insert newlines.
133 Instead it converts them to spaces."
134 :type 'boolean
135 :group 'dabbrev)
137 (defcustom dabbrev-case-fold-search 'case-fold-search
138 "*Control whether dabbrev searches should ignore case.
139 A value of nil means case is significant.
140 A value of `case-fold-search' means case is significant
141 if `case-fold-search' is nil.
142 Any other non-nil version means case is not significant."
143 :type '(choice (const :tag "off" nil)
144 (const :tag "like search" case-fold-search)
145 (other :tag "on" t))
146 :group 'dabbrev)
148 (defcustom dabbrev-upcase-means-case-search nil
149 "*The significance of an uppercase character in an abbreviation.
150 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)
188 (defcustom dabbrev-abbrev-char-regexp nil
189 "*Regexp to recognize a character in an abbreviation or expansion.
190 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
192 Set this variable to \"\\\\sw\" if you want ordinary words or
193 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
194 syntax is \"symbol\" as well as those whose syntax is \"word\".
196 The value nil has a special meaning: the abbreviation is from point to
197 previous word-start, but the search is for symbols.
199 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
200 while `yes', `or', `no' and `p' are considered words. If this
201 variable is nil, then expanding `yes-or-no-' looks for a symbol
202 starting with or containing `no-'. If you set this variable to
203 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
204 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
205 expanding `yes-or-no-' signals an error because `-' is not part of a word;
206 but expanding `yes-or-no' looks for a word starting with `no'.
208 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
209 :type '(choice (const nil)
210 regexp)
211 :group 'dabbrev)
213 (defcustom dabbrev-check-all-buffers t
214 "*Non-nil means dabbrev package should search *all* buffers.
216 Dabbrev always searches the current buffer first. Then, if
217 `dabbrev-check-other-buffers' says so, it searches the buffers
218 designated by `dabbrev-select-buffers-function'.
220 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
221 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
222 or matched by `dabbrev-ignored-regexps'."
223 :type 'boolean
224 :group 'dabbrev)
226 (defcustom dabbrev-ignored-buffer-names '("*Messages*" "*Buffer List*")
227 "*List of buffer names that dabbrev should not check.
228 See also `dabbrev-ignored-buffer-regexps'."
229 :type '(repeat (string :tag "Buffer name"))
230 :group 'dabbrev
231 :version "20.3")
233 (defcustom dabbrev-ignored-buffer-regexps nil
234 "*List of regexps matching names of buffers that dabbrev should not check.
235 See also `dabbrev-ignored-buffer-names'."
236 :type '(repeat regexp)
237 :group 'dabbrev
238 :version "21.1")
240 (defcustom dabbrev-check-other-buffers t
241 "*Should \\[dabbrev-expand] look in other buffers?\
243 nil: Don't look in other buffers.
244 t: Also look for expansions in the buffers pointed out by
245 `dabbrev-select-buffers-function'.
246 Anything else: When we can't find any more expansions in
247 the current buffer, then ask the user whether to look in other
248 buffers too.
250 The default value is t."
251 :type '(choice (const :tag "off" nil)
252 (const :tag "on" t)
253 (other :tag "ask" other))
254 :group 'dabbrev)
256 ;; I guess setting this to a function that selects all C- or C++-
257 ;; mode buffers would be a good choice for a debugging buffer,
258 ;; when debugging C- or C++-code.
259 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
260 "A function that selects buffers that should be searched by dabbrev.
261 The function should take no arguments and return a list of buffers to
262 search for expansions. See the source of `dabbrev--select-buffers'
263 for an example.
265 A mode setting this variable should make it buffer local.")
267 (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
268 "*A function to decide whether dabbrev should search OTHER-BUFFER.
269 The function should take one argument, OTHER-BUFFER, and return
270 non-nil if that buffer should be searched. Have a look at
271 `dabbrev--same-major-mode-p' for an example.
273 The value of `dabbrev-friend-buffer-function' has an effect only if
274 the value of `dabbrev-select-buffers-function' uses it. The function
275 `dabbrev--select-buffers' is one function you can use here.
277 A mode setting this variable should make it buffer local."
278 :type 'function
279 :group 'dabbrev)
281 (defcustom dabbrev-search-these-buffers-only nil
282 "If non-nil, a list of buffers which dabbrev should search.
283 If this variable is non-nil, dabbrev will only look in these buffers.
284 It will not even look in the current buffer if it is not a member of
285 this list."
286 :group 'dabbrev)
288 ;;----------------------------------------------------------------
289 ;; Internal variables
290 ;;----------------------------------------------------------------
292 ;; Last obarray of completions in `dabbrev-completion'
293 (defvar dabbrev--last-obarray nil)
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 ;; The buffer we last did a completion in.
323 (defvar dabbrev--last-completion-buffer nil)
325 ;; If non-nil, a function to use when copying successive words.
326 ;; It should be `upcase' or `downcase'.
327 (defvar dabbrev--last-case-pattern nil)
329 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
330 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
332 ;; The regexp for recognizing a character in an abbreviation.
333 (defvar dabbrev--abbrev-char-regexp nil)
335 ;; The progress reporter for buffer-scanning progress.
336 (defvar dabbrev--progress-reporter nil)
338 ;;----------------------------------------------------------------
339 ;; Macros
340 ;;----------------------------------------------------------------
342 ;;; Get the buffer that mini-buffer was activated from
343 (defsubst dabbrev--minibuffer-origin ()
344 (car (cdr (buffer-list))))
346 ;; Make a list of some of the elements of LIST.
347 ;; Check each element of LIST, storing it temporarily in the
348 ;; variable ELEMENT, and include it in the result
349 ;; if CONDITION evaluates non-nil.
350 (defmacro dabbrev-filter-elements (element list condition)
351 `(let (dabbrev-result dabbrev-tail ,element)
352 (setq dabbrev-tail ,list)
353 (while dabbrev-tail
354 (setq ,element (car dabbrev-tail))
355 (if ,condition
356 (setq dabbrev-result (cons ,element dabbrev-result)))
357 (setq dabbrev-tail (cdr dabbrev-tail)))
358 (nreverse dabbrev-result)))
360 ;;----------------------------------------------------------------
361 ;; Exported functions
362 ;;----------------------------------------------------------------
364 ;;;###autoload (define-key esc-map "/" 'dabbrev-expand)
365 ;;;??? Do we want this?
366 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion)
368 ;;;###autoload
369 (defun dabbrev-completion (&optional arg)
370 "Completion on current word.
371 Like \\[dabbrev-expand] but finds all expansions in the current buffer
372 and presents suggestions for completion.
374 With a prefix argument, it searches all buffers accepted by the
375 function pointed out by `dabbrev-friend-buffer-function' to find the
376 completions.
378 If the prefix argument is 16 (which comes from C-u C-u),
379 then it searches *all* buffers."
380 (interactive "*P")
381 (dabbrev--reset-global-variables)
382 (let* ((dabbrev-check-other-buffers (and arg t))
383 (dabbrev-check-all-buffers
384 (and arg (= (prefix-numeric-value arg) 16)))
385 (abbrev (dabbrev--abbrev-at-point))
386 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
387 case-fold-search
388 dabbrev-case-fold-search)
389 (or (not dabbrev-upcase-means-case-search)
390 (string= abbrev (downcase abbrev)))))
391 (my-obarray dabbrev--last-obarray)
392 init)
393 (save-excursion
394 ;;--------------------------------
395 ;; New abbreviation to expand.
396 ;;--------------------------------
397 (setq dabbrev--last-abbreviation abbrev)
398 ;; Find all expansion
399 (let ((completion-list
400 (dabbrev--find-all-expansions abbrev ignore-case-p))
401 (completion-ignore-case ignore-case-p))
402 ;; Make an obarray with all expansions
403 (setq my-obarray (make-vector (length completion-list) 0))
404 (or (> (length my-obarray) 0)
405 (error "No dynamic expansion for \"%s\" found%s"
406 abbrev
407 (if dabbrev--check-other-buffers "" " in this-buffer")))
408 (cond
409 ((or (not ignore-case-p)
410 (not dabbrev-case-replace))
411 (mapc (function (lambda (string)
412 (intern string my-obarray)))
413 completion-list))
414 ((string= abbrev (upcase abbrev))
415 (mapc (function (lambda (string)
416 (intern (upcase string) my-obarray)))
417 completion-list))
418 ((string= (substring abbrev 0 1)
419 (upcase (substring abbrev 0 1)))
420 (mapc (function (lambda (string)
421 (intern (capitalize string) my-obarray)))
422 completion-list))
424 (mapc (function (lambda (string)
425 (intern (downcase string) my-obarray)))
426 completion-list)))
427 (setq dabbrev--last-obarray my-obarray)
428 (setq dabbrev--last-completion-buffer (current-buffer))
429 ;; Find the longest common string.
430 (setq init (try-completion abbrev my-obarray))))
431 ;;--------------------------------
432 ;; Let the user choose between the expansions
433 ;;--------------------------------
434 (or (stringp init)
435 (setq init abbrev))
436 (cond
437 ;; * Replace string fragment with matched common substring completion.
438 ((and (not (string-equal init ""))
439 (not (string-equal (downcase init) (downcase abbrev))))
440 (if (> (length (all-completions init my-obarray)) 1)
441 (message "Repeat `%s' to see all completions"
442 (key-description (this-command-keys)))
443 (message "The only possible completion"))
444 (dabbrev--substitute-expansion nil abbrev init nil))
446 ;; * String is a common substring completion already. Make list.
447 (message "Making completion list...")
448 (with-output-to-temp-buffer "*Completions*"
449 (display-completion-list (all-completions init my-obarray)
450 init))
451 (message "Making completion list...done")))
452 (and (window-minibuffer-p (selected-window))
453 (message nil))))
455 ;;;###autoload
456 (defun dabbrev-expand (arg)
457 "Expand previous word \"dynamically\".
459 Expands to the most recent, preceding word for which this is a prefix.
460 If no suitable preceding word is found, words following point are
461 considered. If still no suitable word is found, then look in the
462 buffers accepted by the function pointed out by variable
463 `dabbrev-friend-buffer-function'.
465 A positive prefix argument, N, says to take the Nth backward *distinct*
466 possibility. A negative argument says search forward.
468 If the cursor has not moved from the end of the previous expansion and
469 no argument is given, replace the previously-made expansion
470 with the next possible expansion not yet tried.
472 The variable `dabbrev-backward-only' may be used to limit the
473 direction of search to backward if set non-nil.
475 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
476 (interactive "*P")
477 (let (abbrev record-case-pattern
478 expansion old direction (orig-point (point)))
479 ;; abbrev -- the abbrev to expand
480 ;; expansion -- the expansion found (eventually) or nil until then
481 ;; old -- the text currently in the buffer
482 ;; (the abbrev, or the previously-made expansion)
483 (save-excursion
484 (if (and (null arg)
485 (markerp dabbrev--last-abbrev-location)
486 (marker-position dabbrev--last-abbrev-location)
487 (or (eq last-command this-command)
488 (and (window-minibuffer-p (selected-window))
489 (= dabbrev--last-abbrev-location
490 (point)))))
491 ;; Find a different expansion for the same abbrev as last time.
492 (progn
493 (setq abbrev dabbrev--last-abbreviation)
494 (setq old dabbrev--last-expansion)
495 (setq direction dabbrev--last-direction))
496 ;; If the user inserts a space after expanding
497 ;; and then asks to expand again, always fetch the next word.
498 (if (and (eq (preceding-char) ?\s)
499 (markerp dabbrev--last-abbrev-location)
500 (marker-position dabbrev--last-abbrev-location)
501 (= (point) (1+ dabbrev--last-abbrev-location)))
502 (progn
503 ;; The "abbrev" to expand is just the space.
504 (setq abbrev " ")
505 (save-excursion
506 (save-restriction
507 (widen)
508 (if dabbrev--last-buffer
509 (set-buffer dabbrev--last-buffer))
510 ;; Find the end of the last "expansion" word.
511 (if (or (eq dabbrev--last-direction 1)
512 (and (eq dabbrev--last-direction 0)
513 (< dabbrev--last-expansion-location (point))))
514 (setq dabbrev--last-expansion-location
515 (+ dabbrev--last-expansion-location
516 (length dabbrev--last-expansion))))
517 (goto-char dabbrev--last-expansion-location)
518 ;; Take the following word, with intermediate separators,
519 ;; as our expansion this time.
520 (re-search-forward
521 (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
522 (setq expansion (buffer-substring-no-properties
523 dabbrev--last-expansion-location (point)))
525 ;; Record the end of this expansion, in case we repeat this.
526 (setq dabbrev--last-expansion-location (point))))
527 ;; Indicate that dabbrev--last-expansion-location is
528 ;; at the end of the expansion.
529 (setq dabbrev--last-direction -1))
531 ;; We have a different abbrev to expand.
532 (dabbrev--reset-global-variables)
533 (setq direction (if (null arg)
534 (if dabbrev-backward-only 1 0)
535 (prefix-numeric-value arg)))
536 (setq abbrev (dabbrev--abbrev-at-point))
537 (setq record-case-pattern t)
538 (setq old nil)))
540 ;;--------------------------------
541 ;; Find the expansion
542 ;;--------------------------------
543 (or expansion
544 (setq expansion
545 (dabbrev--find-expansion abbrev direction
546 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
547 case-fold-search
548 dabbrev-case-fold-search)
549 (or (not dabbrev-upcase-means-case-search)
550 (string= abbrev (downcase abbrev))))))))
551 (cond
552 ((not expansion)
553 (dabbrev--reset-global-variables)
554 (if old
555 (save-excursion
556 (setq buffer-undo-list (cons orig-point buffer-undo-list))
557 ;; Put back the original abbrev with its original case pattern.
558 (search-backward old)
559 (insert abbrev)
560 (delete-region (point) (+ (point) (length old)))))
561 (error "No%s dynamic expansion for `%s' found"
562 (if old " further" "") abbrev))
564 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
565 (minibuffer-window-active-p (selected-window))))
566 (progn
567 (message "Expansion found in '%s'"
568 (buffer-name dabbrev--last-buffer))
569 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
570 (message nil))
571 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
572 (null dabbrev--last-buffer))
573 (numberp dabbrev--last-expansion-location)
574 (and (> dabbrev--last-expansion-location (point))))
575 (setq dabbrev--last-expansion-location
576 (copy-marker dabbrev--last-expansion-location)))
577 ;; Success: stick it in and return.
578 (setq buffer-undo-list (cons orig-point buffer-undo-list))
579 (dabbrev--substitute-expansion old abbrev expansion
580 record-case-pattern)
582 ;; Save state for re-expand.
583 (setq dabbrev--last-expansion expansion)
584 (setq dabbrev--last-abbreviation abbrev)
585 (setq dabbrev--last-abbrev-location (point-marker))))))
587 ;;----------------------------------------------------------------
588 ;; Local functions
589 ;;----------------------------------------------------------------
591 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
592 (defun dabbrev--same-major-mode-p (other-buffer)
593 (eq major-mode
594 (save-excursion
595 (set-buffer other-buffer)
596 major-mode)))
598 ;;; Back over all abbrev type characters and then moves forward over
599 ;;; all skip characters.
600 (defun dabbrev--goto-start-of-abbrev ()
601 ;; Move backwards over abbrev chars
602 (save-match-data
603 (when (> (point) (minibuffer-prompt-end))
604 (forward-char -1)
605 (while (and (looking-at dabbrev--abbrev-char-regexp)
606 (> (point) (minibuffer-prompt-end))
607 (not (= (point) (field-beginning (point) nil
608 (1- (point))))))
609 (forward-char -1))
610 (or (looking-at dabbrev--abbrev-char-regexp)
611 (forward-char 1)))
612 (and dabbrev-abbrev-skip-leading-regexp
613 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
614 (forward-char 1)))))
616 ;;; Extract the symbol at point to serve as abbreviation.
617 (defun dabbrev--abbrev-at-point ()
618 ;; Check for error
619 (if (bobp)
620 (error "No possible abbreviation preceding point"))
621 ;; Return abbrev at point
622 (save-excursion
623 ;; Record the end of the abbreviation.
624 (setq dabbrev--last-abbrev-location (point))
625 ;; If we aren't right after an abbreviation,
626 ;; move point back to just after one.
627 ;; This is so the user can get successive words
628 ;; by typing the punctuation followed by M-/.
629 (save-match-data
630 (if (save-excursion
631 (forward-char -1)
632 (not (looking-at (concat "\\("
633 (or dabbrev-abbrev-char-regexp
634 "\\sw\\|\\s_")
635 "\\)+"))))
636 (if (re-search-backward (or dabbrev-abbrev-char-regexp
637 "\\sw\\|\\s_")
638 nil t)
639 (forward-char 1)
640 (error "No possible abbreviation preceding point"))))
641 ;; Now find the beginning of that one.
642 (dabbrev--goto-start-of-abbrev)
643 (buffer-substring-no-properties
644 dabbrev--last-abbrev-location (point))))
646 ;;; Initializes all global variables
647 (defun dabbrev--reset-global-variables ()
648 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
649 ;; must not be reset here.
650 (setq dabbrev--last-table nil
651 dabbrev--last-abbreviation nil
652 dabbrev--last-abbrev-location nil
653 dabbrev--last-direction nil
654 dabbrev--last-expansion nil
655 dabbrev--last-expansion-location nil
656 dabbrev--friend-buffer-list nil
657 dabbrev--last-buffer nil
658 dabbrev--last-buffer-found nil
659 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
660 "\\sw\\|\\s_")
661 dabbrev--check-other-buffers dabbrev-check-other-buffers))
663 (defun dabbrev--select-buffers ()
664 "Return a list of other buffers to search for a possible abbrev.
665 The current buffer is not included in the list.
667 This function makes a list of all the buffers returned by `buffer-list',
668 then discards buffers whose names match `dabbrev-ignored-buffer-names'
669 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
670 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
671 with the buffer as argument.
672 It returns the list of the buffers that are not discarded."
673 (dabbrev-filter-elements
674 buffer (buffer-list)
675 (and (not (eq (current-buffer) buffer))
676 (not (dabbrev--ignore-buffer-p buffer))
677 (boundp 'dabbrev-friend-buffer-function)
678 (funcall dabbrev-friend-buffer-function buffer))))
680 (defun dabbrev--try-find (abbrev reverse n ignore-case)
681 "Search for ABBREV, backwards if REVERSE, N times.
682 If IGNORE-CASE is non-nil, ignore case while searching.
683 Return the expansion found, and save the location of the start
684 of the expansion in `dabbrev--last-expansion-location'."
685 (save-excursion
686 (save-restriction
687 (widen)
688 (let ((expansion nil))
689 (and dabbrev--last-expansion-location
690 (goto-char dabbrev--last-expansion-location))
691 (let ((case-fold-search ignore-case)
692 (count n))
693 (while (and (> count 0)
694 (setq expansion (dabbrev--search abbrev
695 reverse
696 (and ignore-case
697 (if (eq dabbrev-case-distinction 'case-replace)
698 case-replace
699 dabbrev-case-distinction))
701 (setq count (1- count))))
702 (and expansion
703 (setq dabbrev--last-expansion-location (point)))
704 expansion))))
706 (defun dabbrev--find-all-expansions (abbrev ignore-case)
707 "Return a list of all possible expansions of ABBREV.
708 If IGNORE-CASE is non-nil, accept matches which differ in case."
709 (let ((all-expansions nil)
710 expansion)
711 (save-excursion
712 (goto-char (point-min))
713 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
714 (setq all-expansions (cons expansion all-expansions))))
715 all-expansions))
717 (defun dabbrev--ignore-buffer-p (buffer)
718 "Return non-nil if BUFFER should be ignored by dabbrev."
719 (let ((bn (buffer-name buffer)))
720 (or (member bn dabbrev-ignored-buffer-names)
721 (let ((tail dabbrev-ignored-buffer-regexps)
722 (match nil))
723 (while (and tail (not match))
724 (setq match (string-match (car tail) bn)
725 tail (cdr tail)))
726 match))))
728 (defun dabbrev--find-expansion (abbrev direction ignore-case)
729 "Find one occurrence of ABBREV, and return the expansion.
730 DIRECTION > 0 means look that many times backwards.
731 DIRECTION < 0 means look that many times forward.
732 DIRECTION = 0 means try both backward and forward.
733 IGNORE-CASE non-nil means ignore case when searching.
734 This sets `dabbrev--last-direction' to 1 or -1 according
735 to the direction in which the occurrence was actually found.
736 It sets `dabbrev--last-expansion-location' to the location
737 of the start of the occurrence."
738 (save-excursion
739 ;; If we were scanning something other than the current buffer,
740 ;; continue scanning there.
741 (when dabbrev--last-buffer
742 (set-buffer dabbrev--last-buffer))
744 ;; ------------------------------------------
745 ;; Look backward in current buffer.
746 ;; ------------------------------------------
747 (and (not dabbrev-search-these-buffers-only)
748 (>= direction 0)
749 (setq dabbrev--last-direction (min 1 direction))
750 (dabbrev--try-find abbrev t
751 (max 1 direction)
752 ignore-case))
753 ;; ------------------------------------------
754 ;; Look forward in current buffer
755 ;; or whatever buffer we were last scanning.
756 ;; ------------------------------------------
757 (and (or (not dabbrev-search-these-buffers-only)
758 dabbrev--last-buffer)
759 (<= direction 0)
760 (setq dabbrev--last-direction -1)
761 (dabbrev--try-find abbrev nil
762 (max 1 (- direction))
763 ignore-case))
764 ;; ------------------------------------------
765 ;; Look in other buffers.
766 ;; Always start at (point-min) and look forward.
767 ;; ------------------------------------------
768 (progn
769 (setq dabbrev--last-direction -1)
770 (unless dabbrev--last-buffer
771 ;; If we have just now begun to search other buffers,
772 ;; determine which other buffers we should check.
773 ;; Put that list in dabbrev--friend-buffer-list.
774 (unless dabbrev--friend-buffer-list
775 (setq dabbrev--friend-buffer-list
776 (dabbrev--make-friend-buffer-list))
777 (setq dabbrev--progress-reporter
778 (make-progress-reporter
779 "Scanning for dabbrevs..."
780 (- (length dabbrev--friend-buffer-list)) 0 0 1 1.5))))
781 ;; Walk through the buffers till we find a match.
782 (let (expansion)
783 (while (and (not expansion) dabbrev--friend-buffer-list)
784 (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
785 (set-buffer dabbrev--last-buffer)
786 (progress-reporter-update dabbrev--progress-reporter
787 (- (length dabbrev--friend-buffer-list)))
788 (setq dabbrev--last-expansion-location (point-min))
789 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
790 expansion)))))
792 ;; Compute the list of buffers to scan.
793 ;; If dabbrev-search-these-buffers-only, then the current buffer
794 ;; is included in this list if it should be searched.
795 ;; Otherwise, the current buffer is searched first specially.,
796 ;; and it is not included in this list.
797 (defun dabbrev--make-friend-buffer-list ()
798 (let ((list (mapcar (function get-buffer)
799 dabbrev-search-these-buffers-only)))
800 (when (and (null dabbrev-search-these-buffers-only)
801 dabbrev--check-other-buffers
802 (or (eq dabbrev--check-other-buffers t)
803 (setq dabbrev--check-other-buffers
804 (y-or-n-p "Scan other buffers also? "))))
805 (setq list (funcall dabbrev-select-buffers-function))
806 ;; If dabbrev-check-all-buffers, tack on all the other
807 ;; buffers at the end of the list, except those which are
808 ;; specifically to be ignored.
809 (if dabbrev-check-all-buffers
810 (setq list
811 (append list
812 (dabbrev-filter-elements
813 buffer (buffer-list)
814 (and (not (memq buffer list))
815 (not (dabbrev--ignore-buffer-p buffer)))))))
816 ;; Remove the current buffer.
817 (setq list (delq (current-buffer) list)))
818 ;; Move buffers in the list that are visible on the screen
819 ;; to the front of the list, but don't add anything to the list.
820 (if list
821 (walk-windows (lambda (w)
822 (unless (eq w (selected-window))
823 (if (memq (window-buffer w) list)
824 (setq list
825 (cons (window-buffer w)
826 (delq (window-buffer w)
827 list))))))))
828 ;; In a minibuffer, search the buffer it was activated from,
829 ;; first after the minibuffer itself. Unless we aren't supposed
830 ;; to search the current buffer either.
831 (if (and (window-minibuffer-p (selected-window))
832 (not dabbrev-search-these-buffers-only))
833 (setq list
834 (cons (dabbrev--minibuffer-origin)
835 (delq (dabbrev--minibuffer-origin) list))))
836 list))
838 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
839 (if (eq major-mode 'picture-mode)
840 (with-no-warnings
841 (picture-replace-match string fixedcase literal))
842 (replace-match string fixedcase literal)))
844 ;;;----------------------------------------------------------------
845 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
846 "Replace OLD with EXPANSION in the buffer.
847 OLD is text currently in the buffer, perhaps the abbreviation
848 or perhaps another expansion that was tried previously.
849 ABBREV is the abbreviation we are expanding.
850 It is \" \" if we are copying subsequent words.
851 EXPANSION is the expansion substring to be used this time.
852 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
853 to record whether we upcased the expansion, downcased it, or did neither."
854 ;;(undo-boundary)
855 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
856 case-fold-search
857 dabbrev-case-fold-search)
858 (or (not dabbrev-upcase-means-case-search)
859 (string= abbrev (downcase abbrev)))
860 (if (eq dabbrev-case-replace 'case-replace)
861 case-replace
862 dabbrev-case-replace))))
864 ;; If we upcased or downcased the original expansion,
865 ;; do likewise for the subsequent words when we copy them.
866 ;; Don't do any of the usual case processing, though.
867 (when (equal abbrev " ")
868 (if dabbrev--last-case-pattern
869 (setq expansion
870 (funcall dabbrev--last-case-pattern expansion)))
871 (setq use-case-replace nil))
873 ;; If the expansion has mixed case
874 ;; and it is not simply a capitalized word,
875 ;; or if the abbrev has mixed case,
876 ;; and if the given abbrev's case pattern
877 ;; matches the start of the expansion,
878 ;; copy the expansion's case
879 ;; instead of downcasing all the rest.
881 ;; Treat a one-capital-letter (possibly with preceding non-letter
882 ;; characters) abbrev as "not all upper case", so as to force
883 ;; preservation of the expansion's pattern if the expansion starts
884 ;; with a capital letter.
885 (let ((expansion-rest (substring expansion 1))
886 (first-letter-position (string-match "[[:alpha:]]" abbrev)))
887 (if (or (null first-letter-position)
888 (and (not (and (or (string= expansion-rest (downcase expansion-rest))
889 (string= expansion-rest (upcase expansion-rest)))
890 (or (string= abbrev (downcase abbrev))
891 (and (string= abbrev (upcase abbrev))
892 (> (- (length abbrev) first-letter-position)
893 1)))))
894 (string= abbrev
895 (substring expansion 0 (length abbrev)))))
896 (setq use-case-replace nil)))
898 ;; If the abbrev and the expansion are both all-lower-case
899 ;; then don't do any conversion. The conversion would be a no-op
900 ;; for this replacement, but it would carry forward to subsequent words.
901 ;; The goal of this is to prevent that carrying forward.
902 (if (and (string= expansion (downcase expansion))
903 (string= abbrev (downcase abbrev)))
904 (setq use-case-replace nil))
906 (if use-case-replace
907 (setq expansion (downcase expansion)))
909 ;; In case we insert subsequent words,
910 ;; record if we upcased or downcased the first word,
911 ;; in order to do likewise for subsequent words.
912 (and record-case-pattern
913 (setq dabbrev--last-case-pattern
914 (and use-case-replace
915 (cond ((equal abbrev (upcase abbrev)) 'upcase)
916 ((equal abbrev (downcase abbrev)) 'downcase)))))
918 ;; Convert whitespace to single spaces.
919 (if dabbrev-eliminate-newlines
920 (let ((pos
921 (if (equal abbrev " ") 0 (length abbrev))))
922 ;; If ABBREV is real, search after the end of it.
923 ;; If ABBREV is space and we are copying successive words,
924 ;; search starting at the front.
925 (while (string-match "[\n \t]+" expansion pos)
926 (setq pos (1+ (match-beginning 0)))
927 (setq expansion (replace-match " " nil nil expansion)))))
929 (if old
930 (save-excursion
931 (search-backward old))
932 ;;(set-match-data (list (point-marker) (point-marker)))
933 (search-backward abbrev)
934 (search-forward abbrev))
936 ;; Make case of replacement conform to case of abbreviation
937 ;; provided (1) that kind of thing is enabled in this buffer
938 ;; and (2) the replacement itself is all lower case.
939 (dabbrev--safe-replace-match expansion
940 (not use-case-replace)
941 t)))
944 ;;;----------------------------------------------------------------
945 ;;; Search function used by dabbrevs library.
948 (defun dabbrev--search (abbrev reverse ignore-case)
949 "Search for something that could be used to expand ABBREV.
951 Second arg, REVERSE, is t for reverse search, nil for forward.
952 The variable `dabbrev-limit' controls the maximum search region size.
953 Third argument IGNORE-CASE non-nil means treat case as insignificant while
954 looking for a match and when comparing with previous matches. Also if
955 that's non-nil and the match is found at the beginning of a sentence
956 and is in lower case except for the initial then it is converted to
957 all lower case for return.
959 Table of expansions already seen is examined in buffer
960 `dabbrev--last-table' so that only distinct possibilities are found
961 by dabbrev-re-expand.
963 Returns the expansion found, or nil if not found.
964 Leaves point at the location of the start of the expansion."
965 (save-match-data
966 (let ((pattern1 (concat (regexp-quote abbrev)
967 "\\(" dabbrev--abbrev-char-regexp "\\)"))
968 (pattern2 (concat (regexp-quote abbrev)
969 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
970 ;; This makes it possible to find matches in minibuffer prompts
971 ;; even when they are "inviolable".
972 (inhibit-point-motion-hooks t)
973 found-string result)
974 ;; Limited search.
975 (save-restriction
976 (and dabbrev-limit
977 (narrow-to-region dabbrev--last-expansion-location
978 (+ (point)
979 (if reverse (- dabbrev-limit) dabbrev-limit))))
980 ;;--------------------------------
981 ;; Look for a distinct expansion, using dabbrev--last-table.
982 ;;--------------------------------
983 (while (and (not found-string)
984 (if reverse
985 (re-search-backward pattern1 nil t)
986 (re-search-forward pattern1 nil t)))
987 (goto-char (match-beginning 0))
988 ;; In case we matched in the middle of a word,
989 ;; back up to start of word and verify we still match.
990 (dabbrev--goto-start-of-abbrev)
992 (if (not (looking-at pattern1))
994 ;; We have a truly valid match. Find the end.
995 (re-search-forward pattern2)
996 (setq found-string (match-string-no-properties 0))
997 (setq result found-string)
998 (and ignore-case (setq found-string (downcase found-string)))
999 ;; Ignore this match if it's already in the table.
1000 (if (dabbrev-filter-elements
1001 table-string dabbrev--last-table
1002 (string= found-string table-string))
1003 (setq found-string nil)))
1004 ;; Prepare to continue searching.
1005 (goto-char (if reverse (match-beginning 0) (match-end 0))))
1006 ;; If we found something, use it.
1007 (when found-string
1008 ;; Put it into `dabbrev--last-table'
1009 ;; and return it (either downcased, or as is).
1010 (setq dabbrev--last-table
1011 (cons found-string dabbrev--last-table))
1012 result)))))
1014 (dolist (mess '("^No dynamic expansion for .* found$"
1015 "^No further dynamic expansion for .* found$"
1016 "^No possible abbreviation preceding point$"))
1017 (add-to-list 'debug-ignored-errors mess))
1019 (provide 'dabbrev)
1021 ;;; arch-tag: 29e58596-f080-4306-a409-70296cf9d46f
1022 ;;; dabbrev.el ends here