662
|
1 ;;; dabbrev.el --- dynamic abbreviation package for GNU Emacs.
|
|
2
|
807
|
3 ;; Maintainer: FSF
|
|
4 ;; Last-Modified: 16 Mar 1992
|
|
5
|
267
|
6 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
|
|
7
|
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
11 ;; it under the terms of the GNU General Public License as published by
|
807
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
267
|
13 ;; any later version.
|
|
14
|
|
15 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 ;; GNU General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
23
|
807
|
24 ;;; Commentary:
|
267
|
25
|
|
26 ; DABBREVS - "Dynamic abbreviations" hack, originally written by Don Morrison
|
|
27 ; for Twenex Emacs. Converted to mlisp by Russ Fish. Supports the table
|
|
28 ; feature to avoid hitting the same expansion on re-expand, and the search
|
|
29 ; size limit variable. Bugs fixed from the Twenex version are flagged by
|
|
30 ; comments starting with ;;; .
|
|
31 ;
|
793
|
32 ; converted to Emacs Lisp by Spencer Thomas.
|
267
|
33 ; Thoroughly cleaned up by Richard Stallman.
|
|
34 ;
|
|
35 ; If anyone feels like hacking at it, Bob Keller (Keller@Utah-20) first
|
|
36 ; suggested the beast, and has some good ideas for its improvement, but
|
807
|
37 ; doesn't know TECO (the lucky devil...). One thing that should definitely
|
267
|
38 ; be done is adding the ability to search some other buffer(s) if you can?t
|
|
39 ; find the expansion you want in the current one.
|
|
40
|
807
|
41 ;;; Code:
|
|
42
|
267
|
43 ;; (defun dabbrevs-help ()
|
|
44 ;; "Give help about dabbrevs."
|
|
45 ;; (interactive)
|
|
46 ;; (&info "emacs" "dabbrevs") ; Select the specific info node.
|
|
47 ;; )
|
|
48 (defvar dabbrevs-limit nil
|
|
49 "*Limits region searched by `dabbrevs-expand' to this many chars away.")
|
|
50 (make-variable-buffer-local 'dabbrevs-limit)
|
|
51
|
|
52 (defvar dabbrevs-backward-only nil
|
|
53 "*If non-NIL, `dabbrevs-expand' only looks backwards.")
|
|
54
|
|
55 ; State vars for dabbrevs-re-expand.
|
|
56 (defvar last-dabbrevs-table nil
|
|
57 "Table of expansions seen so far (local)")
|
|
58 (make-variable-buffer-local 'last-dabbrevs-table)
|
|
59
|
|
60 (defvar last-dabbrevs-abbreviation ""
|
|
61 "Last string we tried to expand (local).")
|
|
62 (make-variable-buffer-local 'last-dabbrevs-abbreviation)
|
|
63
|
|
64 (defvar last-dabbrevs-direction 0
|
|
65 "Direction of last dabbrevs search (local)")
|
|
66 (make-variable-buffer-local 'last-dabbrevs-direction)
|
|
67
|
|
68 (defvar last-dabbrevs-abbrev-location nil
|
|
69 "Location last abbreviation began (local).")
|
|
70 (make-variable-buffer-local 'last-dabbrevs-abbrev-location)
|
|
71
|
|
72 (defvar last-dabbrevs-expansion nil
|
|
73 "Last expansion of an abbreviation. (local)")
|
|
74 (make-variable-buffer-local 'last-dabbrevs-expansion)
|
|
75
|
|
76 (defvar last-dabbrevs-expansion-location nil
|
|
77 "Location the last expansion was found. (local)")
|
|
78 (make-variable-buffer-local 'last-dabbrevs-expansion-location)
|
|
79
|
|
80 ;;;###autoload
|
|
81 (defun dabbrev-expand (arg)
|
|
82 "Expand previous word \"dynamically\".
|
|
83 Expands to the most recent, preceding word for which this is a prefix.
|
|
84 If no suitable preceding word is found, words following point are considered.
|
|
85
|
|
86 If `case-fold-search' and `case-replace' are non-nil (usually true)
|
|
87 then the substituted word may be case-adjusted to match the abbreviation
|
|
88 that you had typed. This takes place if the substituted word, as found,
|
|
89 is all lower case, or if it is at the beginning of a sentence and only
|
|
90 its first letter was upper case.
|
|
91
|
|
92 A positive prefix arg N says to take the Nth backward DISTINCT
|
|
93 possibility. A negative argument says search forward. The variable
|
|
94 `dabbrev-backward-only' may be used to limit the direction of search to
|
|
95 backward if set non-nil.
|
|
96
|
|
97 If the cursor has not moved from the end of the previous expansion and
|
|
98 no argument is given, replace the previously-made expansion
|
|
99 with the next possible expansion not yet tried."
|
|
100 (interactive "*P")
|
|
101 (let (abbrev expansion old which loc n pattern
|
|
102 (do-case (and case-fold-search case-replace)))
|
|
103 ;; abbrev -- the abbrev to expand
|
|
104 ;; expansion -- the expansion found (eventually) or nil until then
|
|
105 ;; old -- the text currently in the buffer
|
|
106 ;; (the abbrev, or the previously-made expansion)
|
|
107 ;; loc -- place where expansion is found
|
|
108 ;; (to start search there for next expansion if requested later)
|
|
109 ;; do-case -- non-nil if should transform case when substituting.
|
|
110 (save-excursion
|
|
111 (if (and (null arg)
|
|
112 (eq last-command this-command)
|
|
113 last-dabbrevs-abbrev-location)
|
|
114 (progn
|
|
115 (setq abbrev last-dabbrevs-abbreviation)
|
|
116 (setq old last-dabbrevs-expansion)
|
|
117 (setq which last-dabbrevs-direction))
|
|
118 (setq which (if (null arg)
|
|
119 (if dabbrevs-backward-only 1 0)
|
|
120 (prefix-numeric-value arg)))
|
|
121 (setq loc (point))
|
|
122 (forward-word -1)
|
|
123 (setq last-dabbrevs-abbrev-location (point)) ; Original location.
|
|
124 (setq abbrev (buffer-substring (point) loc))
|
|
125 (setq old abbrev)
|
|
126 (setq last-dabbrevs-expansion-location nil)
|
|
127 (setq last-dabbrev-table nil)) ; Clear table of things seen.
|
|
128
|
|
129 (setq pattern (concat "\\b" (regexp-quote abbrev) "\\(\\sw\\|\\s_\\)+"))
|
|
130 ;; Try looking backward unless inhibited.
|
|
131 (if (>= which 0)
|
|
132 (progn
|
|
133 (setq n (max 1 which))
|
|
134 (if last-dabbrevs-expansion-location
|
|
135 (goto-char last-dabbrevs-expansion-location))
|
|
136 (while (and (> n 0)
|
|
137 (setq expansion (dabbrevs-search pattern t do-case)))
|
|
138 (setq loc (point-marker))
|
|
139 (setq last-dabbrev-table (cons expansion last-dabbrev-table))
|
|
140 (setq n (1- n)))
|
|
141 (or expansion
|
|
142 (setq last-dabbrevs-expansion-location nil))
|
|
143 (setq last-dabbrevs-direction (min 1 which))))
|
|
144
|
|
145 (if (and (<= which 0) (not expansion)) ; Then look forward.
|
|
146 (progn
|
|
147 (setq n (max 1 (- which)))
|
|
148 (if last-dabbrevs-expansion-location
|
|
149 (goto-char last-dabbrevs-expansion-location))
|
|
150 (while (and (> n 0)
|
|
151 (setq expansion (dabbrevs-search pattern nil do-case)))
|
|
152 (setq loc (point-marker))
|
|
153 (setq last-dabbrev-table (cons expansion last-dabbrev-table))
|
|
154 (setq n (1- n)))
|
|
155 (setq last-dabbrevs-direction -1))))
|
|
156
|
|
157 (if (not expansion)
|
|
158 (let ((first (string= abbrev old)))
|
|
159 (setq last-dabbrevs-abbrev-location nil)
|
|
160 (if (not first)
|
|
161 (progn (undo-boundary)
|
|
162 (delete-backward-char (length old))
|
|
163 (insert abbrev)))
|
|
164 (error (if first
|
|
165 "No dynamic expansion for \"%s\" found."
|
|
166 "No further dynamic expansions for \"%s\" found.")
|
|
167 abbrev))
|
|
168 ;; Success: stick it in and return.
|
|
169 (undo-boundary)
|
|
170 (search-backward old)
|
|
171 ;; Make case of replacement conform to case of abbreviation
|
|
172 ;; provided (1) that kind of thing is enabled in this buffer
|
|
173 ;; and (2) the replacement itself is all lower case.
|
|
174 ;; First put back the original abbreviation with its original
|
|
175 ;; case pattern.
|
|
176 (save-excursion
|
|
177 (replace-match abbrev t 'literal))
|
|
178 (search-forward abbrev)
|
|
179 (let ((do-case (and do-case
|
|
180 (string= (substring expansion 1)
|
|
181 (downcase (substring expansion 1))))))
|
|
182 ;; First put back the original abbreviation with its original
|
|
183 ;; case pattern.
|
|
184 (save-excursion
|
|
185 (replace-match abbrev t 'literal))
|
|
186 (search-forward abbrev)
|
|
187 (replace-match (if do-case (downcase expansion) expansion)
|
|
188 (not do-case)
|
|
189 'literal))
|
|
190 ;; Save state for re-expand.
|
|
191 (setq last-dabbrevs-abbreviation abbrev)
|
|
192 (setq last-dabbrevs-expansion expansion)
|
|
193 (setq last-dabbrevs-expansion-location loc))))
|
|
194
|
|
195 ;;;###autoload
|
|
196 (define-key esc-map "/" 'dabbrev-expand)
|
|
197
|
|
198
|
|
199 ;; Search function used by dabbrevs library.
|
|
200 ;; First arg is string to find as prefix of word. Second arg is
|
|
201 ;; t for reverse search, nil for forward. Variable dabbrevs-limit
|
|
202 ;; controls the maximum search region size.
|
|
203
|
|
204 ;; Table of expansions already seen is examined in buffer last-dabbrev-table,
|
|
205 ;; so that only distinct possibilities are found by dabbrevs-re-expand.
|
|
206 ;; Note that to prevent finding the abbrev itself it must have been
|
|
207 ;; entered in the table.
|
|
208
|
|
209 ;; IGNORE-CASE non-nil means treat case as insignificant while
|
|
210 ;; looking for a match and when comparing with previous matches.
|
|
211 ;; Also if that's non-nil and the match is found at the beginning of a sentence
|
|
212 ;; and is in lower case except for the initial
|
|
213 ;; then it is converted to all lower case for return.
|
|
214
|
|
215 ;; Value is the expansion, or nil if not found. After a successful
|
|
216 ;; search, point is left right after the expansion found.
|
|
217
|
|
218 (defun dabbrevs-search (pattern reverse ignore-case)
|
|
219 (let (missing result (case-fold-search ignore-case))
|
|
220 (save-restriction ; Uses restriction for limited searches.
|
|
221 (if dabbrevs-limit
|
|
222 (narrow-to-region last-dabbrevs-abbrev-location
|
|
223 (+ (point)
|
|
224 (* dabbrevs-limit (if reverse -1 1)))))
|
|
225 ;; Keep looking for a distinct expansion.
|
|
226 (setq result nil)
|
|
227 (setq missing nil)
|
|
228 (while (and (not result) (not missing))
|
|
229 ; Look for it, leave loop if search fails.
|
|
230 (setq missing
|
|
231 (not (if reverse
|
|
232 (re-search-backward pattern nil t)
|
|
233 (re-search-forward pattern nil t))))
|
|
234
|
|
235 (if (not missing)
|
|
236 (progn
|
|
237 (setq result (buffer-substring (match-beginning 0)
|
|
238 (match-end 0)))
|
|
239 (let* ((test last-dabbrev-table))
|
|
240 (while (and test
|
|
241 (not
|
|
242 (if ignore-case
|
|
243 (string= (downcase (car test))
|
|
244 (downcase result))
|
|
245 (string= (car test) result))))
|
|
246 (setq test (cdr test)))
|
|
247 (if test (setq result nil)))))) ; if already in table, ignore
|
|
248 (if result
|
|
249 (save-excursion
|
|
250 (let ((beg (match-beginning 0)))
|
|
251 (goto-char beg)
|
|
252 (and ignore-case
|
|
253 (string= (substring result 1)
|
|
254 (downcase (substring result 1)))
|
|
255 (if (string= paragraph-start
|
|
256 (concat "^$\\|" page-delimiter))
|
|
257 (and (re-search-backward sentence-end nil t)
|
|
258 (= (match-end 0) beg))
|
|
259 (forward-char 1)
|
|
260 (backward-sentence)
|
|
261 (= (point) beg))
|
|
262 (setq result (downcase result))))))
|
|
263 result)))
|
584
|
264
|
|
265 (provide 'dabbrevs)
|
|
266
|
662
|
267 ;;; dabbrev.el ends here
|