Commit | Line | Data |
---|---|---|
7313ccdb | 1 | ;;; dabbrev.el --- dynamic abbreviation package |
b578f267 | 2 | |
7313ccdb | 3 | ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc. |
2f790b20 | 4 | |
6163b3ba RS |
5 | ;; Author: Don Morrison |
6 | ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se> | |
7 | ;; Created: 16 Mars 1992 | |
df6eb420 | 8 | ;; Lindberg's last update version: 5.7 |
6163b3ba | 9 | ;; Keywords: abbrev expand completion |
3a801d0c | 10 | |
b578f267 EN |
11 | ;; This file is part of GNU Emacs. |
12 | ||
13 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
2f790b20 | 14 | ;; it under the terms of the GNU General Public License as published by |
b578f267 EN |
15 | ;; the Free Software Foundation; either version 2, or (at your option) |
16 | ;; any later version. | |
17 | ||
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
2f790b20 JB |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
b578f267 | 22 | |
2f790b20 | 23 | ;; You should have received a copy of the GNU General Public License |
b578f267 EN |
24 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
25 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
26 | ;; Boston, MA 02111-1307, USA. | |
2f790b20 | 27 | |
e5167999 | 28 | ;;; Commentary: |
2f790b20 | 29 | |
6163b3ba RS |
30 | ;; The purpose with this package is to let you write just a few |
31 | ;; characters of words you've written earlier to be able to expand | |
32 | ;; them. | |
33 | ;; | |
34 | ;; To expand a word, just put the point right after the word and press | |
35 | ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion). | |
36 | ;; | |
6163b3ba RS |
37 | ;; Check out the customizable variables below to learn about all the |
38 | ;; features of this package. | |
39 | ||
40 | ;;; Hints and tips for major modes writers: | |
41 | ||
42 | ;; Recommended values C/Lisp etc text | |
43 | ;; dabbrev-case-fold-search nil t | |
44 | ;; dabbrev-case-replace nil t | |
45 | ;; | |
46 | ;; Set the variables you want special for your mode like this: | |
47 | ;; (set (make-local-variable 'dabbrev-case-replace) nil) | |
a7acbbe4 | 48 | ;; Then you don't interfere with other modes. |
6163b3ba RS |
49 | ;; |
50 | ;; If your mode handles buffers that refers to other buffers | |
51 | ;; (i.e. compilation-mode, gud-mode), then try to set | |
52 | ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function' | |
53 | ;; to a function that point out those buffers. | |
54 | ||
55 | ;; Same goes for major-modes that are connected to other modes. There | |
56 | ;; are for instance a number of mail-modes. One for reading, one for | |
57 | ;; creating a new mail etc. Maybe those should be connected. | |
58 | ||
59 | ;; Example for GNUS (when we write a reply, we want dabbrev to look in | |
60 | ;; the article for expansion): | |
61 | ;; (set (make-local-variable 'dabbrev-friend-buffer-function) | |
62 | ;; (lambda (buffer) | |
63 | ;; (save-excursion | |
64 | ;; (set-buffer buffer) | |
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> | |
91 | ;; [ejb] Jay Berkenbilt <ejb@ERA.COM> | |
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 | (defvar dabbrev-backward-only nil |
7313ccdb | 101 | "*If non-nil, `dabbrev-expand' only looks backwards.") |
6163b3ba RS |
102 | |
103 | (defvar dabbrev-limit nil | |
104 | "*Limits region searched by `dabbrev-expand' to this many chars away.") | |
105 | ||
106 | (defvar dabbrev-abbrev-skip-leading-regexp nil | |
107 | "*Regexp for skipping leading characters of an abbreviation. | |
108 | ||
7313ccdb RS |
109 | Example: Set this to \"\\\\$\" for programming languages |
110 | in which variable names may appear with or without a leading `$'. | |
bfbf16af | 111 | \(For example, in Makefiles.) |
6163b3ba RS |
112 | |
113 | Set this to nil if no characters should be skipped.") | |
114 | ||
115 | ;; I recommend that you set this to nil. | |
116 | (defvar dabbrev-case-fold-search 'case-fold-search | |
7313ccdb RS |
117 | "*Non-nil if dabbrev searches should ignore case. |
118 | A value of nil means case is significant. | |
6163b3ba | 119 | |
7313ccdb RS |
120 | The value of this variable is an expression; it is evaluated |
121 | and the resulting value determines the decision. | |
122 | For example: setting this to `case-fold-search' means evaluate that | |
123 | variable to see whether its value is nil.") | |
8d47e7e5 | 124 | (put 'dabbrev-case-fold-search 'risky-local-variable t) |
6163b3ba RS |
125 | |
126 | (defvar dabbrev-upcase-means-case-search nil | |
127 | "*The significance of an uppercase character in an abbreviation. | |
7313ccdb | 128 | nil means case fold search, non-nil means case sensitive search. |
6163b3ba | 129 | |
7313ccdb RS |
130 | This variable has an effect only when the value of |
131 | `dabbrev-case-fold-search' evaluates to t.") | |
6163b3ba RS |
132 | |
133 | ;; I recommend that you set this to nil. | |
134 | (defvar dabbrev-case-replace 'case-replace | |
7313ccdb | 135 | "*Non-nil means dabbrev should preserve case when expanding the abbreviation. |
83f80458 RS |
136 | More precisely, it preserves the case pattern of the abbreviation as you |
137 | typed it--as opposed to the case pattern of the expansion that is copied. | |
7313ccdb RS |
138 | The value of this variable is an expression; it is evaluated |
139 | and the resulting value determines the decision. | |
140 | For example, setting this to `case-replace' means evaluate that | |
141 | variable to see if its value is t or nil. | |
6163b3ba | 142 | |
7313ccdb RS |
143 | This variable has an effect only when the value of |
144 | `dabbrev-case-fold-search' evaluates to t.") | |
8d47e7e5 | 145 | (put 'dabbrev-case-replace 'risky-local-variable t) |
6163b3ba | 146 | |
df6eb420 | 147 | (defvar dabbrev-abbrev-char-regexp nil |
7313ccdb RS |
148 | "*Regexp to recognize a character in an abbreviation or expansion. |
149 | This regexp will be surrounded with \\\\( ... \\\\) when actually used. | |
6163b3ba | 150 | |
7313ccdb | 151 | Set this variable to \"\\\\sw\" if you want ordinary words or |
df6eb420 RS |
152 | \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose |
153 | syntax is \"symbol\" as well as those whose syntax is \"word\". | |
6163b3ba | 154 | |
df6eb420 RS |
155 | The value nil has a special meaning: the abbreviation is from point to |
156 | previous word-start, but the search is for symbols. | |
6163b3ba | 157 | |
7313ccdb | 158 | For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol, |
df6eb420 RS |
159 | while `yes', `or', `no' and `p' are considered words. If this |
160 | variable is nil, then expanding `yes-or-no-' looks for a symbol | |
7313ccdb RS |
161 | starting with or containing `no-'. If you set this variable to |
162 | \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with | |
163 | `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then | |
164 | expanding `yes-or-no-' signals an error because `-' is not part of a word; | |
165 | but expanding `yes-or-no' looks for a word starting with `no'. | |
6163b3ba RS |
166 | |
167 | The recommended value is \"\\\\sw\\\\|\\\\s_\".") | |
168 | ||
df6eb420 RS |
169 | (defvar dabbrev-check-all-buffers t |
170 | "*Non-nil means dabbrev package should search *all* buffers. | |
6163b3ba | 171 | |
df6eb420 RS |
172 | Dabbrev always searches the current buffer first. Then, if |
173 | `dabbrev-check-other-buffers' says so, it searches the buffers | |
174 | designated by `dabbrev-select-buffers-function'. | |
6163b3ba | 175 | |
df6eb420 RS |
176 | Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches |
177 | all the other buffers.") | |
178 | ||
179 | (defvar dabbrev-check-other-buffers t | |
6163b3ba | 180 | "*Should \\[dabbrev-expand] look in other buffers?\ |
6163b3ba | 181 | |
df6eb420 RS |
182 | nil: Don't look in other buffers. |
183 | t: Also look for expansions in the buffers pointed out by | |
184 | `dabbrev-select-buffers-function'. | |
185 | Anything else: When we can't find any more expansions in | |
186 | the current buffer, then ask the user whether to look in other | |
187 | buffers too. | |
188 | ||
189 | The default value is t.") | |
6163b3ba RS |
190 | |
191 | ;; I guess setting this to a function that selects all C- or C++- | |
192 | ;; mode buffers would be a good choice for a debugging buffer, | |
193 | ;; when debugging C- or C++-code. | |
194 | (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers | |
195 | "A function that selects buffers that should be searched by dabbrev. | |
6163b3ba RS |
196 | The function should take no arguments and return a list of buffers to |
197 | search for expansions. Have a look at `dabbrev--select-buffers' for | |
198 | an example. | |
199 | ||
200 | A mode setting this variable should make it buffer local.") | |
201 | ||
202 | (defvar dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p | |
df6eb420 | 203 | "*A function to decide whether dabbrev should search OTHER-BUFFER. |
6163b3ba RS |
204 | The function should take one argument, OTHER-BUFFER, and return |
205 | non-nil if that buffer should be searched. Have a look at | |
206 | `dabbrev--same-major-mode-p' for an example. | |
207 | ||
7313ccdb RS |
208 | The value of `dabbrev-friend-buffer-function' has an effect only if |
209 | the value of `dabbrev-select-buffers-function' uses it. The function | |
210 | `dabbrev--select-buffers' is one function you can use here. | |
6163b3ba RS |
211 | |
212 | A mode setting this variable should make it buffer local.") | |
213 | ||
214 | (defvar dabbrev-search-these-buffers-only nil | |
7313ccdb RS |
215 | "If non-nil, a list of buffers which dabbrev should search. |
216 | If this variable is non-nil, dabbrev will only look in these buffers. | |
217 | It will not even look in the current buffer if it is not a member of | |
218 | this list.") | |
e5167999 | 219 | |
b578f267 EN |
220 | ;;---------------------------------------------------------------- |
221 | ;; Internal variables | |
222 | ;;---------------------------------------------------------------- | |
2f790b20 | 223 | |
6163b3ba RS |
224 | ;; Last obarray of completions in `dabbrev-completion' |
225 | (defvar dabbrev--last-obarray nil) | |
2f790b20 | 226 | |
6163b3ba RS |
227 | ;; Table of expansions seen so far |
228 | (defvar dabbrev--last-table nil) | |
2f790b20 | 229 | |
6163b3ba RS |
230 | ;; Last string we tried to expand. |
231 | (defvar dabbrev--last-abbreviation nil) | |
2f790b20 | 232 | |
6163b3ba RS |
233 | ;; Location last abbreviation began |
234 | (defvar dabbrev--last-abbrev-location nil) | |
2f790b20 | 235 | |
6163b3ba RS |
236 | ;; Direction of last dabbrevs search |
237 | (defvar dabbrev--last-direction 0) | |
2f790b20 | 238 | |
6163b3ba RS |
239 | ;; Last expansion of an abbreviation. |
240 | (defvar dabbrev--last-expansion nil) | |
2f790b20 | 241 | |
6163b3ba RS |
242 | ;; Location the last expansion was found. |
243 | (defvar dabbrev--last-expansion-location nil) | |
244 | ||
245 | ;; The list of remaining buffers with the same mode as current buffer. | |
246 | (defvar dabbrev--friend-buffer-list nil) | |
247 | ||
248 | ;; The buffer we looked in last. | |
249 | (defvar dabbrev--last-buffer nil) | |
250 | ||
251 | ;; The buffer we found the expansion last time. | |
252 | (defvar dabbrev--last-buffer-found nil) | |
253 | ||
254 | ;; The buffer we last did a completion in. | |
255 | (defvar dabbrev--last-completion-buffer nil) | |
256 | ||
df6eb420 RS |
257 | ;; Same as dabbrev-check-other-buffers, but is set for every expand. |
258 | (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers) | |
6163b3ba RS |
259 | |
260 | ;; The regexp for recognizing a character in an abbreviation. | |
261 | (defvar dabbrev--abbrev-char-regexp nil) | |
262 | ||
b578f267 EN |
263 | ;;---------------------------------------------------------------- |
264 | ;; Macros | |
265 | ;;---------------------------------------------------------------- | |
6163b3ba RS |
266 | |
267 | ;;; Get the buffer that mini-buffer was activated from | |
268 | (defsubst dabbrev--minibuffer-origin () | |
269 | (car (cdr (buffer-list)))) | |
270 | ||
7313ccdb RS |
271 | ;; Make a list of some of the elements of LIST. |
272 | ;; Check each element of LIST, storing it temporarily in the | |
273 | ;; variable ELEMENT, and include it in the result | |
274 | ;; if CONDITION evaluates non-nil. | |
275 | (defmacro dabbrev-filter-elements (element list condition) | |
276 | (` (let (dabbrev-result dabbrev-tail (, element)) | |
277 | (setq dabbrev-tail (, list)) | |
278 | (while dabbrev-tail | |
279 | (setq (, element) (car dabbrev-tail)) | |
280 | (if (, condition) | |
281 | (setq dabbrev-result (cons (, element) dabbrev-result))) | |
282 | (setq dabbrev-tail (cdr dabbrev-tail))) | |
283 | (nreverse dabbrev-result)))) | |
284 | ||
b578f267 EN |
285 | ;;---------------------------------------------------------------- |
286 | ;; Exported functions | |
287 | ;;---------------------------------------------------------------- | |
6163b3ba RS |
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") |
4209f479 | 418 | (let (abbrev expansion old direction (orig-point (point))) |
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 | |
4209f479 | 492 | (setq buffer-undo-list (cons orig-point buffer-undo-list)) |
3132e115 RS |
493 | ;; Put back the original abbrev with its original case pattern. |
494 | (search-backward old) | |
495 | (insert abbrev) | |
496 | (delete-region (point) (+ (point) (length old))))) | |
7313ccdb | 497 | (error "No%s dynamic expansion for `%s' found" |
6163b3ba RS |
498 | (if old " further" "") abbrev)) |
499 | (t | |
500 | (if (not (eq dabbrev--last-buffer dabbrev--last-buffer-found)) | |
2f790b20 | 501 | (progn |
6163b3ba RS |
502 | (message "Expansion found in '%s'" |
503 | (buffer-name dabbrev--last-buffer)) | |
504 | (setq dabbrev--last-buffer-found dabbrev--last-buffer)) | |
505 | (message nil)) | |
a8a2d6ca KH |
506 | (if (and (or (eq (current-buffer) dabbrev--last-buffer) |
507 | (null dabbrev--last-buffer)) | |
508 | (numberp dabbrev--last-expansion-location) | |
509 | (and (> dabbrev--last-expansion-location (point)))) | |
510 | (setq dabbrev--last-expansion-location | |
511 | (copy-marker dabbrev--last-expansion-location))) | |
2f790b20 | 512 | ;; Success: stick it in and return. |
4209f479 | 513 | (setq buffer-undo-list (cons orig-point buffer-undo-list)) |
6163b3ba | 514 | (dabbrev--substitute-expansion old abbrev expansion) |
2f790b20 | 515 | ;; Save state for re-expand. |
6163b3ba RS |
516 | (setq dabbrev--last-expansion expansion) |
517 | (setq dabbrev--last-abbreviation abbrev) | |
518 | (setq dabbrev--last-abbrev-location (point-marker)))))) | |
519 | ||
b578f267 EN |
520 | ;;---------------------------------------------------------------- |
521 | ;; Local functions | |
522 | ;;---------------------------------------------------------------- | |
6163b3ba | 523 | |
6163b3ba RS |
524 | ;;; Checks if OTHER-BUFFER has the same major mode as current buffer. |
525 | (defun dabbrev--same-major-mode-p (other-buffer) | |
7313ccdb RS |
526 | (eq major-mode |
527 | (save-excursion | |
528 | (set-buffer other-buffer) | |
529 | major-mode))) | |
6163b3ba RS |
530 | |
531 | ;;; Back over all abbrev type characters and then moves forward over | |
532 | ;;; all skip characters. | |
533 | (defun dabbrev--goto-start-of-abbrev () | |
534 | ;; Move backwards over abbrev chars | |
535 | (save-match-data | |
7313ccdb RS |
536 | (if (not (bobp)) |
537 | (progn | |
538 | (forward-char -1) | |
539 | (while (and (looking-at dabbrev--abbrev-char-regexp) | |
540 | (not (bobp))) | |
541 | (forward-char -1)) | |
542 | (or (looking-at dabbrev--abbrev-char-regexp) | |
543 | (forward-char 1)))) | |
6163b3ba RS |
544 | (and dabbrev-abbrev-skip-leading-regexp |
545 | (while (looking-at dabbrev-abbrev-skip-leading-regexp) | |
546 | (forward-char 1))))) | |
547 | ||
7313ccdb | 548 | ;;; Extract the symbol at point to serve as abbreviation. |
6163b3ba RS |
549 | (defun dabbrev--abbrev-at-point () |
550 | ;; Check for error | |
a8a2d6ca KH |
551 | (if (bobp) |
552 | (error "No possible abbreviation preceding point")) | |
6163b3ba RS |
553 | ;; Return abbrev at point |
554 | (save-excursion | |
a8a2d6ca | 555 | ;; Record the end of the abbreviation. |
6163b3ba | 556 | (setq dabbrev--last-abbrev-location (point)) |
a8a2d6ca KH |
557 | ;; If we aren't right after an abbreviation, |
558 | ;; move point back to just after one. | |
559 | ;; This is so the user can get successive words | |
560 | ;; by typing the punctuation followed by M-/. | |
561 | (save-match-data | |
562 | (if (save-excursion | |
563 | (forward-char -1) | |
564 | (not (looking-at (concat "\\(" | |
565 | (or dabbrev-abbrev-char-regexp | |
566 | "\\sw\\|\\s_") | |
567 | "\\)+")))) | |
568 | (if (re-search-backward (or dabbrev-abbrev-char-regexp | |
569 | "\\sw\\|\\s_") | |
570 | nil t) | |
571 | (forward-char 1) | |
572 | (error "No possible abbreviation preceding point")))) | |
573 | ;; Now find the beginning of that one. | |
574 | (dabbrev--goto-start-of-abbrev) | |
575 | (buffer-substring dabbrev--last-abbrev-location | |
576 | (point)))) | |
6163b3ba RS |
577 | |
578 | ;;; Initializes all global variables | |
579 | (defun dabbrev--reset-global-variables () | |
580 | ;; dabbrev--last-obarray and dabbrev--last-completion-buffer | |
581 | ;; must not be reset here. | |
582 | (setq dabbrev--last-table nil | |
583 | dabbrev--last-abbreviation nil | |
584 | dabbrev--last-abbrev-location nil | |
585 | dabbrev--last-direction nil | |
586 | dabbrev--last-expansion nil | |
587 | dabbrev--last-expansion-location nil | |
588 | dabbrev--friend-buffer-list nil | |
589 | dabbrev--last-buffer nil | |
590 | dabbrev--last-buffer-found nil | |
591 | dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp | |
592 | "\\sw\\|\\s_") | |
df6eb420 | 593 | dabbrev--check-other-buffers dabbrev-check-other-buffers)) |
6163b3ba RS |
594 | |
595 | ;;; Find all buffers that are considered "friends" according to the | |
596 | ;;; function pointed out by dabbrev-friend-buffer-function. | |
597 | (defun dabbrev--select-buffers () | |
598 | (save-excursion | |
599 | (and (window-minibuffer-p (selected-window)) | |
600 | (set-buffer (dabbrev--minibuffer-origin))) | |
601 | (let ((orig-buffer (current-buffer))) | |
7313ccdb RS |
602 | (dabbrev-filter-elements |
603 | buffer (buffer-list) | |
604 | (and (not (eq orig-buffer buffer)) | |
605 | (boundp 'dabbrev-friend-buffer-function) | |
606 | (funcall dabbrev-friend-buffer-function buffer)))))) | |
607 | ||
608 | ;;; Search for ABBREV, N times, normally looking forward, | |
609 | ;;; but looking in reverse instead if REVERSE is non-nil. | |
6163b3ba RS |
610 | (defun dabbrev--try-find (abbrev reverse n ignore-case) |
611 | (save-excursion | |
df6eb420 RS |
612 | (save-restriction |
613 | (widen) | |
614 | (let ((expansion nil)) | |
615 | (and dabbrev--last-expansion-location | |
616 | (goto-char dabbrev--last-expansion-location)) | |
617 | (let ((case-fold-search ignore-case) | |
618 | (count n)) | |
619 | (while (and (> count 0) | |
620 | (setq expansion (dabbrev--search abbrev | |
621 | reverse | |
622 | ignore-case))) | |
623 | (setq count (1- count)))) | |
624 | (and expansion | |
625 | (setq dabbrev--last-expansion-location (point))) | |
626 | expansion)))) | |
6163b3ba RS |
627 | |
628 | ;;; Find all expansions of ABBREV | |
629 | (defun dabbrev--find-all-expansions (abbrev ignore-case) | |
630 | (let ((all-expansions nil) | |
631 | expansion) | |
632 | (save-excursion | |
633 | (goto-char (point-min)) | |
634 | (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case)) | |
df6eb420 | 635 | (setq all-expansions (cons expansion all-expansions)))) |
6163b3ba RS |
636 | all-expansions)) |
637 | ||
638 | (defun dabbrev--scanning-message () | |
7313ccdb | 639 | (message "Scanning `%s'" (buffer-name (current-buffer)))) |
6163b3ba RS |
640 | |
641 | ;;; Find one occasion of ABBREV. | |
642 | ;;; DIRECTION > 0 means look that many times backwards. | |
643 | ;;; DIRECTION < 0 means look that many times forward. | |
644 | ;;; DIRECTION = 0 means try both backward and forward. | |
645 | ;;; IGNORE-CASE non-nil means ignore case when searching. | |
646 | (defun dabbrev--find-expansion (abbrev direction ignore-case) | |
647 | (let (expansion) | |
648 | (save-excursion | |
649 | (cond | |
650 | (dabbrev--last-buffer | |
651 | (set-buffer dabbrev--last-buffer) | |
652 | (dabbrev--scanning-message)) | |
653 | ((and (not dabbrev-search-these-buffers-only) | |
654 | (window-minibuffer-p (selected-window))) | |
655 | (set-buffer (dabbrev--minibuffer-origin)) | |
656 | ;; In the minibuffer-origin buffer we will only search from | |
657 | ;; the top and down. | |
658 | (goto-char (point-min)) | |
659 | (setq direction -1) | |
660 | (dabbrev--scanning-message))) | |
661 | (cond | |
662 | ;; ------------------------------------------ | |
663 | ;; Look backwards | |
664 | ;; ------------------------------------------ | |
665 | ((and (not dabbrev-search-these-buffers-only) | |
666 | (>= direction 0) | |
667 | (setq dabbrev--last-direction (min 1 direction)) | |
668 | (setq expansion (dabbrev--try-find abbrev t | |
669 | (max 1 direction) | |
670 | ignore-case))) | |
671 | expansion) | |
672 | ;; ------------------------------------------ | |
673 | ;; Look forward | |
674 | ;; ------------------------------------------ | |
675 | ((and (or (not dabbrev-search-these-buffers-only) | |
676 | dabbrev--last-buffer) | |
677 | (<= direction 0) | |
678 | (setq dabbrev--last-direction -1) | |
679 | (setq expansion (dabbrev--try-find abbrev nil | |
680 | (max 1 (- direction)) | |
681 | ignore-case))) | |
682 | expansion) | |
683 | ;; ------------------------------------------ | |
684 | ;; Look in other buffers. | |
685 | ;; Start at (point-min) and look forward. | |
686 | ;; ------------------------------------------ | |
687 | (t | |
688 | (setq dabbrev--last-direction -1) | |
689 | ;; Make sure that we should check other buffers | |
690 | (or dabbrev--friend-buffer-list | |
691 | dabbrev--last-buffer | |
692 | (setq dabbrev--friend-buffer-list | |
693 | (mapcar (function get-buffer) | |
694 | dabbrev-search-these-buffers-only)) | |
695 | (not dabbrev--check-other-buffers) | |
696 | (not (or (eq dabbrev--check-other-buffers t) | |
697 | (progn | |
698 | (setq dabbrev--check-other-buffers | |
7313ccdb | 699 | (y-or-n-p "Scan other buffers also? "))))) |
6163b3ba RS |
700 | (let* (friend-buffer-list non-friend-buffer-list) |
701 | (setq dabbrev--friend-buffer-list | |
702 | (funcall dabbrev-select-buffers-function)) | |
df6eb420 | 703 | (if dabbrev-check-all-buffers |
7313ccdb RS |
704 | (setq non-friend-buffer-list |
705 | (nreverse | |
706 | (dabbrev-filter-elements | |
707 | buffer (buffer-list) | |
708 | (not (memq buffer dabbrev--friend-buffer-list)))) | |
709 | dabbrev--friend-buffer-list | |
710 | (append dabbrev--friend-buffer-list | |
711 | non-friend-buffer-list))))) | |
d8f7e85a RS |
712 | ;; Move buffers that are visible on the screen |
713 | ;; to the front of the list. | |
714 | (if dabbrev--friend-buffer-list | |
715 | (let ((w (next-window (selected-window)))) | |
716 | (while (not (eq w (selected-window))) | |
717 | (setq dabbrev--friend-buffer-list | |
718 | (cons (window-buffer w) | |
719 | (delq (window-buffer w) dabbrev--friend-buffer-list))) | |
720 | (setq w (next-window w))))) | |
6163b3ba RS |
721 | ;; Walk through the buffers |
722 | (while (and (not expansion) dabbrev--friend-buffer-list) | |
723 | (setq dabbrev--last-buffer | |
724 | (car dabbrev--friend-buffer-list)) | |
725 | (setq dabbrev--friend-buffer-list | |
726 | (cdr dabbrev--friend-buffer-list)) | |
727 | (set-buffer dabbrev--last-buffer) | |
728 | (dabbrev--scanning-message) | |
729 | (setq dabbrev--last-expansion-location (point-min)) | |
730 | (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case))) | |
731 | expansion))))) | |
732 | ||
6163b3ba RS |
733 | (defun dabbrev--safe-replace-match (string &optional fixedcase literal) |
734 | (if (eq major-mode 'picture-mode) | |
735 | (picture-replace-match string fixedcase literal) | |
736 | (replace-match string fixedcase literal))) | |
737 | ||
738 | ;;;---------------------------------------------------------------- | |
739 | ;;; Substitute the current string in buffer with the expansion | |
740 | ;;; OLD is nil or the last expansion substring. | |
741 | ;;; ABBREV is the abbreviation we are working with. | |
742 | ;;; EXPANSION is the expansion substring. | |
743 | (defun dabbrev--substitute-expansion (old abbrev expansion) | |
744 | ;;(undo-boundary) | |
745 | (let ((use-case-replace (and (eval dabbrev-case-fold-search) | |
746 | (or (not dabbrev-upcase-means-case-search) | |
747 | (string= abbrev (downcase abbrev))) | |
748 | (eval dabbrev-case-replace)))) | |
749 | (and nil use-case-replace | |
750 | (setq old (concat abbrev (or old ""))) | |
751 | (setq expansion (concat abbrev expansion))) | |
752 | (if old | |
753 | (save-excursion | |
754 | (search-backward old)) | |
755 | ;;(store-match-data (list (point-marker) (point-marker))) | |
756 | (search-backward abbrev)) | |
757 | ;; Make case of replacement conform to case of abbreviation | |
758 | ;; provided (1) that kind of thing is enabled in this buffer | |
759 | ;; and (2) the replacement itself is all lower case. | |
760 | (dabbrev--safe-replace-match expansion | |
761 | (not use-case-replace) | |
762 | t))) | |
763 | ||
764 | ||
765 | ;;;---------------------------------------------------------------- | |
766 | ;;; Search function used by dabbrevs library. | |
767 | ||
7313ccdb | 768 | ;;; ABBREV is string to find as prefix of word. Second arg, REVERSE, |
6163b3ba | 769 | ;;; is t for reverse search, nil for forward. Variable dabbrev-limit |
a7acbbe4 | 770 | ;;; controls the maximum search region size. Third argument IGNORE-CASE |
6163b3ba RS |
771 | ;;; non-nil means treat case as insignificant while looking for a match |
772 | ;;; and when comparing with previous matches. Also if that's non-nil | |
773 | ;;; and the match is found at the beginning of a sentence and is in | |
774 | ;;; lower case except for the initial then it is converted to all lower | |
775 | ;;; case for return. | |
776 | ||
777 | ;;; Table of expansions already seen is examined in buffer | |
778 | ;;; `dabbrev--last-table' so that only distinct possibilities are found | |
779 | ;;; by dabbrev-re-expand. | |
780 | ||
781 | ;;; Value is the expansion, or nil if not found. | |
782 | ||
783 | (defun dabbrev--search (abbrev reverse ignore-case) | |
784 | (save-match-data | |
785 | (let ((pattern1 (concat (regexp-quote abbrev) | |
786 | "\\(" dabbrev--abbrev-char-regexp "\\)")) | |
787 | (pattern2 (concat (regexp-quote abbrev) | |
788 | "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)")) | |
789 | (found-string nil)) | |
790 | ;; Limited search. | |
791 | (save-restriction | |
792 | (and dabbrev-limit | |
793 | (narrow-to-region dabbrev--last-expansion-location | |
794 | (+ (point) | |
795 | (if reverse (- dabbrev-limit) dabbrev-limit)))) | |
796 | ;;-------------------------------- | |
797 | ;; Look for a distinct expansion, using dabbrev--last-table. | |
798 | ;;-------------------------------- | |
799 | (while (and (not found-string) | |
800 | (if reverse | |
801 | (re-search-backward pattern1 nil t) | |
802 | (re-search-forward pattern1 nil t))) | |
63d991d6 KH |
803 | (goto-char (match-beginning 0)) |
804 | ;; In case we matched in the middle of a word, | |
805 | ;; back up to start of word and verify we still match. | |
806 | (dabbrev--goto-start-of-abbrev) | |
807 | ||
808 | (if (not (looking-at pattern1)) | |
809 | nil | |
810 | ;; We have a truly valid match. Find the end. | |
6163b3ba RS |
811 | (re-search-forward pattern2) |
812 | (setq found-string | |
813 | (buffer-substring (match-beginning 1) (match-end 1))) | |
814 | (and ignore-case (setq found-string (downcase found-string))) | |
63d991d6 | 815 | ;; Ignore this match if it's already in the table. |
7313ccdb RS |
816 | (if (dabbrev-filter-elements |
817 | table-string dabbrev--last-table | |
818 | (string= found-string table-string)) | |
63d991d6 KH |
819 | (setq found-string nil))) |
820 | ;; Prepare to continue searching. | |
6163b3ba RS |
821 | (if reverse |
822 | (goto-char (match-beginning 0)) | |
823 | (goto-char (match-end 0)))) | |
63d991d6 KH |
824 | ;; If we found something, use it. |
825 | (if found-string | |
826 | ;; Put it into `dabbrev--last-table' | |
827 | ;; and return it (either downcased, or as is). | |
828 | (let ((result | |
829 | (buffer-substring (match-beginning 0) (match-end 0)))) | |
830 | (setq dabbrev--last-table | |
831 | (cons found-string dabbrev--last-table)) | |
832 | (if (and ignore-case (eval dabbrev-case-replace)) | |
833 | (downcase result) | |
834 | result))))))) | |
6163b3ba | 835 | |
01987a6b | 836 | (provide 'dabbrev) |
7313ccdb | 837 | |
b578f267 | 838 | ;;; dabbrev.el ends here |