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