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