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