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