completion.el 93.3 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1
;;; completion.el --- dynamic word-completion code
Erik Naggum's avatar
Erik Naggum committed
2

Karl Heuer's avatar
Karl Heuer committed
3
;; Copyright (C) 1990, 1993, 1995 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

Richard M. Stallman's avatar
Richard M. Stallman committed
5
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Keywords: abbrev
7
;; Author: Jim Salem <alem@bbnplanet.com> of Thinking Machines Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
8
;;  (ideas suggested by Brewster Kahle)
Eric S. Raymond's avatar
Eric S. Raymond committed
9

10
;; This file is part of GNU Emacs.
Eric S. Raymond's avatar
Eric S. Raymond committed
11

12 13 14 15 16 17 18 19 20 21 22
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
23 24 25
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
26 27

;;; Commentary:
Erik Naggum's avatar
Erik Naggum committed
28 29 30 31 32

;; What to put in .emacs
;;-----------------------
;; (load "completion")
;; (initialize-completions)
Richard M. Stallman's avatar
Richard M. Stallman committed
33

Erik Naggum's avatar
Erik Naggum committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
;;---------------------------------------------------------------------------
;; Documentation [Slightly out of date]
;;---------------------------------------------------------------------------
;;  (also check the documentation string of the functions)
;;
;; Introduction
;;---------------
;;  
;;     After you type a few characters, pressing the "complete" key inserts
;; the rest of the word you are likely to type.  
;;
;; This watches all the words that you type and remembers them.  When 
;; typing a new word, pressing "complete" (meta-return) "completes" the
;; word by inserting the most recently used word that begins with the 
;; same characters.  If you press meta-return repeatedly, it cycles
;; through all the words it knows about.
;;
;;  If you like the completion then just continue typing, it is as if you
;; entered the text by hand.  If you want the inserted extra characters 
;; to go away, type control-w or delete.  More options are described below.
;;
;;  The guesses are made in the order of the most recently "used".  Typing
;; in a word and then typing a separator character (such as a space) "uses" 
;; the word.  So does moving a cursor over the word.  If no words are found, 
;; it uses an extended version of the dabbrev style completion.
;;
;;   You automatically save the completions you use to a file between 
;; sessions.  
;;
;;   Completion enables programmers to enter longer, more descriptive 
;; variable names while typing fewer keystrokes than they normally would.
;;
;;
;; Full documentation
;;---------------------
;;
;;   A "word" is any string containing characters with either word or symbol 
;; syntax.  [E.G. Any alphanumeric string with hyphens, underscores, etc.]
;; Unless you change the constants, you must type at least three characters
;; for the word to be recognized.  Only words longer than 6 characters are
;; saved.
;;
;;   When you load this file, completion will be on.  I suggest you use the
;; compiled version (because it is noticeably faster).
;;
;;  M-X completion-mode toggles whether or not new words are added to the
;; database by changing the value of enable-completion.
;;
;;  SAVING/LOADING COMPLETIONS
;;   Completions are automatically saved from one session to another
;; (unless save-completions-flag or enable-completion is nil).
;; Loading this file (or calling initialize-completions) causes EMACS
;; to load a completions database for a saved completions file 
;; (default: ~/.completions).  When you exit, EMACS saves a copy of the
;; completions that you 
;; often use.  When you next start, EMACS loads in the saved completion file.
;;
;;   The number of completions saved depends loosely on 
;; *saved-completions-decay-factor*.  Completions that have never been 
;; inserted via "complete" are not saved.  You are encouraged to experiment
;; with different functions (see compute-completion-min-num-uses).
;;
;;   Some completions are permanent and are always saved out.  These 
;; completions have their num-uses slot set to T.  Use 
;; add-permanent-completion to do this
;;
;;   Completions are saved only if enable-completion is T.  The number of old
;; versions kept of the saved completions file is controlled by 
;; completions-file-versions-kept.
;;
;; COMPLETE KEY OPTIONS
;;   The complete function takes a numeric arguments.  
;;  control-u :: leave the point at the beginning of the completion rather 
;;               than the middle.
;;  a number  :: rotate through the possible completions by that amount
;;  `-'       :: same as -1 (insert previous completion)
;;
;; HOW THE DATABASE IS MAINTAINED
;;  <write>
;;
;; UPDATING THE DATABASE MANUALLY
;;   m-x kill-completion 
;;     kills the completion at point.
;;   m-x add-completion
;;   m-x add-permanent-completion
;;   
;; UPDATING THE DATABASE FROM A SOURCE CODE FILE
;;   m-x add-completions-from-buffer
;;     Parses all the definition names from a C or LISP mode buffer and
;;     adds them to the completion database.
;;
;;   m-x add-completions-from-lisp-file 
;;     Parses all the definition names from a C or Lisp mode file and
;;     adds them to the completion database.
;;
;; UPDATING THE DATABASE FROM A TAGS TABLE
;;   m-x add-completions-from-tags-table
;;     Adds completions from the current tags-table-buffer.
;;
;; HOW A COMPLETION IS FOUND
;;  <write>
;;
;; STRING CASING
;;   Completion is string case independent if case-fold-search has its 
;;  normal default of T.  Also when the completion is inserted the case of the
;;  entry is coerced appropriately.  
;;  [E.G.  APP --> APPROPRIATELY     app --> appropriately  
;;         App --> Appropriately]
;;
;; INITIALIZATION
;;  The form `(initialize-completions)' initializes the completion system by 
;; trying to load in the user's completions.  After the first cal, further 
;; calls have no effect so one should be careful not to put the form in a 
;; site's standard site-init file.
;;
;;---------------------------------------------------------------------------
;;
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
152

Erik Naggum's avatar
Erik Naggum committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
;;---------------------------------------------------------------------------
;; Functions you might like to call
;;---------------------------------------------------------------------------
;;
;;  add-completion  string &optional num-uses
;;    Adds a new string to the database
;;
;;  add-permanent-completion  string
;;    Adds a new string to the database with num-uses = T
;;

;;  kill-completion string
;;    Kills the completion from the database.
;;
;;  clear-all-completions
;;    Clears the database
;;
;;  list-all-completions
;;    Returns a list of all completions.
;;
;;
;;  next-completion string &optional index
;;    Returns a completion entry that starts with string.
;;
;;  find-exact-completion string
;;    Returns a completion entry that exactly matches string.
;;
;;  complete
;;    Inserts a completion at point
;;
;;  initialize-completions
;;    Loads the completions file and sets up so that exiting emacs will 
;;  save them.
;;
;;  save-completions-to-file &optional filename  
;;  load-completions-from-file &optional filename
;;
;;-----------------------------------------------
;; Other functions
;;-----------------------------------------------
;;
;;  get-completion-list string
;;
;; These things are for manipulating the structure
;;  make-completion string num-uses
;;  completion-num-uses completion 
;;  completion-string completion
;;  set-completion-num-uses completion num-uses
;;  set-completion-string completion string
;;  
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
204

