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