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