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