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