comparison lisp/dabbrev.el @ 11820:362251066737

(dabbrev-completion): Use mapcar, not mapc. Call dabbrev--reset-global-variables as the first thing. (dabbrev-abbrev-char-regexp): Default value is nil again. (dabbrev--find-all-expansions): Don't use push. (dabbrev--search): Likewise. (dabbrev-check-other-buffers): Renamed from dabbrev-always-check-other-buffers. (dabbrev-check-all-buffers): Renamed frm dabbrev-check-rest-of-buffers. (dabbrev-expand): Now checks for marker. (dabbrev--try-find): Now does a protected call to `widen'. (dabbrev-always-check-other-buffers): Default now t. (dabbrev--capitalize): Function deleted. (dabbrev-completion): Use `capitalize'.
author Richard M. Stallman <rms@gnu.org>
date Mon, 15 May 1995 23:16:08 +0000
parents bf1c7dcc69db
children e862c6697f39
comparison
equal deleted inserted replaced
11819:d9b1cec100c6 11820:362251066737
2 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc. 2 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc.
3 3
4 ;; Author: Don Morrison 4 ;; Author: Don Morrison
5 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se> 5 ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
6 ;; Created: 16 Mars 1992 6 ;; Created: 16 Mars 1992
7 ;; Lindberg's last update version: 5.2 7 ;; Lindberg's last update version: 5.7
8 ;; Keywords: abbrev expand completion 8 ;; Keywords: abbrev expand completion
9 9
10 ;; This program is free software; you can redistribute it and/or modify 10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by 11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2 of the License, or 12 ;; the Free Software Foundation; either version 2 of the License, or
28 ;; them. 28 ;; them.
29 ;; 29 ;;
30 ;; To expand a word, just put the point right after the word and press 30 ;; To expand a word, just put the point right after the word and press
31 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion). 31 ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
32 ;; 32 ;;
33 ;; There are powerful things in this package that aren't turned on by
34 ;; default. I recommend you to do the following.
35 ;;
36 ;; Put the following 2 lines in your .emacs file:
37 ;; (setq dabbrev-always-check-other-buffers t)
38 ;; (setq dabbrev-abbrev-char-regexp "\\sw\\|\\s_")
39 ;;
40 ;; Dabbrev will now search in all buffers with the same major mode for
41 ;; your expansions. It will also search for complete symbols, the old
42 ;; dabbrev package only looked half-heartedly for symbols.
43 ;;
44 ;; Check out the customizable variables below to learn about all the 33 ;; Check out the customizable variables below to learn about all the
45 ;; features of this package. 34 ;; features of this package.
46 35
47 ;;; Hints and tips for major modes writers: 36 ;;; Hints and tips for major modes writers:
48 37
83 ;; - Add the possibility of searching both forward and backward to 72 ;; - Add the possibility of searching both forward and backward to
84 ;; the nearest expansion. 73 ;; the nearest expansion.
85 ;; - Check the kill-ring when everything else fails. (Maybe something 74 ;; - Check the kill-ring when everything else fails. (Maybe something
86 ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu> 75 ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
87 76
88 ;;; Thanks goes to 77 ;;; These people gave suggestions:
89 ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com> 78 ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
90 ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu> 79 ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
91 ;; [jules] Julian Gosnell <jules@x.co.uk> 80 ;; [jules] Julian Gosnell <jules@x.co.uk>
92 ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu> 81 ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
93 ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se> 82 ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
147 variable to see if its value is t or nil. 136 variable to see if its value is t or nil.
148 137
149 This variable has an effect only when the value of 138 This variable has an effect only when the value of
150 `dabbrev-case-fold-search' evaluates to t.") 139 `dabbrev-case-fold-search' evaluates to t.")
151 140
152 (defvar dabbrev-abbrev-char-regexp "\\sw\\|\\s_" 141 (defvar dabbrev-abbrev-char-regexp nil
153 "*Regexp to recognize a character in an abbreviation or expansion. 142 "*Regexp to recognize a character in an abbreviation or expansion.
154 This regexp will be surrounded with \\\\( ... \\\\) when actually used. 143 This regexp will be surrounded with \\\\( ... \\\\) when actually used.
155 144
156 Set this variable to \"\\\\sw\" if you want ordinary words or 145 Set this variable to \"\\\\sw\" if you want ordinary words or
157 \"\\\\sw\\\\|\\\\s_\" if you want symbols. 146 \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
158 147 syntax is \"symbol\" as well as those whose syntax is \"word\".
159 You can also set it to nil if you want old-style dabbrev searching 148
160 \(the abbreviation is from point to previous word-start, the 149 The value nil has a special meaning: the abbreviation is from point to
161 search is for symbols). 150 previous word-start, but the search is for symbols.
162 151
163 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol, 152 For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
164 while `yes', `or', `no' and `p' are considered words. If you set this 153 while `yes', `or', `no' and `p' are considered words. If this
165 variable to nil, then expanding `yes-or-no-' looks for a symbol 154 variable is nil, then expanding `yes-or-no-' looks for a symbol
166 starting with or containing `no-'. If you set this variable to 155 starting with or containing `no-'. If you set this variable to
167 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with 156 \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
168 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then 157 `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
169 expanding `yes-or-no-' signals an error because `-' is not part of a word; 158 expanding `yes-or-no-' signals an error because `-' is not part of a word;
170 but expanding `yes-or-no' looks for a word starting with `no'. 159 but expanding `yes-or-no' looks for a word starting with `no'.
171 160
172 The recommended value is \"\\\\sw\\\\|\\\\s_\".") 161 The recommended value is \"\\\\sw\\\\|\\\\s_\".")
173 162
174 (defvar dabbrev-check-rest-of-buffers t 163 (defvar dabbrev-check-all-buffers t
175 "*Non-nil means dabbrev package should search in all buffers. 164 "*Non-nil means dabbrev package should search *all* buffers.
176 It searches the buffers pointed out by `dabbrev-select-buffers-function' 165
177 first; afterward it looks in the rest of the buffers.") 166 Dabbrev always searches the current buffer first. Then, if
178 167 `dabbrev-check-other-buffers' says so, it searches the buffers
179 168 designated by `dabbrev-select-buffers-function'.
180 ;; I recommend that you set this to t. 169
181 (defvar dabbrev-always-check-other-buffers nil 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
182 "*Should \\[dabbrev-expand] look in other buffers?\ 174 "*Should \\[dabbrev-expand] look in other buffers?\
183 nil = Don't look in other buffers.\n\ 175
184 t = Look in other buffers.\n\ 176 nil: Don't look in other buffers.
185 Value other than nil and t = ask the user if he want's to look in 177 t: Also look for expansions in the buffers pointed out by
186 other buffers. 178 `dabbrev-select-buffers-function'.
187 179 Anything else: When we can't find any more expansions in
188 The recommended value is t.") 180 the current buffer, then ask the user whether to look in other
181 buffers too.
182
183 The default value is t.")
189 184
190 ;; I guess setting this to a function that selects all C- or C++- 185 ;; I guess setting this to a function that selects all C- or C++-
191 ;; mode buffers would be a good choice for a debugging buffer, 186 ;; mode buffers would be a good choice for a debugging buffer,
192 ;; when debugging C- or C++-code. 187 ;; when debugging C- or C++-code.
193 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers 188 (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
194 "A function that selects buffers that should be searched by dabbrev. 189 "A function that selects buffers that should be searched by dabbrev.
195
196 The function should take no arguments and return a list of buffers to 190 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 191 search for expansions. Have a look at `dabbrev--select-buffers' for
198 an example. 192 an example.
199 193
200 A mode setting this variable should make it buffer local.") 194 A mode setting this variable should make it buffer local.")
201 195
202 (defvar dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p 196 (defvar dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
203 "*A function to check if OTHER-BUFFER should be searched by dabbrev. 197 "*A function to decide whether dabbrev should search OTHER-BUFFER.
204
205 The function should take one argument, OTHER-BUFFER, and return 198 The function should take one argument, OTHER-BUFFER, and return
206 non-nil if that buffer should be searched. Have a look at 199 non-nil if that buffer should be searched. Have a look at
207 `dabbrev--same-major-mode-p' for an example. 200 `dabbrev--same-major-mode-p' for an example.
208 201
209 The value of `dabbrev-friend-buffer-function' has an effect only if 202 The value of `dabbrev-friend-buffer-function' has an effect only if
255 (defvar dabbrev--last-buffer-found nil) 248 (defvar dabbrev--last-buffer-found nil)
256 249
257 ;; The buffer we last did a completion in. 250 ;; The buffer we last did a completion in.
258 (defvar dabbrev--last-completion-buffer nil) 251 (defvar dabbrev--last-completion-buffer nil)
259 252
260 ;; Same as dabbrev-always-check-other-buffers, but is set for every expand. 253 ;; Same as dabbrev-check-other-buffers, but is set for every expand.
261 (defvar dabbrev--check-other-buffers dabbrev-always-check-other-buffers) 254 (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
262 255
263 ;; The regexp for recognizing a character in an abbreviation. 256 ;; The regexp for recognizing a character in an abbreviation.
264 (defvar dabbrev--abbrev-char-regexp nil) 257 (defvar dabbrev--abbrev-char-regexp nil)
265 258
266 ;;;---------------------------------------------------------------- 259 ;;;----------------------------------------------------------------
314 307
315 With no prefix argument, it reuses an old completion list 308 With no prefix argument, it reuses an old completion list
316 if there is a suitable one already." 309 if there is a suitable one already."
317 310
318 (interactive "*P") 311 (interactive "*P")
319 (let* ((dabbrev-always-check-other-buffers (and arg t)) 312 (dabbrev--reset-global-variables)
320 (dabbrev-check-rest-of-buffers 313 (let* ((dabbrev-check-other-buffers (and arg t))
314 (dabbrev-check-all-buffers
321 (and arg (= (prefix-numeric-value arg) 16))) 315 (and arg (= (prefix-numeric-value arg) 16)))
322 (abbrev (dabbrev--abbrev-at-point)) 316 (abbrev (dabbrev--abbrev-at-point))
323 (ignore-case-p (and (eval dabbrev-case-fold-search) 317 (ignore-case-p (and (eval dabbrev-case-fold-search)
324 (or (not dabbrev-upcase-means-case-search) 318 (or (not dabbrev-upcase-means-case-search)
325 (string= abbrev (downcase abbrev))))) 319 (string= abbrev (downcase abbrev)))))
341 ;; We can reuse the existing completion list. 335 ;; We can reuse the existing completion list.
342 nil 336 nil
343 ;;-------------------------------- 337 ;;--------------------------------
344 ;; New abbreviation to expand. 338 ;; New abbreviation to expand.
345 ;;-------------------------------- 339 ;;--------------------------------
346 (dabbrev--reset-global-variables)
347 (setq dabbrev--last-abbreviation abbrev) 340 (setq dabbrev--last-abbreviation abbrev)
348 ;; Find all expansion 341 ;; Find all expansion
349 (let ((completion-list 342 (let ((completion-list
350 (dabbrev--find-all-expansions abbrev ignore-case-p))) 343 (dabbrev--find-all-expansions abbrev ignore-case-p)))
351 ;; Make an obarray with all expansions 344 ;; Make an obarray with all expansions
355 abbrev 348 abbrev
356 (if dabbrev--check-other-buffers "" " in this-buffer"))) 349 (if dabbrev--check-other-buffers "" " in this-buffer")))
357 (cond 350 (cond
358 ((or (not ignore-case-p) 351 ((or (not ignore-case-p)
359 (not dabbrev-case-replace)) 352 (not dabbrev-case-replace))
360 (mapc (function (lambda (string) 353 (mapcar (function (lambda (string)
361 (intern string my-obarray))) 354 (intern string my-obarray)))
362 completion-list)) 355 completion-list))
363 ((string= abbrev (upcase abbrev)) 356 ((string= abbrev (upcase abbrev))
364 (mapc (function (lambda (string) 357 (mapcar (function (lambda (string)
365 (intern (upcase string) my-obarray))) 358 (intern (upcase string) my-obarray)))
366 completion-list)) 359 completion-list))
367 ((string= (substring abbrev 0 1) 360 ((string= (substring abbrev 0 1)
368 (upcase (substring abbrev 0 1))) 361 (upcase (substring abbrev 0 1)))
369 (mapc (function (lambda (string) 362 (mapcar (function (lambda (string)
370 (intern (dabbrev--capitalize string) my-obarray))) 363 (intern (capitalize string) my-obarray)))
371 completion-list)) 364 completion-list))
372 (t 365 (t
373 (mapc (function (lambda (string) 366 (mapcar (function (lambda (string)
374 (intern (downcase string) my-obarray))) 367 (intern (downcase string) my-obarray)))
375 completion-list))) 368 completion-list)))
376 (setq dabbrev--last-obarray my-obarray) 369 (setq dabbrev--last-obarray my-obarray)
377 (setq dabbrev--last-completion-buffer (current-buffer)) 370 (setq dabbrev--last-completion-buffer (current-buffer))
378 ;; Find the longest common string. 371 ;; Find the longest common string.
379 (setq init (try-completion abbrev my-obarray))))) 372 (setq init (try-completion abbrev my-obarray)))))
380 ;;-------------------------------- 373 ;;--------------------------------
418 with the next possible expansion not yet tried. 411 with the next possible expansion not yet tried.
419 412
420 The variable `dabbrev-backward-only' may be used to limit the 413 The variable `dabbrev-backward-only' may be used to limit the
421 direction of search to backward if set non-nil. 414 direction of search to backward if set non-nil.
422 415
423 ???
424 To make it more powerful, make sure that
425 `dabbrev-always-check-other-buffers' is set to t.
426
427 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]." 416 See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
428 (interactive "*P") 417 (interactive "*P")
429 (let (abbrev expansion old direction) 418 (let (abbrev expansion old direction)
430 ;; abbrev -- the abbrev to expand 419 ;; abbrev -- the abbrev to expand
431 ;; expansion -- the expansion found (eventually) or nil until then 420 ;; expansion -- the expansion found (eventually) or nil until then
432 ;; old -- the text currently in the buffer 421 ;; old -- the text currently in the buffer
433 ;; (the abbrev, or the previously-made expansion) 422 ;; (the abbrev, or the previously-made expansion)
434 (save-excursion 423 (save-excursion
435 (if (and (null arg) 424 (if (and (null arg)
436 dabbrev--last-abbrev-location 425 (markerp dabbrev--last-abbrev-location)
426 (marker-position dabbrev--last-abbrev-location)
437 (or (eq last-command this-command) 427 (or (eq last-command this-command)
438 (and (window-minibuffer-p (selected-window)) 428 (and (window-minibuffer-p (selected-window))
439 (= dabbrev--last-abbrev-location 429 (= dabbrev--last-abbrev-location
440 (point))))) 430 (point)))))
441 ;; Find a different expansion for the same abbrev as last time. 431 ;; Find a different expansion for the same abbrev as last time.
485 ;;;---------------------------------------------------------------- 475 ;;;----------------------------------------------------------------
486 ;;;---------------------------------------------------------------- 476 ;;;----------------------------------------------------------------
487 ;;; Local functions 477 ;;; Local functions
488 ;;;---------------------------------------------------------------- 478 ;;;----------------------------------------------------------------
489 ;;;---------------------------------------------------------------- 479 ;;;----------------------------------------------------------------
490
491 (defun dabbrev--capitalize (string)
492 ;; Capitalize STRING (See capitalize-word)
493 (let ((new-string ""))
494 (save-match-data
495 (while (string-match "\\w+" string)
496 (let* ((mb (match-beginning 0))
497 (me (match-end 0))
498 (ms (substring string mb me)))
499 (setq new-string
500 (concat new-string
501 (substring string 0 mb)
502 (upcase (substring ms 0 1))
503 (downcase (substring ms 1))))
504 (setq string (substring string me)))))
505 new-string))
506 480
507 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer. 481 ;;; Checks if OTHER-BUFFER has the same major mode as current buffer.
508 (defun dabbrev--same-major-mode-p (other-buffer) 482 (defun dabbrev--same-major-mode-p (other-buffer)
509 (eq major-mode 483 (eq major-mode
510 (save-excursion 484 (save-excursion
561 dabbrev--friend-buffer-list nil 535 dabbrev--friend-buffer-list nil
562 dabbrev--last-buffer nil 536 dabbrev--last-buffer nil
563 dabbrev--last-buffer-found nil 537 dabbrev--last-buffer-found nil
564 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp 538 dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
565 "\\sw\\|\\s_") 539 "\\sw\\|\\s_")
566 dabbrev--check-other-buffers dabbrev-always-check-other-buffers)) 540 dabbrev--check-other-buffers dabbrev-check-other-buffers))
567 541
568 ;;; Find all buffers that are considered "friends" according to the 542 ;;; Find all buffers that are considered "friends" according to the
569 ;;; function pointed out by dabbrev-friend-buffer-function. 543 ;;; function pointed out by dabbrev-friend-buffer-function.
570 (defun dabbrev--select-buffers () 544 (defun dabbrev--select-buffers ()
571 (save-excursion 545 (save-excursion
580 554
581 ;;; Search for ABBREV, N times, normally looking forward, 555 ;;; Search for ABBREV, N times, normally looking forward,
582 ;;; but looking in reverse instead if REVERSE is non-nil. 556 ;;; but looking in reverse instead if REVERSE is non-nil.
583 (defun dabbrev--try-find (abbrev reverse n ignore-case) 557 (defun dabbrev--try-find (abbrev reverse n ignore-case)
584 (save-excursion 558 (save-excursion
585 (let ((expansion nil)) 559 (save-restriction
586 (and dabbrev--last-expansion-location 560 (widen)
587 (goto-char dabbrev--last-expansion-location)) 561 (let ((expansion nil))
588 (let ((case-fold-search ignore-case) 562 (and dabbrev--last-expansion-location
589 (count n)) 563 (goto-char dabbrev--last-expansion-location))
590 (while (and (> count 0) 564 (let ((case-fold-search ignore-case)
591 (setq expansion (dabbrev--search abbrev 565 (count n))
592 reverse 566 (while (and (> count 0)
593 ignore-case))) 567 (setq expansion (dabbrev--search abbrev
594 (setq count (1- count)))) 568 reverse
595 (and expansion 569 ignore-case)))
596 (setq dabbrev--last-expansion-location (point))) 570 (setq count (1- count))))
597 expansion))) 571 (and expansion
572 (setq dabbrev--last-expansion-location (point)))
573 expansion))))
598 574
599 ;;; Find all expansions of ABBREV 575 ;;; Find all expansions of ABBREV
600 (defun dabbrev--find-all-expansions (abbrev ignore-case) 576 (defun dabbrev--find-all-expansions (abbrev ignore-case)
601 (let ((all-expansions nil) 577 (let ((all-expansions nil)
602 expansion) 578 expansion)
603 (save-excursion 579 (save-excursion
604 (goto-char (point-min)) 580 (goto-char (point-min))
605 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case)) 581 (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
606 (push expansion all-expansions))) 582 (setq all-expansions (cons expansion all-expansions))))
607 all-expansions)) 583 all-expansions))
608 584
609 (defun dabbrev--scanning-message () 585 (defun dabbrev--scanning-message ()
610 (message "Scanning `%s'" (buffer-name (current-buffer)))) 586 (message "Scanning `%s'" (buffer-name (current-buffer))))
611 587
669 (setq dabbrev--check-other-buffers 645 (setq dabbrev--check-other-buffers
670 (y-or-n-p "Scan other buffers also? "))))) 646 (y-or-n-p "Scan other buffers also? ")))))
671 (let* (friend-buffer-list non-friend-buffer-list) 647 (let* (friend-buffer-list non-friend-buffer-list)
672 (setq dabbrev--friend-buffer-list 648 (setq dabbrev--friend-buffer-list
673 (funcall dabbrev-select-buffers-function)) 649 (funcall dabbrev-select-buffers-function))
674 (if dabbrev-check-rest-of-buffers 650 (if dabbrev-check-all-buffers
675 (setq non-friend-buffer-list 651 (setq non-friend-buffer-list
676 (nreverse 652 (nreverse
677 (dabbrev-filter-elements 653 (dabbrev-filter-elements
678 buffer (buffer-list) 654 buffer (buffer-list)
679 (not (memq buffer dabbrev--friend-buffer-list)))) 655 (not (memq buffer dabbrev--friend-buffer-list))))
787 (found-string 763 (found-string
788 ;;-------------------------------- 764 ;;--------------------------------
789 ;; Put in `dabbrev--last-table' and decide if we should return 765 ;; Put in `dabbrev--last-table' and decide if we should return
790 ;; result or (downcase result) 766 ;; result or (downcase result)
791 ;;-------------------------------- 767 ;;--------------------------------
792 (push found-string dabbrev--last-table) 768 (setq dabbrev--last-table (cons found-string dabbrev--last-table))
793 (let ((result (buffer-substring (match-beginning 0) (match-end 0)))) 769 (let ((result (buffer-substring (match-beginning 0) (match-end 0))))
794 (if (and ignore-case (eval dabbrev-case-replace)) 770 (if (and ignore-case (eval dabbrev-case-replace))
795 (downcase result) 771 (downcase result)
796 result)))))))) 772 result))))))))
797 773