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 the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
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 ;; If the previous command was not this one,
182 ;; never scroll, always retry completion.
183 (or (eq last-command this-command
)
184 (setq minibuffer-scroll-window nil
))
185 (let ((window minibuffer-scroll-window
))
186 ;; If there's a fresh completion window with a live buffer,
187 ;; and this command is repeated, scroll that window.
188 (if (and window
(window-buffer window
)
189 (buffer-name (window-buffer window
)))
191 (set-buffer (window-buffer window
))
192 (if (pos-visible-in-window-p (point-max) window
)
193 (set-window-start window
(point-min) nil
)
194 (scroll-other-window)))
195 (PC-do-completion nil
)))))
198 (defun PC-complete-word ()
199 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
200 See `PC-complete' for details.
201 This can be bound to other keys, like `-' and `.', if you wish."
203 (if (eq (PC-was-meta-key) PC-meta-flag
)
204 (if (eq last-command-char ?
)
205 (minibuffer-complete-word)
206 (self-insert-command 1))
207 (self-insert-command 1)
209 (PC-do-completion 'word
))))
212 (defun PC-complete-space ()
213 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
214 See `PC-complete' for details.
215 This is suitable for binding to other keys which should act just like SPC."
217 (if (eq (PC-was-meta-key) PC-meta-flag
)
218 (minibuffer-complete-word)
221 (PC-do-completion 'word
))))
224 (defun PC-complete-and-exit ()
225 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
226 See `PC-complete' for details."
228 (if (eq (PC-was-meta-key) PC-meta-flag
)
229 (minibuffer-complete-and-exit)
230 (PC-do-complete-and-exit)))
232 (defun PC-force-complete-and-exit ()
233 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
234 See `PC-complete' for details."
236 (let ((minibuffer-completion-confirm nil
))
237 (PC-do-complete-and-exit)))
239 (defun PC-do-complete-and-exit ()
240 (if (= (buffer-size) 0) ; Duplicate the "bug" that Info-menu relies on...
242 (let ((flag (PC-do-completion 'exit
)))
244 (if (or (eq flag
'complete
)
245 (not minibuffer-completion-confirm
))
247 (PC-temp-minibuffer-message " [Confirm]"))))))
250 (defun PC-completion-help ()
251 "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
252 See `PC-complete' for details."
254 (if (eq (PC-was-meta-key) PC-meta-flag
)
255 (minibuffer-completion-help)
256 (PC-do-completion 'help
)))
258 (defun PC-was-meta-key ()
259 (or (/= (length (this-command-keys)) 1)
260 (let ((key (aref (this-command-keys) 0)))
263 (not (null (memq 'meta
(event-modifiers key
))))))))
266 (defvar PC-ignored-extensions
'empty-cache
)
267 (defvar PC-delims
'empty-cache
)
268 (defvar PC-ignored-regexp nil
)
269 (defvar PC-word-failed-flag nil
)
270 (defvar PC-delim-regex nil
)
271 (defvar PC-ndelims-regex nil
)
272 (defvar PC-delims-list nil
)
274 (defvar PC-completion-as-file-name-predicate
277 (memq minibuffer-completion-table
278 '(read-file-name-internal read-directory-name-internal
))))
279 "A function testing whether a minibuffer completion now will work filename-style.
280 The function takes no arguments, and typically looks at the value
281 of `minibuffer-completion-table' and the minibuffer contents.")
283 (defun PC-do-completion (&optional mode beg end
)
284 (or beg
(setq beg
(point-min)))
285 (or end
(setq end
(point-max)))
286 (let* ((table minibuffer-completion-table
)
287 (pred minibuffer-completion-predicate
)
288 (filename (funcall PC-completion-as-file-name-predicate
))
291 (str (buffer-substring beg end
))
292 (incname (and filename
(string-match "<\\([^\"<>]*\\)>?$" str
)))
299 (case-fold-search completion-ignore-case
))
301 ;; Check if buffer contents can already be considered complete
302 (if (and (eq mode
'exit
)
303 (PC-is-complete-p str table pred
))
306 ;; Record how many characters at the beginning are not included
310 (length (file-name-directory str
))
313 ;; Do substitutions in directory names
315 (not (equal str
(setq p
(substitute-in-file-name str
))))
317 (delete-region beg end
)
319 (setq str p end
(+ beg
(length str
)))))
321 ;; Prepare various delimiter strings
322 (or (equal PC-word-delimiters PC-delims
)
323 (setq PC-delims PC-word-delimiters
324 PC-delim-regex
(concat "[" PC-delims
"]")
325 PC-ndelims-regex
(concat "[^" PC-delims
"]*")
326 PC-delims-list
(append PC-delims nil
)))
328 ;; Look for wildcard expansions in directory name
330 (string-match "\\*.*/" str
)
333 (setq p
(1+ (string-match "/[^/]*\\'" pat
)))
334 (while (setq p
(string-match PC-delim-regex pat p
))
335 (setq pat
(concat (substring pat
0 p
)
339 (setq files
(PC-expand-many-files (concat pat
"*")))
341 (let ((dir (file-name-directory (car files
)))
343 (while (and (setq p
(cdr p
))
344 (equal dir
(file-name-directory (car p
)))))
346 (setq filename nil table nil pred nil
348 (delete-region beg end
)
349 (setq str
(concat dir
(file-name-nondirectory str
)))
351 (setq end
(+ beg
(length str
)))))
352 (setq filename nil table nil pred nil
))))
354 ;; Strip directory name if appropriate
357 (setq basestr
(substring str incname
)
358 dirname
(substring str
0 incname
))
359 (setq basestr
(file-name-nondirectory str
)
360 dirname
(file-name-directory str
)))
363 ;; Convert search pattern to a standard regular expression
364 (setq regex
(regexp-quote basestr
)
365 offset
(if (and (> (length regex
) 0)
366 (not (eq (aref basestr
0) ?\
*))
367 (or (eq PC-first-char t
)
368 (and PC-first-char filename
))) 1 0)
370 (while (setq p
(string-match PC-delim-regex regex p
))
371 (if (eq (aref regex p
) ?
)
372 (setq regex
(concat (substring regex
0 p
)
375 (substring regex
(1+ p
)))
376 p
(+ p
(length PC-ndelims-regex
) (length PC-delim-regex
)))
377 (let ((bump (if (memq (aref regex p
)
378 '(?$ ?^ ?\. ?
* ?
+ ?? ?
[ ?
] ?
\\))
380 (setq regex
(concat (substring regex
0 (+ p bump
))
382 (substring regex
(+ p bump
)))
383 p
(+ p
(length PC-ndelims-regex
) 1)))))
386 (while (setq p
(string-match "\\\\\\*" regex p
))
387 (setq regex
(concat (substring regex
0 p
)
389 (substring regex
(+ p
2))))))
390 ;;(setq the-regex regex)
391 (setq regex
(concat "\\`" regex
))
393 ;; Find an initial list of possible completions
394 (if (not (setq p
(string-match (concat PC-delim-regex
395 (if filename
"\\|\\*" ""))
397 (+ (length dirname
) offset
))))
399 ;; Minibuffer contains no hyphens -- simple case!
400 (setq poss
(all-completions str
404 ;; Use all-completions to do an initial cull. This is a big win,
405 ;; since all-completions is written in C!
406 (let ((compl (all-completions (substring str
0 p
)
411 (and (string-match regex
(car p
))
412 (setq poss
(cons (car p
) poss
)))
415 ;; Now we have a list of possible completions
418 ;; No valid completions found
420 (if (and (eq mode
'word
)
421 (not PC-word-failed-flag
))
422 (let ((PC-word-failed-flag t
))
423 (delete-backward-char 1)
424 (PC-do-completion 'word
))
426 (PC-temp-minibuffer-message (if ambig
427 " [Ambiguous dir name]"
433 ;; More than one valid completion found
434 ((or (cdr (setq helpposs poss
))
435 (memq mode
'(help word
)))
437 ;; Handle completion-ignored-extensions
439 (not (eq mode
'help
))
442 ;; Build a regular expression representing the extensions list
443 (or (equal completion-ignored-extensions PC-ignored-extensions
)
444 (setq PC-ignored-regexp
448 (setq PC-ignored-extensions
449 completion-ignored-extensions
)
453 ;; Check if there are any without an ignored extension
456 (or (string-match PC-ignored-regexp
(car p2
))
457 (setq p
(cons (car p2
) p
)))
460 ;; If there are "good" names, use them
461 (and p
(setq poss p
))))
463 ;; Is the actual string one of the possible completions?
464 (setq p
(and (not (eq mode
'help
)) poss
))
466 (not (equal (car p
) basestr
)))
469 (PC-temp-minibuffer-message " [Complete, but not unique]"))
471 (not (and (null mode
)
472 (eq this-command last-command
))))
475 ;; If ambiguous, try for a partial completion
481 ;; Check if next few letters are the same in all cases
482 (if (and (not (eq mode
'help
))
483 (setq prefix
(try-completion "" (mapcar 'list poss
))))
486 (setq prefix
(PC-chop-word prefix basestr
)))
487 (goto-char (+ beg
(length dirname
)))
490 (while (< i
(length prefix
))
491 (if (and (< (point) end
)
495 (if (and (< (point) end
)
496 (or (and (looking-at " ")
497 (memq (aref prefix i
)
499 (eq (downcase (aref prefix i
))
505 (and filename
(looking-at "\\*")
508 (setq end
(1- end
))))
510 ;; Use format to discard text properties.
511 (insert (format "%s" (substring prefix i
(1+ i
))))
514 (or pt
(equal (point) beg
)
516 (looking-at PC-delim-regex
))
517 (setq skip
(concat skip
518 (regexp-quote prefix
)
520 prefix
(try-completion
526 (and (string-match skip x
)
531 (or (> i
0) (> (length prefix
) 0))
532 (or (not (eq mode
'word
))
533 (and first
(> (length prefix
) 0)
535 prefix
(substring prefix
0 1))))))
536 (goto-char (if (eq mode
'word
) end
539 (if (and (eq mode
'word
)
540 (not PC-word-failed-flag
))
544 ;; We changed it... would it be complete without the space?
545 (if (PC-is-complete-p (buffer-substring 1 (1- end
))
547 (delete-region (1- end
) end
)))
551 ;; We changed it... enough to be complete?
553 (PC-is-complete-p (buffer-string) table pred
))
555 ;; If totally ambiguous, display a list of completions
556 (if (or completion-auto-help
558 (with-output-to-temp-buffer "*Completions*"
559 (display-completion-list (sort helpposs
'string-lessp
))
561 (set-buffer standard-output
)
562 ;; Record which part of the buffer we are completing
563 ;; so that choosing a completion from the list
564 ;; knows how much old text to replace.
565 (setq completion-base-size dirlength
)))
566 (PC-temp-minibuffer-message " [Next char not unique]"))
569 ;; Only one possible completion
571 (if (equal basestr
(car poss
))
573 (PC-temp-minibuffer-message " [Sole completion]"))
574 (delete-region beg end
)
577 (substitute-in-file-name (concat dirname
(car poss
)))
582 (defun PC-is-complete-p (str table pred
)
583 (let ((res (if (listp table
)
586 (or (equal str
"nil") ; heh, heh, heh
587 (intern-soft str table
))
588 (funcall table str pred
'lambda
)))))
591 (and (not (listp table
)) (not (vectorp table
)))
595 (defun PC-chop-word (new old
)
598 (while (and (setq i
(string-match PC-delim-regex old
(1+ i
)))
599 (setq j
(string-match PC-delim-regex new
(1+ j
)))))
601 (or (not PC-word-failed-flag
)
602 (setq j
(string-match PC-delim-regex new
(1+ j
)))))
603 (substring new
0 (1+ j
))
606 (defvar PC-not-minibuffer nil
)
608 (defun PC-temp-minibuffer-message (m)
609 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
610 (if PC-not-minibuffer
615 (if (fboundp 'temp-minibuffer-message
)
616 (temp-minibuffer-message m
)
617 (let ((savemax (point-max)))
619 (goto-char (point-max))
621 (let ((inhibit-quit t
))
623 (delete-region savemax
(point-max))
626 unread-command-char
7)))))))
629 (defun PC-lisp-complete-symbol ()
630 "Perform completion on Lisp symbol preceding point.
631 That symbol is compared against the symbols that exist
632 and any additional characters determined by what is there
634 If the symbol starts just after an open-parenthesis,
635 only symbols with function definitions are considered.
636 Otherwise, all symbols with function definitions, values
637 or properties are considered."
640 (buffer-syntax (syntax-table))
643 (if lisp-mode-syntax-table
644 (set-syntax-table lisp-mode-syntax-table
))
646 (while (= (char-syntax (following-char)) ?
\')
649 (set-syntax-table buffer-syntax
)))
650 (minibuffer-completion-table obarray
)
651 (minibuffer-completion-predicate
652 (if (eq (char-after (1- beg
)) ?\
()
654 (function (lambda (sym)
655 (or (boundp sym
) (fboundp sym
)
656 (symbol-plist sym
))))))
657 (PC-not-minibuffer t
))
658 (PC-do-completion nil beg end
)))
661 ;;; Wildcards in `C-x C-f' command. This is independent from the main
662 ;;; completion code, except for `PC-expand-many-files' which is called
663 ;;; when "*"'s are found in the path during filename completion. (The
664 ;;; above completion code always understands "*"'s, except in file paths,
665 ;;; without relying on the following code.)
667 (defvar PC-many-files-list nil
)
669 (defun PC-try-load-many-files ()
670 (if (string-match "\\*" buffer-file-name
)
671 (let* ((pat buffer-file-name
)
672 (files (PC-expand-many-files pat
))
675 (kill-buffer (current-buffer))
677 (error "No matching files"))
678 (save-window-excursion
679 (while (setq next
(cdr next
))
680 (let ((buf (find-file-noselect (car next
))))
681 (switch-to-buffer buf
))))
682 ;; This modifies the "buf" variable inside find-file-noselect.
683 (setq buf
(get-file-buffer first
))
685 nil
; should do verify-visited-file-modtime stuff.
686 (setq filename first
)
687 (setq buf
(create-file-buffer filename
))
690 (insert-file-contents filename t
))
692 (setq PC-many-files-list
(mapconcat
693 (if (string-match "\\*.*/" pat
)
695 'file-name-nondirectory
)
697 find-file-hooks
(cons 'PC-after-load-many-files
699 ;; This modifies the "error" variable inside find-file-noselect.
704 (defun PC-after-load-many-files ()
705 (setq find-file-hooks
(delq 'PC-after-load-many-files find-file-hooks
))
706 (message "Also loaded %s." PC-many-files-list
))
708 (defun PC-expand-many-files (name)
710 (set-buffer (generate-new-buffer " *Glob Output*"))
712 (shell-command (concat "echo " name
) t
)
713 (goto-char (point-min))
714 (if (looking-at ".*No match")
717 (while (search-forward " " nil t
)
718 (delete-backward-char 1)
720 (goto-char (point-max))
721 (delete-backward-char 1)
723 (goto-char (point-min))
724 (let ((files (read (current-buffer))))
725 (kill-buffer (current-buffer))
728 (or PC-disable-wildcards
729 (memq 'PC-try-load-many-files find-file-not-found-hooks
)
730 (setq find-file-not-found-hooks
(cons 'PC-try-load-many-files
731 find-file-not-found-hooks
)))
735 ;;; Facilities for loading C header files. This is independent from the
736 ;;; main completion code. See also the variable `PC-include-file-path'
737 ;;; at top of this file.
739 (defun PC-look-for-include-file ()
740 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
741 (let ((name (substring (buffer-file-name)
742 (match-beginning 1) (match-end 1)))
743 (punc (aref (buffer-file-name) (match-beginning 0)))
746 (kill-buffer (current-buffer))
749 (set-buffer (car (buffer-list)))
753 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
754 (setq name
(buffer-substring (match-beginning 1)
756 punc
(char-after (1- (match-beginning 1))))
757 ;; Suggested by Frank Siebenlist:
759 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
761 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
763 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
765 (setq name
(buffer-substring (match-beginning 1)
769 (if (string-match "\\.elc$" name
)
770 (setq name
(substring name
0 -
1))
771 (or (string-match "\\.el$" name
)
772 (setq name
(concat name
".el")))))
773 (error "Not on an #include line"))))))
774 (or (string-match "\\.[a-zA-Z0-9]+$" name
)
775 (setq name
(concat name
".h")))
777 (let ((path (or path
(PC-include-file-path))))
780 (concat (file-name-as-directory (car path
))
782 (setq path
(cdr path
)))
784 (setq name
(concat (file-name-as-directory (car path
)) name
))
785 (error "No such include file: <%s>" name
)))
786 (let ((dir (save-excursion
787 (set-buffer (car (buffer-list)))
789 (if (file-exists-p (concat dir name
))
790 (setq name
(concat dir name
))
791 (error "No such include file: \"%s\"" name
))))
792 (setq new-buf
(get-file-buffer name
))
794 ;; no need to verify last-modified time for this!
796 (setq new-buf
(create-file-buffer name
))
799 (insert-file-contents name t
))
806 (defun PC-include-file-path ()
807 (or PC-include-file-path
808 (let ((env (getenv "INCPATH"))
811 (or env
(error "No include file path specified"))
812 (while (setq pos
(string-match ":[^:]+$" env
))
813 (setq path
(cons (substring env
(1+ pos
)) path
)
814 env
(substring env
0 pos
)))
817 ;;; This is adapted from lib-complete.el, by Mike Williams.
818 (defun PC-include-file-all-completions (file search-path
&optional full
)
819 "Return all completions for FILE in any directory on SEARCH-PATH.
820 If optional third argument FULL is non-nil, returned pathnames should be
821 absolute rather than relative to some directory on the SEARCH-PATH."
823 (mapcar '(lambda (dir)
824 (if dir
(file-name-as-directory dir
) default-directory
))
826 (if (file-name-absolute-p file
)
827 ;; It's an absolute file name, so don't need search-path
829 (setq file
(expand-file-name file
))
830 (file-name-all-completions
831 (file-name-nondirectory file
) (file-name-directory file
)))
832 (let ((subdir (file-name-directory file
))
833 (ndfile (file-name-nondirectory file
))
835 ;; Append subdirectory part to each element of search-path
838 (mapcar '(lambda (dir) (concat dir subdir
))
841 ;; Make list of completions in each directory on search-path
843 (let* ((dir (car search-path
))
844 (subdir (if full dir subdir
)))
845 (if (file-directory-p dir
)
849 (mapcar '(lambda (file) (concat subdir file
))
850 (file-name-all-completions ndfile
853 (setq search-path
(cdr search-path
))))
854 ;; Compress out duplicates while building complete list (slloooow!)
855 (let ((sorted (sort (apply 'nconc file-lists
)
856 '(lambda (x y
) (not (string-lessp x y
)))))
859 (if (equal (car sorted
) (car compressed
)) nil
860 (setq compressed
(cons (car sorted
) compressed
)))
861 (setq sorted
(cdr sorted
)))
864 (defvar PC-old-read-file-name-internal nil
)
866 (defun PC-read-include-file-name-internal (string dir action
)
867 (if (string-match "<\\([^\"<>]*\\)>?$" string
)
868 (let* ((name (substring string
(match-beginning 1) (match-end 1)))
869 (str2 (substring string
(match-beginning 0)))
871 (mapcar (function (lambda (x) (list (format "<%s>" x
))))
872 (PC-include-file-all-completions
873 name
(PC-include-file-path)))))
875 ((not completion-table
) nil
)
876 ((eq action nil
) (try-completion str2 completion-table nil
))
877 ((eq action t
) (all-completions str2 completion-table nil
))
879 (eq (try-completion str2 completion-table nil
) t
))))
880 (funcall PC-old-read-file-name-internal string dir action
)))
882 (or PC-disable-includes
883 (memq 'PC-look-for-include-file find-file-not-found-hooks
)
884 (setq find-file-not-found-hooks
(cons 'PC-look-for-include-file
885 find-file-not-found-hooks
)))
887 (or PC-disable-includes
888 PC-old-read-file-name-internal
890 (setq PC-old-read-file-name-internal
891 (symbol-function 'read-file-name-internal
))
892 (fset 'read-file-name-internal
'PC-read-include-file-name-internal
)))