(help-for-help): Update help text.
[bpt/emacs.git] / lisp / dabbrev.el
1 ;;; dabbrev.el --- dynamic abbreviation package
2
3 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Author: Don Morrison
6 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
7 ;; Created: 16 Mars 1992
8 ;; Lindberg's last update version: 5.7
9 ;; Keywords: abbrev expand completion
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; The purpose with this package is to let you write just a few
31 ;; characters of words you've written earlier to be able to expand
32 ;; them.
33 ;;
34 ;; To expand a word, just put the point right after the word and press
35 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
36 ;;
37 ;; Check out the customizable variables below to learn about all the
38 ;; features of this package.
39
40 ;;; Hints and tips for major modes writers:
41
42 ;; Recommended values C/Lisp etc text
43 ;; dabbrev-case-fold-search nil t
44 ;; dabbrev-case-replace nil t
45 ;;
46 ;; Set the variables you want special for your mode like this:
47 ;; (set (make-local-variable 'dabbrev-case-replace) nil)
48 ;; Then you don't interfere with other modes.
49 ;;
50 ;; If your mode handles buffers that refers to other buffers
51 ;; (i.e. compilation-mode, gud-mode), then try to set
52 ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
53 ;; to a function that point out those buffers.
54
55 ;; Same goes for major-modes that are connected to other modes. There
56 ;; are for instance a number of mail-modes. One for reading, one for
57 ;; creating a new mail etc. Maybe those should be connected.
58
59 ;; Example for GNUS (when we write a reply, we want dabbrev to look in
60 ;; the article for expansion):
61 ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
62 ;; (lambda (buffer)
63 ;; (save-excursion
64 ;; (set-buffer buffer)
65 ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
66
67
68 ;; Known bugs and limitations.
69 ;; - Possible to do several levels of `dabbrev-completion' in the
70 ;; minibuffer.
71 ;; - dabbrev-completion doesn't handle resetting the globals variables
72 ;; right. It resets them after finding the abbrev.
73
74 ;; Future enhancements
75 ;; - Check the tags-files? Like tags-complete?
76 ;; - Add the possibility of searching both forward and backward to
77 ;; the nearest expansion.
78 ;; - Check the kill-ring when everything else fails. (Maybe something
79 ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
80
81 ;;; These people gave suggestions:
82 ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
83 ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
84 ;; [jules] Julian Gosnell <jules@x.co.uk>
85 ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
86 ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
87 ;; [alon] Alon Albert <al%imercury@uunet.uu.net>
88 ;; [tromey] Tom Tromey <tromey@busco.lanl.gov>
89 ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
90 ;; [Petri] Petri Raitio <per@tekla.fi>
91 ;; [ejb] Jay Berkenbilt <ejb@ERA.COM>
92 ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
93 ;; ... and to all the people who have participated in the beta tests.
94
95 ;;; Code:
96
97 ;;----------------------------------------------------------------
98 ;; Customization variables
99 ;;----------------------------------------------------------------
100
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 :type 'regexp
124 :group 'dabbrev))
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 "on" t)
139 (const :tag "like search" 'case-fold-search))
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 "on" t)
161 (const :tag "like M-x query-replace" 'case-replace))
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."
198 :type 'boolean
199 :group 'dabbrev)
200
201 (defcustom dabbrev-check-other-buffers t
202 "*Should \\[dabbrev-expand] look in other buffers?\
203
204 nil: Don't look in other buffers.
205 t: Also look for expansions in the buffers pointed out by
206 `dabbrev-select-buffers-function'.
207 Anything else: When we can't find any more expansions in
208 the current buffer, then ask the user whether to look in other
209 buffers too.
210
211 The default value is t."
212 :type '(choice (const :tag "off" nil)
213 (const :tag "on" t)
214 (const :tag "ask" other))
215 :group 'dabbrev)
216
217 ;; I guess setting this to a function that selects all C- or C++-
218 ;; mode buffers would be a good choice for a debugging buffer,
219 ;; when debugging C- or C++-code.
220 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
221 "A function that selects buffers that should be searched by dabbrev.
222 The function should take no arguments and return a list of buffers to
223 search for expansions. Have a look at `dabbrev--select-buffers' for
224 an example.
225
226 A mode setting this variable should make it buffer local.")
227
228 (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
229 "*A function to decide whether dabbrev should search OTHER-BUFFER.
230 The function should take one argument, OTHER-BUFFER, and return
231 non-nil if that buffer should be searched. Have a look at
232 `dabbrev--same-major-mode-p' for an example.
233
234 The value of `dabbrev-friend-buffer-function' has an effect only if
235 the value of `dabbrev-select-buffers-function' uses it. The function
236 `dabbrev--select-buffers' is one function you can use here.
237
238 A mode setting this variable should make it buffer local."
239 :type 'function
240 :group 'dabbrev)
241
242 (defcustom dabbrev-search-these-buffers-only nil
243 "If non-nil, a list of buffers which dabbrev should search.
244 If this variable is non-nil, dabbrev will only look in these buffers.
245 It will not even look in the current buffer if it is not a member of
246 this list.")
247
248 ;;----------------------------------------------------------------
249 ;; Internal variables
250 ;;----------------------------------------------------------------
251
252 ;; Last obarray of completions in `dabbrev-completion'
253 (defvar dabbrev--last-obarray nil)
254
255 ;; Table of expansions seen so far
256 (defvar dabbrev--last-table nil)
257
258 ;; Last string we tried to expand.
259 (defvar dabbrev--last-abbreviation nil)
260
261 ;; Location last abbreviation began
262 (defvar dabbrev--last-abbrev-location nil)
263
264 ;; Direction of last dabbrevs search
265 (defvar dabbrev--last-direction 0)
266
267 ;; Last expansion of an abbreviation.
268 (defvar dabbrev--last-expansion nil)
269
270 ;; Location the last expansion was found.
271 (defvar dabbrev--last-expansion-location nil)
272
273 ;; The list of remaining buffers with the same mode as current buffer.
274 (defvar dabbrev--friend-buffer-list nil)
275
276 ;; The buffer we looked in last.
277 (defvar dabbrev--last-buffer nil)
278
279 ;; The buffer we found the expansion last time.
280 (defvar dabbrev--last-buffer-found nil)
281
282 ;; The buffer we last did a completion in.
283 (defvar dabbrev--last-completion-buffer nil)
284
285 ;; Non-nil means we should upcase
286 ;; when copying successive words.
287 (defvar dabbrev--last-case-pattern nil)
288
289 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
290 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
291
292 ;; The regexp for recognizing a character in an abbreviation.
293 (defvar dabbrev--abbrev-char-regexp nil)
294
295 ;;----------------------------------------------------------------
296 ;; Macros
297 ;;----------------------------------------------------------------
298
299 ;;; Get the buffer that mini-buffer was activated from
300 (defsubst dabbrev--minibuffer-origin ()
301 (car (cdr (buffer-list))))
302
303 ;; Make a list of some of the elements of LIST.
304 ;; Check each element of LIST, storing it temporarily in the
305 ;; variable ELEMENT, and include it in the result
306 ;; if CONDITION evaluates non-nil.
307 (defmacro dabbrev-filter-elements (element list condition)
308 (` (let (dabbrev-result dabbrev-tail (, element))
309 (setq dabbrev-tail (, list))
310 (while dabbrev-tail
311 (setq (, element) (car dabbrev-tail))
312 (if (, condition)
313 (setq dabbrev-result (cons (, element) dabbrev-result)))
314 (setq dabbrev-tail (cdr dabbrev-tail)))
315 (nreverse dabbrev-result))))
316
317 ;;----------------------------------------------------------------
318 ;; Exported functions
319 ;;----------------------------------------------------------------
320
321 ;;;###autoload
322 (define-key esc-map "/" 'dabbrev-expand)
323 ;;;??? Do we want this?
324 ;;;###autoload
325 (define-key esc-map [?\C-/] 'dabbrev-completion)
326
327 ;;;###autoload
328 (defun dabbrev-completion (&optional arg)
329 "Completion on current word.
330 Like \\[dabbrev-expand] but finds all expansions in the current buffer
331 and presents suggestions for completion.
332
333 With a prefix argument, it searches all buffers accepted by the
334 function pointed out by `dabbrev-friend-buffer-function' to find the
335 completions.
336
337 If the prefix argument is 16 (which comes from C-u C-u),
338 then it searches *all* buffers.
339
340 With no prefix argument, it reuses an old completion list
341 if there is a suitable one already."
342
343 (interactive "*P")
344 (dabbrev--reset-global-variables)
345 (let* ((dabbrev-check-other-buffers (and arg t))
346 (dabbrev-check-all-buffers
347 (and arg (= (prefix-numeric-value arg) 16)))
348 (abbrev (dabbrev--abbrev-at-point))
349 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
350 case-fold-search
351 dabbrev-case-fold-search)
352 (or (not dabbrev-upcase-means-case-search)
353 (string= abbrev (downcase abbrev)))))
354 (my-obarray dabbrev--last-obarray)
355 init)
356 (save-excursion
357 (if (and (null arg)
358 my-obarray
359 (or (eq dabbrev--last-completion-buffer (current-buffer))
360 (and (window-minibuffer-p (selected-window))
361 (eq dabbrev--last-completion-buffer
362 (dabbrev--minibuffer-origin))))
363 dabbrev--last-abbreviation
364 (>= (length abbrev) (length dabbrev--last-abbreviation))
365 (string= dabbrev--last-abbreviation
366 (substring abbrev 0
367 (length dabbrev--last-abbreviation)))
368 (setq init (try-completion abbrev my-obarray)))
369 ;; We can reuse the existing completion list.
370 nil
371 ;;--------------------------------
372 ;; New abbreviation to expand.
373 ;;--------------------------------
374 (setq dabbrev--last-abbreviation abbrev)
375 ;; Find all expansion
376 (let ((completion-list
377 (dabbrev--find-all-expansions abbrev ignore-case-p))
378 (completion-ignore-case ignore-case-p))
379 ;; Make an obarray with all expansions
380 (setq my-obarray (make-vector (length completion-list) 0))
381 (or (> (length my-obarray) 0)
382 (error "No dynamic expansion for \"%s\" found%s"
383 abbrev
384 (if dabbrev--check-other-buffers "" " in this-buffer")))
385 (cond
386 ((or (not ignore-case-p)
387 (not dabbrev-case-replace))
388 (mapcar (function (lambda (string)
389 (intern string my-obarray)))
390 completion-list))
391 ((string= abbrev (upcase abbrev))
392 (mapcar (function (lambda (string)
393 (intern (upcase string) my-obarray)))
394 completion-list))
395 ((string= (substring abbrev 0 1)
396 (upcase (substring abbrev 0 1)))
397 (mapcar (function (lambda (string)
398 (intern (capitalize string) my-obarray)))
399 completion-list))
400 (t
401 (mapcar (function (lambda (string)
402 (intern (downcase string) my-obarray)))
403 completion-list)))
404 (setq dabbrev--last-obarray my-obarray)
405 (setq dabbrev--last-completion-buffer (current-buffer))
406 ;; Find the longest common string.
407 (setq init (try-completion abbrev my-obarray)))))
408 ;;--------------------------------
409 ;; Let the user choose between the expansions
410 ;;--------------------------------
411 (or (stringp init)
412 (setq init abbrev))
413 (cond
414 ;; * Replace string fragment with matched common substring completion.
415 ((and (not (string-equal init ""))
416 (not (string-equal (downcase init) (downcase abbrev))))
417 (if (> (length (all-completions init my-obarray)) 1)
418 (message "Repeat `%s' to see all completions"
419 (key-description (this-command-keys)))
420 (message "The only possible completion"))
421 (dabbrev--substitute-expansion nil abbrev init))
422 (t
423 ;; * String is a common substring completion already. Make list.
424 (message "Making completion list...")
425 (with-output-to-temp-buffer " *Completions*"
426 (display-completion-list (all-completions init my-obarray)))
427 (message "Making completion list...done")))
428 (and (window-minibuffer-p (selected-window))
429 (message nil))))
430
431 ;;;###autoload
432 (defun dabbrev-expand (arg)
433 "Expand previous word \"dynamically\".
434
435 Expands to the most recent, preceding word for which this is a prefix.
436 If no suitable preceding word is found, words following point are
437 considered. If still no suitable word is found, then look in the
438 buffers accepted by the function pointed out by variable
439 `dabbrev-friend-buffer-function'.
440
441 A positive prefix argument, N, says to take the Nth backward *distinct*
442 possibility. A negative argument says search forward.
443
444 If the cursor has not moved from the end of the previous expansion and
445 no argument is given, replace the previously-made expansion
446 with the next possible expansion not yet tried.
447
448 The variable `dabbrev-backward-only' may be used to limit the
449 direction of search to backward if set non-nil.
450
451 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
452 (interactive "*P")
453 (let (abbrev record-case-pattern
454 expansion old direction (orig-point (point)))
455 ;; abbrev -- the abbrev to expand
456 ;; expansion -- the expansion found (eventually) or nil until then
457 ;; old -- the text currently in the buffer
458 ;; (the abbrev, or the previously-made expansion)
459 (save-excursion
460 (if (and (null arg)
461 (markerp dabbrev--last-abbrev-location)
462 (marker-position dabbrev--last-abbrev-location)
463 (or (eq last-command this-command)
464 (and (window-minibuffer-p (selected-window))
465 (= dabbrev--last-abbrev-location
466 (point)))))
467 ;; Find a different expansion for the same abbrev as last time.
468 (progn
469 (setq abbrev dabbrev--last-abbreviation)
470 (setq old dabbrev--last-expansion)
471 (setq direction dabbrev--last-direction))
472 ;; If the user inserts a space after expanding
473 ;; and then asks to expand again, always fetch the next word.
474 (if (and (eq (preceding-char) ?\ )
475 (markerp dabbrev--last-abbrev-location)
476 (marker-position dabbrev--last-abbrev-location)
477 (= (point) (1+ dabbrev--last-abbrev-location)))
478 (progn
479 ;; The "abbrev" to expand is just the space.
480 (setq abbrev " ")
481 (save-excursion
482 (if dabbrev--last-buffer
483 (set-buffer dabbrev--last-buffer))
484 ;; Find the end of the last "expansion" word.
485 (if (or (eq dabbrev--last-direction 1)
486 (and (eq dabbrev--last-direction 0)
487 (< dabbrev--last-expansion-location (point))))
488 (setq dabbrev--last-expansion-location
489 (+ dabbrev--last-expansion-location
490 (length dabbrev--last-expansion))))
491 (goto-char dabbrev--last-expansion-location)
492 ;; Take the following word, with intermediate separators,
493 ;; as our expansion this time.
494 (re-search-forward
495 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
496 (setq expansion (buffer-substring-no-properties
497 dabbrev--last-expansion-location (point)))
498 (if dabbrev--last-case-pattern
499 (setq expansion (upcase expansion)))
500
501 ;; Record the end of this expansion, in case we repeat this.
502 (setq dabbrev--last-expansion-location (point)))
503 ;; Indicate that dabbrev--last-expansion-location is
504 ;; at the end of the expansion.
505 (setq dabbrev--last-direction -1))
506
507 ;; We have a different abbrev to expand.
508 (dabbrev--reset-global-variables)
509 (setq direction (if (null arg)
510 (if dabbrev-backward-only 1 0)
511 (prefix-numeric-value arg)))
512 (setq abbrev (dabbrev--abbrev-at-point))
513 (setq record-case-pattern t)
514 (setq old nil)))
515
516 ;;--------------------------------
517 ;; Find the expansion
518 ;;--------------------------------
519 (or expansion
520 (setq expansion
521 (dabbrev--find-expansion abbrev direction
522 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
523 case-fold-search
524 dabbrev-case-fold-search)
525 (or (not dabbrev-upcase-means-case-search)
526 (string= abbrev (downcase abbrev))))))))
527 (cond
528 ((not expansion)
529 (dabbrev--reset-global-variables)
530 (if old
531 (save-excursion
532 (setq buffer-undo-list (cons orig-point buffer-undo-list))
533 ;; Put back the original abbrev with its original case pattern.
534 (search-backward old)
535 (insert abbrev)
536 (delete-region (point) (+ (point) (length old)))))
537 (error "No%s dynamic expansion for `%s' found"
538 (if old " further" "") abbrev))
539 (t
540 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found))
541 (progn
542 (message "Expansion found in '%s'"
543 (buffer-name dabbrev--last-buffer))
544 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
545 (message nil))
546 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
547 (null dabbrev--last-buffer))
548 (numberp dabbrev--last-expansion-location)
549 (and (> dabbrev--last-expansion-location (point))))
550 (setq dabbrev--last-expansion-location
551 (copy-marker dabbrev--last-expansion-location)))
552 ;; Success: stick it in and return.
553 (setq buffer-undo-list (cons orig-point buffer-undo-list))
554 (dabbrev--substitute-expansion old abbrev expansion)
555
556 ;; If we are not copying successive words now,
557 ;; set dabbrev--last-case-pattern.
558 (and record-case-pattern
559 (setq dabbrev--last-case-pattern
560 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
561 case-fold-search
562 dabbrev-case-fold-search)
563 (not dabbrev-upcase-means-case-search)
564 (equal abbrev (upcase abbrev)))))
565
566 ;; Save state for re-expand.
567 (setq dabbrev--last-expansion expansion)
568 (setq dabbrev--last-abbreviation abbrev)
569 (setq dabbrev--last-abbrev-location (point-marker))))))
570
571 ;;----------------------------------------------------------------
572 ;; Local functions
573 ;;----------------------------------------------------------------
574
575 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
576 (defun dabbrev--same-major-mode-p (other-buffer)
577 (eq major-mode
578 (save-excursion
579 (set-buffer other-buffer)
580 major-mode)))
581
582 ;;; Back over all abbrev type characters and then moves forward over
583 ;;; all skip characters.
584 (defun dabbrev--goto-start-of-abbrev ()
585 ;; Move backwards over abbrev chars
586 (save-match-data
587 (if (not (bobp))
588 (progn
589 (forward-char -1)
590 (while (and (looking-at dabbrev--abbrev-char-regexp)
591 (not (bobp)))
592 (forward-char -1))
593 (or (looking-at dabbrev--abbrev-char-regexp)
594 (forward-char 1))))
595 (and dabbrev-abbrev-skip-leading-regexp
596 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
597 (forward-char 1)))))
598
599 ;;; Extract the symbol at point to serve as abbreviation.
600 (defun dabbrev--abbrev-at-point ()
601 ;; Check for error
602 (if (bobp)
603 (error "No possible abbreviation preceding point"))
604 ;; Return abbrev at point
605 (save-excursion
606 ;; Record the end of the abbreviation.
607 (setq dabbrev--last-abbrev-location (point))
608 ;; If we aren't right after an abbreviation,
609 ;; move point back to just after one.
610 ;; This is so the user can get successive words
611 ;; by typing the punctuation followed by M-/.
612 (save-match-data
613 (if (save-excursion
614 (forward-char -1)
615 (not (looking-at (concat "\\("
616 (or dabbrev-abbrev-char-regexp
617 "\\sw\\|\\s_")
618 "\\)+"))))
619 (if (re-search-backward (or dabbrev-abbrev-char-regexp
620 "\\sw\\|\\s_")
621 nil t)
622 (forward-char 1)
623 (error "No possible abbreviation preceding point"))))
624 ;; Now find the beginning of that one.
625 (dabbrev--goto-start-of-abbrev)
626 (buffer-substring-no-properties
627 dabbrev--last-abbrev-location (point))))
628
629 ;;; Initializes all global variables
630 (defun dabbrev--reset-global-variables ()
631 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
632 ;; must not be reset here.
633 (setq dabbrev--last-table nil
634 dabbrev--last-abbreviation nil
635 dabbrev--last-abbrev-location nil
636 dabbrev--last-direction nil
637 dabbrev--last-expansion nil
638 dabbrev--last-expansion-location nil
639 dabbrev--friend-buffer-list nil
640 dabbrev--last-buffer nil
641 dabbrev--last-buffer-found nil
642 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
643 "\\sw\\|\\s_")
644 dabbrev--check-other-buffers dabbrev-check-other-buffers))
645
646 ;;; Find all buffers that are considered "friends" according to the
647 ;;; function pointed out by dabbrev-friend-buffer-function.
648 (defun dabbrev--select-buffers ()
649 (save-excursion
650 (and (window-minibuffer-p (selected-window))
651 (set-buffer (dabbrev--minibuffer-origin)))
652 (let ((orig-buffer (current-buffer)))
653 (dabbrev-filter-elements
654 buffer (buffer-list)
655 (and (not (eq orig-buffer buffer))
656 (boundp 'dabbrev-friend-buffer-function)
657 (funcall dabbrev-friend-buffer-function buffer))))))
658
659 ;;; Search for ABBREV, N times, normally looking forward,
660 ;;; but looking in reverse instead if REVERSE is non-nil.
661 (defun dabbrev--try-find (abbrev reverse n ignore-case)
662 (save-excursion
663 (save-restriction
664 (widen)
665 (let ((expansion nil))
666 (and dabbrev--last-expansion-location
667 (goto-char dabbrev--last-expansion-location))
668 (let ((case-fold-search ignore-case)
669 (count n))
670 (while (and (> count 0)
671 (setq expansion (dabbrev--search abbrev
672 reverse
673 ignore-case)))
674 (setq count (1- count))))
675 (and expansion
676 (setq dabbrev--last-expansion-location (point)))
677 expansion))))
678
679 ;;; Find all expansions of ABBREV
680 (defun dabbrev--find-all-expansions (abbrev ignore-case)
681 (let ((all-expansions nil)
682 expansion)
683 (save-excursion
684 (goto-char (point-min))
685 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
686 (setq all-expansions (cons expansion all-expansions))))
687 all-expansions))
688
689 (defun dabbrev--scanning-message ()
690 (message "Scanning `%s'" (buffer-name (current-buffer))))
691
692 ;;; Find one occasion of ABBREV.
693 ;;; DIRECTION > 0 means look that many times backwards.
694 ;;; DIRECTION < 0 means look that many times forward.
695 ;;; DIRECTION = 0 means try both backward and forward.
696 ;;; IGNORE-CASE non-nil means ignore case when searching.
697 (defun dabbrev--find-expansion (abbrev direction ignore-case)
698 (let (expansion)
699 (save-excursion
700 (cond
701 (dabbrev--last-buffer
702 (set-buffer dabbrev--last-buffer)
703 (dabbrev--scanning-message))
704 ((and (not dabbrev-search-these-buffers-only)
705 (window-minibuffer-p (selected-window)))
706 (set-buffer (dabbrev--minibuffer-origin))
707 ;; In the minibuffer-origin buffer we will only search from
708 ;; the top and down.
709 (goto-char (point-min))
710 (setq direction -1)
711 (dabbrev--scanning-message)))
712 (cond
713 ;; ------------------------------------------
714 ;; Look backwards
715 ;; ------------------------------------------
716 ((and (not dabbrev-search-these-buffers-only)
717 (>= direction 0)
718 (setq dabbrev--last-direction (min 1 direction))
719 (setq expansion (dabbrev--try-find abbrev t
720 (max 1 direction)
721 ignore-case)))
722 expansion)
723 ;; ------------------------------------------
724 ;; Look forward
725 ;; ------------------------------------------
726 ((and (or (not dabbrev-search-these-buffers-only)
727 dabbrev--last-buffer)
728 (<= direction 0)
729 (setq dabbrev--last-direction -1)
730 (setq expansion (dabbrev--try-find abbrev nil
731 (max 1 (- direction))
732 ignore-case)))
733 expansion)
734 ;; ------------------------------------------
735 ;; Look in other buffers.
736 ;; Start at (point-min) and look forward.
737 ;; ------------------------------------------
738 (t
739 (setq dabbrev--last-direction -1)
740 ;; Make sure that we should check other buffers
741 (or dabbrev--friend-buffer-list
742 dabbrev--last-buffer
743 (setq dabbrev--friend-buffer-list
744 (mapcar (function get-buffer)
745 dabbrev-search-these-buffers-only))
746 (not dabbrev--check-other-buffers)
747 (not (or (eq dabbrev--check-other-buffers t)
748 (progn
749 (setq dabbrev--check-other-buffers
750 (y-or-n-p "Scan other buffers also? ")))))
751 (let* (friend-buffer-list non-friend-buffer-list)
752 (setq dabbrev--friend-buffer-list
753 (funcall dabbrev-select-buffers-function))
754 (if dabbrev-check-all-buffers
755 (setq non-friend-buffer-list
756 (nreverse
757 (dabbrev-filter-elements
758 buffer (buffer-list)
759 (not (memq buffer dabbrev--friend-buffer-list))))
760 dabbrev--friend-buffer-list
761 (append dabbrev--friend-buffer-list
762 non-friend-buffer-list)))))
763 ;; Move buffers that are visible on the screen
764 ;; to the front of the list.
765 (if dabbrev--friend-buffer-list
766 (let ((w (next-window (selected-window))))
767 (while (not (eq w (selected-window)))
768 (setq dabbrev--friend-buffer-list
769 (cons (window-buffer w)
770 (delq (window-buffer w) dabbrev--friend-buffer-list)))
771 (setq w (next-window w)))))
772 ;; Walk through the buffers
773 (while (and (not expansion) dabbrev--friend-buffer-list)
774 (setq dabbrev--last-buffer
775 (car dabbrev--friend-buffer-list))
776 (setq dabbrev--friend-buffer-list
777 (cdr dabbrev--friend-buffer-list))
778 (set-buffer dabbrev--last-buffer)
779 (dabbrev--scanning-message)
780 (setq dabbrev--last-expansion-location (point-min))
781 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
782 expansion)))))
783
784 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
785 (if (eq major-mode 'picture-mode)
786 (picture-replace-match string fixedcase literal)
787 (replace-match string fixedcase literal)))
788
789 ;;;----------------------------------------------------------------
790 ;;; Substitute the current string in buffer with the expansion
791 ;;; OLD is nil or the last expansion substring.
792 ;;; ABBREV is the abbreviation we are working with.
793 ;;; EXPANSION is the expansion substring.
794 (defun dabbrev--substitute-expansion (old abbrev expansion)
795 ;;(undo-boundary)
796 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
797 case-fold-search
798 dabbrev-case-fold-search)
799 (or (not dabbrev-upcase-means-case-search)
800 (string= abbrev (downcase abbrev)))
801 (if (eq dabbrev-case-replace 'case-replace)
802 case-replace
803 dabbrev-case-replace))))
804 (and nil use-case-replace
805 (setq old (concat abbrev (or old "")))
806 (setq expansion (concat abbrev expansion)))
807 ;; If the expansion has mixed case
808 ;; and it is not simply a capitalized word,
809 ;; or if the abbrev has mixed case,
810 ;; and if the given abbrev's case pattern
811 ;; matches the start of the expansion,
812 ;; copy the expansion's case
813 ;; instead of downcasing all the rest.
814 (let ((expansion-rest (substring expansion 1)))
815 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
816 (string= expansion-rest (upcase expansion-rest)))
817 (or (string= abbrev (downcase abbrev))
818 (string= abbrev (upcase abbrev)))))
819 (string= abbrev
820 (substring expansion 0 (length abbrev))))
821 (setq use-case-replace nil)))
822 (if (equal abbrev " ")
823 (setq use-case-replace nil))
824 (if use-case-replace
825 (setq expansion (downcase expansion)))
826 (if old
827 (save-excursion
828 (search-backward old))
829 ;;(store-match-data (list (point-marker) (point-marker)))
830 (search-backward abbrev))
831 ;; Make case of replacement conform to case of abbreviation
832 ;; provided (1) that kind of thing is enabled in this buffer
833 ;; and (2) the replacement itself is all lower case.
834 (dabbrev--safe-replace-match expansion
835 (not use-case-replace)
836 t)))
837
838
839 ;;;----------------------------------------------------------------
840 ;;; Search function used by dabbrevs library.
841
842 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
843 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
844 ;;; controls the maximum search region size. Third argument IGNORE-CASE
845 ;;; non-nil means treat case as insignificant while looking for a match
846 ;;; and when comparing with previous matches. Also if that's non-nil
847 ;;; and the match is found at the beginning of a sentence and is in
848 ;;; lower case except for the initial then it is converted to all lower
849 ;;; case for return.
850
851 ;;; Table of expansions already seen is examined in buffer
852 ;;; `dabbrev--last-table' so that only distinct possibilities are found
853 ;;; by dabbrev-re-expand.
854
855 ;;; Value is the expansion, or nil if not found.
856
857 (defun dabbrev--search (abbrev reverse ignore-case)
858 (save-match-data
859 (let ((pattern1 (concat (regexp-quote abbrev)
860 "\\(" dabbrev--abbrev-char-regexp "\\)"))
861 (pattern2 (concat (regexp-quote abbrev)
862 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
863 (found-string nil))
864 ;; Limited search.
865 (save-restriction
866 (and dabbrev-limit
867 (narrow-to-region dabbrev--last-expansion-location
868 (+ (point)
869 (if reverse (- dabbrev-limit) dabbrev-limit))))
870 ;;--------------------------------
871 ;; Look for a distinct expansion, using dabbrev--last-table.
872 ;;--------------------------------
873 (while (and (not found-string)
874 (if reverse
875 (re-search-backward pattern1 nil t)
876 (re-search-forward pattern1 nil t)))
877 (goto-char (match-beginning 0))
878 ;; In case we matched in the middle of a word,
879 ;; back up to start of word and verify we still match.
880 (dabbrev--goto-start-of-abbrev)
881
882 (if (not (looking-at pattern1))
883 nil
884 ;; We have a truly valid match. Find the end.
885 (re-search-forward pattern2)
886 (setq found-string (buffer-substring-no-properties
887 (match-beginning 1) (match-end 1)))
888 (and ignore-case (setq found-string (downcase found-string)))
889 ;; Ignore this match if it's already in the table.
890 (if (dabbrev-filter-elements
891 table-string dabbrev--last-table
892 (string= found-string table-string))
893 (setq found-string nil)))
894 ;; Prepare to continue searching.
895 (if reverse
896 (goto-char (match-beginning 0))
897 (goto-char (match-end 0))))
898 ;; If we found something, use it.
899 (if found-string
900 ;; Put it into `dabbrev--last-table'
901 ;; and return it (either downcased, or as is).
902 (let ((result (buffer-substring-no-properties
903 (match-beginning 0) (match-end 0))))
904 (setq dabbrev--last-table
905 (cons found-string dabbrev--last-table))
906 (if (and ignore-case (eval dabbrev-case-replace))
907 result
908 result)))))))
909
910 (provide 'dabbrev)
911
912 ;;; dabbrev.el ends here