(easy-mmode-pretty-mode-name): Prettier.
[emacs.git] / lisp / dabbrev.el
blob542f657fceb0b4ccc237f3e99457317b369f7f63
1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 86, 92, 94, 96, 1997, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Don Morrison
7 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
8 ;; Created: 16 Mars 1992
9 ;; Lindberg's last update version: 5.7
10 ;; Keywords: abbrev expand completion convenience
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
29 ;;; Commentary:
31 ;; The purpose with this package is to let you write just a few
32 ;; characters of words you've written earlier to be able to expand
33 ;; them.
35 ;; To expand a word, just put the point right after the word and press
36 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
38 ;; Check out the customizable variables below to learn about all the
39 ;; features of this package.
41 ;;; Hints and tips for major modes writers:
43 ;; Recommended values C/Lisp etc text
44 ;; dabbrev-case-fold-search nil t
45 ;; dabbrev-case-replace nil t
47 ;; Set the variables you want special for your mode like this:
48 ;; (set (make-local-variable 'dabbrev-case-replace) nil)
49 ;; Then you don't interfere with other modes.
51 ;; If your mode handles buffers that refers to other buffers
52 ;; (i.e. compilation-mode, gud-mode), then try to set
53 ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
54 ;; to a function that point out those buffers.
56 ;; Same goes for major-modes that are connected to other modes. There
57 ;; are for instance a number of mail-modes. One for reading, one for
58 ;; creating a new mail etc. Maybe those should be connected.
60 ;; Example for GNUS (when we write a reply, we want dabbrev to look in
61 ;; the article for expansion):
62 ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
63 ;; (lambda (buffer)
64 ;; (save-excursion
65 ;; (set-buffer buffer)
66 ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
69 ;; Known bugs and limitations.
70 ;; - Possible to do several levels of `dabbrev-completion' in the
71 ;; minibuffer.
72 ;; - dabbrev-completion doesn't handle resetting the globals variables
73 ;; right. It resets them after finding the abbrev.
75 ;; Future enhancements
76 ;; - Check the tags-files? Like tags-complete?
77 ;; - Add the possibility of searching both forward and backward to
78 ;; the nearest expansion.
79 ;; - Check the kill-ring when everything else fails. (Maybe something
80 ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
82 ;;; These people gave suggestions:
83 ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
84 ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
85 ;; [jules] Julian Gosnell <jules@x.co.uk>
86 ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
87 ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
88 ;; [alon] Alon Albert <al%imercury@uunet.uu.net>
89 ;; [tromey] Tom Tromey <tromey@busco.lanl.gov>
90 ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
91 ;; [Petri] Petri Raitio <per@tekla.fi>
92 ;; [ejb] Jay Berkenbilt <ejb@ql.org>
93 ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
94 ;; ... and to all the people who have participated in the beta tests.
96 ;;; Code:
98 ;;----------------------------------------------------------------
99 ;; Customization variables
100 ;;----------------------------------------------------------------
102 (defgroup dabbrev nil
103 "Dynamic Abbreviations"
104 :tag "Dynamic Abbreviations"
105 :group 'abbrev
106 :group 'convenience)
108 (defcustom dabbrev-backward-only nil
109 "*If non-nil, `dabbrev-expand' only looks backwards."
110 :type 'boolean
111 :group 'dabbrev)
113 (defcustom dabbrev-limit nil
114 "*Limits region searched by `dabbrev-expand' to this many chars away."
115 :type '(choice (const :tag "off" nil)
116 integer)
117 :group 'dabbrev)
119 (defcustom dabbrev-abbrev-skip-leading-regexp nil
120 "*Regexp for skipping leading characters of an abbreviation.
122 Example: Set this to \"\\\\$\" for programming languages
123 in which variable names may appear with or without a leading `$'.
124 \(For example, in Makefiles.\)
126 Set this to nil if no characters should be skipped."
127 :type '(choice regexp
128 (const :tag "off" nil))
129 :group 'dabbrev)
131 (defcustom dabbrev-case-fold-search 'case-fold-search
132 "*Control whether dabbrev searches should ignore case.
133 A value of nil means case is significant.
134 A value of `case-fold-search' means case is significant
135 if `case-fold-search' is nil.
136 Any other non-nil version means case is not significant."
137 :type '(choice (const :tag "off" nil)
138 (const :tag "like search" case-fold-search)
139 (other :tag "on" t))
140 :group 'dabbrev)
142 (defcustom dabbrev-upcase-means-case-search nil
143 "*The significance of an uppercase character in an abbreviation.
144 nil means case fold search, non-nil means case sensitive search.
146 This variable has an effect only when the value of
147 `dabbrev-case-fold-search' says to ignore case."
148 :type 'boolean
149 :group 'dabbrev)
151 (defcustom dabbrev-case-replace 'case-replace
152 "*Controls whether dabbrev preserves case when expanding the abbreviation.
153 A value of nil means preserve case.
154 A value of `case-replace' means preserve case if `case-replace' is nil.
155 Any other non-nil version means do not preserve case.
157 This variable has an effect only when the value of
158 `dabbrev-case-fold-search' specifies to ignore case."
159 :type '(choice (const :tag "off" nil)
160 (const :tag "like M-x query-replace" case-replace)
161 (other :tag "on" t))
162 :group 'dabbrev)
164 (defcustom dabbrev-abbrev-char-regexp nil
165 "*Regexp to recognize a character in an abbreviation or expansion.
166 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
168 Set this variable to \"\\\\sw\" if you want ordinary words or
169 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
170 syntax is \"symbol\" as well as those whose syntax is \"word\".
172 The value nil has a special meaning: the abbreviation is from point to
173 previous word-start, but the search is for symbols.
175 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
176 while `yes', `or', `no' and `p' are considered words. If this
177 variable is nil, then expanding `yes-or-no-' looks for a symbol
178 starting with or containing `no-'. If you set this variable to
179 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
180 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
181 expanding `yes-or-no-' signals an error because `-' is not part of a word;
182 but expanding `yes-or-no' looks for a word starting with `no'.
184 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
185 :type '(choice (const nil)
186 regexp)
187 :group 'dabbrev)
189 (defcustom dabbrev-check-all-buffers t
190 "*Non-nil means dabbrev package should search *all* buffers.
192 Dabbrev always searches the current buffer first. Then, if
193 `dabbrev-check-other-buffers' says so, it searches the buffers
194 designated by `dabbrev-select-buffers-function'.
196 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
197 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
198 or matched by `dabbrev-ignored-regexps'."
199 :type 'boolean
200 :group 'dabbrev)
202 (defcustom dabbrev-ignored-buffer-names '("*Messages*" "*Buffer List*")
203 "*List of buffer names that dabbrev should not check.
204 See also `dabbrev-ignored-regexps'."
205 :type '(repeat (string :tag "Buffer name"))
206 :group 'dabbrev
207 :version "20.3")
209 (defcustom dabbrev-ignored-regexps nil
210 "*List of regexps matching names of buffers that dabbrev should not check.
211 See also `dabbrev-ignored-buffer-names'."
212 :type '(repeat regexp)
213 :group 'dabbrev
214 :version "21.1")
216 (defcustom dabbrev-check-other-buffers t
217 "*Should \\[dabbrev-expand] look in other buffers?\
219 nil: Don't look in other buffers.
220 t: Also look for expansions in the buffers pointed out by
221 `dabbrev-select-buffers-function'.
222 Anything else: When we can't find any more expansions in
223 the current buffer, then ask the user whether to look in other
224 buffers too.
226 The default value is t."
227 :type '(choice (const :tag "off" nil)
228 (const :tag "on" t)
229 (other :tag "ask" other))
230 :group 'dabbrev)
232 ;; I guess setting this to a function that selects all C- or C++-
233 ;; mode buffers would be a good choice for a debugging buffer,
234 ;; when debugging C- or C++-code.
235 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
236 "A function that selects buffers that should be searched by dabbrev.
237 The function should take no arguments and return a list of buffers to
238 search for expansions. Have a look at `dabbrev--select-buffers' for
239 an example.
241 A mode setting this variable should make it buffer local.")
243 (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
244 "*A function to decide whether dabbrev should search OTHER-BUFFER.
245 The function should take one argument, OTHER-BUFFER, and return
246 non-nil if that buffer should be searched. Have a look at
247 `dabbrev--same-major-mode-p' for an example.
249 The value of `dabbrev-friend-buffer-function' has an effect only if
250 the value of `dabbrev-select-buffers-function' uses it. The function
251 `dabbrev--select-buffers' is one function you can use here.
253 A mode setting this variable should make it buffer local."
254 :type 'function
255 :group 'dabbrev)
257 (defcustom dabbrev-search-these-buffers-only nil
258 "If non-nil, a list of buffers which dabbrev should search.
259 If this variable is non-nil, dabbrev will only look in these buffers.
260 It will not even look in the current buffer if it is not a member of
261 this list.")
263 ;;----------------------------------------------------------------
264 ;; Internal variables
265 ;;----------------------------------------------------------------
267 ;; Last obarray of completions in `dabbrev-completion'
268 (defvar dabbrev--last-obarray nil)
270 ;; Table of expansions seen so far
271 (defvar dabbrev--last-table nil)
273 ;; Last string we tried to expand.
274 (defvar dabbrev--last-abbreviation nil)
276 ;; Location last abbreviation began
277 (defvar dabbrev--last-abbrev-location nil)
279 ;; Direction of last dabbrevs search
280 (defvar dabbrev--last-direction 0)
282 ;; Last expansion of an abbreviation.
283 (defvar dabbrev--last-expansion nil)
285 ;; Location the last expansion was found.
286 (defvar dabbrev--last-expansion-location nil)
288 ;; The list of remaining buffers with the same mode as current buffer.
289 (defvar dabbrev--friend-buffer-list nil)
291 ;; The buffer we looked in last.
292 (defvar dabbrev--last-buffer nil)
294 ;; The buffer we found the expansion last time.
295 (defvar dabbrev--last-buffer-found nil)
297 ;; The buffer we last did a completion in.
298 (defvar dabbrev--last-completion-buffer nil)
300 ;; Non-nil means we should upcase
301 ;; when copying successive words.
302 (defvar dabbrev--last-case-pattern nil)
304 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
305 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
307 ;; The regexp for recognizing a character in an abbreviation.
308 (defvar dabbrev--abbrev-char-regexp nil)
310 ;;----------------------------------------------------------------
311 ;; Macros
312 ;;----------------------------------------------------------------
314 ;;; Get the buffer that mini-buffer was activated from
315 (defsubst dabbrev--minibuffer-origin ()
316 (car (cdr (buffer-list))))
318 ;; Make a list of some of the elements of LIST.
319 ;; Check each element of LIST, storing it temporarily in the
320 ;; variable ELEMENT, and include it in the result
321 ;; if CONDITION evaluates non-nil.
322 (defmacro dabbrev-filter-elements (element list condition)
323 `(let (dabbrev-result dabbrev-tail ,element)
324 (setq dabbrev-tail ,list)
325 (while dabbrev-tail
326 (setq ,element (car dabbrev-tail))
327 (if ,condition
328 (setq dabbrev-result (cons ,element dabbrev-result)))
329 (setq dabbrev-tail (cdr dabbrev-tail)))
330 (nreverse dabbrev-result)))
332 ;;----------------------------------------------------------------
333 ;; Exported functions
334 ;;----------------------------------------------------------------
336 ;;;###autoload
337 (define-key esc-map "/" 'dabbrev-expand)
338 ;;;??? Do we want this?
339 ;;;###autoload
340 (define-key esc-map [?\C-/] 'dabbrev-completion)
342 ;;;###autoload
343 (defun dabbrev-completion (&optional arg)
344 "Completion on current word.
345 Like \\[dabbrev-expand] but finds all expansions in the current buffer
346 and presents suggestions for completion.
348 With a prefix argument, it searches all buffers accepted by the
349 function pointed out by `dabbrev-friend-buffer-function' to find the
350 completions.
352 If the prefix argument is 16 (which comes from C-u C-u),
353 then it searches *all* buffers.
355 With no prefix argument, it reuses an old completion list
356 if there is a suitable one already."
358 (interactive "*P")
359 (dabbrev--reset-global-variables)
360 (let* ((dabbrev-check-other-buffers (and arg t))
361 (dabbrev-check-all-buffers
362 (and arg (= (prefix-numeric-value arg) 16)))
363 (abbrev (dabbrev--abbrev-at-point))
364 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
365 case-fold-search
366 dabbrev-case-fold-search)
367 (or (not dabbrev-upcase-means-case-search)
368 (string= abbrev (downcase abbrev)))))
369 (my-obarray dabbrev--last-obarray)
370 init)
371 (save-excursion
372 (if (and (null arg)
373 my-obarray
374 (or (eq dabbrev--last-completion-buffer (current-buffer))
375 (and (window-minibuffer-p (selected-window))
376 (eq dabbrev--last-completion-buffer
377 (dabbrev--minibuffer-origin))))
378 dabbrev--last-abbreviation
379 (>= (length abbrev) (length dabbrev--last-abbreviation))
380 (string= dabbrev--last-abbreviation
381 (substring abbrev 0
382 (length dabbrev--last-abbreviation)))
383 (setq init (try-completion abbrev my-obarray)))
384 ;; We can reuse the existing completion list.
386 ;;--------------------------------
387 ;; New abbreviation to expand.
388 ;;--------------------------------
389 (setq dabbrev--last-abbreviation abbrev)
390 ;; Find all expansion
391 (let ((completion-list
392 (dabbrev--find-all-expansions abbrev ignore-case-p))
393 (completion-ignore-case ignore-case-p))
394 ;; Make an obarray with all expansions
395 (setq my-obarray (make-vector (length completion-list) 0))
396 (or (> (length my-obarray) 0)
397 (error "No dynamic expansion for \"%s\" found%s"
398 abbrev
399 (if dabbrev--check-other-buffers "" " in this-buffer")))
400 (cond
401 ((or (not ignore-case-p)
402 (not dabbrev-case-replace))
403 (mapc (function (lambda (string)
404 (intern string my-obarray)))
405 completion-list))
406 ((string= abbrev (upcase abbrev))
407 (mapc (function (lambda (string)
408 (intern (upcase string) my-obarray)))
409 completion-list))
410 ((string= (substring abbrev 0 1)
411 (upcase (substring abbrev 0 1)))
412 (mapc (function (lambda (string)
413 (intern (capitalize string) my-obarray)))
414 completion-list))
416 (mapc (function (lambda (string)
417 (intern (downcase string) my-obarray)))
418 completion-list)))
419 (setq dabbrev--last-obarray my-obarray)
420 (setq dabbrev--last-completion-buffer (current-buffer))
421 ;; Find the longest common string.
422 (setq init (try-completion abbrev my-obarray)))))
423 ;;--------------------------------
424 ;; Let the user choose between the expansions
425 ;;--------------------------------
426 (or (stringp init)
427 (setq init abbrev))
428 (cond
429 ;; * Replace string fragment with matched common substring completion.
430 ((and (not (string-equal init ""))
431 (not (string-equal (downcase init) (downcase abbrev))))
432 (if (> (length (all-completions init my-obarray)) 1)
433 (message "Repeat `%s' to see all completions"
434 (key-description (this-command-keys)))
435 (message "The only possible completion"))
436 (dabbrev--substitute-expansion nil abbrev init))
438 ;; * String is a common substring completion already. Make list.
439 (message "Making completion list...")
440 (with-output-to-temp-buffer " *Completions*"
441 (display-completion-list (all-completions init my-obarray)))
442 (message "Making completion list...done")))
443 (and (window-minibuffer-p (selected-window))
444 (message nil))))
446 ;;;###autoload
447 (defun dabbrev-expand (arg)
448 "Expand previous word \"dynamically\".
450 Expands to the most recent, preceding word for which this is a prefix.
451 If no suitable preceding word is found, words following point are
452 considered. If still no suitable word is found, then look in the
453 buffers accepted by the function pointed out by variable
454 `dabbrev-friend-buffer-function'.
456 A positive prefix argument, N, says to take the Nth backward *distinct*
457 possibility. A negative argument says search forward.
459 If the cursor has not moved from the end of the previous expansion and
460 no argument is given, replace the previously-made expansion
461 with the next possible expansion not yet tried.
463 The variable `dabbrev-backward-only' may be used to limit the
464 direction of search to backward if set non-nil.
466 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
467 (interactive "*P")
468 (let (abbrev record-case-pattern
469 expansion old direction (orig-point (point)))
470 ;; abbrev -- the abbrev to expand
471 ;; expansion -- the expansion found (eventually) or nil until then
472 ;; old -- the text currently in the buffer
473 ;; (the abbrev, or the previously-made expansion)
474 (save-excursion
475 (if (and (null arg)
476 (markerp dabbrev--last-abbrev-location)
477 (marker-position dabbrev--last-abbrev-location)
478 (or (eq last-command this-command)
479 (and (window-minibuffer-p (selected-window))
480 (= dabbrev--last-abbrev-location
481 (point)))))
482 ;; Find a different expansion for the same abbrev as last time.
483 (progn
484 (setq abbrev dabbrev--last-abbreviation)
485 (setq old dabbrev--last-expansion)
486 (setq direction dabbrev--last-direction))
487 ;; If the user inserts a space after expanding
488 ;; and then asks to expand again, always fetch the next word.
489 (if (and (eq (preceding-char) ?\ )
490 (markerp dabbrev--last-abbrev-location)
491 (marker-position dabbrev--last-abbrev-location)
492 (= (point) (1+ dabbrev--last-abbrev-location)))
493 (progn
494 ;; The "abbrev" to expand is just the space.
495 (setq abbrev " ")
496 (save-excursion
497 (if dabbrev--last-buffer
498 (set-buffer dabbrev--last-buffer))
499 ;; Find the end of the last "expansion" word.
500 (if (or (eq dabbrev--last-direction 1)
501 (and (eq dabbrev--last-direction 0)
502 (< dabbrev--last-expansion-location (point))))
503 (setq dabbrev--last-expansion-location
504 (+ dabbrev--last-expansion-location
505 (length dabbrev--last-expansion))))
506 (goto-char dabbrev--last-expansion-location)
507 ;; Take the following word, with intermediate separators,
508 ;; as our expansion this time.
509 (re-search-forward
510 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
511 (setq expansion (buffer-substring-no-properties
512 dabbrev--last-expansion-location (point)))
513 (if dabbrev--last-case-pattern
514 (setq expansion (upcase expansion)))
516 ;; Record the end of this expansion, in case we repeat this.
517 (setq dabbrev--last-expansion-location (point)))
518 ;; Indicate that dabbrev--last-expansion-location is
519 ;; at the end of the expansion.
520 (setq dabbrev--last-direction -1))
522 ;; We have a different abbrev to expand.
523 (dabbrev--reset-global-variables)
524 (setq direction (if (null arg)
525 (if dabbrev-backward-only 1 0)
526 (prefix-numeric-value arg)))
527 (setq abbrev (dabbrev--abbrev-at-point))
528 (setq record-case-pattern t)
529 (setq old nil)))
531 ;;--------------------------------
532 ;; Find the expansion
533 ;;--------------------------------
534 (or expansion
535 (setq expansion
536 (dabbrev--find-expansion abbrev direction
537 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
538 case-fold-search
539 dabbrev-case-fold-search)
540 (or (not dabbrev-upcase-means-case-search)
541 (string= abbrev (downcase abbrev))))))))
542 (cond
543 ((not expansion)
544 (dabbrev--reset-global-variables)
545 (if old
546 (save-excursion
547 (setq buffer-undo-list (cons orig-point buffer-undo-list))
548 ;; Put back the original abbrev with its original case pattern.
549 (search-backward old)
550 (insert abbrev)
551 (delete-region (point) (+ (point) (length old)))))
552 (error "No%s dynamic expansion for `%s' found"
553 (if old " further" "") abbrev))
555 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
556 (minibuffer-window-active-p (selected-window))))
557 (progn
558 (message "Expansion found in '%s'"
559 (buffer-name dabbrev--last-buffer))
560 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
561 (message nil))
562 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
563 (null dabbrev--last-buffer))
564 (numberp dabbrev--last-expansion-location)
565 (and (> dabbrev--last-expansion-location (point))))
566 (setq dabbrev--last-expansion-location
567 (copy-marker dabbrev--last-expansion-location)))
568 ;; Success: stick it in and return.
569 (setq buffer-undo-list (cons orig-point buffer-undo-list))
570 (dabbrev--substitute-expansion old abbrev expansion)
572 ;; If we are not copying successive words now,
573 ;; set dabbrev--last-case-pattern.
574 (and record-case-pattern
575 (setq dabbrev--last-case-pattern
576 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
577 case-fold-search
578 dabbrev-case-fold-search)
579 (not dabbrev-upcase-means-case-search)
580 (equal abbrev (upcase abbrev)))))
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 (if (not (bobp))
604 (progn
605 (forward-char -1)
606 (while (and (looking-at dabbrev--abbrev-char-regexp)
607 (not (bobp)))
608 (forward-char -1))
609 (or (looking-at dabbrev--abbrev-char-regexp)
610 (forward-char 1))))
611 (and dabbrev-abbrev-skip-leading-regexp
612 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
613 (forward-char 1)))))
615 ;;; Extract the symbol at point to serve as abbreviation.
616 (defun dabbrev--abbrev-at-point ()
617 ;; Check for error
618 (if (bobp)
619 (error "No possible abbreviation preceding point"))
620 ;; Return abbrev at point
621 (save-excursion
622 ;; Record the end of the abbreviation.
623 (setq dabbrev--last-abbrev-location (point))
624 ;; If we aren't right after an abbreviation,
625 ;; move point back to just after one.
626 ;; This is so the user can get successive words
627 ;; by typing the punctuation followed by M-/.
628 (save-match-data
629 (if (save-excursion
630 (forward-char -1)
631 (not (looking-at (concat "\\("
632 (or dabbrev-abbrev-char-regexp
633 "\\sw\\|\\s_")
634 "\\)+"))))
635 (if (re-search-backward (or dabbrev-abbrev-char-regexp
636 "\\sw\\|\\s_")
637 nil t)
638 (forward-char 1)
639 (error "No possible abbreviation preceding point"))))
640 ;; Now find the beginning of that one.
641 (dabbrev--goto-start-of-abbrev)
642 (buffer-substring-no-properties
643 dabbrev--last-abbrev-location (point))))
645 ;;; Initializes all global variables
646 (defun dabbrev--reset-global-variables ()
647 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
648 ;; must not be reset here.
649 (setq dabbrev--last-table nil
650 dabbrev--last-abbreviation nil
651 dabbrev--last-abbrev-location nil
652 dabbrev--last-direction nil
653 dabbrev--last-expansion nil
654 dabbrev--last-expansion-location nil
655 dabbrev--friend-buffer-list nil
656 dabbrev--last-buffer nil
657 dabbrev--last-buffer-found nil
658 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
659 "\\sw\\|\\s_")
660 dabbrev--check-other-buffers dabbrev-check-other-buffers))
662 ;;; Find all buffers that are considered "friends" according to the
663 ;;; function pointed out by dabbrev-friend-buffer-function.
664 (defun dabbrev--select-buffers ()
665 (save-excursion
666 (and (window-minibuffer-p (selected-window))
667 (set-buffer (dabbrev--minibuffer-origin)))
668 (let ((orig-buffer (current-buffer)))
669 (dabbrev-filter-elements
670 buffer (buffer-list)
671 (and (not (eq orig-buffer buffer))
672 (boundp 'dabbrev-friend-buffer-function)
673 (funcall dabbrev-friend-buffer-function buffer))))))
675 ;;; Search for ABBREV, N times, normally looking forward,
676 ;;; but looking in reverse instead if REVERSE is non-nil.
677 (defun dabbrev--try-find (abbrev reverse n ignore-case)
678 (save-excursion
679 (save-restriction
680 (widen)
681 (let ((expansion nil))
682 (and dabbrev--last-expansion-location
683 (goto-char dabbrev--last-expansion-location))
684 (let ((case-fold-search ignore-case)
685 (count n))
686 (while (and (> count 0)
687 (setq expansion (dabbrev--search abbrev
688 reverse
689 ignore-case)))
690 (setq count (1- count))))
691 (and expansion
692 (setq dabbrev--last-expansion-location (point)))
693 expansion))))
695 ;;; Find all expansions of ABBREV
696 (defun dabbrev--find-all-expansions (abbrev ignore-case)
697 (let ((all-expansions nil)
698 expansion)
699 (save-excursion
700 (goto-char (point-min))
701 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
702 (setq all-expansions (cons expansion all-expansions))))
703 all-expansions))
705 (defun dabbrev--scanning-message ()
706 (message "Scanning `%s'" (buffer-name (current-buffer))))
708 ;;; Find one occasion of ABBREV.
709 ;;; DIRECTION > 0 means look that many times backwards.
710 ;;; DIRECTION < 0 means look that many times forward.
711 ;;; DIRECTION = 0 means try both backward and forward.
712 ;;; IGNORE-CASE non-nil means ignore case when searching.
713 (defun dabbrev--find-expansion (abbrev direction ignore-case)
714 (let (expansion)
715 (save-excursion
716 (cond
717 (dabbrev--last-buffer
718 (set-buffer dabbrev--last-buffer)
719 (dabbrev--scanning-message))
720 ((and (not dabbrev-search-these-buffers-only)
721 (window-minibuffer-p (selected-window)))
722 (set-buffer (dabbrev--minibuffer-origin))
723 ;; In the minibuffer-origin buffer we will only search from
724 ;; the top and down.
725 (goto-char (point-min))
726 (setq direction -1)
727 (dabbrev--scanning-message)))
728 (cond
729 ;; ------------------------------------------
730 ;; Look backwards
731 ;; ------------------------------------------
732 ((and (not dabbrev-search-these-buffers-only)
733 (>= direction 0)
734 (setq dabbrev--last-direction (min 1 direction))
735 (setq expansion (dabbrev--try-find abbrev t
736 (max 1 direction)
737 ignore-case)))
738 expansion)
739 ;; ------------------------------------------
740 ;; Look forward
741 ;; ------------------------------------------
742 ((and (or (not dabbrev-search-these-buffers-only)
743 dabbrev--last-buffer)
744 (<= direction 0)
745 (setq dabbrev--last-direction -1)
746 (setq expansion (dabbrev--try-find abbrev nil
747 (max 1 (- direction))
748 ignore-case)))
749 expansion)
750 ;; ------------------------------------------
751 ;; Look in other buffers.
752 ;; Start at (point-min) and look forward.
753 ;; ------------------------------------------
755 (setq dabbrev--last-direction -1)
756 ;; Make sure that we should check other buffers
757 (or dabbrev--friend-buffer-list
758 dabbrev--last-buffer
759 (setq dabbrev--friend-buffer-list
760 (mapcar (function get-buffer)
761 dabbrev-search-these-buffers-only))
762 (not dabbrev--check-other-buffers)
763 (not (or (eq dabbrev--check-other-buffers t)
764 (progn
765 (setq dabbrev--check-other-buffers
766 (y-or-n-p "Scan other buffers also? ")))))
767 (let* (friend-buffer-list non-friend-buffer-list)
768 (setq dabbrev--friend-buffer-list
769 (funcall dabbrev-select-buffers-function))
770 (if dabbrev-check-all-buffers
771 (setq non-friend-buffer-list
772 (nreverse
773 (dabbrev-filter-elements
774 buffer (buffer-list)
775 (let ((bn (buffer-name buffer)))
776 (and (not (member bn dabbrev-ignored-buffer-names))
777 (not (memq buffer dabbrev--friend-buffer-list))
778 (not
779 (let ((tail dabbrev-ignored-regexps)
780 (match nil))
781 (while (and tail (not match))
782 (setq match (string-match (car tail) bn)
783 tail (cdr tail)))
784 match))))))
785 dabbrev--friend-buffer-list
786 (append dabbrev--friend-buffer-list
787 non-friend-buffer-list)))))
788 ;; Move buffers that are visible on the screen
789 ;; to the front of the list. Remove the current buffer.
790 (when dabbrev--friend-buffer-list
791 (walk-windows (lambda (w)
792 (unless (eq w (selected-window))
793 (setq dabbrev--friend-buffer-list
794 (cons (window-buffer w)
795 (delq (window-buffer w)
796 dabbrev--friend-buffer-list))))))
797 (setq dabbrev--friend-buffer-list
798 (delq (current-buffer) dabbrev--friend-buffer-list)))
799 ;; Walk through the buffers
800 (while (and (not expansion) dabbrev--friend-buffer-list)
801 (setq dabbrev--last-buffer
802 (car dabbrev--friend-buffer-list))
803 (setq dabbrev--friend-buffer-list
804 (cdr dabbrev--friend-buffer-list))
805 (set-buffer dabbrev--last-buffer)
806 (dabbrev--scanning-message)
807 (setq dabbrev--last-expansion-location (point-min))
808 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
809 expansion)))))
811 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
812 (if (eq major-mode 'picture-mode)
813 (picture-replace-match string fixedcase literal)
814 (replace-match string fixedcase literal)))
816 ;;;----------------------------------------------------------------
817 ;;; Substitute the current string in buffer with the expansion
818 ;;; OLD is nil or the last expansion substring.
819 ;;; ABBREV is the abbreviation we are working with.
820 ;;; EXPANSION is the expansion substring.
821 (defun dabbrev--substitute-expansion (old abbrev expansion)
822 ;;(undo-boundary)
823 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
824 case-fold-search
825 dabbrev-case-fold-search)
826 (or (not dabbrev-upcase-means-case-search)
827 (string= abbrev (downcase abbrev)))
828 (if (eq dabbrev-case-replace 'case-replace)
829 case-replace
830 dabbrev-case-replace))))
831 (and nil use-case-replace
832 (setq old (concat abbrev (or old "")))
833 (setq expansion (concat abbrev expansion)))
834 ;; If the expansion has mixed case
835 ;; and it is not simply a capitalized word,
836 ;; or if the abbrev has mixed case,
837 ;; and if the given abbrev's case pattern
838 ;; matches the start of the expansion,
839 ;; copy the expansion's case
840 ;; instead of downcasing all the rest.
841 (let ((expansion-rest (substring expansion 1)))
842 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
843 (string= expansion-rest (upcase expansion-rest)))
844 (or (string= abbrev (downcase abbrev))
845 (string= abbrev (upcase abbrev)))))
846 (string= abbrev
847 (substring expansion 0 (length abbrev))))
848 (setq use-case-replace nil)))
849 (if (equal abbrev " ")
850 (setq use-case-replace nil))
851 (if use-case-replace
852 (setq expansion (downcase expansion)))
853 (if old
854 (save-excursion
855 (search-backward old))
856 ;;(set-match-data (list (point-marker) (point-marker)))
857 (search-backward abbrev))
858 ;; Make case of replacement conform to case of abbreviation
859 ;; provided (1) that kind of thing is enabled in this buffer
860 ;; and (2) the replacement itself is all lower case.
861 (dabbrev--safe-replace-match expansion
862 (not use-case-replace)
863 t)))
866 ;;;----------------------------------------------------------------
867 ;;; Search function used by dabbrevs library.
869 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
870 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
871 ;;; controls the maximum search region size. Third argument IGNORE-CASE
872 ;;; non-nil means treat case as insignificant while looking for a match
873 ;;; and when comparing with previous matches. Also if that's non-nil
874 ;;; and the match is found at the beginning of a sentence and is in
875 ;;; lower case except for the initial then it is converted to all lower
876 ;;; case for return.
878 ;;; Table of expansions already seen is examined in buffer
879 ;;; `dabbrev--last-table' so that only distinct possibilities are found
880 ;;; by dabbrev-re-expand.
882 ;;; Value is the expansion, or nil if not found.
884 (defun dabbrev--search (abbrev reverse ignore-case)
885 (save-match-data
886 (let ((pattern1 (concat (regexp-quote abbrev)
887 "\\(" dabbrev--abbrev-char-regexp "\\)"))
888 (pattern2 (concat (regexp-quote abbrev)
889 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
890 (found-string nil))
891 ;; Limited search.
892 (save-restriction
893 (and dabbrev-limit
894 (narrow-to-region dabbrev--last-expansion-location
895 (+ (point)
896 (if reverse (- dabbrev-limit) dabbrev-limit))))
897 ;;--------------------------------
898 ;; Look for a distinct expansion, using dabbrev--last-table.
899 ;;--------------------------------
900 (while (and (not found-string)
901 (if reverse
902 (re-search-backward pattern1 nil t)
903 (re-search-forward pattern1 nil t)))
904 (goto-char (match-beginning 0))
905 ;; In case we matched in the middle of a word,
906 ;; back up to start of word and verify we still match.
907 (dabbrev--goto-start-of-abbrev)
909 (if (not (looking-at pattern1))
911 ;; We have a truly valid match. Find the end.
912 (re-search-forward pattern2)
913 (setq found-string (buffer-substring-no-properties
914 (match-beginning 1) (match-end 1)))
915 (and ignore-case (setq found-string (downcase found-string)))
916 ;; Ignore this match if it's already in the table.
917 (if (dabbrev-filter-elements
918 table-string dabbrev--last-table
919 (string= found-string table-string))
920 (setq found-string nil)))
921 ;; Prepare to continue searching.
922 (if reverse
923 (goto-char (match-beginning 0))
924 (goto-char (match-end 0))))
925 ;; If we found something, use it.
926 (if found-string
927 ;; Put it into `dabbrev--last-table'
928 ;; and return it (either downcased, or as is).
929 (let ((result (buffer-substring-no-properties
930 (match-beginning 0) (match-end 0))))
931 (setq dabbrev--last-table
932 (cons found-string dabbrev--last-table))
933 (if (and ignore-case (eval dabbrev-case-replace))
934 result
935 result)))))))
937 (dolist (mess '("^No dynamic expansion for .* found$"
938 "^No further dynamic expansion for .* found$"
939 "^No possible abbreviation preceding point$"))
940 (add-to-list 'debug-ignored-errors mess))
942 (provide 'dabbrev)
944 ;;; dabbrev.el ends here