new version
[emacs.git] / lisp / complete.el
bloba20fb7d6516ac3728ec20887a16d87417d5cb165
1 ;;; complete.el --- partial completion mechanism plus other goodies
3 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Keywords: abbrev
7 ;; Version: 2.03
8 ;; Special thanks to Hallvard Furuseth for his many ideas and contributions.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Extended completion for the Emacs minibuffer.
31 ;; The basic idea is that the command name or other completable text is
32 ;; divided into words and each word is completed separately, so that
33 ;; "M-x p-b" expands to "M-x print-buffer". If the entry is ambiguous
34 ;; each word is completed as much as possible and then the cursor is
35 ;; left at the first position where typing another letter will resolve
36 ;; the ambiguity.
38 ;; Word separators for this purpose are hyphen, space, and period.
39 ;; These would most likely occur in command names, Info menu items,
40 ;; and file names, respectively. But all word separators are treated
41 ;; alike at all times.
43 ;; This completion package replaces the old-style completer's key
44 ;; bindings for TAB, SPC, RET, and `?'. The old completer is still
45 ;; available on the Meta versions of those keys. If you set
46 ;; PC-meta-flag to nil, the old completion keys will be left alone
47 ;; and the partial completer will use the Meta versions of the keys.
50 ;; Usage: M-x PC-mode. Now, during completable minibuffer entry,
52 ;; TAB means to do a partial completion;
53 ;; SPC means to do a partial complete-word;
54 ;; RET means to do a partial complete-and-exit;
55 ;; ? means to do a partial completion-help.
57 ;; If you set PC-meta-flag to nil, then TAB, SPC, RET, and ? perform
58 ;; original Emacs completions, and M-TAB etc. do partial completion.
59 ;; To do this, put the command,
61 ;; (setq PC-meta-flag nil)
63 ;; in your .emacs file. To load partial completion automatically, put
65 ;; (PC-mode t)
67 ;; in your .emacs file, too. Things will be faster if you byte-compile
68 ;; this file when you install it.
70 ;; As an extra feature, in cases where RET would not normally
71 ;; complete (such as `C-x b'), the M-RET key will always do a partial
72 ;; complete-and-exit. Thus `C-x b f.c RET' will select or create a
73 ;; buffer called "f.c", but `C-x b f.c M-RET' will select the existing
74 ;; buffer whose name matches that pattern (perhaps "filing.c").
75 ;; (PC-meta-flag does not affect this behavior; M-RET used to be
76 ;; undefined in this situation.)
78 ;; The regular M-TAB (lisp-complete-symbol) command also supports
79 ;; partial completion in this package.
81 ;; This package also contains a wildcard feature for C-x C-f (find-file).
82 ;; For example, `C-x C-f *.c RET' loads all .c files at once, exactly
83 ;; as if you had typed C-x C-f separately for each file. Completion
84 ;; is supported in connection with wildcards. Currently only the `*'
85 ;; wildcard character works.
87 ;; File name completion does not do partial completion of directories
88 ;; on the path, e.g., "/u/b/f" will not complete to "/usr/bin/foo",
89 ;; but you can put *'s in the path to accomplish this: "/u*/b*/f".
90 ;; Stars are required for performance reasons.
92 ;; In addition, this package includes a feature for accessing include
93 ;; files. For example, `C-x C-f <sys/time.h> RET' reads the file
94 ;; /usr/include/sys/time.h. The variable PC-include-file-path is a
95 ;; list of directories in which to search for include files. Completion
96 ;; is supported in include file names.
99 ;;; Code:
101 (defgroup partial-completion nil
102 "Partial Completion of items."
103 :prefix "pc-"
104 :group 'minibuffer)
106 (defcustom partial-completion-mode nil
107 "Toggle Partial Completion mode.
108 When Partial Completion mode is enabled, TAB (or M-TAB if `PC-meta-flag' is
109 nil) is enhanced so that if some string is divided into words and each word is
110 delimited by a character in `PC-word-delimiters', partial words are completed
111 as much as possible and `*' characters are treated likewise in file names.
112 You must modify via \\[customize] for this variable to have an effect."
113 :set (lambda (symbol value)
114 (partial-completion-mode (or value 0)))
115 :initialize 'custom-initialize-default
116 :type 'boolean
117 :group 'partial-completion
118 :require 'complete)
120 (defcustom PC-first-char 'find-file
121 "*Control how the first character of a string is to be interpreted.
122 If nil, the first character of a string is not taken literally if it is a word
123 delimiter, so that \".e\" matches \"*.e*\".
124 If t, the first character of a string is always taken literally even if it is a
125 word delimiter, so that \".e\" matches \".e*\".
126 If non-nil and non-t, the first character is taken literally only for file name
127 completion."
128 :type '(choice (const :tag "delimiter" nil)
129 (const :tag "literal" t)
130 (sexp :tag "find-file" :format "%t\n" find-file))
131 :group 'partial-completion)
133 (defcustom PC-meta-flag t
134 "*If non-nil, TAB means PC completion and M-TAB means normal completion.
135 Otherwise, TAB means normal completion and M-TAB means Partial Completion."
136 :type 'boolean
137 :group 'partial-completion)
139 (defcustom PC-word-delimiters "-_. "
140 "*A string of characters treated as word delimiters for completion.
141 Some arcane rules:
142 If `]' is in this string, it must come first.
143 If `^' is in this string, it must not come first.
144 If `-' is in this string, it must come first or right after `]'.
145 In other words, if S is this string, then `[S]' must be a legal Emacs regular
146 expression (not containing character ranges like `a-z')."
147 :type 'string
148 :group 'partial-completion)
150 (defcustom PC-include-file-path '("/usr/include" "/usr/local/include")
151 "*A list of directories in which to look for include files.
152 If nil, means use the colon-separated path in the variable $INCPATH instead."
153 :type '(repeat directory)
154 :group 'partial-completion)
156 (defcustom PC-disable-wildcards nil
157 "*If non-nil, wildcard support in \\[find-file] is disabled."
158 :type 'boolean
159 :group 'partial-completion)
161 (defcustom PC-disable-includes nil
162 "*If non-nil, include-file support in \\[find-file] is disabled."
163 :type 'boolean
164 :group 'partial-completion)
166 (defvar PC-default-bindings t
167 "If non-nil, default partial completion key bindings are suppressed.")
169 (defvar PC-old-read-file-name-internal nil)
171 ;;;###autoload
172 (defun partial-completion-mode (&optional arg)
173 "Toggle Partial Completion mode.
174 With prefix ARG, turn Partial Completion mode on if ARG is positive.
176 When Partial Completion mode is enabled, TAB (or M-TAB if `PC-meta-flag' is
177 nil) is enhanced so that if some string is divided into words and each word is
178 delimited by a character in `PC-word-delimiters', partial words are completed
179 as much as possible.
181 For example, M-x p-c-b expands to M-x partial-completion-mode since no other
182 command begins with that sequence of characters, and
183 \\[find-file] f_b.c TAB might complete to foo_bar.c if that file existed and no
184 other file in that directory begin with that sequence of characters.
186 Unless `PC-disable-wildcards' is non-nil, the \"*\" wildcard is interpreted
187 specially when entering file or directory names. For example,
188 \\[find-file] *.c RET finds each C file in the currenty directory, and
189 \\[find-file] */foo_bar.c TAB completes the directory name as far as possible.
191 Unless `PC-disable-includes' is non-nil, the \"<...>\" sequence is interpreted
192 specially in \\[find-file]. For example,
193 \\[find-file] <sys/time.h> RET finds the file /usr/include/sys/time.h.
194 See also the variable `PC-include-file-path'."
195 (interactive "P")
196 (let ((on-p (if arg
197 (> (prefix-numeric-value arg) 0)
198 (not partial-completion-mode))))
199 ;; Deal with key bindings...
200 (PC-bindings on-p)
201 ;; Deal with wildcard file feature...
202 (cond ((not on-p)
203 (remove-hook 'find-file-not-found-hooks 'PC-try-load-many-files))
204 ((not PC-disable-wildcards)
205 (add-hook 'find-file-not-found-hooks 'PC-try-load-many-files)))
206 ;; Deal with include file feature...
207 (cond ((not on-p)
208 (remove-hook 'find-file-not-found-hooks 'PC-look-for-include-file))
209 ((not PC-disable-includes)
210 (add-hook 'find-file-not-found-hooks 'PC-look-for-include-file)))
211 ;; ... with some underhand redefining.
212 (cond ((and (not on-p) (functionp PC-old-read-file-name-internal))
213 (fset 'read-file-name-internal PC-old-read-file-name-internal))
214 ((and (not PC-disable-includes) (not PC-old-read-file-name-internal))
215 (setq PC-old-read-file-name-internal
216 (symbol-function 'read-file-name-internal))
217 (fset 'read-file-name-internal
218 'PC-read-include-file-name-internal)))
219 ;; Finally set the mode variable.
220 (setq partial-completion-mode on-p)))
222 (defun PC-bindings (bind)
223 (let ((completion-map minibuffer-local-completion-map)
224 (must-match-map minibuffer-local-must-match-map))
225 (cond ((not bind)
226 ;; These bindings are the default bindings. It would be better to
227 ;; restore the previous bindings.
228 (define-key completion-map "\t" 'minibuffer-complete)
229 (define-key completion-map " " 'minibuffer-complete-word)
230 (define-key completion-map "?" 'minibuffer-completion-help)
232 (define-key must-match-map "\t" 'minibuffer-complete)
233 (define-key must-match-map " " 'minibuffer-complete-word)
234 (define-key must-match-map "\r" 'minibuffer-complete-and-exit)
235 (define-key must-match-map "\n" 'minibuffer-complete-and-exit)
236 (define-key must-match-map "?" 'minibuffer-completion-help)
238 (define-key global-map "\e\t" 'complete-symbol))
239 (PC-default-bindings
240 (define-key completion-map "\t" 'PC-complete)
241 (define-key completion-map " " 'PC-complete-word)
242 (define-key completion-map "?" 'PC-completion-help)
244 (define-key completion-map "\e\t" 'PC-complete)
245 (define-key completion-map "\e " 'PC-complete-word)
246 (define-key completion-map "\e\r" 'PC-force-complete-and-exit)
247 (define-key completion-map "\e\n" 'PC-force-complete-and-exit)
248 (define-key completion-map "\e?" 'PC-completion-help)
250 (define-key must-match-map "\t" 'PC-complete)
251 (define-key must-match-map " " 'PC-complete-word)
252 (define-key must-match-map "\r" 'PC-complete-and-exit)
253 (define-key must-match-map "\n" 'PC-complete-and-exit)
254 (define-key must-match-map "?" 'PC-completion-help)
256 (define-key must-match-map "\e\t" 'PC-complete)
257 (define-key must-match-map "\e " 'PC-complete-word)
258 (define-key must-match-map "\e\r" 'PC-complete-and-exit)
259 (define-key must-match-map "\e\n" 'PC-complete-and-exit)
260 (define-key must-match-map "\e?" 'PC-completion-help)
262 (define-key global-map "\e\t" 'PC-lisp-complete-symbol)))))
264 ;; Because the `partial-completion-mode' option is defined before the
265 ;; `partial-completion-mode' command and its callee, we give the former a
266 ;; default `:initialize' keyword value. Otherwise, the `:set' keyword value
267 ;; would be called to initialise the variable value, and that would call the
268 ;; as-yet undefined `partial-completion-mode' function.
269 ;; Since the default `:initialize' keyword value (obviously) does not turn on
270 ;; Partial Completion Mode, we do that here, once the `partial-completion-mode'
271 ;; function and its callee are defined.
272 (when partial-completion-mode
273 (partial-completion-mode t))
275 (defun PC-complete ()
276 "Like minibuffer-complete, but allows \"b--di\"-style abbreviations.
277 For example, \"M-x b--di\" would match `byte-recompile-directory', or any
278 name which consists of three or more words, the first beginning with \"b\"
279 and the third beginning with \"di\".
281 The pattern \"b--d\" is ambiguous for `byte-recompile-directory' and
282 `beginning-of-defun', so this would produce a list of completions
283 just like when normal Emacs completions are ambiguous.
285 Word-delimiters for the purposes of Partial Completion are \"-\", \"_\",
286 \".\", and SPC."
287 (interactive)
288 (if (PC-was-meta-key)
289 (minibuffer-complete)
290 ;; If the previous command was not this one,
291 ;; never scroll, always retry completion.
292 (or (eq last-command this-command)
293 (setq minibuffer-scroll-window nil))
294 (let ((window minibuffer-scroll-window))
295 ;; If there's a fresh completion window with a live buffer,
296 ;; and this command is repeated, scroll that window.
297 (if (and window (window-buffer window)
298 (buffer-name (window-buffer window)))
299 (save-excursion
300 (set-buffer (window-buffer window))
301 (if (pos-visible-in-window-p (point-max) window)
302 (set-window-start window (point-min) nil)
303 (scroll-other-window)))
304 (PC-do-completion nil)))))
307 (defun PC-complete-word ()
308 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
309 See `PC-complete' for details.
310 This can be bound to other keys, like `-' and `.', if you wish."
311 (interactive)
312 (if (eq (PC-was-meta-key) PC-meta-flag)
313 (if (eq last-command-char ? )
314 (minibuffer-complete-word)
315 (self-insert-command 1))
316 (self-insert-command 1)
317 (if (eobp)
318 (PC-do-completion 'word))))
321 (defun PC-complete-space ()
322 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
323 See `PC-complete' for details.
324 This is suitable for binding to other keys which should act just like SPC."
325 (interactive)
326 (if (eq (PC-was-meta-key) PC-meta-flag)
327 (minibuffer-complete-word)
328 (insert " ")
329 (if (eobp)
330 (PC-do-completion 'word))))
333 (defun PC-complete-and-exit ()
334 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
335 See `PC-complete' for details."
336 (interactive)
337 (if (eq (PC-was-meta-key) PC-meta-flag)
338 (minibuffer-complete-and-exit)
339 (PC-do-complete-and-exit)))
341 (defun PC-force-complete-and-exit ()
342 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
343 See `PC-complete' for details."
344 (interactive)
345 (let ((minibuffer-completion-confirm nil))
346 (PC-do-complete-and-exit)))
348 (defun PC-do-complete-and-exit ()
349 (if (= (buffer-size) 0) ; Duplicate the "bug" that Info-menu relies on...
350 (exit-minibuffer)
351 (let ((flag (PC-do-completion 'exit)))
352 (and flag
353 (if (or (eq flag 'complete)
354 (not minibuffer-completion-confirm))
355 (exit-minibuffer)
356 (PC-temp-minibuffer-message " [Confirm]"))))))
359 (defun PC-completion-help ()
360 "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
361 See `PC-complete' for details."
362 (interactive)
363 (if (eq (PC-was-meta-key) PC-meta-flag)
364 (minibuffer-completion-help)
365 (PC-do-completion 'help)))
367 (defun PC-was-meta-key ()
368 (or (/= (length (this-command-keys)) 1)
369 (let ((key (aref (this-command-keys) 0)))
370 (if (integerp key)
371 (>= key 128)
372 (not (null (memq 'meta (event-modifiers key))))))))
375 (defvar PC-ignored-extensions 'empty-cache)
376 (defvar PC-delims 'empty-cache)
377 (defvar PC-ignored-regexp nil)
378 (defvar PC-word-failed-flag nil)
379 (defvar PC-delim-regex nil)
380 (defvar PC-ndelims-regex nil)
381 (defvar PC-delims-list nil)
383 (defvar PC-completion-as-file-name-predicate
384 (function
385 (lambda ()
386 (memq minibuffer-completion-table
387 '(read-file-name-internal read-directory-name-internal))))
388 "A function testing whether a minibuffer completion now will work filename-style.
389 The function takes no arguments, and typically looks at the value
390 of `minibuffer-completion-table' and the minibuffer contents.")
392 (defun PC-do-completion (&optional mode beg end)
393 (or beg (setq beg (point-min)))
394 (or end (setq end (point-max)))
395 (let* ((table minibuffer-completion-table)
396 (pred minibuffer-completion-predicate)
397 (filename (funcall PC-completion-as-file-name-predicate))
398 (dirname nil)
399 dirlength
400 (str (buffer-substring beg end))
401 (incname (and filename (string-match "<\\([^\"<>]*\\)>?$" str)))
402 (ambig nil)
403 basestr
404 regex
405 p offset
406 (poss nil)
407 helpposs
408 (case-fold-search completion-ignore-case))
410 ;; Check if buffer contents can already be considered complete
411 (if (and (eq mode 'exit)
412 (PC-is-complete-p str table pred))
413 'complete
415 ;; Record how many characters at the beginning are not included
416 ;; in completion.
417 (setq dirlength
418 (if filename
419 (length (file-name-directory str))
422 ;; Do substitutions in directory names
423 (and filename
424 (not (equal str (setq p (substitute-in-file-name str))))
425 (progn
426 (delete-region beg end)
427 (insert p)
428 (setq str p end (+ beg (length str)))))
430 ;; Prepare various delimiter strings
431 (or (equal PC-word-delimiters PC-delims)
432 (setq PC-delims PC-word-delimiters
433 PC-delim-regex (concat "[" PC-delims "]")
434 PC-ndelims-regex (concat "[^" PC-delims "]*")
435 PC-delims-list (append PC-delims nil)))
437 ;; Look for wildcard expansions in directory name
438 (and filename
439 (string-match "\\*.*/" str)
440 (let ((pat str)
441 files)
442 (setq p (1+ (string-match "/[^/]*\\'" pat)))
443 (while (setq p (string-match PC-delim-regex pat p))
444 (setq pat (concat (substring pat 0 p)
446 (substring pat p))
447 p (+ p 2)))
448 (setq files (PC-expand-many-files (concat pat "*")))
449 (if files
450 (let ((dir (file-name-directory (car files)))
451 (p files))
452 (while (and (setq p (cdr p))
453 (equal dir (file-name-directory (car p)))))
454 (if p
455 (setq filename nil table nil pred nil
456 ambig t)
457 (delete-region beg end)
458 (setq str (concat dir (file-name-nondirectory str)))
459 (insert str)
460 (setq end (+ beg (length str)))))
461 (setq filename nil table nil pred nil))))
463 ;; Strip directory name if appropriate
464 (if filename
465 (if incname
466 (setq basestr (substring str incname)
467 dirname (substring str 0 incname))
468 (setq basestr (file-name-nondirectory str)
469 dirname (file-name-directory str)))
470 (setq basestr str))
472 ;; Convert search pattern to a standard regular expression
473 (setq regex (regexp-quote basestr)
474 offset (if (and (> (length regex) 0)
475 (not (eq (aref basestr 0) ?\*))
476 (or (eq PC-first-char t)
477 (and PC-first-char filename))) 1 0)
478 p offset)
479 (while (setq p (string-match PC-delim-regex regex p))
480 (if (eq (aref regex p) ? )
481 (setq regex (concat (substring regex 0 p)
482 PC-ndelims-regex
483 PC-delim-regex
484 (substring regex (1+ p)))
485 p (+ p (length PC-ndelims-regex) (length PC-delim-regex)))
486 (let ((bump (if (memq (aref regex p)
487 '(?$ ?^ ?\. ?* ?+ ?? ?[ ?] ?\\))
488 -1 0)))
489 (setq regex (concat (substring regex 0 (+ p bump))
490 PC-ndelims-regex
491 (substring regex (+ p bump)))
492 p (+ p (length PC-ndelims-regex) 1)))))
493 (setq p 0)
494 (if filename
495 (while (setq p (string-match "\\\\\\*" regex p))
496 (setq regex (concat (substring regex 0 p)
497 "[^/]*"
498 (substring regex (+ p 2))))))
499 ;;(setq the-regex regex)
500 (setq regex (concat "\\`" regex))
502 ;; Find an initial list of possible completions
503 (if (not (setq p (string-match (concat PC-delim-regex
504 (if filename "\\|\\*" ""))
506 (+ (length dirname) offset))))
508 ;; Minibuffer contains no hyphens -- simple case!
509 (setq poss (all-completions str
510 table
511 pred))
513 ;; Use all-completions to do an initial cull. This is a big win,
514 ;; since all-completions is written in C!
515 (let ((compl (all-completions (substring str 0 p)
516 table
517 pred)))
518 (setq p compl)
519 (while p
520 (and (string-match regex (car p))
521 (progn
522 (set-text-properties 0 (length (car p)) '() (car p))
523 (setq poss (cons (car p) poss))))
524 (setq p (cdr p)))))
526 ;; Now we have a list of possible completions
527 (cond
529 ;; No valid completions found
530 ((null poss)
531 (if (and (eq mode 'word)
532 (not PC-word-failed-flag))
533 (let ((PC-word-failed-flag t))
534 (delete-backward-char 1)
535 (PC-do-completion 'word))
536 (beep)
537 (PC-temp-minibuffer-message (if ambig
538 " [Ambiguous dir name]"
539 (if (eq mode 'help)
540 " [No completions]"
541 " [No match]")))
542 nil))
544 ;; More than one valid completion found
545 ((or (cdr (setq helpposs poss))
546 (memq mode '(help word)))
548 ;; Handle completion-ignored-extensions
549 (and filename
550 (not (eq mode 'help))
551 (let ((p2 poss))
553 ;; Build a regular expression representing the extensions list
554 (or (equal completion-ignored-extensions PC-ignored-extensions)
555 (setq PC-ignored-regexp
556 (concat "\\("
557 (mapconcat
558 'regexp-quote
559 (setq PC-ignored-extensions
560 completion-ignored-extensions)
561 "\\|")
562 "\\)\\'")))
564 ;; Check if there are any without an ignored extension
565 (setq p nil)
566 (while p2
567 (or (string-match PC-ignored-regexp (car p2))
568 (setq p (cons (car p2) p)))
569 (setq p2 (cdr p2)))
571 ;; If there are "good" names, use them
572 (and p (setq poss p))))
574 ;; Is the actual string one of the possible completions?
575 (setq p (and (not (eq mode 'help)) poss))
576 (while (and p
577 (not (string-equal (car p) basestr)))
578 (setq p (cdr p)))
579 (and p (null mode)
580 (PC-temp-minibuffer-message " [Complete, but not unique]"))
581 (if (and p
582 (not (and (null mode)
583 (eq this-command last-command))))
586 ;; If ambiguous, try for a partial completion
587 (let ((improved nil)
588 prefix
589 (pt nil)
590 (skip "\\`"))
592 ;; Check if next few letters are the same in all cases
593 (if (and (not (eq mode 'help))
594 (setq prefix (try-completion "" (mapcar 'list poss))))
595 (let ((first t) i)
596 (if (eq mode 'word)
597 (setq prefix (PC-chop-word prefix basestr)))
598 (goto-char (+ beg (length dirname)))
599 (while (and (progn
600 (setq i 0)
601 (while (< i (length prefix))
602 (if (and (< (point) end)
603 (eq (aref prefix i)
604 (following-char)))
605 (forward-char 1)
606 (if (and (< (point) end)
607 (or (and (looking-at " ")
608 (memq (aref prefix i)
609 PC-delims-list))
610 (eq (downcase (aref prefix i))
611 (downcase
612 (following-char)))))
613 (progn
614 (delete-char 1)
615 (setq end (1- end)))
616 (and filename (looking-at "\\*")
617 (progn
618 (delete-char 1)
619 (setq end (1- end))))
620 (setq improved t))
621 (insert (substring prefix i (1+ i)))
622 (setq end (1+ end)))
623 (setq i (1+ i)))
624 (or pt (equal (point) beg)
625 (setq pt (point)))
626 (looking-at PC-delim-regex))
627 (setq skip (concat skip
628 (regexp-quote prefix)
629 PC-ndelims-regex)
630 prefix (try-completion
632 (mapcar
633 (function
634 (lambda (x)
635 (list
636 (and (string-match skip x)
637 (substring
639 (match-end 0))))))
640 poss)))
641 (or (> i 0) (> (length prefix) 0))
642 (or (not (eq mode 'word))
643 (and first (> (length prefix) 0)
644 (setq first nil
645 prefix (substring prefix 0 1))))))
646 (goto-char (if (eq mode 'word) end
647 (or pt beg)))))
649 (if (and (eq mode 'word)
650 (not PC-word-failed-flag))
652 (if improved
654 ;; We changed it... would it be complete without the space?
655 (if (PC-is-complete-p (buffer-substring 1 (1- end))
656 table pred)
657 (delete-region (1- end) end)))
659 (if improved
661 ;; We changed it... enough to be complete?
662 (and (eq mode 'exit)
663 (PC-is-complete-p (buffer-string) table pred))
665 ;; If totally ambiguous, display a list of completions
666 (if (or completion-auto-help
667 (eq mode 'help))
668 (with-output-to-temp-buffer "*Completions*"
669 (display-completion-list (sort helpposs 'string-lessp))
670 (save-excursion
671 (set-buffer standard-output)
672 ;; Record which part of the buffer we are completing
673 ;; so that choosing a completion from the list
674 ;; knows how much old text to replace.
675 (setq completion-base-size dirlength)))
676 (PC-temp-minibuffer-message " [Next char not unique]"))
677 nil)))))
679 ;; Only one possible completion
681 (if (equal basestr (car poss))
682 (if (null mode)
683 (PC-temp-minibuffer-message " [Sole completion]"))
684 (delete-region beg end)
685 (insert (format "%s"
686 (if filename
687 (substitute-in-file-name (concat dirname (car poss)))
688 (car poss)))))
689 t)))))
692 (defun PC-is-complete-p (str table pred)
693 (let ((res (if (listp table)
694 (assoc str table)
695 (if (vectorp table)
696 (or (equal str "nil") ; heh, heh, heh
697 (intern-soft str table))
698 (funcall table str pred 'lambda)))))
699 (and res
700 (or (not pred)
701 (and (not (listp table)) (not (vectorp table)))
702 (funcall pred res))
703 res)))
705 (defun PC-chop-word (new old)
706 (let ((i -1)
707 (j -1))
708 (while (and (setq i (string-match PC-delim-regex old (1+ i)))
709 (setq j (string-match PC-delim-regex new (1+ j)))))
710 (if (and j
711 (or (not PC-word-failed-flag)
712 (setq j (string-match PC-delim-regex new (1+ j)))))
713 (substring new 0 (1+ j))
714 new)))
716 (defvar PC-not-minibuffer nil)
718 (defun PC-temp-minibuffer-message (message)
719 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
720 (cond (PC-not-minibuffer
721 (message message)
722 (sit-for 2)
723 (message ""))
724 ((fboundp 'temp-minibuffer-message)
725 (temp-minibuffer-message message))
727 (let ((point-max (point-max)))
728 (save-excursion
729 (goto-char point-max)
730 (insert message))
731 (let ((inhibit-quit t))
732 (sit-for 2)
733 (delete-region point-max (point-max))
734 (when quit-flag
735 (setq quit-flag nil
736 unread-command-events '(7))))))))
739 (defun PC-lisp-complete-symbol ()
740 "Perform completion on Lisp symbol preceding point.
741 That symbol is compared against the symbols that exist
742 and any additional characters determined by what is there
743 are inserted.
744 If the symbol starts just after an open-parenthesis,
745 only symbols with function definitions are considered.
746 Otherwise, all symbols with function definitions, values
747 or properties are considered."
748 (interactive)
749 (let* ((end (point))
750 (buffer-syntax (syntax-table))
751 (beg (unwind-protect
752 (save-excursion
753 (if lisp-mode-syntax-table
754 (set-syntax-table lisp-mode-syntax-table))
755 (backward-sexp 1)
756 (while (= (char-syntax (following-char)) ?\')
757 (forward-char 1))
758 (point))
759 (set-syntax-table buffer-syntax)))
760 (minibuffer-completion-table obarray)
761 (minibuffer-completion-predicate
762 (if (eq (char-after (1- beg)) ?\()
763 'fboundp
764 (function (lambda (sym)
765 (or (boundp sym) (fboundp sym)
766 (symbol-plist sym))))))
767 (PC-not-minibuffer t))
768 (PC-do-completion nil beg end)))
771 ;;; Wildcards in `C-x C-f' command. This is independent from the main
772 ;;; completion code, except for `PC-expand-many-files' which is called
773 ;;; when "*"'s are found in the path during filename completion. (The
774 ;;; above completion code always understands "*"'s, except in file paths,
775 ;;; without relying on the following code.)
777 (defvar PC-many-files-list nil)
779 (defun PC-try-load-many-files ()
780 (if (string-match "\\*" buffer-file-name)
781 (let* ((pat buffer-file-name)
782 (files (PC-expand-many-files pat))
783 (first (car files))
784 (next files))
785 (kill-buffer (current-buffer))
786 (or files
787 (error "No matching files"))
788 ;; Bring the other files (not the first) into buffers.
789 (save-window-excursion
790 (while (setq next (cdr next))
791 (let ((buf (find-file-noselect (car next))))
792 ;; Put this buffer at the front of the buffer list.
793 (switch-to-buffer buf))))
794 ;; This modifies the `buf' variable inside find-file-noselect.
795 (setq buf (get-file-buffer first))
796 (if buf
797 nil ; should do verify-visited-file-modtime stuff.
798 (setq filename first)
799 (setq buf (create-file-buffer filename))
800 ;; This modified `truename' inside find-file-noselect.
801 (setq truename (abbreviate-file-name (file-truename filename)))
802 (set-buffer buf)
803 (erase-buffer)
804 (insert-file-contents filename t))
805 (if (cdr files)
806 (setq PC-many-files-list (mapconcat
807 (if (string-match "\\*.*/" pat)
808 'identity
809 'file-name-nondirectory)
810 (cdr files) ", ")
811 find-file-hooks (cons 'PC-after-load-many-files
812 find-file-hooks)))
813 ;; This modifies the "error" variable inside find-file-noselect.
814 (setq error nil)
816 nil))
818 (defun PC-after-load-many-files ()
819 (setq find-file-hooks (delq 'PC-after-load-many-files find-file-hooks))
820 (message "Also loaded %s." PC-many-files-list))
822 (defun PC-expand-many-files (name)
823 (save-excursion
824 (set-buffer (generate-new-buffer " *Glob Output*"))
825 (erase-buffer)
826 (shell-command (concat "echo " name) t)
827 (goto-char (point-min))
828 (if (looking-at ".*No match")
830 (insert "(\"")
831 (while (search-forward " " nil t)
832 (delete-backward-char 1)
833 (insert "\" \""))
834 (goto-char (point-max))
835 (delete-backward-char 1)
836 (insert "\")")
837 (goto-char (point-min))
838 (let ((files (read (current-buffer))))
839 (kill-buffer (current-buffer))
840 files))))
842 ;;; Facilities for loading C header files. This is independent from the
843 ;;; main completion code. See also the variable `PC-include-file-path'
844 ;;; at top of this file.
846 (defun PC-look-for-include-file ()
847 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
848 (let ((name (substring (buffer-file-name)
849 (match-beginning 1) (match-end 1)))
850 (punc (aref (buffer-file-name) (match-beginning 0)))
851 (path nil)
852 new-buf)
853 (kill-buffer (current-buffer))
854 (if (equal name "")
855 (save-excursion
856 (set-buffer (car (buffer-list)))
857 (save-excursion
858 (beginning-of-line)
859 (if (looking-at
860 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
861 (setq name (buffer-substring (match-beginning 1)
862 (match-end 1))
863 punc (char-after (1- (match-beginning 1))))
864 ;; Suggested by Frank Siebenlist:
865 (if (or (looking-at
866 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
867 (looking-at
868 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
869 (looking-at
870 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
871 (progn
872 (setq name (buffer-substring (match-beginning 1)
873 (match-end 1))
874 punc ?\<
875 path load-path)
876 (if (string-match "\\.elc$" name)
877 (setq name (substring name 0 -1))
878 (or (string-match "\\.el$" name)
879 (setq name (concat name ".el")))))
880 (error "Not on an #include line"))))))
881 (or (string-match "\\.[a-zA-Z0-9]+$" name)
882 (setq name (concat name ".h")))
883 (if (eq punc ?\<)
884 (let ((path (or path (PC-include-file-path))))
885 (while (and path
886 (not (file-exists-p
887 (concat (file-name-as-directory (car path))
888 name))))
889 (setq path (cdr path)))
890 (if path
891 (setq name (concat (file-name-as-directory (car path)) name))
892 (error "No such include file: <%s>" name)))
893 (let ((dir (save-excursion
894 (set-buffer (car (buffer-list)))
895 default-directory)))
896 (if (file-exists-p (concat dir name))
897 (setq name (concat dir name))
898 (error "No such include file: \"%s\"" name))))
899 (setq new-buf (get-file-buffer name))
900 (if new-buf
901 ;; no need to verify last-modified time for this!
902 (set-buffer new-buf)
903 (setq new-buf (create-file-buffer name))
904 (set-buffer new-buf)
905 (erase-buffer)
906 (insert-file-contents name t))
907 (setq filename name
908 error nil
909 buf new-buf)
911 nil))
913 (defun PC-include-file-path ()
914 (or PC-include-file-path
915 (let ((env (getenv "INCPATH"))
916 (path nil)
917 pos)
918 (or env (error "No include file path specified"))
919 (while (setq pos (string-match ":[^:]+$" env))
920 (setq path (cons (substring env (1+ pos)) path)
921 env (substring env 0 pos)))
922 path)))
924 ;;; This is adapted from lib-complete.el, by Mike Williams.
925 (defun PC-include-file-all-completions (file search-path &optional full)
926 "Return all completions for FILE in any directory on SEARCH-PATH.
927 If optional third argument FULL is non-nil, returned pathnames should be
928 absolute rather than relative to some directory on the SEARCH-PATH."
929 (setq search-path
930 (mapcar '(lambda (dir)
931 (if dir (file-name-as-directory dir) default-directory))
932 search-path))
933 (if (file-name-absolute-p file)
934 ;; It's an absolute file name, so don't need search-path
935 (progn
936 (setq file (expand-file-name file))
937 (file-name-all-completions
938 (file-name-nondirectory file) (file-name-directory file)))
939 (let ((subdir (file-name-directory file))
940 (ndfile (file-name-nondirectory file))
941 file-lists)
942 ;; Append subdirectory part to each element of search-path
943 (if subdir
944 (setq search-path
945 (mapcar '(lambda (dir) (concat dir subdir))
946 search-path)
947 file ))
948 ;; Make list of completions in each directory on search-path
949 (while search-path
950 (let* ((dir (car search-path))
951 (subdir (if full dir subdir)))
952 (if (file-directory-p dir)
953 (progn
954 (setq file-lists
955 (cons
956 (mapcar '(lambda (file) (concat subdir file))
957 (file-name-all-completions ndfile
958 (car search-path)))
959 file-lists))))
960 (setq search-path (cdr search-path))))
961 ;; Compress out duplicates while building complete list (slloooow!)
962 (let ((sorted (sort (apply 'nconc file-lists)
963 '(lambda (x y) (not (string-lessp x y)))))
964 compressed)
965 (while sorted
966 (if (equal (car sorted) (car compressed)) nil
967 (setq compressed (cons (car sorted) compressed)))
968 (setq sorted (cdr sorted)))
969 compressed))))
971 (defun PC-read-include-file-name-internal (string dir action)
972 (if (string-match "<\\([^\"<>]*\\)>?$" string)
973 (let* ((name (substring string (match-beginning 1) (match-end 1)))
974 (str2 (substring string (match-beginning 0)))
975 (completion-table
976 (mapcar (function (lambda (x) (list (format "<%s>" x))))
977 (PC-include-file-all-completions
978 name (PC-include-file-path)))))
979 (cond
980 ((not completion-table) nil)
981 ((eq action nil) (try-completion str2 completion-table nil))
982 ((eq action t) (all-completions str2 completion-table nil))
983 ((eq action 'lambda)
984 (eq (try-completion str2 completion-table nil) t))))
985 (funcall PC-old-read-file-name-internal string dir action)))
988 (provide 'complete)
990 ;;; End.