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