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