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