Update FSF's address.
[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
7 ;; Version: 2.02
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: Load this file. Now, 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 ;; (load "complete")
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 (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.")
104
105
106 (defvar PC-word-delimiters "-_. "
107 "*A string of characters which are to be treated as word delimiters
108 by the Partial Completion system.
109
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').")
115
116
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.")
122
123
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.")
127
128
129 (defvar PC-disable-wildcards nil
130 "Set this to non-nil to disable wildcard support in \\[find-file].")
131
132 (defvar PC-disable-includes nil
133 "Set this to non-nil to disable include-file support in \\[find-file].")
134
135
136 (defvar PC-default-bindings t
137 "Set this to nil to suppress the default partial completion key bindings.")
138
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)
143
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)
149
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)
155
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)
161
162 (define-key global-map "\e\t" 'PC-lisp-complete-symbol)
163 ))
164
165
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\".
171
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.
175
176 Word-delimiters for the purposes of Partial Completion are \"-\", \"_\",
177 \".\", and SPC."
178 (interactive)
179 (if (PC-was-meta-key)
180 (minibuffer-complete)
181 (PC-do-completion nil)))
182
183
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."
188 (interactive)
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)
194 (if (eobp)
195 (PC-do-completion 'word))))
196
197
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."
202 (interactive)
203 (if (eq (PC-was-meta-key) PC-meta-flag)
204 (minibuffer-complete-word)
205 (insert " ")
206 (if (eobp)
207 (PC-do-completion 'word))))
208
209
210 (defun PC-complete-and-exit ()
211 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
212 See `PC-complete' for details."
213 (interactive)
214 (if (eq (PC-was-meta-key) PC-meta-flag)
215 (minibuffer-complete-and-exit)
216 (PC-do-complete-and-exit)))
217
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."
221 (interactive)
222 (let ((minibuffer-completion-confirm nil))
223 (PC-do-complete-and-exit)))
224
225 (defun PC-do-complete-and-exit ()
226 (if (= (buffer-size) 0) ; Duplicate the "bug" that Info-menu relies on...
227 (exit-minibuffer)
228 (let ((flag (PC-do-completion 'exit)))
229 (and flag
230 (if (or (eq flag 'complete)
231 (not minibuffer-completion-confirm))
232 (exit-minibuffer)
233 (PC-temp-minibuffer-message " [Confirm]"))))))
234
235
236 (defun PC-completion-help ()
237 "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
238 See `PC-complete' for details."
239 (interactive)
240 (if (eq (PC-was-meta-key) PC-meta-flag)
241 (minibuffer-completion-help)
242 (PC-do-completion 'help)))
243
244 (defun PC-was-meta-key ()
245 (or (/= (length (this-command-keys)) 1)
246 (let ((key (aref (this-command-keys) 0)))
247 (if (integerp key)
248 (>= key 128)
249 (not (null (memq 'meta (event-modifiers key))))))))
250
251
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)
259
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)))
267 (dirname nil)
268 dirlength
269 (str (buffer-substring beg end))
270 (incname (and filename (string-match "<\\([^\"<>]*\\)>?$" str)))
271 (ambig nil)
272 basestr
273 regex
274 p offset
275 (poss nil)
276 helpposs
277 (case-fold-search completion-ignore-case))
278
279 ;; Check if buffer contents can already be considered complete
280 (if (and (eq mode 'exit)
281 (PC-is-complete-p str table pred))
282 'complete
283
284 ;; Record how many characters at the beginning are not included
285 ;; in completion.
286 (setq dirlength
287 (if filename
288 (length (file-name-directory str))
289 0))
290
291 ;; Do substitutions in directory names
292 (and filename
293 (not (equal str (setq p (substitute-in-file-name str))))
294 (progn
295 (delete-region beg end)
296 (insert p)
297 (setq str p end (+ beg (length str)))))
298
299 ;; Prepare various delimiter strings
300 (or (equal PC-word-delimiters PC-delims)
301 (setq PC-delims PC-word-delimiters
302 PC-delim-regex (concat "[" PC-delims "]")
303 PC-ndelims-regex (concat "[^" PC-delims "]*")
304 PC-delims-list (append PC-delims nil)))
305
306 ;; Look for wildcard expansions in directory name
307 (and filename
308 (string-match "\\*.*/" str)
309 (let ((pat str)
310 files)
311 (setq p (1+ (string-match "/[^/]*\\'" pat)))
312 (while (setq p (string-match PC-delim-regex pat p))
313 (setq pat (concat (substring pat 0 p)
314 "*"
315 (substring pat p))
316 p (+ p 2)))
317 (setq files (PC-expand-many-files (concat pat "*")))
318 (if files
319 (let ((dir (file-name-directory (car files)))
320 (p files))
321 (while (and (setq p (cdr p))
322 (equal dir (file-name-directory (car p)))))
323 (if p
324 (setq filename nil table nil pred nil
325 ambig t)
326 (delete-region beg end)
327 (setq str (concat dir (file-name-nondirectory str)))
328 (insert str)
329 (setq end (+ beg (length str)))))
330 (setq filename nil table nil pred nil))))
331
332 ;; Strip directory name if appropriate
333 (if filename
334 (if incname
335 (setq basestr (substring str incname)
336 dirname (substring str 0 incname))
337 (setq basestr (file-name-nondirectory str)
338 dirname (file-name-directory str)))
339 (setq basestr str))
340
341 ;; Convert search pattern to a standard regular expression
342 (setq regex (regexp-quote basestr)
343 offset (if (and (> (length regex) 0)
344 (not (eq (aref basestr 0) ?\*))
345 (or (eq PC-first-char t)
346 (and PC-first-char filename))) 1 0)
347 p offset)
348 (while (setq p (string-match PC-delim-regex regex p))
349 (if (eq (aref regex p) ? )
350 (setq regex (concat (substring regex 0 p)
351 PC-ndelims-regex
352 PC-delim-regex
353 (substring regex (1+ p)))
354 p (+ p (length PC-ndelims-regex) (length PC-delim-regex)))
355 (let ((bump (if (memq (aref regex p)
356 '(?$ ?^ ?\. ?* ?+ ?? ?[ ?] ?\\))
357 -1 0)))
358 (setq regex (concat (substring regex 0 (+ p bump))
359 PC-ndelims-regex
360 (substring regex (+ p bump)))
361 p (+ p (length PC-ndelims-regex) 1)))))
362 (setq p 0)
363 (if filename
364 (while (setq p (string-match "\\\\\\*" regex p))
365 (setq regex (concat (substring regex 0 p)
366 "[^/]*"
367 (substring regex (+ p 2))))))
368 ;;(setq the-regex regex)
369 (setq regex (concat "\\`" regex))
370
371 ;; Find an initial list of possible completions
372 (if (not (setq p (string-match (concat PC-delim-regex
373 (if filename "\\|\\*" ""))
374 str
375 (+ (length dirname) offset))))
376
377 ;; Minibuffer contains no hyphens -- simple case!
378 (setq poss (all-completions str
379 table
380 pred))
381
382 ;; Use all-completions to do an initial cull. This is a big win,
383 ;; since all-completions is written in C!
384 (let ((compl (all-completions (substring str 0 p)
385 table
386 pred)))
387 (setq p compl)
388 (while p
389 (and (string-match regex (car p))
390 (setq poss (cons (car p) poss)))
391 (setq p (cdr p)))))
392
393 ;; Now we have a list of possible completions
394 (cond
395
396 ;; No valid completions found
397 ((null poss)
398 (if (and (eq mode 'word)
399 (not PC-word-failed-flag))
400 (let ((PC-word-failed-flag t))
401 (delete-backward-char 1)
402 (PC-do-completion 'word))
403 (beep)
404 (PC-temp-minibuffer-message (if ambig
405 " [Ambiguous dir name]"
406 (if (eq mode 'help)
407 " [No completions]"
408 " [No match]")))
409 nil))
410
411 ;; More than one valid completion found
412 ((or (cdr (setq helpposs poss))
413 (memq mode '(help word)))
414
415 ;; Handle completion-ignored-extensions
416 (and filename
417 (not (eq mode 'help))
418 (let ((p2 poss))
419
420 ;; Build a regular expression representing the extensions list
421 (or (equal completion-ignored-extensions PC-ignored-extensions)
422 (setq PC-ignored-regexp
423 (concat "\\("
424 (mapconcat
425 'regexp-quote
426 (setq PC-ignored-extensions
427 completion-ignored-extensions)
428 "\\|")
429 "\\)\\'")))
430
431 ;; Check if there are any without an ignored extension
432 (setq p nil)
433 (while p2
434 (or (string-match PC-ignored-regexp (car p2))
435 (setq p (cons (car p2) p)))
436 (setq p2 (cdr p2)))
437
438 ;; If there are "good" names, use them
439 (and p (setq poss p))))
440
441 ;; Is the actual string one of the possible completions?
442 (setq p (and (not (eq mode 'help)) poss))
443 (while (and p
444 (not (equal (car p) basestr)))
445 (setq p (cdr p)))
446 (and p (null mode)
447 (PC-temp-minibuffer-message " [Complete, but not unique]"))
448 (if (and p
449 (not (and (null mode)
450 (eq this-command last-command))))
451 t
452
453 ;; If ambiguous, try for a partial completion
454 (let ((improved nil)
455 prefix
456 (pt nil)
457 (skip "\\`"))
458
459 ;; Check if next few letters are the same in all cases
460 (if (and (not (eq mode 'help))
461 (setq prefix (try-completion "" (mapcar 'list poss))))
462 (let ((first t) i)
463 (if (eq mode 'word)
464 (setq prefix (PC-chop-word prefix basestr)))
465 (goto-char (+ beg (length dirname)))
466 (while (and (progn
467 (setq i 0)
468 (while (< i (length prefix))
469 (if (and (< (point) end)
470 (eq (aref prefix i)
471 (following-char)))
472 (forward-char 1)
473 (if (and (< (point) end)
474 (or (and (looking-at " ")
475 (memq (aref prefix i)
476 PC-delims-list))
477 (eq (downcase (aref prefix i))
478 (downcase
479 (following-char)))))
480 (progn
481 (delete-char 1)
482 (setq end (1- end)))
483 (and filename (looking-at "\\*")
484 (progn
485 (delete-char 1)
486 (setq end (1- end))))
487 (setq improved t))
488 ;; Use format to discard text properties.
489 (insert (format "%s" (substring prefix i (1+ i))))
490 (setq end (1+ end)))
491 (setq i (1+ i)))
492 (or pt (equal (point) beg)
493 (setq pt (point)))
494 (looking-at PC-delim-regex))
495 (setq skip (concat skip
496 (regexp-quote prefix)
497 PC-ndelims-regex)
498 prefix (try-completion
499 ""
500 (mapcar
501 (function
502 (lambda (x)
503 (list
504 (and (string-match skip x)
505 (substring
506 x
507 (match-end 0))))))
508 poss)))
509 (or (> i 0) (> (length prefix) 0))
510 (or (not (eq mode 'word))
511 (and first (> (length prefix) 0)
512 (setq first nil
513 prefix (substring prefix 0 1))))))
514 (goto-char (if (eq mode 'word) end
515 (or pt beg)))))
516
517 (if (and (eq mode 'word)
518 (not PC-word-failed-flag))
519
520 (if improved
521
522 ;; We changed it... would it be complete without the space?
523 (if (PC-is-complete-p (buffer-substring 1 (1- end))
524 table pred)
525 (delete-region (1- end) end)))
526
527 (if improved
528
529 ;; We changed it... enough to be complete?
530 (and (eq mode 'exit)
531 (PC-is-complete-p (buffer-string) table pred))
532
533 ;; If totally ambiguous, display a list of completions
534 (if (or completion-auto-help
535 (eq mode 'help))
536 (with-output-to-temp-buffer "*Completions*"
537 (display-completion-list (sort helpposs 'string-lessp))
538 (save-excursion
539 (set-buffer standard-output)
540 ;; Record which part of the buffer we are completing
541 ;; so that choosing a completion from the list
542 ;; knows how much old text to replace.
543 (setq completion-base-size dirlength)))
544 (PC-temp-minibuffer-message " [Next char not unique]"))
545 nil)))))
546
547 ;; Only one possible completion
548 (t
549 (if (equal basestr (car poss))
550 (if (null mode)
551 (PC-temp-minibuffer-message " [Sole completion]"))
552 (delete-region beg end)
553 (insert (format "%s"
554 (if filename
555 (substitute-in-file-name (concat dirname (car poss)))
556 (car poss)))))
557 t)))))
558
559
560 (defun PC-is-complete-p (str table pred)
561 (let ((res (if (listp table)
562 (assoc str table)
563 (if (vectorp table)
564 (or (equal str "nil") ; heh, heh, heh
565 (intern-soft str table))
566 (funcall table str pred 'lambda)))))
567 (and res
568 (or (not pred)
569 (and (not (listp table)) (not (vectorp table)))
570 (funcall pred res))
571 res)))
572
573 (defun PC-chop-word (new old)
574 (let ((i -1)
575 (j -1))
576 (while (and (setq i (string-match PC-delim-regex old (1+ i)))
577 (setq j (string-match PC-delim-regex new (1+ j)))))
578 (if (and j
579 (or (not PC-word-failed-flag)
580 (setq j (string-match PC-delim-regex new (1+ j)))))
581 (substring new 0 (1+ j))
582 new)))
583
584 (defvar PC-not-minibuffer nil)
585
586 (defun PC-temp-minibuffer-message (m)
587 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
588 (if PC-not-minibuffer
589 (progn
590 (message m)
591 (sit-for 2)
592 (message ""))
593 (if (fboundp 'temp-minibuffer-message)
594 (temp-minibuffer-message m)
595 (let ((savemax (point-max)))
596 (save-excursion
597 (goto-char (point-max))
598 (insert m))
599 (let ((inhibit-quit t))
600 (sit-for 2)
601 (delete-region savemax (point-max))
602 (if quit-flag
603 (setq quit-flag nil
604 unread-command-char 7)))))))
605
606
607 (defun PC-lisp-complete-symbol ()
608 "Perform completion on Lisp symbol preceding point.
609 That symbol is compared against the symbols that exist
610 and any additional characters determined by what is there
611 are inserted.
612 If the symbol starts just after an open-parenthesis,
613 only symbols with function definitions are considered.
614 Otherwise, all symbols with function definitions, values
615 or properties are considered."
616 (interactive)
617 (let* ((end (point))
618 (buffer-syntax (syntax-table))
619 (beg (unwind-protect
620 (save-excursion
621 (if lisp-mode-syntax-table
622 (set-syntax-table lisp-mode-syntax-table))
623 (backward-sexp 1)
624 (while (= (char-syntax (following-char)) ?\')
625 (forward-char 1))
626 (point))
627 (set-syntax-table buffer-syntax)))
628 (minibuffer-completion-table obarray)
629 (minibuffer-completion-predicate
630 (if (eq (char-after (1- beg)) ?\()
631 'fboundp
632 (function (lambda (sym)
633 (or (boundp sym) (fboundp sym)
634 (symbol-plist sym))))))
635 (PC-not-minibuffer t))
636 (PC-do-completion nil beg end)))
637
638
639 ;;; Wildcards in `C-x C-f' command. This is independent from the main
640 ;;; completion code, except for `PC-expand-many-files' which is called
641 ;;; when "*"'s are found in the path during filename completion. (The
642 ;;; above completion code always understands "*"'s, except in file paths,
643 ;;; without relying on the following code.)
644
645 (defvar PC-many-files-list nil)
646
647 (defun PC-try-load-many-files ()
648 (if (string-match "\\*" buffer-file-name)
649 (let* ((pat buffer-file-name)
650 (files (PC-expand-many-files pat))
651 (first (car files))
652 (next files))
653 (kill-buffer (current-buffer))
654 (or files
655 (error "No matching files"))
656 (save-window-excursion
657 (while (setq next (cdr next))
658 (let ((buf (find-file-noselect (car next))))
659 (switch-to-buffer buf))))
660 ;; This modifies the "buf" variable inside find-file-noselect.
661 (setq buf (get-file-buffer first))
662 (if buf
663 nil ; should do verify-visited-file-modtime stuff.
664 (setq filename first)
665 (setq buf (create-file-buffer filename))
666 (set-buffer buf)
667 (erase-buffer)
668 (insert-file-contents filename t))
669 (if (cdr files)
670 (setq PC-many-files-list (mapconcat
671 (if (string-match "\\*.*/" pat)
672 'identity
673 'file-name-nondirectory)
674 (cdr files) ", ")
675 find-file-hooks (cons 'PC-after-load-many-files
676 find-file-hooks)))
677 ;; This modifies the "error" variable inside find-file-noselect.
678 (setq error nil)
679 t)
680 nil))
681
682 (defun PC-after-load-many-files ()
683 (setq find-file-hooks (delq 'PC-after-load-many-files find-file-hooks))
684 (message "Also loaded %s." PC-many-files-list))
685
686 (defun PC-expand-many-files (name)
687 (save-excursion
688 (set-buffer (generate-new-buffer " *Glob Output*"))
689 (erase-buffer)
690 (shell-command (concat "echo " name) t)
691 (goto-char (point-min))
692 (if (looking-at ".*No match")
693 nil
694 (insert "(\"")
695 (while (search-forward " " nil t)
696 (delete-backward-char 1)
697 (insert "\" \""))
698 (goto-char (point-max))
699 (delete-backward-char 1)
700 (insert "\")")
701 (goto-char (point-min))
702 (let ((files (read (current-buffer))))
703 (kill-buffer (current-buffer))
704 files))))
705
706 (or PC-disable-wildcards
707 (memq 'PC-try-load-many-files find-file-not-found-hooks)
708 (setq find-file-not-found-hooks (cons 'PC-try-load-many-files
709 find-file-not-found-hooks)))
710
711
712
713 ;;; Facilities for loading C header files. This is independent from the
714 ;;; main completion code. See also the variable `PC-include-file-path'
715 ;;; at top of this file.
716
717 (defun PC-look-for-include-file ()
718 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
719 (let ((name (substring (buffer-file-name)
720 (match-beginning 1) (match-end 1)))
721 (punc (aref (buffer-file-name) (match-beginning 0)))
722 (path nil)
723 new-buf)
724 (kill-buffer (current-buffer))
725 (if (equal name "")
726 (save-excursion
727 (set-buffer (car (buffer-list)))
728 (save-excursion
729 (beginning-of-line)
730 (if (looking-at
731 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
732 (setq name (buffer-substring (match-beginning 1)
733 (match-end 1))
734 punc (char-after (1- (match-beginning 1))))
735 ;; Suggested by Frank Siebenlist:
736 (if (or (looking-at
737 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
738 (looking-at
739 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
740 (looking-at
741 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
742 (progn
743 (setq name (buffer-substring (match-beginning 1)
744 (match-end 1))
745 punc ?\<
746 path load-path)
747 (if (string-match "\\.elc$" name)
748 (setq name (substring name 0 -1))
749 (or (string-match "\\.el$" name)
750 (setq name (concat name ".el")))))
751 (error "Not on an #include line"))))))
752 (or (string-match "\\.[a-zA-Z0-9]+$" name)
753 (setq name (concat name ".h")))
754 (if (eq punc ?\<)
755 (let ((path (or path (PC-include-file-path))))
756 (while (and path
757 (not (file-exists-p
758 (concat (file-name-as-directory (car path))
759 name))))
760 (setq path (cdr path)))
761 (if path
762 (setq name (concat (file-name-as-directory (car path)) name))
763 (error "No such include file: <%s>" name)))
764 (let ((dir (save-excursion
765 (set-buffer (car (buffer-list)))
766 default-directory)))
767 (if (file-exists-p (concat dir name))
768 (setq name (concat dir name))
769 (error "No such include file: \"%s\"" name))))
770 (setq new-buf (get-file-buffer name))
771 (if new-buf
772 ;; no need to verify last-modified time for this!
773 (set-buffer new-buf)
774 (setq new-buf (create-file-buffer name))
775 (set-buffer new-buf)
776 (erase-buffer)
777 (insert-file-contents name t))
778 (setq filename name
779 error nil
780 buf new-buf)
781 t)
782 nil))
783
784 (defun PC-include-file-path ()
785 (or PC-include-file-path
786 (let ((env (getenv "INCPATH"))
787 (path nil)
788 pos)
789 (or env (error "No include file path specified"))
790 (while (setq pos (string-match ":[^:]+$" env))
791 (setq path (cons (substring env (1+ pos)) path)
792 env (substring env 0 pos)))
793 path)))
794
795 ;;; This is adapted from lib-complete.el, by Mike Williams.
796 (defun PC-include-file-all-completions (file search-path &optional full)
797 "Return all completions for FILE in any directory on SEARCH-PATH.
798 If optional third argument FULL is non-nil, returned pathnames should be
799 absolute rather than relative to some directory on the SEARCH-PATH."
800 (setq search-path
801 (mapcar '(lambda (dir)
802 (if dir (file-name-as-directory dir) default-directory))
803 search-path))
804 (if (file-name-absolute-p file)
805 ;; It's an absolute file name, so don't need search-path
806 (progn
807 (setq file (expand-file-name file))
808 (file-name-all-completions
809 (file-name-nondirectory file) (file-name-directory file)))
810 (let ((subdir (file-name-directory file))
811 (ndfile (file-name-nondirectory file))
812 file-lists)
813 ;; Append subdirectory part to each element of search-path
814 (if subdir
815 (setq search-path
816 (mapcar '(lambda (dir) (concat dir subdir))
817 search-path)
818 file ))
819 ;; Make list of completions in each directory on search-path
820 (while search-path
821 (let* ((dir (car search-path))
822 (subdir (if full dir subdir)))
823 (if (file-directory-p dir)
824 (progn
825 (setq file-lists
826 (cons
827 (mapcar '(lambda (file) (concat subdir file))
828 (file-name-all-completions ndfile
829 (car search-path)))
830 file-lists))))
831 (setq search-path (cdr search-path))))
832 ;; Compress out duplicates while building complete list (slloooow!)
833 (let ((sorted (sort (apply 'nconc file-lists)
834 '(lambda (x y) (not (string-lessp x y)))))
835 compressed)
836 (while sorted
837 (if (equal (car sorted) (car compressed)) nil
838 (setq compressed (cons (car sorted) compressed)))
839 (setq sorted (cdr sorted)))
840 compressed))))
841
842 (defvar PC-old-read-file-name-internal nil)
843
844 (defun PC-read-include-file-name-internal (string dir action)
845 (if (string-match "<\\([^\"<>]*\\)>?$" string)
846 (let* ((name (substring string (match-beginning 1) (match-end 1)))
847 (str2 (substring string (match-beginning 0)))
848 (completion-table
849 (mapcar (function (lambda (x) (list (format "<%s>" x))))
850 (PC-include-file-all-completions
851 name (PC-include-file-path)))))
852 (cond
853 ((not completion-table) nil)
854 ((eq action nil) (try-completion str2 completion-table nil))
855 ((eq action t) (all-completions str2 completion-table nil))
856 ((eq action 'lambda)
857 (eq (try-completion str2 completion-table nil) t))))
858 (funcall PC-old-read-file-name-internal string dir action)))
859
860 (or PC-disable-includes
861 (memq 'PC-look-for-include-file find-file-not-found-hooks)
862 (setq find-file-not-found-hooks (cons 'PC-look-for-include-file
863 find-file-not-found-hooks)))
864
865 (or PC-disable-includes
866 PC-old-read-file-name-internal
867 (progn
868 (setq PC-old-read-file-name-internal
869 (symbol-function 'read-file-name-internal))
870 (fset 'read-file-name-internal 'PC-read-include-file-name-internal)))
871
872 \f
873 (provide 'complete)
874
875 ;;; End.