(url-cookie-multiple-line): Specify group and type in defcustom.
[bpt/emacs.git] / lisp / dabbrev.el
CommitLineData
7313ccdb 1;;; dabbrev.el --- dynamic abbreviation package
b578f267 2
2e27ed13 3;; Copyright (C) 1985, 86, 92, 94, 96, 1997, 2000, 01, 03, 2004
7e65cba2 4;; 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
b578f267
EN
16;; the Free Software Foundation; either version 2, or (at your option)
17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
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
EN
25;; along with GNU Emacs; see the file COPYING. If not, write to the
26;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, 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
RS
102(defgroup dabbrev nil
103 "Dynamic Abbreviations"
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
fd2dfb40
RS
131(defcustom dabbrev--eliminate-newlines t
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.
93a43334
RS
150nil means case fold search when searching for possible expansions;
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
285this list.")
e5167999 286
b578f267
EN
287;;----------------------------------------------------------------
288;; Internal variables
289;;----------------------------------------------------------------
2f790b20 290
6163b3ba
RS
291;; Last obarray of completions in `dabbrev-completion'
292(defvar dabbrev--last-obarray nil)
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
321;; The buffer we last did a completion in.
322(defvar dabbrev--last-completion-buffer nil)
323
3ffa545b
GM
324;; If non-nil, a function to use when copying successive words.
325;; It should be `upcase' or `downcase'.
dea5efcb
RS
326(defvar dabbrev--last-case-pattern nil)
327
df6eb420
RS
328;; Same as dabbrev-check-other-buffers, but is set for every expand.
329(defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
6163b3ba
RS
330
331;; The regexp for recognizing a character in an abbreviation.
332(defvar dabbrev--abbrev-char-regexp nil)
333
b578f267
EN
334;;----------------------------------------------------------------
335;; Macros
336;;----------------------------------------------------------------
6163b3ba
RS
337
338;;; Get the buffer that mini-buffer was activated from
339(defsubst dabbrev--minibuffer-origin ()
340 (car (cdr (buffer-list))))
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)
7313ccdb 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
7313ccdb
RS
370With a prefix argument, it searches all buffers accepted by the
371function pointed out by `dabbrev-friend-buffer-function' to find the
dd1ae355
RS
372completions.
373
374If the prefix argument is 16 (which comes from C-u C-u),
375then it searches *all* buffers.
6163b3ba 376
7313ccdb
RS
377With no prefix argument, it reuses an old completion list
378if there is a suitable one already."
6163b3ba
RS
379
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
394 (if (and (null arg)
395 my-obarray
396 (or (eq dabbrev--last-completion-buffer (current-buffer))
397 (and (window-minibuffer-p (selected-window))
398 (eq dabbrev--last-completion-buffer
399 (dabbrev--minibuffer-origin))))
400 dabbrev--last-abbreviation
401 (>= (length abbrev) (length dabbrev--last-abbreviation))
402 (string= dabbrev--last-abbreviation
403 (substring abbrev 0
404 (length dabbrev--last-abbreviation)))
405 (setq init (try-completion abbrev my-obarray)))
7313ccdb
RS
406 ;; We can reuse the existing completion list.
407 nil
6163b3ba
RS
408 ;;--------------------------------
409 ;; New abbreviation to expand.
410 ;;--------------------------------
6163b3ba
RS
411 (setq dabbrev--last-abbreviation abbrev)
412 ;; Find all expansion
413 (let ((completion-list
66db4b5b
RS
414 (dabbrev--find-all-expansions abbrev ignore-case-p))
415 (completion-ignore-case ignore-case-p))
6163b3ba
RS
416 ;; Make an obarray with all expansions
417 (setq my-obarray (make-vector (length completion-list) 0))
418 (or (> (length my-obarray) 0)
7313ccdb 419 (error "No dynamic expansion for \"%s\" found%s"
6163b3ba
RS
420 abbrev
421 (if dabbrev--check-other-buffers "" " in this-buffer")))
422 (cond
423 ((or (not ignore-case-p)
424 (not dabbrev-case-replace))
87207d14
DL
425 (mapc (function (lambda (string)
426 (intern string my-obarray)))
df6eb420 427 completion-list))
6163b3ba 428 ((string= abbrev (upcase abbrev))
87207d14
DL
429 (mapc (function (lambda (string)
430 (intern (upcase string) my-obarray)))
df6eb420 431 completion-list))
6163b3ba
RS
432 ((string= (substring abbrev 0 1)
433 (upcase (substring abbrev 0 1)))
87207d14
DL
434 (mapc (function (lambda (string)
435 (intern (capitalize string) my-obarray)))
df6eb420 436 completion-list))
6163b3ba 437 (t
87207d14
DL
438 (mapc (function (lambda (string)
439 (intern (downcase string) my-obarray)))
df6eb420 440 completion-list)))
6163b3ba
RS
441 (setq dabbrev--last-obarray my-obarray)
442 (setq dabbrev--last-completion-buffer (current-buffer))
443 ;; Find the longest common string.
444 (setq init (try-completion abbrev my-obarray)))))
445 ;;--------------------------------
446 ;; Let the user choose between the expansions
447 ;;--------------------------------
448 (or (stringp init)
449 (setq init abbrev))
450 (cond
451 ;; * Replace string fragment with matched common substring completion.
452 ((and (not (string-equal init ""))
453 (not (string-equal (downcase init) (downcase abbrev))))
454 (if (> (length (all-completions init my-obarray)) 1)
7313ccdb
RS
455 (message "Repeat `%s' to see all completions"
456 (key-description (this-command-keys)))
6163b3ba 457 (message "The only possible completion"))
3ffa545b 458 (dabbrev--substitute-expansion nil abbrev init nil))
6163b3ba
RS
459 (t
460 ;; * String is a common substring completion already. Make list.
461 (message "Making completion list...")
ecd91f5f 462 (with-output-to-temp-buffer "*Completions*"
6163b3ba 463 (display-completion-list (all-completions init my-obarray)))
7313ccdb 464 (message "Making completion list...done")))
6163b3ba
RS
465 (and (window-minibuffer-p (selected-window))
466 (message nil))))
2f790b20
JB
467
468;;;###autoload
469(defun dabbrev-expand (arg)
470 "Expand previous word \"dynamically\".
2f790b20 471
6163b3ba
RS
472Expands to the most recent, preceding word for which this is a prefix.
473If no suitable preceding word is found, words following point are
474considered. If still no suitable word is found, then look in the
475buffers accepted by the function pointed out by variable
476`dabbrev-friend-buffer-function'.
2f790b20 477
7313ccdb 478A positive prefix argument, N, says to take the Nth backward *distinct*
6163b3ba 479possibility. A negative argument says search forward.
2f790b20
JB
480
481If the cursor has not moved from the end of the previous expansion and
482no argument is given, replace the previously-made expansion
6163b3ba
RS
483with the next possible expansion not yet tried.
484
485The variable `dabbrev-backward-only' may be used to limit the
486direction of search to backward if set non-nil.
487
7313ccdb 488See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
2f790b20 489 (interactive "*P")
dea5efcb
RS
490 (let (abbrev record-case-pattern
491 expansion old direction (orig-point (point)))
2f790b20
JB
492 ;; abbrev -- the abbrev to expand
493 ;; expansion -- the expansion found (eventually) or nil until then
494 ;; old -- the text currently in the buffer
495 ;; (the abbrev, or the previously-made expansion)
2f790b20
JB
496 (save-excursion
497 (if (and (null arg)
df6eb420
RS
498 (markerp dabbrev--last-abbrev-location)
499 (marker-position dabbrev--last-abbrev-location)
6163b3ba
RS
500 (or (eq last-command this-command)
501 (and (window-minibuffer-p (selected-window))
502 (= dabbrev--last-abbrev-location
503 (point)))))
7313ccdb 504 ;; Find a different expansion for the same abbrev as last time.
6163b3ba
RS
505 (progn
506 (setq abbrev dabbrev--last-abbreviation)
507 (setq old dabbrev--last-expansion)
508 (setq direction dabbrev--last-direction))
a8a2d6ca
KH
509 ;; If the user inserts a space after expanding
510 ;; and then asks to expand again, always fetch the next word.
511 (if (and (eq (preceding-char) ?\ )
512 (markerp dabbrev--last-abbrev-location)
513 (marker-position dabbrev--last-abbrev-location)
514 (= (point) (1+ dabbrev--last-abbrev-location)))
dea5efcb 515 (progn
a8a2d6ca
KH
516 ;; The "abbrev" to expand is just the space.
517 (setq abbrev " ")
518 (save-excursion
b9559a72
RS
519 (save-restriction
520 (widen)
521 (if dabbrev--last-buffer
522 (set-buffer dabbrev--last-buffer))
523 ;; Find the end of the last "expansion" word.
524 (if (or (eq dabbrev--last-direction 1)
525 (and (eq dabbrev--last-direction 0)
526 (< dabbrev--last-expansion-location (point))))
527 (setq dabbrev--last-expansion-location
528 (+ dabbrev--last-expansion-location
529 (length dabbrev--last-expansion))))
530 (goto-char dabbrev--last-expansion-location)
531 ;; Take the following word, with intermediate separators,
532 ;; as our expansion this time.
533 (re-search-forward
534 (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
535 (setq expansion (buffer-substring-no-properties
536 dabbrev--last-expansion-location (point)))
537
538 ;; Record the end of this expansion, in case we repeat this.
539 (setq dabbrev--last-expansion-location (point))))
a8a2d6ca
KH
540 ;; Indicate that dabbrev--last-expansion-location is
541 ;; at the end of the expansion.
542 (setq dabbrev--last-direction -1))
543
544 ;; We have a different abbrev to expand.
545 (dabbrev--reset-global-variables)
546 (setq direction (if (null arg)
547 (if dabbrev-backward-only 1 0)
548 (prefix-numeric-value arg)))
549 (setq abbrev (dabbrev--abbrev-at-point))
dea5efcb 550 (setq record-case-pattern t)
a8a2d6ca 551 (setq old nil)))
6163b3ba
RS
552
553 ;;--------------------------------
554 ;; Find the expansion
555 ;;--------------------------------
a8a2d6ca
KH
556 (or expansion
557 (setq expansion
558 (dabbrev--find-expansion abbrev direction
543abb4a
RS
559 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
560 case-fold-search
561 dabbrev-case-fold-search)
a8a2d6ca
KH
562 (or (not dabbrev-upcase-means-case-search)
563 (string= abbrev (downcase abbrev))))))))
6163b3ba
RS
564 (cond
565 ((not expansion)
566 (dabbrev--reset-global-variables)
567 (if old
568 (save-excursion
4209f479 569 (setq buffer-undo-list (cons orig-point buffer-undo-list))
3132e115
RS
570 ;; Put back the original abbrev with its original case pattern.
571 (search-backward old)
572 (insert abbrev)
573 (delete-region (point) (+ (point) (length old)))))
7313ccdb 574 (error "No%s dynamic expansion for `%s' found"
6163b3ba
RS
575 (if old " further" "") abbrev))
576 (t
fdad0f14
GM
577 (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
578 (minibuffer-window-active-p (selected-window))))
2f790b20 579 (progn
6163b3ba
RS
580 (message "Expansion found in '%s'"
581 (buffer-name dabbrev--last-buffer))
582 (setq dabbrev--last-buffer-found dabbrev--last-buffer))
583 (message nil))
a8a2d6ca
KH
584 (if (and (or (eq (current-buffer) dabbrev--last-buffer)
585 (null dabbrev--last-buffer))
586 (numberp dabbrev--last-expansion-location)
587 (and (> dabbrev--last-expansion-location (point))))
588 (setq dabbrev--last-expansion-location
589 (copy-marker dabbrev--last-expansion-location)))
2f790b20 590 ;; Success: stick it in and return.
4209f479 591 (setq buffer-undo-list (cons orig-point buffer-undo-list))
3ffa545b
GM
592 (dabbrev--substitute-expansion old abbrev expansion
593 record-case-pattern)
dea5efcb 594
2f790b20 595 ;; Save state for re-expand.
da49057c 596 (setq dabbrev--last-expansion expansion)
6163b3ba
RS
597 (setq dabbrev--last-abbreviation abbrev)
598 (setq dabbrev--last-abbrev-location (point-marker))))))
599
b578f267
EN
600;;----------------------------------------------------------------
601;; Local functions
602;;----------------------------------------------------------------
6163b3ba 603
6163b3ba
RS
604;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
605(defun dabbrev--same-major-mode-p (other-buffer)
7313ccdb
RS
606 (eq major-mode
607 (save-excursion
608 (set-buffer other-buffer)
609 major-mode)))
6163b3ba
RS
610
611;;; Back over all abbrev type characters and then moves forward over
612;;; all skip characters.
613(defun dabbrev--goto-start-of-abbrev ()
614 ;; Move backwards over abbrev chars
615 (save-match-data
b0021416
RS
616 (when (> (point) (minibuffer-prompt-end))
617 (forward-char -1)
618 (while (and (looking-at dabbrev--abbrev-char-regexp)
619 (> (point) (minibuffer-prompt-end))
620 (not (= (point) (field-beginning (point) nil
621 (1- (point))))))
622 (forward-char -1))
623 (or (looking-at dabbrev--abbrev-char-regexp)
624 (forward-char 1)))
6163b3ba
RS
625 (and dabbrev-abbrev-skip-leading-regexp
626 (while (looking-at dabbrev-abbrev-skip-leading-regexp)
627 (forward-char 1)))))
628
7313ccdb 629;;; Extract the symbol at point to serve as abbreviation.
6163b3ba
RS
630(defun dabbrev--abbrev-at-point ()
631 ;; Check for error
a8a2d6ca
KH
632 (if (bobp)
633 (error "No possible abbreviation preceding point"))
6163b3ba
RS
634 ;; Return abbrev at point
635 (save-excursion
a8a2d6ca 636 ;; Record the end of the abbreviation.
6163b3ba 637 (setq dabbrev--last-abbrev-location (point))
a8a2d6ca
KH
638 ;; If we aren't right after an abbreviation,
639 ;; move point back to just after one.
640 ;; This is so the user can get successive words
641 ;; by typing the punctuation followed by M-/.
642 (save-match-data
643 (if (save-excursion
644 (forward-char -1)
645 (not (looking-at (concat "\\("
646 (or dabbrev-abbrev-char-regexp
647 "\\sw\\|\\s_")
648 "\\)+"))))
649 (if (re-search-backward (or dabbrev-abbrev-char-regexp
650 "\\sw\\|\\s_")
651 nil t)
652 (forward-char 1)
653 (error "No possible abbreviation preceding point"))))
654 ;; Now find the beginning of that one.
655 (dabbrev--goto-start-of-abbrev)
6f0b000c
RS
656 (buffer-substring-no-properties
657 dabbrev--last-abbrev-location (point))))
da49057c 658
6163b3ba
RS
659;;; Initializes all global variables
660(defun dabbrev--reset-global-variables ()
661 ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
662 ;; must not be reset here.
663 (setq dabbrev--last-table nil
664 dabbrev--last-abbreviation nil
665 dabbrev--last-abbrev-location nil
666 dabbrev--last-direction nil
667 dabbrev--last-expansion nil
668 dabbrev--last-expansion-location nil
669 dabbrev--friend-buffer-list nil
670 dabbrev--last-buffer nil
671 dabbrev--last-buffer-found nil
672 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
673 "\\sw\\|\\s_")
df6eb420 674 dabbrev--check-other-buffers dabbrev-check-other-buffers))
6163b3ba 675
6163b3ba 676(defun dabbrev--select-buffers ()
513e7954
RS
677 "Return a list of other buffers to search for a possible abbrev.
678The current buffer is not included in the list.
679
680This function makes a list of all the buffers returned by `buffer-list',
681then discards buffers whose names match `dabbrev-ignored-buffer-names'
682or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
683`dabbrev-friend-buffer-function', if it is bound, returns nil when called
684with the buffer as argument.
685It returns the list of the buffers that are not discarded."
686 (dabbrev-filter-elements
687 buffer (buffer-list)
688 (and (not (eq (current-buffer) buffer))
689 (not (dabbrev--ignore-buffer-p buffer))
690 (boundp 'dabbrev-friend-buffer-function)
2ebf8f54 691 (funcall dabbrev-friend-buffer-function buffer))))
7313ccdb 692
6163b3ba 693(defun dabbrev--try-find (abbrev reverse n ignore-case)
fd2dfb40
RS
694 "Search for ABBREV, backwards if REVERSE, N times.
695If IGNORE-CASE is non-nil, ignore case while searching.
696Return the expansion found, and save the location of the start
697of the expansion in `dabbrev--last-expansion-location'."
6163b3ba 698 (save-excursion
df6eb420
RS
699 (save-restriction
700 (widen)
701 (let ((expansion nil))
702 (and dabbrev--last-expansion-location
703 (goto-char dabbrev--last-expansion-location))
704 (let ((case-fold-search ignore-case)
705 (count n))
706 (while (and (> count 0)
707 (setq expansion (dabbrev--search abbrev
708 reverse
93a43334
RS
709 (and ignore-case
710 (if (eq dabbrev-case-distinction 'case-replace)
711 case-replace
712 dabbrev-case-distinction))
713 )))
df6eb420
RS
714 (setq count (1- count))))
715 (and expansion
716 (setq dabbrev--last-expansion-location (point)))
717 expansion))))
6163b3ba 718
6163b3ba 719(defun dabbrev--find-all-expansions (abbrev ignore-case)
fd2dfb40
RS
720 "Return a list of all possible expansions of ABBREV.
721If IGNORE-CASE is non-nil, accept matches which differ in case."
6163b3ba
RS
722 (let ((all-expansions nil)
723 expansion)
724 (save-excursion
725 (goto-char (point-min))
726 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
df6eb420 727 (setq all-expansions (cons expansion all-expansions))))
6163b3ba
RS
728 all-expansions))
729
730(defun dabbrev--scanning-message ()
513e7954
RS
731 (unless (window-minibuffer-p (selected-window))
732 (message "Scanning `%s'" (buffer-name (current-buffer)))))
6163b3ba 733
a6a06429
MB
734(defun dabbrev--ignore-buffer-p (buffer)
735 "Return non-nil if BUFFER should be ignored by dabbrev."
736 (let ((bn (buffer-name buffer)))
737 (or (member bn dabbrev-ignored-buffer-names)
738 (let ((tail dabbrev-ignored-buffer-regexps)
739 (match nil))
740 (while (and tail (not match))
741 (setq match (string-match (car tail) bn)
742 tail (cdr tail)))
743 match))))
744
6163b3ba 745(defun dabbrev--find-expansion (abbrev direction ignore-case)
fd2dfb40
RS
746 "Find one occurrence of ABBREV, and return the expansion.
747DIRECTION > 0 means look that many times backwards.
748DIRECTION < 0 means look that many times forward.
749DIRECTION = 0 means try both backward and forward.
750IGNORE-CASE non-nil means ignore case when searching.
751This sets `dabbrev--last-direction' to 1 or -1 according
752to the direction in which the occurrence was actually found.
71296446 753It sets `dabbrev--last-expansion-location' to the location
fd2dfb40 754of the start of the occurrence."
513e7954
RS
755 (save-excursion
756 ;; If we were scanning something other than the current buffer,
757 ;; continue scanning there.
758 (when dabbrev--last-buffer
759 (set-buffer dabbrev--last-buffer)
760 (dabbrev--scanning-message))
761 (or
762 ;; ------------------------------------------
763 ;; Look backward in current buffer.
764 ;; ------------------------------------------
765 (and (not dabbrev-search-these-buffers-only)
766 (>= direction 0)
767 (setq dabbrev--last-direction (min 1 direction))
768 (dabbrev--try-find abbrev t
769 (max 1 direction)
770 ignore-case))
771 ;; ------------------------------------------
772 ;; Look forward in current buffer
773 ;; or whatever buffer we were last scanning.
774 ;; ------------------------------------------
775 (and (or (not dabbrev-search-these-buffers-only)
776 dabbrev--last-buffer)
777 (<= direction 0)
778 (setq dabbrev--last-direction -1)
779 (dabbrev--try-find abbrev nil
780 (max 1 (- direction))
781 ignore-case))
782 ;; ------------------------------------------
783 ;; Look in other buffers.
784 ;; Always start at (point-min) and look forward.
785 ;; ------------------------------------------
786 (progn
787 (setq dabbrev--last-direction -1)
788 (unless dabbrev--last-buffer
789 ;; If we have just now begun to search other buffers,
790 ;; determine which other buffers we should check.
791 ;; Put that list in dabbrev--friend-buffer-list.
792 (or dabbrev--friend-buffer-list
793 (setq dabbrev--friend-buffer-list
794 (dabbrev--make-friend-buffer-list))))
795 ;; Walk through the buffers till we find a match.
796 (let (expansion)
797 (while (and (not expansion) dabbrev--friend-buffer-list)
2e27ed13 798 (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
513e7954
RS
799 (set-buffer dabbrev--last-buffer)
800 (dabbrev--scanning-message)
801 (setq dabbrev--last-expansion-location (point-min))
802 (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
803 expansion)))))
804
805;; Compute the list of buffers to scan.
806;; If dabbrev-search-these-buffers-only, then the current buffer
807;; is included in this list if it should be searched.
808;; Otherwise, the current buffer is searched first specially.,
809;; and it is not included in this list.
810(defun dabbrev--make-friend-buffer-list ()
811 (let ((list (mapcar (function get-buffer)
812 dabbrev-search-these-buffers-only)))
813 (when (and (null dabbrev-search-these-buffers-only)
814 dabbrev--check-other-buffers
815 (or (eq dabbrev--check-other-buffers t)
816 (setq dabbrev--check-other-buffers
817 (y-or-n-p "Scan other buffers also? "))))
818 (setq list (funcall dabbrev-select-buffers-function))
819 ;; If dabbrev-check-all-buffers, tack on all the other
820 ;; buffers at the end of the list, except those which are
821 ;; specifically to be ignored.
822 (if dabbrev-check-all-buffers
823 (setq list
824 (append list
3ffa545b
GM
825 (dabbrev-filter-elements
826 buffer (buffer-list)
513e7954
RS
827 (and (not (memq buffer list))
828 (not (dabbrev--ignore-buffer-p buffer)))))))
829 ;; Remove the current buffer.
830 (setq list (delq (current-buffer) list)))
831 ;; Move buffers in the list that are visible on the screen
832 ;; to the front of the list, but don't add anything to the list.
833 (if list
834 (walk-windows (lambda (w)
835 (unless (eq w (selected-window))
836 (if (memq (window-buffer w) list)
837 (setq list
838 (cons (window-buffer w)
839 (delq (window-buffer w)
840 list))))))))
841 ;; In a minibuffer, search the buffer it was activated from,
842 ;; first after the minibuffer itself. Unless we aren't supposed
843 ;; to search the current buffer either.
844 (if (and (window-minibuffer-p (selected-window))
845 (not dabbrev-search-these-buffers-only))
846 (setq list
847 (cons (dabbrev--minibuffer-origin)
848 (delq (dabbrev--minibuffer-origin) list))))
849 list))
6163b3ba 850
6163b3ba
RS
851(defun dabbrev--safe-replace-match (string &optional fixedcase literal)
852 (if (eq major-mode 'picture-mode)
6ee55cb1
RS
853 (with-no-warnings
854 (picture-replace-match string fixedcase literal))
6163b3ba
RS
855 (replace-match string fixedcase literal)))
856
857;;;----------------------------------------------------------------
3ffa545b
GM
858(defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
859 "Replace OLD with EXPANSION in the buffer.
860OLD is text currently in the buffer, perhaps the abbreviation
861or perhaps another expansion that was tried previously.
862ABBREV is the abbreviation we are expanding.
863It is \" \" if we are copying subsequent words.
864EXPANSION is the expansion substring to be used this time.
865RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
866to record whether we upcased the expansion, downcased it, or did neither."
6163b3ba 867 ;;(undo-boundary)
543abb4a
RS
868 (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search)
869 case-fold-search
870 dabbrev-case-fold-search)
6163b3ba
RS
871 (or (not dabbrev-upcase-means-case-search)
872 (string= abbrev (downcase abbrev)))
543abb4a
RS
873 (if (eq dabbrev-case-replace 'case-replace)
874 case-replace
875 dabbrev-case-replace))))
3ffa545b
GM
876
877 ;; If we upcased or downcased the original expansion,
878 ;; do likewise for the subsequent words when we copy them.
fd2dfb40
RS
879 ;; Don't do any of the usual case processing, though.
880 (when (equal abbrev " ")
881 (if dabbrev--last-case-pattern
882 (setq expansion
883 (funcall dabbrev--last-case-pattern expansion)))
884 (setq use-case-replace nil))
3ffa545b 885
65411629
RS
886 ;; If the expansion has mixed case
887 ;; and it is not simply a capitalized word,
888 ;; or if the abbrev has mixed case,
889 ;; and if the given abbrev's case pattern
66db4b5b
RS
890 ;; matches the start of the expansion,
891 ;; copy the expansion's case
892 ;; instead of downcasing all the rest.
e921af9e
RS
893 ;;
894 ;; Treat a one-capital-letter (possibly with preceding non-letter
895 ;; characters) abbrev as "not all upper case", so as to force
896 ;; preservation of the expansion's pattern if the expansion starts
897 ;; with a capital letter.
898 (let ((expansion-rest (substring expansion 1))
899 (first-letter-position (string-match "[[:alpha:]]" abbrev)))
900 (if (or (null first-letter-position)
901 (and (not (and (or (string= expansion-rest (downcase expansion-rest))
902 (string= expansion-rest (upcase expansion-rest)))
903 (or (string= abbrev (downcase abbrev))
904 (and (string= abbrev (upcase abbrev))
905 (> (- (length abbrev) first-letter-position)
906 1)))))
907 (string= abbrev
908 (substring expansion 0 (length abbrev)))))
65411629 909 (setq use-case-replace nil)))
fd2dfb40
RS
910
911 ;; If the abbrev and the expansion are both all-lower-case
912 ;; then don't do any conversion. The conversion would be a no-op
913 ;; for this replacement, but it would carry forward to subsequent words.
e921af9e 914 ;; The goal of this is to prevent that carrying forward.
fd2dfb40
RS
915 (if (and (string= expansion (downcase expansion))
916 (string= abbrev (downcase abbrev)))
07e47d0b 917 (setq use-case-replace nil))
fd2dfb40 918
07e47d0b
RS
919 (if use-case-replace
920 (setq expansion (downcase expansion)))
3ffa545b
GM
921
922 ;; In case we insert subsequent words,
923 ;; record if we upcased or downcased the first word,
924 ;; in order to do likewise for subsequent words.
925 (and record-case-pattern
71296446 926 (setq dabbrev--last-case-pattern
3ffa545b
GM
927 (and use-case-replace
928 (cond ((equal abbrev (upcase abbrev)) 'upcase)
929 ((equal abbrev (downcase abbrev)) 'downcase)))))
930
19c17c4e 931 ;; Convert whitespace to single spaces.
fd2dfb40 932 (if dabbrev--eliminate-newlines
19c17c4e
RS
933 ;; Start searching at end of ABBREV so that any whitespace
934 ;; carried over from the existing text is not changed.
935 (let ((pos (length abbrev)))
936 (while (string-match "[\n \t]+" expansion pos)
937 (setq pos (1+ (match-beginning 0)))
938 (setq expansion (replace-match " " nil nil expansion)))))
fd2dfb40 939
6163b3ba
RS
940 (if old
941 (save-excursion
942 (search-backward old))
9b7c13e0 943 ;;(set-match-data (list (point-marker) (point-marker)))
fd2dfb40
RS
944 (search-backward abbrev)
945 (search-forward abbrev))
946
6163b3ba
RS
947 ;; Make case of replacement conform to case of abbreviation
948 ;; provided (1) that kind of thing is enabled in this buffer
949 ;; and (2) the replacement itself is all lower case.
950 (dabbrev--safe-replace-match expansion
951 (not use-case-replace)
952 t)))
953
954
955;;;----------------------------------------------------------------
956;;; Search function used by dabbrevs library.
957
6163b3ba
RS
958
959(defun dabbrev--search (abbrev reverse ignore-case)
fd2dfb40
RS
960 "Search for something that could be used to expand ABBREV.
961
962Second arg, REVERSE, is t for reverse search, nil for forward.
963The variable `dabbrev-limit' controls the maximum search region size.
964Third argument IGNORE-CASE non-nil means treat case as insignificant while
965looking for a match and when comparing with previous matches. Also if
966that's non-nil and the match is found at the beginning of a sentence
967and is in lower case except for the initial then it is converted to
968all lower case for return.
969
970Table of expansions already seen is examined in buffer
971`dabbrev--last-table' so that only distinct possibilities are found
972by dabbrev-re-expand.
973
974Returns the expansion found, or nil if not found.
975Leaves point at the location of the start of the expansion."
6163b3ba
RS
976 (save-match-data
977 (let ((pattern1 (concat (regexp-quote abbrev)
978 "\\(" dabbrev--abbrev-char-regexp "\\)"))
979 (pattern2 (concat (regexp-quote abbrev)
980 "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
155133a2
RS
981 ;; This makes it possible to find matches in minibuffer prompts
982 ;; even when they are "inviolable".
983 (inhibit-point-motion-hooks t)
93a43334 984 found-string result)
6163b3ba
RS
985 ;; Limited search.
986 (save-restriction
987 (and dabbrev-limit
988 (narrow-to-region dabbrev--last-expansion-location
989 (+ (point)
990 (if reverse (- dabbrev-limit) dabbrev-limit))))
991 ;;--------------------------------
992 ;; Look for a distinct expansion, using dabbrev--last-table.
993 ;;--------------------------------
994 (while (and (not found-string)
995 (if reverse
996 (re-search-backward pattern1 nil t)
997 (re-search-forward pattern1 nil t)))
63d991d6
KH
998 (goto-char (match-beginning 0))
999 ;; In case we matched in the middle of a word,
1000 ;; back up to start of word and verify we still match.
1001 (dabbrev--goto-start-of-abbrev)
1002
1003 (if (not (looking-at pattern1))
1004 nil
1005 ;; We have a truly valid match. Find the end.
6163b3ba 1006 (re-search-forward pattern2)
2e27ed13 1007 (setq found-string (match-string-no-properties 0))
93a43334 1008 (setq result found-string)
6163b3ba 1009 (and ignore-case (setq found-string (downcase found-string)))
63d991d6 1010 ;; Ignore this match if it's already in the table.
7313ccdb
RS
1011 (if (dabbrev-filter-elements
1012 table-string dabbrev--last-table
1013 (string= found-string table-string))
63d991d6
KH
1014 (setq found-string nil)))
1015 ;; Prepare to continue searching.
2e27ed13 1016 (goto-char (if reverse (match-beginning 0) (match-end 0))))
63d991d6 1017 ;; If we found something, use it.
93a43334
RS
1018 (when found-string
1019 ;; Put it into `dabbrev--last-table'
1020 ;; and return it (either downcased, or as is).
1021 (setq dabbrev--last-table
1022 (cons found-string dabbrev--last-table))
1023 result)))))
6163b3ba 1024
87207d14
DL
1025(dolist (mess '("^No dynamic expansion for .* found$"
1026 "^No further dynamic expansion for .* found$"
1027 "^No possible abbreviation preceding point$"))
1028 (add-to-list 'debug-ignored-errors mess))
1029
01987a6b 1030(provide 'dabbrev)
7313ccdb 1031
ab5796a9 1032;;; arch-tag: 29e58596-f080-4306-a409-70296cf9d46f
b578f267 1033;;; dabbrev.el ends here