(view-mode, view-mode-enter): Doc changes.
[emacs.git] / lisp / complete.el
blob3230524e730f7f43d381677f37a4159f66042468
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
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;; Commentary:
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
35 ;; the ambiguity.
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
64 ;; (load "complete")
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.
98 ;;; Code:
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 \"-\", \"_\",
176 \".\", and SPC."
177 (interactive)
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."
187 (interactive)
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)
193 (if (eobp)
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."
201 (interactive)
202 (if (eq (PC-was-meta-key) PC-meta-flag)
203 (minibuffer-complete-word)
204 (insert " ")
205 (if (eobp)
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."
212 (interactive)
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."
220 (interactive)
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...
226 (exit-minibuffer)
227 (let ((flag (PC-do-completion 'exit)))
228 (and flag
229 (if (or (eq flag 'complete)
230 (not minibuffer-completion-confirm))
231 (exit-minibuffer)
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."
238 (interactive)
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)))
246 (if (integerp key)
247 (>= key 128)
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)))
266 (dirname nil)
267 dirlength
268 (str (buffer-substring beg end))
269 (incname (and filename (string-match "<\\([^\"<>]*\\)>?$" str)))
270 (ambig nil)
271 basestr
272 regex
273 p offset
274 (poss nil)
275 helpposs
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))
281 'complete
283 ;; Record how many characters at the beginning are not included
284 ;; in completion.
285 (setq dirlength
286 (if filename
287 (length (file-name-directory str))
290 ;; Do substitutions in directory names
291 (and filename
292 (not (equal str (setq p (substitute-in-file-name str))))
293 (progn
294 (delete-region beg end)
295 (insert p)
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
306 (and filename
307 (string-match "\\*.*/" str)
308 (let ((pat str)
309 files)
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)
314 (substring pat p))
315 p (+ p 2)))
316 (setq files (PC-expand-many-files (concat pat "*")))
317 (if files
318 (let ((dir (file-name-directory (car files)))
319 (p files))
320 (while (and (setq p (cdr p))
321 (equal dir (file-name-directory (car p)))))
322 (if p
323 (setq filename nil table nil pred nil
324 ambig t)
325 (delete-region beg end)
326 (setq str (concat dir (file-name-nondirectory str)))
327 (insert str)
328 (setq end (+ beg (length str)))))
329 (setq filename nil table nil pred nil))))
331 ;; Strip directory name if appropriate
332 (if filename
333 (if incname
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)))
338 (setq basestr 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)
346 p offset)
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)
350 PC-ndelims-regex
351 PC-delim-regex
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 '(?$ ?^ ?\. ?* ?+ ?? ?[ ?] ?\\))
356 -1 0)))
357 (setq regex (concat (substring regex 0 (+ p bump))
358 PC-ndelims-regex
359 (substring regex (+ p bump)))
360 p (+ p (length PC-ndelims-regex) 1)))))
361 (setq p 0)
362 (if filename
363 (while (setq p (string-match "\\\\\\*" regex p))
364 (setq regex (concat (substring regex 0 p)
365 "[^/]*"
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
378 table
379 pred))
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)
384 table
385 pred)))
386 (setq p compl)
387 (while p
388 (and (string-match regex (car p))
389 (setq poss (cons (car p) poss)))
390 (setq p (cdr p)))))
392 ;; Now we have a list of possible completions
393 (cond
395 ;; No valid completions found
396 ((null poss)
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))
402 (beep)
403 (PC-temp-minibuffer-message (if ambig
404 " [Ambiguous dir name]"
405 (if (eq mode 'help)
406 " [No completions]"
407 " [No match]")))
408 nil))
410 ;; More than one valid completion found
411 ((or (cdr (setq helpposs poss))
412 (memq mode '(help word)))
414 ;; Handle completion-ignored-extensions
415 (and filename
416 (not (eq mode 'help))
417 (let ((p2 poss))
419 ;; Build a regular expression representing the extensions list
420 (or (equal completion-ignored-extensions PC-ignored-extensions)
421 (setq PC-ignored-regexp
422 (concat "\\("
423 (mapconcat
424 'regexp-quote
425 (setq PC-ignored-extensions
426 completion-ignored-extensions)
427 "\\|")
428 "\\)\\'")))
430 ;; Check if there are any without an ignored extension
431 (setq p nil)
432 (while p2
433 (or (string-match PC-ignored-regexp (car p2))
434 (setq p (cons (car p2) p)))
435 (setq p2 (cdr p2)))
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))
442 (while (and p
443 (not (equal (car p) basestr)))
444 (setq p (cdr p)))
445 (and p (null mode)
446 (PC-temp-minibuffer-message " [Complete, but not unique]"))
447 (if (and p
448 (not (and (null mode)
449 (eq this-command last-command))))
452 ;; If ambiguous, try for a partial completion
453 (let ((improved nil)
454 prefix
455 (pt nil)
456 (skip "\\`"))
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))))
461 (let ((first t) i)
462 (if (eq mode 'word)
463 (setq prefix (PC-chop-word prefix basestr)))
464 (goto-char (+ beg (length dirname)))
465 (while (and (progn
466 (setq i 0)
467 (while (< i (length prefix))
468 (if (and (< (point) end)
469 (eq (aref prefix i)
470 (following-char)))
471 (forward-char 1)
472 (if (and (< (point) end)
473 (or (and (looking-at " ")
474 (memq (aref prefix i)
475 PC-delims-list))
476 (eq (downcase (aref prefix i))
477 (downcase
478 (following-char)))))
479 (progn
480 (delete-char 1)
481 (setq end (1- end)))
482 (and filename (looking-at "\\*")
483 (progn
484 (delete-char 1)
485 (setq end (1- end))))
486 (setq improved t))
487 ;; Use format to discard text properties.
488 (insert (format "%s" (substring prefix i (1+ i))))
489 (setq end (1+ end)))
490 (setq i (1+ i)))
491 (or pt (equal (point) beg)
492 (setq pt (point)))
493 (looking-at PC-delim-regex))
494 (setq skip (concat skip
495 (regexp-quote prefix)
496 PC-ndelims-regex)
497 prefix (try-completion
499 (mapcar
500 (function
501 (lambda (x)
502 (list
503 (and (string-match skip x)
504 (substring
506 (match-end 0))))))
507 poss)))
508 (or (> i 0) (> (length prefix) 0))
509 (or (not (eq mode 'word))
510 (and first (> (length prefix) 0)
511 (setq first nil
512 prefix (substring prefix 0 1))))))
513 (goto-char (if (eq mode 'word) end
514 (or pt beg)))))
516 (if (and (eq mode 'word)
517 (not PC-word-failed-flag))
519 (if improved
521 ;; We changed it... would it be complete without the space?
522 (if (PC-is-complete-p (buffer-substring 1 (1- end))
523 table pred)
524 (delete-region (1- end) end)))
526 (if improved
528 ;; We changed it... enough to be complete?
529 (and (eq mode 'exit)
530 (PC-is-complete-p (buffer-string) table pred))
532 ;; If totally ambiguous, display a list of completions
533 (if (or completion-auto-help
534 (eq mode 'help))
535 (with-output-to-temp-buffer "*Completions*"
536 (display-completion-list (sort helpposs 'string-lessp))
537 (save-excursion
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]"))
544 nil)))))
546 ;; Only one possible completion
548 (if (equal basestr (car poss))
549 (if (null mode)
550 (PC-temp-minibuffer-message " [Sole completion]"))
551 (delete-region beg end)
552 (insert (format "%s"
553 (if filename
554 (substitute-in-file-name (concat dirname (car poss)))
555 (car poss)))))
556 t)))))
559 (defun PC-is-complete-p (str table pred)
560 (let ((res (if (listp table)
561 (assoc str table)
562 (if (vectorp table)
563 (or (equal str "nil") ; heh, heh, heh
564 (intern-soft str table))
565 (funcall table str pred 'lambda)))))
566 (and res
567 (or (not pred)
568 (and (not (listp table)) (not (vectorp table)))
569 (funcall pred res))
570 res)))
572 (defun PC-chop-word (new old)
573 (let ((i -1)
574 (j -1))
575 (while (and (setq i (string-match PC-delim-regex old (1+ i)))
576 (setq j (string-match PC-delim-regex new (1+ j)))))
577 (if (and 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))
581 new)))
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
588 (progn
589 (message m)
590 (sit-for 2)
591 (message ""))
592 (if (fboundp 'temp-minibuffer-message)
593 (temp-minibuffer-message m)
594 (let ((savemax (point-max)))
595 (save-excursion
596 (goto-char (point-max))
597 (insert m))
598 (let ((inhibit-quit t))
599 (sit-for 2)
600 (delete-region savemax (point-max))
601 (if quit-flag
602 (setq quit-flag nil
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
610 are inserted.
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."
615 (interactive)
616 (let* ((end (point))
617 (buffer-syntax (syntax-table))
618 (beg (unwind-protect
619 (save-excursion
620 (if lisp-mode-syntax-table
621 (set-syntax-table lisp-mode-syntax-table))
622 (backward-sexp 1)
623 (while (= (char-syntax (following-char)) ?\')
624 (forward-char 1))
625 (point))
626 (set-syntax-table buffer-syntax)))
627 (minibuffer-completion-table obarray)
628 (minibuffer-completion-predicate
629 (if (eq (char-after (1- beg)) ?\()
630 'fboundp
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))
650 (first (car files))
651 (next files))
652 (kill-buffer (current-buffer))
653 (or files
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))
661 (if buf
662 nil ; should do verify-visited-file-modtime stuff.
663 (setq filename first)
664 (setq buf (create-file-buffer filename))
665 (set-buffer buf)
666 (erase-buffer)
667 (insert-file-contents filename t))
668 (if (cdr files)
669 (setq PC-many-files-list (mapconcat
670 (if (string-match "\\*.*/" pat)
671 'identity
672 'file-name-nondirectory)
673 (cdr files) ", ")
674 find-file-hooks (cons 'PC-after-load-many-files
675 find-file-hooks)))
676 ;; This modifies the "error" variable inside find-file-noselect.
677 (setq error nil)
679 nil))
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)
686 (save-excursion
687 (set-buffer (generate-new-buffer " *Glob Output*"))
688 (erase-buffer)
689 (shell-command (concat "echo " name) t)
690 (goto-char (point-min))
691 (if (looking-at ".*No match")
693 (insert "(\"")
694 (while (search-forward " " nil t)
695 (delete-backward-char 1)
696 (insert "\" \""))
697 (goto-char (point-max))
698 (delete-backward-char 1)
699 (insert "\")")
700 (goto-char (point-min))
701 (let ((files (read (current-buffer))))
702 (kill-buffer (current-buffer))
703 files))))
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)))
721 (path nil)
722 new-buf)
723 (kill-buffer (current-buffer))
724 (if (equal name "")
725 (save-excursion
726 (set-buffer (car (buffer-list)))
727 (save-excursion
728 (beginning-of-line)
729 (if (looking-at
730 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
731 (setq name (buffer-substring (match-beginning 1)
732 (match-end 1))
733 punc (char-after (1- (match-beginning 1))))
734 ;; Suggested by Frank Siebenlist:
735 (if (or (looking-at
736 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
737 (looking-at
738 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
739 (looking-at
740 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
741 (progn
742 (setq name (buffer-substring (match-beginning 1)
743 (match-end 1))
744 punc ?\<
745 path load-path)
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")))
753 (if (eq punc ?\<)
754 (let ((path (or path (PC-include-file-path))))
755 (while (and path
756 (not (file-exists-p
757 (concat (file-name-as-directory (car path))
758 name))))
759 (setq path (cdr path)))
760 (if 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)))
765 default-directory)))
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))
770 (if new-buf
771 ;; no need to verify last-modified time for this!
772 (set-buffer new-buf)
773 (setq new-buf (create-file-buffer name))
774 (set-buffer new-buf)
775 (erase-buffer)
776 (insert-file-contents name t))
777 (setq filename name
778 error nil
779 buf new-buf)
781 nil))
783 (defun PC-include-file-path ()
784 (or PC-include-file-path
785 (let ((env (getenv "INCPATH"))
786 (path nil)
787 pos)
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)))
792 path)))
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."
799 (setq search-path
800 (mapcar '(lambda (dir)
801 (if dir (file-name-as-directory dir) default-directory))
802 search-path))
803 (if (file-name-absolute-p file)
804 ;; It's an absolute file name, so don't need search-path
805 (progn
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))
811 file-lists)
812 ;; Append subdirectory part to each element of search-path
813 (if subdir
814 (setq search-path
815 (mapcar '(lambda (dir) (concat dir subdir))
816 search-path)
817 file ))
818 ;; Make list of completions in each directory on search-path
819 (while search-path
820 (let* ((dir (car search-path))
821 (subdir (if full dir subdir)))
822 (if (file-directory-p dir)
823 (progn
824 (setq file-lists
825 (cons
826 (mapcar '(lambda (file) (concat subdir file))
827 (file-name-all-completions ndfile
828 (car search-path)))
829 file-lists))))
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)))))
834 compressed)
835 (while sorted
836 (if (equal (car sorted) (car compressed)) nil
837 (setq compressed (cons (car sorted) compressed)))
838 (setq sorted (cdr sorted)))
839 compressed))))
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)))
847 (completion-table
848 (mapcar (function (lambda (x) (list (format "<%s>" x))))
849 (PC-include-file-all-completions
850 name (PC-include-file-path)))))
851 (cond
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))
855 ((eq action 'lambda)
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
866 (progn
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)))
872 (provide 'complete)
874 ;;; End.