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