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