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))
|
2
|
625 ;; XEmacs change
|
0
|
626 (set-syntax-table cmpl-syntax-table)
|
|
627 (cond
|
|
628 ;; Cursor is on following-char and after preceding-char
|
|
629 ((memq (char-syntax (following-char)) '(?w ?_))
|
|
630 (setq cmpl-saved-point (point)
|
|
631 cmpl-symbol-start (scan-sexps (1+ cmpl-saved-point) -1)
|
|
632 cmpl-symbol-end (scan-sexps cmpl-saved-point 1))
|
|
633 ;; remove chars to ignore at the start
|
|
634 (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w)
|
|
635 (goto-char cmpl-symbol-start)
|
|
636 (forward-word 1)
|
|
637 (setq cmpl-symbol-start (point))
|
|
638 (goto-char cmpl-saved-point)
|
|
639 ))
|
|
640 ;; remove chars to ignore at the end
|
|
641 (cond ((= (char-syntax (char-after (1- cmpl-symbol-end))) ?w)
|
|
642 (goto-char cmpl-symbol-end)
|
|
643 (forward-word -1)
|
|
644 (setq cmpl-symbol-end (point))
|
|
645 (goto-char cmpl-saved-point)
|
|
646 ))
|
|
647 ;; restore state
|
|
648 (set-syntax-table cmpl-saved-syntax)
|
|
649 ;; Return completion if the length is reasonable
|
|
650 (if (and (<= (cmpl-read-time-eval completion-min-length)
|
|
651 (- cmpl-symbol-end cmpl-symbol-start))
|
|
652 (<= (- cmpl-symbol-end cmpl-symbol-start)
|
|
653 (cmpl-read-time-eval completion-max-length)))
|
|
654 (buffer-substring cmpl-symbol-start cmpl-symbol-end))
|
|
655 )
|
|
656 (t
|
|
657 ;; restore table if no symbol
|
|
658 (set-syntax-table cmpl-saved-syntax)
|
|
659 nil)
|
|
660 ))
|
|
661
|
2
|
662 ;; tests for symbol-under-point
|
|
663 ;; `^' indicates cursor pos. where value is returned
|
|
664 ;; simple-word-test
|
|
665 ;; ^^^^^^^^^^^^^^^^ --> simple-word-test
|
|
666 ;; _harder_word_test_
|
|
667 ;; ^^^^^^^^^^^^^^^^^^ --> harder_word_test
|
|
668 ;; .___.______.
|
|
669 ;; --> nil
|
|
670 ;; /foo/bar/quux.hello
|
|
671 ;; ^^^^^^^^^^^^^^^^^^^ --> /foo/bar/quux.hello
|
|
672 ;;
|
0
|
673
|
|
674 (defun symbol-before-point ()
|
|
675 "Returns a string of the symbol immediately before point.
|
|
676 Returns nil if there isn't one longer than `completion-min-length'."
|
|
677 ;; This is called when a word separator is typed so it must be FAST !
|
|
678 (setq cmpl-saved-syntax (syntax-table))
|
2
|
679 ;; XEmacs change
|
0
|
680 (set-syntax-table cmpl-syntax-table)
|
|
681 ;; Cursor is on following-char and after preceding-char
|
|
682 (cond ((= (setq cmpl-preceding-syntax (char-syntax (preceding-char))) ?_)
|
|
683 ;; No chars. to ignore at end
|
|
684 (setq cmpl-symbol-end (point)
|
|
685 cmpl-symbol-start (scan-sexps (1+ cmpl-symbol-end) -1)
|
|
686 )
|
2
|
687 ;; XEmacs change
|
0
|
688 ;; remove chars to ignore at the start
|
|
689 (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w)
|
|
690 (goto-char cmpl-symbol-start)
|
|
691 (forward-word 1)
|
|
692 (setq cmpl-symbol-start (point))
|
|
693 (goto-char cmpl-symbol-end)
|
|
694 ))
|
|
695 ;; restore state
|
|
696 (set-syntax-table cmpl-saved-syntax)
|
|
697 ;; return value if long enough
|
|
698 (if (>= cmpl-symbol-end
|
|
699 (+ cmpl-symbol-start
|
|
700 (cmpl-read-time-eval completion-min-length)))
|
|
701 (buffer-substring cmpl-symbol-start cmpl-symbol-end))
|
|
702 )
|
|
703 ((= cmpl-preceding-syntax ?w)
|
|
704 ;; chars to ignore at end
|
|
705 (setq cmpl-saved-point (point)
|
|
706 cmpl-symbol-start (scan-sexps (1+ cmpl-saved-point) -1))
|
|
707 ;; take off chars. from end
|
|
708 (forward-word -1)
|
|
709 (setq cmpl-symbol-end (point))
|
|
710 ;; remove chars to ignore at the start
|
|
711 (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w)
|
|
712 (goto-char cmpl-symbol-start)
|
|
713 (forward-word 1)
|
|
714 (setq cmpl-symbol-start (point))
|
|
715 ))
|
|
716 ;; restore state
|
|
717 (goto-char cmpl-saved-point)
|
|
718 (set-syntax-table cmpl-saved-syntax)
|
|
719 ;; Return completion if the length is reasonable
|
|
720 (if (and (<= (cmpl-read-time-eval completion-min-length)
|
|
721 (- cmpl-symbol-end cmpl-symbol-start))
|
|
722 (<= (- cmpl-symbol-end cmpl-symbol-start)
|
|
723 (cmpl-read-time-eval completion-max-length)))
|
|
724 (buffer-substring cmpl-symbol-start cmpl-symbol-end))
|
|
725 )
|
|
726 (t
|
|
727 ;; restore table if no symbol
|
|
728 (set-syntax-table cmpl-saved-syntax)
|
|
729 nil)
|
|
730 ))
|
|
731
|
2
|
732 ;; tests for symbol-before-point
|
|
733 ;; `^' indicates cursor pos. where value is returned
|
|
734 ;; simple-word-test
|
|
735 ;; ^ --> nil
|
|
736 ;; ^ --> nil
|
|
737 ;; ^ --> simple-w
|
|
738 ;; ^ --> simple-word-test
|
|
739 ;; _harder_word_test_
|
|
740 ;; ^ --> harder_word_test
|
|
741 ;; ^ --> harder_word_test
|
|
742 ;; ^ --> harder
|
|
743 ;; .___....
|
|
744 ;; --> nil
|
0
|
745
|
|
746 (defun symbol-under-or-before-point ()
|
2
|
747 ;; This could be made slightly faster but it is better to avoid
|
|
748 ;; copying all the code.
|
|
749 ;; However, it is only used by the completion string prompter.
|
|
750 ;; If it comes into common use, it could be rewritten.
|
|
751 ;; XEmacs change
|
0
|
752 (setq cmpl-saved-syntax (syntax-table))
|
|
753 (set-syntax-table cmpl-syntax-table)
|
|
754 (cond ((memq (char-syntax (following-char)) '(?w ?_))
|
|
755 (set-syntax-table cmpl-saved-syntax)
|
|
756 (symbol-under-point))
|
|
757 (t
|
2
|
758 ;; XEmacs change
|
0
|
759 (set-syntax-table cmpl-saved-syntax)
|
|
760 (symbol-before-point))
|
|
761 ))
|
|
762
|
|
763
|
|
764 (defun symbol-before-point-for-complete ()
|
|
765 ;; "Returns a string of the symbol immediately before point
|
|
766 ;; or nil if there isn't one. Like symbol-before-point but doesn't trim the
|
|
767 ;; end chars."
|
|
768 ;; Cursor is on following-char and after preceding-char
|
|
769 (setq cmpl-saved-syntax (syntax-table))
|
2
|
770 ;; XEmacs change
|
0
|
771 (set-syntax-table cmpl-syntax-table)
|
|
772 (cond ((memq (setq cmpl-preceding-syntax (char-syntax (preceding-char)))
|
|
773 '(?_ ?w))
|
|
774 (setq cmpl-symbol-end (point)
|
|
775 cmpl-symbol-start (scan-sexps (1+ cmpl-symbol-end) -1)
|
|
776 )
|
|
777 ;; remove chars to ignore at the start
|
|
778 (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w)
|
|
779 (goto-char cmpl-symbol-start)
|
|
780 (forward-word 1)
|
|
781 (setq cmpl-symbol-start (point))
|
|
782 (goto-char cmpl-symbol-end)
|
|
783 ))
|
|
784 ;; restore state
|
|
785 (set-syntax-table cmpl-saved-syntax)
|
|
786 ;; Return completion if the length is reasonable
|
|
787 (if (and (<= (cmpl-read-time-eval
|
|
788 completion-prefix-min-length)
|
|
789 (- cmpl-symbol-end cmpl-symbol-start))
|
|
790 (<= (- cmpl-symbol-end cmpl-symbol-start)
|
|
791 (cmpl-read-time-eval completion-max-length)))
|
|
792 (buffer-substring cmpl-symbol-start cmpl-symbol-end))
|
|
793 )
|
|
794 (t
|
|
795 ;; restore table if no symbol
|
|
796 (set-syntax-table cmpl-saved-syntax)
|
|
797 nil)
|
|
798 ))
|
|
799
|
2
|
800 ;; tests for symbol-before-point-for-complete
|
|
801 ;; `^' indicates cursor pos. where value is returned
|
|
802 ;; simple-word-test
|
|
803 ;; ^ --> nil
|
|
804 ;; ^ --> nil
|
|
805 ;; ^ --> simple-w
|
|
806 ;; ^ --> simple-word-test
|
|
807 ;; _harder_word_test_
|
|
808 ;; ^ --> harder_word_test
|
|
809 ;; ^ --> harder_word_test_
|
|
810 ;; ^ --> harder_
|
|
811 ;; .___....
|
|
812 ;; --> nil
|
0
|
813
|
|
814
|
|
815
|
2
|
816 ;;---------------------------------------------------------------------------
|
|
817 ;; Statistics Recording
|
|
818 ;;---------------------------------------------------------------------------
|
0
|
819
|
2
|
820 ;; Note that the guts of this has been turned off. The guts
|
|
821 ;; are in completion-stats.el.
|
0
|
822
|
2
|
823 ;;-----------------------------------------------
|
|
824 ;; Conditionalizing code on *record-cmpl-statistics-p*
|
|
825 ;;-----------------------------------------------
|
|
826 ;; All statistics code outside this block should use this
|
0
|
827 (defmacro cmpl-statistics-block (&rest body))
|
2
|
828 ;; "Only executes body if we are recording statistics."
|
|
829 ;; (list 'cond
|
|
830 ;; (list* '*record-cmpl-statistics-p* body)
|
|
831 ;; ))
|
0
|
832
|
2
|
833 ;;-----------------------------------------------
|
|
834 ;; Completion Sources
|
|
835 ;;-----------------------------------------------
|
0
|
836
|
|
837 ;; ID numbers
|
|
838 (defconst cmpl-source-unknown 0)
|
|
839 (defconst cmpl-source-init-file 1)
|
|
840 (defconst cmpl-source-file-parsing 2)
|
|
841 (defconst cmpl-source-separator 3)
|
|
842 (defconst cmpl-source-cursor-moves 4)
|
|
843 (defconst cmpl-source-interactive 5)
|
|
844 (defconst cmpl-source-cdabbrev 6)
|
|
845 (defconst num-cmpl-sources 7)
|
|
846 (defvar current-completion-source cmpl-source-unknown)
|
|
847
|
|
848
|
|
849
|
2
|
850 ;;---------------------------------------------------------------------------
|
|
851 ;; Completion Method #2: dabbrev-expand style
|
|
852 ;;---------------------------------------------------------------------------
|
|
853 ;;
|
|
854 ;; This method is used if there are no useful stored completions. It is
|
|
855 ;; based on dabbrev-expand with these differences :
|
|
856 ;; 1) Faster (we don't use regexps)
|
|
857 ;; 2) case coercion handled correctly
|
|
858 ;; This is called cdabbrev to differentiate it.
|
|
859 ;; We simply search backwards through the file looking for words which
|
|
860 ;; start with the same letters we are trying to complete.
|
|
861 ;;
|
0
|
862
|
|
863 (defvar cdabbrev-completions-tried nil)
|
2
|
864 ;; "A list of all the cdabbrev completions since the last reset.")
|
0
|
865
|
|
866 (defvar cdabbrev-current-point 0)
|
2
|
867 ;; "The current point position the cdabbrev search is at.")
|
0
|
868
|
|
869 (defvar cdabbrev-current-window nil)
|
2
|
870 ;; "The current window we are looking for cdabbrevs in. T if looking in
|
|
871 ;; (other-buffer), NIL if no more cdabbrevs.")
|
0
|
872
|
|
873 (defvar cdabbrev-wrapped-p nil)
|
2
|
874 ;; "T if the cdabbrev search has wrapped around the file.")
|
0
|
875
|
|
876 (defvar cdabbrev-abbrev-string "")
|
|
877 (defvar cdabbrev-start-point 0)
|
|
878 (defvar cdabbrev-stop-point)
|
|
879
|
2
|
880 ;; Test strings for cdabbrev
|
|
881 ;; cdat-upcase ;;same namestring
|
|
882 ;; CDAT-UPCASE ;;ok
|
|
883 ;; cdat2 ;;too short
|
|
884 ;; cdat-1-2-3-4 ;;ok
|
|
885 ;; a-cdat-1 ;;doesn't start correctly
|
|
886 ;; cdat-simple ;;ok
|
0
|
887
|
|
888
|
|
889 (defun reset-cdabbrev (abbrev-string &optional initial-completions-tried)
|
|
890 "Resets the cdabbrev search to search for abbrev-string.
|
|
891 INITIAL-COMPLETIONS-TRIED is a list of downcased strings to ignore
|
|
892 during the search."
|
|
893 (setq cdabbrev-abbrev-string abbrev-string
|
|
894 cdabbrev-completions-tried
|
|
895 (cons (downcase abbrev-string) initial-completions-tried)
|
|
896 )
|
|
897 (reset-cdabbrev-window t)
|
|
898 )
|
|
899
|
|
900 (defun set-cdabbrev-buffer ()
|
|
901 ;; cdabbrev-current-window must not be NIL
|
|
902 (set-buffer (if (eq cdabbrev-current-window t)
|
|
903 (other-buffer)
|
|
904 (window-buffer cdabbrev-current-window)))
|
|
905 )
|
|
906
|
|
907
|
|
908 (defun reset-cdabbrev-window (&optional initializep)
|
|
909 "Resets the cdabbrev search to search for abbrev-string."
|
|
910 ;; Set the window
|
|
911 (cond (initializep
|
|
912 (setq cdabbrev-current-window (selected-window))
|
|
913 )
|
|
914 ((eq cdabbrev-current-window t)
|
|
915 ;; Everything has failed
|
|
916 (setq cdabbrev-current-window nil))
|
|
917 (cdabbrev-current-window
|
|
918 (setq cdabbrev-current-window (next-window cdabbrev-current-window))
|
|
919 (if (eq cdabbrev-current-window (selected-window))
|
|
920 ;; No more windows, try other buffer.
|
|
921 (setq cdabbrev-current-window t)))
|
|
922 )
|
|
923 (if cdabbrev-current-window
|
|
924 (save-excursion
|
|
925 (set-cdabbrev-buffer)
|
|
926 (setq cdabbrev-current-point (point)
|
|
927 cdabbrev-start-point cdabbrev-current-point
|
|
928 cdabbrev-stop-point
|
|
929 (if completion-search-distance
|
|
930 (max (point-min)
|
|
931 (- cdabbrev-start-point completion-search-distance))
|
|
932 (point-min))
|
|
933 cdabbrev-wrapped-p nil)
|
|
934 )))
|
|
935
|
|
936 (defun next-cdabbrev ()
|
|
937 "Return the next possible cdabbrev expansion or nil if there isn't one.
|
|
938 `reset-cdabbrev' must've been called already.
|
|
939 This is sensitive to `case-fold-search'."
|
|
940 ;; note that case-fold-search affects the behavior of this function
|
|
941 ;; Bug: won't pick up an expansion that starts at the top of buffer
|
|
942 (if cdabbrev-current-window
|
|
943 (let (saved-point
|
|
944 saved-syntax
|
|
945 (expansion nil)
|
|
946 downcase-expansion tried-list syntax saved-point-2)
|
|
947 (save-excursion
|
|
948 (unwind-protect
|
|
949 (progn
|
|
950 ;; Switch to current completion buffer
|
|
951 (set-cdabbrev-buffer)
|
|
952 ;; Save current buffer state
|
|
953 (setq saved-point (point)
|
|
954 saved-syntax (syntax-table))
|
|
955 ;; Restore completion state
|
|
956 (set-syntax-table cmpl-syntax-table)
|
|
957 (goto-char cdabbrev-current-point)
|
|
958 ;; Loop looking for completions
|
|
959 (while
|
|
960 ;; This code returns t if it should loop again
|
|
961 (cond
|
|
962 (;; search for the string
|
|
963 (search-backward cdabbrev-abbrev-string cdabbrev-stop-point t)
|
|
964 ;; return nil if the completion is valid
|
|
965 (not
|
|
966 (and
|
|
967 ;; does it start with a separator char ?
|
|
968 (or (= (setq syntax (char-syntax (preceding-char))) ? )
|
|
969 (and (= syntax ?w)
|
|
970 ;; symbol char to ignore at end. Are we at end ?
|
|
971 (progn
|
|
972 (setq saved-point-2 (point))
|
|
973 (forward-word -1)
|
|
974 (prog1
|
|
975 (= (char-syntax (preceding-char)) ? )
|
|
976 (goto-char saved-point-2)
|
|
977 ))))
|
|
978 ;; is the symbol long enough ?
|
|
979 (setq expansion (symbol-under-point))
|
|
980 ;; have we not tried this one before
|
|
981 (progn
|
|
982 ;; See if we've already used it
|
|
983 (setq tried-list cdabbrev-completions-tried
|
|
984 downcase-expansion (downcase expansion))
|
|
985 (while (and tried-list
|
|
986 (not (string-equal downcase-expansion
|
|
987 (car tried-list))))
|
|
988 ;; Already tried, don't choose this one
|
|
989 (setq tried-list (cdr tried-list))
|
|
990 )
|
|
991 ;; at this point tried-list will be nil if this
|
|
992 ;; expansion has not yet been tried
|
|
993 (if tried-list
|
|
994 (setq expansion nil)
|
|
995 t)
|
|
996 ))))
|
|
997 ;; search failed
|
|
998 (cdabbrev-wrapped-p
|
|
999 ;; If already wrapped, then we've failed completely
|
|
1000 nil)
|
|
1001 (t
|
|
1002 ;; need to wrap
|
|
1003 (goto-char (setq cdabbrev-current-point
|
|
1004 (if completion-search-distance
|
|
1005 (min (point-max) (+ cdabbrev-start-point completion-search-distance))
|
|
1006 (point-max))))
|
|
1007
|
|
1008 (setq cdabbrev-wrapped-p t))
|
|
1009 ))
|
|
1010 ;; end of while loop
|
|
1011 (cond (expansion
|
|
1012 ;; successful
|
|
1013 (setq cdabbrev-completions-tried
|
|
1014 (cons downcase-expansion cdabbrev-completions-tried)
|
|
1015 cdabbrev-current-point (point))))
|
|
1016 )
|
|
1017 (set-syntax-table saved-syntax)
|
|
1018 (goto-char saved-point)
|
|
1019 ))
|
|
1020 ;; If no expansion, go to next window
|
|
1021 (cond (expansion)
|
|
1022 (t (reset-cdabbrev-window)
|
|
1023 (next-cdabbrev))))))
|
|
1024
|
2
|
1025 ;; The following must be eval'd in the minibuffer ::
|
|
1026 ;; (reset-cdabbrev "cdat")
|
|
1027 ;; (next-cdabbrev) --> "cdat-simple"
|
|
1028 ;; (next-cdabbrev) --> "cdat-1-2-3-4"
|
|
1029 ;; (next-cdabbrev) --> "CDAT-UPCASE"
|
|
1030 ;; (next-cdabbrev) --> "cdat-wrapping"
|
|
1031 ;; (next-cdabbrev) --> "cdat_start_sym"
|
|
1032 ;; (next-cdabbrev) --> nil
|
|
1033 ;; (next-cdabbrev) --> nil
|
|
1034 ;; (next-cdabbrev) --> nil
|
0
|
1035
|
2
|
1036 ;; _cdat_start_sym
|
|
1037 ;; cdat-wrapping
|
0
|
1038
|
|
1039
|
2
|
1040 ;;---------------------------------------------------------------------------
|
|
1041 ;; Completion Database
|
|
1042 ;;---------------------------------------------------------------------------
|
0
|
1043
|
2
|
1044 ;; We use two storage modes for the two search types ::
|
|
1045 ;; 1) Prefix {cmpl-prefix-obarray} for looking up possible completions
|
|
1046 ;; Used by search-completion-next
|
|
1047 ;; the value of the symbol is nil or a cons of head and tail pointers
|
|
1048 ;; 2) Interning {cmpl-obarray} to see if it's in the database
|
|
1049 ;; Used by find-exact-completion, completion-in-database-p
|
|
1050 ;; The value of the symbol is the completion entry
|
0
|
1051
|
2
|
1052 ;; bad things may happen if this length is changed due to the way
|
|
1053 ;; GNU implements obarrays
|
0
|
1054 (defconst cmpl-obarray-length 511)
|
|
1055
|
|
1056 (defvar cmpl-prefix-obarray (make-vector cmpl-obarray-length 0)
|
|
1057 "An obarray used to store the downcased completion prefixes.
|
|
1058 Each symbol is bound to a list of completion entries.")
|
|
1059
|
|
1060 (defvar cmpl-obarray (make-vector cmpl-obarray-length 0)
|
|
1061 "An obarray used to store the downcased completions.
|
|
1062 Each symbol is bound to a single completion entry.")
|
|
1063
|
2
|
1064 ;;-----------------------------------------------
|
|
1065 ;; Completion Entry Structure Definition
|
|
1066 ;;-----------------------------------------------
|
0
|
1067
|
2
|
1068 ;; A completion entry is a LIST of string, prefix-symbol num-uses, and
|
|
1069 ;; last-use-time (the time the completion was last used)
|
|
1070 ;; last-use-time is T if the string should be kept permanently
|
|
1071 ;; num-uses is incremented every time the completion is used.
|
0
|
1072
|
2
|
1073 ;; We chose lists because (car foo) is faster than (aref foo 0) and the
|
|
1074 ;; creation time is about the same.
|
0
|
1075
|
2
|
1076 ;; READER MACROS
|
0
|
1077
|
|
1078 (defmacro completion-string (completion-entry)
|
|
1079 (list 'car completion-entry))
|
|
1080
|
|
1081 (defmacro completion-num-uses (completion-entry)
|
|
1082 ;; "The number of times it has used. Used to decide whether to save
|
|
1083 ;; it."
|
|
1084 (list 'car (list 'cdr completion-entry)))
|
|
1085
|
|
1086 (defmacro completion-last-use-time (completion-entry)
|
|
1087 ;; "The time it was last used. In hours since origin. Used to decide
|
|
1088 ;; whether to save it. T if one should always save it."
|
|
1089 (list 'nth 2 completion-entry))
|
|
1090
|
|
1091 (defmacro completion-source (completion-entry)
|
|
1092 (list 'nth 3 completion-entry))
|
|
1093
|
2
|
1094 ;; WRITER MACROS
|
0
|
1095 (defmacro set-completion-string (completion-entry string)
|
|
1096 (list 'setcar completion-entry string))
|
|
1097
|
|
1098 (defmacro set-completion-num-uses (completion-entry num-uses)
|
|
1099 (list 'setcar (list 'cdr completion-entry) num-uses))
|
|
1100
|
|
1101 (defmacro set-completion-last-use-time (completion-entry last-use-time)
|
|
1102 (list 'setcar (list 'cdr (list 'cdr completion-entry)) last-use-time))
|
|
1103
|
2
|
1104 ;; CONSTRUCTOR
|
0
|
1105 (defun make-completion (string)
|
|
1106 "Returns a list of a completion entry."
|
|
1107 (list (list string 0 nil current-completion-source)))
|
|
1108
|
|
1109 ;; Obsolete
|
|
1110 ;;(defmacro cmpl-prefix-entry-symbol (completion-entry)
|
|
1111 ;; (list 'car (list 'cdr completion-entry)))
|
|
1112
|
|
1113
|
|
1114
|
2
|
1115 ;;-----------------------------------------------
|
|
1116 ;; Prefix symbol entry definition
|
|
1117 ;;-----------------------------------------------
|
|
1118 ;; A cons of (head . tail)
|
0
|
1119
|
2
|
1120 ;; READER Macros
|
0
|
1121
|
|
1122 (defmacro cmpl-prefix-entry-head (prefix-entry)
|
|
1123 (list 'car prefix-entry))
|
|
1124
|
|
1125 (defmacro cmpl-prefix-entry-tail (prefix-entry)
|
|
1126 (list 'cdr prefix-entry))
|
|
1127
|
2
|
1128 ;; WRITER Macros
|
0
|
1129
|
|
1130 (defmacro set-cmpl-prefix-entry-head (prefix-entry new-head)
|
|
1131 (list 'setcar prefix-entry new-head))
|
|
1132
|
|
1133 (defmacro set-cmpl-prefix-entry-tail (prefix-entry new-tail)
|
|
1134 (list 'setcdr prefix-entry new-tail))
|
|
1135
|
2
|
1136 ;; Constructor
|
0
|
1137
|
|
1138 (defun make-cmpl-prefix-entry (completion-entry-list)
|
|
1139 "Makes a new prefix entry containing only completion-entry."
|
|
1140 (cons completion-entry-list completion-entry-list))
|
|
1141
|
2
|
1142 ;;-----------------------------------------------
|
|
1143 ;; Completion Database - Utilities
|
|
1144 ;;-----------------------------------------------
|
0
|
1145
|
|
1146 (defun clear-all-completions ()
|
|
1147 "Initializes the completion storage. All existing completions are lost."
|
|
1148 (interactive)
|
|
1149 (setq cmpl-prefix-obarray (make-vector cmpl-obarray-length 0))
|
|
1150 (setq cmpl-obarray (make-vector cmpl-obarray-length 0))
|
|
1151 (cmpl-statistics-block
|
|
1152 (record-clear-all-completions))
|
|
1153 )
|
|
1154
|
|
1155 (defvar completions-list-return-value)
|
|
1156
|
|
1157 (defun list-all-completions ()
|
|
1158 "Returns a list of all the known completion entries."
|
|
1159 (let ((completions-list-return-value nil))
|
|
1160 (mapatoms 'list-all-completions-1 cmpl-prefix-obarray)
|
|
1161 completions-list-return-value))
|
|
1162
|
|
1163 (defun list-all-completions-1 (prefix-symbol)
|
|
1164 (if (boundp prefix-symbol)
|
|
1165 (setq completions-list-return-value
|
|
1166 (append (cmpl-prefix-entry-head (symbol-value prefix-symbol))
|
|
1167 completions-list-return-value))))
|
|
1168
|
|
1169 (defun list-all-completions-by-hash-bucket ()
|
|
1170 "Return list of lists of known completion entries, organized by hash bucket."
|
|
1171 (let ((completions-list-return-value nil))
|
|
1172 (mapatoms 'list-all-completions-by-hash-bucket-1 cmpl-prefix-obarray)
|
|
1173 completions-list-return-value))
|
|
1174
|
|
1175 (defun list-all-completions-by-hash-bucket-1 (prefix-symbol)
|
|
1176 (if (boundp prefix-symbol)
|
|
1177 (setq completions-list-return-value
|
|
1178 (cons (cmpl-prefix-entry-head (symbol-value prefix-symbol))
|
|
1179 completions-list-return-value))))
|
|
1180
|
|
1181
|
2
|
1182 ;;-----------------------------------------------
|
|
1183 ;; Updating the database
|
|
1184 ;;-----------------------------------------------
|
|
1185 ;;
|
|
1186 ;; These are the internal functions used to update the datebase
|
|
1187 ;;
|
|
1188 ;;
|
0
|
1189 (defvar completion-to-accept nil)
|
|
1190 ;;"Set to a string that is pending its acceptance."
|
|
1191 ;; this checked by the top level reading functions
|
|
1192
|
|
1193 (defvar cmpl-db-downcase-string nil)
|
|
1194 ;; "Setup by find-exact-completion, etc. The given string, downcased."
|
|
1195 (defvar cmpl-db-symbol nil)
|
|
1196 ;; "The interned symbol corresponding to cmpl-db-downcase-string.
|
|
1197 ;; Set up by cmpl-db-symbol."
|
|
1198 (defvar cmpl-db-prefix-symbol nil)
|
|
1199 ;; "The interned prefix symbol corresponding to cmpl-db-downcase-string."
|
|
1200 (defvar cmpl-db-entry nil)
|
|
1201 (defvar cmpl-db-debug-p nil
|
|
1202 "Set to T if you want to debug the database.")
|
|
1203
|
2
|
1204 ;; READS
|
0
|
1205 (defun find-exact-completion (string)
|
|
1206 "Returns the completion entry for string or nil.
|
|
1207 Sets up `cmpl-db-downcase-string' and `cmpl-db-symbol'."
|
|
1208 (and (boundp (setq cmpl-db-symbol
|
|
1209 (intern (setq cmpl-db-downcase-string (downcase string))
|
|
1210 cmpl-obarray)))
|
|
1211 (symbol-value cmpl-db-symbol)
|
|
1212 ))
|
|
1213
|
|
1214 (defun find-cmpl-prefix-entry (prefix-string)
|
|
1215 "Returns the prefix entry for string.
|
|
1216 Sets `cmpl-db-prefix-symbol'.
|
|
1217 Prefix-string must be exactly `completion-prefix-min-length' long
|
|
1218 and downcased. Sets up `cmpl-db-prefix-symbol'."
|
|
1219 (and (boundp (setq cmpl-db-prefix-symbol
|
|
1220 (intern prefix-string cmpl-prefix-obarray)))
|
|
1221 (symbol-value cmpl-db-prefix-symbol)))
|
|
1222
|
|
1223 (defvar inside-locate-completion-entry nil)
|
|
1224 ;; used to trap lossage in silent error correction
|
|
1225
|
|
1226 (defun locate-completion-entry (completion-entry prefix-entry)
|
|
1227 "Locates the completion entry.
|
|
1228 Returns a pointer to the element before the completion entry or nil if
|
|
1229 the completion entry is at the head.
|
|
1230 Must be called after `find-exact-completion'."
|
|
1231 (let ((prefix-list (cmpl-prefix-entry-head prefix-entry))
|
|
1232 next-prefix-list
|
|
1233 )
|
|
1234 (cond
|
|
1235 ((not (eq (car prefix-list) completion-entry))
|
|
1236 ;; not already at head
|
|
1237 (while (and prefix-list
|
|
1238 (not (eq completion-entry
|
|
1239 (car (setq next-prefix-list (cdr prefix-list)))
|
|
1240 )))
|
|
1241 (setq prefix-list next-prefix-list))
|
|
1242 (cond (;; found
|
|
1243 prefix-list)
|
|
1244 ;; Didn't find it. Database is messed up.
|
|
1245 (cmpl-db-debug-p
|
|
1246 ;; not found, error if debug mode
|
|
1247 (error "Completion entry exists but not on prefix list - %s"
|
|
1248 completion-string))
|
|
1249 (inside-locate-completion-entry
|
|
1250 ;; recursive error: really scrod
|
|
1251 (locate-completion-db-error))
|
|
1252 (t
|
|
1253 ;; Patch out
|
|
1254 (set cmpl-db-symbol nil)
|
|
1255 ;; Retry
|
|
1256 (locate-completion-entry-retry completion-entry)
|
|
1257 ))))))
|
|
1258
|
|
1259 (defun locate-completion-entry-retry (old-entry)
|
|
1260 (let ((inside-locate-completion-entry t))
|
|
1261 (add-completion (completion-string old-entry)
|
|
1262 (completion-num-uses old-entry)
|
|
1263 (completion-last-use-time old-entry))
|
|
1264 (let* ((cmpl-entry (find-exact-completion (completion-string old-entry)))
|
|
1265 (pref-entry
|
|
1266 (if cmpl-entry
|
|
1267 (find-cmpl-prefix-entry
|
|
1268 (substring cmpl-db-downcase-string
|
|
1269 0 completion-prefix-min-length))))
|
|
1270 )
|
|
1271 (if (and cmpl-entry pref-entry)
|
|
1272 ;; try again
|
|
1273 (locate-completion-entry cmpl-entry pref-entry)
|
|
1274 ;; still losing
|
|
1275 (locate-completion-db-error))
|
|
1276 )))
|
|
1277
|
|
1278 (defun locate-completion-db-error ()
|
|
1279 ;; recursive error: really scrod
|
|
1280 (error "Completion database corrupted. Try M-x clear-all-completions. Send bug report.")
|
|
1281 )
|
|
1282
|
2
|
1283 ;; WRITES
|
0
|
1284 (defun add-completion-to-tail-if-new (string)
|
|
1285 "If STRING is not in the database add it to appropriate prefix list.
|
|
1286 STRING is added to the end of the appropriate prefix list with
|
|
1287 num-uses = 0. The database is unchanged if it is there. STRING must be
|
|
1288 longer than `completion-prefix-min-length'.
|
|
1289 This must be very fast.
|
|
1290 Returns the completion entry."
|
|
1291 (or (find-exact-completion string)
|
|
1292 ;; not there
|
|
1293 (let (;; create an entry
|
|
1294 (entry (make-completion string))
|
|
1295 ;; setup the prefix
|
|
1296 (prefix-entry (find-cmpl-prefix-entry
|
|
1297 (substring cmpl-db-downcase-string 0
|
|
1298 (cmpl-read-time-eval
|
|
1299 completion-prefix-min-length))))
|
|
1300 )
|
|
1301 ;; The next two forms should happen as a unit (atomically) but
|
|
1302 ;; no fatal errors should result if that is not the case.
|
|
1303 (cond (prefix-entry
|
|
1304 ;; These two should be atomic, but nothing fatal will happen
|
|
1305 ;; if they're not.
|
|
1306 (setcdr (cmpl-prefix-entry-tail prefix-entry) entry)
|
|
1307 (set-cmpl-prefix-entry-tail prefix-entry entry))
|
|
1308 (t
|
|
1309 (set cmpl-db-prefix-symbol (make-cmpl-prefix-entry entry))
|
|
1310 ))
|
|
1311 ;; statistics
|
|
1312 (cmpl-statistics-block
|
|
1313 (note-added-completion))
|
|
1314 ;; set symbol
|
|
1315 (set cmpl-db-symbol (car entry))
|
|
1316 )))
|
|
1317
|
|
1318 (defun add-completion-to-head (completion-string)
|
|
1319 "If COMPLETION-STRING is not in the database, add it to prefix list.
|
|
1320 We add COMPLETION-STRING to the head of the appropriate prefix list,
|
|
1321 or it to the head of the list.
|
|
1322 COMPLETION-STRING must be longer than `completion-prefix-min-length'.
|
|
1323 Updates the saved string with the supplied string.
|
|
1324 This must be very fast.
|
|
1325 Returns the completion entry."
|
|
1326 ;; Handle pending acceptance
|
|
1327 (if completion-to-accept (accept-completion))
|
|
1328 ;; test if already in database
|
|
1329 (if (setq cmpl-db-entry (find-exact-completion completion-string))
|
|
1330 ;; found
|
|
1331 (let* ((prefix-entry (find-cmpl-prefix-entry
|
|
1332 (substring cmpl-db-downcase-string 0
|
|
1333 (cmpl-read-time-eval
|
|
1334 completion-prefix-min-length))))
|
|
1335 (splice-ptr (locate-completion-entry cmpl-db-entry prefix-entry))
|
|
1336 (cmpl-ptr (cdr splice-ptr))
|
|
1337 )
|
|
1338 ;; update entry
|
|
1339 (set-completion-string cmpl-db-entry completion-string)
|
|
1340 ;; move to head (if necessary)
|
|
1341 (cond (splice-ptr
|
|
1342 ;; These should all execute atomically but it is not fatal if
|
|
1343 ;; they don't.
|
|
1344 ;; splice it out
|
|
1345 (or (setcdr splice-ptr (cdr cmpl-ptr))
|
|
1346 ;; fix up tail if necessary
|
|
1347 (set-cmpl-prefix-entry-tail prefix-entry splice-ptr))
|
|
1348 ;; splice in at head
|
|
1349 (setcdr cmpl-ptr (cmpl-prefix-entry-head prefix-entry))
|
|
1350 (set-cmpl-prefix-entry-head prefix-entry cmpl-ptr)
|
|
1351 ))
|
|
1352 cmpl-db-entry)
|
|
1353 ;; not there
|
|
1354 (let (;; create an entry
|
|
1355 (entry (make-completion completion-string))
|
|
1356 ;; setup the prefix
|
|
1357 (prefix-entry (find-cmpl-prefix-entry
|
|
1358 (substring cmpl-db-downcase-string 0
|
|
1359 (cmpl-read-time-eval
|
|
1360 completion-prefix-min-length))))
|
|
1361 )
|
|
1362 (cond (prefix-entry
|
|
1363 ;; Splice in at head
|
|
1364 (setcdr entry (cmpl-prefix-entry-head prefix-entry))
|
|
1365 (set-cmpl-prefix-entry-head prefix-entry entry))
|
|
1366 (t
|
|
1367 ;; Start new prefix entry
|
|
1368 (set cmpl-db-prefix-symbol (make-cmpl-prefix-entry entry))
|
|
1369 ))
|
|
1370 ;; statistics
|
|
1371 (cmpl-statistics-block
|
|
1372 (note-added-completion))
|
|
1373 ;; Add it to the symbol
|
|
1374 (set cmpl-db-symbol (car entry))
|
|
1375 )))
|
|
1376
|
|
1377 (defun delete-completion (completion-string)
|
|
1378 "Deletes the completion from the database.
|
|
1379 String must be longer than `completion-prefix-min-length'."
|
|
1380 ;; Handle pending acceptance
|
|
1381 (if completion-to-accept (accept-completion))
|
|
1382 (if (setq cmpl-db-entry (find-exact-completion completion-string))
|
|
1383 ;; found
|
|
1384 (let* ((prefix-entry (find-cmpl-prefix-entry
|
|
1385 (substring cmpl-db-downcase-string 0
|
|
1386 (cmpl-read-time-eval
|
|
1387 completion-prefix-min-length))))
|
|
1388 (splice-ptr (locate-completion-entry cmpl-db-entry prefix-entry))
|
|
1389 )
|
|
1390 ;; delete symbol reference
|
|
1391 (set cmpl-db-symbol nil)
|
|
1392 ;; remove from prefix list
|
|
1393 (cond (splice-ptr
|
|
1394 ;; not at head
|
|
1395 (or (setcdr splice-ptr (cdr (cdr splice-ptr)))
|
|
1396 ;; fix up tail if necessary
|
|
1397 (set-cmpl-prefix-entry-tail prefix-entry splice-ptr))
|
|
1398 )
|
|
1399 (t
|
|
1400 ;; at head
|
|
1401 (or (set-cmpl-prefix-entry-head
|
|
1402 prefix-entry (cdr (cmpl-prefix-entry-head prefix-entry)))
|
|
1403 ;; List is now empty
|
|
1404 (set cmpl-db-prefix-symbol nil))
|
|
1405 ))
|
|
1406 (cmpl-statistics-block
|
|
1407 (note-completion-deleted))
|
|
1408 )
|
|
1409 (error "Unknown completion `%s'" completion-string)
|
|
1410 ))
|
|
1411
|
2
|
1412 ;; Tests --
|
|
1413 ;; - Add and Find -
|
|
1414 ;; (add-completion-to-head "banana") --> ("banana" 0 nil 0)
|
|
1415 ;; (find-exact-completion "banana") --> ("banana" 0 nil 0)
|
|
1416 ;; (find-exact-completion "bana") --> nil
|
|
1417 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
|
|
1418 ;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
|
|
1419 ;; (add-completion-to-head "banish") --> ("banish" 0 nil 0)
|
|
1420 ;; (find-exact-completion "banish") --> ("banish" 0 nil 0)
|
|
1421 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banish" ...) ("banana" ...))
|
|
1422 ;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
|
|
1423 ;; (add-completion-to-head "banana") --> ("banana" 0 nil 0)
|
|
1424 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...))
|
|
1425 ;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...))
|
|
1426 ;;
|
|
1427 ;; - Deleting -
|
|
1428 ;; (add-completion-to-head "banner") --> ("banner" 0 nil 0)
|
|
1429 ;; (delete-completion "banner")
|
|
1430 ;; (find-exact-completion "banner") --> nil
|
|
1431 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...))
|
|
1432 ;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...))
|
|
1433 ;; (add-completion-to-head "banner") --> ("banner" 0 nil 0)
|
|
1434 ;; (delete-completion "banana")
|
|
1435 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banner" ...) ("banish" ...))
|
|
1436 ;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banish" ...))
|
|
1437 ;; (delete-completion "banner")
|
|
1438 ;; (delete-completion "banish")
|
|
1439 ;; (find-cmpl-prefix-entry "ban") --> nil
|
|
1440 ;; (delete-completion "banner") --> error
|
|
1441 ;;
|
|
1442 ;; - Tail -
|
|
1443 ;; (add-completion-to-tail-if-new "banana") --> ("banana" 0 nil 0)
|
|
1444 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
|
|
1445 ;; (cdr (find-cmpl-prefix-entry "ban")) --> (("banana" ...))
|
|
1446 ;; (add-completion-to-tail-if-new "banish") --> ("banish" 0 nil 0)
|
|
1447 ;; (car (find-cmpl-prefix-entry "ban")) -->(("banana" ...) ("banish" ...))
|
|
1448 ;; (cdr (find-cmpl-prefix-entry "ban")) -->(("banish" ...))
|
|
1449 ;;
|
0
|
1450
|
|
1451
|
2
|
1452 ;;---------------------------------------------------------------------------
|
|
1453 ;; Database Update :: Interface level routines
|
|
1454 ;;---------------------------------------------------------------------------
|
|
1455 ;;
|
|
1456 ;; These lie on top of the database ref. functions but below the standard
|
|
1457 ;; user interface level
|
0
|
1458
|
|
1459
|
|
1460 (defun interactive-completion-string-reader (prompt)
|
|
1461 (let* ((default (symbol-under-or-before-point))
|
|
1462 (new-prompt
|
|
1463 (if default
|
|
1464 (format "%s: (default: %s) " prompt default)
|
|
1465 (format "%s: " prompt))
|
|
1466 )
|
|
1467 (read (completing-read new-prompt cmpl-obarray))
|
|
1468 )
|
|
1469 (if (zerop (length read)) (setq read (or default "")))
|
|
1470 (list read)
|
|
1471 ))
|
|
1472
|
|
1473 (defun check-completion-length (string)
|
|
1474 (if (< (length string) completion-min-length)
|
|
1475 (error "The string `%s' is too short to be saved as a completion"
|
|
1476 string)
|
|
1477 (list string)))
|
|
1478
|
|
1479 (defun add-completion (string &optional num-uses last-use-time)
|
|
1480 "Add STRING to completion list, or move it to head of list.
|
|
1481 The completion is altered appropriately if num-uses and/or last-use-time is
|
|
1482 specified."
|
|
1483 (interactive (interactive-completion-string-reader "Completion to add"))
|
|
1484 (check-completion-length string)
|
|
1485 (let* ((current-completion-source (if (interactive-p)
|
|
1486 cmpl-source-interactive
|
|
1487 current-completion-source))
|
|
1488 (entry (add-completion-to-head string)))
|
|
1489
|
|
1490 (if num-uses (set-completion-num-uses entry num-uses))
|
|
1491 (if last-use-time
|
|
1492 (set-completion-last-use-time entry last-use-time))
|
|
1493 ))
|
|
1494
|
|
1495 (defun add-permanent-completion (string)
|
|
1496 "Add STRING if it isn't already listed, and mark it permanent."
|
|
1497 (interactive
|
|
1498 (interactive-completion-string-reader "Completion to add permanently"))
|
|
1499 (let ((current-completion-source (if (interactive-p)
|
|
1500 cmpl-source-interactive
|
|
1501 current-completion-source))
|
|
1502 )
|
|
1503 (add-completion string nil t)
|
|
1504 ))
|
|
1505
|
|
1506 (defun kill-completion (string)
|
|
1507 (interactive (interactive-completion-string-reader "Completion to kill"))
|
|
1508 (check-completion-length string)
|
|
1509 (delete-completion string)
|
|
1510 )
|
|
1511
|
|
1512 (defun accept-completion ()
|
|
1513 "Accepts the pending completion in `completion-to-accept'.
|
|
1514 This bumps num-uses. Called by `add-completion-to-head' and
|
|
1515 `completion-search-reset'."
|
|
1516 (let ((string completion-to-accept)
|
|
1517 ;; if this is added afresh here, then it must be a cdabbrev
|
|
1518 (current-completion-source cmpl-source-cdabbrev)
|
|
1519 entry
|
|
1520 )
|
|
1521 (setq completion-to-accept nil)
|
|
1522 (setq entry (add-completion-to-head string))
|
|
1523 (set-completion-num-uses entry (1+ (completion-num-uses entry)))
|
|
1524 (setq cmpl-completions-accepted-p t)
|
|
1525 ))
|
|
1526
|
|
1527 (defun use-completion-under-point ()
|
|
1528 "Add the completion symbol underneath the point into the completion buffer."
|
|
1529 (let ((string (and enable-completion (symbol-under-point)))
|
|
1530 (current-completion-source cmpl-source-cursor-moves))
|
|
1531 (if string (add-completion-to-head string))))
|
|
1532
|
|
1533 (defun use-completion-before-point ()
|
|
1534 "Add the completion symbol before point into the completion buffer."
|
|
1535 (let ((string (and enable-completion (symbol-before-point)))
|
|
1536 (current-completion-source cmpl-source-cursor-moves))
|
|
1537 (if string (add-completion-to-head string))))
|
|
1538
|
|
1539 (defun use-completion-under-or-before-point ()
|
|
1540 "Add the completion symbol before point into the completion buffer."
|
|
1541 (let ((string (and enable-completion (symbol-under-or-before-point)))
|
|
1542 (current-completion-source cmpl-source-cursor-moves))
|
|
1543 (if string (add-completion-to-head string))))
|
|
1544
|
|
1545 (defun use-completion-before-separator ()
|
|
1546 "Add the completion symbol before point into the completion buffer.
|
|
1547 Completions added this way will automatically be saved if
|
|
1548 `completion-on-separator-character' is non-nil."
|
|
1549 (let ((string (and enable-completion (symbol-before-point)))
|
|
1550 (current-completion-source cmpl-source-separator)
|
|
1551 entry)
|
|
1552 (cmpl-statistics-block
|
|
1553 (note-separator-character string)
|
|
1554 )
|
|
1555 (cond (string
|
|
1556 (setq entry (add-completion-to-head string))
|
|
1557 (if (and completion-on-separator-character
|
|
1558 (zerop (completion-num-uses entry)))
|
|
1559 (progn
|
|
1560 (set-completion-num-uses entry 1)
|
|
1561 (setq cmpl-completions-accepted-p t)))))
|
|
1562 ))
|
|
1563
|
2
|
1564 ;; Tests --
|
|
1565 ;; - Add and Find -
|
|
1566 ;; (add-completion "banana" 5 10)
|
|
1567 ;; (find-exact-completion "banana") --> ("banana" 5 10 0)
|
|
1568 ;; (add-completion "banana" 6)
|
|
1569 ;; (find-exact-completion "banana") --> ("banana" 6 10 0)
|
|
1570 ;; (add-completion "banish")
|
|
1571 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banish" ...) ("banana" ...))
|
|
1572 ;;
|
|
1573 ;; - Accepting -
|
|
1574 ;; (setq completion-to-accept "banana")
|
|
1575 ;; (accept-completion)
|
|
1576 ;; (find-exact-completion "banana") --> ("banana" 7 10)
|
|
1577 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banana" ...) ("banish" ...))
|
|
1578 ;; (setq completion-to-accept "banish")
|
|
1579 ;; (add-completion "banner")
|
|
1580 ;; (car (find-cmpl-prefix-entry "ban"))
|
|
1581 ;; --> (("banner" ...) ("banish" 1 ...) ("banana" 7 ...))
|
|
1582 ;;
|
|
1583 ;; - Deleting -
|
|
1584 ;; (kill-completion "banish")
|
|
1585 ;; (car (find-cmpl-prefix-entry "ban")) --> (("banner" ...) ("banana" ...))
|
0
|
1586
|
|
1587
|
2
|
1588 ;;---------------------------------------------------------------------------
|
|
1589 ;; Searching the database
|
|
1590 ;;---------------------------------------------------------------------------
|
|
1591 ;; Functions outside this block must call completion-search-reset followed
|
|
1592 ;; by calls to completion-search-next or completion-search-peek
|
|
1593 ;;
|
0
|
1594
|
2
|
1595 ;; Status variables
|
0
|
1596 ;; Commented out to improve loading speed
|
|
1597 (defvar cmpl-test-string "")
|
|
1598 ;; "The current string used by completion-search-next."
|
|
1599 (defvar cmpl-test-regexp "")
|
|
1600 ;; "The current regexp used by completion-search-next.
|
|
1601 ;; (derived from cmpl-test-string)"
|
|
1602 (defvar cmpl-last-index 0)
|
|
1603 ;; "The last index that completion-search-next was called with."
|
|
1604 (defvar cmpl-cdabbrev-reset-p nil)
|
|
1605 ;; "Set to t when cdabbrevs have been reset."
|
|
1606 (defvar cmpl-next-possibilities nil)
|
|
1607 ;; "A pointer to the element BEFORE the next set of possible completions.
|
|
1608 ;; cadr of this is the cmpl-next-possibility"
|
|
1609 (defvar cmpl-starting-possibilities nil)
|
|
1610 ;; "The initial list of starting possibilities."
|
|
1611 (defvar cmpl-next-possibility nil)
|
|
1612 ;; "The cached next possibility."
|
|
1613 (defvar cmpl-tried-list nil)
|
|
1614 ;; "A downcased list of all the completions we have tried."
|
|
1615
|
|
1616
|
|
1617 (defun completion-search-reset (string)
|
|
1618 "Set up the for completion searching for STRING.
|
|
1619 STRING must be longer than `completion-prefix-min-length'."
|
|
1620 (if completion-to-accept (accept-completion))
|
|
1621 (setq cmpl-starting-possibilities
|
|
1622 (cmpl-prefix-entry-head
|
|
1623 (find-cmpl-prefix-entry
|
|
1624 (downcase (substring string 0 completion-prefix-min-length))))
|
|
1625 cmpl-test-string string
|
|
1626 cmpl-test-regexp (concat (regexp-quote string) "."))
|
|
1627 (completion-search-reset-1)
|
|
1628 )
|
|
1629
|
|
1630 (defun completion-search-reset-1 ()
|
|
1631 (setq cmpl-next-possibilities cmpl-starting-possibilities
|
|
1632 cmpl-next-possibility nil
|
|
1633 cmpl-cdabbrev-reset-p nil
|
|
1634 cmpl-last-index -1
|
|
1635 cmpl-tried-list nil
|
|
1636 ))
|
|
1637
|
|
1638 (defun completion-search-next (index)
|
|
1639 "Return the next completion entry.
|
|
1640 If INDEX is out of sequence, reset and start from the top.
|
|
1641 If there are no more entries, try cdabbrev and returns only a string."
|
|
1642 (cond
|
|
1643 ((= index (setq cmpl-last-index (1+ cmpl-last-index)))
|
|
1644 (completion-search-peek t))
|
|
1645 ((< index 0)
|
|
1646 (completion-search-reset-1)
|
|
1647 (setq cmpl-last-index index)
|
|
1648 ;; reverse the possibilities list
|
|
1649 (setq cmpl-next-possibilities (reverse cmpl-starting-possibilities))
|
|
1650 ;; do a "normal" search
|
|
1651 (while (and (completion-search-peek nil)
|
|
1652 (< (setq index (1+ index)) 0))
|
|
1653 (setq cmpl-next-possibility nil)
|
|
1654 )
|
|
1655 (cond ((not cmpl-next-possibilities))
|
|
1656 ;; If no more possibilities, leave it that way
|
|
1657 ((= -1 cmpl-last-index)
|
|
1658 ;; next completion is at index 0. reset next-possibility list
|
|
1659 ;; to start at beginning
|
|
1660 (setq cmpl-next-possibilities cmpl-starting-possibilities))
|
|
1661 (t
|
|
1662 ;; otherwise point to one before current
|
|
1663 (setq cmpl-next-possibilities
|
|
1664 (nthcdr (- (length cmpl-starting-possibilities)
|
|
1665 (length cmpl-next-possibilities))
|
|
1666 cmpl-starting-possibilities))
|
|
1667 )))
|
|
1668 (t
|
|
1669 ;; non-negative index, reset and search
|
|
1670 ;;(prin1 'reset)
|
|
1671 (completion-search-reset-1)
|
|
1672 (setq cmpl-last-index index)
|
|
1673 (while (and (completion-search-peek t)
|
|
1674 (not (< (setq index (1- index)) 0)))
|
|
1675 (setq cmpl-next-possibility nil)
|
|
1676 ))
|
|
1677 )
|
|
1678 (prog1
|
|
1679 cmpl-next-possibility
|
|
1680 (setq cmpl-next-possibility nil)
|
|
1681 ))
|
|
1682
|
|
1683
|
|
1684 (defun completion-search-peek (use-cdabbrev)
|
|
1685 "Returns the next completion entry without actually moving the pointers.
|
|
1686 Calling this again or calling `completion-search-next' results in the same
|
|
1687 string being returned. Depends on `case-fold-search'.
|
|
1688 If there are no more entries, try cdabbrev and then return only a string."
|
|
1689 (cond
|
|
1690 ;; return the cached value if we have it
|
|
1691 (cmpl-next-possibility)
|
|
1692 ((and cmpl-next-possibilities
|
|
1693 ;; still a few possibilities left
|
|
1694 (progn
|
|
1695 (while
|
|
1696 (and (not (eq 0 (string-match cmpl-test-regexp
|
|
1697 (completion-string (car cmpl-next-possibilities)))))
|
|
1698 (setq cmpl-next-possibilities (cdr cmpl-next-possibilities))
|
|
1699 ))
|
|
1700 cmpl-next-possibilities
|
|
1701 ))
|
|
1702 ;; successful match
|
|
1703 (setq cmpl-next-possibility (car cmpl-next-possibilities)
|
|
1704 cmpl-tried-list (cons (downcase (completion-string cmpl-next-possibility))
|
|
1705 cmpl-tried-list)
|
|
1706 cmpl-next-possibilities (cdr cmpl-next-possibilities)
|
|
1707 )
|
|
1708 cmpl-next-possibility)
|
|
1709 (use-cdabbrev
|
|
1710 ;; unsuccessful, use cdabbrev
|
|
1711 (cond ((not cmpl-cdabbrev-reset-p)
|
|
1712 (reset-cdabbrev cmpl-test-string cmpl-tried-list)
|
|
1713 (setq cmpl-cdabbrev-reset-p t)
|
|
1714 ))
|
|
1715 (setq cmpl-next-possibility (next-cdabbrev))
|
|
1716 )
|
|
1717 ;; Completely unsuccessful, return nil
|
|
1718 ))
|
|
1719
|
2
|
1720 ;; Tests --
|
|
1721 ;; - Add and Find -
|
|
1722 ;; (add-completion "banana")
|
|
1723 ;; (completion-search-reset "ban")
|
|
1724 ;; (completion-search-next 0) --> "banana"
|
|
1725 ;;
|
|
1726 ;; - Discrimination -
|
|
1727 ;; (add-completion "cumberland")
|
|
1728 ;; (add-completion "cumberbund")
|
|
1729 ;; cumbering
|
|
1730 ;; (completion-search-reset "cumb")
|
|
1731 ;; (completion-search-peek t) --> "cumberbund"
|
|
1732 ;; (completion-search-next 0) --> "cumberbund"
|
|
1733 ;; (completion-search-peek t) --> "cumberland"
|
|
1734 ;; (completion-search-next 1) --> "cumberland"
|
|
1735 ;; (completion-search-peek nil) --> nil
|
|
1736 ;; (completion-search-next 2) --> "cumbering" {cdabbrev}
|
|
1737 ;; (completion-search-next 3) --> nil or "cumming"{depends on context}
|
|
1738 ;; (completion-search-next 1) --> "cumberland"
|
|
1739 ;; (completion-search-peek t) --> "cumbering" {cdabbrev}
|
|
1740 ;;
|
|
1741 ;; - Accepting -
|
|
1742 ;; (completion-search-next 1) --> "cumberland"
|
|
1743 ;; (setq completion-to-accept "cumberland")
|
|
1744 ;; (completion-search-reset "foo")
|
|
1745 ;; (completion-search-reset "cum")
|
|
1746 ;; (completion-search-next 0) --> "cumberland"
|
|
1747 ;;
|
|
1748 ;; - Deleting -
|
|
1749 ;; (kill-completion "cumberland")
|
|
1750 ;; cummings
|
|
1751 ;; (completion-search-reset "cum")
|
|
1752 ;; (completion-search-next 0) --> "cumberbund"
|
|
1753 ;; (completion-search-next 1) --> "cummings"
|
|
1754 ;;
|
|
1755 ;; - Ignoring Capitalization -
|
|
1756 ;; (completion-search-reset "CuMb")
|
|
1757 ;; (completion-search-next 0) --> "cumberbund"
|
0
|
1758
|
|
1759
|
|
1760
|
2
|
1761 ;;-----------------------------------------------
|
|
1762 ;; COMPLETE
|
|
1763 ;;-----------------------------------------------
|
0
|
1764
|
|
1765 (defun completion-mode ()
|
|
1766 "Toggles whether or not to add new words to the completion database."
|
|
1767 (interactive)
|
|
1768 (setq enable-completion (not enable-completion))
|
|
1769 (message "Completion mode is now %s." (if enable-completion "ON" "OFF"))
|
|
1770 )
|
|
1771
|
|
1772 (defvar cmpl-current-index 0)
|
|
1773 (defvar cmpl-original-string nil)
|
|
1774 (defvar cmpl-last-insert-location -1)
|
|
1775 (defvar cmpl-leave-point-at-start nil)
|
|
1776
|
|
1777 (defun complete (&optional arg)
|
|
1778 "Fill out a completion of the word before point.
|
|
1779 Point is left at end. Consecutive calls rotate through all possibilities.
|
|
1780 Prefix args ::
|
|
1781 control-u :: leave the point at the beginning of the completion rather
|
|
1782 than at the end.
|
|
1783 a number :: rotate through the possible completions by that amount
|
|
1784 `-' :: same as -1 (insert previous completion)
|
|
1785 {See the comments at the top of `completion.el' for more info.}"
|
|
1786 (interactive "*p")
|
2
|
1787 ;; Set up variables
|
0
|
1788 (cond ((eq last-command this-command)
|
|
1789 ;; Undo last one
|
|
1790 (delete-region cmpl-last-insert-location (point))
|
|
1791 ;; get next completion
|
|
1792 (setq cmpl-current-index (+ cmpl-current-index (or arg 1)))
|
|
1793 )
|
|
1794 (t
|
|
1795 (if (not cmpl-initialized-p)
|
|
1796 (initialize-completions)) ;; make sure everything's loaded
|
|
1797 (cond ((consp current-prefix-arg) ;; control-u
|
|
1798 (setq arg 0)
|
|
1799 (setq cmpl-leave-point-at-start t)
|
|
1800 )
|
|
1801 (t
|
|
1802 (setq cmpl-leave-point-at-start nil)
|
|
1803 ))
|
|
1804 ;; get string
|
|
1805 (setq cmpl-original-string (symbol-before-point-for-complete))
|
|
1806 (cond ((not cmpl-original-string)
|
|
1807 (setq this-command 'failed-complete)
|
|
1808 (error "To complete, point must be after a symbol at least %d character long"
|
|
1809 completion-prefix-min-length)))
|
|
1810 ;; get index
|
|
1811 (setq cmpl-current-index (if current-prefix-arg arg 0))
|
|
1812 ;; statistics
|
|
1813 (cmpl-statistics-block
|
|
1814 (note-complete-entered-afresh cmpl-original-string))
|
|
1815 ;; reset database
|
|
1816 (completion-search-reset cmpl-original-string)
|
|
1817 ;; erase what we've got
|
|
1818 (delete-region cmpl-symbol-start cmpl-symbol-end)
|
|
1819 ))
|
|
1820
|
|
1821 ;; point is at the point to insert the new symbol
|
|
1822 ;; Get the next completion
|
|
1823 (let* ((print-status-p
|
|
1824 ;; XEmacs change
|
|
1825 (and (>= (device-baud-rate) completion-prompt-speed-threshold)
|
|
1826 (not (minibuffer-window-selected-p))))
|
|
1827 (insert-point (point))
|
|
1828 (entry (completion-search-next cmpl-current-index))
|
|
1829 string
|
|
1830 )
|
|
1831 ;; entry is either a completion entry or a string (if cdabbrev)
|
|
1832
|
|
1833 ;; If found, insert
|
|
1834 (cond (entry
|
|
1835 ;; Setup for proper case
|
|
1836 (setq string (if (stringp entry)
|
|
1837 entry (completion-string entry)))
|
|
1838 (setq string (cmpl-merge-string-cases
|
|
1839 string cmpl-original-string))
|
|
1840 ;; insert
|
|
1841 (insert string)
|
|
1842 ;; accept it
|
|
1843 (setq completion-to-accept string)
|
|
1844 ;; fixup and cache point
|
|
1845 (cond (cmpl-leave-point-at-start
|
|
1846 (setq cmpl-last-insert-location (point))
|
|
1847 (goto-char insert-point))
|
|
1848 (t;; point at end,
|
|
1849 (setq cmpl-last-insert-location insert-point))
|
|
1850 )
|
|
1851 ;; statistics
|
|
1852 (cmpl-statistics-block
|
|
1853 (note-complete-inserted entry cmpl-current-index))
|
|
1854 ;; Done ! cmpl-stat-complete-successful
|
|
1855 ;;display the next completion
|
|
1856 (cond
|
|
1857 ((and print-status-p
|
|
1858 ;; This updates the display and only prints if there
|
|
1859 ;; is no typeahead
|
|
1860 (sit-for 0)
|
|
1861 (setq entry
|
|
1862 (completion-search-peek
|
|
1863 completion-cdabbrev-prompt-flag)))
|
|
1864 (setq string (if (stringp entry)
|
|
1865 entry (completion-string entry)))
|
|
1866 (setq string (cmpl-merge-string-cases
|
|
1867 string cmpl-original-string))
|
|
1868 (message "Next completion: %s" string)
|
|
1869 ))
|
|
1870 )
|
|
1871 (t;; none found, insert old
|
|
1872 (insert cmpl-original-string)
|
|
1873 ;; Don't accept completions
|
|
1874 (setq completion-to-accept nil)
|
|
1875 ;; print message
|
|
1876 ;; This used to call cmpl19-sit-for, an undefined function.
|
|
1877 ;; I hope that sit-for does the right thing; I don't know -- rms.
|
|
1878 (if (and print-status-p (sit-for 0))
|
|
1879 (message "No %scompletions."
|
|
1880 (if (eq this-command last-command) "more " "")))
|
|
1881 ;; statistics
|
|
1882 (cmpl-statistics-block
|
|
1883 (record-complete-failed cmpl-current-index))
|
|
1884 ;; Pretend that we were never here
|
|
1885 (setq this-command 'failed-complete)
|
|
1886 ))))
|
|
1887
|
2
|
1888 ;;-----------------------------------------------
|
|
1889 ;; "Complete" Key Keybindings
|
|
1890 ;;-----------------------------------------------
|
0
|
1891
|
|
1892 ;; XEmacs change
|
|
1893 ;;(global-set-key "\M-\r" 'complete)
|
|
1894 ;;(global-set-key [?\C-\r] 'complete)
|
|
1895 ;;(define-key function-key-map [C-return] [?\C-\r])
|
|
1896 (global-set-key '(meta return) 'complete)
|
|
1897 (global-set-key '(control return) 'complete)
|
|
1898 ;; XEmacs: #### still need to take care of function-key-map
|
|
1899
|
2
|
1900 ;; Tests -
|
|
1901 ;; (add-completion "cumberland")
|
|
1902 ;; (add-completion "cumberbund")
|
|
1903 ;; cum
|
|
1904 ;; Cumber
|
|
1905 ;; cumbering
|
|
1906 ;; cumb
|
0
|
1907
|
|
1908
|
2
|
1909 ;;---------------------------------------------------------------------------
|
|
1910 ;; Parsing definitions from files into the database
|
|
1911 ;;---------------------------------------------------------------------------
|
0
|
1912
|
2
|
1913 ;;-----------------------------------------------
|
|
1914 ;; Top Level functions ::
|
|
1915 ;;-----------------------------------------------
|
0
|
1916
|
2
|
1917 ;; User interface
|
0
|
1918 (defun add-completions-from-file (file)
|
|
1919 "Parse possible completions from a file and add them to data base."
|
|
1920 (interactive "fFile: ")
|
|
1921 (setq file (expand-file-name file))
|
|
1922 (let* ((buffer (get-file-buffer file))
|
|
1923 (buffer-already-there-p buffer)
|
|
1924 )
|
|
1925 (if (not buffer-already-there-p)
|
|
1926 (let ((completions-merging-modes nil))
|
|
1927 (setq buffer (find-file-noselect file))))
|
|
1928 (unwind-protect
|
|
1929 (save-excursion
|
|
1930 (set-buffer buffer)
|
|
1931 (add-completions-from-buffer)
|
|
1932 )
|
|
1933 (if (not buffer-already-there-p)
|
|
1934 (kill-buffer buffer)))))
|
|
1935
|
|
1936 (defun add-completions-from-buffer ()
|
|
1937 (interactive)
|
|
1938 (let ((current-completion-source cmpl-source-file-parsing)
|
|
1939 (start-num
|
|
1940 (cmpl-statistics-block
|
|
1941 (aref completion-add-count-vector cmpl-source-file-parsing)))
|
|
1942 mode
|
|
1943 )
|
|
1944 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode))
|
|
1945 (add-completions-from-lisp-buffer)
|
|
1946 (setq mode 'lisp)
|
|
1947 )
|
|
1948 ((memq major-mode '(c-mode))
|
|
1949 (add-completions-from-c-buffer)
|
|
1950 (setq mode 'c)
|
|
1951 )
|
|
1952 (t
|
|
1953 (error "Cannot parse completions in %s buffers"
|
|
1954 major-mode)
|
|
1955 ))
|
|
1956 (cmpl-statistics-block
|
|
1957 (record-cmpl-parse-file
|
|
1958 mode (point-max)
|
|
1959 (- (aref completion-add-count-vector cmpl-source-file-parsing)
|
|
1960 start-num)))
|
|
1961 ))
|
|
1962
|
2
|
1963 ;; Find file hook
|
0
|
1964 (defun cmpl-find-file-hook ()
|
|
1965 (cond (enable-completion
|
|
1966 (cond ((and (memq major-mode '(emacs-lisp-mode lisp-mode))
|
|
1967 (memq 'lisp completions-merging-modes)
|
|
1968 )
|
|
1969 (add-completions-from-buffer))
|
|
1970 ((and (memq major-mode '(c-mode))
|
|
1971 (memq 'c completions-merging-modes)
|
|
1972 )
|
|
1973 (add-completions-from-buffer)
|
|
1974 )))
|
|
1975 ))
|
|
1976
|
|
1977 (add-hook 'find-file-hooks 'cmpl-find-file-hook)
|
|
1978
|
2
|
1979 ;;-----------------------------------------------
|
|
1980 ;; Tags Table Completions
|
|
1981 ;;-----------------------------------------------
|
0
|
1982
|
|
1983 (defun add-completions-from-tags-table ()
|
|
1984 ;; Inspired by eero@media-lab.media.mit.edu
|
|
1985 "Add completions from the current tags table."
|
|
1986 (interactive)
|
|
1987 (visit-tags-table-buffer) ;this will prompt if no tags-table
|
|
1988 (save-excursion
|
|
1989 (goto-char (point-min))
|
|
1990 (let (string)
|
|
1991 (condition-case e
|
|
1992 (while t
|
|
1993 (search-forward "\177")
|
|
1994 (backward-char 3)
|
|
1995 (and (setq string (symbol-under-point))
|
|
1996 (add-completion-to-tail-if-new string))
|
|
1997 (forward-char 3)
|
|
1998 )
|
|
1999 (search-failed)
|
|
2000 ))))
|
|
2001
|
|
2002
|
2
|
2003 ;;-----------------------------------------------
|
|
2004 ;; Lisp File completion parsing
|
|
2005 ;;-----------------------------------------------
|
|
2006 ;; This merely looks for phrases beginning with (def.... or
|
|
2007 ;; (package:def ... and takes the next word.
|
|
2008 ;;
|
|
2009 ;; We tried using forward-lines and explicit searches but the regexp technique
|
|
2010 ;; was faster. (About 100K characters per second)
|
|
2011 ;;
|
0
|
2012 (defconst *lisp-def-regexp*
|
|
2013 "\n(\\(\\w*:\\)?def\\(\\w\\|\\s_\\)*\\s +(*"
|
|
2014 "A regexp that searches for lisp definition form."
|
|
2015 )
|
|
2016
|
2
|
2017 ;; Tests -
|
|
2018 ;; (and (string-match *lisp-def-regexp* "\n(defun foo") (match-end 0)) -> 8
|
|
2019 ;; (and (string-match *lisp-def-regexp* "\n(si:def foo") (match-end 0)) -> 9
|
|
2020 ;; (and (string-match *lisp-def-regexp* "\n(def-bar foo")(match-end 0)) -> 10
|
|
2021 ;; (and (string-match *lisp-def-regexp* "\n(defun (foo") (match-end 0)) -> 9
|
0
|
2022
|
2
|
2023 ;; Parses all the definition names from a Lisp mode buffer and adds them to
|
|
2024 ;; the completion database.
|
0
|
2025 (defun add-completions-from-lisp-buffer ()
|
2
|
2026 ;; Benchmarks
|
|
2027 ;; Sun-3/280 - 1500 to 3000 lines of lisp code per second
|
0
|
2028 (let (string)
|
|
2029 (save-excursion
|
|
2030 (goto-char (point-min))
|
|
2031 (condition-case e
|
|
2032 (while t
|
|
2033 (re-search-forward *lisp-def-regexp*)
|
|
2034 (and (setq string (symbol-under-point))
|
|
2035 (add-completion-to-tail-if-new string))
|
|
2036 )
|
|
2037 (search-failed)
|
|
2038 ))))
|
|
2039
|
|
2040
|
70
|
2041 ;;;-----------------------------------------------
|
|
2042 ;;; C file completion parsing
|
|
2043 ;;;-----------------------------------------------
|
|
2044 ;;; C :
|
|
2045 ;;; Looks for #define or [<storage class>] [<type>] <name>{,<name>}
|
|
2046 ;;; or structure, array or pointer defs.
|
|
2047 ;;; It gets most of the definition names.
|
|
2048 ;;;
|
|
2049 ;;; As you might suspect by now, we use some symbol table hackery
|
|
2050 ;;;
|
|
2051 ;;; Symbol separator chars (have whitespace syntax) --> , ; * = (
|
|
2052 ;;; Opening char --> [ {
|
|
2053 ;;; Closing char --> ] }
|
|
2054 ;;; opening and closing must be skipped over
|
|
2055 ;;; Whitespace chars (have symbol syntax)
|
|
2056 ;;; Everything else has word syntax
|
0
|
2057
|
|
2058 (defun cmpl-make-c-def-completion-syntax-table ()
|
70
|
2059 (let ((table (make-syntax-table))
|
0
|
2060 (whitespace-chars '(? ?\n ?\t ?\f ?\v ?\r))
|
|
2061 ;; unfortunately the ?( causes the parens to appear unbalanced
|
|
2062 (separator-chars '(?, ?* ?= ?\( ?\;
|
|
2063 ))
|
|
2064 i)
|
|
2065 ;; default syntax is whitespace
|
|
2066 (setq i 0)
|
|
2067 (while (< i 256)
|
|
2068 (modify-syntax-entry i "w" table)
|
|
2069 (setq i (1+ i)))
|
|
2070 (completion-dolist (char whitespace-chars)
|
|
2071 (modify-syntax-entry char "_" table))
|
|
2072 (completion-dolist (char separator-chars)
|
|
2073 (modify-syntax-entry char " " table))
|
|
2074 (modify-syntax-entry ?\[ "(]" table)
|
|
2075 (modify-syntax-entry ?\{ "(}" table)
|
|
2076 (modify-syntax-entry ?\] ")[" table)
|
|
2077 (modify-syntax-entry ?\} "){" table)
|
|
2078 table))
|
|
2079
|
|
2080 (defconst cmpl-c-def-syntax-table (cmpl-make-c-def-completion-syntax-table))
|
|
2081
|
2
|
2082 ;; Regexps
|
0
|
2083 (defconst *c-def-regexp*
|
|
2084 ;; This stops on lines with possible definitions
|
|
2085 "\n[_a-zA-Z#]"
|
|
2086 ;; This stops after the symbol to add.
|
|
2087 ;;"\n\\(#define\\s +.\\|\\(\\(\\w\\|\\s_\\)+\\b\\s *\\)+[(;,[*{=]\\)"
|
|
2088 ;; This stops before the symbol to add. {Test cases in parens. below}
|
|
2089 ;;"\n\\(\\(\\w\\|\\s_\\)+\\s *(\\|\\(\\(#define\\|auto\\|extern\\|register\\|static\\|int\\|long\\|short\\|unsigned\\|char\\|void\\|float\\|double\\|enum\\|struct\\|union\\|typedef\\)\\s +\\)+\\)"
|
|
2090 ;; this simple version picks up too much extraneous stuff
|
|
2091 ;; "\n\\(\\w\\|\\s_\\|#\\)\\B"
|
|
2092 "A regexp that searches for a definition form."
|
|
2093 )
|
|
2094 ;
|
|
2095 ;(defconst *c-cont-regexp*
|
|
2096 ; "\\(\\w\\|\\s_\\)+\\b\\s *\\({\\|\\(\\[[0-9\t ]*\\]\\s *\\)*,\\(*\\|\\s \\)*\\b\\)"
|
|
2097 ; "This regexp should be used in a looking-at to parse for lists of variables.")
|
|
2098 ;
|
|
2099 ;(defconst *c-struct-regexp*
|
|
2100 ; "\\(*\\|\\s \\)*\\b"
|
|
2101 ; "This regexp should be used to test whether a symbol follows a structure definition.")
|
|
2102
|
|
2103 ;(defun test-c-def-regexp (regexp string)
|
|
2104 ; (and (eq 0 (string-match regexp string)) (match-end 0))
|
|
2105 ; )
|
|
2106
|
2
|
2107 ;; Tests -
|
|
2108 ;; (test-c-def-regexp *c-def-regexp* "\n#define foo") -> 10 (9)
|
|
2109 ;; (test-c-def-regexp *c-def-regexp* "\nfoo (x, y) {") -> 6 (6)
|
|
2110 ;; (test-c-def-regexp *c-def-regexp* "\nint foo (x, y)") -> 10 (5)
|
|
2111 ;; (test-c-def-regexp *c-def-regexp* "\n int foo (x, y)") -> nil
|
|
2112 ;; (test-c-def-regexp *c-cont-regexp* "oo, bar") -> 4
|
|
2113 ;; (test-c-def-regexp *c-cont-regexp* "oo, *bar") -> 5
|
|
2114 ;; (test-c-def-regexp *c-cont-regexp* "a [5][6], bar") -> 10
|
|
2115 ;; (test-c-def-regexp *c-cont-regexp* "oo(x,y)") -> nil
|
|
2116 ;; (test-c-def-regexp *c-cont-regexp* "a [6] ,\t bar") -> 9
|
|
2117 ;; (test-c-def-regexp *c-cont-regexp* "oo {trout =1} my_carp;") -> 14
|
|
2118 ;; (test-c-def-regexp *c-cont-regexp* "truct_p complex foon") -> nil
|
0
|
2119
|
2
|
2120 ;; Parses all the definition names from a C mode buffer and adds them to the
|
|
2121 ;; completion database.
|
0
|
2122 (defun add-completions-from-c-buffer ()
|
|
2123 ;; Benchmark --
|
|
2124 ;; Sun 3/280-- 1250 lines/sec.
|
|
2125
|
|
2126 (let (string next-point char
|
|
2127 (saved-syntax (syntax-table))
|
|
2128 )
|
|
2129 (save-excursion
|
|
2130 (goto-char (point-min))
|
|
2131 (catch 'finish-add-completions
|
|
2132 (unwind-protect
|
|
2133 (while t
|
|
2134 ;; we loop here only when scan-sexps fails
|
|
2135 ;; (i.e. unbalance exps.)
|
|
2136 (set-syntax-table cmpl-c-def-syntax-table)
|
|
2137 (condition-case e
|
|
2138 (while t
|
|
2139 (re-search-forward *c-def-regexp*)
|
|
2140 (cond
|
|
2141 ((= (preceding-char) ?#)
|
|
2142 ;; preprocessor macro, see if it's one we handle
|
|
2143 (setq string (buffer-substring (point) (+ (point) 6)))
|
|
2144 (cond ((or (string-equal string "define")
|
|
2145 (string-equal string "ifdef ")
|
|
2146 )
|
|
2147 ;; skip forward over definition symbol
|
|
2148 ;; and add it to database
|
|
2149 (and (forward-word 2)
|
|
2150 (setq string (symbol-before-point))
|
|
2151 ;;(push string foo)
|
|
2152 (add-completion-to-tail-if-new string)
|
|
2153 ))))
|
|
2154 (t
|
|
2155 ;; C definition
|
|
2156 (setq next-point (point))
|
|
2157 (while (and
|
|
2158 next-point
|
|
2159 ;; scan to next separator char.
|
|
2160 (setq next-point (scan-sexps next-point 1))
|
|
2161 )
|
|
2162 ;; position the point on the word we want to add
|
|
2163 (goto-char next-point)
|
|
2164 (while (= (setq char (following-char)) ?*)
|
|
2165 ;; handle pointer ref
|
|
2166 ;; move to next separator char.
|
|
2167 (goto-char
|
|
2168 (setq next-point (scan-sexps (point) 1)))
|
|
2169 )
|
|
2170 (forward-word -1)
|
|
2171 ;; add to database
|
|
2172 (if (setq string (symbol-under-point))
|
|
2173 ;; (push string foo)
|
|
2174 (add-completion-to-tail-if-new string)
|
|
2175 ;; Local TMC hack (useful for parsing paris.h)
|
|
2176 (if (and (looking-at "_AP") ;; "ansi prototype"
|
|
2177 (progn
|
|
2178 (forward-word -1)
|
|
2179 (setq string
|
|
2180 (symbol-under-point))
|
|
2181 ))
|
|
2182 (add-completion-to-tail-if-new string)
|
|
2183 )
|
|
2184 )
|
|
2185 ;; go to next
|
|
2186 (goto-char next-point)
|
|
2187 ;; (push (format "%c" (following-char)) foo)
|
|
2188 (if (= (char-syntax char) ?\()
|
|
2189 ;; if on an opening delimiter, go to end
|
|
2190 (while (= (char-syntax char) ?\()
|
|
2191 (setq next-point (scan-sexps next-point 1)
|
|
2192 char (char-after next-point))
|
|
2193 )
|
|
2194 (or (= char ?,)
|
|
2195 ;; Current char is an end char.
|
|
2196 (setq next-point nil)
|
|
2197 ))
|
|
2198 ))))
|
|
2199 (search-failed ;;done
|
|
2200 (throw 'finish-add-completions t)
|
|
2201 )
|
|
2202 (error
|
|
2203 ;; Check for failure in scan-sexps
|
|
2204 (if (or (string-equal (nth 1 e)
|
|
2205 "Containing expression ends prematurely")
|
|
2206 (string-equal (nth 1 e) "Unbalanced parentheses"))
|
|
2207 ;; unbalanced paren., keep going
|
|
2208 ;;(ding)
|
|
2209 (forward-line 1)
|
|
2210 (message "Error parsing C buffer for completions--please send bug report")
|
|
2211 (throw 'finish-add-completions t)
|
|
2212 ))
|
|
2213 ))
|
|
2214 (set-syntax-table saved-syntax)
|
|
2215 )))))
|
|
2216
|
|
2217
|
2
|
2218 ;;---------------------------------------------------------------------------
|
|
2219 ;; Init files
|
|
2220 ;;---------------------------------------------------------------------------
|
0
|
2221
|
2
|
2222 ;; The version of save-completions-to-file called at kill-emacs time.
|
0
|
2223 (defun kill-emacs-save-completions ()
|
|
2224 (if (and save-completions-flag enable-completion cmpl-initialized-p)
|
|
2225 (cond
|
|
2226 ((not cmpl-completions-accepted-p)
|
|
2227 (message "Completions database has not changed - not writing."))
|
|
2228 (t
|
|
2229 (save-completions-to-file)))))
|
|
2230
|
|
2231 ;; There is no point bothering to change this again
|
|
2232 ;; unless the package changes so much that it matters
|
|
2233 ;; for people that have saved completions.
|
|
2234 (defconst completion-version "11")
|
|
2235
|
|
2236 (defconst saved-cmpl-file-header
|
|
2237 ";;; Completion Initialization file.
|
2
|
2238 ;; Version = %s
|
|
2239 ;; Format is (<string> . <last-use-time>)
|
|
2240 ;; <string> is the completion
|
|
2241 ;; <last-use-time> is the time the completion was last used
|
|
2242 ;; If it is t, the completion will never be pruned from the file.
|
|
2243 ;; Otherwise it is in hours since origin.
|
0
|
2244 \n")
|
|
2245
|
|
2246 (defun completion-backup-filename (filename)
|
|
2247 (concat filename ".BAK"))
|
|
2248
|
|
2249 (defun save-completions-to-file (&optional filename)
|
|
2250 "Save completions in init file FILENAME.
|
|
2251 If file name is not specified, use `save-completions-file-name'."
|
|
2252 (interactive)
|
|
2253 (setq filename (expand-file-name (or filename save-completions-file-name)))
|
|
2254 (if (file-writable-p filename)
|
|
2255 (progn
|
|
2256 (if (not cmpl-initialized-p)
|
|
2257 (initialize-completions));; make sure everything's loaded
|
|
2258 (message "Saving completions to file %s" filename)
|
|
2259
|
|
2260 (let* ((delete-old-versions t)
|
|
2261 (kept-old-versions 0)
|
|
2262 (kept-new-versions completions-file-versions-kept)
|
|
2263 last-use-time
|
|
2264 (current-time (cmpl-hours-since-origin))
|
|
2265 (total-in-db 0)
|
|
2266 (total-perm 0)
|
|
2267 (total-saved 0)
|
|
2268 (backup-filename (completion-backup-filename filename))
|
|
2269 )
|
|
2270
|
|
2271 (save-excursion
|
|
2272 (get-buffer-create " *completion-save-buffer*")
|
|
2273 (set-buffer " *completion-save-buffer*")
|
|
2274 (setq buffer-file-name filename)
|
|
2275
|
|
2276 (if (not (verify-visited-file-modtime (current-buffer)))
|
|
2277 (progn
|
|
2278 ;; file has changed on disk. Bring us up-to-date
|
|
2279 (message "Completion file has changed. Merging. . .")
|
|
2280 (load-completions-from-file filename t)
|
|
2281 (message "Merging finished. Saving completions to file %s" filename)))
|
|
2282
|
|
2283 ;; prepare the buffer to be modified
|
|
2284 (clear-visited-file-modtime)
|
|
2285 (erase-buffer)
|
|
2286 ;; (/ 1 0)
|
|
2287 (insert (format saved-cmpl-file-header completion-version))
|
|
2288 (completion-dolist (completion (list-all-completions))
|
|
2289 (setq total-in-db (1+ total-in-db))
|
|
2290 (setq last-use-time (completion-last-use-time completion))
|
|
2291 ;; Update num uses and maybe write completion to a file
|
|
2292 (cond ((or;; Write to file if
|
|
2293 ;; permanent
|
|
2294 (and (eq last-use-time t)
|
|
2295 (setq total-perm (1+ total-perm)))
|
|
2296 ;; or if
|
|
2297 (if (> (completion-num-uses completion) 0)
|
|
2298 ;; it's been used
|
|
2299 (setq last-use-time current-time)
|
|
2300 ;; or it was saved before and
|
|
2301 (and last-use-time
|
|
2302 ;; save-completions-retention-time is nil
|
|
2303 (or (not save-completions-retention-time)
|
|
2304 ;; or time since last use is < ...retention-time*
|
|
2305 (< (- current-time last-use-time)
|
|
2306 save-completions-retention-time))
|
|
2307 )))
|
|
2308 ;; write to file
|
|
2309 (setq total-saved (1+ total-saved))
|
|
2310 (insert (prin1-to-string (cons (completion-string completion)
|
|
2311 last-use-time)) "\n")
|
|
2312 )))
|
|
2313
|
|
2314 ;; write the buffer
|
|
2315 (condition-case e
|
|
2316 (let ((file-exists-p (file-exists-p filename)))
|
|
2317 (if file-exists-p
|
|
2318 (progn
|
|
2319 ;; If file exists . . .
|
|
2320 ;; Save a backup(so GNU doesn't screw us when we're out of disk)
|
|
2321 ;; (GNU leaves a 0 length file if it gets a disk full error!)
|
|
2322
|
|
2323 ;; If backup doesn't exit, Rename current to backup
|
|
2324 ;; {If backup exists the primary file is probably messed up}
|
|
2325 (or (file-exists-p backup-filename)
|
|
2326 (rename-file filename backup-filename))
|
|
2327 ;; Copy the backup back to the current name
|
|
2328 ;; (so versioning works)
|
|
2329 (copy-file backup-filename filename t)))
|
|
2330 ;; Save it
|
|
2331 (save-buffer)
|
|
2332 (if file-exists-p
|
|
2333 ;; If successful, remove backup
|
|
2334 (delete-file backup-filename)))
|
|
2335 (error
|
|
2336 (set-buffer-modified-p nil)
|
|
2337 (message "Couldn't save completion file `%s'" filename)
|
|
2338 ))
|
|
2339 ;; Reset accepted-p flag
|
|
2340 (setq cmpl-completions-accepted-p nil)
|
|
2341 )
|
|
2342 (cmpl-statistics-block
|
|
2343 (record-save-completions total-in-db total-perm total-saved))
|
|
2344 ))))
|
|
2345
|
2
|
2346 ;;(defun autosave-completions ()
|
|
2347 ;; (if (and save-completions-flag enable-completion cmpl-initialized-p
|
|
2348 ;; *completion-auto-save-period*
|
|
2349 ;; (> cmpl-emacs-idle-time *completion-auto-save-period*)
|
|
2350 ;; cmpl-completions-accepted-p)
|
|
2351 ;; (save-completions-to-file)))
|
0
|
2352
|
2
|
2353 ;;(add-hook 'cmpl-emacs-idle-time-hooks 'autosave-completions)
|
0
|
2354
|
|
2355 (defun load-completions-from-file (&optional filename no-message-p)
|
|
2356 "Loads a completion init file FILENAME.
|
|
2357 If file is not specified, then use `save-completions-file-name'."
|
|
2358 (interactive)
|
|
2359 (setq filename (expand-file-name (or filename save-completions-file-name)))
|
|
2360 (let* ((backup-filename (completion-backup-filename filename))
|
|
2361 (backup-readable-p (file-readable-p backup-filename))
|
|
2362 )
|
|
2363 (if backup-readable-p (setq filename backup-filename))
|
|
2364 (if (file-readable-p filename)
|
|
2365 (progn
|
|
2366 (if (not no-message-p)
|
|
2367 (message "Loading completions from %sfile %s . . ."
|
|
2368 (if backup-readable-p "backup " "") filename))
|
|
2369 (save-excursion
|
|
2370 (get-buffer-create " *completion-save-buffer*")
|
|
2371 (set-buffer " *completion-save-buffer*")
|
|
2372 (setq buffer-file-name filename)
|
|
2373 ;; prepare the buffer to be modified
|
|
2374 (clear-visited-file-modtime)
|
|
2375 (erase-buffer)
|
|
2376
|
|
2377 (let ((insert-okay-p nil)
|
|
2378 (buffer (current-buffer))
|
|
2379 (current-time (cmpl-hours-since-origin))
|
|
2380 string num-uses entry last-use-time
|
|
2381 cmpl-entry cmpl-last-use-time
|
|
2382 (current-completion-source cmpl-source-init-file)
|
|
2383 (start-num
|
|
2384 (cmpl-statistics-block
|
|
2385 (aref completion-add-count-vector cmpl-source-file-parsing)))
|
|
2386 (total-in-file 0) (total-perm 0)
|
|
2387 )
|
|
2388 ;; insert the file into a buffer
|
|
2389 (condition-case e
|
|
2390 (progn (insert-file-contents filename t)
|
|
2391 (setq insert-okay-p t))
|
|
2392
|
|
2393 (file-error
|
|
2394 (message "File error trying to load completion file %s."
|
|
2395 filename)))
|
|
2396 ;; parse it
|
|
2397 (if insert-okay-p
|
|
2398 (progn
|
|
2399 (goto-char (point-min))
|
|
2400
|
|
2401 (condition-case e
|
|
2402 (while t
|
|
2403 (setq entry (read buffer))
|
|
2404 (setq total-in-file (1+ total-in-file))
|
|
2405 (cond
|
|
2406 ((and (consp entry)
|
|
2407 (stringp (setq string (car entry)))
|
|
2408 (cond
|
|
2409 ((eq (setq last-use-time (cdr entry)) 'T)
|
|
2410 ;; handle case sensitivity
|
|
2411 (setq total-perm (1+ total-perm))
|
|
2412 (setq last-use-time t))
|
|
2413 ((eq last-use-time t)
|
|
2414 (setq total-perm (1+ total-perm)))
|
|
2415 ((integerp last-use-time))
|
|
2416 ))
|
|
2417 ;; Valid entry
|
|
2418 ;; add it in
|
|
2419 (setq cmpl-last-use-time
|
|
2420 (completion-last-use-time
|
|
2421 (setq cmpl-entry
|
|
2422 (add-completion-to-tail-if-new string))
|
|
2423 ))
|
|
2424 (if (or (eq last-use-time t)
|
|
2425 (and (> last-use-time 1000);;backcompatibility
|
|
2426 (not (eq cmpl-last-use-time t))
|
|
2427 (or (not cmpl-last-use-time)
|
|
2428 ;; more recent
|
|
2429 (> last-use-time cmpl-last-use-time))
|
|
2430 ))
|
|
2431 ;; update last-use-time
|
|
2432 (set-completion-last-use-time cmpl-entry last-use-time)
|
|
2433 ))
|
|
2434 (t
|
|
2435 ;; Bad format
|
|
2436 (message "Error: invalid saved completion - %s"
|
|
2437 (prin1-to-string entry))
|
|
2438 ;; try to get back in sync
|
|
2439 (search-forward "\n(")
|
|
2440 )))
|
|
2441 (search-failed
|
|
2442 (message "End of file while reading completions.")
|
|
2443 )
|
|
2444 (end-of-file
|
|
2445 (if (= (point) (point-max))
|
|
2446 (if (not no-message-p)
|
|
2447 (message "Loading completions from file %s . . . Done."
|
|
2448 filename))
|
|
2449 (message "End of file while reading completions.")
|
|
2450 ))
|
|
2451 )))
|
|
2452
|
|
2453 (cmpl-statistics-block
|
|
2454 (record-load-completions
|
|
2455 total-in-file total-perm
|
|
2456 (- (aref completion-add-count-vector cmpl-source-init-file)
|
|
2457 start-num)))
|
|
2458
|
|
2459 ))))))
|
|
2460
|
|
2461 (defun initialize-completions ()
|
|
2462 "Load the default completions file.
|
|
2463 Also sets up so that exiting emacs will automatically save the file."
|
|
2464 (interactive)
|
|
2465 (cond ((not cmpl-initialized-p)
|
|
2466 (load-completions-from-file)
|
|
2467 ))
|
|
2468 (setq cmpl-initialized-p t)
|
|
2469 )
|
|
2470
|
|
2471
|
2
|
2472 ;;-----------------------------------------------
|
|
2473 ;; Kill EMACS patch
|
|
2474 ;;-----------------------------------------------
|
0
|
2475
|
|
2476 (add-hook 'kill-emacs-hook
|
|
2477 '(lambda ()
|
|
2478 (kill-emacs-save-completions)
|
|
2479 (cmpl-statistics-block
|
|
2480 (record-cmpl-kill-emacs))))
|
|
2481
|
2
|
2482 ;;-----------------------------------------------
|
|
2483 ;; Kill region patch
|
|
2484 ;;-----------------------------------------------
|
0
|
2485
|
|
2486 (defun completion-kill-region (&optional beg end)
|
|
2487 "Kill between point and mark.
|
|
2488 The text is deleted but saved in the kill ring.
|
|
2489 The command \\[yank] can retrieve it from there.
|
|
2490 /(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
|
|
2491
|
|
2492 This is the primitive for programs to kill text (as opposed to deleting it).
|
|
2493 Supply two arguments, character numbers indicating the stretch of text
|
|
2494 to be killed.
|
|
2495 Any command that calls this function is a \"kill command\".
|
|
2496 If the previous command was also a kill command,
|
|
2497 the text killed this time appends to the text killed last time
|
|
2498 to make one entry in the kill ring.
|
|
2499 Patched to remove the most recent completion."
|
|
2500 (interactive "r")
|
|
2501 (cond ((eq last-command 'complete)
|
|
2502 (delete-region (point) cmpl-last-insert-location)
|
|
2503 (insert cmpl-original-string)
|
|
2504 (setq completion-to-accept nil)
|
|
2505 (cmpl-statistics-block
|
|
2506 (record-complete-failed)))
|
|
2507 (t
|
|
2508 (kill-region beg end))))
|
|
2509
|
|
2510 (global-set-key "\C-w" 'completion-kill-region)
|
|
2511
|
2
|
2512 ;;-----------------------------------------------
|
|
2513 ;; Patches to self-insert-command.
|
|
2514 ;;-----------------------------------------------
|
0
|
2515
|
2
|
2516 ;; Need 2 versions: generic separator chars. and space (to get auto fill
|
|
2517 ;; to work)
|
0
|
2518
|
2
|
2519 ;; All common separators (eg. space "(" ")" """) characters go through a
|
|
2520 ;; function to add new words to the list of words to complete from:
|
|
2521 ;; COMPLETION-SEPARATOR-SELF-INSERT-COMMAND (arg).
|
|
2522 ;; If the character before this was an alpha-numeric then this adds the
|
|
2523 ;; symbol before point to the completion list (using ADD-COMPLETION).
|
0
|
2524
|
|
2525 (defun completion-separator-self-insert-command (arg)
|
|
2526 (interactive "p")
|
|
2527 (use-completion-before-separator)
|
|
2528 (self-insert-command arg)
|
|
2529 )
|
|
2530
|
|
2531 (defun completion-separator-self-insert-autofilling (arg)
|
|
2532 (interactive "p")
|
|
2533 (use-completion-before-separator)
|
|
2534 (self-insert-command arg)
|
|
2535 (and auto-fill-function
|
|
2536 (funcall auto-fill-function))
|
|
2537 )
|
|
2538
|
2
|
2539 ;;-----------------------------------------------
|
|
2540 ;; Wrapping Macro
|
|
2541 ;;-----------------------------------------------
|
0
|
2542
|
2
|
2543 ;; Note that because of the way byte compiling works, none of
|
|
2544 ;; the functions defined with this macro get byte compiled.
|
0
|
2545
|
|
2546 (defmacro def-completion-wrapper (function-name type &optional new-name)
|
|
2547 "Add a call to update the completion database before function execution.
|
|
2548 TYPE is the type of the wrapper to be added. Can be :before or :under."
|
|
2549 (cond ((eq type ':separator)
|
|
2550 (list 'put (list 'quote function-name) ''completion-function
|
|
2551 ''use-completion-before-separator))
|
|
2552 ((eq type ':before)
|
|
2553 (list 'put (list 'quote function-name) ''completion-function
|
|
2554 ''use-completion-before-point))
|
|
2555 ((eq type ':backward-under)
|
|
2556 (list 'put (list 'quote function-name) ''completion-function
|
|
2557 ''use-completion-backward-under))
|
|
2558 ((eq type ':backward)
|
|
2559 (list 'put (list 'quote function-name) ''completion-function
|
|
2560 ''use-completion-backward))
|
|
2561 ((eq type ':under)
|
|
2562 (list 'put (list 'quote function-name) ''completion-function
|
|
2563 ''use-completion-under-point))
|
|
2564 ((eq type ':under-or-before)
|
|
2565 (list 'put (list 'quote function-name) ''completion-function
|
|
2566 ''use-completion-under-or-before-point))
|
|
2567 ((eq type ':minibuffer-separator)
|
|
2568 (list 'put (list 'quote function-name) ''completion-function
|
|
2569 ''use-completion-minibuffer-separator))))
|
|
2570
|
|
2571 (defun use-completion-minibuffer-separator ()
|
|
2572 (let ((cmpl-syntax-table cmpl-standard-syntax-table))
|
|
2573 (use-completion-before-separator)))
|
|
2574
|
|
2575 (defun use-completion-backward-under ()
|
|
2576 (use-completion-under-point)
|
|
2577 (if (eq last-command 'complete)
|
|
2578 ;; probably a failed completion if you have to back up
|
|
2579 (cmpl-statistics-block (record-complete-failed))))
|
|
2580
|
|
2581 (defun use-completion-backward ()
|
|
2582 (if (eq last-command 'complete)
|
|
2583 ;; probably a failed completion if you have to back up
|
|
2584 (cmpl-statistics-block (record-complete-failed))))
|
|
2585
|
|
2586 (defun completion-before-command ()
|
|
2587 (funcall (or (and (symbolp this-command)
|
|
2588 (get this-command 'completion-function))
|
|
2589 'use-completion-under-or-before-point)))
|
|
2590 (add-hook 'pre-command-hook 'completion-before-command)
|
|
2591
|
|
2592
|
2
|
2593 ;;---------------------------------------------------------------------------
|
|
2594 ;; Patches to standard keymaps insert completions
|
|
2595 ;;---------------------------------------------------------------------------
|
0
|
2596
|
2
|
2597 ;;-----------------------------------------------
|
|
2598 ;; Separators
|
|
2599 ;;-----------------------------------------------
|
|
2600 ;; We've used the completion syntax table given as a guide.
|
|
2601 ;;
|
|
2602 ;; Global separator chars.
|
|
2603 ;; We left out <tab> because there are too many special cases for it. Also,
|
|
2604 ;; in normal coding it's rarely typed after a word.
|
0
|
2605 (global-set-key " " 'completion-separator-self-insert-autofilling)
|
|
2606 (global-set-key "!" 'completion-separator-self-insert-command)
|
|
2607 (global-set-key "%" 'completion-separator-self-insert-command)
|
|
2608 (global-set-key "^" 'completion-separator-self-insert-command)
|
|
2609 (global-set-key "&" 'completion-separator-self-insert-command)
|
|
2610 (global-set-key "(" 'completion-separator-self-insert-command)
|
|
2611 (global-set-key ")" 'completion-separator-self-insert-command)
|
|
2612 (global-set-key "=" 'completion-separator-self-insert-command)
|
|
2613 (global-set-key "`" 'completion-separator-self-insert-command)
|
|
2614 (global-set-key "|" 'completion-separator-self-insert-command)
|
|
2615 (global-set-key "{" 'completion-separator-self-insert-command)
|
|
2616 (global-set-key "}" 'completion-separator-self-insert-command)
|
|
2617 (global-set-key "[" 'completion-separator-self-insert-command)
|
|
2618 (global-set-key "]" 'completion-separator-self-insert-command)
|
|
2619 (global-set-key ";" 'completion-separator-self-insert-command)
|
|
2620 (global-set-key "\"" 'completion-separator-self-insert-command)
|
|
2621 (global-set-key "'" 'completion-separator-self-insert-command)
|
|
2622 (global-set-key "#" 'completion-separator-self-insert-command)
|
|
2623 (global-set-key "," 'completion-separator-self-insert-command)
|
|
2624 (global-set-key "?" 'completion-separator-self-insert-command)
|
|
2625
|
2
|
2626 ;; We include period and colon even though they are symbol chars because :
|
|
2627 ;; - in text we want to pick up the last word in a sentence.
|
|
2628 ;; - in C pointer refs. we want to pick up the first symbol
|
|
2629 ;; - it won't make a difference for lisp mode (package names are short)
|
0
|
2630 (global-set-key "." 'completion-separator-self-insert-command)
|
|
2631 (global-set-key ":" 'completion-separator-self-insert-command)
|
|
2632
|
2
|
2633 ;; Lisp Mode diffs
|
0
|
2634 (define-key lisp-mode-map "!" 'self-insert-command)
|
|
2635 (define-key lisp-mode-map "&" 'self-insert-command)
|
|
2636 (define-key lisp-mode-map "%" 'self-insert-command)
|
|
2637 (define-key lisp-mode-map "?" 'self-insert-command)
|
|
2638 (define-key lisp-mode-map "=" 'self-insert-command)
|
|
2639 (define-key lisp-mode-map "^" 'self-insert-command)
|
|
2640
|
2
|
2641 ;; Avoid warnings.
|
|
2642 (defvar c-mode-map)
|
|
2643 (defvar fortran-mode-map)
|
|
2644
|
|
2645 ;; C mode diffs.
|
0
|
2646 (defun completion-c-mode-hook ()
|
|
2647 (def-completion-wrapper electric-c-semi :separator)
|
|
2648 (define-key c-mode-map "+" 'completion-separator-self-insert-command)
|
|
2649 (define-key c-mode-map "*" 'completion-separator-self-insert-command)
|
|
2650 (define-key c-mode-map "/" 'completion-separator-self-insert-command))
|
|
2651 ;; Do this either now or whenever C mode is loaded.
|
|
2652 (if (featurep 'cc-mode)
|
|
2653 (completion-c-mode-hook)
|
|
2654 (add-hook 'c-mode-hook 'completion-c-mode-hook))
|
|
2655
|
2
|
2656 ;; FORTRAN mode diffs. (these are defined when fortran is called)
|
0
|
2657 (defun completion-setup-fortran-mode ()
|
|
2658 (define-key fortran-mode-map "+" 'completion-separator-self-insert-command)
|
|
2659 (define-key fortran-mode-map "-" 'completion-separator-self-insert-command)
|
|
2660 (define-key fortran-mode-map "*" 'completion-separator-self-insert-command)
|
|
2661 (define-key fortran-mode-map "/" 'completion-separator-self-insert-command)
|
|
2662 )
|
|
2663
|
2
|
2664 ;;-----------------------------------------------
|
|
2665 ;; End of line chars.
|
|
2666 ;;-----------------------------------------------
|
0
|
2667 (def-completion-wrapper newline :separator)
|
|
2668 (def-completion-wrapper newline-and-indent :separator)
|
|
2669 (def-completion-wrapper comint-send-input :separator)
|
|
2670 (def-completion-wrapper exit-minibuffer :minibuffer-separator)
|
|
2671 (def-completion-wrapper eval-print-last-sexp :separator)
|
|
2672 (def-completion-wrapper eval-last-sexp :separator)
|
|
2673 ;;(def-completion-wrapper minibuffer-complete-and-exit :minibuffer)
|
|
2674
|
2
|
2675 ;;-----------------------------------------------
|
|
2676 ;; Cursor movement
|
|
2677 ;;-----------------------------------------------
|
0
|
2678
|
|
2679 (def-completion-wrapper next-line :under-or-before)
|
|
2680 (def-completion-wrapper previous-line :under-or-before)
|
|
2681 (def-completion-wrapper beginning-of-buffer :under-or-before)
|
|
2682 (def-completion-wrapper end-of-buffer :under-or-before)
|
|
2683 (def-completion-wrapper beginning-of-line :under-or-before)
|
|
2684 (def-completion-wrapper end-of-line :under-or-before)
|
|
2685 (def-completion-wrapper forward-char :under-or-before)
|
|
2686 (def-completion-wrapper forward-word :under-or-before)
|
|
2687 (def-completion-wrapper forward-sexp :under-or-before)
|
|
2688 (def-completion-wrapper backward-char :backward-under)
|
|
2689 (def-completion-wrapper backward-word :backward-under)
|
|
2690 (def-completion-wrapper backward-sexp :backward-under)
|
|
2691
|
|
2692 (def-completion-wrapper delete-backward-char :backward)
|
|
2693 (def-completion-wrapper delete-backward-char-untabify :backward)
|
|
2694
|
2
|
2695 ;; Tests --
|
|
2696 ;; foobarbiz
|
|
2697 ;; foobar
|
|
2698 ;; fooquux
|
|
2699 ;; fooper
|
0
|
2700
|
|
2701 (cmpl-statistics-block
|
|
2702 (record-completion-file-loaded))
|
|
2703
|
2
|
2704 (provide 'completion)
|
|
2705
|
0
|
2706 ;;; completion.el ends here
|