Commit | Line | Data |
---|---|---|
7313ccdb | 1 | ;;; dabbrev.el --- dynamic abbreviation package |
b578f267 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1985-1986, 1992, 1994, 1996-1997, 2000-2011 |
f3ae2521 | 4 | ;; Free Software Foundation, Inc. |
2f790b20 | 5 | |
6163b3ba | 6 | ;; Author: Don Morrison |
f3ae2521 GM |
7 | ;; Lars Lindberg |
8 | ;; (according to ack.texi) | |
6163b3ba RS |
9 | ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se> |
10 | ;; Created: 16 Mars 1992 | |
df6eb420 | 11 | ;; Lindberg's last update version: 5.7 |
f5f727f8 | 12 | ;; Keywords: abbrev expand completion convenience |
3a801d0c | 13 | |
b578f267 EN |
14 | ;; This file is part of GNU Emacs. |
15 | ||
eb3fa2cf | 16 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
2f790b20 | 17 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
18 | ;; the Free Software Foundation, either version 3 of the License, or |
19 | ;; (at your option) any later version. | |
b578f267 EN |
20 | |
21 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
2f790b20 JB |
22 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | ;; GNU General Public License for more details. | |
b578f267 | 25 | |
2f790b20 | 26 | ;; You should have received a copy of the GNU General Public License |
eb3fa2cf | 27 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
2f790b20 | 28 | |
e5167999 | 29 | ;;; Commentary: |
2f790b20 | 30 | |
6163b3ba RS |
31 | ;; The purpose with this package is to let you write just a few |
32 | ;; characters of words you've written earlier to be able to expand | |
33 | ;; them. | |
34 | ;; | |
35 | ;; To expand a word, just put the point right after the word and press | |
36 | ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion). | |
37 | ;; | |
6163b3ba RS |
38 | ;; Check out the customizable variables below to learn about all the |
39 | ;; features of this package. | |
40 | ||
41 | ;;; Hints and tips for major modes writers: | |
42 | ||
43 | ;; Recommended values C/Lisp etc text | |
44 | ;; dabbrev-case-fold-search nil t | |
45 | ;; dabbrev-case-replace nil t | |
46 | ;; | |
47 | ;; Set the variables you want special for your mode like this: | |
48 | ;; (set (make-local-variable 'dabbrev-case-replace) nil) | |
a7acbbe4 | 49 | ;; Then you don't interfere with other modes. |
6163b3ba RS |
50 | ;; |
51 | ;; If your mode handles buffers that refers to other buffers | |
52 | ;; (i.e. compilation-mode, gud-mode), then try to set | |
53 | ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function' | |
54 | ;; to a function that point out those buffers. | |
55 | ||
56 | ;; Same goes for major-modes that are connected to other modes. There | |
57 | ;; are for instance a number of mail-modes. One for reading, one for | |
58 | ;; creating a new mail etc. Maybe those should be connected. | |
59 | ||
60 | ;; Example for GNUS (when we write a reply, we want dabbrev to look in | |
61 | ;; the article for expansion): | |
62 | ;; (set (make-local-variable 'dabbrev-friend-buffer-function) | |
63 | ;; (lambda (buffer) | |
7fdbcd83 | 64 | ;; (with-current-buffer buffer |
6163b3ba RS |
65 | ;; (memq major-mode '(news-reply-mode gnus-article-mode))))) |
66 | ||
6163b3ba RS |
67 | |
68 | ;; Known bugs and limitations. | |
69 | ;; - Possible to do several levels of `dabbrev-completion' in the | |
70 | ;; minibuffer. | |
71 | ;; - dabbrev-completion doesn't handle resetting the globals variables | |
72 | ;; right. It resets them after finding the abbrev. | |
73 | ||
74 | ;; Future enhancements | |
75 | ;; - Check the tags-files? Like tags-complete? | |
76 | ;; - Add the possibility of searching both forward and backward to | |
77 | ;; the nearest expansion. | |
7313ccdb RS |
78 | ;; - Check the kill-ring when everything else fails. (Maybe something |
79 | ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu> | |
6163b3ba | 80 | |
df6eb420 | 81 | ;;; These people gave suggestions: |
6163b3ba RS |
82 | ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com> |
83 | ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu> | |
84 | ;; [jules] Julian Gosnell <jules@x.co.uk> | |
85 | ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu> | |
86 | ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se> | |
87 | ;; [alon] Alon Albert <al%imercury@uunet.uu.net> | |
88 | ;; [tromey] Tom Tromey <tromey@busco.lanl.gov> | |
89 | ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de> | |
90 | ;; [Petri] Petri Raitio <per@tekla.fi> | |
7bd9ba70 | 91 | ;; [ejb] Jay Berkenbilt <ejb@ql.org> |
6163b3ba RS |
92 | ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com> |
93 | ;; ... and to all the people who have participated in the beta tests. | |
2f790b20 | 94 | |
e5167999 | 95 | ;;; Code: |
6163b3ba | 96 | |
b578f267 EN |
97 | ;;---------------------------------------------------------------- |
98 | ;; Customization variables | |
99 | ;;---------------------------------------------------------------- | |
6163b3ba | 100 | |
bbf5eb28 | 101 | (defgroup dabbrev nil |
a6bd541a | 102 | "Dynamic Abbreviations." |
bbf5eb28 | 103 | :tag "Dynamic Abbreviations" |
f5f727f8 DN |
104 | :group 'abbrev |
105 | :group 'convenience) | |
6163b3ba | 106 | |
bbf5eb28 | 107 | (defcustom dabbrev-backward-only nil |
9201cc28 | 108 | "If non-nil, `dabbrev-expand' only looks backwards." |
bbf5eb28 RS |
109 | :type 'boolean |
110 | :group 'dabbrev) | |
111 | ||
112 | (defcustom dabbrev-limit nil | |
9201cc28 | 113 | "Limits region searched by `dabbrev-expand' to this many chars away." |
bbf5eb28 RS |
114 | :type '(choice (const :tag "off" nil) |
115 | integer) | |
116 | :group 'dabbrev) | |
117 | ||
118 | (defcustom dabbrev-abbrev-skip-leading-regexp nil | |
9201cc28 | 119 | "Regexp for skipping leading characters of an abbreviation. |
6163b3ba | 120 | |
7313ccdb RS |
121 | Example: Set this to \"\\\\$\" for programming languages |
122 | in which variable names may appear with or without a leading `$'. | |
79bb4872 | 123 | \(For example, in Makefiles.\) |
6163b3ba | 124 | |
bbf5eb28 RS |
125 | Set this to nil if no characters should be skipped." |
126 | :type '(choice regexp | |
127 | (const :tag "off" nil)) | |
128 | :group 'dabbrev) | |
6163b3ba | 129 | |
e2e75068 | 130 | (defcustom dabbrev-eliminate-newlines t |
9201cc28 | 131 | "Non-nil means dabbrev should not insert newlines. |
fd2dfb40 RS |
132 | Instead it converts them to spaces." |
133 | :type 'boolean | |
134 | :group 'dabbrev) | |
135 | ||
bbf5eb28 | 136 | (defcustom dabbrev-case-fold-search 'case-fold-search |
9201cc28 | 137 | "Control whether dabbrev searches should ignore case. |
7313ccdb | 138 | A value of nil means case is significant. |
543abb4a RS |
139 | A value of `case-fold-search' means case is significant |
140 | if `case-fold-search' is nil. | |
141 | Any other non-nil version means case is not significant." | |
142 | :type '(choice (const :tag "off" nil) | |
eaaca5ee AS |
143 | (const :tag "like search" case-fold-search) |
144 | (other :tag "on" t)) | |
bbf5eb28 | 145 | :group 'dabbrev) |
6dc3311d | 146 | ;;;###autoload(put 'dabbrev-case-fold-search 'risky-local-variable t) |
6163b3ba | 147 | |
bbf5eb28 | 148 | (defcustom dabbrev-upcase-means-case-search nil |
9201cc28 | 149 | "The significance of an uppercase character in an abbreviation. |
602ea79e | 150 | A nil value means case fold search when searching for possible expansions; |
93a43334 | 151 | non-nil means case sensitive search. |
6163b3ba | 152 | |
7313ccdb | 153 | This variable has an effect only when the value of |
543abb4a | 154 | `dabbrev-case-fold-search' says to ignore case." |
bbf5eb28 RS |
155 | :type 'boolean |
156 | :group 'dabbrev) | |
6163b3ba | 157 | |
93a43334 | 158 | (defcustom dabbrev-case-distinction 'case-replace |
9201cc28 | 159 | "Whether dabbrev treats expansions as the same if they differ in case. |
93a43334 RS |
160 | |
161 | A value of nil means treat them as different. | |
162 | A value of `case-replace' means distinguish them if `case-replace' is nil. | |
163 | Any other non-nil value means to treat them as the same. | |
164 | ||
165 | This variable has an effect only when the value of | |
166 | `dabbrev-case-fold-search' specifies to ignore case." | |
167 | :type '(choice (const :tag "off" nil) | |
168 | (const :tag "based on `case-replace'" case-replace) | |
169 | (other :tag "on" t)) | |
170 | :group 'dabbrev | |
bf247b6e | 171 | :version "22.1") |
93a43334 | 172 | |
bbf5eb28 | 173 | (defcustom dabbrev-case-replace 'case-replace |
9201cc28 | 174 | "Whether dabbrev applies the abbreviations's case pattern to the expansion. |
93a43334 RS |
175 | |
176 | A value of nil means preserve the expansion's case pattern. | |
177 | A value of `case-replace' means preserve it if `case-replace' is nil. | |
178 | Any other non-nil value means modify the expansion | |
179 | by applying the abbreviation's case pattern to it. | |
6163b3ba | 180 | |
7313ccdb | 181 | This variable has an effect only when the value of |
543abb4a RS |
182 | `dabbrev-case-fold-search' specifies to ignore case." |
183 | :type '(choice (const :tag "off" nil) | |
93a43334 | 184 | (const :tag "based on `case-replace'" case-replace) |
eaaca5ee | 185 | (other :tag "on" t)) |
bbf5eb28 | 186 | :group 'dabbrev) |
6dc3311d | 187 | ;;;###autoload(put 'dabbrev-case-replace 'risky-local-variable t) |
6163b3ba | 188 | |
bbf5eb28 | 189 | (defcustom dabbrev-abbrev-char-regexp nil |
9201cc28 | 190 | "Regexp to recognize a character in an abbreviation or expansion. |
7313ccdb | 191 | This regexp will be surrounded with \\\\( ... \\\\) when actually used. |
6163b3ba | 192 | |
7313ccdb | 193 | Set this variable to \"\\\\sw\" if you want ordinary words or |
df6eb420 RS |
194 | \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose |
195 | syntax is \"symbol\" as well as those whose syntax is \"word\". | |
6163b3ba | 196 | |
df6eb420 RS |
197 | The value nil has a special meaning: the abbreviation is from point to |
198 | previous word-start, but the search is for symbols. | |
6163b3ba | 199 | |
7313ccdb | 200 | For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol, |
df6eb420 RS |
201 | while `yes', `or', `no' and `p' are considered words. If this |
202 | variable is nil, then expanding `yes-or-no-' looks for a symbol | |
7313ccdb RS |
203 | starting with or containing `no-'. If you set this variable to |
204 | \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with | |
205 | `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then | |
206 | expanding `yes-or-no-' signals an error because `-' is not part of a word; | |
207 | but expanding `yes-or-no' looks for a word starting with `no'. | |
6163b3ba | 208 | |
fe204702 LMI |
209 | The recommended value is nil, which will make dabbrev default to |
210 | using \"\\\\sw\\\\|\\\\s_\"." | |
bbf5eb28 RS |
211 | :type '(choice (const nil) |
212 | regexp) | |
213 | :group 'dabbrev) | |
6163b3ba | 214 | |
bbf5eb28 | 215 | (defcustom dabbrev-check-all-buffers t |
9201cc28 | 216 | "Non-nil means dabbrev package should search *all* buffers. |
6163b3ba | 217 | |
df6eb420 RS |
218 | Dabbrev always searches the current buffer first. Then, if |
219 | `dabbrev-check-other-buffers' says so, it searches the buffers | |
220 | designated by `dabbrev-select-buffers-function'. | |
6163b3ba | 221 | |
df6eb420 | 222 | Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches |
7e65cba2 GM |
223 | all the other buffers, except those named in `dabbrev-ignored-buffer-names', |
224 | or 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 | 230 | See 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 |
237 | See 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 |
245 | nil: Don't look in other buffers. |
246 | t: Also look for expansions in the buffers pointed out by | |
247 | `dabbrev-select-buffers-function'. | |
248 | Anything else: When we can't find any more expansions in | |
249 | the current buffer, then ask the user whether to look in other | |
250 | buffers too. | |
251 | ||
bbf5eb28 RS |
252 | The 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 | 263 | The function should take no arguments and return a list of buffers to |
05be3833 RS |
264 | search for expansions. See the source of `dabbrev--select-buffers' |
265 | for an example. | |
6163b3ba RS |
266 | |
267 | A 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 |
271 | The function should take one argument, OTHER-BUFFER, and return |
272 | non-nil if that buffer should be searched. Have a look at | |
273 | `dabbrev--same-major-mode-p' for an example. | |
274 | ||
7313ccdb RS |
275 | The value of `dabbrev-friend-buffer-function' has an effect only if |
276 | the 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 |
279 | A 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. |
285 | If this variable is non-nil, dabbrev will only look in these buffers. | |
286 | It will not even look in the current buffer if it is not a member of | |
f5307782 JB |
287 | this 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 | 343 | |
6163b3ba | 344 | (defsubst dabbrev--minibuffer-origin () |
5f24557b SM |
345 | "Get the buffer from which mini-buffer." |
346 | (window-buffer (minibuffer-selected-window))) | |
6163b3ba | 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) |
5f24557b | 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 |
373 | Like \\[dabbrev-expand] but finds all expansions in the current buffer |
374 | and presents suggestions for completion. | |
375 | ||
5f24557b | 376 | With a prefix argument ARG, it searches all buffers accepted by the |
7313ccdb | 377 | function pointed out by `dabbrev-friend-buffer-function' to find the |
dd1ae355 RS |
378 | completions. |
379 | ||
bbc4e17c | 380 | If the prefix argument is 16 (which comes from \\[universal-argument] \\[universal-argument]), |
fe0e0a47 | 381 | then 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)) |
5f24557b SM |
388 | (beg (progn (search-backward abbrev) (point))) |
389 | (end (progn (search-forward abbrev) (point))) | |
543abb4a RS |
390 | (ignore-case-p (and (if (eq dabbrev-case-fold-search 'case-fold-search) |
391 | case-fold-search | |
392 | dabbrev-case-fold-search) | |
393 | (or (not dabbrev-upcase-means-case-search) | |
394 | (string= abbrev (downcase abbrev))))) | |
06b60517 | 395 | (my-obarray dabbrev--last-obarray)) |
6163b3ba | 396 | (save-excursion |
fe0e0a47 LT |
397 | ;;-------------------------------- |
398 | ;; New abbreviation to expand. | |
399 | ;;-------------------------------- | |
400 | (setq dabbrev--last-abbreviation abbrev) | |
401 | ;; Find all expansion | |
402 | (let ((completion-list | |
403 | (dabbrev--find-all-expansions abbrev ignore-case-p)) | |
404 | (completion-ignore-case ignore-case-p)) | |
405 | ;; Make an obarray with all expansions | |
406 | (setq my-obarray (make-vector (length completion-list) 0)) | |
407 | (or (> (length my-obarray) 0) | |
408 | (error "No dynamic expansion for \"%s\" found%s" | |
409 | abbrev | |
410 | (if dabbrev--check-other-buffers "" " in this-buffer"))) | |
411 | (cond | |
412 | ((or (not ignore-case-p) | |
413 | (not dabbrev-case-replace)) | |
414 | (mapc (function (lambda (string) | |
415 | (intern string my-obarray))) | |
416 | completion-list)) | |
417 | ((string= abbrev (upcase abbrev)) | |
418 | (mapc (function (lambda (string) | |
419 | (intern (upcase string) my-obarray))) | |
420 | completion-list)) | |
421 | ((string= (substring abbrev 0 1) | |
422 | (upcase (substring abbrev 0 1))) | |
423 | (mapc (function (lambda (string) | |
424 | (intern (capitalize string) my-obarray))) | |
425 | completion-list)) | |
426 | (t | |
427 | (mapc (function (lambda (string) | |
428 | (intern (downcase string) my-obarray))) | |
429 | completion-list))) | |
430 | (setq dabbrev--last-obarray my-obarray) | |
5f24557b SM |
431 | (setq dabbrev--last-completion-buffer (current-buffer)))) |
432 | (completion-in-region beg end my-obarray))) | |
2f790b20 JB |
433 | |
434 | ;;;###autoload | |
435 | (defun dabbrev-expand (arg) | |
436 | "Expand previous word \"dynamically\". | |
2f790b20 | 437 | |
6163b3ba RS |
438 | Expands to the most recent, preceding word for which this is a prefix. |
439 | If no suitable preceding word is found, words following point are | |
440 | considered. If still no suitable word is found, then look in the | |
441 | buffers accepted by the function pointed out by variable | |
442 | `dabbrev-friend-buffer-function'. | |
2f790b20 | 443 | |
7313ccdb | 444 | A positive prefix argument, N, says to take the Nth backward *distinct* |
6163b3ba | 445 | possibility. A negative argument says search forward. |
2f790b20 JB |
446 | |
447 | If the cursor has not moved from the end of the previous expansion and | |
448 | no argument is given, replace the previously-made expansion | |
6163b3ba RS |
449 | with the next possible expansion not yet tried. |
450 | ||
451 | The variable `dabbrev-backward-only' may be used to limit the | |
452 | direction of search to backward if set non-nil. | |
453 | ||
7313ccdb | 454 | See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]." |
2f790b20 | 455 | (interactive "*P") |
dea5efcb RS |
456 | (let (abbrev record-case-pattern |
457 | expansion old direction (orig-point (point))) | |
2f790b20 JB |
458 | ;; abbrev -- the abbrev to expand |
459 | ;; expansion -- the expansion found (eventually) or nil until then | |
460 | ;; old -- the text currently in the buffer | |
461 | ;; (the abbrev, or the previously-made expansion) | |
2f790b20 JB |
462 | (save-excursion |
463 | (if (and (null arg) | |
df6eb420 RS |
464 | (markerp dabbrev--last-abbrev-location) |
465 | (marker-position dabbrev--last-abbrev-location) | |
6163b3ba RS |
466 | (or (eq last-command this-command) |
467 | (and (window-minibuffer-p (selected-window)) | |
468 | (= dabbrev--last-abbrev-location | |
469 | (point))))) | |
7313ccdb | 470 | ;; Find a different expansion for the same abbrev as last time. |
6163b3ba RS |
471 | (progn |
472 | (setq abbrev dabbrev--last-abbreviation) | |
473 | (setq old dabbrev--last-expansion) | |
474 | (setq direction dabbrev--last-direction)) | |
a8a2d6ca KH |
475 | ;; If the user inserts a space after expanding |
476 | ;; and then asks to expand again, always fetch the next word. | |
a6bd541a | 477 | (if (and (eq (preceding-char) ?\s) |
a8a2d6ca KH |
478 | (markerp dabbrev--last-abbrev-location) |
479 | (marker-position dabbrev--last-abbrev-location) | |
480 | (= (point) (1+ dabbrev--last-abbrev-location))) | |
dea5efcb | 481 | (progn |
a8a2d6ca KH |
482 | ;; The "abbrev" to expand is just the space. |
483 | (setq abbrev " ") | |
484 | (save-excursion | |
b9559a72 RS |
485 | (save-restriction |
486 | (widen) | |
487 | (if dabbrev--last-buffer | |
488 | (set-buffer dabbrev--last-buffer)) | |
489 | ;; Find the end of the last "expansion" word. | |
490 | (if (or (eq dabbrev--last-direction 1) | |
491 | (and (eq dabbrev--last-direction 0) | |
492 | (< dabbrev--last-expansion-location (point)))) | |
493 | (setq dabbrev--last-expansion-location | |
494 | (+ dabbrev--last-expansion-location | |
495 | (length dabbrev--last-expansion)))) | |
496 | (goto-char dabbrev--last-expansion-location) | |
497 | ;; Take the following word, with intermediate separators, | |
498 | ;; as our expansion this time. | |
499 | (re-search-forward | |
500 | (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+")) | |
501 | (setq expansion (buffer-substring-no-properties | |
502 | dabbrev--last-expansion-location (point))) | |
503 | ||
504 | ;; Record the end of this expansion, in case we repeat this. | |
505 | (setq dabbrev--last-expansion-location (point)))) | |
a8a2d6ca KH |
506 | ;; Indicate that dabbrev--last-expansion-location is |
507 | ;; at the end of the expansion. | |
508 | (setq dabbrev--last-direction -1)) | |
509 | ||
510 | ;; We have a different abbrev to expand. | |
511 | (dabbrev--reset-global-variables) | |
512 | (setq direction (if (null arg) | |
513 | (if dabbrev-backward-only 1 0) | |
514 | (prefix-numeric-value arg))) | |
515 | (setq abbrev (dabbrev--abbrev-at-point)) | |
dea5efcb | 516 | (setq record-case-pattern t) |
a8a2d6ca | 517 | (setq old nil))) |
6163b3ba RS |
518 | |
519 | ;;-------------------------------- | |
520 | ;; Find the expansion | |
521 | ;;-------------------------------- | |
a8a2d6ca KH |
522 | (or expansion |
523 | (setq expansion | |
524 | (dabbrev--find-expansion abbrev direction | |
543abb4a RS |
525 | (and (if (eq dabbrev-case-fold-search 'case-fold-search) |
526 | case-fold-search | |
527 | dabbrev-case-fold-search) | |
a8a2d6ca KH |
528 | (or (not dabbrev-upcase-means-case-search) |
529 | (string= abbrev (downcase abbrev)))))))) | |
6163b3ba RS |
530 | (cond |
531 | ((not expansion) | |
532 | (dabbrev--reset-global-variables) | |
533 | (if old | |
534 | (save-excursion | |
4209f479 | 535 | (setq buffer-undo-list (cons orig-point buffer-undo-list)) |
3132e115 RS |
536 | ;; Put back the original abbrev with its original case pattern. |
537 | (search-backward old) | |
538 | (insert abbrev) | |
539 | (delete-region (point) (+ (point) (length old))))) | |
7313ccdb | 540 | (error "No%s dynamic expansion for `%s' found" |
6163b3ba RS |
541 | (if old " further" "") abbrev)) |
542 | (t | |
fdad0f14 GM |
543 | (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found) |
544 | (minibuffer-window-active-p (selected-window)))) | |
2f790b20 | 545 | (progn |
6163b3ba RS |
546 | (message "Expansion found in '%s'" |
547 | (buffer-name dabbrev--last-buffer)) | |
548 | (setq dabbrev--last-buffer-found dabbrev--last-buffer)) | |
549 | (message nil)) | |
a8a2d6ca KH |
550 | (if (and (or (eq (current-buffer) dabbrev--last-buffer) |
551 | (null dabbrev--last-buffer)) | |
552 | (numberp dabbrev--last-expansion-location) | |
553 | (and (> dabbrev--last-expansion-location (point)))) | |
554 | (setq dabbrev--last-expansion-location | |
555 | (copy-marker dabbrev--last-expansion-location))) | |
2f790b20 | 556 | ;; Success: stick it in and return. |
4209f479 | 557 | (setq buffer-undo-list (cons orig-point buffer-undo-list)) |
3ffa545b GM |
558 | (dabbrev--substitute-expansion old abbrev expansion |
559 | record-case-pattern) | |
dea5efcb | 560 | |
2f790b20 | 561 | ;; Save state for re-expand. |
da49057c | 562 | (setq dabbrev--last-expansion expansion) |
6163b3ba RS |
563 | (setq dabbrev--last-abbreviation abbrev) |
564 | (setq dabbrev--last-abbrev-location (point-marker)))))) | |
565 | ||
b578f267 EN |
566 | ;;---------------------------------------------------------------- |
567 | ;; Local functions | |
568 | ;;---------------------------------------------------------------- | |
6163b3ba | 569 | |
6163b3ba | 570 | (defun dabbrev--same-major-mode-p (other-buffer) |
5f24557b | 571 | "Check if OTHER-BUFFER has the same major mode as current buffer." |
7313ccdb | 572 | (eq major-mode |
7fdbcd83 | 573 | (with-current-buffer other-buffer |
7313ccdb | 574 | major-mode))) |
6163b3ba | 575 | |
6163b3ba | 576 | (defun dabbrev--goto-start-of-abbrev () |
5f24557b SM |
577 | "Back over all abbrev type characters and then moves forward over |
578 | all skip characters." | |
6163b3ba RS |
579 | ;; Move backwards over abbrev chars |
580 | (save-match-data | |
b0021416 RS |
581 | (when (> (point) (minibuffer-prompt-end)) |
582 | (forward-char -1) | |
583 | (while (and (looking-at dabbrev--abbrev-char-regexp) | |
584 | (> (point) (minibuffer-prompt-end)) | |
585 | (not (= (point) (field-beginning (point) nil | |
586 | (1- (point)))))) | |
587 | (forward-char -1)) | |
588 | (or (looking-at dabbrev--abbrev-char-regexp) | |
589 | (forward-char 1))) | |
6163b3ba RS |
590 | (and dabbrev-abbrev-skip-leading-regexp |
591 | (while (looking-at dabbrev-abbrev-skip-leading-regexp) | |
592 | (forward-char 1))))) | |
593 | ||
6163b3ba | 594 | (defun dabbrev--abbrev-at-point () |
5f24557b | 595 | "Extract the symbol at point to serve as abbreviation." |
6163b3ba | 596 | ;; Check for error |
a8a2d6ca KH |
597 | (if (bobp) |
598 | (error "No possible abbreviation preceding point")) | |
6163b3ba RS |
599 | ;; Return abbrev at point |
600 | (save-excursion | |
a8a2d6ca | 601 | ;; Record the end of the abbreviation. |
6163b3ba | 602 | (setq dabbrev--last-abbrev-location (point)) |
a8a2d6ca KH |
603 | ;; If we aren't right after an abbreviation, |
604 | ;; move point back to just after one. | |
605 | ;; This is so the user can get successive words | |
606 | ;; by typing the punctuation followed by M-/. | |
607 | (save-match-data | |
608 | (if (save-excursion | |
609 | (forward-char -1) | |
5f24557b SM |
610 | (not (looking-at (or dabbrev-abbrev-char-regexp |
611 | "\\sw\\|\\s_")))) | |
a8a2d6ca KH |
612 | (if (re-search-backward (or dabbrev-abbrev-char-regexp |
613 | "\\sw\\|\\s_") | |
614 | nil t) | |
615 | (forward-char 1) | |
616 | (error "No possible abbreviation preceding point")))) | |
617 | ;; Now find the beginning of that one. | |
618 | (dabbrev--goto-start-of-abbrev) | |
6f0b000c RS |
619 | (buffer-substring-no-properties |
620 | dabbrev--last-abbrev-location (point)))) | |
da49057c | 621 | |
6163b3ba | 622 | (defun dabbrev--reset-global-variables () |
5f24557b | 623 | "Initialize all global variables." |
6163b3ba RS |
624 | ;; dabbrev--last-obarray and dabbrev--last-completion-buffer |
625 | ;; must not be reset here. | |
626 | (setq dabbrev--last-table nil | |
627 | dabbrev--last-abbreviation nil | |
628 | dabbrev--last-abbrev-location nil | |
629 | dabbrev--last-direction nil | |
630 | dabbrev--last-expansion nil | |
631 | dabbrev--last-expansion-location nil | |
632 | dabbrev--friend-buffer-list nil | |
633 | dabbrev--last-buffer nil | |
634 | dabbrev--last-buffer-found nil | |
635 | dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp | |
636 | "\\sw\\|\\s_") | |
df6eb420 | 637 | dabbrev--check-other-buffers dabbrev-check-other-buffers)) |
6163b3ba | 638 | |
6163b3ba | 639 | (defun dabbrev--select-buffers () |
513e7954 RS |
640 | "Return a list of other buffers to search for a possible abbrev. |
641 | The current buffer is not included in the list. | |
642 | ||
643 | This function makes a list of all the buffers returned by `buffer-list', | |
644 | then discards buffers whose names match `dabbrev-ignored-buffer-names' | |
645 | or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which | |
646 | `dabbrev-friend-buffer-function', if it is bound, returns nil when called | |
647 | with the buffer as argument. | |
648 | It returns the list of the buffers that are not discarded." | |
649 | (dabbrev-filter-elements | |
650 | buffer (buffer-list) | |
651 | (and (not (eq (current-buffer) buffer)) | |
652 | (not (dabbrev--ignore-buffer-p buffer)) | |
653 | (boundp 'dabbrev-friend-buffer-function) | |
2ebf8f54 | 654 | (funcall dabbrev-friend-buffer-function buffer)))) |
7313ccdb | 655 | |
6163b3ba | 656 | (defun dabbrev--try-find (abbrev reverse n ignore-case) |
fd2dfb40 RS |
657 | "Search for ABBREV, backwards if REVERSE, N times. |
658 | If IGNORE-CASE is non-nil, ignore case while searching. | |
659 | Return the expansion found, and save the location of the start | |
660 | of the expansion in `dabbrev--last-expansion-location'." | |
6163b3ba | 661 | (save-excursion |
df6eb420 RS |
662 | (save-restriction |
663 | (widen) | |
664 | (let ((expansion nil)) | |
665 | (and dabbrev--last-expansion-location | |
666 | (goto-char dabbrev--last-expansion-location)) | |
667 | (let ((case-fold-search ignore-case) | |
668 | (count n)) | |
669 | (while (and (> count 0) | |
670 | (setq expansion (dabbrev--search abbrev | |
671 | reverse | |
93a43334 RS |
672 | (and ignore-case |
673 | (if (eq dabbrev-case-distinction 'case-replace) | |
674 | case-replace | |
675 | dabbrev-case-distinction)) | |
676 | ))) | |
df6eb420 RS |
677 | (setq count (1- count)))) |
678 | (and expansion | |
679 | (setq dabbrev--last-expansion-location (point))) | |
680 | expansion)))) | |
6163b3ba | 681 | |
6163b3ba | 682 | (defun dabbrev--find-all-expansions (abbrev ignore-case) |
fd2dfb40 RS |
683 | "Return a list of all possible expansions of ABBREV. |
684 | If IGNORE-CASE is non-nil, accept matches which differ in case." | |
6163b3ba RS |
685 | (let ((all-expansions nil) |
686 | expansion) | |
687 | (save-excursion | |
688 | (goto-char (point-min)) | |
689 | (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case)) | |
df6eb420 | 690 | (setq all-expansions (cons expansion all-expansions)))) |
6163b3ba RS |
691 | all-expansions)) |
692 | ||
a6a06429 MB |
693 | (defun dabbrev--ignore-buffer-p (buffer) |
694 | "Return non-nil if BUFFER should be ignored by dabbrev." | |
695 | (let ((bn (buffer-name buffer))) | |
696 | (or (member bn dabbrev-ignored-buffer-names) | |
697 | (let ((tail dabbrev-ignored-buffer-regexps) | |
698 | (match nil)) | |
699 | (while (and tail (not match)) | |
700 | (setq match (string-match (car tail) bn) | |
701 | tail (cdr tail))) | |
702 | match)))) | |
703 | ||
6163b3ba | 704 | (defun dabbrev--find-expansion (abbrev direction ignore-case) |
fd2dfb40 RS |
705 | "Find one occurrence of ABBREV, and return the expansion. |
706 | DIRECTION > 0 means look that many times backwards. | |
707 | DIRECTION < 0 means look that many times forward. | |
708 | DIRECTION = 0 means try both backward and forward. | |
709 | IGNORE-CASE non-nil means ignore case when searching. | |
710 | This sets `dabbrev--last-direction' to 1 or -1 according | |
711 | to the direction in which the occurrence was actually found. | |
71296446 | 712 | It sets `dabbrev--last-expansion-location' to the location |
fd2dfb40 | 713 | of the start of the occurrence." |
513e7954 RS |
714 | (save-excursion |
715 | ;; If we were scanning something other than the current buffer, | |
716 | ;; continue scanning there. | |
717 | (when dabbrev--last-buffer | |
7daa3523 | 718 | (set-buffer dabbrev--last-buffer)) |
513e7954 RS |
719 | (or |
720 | ;; ------------------------------------------ | |
721 | ;; Look backward in current buffer. | |
722 | ;; ------------------------------------------ | |
723 | (and (not dabbrev-search-these-buffers-only) | |
724 | (>= direction 0) | |
725 | (setq dabbrev--last-direction (min 1 direction)) | |
726 | (dabbrev--try-find abbrev t | |
727 | (max 1 direction) | |
728 | ignore-case)) | |
729 | ;; ------------------------------------------ | |
730 | ;; Look forward in current buffer | |
731 | ;; or whatever buffer we were last scanning. | |
732 | ;; ------------------------------------------ | |
733 | (and (or (not dabbrev-search-these-buffers-only) | |
734 | dabbrev--last-buffer) | |
735 | (<= direction 0) | |
736 | (setq dabbrev--last-direction -1) | |
737 | (dabbrev--try-find abbrev nil | |
738 | (max 1 (- direction)) | |
739 | ignore-case)) | |
740 | ;; ------------------------------------------ | |
741 | ;; Look in other buffers. | |
742 | ;; Always start at (point-min) and look forward. | |
743 | ;; ------------------------------------------ | |
744 | (progn | |
745 | (setq dabbrev--last-direction -1) | |
746 | (unless dabbrev--last-buffer | |
747 | ;; If we have just now begun to search other buffers, | |
748 | ;; determine which other buffers we should check. | |
749 | ;; Put that list in dabbrev--friend-buffer-list. | |
7daa3523 TTN |
750 | (unless dabbrev--friend-buffer-list |
751 | (setq dabbrev--friend-buffer-list | |
752 | (dabbrev--make-friend-buffer-list)) | |
753 | (setq dabbrev--progress-reporter | |
754 | (make-progress-reporter | |
755 | "Scanning for dabbrevs..." | |
756 | (- (length dabbrev--friend-buffer-list)) 0 0 1 1.5)))) | |
513e7954 RS |
757 | ;; Walk through the buffers till we find a match. |
758 | (let (expansion) | |
759 | (while (and (not expansion) dabbrev--friend-buffer-list) | |
2e27ed13 | 760 | (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list)) |
513e7954 | 761 | (set-buffer dabbrev--last-buffer) |
7daa3523 TTN |
762 | (progress-reporter-update dabbrev--progress-reporter |
763 | (- (length dabbrev--friend-buffer-list))) | |
513e7954 RS |
764 | (setq dabbrev--last-expansion-location (point-min)) |
765 | (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case))) | |
766 | expansion))))) | |
767 | ||
768 | ;; Compute the list of buffers to scan. | |
769 | ;; If dabbrev-search-these-buffers-only, then the current buffer | |
770 | ;; is included in this list if it should be searched. | |
771 | ;; Otherwise, the current buffer is searched first specially., | |
772 | ;; and it is not included in this list. | |
773 | (defun dabbrev--make-friend-buffer-list () | |
774 | (let ((list (mapcar (function get-buffer) | |
775 | dabbrev-search-these-buffers-only))) | |
776 | (when (and (null dabbrev-search-these-buffers-only) | |
777 | dabbrev--check-other-buffers | |
778 | (or (eq dabbrev--check-other-buffers t) | |
779 | (setq dabbrev--check-other-buffers | |
780 | (y-or-n-p "Scan other buffers also? ")))) | |
781 | (setq list (funcall dabbrev-select-buffers-function)) | |
782 | ;; If dabbrev-check-all-buffers, tack on all the other | |
783 | ;; buffers at the end of the list, except those which are | |
784 | ;; specifically to be ignored. | |
785 | (if dabbrev-check-all-buffers | |
786 | (setq list | |
787 | (append list | |
3ffa545b GM |
788 | (dabbrev-filter-elements |
789 | buffer (buffer-list) | |
513e7954 RS |
790 | (and (not (memq buffer list)) |
791 | (not (dabbrev--ignore-buffer-p buffer))))))) | |
792 | ;; Remove the current buffer. | |
793 | (setq list (delq (current-buffer) list))) | |
794 | ;; Move buffers in the list that are visible on the screen | |
795 | ;; to the front of the list, but don't add anything to the list. | |
796 | (if list | |
797 | (walk-windows (lambda (w) | |
798 | (unless (eq w (selected-window)) | |
799 | (if (memq (window-buffer w) list) | |
800 | (setq list | |
801 | (cons (window-buffer w) | |
802 | (delq (window-buffer w) | |
803 | list)))))))) | |
804 | ;; In a minibuffer, search the buffer it was activated from, | |
805 | ;; first after the minibuffer itself. Unless we aren't supposed | |
806 | ;; to search the current buffer either. | |
807 | (if (and (window-minibuffer-p (selected-window)) | |
808 | (not dabbrev-search-these-buffers-only)) | |
809 | (setq list | |
810 | (cons (dabbrev--minibuffer-origin) | |
811 | (delq (dabbrev--minibuffer-origin) list)))) | |
812 | list)) | |
6163b3ba | 813 | |
6163b3ba RS |
814 | (defun dabbrev--safe-replace-match (string &optional fixedcase literal) |
815 | (if (eq major-mode 'picture-mode) | |
6ee55cb1 RS |
816 | (with-no-warnings |
817 | (picture-replace-match string fixedcase literal)) | |
6163b3ba RS |
818 | (replace-match string fixedcase literal))) |
819 | ||
820 | ;;;---------------------------------------------------------------- | |
3ffa545b GM |
821 | (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern) |
822 | "Replace OLD with EXPANSION in the buffer. | |
823 | OLD is text currently in the buffer, perhaps the abbreviation | |
824 | or perhaps another expansion that was tried previously. | |
825 | ABBREV is the abbreviation we are expanding. | |
826 | It is \" \" if we are copying subsequent words. | |
827 | EXPANSION is the expansion substring to be used this time. | |
828 | RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern' | |
829 | to record whether we upcased the expansion, downcased it, or did neither." | |
6163b3ba | 830 | ;;(undo-boundary) |
543abb4a RS |
831 | (let ((use-case-replace (and (if (eq dabbrev-case-fold-search 'case-fold-search) |
832 | case-fold-search | |
833 | dabbrev-case-fold-search) | |
6163b3ba RS |
834 | (or (not dabbrev-upcase-means-case-search) |
835 | (string= abbrev (downcase abbrev))) | |
543abb4a RS |
836 | (if (eq dabbrev-case-replace 'case-replace) |
837 | case-replace | |
838 | dabbrev-case-replace)))) | |
3ffa545b GM |
839 | |
840 | ;; If we upcased or downcased the original expansion, | |
841 | ;; do likewise for the subsequent words when we copy them. | |
fd2dfb40 RS |
842 | ;; Don't do any of the usual case processing, though. |
843 | (when (equal abbrev " ") | |
844 | (if dabbrev--last-case-pattern | |
845 | (setq expansion | |
846 | (funcall dabbrev--last-case-pattern expansion))) | |
847 | (setq use-case-replace nil)) | |
3ffa545b | 848 | |
65411629 RS |
849 | ;; If the expansion has mixed case |
850 | ;; and it is not simply a capitalized word, | |
851 | ;; or if the abbrev has mixed case, | |
852 | ;; and if the given abbrev's case pattern | |
66db4b5b RS |
853 | ;; matches the start of the expansion, |
854 | ;; copy the expansion's case | |
855 | ;; instead of downcasing all the rest. | |
e921af9e RS |
856 | ;; |
857 | ;; Treat a one-capital-letter (possibly with preceding non-letter | |
858 | ;; characters) abbrev as "not all upper case", so as to force | |
859 | ;; preservation of the expansion's pattern if the expansion starts | |
860 | ;; with a capital letter. | |
861 | (let ((expansion-rest (substring expansion 1)) | |
862 | (first-letter-position (string-match "[[:alpha:]]" abbrev))) | |
863 | (if (or (null first-letter-position) | |
864 | (and (not (and (or (string= expansion-rest (downcase expansion-rest)) | |
865 | (string= expansion-rest (upcase expansion-rest))) | |
866 | (or (string= abbrev (downcase abbrev)) | |
867 | (and (string= abbrev (upcase abbrev)) | |
868 | (> (- (length abbrev) first-letter-position) | |
869 | 1))))) | |
870 | (string= abbrev | |
871 | (substring expansion 0 (length abbrev))))) | |
65411629 | 872 | (setq use-case-replace nil))) |
fd2dfb40 RS |
873 | |
874 | ;; If the abbrev and the expansion are both all-lower-case | |
875 | ;; then don't do any conversion. The conversion would be a no-op | |
876 | ;; for this replacement, but it would carry forward to subsequent words. | |
e921af9e | 877 | ;; The goal of this is to prevent that carrying forward. |
fd2dfb40 RS |
878 | (if (and (string= expansion (downcase expansion)) |
879 | (string= abbrev (downcase abbrev))) | |
07e47d0b | 880 | (setq use-case-replace nil)) |
fd2dfb40 | 881 | |
07e47d0b RS |
882 | (if use-case-replace |
883 | (setq expansion (downcase expansion))) | |
3ffa545b GM |
884 | |
885 | ;; In case we insert subsequent words, | |
886 | ;; record if we upcased or downcased the first word, | |
887 | ;; in order to do likewise for subsequent words. | |
888 | (and record-case-pattern | |
71296446 | 889 | (setq dabbrev--last-case-pattern |
3ffa545b GM |
890 | (and use-case-replace |
891 | (cond ((equal abbrev (upcase abbrev)) 'upcase) | |
892 | ((equal abbrev (downcase abbrev)) 'downcase))))) | |
893 | ||
19c17c4e | 894 | ;; Convert whitespace to single spaces. |
e2e75068 RS |
895 | (if dabbrev-eliminate-newlines |
896 | (let ((pos | |
897 | (if (equal abbrev " ") 0 (length abbrev)))) | |
898 | ;; If ABBREV is real, search after the end of it. | |
899 | ;; If ABBREV is space and we are copying successive words, | |
900 | ;; search starting at the front. | |
19c17c4e RS |
901 | (while (string-match "[\n \t]+" expansion pos) |
902 | (setq pos (1+ (match-beginning 0))) | |
903 | (setq expansion (replace-match " " nil nil expansion))))) | |
fd2dfb40 | 904 | |
6163b3ba RS |
905 | (if old |
906 | (save-excursion | |
907 | (search-backward old)) | |
9b7c13e0 | 908 | ;;(set-match-data (list (point-marker) (point-marker))) |
fd2dfb40 RS |
909 | (search-backward abbrev) |
910 | (search-forward abbrev)) | |
911 | ||
6163b3ba RS |
912 | ;; Make case of replacement conform to case of abbreviation |
913 | ;; provided (1) that kind of thing is enabled in this buffer | |
914 | ;; and (2) the replacement itself is all lower case. | |
915 | (dabbrev--safe-replace-match expansion | |
916 | (not use-case-replace) | |
917 | t))) | |
918 | ||
919 | ||
920 | ;;;---------------------------------------------------------------- | |
921 | ;;; Search function used by dabbrevs library. | |
922 | ||
6163b3ba RS |
923 | |
924 | (defun dabbrev--search (abbrev reverse ignore-case) | |
fd2dfb40 RS |
925 | "Search for something that could be used to expand ABBREV. |
926 | ||
927 | Second arg, REVERSE, is t for reverse search, nil for forward. | |
928 | The variable `dabbrev-limit' controls the maximum search region size. | |
929 | Third argument IGNORE-CASE non-nil means treat case as insignificant while | |
930 | looking for a match and when comparing with previous matches. Also if | |
931 | that's non-nil and the match is found at the beginning of a sentence | |
932 | and is in lower case except for the initial then it is converted to | |
933 | all lower case for return. | |
934 | ||
935 | Table of expansions already seen is examined in buffer | |
936 | `dabbrev--last-table' so that only distinct possibilities are found | |
937 | by dabbrev-re-expand. | |
938 | ||
939 | Returns the expansion found, or nil if not found. | |
940 | Leaves point at the location of the start of the expansion." | |
6163b3ba RS |
941 | (save-match-data |
942 | (let ((pattern1 (concat (regexp-quote abbrev) | |
943 | "\\(" dabbrev--abbrev-char-regexp "\\)")) | |
944 | (pattern2 (concat (regexp-quote abbrev) | |
945 | "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)")) | |
155133a2 RS |
946 | ;; This makes it possible to find matches in minibuffer prompts |
947 | ;; even when they are "inviolable". | |
948 | (inhibit-point-motion-hooks t) | |
93a43334 | 949 | found-string result) |
6163b3ba RS |
950 | ;; Limited search. |
951 | (save-restriction | |
952 | (and dabbrev-limit | |
953 | (narrow-to-region dabbrev--last-expansion-location | |
954 | (+ (point) | |
955 | (if reverse (- dabbrev-limit) dabbrev-limit)))) | |
956 | ;;-------------------------------- | |
957 | ;; Look for a distinct expansion, using dabbrev--last-table. | |
958 | ;;-------------------------------- | |
959 | (while (and (not found-string) | |
960 | (if reverse | |
961 | (re-search-backward pattern1 nil t) | |
962 | (re-search-forward pattern1 nil t))) | |
63d991d6 KH |
963 | (goto-char (match-beginning 0)) |
964 | ;; In case we matched in the middle of a word, | |
965 | ;; back up to start of word and verify we still match. | |
966 | (dabbrev--goto-start-of-abbrev) | |
967 | ||
968 | (if (not (looking-at pattern1)) | |
969 | nil | |
970 | ;; We have a truly valid match. Find the end. | |
6163b3ba | 971 | (re-search-forward pattern2) |
2e27ed13 | 972 | (setq found-string (match-string-no-properties 0)) |
93a43334 | 973 | (setq result found-string) |
6163b3ba | 974 | (and ignore-case (setq found-string (downcase found-string))) |
63d991d6 | 975 | ;; Ignore this match if it's already in the table. |
7313ccdb RS |
976 | (if (dabbrev-filter-elements |
977 | table-string dabbrev--last-table | |
978 | (string= found-string table-string)) | |
63d991d6 KH |
979 | (setq found-string nil))) |
980 | ;; Prepare to continue searching. | |
2e27ed13 | 981 | (goto-char (if reverse (match-beginning 0) (match-end 0)))) |
63d991d6 | 982 | ;; If we found something, use it. |
93a43334 RS |
983 | (when found-string |
984 | ;; Put it into `dabbrev--last-table' | |
985 | ;; and return it (either downcased, or as is). | |
986 | (setq dabbrev--last-table | |
987 | (cons found-string dabbrev--last-table)) | |
988 | result))))) | |
6163b3ba | 989 | |
df27f31f | 990 | (dolist (mess '("^No dynamic expansion for .* found" |
87207d14 DL |
991 | "^No further dynamic expansion for .* found$" |
992 | "^No possible abbreviation preceding point$")) | |
993 | (add-to-list 'debug-ignored-errors mess)) | |
994 | ||
01987a6b | 995 | (provide 'dabbrev) |
7313ccdb | 996 | |
b578f267 | 997 | ;;; dabbrev.el ends here |