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