(enum syntaxcode): New members Scomment_fence, Sstring_fence.
[emacs.git] / lisp / complete.el
blobc12e2ce95cb66c423aa381c04821a7f383912d0f
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>
6 ;; Keywords: abbrev
7 ;; Version: 2.02
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)
15 ;; any later version.
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.
27 ;;; Commentary:
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
36 ;; the ambiguity.
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
65 ;; (load "complete")
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.
99 ;;; Code:
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 \"-\", \"_\",
177 \".\", and SPC."
178 (interactive)
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)))
190 (save-excursion
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."
202 (interactive)
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)
208 (if (eobp)
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."
216 (interactive)
217 (if (eq (PC-was-meta-key) PC-meta-flag)
218 (minibuffer-complete-word)
219 (insert " ")
220 (if (eobp)
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."
227 (interactive)
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."
235 (interactive)
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...
241 (exit-minibuffer)
242 (let ((flag (PC-do-completion 'exit)))
243 (and flag
244 (if (or (eq flag 'complete)
245 (not minibuffer-completion-confirm))
246 (exit-minibuffer)
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."
253 (interactive)
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)))
261 (if (integerp key)
262 (>= key 128)
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
275 (function
276 (lambda ()
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))
289 (dirname nil)
290 dirlength
291 (str (buffer-substring beg end))
292 (incname (and filename (string-match "<\\([^\"<>]*\\)>?$" str)))
293 (ambig nil)
294 basestr
295 regex
296 p offset
297 (poss nil)
298 helpposs
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))
304 'complete
306 ;; Record how many characters at the beginning are not included
307 ;; in completion.
308 (setq dirlength
309 (if filename
310 (length (file-name-directory str))
313 ;; Do substitutions in directory names
314 (and filename
315 (not (equal str (setq p (substitute-in-file-name str))))
316 (progn
317 (delete-region beg end)
318 (insert p)
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
329 (and filename
330 (string-match "\\*.*/" str)
331 (let ((pat str)
332 files)
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)
337 (substring pat p))
338 p (+ p 2)))
339 (setq files (PC-expand-many-files (concat pat "*")))
340 (if files
341 (let ((dir (file-name-directory (car files)))
342 (p files))
343 (while (and (setq p (cdr p))
344 (equal dir (file-name-directory (car p)))))
345 (if p
346 (setq filename nil table nil pred nil
347 ambig t)
348 (delete-region beg end)
349 (setq str (concat dir (file-name-nondirectory str)))
350 (insert str)
351 (setq end (+ beg (length str)))))
352 (setq filename nil table nil pred nil))))
354 ;; Strip directory name if appropriate
355 (if filename
356 (if incname
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)))
361 (setq basestr 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)
369 p offset)
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)
373 PC-ndelims-regex
374 PC-delim-regex
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 '(?$ ?^ ?\. ?* ?+ ?? ?[ ?] ?\\))
379 -1 0)))
380 (setq regex (concat (substring regex 0 (+ p bump))
381 PC-ndelims-regex
382 (substring regex (+ p bump)))
383 p (+ p (length PC-ndelims-regex) 1)))))
384 (setq p 0)
385 (if filename
386 (while (setq p (string-match "\\\\\\*" regex p))
387 (setq regex (concat (substring regex 0 p)
388 "[^/]*"
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
401 table
402 pred))
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)
407 table
408 pred)))
409 (setq p compl)
410 (while p
411 (and (string-match regex (car p))
412 (progn
413 (set-text-properties 0 (length (car p)) '() (car p))
414 (setq poss (cons (car p) poss))))
415 (setq p (cdr p)))))
417 ;; Now we have a list of possible completions
418 (cond
420 ;; No valid completions found
421 ((null poss)
422 (if (and (eq mode 'word)
423 (not PC-word-failed-flag))
424 (let ((PC-word-failed-flag t))
425 (delete-backward-char 1)
426 (PC-do-completion 'word))
427 (beep)
428 (PC-temp-minibuffer-message (if ambig
429 " [Ambiguous dir name]"
430 (if (eq mode 'help)
431 " [No completions]"
432 " [No match]")))
433 nil))
435 ;; More than one valid completion found
436 ((or (cdr (setq helpposs poss))
437 (memq mode '(help word)))
439 ;; Handle completion-ignored-extensions
440 (and filename
441 (not (eq mode 'help))
442 (let ((p2 poss))
444 ;; Build a regular expression representing the extensions list
445 (or (equal completion-ignored-extensions PC-ignored-extensions)
446 (setq PC-ignored-regexp
447 (concat "\\("
448 (mapconcat
449 'regexp-quote
450 (setq PC-ignored-extensions
451 completion-ignored-extensions)
452 "\\|")
453 "\\)\\'")))
455 ;; Check if there are any without an ignored extension
456 (setq p nil)
457 (while p2
458 (or (string-match PC-ignored-regexp (car p2))
459 (setq p (cons (car p2) p)))
460 (setq p2 (cdr p2)))
462 ;; If there are "good" names, use them
463 (and p (setq poss p))))
465 ;; Is the actual string one of the possible completions?
466 (setq p (and (not (eq mode 'help)) poss))
467 (while (and p
468 (not (string-equal (car p) basestr)))
469 (setq p (cdr p)))
470 (and p (null mode)
471 (PC-temp-minibuffer-message " [Complete, but not unique]"))
472 (if (and p
473 (not (and (null mode)
474 (eq this-command last-command))))
477 ;; If ambiguous, try for a partial completion
478 (let ((improved nil)
479 prefix
480 (pt nil)
481 (skip "\\`"))
483 ;; Check if next few letters are the same in all cases
484 (if (and (not (eq mode 'help))
485 (setq prefix (try-completion "" (mapcar 'list poss))))
486 (let ((first t) i)
487 (if (eq mode 'word)
488 (setq prefix (PC-chop-word prefix basestr)))
489 (goto-char (+ beg (length dirname)))
490 (while (and (progn
491 (setq i 0)
492 (while (< i (length prefix))
493 (if (and (< (point) end)
494 (eq (aref prefix i)
495 (following-char)))
496 (forward-char 1)
497 (if (and (< (point) end)
498 (or (and (looking-at " ")
499 (memq (aref prefix i)
500 PC-delims-list))
501 (eq (downcase (aref prefix i))
502 (downcase
503 (following-char)))))
504 (progn
505 (delete-char 1)
506 (setq end (1- end)))
507 (and filename (looking-at "\\*")
508 (progn
509 (delete-char 1)
510 (setq end (1- end))))
511 (setq improved t))
512 (insert (substring prefix i (1+ i)))
513 (setq end (1+ end)))
514 (setq i (1+ i)))
515 (or pt (equal (point) beg)
516 (setq pt (point)))
517 (looking-at PC-delim-regex))
518 (setq skip (concat skip
519 (regexp-quote prefix)
520 PC-ndelims-regex)
521 prefix (try-completion
523 (mapcar
524 (function
525 (lambda (x)
526 (list
527 (and (string-match skip x)
528 (substring
530 (match-end 0))))))
531 poss)))
532 (or (> i 0) (> (length prefix) 0))
533 (or (not (eq mode 'word))
534 (and first (> (length prefix) 0)
535 (setq first nil
536 prefix (substring prefix 0 1))))))
537 (goto-char (if (eq mode 'word) end
538 (or pt beg)))))
540 (if (and (eq mode 'word)
541 (not PC-word-failed-flag))
543 (if improved
545 ;; We changed it... would it be complete without the space?
546 (if (PC-is-complete-p (buffer-substring 1 (1- end))
547 table pred)
548 (delete-region (1- end) end)))
550 (if improved
552 ;; We changed it... enough to be complete?
553 (and (eq mode 'exit)
554 (PC-is-complete-p (buffer-string) table pred))
556 ;; If totally ambiguous, display a list of completions
557 (if (or completion-auto-help
558 (eq mode 'help))
559 (with-output-to-temp-buffer "*Completions*"
560 (display-completion-list (sort helpposs 'string-lessp))
561 (save-excursion
562 (set-buffer standard-output)
563 ;; Record which part of the buffer we are completing
564 ;; so that choosing a completion from the list
565 ;; knows how much old text to replace.
566 (setq completion-base-size dirlength)))
567 (PC-temp-minibuffer-message " [Next char not unique]"))
568 nil)))))
570 ;; Only one possible completion
572 (if (equal basestr (car poss))
573 (if (null mode)
574 (PC-temp-minibuffer-message " [Sole completion]"))
575 (delete-region beg end)
576 (insert (format "%s"
577 (if filename
578 (substitute-in-file-name (concat dirname (car poss)))
579 (car poss)))))
580 t)))))
583 (defun PC-is-complete-p (str table pred)
584 (let ((res (if (listp table)
585 (assoc str table)
586 (if (vectorp table)
587 (or (equal str "nil") ; heh, heh, heh
588 (intern-soft str table))
589 (funcall table str pred 'lambda)))))
590 (and res
591 (or (not pred)
592 (and (not (listp table)) (not (vectorp table)))
593 (funcall pred res))
594 res)))
596 (defun PC-chop-word (new old)
597 (let ((i -1)
598 (j -1))
599 (while (and (setq i (string-match PC-delim-regex old (1+ i)))
600 (setq j (string-match PC-delim-regex new (1+ j)))))
601 (if (and j
602 (or (not PC-word-failed-flag)
603 (setq j (string-match PC-delim-regex new (1+ j)))))
604 (substring new 0 (1+ j))
605 new)))
607 (defvar PC-not-minibuffer nil)
609 (defun PC-temp-minibuffer-message (m)
610 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
611 (if PC-not-minibuffer
612 (progn
613 (message m)
614 (sit-for 2)
615 (message ""))
616 (if (fboundp 'temp-minibuffer-message)
617 (temp-minibuffer-message m)
618 (let ((savemax (point-max)))
619 (save-excursion
620 (goto-char (point-max))
621 (insert m))
622 (let ((inhibit-quit t))
623 (sit-for 2)
624 (delete-region savemax (point-max))
625 (if quit-flag
626 (setq quit-flag nil
627 unread-command-char 7)))))))
630 (defun PC-lisp-complete-symbol ()
631 "Perform completion on Lisp symbol preceding point.
632 That symbol is compared against the symbols that exist
633 and any additional characters determined by what is there
634 are inserted.
635 If the symbol starts just after an open-parenthesis,
636 only symbols with function definitions are considered.
637 Otherwise, all symbols with function definitions, values
638 or properties are considered."
639 (interactive)
640 (let* ((end (point))
641 (buffer-syntax (syntax-table))
642 (beg (unwind-protect
643 (save-excursion
644 (if lisp-mode-syntax-table
645 (set-syntax-table lisp-mode-syntax-table))
646 (backward-sexp 1)
647 (while (= (char-syntax (following-char)) ?\')
648 (forward-char 1))
649 (point))
650 (set-syntax-table buffer-syntax)))
651 (minibuffer-completion-table obarray)
652 (minibuffer-completion-predicate
653 (if (eq (char-after (1- beg)) ?\()
654 'fboundp
655 (function (lambda (sym)
656 (or (boundp sym) (fboundp sym)
657 (symbol-plist sym))))))
658 (PC-not-minibuffer t))
659 (PC-do-completion nil beg end)))
662 ;;; Wildcards in `C-x C-f' command. This is independent from the main
663 ;;; completion code, except for `PC-expand-many-files' which is called
664 ;;; when "*"'s are found in the path during filename completion. (The
665 ;;; above completion code always understands "*"'s, except in file paths,
666 ;;; without relying on the following code.)
668 (defvar PC-many-files-list nil)
670 (defun PC-try-load-many-files ()
671 (if (string-match "\\*" buffer-file-name)
672 (let* ((pat buffer-file-name)
673 (files (PC-expand-many-files pat))
674 (first (car files))
675 (next files))
676 (kill-buffer (current-buffer))
677 (or files
678 (error "No matching files"))
679 ;; Bring the other files (not the first) into buffers.
680 (save-window-excursion
681 (while (setq next (cdr next))
682 (let ((buf (find-file-noselect (car next))))
683 ;; Put this buffer at the front of the buffer list.
684 (switch-to-buffer buf))))
685 ;; This modifies the `buf' variable inside find-file-noselect.
686 (setq buf (get-file-buffer first))
687 (if buf
688 nil ; should do verify-visited-file-modtime stuff.
689 (setq filename first)
690 (setq buf (create-file-buffer filename))
691 ;; This modified `truename' inside find-file-noselect.
692 (setq truename (abbreviate-file-name (file-truename filename)))
693 (set-buffer buf)
694 (erase-buffer)
695 (insert-file-contents filename t))
696 (if (cdr files)
697 (setq PC-many-files-list (mapconcat
698 (if (string-match "\\*.*/" pat)
699 'identity
700 'file-name-nondirectory)
701 (cdr files) ", ")
702 find-file-hooks (cons 'PC-after-load-many-files
703 find-file-hooks)))
704 ;; This modifies the "error" variable inside find-file-noselect.
705 (setq error nil)
707 nil))
709 (defun PC-after-load-many-files ()
710 (setq find-file-hooks (delq 'PC-after-load-many-files find-file-hooks))
711 (message "Also loaded %s." PC-many-files-list))
713 (defun PC-expand-many-files (name)
714 (save-excursion
715 (set-buffer (generate-new-buffer " *Glob Output*"))
716 (erase-buffer)
717 (shell-command (concat "echo " name) t)
718 (goto-char (point-min))
719 (if (looking-at ".*No match")
721 (insert "(\"")
722 (while (search-forward " " nil t)
723 (delete-backward-char 1)
724 (insert "\" \""))
725 (goto-char (point-max))
726 (delete-backward-char 1)
727 (insert "\")")
728 (goto-char (point-min))
729 (let ((files (read (current-buffer))))
730 (kill-buffer (current-buffer))
731 files))))
733 (or PC-disable-wildcards
734 (memq 'PC-try-load-many-files find-file-not-found-hooks)
735 (setq find-file-not-found-hooks (cons 'PC-try-load-many-files
736 find-file-not-found-hooks)))
740 ;;; Facilities for loading C header files. This is independent from the
741 ;;; main completion code. See also the variable `PC-include-file-path'
742 ;;; at top of this file.
744 (defun PC-look-for-include-file ()
745 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
746 (let ((name (substring (buffer-file-name)
747 (match-beginning 1) (match-end 1)))
748 (punc (aref (buffer-file-name) (match-beginning 0)))
749 (path nil)
750 new-buf)
751 (kill-buffer (current-buffer))
752 (if (equal name "")
753 (save-excursion
754 (set-buffer (car (buffer-list)))
755 (save-excursion
756 (beginning-of-line)
757 (if (looking-at
758 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
759 (setq name (buffer-substring (match-beginning 1)
760 (match-end 1))
761 punc (char-after (1- (match-beginning 1))))
762 ;; Suggested by Frank Siebenlist:
763 (if (or (looking-at
764 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
765 (looking-at
766 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
767 (looking-at
768 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
769 (progn
770 (setq name (buffer-substring (match-beginning 1)
771 (match-end 1))
772 punc ?\<
773 path load-path)
774 (if (string-match "\\.elc$" name)
775 (setq name (substring name 0 -1))
776 (or (string-match "\\.el$" name)
777 (setq name (concat name ".el")))))
778 (error "Not on an #include line"))))))
779 (or (string-match "\\.[a-zA-Z0-9]+$" name)
780 (setq name (concat name ".h")))
781 (if (eq punc ?\<)
782 (let ((path (or path (PC-include-file-path))))
783 (while (and path
784 (not (file-exists-p
785 (concat (file-name-as-directory (car path))
786 name))))
787 (setq path (cdr path)))
788 (if path
789 (setq name (concat (file-name-as-directory (car path)) name))
790 (error "No such include file: <%s>" name)))
791 (let ((dir (save-excursion
792 (set-buffer (car (buffer-list)))
793 default-directory)))
794 (if (file-exists-p (concat dir name))
795 (setq name (concat dir name))
796 (error "No such include file: \"%s\"" name))))
797 (setq new-buf (get-file-buffer name))
798 (if new-buf
799 ;; no need to verify last-modified time for this!
800 (set-buffer new-buf)
801 (setq new-buf (create-file-buffer name))
802 (set-buffer new-buf)
803 (erase-buffer)
804 (insert-file-contents name t))
805 (setq filename name
806 error nil
807 buf new-buf)
809 nil))
811 (defun PC-include-file-path ()
812 (or PC-include-file-path
813 (let ((env (getenv "INCPATH"))
814 (path nil)
815 pos)
816 (or env (error "No include file path specified"))
817 (while (setq pos (string-match ":[^:]+$" env))
818 (setq path (cons (substring env (1+ pos)) path)
819 env (substring env 0 pos)))
820 path)))
822 ;;; This is adapted from lib-complete.el, by Mike Williams.
823 (defun PC-include-file-all-completions (file search-path &optional full)
824 "Return all completions for FILE in any directory on SEARCH-PATH.
825 If optional third argument FULL is non-nil, returned pathnames should be
826 absolute rather than relative to some directory on the SEARCH-PATH."
827 (setq search-path
828 (mapcar '(lambda (dir)
829 (if dir (file-name-as-directory dir) default-directory))
830 search-path))
831 (if (file-name-absolute-p file)
832 ;; It's an absolute file name, so don't need search-path
833 (progn
834 (setq file (expand-file-name file))
835 (file-name-all-completions
836 (file-name-nondirectory file) (file-name-directory file)))
837 (let ((subdir (file-name-directory file))
838 (ndfile (file-name-nondirectory file))
839 file-lists)
840 ;; Append subdirectory part to each element of search-path
841 (if subdir
842 (setq search-path
843 (mapcar '(lambda (dir) (concat dir subdir))
844 search-path)
845 file ))
846 ;; Make list of completions in each directory on search-path
847 (while search-path
848 (let* ((dir (car search-path))
849 (subdir (if full dir subdir)))
850 (if (file-directory-p dir)
851 (progn
852 (setq file-lists
853 (cons
854 (mapcar '(lambda (file) (concat subdir file))
855 (file-name-all-completions ndfile
856 (car search-path)))
857 file-lists))))
858 (setq search-path (cdr search-path))))
859 ;; Compress out duplicates while building complete list (slloooow!)
860 (let ((sorted (sort (apply 'nconc file-lists)
861 '(lambda (x y) (not (string-lessp x y)))))
862 compressed)
863 (while sorted
864 (if (equal (car sorted) (car compressed)) nil
865 (setq compressed (cons (car sorted) compressed)))
866 (setq sorted (cdr sorted)))
867 compressed))))
869 (defvar PC-old-read-file-name-internal nil)
871 (defun PC-read-include-file-name-internal (string dir action)
872 (if (string-match "<\\([^\"<>]*\\)>?$" string)
873 (let* ((name (substring string (match-beginning 1) (match-end 1)))
874 (str2 (substring string (match-beginning 0)))
875 (completion-table
876 (mapcar (function (lambda (x) (list (format "<%s>" x))))
877 (PC-include-file-all-completions
878 name (PC-include-file-path)))))
879 (cond
880 ((not completion-table) nil)
881 ((eq action nil) (try-completion str2 completion-table nil))
882 ((eq action t) (all-completions str2 completion-table nil))
883 ((eq action 'lambda)
884 (eq (try-completion str2 completion-table nil) t))))
885 (funcall PC-old-read-file-name-internal string dir action)))
887 (or PC-disable-includes
888 (memq 'PC-look-for-include-file find-file-not-found-hooks)
889 (setq find-file-not-found-hooks (cons 'PC-look-for-include-file
890 find-file-not-found-hooks)))
892 (or PC-disable-includes
893 PC-old-read-file-name-internal
894 (progn
895 (setq PC-old-read-file-name-internal
896 (symbol-function 'read-file-name-internal))
897 (fset 'read-file-name-internal 'PC-read-include-file-name-internal)))
900 (provide 'complete)
902 ;;; End.