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