(single_submenu, xmenu_show) [!HAVE_MULTILINGUAL_MENU]:
[emacs.git] / lisp / dabbrev.el
blob38c3a19b5acd399da4a8cccb962c8689e5716aa9
1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 86, 92, 94, 96, 1997, 2000, 2001
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--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 nil means case fold search, non-nil means case sensitive search.
152 This variable has an effect only when the value of
153 `dabbrev-case-fold-search' says to ignore case."
154 :type 'boolean
155 :group 'dabbrev)
157 (defcustom dabbrev-case-replace 'case-replace
158 "*Controls whether dabbrev preserves case when expanding the abbreviation.
159 A value of nil means preserve case.
160 A value of `case-replace' means preserve case if `case-replace' is nil.
161 Any other non-nil version means do not preserve case.
163 This variable has an effect only when the value of
164 `dabbrev-case-fold-search' specifies to ignore case."
165 :type '(choice (const :tag "off" nil)
166 (const :tag "like M-x query-replace" case-replace)
167 (other :tag "on" t))
168 :group 'dabbrev)
170 (defcustom dabbrev-abbrev-char-regexp nil
171 "*Regexp to recognize a character in an abbreviation or expansion.
172 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
174 Set this variable to \"\\\\sw\" if you want ordinary words or
175 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
176 syntax is \"symbol\" as well as those whose syntax is \"word\".
178 The value nil has a special meaning: the abbreviation is from point to
179 previous word-start, but the search is for symbols.
181 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
182 while `yes', `or', `no' and `p' are considered words. If this
183 variable is nil, then expanding `yes-or-no-' looks for a symbol
184 starting with or containing `no-'. If you set this variable to
185 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
186 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
187 expanding `yes-or-no-' signals an error because `-' is not part of a word;
188 but expanding `yes-or-no' looks for a word starting with `no'.
190 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
191 :type '(choice (const nil)
192 regexp)
193 :group 'dabbrev)
195 (defcustom dabbrev-check-all-buffers t
196 "*Non-nil means dabbrev package should search *all* buffers.
198 Dabbrev always searches the current buffer first. Then, if
199 `dabbrev-check-other-buffers' says so, it searches the buffers
200 designated by `dabbrev-select-buffers-function'.
202 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
203 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
204 or matched by `dabbrev-ignored-regexps'."
205 :type 'boolean
206 :group 'dabbrev)
208 (defcustom dabbrev-ignored-buffer-names '("*Messages*" "*Buffer List*")
209 "*List of buffer names that dabbrev should not check.
210 See also `dabbrev-ignored-buffer-regexps'."
211 :type '(repeat (string :tag "Buffer name"))
212 :group 'dabbrev
213 :version "20.3")
215 (defcustom dabbrev-ignored-buffer-regexps nil
216 "*List of regexps matching names of buffers that dabbrev should not check.
217 See also `dabbrev-ignored-buffer-names'."
218 :type '(repeat regexp)
219 :group 'dabbrev
220 :version "21.1")
222 (defcustom dabbrev-check-other-buffers t
223 "*Should \\[dabbrev-expand] look in other buffers?\
225 nil: Don't look in other buffers.
226 t: Also look for expansions in the buffers pointed out by
227 `dabbrev-select-buffers-function'.
228 Anything else: When we can't find any more expansions in
229 the current buffer, then ask the user whether to look in other
230 buffers too.
232 The default value is t."
233 :type '(choice (const :tag "off" nil)
234 (const :tag "on" t)
235 (other :tag "ask" other))
236 :group 'dabbrev)
238 ;; I guess setting this to a function that selects all C- or C++-
239 ;; mode buffers would be a good choice for a debugging buffer,
240 ;; when debugging C- or C++-code.
241 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
242 "A function that selects buffers that should be searched by dabbrev.
243 The function should take no arguments and return a list of buffers to
244 search for expansions. See the source of `dabbrev--select-buffers'
245 for an example.
247 A mode setting this variable should make it buffer local.")
249 (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
250 "*A function to decide whether dabbrev should search OTHER-BUFFER.
251 The function should take one argument, OTHER-BUFFER, and return
252 non-nil if that buffer should be searched. Have a look at
253 `dabbrev--same-major-mode-p' for an example.
255 The value of `dabbrev-friend-buffer-function' has an effect only if
256 the value of `dabbrev-select-buffers-function' uses it. The function
257 `dabbrev--select-buffers' is one function you can use here.
259 A mode setting this variable should make it buffer local."
260 :type 'function
261 :group 'dabbrev)
263 (defcustom dabbrev-search-these-buffers-only nil
264 "If non-nil, a list of buffers which dabbrev should search.
265 If this variable is non-nil, dabbrev will only look in these buffers.
266 It will not even look in the current buffer if it is not a member of
267 this list.")
269 ;;----------------------------------------------------------------
270 ;; Internal variables
271 ;;----------------------------------------------------------------
273 ;; Last obarray of completions in `dabbrev-completion'
274 (defvar dabbrev--last-obarray nil)
276 ;; Table of expansions seen so far
277 (defvar dabbrev--last-table nil)
279 ;; Last string we tried to expand.
280 (defvar dabbrev--last-abbreviation nil)
282 ;; Location last abbreviation began
283 (defvar dabbrev--last-abbrev-location nil)
285 ;; Direction of last dabbrevs search
286 (defvar dabbrev--last-direction 0)
288 ;; Last expansion of an abbreviation.
289 (defvar dabbrev--last-expansion nil)
291 ;; Location the last expansion was found.
292 (defvar dabbrev--last-expansion-location nil)
294 ;; The list of remaining buffers with the same mode as current buffer.
295 (defvar dabbrev--friend-buffer-list nil)
297 ;; The buffer we looked in last.
298 (defvar dabbrev--last-buffer nil)
300 ;; The buffer we found the expansion last time.
301 (defvar dabbrev--last-buffer-found nil)
303 ;; The buffer we last did a completion in.
304 (defvar dabbrev--last-completion-buffer nil)
306 ;; If non-nil, a function to use when copying successive words.
307 ;; It should be `upcase' or `downcase'.
308 (defvar dabbrev--last-case-pattern nil)
310 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
311 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
313 ;; The regexp for recognizing a character in an abbreviation.
314 (defvar dabbrev--abbrev-char-regexp nil)
316 ;;----------------------------------------------------------------
317 ;; Macros
318 ;;----------------------------------------------------------------
320 ;;; Get the buffer that mini-buffer was activated from
321 (defsubst dabbrev--minibuffer-origin ()
322 (car (cdr (buffer-list))))
324 ;; Make a list of some of the elements of LIST.
325 ;; Check each element of LIST, storing it temporarily in the
326 ;; variable ELEMENT, and include it in the result
327 ;; if CONDITION evaluates non-nil.
328 (defmacro dabbrev-filter-elements (element list condition)
329 `(let (dabbrev-result dabbrev-tail ,element)
330 (setq dabbrev-tail ,list)
331 (while dabbrev-tail
332 (setq ,element (car dabbrev-tail))
333 (if ,condition
334 (setq dabbrev-result (cons ,element dabbrev-result)))
335 (setq dabbrev-tail (cdr dabbrev-tail)))
336 (nreverse dabbrev-result)))
338 ;;----------------------------------------------------------------
339 ;; Exported functions
340 ;;----------------------------------------------------------------
342 ;;;###autoload
343 (define-key esc-map "/" 'dabbrev-expand)
344 ;;;??? Do we want this?
345 ;;;###autoload
346 (define-key esc-map [?\C-/] 'dabbrev-completion)
348 ;;;###autoload
349 (defun dabbrev-completion (&optional arg)
350 "Completion on current word.
351 Like \\[dabbrev-expand] but finds all expansions in the current buffer
352 and presents suggestions for completion.
354 With a prefix argument, it searches all buffers accepted by the
355 function pointed out by `dabbrev-friend-buffer-function' to find the
356 completions.
358 If the prefix argument is 16 (which comes from C-u C-u),
359 then it searches *all* buffers.
361 With no prefix argument, it reuses an old completion list
362 if there is a suitable one already."
364 (interactive "*P")
365 (dabbrev--reset-global-variables)
366 (let* ((dabbrev-check-other-buffers (and arg t))
367 (dabbrev-check-all-buffers
368 (and arg (= (prefix-numeric-value arg) 16)))
369 (abbrev (dabbrev--abbrev-at-point))
370 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
371 case-fold-search
372 dabbrev-case-fold-search)
373 (or (not dabbrev-upcase-means-case-search)
374 (string= abbrev (downcase abbrev)))))
375 (my-obarray dabbrev--last-obarray)
376 init)
377 (save-excursion
378 (if (and (null arg)
379 my-obarray
380 (or (eq dabbrev--last-completion-buffer (current-buffer))
381 (and (window-minibuffer-p (selected-window))
382 (eq dabbrev--last-completion-buffer
383 (dabbrev--minibuffer-origin))))
384 dabbrev--last-abbreviation
385 (>= (length abbrev) (length dabbrev--last-abbreviation))
386 (string= dabbrev--last-abbreviation
387 (substring abbrev 0
388 (length dabbrev--last-abbreviation)))
389 (setq init (try-completion abbrev my-obarray)))
390 ;; We can reuse the existing completion list.
392 ;;--------------------------------
393 ;; New abbreviation to expand.
394 ;;--------------------------------
395 (setq dabbrev--last-abbreviation abbrev)
396 ;; Find all expansion
397 (let ((completion-list
398 (dabbrev--find-all-expansions abbrev ignore-case-p))
399 (completion-ignore-case ignore-case-p))
400 ;; Make an obarray with all expansions
401 (setq my-obarray (make-vector (length completion-list) 0))
402 (or (> (length my-obarray) 0)
403 (error "No dynamic expansion for \"%s\" found%s"
404 abbrev
405 (if dabbrev--check-other-buffers "" " in this-buffer")))
406 (cond
407 ((or (not ignore-case-p)
408 (not dabbrev-case-replace))
409 (mapc (function (lambda (string)
410 (intern string my-obarray)))
411 completion-list))
412 ((string= abbrev (upcase abbrev))
413 (mapc (function (lambda (string)
414 (intern (upcase string) my-obarray)))
415 completion-list))
416 ((string= (substring abbrev 0 1)
417 (upcase (substring abbrev 0 1)))
418 (mapc (function (lambda (string)
419 (intern (capitalize string) my-obarray)))
420 completion-list))
422 (mapc (function (lambda (string)
423 (intern (downcase string) my-obarray)))
424 completion-list)))
425 (setq dabbrev--last-obarray my-obarray)
426 (setq dabbrev--last-completion-buffer (current-buffer))
427 ;; Find the longest common string.
428 (setq init (try-completion abbrev my-obarray)))))
429 ;;--------------------------------
430 ;; Let the user choose between the expansions
431 ;;--------------------------------
432 (or (stringp init)
433 (setq init abbrev))
434 (cond
435 ;; * Replace string fragment with matched common substring completion.
436 ((and (not (string-equal init ""))
437 (not (string-equal (downcase init) (downcase abbrev))))
438 (if (> (length (all-completions init my-obarray)) 1)
439 (message "Repeat `%s' to see all completions"
440 (key-description (this-command-keys)))
441 (message "The only possible completion"))
442 (dabbrev--substitute-expansion nil abbrev init nil))
444 ;; * String is a common substring completion already. Make list.
445 (message "Making completion list...")
446 (with-output-to-temp-buffer " *Completions*"
447 (display-completion-list (all-completions init my-obarray)))
448 (message "Making completion list...done")))
449 (and (window-minibuffer-p (selected-window))
450 (message nil))))
452 ;;;###autoload
453 (defun dabbrev-expand (arg)
454 "Expand previous word \"dynamically\".
456 Expands to the most recent, preceding word for which this is a prefix.
457 If no suitable preceding word is found, words following point are
458 considered. If still no suitable word is found, then look in the
459 buffers accepted by the function pointed out by variable
460 `dabbrev-friend-buffer-function'.
462 A positive prefix argument, N, says to take the Nth backward *distinct*
463 possibility. A negative argument says search forward.
465 If the cursor has not moved from the end of the previous expansion and
466 no argument is given, replace the previously-made expansion
467 with the next possible expansion not yet tried.
469 The variable `dabbrev-backward-only' may be used to limit the
470 direction of search to backward if set non-nil.
472 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
473 (interactive "*P")
474 (let (abbrev record-case-pattern
475 expansion old direction (orig-point (point)))
476 ;; abbrev -- the abbrev to expand
477 ;; expansion -- the expansion found (eventually) or nil until then
478 ;; old -- the text currently in the buffer
479 ;; (the abbrev, or the previously-made expansion)
480 (save-excursion
481 (if (and (null arg)
482 (markerp dabbrev--last-abbrev-location)
483 (marker-position dabbrev--last-abbrev-location)
484 (or (eq last-command this-command)
485 (and (window-minibuffer-p (selected-window))
486 (= dabbrev--last-abbrev-location
487 (point)))))
488 ;; Find a different expansion for the same abbrev as last time.
489 (progn
490 (setq abbrev dabbrev--last-abbreviation)
491 (setq old dabbrev--last-expansion)
492 (setq direction dabbrev--last-direction))
493 ;; If the user inserts a space after expanding
494 ;; and then asks to expand again, always fetch the next word.
495 (if (and (eq (preceding-char) ?\ )
496 (markerp dabbrev--last-abbrev-location)
497 (marker-position dabbrev--last-abbrev-location)
498 (= (point) (1+ dabbrev--last-abbrev-location)))
499 (progn
500 ;; The "abbrev" to expand is just the space.
501 (setq abbrev " ")
502 (save-excursion
503 (if dabbrev--last-buffer
504 (set-buffer dabbrev--last-buffer))
505 ;; Find the end of the last "expansion" word.
506 (if (or (eq dabbrev--last-direction 1)
507 (and (eq dabbrev--last-direction 0)
508 (< dabbrev--last-expansion-location (point))))
509 (setq dabbrev--last-expansion-location
510 (+ dabbrev--last-expansion-location
511 (length dabbrev--last-expansion))))
512 (goto-char dabbrev--last-expansion-location)
513 ;; Take the following word, with intermediate separators,
514 ;; as our expansion this time.
515 (re-search-forward
516 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
517 (setq expansion (buffer-substring-no-properties
518 dabbrev--last-expansion-location (point)))
520 ;; Record the end of this expansion, in case we repeat this.
521 (setq dabbrev--last-expansion-location (point)))
522 ;; Indicate that dabbrev--last-expansion-location is
523 ;; at the end of the expansion.
524 (setq dabbrev--last-direction -1))
526 ;; We have a different abbrev to expand.
527 (dabbrev--reset-global-variables)
528 (setq direction (if (null arg)
529 (if dabbrev-backward-only 1 0)
530 (prefix-numeric-value arg)))
531 (setq abbrev (dabbrev--abbrev-at-point))
532 (setq record-case-pattern t)
533 (setq old nil)))
535 ;;--------------------------------
536 ;; Find the expansion
537 ;;--------------------------------
538 (or expansion
539 (setq expansion
540 (dabbrev--find-expansion abbrev direction
541 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
542 case-fold-search
543 dabbrev-case-fold-search)
544 (or (not dabbrev-upcase-means-case-search)
545 (string= abbrev (downcase abbrev))))))))
546 (cond
547 ((not expansion)
548 (dabbrev--reset-global-variables)
549 (if old
550 (save-excursion
551 (setq buffer-undo-list (cons orig-point buffer-undo-list))
552 ;; Put back the original abbrev with its original case pattern.
553 (search-backward old)
554 (insert abbrev)
555 (delete-region (point) (+ (point) (length old)))))
556 (error "No%s dynamic expansion for `%s' found"
557 (if old " further" "") abbrev))
559 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
560 (minibuffer-window-active-p (selected-window))))
561 (progn
562 (message "Expansion found in '%s'"
563 (buffer-name dabbrev--last-buffer))
564 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
565 (message nil))
566 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
567 (null dabbrev--last-buffer))
568 (numberp dabbrev--last-expansion-location)
569 (and (> dabbrev--last-expansion-location (point))))
570 (setq dabbrev--last-expansion-location
571 (copy-marker dabbrev--last-expansion-location)))
572 ;; Success: stick it in and return.
573 (setq buffer-undo-list (cons orig-point buffer-undo-list))
574 (dabbrev--substitute-expansion old abbrev expansion
575 record-case-pattern)
577 ;; Save state for re-expand.
578 (setq dabbrev--last-expansion expansion)
579 (setq dabbrev--last-abbreviation abbrev)
580 (setq dabbrev--last-abbrev-location (point-marker))))))
582 ;;----------------------------------------------------------------
583 ;; Local functions
584 ;;----------------------------------------------------------------
586 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
587 (defun dabbrev--same-major-mode-p (other-buffer)
588 (eq major-mode
589 (save-excursion
590 (set-buffer other-buffer)
591 major-mode)))
593 ;;; Back over all abbrev type characters and then moves forward over
594 ;;; all skip characters.
595 (defun dabbrev--goto-start-of-abbrev ()
596 ;; Move backwards over abbrev chars
597 (save-match-data
598 (if (not (bobp))
599 (progn
600 (forward-char -1)
601 (while (and (looking-at dabbrev--abbrev-char-regexp)
602 (not (bobp)))
603 (forward-char -1))
604 (or (looking-at dabbrev--abbrev-char-regexp)
605 (forward-char 1))))
606 (and dabbrev-abbrev-skip-leading-regexp
607 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
608 (forward-char 1)))))
610 ;;; Extract the symbol at point to serve as abbreviation.
611 (defun dabbrev--abbrev-at-point ()
612 ;; Check for error
613 (if (bobp)
614 (error "No possible abbreviation preceding point"))
615 ;; Return abbrev at point
616 (save-excursion
617 ;; Record the end of the abbreviation.
618 (setq dabbrev--last-abbrev-location (point))
619 ;; If we aren't right after an abbreviation,
620 ;; move point back to just after one.
621 ;; This is so the user can get successive words
622 ;; by typing the punctuation followed by M-/.
623 (save-match-data
624 (if (save-excursion
625 (forward-char -1)
626 (not (looking-at (concat "\\("
627 (or dabbrev-abbrev-char-regexp
628 "\\sw\\|\\s_")
629 "\\)+"))))
630 (if (re-search-backward (or dabbrev-abbrev-char-regexp
631 "\\sw\\|\\s_")
632 nil t)
633 (forward-char 1)
634 (error "No possible abbreviation preceding point"))))
635 ;; Now find the beginning of that one.
636 (dabbrev--goto-start-of-abbrev)
637 (buffer-substring-no-properties
638 dabbrev--last-abbrev-location (point))))
640 ;;; Initializes all global variables
641 (defun dabbrev--reset-global-variables ()
642 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
643 ;; must not be reset here.
644 (setq dabbrev--last-table nil
645 dabbrev--last-abbreviation nil
646 dabbrev--last-abbrev-location nil
647 dabbrev--last-direction nil
648 dabbrev--last-expansion nil
649 dabbrev--last-expansion-location nil
650 dabbrev--friend-buffer-list nil
651 dabbrev--last-buffer nil
652 dabbrev--last-buffer-found nil
653 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
654 "\\sw\\|\\s_")
655 dabbrev--check-other-buffers dabbrev-check-other-buffers))
657 (defun dabbrev--select-buffers ()
658 "Return a list of all buffers that should be searched for a possible abbrev.
660 This function makes a list of all the buffers returned by `buffer-list', and
661 then filters out every buffer for which `dabbrev-friend-buffer-function',
662 if it is bound, returns nil. The resulting partial list is returned."
663 (save-excursion
664 (and (window-minibuffer-p (selected-window))
665 (set-buffer (dabbrev--minibuffer-origin)))
666 (let ((orig-buffer (current-buffer)))
667 (dabbrev-filter-elements
668 buffer (buffer-list)
669 (and (not (eq orig-buffer buffer))
670 (not (dabbrev--ignore-buffer-p buffer))
671 (boundp 'dabbrev-friend-buffer-function)
672 (funcall dabbrev-friend-buffer-function buffer))))))
674 (defun dabbrev--try-find (abbrev reverse n ignore-case)
675 "Search for ABBREV, backwards if REVERSE, N times.
676 If IGNORE-CASE is non-nil, ignore case while searching.
677 Return the expansion found, and save the location of the start
678 of the expansion in `dabbrev--last-expansion-location'."
679 (save-excursion
680 (save-restriction
681 (widen)
682 (let ((expansion nil))
683 (and dabbrev--last-expansion-location
684 (goto-char dabbrev--last-expansion-location))
685 (let ((case-fold-search ignore-case)
686 (count n))
687 (while (and (> count 0)
688 (setq expansion (dabbrev--search abbrev
689 reverse
690 ignore-case)))
691 (setq count (1- count))))
692 (and expansion
693 (setq dabbrev--last-expansion-location (point)))
694 expansion))))
696 (defun dabbrev--find-all-expansions (abbrev ignore-case)
697 "Return a list of all possible expansions of ABBREV.
698 If IGNORE-CASE is non-nil, accept matches which differ in case."
699 (let ((all-expansions nil)
700 expansion)
701 (save-excursion
702 (goto-char (point-min))
703 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
704 (setq all-expansions (cons expansion all-expansions))))
705 all-expansions))
707 (defun dabbrev--scanning-message ()
708 (message "Scanning `%s'" (buffer-name (current-buffer))))
710 (defun dabbrev--ignore-buffer-p (buffer)
711 "Return non-nil if BUFFER should be ignored by dabbrev."
712 (let ((bn (buffer-name buffer)))
713 (or (member bn dabbrev-ignored-buffer-names)
714 (let ((tail dabbrev-ignored-buffer-regexps)
715 (match nil))
716 (while (and tail (not match))
717 (setq match (string-match (car tail) bn)
718 tail (cdr tail)))
719 match))))
721 (defun dabbrev--find-expansion (abbrev direction ignore-case)
722 "Find one occurrence of ABBREV, and return the expansion.
723 DIRECTION > 0 means look that many times backwards.
724 DIRECTION < 0 means look that many times forward.
725 DIRECTION = 0 means try both backward and forward.
726 IGNORE-CASE non-nil means ignore case when searching.
727 This sets `dabbrev--last-direction' to 1 or -1 according
728 to the direction in which the occurrence was actually found.
729 It sets `dabbrev--last-expansion-location' to the location
730 of the start of the occurrence."
731 (let (expansion)
732 (save-excursion
733 (cond
734 (dabbrev--last-buffer
735 (set-buffer dabbrev--last-buffer)
736 (dabbrev--scanning-message))
737 ((and (not dabbrev-search-these-buffers-only)
738 (window-minibuffer-p (selected-window)))
739 (set-buffer (dabbrev--minibuffer-origin))
740 ;; In the minibuffer-origin buffer we will only search from
741 ;; the top and down.
742 (goto-char (point-min))
743 (setq direction -1)
744 (dabbrev--scanning-message)))
745 (cond
746 ;; ------------------------------------------
747 ;; Look backwards
748 ;; ------------------------------------------
749 ((and (not dabbrev-search-these-buffers-only)
750 (>= direction 0)
751 (setq dabbrev--last-direction (min 1 direction))
752 (setq expansion (dabbrev--try-find abbrev t
753 (max 1 direction)
754 ignore-case)))
755 expansion)
756 ;; ------------------------------------------
757 ;; Look forward
758 ;; ------------------------------------------
759 ((and (or (not dabbrev-search-these-buffers-only)
760 dabbrev--last-buffer)
761 (<= direction 0)
762 (setq dabbrev--last-direction -1)
763 (setq expansion (dabbrev--try-find abbrev nil
764 (max 1 (- direction))
765 ignore-case)))
766 expansion)
767 ;; ------------------------------------------
768 ;; Look in other buffers.
769 ;; Start at (point-min) and look forward.
770 ;; ------------------------------------------
772 (setq dabbrev--last-direction -1)
773 ;; Make sure that we should check other buffers
774 (or dabbrev--friend-buffer-list
775 dabbrev--last-buffer
776 (setq dabbrev--friend-buffer-list
777 (mapcar (function get-buffer)
778 dabbrev-search-these-buffers-only))
779 (not dabbrev--check-other-buffers)
780 (not (or (eq dabbrev--check-other-buffers t)
781 (progn
782 (setq dabbrev--check-other-buffers
783 (y-or-n-p "Scan other buffers also? ")))))
784 (let* (friend-buffer-list non-friend-buffer-list)
785 (setq dabbrev--friend-buffer-list
786 (funcall dabbrev-select-buffers-function))
787 (if dabbrev-check-all-buffers
788 (setq non-friend-buffer-list
789 (dabbrev-filter-elements
790 buffer (buffer-list)
791 (and (not (memq buffer dabbrev--friend-buffer-list))
792 (not (dabbrev--ignore-buffer-p buffer))))
793 dabbrev--friend-buffer-list
794 (append dabbrev--friend-buffer-list
795 non-friend-buffer-list)))))
796 ;; Move buffers that are visible on the screen
797 ;; to the front of the list. Remove the current buffer.
798 (when dabbrev--friend-buffer-list
799 (walk-windows (lambda (w)
800 (unless (eq w (selected-window))
801 (setq dabbrev--friend-buffer-list
802 (cons (window-buffer w)
803 (delq (window-buffer w)
804 dabbrev--friend-buffer-list))))))
805 (setq dabbrev--friend-buffer-list
806 (delq (current-buffer) dabbrev--friend-buffer-list)))
807 ;; Walk through the buffers
808 (while (and (not expansion) dabbrev--friend-buffer-list)
809 (setq dabbrev--last-buffer
810 (car dabbrev--friend-buffer-list))
811 (setq dabbrev--friend-buffer-list
812 (cdr dabbrev--friend-buffer-list))
813 (set-buffer dabbrev--last-buffer)
814 (dabbrev--scanning-message)
815 (setq dabbrev--last-expansion-location (point-min))
816 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
817 expansion)))))
819 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
820 (if (eq major-mode 'picture-mode)
821 (picture-replace-match string fixedcase literal)
822 (replace-match string fixedcase literal)))
824 ;;;----------------------------------------------------------------
825 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
826 "Replace OLD with EXPANSION in the buffer.
827 OLD is text currently in the buffer, perhaps the abbreviation
828 or perhaps another expansion that was tried previously.
829 ABBREV is the abbreviation we are expanding.
830 It is \" \" if we are copying subsequent words.
831 EXPANSION is the expansion substring to be used this time.
832 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
833 to record whether we upcased the expansion, downcased it, or did neither."
834 ;;(undo-boundary)
835 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
836 case-fold-search
837 dabbrev-case-fold-search)
838 (or (not dabbrev-upcase-means-case-search)
839 (string= abbrev (downcase abbrev)))
840 (if (eq dabbrev-case-replace 'case-replace)
841 case-replace
842 dabbrev-case-replace))))
844 ;; If we upcased or downcased the original expansion,
845 ;; do likewise for the subsequent words when we copy them.
846 ;; Don't do any of the usual case processing, though.
847 (when (equal abbrev " ")
848 (if dabbrev--last-case-pattern
849 (setq expansion
850 (funcall dabbrev--last-case-pattern expansion)))
851 (setq use-case-replace nil))
853 ;; If the expansion has mixed case
854 ;; and it is not simply a capitalized word,
855 ;; or if the abbrev has mixed case,
856 ;; and if the given abbrev's case pattern
857 ;; matches the start of the expansion,
858 ;; copy the expansion's case
859 ;; instead of downcasing all the rest.
860 ;; Treat a one-capital-letter abbrev as "not all upper case",
861 ;; so as to force preservation of the expansion's pattern
862 ;; if the expansion starts with a capital letter.
863 (let ((expansion-rest (substring expansion 1)))
864 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
865 (string= expansion-rest (upcase expansion-rest)))
866 (or (string= abbrev (downcase abbrev))
867 (and (string= abbrev (upcase abbrev))
868 (> (length abbrev) 1)))))
869 (string= abbrev
870 (substring expansion 0 (length abbrev))))
871 (setq use-case-replace nil)))
873 ;; If the abbrev and the expansion are both all-lower-case
874 ;; then don't do any conversion. The conversion would be a no-op
875 ;; for this replacement, but it would carry forward to subsequent words.
876 ;; The goal of this is to preven that carrying forward.
877 (if (and (string= expansion (downcase expansion))
878 (string= abbrev (downcase abbrev)))
879 (setq use-case-replace nil))
881 (if use-case-replace
882 (setq expansion (downcase expansion)))
884 ;; In case we insert subsequent words,
885 ;; record if we upcased or downcased the first word,
886 ;; in order to do likewise for subsequent words.
887 (and record-case-pattern
888 (setq dabbrev--last-case-pattern
889 (and use-case-replace
890 (cond ((equal abbrev (upcase abbrev)) 'upcase)
891 ((equal abbrev (downcase abbrev)) 'downcase)))))
893 ;; Convert newlines to spaces.
894 (if dabbrev--eliminate-newlines
895 (while (string-match "\n" expansion)
896 (setq expansion (replace-match " " nil nil expansion))))
898 (if old
899 (save-excursion
900 (search-backward old))
901 ;;(set-match-data (list (point-marker) (point-marker)))
902 (search-backward abbrev)
903 (search-forward abbrev))
905 ;; Make case of replacement conform to case of abbreviation
906 ;; provided (1) that kind of thing is enabled in this buffer
907 ;; and (2) the replacement itself is all lower case.
908 (dabbrev--safe-replace-match expansion
909 (not use-case-replace)
910 t)))
913 ;;;----------------------------------------------------------------
914 ;;; Search function used by dabbrevs library.
917 (defun dabbrev--search (abbrev reverse ignore-case)
918 "Search for something that could be used to expand ABBREV.
920 Second arg, REVERSE, is t for reverse search, nil for forward.
921 The variable `dabbrev-limit' controls the maximum search region size.
922 Third argument IGNORE-CASE non-nil means treat case as insignificant while
923 looking for a match and when comparing with previous matches. Also if
924 that's non-nil and the match is found at the beginning of a sentence
925 and is in lower case except for the initial then it is converted to
926 all lower case for return.
928 Table of expansions already seen is examined in buffer
929 `dabbrev--last-table' so that only distinct possibilities are found
930 by dabbrev-re-expand.
932 Returns the expansion found, or nil if not found.
933 Leaves point at the location of the start of the expansion."
934 (save-match-data
935 (let ((pattern1 (concat (regexp-quote abbrev)
936 "\\(" dabbrev--abbrev-char-regexp "\\)"))
937 (pattern2 (concat (regexp-quote abbrev)
938 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
939 (found-string nil))
940 ;; Limited search.
941 (save-restriction
942 (and dabbrev-limit
943 (narrow-to-region dabbrev--last-expansion-location
944 (+ (point)
945 (if reverse (- dabbrev-limit) dabbrev-limit))))
946 ;;--------------------------------
947 ;; Look for a distinct expansion, using dabbrev--last-table.
948 ;;--------------------------------
949 (while (and (not found-string)
950 (if reverse
951 (re-search-backward pattern1 nil t)
952 (re-search-forward pattern1 nil t)))
953 (goto-char (match-beginning 0))
954 ;; In case we matched in the middle of a word,
955 ;; back up to start of word and verify we still match.
956 (dabbrev--goto-start-of-abbrev)
958 (if (not (looking-at pattern1))
960 ;; We have a truly valid match. Find the end.
961 (re-search-forward pattern2)
962 (setq found-string (buffer-substring-no-properties
963 (match-beginning 1) (match-end 1)))
964 (and ignore-case (setq found-string (downcase found-string)))
965 ;; Ignore this match if it's already in the table.
966 (if (dabbrev-filter-elements
967 table-string dabbrev--last-table
968 (string= found-string table-string))
969 (setq found-string nil)))
970 ;; Prepare to continue searching.
971 (if reverse
972 (goto-char (match-beginning 0))
973 (goto-char (match-end 0))))
974 ;; If we found something, use it.
975 (if found-string
976 ;; Put it into `dabbrev--last-table'
977 ;; and return it (either downcased, or as is).
978 (let ((result (buffer-substring-no-properties
979 (match-beginning 0) (match-end 0))))
980 (setq dabbrev--last-table
981 (cons found-string dabbrev--last-table))
982 (if (and ignore-case (eval dabbrev-case-replace))
983 result
984 result)))))))
986 (dolist (mess '("^No dynamic expansion for .* found$"
987 "^No further dynamic expansion for .* found$"
988 "^No possible abbreviation preceding point$"))
989 (add-to-list 'debug-ignored-errors mess))
991 (provide 'dabbrev)
993 ;;; dabbrev.el ends here