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>
7 ;; Special thanks to Hallvard Furuseth for his many ideas and contributions.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
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
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: Load this file. 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
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.
101 (defvar PC-meta-flag t
102 "*If nil, TAB does normal Emacs completion and M-TAB does Partial Completion.
103 If t, TAB does Partial Completion and M-TAB does normal completion.")
106 (defvar PC-word-delimiters
"-_. "
107 "*A string of characters which are to be treated as word delimiters
108 by the Partial Completion system.
110 Some arcane rules: If `]' is in this string it must come first.
111 If `^' is in this string it must NOT come first. If `-' is in this
112 string, it must come first or right after `]'. In other words, if
113 S is this string, then `[S]' must be a legal Emacs regular expression
114 (not containing character ranges like `a-z').")
117 (defvar PC-first-char
'x
118 "*If t, first character of a string to be completed is always taken literally.
119 If nil, word delimiters are handled even if they appear as first character.
120 This controls whether \".e\" matches \".e*\" (t) or \"*.e*\" (nil).
121 If neither nil nor t, first char is literal only for filename completion.")
124 (defvar PC-include-file-path
'("/usr/include")
125 "*List of directories in which to look for include files.
126 If this is nil, uses the colon-separated path in $INCPATH instead.")
129 (defvar PC-disable-wildcards nil
130 "Set this to non-nil to disable wildcard support in \\[find-file].")
132 (defvar PC-disable-includes nil
133 "Set this to non-nil to disable include-file support in \\[find-file].")
136 (defvar PC-default-bindings t
137 "Set this to nil to suppress the default partial completion key bindings.")
139 (if PC-default-bindings
(progn
140 (define-key minibuffer-local-completion-map
"\t" 'PC-complete
)
141 (define-key minibuffer-local-completion-map
" " 'PC-complete-word
)
142 (define-key minibuffer-local-completion-map
"?" 'PC-completion-help
)
144 (define-key minibuffer-local-completion-map
"\e\t" 'PC-complete
)
145 (define-key minibuffer-local-completion-map
"\e " 'PC-complete-word
)
146 (define-key minibuffer-local-completion-map
"\e\r" 'PC-force-complete-and-exit
)
147 (define-key minibuffer-local-completion-map
"\e\n" 'PC-force-complete-and-exit
)
148 (define-key minibuffer-local-completion-map
"\e?" 'PC-completion-help
)
150 (define-key minibuffer-local-must-match-map
"\t" 'PC-complete
)
151 (define-key minibuffer-local-must-match-map
" " 'PC-complete-word
)
152 (define-key minibuffer-local-must-match-map
"\r" 'PC-complete-and-exit
)
153 (define-key minibuffer-local-must-match-map
"\n" 'PC-complete-and-exit
)
154 (define-key minibuffer-local-must-match-map
"?" 'PC-completion-help
)
156 (define-key minibuffer-local-must-match-map
"\e\t" 'PC-complete
)
157 (define-key minibuffer-local-must-match-map
"\e " 'PC-complete-word
)
158 (define-key minibuffer-local-must-match-map
"\e\r" 'PC-complete-and-exit
)
159 (define-key minibuffer-local-must-match-map
"\e\n" 'PC-complete-and-exit
)
160 (define-key minibuffer-local-must-match-map
"\e?" 'PC-completion-help
)
162 (define-key global-map
"\e\t" 'PC-lisp-complete-symbol
)
166 (defun PC-complete ()
167 "Like minibuffer-complete, but allows \"b--di\"-style abbreviations.
168 For example, \"M-x b--di\" would match `byte-recompile-directory', or any
169 name which consists of three or more words, the first beginning with \"b\"
170 and the third beginning with \"di\".
172 The pattern \"b--d\" is ambiguous for `byte-recompile-directory' and
173 `beginning-of-defun', so this would produce a list of completions
174 just like when normal Emacs completions are ambiguous.
176 Word-delimiters for the purposes of Partial Completion are \"-\", \"_\",
179 (if (PC-was-meta-key)
180 (minibuffer-complete)
181 (PC-do-completion nil
)))
184 (defun PC-complete-word ()
185 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
186 See `PC-complete' for details.
187 This can be bound to other keys, like `-' and `.', if you wish."
189 (if (eq (PC-was-meta-key) PC-meta-flag
)
190 (if (eq last-command-char ?
)
191 (minibuffer-complete-word)
192 (self-insert-command 1))
193 (self-insert-command 1)
195 (PC-do-completion 'word
))))
198 (defun PC-complete-space ()
199 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
200 See `PC-complete' for details.
201 This is suitable for binding to other keys which should act just like SPC."
203 (if (eq (PC-was-meta-key) PC-meta-flag
)
204 (minibuffer-complete-word)
207 (PC-do-completion 'word
))))
210 (defun PC-complete-and-exit ()
211 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
212 See `PC-complete' for details."
214 (if (eq (PC-was-meta-key) PC-meta-flag
)
215 (minibuffer-complete-and-exit)
216 (PC-do-complete-and-exit)))
218 (defun PC-force-complete-and-exit ()
219 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
220 See `PC-complete' for details."
222 (let ((minibuffer-completion-confirm nil
))
223 (PC-do-complete-and-exit)))
225 (defun PC-do-complete-and-exit ()
226 (if (= (buffer-size) 0) ; Duplicate the "bug" that Info-menu relies on...
228 (let ((flag (PC-do-completion 'exit
)))
230 (if (or (eq flag
'complete
)
231 (not minibuffer-completion-confirm
))
233 (PC-temp-minibuffer-message " (Confirm)"))))))
236 (defun PC-completion-help ()
237 "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
238 See `PC-complete' for details."
240 (if (eq (PC-was-meta-key) PC-meta-flag
)
241 (minibuffer-completion-help)
242 (PC-do-completion 'help
)))
244 (defun PC-was-meta-key ()
245 (or (/= (length (this-command-keys)) 1)
246 (let ((key (aref (this-command-keys) 0)))
249 (not (null (memq 'meta
(event-modifiers key
))))))))
252 (defvar PC-ignored-extensions
'empty-cache
)
253 (defvar PC-delims
'empty-cache
)
254 (defvar PC-ignored-regexp nil
)
255 (defvar PC-word-failed-flag nil
)
256 (defvar PC-delim-regex nil
)
257 (defvar PC-ndelims-regex nil
)
258 (defvar PC-delims-list nil
)
260 (defun PC-do-completion (&optional mode beg end
)
261 (or beg
(setq beg
(point-min)))
262 (or end
(setq end
(point-max)))
263 (let* ((table minibuffer-completion-table
)
264 (pred minibuffer-completion-predicate
)
265 (filename (memq table
'(read-file-name-internal
266 read-directory-name-internal
)))
268 (str (buffer-substring beg end
))
269 (incname (and filename
(string-match "<\\([^\"<>]*\\)>?$" str
)))
276 (case-fold-search completion-ignore-case
))
278 ;; Check if buffer contents can already be considered complete
279 (if (and (eq mode
'exit
)
280 (PC-is-complete-p str table pred
))
283 ;; Do substitutions in directory names
285 (not (equal str
(setq p
(substitute-in-file-name str
))))
287 (delete-region beg end
)
289 (setq str p end
(+ beg
(length str
)))))
291 ;; Prepare various delimiter strings
292 (or (equal PC-word-delimiters PC-delims
)
293 (setq PC-delims PC-word-delimiters
294 PC-delim-regex
(concat "[" PC-delims
"]")
295 PC-ndelims-regex
(concat "[^" PC-delims
"]*")
296 PC-delims-list
(append PC-delims nil
)))
298 ;; Look for wildcard expansions in directory name
300 (string-match "\\*.*/" str
)
303 (setq p
(1+ (string-match "/[^/]*\\'" pat
)))
304 (while (setq p
(string-match PC-delim-regex pat p
))
305 (setq pat
(concat (substring pat
0 p
)
309 (setq files
(PC-expand-many-files (concat pat
"*")))
311 (let ((dir (file-name-directory (car files
)))
313 (while (and (setq p
(cdr p
))
314 (equal dir
(file-name-directory (car p
)))))
316 (setq filename nil table nil pred nil
318 (delete-region beg end
)
319 (setq str
(concat dir
(file-name-nondirectory str
)))
321 (setq end
(+ beg
(length str
)))))
322 (setq filename nil table nil pred nil
))))
324 ;; Strip directory name if appropriate
327 (setq basestr
(substring str incname
)
328 dirname
(substring str
0 incname
))
329 (setq basestr
(file-name-nondirectory str
)
330 dirname
(file-name-directory str
)))
333 ;; Convert search pattern to a standard regular expression
334 (setq regex
(regexp-quote basestr
)
335 offset
(if (and (> (length regex
) 0)
336 (not (eq (aref basestr
0) ?\
*))
337 (or (eq PC-first-char t
)
338 (and PC-first-char filename
))) 1 0)
340 (while (setq p
(string-match PC-delim-regex regex p
))
341 (if (eq (aref regex p
) ?
)
342 (setq regex
(concat (substring regex
0 p
)
345 (substring regex
(1+ p
)))
346 p
(+ p
(length PC-ndelims-regex
) (length PC-delim-regex
)))
347 (let ((bump (if (memq (aref regex p
)
348 '(?$ ?^ ?\. ?
* ?
+ ?? ?
[ ?
] ?
\\))
350 (setq regex
(concat (substring regex
0 (+ p bump
))
352 (substring regex
(+ p bump
)))
353 p
(+ p
(length PC-ndelims-regex
) 1)))))
356 (while (setq p
(string-match "\\\\\\*" regex p
))
357 (setq regex
(concat (substring regex
0 p
)
359 (substring regex
(+ p
2))))))
360 ;;(setq the-regex regex)
361 (setq regex
(concat "\\`" regex
))
363 ;; Find an initial list of possible completions
364 (if (not (setq p
(string-match (concat PC-delim-regex
365 (if filename
"\\|\\*" ""))
367 (+ (length dirname
) offset
))))
369 ;; Minibuffer contains no hyphens -- simple case!
370 (setq poss
(all-completions str
374 ;; Use all-completions to do an initial cull. This is a big win,
375 ;; since all-completions is written in C!
376 (let ((compl (all-completions (substring str
0 p
)
381 (and (string-match regex
(car p
))
382 (setq poss
(cons (car p
) poss
)))
385 ;; Now we have a list of possible completions
388 ;; No valid completions found
390 (if (and (eq mode
'word
)
391 (not PC-word-failed-flag
))
392 (let ((PC-word-failed-flag t
))
393 (delete-backward-char 1)
394 (PC-do-completion 'word
))
396 (PC-temp-minibuffer-message (if ambig
397 " (Ambiguous dir name)"
403 ;; More than one valid completion found
404 ((or (cdr (setq helpposs poss
))
405 (memq mode
'(help word
)))
407 ;; Handle completion-ignored-extensions
409 (not (eq mode
'help
))
412 ;; Build a regular expression representing the extensions list
413 (or (equal completion-ignored-extensions PC-ignored-extensions
)
414 (setq PC-ignored-regexp
418 (setq PC-ignored-extensions
419 completion-ignored-extensions
)
423 ;; Check if there are any without an ignored extension
426 (or (string-match PC-ignored-regexp
(car p2
))
427 (setq p
(cons (car p2
) p
)))
430 ;; If there are "good" names, use them
431 (and p
(setq poss p
))))
433 ;; Is the actual string one of the possible completions?
434 (setq p
(and (not (eq mode
'help
)) poss
))
436 (not (equal (car p
) basestr
)))
442 (PC-temp-minibuffer-message " (Complete, but not unique)"))
445 ;; If ambiguous, try for a partial completion
451 ;; Check if next few letters are the same in all cases
452 (if (and (not (eq mode
'help
))
453 (setq prefix
(try-completion "" (mapcar 'list poss
))))
456 (setq prefix
(PC-chop-word prefix basestr
)))
457 (goto-char (+ beg
(length dirname
)))
460 (while (< i
(length prefix
))
461 (if (and (< (point) end
)
465 (if (and (< (point) end
)
466 (or (and (looking-at " ")
467 (memq (aref prefix i
)
469 (eq (downcase (aref prefix i
))
475 (and filename
(looking-at "\\*")
478 (setq end
(1- end
))))
480 (insert (substring prefix i
(1+ i
)))
483 (or pt
(equal (point) beg
)
485 (looking-at PC-delim-regex
))
486 (setq skip
(concat skip
487 (regexp-quote prefix
)
489 prefix
(try-completion
495 (and (string-match skip x
)
500 (or (> i
0) (> (length prefix
) 0))
501 (or (not (eq mode
'word
))
502 (and first
(> (length prefix
) 0)
504 prefix
(substring prefix
0 1))))))
505 (goto-char (if (eq mode
'word
) end
508 (if (and (eq mode
'word
)
509 (not PC-word-failed-flag
))
513 ;; We changed it... would it be complete without the space?
514 (if (PC-is-complete-p (buffer-substring 1 (1- end
))
516 (delete-region (1- end
) end
)))
520 ;; We changed it... enough to be complete?
522 (PC-is-complete-p (buffer-string) table pred
))
524 ;; If totally ambiguous, display a list of completions
525 (if (or completion-auto-help
527 (with-output-to-temp-buffer " *Completions*"
528 (display-completion-list (sort helpposs
'string-lessp
)))
529 (PC-temp-minibuffer-message " (Next char not unique)"))
532 ;; Only one possible completion
534 (if (equal basestr
(car poss
))
536 (PC-temp-minibuffer-message " (Sole completion)"))
537 (delete-region beg end
)
539 (substitute-in-file-name (concat dirname
(car poss
)))
544 (defun PC-is-complete-p (str table pred
)
545 (let ((res (if (listp table
)
548 (or (equal str
"nil") ; heh, heh, heh
549 (intern-soft str table
))
550 (funcall table str pred
'lambda
)))))
553 (and (not (listp table
)) (not (vectorp table
)))
557 (defun PC-chop-word (new old
)
560 (while (and (setq i
(string-match PC-delim-regex old
(1+ i
)))
561 (setq j
(string-match PC-delim-regex new
(1+ j
)))))
563 (or (not PC-word-failed-flag
)
564 (setq j
(string-match PC-delim-regex new
(1+ j
)))))
565 (substring new
0 (1+ j
))
568 (defvar PC-not-minibuffer nil
)
570 (defun PC-temp-minibuffer-message (m)
571 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
572 (if PC-not-minibuffer
577 (if (fboundp 'temp-minibuffer-message
)
578 (temp-minibuffer-message m
)
579 (let ((savemax (point-max)))
581 (goto-char (point-max))
583 (let ((inhibit-quit t
))
585 (delete-region savemax
(point-max))
588 unread-command-char
7)))))))
591 (defun PC-lisp-complete-symbol ()
592 "Perform completion on Lisp symbol preceding point.
593 That symbol is compared against the symbols that exist
594 and any additional characters determined by what is there
596 If the symbol starts just after an open-parenthesis,
597 only symbols with function definitions are considered.
598 Otherwise, all symbols with function definitions, values
599 or properties are considered."
602 (buffer-syntax (syntax-table))
605 (if lisp-mode-syntax-table
606 (set-syntax-table lisp-mode-syntax-table
))
608 (while (= (char-syntax (following-char)) ?
\')
611 (set-syntax-table buffer-syntax
)))
612 (minibuffer-completion-table obarray
)
613 (minibuffer-completion-predicate
614 (if (eq (char-after (1- beg
)) ?\
()
616 (function (lambda (sym)
617 (or (boundp sym
) (fboundp sym
)
618 (symbol-plist sym
))))))
619 (PC-not-minibuffer t
))
620 (PC-do-completion nil beg end
)))
623 ;;; Wildcards in `C-x C-f' command. This is independent from the main
624 ;;; completion code, except for `PC-expand-many-files' which is called
625 ;;; when "*"'s are found in the path during filename completion. (The
626 ;;; above completion code always understands "*"'s, except in file paths,
627 ;;; without relying on the following code.)
629 (defvar PC-many-files-list nil
)
631 (defun PC-try-load-many-files ()
632 (if (string-match "\\*" buffer-file-name
)
633 (let* ((pat buffer-file-name
)
634 (files (PC-expand-many-files pat
))
637 (kill-buffer (current-buffer))
639 (error "No matching files"))
640 (save-window-excursion
641 (while (setq next
(cdr next
))
642 (let ((buf (find-file-noselect (car next
))))
643 (switch-to-buffer buf
))))
644 ;; This modifies the "buf" variable inside find-file-noselect.
645 (setq buf
(get-file-buffer first
))
647 nil
; should do verify-visited-file-modtime stuff.
648 (setq filename first
)
649 (setq buf
(create-file-buffer filename
))
652 (insert-file-contents filename t
))
654 (setq PC-many-files-list
(mapconcat
655 (if (string-match "\\*.*/" pat
)
657 'file-name-nondirectory
)
659 find-file-hooks
(cons 'PC-after-load-many-files
661 ;; This modifies the "error" variable inside find-file-noselect.
666 (defun PC-after-load-many-files ()
667 (setq find-file-hooks
(delq 'PC-after-load-many-files find-file-hooks
))
668 (message "Also loaded %s." PC-many-files-list
))
670 (defun PC-expand-many-files (name)
672 (set-buffer (generate-new-buffer " *Glob Output*"))
674 (shell-command (concat "echo " name
) t
)
675 (goto-char (point-min))
676 (if (looking-at ".*No match")
679 (while (search-forward " " nil t
)
680 (delete-backward-char 1)
682 (goto-char (point-max))
683 (delete-backward-char 1)
685 (goto-char (point-min))
686 (let ((files (read (current-buffer))))
687 (kill-buffer (current-buffer))
690 (or PC-disable-wildcards
691 (memq 'PC-try-load-many-files find-file-not-found-hooks
)
692 (setq find-file-not-found-hooks
(cons 'PC-try-load-many-files
693 find-file-not-found-hooks
)))
697 ;;; Facilities for loading C header files. This is independent from the
698 ;;; main completion code. See also the variable `PC-include-file-path'
699 ;;; at top of this file.
701 (defun PC-look-for-include-file ()
702 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
703 (let ((name (substring (buffer-file-name)
704 (match-beginning 1) (match-end 1)))
705 (punc (aref (buffer-file-name) (match-beginning 0)))
708 (kill-buffer (current-buffer))
711 (set-buffer (car (buffer-list)))
715 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
716 (setq name
(buffer-substring (match-beginning 1)
718 punc
(char-after (1- (match-beginning 1))))
719 ;; Suggested by Frank Siebenlist:
721 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
723 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
725 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
727 (setq name
(buffer-substring (match-beginning 1)
731 (if (string-match "\\.elc$" name
)
732 (setq name
(substring name
0 -
1))
733 (or (string-match "\\.el$" name
)
734 (setq name
(concat name
".el")))))
735 (error "Not on an #include line"))))))
736 (or (string-match "\\.[a-zA-Z0-9]+$" name
)
737 (setq name
(concat name
".h")))
739 (let ((path (or path
(PC-include-file-path))))
742 (concat (file-name-as-directory (car path
))
744 (setq path
(cdr path
)))
746 (setq name
(concat (file-name-as-directory (car path
)) name
))
747 (error "No such include file: <%s>" name
)))
748 (let ((dir (save-excursion
749 (set-buffer (car (buffer-list)))
751 (if (file-exists-p (concat dir name
))
752 (setq name
(concat dir name
))
753 (error "No such include file: \"%s\"" name
))))
754 (setq new-buf
(get-file-buffer name
))
756 ;; no need to verify last-modified time for this!
758 (setq new-buf
(create-file-buffer name
))
761 (insert-file-contents name t
))
768 (defun PC-include-file-path ()
769 (or PC-include-file-path
770 (let ((env (getenv "INCPATH"))
773 (or env
(error "No include file path specified"))
774 (while (setq pos
(string-match ":[^:]+$" env
))
775 (setq path
(cons (substring env
(1+ pos
)) path
)
776 env
(substring env
0 pos
)))
779 ;;; This is adapted from lib-complete.el, by Mike Williams.
780 (defun PC-include-file-all-completions (file search-path
&optional full
)
781 "Return all completions for FILE in any directory on SEARCH-PATH.
782 If optional third argument FULL is non-nil, returned pathnames should be
783 absolute rather than relative to some directory on the SEARCH-PATH."
785 (mapcar '(lambda (dir)
786 (if dir
(file-name-as-directory dir
) default-directory
))
788 (if (file-name-absolute-p file
)
789 ;; It's an absolute file name, so don't need search-path
791 (setq file
(expand-file-name file
))
792 (file-name-all-completions
793 (file-name-nondirectory file
) (file-name-directory file
)))
794 (let ((subdir (file-name-directory file
))
795 (ndfile (file-name-nondirectory file
))
797 ;; Append subdirectory part to each element of search-path
800 (mapcar '(lambda (dir) (concat dir subdir
))
803 ;; Make list of completions in each directory on search-path
805 (let* ((dir (car search-path
))
806 (subdir (if full dir subdir
)))
807 (if (file-directory-p dir
)
811 (mapcar '(lambda (file) (concat subdir file
))
812 (file-name-all-completions ndfile
815 (setq search-path
(cdr search-path
))))
816 ;; Compress out duplicates while building complete list (slloooow!)
817 (let ((sorted (sort (apply 'nconc file-lists
)
818 '(lambda (x y
) (not (string-lessp x y
)))))
821 (if (equal (car sorted
) (car compressed
)) nil
822 (setq compressed
(cons (car sorted
) compressed
)))
823 (setq sorted
(cdr sorted
)))
826 (defvar PC-old-read-file-name-internal nil
)
828 (defun PC-read-include-file-name-internal (string dir action
)
829 (if (string-match "<\\([^\"<>]*\\)>?$" string
)
830 (let* ((name (substring string
(match-beginning 1) (match-end 1)))
831 (str2 (substring string
(match-beginning 0)))
833 (mapcar (function (lambda (x) (list (format "<%s>" x
))))
834 (PC-include-file-all-completions
835 name
(PC-include-file-path)))))
837 ((not completion-table
) nil
)
838 ((eq action nil
) (try-completion str2 completion-table nil
))
839 ((eq action t
) (all-completions str2 completion-table nil
))
841 (eq (try-completion str2 completion-table nil
) t
))))
842 (funcall PC-old-read-file-name-internal string dir action
)))
844 (or PC-disable-includes
845 (memq 'PC-look-for-include-file find-file-not-found-hooks
)
846 (setq find-file-not-found-hooks
(cons 'PC-look-for-include-file
847 find-file-not-found-hooks
)))
849 (or PC-disable-includes
850 PC-old-read-file-name-internal
852 (setq PC-old-read-file-name-internal
853 (symbol-function 'read-file-name-internal
))
854 (fset 'read-file-name-internal
'PC-read-include-file-name-internal
)))