1 ;;; dabbrev.el --- dynamic abbreviation package for GNU Emacs.
4 ;; Last-Modified: 16 Mar 1992
6 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
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.
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 ;;; .
32 ; converted to Emacs Lisp by Spencer Thomas.
33 ; Thoroughly cleaned up by Richard Stallman.
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
37 ; doesn't know TECO (the lucky devil...). One thing that should definitely
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.
43 ;; (defun dabbrevs-help ()
44 ;; "Give help about dabbrevs."
46 ;; (&info "emacs" "dabbrevs") ; Select the specific info node.
48 (defvar dabbrevs-limit nil
49 "*Limits region searched by `dabbrevs-expand' to this many chars away.")
50 (make-variable-buffer-local 'dabbrevs-limit
)
52 (defvar dabbrevs-backward-only nil
53 "*If non-NIL, `dabbrevs-expand' only looks backwards.")
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
)
60 (defvar last-dabbrevs-abbreviation
""
61 "Last string we tried to expand (local).")
62 (make-variable-buffer-local 'last-dabbrevs-abbreviation
)
64 (defvar last-dabbrevs-direction
0
65 "Direction of last dabbrevs search (local)")
66 (make-variable-buffer-local 'last-dabbrevs-direction
)
68 (defvar last-dabbrevs-abbrev-location nil
69 "Location last abbreviation began (local).")
70 (make-variable-buffer-local 'last-dabbrevs-abbrev-location
)
72 (defvar last-dabbrevs-expansion nil
73 "Last expansion of an abbreviation. (local)")
74 (make-variable-buffer-local 'last-dabbrevs-expansion
)
76 (defvar last-dabbrevs-expansion-location nil
77 "Location the last expansion was found. (local)")
78 (make-variable-buffer-local 'last-dabbrevs-expansion-location
)
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.
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.
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.
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."
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.
112 (eq last-command this-command
)
113 last-dabbrevs-abbrev-location
)
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
)))
123 (setq last-dabbrevs-abbrev-location
(point)) ; Original location.
124 (setq abbrev
(buffer-substring (point) loc
))
126 (setq last-dabbrevs-expansion-location nil
)
127 (setq last-dabbrev-table nil
)) ; Clear table of things seen.
129 (setq pattern
(concat "\\b" (regexp-quote abbrev
) "\\(\\sw\\|\\s_\\)+"))
130 ;; Try looking backward unless inhibited.
133 (setq n
(max 1 which
))
134 (if last-dabbrevs-expansion-location
135 (goto-char last-dabbrevs-expansion-location
))
137 (setq expansion
(dabbrevs-search pattern t do-case
)))
138 (setq loc
(point-marker))
139 (setq last-dabbrev-table
(cons expansion last-dabbrev-table
))
142 (setq last-dabbrevs-expansion-location nil
))
143 (setq last-dabbrevs-direction
(min 1 which
))))
145 (if (and (<= which
0) (not expansion
)) ; Then look forward.
147 (setq n
(max 1 (- which
)))
148 (if last-dabbrevs-expansion-location
149 (goto-char last-dabbrevs-expansion-location
))
151 (setq expansion
(dabbrevs-search pattern nil do-case
)))
152 (setq loc
(point-marker))
153 (setq last-dabbrev-table
(cons expansion last-dabbrev-table
))
155 (setq last-dabbrevs-direction -
1))))
158 (let ((first (string= abbrev old
)))
159 (setq last-dabbrevs-abbrev-location nil
)
161 (progn (undo-boundary)
162 (delete-backward-char (length old
))
165 "No dynamic expansion for \"%s\" found."
166 "No further dynamic expansions for \"%s\" found.")
168 ;; Success: stick it in and return.
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
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
185 (replace-match abbrev t
'literal
))
186 (search-forward abbrev
)
187 (replace-match (if do-case
(downcase expansion
) expansion
)
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
))))
196 (define-key esc-map
"/" 'dabbrev-expand
)
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.
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.
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.
215 ;; Value is the expansion, or nil if not found. After a successful
216 ;; search, point is left right after the expansion found.
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.
222 (narrow-to-region last-dabbrevs-abbrev-location
224 (* dabbrevs-limit
(if reverse -
1 1)))))
225 ;; Keep looking for a distinct expansion.
228 (while (and (not result
) (not missing
))
229 ; Look for it, leave loop if search fails.
232 (re-search-backward pattern nil t
)
233 (re-search-forward pattern nil t
))))
237 (setq result
(buffer-substring (match-beginning 0)
239 (let* ((test last-dabbrev-table
))
243 (string= (downcase (car test
))
245 (string= (car test
) result
))))
246 (setq test
(cdr test
)))
247 (if test
(setq result nil
)))))) ; if already in table, ignore
250 (let ((beg (match-beginning 0)))
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
))
262 (setq result
(downcase result
))))))
267 ;;; dabbrev.el ends here