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