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