(enum syntaxcode): New members Scomment_fence, Sstring_fence.
[emacs.git] / lisp / dabbrev.el
blob87f87e014d0dca8ff5d079ab4335617774a0a6b3
1 ;;; dabbrev.el --- dynamic abbreviation package
3 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
5 ;; Author: Don Morrison
6 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
7 ;; Created: 16 Mars 1992
8 ;; Lindberg's last update version: 5.7
9 ;; Keywords: abbrev expand completion
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
30 ;; The purpose with this package is to let you write just a few
31 ;; characters of words you've written earlier to be able to expand
32 ;; them.
34 ;; To expand a word, just put the point right after the word and press
35 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
37 ;; Check out the customizable variables below to learn about all the
38 ;; features of this package.
40 ;;; Hints and tips for major modes writers:
42 ;; Recommended values C/Lisp etc text
43 ;; dabbrev-case-fold-search nil t
44 ;; dabbrev-case-replace nil t
46 ;; Set the variables you want special for your mode like this:
47 ;; (set (make-local-variable 'dabbrev-case-replace) nil)
48 ;; Then you don't interfere with other modes.
50 ;; If your mode handles buffers that refers to other buffers
51 ;; (i.e. compilation-mode, gud-mode), then try to set
52 ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
53 ;; to a function that point out those buffers.
55 ;; Same goes for major-modes that are connected to other modes. There
56 ;; are for instance a number of mail-modes. One for reading, one for
57 ;; creating a new mail etc. Maybe those should be connected.
59 ;; Example for GNUS (when we write a reply, we want dabbrev to look in
60 ;; the article for expansion):
61 ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
62 ;; (lambda (buffer)
63 ;; (save-excursion
64 ;; (set-buffer buffer)
65 ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
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@ERA.COM>
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)
106 (defcustom dabbrev-backward-only nil
107 "*If non-nil, `dabbrev-expand' only looks backwards."
108 :type 'boolean
109 :group 'dabbrev)
111 (defcustom dabbrev-limit nil
112 "*Limits region searched by `dabbrev-expand' to this many chars away."
113 :type '(choice (const :tag "off" nil)
114 integer)
115 :group 'dabbrev)
117 (defcustom dabbrev-abbrev-skip-leading-regexp nil
118 "*Regexp for skipping leading characters of an abbreviation.
120 Example: Set this to \"\\\\$\" for programming languages
121 in which variable names may appear with or without a leading `$'.
122 \(For example, in Makefiles.
123 :type 'regexp
124 :group 'dabbrev))
126 Set this to nil if no characters should be skipped."
127 :type '(choice regexp
128 (const :tag "off" nil))
129 :group 'dabbrev)
131 ;; I recommend that you set this to nil.
132 (defcustom dabbrev-case-fold-search 'case-fold-search
133 "*Non-nil if dabbrev searches should ignore case.
134 A value of nil means case is significant.
136 The value of this variable is an expression; it is evaluated
137 and the resulting value determines the decision.
138 For example: setting this to `case-fold-search' means evaluate that
139 variable to see whether its value is nil."
140 :type 'sexp
141 :group 'dabbrev)
143 (defcustom dabbrev-upcase-means-case-search nil
144 "*The significance of an uppercase character in an abbreviation.
145 nil means case fold search, non-nil means case sensitive search.
147 This variable has an effect only when the value of
148 `dabbrev-case-fold-search' evaluates to t."
149 :type 'boolean
150 :group 'dabbrev)
152 ;; I recommend that you set this to nil.
153 (defcustom dabbrev-case-replace 'case-replace
154 "*Non-nil means dabbrev should preserve case when expanding the abbreviation.
155 More precisely, it preserves the case pattern of the abbreviation as you
156 typed it--as opposed to the case pattern of the expansion that is copied.
157 The value of this variable is an expression; it is evaluated
158 and the resulting value determines the decision.
159 For example, setting this to `case-replace' means evaluate that
160 variable to see if its value is t or nil.
162 This variable has an effect only when the value of
163 `dabbrev-case-fold-search' evaluates to t."
164 :type 'sexp
165 :group 'dabbrev)
167 (defcustom dabbrev-abbrev-char-regexp nil
168 "*Regexp to recognize a character in an abbreviation or expansion.
169 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
171 Set this variable to \"\\\\sw\" if you want ordinary words or
172 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
173 syntax is \"symbol\" as well as those whose syntax is \"word\".
175 The value nil has a special meaning: the abbreviation is from point to
176 previous word-start, but the search is for symbols.
178 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
179 while `yes', `or', `no' and `p' are considered words. If this
180 variable is nil, then expanding `yes-or-no-' looks for a symbol
181 starting with or containing `no-'. If you set this variable to
182 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
183 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
184 expanding `yes-or-no-' signals an error because `-' is not part of a word;
185 but expanding `yes-or-no' looks for a word starting with `no'.
187 The recommended value is \"\\\\sw\\\\|\\\\s_\"."
188 :type '(choice (const nil)
189 regexp)
190 :group 'dabbrev)
192 (defcustom dabbrev-check-all-buffers t
193 "*Non-nil means dabbrev package should search *all* buffers.
195 Dabbrev always searches the current buffer first. Then, if
196 `dabbrev-check-other-buffers' says so, it searches the buffers
197 designated by `dabbrev-select-buffers-function'.
199 Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
200 all the other buffers."
201 :type 'boolean
202 :group 'dabbrev)
204 (defcustom dabbrev-check-other-buffers t
205 "*Should \\[dabbrev-expand] look in other buffers?\
207 nil: Don't look in other buffers.
208 t: Also look for expansions in the buffers pointed out by
209 `dabbrev-select-buffers-function'.
210 Anything else: When we can't find any more expansions in
211 the current buffer, then ask the user whether to look in other
212 buffers too.
214 The default value is t."
215 :type '(choice (const :tag "off" nil)
216 (const :tag "on" t)
217 (const :tag "ask" other))
218 :group 'dabbrev)
220 ;; I guess setting this to a function that selects all C- or C++-
221 ;; mode buffers would be a good choice for a debugging buffer,
222 ;; when debugging C- or C++-code.
223 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
224 "A function that selects buffers that should be searched by dabbrev.
225 The function should take no arguments and return a list of buffers to
226 search for expansions. Have a look at `dabbrev--select-buffers' for
227 an example.
229 A mode setting this variable should make it buffer local.")
231 (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
232 "*A function to decide whether dabbrev should search OTHER-BUFFER.
233 The function should take one argument, OTHER-BUFFER, and return
234 non-nil if that buffer should be searched. Have a look at
235 `dabbrev--same-major-mode-p' for an example.
237 The value of `dabbrev-friend-buffer-function' has an effect only if
238 the value of `dabbrev-select-buffers-function' uses it. The function
239 `dabbrev--select-buffers' is one function you can use here.
241 A mode setting this variable should make it buffer local."
242 :type 'function
243 :group 'dabbrev)
245 (defcustom dabbrev-search-these-buffers-only nil
246 "If non-nil, a list of buffers which dabbrev should search.
247 If this variable is non-nil, dabbrev will only look in these buffers.
248 It will not even look in the current buffer if it is not a member of
249 this list.")
251 ;;----------------------------------------------------------------
252 ;; Internal variables
253 ;;----------------------------------------------------------------
255 ;; Last obarray of completions in `dabbrev-completion'
256 (defvar dabbrev--last-obarray nil)
258 ;; Table of expansions seen so far
259 (defvar dabbrev--last-table nil)
261 ;; Last string we tried to expand.
262 (defvar dabbrev--last-abbreviation nil)
264 ;; Location last abbreviation began
265 (defvar dabbrev--last-abbrev-location nil)
267 ;; Direction of last dabbrevs search
268 (defvar dabbrev--last-direction 0)
270 ;; Last expansion of an abbreviation.
271 (defvar dabbrev--last-expansion nil)
273 ;; Location the last expansion was found.
274 (defvar dabbrev--last-expansion-location nil)
276 ;; The list of remaining buffers with the same mode as current buffer.
277 (defvar dabbrev--friend-buffer-list nil)
279 ;; The buffer we looked in last.
280 (defvar dabbrev--last-buffer nil)
282 ;; The buffer we found the expansion last time.
283 (defvar dabbrev--last-buffer-found nil)
285 ;; The buffer we last did a completion in.
286 (defvar dabbrev--last-completion-buffer nil)
288 ;; Non-nil means we should upcase
289 ;; when copying successive words.
290 (defvar dabbrev--last-case-pattern nil)
292 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
293 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
295 ;; The regexp for recognizing a character in an abbreviation.
296 (defvar dabbrev--abbrev-char-regexp nil)
298 ;;----------------------------------------------------------------
299 ;; Macros
300 ;;----------------------------------------------------------------
302 ;;; Get the buffer that mini-buffer was activated from
303 (defsubst dabbrev--minibuffer-origin ()
304 (car (cdr (buffer-list))))
306 ;; Make a list of some of the elements of LIST.
307 ;; Check each element of LIST, storing it temporarily in the
308 ;; variable ELEMENT, and include it in the result
309 ;; if CONDITION evaluates non-nil.
310 (defmacro dabbrev-filter-elements (element list condition)
311 (` (let (dabbrev-result dabbrev-tail (, element))
312 (setq dabbrev-tail (, list))
313 (while dabbrev-tail
314 (setq (, element) (car dabbrev-tail))
315 (if (, condition)
316 (setq dabbrev-result (cons (, element) dabbrev-result)))
317 (setq dabbrev-tail (cdr dabbrev-tail)))
318 (nreverse dabbrev-result))))
320 ;;----------------------------------------------------------------
321 ;; Exported functions
322 ;;----------------------------------------------------------------
324 ;;;###autoload
325 (define-key esc-map "/" 'dabbrev-expand)
326 ;;;??? Do we want this?
327 ;;;###autoload
328 (define-key esc-map [?\C-/] 'dabbrev-completion)
330 ;;;###autoload
331 (defun dabbrev-completion (&optional arg)
332 "Completion on current word.
333 Like \\[dabbrev-expand] but finds all expansions in the current buffer
334 and presents suggestions for completion.
336 With a prefix argument, it searches all buffers accepted by the
337 function pointed out by `dabbrev-friend-buffer-function' to find the
338 completions.
340 If the prefix argument is 16 (which comes from C-u C-u),
341 then it searches *all* buffers.
343 With no prefix argument, it reuses an old completion list
344 if there is a suitable one already."
346 (interactive "*P")
347 (dabbrev--reset-global-variables)
348 (let* ((dabbrev-check-other-buffers (and arg t))
349 (dabbrev-check-all-buffers
350 (and arg (= (prefix-numeric-value arg) 16)))
351 (abbrev (dabbrev--abbrev-at-point))
352 (ignore-case-p (and (eval dabbrev-case-fold-search)
353 (or (not dabbrev-upcase-means-case-search)
354 (string= abbrev (downcase abbrev)))))
355 (my-obarray dabbrev--last-obarray)
356 init)
357 (save-excursion
358 (if (and (null arg)
359 my-obarray
360 (or (eq dabbrev--last-completion-buffer (current-buffer))
361 (and (window-minibuffer-p (selected-window))
362 (eq dabbrev--last-completion-buffer
363 (dabbrev--minibuffer-origin))))
364 dabbrev--last-abbreviation
365 (>= (length abbrev) (length dabbrev--last-abbreviation))
366 (string= dabbrev--last-abbreviation
367 (substring abbrev 0
368 (length dabbrev--last-abbreviation)))
369 (setq init (try-completion abbrev my-obarray)))
370 ;; We can reuse the existing completion list.
372 ;;--------------------------------
373 ;; New abbreviation to expand.
374 ;;--------------------------------
375 (setq dabbrev--last-abbreviation abbrev)
376 ;; Find all expansion
377 (let ((completion-list
378 (dabbrev--find-all-expansions abbrev ignore-case-p))
379 (completion-ignore-case ignore-case-p))
380 ;; Make an obarray with all expansions
381 (setq my-obarray (make-vector (length completion-list) 0))
382 (or (> (length my-obarray) 0)
383 (error "No dynamic expansion for \"%s\" found%s"
384 abbrev
385 (if dabbrev--check-other-buffers "" " in this-buffer")))
386 (cond
387 ((or (not ignore-case-p)
388 (not dabbrev-case-replace))
389 (mapcar (function (lambda (string)
390 (intern string my-obarray)))
391 completion-list))
392 ((string= abbrev (upcase abbrev))
393 (mapcar (function (lambda (string)
394 (intern (upcase string) my-obarray)))
395 completion-list))
396 ((string= (substring abbrev 0 1)
397 (upcase (substring abbrev 0 1)))
398 (mapcar (function (lambda (string)
399 (intern (capitalize string) my-obarray)))
400 completion-list))
402 (mapcar (function (lambda (string)
403 (intern (downcase string) my-obarray)))
404 completion-list)))
405 (setq dabbrev--last-obarray my-obarray)
406 (setq dabbrev--last-completion-buffer (current-buffer))
407 ;; Find the longest common string.
408 (setq init (try-completion abbrev my-obarray)))))
409 ;;--------------------------------
410 ;; Let the user choose between the expansions
411 ;;--------------------------------
412 (or (stringp init)
413 (setq init abbrev))
414 (cond
415 ;; * Replace string fragment with matched common substring completion.
416 ((and (not (string-equal init ""))
417 (not (string-equal (downcase init) (downcase abbrev))))
418 (if (> (length (all-completions init my-obarray)) 1)
419 (message "Repeat `%s' to see all completions"
420 (key-description (this-command-keys)))
421 (message "The only possible completion"))
422 (dabbrev--substitute-expansion nil abbrev init))
424 ;; * String is a common substring completion already. Make list.
425 (message "Making completion list...")
426 (with-output-to-temp-buffer " *Completions*"
427 (display-completion-list (all-completions init my-obarray)))
428 (message "Making completion list...done")))
429 (and (window-minibuffer-p (selected-window))
430 (message nil))))
432 ;;;###autoload
433 (defun dabbrev-expand (arg)
434 "Expand previous word \"dynamically\".
436 Expands to the most recent, preceding word for which this is a prefix.
437 If no suitable preceding word is found, words following point are
438 considered. If still no suitable word is found, then look in the
439 buffers accepted by the function pointed out by variable
440 `dabbrev-friend-buffer-function'.
442 A positive prefix argument, N, says to take the Nth backward *distinct*
443 possibility. A negative argument says search forward.
445 If the cursor has not moved from the end of the previous expansion and
446 no argument is given, replace the previously-made expansion
447 with the next possible expansion not yet tried.
449 The variable `dabbrev-backward-only' may be used to limit the
450 direction of search to backward if set non-nil.
452 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
453 (interactive "*P")
454 (let (abbrev record-case-pattern
455 expansion old direction (orig-point (point)))
456 ;; abbrev -- the abbrev to expand
457 ;; expansion -- the expansion found (eventually) or nil until then
458 ;; old -- the text currently in the buffer
459 ;; (the abbrev, or the previously-made expansion)
460 (save-excursion
461 (if (and (null arg)
462 (markerp dabbrev--last-abbrev-location)
463 (marker-position dabbrev--last-abbrev-location)
464 (or (eq last-command this-command)
465 (and (window-minibuffer-p (selected-window))
466 (= dabbrev--last-abbrev-location
467 (point)))))
468 ;; Find a different expansion for the same abbrev as last time.
469 (progn
470 (setq abbrev dabbrev--last-abbreviation)
471 (setq old dabbrev--last-expansion)
472 (setq direction dabbrev--last-direction))
473 ;; If the user inserts a space after expanding
474 ;; and then asks to expand again, always fetch the next word.
475 (if (and (eq (preceding-char) ?\ )
476 (markerp dabbrev--last-abbrev-location)
477 (marker-position dabbrev--last-abbrev-location)
478 (= (point) (1+ dabbrev--last-abbrev-location)))
479 (progn
480 ;; The "abbrev" to expand is just the space.
481 (setq abbrev " ")
482 (save-excursion
483 (if dabbrev--last-buffer
484 (set-buffer dabbrev--last-buffer))
485 ;; Find the end of the last "expansion" word.
486 (if (or (eq dabbrev--last-direction 1)
487 (and (eq dabbrev--last-direction 0)
488 (< dabbrev--last-expansion-location (point))))
489 (setq dabbrev--last-expansion-location
490 (+ dabbrev--last-expansion-location
491 (length dabbrev--last-expansion))))
492 (goto-char dabbrev--last-expansion-location)
493 ;; Take the following word, with intermediate separators,
494 ;; as our expansion this time.
495 (re-search-forward
496 (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
497 (setq expansion
498 (buffer-substring dabbrev--last-expansion-location
499 (point)))
500 (if dabbrev--last-case-pattern
501 (setq expansion (upcase expansion)))
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 (eval dabbrev-case-fold-search)
525 (or (not dabbrev-upcase-means-case-search)
526 (string= abbrev (downcase abbrev))))))))
527 (cond
528 ((not expansion)
529 (dabbrev--reset-global-variables)
530 (if old
531 (save-excursion
532 (setq buffer-undo-list (cons orig-point buffer-undo-list))
533 ;; Put back the original abbrev with its original case pattern.
534 (search-backward old)
535 (insert abbrev)
536 (delete-region (point) (+ (point) (length old)))))
537 (error "No%s dynamic expansion for `%s' found"
538 (if old " further" "") abbrev))
540 (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found))
541 (progn
542 (message "Expansion found in '%s'"
543 (buffer-name dabbrev--last-buffer))
544 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
545 (message nil))
546 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
547 (null dabbrev--last-buffer))
548 (numberp dabbrev--last-expansion-location)
549 (and (> dabbrev--last-expansion-location (point))))
550 (setq dabbrev--last-expansion-location
551 (copy-marker dabbrev--last-expansion-location)))
552 ;; Success: stick it in and return.
553 (setq buffer-undo-list (cons orig-point buffer-undo-list))
554 (dabbrev--substitute-expansion old abbrev expansion)
556 ;; If we are not copying successive words now,
557 ;; set dabbrev--last-case-pattern.
558 (and record-case-pattern
559 (setq dabbrev--last-case-pattern
560 (and (eval dabbrev-case-fold-search)
561 (not dabbrev-upcase-means-case-search)
562 (equal abbrev (upcase abbrev)))))
564 ;; Save state for re-expand.
565 (setq dabbrev--last-expansion expansion)
566 (setq dabbrev--last-abbreviation abbrev)
567 (setq dabbrev--last-abbrev-location (point-marker))))))
569 ;;----------------------------------------------------------------
570 ;; Local functions
571 ;;----------------------------------------------------------------
573 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
574 (defun dabbrev--same-major-mode-p (other-buffer)
575 (eq major-mode
576 (save-excursion
577 (set-buffer other-buffer)
578 major-mode)))
580 ;;; Back over all abbrev type characters and then moves forward over
581 ;;; all skip characters.
582 (defun dabbrev--goto-start-of-abbrev ()
583 ;; Move backwards over abbrev chars
584 (save-match-data
585 (if (not (bobp))
586 (progn
587 (forward-char -1)
588 (while (and (looking-at dabbrev--abbrev-char-regexp)
589 (not (bobp)))
590 (forward-char -1))
591 (or (looking-at dabbrev--abbrev-char-regexp)
592 (forward-char 1))))
593 (and dabbrev-abbrev-skip-leading-regexp
594 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
595 (forward-char 1)))))
597 ;;; Extract the symbol at point to serve as abbreviation.
598 (defun dabbrev--abbrev-at-point ()
599 ;; Check for error
600 (if (bobp)
601 (error "No possible abbreviation preceding point"))
602 ;; Return abbrev at point
603 (save-excursion
604 ;; Record the end of the abbreviation.
605 (setq dabbrev--last-abbrev-location (point))
606 ;; If we aren't right after an abbreviation,
607 ;; move point back to just after one.
608 ;; This is so the user can get successive words
609 ;; by typing the punctuation followed by M-/.
610 (save-match-data
611 (if (save-excursion
612 (forward-char -1)
613 (not (looking-at (concat "\\("
614 (or dabbrev-abbrev-char-regexp
615 "\\sw\\|\\s_")
616 "\\)+"))))
617 (if (re-search-backward (or dabbrev-abbrev-char-regexp
618 "\\sw\\|\\s_")
619 nil t)
620 (forward-char 1)
621 (error "No possible abbreviation preceding point"))))
622 ;; Now find the beginning of that one.
623 (dabbrev--goto-start-of-abbrev)
624 (buffer-substring dabbrev--last-abbrev-location
625 (point))))
627 ;;; Initializes all global variables
628 (defun dabbrev--reset-global-variables ()
629 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
630 ;; must not be reset here.
631 (setq dabbrev--last-table nil
632 dabbrev--last-abbreviation nil
633 dabbrev--last-abbrev-location nil
634 dabbrev--last-direction nil
635 dabbrev--last-expansion nil
636 dabbrev--last-expansion-location nil
637 dabbrev--friend-buffer-list nil
638 dabbrev--last-buffer nil
639 dabbrev--last-buffer-found nil
640 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
641 "\\sw\\|\\s_")
642 dabbrev--check-other-buffers dabbrev-check-other-buffers))
644 ;;; Find all buffers that are considered "friends" according to the
645 ;;; function pointed out by dabbrev-friend-buffer-function.
646 (defun dabbrev--select-buffers ()
647 (save-excursion
648 (and (window-minibuffer-p (selected-window))
649 (set-buffer (dabbrev--minibuffer-origin)))
650 (let ((orig-buffer (current-buffer)))
651 (dabbrev-filter-elements
652 buffer (buffer-list)
653 (and (not (eq orig-buffer buffer))
654 (boundp 'dabbrev-friend-buffer-function)
655 (funcall dabbrev-friend-buffer-function buffer))))))
657 ;;; Search for ABBREV, N times, normally looking forward,
658 ;;; but looking in reverse instead if REVERSE is non-nil.
659 (defun dabbrev--try-find (abbrev reverse n ignore-case)
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 ignore-case)))
672 (setq count (1- count))))
673 (and expansion
674 (setq dabbrev--last-expansion-location (point)))
675 expansion))))
677 ;;; Find all expansions of ABBREV
678 (defun dabbrev--find-all-expansions (abbrev ignore-case)
679 (let ((all-expansions nil)
680 expansion)
681 (save-excursion
682 (goto-char (point-min))
683 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
684 (setq all-expansions (cons expansion all-expansions))))
685 all-expansions))
687 (defun dabbrev--scanning-message ()
688 (message "Scanning `%s'" (buffer-name (current-buffer))))
690 ;;; Find one occasion of ABBREV.
691 ;;; DIRECTION > 0 means look that many times backwards.
692 ;;; DIRECTION < 0 means look that many times forward.
693 ;;; DIRECTION = 0 means try both backward and forward.
694 ;;; IGNORE-CASE non-nil means ignore case when searching.
695 (defun dabbrev--find-expansion (abbrev direction ignore-case)
696 (let (expansion)
697 (save-excursion
698 (cond
699 (dabbrev--last-buffer
700 (set-buffer dabbrev--last-buffer)
701 (dabbrev--scanning-message))
702 ((and (not dabbrev-search-these-buffers-only)
703 (window-minibuffer-p (selected-window)))
704 (set-buffer (dabbrev--minibuffer-origin))
705 ;; In the minibuffer-origin buffer we will only search from
706 ;; the top and down.
707 (goto-char (point-min))
708 (setq direction -1)
709 (dabbrev--scanning-message)))
710 (cond
711 ;; ------------------------------------------
712 ;; Look backwards
713 ;; ------------------------------------------
714 ((and (not dabbrev-search-these-buffers-only)
715 (>= direction 0)
716 (setq dabbrev--last-direction (min 1 direction))
717 (setq expansion (dabbrev--try-find abbrev t
718 (max 1 direction)
719 ignore-case)))
720 expansion)
721 ;; ------------------------------------------
722 ;; Look forward
723 ;; ------------------------------------------
724 ((and (or (not dabbrev-search-these-buffers-only)
725 dabbrev--last-buffer)
726 (<= direction 0)
727 (setq dabbrev--last-direction -1)
728 (setq expansion (dabbrev--try-find abbrev nil
729 (max 1 (- direction))
730 ignore-case)))
731 expansion)
732 ;; ------------------------------------------
733 ;; Look in other buffers.
734 ;; Start at (point-min) and look forward.
735 ;; ------------------------------------------
737 (setq dabbrev--last-direction -1)
738 ;; Make sure that we should check other buffers
739 (or dabbrev--friend-buffer-list
740 dabbrev--last-buffer
741 (setq dabbrev--friend-buffer-list
742 (mapcar (function get-buffer)
743 dabbrev-search-these-buffers-only))
744 (not dabbrev--check-other-buffers)
745 (not (or (eq dabbrev--check-other-buffers t)
746 (progn
747 (setq dabbrev--check-other-buffers
748 (y-or-n-p "Scan other buffers also? ")))))
749 (let* (friend-buffer-list non-friend-buffer-list)
750 (setq dabbrev--friend-buffer-list
751 (funcall dabbrev-select-buffers-function))
752 (if dabbrev-check-all-buffers
753 (setq non-friend-buffer-list
754 (nreverse
755 (dabbrev-filter-elements
756 buffer (buffer-list)
757 (not (memq buffer dabbrev--friend-buffer-list))))
758 dabbrev--friend-buffer-list
759 (append dabbrev--friend-buffer-list
760 non-friend-buffer-list)))))
761 ;; Move buffers that are visible on the screen
762 ;; to the front of the list.
763 (if dabbrev--friend-buffer-list
764 (let ((w (next-window (selected-window))))
765 (while (not (eq w (selected-window)))
766 (setq dabbrev--friend-buffer-list
767 (cons (window-buffer w)
768 (delq (window-buffer w) dabbrev--friend-buffer-list)))
769 (setq w (next-window w)))))
770 ;; Walk through the buffers
771 (while (and (not expansion) dabbrev--friend-buffer-list)
772 (setq dabbrev--last-buffer
773 (car dabbrev--friend-buffer-list))
774 (setq dabbrev--friend-buffer-list
775 (cdr dabbrev--friend-buffer-list))
776 (set-buffer dabbrev--last-buffer)
777 (dabbrev--scanning-message)
778 (setq dabbrev--last-expansion-location (point-min))
779 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
780 expansion)))))
782 (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
783 (if (eq major-mode 'picture-mode)
784 (picture-replace-match string fixedcase literal)
785 (replace-match string fixedcase literal)))
787 ;;;----------------------------------------------------------------
788 ;;; Substitute the current string in buffer with the expansion
789 ;;; OLD is nil or the last expansion substring.
790 ;;; ABBREV is the abbreviation we are working with.
791 ;;; EXPANSION is the expansion substring.
792 (defun dabbrev--substitute-expansion (old abbrev expansion)
793 ;;(undo-boundary)
794 (let ((use-case-replace (and (eval dabbrev-case-fold-search)
795 (or (not dabbrev-upcase-means-case-search)
796 (string= abbrev (downcase abbrev)))
797 (eval dabbrev-case-replace))))
798 (and nil use-case-replace
799 (setq old (concat abbrev (or old "")))
800 (setq expansion (concat abbrev expansion)))
801 ;; If the expansion has mixed case
802 ;; and it is not simply a capitalized word,
803 ;; or if the abbrev has mixed case,
804 ;; and if the given abbrev's case pattern
805 ;; matches the start of the expansion,
806 ;; copy the expansion's case
807 ;; instead of downcasing all the rest.
808 (let ((expansion-rest (substring expansion 1)))
809 (if (and (not (and (or (string= expansion-rest (downcase expansion-rest))
810 (string= expansion-rest (upcase expansion-rest)))
811 (or (string= abbrev (downcase abbrev))
812 (string= abbrev (upcase abbrev)))))
813 (string= abbrev
814 (substring expansion 0 (length abbrev))))
815 (setq use-case-replace nil)))
816 (if (equal abbrev " ")
817 (setq use-case-replace nil))
818 (if use-case-replace
819 (setq expansion (downcase expansion)))
820 (if old
821 (save-excursion
822 (search-backward old))
823 ;;(store-match-data (list (point-marker) (point-marker)))
824 (search-backward abbrev))
825 ;; Make case of replacement conform to case of abbreviation
826 ;; provided (1) that kind of thing is enabled in this buffer
827 ;; and (2) the replacement itself is all lower case.
828 (dabbrev--safe-replace-match expansion
829 (not use-case-replace)
830 t)))
833 ;;;----------------------------------------------------------------
834 ;;; Search function used by dabbrevs library.
836 ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE,
837 ;;; is t for reverse search, nil for forward. Variable dabbrev-limit
838 ;;; controls the maximum search region size. Third argument IGNORE-CASE
839 ;;; non-nil means treat case as insignificant while looking for a match
840 ;;; and when comparing with previous matches. Also if that's non-nil
841 ;;; and the match is found at the beginning of a sentence and is in
842 ;;; lower case except for the initial then it is converted to all lower
843 ;;; case for return.
845 ;;; Table of expansions already seen is examined in buffer
846 ;;; `dabbrev--last-table' so that only distinct possibilities are found
847 ;;; by dabbrev-re-expand.
849 ;;; Value is the expansion, or nil if not found.
851 (defun dabbrev--search (abbrev reverse ignore-case)
852 (save-match-data
853 (let ((pattern1 (concat (regexp-quote abbrev)
854 "\\(" dabbrev--abbrev-char-regexp "\\)"))
855 (pattern2 (concat (regexp-quote abbrev)
856 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
857 (found-string nil))
858 ;; Limited search.
859 (save-restriction
860 (and dabbrev-limit
861 (narrow-to-region dabbrev--last-expansion-location
862 (+ (point)
863 (if reverse (- dabbrev-limit) dabbrev-limit))))
864 ;;--------------------------------
865 ;; Look for a distinct expansion, using dabbrev--last-table.
866 ;;--------------------------------
867 (while (and (not found-string)
868 (if reverse
869 (re-search-backward pattern1 nil t)
870 (re-search-forward pattern1 nil t)))
871 (goto-char (match-beginning 0))
872 ;; In case we matched in the middle of a word,
873 ;; back up to start of word and verify we still match.
874 (dabbrev--goto-start-of-abbrev)
876 (if (not (looking-at pattern1))
878 ;; We have a truly valid match. Find the end.
879 (re-search-forward pattern2)
880 (setq found-string
881 (buffer-substring (match-beginning 1) (match-end 1)))
882 (and ignore-case (setq found-string (downcase found-string)))
883 ;; Ignore this match if it's already in the table.
884 (if (dabbrev-filter-elements
885 table-string dabbrev--last-table
886 (string= found-string table-string))
887 (setq found-string nil)))
888 ;; Prepare to continue searching.
889 (if reverse
890 (goto-char (match-beginning 0))
891 (goto-char (match-end 0))))
892 ;; If we found something, use it.
893 (if found-string
894 ;; Put it into `dabbrev--last-table'
895 ;; and return it (either downcased, or as is).
896 (let ((result
897 (buffer-substring (match-beginning 0) (match-end 0))))
898 (setq dabbrev--last-table
899 (cons found-string dabbrev--last-table))
900 (if (and ignore-case (eval dabbrev-case-replace))
901 result
902 result)))))))
904 (provide 'dabbrev)
906 ;;; dabbrev.el ends here