Commit | Line | Data |
---|---|---|
7313ccdb RS |
1 | ;;; dabbrev.el --- dynamic abbreviation package |
2 | ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc. | |
2f790b20 | 3 | |
6163b3ba RS |
4 | ;; Author: Don Morrison |
5 | ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se> | |
6 | ;; Created: 16 Mars 1992 | |
df6eb420 | 7 | ;; Lindberg's last update version: 5.7 |
6163b3ba | 8 | ;; Keywords: abbrev expand completion |
3a801d0c | 9 | |
6163b3ba | 10 | ;; This program is free software; you can redistribute it and/or modify |
2f790b20 | 11 | ;; it under the terms of the GNU General Public License as published by |
6163b3ba RS |
12 | ;; the Free Software Foundation; either version 2 of the License, or |
13 | ;; (at your option) any later version. | |
14 | ;; | |
15 | ;; This program is distributed in the hope that it will be useful, | |
2f790b20 JB |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
6163b3ba | 19 | ;; |
2f790b20 | 20 | ;; You should have received a copy of the GNU General Public License |
6163b3ba RS |
21 | ;; along with this program; if not, write to the Free Software |
22 | ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
2f790b20 | 23 | |
e5167999 | 24 | ;;; Commentary: |
2f790b20 | 25 | |
6163b3ba RS |
26 | ;; The purpose with this package is to let you write just a few |
27 | ;; characters of words you've written earlier to be able to expand | |
28 | ;; them. | |
29 | ;; | |
30 | ;; To expand a word, just put the point right after the word and press | |
31 | ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion). | |
32 | ;; | |
6163b3ba RS |
33 | ;; Check out the customizable variables below to learn about all the |
34 | ;; features of this package. | |
35 | ||
36 | ;;; Hints and tips for major modes writers: | |
37 | ||
38 | ;; Recommended values C/Lisp etc text | |
39 | ;; dabbrev-case-fold-search nil t | |
40 | ;; dabbrev-case-replace nil t | |
41 | ;; | |
42 | ;; Set the variables you want special for your mode like this: | |
43 | ;; (set (make-local-variable 'dabbrev-case-replace) nil) | |
44 | ;; Then you don't interfer with other modes. | |
45 | ;; | |
46 | ;; If your mode handles buffers that refers to other buffers | |
47 | ;; (i.e. compilation-mode, gud-mode), then try to set | |
48 | ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function' | |
49 | ;; to a function that point out those buffers. | |
50 | ||
51 | ;; Same goes for major-modes that are connected to other modes. There | |
52 | ;; are for instance a number of mail-modes. One for reading, one for | |
53 | ;; creating a new mail etc. Maybe those should be connected. | |
54 | ||
55 | ;; Example for GNUS (when we write a reply, we want dabbrev to look in | |
56 | ;; the article for expansion): | |
57 | ;; (set (make-local-variable 'dabbrev-friend-buffer-function) | |
58 | ;; (lambda (buffer) | |
59 | ;; (save-excursion | |
60 | ;; (set-buffer buffer) | |
61 | ;; (memq major-mode '(news-reply-mode gnus-article-mode))))) | |
62 | ||
6163b3ba RS |
63 | |
64 | ;; Known bugs and limitations. | |
65 | ;; - Possible to do several levels of `dabbrev-completion' in the | |
66 | ;; minibuffer. | |
67 | ;; - dabbrev-completion doesn't handle resetting the globals variables | |
68 | ;; right. It resets them after finding the abbrev. | |
69 | ||
70 | ;; Future enhancements | |
71 | ;; - Check the tags-files? Like tags-complete? | |
72 | ;; - Add the possibility of searching both forward and backward to | |
73 | ;; the nearest expansion. | |
7313ccdb RS |
74 | ;; - Check the kill-ring when everything else fails. (Maybe something |
75 | ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu> | |
6163b3ba | 76 | |
df6eb420 | 77 | ;;; These people gave suggestions: |
6163b3ba RS |
78 | ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com> |
79 | ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu> | |
80 | ;; [jules] Julian Gosnell <jules@x.co.uk> | |
81 | ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu> | |
82 | ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se> | |
83 | ;; [alon] Alon Albert <al%imercury@uunet.uu.net> | |
84 | ;; [tromey] Tom Tromey <tromey@busco.lanl.gov> | |
85 | ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de> | |
86 | ;; [Petri] Petri Raitio <per@tekla.fi> | |
87 | ;; [ejb] Jay Berkenbilt <ejb@ERA.COM> | |
88 | ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com> | |
89 | ;; ... and to all the people who have participated in the beta tests. | |
2f790b20 | 90 | |
e5167999 | 91 | ;;; Code: |
6163b3ba RS |
92 | |
93 | ;;;---------------------------------------------------------------- | |
94 | ;;;---------------------------------------------------------------- | |
95 | ;;; Customization variables | |
96 | ;;;---------------------------------------------------------------- | |
97 | ;;;---------------------------------------------------------------- | |
98 | (defvar dabbrev-backward-only nil | |
7313ccdb | 99 | "*If non-nil, `dabbrev-expand' only looks backwards.") |
6163b3ba RS |
100 | |
101 | (defvar dabbrev-limit nil | |
102 | "*Limits region searched by `dabbrev-expand' to this many chars away.") | |
103 | ||
104 | (defvar dabbrev-abbrev-skip-leading-regexp nil | |
105 | "*Regexp for skipping leading characters of an abbreviation. | |
106 | ||
7313ccdb RS |
107 | Example: Set this to \"\\\\$\" for programming languages |
108 | in which variable names may appear with or without a leading `$'. | |
109 | (For example, in Makefiles.) | |
6163b3ba RS |
110 | |
111 | Set this to nil if no characters should be skipped.") | |
112 | ||
113 | ;; I recommend that you set this to nil. | |
114 | (defvar dabbrev-case-fold-search 'case-fold-search | |
7313ccdb RS |
115 | "*Non-nil if dabbrev searches should ignore case. |
116 | A value of nil means case is significant. | |
6163b3ba | 117 | |
7313ccdb RS |
118 | The value of this variable is an expression; it is evaluated |
119 | and the resulting value determines the decision. | |
120 | For example: setting this to `case-fold-search' means evaluate that | |
121 | variable to see whether its value is nil.") | |
6163b3ba RS |
122 | |
123 | (defvar dabbrev-upcase-means-case-search nil | |
124 | "*The significance of an uppercase character in an abbreviation. | |
7313ccdb | 125 | nil means case fold search, non-nil means case sensitive search. |
6163b3ba | 126 | |
7313ccdb RS |
127 | This variable has an effect only when the value of |
128 | `dabbrev-case-fold-search' evaluates to t.") | |
6163b3ba RS |
129 | |
130 | ;; I recommend that you set this to nil. | |
131 | (defvar dabbrev-case-replace 'case-replace | |
7313ccdb RS |
132 | "*Non-nil means dabbrev should preserve case when expanding the abbreviation. |
133 | The value of this variable is an expression; it is evaluated | |
134 | and the resulting value determines the decision. | |
135 | For example, setting this to `case-replace' means evaluate that | |
136 | variable to see if its value is t or nil. | |
6163b3ba | 137 | |
7313ccdb RS |
138 | This variable has an effect only when the value of |
139 | `dabbrev-case-fold-search' evaluates to t.") | |
6163b3ba | 140 | |
df6eb420 | 141 | (defvar dabbrev-abbrev-char-regexp nil |
7313ccdb RS |
142 | "*Regexp to recognize a character in an abbreviation or expansion. |
143 | This regexp will be surrounded with \\\\( ... \\\\) when actually used. | |
6163b3ba | 144 | |
7313ccdb | 145 | Set this variable to \"\\\\sw\" if you want ordinary words or |
df6eb420 RS |
146 | \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose |
147 | syntax is \"symbol\" as well as those whose syntax is \"word\". | |
6163b3ba | 148 | |
df6eb420 RS |
149 | The value nil has a special meaning: the abbreviation is from point to |
150 | previous word-start, but the search is for symbols. | |
6163b3ba | 151 | |
7313ccdb | 152 | For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol, |
df6eb420 RS |
153 | while `yes', `or', `no' and `p' are considered words. If this |
154 | variable is nil, then expanding `yes-or-no-' looks for a symbol | |
7313ccdb RS |
155 | starting with or containing `no-'. If you set this variable to |
156 | \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with | |
157 | `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then | |
158 | expanding `yes-or-no-' signals an error because `-' is not part of a word; | |
159 | but expanding `yes-or-no' looks for a word starting with `no'. | |
6163b3ba RS |
160 | |
161 | The recommended value is \"\\\\sw\\\\|\\\\s_\".") | |
162 | ||
df6eb420 RS |
163 | (defvar dabbrev-check-all-buffers t |
164 | "*Non-nil means dabbrev package should search *all* buffers. | |
6163b3ba | 165 | |
df6eb420 RS |
166 | Dabbrev always searches the current buffer first. Then, if |
167 | `dabbrev-check-other-buffers' says so, it searches the buffers | |
168 | designated by `dabbrev-select-buffers-function'. | |
6163b3ba | 169 | |
df6eb420 RS |
170 | Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches |
171 | all the other buffers.") | |
172 | ||
173 | (defvar dabbrev-check-other-buffers t | |
6163b3ba | 174 | "*Should \\[dabbrev-expand] look in other buffers?\ |
6163b3ba | 175 | |
df6eb420 RS |
176 | nil: Don't look in other buffers. |
177 | t: Also look for expansions in the buffers pointed out by | |
178 | `dabbrev-select-buffers-function'. | |
179 | Anything else: When we can't find any more expansions in | |
180 | the current buffer, then ask the user whether to look in other | |
181 | buffers too. | |
182 | ||
183 | The default value is t.") | |
6163b3ba RS |
184 | |
185 | ;; I guess setting this to a function that selects all C- or C++- | |
186 | ;; mode buffers would be a good choice for a debugging buffer, | |
187 | ;; when debugging C- or C++-code. | |
188 | (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers | |
189 | "A function that selects buffers that should be searched by dabbrev. | |
6163b3ba RS |
190 | The function should take no arguments and return a list of buffers to |
191 | search for expansions. Have a look at `dabbrev--select-buffers' for | |
192 | an example. | |
193 | ||
194 | A mode setting this variable should make it buffer local.") | |
195 | ||
196 | (defvar dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p | |
df6eb420 | 197 | "*A function to decide whether dabbrev should search OTHER-BUFFER. |
6163b3ba RS |
198 | The function should take one argument, OTHER-BUFFER, and return |
199 | non-nil if that buffer should be searched. Have a look at | |
200 | `dabbrev--same-major-mode-p' for an example. | |
201 | ||
7313ccdb RS |
202 | The value of `dabbrev-friend-buffer-function' has an effect only if |
203 | the value of `dabbrev-select-buffers-function' uses it. The function | |
204 | `dabbrev--select-buffers' is one function you can use here. | |
6163b3ba RS |
205 | |
206 | A mode setting this variable should make it buffer local.") | |
207 | ||
208 | (defvar dabbrev-search-these-buffers-only nil | |
7313ccdb RS |
209 | "If non-nil, a list of buffers which dabbrev should search. |
210 | If this variable is non-nil, dabbrev will only look in these buffers. | |
211 | It will not even look in the current buffer if it is not a member of | |
212 | this list.") | |
e5167999 | 213 | |
6163b3ba RS |
214 | ;;;---------------------------------------------------------------- |
215 | ;;;---------------------------------------------------------------- | |
216 | ;;; Internal variables | |
217 | ;;;---------------------------------------------------------------- | |
218 | ;;;---------------------------------------------------------------- | |
2f790b20 | 219 | |
6163b3ba RS |
220 | ;; Last obarray of completions in `dabbrev-completion' |
221 | (defvar dabbrev--last-obarray nil) | |
2f790b20 | 222 | |
6163b3ba RS |
223 | ;; Table of expansions seen so far |
224 | (defvar dabbrev--last-table nil) | |
2f790b20 | 225 | |
6163b3ba RS |
226 | ;; Last string we tried to expand. |
227 | (defvar dabbrev--last-abbreviation nil) | |
2f790b20 | 228 | |
6163b3ba RS |
229 | ;; Location last abbreviation began |
230 | (defvar dabbrev--last-abbrev-location nil) | |
2f790b20 | 231 | |
6163b3ba RS |
232 | ;; Direction of last dabbrevs search |
233 | (defvar dabbrev--last-direction 0) | |
2f790b20 | 234 | |
6163b3ba RS |
235 | ;; Last expansion of an abbreviation. |
236 | (defvar dabbrev--last-expansion nil) | |
2f790b20 | 237 | |
6163b3ba RS |
238 | ;; Location the last expansion was found. |
239 | (defvar dabbrev--last-expansion-location nil) | |
240 | ||
241 | ;; The list of remaining buffers with the same mode as current buffer. | |
242 | (defvar dabbrev--friend-buffer-list nil) | |
243 | ||
244 | ;; The buffer we looked in last. | |
245 | (defvar dabbrev--last-buffer nil) | |
246 | ||
247 | ;; The buffer we found the expansion last time. | |
248 | (defvar dabbrev--last-buffer-found nil) | |
249 | ||
250 | ;; The buffer we last did a completion in. | |
251 | (defvar dabbrev--last-completion-buffer nil) | |
252 | ||
df6eb420 RS |
253 | ;; Same as dabbrev-check-other-buffers, but is set for every expand. |
254 | (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers) | |
6163b3ba RS |
255 | |
256 | ;; The regexp for recognizing a character in an abbreviation. | |
257 | (defvar dabbrev--abbrev-char-regexp nil) | |
258 | ||
259 | ;;;---------------------------------------------------------------- | |
260 | ;;;---------------------------------------------------------------- | |
261 | ;;; Macros | |
262 | ;;;---------------------------------------------------------------- | |
263 | ;;;---------------------------------------------------------------- | |
264 | ||
265 | ;;; Get the buffer that mini-buffer was activated from | |
266 | (defsubst dabbrev--minibuffer-origin () | |
267 | (car (cdr (buffer-list)))) | |
268 | ||
7313ccdb RS |
269 | ;; Make a list of some of the elements of LIST. |
270 | ;; Check each element of LIST, storing it temporarily in the | |
271 | ;; variable ELEMENT, and include it in the result | |
272 | ;; if CONDITION evaluates non-nil. | |
273 | (defmacro dabbrev-filter-elements (element list condition) | |
274 | (` (let (dabbrev-result dabbrev-tail (, element)) | |
275 | (setq dabbrev-tail (, list)) | |
276 | (while dabbrev-tail | |
277 | (setq (, element) (car dabbrev-tail)) | |
278 | (if (, condition) | |
279 | (setq dabbrev-result (cons (, element) dabbrev-result))) | |
280 | (setq dabbrev-tail (cdr dabbrev-tail))) | |
281 | (nreverse dabbrev-result)))) | |
282 | ||
6163b3ba RS |
283 | ;;;---------------------------------------------------------------- |
284 | ;;;---------------------------------------------------------------- | |
285 | ;;; Exported functions | |
286 | ;;;---------------------------------------------------------------- | |
287 | ;;;---------------------------------------------------------------- | |
288 | ||
289 | ;;;###autoload | |
290 | (define-key esc-map "/" 'dabbrev-expand) | |
7313ccdb | 291 | ;;;??? Do we want this? |
6163b3ba | 292 | ;;;###autoload |
f8b581fa | 293 | (define-key esc-map [?\C-/] 'dabbrev-completion) |
6163b3ba RS |
294 | |
295 | ;;;###autoload | |
296 | (defun dabbrev-completion (&optional arg) | |
297 | "Completion on current word. | |
6163b3ba RS |
298 | Like \\[dabbrev-expand] but finds all expansions in the current buffer |
299 | and presents suggestions for completion. | |
300 | ||
7313ccdb RS |
301 | With a prefix argument, it searches all buffers accepted by the |
302 | function pointed out by `dabbrev-friend-buffer-function' to find the | |
dd1ae355 RS |
303 | completions. |
304 | ||
305 | If the prefix argument is 16 (which comes from C-u C-u), | |
306 | then it searches *all* buffers. | |
6163b3ba | 307 | |
7313ccdb RS |
308 | With no prefix argument, it reuses an old completion list |
309 | if there is a suitable one already." | |
6163b3ba RS |
310 | |
311 | (interactive "*P") | |
df6eb420 RS |
312 | (dabbrev--reset-global-variables) |
313 | (let* ((dabbrev-check-other-buffers (and arg t)) | |
314 | (dabbrev-check-all-buffers | |
dd1ae355 | 315 | (and arg (= (prefix-numeric-value arg) 16))) |
6163b3ba RS |
316 | (abbrev (dabbrev--abbrev-at-point)) |
317 | (ignore-case-p (and (eval dabbrev-case-fold-search) | |
318 | (or (not dabbrev-upcase-means-case-search) | |
319 | (string= abbrev (downcase abbrev))))) | |
320 | (my-obarray dabbrev--last-obarray) | |
321 | init) | |
322 | (save-excursion | |
323 | (if (and (null arg) | |
324 | my-obarray | |
325 | (or (eq dabbrev--last-completion-buffer (current-buffer)) | |
326 | (and (window-minibuffer-p (selected-window)) | |
327 | (eq dabbrev--last-completion-buffer | |
328 | (dabbrev--minibuffer-origin)))) | |
329 | dabbrev--last-abbreviation | |
330 | (>= (length abbrev) (length dabbrev--last-abbreviation)) | |
331 | (string= dabbrev--last-abbreviation | |
332 | (substring abbrev 0 | |
333 | (length dabbrev--last-abbreviation))) | |
334 | (setq init (try-completion abbrev my-obarray))) | |
7313ccdb RS |
335 | ;; We can reuse the existing completion list. |
336 | nil | |
6163b3ba RS |
337 | ;;-------------------------------- |
338 | ;; New abbreviation to expand. | |
339 | ;;-------------------------------- | |
6163b3ba RS |
340 | (setq dabbrev--last-abbreviation abbrev) |
341 | ;; Find all expansion | |
342 | (let ((completion-list | |
343 | (dabbrev--find-all-expansions abbrev ignore-case-p))) | |
344 | ;; Make an obarray with all expansions | |
345 | (setq my-obarray (make-vector (length completion-list) 0)) | |
346 | (or (> (length my-obarray) 0) | |
7313ccdb | 347 | (error "No dynamic expansion for \"%s\" found%s" |
6163b3ba RS |
348 | abbrev |
349 | (if dabbrev--check-other-buffers "" " in this-buffer"))) | |
350 | (cond | |
351 | ((or (not ignore-case-p) | |
352 | (not dabbrev-case-replace)) | |
df6eb420 RS |
353 | (mapcar (function (lambda (string) |
354 | (intern string my-obarray))) | |
355 | completion-list)) | |
6163b3ba | 356 | ((string= abbrev (upcase abbrev)) |
df6eb420 RS |
357 | (mapcar (function (lambda (string) |
358 | (intern (upcase string) my-obarray))) | |
359 | completion-list)) | |
6163b3ba RS |
360 | ((string= (substring abbrev 0 1) |
361 | (upcase (substring abbrev 0 1))) | |
df6eb420 RS |
362 | (mapcar (function (lambda (string) |
363 | (intern (capitalize string) my-obarray))) | |
364 | completion-list)) | |
6163b3ba | 365 | (t |
df6eb420 RS |
366 | (mapcar (function (lambda (string) |
367 | (intern (downcase string) my-obarray))) | |
368 | completion-list))) | |
6163b3ba RS |
369 | (setq dabbrev--last-obarray my-obarray) |
370 | (setq dabbrev--last-completion-buffer (current-buffer)) | |
371 | ;; Find the longest common string. | |
372 | (setq init (try-completion abbrev my-obarray))))) | |
373 | ;;-------------------------------- | |
374 | ;; Let the user choose between the expansions | |
375 | ;;-------------------------------- | |
376 | (or (stringp init) | |
377 | (setq init abbrev)) | |
378 | (cond | |
379 | ;; * Replace string fragment with matched common substring completion. | |
380 | ((and (not (string-equal init "")) | |
381 | (not (string-equal (downcase init) (downcase abbrev)))) | |
382 | (if (> (length (all-completions init my-obarray)) 1) | |
7313ccdb RS |
383 | (message "Repeat `%s' to see all completions" |
384 | (key-description (this-command-keys))) | |
6163b3ba RS |
385 | (message "The only possible completion")) |
386 | (dabbrev--substitute-expansion nil abbrev init)) | |
387 | (t | |
388 | ;; * String is a common substring completion already. Make list. | |
389 | (message "Making completion list...") | |
390 | (with-output-to-temp-buffer " *Completions*" | |
391 | (display-completion-list (all-completions init my-obarray))) | |
7313ccdb | 392 | (message "Making completion list...done"))) |
6163b3ba RS |
393 | (and (window-minibuffer-p (selected-window)) |
394 | (message nil)))) | |
2f790b20 JB |
395 | |
396 | ;;;###autoload | |
397 | (defun dabbrev-expand (arg) | |
398 | "Expand previous word \"dynamically\". | |
2f790b20 | 399 | |
6163b3ba RS |
400 | Expands to the most recent, preceding word for which this is a prefix. |
401 | If no suitable preceding word is found, words following point are | |
402 | considered. If still no suitable word is found, then look in the | |
403 | buffers accepted by the function pointed out by variable | |
404 | `dabbrev-friend-buffer-function'. | |
2f790b20 | 405 | |
7313ccdb | 406 | A positive prefix argument, N, says to take the Nth backward *distinct* |
6163b3ba | 407 | possibility. A negative argument says search forward. |
2f790b20 JB |
408 | |
409 | If the cursor has not moved from the end of the previous expansion and | |
410 | no argument is given, replace the previously-made expansion | |
6163b3ba RS |
411 | with the next possible expansion not yet tried. |
412 | ||
413 | The variable `dabbrev-backward-only' may be used to limit the | |
414 | direction of search to backward if set non-nil. | |
415 | ||
7313ccdb | 416 | See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]." |
2f790b20 | 417 | (interactive "*P") |
6163b3ba | 418 | (let (abbrev expansion old direction) |
2f790b20 JB |
419 | ;; abbrev -- the abbrev to expand |
420 | ;; expansion -- the expansion found (eventually) or nil until then | |
421 | ;; old -- the text currently in the buffer | |
422 | ;; (the abbrev, or the previously-made expansion) | |
2f790b20 JB |
423 | (save-excursion |
424 | (if (and (null arg) | |
df6eb420 RS |
425 | (markerp dabbrev--last-abbrev-location) |
426 | (marker-position dabbrev--last-abbrev-location) | |
6163b3ba RS |
427 | (or (eq last-command this-command) |
428 | (and (window-minibuffer-p (selected-window)) | |
429 | (= dabbrev--last-abbrev-location | |
430 | (point))))) | |
7313ccdb | 431 | ;; Find a different expansion for the same abbrev as last time. |
6163b3ba RS |
432 | (progn |
433 | (setq abbrev dabbrev--last-abbreviation) | |
434 | (setq old dabbrev--last-expansion) | |
435 | (setq direction dabbrev--last-direction)) | |
a8a2d6ca KH |
436 | ;; If the user inserts a space after expanding |
437 | ;; and then asks to expand again, always fetch the next word. | |
438 | (if (and (eq (preceding-char) ?\ ) | |
439 | (markerp dabbrev--last-abbrev-location) | |
440 | (marker-position dabbrev--last-abbrev-location) | |
441 | (= (point) (1+ dabbrev--last-abbrev-location))) | |
442 | (progn | |
443 | ;; The "abbrev" to expand is just the space. | |
444 | (setq abbrev " ") | |
445 | (save-excursion | |
446 | (if dabbrev--last-buffer | |
447 | (set-buffer dabbrev--last-buffer)) | |
448 | ;; Find the end of the last "expansion" word. | |
449 | (if (or (eq dabbrev--last-direction 1) | |
450 | (and (eq dabbrev--last-direction 0) | |
451 | (< dabbrev--last-expansion-location (point)))) | |
452 | (setq dabbrev--last-expansion-location | |
453 | (+ dabbrev--last-expansion-location | |
454 | (length dabbrev--last-expansion)))) | |
455 | (goto-char dabbrev--last-expansion-location) | |
456 | ;; Take the following word, with intermediate separators, | |
457 | ;; as our expansion this time. | |
458 | (re-search-forward | |
459 | (concat "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)")) | |
460 | (setq expansion | |
461 | (buffer-substring dabbrev--last-expansion-location | |
462 | (point))) | |
463 | ||
464 | ;; Record the end of this expansion, in case we repeat this. | |
465 | (setq dabbrev--last-expansion-location (point))) | |
466 | ;; Indicate that dabbrev--last-expansion-location is | |
467 | ;; at the end of the expansion. | |
468 | (setq dabbrev--last-direction -1)) | |
469 | ||
470 | ;; We have a different abbrev to expand. | |
471 | (dabbrev--reset-global-variables) | |
472 | (setq direction (if (null arg) | |
473 | (if dabbrev-backward-only 1 0) | |
474 | (prefix-numeric-value arg))) | |
475 | (setq abbrev (dabbrev--abbrev-at-point)) | |
476 | (setq old nil))) | |
6163b3ba RS |
477 | |
478 | ;;-------------------------------- | |
479 | ;; Find the expansion | |
480 | ;;-------------------------------- | |
a8a2d6ca KH |
481 | (or expansion |
482 | (setq expansion | |
483 | (dabbrev--find-expansion abbrev direction | |
484 | (and (eval dabbrev-case-fold-search) | |
485 | (or (not dabbrev-upcase-means-case-search) | |
486 | (string= abbrev (downcase abbrev)))))))) | |
6163b3ba RS |
487 | (cond |
488 | ((not expansion) | |
489 | (dabbrev--reset-global-variables) | |
490 | (if old | |
491 | (save-excursion | |
492 | (search-backward (substring old (length abbrev))) | |
493 | (delete-region (match-beginning 0) (match-end 0)))) | |
7313ccdb | 494 | (error "No%s dynamic expansion for `%s' found" |
6163b3ba RS |
495 | (if old " further" "") abbrev)) |
496 | (t | |
497 | (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found)) | |
2f790b20 | 498 | (progn |
6163b3ba RS |
499 | (message "Expansion found in '%s'" |
500 | (buffer-name dabbrev--last-buffer)) | |
501 | (setq dabbrev--last-buffer-found dabbrev--last-buffer)) | |
502 | (message nil)) | |
a8a2d6ca KH |
503 | (if (and (or (eq (current-buffer) dabbrev--last-buffer) |
504 | (null dabbrev--last-buffer)) | |
505 | (numberp dabbrev--last-expansion-location) | |
506 | (and (> dabbrev--last-expansion-location (point)))) | |
507 | (setq dabbrev--last-expansion-location | |
508 | (copy-marker dabbrev--last-expansion-location))) | |
2f790b20 | 509 | ;; Success: stick it in and return. |
6163b3ba | 510 | (dabbrev--substitute-expansion old abbrev expansion) |
2f790b20 | 511 | ;; Save state for re-expand. |
6163b3ba RS |
512 | (setq dabbrev--last-expansion expansion) |
513 | (setq dabbrev--last-abbreviation abbrev) | |
514 | (setq dabbrev--last-abbrev-location (point-marker)))))) | |
515 | ||
6163b3ba RS |
516 | ;;;---------------------------------------------------------------- |
517 | ;;;---------------------------------------------------------------- | |
518 | ;;; Local functions | |
519 | ;;;---------------------------------------------------------------- | |
520 | ;;;---------------------------------------------------------------- | |
521 | ||
6163b3ba RS |
522 | ;;; Checks if OTHER-BUFFER has the same major mode as current buffer. |
523 | (defun dabbrev--same-major-mode-p (other-buffer) | |
7313ccdb RS |
524 | (eq major-mode |
525 | (save-excursion | |
526 | (set-buffer other-buffer) | |
527 | major-mode))) | |
6163b3ba RS |
528 | |
529 | ;;; Back over all abbrev type characters and then moves forward over | |
530 | ;;; all skip characters. | |
531 | (defun dabbrev--goto-start-of-abbrev () | |
532 | ;; Move backwards over abbrev chars | |
533 | (save-match-data | |
7313ccdb RS |
534 | (if (not (bobp)) |
535 | (progn | |
536 | (forward-char -1) | |
537 | (while (and (looking-at dabbrev--abbrev-char-regexp) | |
538 | (not (bobp))) | |
539 | (forward-char -1)) | |
540 | (or (looking-at dabbrev--abbrev-char-regexp) | |
541 | (forward-char 1)))) | |
6163b3ba RS |
542 | (and dabbrev-abbrev-skip-leading-regexp |
543 | (while (looking-at dabbrev-abbrev-skip-leading-regexp) | |
544 | (forward-char 1))))) | |
545 | ||
7313ccdb | 546 | ;;; Extract the symbol at point to serve as abbreviation. |
6163b3ba RS |
547 | (defun dabbrev--abbrev-at-point () |
548 | ;; Check for error | |
a8a2d6ca KH |
549 | (if (bobp) |
550 | (error "No possible abbreviation preceding point")) | |
6163b3ba RS |
551 | ;; Return abbrev at point |
552 | (save-excursion | |
a8a2d6ca | 553 | ;; Record the end of the abbreviation. |
6163b3ba | 554 | (setq dabbrev--last-abbrev-location (point)) |
a8a2d6ca KH |
555 | ;; If we aren't right after an abbreviation, |
556 | ;; move point back to just after one. | |
557 | ;; This is so the user can get successive words | |
558 | ;; by typing the punctuation followed by M-/. | |
559 | (save-match-data | |
560 | (if (save-excursion | |
561 | (forward-char -1) | |
562 | (not (looking-at (concat "\\(" | |
563 | (or dabbrev-abbrev-char-regexp | |
564 | "\\sw\\|\\s_") | |
565 | "\\)+")))) | |
566 | (if (re-search-backward (or dabbrev-abbrev-char-regexp | |
567 | "\\sw\\|\\s_") | |
568 | nil t) | |
569 | (forward-char 1) | |
570 | (error "No possible abbreviation preceding point")))) | |
571 | ;; Now find the beginning of that one. | |
572 | (dabbrev--goto-start-of-abbrev) | |
573 | (buffer-substring dabbrev--last-abbrev-location | |
574 | (point)))) | |
6163b3ba RS |
575 | |
576 | ;;; Initializes all global variables | |
577 | (defun dabbrev--reset-global-variables () | |
578 | ;; dabbrev--last-obarray and dabbrev--last-completion-buffer | |
579 | ;; must not be reset here. | |
580 | (setq dabbrev--last-table nil | |
581 | dabbrev--last-abbreviation nil | |
582 | dabbrev--last-abbrev-location nil | |
583 | dabbrev--last-direction nil | |
584 | dabbrev--last-expansion nil | |
585 | dabbrev--last-expansion-location nil | |
586 | dabbrev--friend-buffer-list nil | |
587 | dabbrev--last-buffer nil | |
588 | dabbrev--last-buffer-found nil | |
589 | dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp | |
590 | "\\sw\\|\\s_") | |
df6eb420 | 591 | dabbrev--check-other-buffers dabbrev-check-other-buffers)) |
6163b3ba RS |
592 | |
593 | ;;; Find all buffers that are considered "friends" according to the | |
594 | ;;; function pointed out by dabbrev-friend-buffer-function. | |
595 | (defun dabbrev--select-buffers () | |
596 | (save-excursion | |
597 | (and (window-minibuffer-p (selected-window)) | |
598 | (set-buffer (dabbrev--minibuffer-origin))) | |
599 | (let ((orig-buffer (current-buffer))) | |
7313ccdb RS |
600 | (dabbrev-filter-elements |
601 | buffer (buffer-list) | |
602 | (and (not (eq orig-buffer buffer)) | |
603 | (boundp 'dabbrev-friend-buffer-function) | |
604 | (funcall dabbrev-friend-buffer-function buffer)))))) | |
605 | ||
606 | ;;; Search for ABBREV, N times, normally looking forward, | |
607 | ;;; but looking in reverse instead if REVERSE is non-nil. | |
6163b3ba RS |
608 | (defun dabbrev--try-find (abbrev reverse n ignore-case) |
609 | (save-excursion | |
df6eb420 RS |
610 | (save-restriction |
611 | (widen) | |
612 | (let ((expansion nil)) | |
613 | (and dabbrev--last-expansion-location | |
614 | (goto-char dabbrev--last-expansion-location)) | |
615 | (let ((case-fold-search ignore-case) | |
616 | (count n)) | |
617 | (while (and (> count 0) | |
618 | (setq expansion (dabbrev--search abbrev | |
619 | reverse | |
620 | ignore-case))) | |
621 | (setq count (1- count)))) | |
622 | (and expansion | |
623 | (setq dabbrev--last-expansion-location (point))) | |
624 | expansion)))) | |
6163b3ba RS |
625 | |
626 | ;;; Find all expansions of ABBREV | |
627 | (defun dabbrev--find-all-expansions (abbrev ignore-case) | |
628 | (let ((all-expansions nil) | |
629 | expansion) | |
630 | (save-excursion | |
631 | (goto-char (point-min)) | |
632 | (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case)) | |
df6eb420 | 633 | (setq all-expansions (cons expansion all-expansions)))) |
6163b3ba RS |
634 | all-expansions)) |
635 | ||
636 | (defun dabbrev--scanning-message () | |
7313ccdb | 637 | (message "Scanning `%s'" (buffer-name (current-buffer)))) |
6163b3ba RS |
638 | |
639 | ;;; Find one occasion of ABBREV. | |
640 | ;;; DIRECTION > 0 means look that many times backwards. | |
641 | ;;; DIRECTION < 0 means look that many times forward. | |
642 | ;;; DIRECTION = 0 means try both backward and forward. | |
643 | ;;; IGNORE-CASE non-nil means ignore case when searching. | |
644 | (defun dabbrev--find-expansion (abbrev direction ignore-case) | |
645 | (let (expansion) | |
646 | (save-excursion | |
647 | (cond | |
648 | (dabbrev--last-buffer | |
649 | (set-buffer dabbrev--last-buffer) | |
650 | (dabbrev--scanning-message)) | |
651 | ((and (not dabbrev-search-these-buffers-only) | |
652 | (window-minibuffer-p (selected-window))) | |
653 | (set-buffer (dabbrev--minibuffer-origin)) | |
654 | ;; In the minibuffer-origin buffer we will only search from | |
655 | ;; the top and down. | |
656 | (goto-char (point-min)) | |
657 | (setq direction -1) | |
658 | (dabbrev--scanning-message))) | |
659 | (cond | |
660 | ;; ------------------------------------------ | |
661 | ;; Look backwards | |
662 | ;; ------------------------------------------ | |
663 | ((and (not dabbrev-search-these-buffers-only) | |
664 | (>= direction 0) | |
665 | (setq dabbrev--last-direction (min 1 direction)) | |
666 | (setq expansion (dabbrev--try-find abbrev t | |
667 | (max 1 direction) | |
668 | ignore-case))) | |
669 | expansion) | |
670 | ;; ------------------------------------------ | |
671 | ;; Look forward | |
672 | ;; ------------------------------------------ | |
673 | ((and (or (not dabbrev-search-these-buffers-only) | |
674 | dabbrev--last-buffer) | |
675 | (<= direction 0) | |
676 | (setq dabbrev--last-direction -1) | |
677 | (setq expansion (dabbrev--try-find abbrev nil | |
678 | (max 1 (- direction)) | |
679 | ignore-case))) | |
680 | expansion) | |
681 | ;; ------------------------------------------ | |
682 | ;; Look in other buffers. | |
683 | ;; Start at (point-min) and look forward. | |
684 | ;; ------------------------------------------ | |
685 | (t | |
686 | (setq dabbrev--last-direction -1) | |
687 | ;; Make sure that we should check other buffers | |
688 | (or dabbrev--friend-buffer-list | |
689 | dabbrev--last-buffer | |
690 | (setq dabbrev--friend-buffer-list | |
691 | (mapcar (function get-buffer) | |
692 | dabbrev-search-these-buffers-only)) | |
693 | (not dabbrev--check-other-buffers) | |
694 | (not (or (eq dabbrev--check-other-buffers t) | |
695 | (progn | |
696 | (setq dabbrev--check-other-buffers | |
7313ccdb | 697 | (y-or-n-p "Scan other buffers also? "))))) |
6163b3ba RS |
698 | (let* (friend-buffer-list non-friend-buffer-list) |
699 | (setq dabbrev--friend-buffer-list | |
700 | (funcall dabbrev-select-buffers-function)) | |
df6eb420 | 701 | (if dabbrev-check-all-buffers |
7313ccdb RS |
702 | (setq non-friend-buffer-list |
703 | (nreverse | |
704 | (dabbrev-filter-elements | |
705 | buffer (buffer-list) | |
706 | (not (memq buffer dabbrev--friend-buffer-list)))) | |
707 | dabbrev--friend-buffer-list | |
708 | (append dabbrev--friend-buffer-list | |
709 | non-friend-buffer-list))))) | |
6163b3ba RS |
710 | ;; Walk through the buffers |
711 | (while (and (not expansion) dabbrev--friend-buffer-list) | |
712 | (setq dabbrev--last-buffer | |
713 | (car dabbrev--friend-buffer-list)) | |
714 | (setq dabbrev--friend-buffer-list | |
715 | (cdr dabbrev--friend-buffer-list)) | |
716 | (set-buffer dabbrev--last-buffer) | |
717 | (dabbrev--scanning-message) | |
718 | (setq dabbrev--last-expansion-location (point-min)) | |
719 | (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case))) | |
720 | expansion))))) | |
721 | ||
6163b3ba RS |
722 | (defun dabbrev--safe-replace-match (string &optional fixedcase literal) |
723 | (if (eq major-mode 'picture-mode) | |
724 | (picture-replace-match string fixedcase literal) | |
725 | (replace-match string fixedcase literal))) | |
726 | ||
727 | ;;;---------------------------------------------------------------- | |
728 | ;;; Substitute the current string in buffer with the expansion | |
729 | ;;; OLD is nil or the last expansion substring. | |
730 | ;;; ABBREV is the abbreviation we are working with. | |
731 | ;;; EXPANSION is the expansion substring. | |
732 | (defun dabbrev--substitute-expansion (old abbrev expansion) | |
733 | ;;(undo-boundary) | |
734 | (let ((use-case-replace (and (eval dabbrev-case-fold-search) | |
735 | (or (not dabbrev-upcase-means-case-search) | |
736 | (string= abbrev (downcase abbrev))) | |
737 | (eval dabbrev-case-replace)))) | |
738 | (and nil use-case-replace | |
739 | (setq old (concat abbrev (or old ""))) | |
740 | (setq expansion (concat abbrev expansion))) | |
741 | (if old | |
742 | (save-excursion | |
743 | (search-backward old)) | |
744 | ;;(store-match-data (list (point-marker) (point-marker))) | |
745 | (search-backward abbrev)) | |
746 | ;; Make case of replacement conform to case of abbreviation | |
747 | ;; provided (1) that kind of thing is enabled in this buffer | |
748 | ;; and (2) the replacement itself is all lower case. | |
749 | (dabbrev--safe-replace-match expansion | |
750 | (not use-case-replace) | |
751 | t))) | |
752 | ||
753 | ||
754 | ;;;---------------------------------------------------------------- | |
755 | ;;; Search function used by dabbrevs library. | |
756 | ||
7313ccdb | 757 | ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE, |
6163b3ba RS |
758 | ;;; is t for reverse search, nil for forward. Variable dabbrev-limit |
759 | ;;; controls the maximum search region size. Third argment IGNORE-CASE | |
760 | ;;; non-nil means treat case as insignificant while looking for a match | |
761 | ;;; and when comparing with previous matches. Also if that's non-nil | |
762 | ;;; and the match is found at the beginning of a sentence and is in | |
763 | ;;; lower case except for the initial then it is converted to all lower | |
764 | ;;; case for return. | |
765 | ||
766 | ;;; Table of expansions already seen is examined in buffer | |
767 | ;;; `dabbrev--last-table' so that only distinct possibilities are found | |
768 | ;;; by dabbrev-re-expand. | |
769 | ||
770 | ;;; Value is the expansion, or nil if not found. | |
771 | ||
772 | (defun dabbrev--search (abbrev reverse ignore-case) | |
773 | (save-match-data | |
774 | (let ((pattern1 (concat (regexp-quote abbrev) | |
775 | "\\(" dabbrev--abbrev-char-regexp "\\)")) | |
776 | (pattern2 (concat (regexp-quote abbrev) | |
777 | "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)")) | |
778 | (found-string nil)) | |
779 | ;; Limited search. | |
780 | (save-restriction | |
781 | (and dabbrev-limit | |
782 | (narrow-to-region dabbrev--last-expansion-location | |
783 | (+ (point) | |
784 | (if reverse (- dabbrev-limit) dabbrev-limit)))) | |
785 | ;;-------------------------------- | |
786 | ;; Look for a distinct expansion, using dabbrev--last-table. | |
787 | ;;-------------------------------- | |
788 | (while (and (not found-string) | |
789 | (if reverse | |
790 | (re-search-backward pattern1 nil t) | |
791 | (re-search-forward pattern1 nil t))) | |
792 | (cond | |
793 | ((progn | |
794 | (goto-char (match-beginning 0)) | |
795 | (dabbrev--goto-start-of-abbrev) | |
796 | (/= (point) (match-beginning 0))) | |
797 | ;; Prefix of found abbreviation not OK | |
798 | nil) | |
799 | (t | |
800 | (goto-char (match-beginning 0)) | |
801 | (re-search-forward pattern2) | |
802 | (setq found-string | |
803 | (buffer-substring (match-beginning 1) (match-end 1))) | |
804 | (and ignore-case (setq found-string (downcase found-string))) | |
805 | ;; Throw away if found in table | |
7313ccdb RS |
806 | (if (dabbrev-filter-elements |
807 | table-string dabbrev--last-table | |
808 | (string= found-string table-string)) | |
809 | (setq found-string nil)))) | |
6163b3ba RS |
810 | (if reverse |
811 | (goto-char (match-beginning 0)) | |
812 | (goto-char (match-end 0)))) | |
813 | (cond | |
814 | (found-string | |
815 | ;;-------------------------------- | |
816 | ;; Put in `dabbrev--last-table' and decide if we should return | |
817 | ;; result or (downcase result) | |
818 | ;;-------------------------------- | |
df6eb420 | 819 | (setq dabbrev--last-table (cons found-string dabbrev--last-table)) |
6163b3ba RS |
820 | (let ((result (buffer-substring (match-beginning 0) (match-end 0)))) |
821 | (if (and ignore-case (eval dabbrev-case-replace)) | |
822 | (downcase result) | |
823 | result)))))))) | |
824 | ||
01987a6b | 825 | (provide 'dabbrev) |
7313ccdb RS |
826 | |
827 | ;; dabbrev.el ends here | |
6163b3ba | 828 | |
49116ac0 | 829 |