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