*** empty log message ***
[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@ql.org>
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, except those named in `dabbrev-ignored-buffer-names'."
196 :type 'boolean
197 :group 'dabbrev)
198
199 (defcustom dabbrev-ignored-buffer-names '("*Messages")
200 "*List of buffer names that dabbrev should not check."
201 :type '(repeat (string :tag "Buffer name"))
202 :group 'dabbrev
203 :version "20.3")
204
205 (defcustom dabbrev-check-other-buffers t
206 "*Should \\[dabbrev-expand] look in other buffers?\
207
208 nil: Don't look in other buffers.
209 t: Also look for expansions in the buffers pointed out by
210 `dabbrev-select-buffers-function'.
211 Anything else: When we can't find any more expansions in
212 the current buffer, then ask the user whether to look in other
213 buffers too.
214
215 The default value is t."
216 :type '(choice (const :tag "off" nil)
217 (const :tag "on" t)
218 (const :tag "ask" other))
219 :group 'dabbrev)
220
221 ;; I guess setting this to a function that selects all C- or C++-
222 ;; mode buffers would be a good choice for a debugging buffer,
223 ;; when debugging C- or C++-code.
224 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
225 "A function that selects buffers that should be searched by dabbrev.
226 The function should take no arguments and return a list of buffers to
227 search for expansions. Have a look at `dabbrev--select-buffers' for
228 an example.
229
230 A mode setting this variable should make it buffer local.")
231
232 (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
233 "*A function to decide whether dabbrev should search OTHER-BUFFER.
234 The function should take one argument, OTHER-BUFFER, and return
235 non-nil if that buffer should be searched. Have a look at
236 `dabbrev--same-major-mode-p' for an example.
237
238 The value of `dabbrev-friend-buffer-function' has an effect only if
239 the value of `dabbrev-select-buffers-function' uses it. The function
240 `dabbrev--select-buffers' is one function you can use here.
241
242 A mode setting this variable should make it buffer local."
243 :type 'function
244 :group 'dabbrev)
245
246 (defcustom dabbrev-search-these-buffers-only nil
247 "If non-nil, a list of buffers which dabbrev should search.
248 If this variable is non-nil, dabbrev will only look in these buffers.
249 It will not even look in the current buffer if it is not a member of
250 this list.")
251
252 ;;----------------------------------------------------------------
253 ;; Internal variables
254 ;;----------------------------------------------------------------
255
256 ;; Last obarray of completions in `dabbrev-completion'
257 (defvar dabbrev--last-obarray nil)
258
259 ;; Table of expansions seen so far
260 (defvar dabbrev--last-table nil)
261
262 ;; Last string we tried to expand.
263 (defvar dabbrev--last-abbreviation nil)
264
265 ;; Location last abbreviation began
266 (defvar dabbrev--last-abbrev-location nil)
267
268 ;; Direction of last dabbrevs search
269 (defvar dabbrev--last-direction 0)
270
271 ;; Last expansion of an abbreviation.
272 (defvar dabbrev--last-expansion nil)
273
274 ;; Location the last expansion was found.
275 (defvar dabbrev--last-expansion-location nil)
276
277 ;; The list of remaining buffers with the same mode as current buffer.
278 (defvar dabbrev--friend-buffer-list nil)
279
280 ;; The buffer we looked in last.
281 (defvar dabbrev--last-buffer nil)
282
283 ;; The buffer we found the expansion last time.
284 (defvar dabbrev--last-buffer-found nil)
285
286 ;; The buffer we last did a completion in.
287 (defvar dabbrev--last-completion-buffer nil)
288
289 ;; Non-nil means we should upcase
290 ;; when copying successive words.
291 (defvar dabbrev--last-case-pattern nil)
292
293 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
294 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
295
296 ;; The regexp for recognizing a character in an abbreviation.
297 (defvar dabbrev--abbrev-char-regexp nil)
298
299 ;;----------------------------------------------------------------
300 ;; Macros
301 ;;----------------------------------------------------------------
302
303 ;;; Get the buffer that mini-buffer was activated from
304 (defsubst dabbrev--minibuffer-origin ()
305 (car (cdr (buffer-list))))
306
307 ;; Make a list of some of the elements of LIST.
308 ;; Check each element of LIST, storing it temporarily in the
309 ;; variable ELEMENT, and include it in the result
310 ;; if CONDITION evaluates non-nil.
311 (defmacro dabbrev-filter-elements (element list condition)
312 (` (let (dabbrev-result dabbrev-tail (, element))
313 (setq dabbrev-tail (, list))
314 (while dabbrev-tail
315 (setq (, element) (car dabbrev-tail))
316 (if (, condition)
317 (setq dabbrev-result (cons (, element) dabbrev-result)))
318 (setq dabbrev-tail (cdr dabbrev-tail)))
319 (nreverse dabbrev-result))))
320
321 ;;----------------------------------------------------------------
322 ;; Exported functions
323 ;;----------------------------------------------------------------
324
325 ;;;###autoload
326 (define-key esc-map "/" 'dabbrev-expand)
327 ;;;??? Do we want this?
328 ;;;###autoload
329 (define-key esc-map [?\C-/] 'dabbrev-completion)
330
331 ;;;###autoload
332 (defun dabbrev-completion (&optional arg)
333 "Completion on current word.
334 Like \\[dabbrev-expand] but finds all expansions in the current buffer
335 and presents suggestions for completion.
336
337 With a prefix argument, it searches all buffers accepted by the
338 function pointed out by `dabbrev-friend-buffer-function' to find the
339 completions.
340
341 If the prefix argument is 16 (which comes from C-u C-u),
342 then it searches *all* buffers.
343
344 With no prefix argument, it reuses an old completion list
345 if there is a suitable one already."
346
347 (interactive "*P")
348 (dabbrev--reset-global-variables)
349 (let* ((dabbrev-check-other-buffers (and arg t))
350 (dabbrev-check-all-buffers
351 (and arg (= (prefix-numeric-value arg) 16)))
352 (abbrev (dabbrev--abbrev-at-point))
353 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
354 case-fold-search
355 dabbrev-case-fold-search)
356 (or (not dabbrev-upcase-means-case-search)
357 (string= abbrev (downcase abbrev)))))
358 (my-obarray dabbrev--last-obarray)
359 init)
360 (save-excursion
361 (if (and (null arg)
362 my-obarray
363 (or (eq dabbrev--last-completion-buffer (current-buffer))
364 (and (window-minibuffer-p (selected-window))
365 (eq dabbrev--last-completion-buffer
366 (dabbrev--minibuffer-origin))))
367 dabbrev--last-abbreviation
368 (>= (length abbrev) (length dabbrev--last-abbreviation))
369 (string= dabbrev--last-abbreviation
370 (substring abbrev 0
371 (length dabbrev--last-abbreviation)))
372 (setq init (try-completion abbrev my-obarray)))
373 ;; We can reuse the existing completion list.
374 nil
375 ;;--------------------------------
376 ;; New abbreviation to expand.
377 ;;--------------------------------
378 (setq dabbrev--last-abbreviation abbrev)
379 ;; Find all expansion
380 (let ((completion-list
381 (dabbrev--find-all-expansions abbrev ignore-case-p))
382 (completion-ignore-case ignore-case-p))
383 ;; Make an obarray with all expansions
384 (setq my-obarray (make-vector (length completion-list) 0))
385 (or (> (length my-obarray) 0)
386 (error "No dynamic expansion for \"%s\" found%s"
387 abbrev
388 (if dabbrev--check-other-buffers "" " in this-buffer")))
389 (cond
390 ((or (not ignore-case-p)
391 (not dabbrev-case-replace))
392 (mapcar (function (lambda (string)
393 (intern string my-obarray)))
394 completion-list))
395 ((string= abbrev (upcase abbrev))
396 (mapcar (function (lambda (string)
397 (intern (upcase string) my-obarray)))
398 completion-list))
399 ((string= (substring abbrev 0 1)
400 (upcase (substring abbrev 0 1)))
401 (mapcar (function (lambda (string)
402 (intern (capitalize string) my-obarray)))
403 completion-list))
404 (t
405 (mapcar (function (lambda (string)
406 (intern (downcase string) my-obarray)))
407 completion-list)))
408 (setq dabbrev--last-obarray my-obarray)
409 (setq dabbrev--last-completion-buffer (current-buffer))
410 ;; Find the longest common string.
411 (setq init (try-completion abbrev my-obarray)))))
412 ;;--------------------------------
413 ;; Let the user choose between the expansions
414 ;;--------------------------------
415 (or (stringp init)
416 (setq init abbrev))
417 (cond
418 ;; * Replace string fragment with matched common substring completion.
419 ((and (not (string-equal init ""))
420 (not (string-equal (downcase init) (downcase abbrev))))
421 (if (> (length (all-completions init my-obarray)) 1)
422 (message "Repeat `%s' to see all completions"
423 (key-description (this-command-keys)))
424 (message "The only possible completion"))
425 (dabbrev--substitute-expansion nil abbrev init))
426 (t
427 ;; * String is a common substring completion already. Make list.
428 (message "Making completion list...")
429 (with-output-to-temp-buffer " *Completions*"
430 (display-completion-list (all-completions init my-obarray)))
431 (message "Making completion list...done")))
432 (and (window-minibuffer-p (selected-window))
433 (message nil))))
434
435 ;;;###autoload
436 (defun dabbrev-expand (arg)
437 "Expand previous word \"dynamically\".
438
439 Expands to the most recent, preceding word for which this is a prefix.
440 If no suitable preceding word is found, words following point are
441 considered. If still no suitable word is found, then look in the
442 buffers accepted by the function pointed out by variable
443 `dabbrev-friend-buffer-function'.
444
445 A positive prefix argument, N, says to take the Nth backward *distinct*
446 possibility. A negative argument says search forward.
447
448 If the cursor has not moved from the end of the previous expansion and
449 no argument is given, replace the previously-made expansion
450 with the next possible expansion not yet tried.
451
452 The variable `dabbrev-backward-only' may be used to limit the
453 direction of search to backward if set non-nil.
454
455 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
456 (interactive "*P")
457 (let (abbrev record-case-pattern
458 expansion old direction (orig-point (point)))
459 ;; abbrev -- the abbrev to expand
460 ;; expansion -- the expansion found (eventually) or nil until then
461 ;; old -- the text currently in the buffer
462 ;; (the abbrev, or the previously-made expansion)
463 (save-excursion
464 (if (and (null arg)
465 (markerp dabbrev--last-abbrev-location)
466 (marker-position dabbrev--last-abbrev-location)
467 (or (eq last-command this-command)
468 (and (window-minibuffer-p (selected-window))
469 (= dabbrev--last-abbrev-location
470 (point)))))
471 ;; Find a different expansion for the same abbrev as last time.
472 (progn
473 (setq abbrev dabbrev--last-abbreviation)
474 (setq old dabbrev--last-expansion)
475 (setq direction dabbrev--last-direction))
476 ;; If the user inserts a space after expanding
477 ;; and then asks to expand again, always fetch the next word.
478 (if (and (eq (preceding-char) ?\ )
479 (markerp dabbrev--last-abbrev-location)
480 (marker-position dabbrev--last-abbrev-location)
481 (= (point) (1+ dabbrev--last-abbrev-location)))
482 (progn
483 ;; The "abbrev" to expand is just the space.
484 (setq abbrev " ")
485 (save-excursion
486 (if dabbrev--last-buffer
487 (set-buffer dabbrev--last-buffer))
488 ;; Find the end of the last "expansion" word.
489 (if (or (eq dabbrev--last-direction 1)
490 (and (eq dabbrev--last-direction 0)
491 (< dabbrev--last-expansion-location (point))))
492 (setq dabbrev--last-expansion-location
493 (+ dabbrev--last-expansion-location
494 (length dabbrev--last-expansion))))
495 (goto-char dabbrev--last-expansion-location)
496 ;; Take the following word, with intermediate separators,
497 ;; as our expansion this time.
498 (re-search-forward
499 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
500 (setq expansion (buffer-substring-no-properties
501 dabbrev--last-expansion-location (point)))
502 (if dabbrev--last-case-pattern
503 (setq expansion (upcase expansion)))
504
505 ;; Record the end of this expansion, in case we repeat this.
506 (setq dabbrev--last-expansion-location (point)))
507 ;; Indicate that dabbrev--last-expansion-location is
508 ;; at the end of the expansion.
509 (setq dabbrev--last-direction -1))
510
511 ;; We have a different abbrev to expand.
512 (dabbrev--reset-global-variables)
513 (setq direction (if (null arg)
514 (if dabbrev-backward-only 1 0)
515 (prefix-numeric-value arg)))
516 (setq abbrev (dabbrev--abbrev-at-point))
517 (setq record-case-pattern t)
518 (setq old nil)))
519
520 ;;--------------------------------
521 ;; Find the expansion
522 ;;--------------------------------
523 (or expansion
524 (setq expansion
525 (dabbrev--find-expansion abbrev direction
526 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
527 case-fold-search
528 dabbrev-case-fold-search)
529 (or (not dabbrev-upcase-means-case-search)
530 (string= abbrev (downcase abbrev))))))))
531 (cond
532 ((not expansion)
533 (dabbrev--reset-global-variables)
534 (if old
535 (save-excursion
536 (setq buffer-undo-list (cons orig-point buffer-undo-list))
537 ;; Put back the original abbrev with its original case pattern.
538 (search-backward old)
539 (insert abbrev)
540 (delete-region (point) (+ (point) (length old)))))
541 (error "No%s dynamic expansion for `%s' found"
542 (if old " further" "") abbrev))
543 (t
544 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found))
545 (progn
546 (message "Expansion found in '%s'"
547 (buffer-name dabbrev--last-buffer))
548 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
549 (message nil))
550 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
551 (null dabbrev--last-buffer))
552 (numberp dabbrev--last-expansion-location)
553 (and (> dabbrev--last-expansion-location (point))))
554 (setq dabbrev--last-expansion-location
555 (copy-marker dabbrev--last-expansion-location)))
556 ;; Success: stick it in and return.
557 (setq buffer-undo-list (cons orig-point buffer-undo-list))
558 (dabbrev--substitute-expansion old abbrev expansion)
559
560 ;; If we are not copying successive words now,
561 ;; set dabbrev--last-case-pattern.
562 (and record-case-pattern
563 (setq dabbrev--last-case-pattern
564 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
565 case-fold-search
566 dabbrev-case-fold-search)
567 (not dabbrev-upcase-means-case-search)
568 (equal abbrev (upcase abbrev)))))
569
570 ;; Save state for re-expand.
571 (setq dabbrev--last-expansion expansion)
572 (setq dabbrev--last-abbreviation abbrev)
573 (setq dabbrev--last-abbrev-location (point-marker))))))
574
575 ;;----------------------------------------------------------------
576 ;; Local functions
577 ;;----------------------------------------------------------------
578
579 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
580 (defun dabbrev--same-major-mode-p (other-buffer)
581 (eq major-mode
582 (save-excursion
583 (set-buffer other-buffer)
584 major-mode)))
585
586 ;;; Back over all abbrev type characters and then moves forward over
587 ;;; all skip characters.
588 (defun dabbrev--goto-start-of-abbrev ()
589 ;; Move backwards over abbrev chars
590 (save-match-data
591 (if (not (bobp))
592 (progn
593 (forward-char -1)
594 (while (and (looking-at dabbrev--abbrev-char-regexp)
595 (not (bobp)))
596 (forward-char -1))
597 (or (looking-at dabbrev--abbrev-char-regexp)
598 (forward-char 1))))
599 (and dabbrev-abbrev-skip-leading-regexp
600 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
601 (forward-char 1)))))
602
603 ;;; Extract the symbol at point to serve as abbreviation.
604 (defun dabbrev--abbrev-at-point ()
605 ;; Check for error
606 (if (bobp)
607 (error "No possible abbreviation preceding point"))
608 ;; Return abbrev at point
609 (save-excursion
610 ;; Record the end of the abbreviation.
611 (setq dabbrev--last-abbrev-location (point))
612 ;; If we aren't right after an abbreviation,
613 ;; move point back to just after one.
614 ;; This is so the user can get successive words
615 ;; by typing the punctuation followed by M-/.
616 (save-match-data
617 (if (save-excursion
618 (forward-char -1)
619 (not (looking-at (concat "\\("
620 (or dabbrev-abbrev-char-regexp
621 "\\sw\\|\\s_")
622 "\\)+"))))
623 (if (re-search-backward (or dabbrev-abbrev-char-regexp
624 "\\sw\\|\\s_")
625 nil t)
626 (forward-char 1)
627 (error "No possible abbreviation preceding point"))))
628 ;; Now find the beginning of that one.
629 (dabbrev--goto-start-of-abbrev)
630 (buffer-substring-no-properties
631 dabbrev--last-abbrev-location (point))))
632
633 ;;; Initializes all global variables
634 (defun dabbrev--reset-global-variables ()
635 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
636 ;; must not be reset here.
637 (setq dabbrev--last-table nil
638 dabbrev--last-abbreviation nil
639 dabbrev--last-abbrev-location nil
640 dabbrev--last-direction nil
641 dabbrev--last-expansion nil
642 dabbrev--last-expansion-location nil
643 dabbrev--friend-buffer-list nil
644 dabbrev--last-buffer nil
645 dabbrev--last-buffer-found nil
646 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
647 "\\sw\\|\\s_")
648 dabbrev--check-other-buffers dabbrev-check-other-buffers))
649
650 ;;; Find all buffers that are considered "friends" according to the
651 ;;; function pointed out by dabbrev-friend-buffer-function.
652 (defun dabbrev--select-buffers ()
653 (save-excursion
654 (and (window-minibuffer-p (selected-window))
655 (set-buffer (dabbrev--minibuffer-origin)))
656 (let ((orig-buffer (current-buffer)))
657 (dabbrev-filter-elements
658 buffer (buffer-list)
659 (and (not (eq orig-buffer buffer))
660 (boundp 'dabbrev-friend-buffer-function)
661 (funcall dabbrev-friend-buffer-function buffer))))))
662
663 ;;; Search for ABBREV, N times, normally looking forward,
664 ;;; but looking in reverse instead if REVERSE is non-nil.
665 (defun dabbrev--try-find (abbrev reverse n ignore-case)
666 (save-excursion
667 (save-restriction
668 (widen)
669 (let ((expansion nil))
670 (and dabbrev--last-expansion-location
671 (goto-char dabbrev--last-expansion-location))
672 (let ((case-fold-search ignore-case)
673 (count n))
674 (while (and (> count 0)
675 (setq expansion (dabbrev--search abbrev
676 reverse
677 ignore-case)))
678 (setq count (1- count))))
679 (and expansion
680 (setq dabbrev--last-expansion-location (point)))
681 expansion))))
682
683 ;;; Find all expansions of ABBREV
684 (defun dabbrev--find-all-expansions (abbrev ignore-case)
685 (let ((all-expansions nil)
686 expansion)
687 (save-excursion
688 (goto-char (point-min))
689 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
690 (setq all-expansions (cons expansion all-expansions))))
691 all-expansions))
692
693 (defun dabbrev--scanning-message ()
694 (message "Scanning `%s'" (buffer-name (current-buffer))))
695
696 ;;; Find one occasion of ABBREV.
697 ;;; DIRECTION > 0 means look that many times backwards.
698 ;;; DIRECTION < 0 means look that many times forward.
699 ;;; DIRECTION = 0 means try both backward and forward.
700 ;;; IGNORE-CASE non-nil means ignore case when searching.
701 (defun dabbrev--find-expansion (abbrev direction ignore-case)
702 (let (expansion)
703 (save-excursion
704 (cond
705 (dabbrev--last-buffer
706 (set-buffer dabbrev--last-buffer)
707 (dabbrev--scanning-message))
708 ((and (not dabbrev-search-these-buffers-only)
709 (window-minibuffer-p (selected-window)))
710 (set-buffer (dabbrev--minibuffer-origin))
711 ;; In the minibuffer-origin buffer we will only search from
712 ;; the top and down.
713 (goto-char (point-min))
714 (setq direction -1)
715 (dabbrev--scanning-message)))
716 (cond
717 ;; ------------------------------------------
718 ;; Look backwards
719 ;; ------------------------------------------
720 ((and (not dabbrev-search-these-buffers-only)
721 (>= direction 0)
722 (setq dabbrev--last-direction (min 1 direction))
723 (setq expansion (dabbrev--try-find abbrev t
724 (max 1 direction)
725 ignore-case)))
726 expansion)
727 ;; ------------------------------------------
728 ;; Look forward
729 ;; ------------------------------------------
730 ((and (or (not dabbrev-search-these-buffers-only)
731 dabbrev--last-buffer)
732 (<= direction 0)
733 (setq dabbrev--last-direction -1)
734 (setq expansion (dabbrev--try-find abbrev nil
735 (max 1 (- direction))
736 ignore-case)))
737 expansion)
738 ;; ------------------------------------------
739 ;; Look in other buffers.
740 ;; Start at (point-min) and look forward.
741 ;; ------------------------------------------
742 (t
743 (setq dabbrev--last-direction -1)
744 ;; Make sure that we should check other buffers
745 (or dabbrev--friend-buffer-list
746 dabbrev--last-buffer
747 (setq dabbrev--friend-buffer-list
748 (mapcar (function get-buffer)
749 dabbrev-search-these-buffers-only))
750 (not dabbrev--check-other-buffers)
751 (not (or (eq dabbrev--check-other-buffers t)
752 (progn
753 (setq dabbrev--check-other-buffers
754 (y-or-n-p "Scan other buffers also? ")))))
755 (let* (friend-buffer-list non-friend-buffer-list)
756 (setq dabbrev--friend-buffer-list
757 (funcall dabbrev-select-buffers-function))
758 (if dabbrev-check-all-buffers
759 (setq non-friend-buffer-list
760 (nreverse
761 (dabbrev-filter-elements
762 buffer (buffer-list)
763 (and (not (member (buffer-name buffer)
764 dabbrev-ignored-buffer-names))
765 (not (memq buffer dabbrev--friend-buffer-list)))))
766 dabbrev--friend-buffer-list
767 (append dabbrev--friend-buffer-list
768 non-friend-buffer-list)))))
769 ;; Move buffers that are visible on the screen
770 ;; to the front of the list.
771 (if dabbrev--friend-buffer-list
772 (let ((w (next-window (selected-window))))
773 (while (not (eq w (selected-window)))
774 (setq dabbrev--friend-buffer-list
775 (cons (window-buffer w)
776 (delq (window-buffer w) dabbrev--friend-buffer-list)))
777 (setq w (next-window w)))))
778 ;; Walk through the buffers
779 (while (and (not expansion) dabbrev--friend-buffer-list)
780 (setq dabbrev--last-buffer
781 (car dabbrev--friend-buffer-list))
782 (setq dabbrev--friend-buffer-list
783 (cdr dabbrev--friend-buffer-list))
784 (set-buffer dabbrev--last-buffer)
785 (dabbrev--scanning-message)
786 (setq dabbrev--last-expansion-location (point-min))
787 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
788 expansion)))))
789
790 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
791 (if (eq major-mode 'picture-mode)
792 (picture-replace-match string fixedcase literal)
793 (replace-match string fixedcase literal)))
794
795 ;;;----------------------------------------------------------------
796 ;;; Substitute the current string in buffer with the expansion
797 ;;; OLD is nil or the last expansion substring.
798 ;;; ABBREV is the abbreviation we are working with.
799 ;;; EXPANSION is the expansion substring.
800 (defun dabbrev--substitute-expansion (old abbrev expansion)
801 ;;(undo-boundary)
802 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
803 case-fold-search
804 dabbrev-case-fold-search)
805 (or (not dabbrev-upcase-means-case-search)
806 (string= abbrev (downcase abbrev)))
807 (if (eq dabbrev-case-replace 'case-replace)
808 case-replace
809 dabbrev-case-replace))))
810 (and nil use-case-replace
811 (setq old (concat abbrev (or old "")))
812 (setq expansion (concat abbrev expansion)))
813 ;; If the expansion has mixed case
814 ;; and it is not simply a capitalized word,
815 ;; or if the abbrev has mixed case,
816 ;; and if the given abbrev's case pattern
817 ;; matches the start of the expansion,
818 ;; copy the expansion's case
819 ;; instead of downcasing all the rest.
820 (let ((expansion-rest (substring expansion 1)))
821 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
822 (string= expansion-rest (upcase expansion-rest)))
823 (or (string= abbrev (downcase abbrev))
824 (string= abbrev (upcase abbrev)))))
825 (string= abbrev
826 (substring expansion 0 (length abbrev))))
827 (setq use-case-replace nil)))
828 (if (equal abbrev " ")
829 (setq use-case-replace nil))
830 (if use-case-replace
831 (setq expansion (downcase expansion)))
832 (if old
833 (save-excursion
834 (search-backward old))
835 ;;(set-match-data (list (point-marker) (point-marker)))
836 (search-backward abbrev))
837 ;; Make case of replacement conform to case of abbreviation
838 ;; provided (1) that kind of thing is enabled in this buffer
839 ;; and (2) the replacement itself is all lower case.
840 (dabbrev--safe-replace-match expansion
841 (not use-case-replace)
842 t)))
843
844
845 ;;;----------------------------------------------------------------
846 ;;; Search function used by dabbrevs library.
847
848 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
849 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
850 ;;; controls the maximum search region size. Third argument IGNORE-CASE
851 ;;; non-nil means treat case as insignificant while looking for a match
852 ;;; and when comparing with previous matches. Also if that's non-nil
853 ;;; and the match is found at the beginning of a sentence and is in
854 ;;; lower case except for the initial then it is converted to all lower
855 ;;; case for return.
856
857 ;;; Table of expansions already seen is examined in buffer
858 ;;; `dabbrev--last-table' so that only distinct possibilities are found
859 ;;; by dabbrev-re-expand.
860
861 ;;; Value is the expansion, or nil if not found.
862
863 (defun dabbrev--search (abbrev reverse ignore-case)
864 (save-match-data
865 (let ((pattern1 (concat (regexp-quote abbrev)
866 "\\(" dabbrev--abbrev-char-regexp "\\)"))
867 (pattern2 (concat (regexp-quote abbrev)
868 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
869 (found-string nil))
870 ;; Limited search.
871 (save-restriction
872 (and dabbrev-limit
873 (narrow-to-region dabbrev--last-expansion-location
874 (+ (point)
875 (if reverse (- dabbrev-limit) dabbrev-limit))))
876 ;;--------------------------------
877 ;; Look for a distinct expansion, using dabbrev--last-table.
878 ;;--------------------------------
879 (while (and (not found-string)
880 (if reverse
881 (re-search-backward pattern1 nil t)
882 (re-search-forward pattern1 nil t)))
883 (goto-char (match-beginning 0))
884 ;; In case we matched in the middle of a word,
885 ;; back up to start of word and verify we still match.
886 (dabbrev--goto-start-of-abbrev)
887
888 (if (not (looking-at pattern1))
889 nil
890 ;; We have a truly valid match. Find the end.
891 (re-search-forward pattern2)
892 (setq found-string (buffer-substring-no-properties
893 (match-beginning 1) (match-end 1)))
894 (and ignore-case (setq found-string (downcase found-string)))
895 ;; Ignore this match if it's already in the table.
896 (if (dabbrev-filter-elements
897 table-string dabbrev--last-table
898 (string= found-string table-string))
899 (setq found-string nil)))
900 ;; Prepare to continue searching.
901 (if reverse
902 (goto-char (match-beginning 0))
903 (goto-char (match-end 0))))
904 ;; If we found something, use it.
905 (if found-string
906 ;; Put it into `dabbrev--last-table'
907 ;; and return it (either downcased, or as is).
908 (let ((result (buffer-substring-no-properties
909 (match-beginning 0) (match-end 0))))
910 (setq dabbrev--last-table
911 (cons found-string dabbrev--last-table))
912 (if (and ignore-case (eval dabbrev-case-replace))
913 result
914 result)))))))
915
916 (provide 'dabbrev)
917
918 ;;; dabbrev.el ends here