Erik Naggum's avatar
Erik Naggum committed
205 206 207 208 209 210 211 212 213 214 215 216
;;-----------------------------------------------
;; To Do :: (anybody ?)
;;-----------------------------------------------
;;
;;   Implement Lookup and keyboard interface in C
;;   Add package prefix smarts (for Common Lisp)
;;   Add autoprompting of possible completions after every keystroke (fast
;;      terminals only !)
;;   Add doc. to texinfo
;;
;;
;;-----------------------------------------------
Erik Naggum's avatar
Erik Naggum committed
217
;;; Change Log:
Erik Naggum's avatar
Erik Naggum committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
;;-----------------------------------------------
;;    Sometime in '84 Brewster implemented a somewhat buggy version for 
;; Symbolics LISPMs.
;;    Jan. '85 Jim became enamored of the idea and implemented a faster, 
;; more robust version.
;;    With input from many users at TMC, (rose, craig, and gls come to mind),
;; the current style of interface was developed. 
;;    9/87, Jim and Brewster took terminals home.  Yuck.  After 
;; complaining for a while Brewster implemented a subset of the current 
;; LISPM version for GNU Emacs.  
;;    8/88  After complaining for a while (and with sufficient 
;; promised rewards), Jim reimplemented a version of GNU completion
;; superior to that of the LISPM version.
;;
;;-----------------------------------------------
;; Acknowledgements
;;-----------------------------------------------
;;  Cliff Lasser (cal@think.com), Kevin Herbert (kph@cisco.com),
;;  eero@media-lab, kgk@cs.brown.edu, jla@ai.mit.edu,
;;
;;-----------------------------------------------
;; Change Log
;;-----------------------------------------------
;; From version 9 to 10
;;  - Allowance for non-integral *completion-version* nos.
;;  - Fix cmpl-apply-as-top-level for keyboard macros
;;  - Fix broken completion merging (in save-completions-to-file)
;;  - More misc. fixes for version 19.0 of emacs
;;
;; From Version 8 to 9
;;  - Ported to version 19.0 of emacs (backcompatible with version 18)
;;  - Added add-completions-from-tags-table (with thanks to eero@media-lab)
;;
;; From Version 7 to 8
;;  - Misc. changes to comments
;;  - new completion key bindings: c-x o, M->, M-<, c-a, c-e
;;  - cdabbrev now checks all the visible window buffers and the "other buffer"
;;  - `%' is now a symbol character rather than a separator (except in C mode)
;;
;; From Version 6 to 7
;;  - Fixed bug with saving out .completion file the first time
;;
;; From Version 5 to 6
;;  - removed statistics recording
;;  - reworked advise to handle autoloads
;;  - Fixed fortran mode support
;;  - Added new cursor motion triggers
;;
;; From Version 4 to 5
;;  - doesn't bother saving if nothing has changed
;;  - auto-save if haven't used for a 1/2 hour
;;  - save period extended to two weeks
;;  - minor fix to capitalization code
;;  - added *completion-auto-save-period* to variables recorded.
;;  - added reenter protection to cmpl-record-statistics-filter
;;  - added backup protection to save-completions-to-file (prevents 
;;    problems with disk full errors)
Richard M. Stallman's avatar
Richard M. Stallman committed
275

Eric S. Raymond's avatar
Eric S. Raymond committed
276 277
;;; Code:

Erik Naggum's avatar
Erik Naggum committed
278 279 280
;;---------------------------------------------------------------------------
;; User changeable parameters
;;---------------------------------------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
281

282 283 284
(defvar enable-completion t
  "*Non-nil means enable recording and saving of completions.
If nil, no new words added to the database or saved to the init file.")
Richard M. Stallman's avatar
Richard M. Stallman committed
285

286 287 288
(defvar save-completions-flag t
  "*Non-nil means save most-used completions when exiting Emacs.
See also `saved-completions-retention-time'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
289

290
(defvar save-completions-file-name (convert-standard-filename "~/.completions")
Richard M. Stallman's avatar
Richard M. Stallman committed
291 292
  "*The filename to save completions to.")

