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