*** empty log message ***
[bpt/emacs.git] / lisp / dabbrev.el
CommitLineData
2f790b20
JB
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(provide 'dabbrevs)
42
43(defvar dabbrevs-limit nil
44 "*Limits region searched by `dabbrevs-expand' to this many chars away.")
45(make-variable-buffer-local 'dabbrevs-limit)
46
47(defvar dabbrevs-backward-only nil
48 "*If non-NIL, `dabbrevs-expand' only looks backwards.")
49
50; State vars for dabbrevs-re-expand.
51(defvar last-dabbrevs-table nil
52 "Table of expansions seen so far (local)")
53(make-variable-buffer-local 'last-dabbrevs-table)
54
55(defvar last-dabbrevs-abbreviation ""
56 "Last string we tried to expand (local).")
57(make-variable-buffer-local 'last-dabbrevs-abbreviation)
58
59(defvar last-dabbrevs-direction 0
60 "Direction of last dabbrevs search (local)")
61(make-variable-buffer-local 'last-dabbrevs-direction)
62
63(defvar last-dabbrevs-abbrev-location nil
64 "Location last abbreviation began (local).")
65(make-variable-buffer-local 'last-dabbrevs-abbrev-location)
66
67(defvar last-dabbrevs-expansion nil
68 "Last expansion of an abbreviation. (local)")
69(make-variable-buffer-local 'last-dabbrevs-expansion)
70
71(defvar last-dabbrevs-expansion-location nil
72 "Location the last expansion was found. (local)")
73(make-variable-buffer-local 'last-dabbrevs-expansion-location)
74
75;;;###autoload
76(defun dabbrev-expand (arg)
77 "Expand previous word \"dynamically\".
78Expands to the most recent, preceding word for which this is a prefix.
79If no suitable preceding word is found, words following point are considered.
80
81If `case-fold-search' and `case-replace' are non-nil (usually true)
82then the substituted word may be case-adjusted to match the abbreviation
83that you had typed. This takes place if the substituted word, as found,
84is all lower case, or if it is at the beginning of a sentence and only
85its first letter was upper case.
86
87A positive prefix arg N says to take the Nth backward DISTINCT
88possibility. A negative argument says search forward. The variable
89`dabbrev-backward-only' may be used to limit the direction of search to
90backward if set non-nil.
91
92If the cursor has not moved from the end of the previous expansion and
93no argument is given, replace the previously-made expansion
94with the next possible expansion not yet tried."
95 (interactive "*P")
96 (let (abbrev expansion old which loc n pattern
97 (do-case (and case-fold-search case-replace)))
98 ;; abbrev -- the abbrev to expand
99 ;; expansion -- the expansion found (eventually) or nil until then
100 ;; old -- the text currently in the buffer
101 ;; (the abbrev, or the previously-made expansion)
102 ;; loc -- place where expansion is found
103 ;; (to start search there for next expansion if requested later)
104 ;; do-case -- non-nil if should transform case when substituting.
105 (save-excursion
106 (if (and (null arg)
107 (eq last-command this-command)
108 last-dabbrevs-abbrev-location)
109 (progn
110 (setq abbrev last-dabbrevs-abbreviation)
111 (setq old last-dabbrevs-expansion)
112 (setq which last-dabbrevs-direction))
113 (setq which (if (null arg)
114 (if dabbrevs-backward-only 1 0)
115 (prefix-numeric-value arg)))
116 (setq loc (point))
117 (forward-word -1)
118 (setq last-dabbrevs-abbrev-location (point)) ; Original location.
119 (setq abbrev (buffer-substring (point) loc))
120 (setq old abbrev)
121 (setq last-dabbrevs-expansion-location nil)
122 (setq last-dabbrev-table nil)) ; Clear table of things seen.
123
124 (setq pattern (concat "\\b" (regexp-quote abbrev) "\\(\\sw\\|\\s_\\)+"))
125 ;; Try looking backward unless inhibited.
126 (if (>= which 0)
127 (progn
128 (setq n (max 1 which))
129 (if last-dabbrevs-expansion-location
130 (goto-char last-dabbrevs-expansion-location))
131 (while (and (> n 0)
132 (setq expansion (dabbrevs-search pattern t do-case)))
133 (setq loc (point-marker))
134 (setq last-dabbrev-table (cons expansion last-dabbrev-table))
135 (setq n (1- n)))
136 (or expansion
137 (setq last-dabbrevs-expansion-location nil))
138 (setq last-dabbrevs-direction (min 1 which))))
139
140 (if (and (<= which 0) (not expansion)) ; Then look forward.
141 (progn
142 (setq n (max 1 (- which)))
143 (if last-dabbrevs-expansion-location
144 (goto-char last-dabbrevs-expansion-location))
145 (while (and (> n 0)
146 (setq expansion (dabbrevs-search pattern nil do-case)))
147 (setq loc (point-marker))
148 (setq last-dabbrev-table (cons expansion last-dabbrev-table))
149 (setq n (1- n)))
150 (setq last-dabbrevs-direction -1))))
151
152 (if (not expansion)
153 (let ((first (string= abbrev old)))
154 (setq last-dabbrevs-abbrev-location nil)
155 (if (not first)
156 (progn (undo-boundary)
157 (delete-backward-char (length old))
158 (insert abbrev)))
159 (error (if first
160 "No dynamic expansion for \"%s\" found."
161 "No further dynamic expansions for \"%s\" found.")
162 abbrev))
163 ;; Success: stick it in and return.
164 (undo-boundary)
165 (search-backward old)
166 ;; Make case of replacement conform to case of abbreviation
167 ;; provided (1) that kind of thing is enabled in this buffer
168 ;; and (2) the replacement itself is all lower case.
169 ;; First put back the original abbreviation with its original
170 ;; case pattern.
171 (save-excursion
172 (replace-match abbrev t 'literal))
173 (search-forward abbrev)
174 (let ((do-case (and do-case
175 (string= (substring expansion 1)
176 (downcase (substring expansion 1))))))
177 ;; First put back the original abbreviation with its original
178 ;; case pattern.
179 (save-excursion
180 (replace-match abbrev t 'literal))
181 (search-forward abbrev)
182 (replace-match (if do-case (downcase expansion) expansion)
183 (not do-case)
184 'literal))
185 ;; Save state for re-expand.
186 (setq last-dabbrevs-abbreviation abbrev)
187 (setq last-dabbrevs-expansion expansion)
188 (setq last-dabbrevs-expansion-location loc))))
189
190;;;###autoload
191(define-key esc-map "/" 'dabbrev-expand)
192
193
194;; Search function used by dabbrevs library.
195;; First arg is string to find as prefix of word. Second arg is
196;; t for reverse search, nil for forward. Variable dabbrevs-limit
197;; controls the maximum search region size.
198
199;; Table of expansions already seen is examined in buffer last-dabbrev-table,
200;; so that only distinct possibilities are found by dabbrevs-re-expand.
201;; Note that to prevent finding the abbrev itself it must have been
202;; entered in the table.
203
204;; IGNORE-CASE non-nil means treat case as insignificant while
205;; looking for a match and when comparing with previous matches.
206;; Also if that's non-nil and the match is found at the beginning of a sentence
207;; and is in lower case except for the initial
208;; then it is converted to all lower case for return.
209
210;; Value is the expansion, or nil if not found. After a successful
211;; search, point is left right after the expansion found.
212
213(defun dabbrevs-search (pattern reverse ignore-case)
214 (let (missing result (case-fold-search ignore-case))
215 (save-restriction ; Uses restriction for limited searches.
216 (if dabbrevs-limit
217 (narrow-to-region last-dabbrevs-abbrev-location
218 (+ (point)
219 (* dabbrevs-limit (if reverse -1 1)))))
220 ;; Keep looking for a distinct expansion.
221 (setq result nil)
222 (setq missing nil)
223 (while (and (not result) (not missing))
224 ; Look for it, leave loop if search fails.
225 (setq missing
226 (not (if reverse
227 (re-search-backward pattern nil t)
228 (re-search-forward pattern nil t))))
229
230 (if (not missing)
231 (progn
232 (setq result (buffer-substring (match-beginning 0)
233 (match-end 0)))
234 (let* ((test last-dabbrev-table))
235 (while (and test
236 (not
237 (if ignore-case
238 (string= (downcase (car test))
239 (downcase result))
240 (string= (car test) result))))
241 (setq test (cdr test)))
242 (if test (setq result nil)))))) ; if already in table, ignore
243 (if result
244 (save-excursion
245 (let ((beg (match-beginning 0)))
246 (goto-char beg)
247 (and ignore-case
248 (string= (substring result 1)
249 (downcase (substring result 1)))
250 (if (string= paragraph-start
251 (concat "^$\\|" page-delimiter))
252 (and (re-search-backward sentence-end nil t)
253 (= (match-end 0) beg))
254 (forward-char 1)
255 (backward-sentence)
256 (= (point) beg))
257 (setq result (downcase result))))))
258 result)))