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>
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)
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
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; Extended completion for the Emacs minibuffer.
30 ;; The basic idea is that the command name or other completable text is
31 ;; divided into words and each word is completed separately, so that
32 ;; "M-x p-b" expands to "M-x print-buffer". If the entry is ambiguous
33 ;; each word is completed as much as possible and then the cursor is
34 ;; left at the first position where typing another letter will resolve
37 ;; Word separators for this purpose are hyphen, space, and period.
38 ;; These would most likely occur in command names, Info menu items,
39 ;; and file names, respectively. But all word separators are treated
40 ;; alike at all times.
42 ;; This completion package replaces the old-style completer's key
43 ;; bindings for TAB, SPC, RET, and `?'. The old completer is still
44 ;; available on the Meta versions of those keys. If you set
45 ;; PC-meta-flag to nil, the old completion keys will be left alone
46 ;; and the partial completer will use the Meta versions of the keys.
49 ;; Usage: Load this file. Now, during completable minibuffer entry,
51 ;; TAB means to do a partial completion;
52 ;; SPC means to do a partial complete-word;
53 ;; RET means to do a partial complete-and-exit;
54 ;; ? means to do a partial completion-help.
56 ;; If you set PC-meta-flag to nil, then TAB, SPC, RET, and ? perform
57 ;; original Emacs completions, and M-TAB etc. do partial completion.
58 ;; To do this, put the command,
60 ;; (setq PC-meta-flag nil)
62 ;; in your .emacs file. To load partial completion automatically, put
66 ;; in your .emacs file, too. Things will be faster if you byte-compile
67 ;; this file when you install it.
69 ;; As an extra feature, in cases where RET would not normally
70 ;; complete (such as `C-x b'), the M-RET key will always do a partial
71 ;; complete-and-exit. Thus `C-x b f.c RET' will select or create a
72 ;; buffer called "f.c", but `C-x b f.c M-RET' will select the existing
73 ;; buffer whose name matches that pattern (perhaps "filing.c").
74 ;; (PC-meta-flag does not affect this behavior; M-RET used to be
75 ;; undefined in this situation.)
77 ;; The regular M-TAB (lisp-complete-symbol) command also supports
78 ;; partial completion in this package.
80 ;; This package also contains a wildcard feature for C-x C-f (find-file).
81 ;; For example, `C-x C-f *.c RET' loads all .c files at once, exactly
82 ;; as if you had typed C-x C-f separately for each file. Completion
83 ;; is supported in connection with wildcards. Currently only the `*'
84 ;; wildcard character works.
86 ;; File name completion does not do partial completion of directories
87 ;; on the path, e.g., "/u/b/f" will not complete to "/usr/bin/foo",
88 ;; but you can put *'s in the path to accomplish this: "/u*/b*/f".
89 ;; Stars are required for performance reasons.
91 ;; In addition, this package includes a feature for accessing include
92 ;; files. For example, `C-x C-f <sys/time.h> RET' reads the file
93 ;; /usr/include/sys/time.h. The variable PC-include-file-path is a
94 ;; list of directories in which to search for include files. Completion
95 ;; is supported in include file names.
100 (defvar PC-meta-flag t
101 "*If nil, TAB does normal Emacs completion and M-TAB does Partial Completion.
102 If t, TAB does Partial Completion and M-TAB does normal completion.")
105 (defvar PC-word-delimiters
"-_. "
106 "*A string of characters which are to be treated as word delimiters
107 by the Partial Completion system.
109 Some arcane rules: If `]' is in this string it must come first.
110 If `^' is in this string it must NOT come first. If `-' is in this
111 string, it must come first or right after `]'. In other words, if
112 S is this string, then `[S]' must be a legal Emacs regular expression
113 \(not containing character ranges like `a-z').")
116 (defvar PC-first-char
'x
117 "*If t, first character of a string to be completed is always taken literally.
118 If nil, word delimiters are handled even if they appear as first character.
119 This controls whether \".e\" matches \".e*\" (t) or \"*.e*\" (nil).
120 If neither nil nor t, first char is literal only for filename completion.")
123 (defvar PC-include-file-path
'("/usr/include")
124 "*List of directories in which to look for include files.
125 If this is nil, uses the colon-separated path in $INCPATH instead.")
128 (defvar PC-disable-wildcards nil
129 "Set this to non-nil to disable wildcard support in \\[find-file].")
131 (defvar PC-disable-includes nil
132 "Set this to non-nil to disable include-file support in \\[find-file].")
135 (defvar PC-default-bindings t
136 "Set this to nil to suppress the default partial completion key bindings.")
138 (if PC-default-bindings
(progn
139 (define-key minibuffer-local-completion-map
"\t" 'PC-complete
)
140 (define-key minibuffer-local-completion-map
" " 'PC-complete-word
)
141 (define-key minibuffer-local-completion-map
"?" 'PC-completion-help
)
143 (define-key minibuffer-local-completion-map
"\e\t" 'PC-complete
)
144 (define-key minibuffer-local-completion-map
"\e " 'PC-complete-word
)
145 (define-key minibuffer-local-completion-map
"\e\r" 'PC-force-complete-and-exit
)
146 (define-key minibuffer-local-completion-map
"\e\n" 'PC-force-complete-and-exit
)
147 (define-key minibuffer-local-completion-map
"\e?" 'PC-completion-help
)
149 (define-key minibuffer-local-must-match-map
"\t" 'PC-complete
)
150 (define-key minibuffer-local-must-match-map
" " 'PC-complete-word
)
151 (define-key minibuffer-local-must-match-map
"\r" 'PC-complete-and-exit
)
152 (define-key minibuffer-local-must-match-map
"\n" 'PC-complete-and-exit
)
153 (define-key minibuffer-local-must-match-map
"?" 'PC-completion-help
)
155 (define-key minibuffer-local-must-match-map
"\e\t" 'PC-complete
)
156 (define-key minibuffer-local-must-match-map
"\e " 'PC-complete-word
)
157 (define-key minibuffer-local-must-match-map
"\e\r" 'PC-complete-and-exit
)
158 (define-key minibuffer-local-must-match-map
"\e\n" 'PC-complete-and-exit
)
159 (define-key minibuffer-local-must-match-map
"\e?" 'PC-completion-help
)
161 (define-key global-map
"\e\t" 'PC-lisp-complete-symbol
)
165 (defun PC-complete ()
166 "Like minibuffer-complete, but allows \"b--di\"-style abbreviations.
167 For example, \"M-x b--di\" would match `byte-recompile-directory', or any
168 name which consists of three or more words, the first beginning with \"b\"
169 and the third beginning with \"di\".
171 The pattern \"b--d\" is ambiguous for `byte-recompile-directory' and
172 `beginning-of-defun', so this would produce a list of completions
173 just like when normal Emacs completions are ambiguous.
175 Word-delimiters for the purposes of Partial Completion are \"-\", \"_\",
178 (if (PC-was-meta-key)
179 (minibuffer-complete)
180 (PC-do-completion nil
)))
183 (defun PC-complete-word ()
184 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
185 See `PC-complete' for details.
186 This can be bound to other keys, like `-' and `.', if you wish."
188 (if (eq (PC-was-meta-key) PC-meta-flag
)
189 (if (eq last-command-char ?
)
190 (minibuffer-complete-word)
191 (self-insert-command 1))
192 (self-insert-command 1)
194 (PC-do-completion 'word
))))
197 (defun PC-complete-space ()
198 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
199 See `PC-complete' for details.
200 This is suitable for binding to other keys which should act just like SPC."
202 (if (eq (PC-was-meta-key) PC-meta-flag
)
203 (minibuffer-complete-word)
206 (PC-do-completion 'word
))))
209 (defun PC-complete-and-exit ()
210 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
211 See `PC-complete' for details."
213 (if (eq (PC-was-meta-key) PC-meta-flag
)
214 (minibuffer-complete-and-exit)
215 (PC-do-complete-and-exit)))
217 (defun PC-force-complete-and-exit ()
218 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
219 See `PC-complete' for details."
221 (let ((minibuffer-completion-confirm nil
))
222 (PC-do-complete-and-exit)))
224 (defun PC-do-complete-and-exit ()
225 (if (= (buffer-size) 0) ; Duplicate the "bug" that Info-menu relies on...
227 (let ((flag (PC-do-completion 'exit
)))
229 (if (or (eq flag
'complete
)
230 (not minibuffer-completion-confirm
))
232 (PC-temp-minibuffer-message " [Confirm]"))))))
235 (defun PC-completion-help ()
236 "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
237 See `PC-complete' for details."
239 (if (eq (PC-was-meta-key) PC-meta-flag
)
240 (minibuffer-completion-help)
241 (PC-do-completion 'help
)))
243 (defun PC-was-meta-key ()
244 (or (/= (length (this-command-keys)) 1)
245 (let ((key (aref (this-command-keys) 0)))
248 (not (null (memq 'meta
(event-modifiers key
))))))))
251 (defvar PC-ignored-extensions
'empty-cache
)
252 (defvar PC-delims
'empty-cache
)
253 (defvar PC-ignored-regexp nil
)
254 (defvar PC-word-failed-flag nil
)
255 (defvar PC-delim-regex nil
)
256 (defvar PC-ndelims-regex nil
)
257 (defvar PC-delims-list nil
)
259 (defun PC-do-completion (&optional mode beg end
)
260 (or beg
(setq beg
(point-min)))
261 (or end
(setq end
(point-max)))
262 (let* ((table minibuffer-completion-table
)
263 (pred minibuffer-completion-predicate
)
264 (filename (memq table
'(read-file-name-internal
265 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 ;; Record how many characters at the beginning are not included
287 (length (file-name-directory str
))
290 ;; Do substitutions in directory names
292 (not (equal str
(setq p
(substitute-in-file-name str
))))
294 (delete-region beg end
)
296 (setq str p end
(+ beg
(length str
)))))
298 ;; Prepare various delimiter strings
299 (or (equal PC-word-delimiters PC-delims
)
300 (setq PC-delims PC-word-delimiters
301 PC-delim-regex
(concat "[" PC-delims
"]")
302 PC-ndelims-regex
(concat "[^" PC-delims
"]*")
303 PC-delims-list
(append PC-delims nil
)))
305 ;; Look for wildcard expansions in directory name
307 (string-match "\\*.*/" str
)
310 (setq p
(1+ (string-match "/[^/]*\\'" pat
)))
311 (while (setq p
(string-match PC-delim-regex pat p
))
312 (setq pat
(concat (substring pat
0 p
)
316 (setq files
(PC-expand-many-files (concat pat
"*")))
318 (let ((dir (file-name-directory (car files
)))
320 (while (and (setq p
(cdr p
))
321 (equal dir
(file-name-directory (car p
)))))
323 (setq filename nil table nil pred nil
325 (delete-region beg end
)
326 (setq str
(concat dir
(file-name-nondirectory str
)))
328 (setq end
(+ beg
(length str
)))))
329 (setq filename nil table nil pred nil
))))
331 ;; Strip directory name if appropriate
334 (setq basestr
(substring str incname
)
335 dirname
(substring str
0 incname
))
336 (setq basestr
(file-name-nondirectory str
)
337 dirname
(file-name-directory str
)))
340 ;; Convert search pattern to a standard regular expression
341 (setq regex
(regexp-quote basestr
)
342 offset
(if (and (> (length regex
) 0)
343 (not (eq (aref basestr
0) ?\
*))
344 (or (eq PC-first-char t
)
345 (and PC-first-char filename
))) 1 0)
347 (while (setq p
(string-match PC-delim-regex regex p
))
348 (if (eq (aref regex p
) ?
)
349 (setq regex
(concat (substring regex
0 p
)
352 (substring regex
(1+ p
)))
353 p
(+ p
(length PC-ndelims-regex
) (length PC-delim-regex
)))
354 (let ((bump (if (memq (aref regex p
)
355 '(?$ ?^ ?\. ?
* ?
+ ?? ?
[ ?
] ?
\\))
357 (setq regex
(concat (substring regex
0 (+ p bump
))
359 (substring regex
(+ p bump
)))
360 p
(+ p
(length PC-ndelims-regex
) 1)))))
363 (while (setq p
(string-match "\\\\\\*" regex p
))
364 (setq regex
(concat (substring regex
0 p
)
366 (substring regex
(+ p
2))))))
367 ;;(setq the-regex regex)
368 (setq regex
(concat "\\`" regex
))
370 ;; Find an initial list of possible completions
371 (if (not (setq p
(string-match (concat PC-delim-regex
372 (if filename
"\\|\\*" ""))
374 (+ (length dirname
) offset
))))
376 ;; Minibuffer contains no hyphens -- simple case!
377 (setq poss
(all-completions str
381 ;; Use all-completions to do an initial cull. This is a big win,
382 ;; since all-completions is written in C!
383 (let ((compl (all-completions (substring str
0 p
)
388 (and (string-match regex
(car p
))
389 (setq poss
(cons (car p
) poss
)))
392 ;; Now we have a list of possible completions
395 ;; No valid completions found
397 (if (and (eq mode
'word
)
398 (not PC-word-failed-flag
))
399 (let ((PC-word-failed-flag t
))
400 (delete-backward-char 1)
401 (PC-do-completion 'word
))
403 (PC-temp-minibuffer-message (if ambig
404 " [Ambiguous dir name]"
410 ;; More than one valid completion found
411 ((or (cdr (setq helpposs poss
))
412 (memq mode
'(help word
)))
414 ;; Handle completion-ignored-extensions
416 (not (eq mode
'help
))
419 ;; Build a regular expression representing the extensions list
420 (or (equal completion-ignored-extensions PC-ignored-extensions
)
421 (setq PC-ignored-regexp
425 (setq PC-ignored-extensions
426 completion-ignored-extensions
)
430 ;; Check if there are any without an ignored extension
433 (or (string-match PC-ignored-regexp
(car p2
))
434 (setq p
(cons (car p2
) p
)))
437 ;; If there are "good" names, use them
438 (and p
(setq poss p
))))
440 ;; Is the actual string one of the possible completions?
441 (setq p
(and (not (eq mode
'help
)) poss
))
443 (not (equal (car p
) basestr
)))
446 (PC-temp-minibuffer-message " [Complete, but not unique]"))
448 (not (and (null mode
)
449 (eq this-command last-command
))))
452 ;; If ambiguous, try for a partial completion
458 ;; Check if next few letters are the same in all cases
459 (if (and (not (eq mode
'help
))
460 (setq prefix
(try-completion "" (mapcar 'list poss
))))
463 (setq prefix
(PC-chop-word prefix basestr
)))
464 (goto-char (+ beg
(length dirname
)))
467 (while (< i
(length prefix
))
468 (if (and (< (point) end
)
472 (if (and (< (point) end
)
473 (or (and (looking-at " ")
474 (memq (aref prefix i
)
476 (eq (downcase (aref prefix i
))
482 (and filename
(looking-at "\\*")
485 (setq end
(1- end
))))
487 ;; Use format to discard text properties.
488 (insert (format "%s" (substring prefix i
(1+ i
))))
491 (or pt
(equal (point) beg
)
493 (looking-at PC-delim-regex
))
494 (setq skip
(concat skip
495 (regexp-quote prefix
)
497 prefix
(try-completion
503 (and (string-match skip x
)
508 (or (> i
0) (> (length prefix
) 0))
509 (or (not (eq mode
'word
))
510 (and first
(> (length prefix
) 0)
512 prefix
(substring prefix
0 1))))))
513 (goto-char (if (eq mode
'word
) end
516 (if (and (eq mode
'word
)
517 (not PC-word-failed-flag
))
521 ;; We changed it... would it be complete without the space?
522 (if (PC-is-complete-p (buffer-substring 1 (1- end
))
524 (delete-region (1- end
) end
)))
528 ;; We changed it... enough to be complete?
530 (PC-is-complete-p (buffer-string) table pred
))
532 ;; If totally ambiguous, display a list of completions
533 (if (or completion-auto-help
535 (with-output-to-temp-buffer "*Completions*"
536 (display-completion-list (sort helpposs
'string-lessp
))
538 (set-buffer standard-output
)
539 ;; Record which part of the buffer we are completing
540 ;; so that choosing a completion from the list
541 ;; knows how much old text to replace.
542 (setq completion-base-size dirlength
)))
543 (PC-temp-minibuffer-message " [Next char not unique]"))
546 ;; Only one possible completion
548 (if (equal basestr
(car poss
))
550 (PC-temp-minibuffer-message " [Sole completion]"))
551 (delete-region beg end
)
554 (substitute-in-file-name (concat dirname
(car poss
)))
559 (defun PC-is-complete-p (str table pred
)
560 (let ((res (if (listp table
)
563 (or (equal str
"nil") ; heh, heh, heh
564 (intern-soft str table
))
565 (funcall table str pred
'lambda
)))))
568 (and (not (listp table
)) (not (vectorp table
)))
572 (defun PC-chop-word (new old
)
575 (while (and (setq i
(string-match PC-delim-regex old
(1+ i
)))
576 (setq j
(string-match PC-delim-regex new
(1+ j
)))))
578 (or (not PC-word-failed-flag
)
579 (setq j
(string-match PC-delim-regex new
(1+ j
)))))
580 (substring new
0 (1+ j
))
583 (defvar PC-not-minibuffer nil
)
585 (defun PC-temp-minibuffer-message (m)
586 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
587 (if PC-not-minibuffer
592 (if (fboundp 'temp-minibuffer-message
)
593 (temp-minibuffer-message m
)
594 (let ((savemax (point-max)))
596 (goto-char (point-max))
598 (let ((inhibit-quit t
))
600 (delete-region savemax
(point-max))
603 unread-command-char
7)))))))
606 (defun PC-lisp-complete-symbol ()
607 "Perform completion on Lisp symbol preceding point.
608 That symbol is compared against the symbols that exist
609 and any additional characters determined by what is there
611 If the symbol starts just after an open-parenthesis,
612 only symbols with function definitions are considered.
613 Otherwise, all symbols with function definitions, values
614 or properties are considered."
617 (buffer-syntax (syntax-table))
620 (if lisp-mode-syntax-table
621 (set-syntax-table lisp-mode-syntax-table
))
623 (while (= (char-syntax (following-char)) ?
\')
626 (set-syntax-table buffer-syntax
)))
627 (minibuffer-completion-table obarray
)
628 (minibuffer-completion-predicate
629 (if (eq (char-after (1- beg
)) ?\
()
631 (function (lambda (sym)
632 (or (boundp sym
) (fboundp sym
)
633 (symbol-plist sym
))))))
634 (PC-not-minibuffer t
))
635 (PC-do-completion nil beg end
)))
638 ;;; Wildcards in `C-x C-f' command. This is independent from the main
639 ;;; completion code, except for `PC-expand-many-files' which is called
640 ;;; when "*"'s are found in the path during filename completion. (The
641 ;;; above completion code always understands "*"'s, except in file paths,
642 ;;; without relying on the following code.)
644 (defvar PC-many-files-list nil
)
646 (defun PC-try-load-many-files ()
647 (if (string-match "\\*" buffer-file-name
)
648 (let* ((pat buffer-file-name
)
649 (files (PC-expand-many-files pat
))
652 (kill-buffer (current-buffer))
654 (error "No matching files"))
655 (save-window-excursion
656 (while (setq next
(cdr next
))
657 (let ((buf (find-file-noselect (car next
))))
658 (switch-to-buffer buf
))))
659 ;; This modifies the "buf" variable inside find-file-noselect.
660 (setq buf
(get-file-buffer first
))
662 nil
; should do verify-visited-file-modtime stuff.
663 (setq filename first
)
664 (setq buf
(create-file-buffer filename
))
667 (insert-file-contents filename t
))
669 (setq PC-many-files-list
(mapconcat
670 (if (string-match "\\*.*/" pat
)
672 'file-name-nondirectory
)
674 find-file-hooks
(cons 'PC-after-load-many-files
676 ;; This modifies the "error" variable inside find-file-noselect.
681 (defun PC-after-load-many-files ()
682 (setq find-file-hooks
(delq 'PC-after-load-many-files find-file-hooks
))
683 (message "Also loaded %s." PC-many-files-list
))
685 (defun PC-expand-many-files (name)
687 (set-buffer (generate-new-buffer " *Glob Output*"))
689 (shell-command (concat "echo " name
) t
)
690 (goto-char (point-min))
691 (if (looking-at ".*No match")
694 (while (search-forward " " nil t
)
695 (delete-backward-char 1)
697 (goto-char (point-max))
698 (delete-backward-char 1)
700 (goto-char (point-min))
701 (let ((files (read (current-buffer))))
702 (kill-buffer (current-buffer))
705 (or PC-disable-wildcards
706 (memq 'PC-try-load-many-files find-file-not-found-hooks
)
707 (setq find-file-not-found-hooks
(cons 'PC-try-load-many-files
708 find-file-not-found-hooks
)))
712 ;;; Facilities for loading C header files. This is independent from the
713 ;;; main completion code. See also the variable `PC-include-file-path'
714 ;;; at top of this file.
716 (defun PC-look-for-include-file ()
717 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
718 (let ((name (substring (buffer-file-name)
719 (match-beginning 1) (match-end 1)))
720 (punc (aref (buffer-file-name) (match-beginning 0)))
723 (kill-buffer (current-buffer))
726 (set-buffer (car (buffer-list)))
730 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
731 (setq name
(buffer-substring (match-beginning 1)
733 punc
(char-after (1- (match-beginning 1))))
734 ;; Suggested by Frank Siebenlist:
736 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
738 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
740 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
742 (setq name
(buffer-substring (match-beginning 1)
746 (if (string-match "\\.elc$" name
)
747 (setq name
(substring name
0 -
1))
748 (or (string-match "\\.el$" name
)
749 (setq name
(concat name
".el")))))
750 (error "Not on an #include line"))))))
751 (or (string-match "\\.[a-zA-Z0-9]+$" name
)
752 (setq name
(concat name
".h")))
754 (let ((path (or path
(PC-include-file-path))))
757 (concat (file-name-as-directory (car path
))
759 (setq path
(cdr path
)))
761 (setq name
(concat (file-name-as-directory (car path
)) name
))
762 (error "No such include file: <%s>" name
)))
763 (let ((dir (save-excursion
764 (set-buffer (car (buffer-list)))
766 (if (file-exists-p (concat dir name
))
767 (setq name
(concat dir name
))
768 (error "No such include file: \"%s\"" name
))))
769 (setq new-buf
(get-file-buffer name
))
771 ;; no need to verify last-modified time for this!
773 (setq new-buf
(create-file-buffer name
))
776 (insert-file-contents name t
))
783 (defun PC-include-file-path ()
784 (or PC-include-file-path
785 (let ((env (getenv "INCPATH"))
788 (or env
(error "No include file path specified"))
789 (while (setq pos
(string-match ":[^:]+$" env
))
790 (setq path
(cons (substring env
(1+ pos
)) path
)
791 env
(substring env
0 pos
)))
794 ;;; This is adapted from lib-complete.el, by Mike Williams.
795 (defun PC-include-file-all-completions (file search-path
&optional full
)
796 "Return all completions for FILE in any directory on SEARCH-PATH.
797 If optional third argument FULL is non-nil, returned pathnames should be
798 absolute rather than relative to some directory on the SEARCH-PATH."
800 (mapcar '(lambda (dir)
801 (if dir
(file-name-as-directory dir
) default-directory
))
803 (if (file-name-absolute-p file
)
804 ;; It's an absolute file name, so don't need search-path
806 (setq file
(expand-file-name file
))
807 (file-name-all-completions
808 (file-name-nondirectory file
) (file-name-directory file
)))
809 (let ((subdir (file-name-directory file
))
810 (ndfile (file-name-nondirectory file
))
812 ;; Append subdirectory part to each element of search-path
815 (mapcar '(lambda (dir) (concat dir subdir
))
818 ;; Make list of completions in each directory on search-path
820 (let* ((dir (car search-path
))
821 (subdir (if full dir subdir
)))
822 (if (file-directory-p dir
)
826 (mapcar '(lambda (file) (concat subdir file
))
827 (file-name-all-completions ndfile
830 (setq search-path
(cdr search-path
))))
831 ;; Compress out duplicates while building complete list (slloooow!)
832 (let ((sorted (sort (apply 'nconc file-lists
)
833 '(lambda (x y
) (not (string-lessp x y
)))))
836 (if (equal (car sorted
) (car compressed
)) nil
837 (setq compressed
(cons (car sorted
) compressed
)))
838 (setq sorted
(cdr sorted
)))
841 (defvar PC-old-read-file-name-internal nil
)
843 (defun PC-read-include-file-name-internal (string dir action
)
844 (if (string-match "<\\([^\"<>]*\\)>?$" string
)
845 (let* ((name (substring string
(match-beginning 1) (match-end 1)))
846 (str2 (substring string
(match-beginning 0)))
848 (mapcar (function (lambda (x) (list (format "<%s>" x
))))
849 (PC-include-file-all-completions
850 name
(PC-include-file-path)))))
852 ((not completion-table
) nil
)
853 ((eq action nil
) (try-completion str2 completion-table nil
))
854 ((eq action t
) (all-completions str2 completion-table nil
))
856 (eq (try-completion str2 completion-table nil
) t
))))
857 (funcall PC-old-read-file-name-internal string dir action
)))
859 (or PC-disable-includes
860 (memq 'PC-look-for-include-file find-file-not-found-hooks
)
861 (setq find-file-not-found-hooks
(cons 'PC-look-for-include-file
862 find-file-not-found-hooks
)))
864 (or PC-disable-includes
865 PC-old-read-file-name-internal
867 (setq PC-old-read-file-name-internal
868 (symbol-function 'read-file-name-internal
))
869 (fset 'read-file-name-internal
'PC-read-include-file-name-internal
)))