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