]> code.delx.au - gnu-emacs/blob - lisp/dabbrev.el
(Fbuffer_substring): Doc fix.
[gnu-emacs] / lisp / dabbrev.el
1 ;;; dabbrev.el --- dynamic abbreviation package
2
3 ;; Copyright (C) 1985, 86, 92, 94, 96, 1997, 2000
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Don Morrison
7 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
8 ;; Created: 16 Mars 1992
9 ;; Lindberg's last update version: 5.7
10 ;; Keywords: abbrev expand completion convenience
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; The purpose with this package is to let you write just a few
32 ;; characters of words you've written earlier to be able to expand
33 ;; them.
34 ;;
35 ;; To expand a word, just put the point right after the word and press
36 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
37 ;;
38 ;; Check out the customizable variables below to learn about all the
39 ;; features of this package.
40
41 ;;; Hints and tips for major modes writers:
42
43 ;; Recommended values C/Lisp etc text
44 ;; dabbrev-case-fold-search nil t
45 ;; dabbrev-case-replace nil t
46 ;;
47 ;; Set the variables you want special for your mode like this:
48 ;; (set (make-local-variable 'dabbrev-case-replace) nil)
49 ;; Then you don't interfere with other modes.
50 ;;
51 ;; If your mode handles buffers that refers to other buffers
52 ;; (i.e. compilation-mode, gud-mode), then try to set
53 ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
54 ;; to a function that point out those buffers.
55
56 ;; Same goes for major-modes that are connected to other modes. There
57 ;; are for instance a number of mail-modes. One for reading, one for
58 ;; creating a new mail etc. Maybe those should be connected.
59
60 ;; Example for GNUS (when we write a reply, we want dabbrev to look in
61 ;; the article for expansion):
62 ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
63 ;; (lambda (buffer)
64 ;; (save-excursion
65 ;; (set-buffer buffer)
66 ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
67
68
69 ;; Known bugs and limitations.
70 ;; - Possible to do several levels of `dabbrev-completion' in the
71 ;; minibuffer.
72 ;; - dabbrev-completion doesn't handle resetting the globals variables
73 ;; right. It resets them after finding the abbrev.
74
75 ;; Future enhancements
76 ;; - Check the tags-files? Like tags-complete?
77 ;; - Add the possibility of searching both forward and backward to
78 ;; the nearest expansion.
79 ;; - Check the kill-ring when everything else fails. (Maybe something
80 ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
81
82 ;;; These people gave suggestions:
83 ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
84 ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
85 ;; [jules] Julian Gosnell <jules@x.co.uk>
86 ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
87 ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
88 ;; [alon] Alon Albert <al%imercury@uunet.uu.net>
89 ;; [tromey] Tom Tromey <tromey@busco.lanl.gov>
90 ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
91 ;; [Petri] Petri Raitio <per@tekla.fi>
92 ;; [ejb] Jay Berkenbilt <ejb@ql.org>
93 ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
94 ;; ... and to all the people who have participated in the beta tests.
95
96 ;;; Code:
97
98 ;;----------------------------------------------------------------
99 ;; Customization variables
100 ;;----------------------------------------------------------------
101
102 (defgroup dabbrev nil
103 "Dynamic Abbreviations"
104 :tag "Dynamic Abbreviations"
105 :group 'abbrev
106 :group 'convenience)
107
108 (defcustom dabbrev-backward-only nil
109 "*If non-nil, `dabbrev-expand' only looks backwards."
110 :type 'boolean
111 :group 'dabbrev)
112
113 (defcustom dabbrev-limit nil
114 "*Limits region searched by `dabbrev-expand' to this many chars away."
115 :type '(choice (const :tag "off" nil)
116 integer)
117 :group 'dabbrev)
118
119 (defcustom dabbrev-abbrev-skip-leading-regexp nil
120 "*Regexp for skipping leading characters of an abbreviation.
121
122 Example: Set this to \"\\\\$\" for programming languages
123 in which variable names may appear with or without a leading `$'.
124 \(For example, in Makefiles.\)
125
126 Set this to nil if no characters should be skipped."
127 :type '(choice regexp
128 (const :tag "off" nil))
129 :group 'dabbrev)
130
131 (defcustom dabbrev-case-fold-search 'case-fold-search
132 "*Control whether dabbrev searches should ignore case.
133 A value of nil means case is significant.
134 A value of `case-fold-search' means case is significant
135 if `case-fold-search' is nil.
136 Any other non-nil version means case is not significant."
137 :type '(choice (const :tag "off" nil)
138 (const :tag "like search" case-fold-search)
139 (other :tag "on" t))
140 :group 'dabbrev)
141
142 (defcustom dabbrev-upcase-means-case-search nil
143 "*The significance of an uppercase character in an abbreviation.
144 nil means case fold search, non-nil means case sensitive search.
145
146 This variable has an effect only when the value of
147 `dabbrev-case-fold-search' says to ignore case."
148 :type 'boolean
149 :group 'dabbrev)
150
151 (defcustom dabbrev-case-replace 'case-replace
152 "*Controls whether dabbrev preserves case when expanding the abbreviation.
153 A value of nil means preserve case.
154 A value of `case-replace' means preserve case if `case-replace' is nil.
155 Any other non-nil version means do not preserve case.
156
157 This variable has an effect only when the value of
158 `dabbrev-case-fold-search' specifies to ignore case."
159 :type '(choice (const :tag "off" nil)
160 (const :tag "like M-x query-replace" case-replace)
161 (other :tag "on" t))
162 :group 'dabbrev)
163
164 (defcustom dabbrev-abbrev-char-regexp nil
165 "*Regexp to recognize a character in an abbreviation or expansion.
166 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
167
168 Set this variable to \"\\\\sw\" if you want ordinary words or
169 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
170 syntax is \"symbol\" as well as those whose syntax is \"word\".
171
172 The value nil has a special meaning: the abbreviation is from point to
173 previous word-start, but the search is for symbols.
174
175 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
176 while `yes', `or', `no' and `p' are considered words. If this
177 variable is nil, then expanding `yes-or-no-' looks for a symbol
178 starting with or containing `no-'. If you set this variable to
179 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
180 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
181 expanding `yes-or-no-' signals an error because `-' is not part of a word;
182 but expanding `yes-or-no' looks for a word starting with `no'.
183
184 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
185 :type '(choice (const nil)
186 regexp)
187 :group 'dabbrev)
188
189 (defcustom dabbrev-check-all-buffers t
190 "*Non-nil means dabbrev package should search *all* buffers.
191
192 Dabbrev always searches the current buffer first. Then, if
193 `dabbrev-check-other-buffers' says so, it searches the buffers
194 designated by `dabbrev-select-buffers-function'.
195
196 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
197 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
198 or matched by `dabbrev-ignored-regexps'."
199 :type 'boolean
200 :group 'dabbrev)
201
202 (defcustom dabbrev-ignored-buffer-names '("*Messages*" "*Buffer List*")
203 "*List of buffer names that dabbrev should not check.
204 See also `dabbrev-ignored-regexps'."
205 :type '(repeat (string :tag "Buffer name"))
206 :group 'dabbrev
207 :version "20.3")
208
209 (defcustom dabbrev-ignored-regexps nil
210 "*List of regexps matching names of buffers that dabbrev should not check.
211 See also `dabbrev-ignored-buffer-names'."
212 :type '(repeat regexp)
213 :group 'dabbrev
214 :version "21.1")
215
216 (defcustom dabbrev-check-other-buffers t
217 "*Should \\[dabbrev-expand] look in other buffers?\
218
219 nil: Don't look in other buffers.
220 t: Also look for expansions in the buffers pointed out by
221 `dabbrev-select-buffers-function'.
222 Anything else: When we can't find any more expansions in
223 the current buffer, then ask the user whether to look in other
224 buffers too.
225
226 The default value is t."
227 :type '(choice (const :tag "off" nil)
228 (const :tag "on" t)
229 (other :tag "ask" other))
230 :group 'dabbrev)
231
232 ;; I guess setting this to a function that selects all C- or C++-
233 ;; mode buffers would be a good choice for a debugging buffer,
234 ;; when debugging C- or C++-code.
235 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
236 "A function that selects buffers that should be searched by dabbrev.
237 The function should take no arguments and return a list of buffers to
238 search for expansions. Have a look at `dabbrev--select-buffers' for
239 an example.
240
241 A mode setting this variable should make it buffer local.")
242
243 (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
244 "*A function to decide whether dabbrev should search OTHER-BUFFER.
245 The function should take one argument, OTHER-BUFFER, and return
246 non-nil if that buffer should be searched. Have a look at
247 `dabbrev--same-major-mode-p' for an example.
248
249 The value of `dabbrev-friend-buffer-function' has an effect only if
250 the value of `dabbrev-select-buffers-function' uses it. The function
251 `dabbrev--select-buffers' is one function you can use here.
252
253 A mode setting this variable should make it buffer local."
254 :type 'function
255 :group 'dabbrev)
256
257 (defcustom dabbrev-search-these-buffers-only nil
258 "If non-nil, a list of buffers which dabbrev should search.
259 If this variable is non-nil, dabbrev will only look in these buffers.
260 It will not even look in the current buffer if it is not a member of
261 this list.")
262
263 ;;----------------------------------------------------------------
264 ;; Internal variables
265 ;;----------------------------------------------------------------
266
267 ;; Last obarray of completions in `dabbrev-completion'
268 (defvar dabbrev--last-obarray nil)
269
270 ;; Table of expansions seen so far
271 (defvar dabbrev--last-table nil)
272
273 ;; Last string we tried to expand.
274 (defvar dabbrev--last-abbreviation nil)
275
276 ;; Location last abbreviation began
277 (defvar dabbrev--last-abbrev-location nil)
278
279 ;; Direction of last dabbrevs search
280 (defvar dabbrev--last-direction 0)
281
282 ;; Last expansion of an abbreviation.
283 (defvar dabbrev--last-expansion nil)
284
285 ;; Location the last expansion was found.
286 (defvar dabbrev--last-expansion-location nil)
287
288 ;; The list of remaining buffers with the same mode as current buffer.
289 (defvar dabbrev--friend-buffer-list nil)
290
291 ;; The buffer we looked in last.
292 (defvar dabbrev--last-buffer nil)
293
294 ;; The buffer we found the expansion last time.
295 (defvar dabbrev--last-buffer-found nil)
296
297 ;; The buffer we last did a completion in.
298 (defvar dabbrev--last-completion-buffer nil)
299
300 ;; If non-nil, a function to use when copying successive words.
301 ;; It should be `upcase' or `downcase'.
302 (defvar dabbrev--last-case-pattern nil)
303
304 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
305 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
306
307 ;; The regexp for recognizing a character in an abbreviation.
308 (defvar dabbrev--abbrev-char-regexp nil)
309
310 ;;----------------------------------------------------------------
311 ;; Macros
312 ;;----------------------------------------------------------------
313
314 ;;; Get the buffer that mini-buffer was activated from
315 (defsubst dabbrev--minibuffer-origin ()
316 (car (cdr (buffer-list))))
317
318 ;; Make a list of some of the elements of LIST.
319 ;; Check each element of LIST, storing it temporarily in the
320 ;; variable ELEMENT, and include it in the result
321 ;; if CONDITION evaluates non-nil.
322 (defmacro dabbrev-filter-elements (element list condition)
323 `(let (dabbrev-result dabbrev-tail ,element)
324 (setq dabbrev-tail ,list)
325 (while dabbrev-tail
326 (setq ,element (car dabbrev-tail))
327 (if ,condition
328 (setq dabbrev-result (cons ,element dabbrev-result)))
329 (setq dabbrev-tail (cdr dabbrev-tail)))
330 (nreverse dabbrev-result)))
331
332 ;;----------------------------------------------------------------
333 ;; Exported functions
334 ;;----------------------------------------------------------------
335
336 ;;;###autoload
337 (define-key esc-map "/" 'dabbrev-expand)
338 ;;;??? Do we want this?
339 ;;;###autoload
340 (define-key esc-map [?\C-/] 'dabbrev-completion)
341
342 ;;;###autoload
343 (defun dabbrev-completion (&optional arg)
344 "Completion on current word.
345 Like \\[dabbrev-expand] but finds all expansions in the current buffer
346 and presents suggestions for completion.
347
348 With a prefix argument, it searches all buffers accepted by the
349 function pointed out by `dabbrev-friend-buffer-function' to find the
350 completions.
351
352 If the prefix argument is 16 (which comes from C-u C-u),
353 then it searches *all* buffers.
354
355 With no prefix argument, it reuses an old completion list
356 if there is a suitable one already."
357
358 (interactive "*P")
359 (dabbrev--reset-global-variables)
360 (let* ((dabbrev-check-other-buffers (and arg t))
361 (dabbrev-check-all-buffers
362 (and arg (= (prefix-numeric-value arg) 16)))
363 (abbrev (dabbrev--abbrev-at-point))
364 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
365 case-fold-search
366 dabbrev-case-fold-search)
367 (or (not dabbrev-upcase-means-case-search)
368 (string= abbrev (downcase abbrev)))))
369 (my-obarray dabbrev--last-obarray)
370 init)
371 (save-excursion
372 (if (and (null arg)
373 my-obarray
374 (or (eq dabbrev--last-completion-buffer (current-buffer))
375 (and (window-minibuffer-p (selected-window))
376 (eq dabbrev--last-completion-buffer
377 (dabbrev--minibuffer-origin))))
378 dabbrev--last-abbreviation
379 (>= (length abbrev) (length dabbrev--last-abbreviation))
380 (string= dabbrev--last-abbreviation
381 (substring abbrev 0
382 (length dabbrev--last-abbreviation)))
383 (setq init (try-completion abbrev my-obarray)))
384 ;; We can reuse the existing completion list.
385 nil
386 ;;--------------------------------
387 ;; New abbreviation to expand.
388 ;;--------------------------------
389 (setq dabbrev--last-abbreviation abbrev)
390 ;; Find all expansion
391 (let ((completion-list
392 (dabbrev--find-all-expansions abbrev ignore-case-p))
393 (completion-ignore-case ignore-case-p))
394 ;; Make an obarray with all expansions
395 (setq my-obarray (make-vector (length completion-list) 0))
396 (or (> (length my-obarray) 0)
397 (error "No dynamic expansion for \"%s\" found%s"
398 abbrev
399 (if dabbrev--check-other-buffers "" " in this-buffer")))
400 (cond
401 ((or (not ignore-case-p)
402 (not dabbrev-case-replace))
403 (mapc (function (lambda (string)
404 (intern string my-obarray)))
405 completion-list))
406 ((string= abbrev (upcase abbrev))
407 (mapc (function (lambda (string)
408 (intern (upcase string) my-obarray)))
409 completion-list))
410 ((string= (substring abbrev 0 1)
411 (upcase (substring abbrev 0 1)))
412 (mapc (function (lambda (string)
413 (intern (capitalize string) my-obarray)))
414 completion-list))
415 (t
416 (mapc (function (lambda (string)
417 (intern (downcase string) my-obarray)))
418 completion-list)))
419 (setq dabbrev--last-obarray my-obarray)
420 (setq dabbrev--last-completion-buffer (current-buffer))
421 ;; Find the longest common string.
422 (setq init (try-completion abbrev my-obarray)))))
423 ;;--------------------------------
424 ;; Let the user choose between the expansions
425 ;;--------------------------------
426 (or (stringp init)
427 (setq init abbrev))
428 (cond
429 ;; * Replace string fragment with matched common substring completion.
430 ((and (not (string-equal init ""))
431 (not (string-equal (downcase init) (downcase abbrev))))
432 (if (> (length (all-completions init my-obarray)) 1)
433 (message "Repeat `%s' to see all completions"
434 (key-description (this-command-keys)))
435 (message "The only possible completion"))
436 (dabbrev--substitute-expansion nil abbrev init nil))
437 (t
438 ;; * String is a common substring completion already. Make list.
439 (message "Making completion list...")
440 (with-output-to-temp-buffer " *Completions*"
441 (display-completion-list (all-completions init my-obarray)))
442 (message "Making completion list...done")))
443 (and (window-minibuffer-p (selected-window))
444 (message nil))))
445
446 ;;;###autoload
447 (defun dabbrev-expand (arg)
448 "Expand previous word \"dynamically\".
449
450 Expands to the most recent, preceding word for which this is a prefix.
451 If no suitable preceding word is found, words following point are
452 considered. If still no suitable word is found, then look in the
453 buffers accepted by the function pointed out by variable
454 `dabbrev-friend-buffer-function'.
455
456 A positive prefix argument, N, says to take the Nth backward *distinct*
457 possibility. A negative argument says search forward.
458
459 If the cursor has not moved from the end of the previous expansion and
460 no argument is given, replace the previously-made expansion
461 with the next possible expansion not yet tried.
462
463 The variable `dabbrev-backward-only' may be used to limit the
464 direction of search to backward if set non-nil.
465
466 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
467 (interactive "*P")
468 (let (abbrev record-case-pattern
469 expansion old direction (orig-point (point)))
470 ;; abbrev -- the abbrev to expand
471 ;; expansion -- the expansion found (eventually) or nil until then
472 ;; old -- the text currently in the buffer
473 ;; (the abbrev, or the previously-made expansion)
474 (save-excursion
475 (if (and (null arg)
476 (markerp dabbrev--last-abbrev-location)
477 (marker-position dabbrev--last-abbrev-location)
478 (or (eq last-command this-command)
479 (and (window-minibuffer-p (selected-window))
480 (= dabbrev--last-abbrev-location
481 (point)))))
482 ;; Find a different expansion for the same abbrev as last time.
483 (progn
484 (setq abbrev dabbrev--last-abbreviation)
485 (setq old dabbrev--last-expansion)
486 (setq direction dabbrev--last-direction))
487 ;; If the user inserts a space after expanding
488 ;; and then asks to expand again, always fetch the next word.
489 (if (and (eq (preceding-char) ?\ )
490 (markerp dabbrev--last-abbrev-location)
491 (marker-position dabbrev--last-abbrev-location)
492 (= (point) (1+ dabbrev--last-abbrev-location)))
493 (progn
494 ;; The "abbrev" to expand is just the space.
495 (setq abbrev " ")
496 (save-excursion
497 (if dabbrev--last-buffer
498 (set-buffer dabbrev--last-buffer))
499 ;; Find the end of the last "expansion" word.
500 (if (or (eq dabbrev--last-direction 1)
501 (and (eq dabbrev--last-direction 0)
502 (< dabbrev--last-expansion-location (point))))
503 (setq dabbrev--last-expansion-location
504 (+ dabbrev--last-expansion-location
505 (length dabbrev--last-expansion))))
506 (goto-char dabbrev--last-expansion-location)
507 ;; Take the following word, with intermediate separators,
508 ;; as our expansion this time.
509 (re-search-forward
510 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
511 (setq expansion (buffer-substring-no-properties
512 dabbrev--last-expansion-location (point)))
513
514 ;; Record the end of this expansion, in case we repeat this.
515 (setq dabbrev--last-expansion-location (point)))
516 ;; Indicate that dabbrev--last-expansion-location is
517 ;; at the end of the expansion.
518 (setq dabbrev--last-direction -1))
519
520 ;; We have a different abbrev to expand.
521 (dabbrev--reset-global-variables)
522 (setq direction (if (null arg)
523 (if dabbrev-backward-only 1 0)
524 (prefix-numeric-value arg)))
525 (setq abbrev (dabbrev--abbrev-at-point))
526 (setq record-case-pattern t)
527 (setq old nil)))
528
529 ;;--------------------------------
530 ;; Find the expansion
531 ;;--------------------------------
532 (or expansion
533 (setq expansion
534 (dabbrev--find-expansion abbrev direction
535 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
536 case-fold-search
537 dabbrev-case-fold-search)
538 (or (not dabbrev-upcase-means-case-search)
539 (string= abbrev (downcase abbrev))))))))
540 (cond
541 ((not expansion)
542 (dabbrev--reset-global-variables)
543 (if old
544 (save-excursion
545 (setq buffer-undo-list (cons orig-point buffer-undo-list))
546 ;; Put back the original abbrev with its original case pattern.
547 (search-backward old)
548 (insert abbrev)
549 (delete-region (point) (+ (point) (length old)))))
550 (error "No%s dynamic expansion for `%s' found"
551 (if old " further" "") abbrev))
552 (t
553 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
554 (minibuffer-window-active-p (selected-window))))
555 (progn
556 (message "Expansion found in '%s'"
557 (buffer-name dabbrev--last-buffer))
558 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
559 (message nil))
560 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
561 (null dabbrev--last-buffer))
562 (numberp dabbrev--last-expansion-location)
563 (and (> dabbrev--last-expansion-location (point))))
564 (setq dabbrev--last-expansion-location
565 (copy-marker dabbrev--last-expansion-location)))
566 ;; Success: stick it in and return.
567 (setq buffer-undo-list (cons orig-point buffer-undo-list))
568 (dabbrev--substitute-expansion old abbrev expansion
569 record-case-pattern)
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 (dabbrev-filter-elements
762 buffer (buffer-list)
763 (let ((bn (buffer-name buffer)))
764 (and (not (member bn dabbrev-ignored-buffer-names))
765 (not (memq buffer dabbrev--friend-buffer-list))
766 (not
767 (let ((tail dabbrev-ignored-regexps)
768 (match nil))
769 (while (and tail (not match))
770 (setq match (string-match (car tail) bn)
771 tail (cdr tail)))
772 match)))))
773 dabbrev--friend-buffer-list
774 (append dabbrev--friend-buffer-list
775 non-friend-buffer-list)))))
776 ;; Move buffers that are visible on the screen
777 ;; to the front of the list. Remove the current buffer.
778 (when dabbrev--friend-buffer-list
779 (walk-windows (lambda (w)
780 (unless (eq w (selected-window))
781 (setq dabbrev--friend-buffer-list
782 (cons (window-buffer w)
783 (delq (window-buffer w)
784 dabbrev--friend-buffer-list))))))
785 (setq dabbrev--friend-buffer-list
786 (delq (current-buffer) dabbrev--friend-buffer-list)))
787 ;; Walk through the buffers
788 (while (and (not expansion) dabbrev--friend-buffer-list)
789 (setq dabbrev--last-buffer
790 (car dabbrev--friend-buffer-list))
791 (setq dabbrev--friend-buffer-list
792 (cdr dabbrev--friend-buffer-list))
793 (set-buffer dabbrev--last-buffer)
794 (dabbrev--scanning-message)
795 (setq dabbrev--last-expansion-location (point-min))
796 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
797 expansion)))))
798
799 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
800 (if (eq major-mode 'picture-mode)
801 (picture-replace-match string fixedcase literal)
802 (replace-match string fixedcase literal)))
803
804 ;;;----------------------------------------------------------------
805 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
806 "Replace OLD with EXPANSION in the buffer.
807 OLD is text currently in the buffer, perhaps the abbreviation
808 or perhaps another expansion that was tried previously.
809 ABBREV is the abbreviation we are expanding.
810 It is \" \" if we are copying subsequent words.
811 EXPANSION is the expansion substring to be used this time.
812 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
813 to record whether we upcased the expansion, downcased it, or did neither."
814 ;;(undo-boundary)
815 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
816 case-fold-search
817 dabbrev-case-fold-search)
818 (or (not dabbrev-upcase-means-case-search)
819 (string= abbrev (downcase abbrev)))
820 (if (eq dabbrev-case-replace 'case-replace)
821 case-replace
822 dabbrev-case-replace))))
823
824 ;; If we upcased or downcased the original expansion,
825 ;; do likewise for the subsequent words when we copy them.
826 (and (equal abbrev " ")
827 dabbrev--last-case-pattern
828 (setq expansion
829 (funcall dabbrev--last-case-pattern expansion)))
830
831 ;; If the expansion has mixed case
832 ;; and it is not simply a capitalized word,
833 ;; or if the abbrev has mixed case,
834 ;; and if the given abbrev's case pattern
835 ;; matches the start of the expansion,
836 ;; copy the expansion's case
837 ;; instead of downcasing all the rest.
838 (let ((expansion-rest (substring expansion 1)))
839 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
840 (string= expansion-rest (upcase expansion-rest)))
841 (or (string= abbrev (downcase abbrev))
842 (string= abbrev (upcase abbrev)))))
843 (string= abbrev
844 (substring expansion 0 (length abbrev))))
845 (setq use-case-replace nil)))
846 (if (equal abbrev " ")
847 (setq use-case-replace nil))
848 (if use-case-replace
849 (setq expansion (downcase expansion)))
850
851 ;; In case we insert subsequent words,
852 ;; record if we upcased or downcased the first word,
853 ;; in order to do likewise for subsequent words.
854 (and record-case-pattern
855 (setq dabbrev--last-case-pattern
856 (and use-case-replace
857 (cond ((equal abbrev (upcase abbrev)) 'upcase)
858 ((equal abbrev (downcase abbrev)) 'downcase)))))
859
860 (if old
861 (save-excursion
862 (search-backward old))
863 ;;(set-match-data (list (point-marker) (point-marker)))
864 (search-backward abbrev))
865 ;; Make case of replacement conform to case of abbreviation
866 ;; provided (1) that kind of thing is enabled in this buffer
867 ;; and (2) the replacement itself is all lower case.
868 (dabbrev--safe-replace-match expansion
869 (not use-case-replace)
870 t)))
871
872
873 ;;;----------------------------------------------------------------
874 ;;; Search function used by dabbrevs library.
875
876 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
877 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
878 ;;; controls the maximum search region size. Third argument IGNORE-CASE
879 ;;; non-nil means treat case as insignificant while looking for a match
880 ;;; and when comparing with previous matches. Also if that's non-nil
881 ;;; and the match is found at the beginning of a sentence and is in
882 ;;; lower case except for the initial then it is converted to all lower
883 ;;; case for return.
884
885 ;;; Table of expansions already seen is examined in buffer
886 ;;; `dabbrev--last-table' so that only distinct possibilities are found
887 ;;; by dabbrev-re-expand.
888
889 ;;; Value is the expansion, or nil if not found.
890
891 (defun dabbrev--search (abbrev reverse ignore-case)
892 (save-match-data
893 (let ((pattern1 (concat (regexp-quote abbrev)
894 "\\(" dabbrev--abbrev-char-regexp "\\)"))
895 (pattern2 (concat (regexp-quote abbrev)
896 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
897 (found-string nil))
898 ;; Limited search.
899 (save-restriction
900 (and dabbrev-limit
901 (narrow-to-region dabbrev--last-expansion-location
902 (+ (point)
903 (if reverse (- dabbrev-limit) dabbrev-limit))))
904 ;;--------------------------------
905 ;; Look for a distinct expansion, using dabbrev--last-table.
906 ;;--------------------------------
907 (while (and (not found-string)
908 (if reverse
909 (re-search-backward pattern1 nil t)
910 (re-search-forward pattern1 nil t)))
911 (goto-char (match-beginning 0))
912 ;; In case we matched in the middle of a word,
913 ;; back up to start of word and verify we still match.
914 (dabbrev--goto-start-of-abbrev)
915
916 (if (not (looking-at pattern1))
917 nil
918 ;; We have a truly valid match. Find the end.
919 (re-search-forward pattern2)
920 (setq found-string (buffer-substring-no-properties
921 (match-beginning 1) (match-end 1)))
922 (and ignore-case (setq found-string (downcase found-string)))
923 ;; Ignore this match if it's already in the table.
924 (if (dabbrev-filter-elements
925 table-string dabbrev--last-table
926 (string= found-string table-string))
927 (setq found-string nil)))
928 ;; Prepare to continue searching.
929 (if reverse
930 (goto-char (match-beginning 0))
931 (goto-char (match-end 0))))
932 ;; If we found something, use it.
933 (if found-string
934 ;; Put it into `dabbrev--last-table'
935 ;; and return it (either downcased, or as is).
936 (let ((result (buffer-substring-no-properties
937 (match-beginning 0) (match-end 0))))
938 (setq dabbrev--last-table
939 (cons found-string dabbrev--last-table))
940 (if (and ignore-case (eval dabbrev-case-replace))
941 result
942 result)))))))
943
944 (dolist (mess '("^No dynamic expansion for .* found$"
945 "^No further dynamic expansion for .* found$"
946 "^No possible abbreviation preceding point$"))
947 (add-to-list 'debug-ignored-errors mess))
948
949 (provide 'dabbrev)
950
951 ;;; dabbrev.el ends here