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