(vc-svn-registered): Catch all errors.
[bpt/emacs.git] / lisp / dabbrev.el
1 ;;; dabbrev.el --- dynamic abbreviation package
2
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1996, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, 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 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
188 (defcustom dabbrev-abbrev-char-regexp nil
189 "*Regexp to recognize a character in an abbreviation or expansion.
190 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
191
192 Set this variable to \"\\\\sw\" if you want ordinary words or
193 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
194 syntax is \"symbol\" as well as those whose syntax is \"word\".
195
196 The value nil has a special meaning: the abbreviation is from point to
197 previous word-start, but the search is for symbols.
198
199 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
200 while `yes', `or', `no' and `p' are considered words. If this
201 variable is nil, then expanding `yes-or-no-' looks for a symbol
202 starting with or containing `no-'. If you set this variable to
203 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
204 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
205 expanding `yes-or-no-' signals an error because `-' is not part of a word;
206 but expanding `yes-or-no' looks for a word starting with `no'.
207
208 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
209 :type '(choice (const nil)
210 regexp)
211 :group 'dabbrev)
212
213 (defcustom dabbrev-check-all-buffers t
214 "*Non-nil means dabbrev package should search *all* buffers.
215
216 Dabbrev always searches the current buffer first. Then, if
217 `dabbrev-check-other-buffers' says so, it searches the buffers
218 designated by `dabbrev-select-buffers-function'.
219
220 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
221 all the other buffers, except those named in `dabbrev-ignored-buffer-names',
222 or matched by `dabbrev-ignored-regexps'."
223 :type 'boolean
224 :group 'dabbrev)
225
226 (defcustom dabbrev-ignored-buffer-names '("*Messages*" "*Buffer List*")
227 "*List of buffer names that dabbrev should not check.
228 See also `dabbrev-ignored-buffer-regexps'."
229 :type '(repeat (string :tag "Buffer name"))
230 :group 'dabbrev
231 :version "20.3")
232
233 (defcustom dabbrev-ignored-buffer-regexps nil
234 "*List of regexps matching names of buffers that dabbrev should not check.
235 See also `dabbrev-ignored-buffer-names'."
236 :type '(repeat regexp)
237 :group 'dabbrev
238 :version "21.1")
239
240 (defcustom dabbrev-check-other-buffers t
241 "*Should \\[dabbrev-expand] look in other buffers?\
242
243 nil: Don't look in other buffers.
244 t: Also look for expansions in the buffers pointed out by
245 `dabbrev-select-buffers-function'.
246 Anything else: When we can't find any more expansions in
247 the current buffer, then ask the user whether to look in other
248 buffers too.
249
250 The default value is t."
251 :type '(choice (const :tag "off" nil)
252 (const :tag "on" t)
253 (other :tag "ask" other))
254 :group 'dabbrev)
255
256 ;; I guess setting this to a function that selects all C- or C++-
257 ;; mode buffers would be a good choice for a debugging buffer,
258 ;; when debugging C- or C++-code.
259 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
260 "A function that selects buffers that should be searched by dabbrev.
261 The function should take no arguments and return a list of buffers to
262 search for expansions. See the source of `dabbrev--select-buffers'
263 for an example.
264
265 A mode setting this variable should make it buffer local.")
266
267 (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
268 "*A function to decide whether dabbrev should search OTHER-BUFFER.
269 The function should take one argument, OTHER-BUFFER, and return
270 non-nil if that buffer should be searched. Have a look at
271 `dabbrev--same-major-mode-p' for an example.
272
273 The value of `dabbrev-friend-buffer-function' has an effect only if
274 the value of `dabbrev-select-buffers-function' uses it. The function
275 `dabbrev--select-buffers' is one function you can use here.
276
277 A mode setting this variable should make it buffer local."
278 :type 'function
279 :group 'dabbrev)
280
281 (defcustom dabbrev-search-these-buffers-only nil
282 "If non-nil, a list of buffers which dabbrev should search.
283 If this variable is non-nil, dabbrev will only look in these buffers.
284 It will not even look in the current buffer if it is not a member of
285 this list."
286 :group 'dabbrev)
287
288 ;;----------------------------------------------------------------
289 ;; Internal variables
290 ;;----------------------------------------------------------------
291
292 ;; Last obarray of completions in `dabbrev-completion'
293 (defvar dabbrev--last-obarray nil)
294
295 ;; Table of expansions seen so far
296 (defvar dabbrev--last-table nil)
297
298 ;; Last string we tried to expand.
299 (defvar dabbrev--last-abbreviation nil)
300
301 ;; Location last abbreviation began
302 (defvar dabbrev--last-abbrev-location nil)
303
304 ;; Direction of last dabbrevs search
305 (defvar dabbrev--last-direction 0)
306
307 ;; Last expansion of an abbreviation.
308 (defvar dabbrev--last-expansion nil)
309
310 ;; Location the last expansion was found.
311 (defvar dabbrev--last-expansion-location nil)
312
313 ;; The list of remaining buffers with the same mode as current buffer.
314 (defvar dabbrev--friend-buffer-list nil)
315
316 ;; The buffer we looked in last, not counting the current buffer.
317 (defvar dabbrev--last-buffer nil)
318
319 ;; The buffer we found the expansion last time.
320 (defvar dabbrev--last-buffer-found nil)
321
322 ;; The buffer we last did a completion in.
323 (defvar dabbrev--last-completion-buffer nil)
324
325 ;; If non-nil, a function to use when copying successive words.
326 ;; It should be `upcase' or `downcase'.
327 (defvar dabbrev--last-case-pattern nil)
328
329 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
330 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
331
332 ;; The regexp for recognizing a character in an abbreviation.
333 (defvar dabbrev--abbrev-char-regexp nil)
334
335 ;;----------------------------------------------------------------
336 ;; Macros
337 ;;----------------------------------------------------------------
338
339 ;;; Get the buffer that mini-buffer was activated from
340 (defsubst dabbrev--minibuffer-origin ()
341 (car (cdr (buffer-list))))
342
343 ;; Make a list of some of the elements of LIST.
344 ;; Check each element of LIST, storing it temporarily in the
345 ;; variable ELEMENT, and include it in the result
346 ;; if CONDITION evaluates non-nil.
347 (defmacro dabbrev-filter-elements (element list condition)
348 `(let (dabbrev-result dabbrev-tail ,element)
349 (setq dabbrev-tail ,list)
350 (while dabbrev-tail
351 (setq ,element (car dabbrev-tail))
352 (if ,condition
353 (setq dabbrev-result (cons ,element dabbrev-result)))
354 (setq dabbrev-tail (cdr dabbrev-tail)))
355 (nreverse dabbrev-result)))
356
357 ;;----------------------------------------------------------------
358 ;; Exported functions
359 ;;----------------------------------------------------------------
360
361 ;;;###autoload (define-key esc-map "/" 'dabbrev-expand)
362 ;;;??? Do we want this?
363 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion)
364
365 ;;;###autoload
366 (defun dabbrev-completion (&optional arg)
367 "Completion on current word.
368 Like \\[dabbrev-expand] but finds all expansions in the current buffer
369 and presents suggestions for completion.
370
371 With a prefix argument, it searches all buffers accepted by the
372 function pointed out by `dabbrev-friend-buffer-function' to find the
373 completions.
374
375 If the prefix argument is 16 (which comes from C-u C-u),
376 then it searches *all* buffers.
377
378 With no prefix argument, it reuses an old completion list
379 if there is a suitable one already."
380
381 (interactive "*P")
382 (dabbrev--reset-global-variables)
383 (let* ((dabbrev-check-other-buffers (and arg t))
384 (dabbrev-check-all-buffers
385 (and arg (= (prefix-numeric-value arg) 16)))
386 (abbrev (dabbrev--abbrev-at-point))
387 (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search)
388 case-fold-search
389 dabbrev-case-fold-search)
390 (or (not dabbrev-upcase-means-case-search)
391 (string= abbrev (downcase abbrev)))))
392 (my-obarray dabbrev--last-obarray)
393 init)
394 (save-excursion
395 (if (and (null arg)
396 my-obarray
397 (or (eq dabbrev--last-completion-buffer (current-buffer))
398 (and (window-minibuffer-p (selected-window))
399 (eq dabbrev--last-completion-buffer
400 (dabbrev--minibuffer-origin))))
401 dabbrev--last-abbreviation
402 (>= (length abbrev) (length dabbrev--last-abbreviation))
403 (string= dabbrev--last-abbreviation
404 (substring abbrev 0
405 (length dabbrev--last-abbreviation)))
406 (setq init (try-completion abbrev my-obarray)))
407 ;; We can reuse the existing completion list.
408 nil
409 ;;--------------------------------
410 ;; New abbreviation to expand.
411 ;;--------------------------------
412 (setq dabbrev--last-abbreviation abbrev)
413 ;; Find all expansion
414 (let ((completion-list
415 (dabbrev--find-all-expansions abbrev ignore-case-p))
416 (completion-ignore-case ignore-case-p))
417 ;; Make an obarray with all expansions
418 (setq my-obarray (make-vector (length completion-list) 0))
419 (or (> (length my-obarray) 0)
420 (error "No dynamic expansion for \"%s\" found%s"
421 abbrev
422 (if dabbrev--check-other-buffers "" " in this-buffer")))
423 (cond
424 ((or (not ignore-case-p)
425 (not dabbrev-case-replace))
426 (mapc (function (lambda (string)
427 (intern string my-obarray)))
428 completion-list))
429 ((string= abbrev (upcase abbrev))
430 (mapc (function (lambda (string)
431 (intern (upcase string) my-obarray)))
432 completion-list))
433 ((string= (substring abbrev 0 1)
434 (upcase (substring abbrev 0 1)))
435 (mapc (function (lambda (string)
436 (intern (capitalize string) my-obarray)))
437 completion-list))
438 (t
439 (mapc (function (lambda (string)
440 (intern (downcase string) my-obarray)))
441 completion-list)))
442 (setq dabbrev--last-obarray my-obarray)
443 (setq dabbrev--last-completion-buffer (current-buffer))
444 ;; Find the longest common string.
445 (setq init (try-completion abbrev my-obarray)))))
446 ;;--------------------------------
447 ;; Let the user choose between the expansions
448 ;;--------------------------------
449 (or (stringp init)
450 (setq init abbrev))
451 (cond
452 ;; * Replace string fragment with matched common substring completion.
453 ((and (not (string-equal init ""))
454 (not (string-equal (downcase init) (downcase abbrev))))
455 (if (> (length (all-completions init my-obarray)) 1)
456 (message "Repeat `%s' to see all completions"
457 (key-description (this-command-keys)))
458 (message "The only possible completion"))
459 (dabbrev--substitute-expansion nil abbrev init nil))
460 (t
461 ;; * String is a common substring completion already. Make list.
462 (message "Making completion list...")
463 (with-output-to-temp-buffer "*Completions*"
464 (display-completion-list (all-completions init my-obarray)
465 init))
466 (message "Making completion list...done")))
467 (and (window-minibuffer-p (selected-window))
468 (message nil))))
469
470 ;;;###autoload
471 (defun dabbrev-expand (arg)
472 "Expand previous word \"dynamically\".
473
474 Expands to the most recent, preceding word for which this is a prefix.
475 If no suitable preceding word is found, words following point are
476 considered. If still no suitable word is found, then look in the
477 buffers accepted by the function pointed out by variable
478 `dabbrev-friend-buffer-function'.
479
480 A positive prefix argument, N, says to take the Nth backward *distinct*
481 possibility. A negative argument says search forward.
482
483 If the cursor has not moved from the end of the previous expansion and
484 no argument is given, replace the previously-made expansion
485 with the next possible expansion not yet tried.
486
487 The variable `dabbrev-backward-only' may be used to limit the
488 direction of search to backward if set non-nil.
489
490 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
491 (interactive "*P")
492 (let (abbrev record-case-pattern
493 expansion old direction (orig-point (point)))
494 ;; abbrev -- the abbrev to expand
495 ;; expansion -- the expansion found (eventually) or nil until then
496 ;; old -- the text currently in the buffer
497 ;; (the abbrev, or the previously-made expansion)
498 (save-excursion
499 (if (and (null arg)
500 (markerp dabbrev--last-abbrev-location)
501 (marker-position dabbrev--last-abbrev-location)
502 (or (eq last-command this-command)
503 (and (window-minibuffer-p (selected-window))
504 (= dabbrev--last-abbrev-location
505 (point)))))
506 ;; Find a different expansion for the same abbrev as last time.
507 (progn
508 (setq abbrev dabbrev--last-abbreviation)
509 (setq old dabbrev--last-expansion)
510 (setq direction dabbrev--last-direction))
511 ;; If the user inserts a space after expanding
512 ;; and then asks to expand again, always fetch the next word.
513 (if (and (eq (preceding-char) ?\s)
514 (markerp dabbrev--last-abbrev-location)
515 (marker-position dabbrev--last-abbrev-location)
516 (= (point) (1+ dabbrev--last-abbrev-location)))
517 (progn
518 ;; The "abbrev" to expand is just the space.
519 (setq abbrev " ")
520 (save-excursion
521 (save-restriction
522 (widen)
523 (if dabbrev--last-buffer
524 (set-buffer dabbrev--last-buffer))
525 ;; Find the end of the last "expansion" word.
526 (if (or (eq dabbrev--last-direction 1)
527 (and (eq dabbrev--last-direction 0)
528 (< dabbrev--last-expansion-location (point))))
529 (setq dabbrev--last-expansion-location
530 (+ dabbrev--last-expansion-location
531 (length dabbrev--last-expansion))))
532 (goto-char dabbrev--last-expansion-location)
533 ;; Take the following word, with intermediate separators,
534 ;; as our expansion this time.
535 (re-search-forward
536 (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
537 (setq expansion (buffer-substring-no-properties
538 dabbrev--last-expansion-location (point)))
539
540 ;; Record the end of this expansion, in case we repeat this.
541 (setq dabbrev--last-expansion-location (point))))
542 ;; Indicate that dabbrev--last-expansion-location is
543 ;; at the end of the expansion.
544 (setq dabbrev--last-direction -1))
545
546 ;; We have a different abbrev to expand.
547 (dabbrev--reset-global-variables)
548 (setq direction (if (null arg)
549 (if dabbrev-backward-only 1 0)
550 (prefix-numeric-value arg)))
551 (setq abbrev (dabbrev--abbrev-at-point))
552 (setq record-case-pattern t)
553 (setq old nil)))
554
555 ;;--------------------------------
556 ;; Find the expansion
557 ;;--------------------------------
558 (or expansion
559 (setq expansion
560 (dabbrev--find-expansion abbrev direction
561 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
562 case-fold-search
563 dabbrev-case-fold-search)
564 (or (not dabbrev-upcase-means-case-search)
565 (string= abbrev (downcase abbrev))))))))
566 (cond
567 ((not expansion)
568 (dabbrev--reset-global-variables)
569 (if old
570 (save-excursion
571 (setq buffer-undo-list (cons orig-point buffer-undo-list))
572 ;; Put back the original abbrev with its original case pattern.
573 (search-backward old)
574 (insert abbrev)
575 (delete-region (point) (+ (point) (length old)))))
576 (error "No%s dynamic expansion for `%s' found"
577 (if old " further" "") abbrev))
578 (t
579 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
580 (minibuffer-window-active-p (selected-window))))
581 (progn
582 (message "Expansion found in '%s'"
583 (buffer-name dabbrev--last-buffer))
584 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
585 (message nil))
586 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
587 (null dabbrev--last-buffer))
588 (numberp dabbrev--last-expansion-location)
589 (and (> dabbrev--last-expansion-location (point))))
590 (setq dabbrev--last-expansion-location
591 (copy-marker dabbrev--last-expansion-location)))
592 ;; Success: stick it in and return.
593 (setq buffer-undo-list (cons orig-point buffer-undo-list))
594 (dabbrev--substitute-expansion old abbrev expansion
595 record-case-pattern)
596
597 ;; Save state for re-expand.
598 (setq dabbrev--last-expansion expansion)
599 (setq dabbrev--last-abbreviation abbrev)
600 (setq dabbrev--last-abbrev-location (point-marker))))))
601
602 ;;----------------------------------------------------------------
603 ;; Local functions
604 ;;----------------------------------------------------------------
605
606 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
607 (defun dabbrev--same-major-mode-p (other-buffer)
608 (eq major-mode
609 (save-excursion
610 (set-buffer other-buffer)
611 major-mode)))
612
613 ;;; Back over all abbrev type characters and then moves forward over
614 ;;; all skip characters.
615 (defun dabbrev--goto-start-of-abbrev ()
616 ;; Move backwards over abbrev chars
617 (save-match-data
618 (when (> (point) (minibuffer-prompt-end))
619 (forward-char -1)
620 (while (and (looking-at dabbrev--abbrev-char-regexp)
621 (> (point) (minibuffer-prompt-end))
622 (not (= (point) (field-beginning (point) nil
623 (1- (point))))))
624 (forward-char -1))
625 (or (looking-at dabbrev--abbrev-char-regexp)
626 (forward-char 1)))
627 (and dabbrev-abbrev-skip-leading-regexp
628 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
629 (forward-char 1)))))
630
631 ;;; Extract the symbol at point to serve as abbreviation.
632 (defun dabbrev--abbrev-at-point ()
633 ;; Check for error
634 (if (bobp)
635 (error "No possible abbreviation preceding point"))
636 ;; Return abbrev at point
637 (save-excursion
638 ;; Record the end of the abbreviation.
639 (setq dabbrev--last-abbrev-location (point))
640 ;; If we aren't right after an abbreviation,
641 ;; move point back to just after one.
642 ;; This is so the user can get successive words
643 ;; by typing the punctuation followed by M-/.
644 (save-match-data
645 (if (save-excursion
646 (forward-char -1)
647 (not (looking-at (concat "\\("
648 (or dabbrev-abbrev-char-regexp
649 "\\sw\\|\\s_")
650 "\\)+"))))
651 (if (re-search-backward (or dabbrev-abbrev-char-regexp
652 "\\sw\\|\\s_")
653 nil t)
654 (forward-char 1)
655 (error "No possible abbreviation preceding point"))))
656 ;; Now find the beginning of that one.
657 (dabbrev--goto-start-of-abbrev)
658 (buffer-substring-no-properties
659 dabbrev--last-abbrev-location (point))))
660
661 ;;; Initializes all global variables
662 (defun dabbrev--reset-global-variables ()
663 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
664 ;; must not be reset here.
665 (setq dabbrev--last-table nil
666 dabbrev--last-abbreviation nil
667 dabbrev--last-abbrev-location nil
668 dabbrev--last-direction nil
669 dabbrev--last-expansion nil
670 dabbrev--last-expansion-location nil
671 dabbrev--friend-buffer-list nil
672 dabbrev--last-buffer nil
673 dabbrev--last-buffer-found nil
674 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
675 "\\sw\\|\\s_")
676 dabbrev--check-other-buffers dabbrev-check-other-buffers))
677
678 (defun dabbrev--select-buffers ()
679 "Return a list of other buffers to search for a possible abbrev.
680 The current buffer is not included in the list.
681
682 This function makes a list of all the buffers returned by `buffer-list',
683 then discards buffers whose names match `dabbrev-ignored-buffer-names'
684 or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
685 `dabbrev-friend-buffer-function', if it is bound, returns nil when called
686 with the buffer as argument.
687 It returns the list of the buffers that are not discarded."
688 (dabbrev-filter-elements
689 buffer (buffer-list)
690 (and (not (eq (current-buffer) buffer))
691 (not (dabbrev--ignore-buffer-p buffer))
692 (boundp 'dabbrev-friend-buffer-function)
693 (funcall dabbrev-friend-buffer-function buffer))))
694
695 (defun dabbrev--try-find (abbrev reverse n ignore-case)
696 "Search for ABBREV, backwards if REVERSE, N times.
697 If IGNORE-CASE is non-nil, ignore case while searching.
698 Return the expansion found, and save the location of the start
699 of the expansion in `dabbrev--last-expansion-location'."
700 (save-excursion
701 (save-restriction
702 (widen)
703 (let ((expansion nil))
704 (and dabbrev--last-expansion-location
705 (goto-char dabbrev--last-expansion-location))
706 (let ((case-fold-search ignore-case)
707 (count n))
708 (while (and (> count 0)
709 (setq expansion (dabbrev--search abbrev
710 reverse
711 (and ignore-case
712 (if (eq dabbrev-case-distinction 'case-replace)
713 case-replace
714 dabbrev-case-distinction))
715 )))
716 (setq count (1- count))))
717 (and expansion
718 (setq dabbrev--last-expansion-location (point)))
719 expansion))))
720
721 (defun dabbrev--find-all-expansions (abbrev ignore-case)
722 "Return a list of all possible expansions of ABBREV.
723 If IGNORE-CASE is non-nil, accept matches which differ in case."
724 (let ((all-expansions nil)
725 expansion)
726 (save-excursion
727 (goto-char (point-min))
728 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
729 (setq all-expansions (cons expansion all-expansions))))
730 all-expansions))
731
732 (defun dabbrev--scanning-message ()
733 (unless (window-minibuffer-p (selected-window))
734 (message "Scanning `%s'" (buffer-name (current-buffer)))))
735
736 (defun dabbrev--ignore-buffer-p (buffer)
737 "Return non-nil if BUFFER should be ignored by dabbrev."
738 (let ((bn (buffer-name buffer)))
739 (or (member bn dabbrev-ignored-buffer-names)
740 (let ((tail dabbrev-ignored-buffer-regexps)
741 (match nil))
742 (while (and tail (not match))
743 (setq match (string-match (car tail) bn)
744 tail (cdr tail)))
745 match))))
746
747 (defun dabbrev--find-expansion (abbrev direction ignore-case)
748 "Find one occurrence of ABBREV, and return the expansion.
749 DIRECTION > 0 means look that many times backwards.
750 DIRECTION < 0 means look that many times forward.
751 DIRECTION = 0 means try both backward and forward.
752 IGNORE-CASE non-nil means ignore case when searching.
753 This sets `dabbrev--last-direction' to 1 or -1 according
754 to the direction in which the occurrence was actually found.
755 It sets `dabbrev--last-expansion-location' to the location
756 of the start of the occurrence."
757 (save-excursion
758 ;; If we were scanning something other than the current buffer,
759 ;; continue scanning there.
760 (when dabbrev--last-buffer
761 (set-buffer dabbrev--last-buffer)
762 (dabbrev--scanning-message))
763 (or
764 ;; ------------------------------------------
765 ;; Look backward in current buffer.
766 ;; ------------------------------------------
767 (and (not dabbrev-search-these-buffers-only)
768 (>= direction 0)
769 (setq dabbrev--last-direction (min 1 direction))
770 (dabbrev--try-find abbrev t
771 (max 1 direction)
772 ignore-case))
773 ;; ------------------------------------------
774 ;; Look forward in current buffer
775 ;; or whatever buffer we were last scanning.
776 ;; ------------------------------------------
777 (and (or (not dabbrev-search-these-buffers-only)
778 dabbrev--last-buffer)
779 (<= direction 0)
780 (setq dabbrev--last-direction -1)
781 (dabbrev--try-find abbrev nil
782 (max 1 (- direction))
783 ignore-case))
784 ;; ------------------------------------------
785 ;; Look in other buffers.
786 ;; Always start at (point-min) and look forward.
787 ;; ------------------------------------------
788 (progn
789 (setq dabbrev--last-direction -1)
790 (unless dabbrev--last-buffer
791 ;; If we have just now begun to search other buffers,
792 ;; determine which other buffers we should check.
793 ;; Put that list in dabbrev--friend-buffer-list.
794 (or dabbrev--friend-buffer-list
795 (setq dabbrev--friend-buffer-list
796 (dabbrev--make-friend-buffer-list))))
797 ;; Walk through the buffers till we find a match.
798 (let (expansion)
799 (while (and (not expansion) dabbrev--friend-buffer-list)
800 (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
801 (set-buffer dabbrev--last-buffer)
802 (dabbrev--scanning-message)
803 (setq dabbrev--last-expansion-location (point-min))
804 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
805 expansion)))))
806
807 ;; Compute the list of buffers to scan.
808 ;; If dabbrev-search-these-buffers-only, then the current buffer
809 ;; is included in this list if it should be searched.
810 ;; Otherwise, the current buffer is searched first specially.,
811 ;; and it is not included in this list.
812 (defun dabbrev--make-friend-buffer-list ()
813 (let ((list (mapcar (function get-buffer)
814 dabbrev-search-these-buffers-only)))
815 (when (and (null dabbrev-search-these-buffers-only)
816 dabbrev--check-other-buffers
817 (or (eq dabbrev--check-other-buffers t)
818 (setq dabbrev--check-other-buffers
819 (y-or-n-p "Scan other buffers also? "))))
820 (setq list (funcall dabbrev-select-buffers-function))
821 ;; If dabbrev-check-all-buffers, tack on all the other
822 ;; buffers at the end of the list, except those which are
823 ;; specifically to be ignored.
824 (if dabbrev-check-all-buffers
825 (setq list
826 (append list
827 (dabbrev-filter-elements
828 buffer (buffer-list)
829 (and (not (memq buffer list))
830 (not (dabbrev--ignore-buffer-p buffer)))))))
831 ;; Remove the current buffer.
832 (setq list (delq (current-buffer) list)))
833 ;; Move buffers in the list that are visible on the screen
834 ;; to the front of the list, but don't add anything to the list.
835 (if list
836 (walk-windows (lambda (w)
837 (unless (eq w (selected-window))
838 (if (memq (window-buffer w) list)
839 (setq list
840 (cons (window-buffer w)
841 (delq (window-buffer w)
842 list))))))))
843 ;; In a minibuffer, search the buffer it was activated from,
844 ;; first after the minibuffer itself. Unless we aren't supposed
845 ;; to search the current buffer either.
846 (if (and (window-minibuffer-p (selected-window))
847 (not dabbrev-search-these-buffers-only))
848 (setq list
849 (cons (dabbrev--minibuffer-origin)
850 (delq (dabbrev--minibuffer-origin) list))))
851 list))
852
853 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
854 (if (eq major-mode 'picture-mode)
855 (with-no-warnings
856 (picture-replace-match string fixedcase literal))
857 (replace-match string fixedcase literal)))
858
859 ;;;----------------------------------------------------------------
860 (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
861 "Replace OLD with EXPANSION in the buffer.
862 OLD is text currently in the buffer, perhaps the abbreviation
863 or perhaps another expansion that was tried previously.
864 ABBREV is the abbreviation we are expanding.
865 It is \" \" if we are copying subsequent words.
866 EXPANSION is the expansion substring to be used this time.
867 RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
868 to record whether we upcased the expansion, downcased it, or did neither."
869 ;;(undo-boundary)
870 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
871 case-fold-search
872 dabbrev-case-fold-search)
873 (or (not dabbrev-upcase-means-case-search)
874 (string= abbrev (downcase abbrev)))
875 (if (eq dabbrev-case-replace 'case-replace)
876 case-replace
877 dabbrev-case-replace))))
878
879 ;; If we upcased or downcased the original expansion,
880 ;; do likewise for the subsequent words when we copy them.
881 ;; Don't do any of the usual case processing, though.
882 (when (equal abbrev " ")
883 (if dabbrev--last-case-pattern
884 (setq expansion
885 (funcall dabbrev--last-case-pattern expansion)))
886 (setq use-case-replace nil))
887
888 ;; If the expansion has mixed case
889 ;; and it is not simply a capitalized word,
890 ;; or if the abbrev has mixed case,
891 ;; and if the given abbrev's case pattern
892 ;; matches the start of the expansion,
893 ;; copy the expansion's case
894 ;; instead of downcasing all the rest.
895 ;;
896 ;; Treat a one-capital-letter (possibly with preceding non-letter
897 ;; characters) abbrev as "not all upper case", so as to force
898 ;; preservation of the expansion's pattern if the expansion starts
899 ;; with a capital letter.
900 (let ((expansion-rest (substring expansion 1))
901 (first-letter-position (string-match "[[:alpha:]]" abbrev)))
902 (if (or (null first-letter-position)
903 (and (not (and (or (string= expansion-rest (downcase expansion-rest))
904 (string= expansion-rest (upcase expansion-rest)))
905 (or (string= abbrev (downcase abbrev))
906 (and (string= abbrev (upcase abbrev))
907 (> (- (length abbrev) first-letter-position)
908 1)))))
909 (string= abbrev
910 (substring expansion 0 (length abbrev)))))
911 (setq use-case-replace nil)))
912
913 ;; If the abbrev and the expansion are both all-lower-case
914 ;; then don't do any conversion. The conversion would be a no-op
915 ;; for this replacement, but it would carry forward to subsequent words.
916 ;; The goal of this is to prevent that carrying forward.
917 (if (and (string= expansion (downcase expansion))
918 (string= abbrev (downcase abbrev)))
919 (setq use-case-replace nil))
920
921 (if use-case-replace
922 (setq expansion (downcase expansion)))
923
924 ;; In case we insert subsequent words,
925 ;; record if we upcased or downcased the first word,
926 ;; in order to do likewise for subsequent words.
927 (and record-case-pattern
928 (setq dabbrev--last-case-pattern
929 (and use-case-replace
930 (cond ((equal abbrev (upcase abbrev)) 'upcase)
931 ((equal abbrev (downcase abbrev)) 'downcase)))))
932
933 ;; Convert whitespace to single spaces.
934 (if dabbrev--eliminate-newlines
935 ;; Start searching at end of ABBREV so that any whitespace
936 ;; carried over from the existing text is not changed.
937 (let ((pos (length abbrev)))
938 (while (string-match "[\n \t]+" expansion pos)
939 (setq pos (1+ (match-beginning 0)))
940 (setq expansion (replace-match " " nil nil expansion)))))
941
942 (if old
943 (save-excursion
944 (search-backward old))
945 ;;(set-match-data (list (point-marker) (point-marker)))
946 (search-backward abbrev)
947 (search-forward abbrev))
948
949 ;; Make case of replacement conform to case of abbreviation
950 ;; provided (1) that kind of thing is enabled in this buffer
951 ;; and (2) the replacement itself is all lower case.
952 (dabbrev--safe-replace-match expansion
953 (not use-case-replace)
954 t)))
955
956
957 ;;;----------------------------------------------------------------
958 ;;; Search function used by dabbrevs library.
959
960
961 (defun dabbrev--search (abbrev reverse ignore-case)
962 "Search for something that could be used to expand ABBREV.
963
964 Second arg, REVERSE, is t for reverse search, nil for forward.
965 The variable `dabbrev-limit' controls the maximum search region size.
966 Third argument IGNORE-CASE non-nil means treat case as insignificant while
967 looking for a match and when comparing with previous matches. Also if
968 that's non-nil and the match is found at the beginning of a sentence
969 and is in lower case except for the initial then it is converted to
970 all lower case for return.
971
972 Table of expansions already seen is examined in buffer
973 `dabbrev--last-table' so that only distinct possibilities are found
974 by dabbrev-re-expand.
975
976 Returns the expansion found, or nil if not found.
977 Leaves point at the location of the start of the expansion."
978 (save-match-data
979 (let ((pattern1 (concat (regexp-quote abbrev)
980 "\\(" dabbrev--abbrev-char-regexp "\\)"))
981 (pattern2 (concat (regexp-quote abbrev)
982 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
983 ;; This makes it possible to find matches in minibuffer prompts
984 ;; even when they are "inviolable".
985 (inhibit-point-motion-hooks t)
986 found-string result)
987 ;; Limited search.
988 (save-restriction
989 (and dabbrev-limit
990 (narrow-to-region dabbrev--last-expansion-location
991 (+ (point)
992 (if reverse (- dabbrev-limit) dabbrev-limit))))
993 ;;--------------------------------
994 ;; Look for a distinct expansion, using dabbrev--last-table.
995 ;;--------------------------------
996 (while (and (not found-string)
997 (if reverse
998 (re-search-backward pattern1 nil t)
999 (re-search-forward pattern1 nil t)))
1000 (goto-char (match-beginning 0))
1001 ;; In case we matched in the middle of a word,
1002 ;; back up to start of word and verify we still match.
1003 (dabbrev--goto-start-of-abbrev)
1004
1005 (if (not (looking-at pattern1))
1006 nil
1007 ;; We have a truly valid match. Find the end.
1008 (re-search-forward pattern2)
1009 (setq found-string (match-string-no-properties 0))
1010 (setq result found-string)
1011 (and ignore-case (setq found-string (downcase found-string)))
1012 ;; Ignore this match if it's already in the table.
1013 (if (dabbrev-filter-elements
1014 table-string dabbrev--last-table
1015 (string= found-string table-string))
1016 (setq found-string nil)))
1017 ;; Prepare to continue searching.
1018 (goto-char (if reverse (match-beginning 0) (match-end 0))))
1019 ;; If we found something, use it.
1020 (when found-string
1021 ;; Put it into `dabbrev--last-table'
1022 ;; and return it (either downcased, or as is).
1023 (setq dabbrev--last-table
1024 (cons found-string dabbrev--last-table))
1025 result)))))
1026
1027 (dolist (mess '("^No dynamic expansion for .* found$"
1028 "^No further dynamic expansion for .* found$"
1029 "^No possible abbreviation preceding point$"))
1030 (add-to-list 'debug-ignored-errors mess))
1031
1032 (provide 'dabbrev)
1033
1034 ;;; arch-tag: 29e58596-f080-4306-a409-70296cf9d46f
1035 ;;; dabbrev.el ends here