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