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