293 294 295
(defvar save-completions-retention-time 336
  "*Discard a completion if unused for this many hours.
\(1 day = 24, 1 week = 168).  If this is 0, non-permanent completions
Jim Blandy's avatar
Jim Blandy committed
296
will not be saved unless these are used.  Default is two weeks.")
Richard M. Stallman's avatar
Richard M. Stallman committed
297

298 299 300
(defvar completion-on-separator-character nil
  "*Non-nil means separator characters mark previous word as used.
This means the word will be saved as a completion.")
Richard M. Stallman's avatar
Richard M. Stallman committed
301

302 303
(defvar completions-file-versions-kept kept-new-versions
  "*Number of versions to keep for the saved completions file.")
Richard M. Stallman's avatar
Richard M. Stallman committed
304

305 306
(defvar completion-prompt-speed-threshold 4800
  "*Minimum output speed at which to display next potential completion.")
Richard M. Stallman's avatar
Richard M. Stallman committed
307

308 309
(defvar completion-cdabbrev-prompt-flag nil
  "*If non-nil, the next completion prompt does a cdabbrev search.
Richard M. Stallman's avatar
Richard M. Stallman committed
310 311
This can be time consuming.")

312 313 314
(defvar completion-search-distance 15000
  "*How far to search in the buffer when looking for completions.
In number of characters.  If nil, search the whole buffer.")
Richard M. Stallman's avatar
Richard M. Stallman committed
315

316 317 318 319
(defvar completions-merging-modes '(lisp c)
  "*List of modes {`c' or `lisp'} for automatic completions merging.
Definitions from visited files which have these modes
are automatically added to the completion database.")
Richard M. Stallman's avatar
Richard M. Stallman committed
320

Erik Naggum's avatar
Erik Naggum committed
321 322
;;(defvar *record-cmpl-statistics-p* nil
;;  "*If non-nil, record completion statistics.")
Richard M. Stallman's avatar
Richard M. Stallman committed
323

Erik Naggum's avatar
Erik Naggum committed
324 325 326
;;(defvar *completion-auto-save-period* 1800
;;  "*The period in seconds to wait for emacs to be idle before autosaving
;;the completions.  Default is a 1/2 hour.")
Richard M. Stallman's avatar
Richard M. Stallman committed
327

328
(defconst completion-min-length nil ;; defined below in eval-when
Richard M. Stallman's avatar
Richard M. Stallman committed
329 330 331
  "*The minimum length of a stored completion.
DON'T CHANGE WITHOUT RECOMPILING !  This is used by macros.")

332
(defconst completion-max-length nil ;; defined below in eval-when
Richard M. Stallman's avatar
Richard M. Stallman committed
333 334 335
  "*The maximum length of a stored completion.
DON'T CHANGE WITHOUT RECOMPILING !  This is used by macros.")

336
(defconst completion-prefix-min-length nil ;; defined below in eval-when
Richard M. Stallman's avatar
Richard M. Stallman committed
337 338 339 340 341 342
  "The minimum length of a completion search string.
DON'T CHANGE WITHOUT RECOMPILING !  This is used by macros.")

(defmacro eval-when-compile-load-eval (&rest body)
  ;; eval everything before expanding
  (mapcar 'eval body)
Jim Blandy's avatar
Jim Blandy committed
343
  (cons 'progn body))
Richard M. Stallman's avatar
Richard M. Stallman committed
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
(eval-when-compile
  (defvar completion-gensym-counter 0)
  (defun completion-gensym (&optional arg)
    "Generate a new uninterned symbol.
The name is made by appending a number to PREFIX, default \"G\"."
    (let ((prefix (if (stringp arg) arg "G"))
	  (num (if (integerp arg) arg
		 (prog1 completion-gensym-counter
		   (setq completion-gensym-counter (1+ completion-gensym-counter))))))
      (make-symbol (format "%s%d" prefix num)))))

(defmacro completion-dolist (spec &rest body)
  "(completion-dolist (VAR LIST [RESULT]) BODY...): loop over a list.
Evaluate BODY with VAR bound to each `car' from LIST, in turn.
Then evaluate RESULT to get return value, default nil."
  (let ((temp (completion-gensym "--dolist-temp--")))
    (append (list 'let (list (list temp (nth 1 spec)) (car spec))
		  (append (list 'while temp
				(list 'setq (car spec) (list 'car temp)))
			  body (list (list 'setq temp
					   (list 'cdr temp)))))
	    (if (cdr (cdr spec))
		(cons (list 'setq (car spec) nil) (cdr (cdr spec)))
	      '(nil)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
370 371 372
(defun completion-eval-when ()
  (eval-when-compile-load-eval
   ;; These vars. are defined at both compile and load time.
373 374 375
   (setq completion-min-length 6)
   (setq completion-max-length 200)
   (setq completion-prefix-min-length 3)))
Richard M. Stallman's avatar
Richard M. Stallman committed
376 377 378

(completion-eval-when)
 
Erik Naggum's avatar
Erik Naggum committed
379 380 381
;;---------------------------------------------------------------------------
;; Internal Variables
;;---------------------------------------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
382 383

(defvar cmpl-initialized-p nil
384 385
  "Set to t when the completion system is initialized.
Indicates that the old completion file has been read in.")
Richard M. Stallman's avatar
Richard M. Stallman committed
386 387

(defvar cmpl-completions-accepted-p nil
388 389
  "Set to t as soon as the first completion has been accepted.
Used to decide whether to save completions.")
Richard M. Stallman's avatar
Richard M. Stallman committed
390

391
(defvar cmpl-preceding-syntax)
392 393

(defvar completion-string)
Richard M. Stallman's avatar
Richard M. Stallman committed
394

Erik Naggum's avatar
Erik Naggum committed
395 396 397
;;---------------------------------------------------------------------------
;; Low level tools
;;---------------------------------------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
398

Erik Naggum's avatar
Erik Naggum committed
399 400 401
;;-----------------------------------------------
;; Misc.
;;-----------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
402 403 404

(defun minibuffer-window-selected-p ()
  "True iff the current window is the minibuffer."
405
  (window-minibuffer-p (selected-window)))
Richard M. Stallman's avatar
Richard M. Stallman committed
406

407
;; This used to be `(eval form)'.  Eval FORM at run time now.
408
(defmacro cmpl-read-time-eval (form)
409
  form)
Richard M. Stallman's avatar
Richard M. Stallman committed
410

Erik Naggum's avatar
Erik Naggum committed
411 412 413
;;-----------------------------------------------
;; String case coercion
;;-----------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

(defun cmpl-string-case-type (string)
  "Returns :capitalized, :up, :down, :mixed, or :neither."
  (let ((case-fold-search nil))
    (cond ((string-match "[a-z]" string)
	   (cond ((string-match "[A-Z]" string)
		  (cond ((and (> (length string) 1)
			      (null (string-match "[A-Z]" string 1)))
			 ':capitalized)
			(t
			 ':mixed)))
		 (t ':down)))
	  (t
	   (cond ((string-match "[A-Z]" string)
		  ':up)
		 (t ':neither))))
    ))

Erik Naggum's avatar
Erik Naggum committed
432 433 434 435 436 437
;; Tests -
;; (cmpl-string-case-type "123ABCDEF456") --> :up
;; (cmpl-string-case-type "123abcdef456") --> :down
;; (cmpl-string-case-type "123aBcDeF456") --> :mixed
;; (cmpl-string-case-type "123456")       --> :neither
;; (cmpl-string-case-type "Abcde123")     --> :capitalized
Richard M. Stallman's avatar
Richard M. Stallman committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

(defun cmpl-coerce-string-case (string case-type)
  (cond ((eq case-type ':down) (downcase string))
	((eq case-type ':up) (upcase string))
	((eq case-type ':capitalized)
	 (setq string (downcase string))
	 (aset string 0 (logand ?\337 (aref string 0)))
	 string)
	(t string)
	))

(defun cmpl-merge-string-cases (string-to-coerce given-string)
  (let ((string-case-type (cmpl-string-case-type string-to-coerce))
	)
    (cond ((memq string-case-type '(:down :up :capitalized))
	   ;; Found string is in a standard case.  Coerce to a type based on
	   ;; the given string
	   (cmpl-coerce-string-case string-to-coerce
			       (cmpl-string-case-type given-string))
	   )
	  (t
	   ;; If the found string is in some unusual case, just insert it
	   ;; as is
	   string-to-coerce)
	  )))

Erik Naggum's avatar
Erik Naggum committed
464 465 466 467 468
;; Tests -
;; (cmpl-merge-string-cases "AbCdEf456" "abc")     --> AbCdEf456
;; (cmpl-merge-string-cases "abcdef456" "ABC")     --> ABCDEF456
;; (cmpl-merge-string-cases "ABCDEF456" "Abc")     --> Abcdef456
;; (cmpl-merge-string-cases "ABCDEF456" "abc")     --> abcdef456
Richard M. Stallman's avatar
Richard M. Stallman committed
469 470


471 472
(defun cmpl-hours-since-origin ()
  (let ((time (current-time)))
473
    (floor (+ (* 65536.0 (nth 0 time)) (nth 1 time)) 3600)))
Richard M. Stallman's avatar
Richard M. Stallman committed
474

Erik Naggum's avatar
Erik Naggum committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
;;---------------------------------------------------------------------------
;; "Symbol" parsing functions
;;---------------------------------------------------------------------------
;; The functions symbol-before-point, symbol-under-point, etc. quickly return
;; an appropriate symbol string.  The strategy is to temporarily change
;; the syntax table to enable fast symbol searching.  There are three classes
;; of syntax in these "symbol" syntax tables ::
;;
;; syntax (?_) - "symbol" chars (e.g. alphanumerics)
;; syntax (?w) - symbol chars to ignore at end of words (e.g. period).  
;; syntax (? ) - everything else
;;
;; Thus by judicious use of scan-sexps and forward-word, we can get
;; the word we want relatively fast and without consing.  
;;
;; Why do we need a separate category for "symbol chars to ignore at ends" ?
;; For example, in LISP we want starting :'s trimmed 
;; so keyword argument specifiers also define the keyword completion.  And,
;; for example, in C we want `.' appearing in a structure ref. to
;; be kept intact in order to store the whole structure ref.; however, if 
;; it appears at the end of a symbol it should be discarded because it is
;; probably used as a period.

;; Here is the default completion syntax ::
;; Symbol chars :: A-Z a-z 0-9 @ / \ * + ~ $ < > %
;; Symbol chars to ignore at ends :: _ : . -
;; Separator chars. :: <tab> <space> ! ^ & ( ) = ` | { } [ ] ; " ' #
;;                     , ? <Everything else>

;; Mode specific differences and notes ::
;;  LISP diffs ->
;;    Symbol chars :: ! & ? = ^
;;
;; C diffs ->
;;   Separator chars :: + * / : %
;;  A note on the hyphen (`-').  Perhaps the hyphen should also be a separator
;; char., however, we wanted to have completion symbols include pointer 
;; references.  For example, "foo->bar" is a symbol as far as completion is
;; concerned.
;;
;; FORTRAN diffs ->
;;   Separator chars :: + - * / :
;;
;; Pathname diffs ->
;;   Symbol chars :: .
;;  Of course there is no pathname "mode" and in fact we have not implemented
;; this table.  However, if there was such a mode, this is what it would look
;; like.

;;-----------------------------------------------
;; Table definitions
;;-----------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
527

528
(defun cmpl-make-standard-completion-syntax-table ()
Richard M. Stallman's avatar
Richard M. Stallman committed
529
  (let ((table (make-syntax-table))
530
	i)
Richard M. Stallman's avatar
Richard M. Stallman committed
531 532 533 534 535
    ;; Default syntax is whitespace.
    (setq i 0)
    (while (< i 256)
      (modify-syntax-entry i " " table)
      (setq i (1+ i)))
Richard M. Stallman's avatar
Richard M. Stallman committed
536
    ;; alpha chars
537 538
    (setq i 0)
    (while (< i 26)
Richard M. Stallman's avatar
Richard M. Stallman committed
539
      (modify-syntax-entry (+ ?a i) "_" table)
540 541
      (modify-syntax-entry (+ ?A i) "_" table)
      (setq i (1+ i)))
Richard M. Stallman's avatar
Richard M. Stallman committed
542
    ;; digit chars.
543 544 545 546
    (setq i 0)
    (while (< i 10)
      (modify-syntax-entry (+ ?0 i) "_" table)
      (setq i (1+ i)))
Richard M. Stallman's avatar
Richard M. Stallman committed
547 548 549 550
    ;; Other ones
    (let ((symbol-chars '(?@ ?/ ?\\ ?* ?+ ?~ ?$ ?< ?> ?%))
	  (symbol-chars-ignore '(?_ ?- ?: ?.))
	  )
551
      (completion-dolist (char symbol-chars)
Richard M. Stallman's avatar
Richard M. Stallman committed
552
	(modify-syntax-entry char "_" table))
553
      (completion-dolist (char symbol-chars-ignore)
Richard M. Stallman's avatar
Richard M. Stallman committed
554 555 556 557 558
	(modify-syntax-entry char "w" table)
	)
      )
    table))

559
(defconst cmpl-standard-syntax-table (cmpl-make-standard-completion-syntax-table))
Richard M. Stallman's avatar
Richard M. Stallman committed
560

561
(defun cmpl-make-lisp-completion-syntax-table ()
Richard M. Stallman's avatar
Richard M. Stallman committed
562 563 564
  (let ((table (copy-syntax-table cmpl-standard-syntax-table))
	(symbol-chars '(?! ?& ?? ?= ?^))
	)
565
    (completion-dolist (char symbol-chars)
Richard M. Stallman's avatar
Richard M. Stallman committed
566 567 568
      (modify-syntax-entry char "_" table))
    table))
	   
569
(defun cmpl-make-c-completion-syntax-table ()
Richard M. Stallman's avatar
Richard M. Stallman committed
570 571 572
  (let ((table (copy-syntax-table cmpl-standard-syntax-table))
	(separator-chars '(?+ ?* ?/ ?: ?%))
	)
573
    (completion-dolist (char separator-chars)
Richard M. Stallman's avatar
Richard M. Stallman committed
574 575 576
      (modify-syntax-entry char " " table))
    table))

577
(defun cmpl-make-fortran-completion-syntax-table ()
Richard M. Stallman's avatar
Richard M. Stallman committed
578 579 580
  (let ((table (copy-syntax-table cmpl-standard-syntax-table))
	(separator-chars '(?+ ?- ?* ?/ ?:))
	)
581
    (completion-dolist (char separator-chars)
Richard M. Stallman's avatar
Richard M. Stallman committed
582 583 584
      (modify-syntax-entry char " " table))
    table))

585 586 587
(defconst cmpl-lisp-syntax-table       (cmpl-make-lisp-completion-syntax-table))
(defconst cmpl-c-syntax-table          (cmpl-make-c-completion-syntax-table))
(defconst cmpl-fortran-syntax-table    (cmpl-make-fortran-completion-syntax-table))
Richard M. Stallman's avatar
Richard M. Stallman committed
588 589 590 591 592

(defvar cmpl-syntax-table cmpl-standard-syntax-table
  "This variable holds the current completion syntax table.")
(make-variable-buffer-local 'cmpl-syntax-table)

Erik Naggum's avatar
Erik Naggum committed
593 594 595
;;-----------------------------------------------
;; Installing the appropriate mode tables
;;-----------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
596

597 598 599
(add-hook 'lisp-mode-hook
	  '(lambda ()
	     (setq cmpl-syntax-table cmpl-lisp-syntax-table)))
Richard M. Stallman's avatar
Richard M. Stallman committed
600

601 602 603
(add-hook 'c-mode-hook
	  '(lambda ()
	     (setq cmpl-syntax-table cmpl-c-syntax-table)))
Richard M. Stallman's avatar
Richard M. Stallman committed
604

605 606 607 608
(add-hook 'fortran-mode-hook
	  '(lambda ()
	     (setq cmpl-syntax-table cmpl-fortran-syntax-table)
	     (completion-setup-fortran-mode)))
Richard M. Stallman's avatar
Richard M. Stallman committed
609

Erik Naggum's avatar
Erik Naggum committed
610 611 612
;;-----------------------------------------------
;; Symbol functions
;;-----------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
613
(defvar cmpl-symbol-start nil
614
  "Holds first character of symbol, after any completion symbol function.")
Richard M. Stallman's avatar
Richard M. Stallman committed
615
(defvar cmpl-symbol-end nil
616
  "Holds last character of symbol, after any completion symbol function.")
Erik Naggum's avatar
Erik Naggum committed
617 618
;; These are temp. vars. we use to avoid using let.
;;   Why ?  Small speed improvement.
Richard M. Stallman's avatar
Richard M. Stallman committed
619 620 621 622
(defvar cmpl-saved-syntax nil)
(defvar cmpl-saved-point nil)

(defun symbol-under-point ()
623 624
  "Returns the symbol that the point is currently on.
But only if it is longer than `completion-min-length'."
Richard M. Stallman's avatar
Richard M. Stallman committed
625
  (setq cmpl-saved-syntax (syntax-table))
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
  (unwind-protect
      (progn
	(set-syntax-table cmpl-syntax-table)
	(cond 
	;; Cursor is on following-char and after preceding-char
	  ((memq (char-syntax (following-char)) '(?w ?_))     
	   (setq cmpl-saved-point (point)
		 cmpl-symbol-start (scan-sexps (1+ cmpl-saved-point) -1)
		 cmpl-symbol-end (scan-sexps cmpl-saved-point 1))
	   ;; Remove chars to ignore at the start.
	   (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w)
		  (goto-char cmpl-symbol-start)
		  (forward-word 1)
		  (setq cmpl-symbol-start (point))
		  (goto-char cmpl-saved-point)
		  ))
	   ;; Remove chars to ignore at the end.
	   (cond ((= (char-syntax (char-after (1- cmpl-symbol-end))) ?w)
		  (goto-char cmpl-symbol-end)
		  (forward-word -1)
		  (setq cmpl-symbol-end (point))
		  (goto-char cmpl-saved-point)
		  ))
	   ;; Return completion if the length is reasonable.
	   (if (and (<= (cmpl-read-time-eval completion-min-length)
			(- cmpl-symbol-end cmpl-symbol-start))
		    (<= (- cmpl-symbol-end cmpl-symbol-start)
			(cmpl-read-time-eval completion-max-length)))
	       (buffer-substring cmpl-symbol-start cmpl-symbol-end)))))
    (set-syntax-table cmpl-saved-syntax)))
Richard M. Stallman's avatar
Richard M. Stallman committed
656

Erik Naggum's avatar
Erik Naggum committed
657 658 659 660 661 662 663 664 665 666 667
;; tests for symbol-under-point
;;  `^' indicates cursor pos. where value is returned
;;  simple-word-test
;;  ^^^^^^^^^^^^^^^^  --> simple-word-test
;;  _harder_word_test_
;;  ^^^^^^^^^^^^^^^^^^ --> harder_word_test
;;  .___.______.
;;  --> nil
;;  /foo/bar/quux.hello
;;  ^^^^^^^^^^^^^^^^^^^ --> /foo/bar/quux.hello
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
668 669

(defun symbol-before-point ()
670 671
  "Returns a string of the symbol immediately before point.
Returns nil if there isn't one longer than `completion-min-length'."       
Richard M. Stallman's avatar
Richard M. Stallman committed
672 673
  ;; This is called when a word separator is typed so it must be FAST !
  (setq cmpl-saved-syntax (syntax-table))
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  (unwind-protect
      (progn
	(set-syntax-table cmpl-syntax-table)
	;; Cursor is on following-char and after preceding-char
	(cond ((= (setq cmpl-preceding-syntax (char-syntax (preceding-char))) ?_)
	       ;; Number of chars to ignore at end.
	       (setq cmpl-symbol-end (point)
		     cmpl-symbol-start (scan-sexps cmpl-symbol-end -1)
		     )
	       ;; Remove chars to ignore at the start.
	       (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w)
		      (goto-char cmpl-symbol-start)
		      (forward-word 1)
		      (setq cmpl-symbol-start (point))
		      (goto-char cmpl-symbol-end)
		      ))
	       ;; Return value if long enough.
	       (if (>= cmpl-symbol-end
		       (+ cmpl-symbol-start
			  (cmpl-read-time-eval completion-min-length)))
		   (buffer-substring cmpl-symbol-start cmpl-symbol-end))
Richard M. Stallman's avatar
Richard M. Stallman committed
695
	       )
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
	      ((= cmpl-preceding-syntax ?w)
	       ;; chars to ignore at end
	       (setq cmpl-saved-point (point)
		     cmpl-symbol-start (scan-sexps cmpl-saved-point -1))
	       ;; take off chars. from end
	       (forward-word -1)
	       (setq cmpl-symbol-end (point))
	       ;; remove chars to ignore at the start
	       (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w)
		      (goto-char cmpl-symbol-start)
		      (forward-word 1)
		      (setq cmpl-symbol-start (point))
		      ))
	       ;; Restore state.
	       (goto-char cmpl-saved-point)
	       ;; Return completion if the length is reasonable
	       (if (and (<= (cmpl-read-time-eval completion-min-length)
			    (- cmpl-symbol-end cmpl-symbol-start))
			(<= (- cmpl-symbol-end cmpl-symbol-start)
			    (cmpl-read-time-eval completion-max-length)))
		   (buffer-substring cmpl-symbol-start cmpl-symbol-end)))))
    (set-syntax-table cmpl-saved-syntax)))
Richard M. Stallman's avatar
Richard M. Stallman committed
718

Erik Naggum's avatar
Erik Naggum committed
719 720 721 722 723 724 725 726 727 728 729 730 731
;; tests for symbol-before-point
;;  `^' indicates cursor pos. where value is returned
;;  simple-word-test
;;  ^ --> nil
;;   ^ --> nil
;;          ^  --> simple-w
;;                  ^ --> simple-word-test
;;  _harder_word_test_
;;                   ^  --> harder_word_test
;;                    ^  --> harder_word_test
;;          ^ --> harder
;;  .___....
;;  --> nil
Richard M. Stallman's avatar
Richard M. Stallman committed
732 733

(defun symbol-under-or-before-point ()
Erik Naggum's avatar
Erik Naggum committed
734 735 736 737
  ;; This could be made slightly faster but it is better to avoid
  ;; copying all the code.
  ;; However, it is only used by the completion string prompter.
  ;; If it comes into common use, it could be rewritten.
738 739 740 741 742 743 744 745
  (cond ((memq (progn
		 (setq cmpl-saved-syntax (syntax-table))
		 (unwind-protect
		     (progn
		       (set-syntax-table cmpl-syntax-table)
		       (char-syntax (following-char)))
		   (set-syntax-table cmpl-saved-syntax)))
	       '(?w ?_))
Richard M. Stallman's avatar
Richard M. Stallman committed
746 747
	 (symbol-under-point))
	(t
748
	 (symbol-before-point))))
Richard M. Stallman's avatar
Richard M. Stallman committed
749 750 751 752 753 754 755 756


(defun symbol-before-point-for-complete ()
  ;; "Returns a string of the symbol immediately before point
  ;; or nil if there isn't one.  Like symbol-before-point but doesn't trim the
  ;; end chars."
  ;; Cursor is on following-char and after preceding-char
  (setq cmpl-saved-syntax (syntax-table))
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
  (unwind-protect
      (progn
	(set-syntax-table cmpl-syntax-table)
	(cond ((memq (setq cmpl-preceding-syntax (char-syntax (preceding-char)))
		     '(?_ ?w))
	       (setq cmpl-symbol-end (point)
		     cmpl-symbol-start (scan-sexps cmpl-symbol-end -1)
		     )
	       ;; Remove chars to ignore at the start.
	       (cond ((= (char-syntax (char-after cmpl-symbol-start)) ?w)
		      (goto-char cmpl-symbol-start)
		      (forward-word 1)
		      (setq cmpl-symbol-start (point))
		      (goto-char cmpl-symbol-end)
		      ))
	       ;; Return completion if the length is reasonable.
	       (if (and (<= (cmpl-read-time-eval
			     completion-prefix-min-length)
			    (- cmpl-symbol-end cmpl-symbol-start))
			(<= (- cmpl-symbol-end cmpl-symbol-start)
			    (cmpl-read-time-eval completion-max-length)))
		   (buffer-substring cmpl-symbol-start cmpl-symbol-end)))))
    ;; Restore syntax table.
    (set-syntax-table cmpl-saved-syntax)))
Richard M. Stallman's avatar
Richard M. Stallman committed
781

Erik Naggum's avatar
Erik Naggum committed
782 783 784 785 786 787 788 789 790 791 792 793 794
;; tests for symbol-before-point-for-complete
;;  `^' indicates cursor pos. where value is returned
;;  simple-word-test
;;  ^ --> nil
;;   ^ --> nil
;;          ^  --> simple-w
;;                  ^ --> simple-word-test
;;  _harder_word_test_
;;                   ^  --> harder_word_test
;;                    ^  --> harder_word_test_
;;          ^ --> harder_
;;  .___....
;;  --> nil
Richard M. Stallman's avatar
Richard M. Stallman committed
795 796 797



Erik Naggum's avatar
Erik Naggum committed
798 799 800
;;---------------------------------------------------------------------------
;; Statistics Recording
;;---------------------------------------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
801

Erik Naggum's avatar
Erik Naggum committed
802 803
;; Note that the guts of this has been turned off.  The guts
;; are in completion-stats.el.
Richard M. Stallman's avatar
Richard M. Stallman committed
804

Erik Naggum's avatar
Erik Naggum committed
805 806 807 808
;;-----------------------------------------------
;; Conditionalizing code on *record-cmpl-statistics-p*
;;-----------------------------------------------
;; All statistics code outside this block should use this
809
(defmacro cmpl-statistics-block (&rest body))
Erik Naggum's avatar
Erik Naggum committed
810 811 812 813
;;  "Only executes body if we are recording statistics."
;;  (list 'cond
;;	(list* '*record-cmpl-statistics-p* body)
;;	))		 
Richard M. Stallman's avatar
Richard M. Stallman committed
814

Erik Naggum's avatar
Erik Naggum committed
815 816 817
;;-----------------------------------------------
;; Completion Sources
;;-----------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831

;; ID numbers
(defconst cmpl-source-unknown 0)
(defconst cmpl-source-init-file 1)
(defconst cmpl-source-file-parsing 2)
(defconst cmpl-source-separator 3)
(defconst cmpl-source-cursor-moves 4)
(defconst cmpl-source-interactive 5)
(defconst cmpl-source-cdabbrev 6)
(defconst num-cmpl-sources 7)
(defvar current-completion-source cmpl-source-unknown)



Erik Naggum's avatar
Erik Naggum committed
832 833 834 835 836 837 838 839 840 841 842 843
;;---------------------------------------------------------------------------
;; Completion Method #2: dabbrev-expand style
;;---------------------------------------------------------------------------
;;
;;   This method is used if there are no useful stored completions.  It is 
;; based on dabbrev-expand with these differences :
;;   1) Faster (we don't use regexps)
;;   2) case coercion handled correctly
;; This is called cdabbrev to differentiate it.
;;   We simply search backwards through the file looking for words which
;; start with the same letters we are trying to complete.
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
844 845

(defvar cdabbrev-completions-tried nil)
Erik Naggum's avatar
Erik Naggum committed
846
;;  "A list of all the cdabbrev completions since the last reset.")
Richard M. Stallman's avatar
Richard M. Stallman committed
847 848

(defvar cdabbrev-current-point 0)
Erik Naggum's avatar
Erik Naggum committed
849
;;  "The current point position the cdabbrev search is at.")
Richard M. Stallman's avatar
Richard M. Stallman committed
850 851

(defvar cdabbrev-current-window nil)
Erik Naggum's avatar
Erik Naggum committed
852 853
;;  "The current window we are looking for cdabbrevs in.  T if looking in
;; (other-buffer), NIL if no more  cdabbrevs.")
Richard M. Stallman's avatar
Richard M. Stallman committed
854 855

(defvar cdabbrev-wrapped-p nil)
Erik Naggum's avatar
Erik Naggum committed
856
;;  "T if the cdabbrev search has wrapped around the file.")
Richard M. Stallman's avatar
Richard M. Stallman committed
857 858 859

(defvar cdabbrev-abbrev-string "")
(defvar cdabbrev-start-point 0)
860
(defvar cdabbrev-stop-point)
Richard M. Stallman's avatar
Richard M. Stallman committed
861

Erik Naggum's avatar
Erik Naggum committed
862 863 864 865 866 867 868
;; Test strings for cdabbrev
;; cdat-upcase   ;;same namestring
;; CDAT-UPCASE   ;;ok
;; cdat2         ;;too short
;; cdat-1-2-3-4  ;;ok
;; a-cdat-1      ;;doesn't start correctly
;; cdat-simple   ;;ok
Richard M. Stallman's avatar
Richard M. Stallman committed
869 870 871 872


(defun reset-cdabbrev (abbrev-string &optional initial-completions-tried)
  "Resets the cdabbrev search to search for abbrev-string.
873
INITIAL-COMPLETIONS-TRIED is a list of downcased strings to ignore
Richard M. Stallman's avatar
Richard M. Stallman committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
during the search."
  (setq cdabbrev-abbrev-string abbrev-string
	cdabbrev-completions-tried
	(cons (downcase abbrev-string) initial-completions-tried)
	)
  (reset-cdabbrev-window t)
  )

(defun set-cdabbrev-buffer ()
  ;; cdabbrev-current-window must not be NIL
  (set-buffer (if (eq cdabbrev-current-window t)
		  (other-buffer)
		  (window-buffer cdabbrev-current-window)))
  )


(defun reset-cdabbrev-window (&optional initializep)
891
  "Resets the cdabbrev search to search for abbrev-string."
Richard M. Stallman's avatar
Richard M. Stallman committed
892 893 894 895 896 897 898 899 900 901 902 903 904
  ;; Set the window
  (cond (initializep
	 (setq cdabbrev-current-window (selected-window))
	 )
	((eq cdabbrev-current-window t)
	 ;; Everything has failed
	 (setq cdabbrev-current-window nil))
	(cdabbrev-current-window
	 (setq cdabbrev-current-window (next-window cdabbrev-current-window))
	 (if (eq cdabbrev-current-window (selected-window))
	     ;; No more windows, try other buffer.
	     (setq cdabbrev-current-window t)))
	)
905 906 907 908 909 910 911 912 913 914 915 916
  (if cdabbrev-current-window
      (save-excursion
	(set-cdabbrev-buffer)
	(setq cdabbrev-current-point (point)
	      cdabbrev-start-point cdabbrev-current-point
	      cdabbrev-stop-point
	      (if completion-search-distance
		  (max (point-min)
		       (- cdabbrev-start-point completion-search-distance))
		  (point-min))
	      cdabbrev-wrapped-p nil)
	)))
Richard M. Stallman's avatar
Richard M. Stallman committed
917 918 919

(defun next-cdabbrev ()
  "Return the next possible cdabbrev expansion or nil if there isn't one.
920 921
`reset-cdabbrev' must've been called already.
This is sensitive to `case-fold-search'."
Richard M. Stallman's avatar
Richard M. Stallman committed
922 923
  ;; note that case-fold-search affects the behavior of this function
  ;; Bug: won't pick up an expansion that starts at the top of buffer
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  (if cdabbrev-current-window
      (let (saved-point 
	    saved-syntax
	    (expansion nil)
	    downcase-expansion tried-list syntax saved-point-2)
	(save-excursion
	  (unwind-protect
	      (progn
		;; Switch to current completion buffer
		(set-cdabbrev-buffer)
		;; Save current buffer state
		(setq saved-point  (point)
		      saved-syntax (syntax-table))
		;; Restore completion state
		(set-syntax-table cmpl-syntax-table)
		(goto-char cdabbrev-current-point)
		;; Loop looking for completions
		(while
		    ;; This code returns t if it should loop again
		    (cond
		      (;; search for the string
		       (search-backward cdabbrev-abbrev-string cdabbrev-stop-point t)
		       ;; return nil if the completion is valid
		       (not
			(and
			 ;; does it start with a separator char ?
			 (or (= (setq syntax (char-syntax (preceding-char))) ? )
			     (and (= syntax ?w)
				  ;; symbol char to ignore at end.  Are we at end ?
				  (progn
				    (setq saved-point-2 (point))
				    (forward-word -1)
				    (prog1
				      (= (char-syntax (preceding-char)) ? )
				      (goto-char saved-point-2)
				      ))))
			 ;; is the symbol long enough ?
			 (setq expansion (symbol-under-point))
			 ;; have we not tried this one before
			 (progn
			   ;; See if we've already used it
			   (setq tried-list cdabbrev-completions-tried
				 downcase-expansion (downcase expansion))
			   (while (and tried-list
				       (not (string-equal downcase-expansion
							  (car tried-list))))
			     ;; Already tried, don't choose this one
			     (setq tried-list (cdr tried-list))
			     )
			   ;; at this point tried-list will be nil if this
			   ;; expansion has not yet been tried
			   (if tried-list
			       (setq expansion nil)
			       t)
			   ))))
		      ;; search failed
		      (cdabbrev-wrapped-p
		       ;; If already wrapped, then we've failed completely
		       nil)
		      (t
		       ;; need to wrap
		       (goto-char (setq cdabbrev-current-point
					(if completion-search-distance
					    (min (point-max) (+ cdabbrev-start-point completion-search-distance))
					    (point-max))))

		       (setq cdabbrev-wrapped-p t))
		      ))
		;; end of while loop
		(cond (expansion
		       ;; successful
		       (setq cdabbrev-completions-tried
			     (cons downcase-expansion cdabbrev-completions-tried)
			     cdabbrev-current-point (point))))
		)
	    (set-syntax-table saved-syntax)
	    (goto-char saved-point)
	    ))
	;; If no expansion, go to next window
	(cond (expansion)
	      (t (reset-cdabbrev-window)
		 (next-cdabbrev))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1006

Erik Naggum's avatar
Erik Naggum committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
;; The following must be eval'd in the minibuffer ::
;; (reset-cdabbrev "cdat")
;; (next-cdabbrev)  --> "cdat-simple"
;; (next-cdabbrev)  --> "cdat-1-2-3-4"
;; (next-cdabbrev)  --> "CDAT-UPCASE"
;; (next-cdabbrev)  --> "cdat-wrapping"
;; (next-cdabbrev)  --> "cdat_start_sym"
;; (next-cdabbrev)  --> nil
;; (next-cdabbrev)  --> nil
;; (next-cdabbrev)  --> nil
Richard M. Stallman's avatar
Richard M. Stallman committed
1017

Erik Naggum's avatar
Erik Naggum committed
1018 1019
;; _cdat_start_sym
;; cdat-wrapping
Richard M. Stallman's avatar
Richard M. Stallman committed
1020 1021


Erik Naggum's avatar
Erik Naggum committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
;;---------------------------------------------------------------------------
;; Completion Database
;;---------------------------------------------------------------------------

;; We use two storage modes for the two search types ::
;;  1) Prefix {cmpl-prefix-obarray} for looking up possible completions
;;      Used by search-completion-next
;;      the value of the symbol is nil or a cons of head and tail pointers
;;  2) Interning {cmpl-obarray} to see if it's in the database
;;      Used by find-exact-completion, completion-in-database-p
;;      The value of the symbol is the completion entry

;; bad things may happen if this length is changed due to the way
;; GNU implements obarrays
Richard M. Stallman's avatar
Richard M. Stallman committed
1036 1037 1038
(defconst cmpl-obarray-length 511)

(defvar cmpl-prefix-obarray (make-vector cmpl-obarray-length 0)
1039
  "An obarray used to store the downcased completion prefixes.
Richard M. Stallman's avatar
Richard M. Stallman committed
1040 1041 1042 1043 1044 1045
Each symbol is bound to a list of completion entries.")

(defvar cmpl-obarray (make-vector cmpl-obarray-length 0)
  "An obarray used to store the downcased completions.
Each symbol is bound to a single completion entry.")

Erik Naggum's avatar
Erik Naggum committed
1046 1047 1048
;;-----------------------------------------------
;; Completion Entry Structure Definition
;;-----------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
1049

Erik Naggum's avatar
Erik Naggum committed
1050 1051 1052 1053
;; A completion entry is a LIST of string, prefix-symbol num-uses, and
;; last-use-time (the time the completion was last used)
;; last-use-time is T if the string should be kept permanently
;; num-uses is incremented every time the completion is used.
Richard M. Stallman's avatar
Richard M. Stallman committed
1054

Erik Naggum's avatar
Erik Naggum committed
1055 1056
;; We chose lists because (car foo) is faster than (aref foo 0) and the 
;; creation time is about the same.
Richard M. Stallman's avatar
Richard M. Stallman committed
1057

Erik Naggum's avatar
Erik Naggum committed
1058
;; READER MACROS
Richard M. Stallman's avatar
Richard M. Stallman committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

(defmacro completion-string (completion-entry)
  (list 'car completion-entry))

(defmacro completion-num-uses (completion-entry)
  ;;  "The number of times it has used.  Used to decide whether to save 
  ;; it."
  (list 'car (list 'cdr completion-entry)))

(defmacro completion-last-use-time (completion-entry)
1069
  ;;  "The time it was last used.  In hours since origin.  Used to decide
Richard M. Stallman's avatar
Richard M. Stallman committed
1070 1071 1072 1073 1074 1075
  ;; whether to save it.  T if one should always save it."
  (list 'nth 2 completion-entry))

(defmacro completion-source (completion-entry)
  (list 'nth 3 completion-entry))

Erik Naggum's avatar
Erik Naggum committed
1076
;; WRITER MACROS
Richard M. Stallman's avatar
Richard M. Stallman committed
1077 1078 1079 1080 1081 1082 1083 1084 1085
(defmacro set-completion-string (completion-entry string)
  (list 'setcar completion-entry string))

(defmacro set-completion-num-uses (completion-entry num-uses)
  (list 'setcar (list 'cdr completion-entry) num-uses))

(defmacro set-completion-last-use-time (completion-entry last-use-time)
  (list 'setcar (list 'cdr (list 'cdr completion-entry)) last-use-time))

Erik Naggum's avatar
Erik Naggum committed
1086
;; CONSTRUCTOR
Richard M. Stallman's avatar
Richard M. Stallman committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
(defun make-completion (string)
  "Returns a list of a completion entry."
  (list (list string 0 nil current-completion-source)))

;; Obsolete
;;(defmacro cmpl-prefix-entry-symbol (completion-entry)
;;  (list 'car (list 'cdr completion-entry)))



Erik Naggum's avatar
Erik Naggum committed
1097 1098 1099 1100
;;-----------------------------------------------
;; Prefix symbol entry definition
;;-----------------------------------------------
;; A cons of (head . tail)
Richard M. Stallman's avatar
Richard M. Stallman committed
1101

Erik Naggum's avatar
Erik Naggum committed
1102
;; READER Macros
Richard M. Stallman's avatar
Richard M. Stallman committed
1103 1104 1105 1106 1107 1108 1109

(defmacro cmpl-prefix-entry-head (prefix-entry)
  (list 'car prefix-entry))

(defmacro cmpl-prefix-entry-tail (prefix-entry)
  (list 'cdr prefix-entry))

Erik Naggum's avatar
Erik Naggum committed
1110
;; WRITER Macros
Richard M. Stallman's avatar
Richard M. Stallman committed
1111 1112 1113 1114 1115 1116 1117

(defmacro set-cmpl-prefix-entry-head (prefix-entry new-head)
  (list 'setcar prefix-entry new-head))

(defmacro set-cmpl-prefix-entry-tail (prefix-entry new-tail)
  (list 'setcdr prefix-entry new-tail))

Erik Naggum's avatar
Erik Naggum committed
1118
;; Constructor
Richard M. Stallman's avatar
Richard M. Stallman committed
1119 1120 1121 1122 1123

(defun make-cmpl-prefix-entry (completion-entry-list)
  "Makes a new prefix entry containing only completion-entry."
  (cons completion-entry-list completion-entry-list))

Erik Naggum's avatar
Erik Naggum committed
1124 1125 1126
;;-----------------------------------------------
;; Completion Database - Utilities
;;-----------------------------------------------
Richard M. Stallman's avatar
Richard M. Stallman committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

(defun clear-all-completions ()
  "Initializes the completion storage.  All existing completions are lost."
  (interactive)
  (setq cmpl-prefix-obarray (make-vector cmpl-obarray-length 0))
  (setq cmpl-obarray (make-vector cmpl-obarray-length 0))
  (cmpl-statistics-block
    (record-clear-all-completions))
  )

1137 1138
(defvar completions-list-return-value)

Richard M. Stallman's avatar
Richard M. Stallman committed
1139 1140
(defun list-all-completions ()
  "Returns a list of all the known completion entries."
1141
  (let ((completions-list-return-value nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
1142
    (mapatoms 'list-all-completions-1 cmpl-prefix-obarray)
1143
    completions-list-return-value))
Richard M. Stallman's avatar
Richard M. Stallman committed
1144 1145 1146

(defun list-all-completions-1 (prefix-symbol)
  (if (boundp prefix-symbol)
1147
      (setq completions-list-return-value
Richard M. Stallman's avatar
Richard M. Stallman committed
1148
	    (append (cmpl-prefix-entry-head (symbol-value prefix-symbol))
1149
		    completions-list-return-value))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1150 1151

(defun list-all-completions-by-hash-bucket ()
1152
  "Return list of lists of known completion entries, organized by hash bucket."
1153
  (let ((completions-list-return-value nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
1154
    (mapatoms 'list-all-completions-by-hash-bucket-1 cmpl-prefix-obarray)
1155
    completions-list-return-value))
Richard M. Stallman's avatar
Richard M. Stallman committed
1156 1157 1158

(defun list-all-completions-by-hash-bucket-1 (prefix-symbol)
  (if (boundp prefix-symbol)
1159
      (setq completions-list-return-value
Richard M. Stallman's avatar
Richard M. Stallman committed
1160
	    (cons (cmpl-prefix-entry-head (symbol-value prefix-symbol))
1161
		  completions-list-return-value))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1162 1163


Erik Naggum's avatar
Erik Naggum committed
1164 1165 1166 1167 1168 1169 1170
;;-----------------------------------------------
;; Updating the database
;;-----------------------------------------------
;;
;;   These are the internal functions used to update the datebase
;;
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
(defvar completion-to-accept nil)
  ;;"Set to a string that is pending its acceptance."
  ;; this checked by the top level reading functions

(defvar cmpl-db-downcase-string nil)
  ;; "Setup by find-exact-completion, etc.  The given string, downcased."
(defvar cmpl-db-symbol nil)
  ;; "The interned symbol corresponding to cmpl-db-downcase-string.
  ;; Set up by cmpl-db-symbol."
(defvar cmpl-db-prefix-symbol nil)
  ;; "The interned prefix symbol corresponding to cmpl-db-downcase-string."
(defvar cmpl-db-entry nil)
(defvar cmpl-db-debug-p nil
  "Set to T if you want to debug the database.")

Erik Naggum's avatar
Erik Naggum committed
1186
;; READS
Richard M. Stallman's avatar
Richard M. Stallman committed
1187 1188
(defun find-exact-completion (string)
  "Returns the completion entry for string or nil.
1189
Sets up `cmpl-db-downcase-string' and `cmpl-db-symbol'."
Richard M. Stallman's avatar
Richard M. Stallman committed
1190 1191 1192 1193 1194 1195 1196
  (and (boundp (setq cmpl-db-symbol
		     (intern (setq cmpl-db-downcase-string (downcase string))
			     cmpl-obarray)))
       (symbol-value cmpl-db-symbol)
       ))

(defun find-cmpl-prefix-entry (prefix-string)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1197
  "Returns the prefix entry for string.
1198 1199 1200
Sets `cmpl-db-prefix-symbol'.
Prefix-string must be exactly `completion-prefix-min-length' long
and downcased.  Sets up `cmpl-db-prefix-symbol'."
Richard M. Stallman's avatar
Richard M. Stallman committed
1201 1202 1203 1204 1205 1206 1207 1208
  (and (boundp (setq cmpl-db-prefix-symbol
		     (intern prefix-string cmpl-prefix-obarray)))
       (symbol-value cmpl-db-prefix-symbol)))

(defvar inside-locate-completion-entry nil)
;; used to trap lossage in silent error correction

(defun locate-completion-entry (completion-entry prefix-entry)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1209 1210 1211
  "Locates the completion entry.
Returns a pointer to the element before the completion entry or nil if
the completion entry is at the head.
1212
Must be called after `find-exact-completion'."
Richard M. Stallman's avatar
Richard M. Stallman committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
  (let ((prefix-list (cmpl-prefix-entry-head prefix-entry))
	 next-prefix-list
	 )
    (cond
      ((not (eq (car prefix-list) completion-entry))
       ;; not already at head
       (while (and prefix-list
		   (not (eq completion-entry
			    (car (setq next-prefix-list (cdr prefix-list)))
			    )))
	 (setq prefix-list next-prefix-list))
       (cond (;; found
	      prefix-list)
	     ;; Didn't find it.  Database is messed up.
	     (cmpl-db-debug-p
	      ;; not found, error if debug mode
	      (error "Completion entry exists but not on prefix list - %s"
1230
		     completion-string))
Richard M. Stallman's avatar
Richard M. Stallman committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	     (inside-locate-completion-entry
	      ;; recursive error: really scrod
	      (locate-completion-db-error))
	     (t
	       ;; Patch out
	       (set cmpl-db-symbol nil)
	       ;; Retry
	       (locate-completion-entry-retry completion-entry)
	       ))))))

(defun locate-completion-entry-retry (old-entry)
  (let ((inside-locate-completion-entry t))
    (add-completion (completion-string old-entry)
		    (completion-num-uses old-entry)
		    (completion-last-use-time old-entry))
1246 1247 1248 1249 1250 1251
    (let* ((cmpl-entry (find-exact-completion (completion-string old-entry)))
	   (pref-entry
	    (if cmpl-entry
		(find-cmpl-prefix-entry
		  (substring cmpl-db-downcase-string
			     0 completion-prefix-min-length))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	  )
      (if (and cmpl-entry pref-entry)
	  ;; try again
	  (locate-completion-entry cmpl-entry pref-entry)
	  ;; still losing
	  (locate-completion-db-error))
      )))

(defun locate-completion-db-error ()
  ;; recursive error: really scrod
  (error "Completion database corrupted.  Try M-x clear-all-completions.  Send bug report.")
  )

Erik Naggum's avatar
Erik Naggum committed
1265
;; WRITES
Richard M. Stallman's avatar
Richard M. Stallman committed
1266
(defun add-completion-to-tail-if-new (string)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1267
  "If STRING is not in the database add it to appropriate prefix list.
1268
STRING is added to the end of the appropriate prefix list with
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1269
num-uses = 0.  The database is unchanged if it is there.  STRING must be
1270
longer than `completion-prefix-min-length'.
Richard M. Stallman's avatar
Richard M. Stallman committed
1271 1272 1273 1274 1275 1276 1277 1278 1279
This must be very fast.
Returns the completion entry."
  (or (find-exact-completion string)
      ;; not there
      (let (;; create an entry
	    (entry (make-completion string))
	    ;; setup the prefix
	    (prefix-entry (find-cmpl-prefix-entry
			    (substring cmpl-db-downcase-string 0
1280 1281
				       (cmpl-read-time-eval
					completion-prefix-min-length))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
	    )
	;; The next two forms should happen as a unit (atomically) but
	;; no fatal errors should result if that is not the case.
	(cond (prefix-entry
	       ;; These two should be atomic, but nothing fatal will happen
	       ;; if they're not.
	       (setcdr (cmpl-prefix-entry-tail prefix-entry) entry)
	       (set-cmpl-prefix-entry-tail prefix-entry entry))
	      (t
	       (set cmpl-db-prefix-symbol (make-cmpl-prefix-entry entry))
	       ))
	;; statistics
	(cmpl-statistics-block
	  (note-added-completion))
	;; set symbol
	(set cmpl-db-symbol (car entry))
	)))

1300 1301 1302 1303 1304
(defun add-completion-to-head (completion-string)
  "If COMPLETION-STRING is not in the database, add it to prefix list.
We add COMPLETION-STRING to the head of the appropriate prefix list,
or it to the head of the list.
COMPLETION-STRING must be longer than `completion-prefix-min-length'.
Richard M. Stallman's avatar
Richard M. Stallman committed
1305 1306 1307 1308 1309 1310
Updates the saved string with the supplied string.
This must be very fast.
Returns the completion entry."
  ;; Handle pending acceptance
  (if completion-to-accept (accept-completion))
  ;; test if already in database
1311
  (if (setq cmpl-db-entry (find-exact-completion completion-string))
Richard M. Stallman's avatar
Richard M. Stallman committed
1312 1313 1314
      ;; found
      (let* ((prefix-entry (find-cmpl-prefix-entry
			     (substring cmpl-db-downcase-string 0
1315 1316
					(cmpl-read-time-eval
					 completion-prefix-min-length))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1317 1318 1319 1320
	     (splice-ptr (locate-completion-entry cmpl-db-entry prefix-entry))
	     (cmpl-ptr (cdr splice-ptr))
	     )
	;; update entry
1321
	(set-completion-string cmpl-db-entry completion-string)
Richard M. Stallman's avatar
Richard M. Stallman committed
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	;; move to head (if necessary)
	(cond (splice-ptr
	       ;; These should all execute atomically but it is not fatal if
	       ;; they don't.
	       ;; splice it out
	       (or (setcdr splice-ptr (cdr cmpl-ptr))
		   ;; fix up tail if necessary
		   (set-cmpl-prefix-entry-tail prefix-entry splice-ptr))
	       ;; splice in at head
	       (setcdr cmpl-ptr (cmpl-prefix-entry-head prefix-entry))
	       (set-cmpl-prefix-entry-head prefix-entry cmpl-ptr)
	       ))
	cmpl-db-entry)
    ;; not there
    (let (;; create an entry
1337
	  (entry (make-completion completion-string))
Richard M. Stallman's avatar
Richard M. Stallman committed
1338 1339 1340
	  ;; setup the prefix
	  (prefix-entry (find-cmpl-prefix-entry
			  (substring cmpl-db-downcase-string 0
1341 1342
				     (cmpl-read-time-eval
				      completion-prefix-min-length))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
	  )
      (cond (prefix-entry
	     ;; Splice in at head
	     (setcdr entry (cmpl-prefix-entry-head prefix-entry))
	     (set-cmpl-prefix-entry-head prefix-entry entry))
	    (t
	     ;; Start new prefix entry
	     (set cmpl-db-prefix-symbol (make-cmpl-prefix-entry entry))
	     ))
      ;; statistics
      (cmpl-statistics-block
	(note-added-completion))
      ;; Add it to the symbol
      (set cmpl-db-symbol (car entry))
      )))
      
1359
(defun delete-completion (completion-string)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
1360
  "Deletes the completion from the database.
1361
String must be longer than `completion-prefix-min-length'."
Richard M. Stallman's avatar
Richard M. Stallman committed
1362 1363
  ;; Handle pending acceptance
  (if completion-to-accept (accept-completion))
1364
  (if (setq cmpl-db-entry (find-exact-completion completion-string))
Richard M. Stallman's avatar
Richard M. Stallman committed
1365 1366 1367
      ;; found
      (let* ((prefix-entry (find-cmpl-prefix-entry 
			     (substring cmpl-db-downcase-string 0
1368 1369
					(cmpl-read-time-eval
					 completion-prefix-min-length))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
	     (splice-ptr (locate-completion-entry cmpl-db-entry prefix-entry))
	     )
	 ;; delete symbol reference
	 (set cmpl-db-symbol nil)
	 ;; remove from prefix list
	 (cond (splice-ptr
		;; not at head
		(or (setcdr splice-ptr (cdr (cdr splice-ptr)))
		    ;; fix up tail if necessary
		    (set-cmpl-prefix-entry-tail prefix-entry splice-ptr))
		)
	       (t
		;; at head
		(or (set-cmpl-prefix-entry-head
		      prefix-entry (cdr (cmpl-prefix-entry-head prefix-entry)))
		    ;; List is now empty
		    (set cmpl-db-prefix-symbol nil))
		))
	 (cmpl-statistics-block
	   (note-completion-deleted))
	 )
1391
      (error "Unknown completion `%s'" completion-string)
Richard M. Stallman's avatar
Richard M. Stallman committed
1392 1393
      ))

Erik Naggum's avatar
Erik Naggum committed
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
;; Tests --
;;  - Add and Find -
;; (add-completion-to-head "banana")     --> ("banana" 0 nil 0)
;; (find-exact-completion "banana")      --> ("banana" 0 nil 0)
;; (find-exact-completion "bana")        --> nil
;; (car (find-cmpl-prefix-entry "ban"))  --> (("banana" ...))
;; (cdr (find-cmpl-prefix-entry "ban"))  --> (("banana" ...))
;; (add-completion-to-head "banish")     --> ("banish" 0 nil 0)
;; (find-exact-completion "banish")      --> ("banish" 0 nil 0)
;; (car (find-cmpl-prefix-entry "ban"))  --> (("banish" ...) ("banana" ...))
;; (cdr (find-cmpl-prefix-entry "ban"))  --> (("banana" ...))
;; (add-completion-to-head "banana")     --> ("banana" 0 nil 0)
;; (car (find-cmpl-prefix-entry "ban"))  --> (("banana" ...) ("banish" ...))
;; (cdr (find-cmpl-prefix-entry "ban"))  --> (("banish" ...))
;;
;;  - Deleting -
;; (add-completion-to-head "banner")     --> ("banner" 0 nil 0)
;; (delete-completion "banner")        
;; (find-exact-completion "banner")      --> nil
;; (car (find-cmpl-prefix-entry "ban"))  --> (("banana" ...) ("banish" ...))
;; (cdr (find-cmpl-prefix-entry "ban"))  --> (("banish" ...))
;; (add-completion-to-head "banner")     --> ("banner" 0 nil 0) 
;; (delete-completion "banana")        
;; (car (find-cmpl-prefix-entry "ban"))  --> (("banner" ...) ("banish" ...))
;; (cdr (find-cmpl-prefix-entry "ban"))  --> (("banish" ...))
;; (delete-completion "banner")        
;; (delete-completion "banish")                 
;; (find-cmpl-prefix-entry "ban")        --> nil
;; (delete-completion "banner")          --> error
;;
;; - Tail -
;; (add-completion-to-tail-if-new "banana") --> ("banana" 0 nil 0)
;; (car (find-cmpl-prefix-entry "ban"))     --> (("banana" ...))
;; (cdr (find-cmpl-prefix-entry "ban"))     --> (("banana" ...))
;; (add-completion-to-tail-if-new "banish") --> ("banish" 0 nil 0)
;; (car (find-cmpl-prefix-entry "ban"))     -->(("banana" ...) ("banish" ...))
;; (cdr (find-cmpl-prefix-entry "ban"))     -->(("banish" ...))
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
1432 1433


Erik Naggum's avatar
Erik Naggum committed
1434 1435 1436 1437 1438 1439
;;---------------------------------------------------------------------------
;; Database Update :: Interface level routines
;;---------------------------------------------------------------------------
;; 
;; These lie on top of the database ref. functions but below the standard
;; user interface level
Richard M. Stallman's avatar
Richard M. Stallman committed
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455


(defun interactive-completion-string-reader (prompt)
  (let* ((default (symbol-under-or-before-point))
	 (new-prompt
	  (if default
	      (format "%s: (default: %s) " prompt default)
	      (format "%s: " prompt))
	   )
	 (read (completing-read new-prompt cmpl-obarray))
	 )
    (if (zerop (length read)) (setq read (or default "")))
    (list read)
    ))

(defun check-completion-length (string)
1456
  (if (< (length string) completion-min-length)
1457
      (error "The string `%s' is too short to be saved as a completion"
Richard M. Stallman's avatar
Richard M. Stallman committed
1458 1459 1460 1461
	     string)
      (list string)))

(defun add-completion (string &optional num-uses last-use-time)
1462
  "Add STRING to completion list, or move it to head of list.
Richard M. Stallman's avatar
Richard M. Stallman committed
1463 1464 1465 1466 1467 1468 1469 1470