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