Mercurial > emacs
annotate lisp/completion.el @ 3175:15d3c2e32922
Pervasive changes to use Emacs 19 features
and conform to Emacs conventions.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 27 May 1993 12:44:54 +0000 |
parents | ff7e4f44269d |
children | 326e28cbf102 |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
190
diff
changeset
|
1 ;;; completion.el --- dynamic word-completion code |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
190
diff
changeset
|
2 |
795
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
3 ;; Maintainer: bug-completion@think.com |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1356
diff
changeset
|
4 ;; Keywords: abbrev |
795
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
5 |
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
6 ;;; Commentary: |
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
7 |
3169
ff7e4f44269d
Comment out handling of shell-send-input.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
8 ;;; This file is very badly designed in that it redefines |
ff7e4f44269d
Comment out handling of shell-send-input.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
9 ;;; standard functions of Emacs. This is bad design, because |
ff7e4f44269d
Comment out handling of shell-send-input.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
10 ;;; this file cannot be updated to correspond to the latest |
ff7e4f44269d
Comment out handling of shell-send-input.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
11 ;;; versions of those functions. Therefore, you must expect |
ff7e4f44269d
Comment out handling of shell-send-input.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
12 ;;; it to produce unpredictable and undesirable results. |
ff7e4f44269d
Comment out handling of shell-send-input.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
13 ;;; This file needs to be redesigned to work in a modular fashion. |
ff7e4f44269d
Comment out handling of shell-send-input.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
14 ;;; -- rms. |
ff7e4f44269d
Comment out handling of shell-send-input.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
15 |
56 | 16 ;;; This is a Completion system for GNU Emacs |
17 ;;; | |
18 ;;; E-Mail: | |
19 ;;; Internet: completion@think.com, bug-completion@think.com | |
20 ;;; UUCP: {rutgers,harvard,mit-eddie}!think!completion | |
21 ;;; | |
22 ;;; If you are a new user, we'd appreciate knowing your site name and | |
23 ;;; any comments you have. | |
24 ;;; | |
25 ;;; | |
26 ;;; NO WARRANTY | |
27 ;;; | |
28 ;;; This software is distributed free of charge and is in the public domain. | |
29 ;;; Anyone may use, duplicate or modify this program. Thinking Machines | |
30 ;;; Corporation does not restrict in any way the use of this software by | |
31 ;;; anyone. | |
32 ;;; | |
33 ;;; Thinking Machines Corporation provides absolutely no warranty of any kind. | |
34 ;;; The entire risk as to the quality and performance of this program is with | |
35 ;;; you. In no event will Thinking Machines Corporation be liable to you for | |
36 ;;; damages, including any lost profits, lost monies, or other special, | |
37 ;;; incidental or consequential damages arising out of the use of this program. | |
38 ;;; | |
39 ;;; You must not restrict the distribution of this software. | |
40 ;;; | |
41 ;;; Please keep this notice and author information in any copies you make. | |
42 ;;; | |
43 ;;; 4/90 | |
44 ;;; | |
45 ;;; | |
46 ;;; Advertisement | |
47 ;;;--------------- | |
48 ;;; Try using this. If you are like most you will be happy you did. | |
49 ;;; | |
50 ;;; What to put in .emacs | |
51 ;;;----------------------- | |
52 ;;; (load "completion") ;; If it's not part of the standard band. | |
53 ;;; (initialize-completions) | |
54 ;;; | |
55 ;;; For best results, be sure to byte-compile the file first. | |
56 ;;; | |
57 | |
58 ;;; Authors | |
59 ;;;--------- | |
60 ;;; Jim Salem {salem@think.com} | |
61 ;;; Brewster Kahle {brewster@think.com} | |
62 ;;; Thinking Machines Corporation | |
63 ;;; 245 First St., Cambridge MA 02142 (617) 876-1111 | |
64 ;;; | |
65 ;;; Mailing Lists | |
66 ;;;--------------- | |
67 ;;; | |
68 ;;; Bugs to bug-completion@think.com | |
69 ;;; Comments to completion@think.com | |
70 ;;; Requests to be added completion-request@think.com | |
71 ;;; | |
72 ;;; Availability | |
73 ;;;-------------- | |
74 ;;; Anonymous FTP from think.com | |
75 ;;; | |
76 | |
77 ;;;--------------------------------------------------------------------------- | |
78 ;;; Documentation [Slightly out of date] | |
79 ;;;--------------------------------------------------------------------------- | |
80 ;;; (also check the documentation string of the functions) | |
81 ;;; | |
82 ;;; Introduction | |
83 ;;;--------------- | |
84 ;;; | |
85 ;;; After you type a few characters, pressing the "complete" key inserts | |
86 ;;; the rest of the word you are likely to type. | |
87 ;;; | |
88 ;;; This watches all the words that you type and remembers them. When | |
89 ;;; typing a new word, pressing "complete" (meta-return) "completes" the | |
90 ;;; word by inserting the most recently used word that begins with the | |
91 ;;; same characters. If you press meta-return repeatedly, it cycles | |
92 ;;; through all the words it knows about. | |
93 ;;; | |
94 ;;; If you like the completion then just continue typing, it is as if you | |
95 ;;; entered the text by hand. If you want the inserted extra characters | |
96 ;;; to go away, type control-w or delete. More options are described below. | |
97 ;;; | |
98 ;;; The guesses are made in the order of the most recently "used". Typing | |
99 ;;; in a word and then typing a separator character (such as a space) "uses" | |
100 ;;; the word. So does moving a cursor over the word. If no words are found, | |
101 ;;; it uses an extended version of the dabbrev style completion. | |
102 ;;; | |
103 ;;; You automatically save the completions you use to a file between | |
104 ;;; sessions. | |
105 ;;; | |
106 ;;; Completion enables programmers to enter longer, more descriptive | |
107 ;;; variable names while typing fewer keystrokes than they normally would. | |
108 ;;; | |
109 ;;; | |
110 ;;; Full documentation | |
111 ;;;--------------------- | |
112 ;;; | |
113 ;;; A "word" is any string containing characters with either word or symbol | |
114 ;;; syntax. [E.G. Any alphanumeric string with hypens, underscores, etc.] | |
115 ;;; Unless you change the constants, you must type at least three characters | |
116 ;;; for the word to be recognized. Only words longer than 6 characters are | |
117 ;;; saved. | |
118 ;;; | |
119 ;;; When you load this file, completion will be on. I suggest you use the | |
120 ;;; compiled version (because it is noticibly faster). | |
121 ;;; | |
122 ;;; M-X completion-mode toggles whether or not new words are added to the | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
123 ;;; database by changing the value of enable-completion. |
56 | 124 ;;; |
125 ;;; SAVING/LOADING COMPLETIONS | |
126 ;;; Completions are automatically saved from one session to another | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
127 ;;; (unless save-completions-flag or enable-completion is nil). |
56 | 128 ;;; Loading this file (or calling initialize-completions) causes EMACS |
129 ;;; to load a completions database for a saved completions file | |
130 ;;; (default: ~/.completions). When you exit, EMACS saves a copy of the | |
131 ;;; completions that you | |
132 ;;; often use. When you next start, EMACS loads in the saved completion file. | |
133 ;;; | |
134 ;;; The number of completions saved depends loosely on | |
135 ;;; *saved-completions-decay-factor*. Completions that have never been | |
136 ;;; inserted via "complete" are not saved. You are encouraged to experiment | |
137 ;;; with different functions (see compute-completion-min-num-uses). | |
138 ;;; | |
139 ;;; Some completions are permanent and are always saved out. These | |
140 ;;; completions have their num-uses slot set to T. Use | |
141 ;;; add-permanent-completion to do this | |
142 ;;; | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
143 ;;; Completions are saved only if enable-completion is T. The number of old |
56 | 144 ;;; versions kept of the saved completions file is controlled by |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
145 ;;; completions-file-versions-kept. |
56 | 146 ;;; |
147 ;;; COMPLETE KEY OPTIONS | |
148 ;;; The complete function takes a numeric arguments. | |
149 ;;; control-u :: leave the point at the beginning of the completion rather | |
150 ;;; than the middle. | |
151 ;;; a number :: rotate through the possible completions by that amount | |
152 ;;; `-' :: same as -1 (insert previous completion) | |
153 ;;; | |
154 ;;; HOW THE DATABASE IS MAINTAINED | |
155 ;;; <write> | |
156 ;;; | |
157 ;;; UPDATING THE DATABASE MANUALLY | |
158 ;;; m-x kill-completion | |
159 ;;; kills the completion at point. | |
160 ;;; m-x add-completion | |
161 ;;; m-x add-permanent-completion | |
162 ;;; | |
163 ;;; UPDATING THE DATABASE FROM A SOURCE CODE FILE | |
164 ;;; m-x add-completions-from-buffer | |
165 ;;; Parses all the definition names from a C or LISP mode buffer and | |
166 ;;; adds them to the completion database. | |
167 ;;; | |
168 ;;; m-x add-completions-from-lisp-file | |
169 ;;; Parses all the definition names from a C or Lisp mode file and | |
170 ;;; adds them to the completion database. | |
171 ;;; | |
172 ;;; UPDATING THE DATABASE FROM A TAGS TABLE | |
173 ;;; m-x add-completions-from-tags-table | |
174 ;;; Adds completions from the current tags-table-buffer. | |
175 ;;; | |
176 ;;; HOW A COMPLETION IS FOUND | |
177 ;;; <write> | |
178 ;;; | |
179 ;;; STRING CASING | |
180 ;;; Completion is string case independent if case-fold-search has its | |
181 ;;; normal default of T. Also when the completion is inserted the case of the | |
182 ;;; entry is coerced appropriately. | |
183 ;;; [E.G. APP --> APPROPRIATELY app --> appropriately | |
184 ;;; App --> Appropriately] | |
185 ;;; | |
186 ;;; INITIALIZATION | |
187 ;;; The form `(initialize-completions)' initializes the completion system by | |
188 ;;; trying to load in the user's completions. After the first cal, further | |
189 ;;; calls have no effect so one should be careful not to put the form in a | |
190 ;;; site's standard site-init file. | |
191 ;;; | |
192 ;;;--------------------------------------------------------------------------- | |
193 ;;; | |
194 ;;; | |
195 | |
196 ;;;----------------------------------------------- | |
197 ;;; Porting Notes | |
198 ;;;----------------------------------------------- | |
199 ;;; | |
200 ;;; Should run on 18.49, 18.52, and 19.0 | |
201 ;;; Tested on vanilla version. | |
202 ;;; This requires the standard cl.el file. It could easily rewritten to not | |
203 ;;; require it. It defines remove which is not in cl.el. | |
204 ;;; | |
205 ;;; FUNCTIONS BASHED | |
206 ;;; The following functions are bashed but it is done carefully and should not | |
207 ;;; cause problems :: | |
208 ;;; kill-region, next-line, previous-line, newline, newline-and-indent, | |
209 ;;; kill-emacs | |
210 ;;; | |
211 ;;; | |
212 ;;;--------------------------------------------------------------------------- | |
213 ;;; Functions you might like to call | |
214 ;;;--------------------------------------------------------------------------- | |
215 ;;; | |
216 ;;; add-completion string &optional num-uses | |
217 ;;; Adds a new string to the database | |
218 ;;; | |
219 ;;; add-permanent-completion string | |
220 ;;; Adds a new string to the database with num-uses = T | |
221 ;;; | |
222 | |
223 ;;; kill-completion string | |
224 ;;; Kills the completion from the database. | |
225 ;;; | |
226 ;;; clear-all-completions | |
227 ;;; Clears the database | |
228 ;;; | |
229 ;;; list-all-completions | |
230 ;;; Returns a list of all completions. | |
231 ;;; | |
232 ;;; | |
233 ;;; next-completion string &optional index | |
234 ;;; Returns a completion entry that starts with string. | |
235 ;;; | |
236 ;;; find-exact-completion string | |
237 ;;; Returns a completion entry that exactly matches string. | |
238 ;;; | |
239 ;;; complete | |
240 ;;; Inserts a completion at point | |
241 ;;; | |
242 ;;; initialize-completions | |
243 ;;; Loads the completions file and sets up so that exiting emacs will | |
244 ;;; save them. | |
245 ;;; | |
246 ;;; save-completions-to-file &optional filename | |
247 ;;; load-completions-from-file &optional filename | |
248 ;;; | |
249 ;;;----------------------------------------------- | |
250 ;;; Other functions | |
251 ;;;----------------------------------------------- | |
252 ;;; | |
253 ;;; get-completion-list string | |
254 ;;; | |
255 ;;; These things are for manipulating the structure | |
256 ;;; make-completion string num-uses | |
257 ;;; completion-num-uses completion | |
258 ;;; completion-string completion | |
259 ;;; set-completion-num-uses completion num-uses | |
260 ;;; set-completion-string completion string | |
261 ;;; | |
262 ;;; | |
263 | |
264 ;;;----------------------------------------------- | |
265 ;;; To Do :: (anybody ?) | |
266 ;;;----------------------------------------------- | |
267 ;;; | |
268 ;;; Implement Lookup and keyboard interface in C | |
269 ;;; Add package prefix smarts (for Common Lisp) | |
270 ;;; Add autoprompting of possible completions after every keystroke (fast | |
271 ;;; terminals only !) | |
272 ;;; Add doc. to texinfo | |
273 ;;; | |
274 ;;; | |
275 ;;;----------------------------------------------- | |
795
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
276 ;;; Change Log: |
56 | 277 ;;;----------------------------------------------- |
278 ;;; Sometime in '84 Brewster implemented a somewhat buggy version for | |
279 ;;; Symbolics LISPMs. | |
280 ;;; Jan. '85 Jim became enamored of the idea and implemented a faster, | |
281 ;;; more robust version. | |
282 ;;; With input from many users at TMC, (rose, craig, and gls come to mind), | |
283 ;;; the current style of interface was developed. | |
284 ;;; 9/87, Jim and Brewster took terminals home. Yuck. After | |
285 ;;; complaining for a while Brewester implemented a subset of the current | |
286 ;;; LISPM version for GNU Emacs. | |
287 ;;; 8/88 After complaining for a while (and with sufficient | |
288 ;;; promised rewards), Jim reimplemented a version of GNU completion | |
289 ;;; superior to that of the LISPM version. | |
290 ;;; | |
291 ;;;----------------------------------------------- | |
292 ;;; Acknowlegements | |
293 ;;;----------------------------------------------- | |
294 ;;; Cliff Lasser (cal@think.com), Kevin Herbert (kph@cisco.com), | |
295 ;;; eero@media-lab, kgk@cs.brown.edu, jla@ai.mit.edu, | |
296 ;;; | |
297 ;;;----------------------------------------------- | |
298 ;;; Change Log | |
299 ;;;----------------------------------------------- | |
300 ;;; From version 9 to 10 | |
301 ;;; - Allowance for non-integral *completion-version* nos. | |
302 ;;; - Fix cmpl-apply-as-top-level for keyboard macros | |
303 ;;; - Fix broken completion merging (in save-completions-to-file) | |
304 ;;; - More misc. fixes for version 19.0 of emacs | |
305 ;;; | |
306 ;;; From Version 8 to 9 | |
307 ;;; - Ported to version 19.0 of emacs (backcompatible with version 18) | |
308 ;;; - Added add-completions-from-tags-table (with thanks to eero@media-lab) | |
309 ;;; | |
310 ;;; From Version 7 to 8 | |
311 ;;; - Misc. changes to comments | |
312 ;;; - new completion key bindings: c-x o, M->, M-<, c-a, c-e | |
313 ;;; - cdabbrev now checks all the visible window buffers and the "other buffer" | |
314 ;;; - `%' is now a symbol character rather than a separator (except in C mode) | |
315 ;;; | |
316 ;;; From Version 6 to 7 | |
317 ;;; - Fixed bug with saving out .completion file the first time | |
318 ;;; | |
319 ;;; From Version 5 to 6 | |
320 ;;; - removed statistics recording | |
321 ;;; - reworked advise to handle autoloads | |
322 ;;; - Fixed fortran mode support | |
323 ;;; - Added new cursor motion triggers | |
324 ;;; | |
325 ;;; From Version 4 to 5 | |
326 ;;; - doesn't bother saving if nothing has changed | |
327 ;;; - auto-save if haven't used for a 1/2 hour | |
328 ;;; - save period extended to two weeks | |
329 ;;; - minor fix to capitalization code | |
330 ;;; - added *completion-auto-save-period* to variables recorded. | |
331 ;;; - added reenter protection to cmpl-record-statistics-filter | |
332 ;;; - added backup protection to save-completions-to-file (prevents | |
333 ;;; problems with disk full errors) | |
334 | |
795
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
335 ;;; Code: |
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
336 |
56 | 337 ;;;--------------------------------------------------------------------------- |
338 ;;; User changeable parameters | |
339 ;;;--------------------------------------------------------------------------- | |
340 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
341 (defvar enable-completion t |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
342 "*Non-nil means enable recording and saving of completions. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
343 If nil, no new words added to the database or saved to the init file.") |
56 | 344 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
345 (defvar save-completions-flag t |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
346 "*Non-nil means save most-used completions when exiting Emacs. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
347 See also `saved-completions-retention-time'.") |
56 | 348 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
349 (defvar save-completions-file-name "~/.completions" |
56 | 350 "*The filename to save completions to.") |
351 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
352 (defvar save-completions-retention-time 336 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
353 "*Discard a completion if unused for this many hours. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
354 \(1 day = 24, 1 week = 168). If this is 0, non-permanent completions |
190 | 355 will not be saved unless these are used. Default is two weeks.") |
56 | 356 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
357 (defvar completion-on-separator-character nil |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
358 "*Non-nil means separator characters mark previous word as used. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
359 This means the word will be saved as a completion.") |
56 | 360 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
361 (defvar completions-file-versions-kept kept-new-versions |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
362 "*Number of versions to keep for the saved completions file.") |
56 | 363 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
364 (defvar completion-prompt-speed-threshold 4800 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
365 "*Minimum output speed at which to display next potential completion.") |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
366 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
367 (defvar completion-cdabbrev-prompt-flag nil |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
368 "*If non-nil, the next completion prompt does a cdabbrev search. |
56 | 369 This can be time consuming.") |
370 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
371 (defvar completion-search-distance 15000 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
372 "*How far to search in the buffer when looking for completions. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
373 In number of characters. If nil, search the whole buffer.") |
56 | 374 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
375 (defvar completions-merging-modes '(lisp c) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
376 "*List of modes {`c' or `lisp'} for automatic completions merging. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
377 Definitions from visited files which have these modes |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
378 are automatically added to the completion database.") |
56 | 379 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
380 ;;;(defvar *record-cmpl-statistics-p* nil |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
381 ;;; "*If non-nil, record completion statistics.") |
56 | 382 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
383 ;;;(defvar *completion-auto-save-period* 1800 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
384 ;;; "*The period in seconds to wait for emacs to be idle before autosaving |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
385 ;;;the completions. Default is a 1/2 hour.") |
56 | 386 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
387 (defconst completion-min-length nil ;; defined below in eval-when |
56 | 388 "*The minimum length of a stored completion. |
389 DON'T CHANGE WITHOUT RECOMPILING ! This is used by macros.") | |
390 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
391 (defconst completion-max-length nil ;; defined below in eval-when |
56 | 392 "*The maximum length of a stored completion. |
393 DON'T CHANGE WITHOUT RECOMPILING ! This is used by macros.") | |
394 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
395 (defconst completion-prefix-min-length nil ;; defined below in eval-when |
56 | 396 "The minimum length of a completion search string. |
397 DON'T CHANGE WITHOUT RECOMPILING ! This is used by macros.") | |
398 | |
399 (defmacro eval-when-compile-load-eval (&rest body) | |
400 ;; eval everything before expanding | |
401 (mapcar 'eval body) | |
190 | 402 (cons 'progn body)) |
56 | 403 |
404 (defun completion-eval-when () | |
405 (eval-when-compile-load-eval | |
406 ;; These vars. are defined at both compile and load time. | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
407 (setq completion-min-length 6) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
408 (setq completion-max-length 200) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
409 (setq completion-prefix-min-length 3))) |
56 | 410 |
411 (completion-eval-when) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
412 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
413 ;; Need this file around too |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
414 (require 'cl) |
56 | 415 |
416 ;;;--------------------------------------------------------------------------- | |
417 ;;; Internal Variables | |
418 ;;;--------------------------------------------------------------------------- | |
419 | |
420 (defvar cmpl-initialized-p nil | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
421 "Set to t when the completion system is initialized. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
422 Indicates that the old completion file has been read in.") |
56 | 423 |
424 (defvar cmpl-completions-accepted-p nil | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
425 "Set to t as soon as the first completion has been accepted. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
426 Used to decide whether to save completions.") |
56 | 427 |
428 | |
429 ;;;--------------------------------------------------------------------------- | |
430 ;;; Low level tools | |
431 ;;;--------------------------------------------------------------------------- | |
432 | |
433 ;;;----------------------------------------------- | |
434 ;;; Misc. | |
435 ;;;----------------------------------------------- | |
436 | |
437 (defun minibuffer-window-selected-p () | |
438 "True iff the current window is the minibuffer." | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
439 (window-minibuffer-p (selected-window))) |
56 | 440 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
441 (defmacro cmpl-read-time-eval (form) |
56 | 442 ;; Like the #. reader macro |
190 | 443 (eval form)) |
56 | 444 |
445 | |
446 ;;;----------------------------------------------- | |
447 ;;; String case coercion | |
448 ;;;----------------------------------------------- | |
449 | |
450 (defun cmpl-string-case-type (string) | |
451 "Returns :capitalized, :up, :down, :mixed, or :neither." | |
452 (let ((case-fold-search nil)) | |
453 (cond ((string-match "[a-z]" string) | |
454 (cond ((string-match "[A-Z]" string) | |
455 (cond ((and (> (length string) 1) | |
456 (null (string-match "[A-Z]" string 1))) | |
457 ':capitalized) | |
458 (t | |
459 ':mixed))) | |
460 (t ':down))) | |
461 (t | |
462 (cond ((string-match "[A-Z]" string) | |
463 ':up) | |
464 (t ':neither)))) | |
465 )) | |
466 | |
467 ;;; Tests - | |
468 ;;; (cmpl-string-case-type "123ABCDEF456") --> :up | |
469 ;;; (cmpl-string-case-type "123abcdef456") --> :down | |
470 ;;; (cmpl-string-case-type "123aBcDeF456") --> :mixed | |
471 ;;; (cmpl-string-case-type "123456") --> :neither | |
472 ;;; (cmpl-string-case-type "Abcde123") --> :capitalized | |
473 | |
474 (defun cmpl-coerce-string-case (string case-type) | |
475 (cond ((eq case-type ':down) (downcase string)) | |
476 ((eq case-type ':up) (upcase string)) | |
477 ((eq case-type ':capitalized) | |
478 (setq string (downcase string)) | |
479 (aset string 0 (logand ?\337 (aref string 0))) | |
480 string) | |
481 (t string) | |
482 )) | |
483 | |
484 (defun cmpl-merge-string-cases (string-to-coerce given-string) | |
485 (let ((string-case-type (cmpl-string-case-type string-to-coerce)) | |
486 ) | |
487 (cond ((memq string-case-type '(:down :up :capitalized)) | |
488 ;; Found string is in a standard case. Coerce to a type based on | |
489 ;; the given string | |
490 (cmpl-coerce-string-case string-to-coerce | |
491 (cmpl-string-case-type given-string)) | |
492 ) | |
493 (t | |
494 ;; If the found string is in some unusual case, just insert it | |
495 ;; as is | |
496 string-to-coerce) | |
497 ))) | |
498 | |
499 ;;; Tests - | |
500 ;;; (cmpl-merge-string-cases "AbCdEf456" "abc") --> AbCdEf456 | |
501 ;;; (cmpl-merge-string-cases "abcdef456" "ABC") --> ABCDEF456 | |
502 ;;; (cmpl-merge-string-cases "ABCDEF456" "Abc") --> Abcdef456 | |
503 ;;; (cmpl-merge-string-cases "ABCDEF456" "abc") --> abcdef456 | |
504 | |
505 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
506 (defun cmpl-hours-since-origin () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
507 (let ((time (current-time))) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
508 (+ (* (/ (car time) 3600.0) (lsh 1 16)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
509 (/ (nth 2 time) 3600.0)))) |
56 | 510 |
511 ;;;--------------------------------------------------------------------------- | |
512 ;;; "Symbol" parsing functions | |
513 ;;;--------------------------------------------------------------------------- | |
514 ;;; The functions symbol-before-point, symbol-under-point, etc. quickly return | |
515 ;;; an appropriate symbol string. The strategy is to temporarily change | |
516 ;;; the syntax table to enable fast symbol searching. There are three classes | |
517 ;;; of syntax in these "symbol" syntax tables :: | |
518 ;;; | |
519 ;;; syntax (?_) - "symbol" chars (e.g. alphanumerics) | |
520 ;;; syntax (?w) - symbol chars to ignore at end of words (e.g. period). | |
521 ;;; syntax (? ) - everything else | |
522 ;;; | |
523 ;;; Thus by judicious use of scan-sexps and forward-word, we can get | |
524 ;;; the word we want relatively fast and without consing. | |
525 ;;; | |
526 ;;; Why do we need a separate category for "symbol chars to ignore at ends" ? | |
527 ;;; For example, in LISP we want starting :'s trimmed | |
528 ;;; so keyword argument specifiers also define the keyword completion. And, | |
529 ;;; for example, in C we want `.' appearing in a structure ref. to | |
530 ;;; be kept intact in order to store the whole structure ref.; however, if | |
531 ;;; it appears at the end of a symbol it should be discarded because it is | |
532 ;;; probably used as a period. | |
533 | |
534 ;;; Here is the default completion syntax :: | |
535 ;;; Symbol chars :: A-Z a-z 0-9 @ / \ * + ~ $ < > % | |
536 ;;; Symbol chars to ignore at ends :: _ : . - | |
537 ;;; Separator chars. :: <tab> <space> ! ^ & ( ) = ` | { } [ ] ; " ' # | |
538 ;;; , ? <Everything else> | |
539 | |
540 ;;; Mode specific differences and notes :: | |
541 ;;; LISP diffs -> | |
542 ;;; Symbol chars :: ! & ? = ^ | |
543 ;;; | |
544 ;;; C diffs -> | |
545 ;;; Separator chars :: + * / : % | |
546 ;;; A note on the hypen (`-'). Perhaps, the hypen should also be a separator | |
547 ;;; char., however, we wanted to have completion symbols include pointer | |
548 ;;; references. For example, "foo->bar" is a symbol as far as completion is | |
549 ;;; concerned. | |
550 ;;; | |
551 ;;; FORTRAN diffs -> | |
552 ;;; Separator chars :: + - * / : | |
553 ;;; | |
554 ;;; Pathname diffs -> | |
555 ;;; Symbol chars :: . | |
556 ;;; Of course there is no pathname "mode" and in fact we have not implemented | |
557 ;;; this table. However, if there was such a mode, this is what it would look | |
558 ;;; like. | |
559 | |
560 ;;;----------------------------------------------- | |
561 ;;; Table definitions | |
562 ;;;----------------------------------------------- | |
563 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
564 (defun cmpl-make-standard-completion-syntax-table () |
56 | 565 (let ((table (make-vector 256 0)) ;; default syntax is whitespace |
566 ) | |
567 ;; alpha chars | |
568 (dotimes (i 26) | |
569 (modify-syntax-entry (+ ?a i) "_" table) | |
570 (modify-syntax-entry (+ ?A i) "_" table)) | |
571 ;; digit chars. | |
572 (dotimes (i 10) | |
573 (modify-syntax-entry (+ ?0 i) "_" table)) | |
574 ;; Other ones | |
575 (let ((symbol-chars '(?@ ?/ ?\\ ?* ?+ ?~ ?$ ?< ?> ?%)) | |
576 (symbol-chars-ignore '(?_ ?- ?: ?.)) | |
577 ) | |
578 (dolist (char symbol-chars) | |
579 (modify-syntax-entry char "_" table)) | |
580 (dolist (char symbol-chars-ignore) | |
581 (modify-syntax-entry char "w" table) | |
582 ) | |
583 ) | |
584 table)) | |
585 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
586 (defconst cmpl-standard-syntax-table (cmpl-make-standard-completion-syntax-table)) |
56 | 587 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
588 (defun cmpl-make-lisp-completion-syntax-table () |
56 | 589 (let ((table (copy-syntax-table cmpl-standard-syntax-table)) |
590 (symbol-chars '(?! ?& ?? ?= ?^)) | |
591 ) | |
592 (dolist (char symbol-chars) | |
593 (modify-syntax-entry char "_" table)) | |
594 table)) | |
595 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
596 (defun cmpl-make-c-completion-syntax-table () |
56 | 597 (let ((table (copy-syntax-table cmpl-standard-syntax-table)) |
598 (separator-chars '(?+ ?* ?/ ?: ?%)) | |
599 ) | |
600 (dolist (char separator-chars) | |
601 (modify-syntax-entry char " " table)) | |
602 table)) | |
603 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
604 (defun cmpl-make-fortran-completion-syntax-table () |
56 | 605 (let ((table (copy-syntax-table cmpl-standard-syntax-table)) |
606 (separator-chars '(?+ ?- ?* ?/ ?:)) | |
607 ) | |
608 (dolist (char separator-chars) | |
609 (modify-syntax-entry char " " table)) | |
610 table)) | |
611 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
612 (defconst cmpl-lisp-syntax-table (cmpl-make-lisp-completion-syntax-table)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
613 (defconst cmpl-c-syntax-table (cmpl-make-c-completion-syntax-table)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
614 (defconst cmpl-fortran-syntax-table (cmpl-make-fortran-completion-syntax-table)) |
56 | 615 |
616 (defvar cmpl-syntax-table cmpl-standard-syntax-table | |
617 "This variable holds the current completion syntax table.") | |
618 (make-variable-buffer-local 'cmpl-syntax-table) | |
619 | |
620 ;;;----------------------------------------------- | |
621 ;;; Installing the appropriate mode tables | |
622 ;;;----------------------------------------------- | |
623 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
624 (add-hook 'lisp-mode-hook |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
625 '(lambda () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
626 (setq cmpl-syntax-table cmpl-lisp-syntax-table))) |
56 | 627 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
628 (add-hook 'c-mode-hook |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
629 '(lambda () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
630 (setq cmpl-syntax-table cmpl-c-syntax-table))) |
56 | 631 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
632 (add-hook 'fortran-mode-hook |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
633 '(lambda () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
634 (setq cmpl-syntax-table cmpl-fortran-syntax-table) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
635 (completion-setup-fortran-mode))) |
56 | 636 |
637 ;;;----------------------------------------------- | |
638 ;;; Symbol functions | |
639 ;;;----------------------------------------------- | |
640 (defvar cmpl-symbol-start nil | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
641 "Holds first character of symbol, after any completion symbol function.") |
56 | 642 (defvar cmpl-symbol-end nil |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
643 "Holds last character of symbol, after any completion symbol function.") |
56 | 644 ;;; These are temp. vars. we use to avoid using let. |
645 ;;; Why ? Small speed improvement. | |
646 (defvar cmpl-saved-syntax nil) | |
647 (defvar cmpl-saved-point nil) | |
648 | |
649 (defun symbol-under-point () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
650 "Returns the symbol that the point is currently on. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
651 But only if it is longer than `completion-min-length'." |
56 | 652 (setq cmpl-saved-syntax (syntax-table)) |
653 (set-syntax-table cmpl-syntax-table) | |
654 (cond | |
655 ;; Cursor is on following-char and after preceding-char | |
656 ((memq (char-syntax (following-char)) '(?w ?_)) | |
657 (setq cmpl-saved-point (point) | |
658 cmpl-symbol-start (scan-sexps (1+ cmpl-saved-point) -1) | |
659 cmpl-symbol-end (scan-sexps cmpl-saved-point 1)) | |
660 ;; remove chars to ignore at the start | |
661 (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w) | |
662 (goto-char cmpl-symbol-start) | |
663 (forward-word 1) | |
664 (setq cmpl-symbol-start (point)) | |
665 (goto-char cmpl-saved-point) | |
666 )) | |
667 ;; remove chars to ignore at the end | |
668 (cond ((= (char-syntax (char-after (1- cmpl-symbol-end))) ?w) | |
669 (goto-char cmpl-symbol-end) | |
670 (forward-word -1) | |
671 (setq cmpl-symbol-end (point)) | |
672 (goto-char cmpl-saved-point) | |
673 )) | |
674 ;; restore state | |
675 (set-syntax-table cmpl-saved-syntax) | |
676 ;; Return completion if the length is reasonable | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
677 (if (and (<= (cmpl-read-time-eval completion-min-length) |
56 | 678 (- cmpl-symbol-end cmpl-symbol-start)) |
679 (<= (- cmpl-symbol-end cmpl-symbol-start) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
680 (cmpl-read-time-eval completion-max-length))) |
56 | 681 (buffer-substring cmpl-symbol-start cmpl-symbol-end)) |
682 ) | |
683 (t | |
684 ;; restore table if no symbol | |
685 (set-syntax-table cmpl-saved-syntax) | |
686 nil) | |
687 )) | |
688 | |
689 ;;; tests for symbol-under-point | |
690 ;;; `^' indicates cursor pos. where value is returned | |
691 ;;; simple-word-test | |
692 ;;; ^^^^^^^^^^^^^^^^ --> simple-word-test | |
693 ;;; _harder_word_test_ | |
694 ;;; ^^^^^^^^^^^^^^^^^^ --> harder_word_test | |
695 ;;; .___.______. | |
696 ;;; --> nil | |
697 ;;; /foo/bar/quux.hello | |
698 ;;; ^^^^^^^^^^^^^^^^^^^ --> /foo/bar/quux.hello | |
699 ;;; | |
700 | |
701 (defun symbol-before-point () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
702 "Returns a string of the symbol immediately before point. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
703 Returns nil if there isn't one longer than `completion-min-length'." |
56 | 704 ;; This is called when a word separator is typed so it must be FAST ! |
705 (setq cmpl-saved-syntax (syntax-table)) | |
706 (set-syntax-table cmpl-syntax-table) | |
707 ;; Cursor is on following-char and after preceding-char | |
708 (cond ((= (setq cmpl-preceding-syntax (char-syntax (preceding-char))) ?_) | |
709 ;; No chars. to ignore at end | |
710 (setq cmpl-symbol-end (point) | |
711 cmpl-symbol-start (scan-sexps (1+ cmpl-symbol-end) -1) | |
712 ) | |
713 ;; remove chars to ignore at the start | |
714 (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w) | |
715 (goto-char cmpl-symbol-start) | |
716 (forward-word 1) | |
717 (setq cmpl-symbol-start (point)) | |
718 (goto-char cmpl-symbol-end) | |
719 )) | |
720 ;; restore state | |
721 (set-syntax-table cmpl-saved-syntax) | |
722 ;; return value if long enough | |
723 (if (>= cmpl-symbol-end | |
724 (+ cmpl-symbol-start | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
725 (cmpl-read-time-eval completion-min-length))) |
56 | 726 (buffer-substring cmpl-symbol-start cmpl-symbol-end)) |
727 ) | |
728 ((= cmpl-preceding-syntax ?w) | |
729 ;; chars to ignore at end | |
730 (setq cmpl-saved-point (point) | |
731 cmpl-symbol-start (scan-sexps (1+ cmpl-saved-point) -1)) | |
732 ;; take off chars. from end | |
733 (forward-word -1) | |
734 (setq cmpl-symbol-end (point)) | |
735 ;; remove chars to ignore at the start | |
736 (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w) | |
737 (goto-char cmpl-symbol-start) | |
738 (forward-word 1) | |
739 (setq cmpl-symbol-start (point)) | |
740 )) | |
741 ;; restore state | |
742 (goto-char cmpl-saved-point) | |
743 (set-syntax-table cmpl-saved-syntax) | |
744 ;; Return completion if the length is reasonable | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
745 (if (and (<= (cmpl-read-time-eval completion-min-length) |
56 | 746 (- cmpl-symbol-end cmpl-symbol-start)) |
747 (<= (- cmpl-symbol-end cmpl-symbol-start) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
748 (cmpl-read-time-eval completion-max-length))) |
56 | 749 (buffer-substring cmpl-symbol-start cmpl-symbol-end)) |
750 ) | |
751 (t | |
752 ;; restore table if no symbol | |
753 (set-syntax-table cmpl-saved-syntax) | |
754 nil) | |
755 )) | |
756 | |
757 ;;; tests for symbol-before-point | |
758 ;;; `^' indicates cursor pos. where value is returned | |
759 ;;; simple-word-test | |
760 ;;; ^ --> nil | |
761 ;;; ^ --> nil | |
762 ;;; ^ --> simple-w | |
763 ;;; ^ --> simple-word-test | |
764 ;;; _harder_word_test_ | |
765 ;;; ^ --> harder_word_test | |
766 ;;; ^ --> harder_word_test | |
767 ;;; ^ --> harder | |
768 ;;; .___.... | |
769 ;;; --> nil | |
770 | |
771 (defun symbol-under-or-before-point () | |
772 ;;; This could be made slightly faster but it is better to avoid | |
773 ;;; copying all the code. | |
774 ;;; However, it is only used by the completion string prompter. | |
775 ;;; If it comes into common use, it could be rewritten. | |
776 (setq cmpl-saved-syntax (syntax-table)) | |
777 (set-syntax-table cmpl-syntax-table) | |
778 (cond ((memq (char-syntax (following-char)) '(?w ?_)) | |
779 (set-syntax-table cmpl-saved-syntax) | |
780 (symbol-under-point)) | |
781 (t | |
782 (set-syntax-table cmpl-saved-syntax) | |
783 (symbol-before-point)) | |
784 )) | |
785 | |
786 | |
787 (defun symbol-before-point-for-complete () | |
788 ;; "Returns a string of the symbol immediately before point | |
789 ;; or nil if there isn't one. Like symbol-before-point but doesn't trim the | |
790 ;; end chars." | |
791 ;; Cursor is on following-char and after preceding-char | |
792 (setq cmpl-saved-syntax (syntax-table)) | |
793 (set-syntax-table cmpl-syntax-table) | |
794 (cond ((memq (setq cmpl-preceding-syntax (char-syntax (preceding-char))) | |
795 '(?_ ?w)) | |
796 (setq cmpl-symbol-end (point) | |
797 cmpl-symbol-start (scan-sexps (1+ cmpl-symbol-end) -1) | |
798 ) | |
799 ;; remove chars to ignore at the start | |
800 (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w) | |
801 (goto-char cmpl-symbol-start) | |
802 (forward-word 1) | |
803 (setq cmpl-symbol-start (point)) | |
804 (goto-char cmpl-symbol-end) | |
805 )) | |
806 ;; restore state | |
807 (set-syntax-table cmpl-saved-syntax) | |
808 ;; Return completion if the length is reasonable | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
809 (if (and (<= (cmpl-read-time-eval |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
810 completion-prefix-min-length) |
56 | 811 (- cmpl-symbol-end cmpl-symbol-start)) |
812 (<= (- cmpl-symbol-end cmpl-symbol-start) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
813 (cmpl-read-time-eval completion-max-length))) |
56 | 814 (buffer-substring cmpl-symbol-start cmpl-symbol-end)) |
815 ) | |
816 (t | |
817 ;; restore table if no symbol | |
818 (set-syntax-table cmpl-saved-syntax) | |
819 nil) | |
820 )) | |
821 | |
822 ;;; tests for symbol-before-point-for-complete | |
823 ;;; `^' indicates cursor pos. where value is returned | |
824 ;;; simple-word-test | |
825 ;;; ^ --> nil | |
826 ;;; ^ --> nil | |
827 ;;; ^ --> simple-w | |
828 ;;; ^ --> simple-word-test | |
829 ;;; _harder_word_test_ | |
830 ;;; ^ --> harder_word_test | |
831 ;;; ^ --> harder_word_test_ | |
832 ;;; ^ --> harder_ | |
833 ;;; .___.... | |
834 ;;; --> nil | |
835 | |
836 | |
837 | |
838 ;;;--------------------------------------------------------------------------- | |
839 ;;; Statistics Recording | |
840 ;;;--------------------------------------------------------------------------- | |
841 | |
842 ;;; Note that the guts of this has been turned off. The guts | |
843 ;;; are in completion-stats.el. | |
844 | |
845 ;;;----------------------------------------------- | |
846 ;;; Conditionalizing code on *record-cmpl-statistics-p* | |
847 ;;;----------------------------------------------- | |
848 ;;; All statistics code outside this block should use this | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
849 (defmacro cmpl-statistics-block (&rest body)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
850 ;;; "Only executes body if we are recording statistics." |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
851 ;;; (list 'cond |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
852 ;;; (list* '*record-cmpl-statistics-p* body) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
853 ;;; )) |
56 | 854 |
855 ;;;----------------------------------------------- | |
856 ;;; Completion Sources | |
857 ;;;----------------------------------------------- | |
858 | |
859 ;; ID numbers | |
860 (defconst cmpl-source-unknown 0) | |
861 (defconst cmpl-source-init-file 1) | |
862 (defconst cmpl-source-file-parsing 2) | |
863 (defconst cmpl-source-separator 3) | |
864 (defconst cmpl-source-cursor-moves 4) | |
865 (defconst cmpl-source-interactive 5) | |
866 (defconst cmpl-source-cdabbrev 6) | |
867 (defconst num-cmpl-sources 7) | |
868 (defvar current-completion-source cmpl-source-unknown) | |
869 | |
870 | |
871 | |
872 ;;;--------------------------------------------------------------------------- | |
873 ;;; Completion Method #2: dabbrev-expand style | |
874 ;;;--------------------------------------------------------------------------- | |
875 ;;; | |
876 ;;; This method is used if there are no useful stored completions. It is | |
877 ;;; based on dabbrev-expand with these differences : | |
878 ;;; 1) Faster (we don't use regexps) | |
879 ;;; 2) case coercion handled correctly | |
880 ;;; This is called cdabbrev to differentiate it. | |
881 ;;; We simply search backwards through the file looking for words which | |
882 ;;; start with the same letters we are trying to complete. | |
883 ;;; | |
884 | |
885 (defvar cdabbrev-completions-tried nil) | |
886 ;;; "A list of all the cdabbrev completions since the last reset.") | |
887 | |
888 (defvar cdabbrev-current-point 0) | |
889 ;;; "The current point position the cdabbrev search is at.") | |
890 | |
891 (defvar cdabbrev-current-window nil) | |
892 ;;; "The current window we are looking for cdabbrevs in. T if looking in | |
893 ;;; (other-buffer), NIL if no more cdabbrevs.") | |
894 | |
895 (defvar cdabbrev-wrapped-p nil) | |
896 ;;; "T if the cdabbrev search has wrapped around the file.") | |
897 | |
898 (defvar cdabbrev-abbrev-string "") | |
899 (defvar cdabbrev-start-point 0) | |
900 | |
901 ;;; Test strings for cdabbrev | |
902 ;;; cdat-upcase ;;same namestring | |
903 ;;; CDAT-UPCASE ;;ok | |
904 ;;; cdat2 ;;too short | |
905 ;;; cdat-1-2-3-4 ;;ok | |
906 ;;; a-cdat-1 ;;doesn't start correctly | |
907 ;;; cdat-simple ;;ok | |
908 | |
909 | |
910 (defun reset-cdabbrev (abbrev-string &optional initial-completions-tried) | |
911 "Resets the cdabbrev search to search for abbrev-string. | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
912 INITIAL-COMPLETIONS-TRIED is a list of downcased strings to ignore |
56 | 913 during the search." |
914 (setq cdabbrev-abbrev-string abbrev-string | |
915 cdabbrev-completions-tried | |
916 (cons (downcase abbrev-string) initial-completions-tried) | |
917 ) | |
918 (reset-cdabbrev-window t) | |
919 ) | |
920 | |
921 (defun set-cdabbrev-buffer () | |
922 ;; cdabbrev-current-window must not be NIL | |
923 (set-buffer (if (eq cdabbrev-current-window t) | |
924 (other-buffer) | |
925 (window-buffer cdabbrev-current-window))) | |
926 ) | |
927 | |
928 | |
929 (defun reset-cdabbrev-window (&optional initializep) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
930 "Resets the cdabbrev search to search for abbrev-string." |
56 | 931 ;; Set the window |
932 (cond (initializep | |
933 (setq cdabbrev-current-window (selected-window)) | |
934 ) | |
935 ((eq cdabbrev-current-window t) | |
936 ;; Everything has failed | |
937 (setq cdabbrev-current-window nil)) | |
938 (cdabbrev-current-window | |
939 (setq cdabbrev-current-window (next-window cdabbrev-current-window)) | |
940 (if (eq cdabbrev-current-window (selected-window)) | |
941 ;; No more windows, try other buffer. | |
942 (setq cdabbrev-current-window t))) | |
943 ) | |
944 (when cdabbrev-current-window | |
945 (save-excursion | |
946 (set-cdabbrev-buffer) | |
947 (setq cdabbrev-current-point (point) | |
948 cdabbrev-start-point cdabbrev-current-point | |
949 cdabbrev-stop-point | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
950 (if completion-search-distance |
56 | 951 (max (point-min) |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
952 (- cdabbrev-start-point completion-search-distance)) |
56 | 953 (point-min)) |
954 cdabbrev-wrapped-p nil) | |
955 ))) | |
956 | |
957 (defun next-cdabbrev () | |
958 "Return the next possible cdabbrev expansion or nil if there isn't one. | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
959 `reset-cdabbrev' must've been called already. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
960 This is sensitive to `case-fold-search'." |
56 | 961 ;; note that case-fold-search affects the behavior of this function |
962 ;; Bug: won't pick up an expansion that starts at the top of buffer | |
963 (when cdabbrev-current-window | |
964 (let (saved-point | |
965 saved-syntax | |
966 (expansion nil) | |
967 downcase-expansion tried-list syntax saved-point-2) | |
968 (save-excursion | |
969 (unwind-protect | |
970 (progn | |
971 ;; Switch to current completion buffer | |
972 (set-cdabbrev-buffer) | |
973 ;; Save current buffer state | |
974 (setq saved-point (point) | |
975 saved-syntax (syntax-table)) | |
976 ;; Restore completion state | |
977 (set-syntax-table cmpl-syntax-table) | |
978 (goto-char cdabbrev-current-point) | |
979 ;; Loop looking for completions | |
980 (while | |
981 ;; This code returns t if it should loop again | |
982 (cond | |
983 (;; search for the string | |
984 (search-backward cdabbrev-abbrev-string cdabbrev-stop-point t) | |
985 ;; return nil if the completion is valid | |
986 (not | |
987 (and | |
988 ;; does it start with a separator char ? | |
989 (or (= (setq syntax (char-syntax (preceding-char))) ? ) | |
990 (and (= syntax ?w) | |
991 ;; symbol char to ignore at end. Are we at end ? | |
992 (progn | |
993 (setq saved-point-2 (point)) | |
994 (forward-word -1) | |
995 (prog1 | |
996 (= (char-syntax (preceding-char)) ? ) | |
997 (goto-char saved-point-2) | |
998 )))) | |
999 ;; is the symbol long enough ? | |
1000 (setq expansion (symbol-under-point)) | |
1001 ;; have we not tried this one before | |
1002 (progn | |
1003 ;; See if we've already used it | |
1004 (setq tried-list cdabbrev-completions-tried | |
1005 downcase-expansion (downcase expansion)) | |
1006 (while (and tried-list | |
1007 (not (string-equal downcase-expansion | |
1008 (car tried-list)))) | |
1009 ;; Already tried, don't choose this one | |
1010 (setq tried-list (cdr tried-list)) | |
1011 ) | |
1012 ;; at this point tried-list will be nil if this | |
1013 ;; expansion has not yet been tried | |
1014 (if tried-list | |
1015 (setq expansion nil) | |
1016 t) | |
1017 )))) | |
1018 ;; search failed | |
1019 (cdabbrev-wrapped-p | |
1020 ;; If already wrapped, then we've failed completely | |
1021 nil) | |
1022 (t | |
1023 ;; need to wrap | |
1024 (goto-char (setq cdabbrev-current-point | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1025 (if completion-search-distance |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1026 (min (point-max) (+ cdabbrev-start-point completion-search-distance)) |
56 | 1027 (point-max)))) |
1028 | |
1029 (setq cdabbrev-wrapped-p t)) | |
1030 )) | |
1031 ;; end of while loop | |
1032 (cond (expansion | |
1033 ;; successful | |
1034 (setq cdabbrev-completions-tried | |
1035 (cons downcase-expansion cdabbrev-completions-tried) | |
1036 cdabbrev-current-point (point)))) | |
1037 ) | |
1038 (set-syntax-table saved-syntax) | |
1039 (goto-char saved-point) | |
1040 )) | |
1041 ;; If no expansion, go to next window | |
1042 (cond (expansion) | |
1043 (t (reset-cdabbrev-window) | |
1044 (next-cdabbrev))) | |
1045 ))) | |
1046 | |
1047 ;;; The following must be eval'd in the minibuffer :: | |
1048 ;;; (reset-cdabbrev "cdat") | |
1049 ;;; (next-cdabbrev) --> "cdat-simple" | |
1050 ;;; (next-cdabbrev) --> "cdat-1-2-3-4" | |
1051 ;;; (next-cdabbrev) --> "CDAT-UPCASE" | |
1052 ;;; (next-cdabbrev) --> "cdat-wrapping" | |
1053 ;;; (next-cdabbrev) --> "cdat_start_sym" | |
1054 ;;; (next-cdabbrev) --> nil | |
1055 ;;; (next-cdabbrev) --> nil | |
1056 ;;; (next-cdabbrev) --> nil | |
1057 | |
1058 ;;; _cdat_start_sym | |
1059 ;;; cdat-wrapping | |
1060 | |
1061 | |
1062 ;;;--------------------------------------------------------------------------- | |
1063 ;;; Completion Database | |
1064 ;;;--------------------------------------------------------------------------- | |
1065 | |
1066 ;;; We use two storage modes for the two search types :: | |
1067 ;;; 1) Prefix {cmpl-prefix-obarray} for looking up possible completions | |
1068 ;;; Used by search-completion-next | |
1069 ;;; the value of the symbol is nil or a cons of head and tail pointers | |
1070 ;;; 2) Interning {cmpl-obarray} to see if it's in the database | |
1071 ;;; Used by find-exact-completion, completion-in-database-p | |
1072 ;;; The value of the symbol is the completion entry | |
1073 | |
1074 ;;; bad things may happen if this length is changed due to the way | |
1075 ;;; GNU implements obarrays | |
1076 (defconst cmpl-obarray-length 511) | |
1077 | |
1078 (defvar cmpl-prefix-obarray (make-vector cmpl-obarray-length 0) | |
1079 "An obarray used to store the downcased completion prefices. | |
1080 Each symbol is bound to a list of completion entries.") | |
1081 | |
1082 (defvar cmpl-obarray (make-vector cmpl-obarray-length 0) | |
1083 "An obarray used to store the downcased completions. | |
1084 Each symbol is bound to a single completion entry.") | |
1085 | |
1086 ;;;----------------------------------------------- | |
1087 ;;; Completion Entry Structure Definition | |
1088 ;;;----------------------------------------------- | |
1089 | |
1090 ;;; A completion entry is a LIST of string, prefix-symbol num-uses, and | |
1091 ;;; last-use-time (the time the completion was last used) | |
1092 ;;; last-use-time is T if the string should be kept permanently | |
1093 ;;; num-uses is incremented everytime the completion is used. | |
1094 | |
1095 ;;; We chose lists because (car foo) is faster than (aref foo 0) and the | |
1096 ;;; creation time is about the same. | |
1097 | |
1098 ;;; READER MACROS | |
1099 | |
1100 (defmacro completion-string (completion-entry) | |
1101 (list 'car completion-entry)) | |
1102 | |
1103 (defmacro completion-num-uses (completion-entry) | |
1104 ;; "The number of times it has used. Used to decide whether to save | |
1105 ;; it." | |
1106 (list 'car (list 'cdr completion-entry))) | |
1107 | |
1108 (defmacro completion-last-use-time (completion-entry) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1109 ;; "The time it was last used. In hours since origin. Used to decide |
56 | 1110 ;; whether to save it. T if one should always save it." |
1111 (list 'nth 2 completion-entry)) | |
1112 | |
1113 (defmacro completion-source (completion-entry) | |
1114 (list 'nth 3 completion-entry)) | |
1115 | |
1116 ;;; WRITER MACROS | |
1117 (defmacro set-completion-string (completion-entry string) | |
1118 (list 'setcar completion-entry string)) | |
1119 | |
1120 (defmacro set-completion-num-uses (completion-entry num-uses) | |
1121 (list 'setcar (list 'cdr completion-entry) num-uses)) | |
1122 | |
1123 (defmacro set-completion-last-use-time (completion-entry last-use-time) | |
1124 (list 'setcar (list 'cdr (list 'cdr completion-entry)) last-use-time)) | |
1125 | |
1126 ;;; CONSTRUCTOR | |
1127 (defun make-completion (string) | |
1128 "Returns a list of a completion entry." | |
1129 (list (list string 0 nil current-completion-source))) | |
1130 | |
1131 ;; Obsolete | |
1132 ;;(defmacro cmpl-prefix-entry-symbol (completion-entry) | |
1133 ;; (list 'car (list 'cdr completion-entry))) | |
1134 | |
1135 | |
1136 | |
1137 ;;;----------------------------------------------- | |
1138 ;;; Prefix symbol entry definition | |
1139 ;;;----------------------------------------------- | |
1140 ;;; A cons of (head . tail) | |
1141 | |
1142 ;;; READER Macros | |
1143 | |
1144 (defmacro cmpl-prefix-entry-head (prefix-entry) | |
1145 (list 'car prefix-entry)) | |
1146 | |
1147 (defmacro cmpl-prefix-entry-tail (prefix-entry) | |
1148 (list 'cdr prefix-entry)) | |
1149 | |
1150 ;;; WRITER Macros | |
1151 | |
1152 (defmacro set-cmpl-prefix-entry-head (prefix-entry new-head) | |
1153 (list 'setcar prefix-entry new-head)) | |
1154 | |
1155 (defmacro set-cmpl-prefix-entry-tail (prefix-entry new-tail) | |
1156 (list 'setcdr prefix-entry new-tail)) | |
1157 | |
1158 ;;; Contructor | |
1159 | |
1160 (defun make-cmpl-prefix-entry (completion-entry-list) | |
1161 "Makes a new prefix entry containing only completion-entry." | |
1162 (cons completion-entry-list completion-entry-list)) | |
1163 | |
1164 ;;;----------------------------------------------- | |
1165 ;;; Completion Database - Utilities | |
1166 ;;;----------------------------------------------- | |
1167 | |
1168 (defun clear-all-completions () | |
1169 "Initializes the completion storage. All existing completions are lost." | |
1170 (interactive) | |
1171 (setq cmpl-prefix-obarray (make-vector cmpl-obarray-length 0)) | |
1172 (setq cmpl-obarray (make-vector cmpl-obarray-length 0)) | |
1173 (cmpl-statistics-block | |
1174 (record-clear-all-completions)) | |
1175 ) | |
1176 | |
1177 (defun list-all-completions () | |
1178 "Returns a list of all the known completion entries." | |
1179 (let ((return-completions nil)) | |
1180 (mapatoms 'list-all-completions-1 cmpl-prefix-obarray) | |
1181 return-completions)) | |
1182 | |
1183 (defun list-all-completions-1 (prefix-symbol) | |
1184 (if (boundp prefix-symbol) | |
1185 (setq return-completions | |
1186 (append (cmpl-prefix-entry-head (symbol-value prefix-symbol)) | |
1187 return-completions)))) | |
1188 | |
1189 (defun list-all-completions-by-hash-bucket () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1190 "Return list of lists of known completion entries, organized by hash bucket." |
56 | 1191 (let ((return-completions nil)) |
1192 (mapatoms 'list-all-completions-by-hash-bucket-1 cmpl-prefix-obarray) | |
1193 return-completions)) | |
1194 | |
1195 (defun list-all-completions-by-hash-bucket-1 (prefix-symbol) | |
1196 (if (boundp prefix-symbol) | |
1197 (setq return-completions | |
1198 (cons (cmpl-prefix-entry-head (symbol-value prefix-symbol)) | |
1199 return-completions)))) | |
1200 | |
1201 | |
1202 ;;;----------------------------------------------- | |
1203 ;;; Updating the database | |
1204 ;;;----------------------------------------------- | |
1205 ;;; | |
1206 ;;; These are the internal functions used to update the datebase | |
1207 ;;; | |
1208 ;;; | |
1209 (defvar completion-to-accept nil) | |
1210 ;;"Set to a string that is pending its acceptance." | |
1211 ;; this checked by the top level reading functions | |
1212 | |
1213 (defvar cmpl-db-downcase-string nil) | |
1214 ;; "Setup by find-exact-completion, etc. The given string, downcased." | |
1215 (defvar cmpl-db-symbol nil) | |
1216 ;; "The interned symbol corresponding to cmpl-db-downcase-string. | |
1217 ;; Set up by cmpl-db-symbol." | |
1218 (defvar cmpl-db-prefix-symbol nil) | |
1219 ;; "The interned prefix symbol corresponding to cmpl-db-downcase-string." | |
1220 (defvar cmpl-db-entry nil) | |
1221 (defvar cmpl-db-debug-p nil | |
1222 "Set to T if you want to debug the database.") | |
1223 | |
1224 ;;; READS | |
1225 (defun find-exact-completion (string) | |
1226 "Returns the completion entry for string or nil. | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1227 Sets up `cmpl-db-downcase-string' and `cmpl-db-symbol'." |
56 | 1228 (and (boundp (setq cmpl-db-symbol |
1229 (intern (setq cmpl-db-downcase-string (downcase string)) | |
1230 cmpl-obarray))) | |
1231 (symbol-value cmpl-db-symbol) | |
1232 )) | |
1233 | |
1234 (defun find-cmpl-prefix-entry (prefix-string) | |
1356 | 1235 "Returns the prefix entry for string. |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1236 Sets `cmpl-db-prefix-symbol'. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1237 Prefix-string must be exactly `completion-prefix-min-length' long |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1238 and downcased. Sets up `cmpl-db-prefix-symbol'." |
56 | 1239 (and (boundp (setq cmpl-db-prefix-symbol |
1240 (intern prefix-string cmpl-prefix-obarray))) | |
1241 (symbol-value cmpl-db-prefix-symbol))) | |
1242 | |
1243 (defvar inside-locate-completion-entry nil) | |
1244 ;; used to trap lossage in silent error correction | |
1245 | |
1246 (defun locate-completion-entry (completion-entry prefix-entry) | |
1356 | 1247 "Locates the completion entry. |
1248 Returns a pointer to the element before the completion entry or nil if | |
1249 the completion entry is at the head. | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1250 Must be called after `find-exact-completion'." |
56 | 1251 (let ((prefix-list (cmpl-prefix-entry-head prefix-entry)) |
1252 next-prefix-list | |
1253 ) | |
1254 (cond | |
1255 ((not (eq (car prefix-list) completion-entry)) | |
1256 ;; not already at head | |
1257 (while (and prefix-list | |
1258 (not (eq completion-entry | |
1259 (car (setq next-prefix-list (cdr prefix-list))) | |
1260 ))) | |
1261 (setq prefix-list next-prefix-list)) | |
1262 (cond (;; found | |
1263 prefix-list) | |
1264 ;; Didn't find it. Database is messed up. | |
1265 (cmpl-db-debug-p | |
1266 ;; not found, error if debug mode | |
1267 (error "Completion entry exists but not on prefix list - %s" | |
1268 string)) | |
1269 (inside-locate-completion-entry | |
1270 ;; recursive error: really scrod | |
1271 (locate-completion-db-error)) | |
1272 (t | |
1273 ;; Patch out | |
1274 (set cmpl-db-symbol nil) | |
1275 ;; Retry | |
1276 (locate-completion-entry-retry completion-entry) | |
1277 )))))) | |
1278 | |
1279 (defun locate-completion-entry-retry (old-entry) | |
1280 (let ((inside-locate-completion-entry t)) | |
1281 (add-completion (completion-string old-entry) | |
1282 (completion-num-uses old-entry) | |
1283 (completion-last-use-time old-entry)) | |
1284 (let ((cmpl-entry (find-exact-completion (completion-string old-entry))) | |
1285 (pref-entry | |
1286 (if cmpl-entry | |
1287 (find-cmpl-prefix-entry | |
1288 (substring cmpl-db-downcase-string | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1289 0 completion-prefix-min-length)))) |
56 | 1290 ) |
1291 (if (and cmpl-entry pref-entry) | |
1292 ;; try again | |
1293 (locate-completion-entry cmpl-entry pref-entry) | |
1294 ;; still losing | |
1295 (locate-completion-db-error)) | |
1296 ))) | |
1297 | |
1298 (defun locate-completion-db-error () | |
1299 ;; recursive error: really scrod | |
1300 (error "Completion database corrupted. Try M-x clear-all-completions. Send bug report.") | |
1301 ) | |
1302 | |
1303 ;;; WRITES | |
1304 (defun add-completion-to-tail-if-new (string) | |
1356 | 1305 "If STRING is not in the database add it to appropriate prefix list. |
1306 STRING is added to the end of the approppriate prefix list with | |
1307 num-uses = 0. The database is unchanged if it is there. STRING must be | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1308 longer than `completion-prefix-min-length'. |
56 | 1309 This must be very fast. |
1310 Returns the completion entry." | |
1311 (or (find-exact-completion string) | |
1312 ;; not there | |
1313 (let (;; create an entry | |
1314 (entry (make-completion string)) | |
1315 ;; setup the prefix | |
1316 (prefix-entry (find-cmpl-prefix-entry | |
1317 (substring cmpl-db-downcase-string 0 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1318 (cmpl-read-time-eval |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1319 completion-prefix-min-length)))) |
56 | 1320 ) |
1321 ;; The next two forms should happen as a unit (atomically) but | |
1322 ;; no fatal errors should result if that is not the case. | |
1323 (cond (prefix-entry | |
1324 ;; These two should be atomic, but nothing fatal will happen | |
1325 ;; if they're not. | |
1326 (setcdr (cmpl-prefix-entry-tail prefix-entry) entry) | |
1327 (set-cmpl-prefix-entry-tail prefix-entry entry)) | |
1328 (t | |
1329 (set cmpl-db-prefix-symbol (make-cmpl-prefix-entry entry)) | |
1330 )) | |
1331 ;; statistics | |
1332 (cmpl-statistics-block | |
1333 (note-added-completion)) | |
1334 ;; set symbol | |
1335 (set cmpl-db-symbol (car entry)) | |
1336 ))) | |
1337 | |
1338 (defun add-completion-to-head (string) | |
1356 | 1339 "If STRING is not in the database, add it to prefix list. |
1340 STRING is added to the head of the approppriate prefix list. Otherwise | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1341 it is moved to the head of the list. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1342 STRING must be longer than `completion-prefix-min-length'. |
56 | 1343 Updates the saved string with the supplied string. |
1344 This must be very fast. | |
1345 Returns the completion entry." | |
1346 ;; Handle pending acceptance | |
1347 (if completion-to-accept (accept-completion)) | |
1348 ;; test if already in database | |
1349 (if (setq cmpl-db-entry (find-exact-completion string)) | |
1350 ;; found | |
1351 (let* ((prefix-entry (find-cmpl-prefix-entry | |
1352 (substring cmpl-db-downcase-string 0 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1353 (cmpl-read-time-eval |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1354 completion-prefix-min-length)))) |
56 | 1355 (splice-ptr (locate-completion-entry cmpl-db-entry prefix-entry)) |
1356 (cmpl-ptr (cdr splice-ptr)) | |
1357 ) | |
1358 ;; update entry | |
1359 (set-completion-string cmpl-db-entry string) | |
1360 ;; move to head (if necessary) | |
1361 (cond (splice-ptr | |
1362 ;; These should all execute atomically but it is not fatal if | |
1363 ;; they don't. | |
1364 ;; splice it out | |
1365 (or (setcdr splice-ptr (cdr cmpl-ptr)) | |
1366 ;; fix up tail if necessary | |
1367 (set-cmpl-prefix-entry-tail prefix-entry splice-ptr)) | |
1368 ;; splice in at head | |
1369 (setcdr cmpl-ptr (cmpl-prefix-entry-head prefix-entry)) | |
1370 (set-cmpl-prefix-entry-head prefix-entry cmpl-ptr) | |
1371 )) | |
1372 cmpl-db-entry) | |
1373 ;; not there | |
1374 (let (;; create an entry | |
1375 (entry (make-completion string)) | |
1376 ;; setup the prefix | |
1377 (prefix-entry (find-cmpl-prefix-entry | |
1378 (substring cmpl-db-downcase-string 0 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1379 (cmpl-read-time-eval |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1380 completion-prefix-min-length)))) |
56 | 1381 ) |
1382 (cond (prefix-entry | |
1383 ;; Splice in at head | |
1384 (setcdr entry (cmpl-prefix-entry-head prefix-entry)) | |
1385 (set-cmpl-prefix-entry-head prefix-entry entry)) | |
1386 (t | |
1387 ;; Start new prefix entry | |
1388 (set cmpl-db-prefix-symbol (make-cmpl-prefix-entry entry)) | |
1389 )) | |
1390 ;; statistics | |
1391 (cmpl-statistics-block | |
1392 (note-added-completion)) | |
1393 ;; Add it to the symbol | |
1394 (set cmpl-db-symbol (car entry)) | |
1395 ))) | |
1396 | |
1397 (defun delete-completion (string) | |
1356 | 1398 "Deletes the completion from the database. |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1399 String must be longer than `completion-prefix-min-length'." |
56 | 1400 ;; Handle pending acceptance |
1401 (if completion-to-accept (accept-completion)) | |
1402 (if (setq cmpl-db-entry (find-exact-completion string)) | |
1403 ;; found | |
1404 (let* ((prefix-entry (find-cmpl-prefix-entry | |
1405 (substring cmpl-db-downcase-string 0 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1406 (cmpl-read-time-eval |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1407 completion-prefix-min-length)))) |
56 | 1408 (splice-ptr (locate-completion-entry cmpl-db-entry prefix-entry)) |
1409 ) | |
1410 ;; delete symbol reference | |
1411 (set cmpl-db-symbol nil) | |
1412 ;; remove from prefix list | |
1413 (cond (splice-ptr | |
1414 ;; not at head | |
1415 (or (setcdr splice-ptr (cdr (cdr splice-ptr))) | |
1416 ;; fix up tail if necessary | |
1417 (set-cmpl-prefix-entry-tail prefix-entry splice-ptr)) | |
1418 ) | |
1419 (t | |
1420 ;; at head | |
1421 (or (set-cmpl-prefix-entry-head | |
1422 prefix-entry (cdr (cmpl-prefix-entry-head prefix-entry))) | |
1423 ;; List is now empty | |
1424 (set cmpl-db-prefix-symbol nil)) | |
1425 )) | |
1426 (cmpl-statistics-block | |
1427 (note-completion-deleted)) | |
1428 ) | |
1429 (error "Unknown completion: %s. Couldn't delete it." string) | |
1430 )) | |
1431 | |
1432 ;;; Tests -- | |
1433 ;;; - Add and Find - | |
1434 ;;; (add-completion-to-head "banana") --> ("banana" 0 nil 0) | |
1435 ;;; (find-exact-completion "banana") --> ("banana" 0 nil 0) | |
1436 ;;; (find-exact-completion "bana") --> nil | |
1437 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...)) | |
1438 ;;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...)) | |
1439 ;;; (add-completion-to-head "banish") --> ("banish" 0 nil 0) | |
1440 ;;; (find-exact-completion "banish") --> ("banish" 0 nil 0) | |
1441 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banish" ...) ("banana" ...)) | |
1442 ;;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...)) | |
1443 ;;; (add-completion-to-head "banana") --> ("banana" 0 nil 0) | |
1444 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...)) | |
1445 ;;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...)) | |
1446 ;;; | |
1447 ;;; - Deleting - | |
1448 ;;; (add-completion-to-head "banner") --> ("banner" 0 nil 0) | |
1449 ;;; (delete-completion "banner") | |
1450 ;;; (find-exact-completion "banner") --> nil | |
1451 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...)) | |
1452 ;;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...)) | |
1453 ;;; (add-completion-to-head "banner") --> ("banner" 0 nil 0) | |
1454 ;;; (delete-completion "banana") | |
1455 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banner" ...) ("banish" ...)) | |
1456 ;;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...)) | |
1457 ;;; (delete-completion "banner") | |
1458 ;;; (delete-completion "banish") | |
1459 ;;; (find-cmpl-prefix-entry "ban") --> nil | |
1460 ;;; (delete-completion "banner") --> error | |
1461 ;;; | |
1462 ;;; - Tail - | |
1463 ;;; (add-completion-to-tail-if-new "banana") --> ("banana" 0 nil 0) | |
1464 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...)) | |
1465 ;;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...)) | |
1466 ;;; (add-completion-to-tail-if-new "banish") --> ("banish" 0 nil 0) | |
1467 ;;; (car (find-cmpl-prefix-entry "ban")) -->(("banana" ...) ("banish" ...)) | |
1468 ;;; (cdr (find-cmpl-prefix-entry "ban")) -->(("banish" ...)) | |
1469 ;;; | |
1470 | |
1471 | |
1472 ;;;--------------------------------------------------------------------------- | |
1473 ;;; Database Update :: Interface level routines | |
1474 ;;;--------------------------------------------------------------------------- | |
1475 ;;; | |
1476 ;;; These lie on top of the database ref. functions but below the standard | |
1477 ;;; user interface level | |
1478 | |
1479 | |
1480 (defun interactive-completion-string-reader (prompt) | |
1481 (let* ((default (symbol-under-or-before-point)) | |
1482 (new-prompt | |
1483 (if default | |
1484 (format "%s: (default: %s) " prompt default) | |
1485 (format "%s: " prompt)) | |
1486 ) | |
1487 (read (completing-read new-prompt cmpl-obarray)) | |
1488 ) | |
1489 (if (zerop (length read)) (setq read (or default ""))) | |
1490 (list read) | |
1491 )) | |
1492 | |
1493 (defun check-completion-length (string) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1494 (if (< (length string) completion-min-length) |
56 | 1495 (error "The string \"%s\" is too short to be saved as a completion." |
1496 string) | |
1497 (list string))) | |
1498 | |
1499 (defun add-completion (string &optional num-uses last-use-time) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1500 "Add STRING to completion list, or move it to head of list. |
56 | 1501 The completion is altered appropriately if num-uses and/or last-use-time is |
1502 specified." | |
1503 (interactive (interactive-completion-string-reader "Completion to add")) | |
1504 (check-completion-length string) | |
1505 (let* ((current-completion-source (if (interactive-p) | |
1506 cmpl-source-interactive | |
1507 current-completion-source)) | |
1508 (entry (add-completion-to-head string))) | |
1509 | |
1510 (if num-uses (set-completion-num-uses entry num-uses)) | |
1511 (if last-use-time | |
1512 (set-completion-last-use-time entry last-use-time)) | |
1513 )) | |
1514 | |
1515 (defun add-permanent-completion (string) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1516 "Add STRING if it isn't already listed, and mark it permanent." |
56 | 1517 (interactive |
1518 (interactive-completion-string-reader "Completion to add permanently")) | |
1519 (let ((current-completion-source (if (interactive-p) | |
1520 cmpl-source-interactive | |
1521 current-completion-source)) | |
1522 ) | |
1523 (add-completion string nil t) | |
1524 )) | |
1525 | |
1526 (defun kill-completion (string) | |
1527 (interactive (interactive-completion-string-reader "Completion to kill")) | |
1528 (check-completion-length string) | |
1529 (delete-completion string) | |
1530 ) | |
1531 | |
1532 (defun accept-completion () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1533 "Accepts the pending completion in `completion-to-accept'. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1534 This bumps num-uses. Called by `add-completion-to-head' and |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1535 `completion-search-reset'." |
56 | 1536 (let ((string completion-to-accept) |
1537 ;; if this is added afresh here, then it must be a cdabbrev | |
1538 (current-completion-source cmpl-source-cdabbrev) | |
1539 entry | |
1540 ) | |
1541 (setq completion-to-accept nil) | |
1542 (setq entry (add-completion-to-head string)) | |
1543 (set-completion-num-uses entry (1+ (completion-num-uses entry))) | |
1544 (setq cmpl-completions-accepted-p t) | |
1545 )) | |
1546 | |
1547 (defun use-completion-under-point () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1548 "Add the completion symbol underneath the point into the completion buffer." |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1549 (let ((string (and enable-completion (symbol-under-point))) |
56 | 1550 (current-completion-source cmpl-source-cursor-moves)) |
1551 (if string (add-completion-to-head string)))) | |
1552 | |
1553 (defun use-completion-before-point () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1554 "Add the completion symbol before point into the completion buffer." |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1555 (let ((string (and enable-completion (symbol-before-point))) |
56 | 1556 (current-completion-source cmpl-source-cursor-moves)) |
1557 (if string (add-completion-to-head string)))) | |
1558 | |
1559 (defun use-completion-under-or-before-point () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1560 "Add the completion symbol before point into the completion buffer." |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1561 (let ((string (and enable-completion (symbol-under-or-before-point))) |
56 | 1562 (current-completion-source cmpl-source-cursor-moves)) |
1563 (if string (add-completion-to-head string)))) | |
1564 | |
1565 (defun use-completion-before-separator () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1566 "Add the completion symbol before point into the completion buffer. |
1356 | 1567 Completions added this way will automatically be saved if |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1568 `completion-on-separator-character' is non-nil." |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1569 (let ((string (and enable-completion (symbol-before-point))) |
56 | 1570 (current-completion-source cmpl-source-separator) |
1571 entry) | |
1572 (cmpl-statistics-block | |
1573 (note-separator-character string) | |
1574 ) | |
1575 (cond (string | |
1576 (setq entry (add-completion-to-head string)) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1577 (when (and completion-on-separator-character |
56 | 1578 (zerop (completion-num-uses entry))) |
1579 (set-completion-num-uses entry 1) | |
1580 (setq cmpl-completions-accepted-p t) | |
1581 ))) | |
1582 )) | |
1583 | |
1584 ;;; Tests -- | |
1585 ;;; - Add and Find - | |
1586 ;;; (add-completion "banana" 5 10) | |
1587 ;;; (find-exact-completion "banana") --> ("banana" 5 10 0) | |
1588 ;;; (add-completion "banana" 6) | |
1589 ;;; (find-exact-completion "banana") --> ("banana" 6 10 0) | |
1590 ;;; (add-completion "banish") | |
1591 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banish" ...) ("banana" ...)) | |
1592 ;;; | |
1593 ;;; - Accepting - | |
1594 ;;; (setq completion-to-accept "banana") | |
1595 ;;; (accept-completion) | |
1596 ;;; (find-exact-completion "banana") --> ("banana" 7 10) | |
1597 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...)) | |
1598 ;;; (setq completion-to-accept "banish") | |
1599 ;;; (add-completion "banner") | |
1600 ;;; (car (find-cmpl-prefix-entry "ban")) | |
1601 ;;; --> (("banner" ...) ("banish" 1 ...) ("banana" 7 ...)) | |
1602 ;;; | |
1603 ;;; - Deleting - | |
1604 ;;; (kill-completion "banish") | |
1605 ;;; (car (find-cmpl-prefix-entry "ban")) --> (("banner" ...) ("banana" ...)) | |
1606 | |
1607 | |
1608 ;;;--------------------------------------------------------------------------- | |
1609 ;;; Searching the database | |
1610 ;;;--------------------------------------------------------------------------- | |
1611 ;;; Functions outside this block must call completion-search-reset followed | |
1612 ;;; by calls to completion-search-next or completion-search-peek | |
1613 ;;; | |
1614 | |
1615 ;;; Status variables | |
1616 ;; Commented out to improve loading speed | |
1617 (defvar cmpl-test-string "") | |
1618 ;; "The current string used by completion-search-next." | |
1619 (defvar cmpl-test-regexp "") | |
1620 ;; "The current regexp used by completion-search-next. | |
1621 ;; (derived from cmpl-test-string)" | |
1622 (defvar cmpl-last-index 0) | |
1623 ;; "The last index that completion-search-next was called with." | |
1624 (defvar cmpl-cdabbrev-reset-p nil) | |
1625 ;; "Set to t when cdabbrevs have been reset." | |
1626 (defvar cmpl-next-possibilities nil) | |
1627 ;; "A pointer to the element BEFORE the next set of possible completions. | |
1628 ;; cadr of this is the cmpl-next-possibility" | |
1629 (defvar cmpl-starting-possibilities nil) | |
1630 ;; "The initial list of starting possibilities." | |
1631 (defvar cmpl-next-possibility nil) | |
1632 ;; "The cached next possibility." | |
1633 (defvar cmpl-tried-list nil) | |
1634 ;; "A downcased list of all the completions we have tried." | |
1635 | |
1636 | |
1637 (defun completion-search-reset (string) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1638 "Set up the for completion searching for STRING. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1639 STRING must be longer than `completion-prefix-min-length'." |
56 | 1640 (if completion-to-accept (accept-completion)) |
1641 (setq cmpl-starting-possibilities | |
1642 (cmpl-prefix-entry-head | |
1643 (find-cmpl-prefix-entry (downcase (substring string 0 3)))) | |
1644 cmpl-test-string string | |
1645 cmpl-test-regexp (concat (regexp-quote string) ".")) | |
1646 (completion-search-reset-1) | |
1647 ) | |
1648 | |
1649 (defun completion-search-reset-1 () | |
1650 (setq cmpl-next-possibilities cmpl-starting-possibilities | |
1651 cmpl-next-possibility nil | |
1652 cmpl-cdabbrev-reset-p nil | |
1653 cmpl-last-index -1 | |
1654 cmpl-tried-list nil | |
1655 )) | |
1656 | |
1657 (defun completion-search-next (index) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1658 "Return the next completion entry. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1659 If INDEX is out of sequence, reset and start from the top. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1660 If there are no more entries, try cdabbrev and returns only a string." |
56 | 1661 (cond |
1662 ((= index (setq cmpl-last-index (1+ cmpl-last-index))) | |
1663 (completion-search-peek t)) | |
1664 ((minusp index) | |
1665 (completion-search-reset-1) | |
1666 (setq cmpl-last-index index) | |
1667 ;; reverse the possibilities list | |
1668 (setq cmpl-next-possibilities (reverse cmpl-starting-possibilities)) | |
1669 ;; do a "normal" search | |
1670 (while (and (completion-search-peek nil) | |
1671 (minusp (setq index (1+ index)))) | |
1672 (setq cmpl-next-possibility nil) | |
1673 ) | |
1674 (cond ((not cmpl-next-possibilities)) | |
1675 ;; If no more possibilities, leave it that way | |
1676 ((= -1 cmpl-last-index) | |
1677 ;; next completion is at index 0. reset next-possibility list | |
1678 ;; to start at beginning | |
1679 (setq cmpl-next-possibilities cmpl-starting-possibilities)) | |
1680 (t | |
1681 ;; otherwise point to one before current | |
1682 (setq cmpl-next-possibilities | |
1683 (nthcdr (- (length cmpl-starting-possibilities) | |
1684 (length cmpl-next-possibilities)) | |
1685 cmpl-starting-possibilities)) | |
1686 ))) | |
1687 (t | |
1688 ;; non-negative index, reset and search | |
1689 ;;(prin1 'reset) | |
1690 (completion-search-reset-1) | |
1691 (setq cmpl-last-index index) | |
1692 (while (and (completion-search-peek t) | |
1693 (not (minusp (setq index (1- index))))) | |
1694 (setq cmpl-next-possibility nil) | |
1695 )) | |
1696 ) | |
1697 (prog1 | |
1698 cmpl-next-possibility | |
1699 (setq cmpl-next-possibility nil) | |
1700 )) | |
1701 | |
1702 | |
1703 (defun completion-search-peek (use-cdabbrev) | |
1704 "Returns the next completion entry without actually moving the pointers. | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1705 Calling this again or calling `completion-search-next' results in the same |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1706 string being returned. Depends on `case-fold-search'. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1707 If there are no more entries, try cdabbrev and then return only a string." |
56 | 1708 (cond |
1709 ;; return the cached value if we have it | |
1710 (cmpl-next-possibility) | |
1711 ((and cmpl-next-possibilities | |
1712 ;; still a few possibilities left | |
1713 (progn | |
1714 (while | |
1715 (and (not (eq 0 (string-match cmpl-test-regexp | |
1716 (completion-string (car cmpl-next-possibilities))))) | |
1717 (setq cmpl-next-possibilities (cdr cmpl-next-possibilities)) | |
1718 )) | |
1719 cmpl-next-possibilities | |
1720 )) | |
1721 ;; successful match | |
1722 (setq cmpl-next-possibility (car cmpl-next-possibilities) | |
1723 cmpl-tried-list (cons (downcase (completion-string cmpl-next-possibility)) | |
1724 cmpl-tried-list) | |
1725 cmpl-next-possibilities (cdr cmpl-next-possibilities) | |
1726 ) | |
1727 cmpl-next-possibility) | |
1728 (use-cdabbrev | |
1729 ;; unsuccessful, use cdabbrev | |
1730 (cond ((not cmpl-cdabbrev-reset-p) | |
1731 (reset-cdabbrev cmpl-test-string cmpl-tried-list) | |
1732 (setq cmpl-cdabbrev-reset-p t) | |
1733 )) | |
1734 (setq cmpl-next-possibility (next-cdabbrev)) | |
1735 ) | |
1736 ;; Completely unsuccessful, return nil | |
1737 )) | |
1738 | |
1739 ;;; Tests -- | |
1740 ;;; - Add and Find - | |
1741 ;;; (add-completion "banana") | |
1742 ;;; (completion-search-reset "ban") | |
1743 ;;; (completion-search-next 0) --> "banana" | |
1744 ;;; | |
1745 ;;; - Discrimination - | |
1746 ;;; (add-completion "cumberland") | |
1747 ;;; (add-completion "cumberbund") | |
1748 ;;; cumbering | |
1749 ;;; (completion-search-reset "cumb") | |
1750 ;;; (completion-search-peek t) --> "cumberbund" | |
1751 ;;; (completion-search-next 0) --> "cumberbund" | |
1752 ;;; (completion-search-peek t) --> "cumberland" | |
1753 ;;; (completion-search-next 1) --> "cumberland" | |
1754 ;;; (completion-search-peek nil) --> nil | |
1755 ;;; (completion-search-next 2) --> "cumbering" {cdabbrev} | |
1756 ;;; (completion-search-next 3) --> nil or "cumming"{depends on context} | |
1757 ;;; (completion-search-next 1) --> "cumberland" | |
1758 ;;; (completion-search-peek t) --> "cumbering" {cdabbrev} | |
1759 ;;; | |
1760 ;;; - Accepting - | |
1761 ;;; (completion-search-next 1) --> "cumberland" | |
1762 ;;; (setq completion-to-accept "cumberland") | |
1763 ;;; (completion-search-reset "foo") | |
1764 ;;; (completion-search-reset "cum") | |
1765 ;;; (completion-search-next 0) --> "cumberland" | |
1766 ;;; | |
1767 ;;; - Deleting - | |
1768 ;;; (kill-completion "cumberland") | |
1769 ;;; cummings | |
1770 ;;; (completion-search-reset "cum") | |
1771 ;;; (completion-search-next 0) --> "cumberbund" | |
1772 ;;; (completion-search-next 1) --> "cummings" | |
1773 ;;; | |
1774 ;;; - Ignoring Capitalization - | |
1775 ;;; (completion-search-reset "CuMb") | |
1776 ;;; (completion-search-next 0) --> "cumberbund" | |
1777 | |
1778 | |
1779 | |
1780 ;;;----------------------------------------------- | |
1781 ;;; COMPLETE | |
1782 ;;;----------------------------------------------- | |
1783 | |
1784 (defun completion-mode () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1785 "Toggles whether or not to add new words to the completion database." |
56 | 1786 (interactive) |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1787 (setq enable-completion (not enable-completion)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1788 (message "Completion mode is now %s." (if enable-completion "ON" "OFF")) |
56 | 1789 ) |
1790 | |
1791 (defvar cmpl-current-index 0) | |
1792 (defvar cmpl-original-string nil) | |
1793 (defvar cmpl-last-insert-location -1) | |
1794 (defvar cmpl-leave-point-at-start nil) | |
1795 | |
1796 (defun complete (&optional arg) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1797 "Fill out a completion of the word before point. |
56 | 1798 Point is left at end. Consective calls rotate through all possibilities. |
1799 Prefix args :: | |
1800 control-u :: leave the point at the beginning of the completion rather | |
1801 than at the end. | |
1802 a number :: rotate through the possible completions by that amount | |
1803 `-' :: same as -1 (insert previous completion) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1804 {See the comments at the top of `completion.el' for more info.}" |
56 | 1805 (interactive "*p") |
1806 ;;; Set up variables | |
1807 (cond ((eq last-command this-command) | |
1808 ;; Undo last one | |
1809 (delete-region cmpl-last-insert-location (point)) | |
1810 ;; get next completion | |
1811 (setq cmpl-current-index (+ cmpl-current-index (or arg 1))) | |
1812 ) | |
1813 (t | |
1814 (if (not cmpl-initialized-p) | |
1815 (initialize-completions)) ;; make sure everything's loaded | |
1816 (cond ((consp current-prefix-arg) ;; control-u | |
1817 (setq arg 0) | |
1818 (setq cmpl-leave-point-at-start t) | |
1819 ) | |
1820 (t | |
1821 (setq cmpl-leave-point-at-start nil) | |
1822 )) | |
1823 ;; get string | |
1824 (setq cmpl-original-string (symbol-before-point-for-complete)) | |
1825 (cond ((not cmpl-original-string) | |
1826 (setq this-command 'failed-complete) | |
1827 (error "To complete, the point must be after a symbol at least %d character long." | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1828 completion-prefix-min-length))) |
56 | 1829 ;; get index |
1830 (setq cmpl-current-index (if current-prefix-arg arg 0)) | |
1831 ;; statistics | |
1832 (cmpl-statistics-block | |
1833 (note-complete-entered-afresh cmpl-original-string)) | |
1834 ;; reset database | |
1835 (completion-search-reset cmpl-original-string) | |
1836 ;; erase what we've got | |
1837 (delete-region cmpl-symbol-start cmpl-symbol-end) | |
1838 )) | |
1839 | |
1840 ;; point is at the point to insert the new symbol | |
1841 ;; Get the next completion | |
1842 (let* ((print-status-p | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1843 (and (>= baud-rate completion-prompt-speed-threshold) |
56 | 1844 (not (minibuffer-window-selected-p)))) |
1845 (insert-point (point)) | |
1846 (entry (completion-search-next cmpl-current-index)) | |
1847 string | |
1848 ) | |
1849 ;; entry is either a completion entry or a string (if cdabbrev) | |
1850 | |
1851 ;; If found, insert | |
1852 (cond (entry | |
1853 ;; Setup for proper case | |
1854 (setq string (if (stringp entry) | |
1855 entry (completion-string entry))) | |
1856 (setq string (cmpl-merge-string-cases | |
1857 string cmpl-original-string)) | |
1858 ;; insert | |
1859 (insert string) | |
1860 ;; accept it | |
1861 (setq completion-to-accept string) | |
1862 ;; fixup and cache point | |
1863 (cond (cmpl-leave-point-at-start | |
1864 (setq cmpl-last-insert-location (point)) | |
1865 (goto-char insert-point)) | |
1866 (t;; point at end, | |
1867 (setq cmpl-last-insert-location insert-point)) | |
1868 ) | |
1869 ;; statistics | |
1870 (cmpl-statistics-block | |
1871 (note-complete-inserted entry cmpl-current-index)) | |
1872 ;; Done ! cmpl-stat-complete-successful | |
1873 ;;display the next completion | |
1874 (cond | |
1875 ((and print-status-p | |
1876 ;; This updates the display and only prints if there | |
1877 ;; is no typeahead | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1878 (sit-for 0) |
56 | 1879 (setq entry |
1880 (completion-search-peek | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1881 completion-cdabbrev-prompt-flag))) |
56 | 1882 (setq string (if (stringp entry) |
1883 entry (completion-string entry))) | |
1884 (setq string (cmpl-merge-string-cases | |
1885 string cmpl-original-string)) | |
1886 (message "Next completion: %s" string) | |
1887 )) | |
1888 ) | |
1889 (t;; none found, insert old | |
1890 (insert cmpl-original-string) | |
1891 ;; Don't accept completions | |
1892 (setq completion-to-accept nil) | |
1893 ;; print message | |
1894 (if (and print-status-p (cmpl19-sit-for 0)) | |
1895 (message "No %scompletions." | |
1896 (if (eq this-command last-command) "more " ""))) | |
1897 ;; statistics | |
1898 (cmpl-statistics-block | |
1899 (record-complete-failed cmpl-current-index)) | |
1900 ;; Pretend that we were never here | |
1901 (setq this-command 'failed-complete) | |
1902 )))) | |
1903 | |
1904 ;;;----------------------------------------------- | |
1905 ;;; "Complete" Key Keybindings | |
1906 ;;;----------------------------------------------- | |
1907 | |
1908 (global-set-key "\M-\r" 'complete) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1909 (global-set-key [?\C-\r] 'complete) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1910 (define-key function-key-map [C-return] [?\C-\r]) |
56 | 1911 |
1912 ;;; Tests - | |
1913 ;;; (add-completion "cumberland") | |
1914 ;;; (add-completion "cumberbund") | |
1915 ;;; cum | |
1916 ;;; Cumber | |
1917 ;;; cumbering | |
1918 ;;; cumb | |
1919 | |
1920 | |
1921 ;;;--------------------------------------------------------------------------- | |
1922 ;;; Parsing definitions from files into the database | |
1923 ;;;--------------------------------------------------------------------------- | |
1924 | |
1925 ;;;----------------------------------------------- | |
1926 ;;; Top Level functions :: | |
1927 ;;;----------------------------------------------- | |
1928 | |
1929 ;;; User interface | |
1930 (defun add-completions-from-file (file) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1931 "Parse possible completions from a file and add them to data base." |
56 | 1932 (interactive "fFile: ") |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1933 (setq file (expand-file-name file)) |
56 | 1934 (let* ((buffer (get-file-buffer file)) |
1935 (buffer-already-there-p buffer) | |
1936 ) | |
1937 (when (not buffer-already-there-p) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1938 (let ((completions-merging-modes nil)) |
56 | 1939 (setq buffer (find-file-noselect file)) |
1940 )) | |
1941 (unwind-protect | |
1942 (save-excursion | |
1943 (set-buffer buffer) | |
1944 (add-completions-from-buffer) | |
1945 ) | |
1946 (when (not buffer-already-there-p) | |
1947 (kill-buffer buffer)) | |
1948 ))) | |
1949 | |
1950 (defun add-completions-from-buffer () | |
1951 (interactive) | |
1952 (let ((current-completion-source cmpl-source-file-parsing) | |
1953 (start-num | |
1954 (cmpl-statistics-block | |
1955 (aref completion-add-count-vector cmpl-source-file-parsing))) | |
1956 mode | |
1957 ) | |
1958 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode)) | |
1959 (add-completions-from-lisp-buffer) | |
1960 (setq mode 'lisp) | |
1961 ) | |
1962 ((memq major-mode '(c-mode)) | |
1963 (add-completions-from-c-buffer) | |
1964 (setq mode 'c) | |
1965 ) | |
1966 (t | |
1967 (error "Do not know how to parse completions in %s buffers." | |
1968 major-mode) | |
1969 )) | |
1970 (cmpl-statistics-block | |
1971 (record-cmpl-parse-file | |
1972 mode (point-max) | |
1973 (- (aref completion-add-count-vector cmpl-source-file-parsing) | |
1974 start-num))) | |
1975 )) | |
1976 | |
1977 ;;; Find file hook | |
1978 (defun cmpl-find-file-hook () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1979 (cond (enable-completion |
56 | 1980 (cond ((and (memq major-mode '(emacs-lisp-mode lisp-mode)) |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1981 (memq 'lisp completions-merging-modes) |
56 | 1982 ) |
1983 (add-completions-from-buffer)) | |
1984 ((and (memq major-mode '(c-mode)) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1985 (memq 'c completions-merging-modes) |
56 | 1986 ) |
1987 (add-completions-from-buffer) | |
1988 ))) | |
1989 )) | |
1990 | |
1991 (pushnew 'cmpl-find-file-hook find-file-hooks) | |
1992 | |
1993 ;;;----------------------------------------------- | |
1994 ;;; Tags Table Completions | |
1995 ;;;----------------------------------------------- | |
1996 | |
1997 (defun add-completions-from-tags-table () | |
1998 ;; Inspired by eero@media-lab.media.mit.edu | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
1999 "Add completions from the current tags table." |
56 | 2000 (interactive) |
2001 (visit-tags-table-buffer) ;this will prompt if no tags-table | |
2002 (save-excursion | |
2003 (goto-char (point-min)) | |
2004 (let (string) | |
2005 (condition-case e | |
2006 (while t | |
2007 (search-forward "\177") | |
2008 (backward-char 3) | |
2009 (and (setq string (symbol-under-point)) | |
2010 (add-completion-to-tail-if-new string)) | |
2011 (forward-char 3) | |
2012 ) | |
2013 (search-failed) | |
2014 )))) | |
2015 | |
2016 | |
2017 ;;;----------------------------------------------- | |
2018 ;;; Lisp File completion parsing | |
2019 ;;;----------------------------------------------- | |
2020 ;;; This merely looks for phrases beginning with (def.... or | |
2021 ;;; (package:def ... and takes the next word. | |
2022 ;;; | |
2023 ;;; We tried using forward-lines and explicit searches but the regexp technique | |
2024 ;;; was faster. (About 100K characters per second) | |
2025 ;;; | |
2026 (defconst *lisp-def-regexp* | |
2027 "\n(\\(\\w*:\\)?def\\(\\w\\|\\s_\\)*\\s +(*" | |
2028 "A regexp that searches for lisp definition form." | |
2029 ) | |
2030 | |
2031 ;;; Tests - | |
2032 ;;; (and (string-match *lisp-def-regexp* "\n(defun foo") (match-end 0)) -> 8 | |
2033 ;;; (and (string-match *lisp-def-regexp* "\n(si:def foo") (match-end 0)) -> 9 | |
2034 ;;; (and (string-match *lisp-def-regexp* "\n(def-bar foo")(match-end 0)) -> 10 | |
2035 ;;; (and (string-match *lisp-def-regexp* "\n(defun (foo") (match-end 0)) -> 9 | |
2036 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2037 ;;; Parses all the definition names from a Lisp mode buffer and adds them to |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2038 ;;; the completion database. |
56 | 2039 (defun add-completions-from-lisp-buffer () |
2040 ;;; Benchmarks | |
2041 ;;; Sun-3/280 - 1500 to 3000 lines of lisp code per second | |
2042 (let (string) | |
2043 (save-excursion | |
2044 (goto-char (point-min)) | |
2045 (condition-case e | |
2046 (while t | |
2047 (re-search-forward *lisp-def-regexp*) | |
2048 (and (setq string (symbol-under-point)) | |
2049 (add-completion-to-tail-if-new string)) | |
2050 ) | |
2051 (search-failed) | |
2052 )))) | |
2053 | |
2054 | |
2055 ;;;----------------------------------------------- | |
2056 ;;; C file completion parsing | |
2057 ;;;----------------------------------------------- | |
2058 ;;; C : | |
2059 ;;; Looks for #define or [<storage class>] [<type>] <name>{,<name>} | |
2060 ;;; or structure, array or pointer defs. | |
2061 ;;; It gets most of the definition names. | |
2062 ;;; | |
2063 ;;; As you might suspect by now, we use some symbol table hackery | |
2064 ;;; | |
2065 ;;; Symbol separator chars (have whitespace syntax) --> , ; * = ( | |
2066 ;;; Opening char --> [ { | |
2067 ;;; Closing char --> ] } | |
2068 ;;; openning and closing must be skipped over | |
2069 ;;; Whitespace chars (have symbol syntax) | |
2070 ;;; Everything else has word syntax | |
2071 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2072 (defun cmpl-make-c-def-completion-syntax-table () |
56 | 2073 (let ((table (make-vector 256 0)) |
2074 (whitespace-chars '(? ?\n ?\t ?\f ?\v ?\r)) | |
2075 ;; unforunately the ?( causes the parens to appear unbalanced | |
2076 (separator-chars '(?, ?* ?= ?\( ?\; | |
2077 )) | |
2078 ) | |
2079 ;; default syntax is whitespace | |
2080 (dotimes (i 256) | |
2081 (modify-syntax-entry i "w" table)) | |
2082 (dolist (char whitespace-chars) | |
2083 (modify-syntax-entry char "_" table)) | |
2084 (dolist (char separator-chars) | |
2085 (modify-syntax-entry char " " table)) | |
2086 (modify-syntax-entry ?\[ "(]" table) | |
2087 (modify-syntax-entry ?\{ "(}" table) | |
2088 (modify-syntax-entry ?\] ")[" table) | |
2089 (modify-syntax-entry ?\} "){" table) | |
2090 table)) | |
2091 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2092 (defconst cmpl-c-def-syntax-table (cmpl-make-c-def-completion-syntax-table)) |
56 | 2093 |
2094 ;;; Regexps | |
2095 (defconst *c-def-regexp* | |
2096 ;; This stops on lines with possible definitions | |
2097 "\n[_a-zA-Z#]" | |
2098 ;; This stops after the symbol to add. | |
2099 ;;"\n\\(#define\\s +.\\|\\(\\(\\w\\|\\s_\\)+\\b\\s *\\)+[(;,[*{=]\\)" | |
2100 ;; This stops before the symbol to add. {Test cases in parens. below} | |
2101 ;;"\n\\(\\(\\w\\|\\s_\\)+\\s *(\\|\\(\\(#define\\|auto\\|extern\\|register\\|static\\|int\\|long\\|short\\|unsigned\\|char\\|void\\|float\\|double\\|enum\\|struct\\|union\\|typedef\\)\\s +\\)+\\)" | |
2102 ;; this simple version picks up too much extraneous stuff | |
2103 ;; "\n\\(\\w\\|\\s_\\|#\\)\\B" | |
2104 "A regexp that searches for a definition form." | |
2105 ) | |
2106 ; | |
2107 ;(defconst *c-cont-regexp* | |
2108 ; "\\(\\w\\|\\s_\\)+\\b\\s *\\({\\|\\(\\[[0-9\t ]*\\]\\s *\\)*,\\(*\\|\\s \\)*\\b\\)" | |
2109 ; "This regexp should be used in a looking-at to parse for lists of variables.") | |
2110 ; | |
2111 ;(defconst *c-struct-regexp* | |
2112 ; "\\(*\\|\\s \\)*\\b" | |
2113 ; "This regexp should be used to test whether a symbol follows a structure definition.") | |
2114 | |
2115 ;(defun test-c-def-regexp (regexp string) | |
2116 ; (and (eq 0 (string-match regexp string)) (match-end 0)) | |
2117 ; ) | |
2118 | |
2119 ;;; Tests - | |
2120 ;;; (test-c-def-regexp *c-def-regexp* "\n#define foo") -> 10 (9) | |
2121 ;;; (test-c-def-regexp *c-def-regexp* "\nfoo (x, y) {") -> 6 (6) | |
2122 ;;; (test-c-def-regexp *c-def-regexp* "\nint foo (x, y)") -> 10 (5) | |
2123 ;;; (test-c-def-regexp *c-def-regexp* "\n int foo (x, y)") -> nil | |
2124 ;;; (test-c-def-regexp *c-cont-regexp* "oo, bar") -> 4 | |
2125 ;;; (test-c-def-regexp *c-cont-regexp* "oo, *bar") -> 5 | |
2126 ;;; (test-c-def-regexp *c-cont-regexp* "a [5][6], bar") -> 10 | |
2127 ;;; (test-c-def-regexp *c-cont-regexp* "oo(x,y)") -> nil | |
2128 ;;; (test-c-def-regexp *c-cont-regexp* "a [6] ,\t bar") -> 9 | |
2129 ;;; (test-c-def-regexp *c-cont-regexp* "oo {trout =1} my_carp;") -> 14 | |
2130 ;;; (test-c-def-regexp *c-cont-regexp* "truct_p complex foon") -> nil | |
2131 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2132 ;;; Parses all the definition names from a C mode buffer and adds them to the |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2133 ;;; completion database. |
56 | 2134 (defun add-completions-from-c-buffer () |
2135 ;; Benchmark -- | |
2136 ;; Sun 3/280-- 1250 lines/sec. | |
2137 | |
2138 (let (string next-point char | |
2139 (saved-syntax (syntax-table)) | |
2140 ) | |
2141 (save-excursion | |
2142 (goto-char (point-min)) | |
2143 (catch 'finish-add-completions | |
2144 (unwind-protect | |
2145 (while t | |
2146 ;; we loop here only when scan-sexps fails | |
2147 ;; (i.e. unbalance exps.) | |
2148 (set-syntax-table cmpl-c-def-syntax-table) | |
2149 (condition-case e | |
2150 (while t | |
2151 (re-search-forward *c-def-regexp*) | |
2152 (cond | |
2153 ((= (preceding-char) ?#) | |
2154 ;; preprocessor macro, see if it's one we handle | |
2155 (setq string (buffer-substring (point) (+ (point) 6))) | |
2156 (cond ((or (string-equal string "define") | |
2157 (string-equal string "ifdef ") | |
2158 ) | |
2159 ;; skip forward over definition symbol | |
2160 ;; and add it to database | |
2161 (and (forward-word 2) | |
2162 (setq string (symbol-before-point)) | |
2163 ;;(push string foo) | |
2164 (add-completion-to-tail-if-new string) | |
2165 )))) | |
2166 (t | |
2167 ;; C definition | |
2168 (setq next-point (point)) | |
2169 (while (and | |
2170 next-point | |
2171 ;; scan to next separator char. | |
2172 (setq next-point (scan-sexps next-point 1)) | |
2173 ) | |
2174 ;; position the point on the word we want to add | |
2175 (goto-char next-point) | |
2176 (while (= (setq char (following-char)) ?*) | |
2177 ;; handle pointer ref | |
2178 ;; move to next separator char. | |
2179 (goto-char | |
2180 (setq next-point (scan-sexps (point) 1))) | |
2181 ) | |
2182 (forward-word -1) | |
2183 ;; add to database | |
2184 (if (setq string (symbol-under-point)) | |
2185 ;; (push string foo) | |
2186 (add-completion-to-tail-if-new string) | |
2187 ;; Local TMC hack (useful for parsing paris.h) | |
2188 (if (and (looking-at "_AP") ;; "ansi prototype" | |
2189 (progn | |
2190 (forward-word -1) | |
2191 (setq string | |
2192 (symbol-under-point)) | |
2193 )) | |
2194 (add-completion-to-tail-if-new string) | |
2195 ) | |
2196 ) | |
2197 ;; go to next | |
2198 (goto-char next-point) | |
2199 ;; (push (format "%c" (following-char)) foo) | |
2200 (if (= (char-syntax char) ?\() | |
2201 ;; if on an opening delimiter, go to end | |
2202 (while (= (char-syntax char) ?\() | |
2203 (setq next-point (scan-sexps next-point 1) | |
2204 char (char-after next-point)) | |
2205 ) | |
2206 (or (= char ?,) | |
2207 ;; Current char is an end char. | |
2208 (setq next-point nil) | |
2209 )) | |
2210 )))) | |
2211 (search-failed ;;done | |
2212 (throw 'finish-add-completions t) | |
2213 ) | |
2214 (error | |
2215 ;; Check for failure in scan-sexps | |
2216 (if (or (string-equal (second e) | |
2217 "Containing expression ends prematurely") | |
2218 (string-equal (second e) "Unbalanced parentheses")) | |
2219 ;; unbalanced paren., keep going | |
2220 ;;(ding) | |
2221 (forward-line 1) | |
2222 (message "Error parsing C buffer for completions. Please bug report.") | |
2223 (throw 'finish-add-completions t) | |
2224 )) | |
2225 )) | |
2226 (set-syntax-table saved-syntax) | |
2227 ))))) | |
2228 | |
2229 | |
2230 ;;;--------------------------------------------------------------------------- | |
2231 ;;; Init files | |
2232 ;;;--------------------------------------------------------------------------- | |
2233 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2234 ;;; The version of save-completions-to-file called at kill-emacs time. |
56 | 2235 (defun kill-emacs-save-completions () |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2236 (when (and save-completions-flag enable-completion cmpl-initialized-p) |
56 | 2237 (cond |
2238 ((not cmpl-completions-accepted-p) | |
2239 (message "Completions database has not changed - not writing.")) | |
2240 (t | |
2241 (save-completions-to-file) | |
2242 )) | |
2243 )) | |
2244 | |
2245 (defconst saved-cmpl-file-header | |
2246 ";;; Completion Initialization file. | |
2247 ;;; Version = %s | |
2248 ;;; Format is (<string> . <last-use-time>) | |
2249 ;;; <string> is the completion | |
2250 ;;; <last-use-time> is the time the completion was last used | |
2251 ;;; If it is t, the completion will never be pruned from the file. | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2252 ;;; Otherwise it is in hours since origin. |
56 | 2253 \n") |
2254 | |
2255 (defun completion-backup-filename (filename) | |
2256 (concat filename ".BAK")) | |
2257 | |
2258 (defun save-completions-to-file (&optional filename) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2259 "Save completions in init file FILENAME. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2260 If file name is not specified, use `save-completions-file-name'." |
56 | 2261 (interactive) |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2262 (setq filename (expand-file-name (or filename save-completions-file-name))) |
56 | 2263 (when (file-writable-p filename) |
2264 (if (not cmpl-initialized-p) | |
2265 (initialize-completions));; make sure everything's loaded | |
2266 (message "Saving completions to file %s" filename) | |
2267 | |
2268 (let* ((trim-versions-without-asking t) | |
2269 (kept-old-versions 0) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2270 (kept-new-versions completions-file-versions-kept) |
56 | 2271 last-use-time |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2272 (current-time (cmpl-hours-since-origin)) |
56 | 2273 (total-in-db 0) |
2274 (total-perm 0) | |
2275 (total-saved 0) | |
2276 (backup-filename (completion-backup-filename filename)) | |
2277 ) | |
2278 | |
2279 (save-excursion | |
2280 (get-buffer-create " *completion-save-buffer*") | |
2281 (set-buffer " *completion-save-buffer*") | |
2282 (setq buffer-file-name filename) | |
2283 | |
2284 (when (not (verify-visited-file-modtime (current-buffer))) | |
2285 ;; file has changed on disk. Bring us up-to-date | |
2286 (message "Completion file has changed. Merging. . .") | |
2287 (load-completions-from-file filename t) | |
2288 (message "Merging finished. Saving completions to file %s" filename) | |
2289 ) | |
2290 | |
2291 ;; prepare the buffer to be modified | |
2292 (clear-visited-file-modtime) | |
2293 (erase-buffer) | |
2294 ;; (/ 1 0) | |
2295 (insert (format saved-cmpl-file-header *completion-version*)) | |
2296 (dolist (completion (list-all-completions)) | |
2297 (setq total-in-db (1+ total-in-db)) | |
2298 (setq last-use-time (completion-last-use-time completion)) | |
2299 ;; Update num uses and maybe write completion to a file | |
2300 (cond ((or;; Write to file if | |
2301 ;; permanent | |
2302 (and (eq last-use-time t) | |
2303 (setq total-perm (1+ total-perm))) | |
2304 ;; or if | |
2305 (if (plusp (completion-num-uses completion)) | |
2306 ;; it's been used | |
2307 (setq last-use-time current-time) | |
2308 ;; or it was saved before and | |
2309 (and last-use-time | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2310 ;; save-completions-retention-time is nil |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2311 (or (not save-completions-retention-time) |
56 | 2312 ;; or time since last use is < ...retention-time* |
2313 (< (- current-time last-use-time) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2314 save-completions-retention-time)) |
56 | 2315 ))) |
2316 ;; write to file | |
2317 (setq total-saved (1+ total-saved)) | |
2318 (insert (prin1-to-string (cons (completion-string completion) | |
2319 last-use-time)) "\n") | |
2320 ))) | |
2321 | |
2322 ;; write the buffer | |
2323 (condition-case e | |
2324 (let ((file-exists-p (file-exists-p filename))) | |
2325 (when file-exists-p | |
2326 ;; If file exists . . . | |
2327 ;; Save a backup(so GNU doesn't screw us when we're out of disk) | |
2328 ;; (GNU leaves a 0 length file if it gets a disk full error!) | |
2329 | |
2330 ;; If backup doesn't exit, Rename current to backup | |
2331 ;; {If backup exists the primary file is probably messed up} | |
2332 (unless (file-exists-p backup-filename) | |
2333 (rename-file filename backup-filename)) | |
2334 ;; Copy the backup back to the current name | |
2335 ;; (so versioning works) | |
2336 (copy-file backup-filename filename t) | |
2337 ) | |
2338 ;; Save it | |
2339 (save-buffer) | |
2340 (when file-exists-p | |
2341 ;; If successful, remove backup | |
2342 (delete-file backup-filename) | |
2343 )) | |
2344 (error | |
2345 (set-buffer-modified-p nil) | |
2346 (message "Couldn't save completion file %s." filename) | |
2347 )) | |
2348 ;; Reset accepted-p flag | |
2349 (setq cmpl-completions-accepted-p nil) | |
2350 ) | |
2351 (cmpl-statistics-block | |
2352 (record-save-completions total-in-db total-perm total-saved)) | |
2353 ))) | |
2354 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2355 ;;;(defun autosave-completions () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2356 ;;; (when (and save-completions-flag enable-completion cmpl-initialized-p |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2357 ;;; *completion-auto-save-period* |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2358 ;;; (> cmpl-emacs-idle-time *completion-auto-save-period*) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2359 ;;; cmpl-completions-accepted-p) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2360 ;;; (save-completions-to-file) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2361 ;;; )) |
56 | 2362 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2363 ;;;(pushnew 'autosave-completions cmpl-emacs-idle-time-hooks) |
56 | 2364 |
2365 (defun load-completions-from-file (&optional filename no-message-p) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2366 "Loads a completion init file FILENAME. |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2367 If file is not specified, then use `save-completions-file-name'." |
56 | 2368 (interactive) |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2369 (setq filename (expand-file-name (or filename save-completions-file-name))) |
56 | 2370 (let* ((backup-filename (completion-backup-filename filename)) |
2371 (backup-readable-p (file-readable-p backup-filename)) | |
2372 ) | |
2373 (when backup-readable-p (setq filename backup-filename)) | |
2374 (when (file-readable-p filename) | |
2375 (if (not no-message-p) | |
2376 (message "Loading completions from %sfile %s . . ." | |
2377 (if backup-readable-p "backup " "") filename)) | |
2378 (save-excursion | |
2379 (get-buffer-create " *completion-save-buffer*") | |
2380 (set-buffer " *completion-save-buffer*") | |
2381 (setq buffer-file-name filename) | |
2382 ;; prepare the buffer to be modified | |
2383 (clear-visited-file-modtime) | |
2384 (erase-buffer) | |
2385 | |
2386 (let ((insert-okay-p nil) | |
2387 (buffer (current-buffer)) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2388 (current-time (cmpl-hours-since-origin)) |
56 | 2389 string num-uses entry last-use-time |
2390 cmpl-entry cmpl-last-use-time | |
2391 (current-completion-source cmpl-source-init-file) | |
2392 (start-num | |
2393 (cmpl-statistics-block | |
2394 (aref completion-add-count-vector cmpl-source-file-parsing))) | |
2395 (total-in-file 0) (total-perm 0) | |
2396 ) | |
2397 ;; insert the file into a buffer | |
2398 (condition-case e | |
2399 (progn (insert-file-contents filename t) | |
2400 (setq insert-okay-p t)) | |
2401 | |
2402 (file-error | |
2403 (message "File error trying to load completion file %s." | |
2404 filename))) | |
2405 ;; parse it | |
2406 (when insert-okay-p | |
2407 (goto-char (point-min)) | |
2408 | |
2409 (condition-case e | |
2410 (while t | |
2411 (setq entry (read buffer)) | |
2412 (setq total-in-file (1+ total-in-file)) | |
2413 (cond | |
2414 ((and (consp entry) | |
2415 (stringp (setq string (car entry))) | |
2416 (cond | |
2417 ((eq (setq last-use-time (cdr entry)) 'T) | |
2418 ;; handle case sensitivity | |
2419 (setq total-perm (1+ total-perm)) | |
2420 (setq last-use-time t)) | |
2421 ((eq last-use-time t) | |
2422 (setq total-perm (1+ total-perm))) | |
2423 ((integerp last-use-time)) | |
2424 )) | |
2425 ;; Valid entry | |
2426 ;; add it in | |
2427 (setq cmpl-last-use-time | |
2428 (completion-last-use-time | |
2429 (setq cmpl-entry | |
2430 (add-completion-to-tail-if-new string)) | |
2431 )) | |
2432 (if (or (eq last-use-time t) | |
2433 (and (> last-use-time 1000);;backcompatibility | |
2434 (not (eq cmpl-last-use-time t)) | |
2435 (or (not cmpl-last-use-time) | |
2436 ;; more recent | |
2437 (> last-use-time cmpl-last-use-time)) | |
2438 )) | |
2439 ;; update last-use-time | |
2440 (set-completion-last-use-time cmpl-entry last-use-time) | |
2441 )) | |
2442 (t | |
2443 ;; Bad format | |
2444 (message "Error: invalid saved completion - %s" | |
2445 (prin1-to-string entry)) | |
2446 ;; try to get back in sync | |
2447 (search-forward "\n(") | |
2448 ))) | |
2449 (search-failed | |
2450 (message "End of file while reading completions.") | |
2451 ) | |
2452 (end-of-file | |
2453 (if (= (point) (point-max)) | |
2454 (if (not no-message-p) | |
2455 (message "Loading completions from file %s . . . Done." | |
2456 filename)) | |
2457 (message "End of file while reading completions.") | |
2458 )) | |
2459 )) | |
2460 | |
2461 (cmpl-statistics-block | |
2462 (record-load-completions | |
2463 total-in-file total-perm | |
2464 (- (aref completion-add-count-vector cmpl-source-init-file) | |
2465 start-num))) | |
2466 | |
2467 ))))) | |
2468 | |
2469 (defun initialize-completions () | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2470 "Load the default completions file. |
1356 | 2471 Also sets up so that exiting emacs will automatically save the file." |
56 | 2472 (interactive) |
2473 (cond ((not cmpl-initialized-p) | |
2474 (load-completions-from-file) | |
2475 )) | |
2476 (setq cmpl-initialized-p t) | |
2477 ) | |
2478 | |
2479 | |
2480 ;;;----------------------------------------------- | |
2481 ;;; Kill EMACS patch | |
2482 ;;;----------------------------------------------- | |
2483 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2484 (add-hook 'kill-emacs-hook |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2485 '(lambda () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2486 (kill-emacs-save-completions) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2487 (cmpl-statistics-block |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2488 (record-cmpl-kill-emacs)))) |
56 | 2489 |
2490 ;;;----------------------------------------------- | |
2491 ;;; Kill region patch | |
2492 ;;;----------------------------------------------- | |
2493 | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2494 (defun completion-kill-region (&optional beg end) |
56 | 2495 "Kill between point and mark. |
2496 The text is deleted but saved in the kill ring. | |
2497 The command \\[yank] can retrieve it from there. | |
2498 /(If you want to kill and then yank immediately, use \\[copy-region-as-kill].) | |
2499 | |
2500 This is the primitive for programs to kill text (as opposed to deleting it). | |
2501 Supply two arguments, character numbers indicating the stretch of text | |
2502 to be killed. | |
2503 Any command that calls this function is a \"kill command\". | |
2504 If the previous command was also a kill command, | |
2505 the text killed this time appends to the text killed last time | |
2506 to make one entry in the kill ring. | |
2507 Patched to remove the most recent completion." | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2508 (interactive "r") |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2509 (cond ((eq last-command 'complete) |
56 | 2510 (delete-region (point) cmpl-last-insert-location) |
2511 (insert cmpl-original-string) | |
2512 (setq completion-to-accept nil) | |
2513 (cmpl-statistics-block | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2514 (record-complete-failed))) |
56 | 2515 (t |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2516 (kill-region beg end)))) |
56 | 2517 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2518 (global-set-key "\C-w" 'completion-kill-region) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2519 |
56 | 2520 ;;;----------------------------------------------- |
2521 ;;; Patches to self-insert-command. | |
2522 ;;;----------------------------------------------- | |
2523 | |
2524 ;;; Need 2 versions: generic seperator chars. and space (to get auto fill | |
2525 ;;; to work) | |
2526 | |
2527 ;;; All common separators (eg. space "(" ")" """) characters go through a | |
2528 ;;; function to add new words to the list of words to complete from: | |
2529 ;;; COMPLETION-SEPARATOR-SELF-INSERT-COMMAND (arg). | |
2530 ;;; If the character before this was an alpha-numeric then this adds the | |
2531 ;;; symbol befoe point to the completion list (using ADD-COMPLETION). | |
2532 | |
2533 (defun completion-separator-self-insert-command (arg) | |
2534 (interactive "p") | |
2535 (use-completion-before-separator) | |
2536 (self-insert-command arg) | |
2537 ) | |
2538 | |
2539 (defun completion-separator-self-insert-autofilling (arg) | |
2540 (interactive "p") | |
2541 (use-completion-before-separator) | |
2542 (self-insert-command arg) | |
2543 (and (> (current-column) fill-column) | |
732 | 2544 auto-fill-function |
2545 (funcall auto-fill-function)) | |
56 | 2546 ) |
2547 | |
2548 ;;;----------------------------------------------- | |
2549 ;;; Wrapping Macro | |
2550 ;;;----------------------------------------------- | |
2551 | |
2552 ;;; Note that because of the way byte compiling works, none of | |
2553 ;;; the functions defined with this macro get byte compiled. | |
2554 | |
2555 (defmacro def-completion-wrapper (function-name type &optional new-name) | |
1356 | 2556 "Add a call to update the completion database before function execution. |
2557 TYPE is the type of the wrapper to be added. Can be :before or :under." | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2558 (cond ((eq type ':separator) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2559 (list 'put (list 'quote function-name) ''completion-function |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2560 ''use-completion-before-separator)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2561 ((eq type ':before) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2562 (list 'put (list 'quote function-name) ''completion-function |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2563 ''use-completion-before-point)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2564 ((eq type ':backward-under) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2565 (list 'put (list 'quote function-name) ''completion-function |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2566 ''use-completion-backward-under)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2567 ((eq type ':backward) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2568 (list 'put (list 'quote function-name) ''completion-function |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2569 ''use-completion-backward)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2570 ((eq type ':under) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2571 (list 'put (list 'quote function-name) ''completion-function |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2572 ''use-completion-under-point)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2573 ((eq type ':under-or-before) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2574 (list 'put (list 'quote function-name) ''completion-function |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2575 ''use-completion-under-or-before-point)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2576 ((eq type ':minibuffer-separator) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2577 (list 'put (list 'quote function-name) ''completion-function |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2578 ''use-completion-minibuffer-separator)))) |
56 | 2579 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2580 (defun use-completion-minibuffer-separator () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2581 (let ((cmpl-syntax-table cmpl-standard-syntax-table)) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2582 (use-completion-before-separator))) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2583 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2584 (defun use-completion-backward-under () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2585 (use-completion-under-point) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2586 (if (eq last-command 'complete) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2587 ;; probably a failed completion if you have to back up |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2588 (cmpl-statistics-block (record-complete-failed)))) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2589 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2590 (defun use-completion-backward () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2591 (if (eq last-command 'complete) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2592 ;; probably a failed completion if you have to back up |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2593 (cmpl-statistics-block (record-complete-failed)))) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2594 |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2595 (defun completion-before-command () |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2596 (funcall (or (get this-command 'completion-function) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2597 'use-completion-under-or-before-point))) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2598 (add-hook 'before-command-hook 'completion-before-command) |
56 | 2599 |
2600 | |
2601 ;;;--------------------------------------------------------------------------- | |
2602 ;;; Patches to standard keymaps insert completions | |
2603 ;;;--------------------------------------------------------------------------- | |
2604 | |
2605 ;;;----------------------------------------------- | |
2606 ;;; Separators | |
2607 ;;;----------------------------------------------- | |
2608 ;;; We've used the completion syntax table given as a guide. | |
2609 ;;; | |
2610 ;;; Global separator chars. | |
2611 ;;; We left out <tab> because there are too many special cases for it. Also, | |
2612 ;;; in normal coding it's rarely typed after a word. | |
2613 (global-set-key " " 'completion-separator-self-insert-autofilling) | |
2614 (global-set-key "!" 'completion-separator-self-insert-command) | |
2615 (global-set-key "%" 'completion-separator-self-insert-command) | |
2616 (global-set-key "^" 'completion-separator-self-insert-command) | |
2617 (global-set-key "&" 'completion-separator-self-insert-command) | |
2618 (global-set-key "(" 'completion-separator-self-insert-command) | |
2619 (global-set-key ")" 'completion-separator-self-insert-command) | |
2620 (global-set-key "=" 'completion-separator-self-insert-command) | |
2621 (global-set-key "`" 'completion-separator-self-insert-command) | |
2622 (global-set-key "|" 'completion-separator-self-insert-command) | |
2623 (global-set-key "{" 'completion-separator-self-insert-command) | |
2624 (global-set-key "}" 'completion-separator-self-insert-command) | |
2625 (global-set-key "[" 'completion-separator-self-insert-command) | |
2626 (global-set-key "]" 'completion-separator-self-insert-command) | |
2627 (global-set-key ";" 'completion-separator-self-insert-command) | |
2628 (global-set-key "\"" 'completion-separator-self-insert-command) | |
2629 (global-set-key "'" 'completion-separator-self-insert-command) | |
2630 (global-set-key "#" 'completion-separator-self-insert-command) | |
2631 (global-set-key "," 'completion-separator-self-insert-command) | |
2632 (global-set-key "?" 'completion-separator-self-insert-command) | |
2633 | |
2634 ;;; We include period and colon even though they are symbol chars because : | |
2635 ;;; - in text we want to pick up the last word in a sentence. | |
2636 ;;; - in C pointer refs. we want to pick up the first symbol | |
2637 ;;; - it won't make a difference for lisp mode (package names are short) | |
2638 (global-set-key "." 'completion-separator-self-insert-command) | |
2639 (global-set-key ":" 'completion-separator-self-insert-command) | |
2640 | |
2641 ;;; Lisp Mode diffs | |
2642 (define-key lisp-mode-map "!" 'self-insert-command) | |
2643 (define-key lisp-mode-map "&" 'self-insert-command) | |
2644 (define-key lisp-mode-map "%" 'self-insert-command) | |
2645 (define-key lisp-mode-map "?" 'self-insert-command) | |
2646 (define-key lisp-mode-map "=" 'self-insert-command) | |
2647 (define-key lisp-mode-map "^" 'self-insert-command) | |
2648 | |
2649 ;;; C mode diffs. | |
2650 (def-completion-wrapper electric-c-semi :separator) | |
2651 (define-key c-mode-map "+" 'completion-separator-self-insert-command) | |
2652 (define-key c-mode-map "*" 'completion-separator-self-insert-command) | |
2653 (define-key c-mode-map "/" 'completion-separator-self-insert-command) | |
2654 | |
2655 ;;; FORTRAN mode diffs. (these are defined when fortran is called) | |
2656 (defun completion-setup-fortran-mode () | |
2657 (define-key fortran-mode-map "+" 'completion-separator-self-insert-command) | |
2658 (define-key fortran-mode-map "-" 'completion-separator-self-insert-command) | |
2659 (define-key fortran-mode-map "*" 'completion-separator-self-insert-command) | |
2660 (define-key fortran-mode-map "/" 'completion-separator-self-insert-command) | |
2661 ) | |
2662 | |
2663 ;;;----------------------------------------------- | |
2664 ;;; End of line chars. | |
2665 ;;;----------------------------------------------- | |
2666 (def-completion-wrapper newline :separator) | |
2667 (def-completion-wrapper newline-and-indent :separator) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2668 (def-completion-wrapper comint-send-input :separator)) |
56 | 2669 (def-completion-wrapper exit-minibuffer :minibuffer-separator) |
2670 (def-completion-wrapper eval-print-last-sexp :separator) | |
2671 (def-completion-wrapper eval-last-sexp :separator) | |
2672 ;;(def-completion-wrapper minibuffer-complete-and-exit :minibuffer) | |
2673 | |
2674 ;;;----------------------------------------------- | |
2675 ;;; Cursor movement | |
2676 ;;;----------------------------------------------- | |
2677 | |
2678 (def-completion-wrapper next-line :under-or-before) | |
2679 (def-completion-wrapper previous-line :under-or-before) | |
2680 (def-completion-wrapper beginning-of-buffer :under-or-before) | |
2681 (def-completion-wrapper end-of-buffer :under-or-before) | |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2682 (def-completion-wrapper beginning-of-line :under-or-before) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2683 (def-completion-wrapper end-of-line :under-or-before) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2684 (def-completion-wrapper forward-char :under-or-before) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2685 (def-completion-wrapper forward-word :under-or-before) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2686 (def-completion-wrapper forward-sexp :under-or-before) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2687 (def-completion-wrapper backward-char :backward-under) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2688 (def-completion-wrapper backward-word :backward-under) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2689 (def-completion-wrapper backward-sexp :backward-under) |
56 | 2690 |
3175
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2691 (def-completion-wrapper delete-backward-char :backward) |
15d3c2e32922
Pervasive changes to use Emacs 19 features
Richard M. Stallman <rms@gnu.org>
parents:
3169
diff
changeset
|
2692 (def-completion-wrapper delete-backward-char-untabify :backward) |
56 | 2693 |
2694 ;;; Tests -- | |
2695 ;;; foobarbiz | |
2696 ;;; foobar | |
2697 ;;; fooquux | |
2698 ;;; fooper | |
2699 | |
2700 (cmpl-statistics-block | |
2701 (record-completion-file-loaded)) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
190
diff
changeset
|
2702 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
190
diff
changeset
|
2703 ;;; completion.el ends here |