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