]> code.delx.au - gnu-emacs/blob - lisp/dabbrev.el
(diary-islamic-date): Use `date'.
[gnu-emacs] / lisp / dabbrev.el
1 ;;; dabbrev.el --- dynamic abbreviation package
2
3 ;; Copyright (C) 1985, 1986, 1992, 1994 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 (defvar dabbrev-backward-only nil
101 "*If non-nil, `dabbrev-expand' only looks backwards.")
102
103 (defvar dabbrev-limit nil
104 "*Limits region searched by `dabbrev-expand' to this many chars away.")
105
106 (defvar dabbrev-abbrev-skip-leading-regexp nil
107 "*Regexp for skipping leading characters of an abbreviation.
108
109 Example: Set this to \"\\\\$\" for programming languages
110 in which variable names may appear with or without a leading `$'.
111 \(For example, in Makefiles.)
112
113 Set this to nil if no characters should be skipped.")
114
115 ;; I recommend that you set this to nil.
116 (defvar dabbrev-case-fold-search 'case-fold-search
117 "*Non-nil if dabbrev searches should ignore case.
118 A value of nil means case is significant.
119
120 The value of this variable is an expression; it is evaluated
121 and the resulting value determines the decision.
122 For example: setting this to `case-fold-search' means evaluate that
123 variable to see whether its value is nil.")
124
125 (defvar dabbrev-upcase-means-case-search nil
126 "*The significance of an uppercase character in an abbreviation.
127 nil means case fold search, non-nil means case sensitive search.
128
129 This variable has an effect only when the value of
130 `dabbrev-case-fold-search' evaluates to t.")
131
132 ;; I recommend that you set this to nil.
133 (defvar dabbrev-case-replace 'case-replace
134 "*Non-nil means dabbrev should preserve case when expanding the abbreviation.
135 More precisely, it preserves the case pattern of the abbreviation as you
136 typed it--as opposed to the case pattern of the expansion that is copied.
137 The value of this variable is an expression; it is evaluated
138 and the resulting value determines the decision.
139 For example, setting this to `case-replace' means evaluate that
140 variable to see if its value is t or nil.
141
142 This variable has an effect only when the value of
143 `dabbrev-case-fold-search' evaluates to t.")
144
145 (defvar dabbrev-abbrev-char-regexp nil
146 "*Regexp to recognize a character in an abbreviation or expansion.
147 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
148
149 Set this variable to \"\\\\sw\" if you want ordinary words or
150 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
151 syntax is \"symbol\" as well as those whose syntax is \"word\".
152
153 The value nil has a special meaning: the abbreviation is from point to
154 previous word-start, but the search is for symbols.
155
156 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
157 while `yes', `or', `no' and `p' are considered words. If this
158 variable is nil, then expanding `yes-or-no-' looks for a symbol
159 starting with or containing `no-'. If you set this variable to
160 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
161 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
162 expanding `yes-or-no-' signals an error because `-' is not part of a word;
163 but expanding `yes-or-no' looks for a word starting with `no'.
164
165 The recommended value is \"\\\\sw\\\\|\\\\s_\".")
166
167 (defvar dabbrev-check-all-buffers t
168 "*Non-nil means dabbrev package should search *all* buffers.
169
170 Dabbrev always searches the current buffer first. Then, if
171 `dabbrev-check-other-buffers' says so, it searches the buffers
172 designated by `dabbrev-select-buffers-function'.
173
174 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
175 all the other buffers.")
176
177 (defvar dabbrev-check-other-buffers t
178 "*Should \\[dabbrev-expand] look in other buffers?\
179
180 nil: Don't look in other buffers.
181 t: Also look for expansions in the buffers pointed out by
182 `dabbrev-select-buffers-function'.
183 Anything else: When we can't find any more expansions in
184 the current buffer, then ask the user whether to look in other
185 buffers too.
186
187 The default value is t.")
188
189 ;; I guess setting this to a function that selects all C- or C++-
190 ;; mode buffers would be a good choice for a debugging buffer,
191 ;; when debugging C- or C++-code.
192 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
193 "A function that selects buffers that should be searched by dabbrev.
194 The function should take no arguments and return a list of buffers to
195 search for expansions. Have a look at `dabbrev--select-buffers' for
196 an example.
197
198 A mode setting this variable should make it buffer local.")
199
200 (defvar dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
201 "*A function to decide whether dabbrev should search OTHER-BUFFER.
202 The function should take one argument, OTHER-BUFFER, and return
203 non-nil if that buffer should be searched. Have a look at
204 `dabbrev--same-major-mode-p' for an example.
205
206 The value of `dabbrev-friend-buffer-function' has an effect only if
207 the value of `dabbrev-select-buffers-function' uses it. The function
208 `dabbrev--select-buffers' is one function you can use here.
209
210 A mode setting this variable should make it buffer local.")
211
212 (defvar dabbrev-search-these-buffers-only nil
213 "If non-nil, a list of buffers which dabbrev should search.
214 If this variable is non-nil, dabbrev will only look in these buffers.
215 It will not even look in the current buffer if it is not a member of
216 this list.")
217
218 ;;----------------------------------------------------------------
219 ;; Internal variables
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 ;; Non-nil means we should upcase
256 ;; when copying successive words.
257 (defvar dabbrev--last-case-pattern nil)
258
259 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
260 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
261
262 ;; The regexp for recognizing a character in an abbreviation.
263 (defvar dabbrev--abbrev-char-regexp nil)
264
265 ;;----------------------------------------------------------------
266 ;; Macros
267 ;;----------------------------------------------------------------
268
269 ;;; Get the buffer that mini-buffer was activated from
270 (defsubst dabbrev--minibuffer-origin ()
271 (car (cdr (buffer-list))))
272
273 ;; Make a list of some of the elements of LIST.
274 ;; Check each element of LIST, storing it temporarily in the
275 ;; variable ELEMENT, and include it in the result
276 ;; if CONDITION evaluates non-nil.
277 (defmacro dabbrev-filter-elements (element list condition)
278 (` (let (dabbrev-result dabbrev-tail (, element))
279 (setq dabbrev-tail (, list))
280 (while dabbrev-tail
281 (setq (, element) (car dabbrev-tail))
282 (if (, condition)
283 (setq dabbrev-result (cons (, element) dabbrev-result)))
284 (setq dabbrev-tail (cdr dabbrev-tail)))
285 (nreverse dabbrev-result))))
286
287 ;;----------------------------------------------------------------
288 ;; Exported functions
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 (completion-ignore-case ignore-case-p))
347 ;; Make an obarray with all expansions
348 (setq my-obarray (make-vector (length completion-list) 0))
349 (or (> (length my-obarray) 0)
350 (error "No dynamic expansion for \"%s\" found%s"
351 abbrev
352 (if dabbrev--check-other-buffers "" " in this-buffer")))
353 (cond
354 ((or (not ignore-case-p)
355 (not dabbrev-case-replace))
356 (mapcar (function (lambda (string)
357 (intern string my-obarray)))
358 completion-list))
359 ((string= abbrev (upcase abbrev))
360 (mapcar (function (lambda (string)
361 (intern (upcase string) my-obarray)))
362 completion-list))
363 ((string= (substring abbrev 0 1)
364 (upcase (substring abbrev 0 1)))
365 (mapcar (function (lambda (string)
366 (intern (capitalize string) my-obarray)))
367 completion-list))
368 (t
369 (mapcar (function (lambda (string)
370 (intern (downcase string) my-obarray)))
371 completion-list)))
372 (setq dabbrev--last-obarray my-obarray)
373 (setq dabbrev--last-completion-buffer (current-buffer))
374 ;; Find the longest common string.
375 (setq init (try-completion abbrev my-obarray)))))
376 ;;--------------------------------
377 ;; Let the user choose between the expansions
378 ;;--------------------------------
379 (or (stringp init)
380 (setq init abbrev))
381 (cond
382 ;; * Replace string fragment with matched common substring completion.
383 ((and (not (string-equal init ""))
384 (not (string-equal (downcase init) (downcase abbrev))))
385 (if (> (length (all-completions init my-obarray)) 1)
386 (message "Repeat `%s' to see all completions"
387 (key-description (this-command-keys)))
388 (message "The only possible completion"))
389 (dabbrev--substitute-expansion nil abbrev init))
390 (t
391 ;; * String is a common substring completion already. Make list.
392 (message "Making completion list...")
393 (with-output-to-temp-buffer " *Completions*"
394 (display-completion-list (all-completions init my-obarray)))
395 (message "Making completion list...done")))
396 (and (window-minibuffer-p (selected-window))
397 (message nil))))
398
399 ;;;###autoload
400 (defun dabbrev-expand (arg)
401 "Expand previous word \"dynamically\".
402
403 Expands to the most recent, preceding word for which this is a prefix.
404 If no suitable preceding word is found, words following point are
405 considered. If still no suitable word is found, then look in the
406 buffers accepted by the function pointed out by variable
407 `dabbrev-friend-buffer-function'.
408
409 A positive prefix argument, N, says to take the Nth backward *distinct*
410 possibility. A negative argument says search forward.
411
412 If the cursor has not moved from the end of the previous expansion and
413 no argument is given, replace the previously-made expansion
414 with the next possible expansion not yet tried.
415
416 The variable `dabbrev-backward-only' may be used to limit the
417 direction of search to backward if set non-nil.
418
419 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
420 (interactive "*P")
421 (let (abbrev record-case-pattern
422 expansion old direction (orig-point (point)))
423 ;; abbrev -- the abbrev to expand
424 ;; expansion -- the expansion found (eventually) or nil until then
425 ;; old -- the text currently in the buffer
426 ;; (the abbrev, or the previously-made expansion)
427 (save-excursion
428 (if (and (null arg)
429 (markerp dabbrev--last-abbrev-location)
430 (marker-position dabbrev--last-abbrev-location)
431 (or (eq last-command this-command)
432 (and (window-minibuffer-p (selected-window))
433 (= dabbrev--last-abbrev-location
434 (point)))))
435 ;; Find a different expansion for the same abbrev as last time.
436 (progn
437 (setq abbrev dabbrev--last-abbreviation)
438 (setq old dabbrev--last-expansion)
439 (setq direction dabbrev--last-direction))
440 ;; If the user inserts a space after expanding
441 ;; and then asks to expand again, always fetch the next word.
442 (if (and (eq (preceding-char) ?\ )
443 (markerp dabbrev--last-abbrev-location)
444 (marker-position dabbrev--last-abbrev-location)
445 (= (point) (1+ dabbrev--last-abbrev-location)))
446 (progn
447 ;; The "abbrev" to expand is just the space.
448 (setq abbrev " ")
449 (save-excursion
450 (if dabbrev--last-buffer
451 (set-buffer dabbrev--last-buffer))
452 ;; Find the end of the last "expansion" word.
453 (if (or (eq dabbrev--last-direction 1)
454 (and (eq dabbrev--last-direction 0)
455 (< dabbrev--last-expansion-location (point))))
456 (setq dabbrev--last-expansion-location
457 (+ dabbrev--last-expansion-location
458 (length dabbrev--last-expansion))))
459 (goto-char dabbrev--last-expansion-location)
460 ;; Take the following word, with intermediate separators,
461 ;; as our expansion this time.
462 (re-search-forward
463 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
464 (setq expansion
465 (buffer-substring dabbrev--last-expansion-location
466 (point)))
467 (if dabbrev--last-case-pattern
468 (setq expansion (upcase expansion)))
469
470 ;; Record the end of this expansion, in case we repeat this.
471 (setq dabbrev--last-expansion-location (point)))
472 ;; Indicate that dabbrev--last-expansion-location is
473 ;; at the end of the expansion.
474 (setq dabbrev--last-direction -1))
475
476 ;; We have a different abbrev to expand.
477 (dabbrev--reset-global-variables)
478 (setq direction (if (null arg)
479 (if dabbrev-backward-only 1 0)
480 (prefix-numeric-value arg)))
481 (setq abbrev (dabbrev--abbrev-at-point))
482 (setq record-case-pattern t)
483 (setq old nil)))
484
485 ;;--------------------------------
486 ;; Find the expansion
487 ;;--------------------------------
488 (or expansion
489 (setq expansion
490 (dabbrev--find-expansion abbrev direction
491 (and (eval dabbrev-case-fold-search)
492 (or (not dabbrev-upcase-means-case-search)
493 (string= abbrev (downcase abbrev))))))))
494 (cond
495 ((not expansion)
496 (dabbrev--reset-global-variables)
497 (if old
498 (save-excursion
499 (setq buffer-undo-list (cons orig-point buffer-undo-list))
500 ;; Put back the original abbrev with its original case pattern.
501 (search-backward old)
502 (insert abbrev)
503 (delete-region (point) (+ (point) (length old)))))
504 (error "No%s dynamic expansion for `%s' found"
505 (if old " further" "") abbrev))
506 (t
507 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found))
508 (progn
509 (message "Expansion found in '%s'"
510 (buffer-name dabbrev--last-buffer))
511 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
512 (message nil))
513 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
514 (null dabbrev--last-buffer))
515 (numberp dabbrev--last-expansion-location)
516 (and (> dabbrev--last-expansion-location (point))))
517 (setq dabbrev--last-expansion-location
518 (copy-marker dabbrev--last-expansion-location)))
519 ;; Success: stick it in and return.
520 (setq buffer-undo-list (cons orig-point buffer-undo-list))
521 (dabbrev--substitute-expansion old abbrev expansion)
522
523 ;; If we are not copying successive words now,
524 ;; set dabbrev--last-case-pattern.
525 (and record-case-pattern
526 (setq dabbrev--last-case-pattern
527 (and (eval dabbrev-case-fold-search)
528 (not dabbrev-upcase-means-case-search)
529 (equal abbrev (upcase abbrev)))))
530
531 ;; Save state for re-expand.
532 (setq dabbrev--last-expansion expansion)
533 (setq dabbrev--last-abbreviation abbrev)
534 (setq dabbrev--last-abbrev-location (point-marker))))))
535
536 ;;----------------------------------------------------------------
537 ;; Local functions
538 ;;----------------------------------------------------------------
539
540 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
541 (defun dabbrev--same-major-mode-p (other-buffer)
542 (eq major-mode
543 (save-excursion
544 (set-buffer other-buffer)
545 major-mode)))
546
547 ;;; Back over all abbrev type characters and then moves forward over
548 ;;; all skip characters.
549 (defun dabbrev--goto-start-of-abbrev ()
550 ;; Move backwards over abbrev chars
551 (save-match-data
552 (if (not (bobp))
553 (progn
554 (forward-char -1)
555 (while (and (looking-at dabbrev--abbrev-char-regexp)
556 (not (bobp)))
557 (forward-char -1))
558 (or (looking-at dabbrev--abbrev-char-regexp)
559 (forward-char 1))))
560 (and dabbrev-abbrev-skip-leading-regexp
561 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
562 (forward-char 1)))))
563
564 ;;; Extract the symbol at point to serve as abbreviation.
565 (defun dabbrev--abbrev-at-point ()
566 ;; Check for error
567 (if (bobp)
568 (error "No possible abbreviation preceding point"))
569 ;; Return abbrev at point
570 (save-excursion
571 ;; Record the end of the abbreviation.
572 (setq dabbrev--last-abbrev-location (point))
573 ;; If we aren't right after an abbreviation,
574 ;; move point back to just after one.
575 ;; This is so the user can get successive words
576 ;; by typing the punctuation followed by M-/.
577 (save-match-data
578 (if (save-excursion
579 (forward-char -1)
580 (not (looking-at (concat "\\("
581 (or dabbrev-abbrev-char-regexp
582 "\\sw\\|\\s_")
583 "\\)+"))))
584 (if (re-search-backward (or dabbrev-abbrev-char-regexp
585 "\\sw\\|\\s_")
586 nil t)
587 (forward-char 1)
588 (error "No possible abbreviation preceding point"))))
589 ;; Now find the beginning of that one.
590 (dabbrev--goto-start-of-abbrev)
591 (buffer-substring dabbrev--last-abbrev-location
592 (point))))
593
594 ;;; Initializes all global variables
595 (defun dabbrev--reset-global-variables ()
596 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
597 ;; must not be reset here.
598 (setq dabbrev--last-table nil
599 dabbrev--last-abbreviation nil
600 dabbrev--last-abbrev-location nil
601 dabbrev--last-direction nil
602 dabbrev--last-expansion nil
603 dabbrev--last-expansion-location nil
604 dabbrev--friend-buffer-list nil
605 dabbrev--last-buffer nil
606 dabbrev--last-buffer-found nil
607 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
608 "\\sw\\|\\s_")
609 dabbrev--check-other-buffers dabbrev-check-other-buffers))
610
611 ;;; Find all buffers that are considered "friends" according to the
612 ;;; function pointed out by dabbrev-friend-buffer-function.
613 (defun dabbrev--select-buffers ()
614 (save-excursion
615 (and (window-minibuffer-p (selected-window))
616 (set-buffer (dabbrev--minibuffer-origin)))
617 (let ((orig-buffer (current-buffer)))
618 (dabbrev-filter-elements
619 buffer (buffer-list)
620 (and (not (eq orig-buffer buffer))
621 (boundp 'dabbrev-friend-buffer-function)
622 (funcall dabbrev-friend-buffer-function buffer))))))
623
624 ;;; Search for ABBREV, N times, normally looking forward,
625 ;;; but looking in reverse instead if REVERSE is non-nil.
626 (defun dabbrev--try-find (abbrev reverse n ignore-case)
627 (save-excursion
628 (save-restriction
629 (widen)
630 (let ((expansion nil))
631 (and dabbrev--last-expansion-location
632 (goto-char dabbrev--last-expansion-location))
633 (let ((case-fold-search ignore-case)
634 (count n))
635 (while (and (> count 0)
636 (setq expansion (dabbrev--search abbrev
637 reverse
638 ignore-case)))
639 (setq count (1- count))))
640 (and expansion
641 (setq dabbrev--last-expansion-location (point)))
642 expansion))))
643
644 ;;; Find all expansions of ABBREV
645 (defun dabbrev--find-all-expansions (abbrev ignore-case)
646 (let ((all-expansions nil)
647 expansion)
648 (save-excursion
649 (goto-char (point-min))
650 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
651 (setq all-expansions (cons expansion all-expansions))))
652 all-expansions))
653
654 (defun dabbrev--scanning-message ()
655 (message "Scanning `%s'" (buffer-name (current-buffer))))
656
657 ;;; Find one occasion of ABBREV.
658 ;;; DIRECTION > 0 means look that many times backwards.
659 ;;; DIRECTION < 0 means look that many times forward.
660 ;;; DIRECTION = 0 means try both backward and forward.
661 ;;; IGNORE-CASE non-nil means ignore case when searching.
662 (defun dabbrev--find-expansion (abbrev direction ignore-case)
663 (let (expansion)
664 (save-excursion
665 (cond
666 (dabbrev--last-buffer
667 (set-buffer dabbrev--last-buffer)
668 (dabbrev--scanning-message))
669 ((and (not dabbrev-search-these-buffers-only)
670 (window-minibuffer-p (selected-window)))
671 (set-buffer (dabbrev--minibuffer-origin))
672 ;; In the minibuffer-origin buffer we will only search from
673 ;; the top and down.
674 (goto-char (point-min))
675 (setq direction -1)
676 (dabbrev--scanning-message)))
677 (cond
678 ;; ------------------------------------------
679 ;; Look backwards
680 ;; ------------------------------------------
681 ((and (not dabbrev-search-these-buffers-only)
682 (>= direction 0)
683 (setq dabbrev--last-direction (min 1 direction))
684 (setq expansion (dabbrev--try-find abbrev t
685 (max 1 direction)
686 ignore-case)))
687 expansion)
688 ;; ------------------------------------------
689 ;; Look forward
690 ;; ------------------------------------------
691 ((and (or (not dabbrev-search-these-buffers-only)
692 dabbrev--last-buffer)
693 (<= direction 0)
694 (setq dabbrev--last-direction -1)
695 (setq expansion (dabbrev--try-find abbrev nil
696 (max 1 (- direction))
697 ignore-case)))
698 expansion)
699 ;; ------------------------------------------
700 ;; Look in other buffers.
701 ;; Start at (point-min) and look forward.
702 ;; ------------------------------------------
703 (t
704 (setq dabbrev--last-direction -1)
705 ;; Make sure that we should check other buffers
706 (or dabbrev--friend-buffer-list
707 dabbrev--last-buffer
708 (setq dabbrev--friend-buffer-list
709 (mapcar (function get-buffer)
710 dabbrev-search-these-buffers-only))
711 (not dabbrev--check-other-buffers)
712 (not (or (eq dabbrev--check-other-buffers t)
713 (progn
714 (setq dabbrev--check-other-buffers
715 (y-or-n-p "Scan other buffers also? ")))))
716 (let* (friend-buffer-list non-friend-buffer-list)
717 (setq dabbrev--friend-buffer-list
718 (funcall dabbrev-select-buffers-function))
719 (if dabbrev-check-all-buffers
720 (setq non-friend-buffer-list
721 (nreverse
722 (dabbrev-filter-elements
723 buffer (buffer-list)
724 (not (memq buffer dabbrev--friend-buffer-list))))
725 dabbrev--friend-buffer-list
726 (append dabbrev--friend-buffer-list
727 non-friend-buffer-list)))))
728 ;; Move buffers that are visible on the screen
729 ;; to the front of the list.
730 (if dabbrev--friend-buffer-list
731 (let ((w (next-window (selected-window))))
732 (while (not (eq w (selected-window)))
733 (setq dabbrev--friend-buffer-list
734 (cons (window-buffer w)
735 (delq (window-buffer w) dabbrev--friend-buffer-list)))
736 (setq w (next-window w)))))
737 ;; Walk through the buffers
738 (while (and (not expansion) dabbrev--friend-buffer-list)
739 (setq dabbrev--last-buffer
740 (car dabbrev--friend-buffer-list))
741 (setq dabbrev--friend-buffer-list
742 (cdr dabbrev--friend-buffer-list))
743 (set-buffer dabbrev--last-buffer)
744 (dabbrev--scanning-message)
745 (setq dabbrev--last-expansion-location (point-min))
746 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
747 expansion)))))
748
749 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
750 (if (eq major-mode 'picture-mode)
751 (picture-replace-match string fixedcase literal)
752 (replace-match string fixedcase literal)))
753
754 ;;;----------------------------------------------------------------
755 ;;; Substitute the current string in buffer with the expansion
756 ;;; OLD is nil or the last expansion substring.
757 ;;; ABBREV is the abbreviation we are working with.
758 ;;; EXPANSION is the expansion substring.
759 (defun dabbrev--substitute-expansion (old abbrev expansion)
760 ;;(undo-boundary)
761 (let ((use-case-replace (and (eval dabbrev-case-fold-search)
762 (or (not dabbrev-upcase-means-case-search)
763 (string= abbrev (downcase abbrev)))
764 (eval dabbrev-case-replace))))
765 (and nil use-case-replace
766 (setq old (concat abbrev (or old "")))
767 (setq expansion (concat abbrev expansion)))
768 ;; If the expansion has mixed case
769 ;; and it is not simply a capitalized word,
770 ;; or if the abbrev has mixed case,
771 ;; and if the given abbrev's case pattern
772 ;; matches the start of the expansion,
773 ;; copy the expansion's case
774 ;; instead of downcasing all the rest.
775 (let ((expansion-rest (substring expansion 1)))
776 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
777 (string= expansion-rest (upcase expansion-rest)))
778 (or (string= abbrev (downcase abbrev))
779 (string= abbrev (upcase abbrev)))))
780 (string= abbrev
781 (substring expansion 0 (length abbrev))))
782 (setq use-case-replace nil)))
783 (if (equal abbrev " ")
784 (setq use-case-replace nil))
785 (if use-case-replace
786 (setq expansion (downcase expansion)))
787 (if old
788 (save-excursion
789 (search-backward old))
790 ;;(store-match-data (list (point-marker) (point-marker)))
791 (search-backward abbrev))
792 ;; Make case of replacement conform to case of abbreviation
793 ;; provided (1) that kind of thing is enabled in this buffer
794 ;; and (2) the replacement itself is all lower case.
795 (dabbrev--safe-replace-match expansion
796 (not use-case-replace)
797 t)))
798
799
800 ;;;----------------------------------------------------------------
801 ;;; Search function used by dabbrevs library.
802
803 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
804 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
805 ;;; controls the maximum search region size. Third argument IGNORE-CASE
806 ;;; non-nil means treat case as insignificant while looking for a match
807 ;;; and when comparing with previous matches. Also if that's non-nil
808 ;;; and the match is found at the beginning of a sentence and is in
809 ;;; lower case except for the initial then it is converted to all lower
810 ;;; case for return.
811
812 ;;; Table of expansions already seen is examined in buffer
813 ;;; `dabbrev--last-table' so that only distinct possibilities are found
814 ;;; by dabbrev-re-expand.
815
816 ;;; Value is the expansion, or nil if not found.
817
818 (defun dabbrev--search (abbrev reverse ignore-case)
819 (save-match-data
820 (let ((pattern1 (concat (regexp-quote abbrev)
821 "\\(" dabbrev--abbrev-char-regexp "\\)"))
822 (pattern2 (concat (regexp-quote abbrev)
823 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
824 (found-string nil))
825 ;; Limited search.
826 (save-restriction
827 (and dabbrev-limit
828 (narrow-to-region dabbrev--last-expansion-location
829 (+ (point)
830 (if reverse (- dabbrev-limit) dabbrev-limit))))
831 ;;--------------------------------
832 ;; Look for a distinct expansion, using dabbrev--last-table.
833 ;;--------------------------------
834 (while (and (not found-string)
835 (if reverse
836 (re-search-backward pattern1 nil t)
837 (re-search-forward pattern1 nil t)))
838 (goto-char (match-beginning 0))
839 ;; In case we matched in the middle of a word,
840 ;; back up to start of word and verify we still match.
841 (dabbrev--goto-start-of-abbrev)
842
843 (if (not (looking-at pattern1))
844 nil
845 ;; We have a truly valid match. Find the end.
846 (re-search-forward pattern2)
847 (setq found-string
848 (buffer-substring (match-beginning 1) (match-end 1)))
849 (and ignore-case (setq found-string (downcase found-string)))
850 ;; Ignore this match if it's already in the table.
851 (if (dabbrev-filter-elements
852 table-string dabbrev--last-table
853 (string= found-string table-string))
854 (setq found-string nil)))
855 ;; Prepare to continue searching.
856 (if reverse
857 (goto-char (match-beginning 0))
858 (goto-char (match-end 0))))
859 ;; If we found something, use it.
860 (if found-string
861 ;; Put it into `dabbrev--last-table'
862 ;; and return it (either downcased, or as is).
863 (let ((result
864 (buffer-substring (match-beginning 0) (match-end 0))))
865 (setq dabbrev--last-table
866 (cons found-string dabbrev--last-table))
867 (if (and ignore-case (eval dabbrev-case-replace))
868 result
869 result)))))))
870
871 (provide 'dabbrev)
872
873 ;;; dabbrev.el ends here