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