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