(woman-menu): Move symbol before the comment so that
[emacs.git] / lisp / dabbrev.el
blob55b080ee0b15697916f9aec19a25ee5c6b798835
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, not counting the current buffer.
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 (not (= (point) (field-beginning (point) nil
604 (1- (point))))))
605 (forward-char -1))
606 (or (looking-at dabbrev--abbrev-char-regexp)
607 (forward-char 1))))
608 (and dabbrev-abbrev-skip-leading-regexp
609 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
610 (forward-char 1)))))
612 ;;; Extract the symbol at point to serve as abbreviation.
613 (defun dabbrev--abbrev-at-point ()
614 ;; Check for error
615 (if (bobp)
616 (error "No possible abbreviation preceding point"))
617 ;; Return abbrev at point
618 (save-excursion
619 ;; Record the end of the abbreviation.
620 (setq dabbrev--last-abbrev-location (point))
621 ;; If we aren't right after an abbreviation,
622 ;; move point back to just after one.
623 ;; This is so the user can get successive words
624 ;; by typing the punctuation followed by M-/.
625 (save-match-data
626 (if (save-excursion
627 (forward-char -1)
628 (not (looking-at (concat "\\("
629 (or dabbrev-abbrev-char-regexp
630 "\\sw\\|\\s_")
631 "\\)+"))))
632 (if (re-search-backward (or dabbrev-abbrev-char-regexp
633 "\\sw\\|\\s_")
634 nil t)
635 (forward-char 1)
636 (error "No possible abbreviation preceding point"))))
637 ;; Now find the beginning of that one.
638 (dabbrev--goto-start-of-abbrev)
639 (buffer-substring-no-properties
640 dabbrev--last-abbrev-location (point))))
642 ;;; Initializes all global variables
643 (defun dabbrev--reset-global-variables ()
644 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
645 ;; must not be reset here.
646 (setq dabbrev--last-table nil
647 dabbrev--last-abbreviation nil
648 dabbrev--last-abbrev-location nil
649 dabbrev--last-direction nil
650 dabbrev--last-expansion nil
651 dabbrev--last-expansion-location nil
652 dabbrev--friend-buffer-list nil
653 dabbrev--last-buffer nil
654 dabbrev--last-buffer-found nil
655 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
656 "\\sw\\|\\s_")
657 dabbrev--check-other-buffers dabbrev-check-other-buffers))
659 (defun dabbrev--select-buffers ()
660 "Return a list of other buffers to search for a possible abbrev.
661 The current buffer is not included in the list.
663 This function makes a list of all the buffers returned by `buffer-list',
664 then discards buffers whose names match `dabbrev-ignored-buffer-names'
665 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
666 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
667 with the buffer as argument.
668 It returns the list of the buffers that are not discarded."
669 (dabbrev-filter-elements
670 buffer (buffer-list)
671 (and (not (eq (current-buffer) buffer))
672 (not (dabbrev--ignore-buffer-p buffer))
673 (boundp 'dabbrev-friend-buffer-function)
674 (funcall dabbrev-friend-buffer-function buffer))))
676 (defun dabbrev--try-find (abbrev reverse n ignore-case)
677 "Search for ABBREV, backwards if REVERSE, N times.
678 If IGNORE-CASE is non-nil, ignore case while searching.
679 Return the expansion found, and save the location of the start
680 of the expansion in `dabbrev--last-expansion-location'."
681 (save-excursion
682 (save-restriction
683 (widen)
684 (let ((expansion nil))
685 (and dabbrev--last-expansion-location
686 (goto-char dabbrev--last-expansion-location))
687 (let ((case-fold-search ignore-case)
688 (count n))
689 (while (and (> count 0)
690 (setq expansion (dabbrev--search abbrev
691 reverse
692 ignore-case)))
693 (setq count (1- count))))
694 (and expansion
695 (setq dabbrev--last-expansion-location (point)))
696 expansion))))
698 (defun dabbrev--find-all-expansions (abbrev ignore-case)
699 "Return a list of all possible expansions of ABBREV.
700 If IGNORE-CASE is non-nil, accept matches which differ in case."
701 (let ((all-expansions nil)
702 expansion)
703 (save-excursion
704 (goto-char (point-min))
705 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
706 (setq all-expansions (cons expansion all-expansions))))
707 all-expansions))
709 (defun dabbrev--scanning-message ()
710 (unless (window-minibuffer-p (selected-window))
711 (message "Scanning `%s'" (buffer-name (current-buffer)))))
713 (defun dabbrev--ignore-buffer-p (buffer)
714 "Return non-nil if BUFFER should be ignored by dabbrev."
715 (let ((bn (buffer-name buffer)))
716 (or (member bn dabbrev-ignored-buffer-names)
717 (let ((tail dabbrev-ignored-buffer-regexps)
718 (match nil))
719 (while (and tail (not match))
720 (setq match (string-match (car tail) bn)
721 tail (cdr tail)))
722 match))))
724 (defun dabbrev--find-expansion (abbrev direction ignore-case)
725 "Find one occurrence of ABBREV, and return the expansion.
726 DIRECTION > 0 means look that many times backwards.
727 DIRECTION < 0 means look that many times forward.
728 DIRECTION = 0 means try both backward and forward.
729 IGNORE-CASE non-nil means ignore case when searching.
730 This sets `dabbrev--last-direction' to 1 or -1 according
731 to the direction in which the occurrence was actually found.
732 It sets `dabbrev--last-expansion-location' to the location
733 of the start of the occurrence."
734 (save-excursion
735 ;; If we were scanning something other than the current buffer,
736 ;; continue scanning there.
737 (when dabbrev--last-buffer
738 (set-buffer dabbrev--last-buffer)
739 (dabbrev--scanning-message))
741 ;; ------------------------------------------
742 ;; Look backward in current buffer.
743 ;; ------------------------------------------
744 (and (not dabbrev-search-these-buffers-only)
745 (>= direction 0)
746 (setq dabbrev--last-direction (min 1 direction))
747 (dabbrev--try-find abbrev t
748 (max 1 direction)
749 ignore-case))
750 ;; ------------------------------------------
751 ;; Look forward in current buffer
752 ;; or whatever buffer we were last scanning.
753 ;; ------------------------------------------
754 (and (or (not dabbrev-search-these-buffers-only)
755 dabbrev--last-buffer)
756 (<= direction 0)
757 (setq dabbrev--last-direction -1)
758 (dabbrev--try-find abbrev nil
759 (max 1 (- direction))
760 ignore-case))
761 ;; ------------------------------------------
762 ;; Look in other buffers.
763 ;; Always start at (point-min) and look forward.
764 ;; ------------------------------------------
765 (progn
766 (setq dabbrev--last-direction -1)
767 (unless dabbrev--last-buffer
768 ;; If we have just now begun to search other buffers,
769 ;; determine which other buffers we should check.
770 ;; Put that list in dabbrev--friend-buffer-list.
771 (or dabbrev--friend-buffer-list
772 (setq dabbrev--friend-buffer-list
773 (dabbrev--make-friend-buffer-list))))
774 ;; Walk through the buffers till we find a match.
775 (let (expansion)
776 (while (and (not expansion) dabbrev--friend-buffer-list)
777 (setq dabbrev--last-buffer
778 (car dabbrev--friend-buffer-list))
779 (setq dabbrev--friend-buffer-list
780 (cdr dabbrev--friend-buffer-list))
781 (set-buffer dabbrev--last-buffer)
782 (dabbrev--scanning-message)
783 (setq dabbrev--last-expansion-location (point-min))
784 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
785 expansion)))))
787 ;; Compute the list of buffers to scan.
788 ;; If dabbrev-search-these-buffers-only, then the current buffer
789 ;; is included in this list if it should be searched.
790 ;; Otherwise, the current buffer is searched first specially.,
791 ;; and it is not included in this list.
792 (defun dabbrev--make-friend-buffer-list ()
793 (let ((list (mapcar (function get-buffer)
794 dabbrev-search-these-buffers-only)))
795 (when (and (null dabbrev-search-these-buffers-only)
796 dabbrev--check-other-buffers
797 (or (eq dabbrev--check-other-buffers t)
798 (setq dabbrev--check-other-buffers
799 (y-or-n-p "Scan other buffers also? "))))
800 (setq list (funcall dabbrev-select-buffers-function))
801 ;; If dabbrev-check-all-buffers, tack on all the other
802 ;; buffers at the end of the list, except those which are
803 ;; specifically to be ignored.
804 (if dabbrev-check-all-buffers
805 (setq list
806 (append list
807 (dabbrev-filter-elements
808 buffer (buffer-list)
809 (and (not (memq buffer list))
810 (not (dabbrev--ignore-buffer-p buffer)))))))
811 ;; Remove the current buffer.
812 (setq list (delq (current-buffer) list)))
813 ;; Move buffers in the list that are visible on the screen
814 ;; to the front of the list, but don't add anything to the list.
815 (if list
816 (walk-windows (lambda (w)
817 (unless (eq w (selected-window))
818 (if (memq (window-buffer w) list)
819 (setq list
820 (cons (window-buffer w)
821 (delq (window-buffer w)
822 list))))))))
823 ;; In a minibuffer, search the buffer it was activated from,
824 ;; first after the minibuffer itself. Unless we aren't supposed
825 ;; to search the current buffer either.
826 (if (and (window-minibuffer-p (selected-window))
827 (not dabbrev-search-these-buffers-only))
828 (setq list
829 (cons (dabbrev--minibuffer-origin)
830 (delq (dabbrev--minibuffer-origin) list))))
831 list))
833 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
834 (if (eq major-mode 'picture-mode)
835 (picture-replace-match string fixedcase literal)
836 (replace-match string fixedcase literal)))
838 ;;;----------------------------------------------------------------
839 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
840 "Replace OLD with EXPANSION in the buffer.
841 OLD is text currently in the buffer, perhaps the abbreviation
842 or perhaps another expansion that was tried previously.
843 ABBREV is the abbreviation we are expanding.
844 It is \" \" if we are copying subsequent words.
845 EXPANSION is the expansion substring to be used this time.
846 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
847 to record whether we upcased the expansion, downcased it, or did neither."
848 ;;(undo-boundary)
849 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
850 case-fold-search
851 dabbrev-case-fold-search)
852 (or (not dabbrev-upcase-means-case-search)
853 (string= abbrev (downcase abbrev)))
854 (if (eq dabbrev-case-replace 'case-replace)
855 case-replace
856 dabbrev-case-replace))))
858 ;; If we upcased or downcased the original expansion,
859 ;; do likewise for the subsequent words when we copy them.
860 ;; Don't do any of the usual case processing, though.
861 (when (equal abbrev " ")
862 (if dabbrev--last-case-pattern
863 (setq expansion
864 (funcall dabbrev--last-case-pattern expansion)))
865 (setq use-case-replace nil))
867 ;; If the expansion has mixed case
868 ;; and it is not simply a capitalized word,
869 ;; or if the abbrev has mixed case,
870 ;; and if the given abbrev's case pattern
871 ;; matches the start of the expansion,
872 ;; copy the expansion's case
873 ;; instead of downcasing all the rest.
874 ;; Treat a one-capital-letter abbrev as "not all upper case",
875 ;; so as to force preservation of the expansion's pattern
876 ;; if the expansion starts with a capital letter.
877 (let ((expansion-rest (substring expansion 1)))
878 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
879 (string= expansion-rest (upcase expansion-rest)))
880 (or (string= abbrev (downcase abbrev))
881 (and (string= abbrev (upcase abbrev))
882 (> (length abbrev) 1)))))
883 (string= abbrev
884 (substring expansion 0 (length abbrev))))
885 (setq use-case-replace nil)))
887 ;; If the abbrev and the expansion are both all-lower-case
888 ;; then don't do any conversion. The conversion would be a no-op
889 ;; for this replacement, but it would carry forward to subsequent words.
890 ;; The goal of this is to preven that carrying forward.
891 (if (and (string= expansion (downcase expansion))
892 (string= abbrev (downcase abbrev)))
893 (setq use-case-replace nil))
895 (if use-case-replace
896 (setq expansion (downcase expansion)))
898 ;; In case we insert subsequent words,
899 ;; record if we upcased or downcased the first word,
900 ;; in order to do likewise for subsequent words.
901 (and record-case-pattern
902 (setq dabbrev--last-case-pattern
903 (and use-case-replace
904 (cond ((equal abbrev (upcase abbrev)) 'upcase)
905 ((equal abbrev (downcase abbrev)) 'downcase)))))
907 ;; Convert newlines to spaces.
908 (if dabbrev--eliminate-newlines
909 (while (string-match "\n" expansion)
910 (setq expansion (replace-match " " nil nil expansion))))
912 (if old
913 (save-excursion
914 (search-backward old))
915 ;;(set-match-data (list (point-marker) (point-marker)))
916 (search-backward abbrev)
917 (search-forward abbrev))
919 ;; Make case of replacement conform to case of abbreviation
920 ;; provided (1) that kind of thing is enabled in this buffer
921 ;; and (2) the replacement itself is all lower case.
922 (dabbrev--safe-replace-match expansion
923 (not use-case-replace)
924 t)))
927 ;;;----------------------------------------------------------------
928 ;;; Search function used by dabbrevs library.
931 (defun dabbrev--search (abbrev reverse ignore-case)
932 "Search for something that could be used to expand ABBREV.
934 Second arg, REVERSE, is t for reverse search, nil for forward.
935 The variable `dabbrev-limit' controls the maximum search region size.
936 Third argument IGNORE-CASE non-nil means treat case as insignificant while
937 looking for a match and when comparing with previous matches. Also if
938 that's non-nil and the match is found at the beginning of a sentence
939 and is in lower case except for the initial then it is converted to
940 all lower case for return.
942 Table of expansions already seen is examined in buffer
943 `dabbrev--last-table' so that only distinct possibilities are found
944 by dabbrev-re-expand.
946 Returns the expansion found, or nil if not found.
947 Leaves point at the location of the start of the expansion."
948 (save-match-data
949 (let ((pattern1 (concat (regexp-quote abbrev)
950 "\\(" dabbrev--abbrev-char-regexp "\\)"))
951 (pattern2 (concat (regexp-quote abbrev)
952 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
953 (found-string nil))
954 ;; Limited search.
955 (save-restriction
956 (and dabbrev-limit
957 (narrow-to-region dabbrev--last-expansion-location
958 (+ (point)
959 (if reverse (- dabbrev-limit) dabbrev-limit))))
960 ;;--------------------------------
961 ;; Look for a distinct expansion, using dabbrev--last-table.
962 ;;--------------------------------
963 (while (and (not found-string)
964 (if reverse
965 (re-search-backward pattern1 nil t)
966 (re-search-forward pattern1 nil t)))
967 (goto-char (match-beginning 0))
968 ;; In case we matched in the middle of a word,
969 ;; back up to start of word and verify we still match.
970 (dabbrev--goto-start-of-abbrev)
972 (if (not (looking-at pattern1))
974 ;; We have a truly valid match. Find the end.
975 (re-search-forward pattern2)
976 (setq found-string (buffer-substring-no-properties
977 (match-beginning 1) (match-end 1)))
978 (and ignore-case (setq found-string (downcase found-string)))
979 ;; Ignore this match if it's already in the table.
980 (if (dabbrev-filter-elements
981 table-string dabbrev--last-table
982 (string= found-string table-string))
983 (setq found-string nil)))
984 ;; Prepare to continue searching.
985 (if reverse
986 (goto-char (match-beginning 0))
987 (goto-char (match-end 0))))
988 ;; If we found something, use it.
989 (if found-string
990 ;; Put it into `dabbrev--last-table'
991 ;; and return it (either downcased, or as is).
992 (let ((result (buffer-substring-no-properties
993 (match-beginning 0) (match-end 0))))
994 (setq dabbrev--last-table
995 (cons found-string dabbrev--last-table))
996 (if (and ignore-case (eval dabbrev-case-replace))
997 result
998 result)))))))
1000 (dolist (mess '("^No dynamic expansion for .* found$"
1001 "^No further dynamic expansion for .* found$"
1002 "^No possible abbreviation preceding point$"))
1003 (add-to-list 'debug-ignored-errors mess))
1005 (provide 'dabbrev)
1007 ;;; dabbrev.el ends here