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