|
|
1.1 ! root 1: ;; Dynamic abbreviation package for GNU Emacs. ! 2: ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc. ! 3: ! 4: ;; This file is part of GNU Emacs. ! 5: ! 6: ;; GNU Emacs is distributed in the hope that it will be useful, ! 7: ;; but WITHOUT ANY WARRANTY. No author or distributor ! 8: ;; accepts responsibility to anyone for the consequences of using it ! 9: ;; or for whether it serves any particular purpose or works at all, ! 10: ;; unless he says so in writing. Refer to the GNU Emacs General Public ! 11: ;; License for full details. ! 12: ! 13: ;; Everyone is granted permission to copy, modify and redistribute ! 14: ;; GNU Emacs, but only under the conditions described in the ! 15: ;; GNU Emacs General Public License. A copy of this license is ! 16: ;; supposed to have been given to you along with GNU Emacs so you ! 17: ;; can know your rights and responsibilities. It should be in a ! 18: ;; file named COPYING. Among other things, the copyright notice ! 19: ;; and this notice must be preserved on all copies. ! 20: ! 21: ! 22: ; DABBREVS - "Dynamic abbreviations" hack, originally written by Don Morrison ! 23: ; for Twenex Emacs. Converted to mlisp by Russ Fish. Supports the table ! 24: ; feature to avoid hitting the same expansion on re-expand, and the search ! 25: ; size limit variable. Bugs fixed from the Twenex version are flagged by ! 26: ; comments starting with ;;; . ! 27: ; ! 28: ; converted to elisp by Spencer Thomas. ! 29: ; Thoroughly cleaned up by Richard Stallman. ! 30: ; ! 31: ; If anyone feels like hacking at it, Bob Keller (Keller@Utah-20) first ! 32: ; suggested the beast, and has some good ideas for its improvement, but ! 33: ; doesn?tknow TECO (the lucky devil...). One thing that should definitely ! 34: ; be done is adding the ability to search some other buffer(s) if you can?t ! 35: ; find the expansion you want in the current one. ! 36: ! 37: ;; (defun dabbrevs-help () ! 38: ;; "Give help about dabbrevs." ! 39: ;; (interactive) ! 40: ;; (&info "emacs" "dabbrevs") ; Select the specific info node. ! 41: ;; ) ! 42: (provide 'dabbrevs) ! 43: ! 44: (defvar dabbrevs-limit nil ! 45: "*Limits region searched by dabbrevs-expand to that many chars away (local).") ! 46: (make-variable-buffer-local 'dabbrevs-limit) ! 47: ! 48: (defvar dabbrevs-backward-only nil ! 49: "*If non-NIL, dabbrevs-expand only looks backwards.") ! 50: ! 51: ; State vars for dabbrevs-re-expand. ! 52: (defvar last-dabbrevs-table nil ! 53: "Table of expansions seen so far. (local)") ! 54: (make-variable-buffer-local 'last-dabbrevs-table) ! 55: ! 56: (defvar last-dabbrevs-abbreviation "" ! 57: "Last string we tried to expand. Buffer-local.") ! 58: (make-variable-buffer-local 'last-dabbrevs-abbreviation) ! 59: ! 60: (defvar last-dabbrevs-direction 0 ! 61: "Direction of last dabbrevs search. (local)") ! 62: (make-variable-buffer-local 'last-dabbrevs-direction) ! 63: ! 64: (defvar last-dabbrevs-abbrev-location nil ! 65: "Location last abbreviation began (local).") ! 66: (make-variable-buffer-local 'last-dabbrevs-abbrev-location) ! 67: ! 68: (defvar last-dabbrevs-expansion nil ! 69: "Last expansion of an abbreviation. (local)") ! 70: (make-variable-buffer-local 'last-dabbrevs-expansion) ! 71: ! 72: (defvar last-dabbrevs-expansion-location nil ! 73: "Location the last expansion was found. (local)") ! 74: (make-variable-buffer-local 'last-dabbrevs-expansion-location) ! 75: ! 76: (defun dabbrev-expand (arg) ! 77: "Expand previous word \"dynamically\". ! 78: Expands to the most recent, preceding word for which this is a prefix. ! 79: If no suitable preceding word is found, words following point are considered. ! 80: ! 81: A positive prefix argument, N, says to take the Nth backward DISTINCT ! 82: possibility. A negative argument says search forward. The variable ! 83: dabbrev-backward-only may be used to limit the direction of search to ! 84: backward if set non-nil. ! 85: ! 86: If the cursor has not moved from the end of the previous expansion and ! 87: no argument is given, replace the previously-made expansion ! 88: with the next possible expansion not yet tried." ! 89: (interactive "*P") ! 90: (let (abbrev expansion old which loc n pattern ! 91: (nocase (and case-fold-search case-replace))) ! 92: ;; abbrev -- the abbrev to expand ! 93: ;; expansion -- the expansion found (eventually) or nil until then ! 94: ;; old -- the text currently in the buffer ! 95: ;; (the abbrev, or the previously-made expansion) ! 96: ;; loc -- place where expansion is found ! 97: ;; (to start search there for next expansion if requested later) ! 98: ;; nocase -- non-nil if should consider case significant. ! 99: (save-excursion ! 100: (if (and (null arg) ! 101: (eq last-command this-command) ! 102: last-dabbrevs-abbrev-location) ! 103: (progn ! 104: (setq abbrev last-dabbrevs-abbreviation) ! 105: (setq old last-dabbrevs-expansion) ! 106: (setq which last-dabbrevs-direction)) ! 107: (setq which (if (null arg) ! 108: (if dabbrevs-backward-only 1 0) ! 109: (prefix-numeric-value arg))) ! 110: (setq loc (point)) ! 111: (forward-word -1) ! 112: (setq last-dabbrevs-abbrev-location (point)) ; Original location. ! 113: (setq abbrev (buffer-substring (point) loc)) ! 114: (setq old abbrev) ! 115: (setq last-dabbrevs-expansion-location nil) ! 116: (setq last-dabbrev-table nil)) ; Clear table of things seen. ! 117: ! 118: (setq pattern (concat "\\b" (regexp-quote abbrev) "\\(\\sw\\|\\s_\\)+")) ! 119: ;; Try looking backward unless inhibited. ! 120: (if (>= which 0) ! 121: (progn ! 122: (setq n (max 1 which)) ! 123: (if last-dabbrevs-expansion-location ! 124: (goto-char last-dabbrevs-expansion-location)) ! 125: (while (and (> n 0) ! 126: (setq expansion (dabbrevs-search pattern t nocase))) ! 127: (setq loc (point-marker)) ! 128: (setq last-dabbrev-table (cons expansion last-dabbrev-table)) ! 129: (setq n (1- n))) ! 130: (or expansion ! 131: (setq last-dabbrevs-expansion-location nil)) ! 132: (setq last-dabbrevs-direction (min 1 which)))) ! 133: ! 134: (if (and (<= which 0) (not expansion)) ; Then look forward. ! 135: (progn ! 136: (setq n (max 1 (- which))) ! 137: (if last-dabbrevs-expansion-location ! 138: (goto-char last-dabbrevs-expansion-location)) ! 139: (while (and (> n 0) ! 140: (setq expansion (dabbrevs-search pattern nil nocase))) ! 141: (setq loc (point-marker)) ! 142: (setq last-dabbrev-table (cons expansion last-dabbrev-table)) ! 143: (setq n (1- n))) ! 144: (setq last-dabbrevs-direction -1)))) ! 145: ! 146: (if (not expansion) ! 147: (let ((first (string= abbrev old))) ! 148: (setq last-dabbrevs-abbrev-location nil) ! 149: (if (not first) ! 150: (progn (undo-boundary) ! 151: (delete-backward-char (length old)) ! 152: (insert abbrev))) ! 153: (error (if first ! 154: "No dynamic expansion for \"%s\" found." ! 155: "No further dynamic expansions for \"%s\" found.") ! 156: abbrev)) ! 157: ;; Success: stick it in and return. ! 158: (undo-boundary) ! 159: (search-backward old) ! 160: ;; Make case of replacement conform to case of abbreviation ! 161: ;; provided (1) that kind of thing is enabled in this buffer ! 162: ;; and (2) the replacement itself is all lower case ! 163: ;; except perhaps for the first character. ! 164: (let ((nocase (and nocase ! 165: (string= expansion ! 166: (concat (substring expansion 0 1) ! 167: (downcase (substring expansion 1))))))) ! 168: (replace-match (if nocase (downcase expansion) expansion) ! 169: (not nocase) ! 170: 'literal)) ! 171: ;; Save state for re-expand. ! 172: (setq last-dabbrevs-abbreviation abbrev) ! 173: (setq last-dabbrevs-expansion expansion) ! 174: (setq last-dabbrevs-expansion-location loc)))) ! 175: ! 176: ;; Search function used by dabbrevs library. ! 177: ;; First arg is string to find as prefix of word. Second arg is ! 178: ;; t for reverse search, nil for forward. Variable dabbrevs-limit ! 179: ;; controls the maximum search region size. ! 180: ! 181: ;; Table of expansions already seen is examined in buffer last-dabbrev-table, ! 182: ;; so that only distinct possibilities are found by dabbrevs-re-expand. ! 183: ;; Note that to prevent finding the abbrev itself it must have been ! 184: ;; entered in the table. ! 185: ! 186: ;; Value is the expansion, or nil if not found. After a successful ! 187: ;; search, point is left right after the expansion found. ! 188: ! 189: (defun dabbrevs-search (pattern reverse nocase) ! 190: (let (missing result) ! 191: (save-restriction ; Uses restriction for limited searches. ! 192: (if dabbrevs-limit ! 193: (narrow-to-region last-dabbrevs-abbrev-location ! 194: (+ (point) ! 195: (* dabbrevs-limit (if reverse -1 1))))) ! 196: ;; Keep looking for a distinct expansion. ! 197: (setq result nil) ! 198: (setq missing nil) ! 199: (while (and (not result) (not missing)) ! 200: ; Look for it, leave loop if search fails. ! 201: (setq missing ! 202: (not (if reverse ! 203: (re-search-backward pattern nil t) ! 204: (re-search-forward pattern nil t)))) ! 205: ! 206: (if (not missing) ! 207: (progn ! 208: (setq result (buffer-substring (match-beginning 0) ! 209: (match-end 0))) ! 210: (let* ((test last-dabbrev-table)) ! 211: (while (and test ! 212: (not ! 213: (if nocase ! 214: (string= (downcase (car test)) (downcase result)) ! 215: (string= (car test) result)))) ! 216: (setq test (cdr test))) ! 217: (if test (setq result nil)))))) ; if already in table, ignore ! 218: result)))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.