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 convenience
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: M-x partial-completion-mode. 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 ;; (partial-completion-mode t)
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 (defgroup partial-completion nil
102 "Partial Completion of items."
107 (defcustom partial-completion-mode nil
108 "Toggle Partial Completion mode.
109 When Partial Completion mode is enabled, TAB (or M-TAB if `PC-meta-flag' is
110 nil) is enhanced so that if some string is divided into words and each word is
111 delimited by a character in `PC-word-delimiters', partial words are completed
112 as much as possible and `*' characters are treated likewise in file names.
113 You must modify via \\[customize] for this variable to have an effect."
114 :set (lambda (symbol value)
115 (partial-completion-mode (or value 0)))
116 :initialize 'custom-initialize-default
118 :group 'partial-completion
121 (defcustom PC-first-char 'find-file
122 "*Control how the first character of a string is to be interpreted.
123 If nil, the first character of a string is not taken literally if it is a word
124 delimiter, so that \".e\" matches \"*.e*\".
125 If t, the first character of a string is always taken literally even if it is a
126 word delimiter, so that \".e\" matches \".e*\".
127 If non-nil and non-t, the first character is taken literally only for file name
129 :type '(choice (const :tag "delimiter" nil)
130 (const :tag "literal" t)
131 (other :tag "find-file" find-file))
132 :group 'partial-completion)
134 (defcustom PC-meta-flag t
135 "*If non-nil, TAB means PC completion and M-TAB means normal completion.
136 Otherwise, TAB means normal completion and M-TAB means Partial Completion."
138 :group 'partial-completion)
140 (defcustom PC-word-delimiters "-_. "
141 "*A string of characters treated as word delimiters for completion.
143 If `]' is in this string, it must come first.
144 If `^' is in this string, it must not come first.
145 If `-' is in this string, it must come first or right after `]'.
146 In other words, if S is this string, then `[S]' must be a legal Emacs regular
147 expression (not containing character ranges like `a-z')."
149 :group 'partial-completion)
151 (defcustom PC-include-file-path '("/usr/include" "/usr/local/include")
152 "*A list of directories in which to look for include files.
153 If nil, means use the colon-separated path in the variable $INCPATH instead."
154 :type '(repeat directory)
155 :group 'partial-completion)
157 (defcustom PC-disable-wildcards nil
158 "*If non-nil, wildcard support in \\[find-file] is disabled."
160 :group 'partial-completion)
162 (defcustom PC-disable-includes nil
163 "*If non-nil, include-file support in \\[find-file] is disabled."
165 :group 'partial-completion)
167 (defvar PC-default-bindings t
168 "If non-nil, default partial completion key bindings are suppressed.")
170 (defvar PC-old-read-file-name-internal nil)
173 (defun partial-completion-mode (&optional arg)
174 "Toggle Partial Completion mode.
175 With prefix ARG, turn Partial Completion mode on if ARG is positive.
177 When Partial Completion mode is enabled, TAB (or M-TAB if `PC-meta-flag' is
178 nil) is enhanced so that if some string is divided into words and each word is
179 delimited by a character in `PC-word-delimiters', partial words are completed
182 For example, M-x p-c-b expands to M-x partial-completion-mode since no other
183 command begins with that sequence of characters, and
184 \\[find-file] f_b.c TAB might complete to foo_bar.c if that file existed and no
185 other file in that directory begin with that sequence of characters.
187 Unless `PC-disable-wildcards' is non-nil, the \"*\" wildcard is interpreted
188 specially when entering file or directory names. For example,
189 \\[find-file] *.c RET finds each C file in the currenty directory, and
190 \\[find-file] */foo_bar.c TAB completes the directory name as far as possible.
192 Unless `PC-disable-includes' is non-nil, the \"<...>\" sequence is interpreted
193 specially in \\[find-file]. For example,
194 \\[find-file] <sys/time.h> RET finds the file /usr/include/sys/time.h.
195 See also the variable `PC-include-file-path'."
198 (> (prefix-numeric-value arg) 0)
199 (not partial-completion-mode))))
200 ;; Deal with key bindings...
202 ;; Deal with wildcard file feature...
204 (remove-hook 'find-file-not-found-hooks 'PC-try-load-many-files))
205 ((not PC-disable-wildcards)
206 (add-hook 'find-file-not-found-hooks 'PC-try-load-many-files)))
207 ;; Deal with include file feature...
209 (remove-hook 'find-file-not-found-hooks 'PC-look-for-include-file))
210 ((not PC-disable-includes)
211 (add-hook 'find-file-not-found-hooks 'PC-look-for-include-file)))
212 ;; ... with some underhand redefining.
213 (cond ((and (not on-p) (functionp PC-old-read-file-name-internal))
214 (fset 'read-file-name-internal PC-old-read-file-name-internal))
215 ((and (not PC-disable-includes) (not PC-old-read-file-name-internal))
216 (setq PC-old-read-file-name-internal
217 (symbol-function 'read-file-name-internal))
218 (fset 'read-file-name-internal
219 'PC-read-include-file-name-internal)))
220 ;; Finally set the mode variable.
221 (setq partial-completion-mode on-p)))
223 (defun PC-bindings (bind)
224 (let ((completion-map minibuffer-local-completion-map)
225 (must-match-map minibuffer-local-must-match-map))
227 ;; These bindings are the default bindings. It would be better to
228 ;; restore the previous bindings.
229 (define-key completion-map "\t" 'minibuffer-complete)
230 (define-key completion-map " " 'minibuffer-complete-word)
231 (define-key completion-map "?" 'minibuffer-completion-help)
233 (define-key must-match-map "\t" 'minibuffer-complete)
234 (define-key must-match-map " " 'minibuffer-complete-word)
235 (define-key must-match-map "\r" 'minibuffer-complete-and-exit)
236 (define-key must-match-map "\n" 'minibuffer-complete-and-exit)
237 (define-key must-match-map "?" 'minibuffer-completion-help)
239 (define-key global-map "\e\t" 'complete-symbol))
241 (define-key completion-map "\t" 'PC-complete)
242 (define-key completion-map " " 'PC-complete-word)
243 (define-key completion-map "?" 'PC-completion-help)
245 (define-key completion-map "\e\t" 'PC-complete)
246 (define-key completion-map "\e " 'PC-complete-word)
247 (define-key completion-map "\e\r" 'PC-force-complete-and-exit)
248 (define-key completion-map "\e\n" 'PC-force-complete-and-exit)
249 (define-key completion-map "\e?" 'PC-completion-help)
251 (define-key must-match-map "\t" 'PC-complete)
252 (define-key must-match-map " " 'PC-complete-word)
253 (define-key must-match-map "\r" 'PC-complete-and-exit)
254 (define-key must-match-map "\n" 'PC-complete-and-exit)
255 (define-key must-match-map "?" 'PC-completion-help)
257 (define-key must-match-map "\e\t" 'PC-complete)
258 (define-key must-match-map "\e " 'PC-complete-word)
259 (define-key must-match-map "\e\r" 'PC-complete-and-exit)
260 (define-key must-match-map "\e\n" 'PC-complete-and-exit)
261 (define-key must-match-map "\e?" 'PC-completion-help)
263 (define-key global-map "\e\t" 'PC-lisp-complete-symbol)))))
265 ;; Because the `partial-completion-mode' option is defined before the
266 ;; `partial-completion-mode' command and its callee, we give the former a
267 ;; default `:initialize' keyword value. Otherwise, the `:set' keyword value
268 ;; would be called to initialise the variable value, and that would call the
269 ;; as-yet undefined `partial-completion-mode' function.
270 ;; Since the default `:initialize' keyword value (obviously) does not turn on
271 ;; Partial Completion Mode, we do that here, once the `partial-completion-mode'
272 ;; function and its callee are defined.
273 (when partial-completion-mode
274 (partial-completion-mode t))
276 (defun PC-complete ()
277 "Like minibuffer-complete, but allows \"b--di\"-style abbreviations.
278 For example, \"M-x b--di\" would match `byte-recompile-directory', or any
279 name which consists of three or more words, the first beginning with \"b\"
280 and the third beginning with \"di\".
282 The pattern \"b--d\" is ambiguous for `byte-recompile-directory' and
283 `beginning-of-defun', so this would produce a list of completions
284 just like when normal Emacs completions are ambiguous.
286 Word-delimiters for the purposes of Partial Completion are \"-\", \"_\",
289 (if (PC-was-meta-key)
290 (minibuffer-complete)
291 ;; If the previous command was not this one,
292 ;; never scroll, always retry completion.
293 (or (eq last-command this-command)
294 (setq minibuffer-scroll-window nil))
295 (let ((window minibuffer-scroll-window))
296 ;; If there's a fresh completion window with a live buffer,
297 ;; and this command is repeated, scroll that window.
298 (if (and window (window-buffer window)
299 (buffer-name (window-buffer window)))
301 (set-buffer (window-buffer window))
302 (if (pos-visible-in-window-p (point-max) window)
303 (set-window-start window (point-min) nil)
304 (scroll-other-window)))
305 (PC-do-completion nil)))))
308 (defun PC-complete-word ()
309 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
310 See `PC-complete' for details.
311 This can be bound to other keys, like `-' and `.', if you wish."
313 (if (eq (PC-was-meta-key) PC-meta-flag)
314 (if (eq last-command-char ? )
315 (minibuffer-complete-word)
316 (self-insert-command 1))
317 (self-insert-command 1)
319 (PC-do-completion 'word))))
322 (defun PC-complete-space ()
323 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
324 See `PC-complete' for details.
325 This is suitable for binding to other keys which should act just like SPC."
327 (if (eq (PC-was-meta-key) PC-meta-flag)
328 (minibuffer-complete-word)
331 (PC-do-completion 'word))))
334 (defun PC-complete-and-exit ()
335 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
336 See `PC-complete' for details."
338 (if (eq (PC-was-meta-key) PC-meta-flag)
339 (minibuffer-complete-and-exit)
340 (PC-do-complete-and-exit)))
342 (defun PC-force-complete-and-exit ()
343 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
344 See `PC-complete' for details."
346 (let ((minibuffer-completion-confirm nil))
347 (PC-do-complete-and-exit)))
349 (defun PC-do-complete-and-exit ()
350 (if (= (buffer-size) 0) ; Duplicate the "bug" that Info-menu relies on...
352 (let ((flag (PC-do-completion 'exit)))
354 (if (or (eq flag 'complete)
355 (not minibuffer-completion-confirm))
357 (PC-temp-minibuffer-message " [Confirm]"))))))
360 (defun PC-completion-help ()
361 "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
362 See `PC-complete' for details."
364 (if (eq (PC-was-meta-key) PC-meta-flag)
365 (minibuffer-completion-help)
366 (PC-do-completion 'help)))
368 (defun PC-was-meta-key ()
369 (or (/= (length (this-command-keys)) 1)
370 (let ((key (aref (this-command-keys) 0)))
373 (not (null (memq 'meta (event-modifiers key))))))))
376 (defvar PC-ignored-extensions 'empty-cache)
377 (defvar PC-delims 'empty-cache)
378 (defvar PC-ignored-regexp nil)
379 (defvar PC-word-failed-flag nil)
380 (defvar PC-delim-regex nil)
381 (defvar PC-ndelims-regex nil)
382 (defvar PC-delims-list nil)
384 (defvar PC-completion-as-file-name-predicate
387 (memq minibuffer-completion-table
388 '(read-file-name-internal read-directory-name-internal))))
389 "A function testing whether a minibuffer completion now will work filename-style.
390 The function takes no arguments, and typically looks at the value
391 of `minibuffer-completion-table' and the minibuffer contents.")
393 (defun PC-do-completion (&optional mode beg end)
394 (or beg (setq beg (point-min)))
395 (or end (setq end (point-max)))
396 (let* ((table minibuffer-completion-table)
397 (pred minibuffer-completion-predicate)
398 (filename (funcall PC-completion-as-file-name-predicate))
401 (str (buffer-substring beg end))
402 (incname (and filename (string-match "<\\([^\"<>]*\\)>?$" str)))
409 (case-fold-search completion-ignore-case))
411 ;; Check if buffer contents can already be considered complete
412 (if (and (eq mode 'exit)
413 (PC-is-complete-p str table pred))
416 ;; Record how many characters at the beginning are not included
420 (length (file-name-directory str))
423 ;; Do substitutions in directory names
425 (not (equal str (setq p (substitute-in-file-name str))))
427 (delete-region beg end)
429 (setq str p end (+ beg (length str)))))
431 ;; Prepare various delimiter strings
432 (or (equal PC-word-delimiters PC-delims)
433 (setq PC-delims PC-word-delimiters
434 PC-delim-regex (concat "[" PC-delims "]")
435 PC-ndelims-regex (concat "[^" PC-delims "]*")
436 PC-delims-list (append PC-delims nil)))
438 ;; Look for wildcard expansions in directory name
440 (string-match "\\*.*/" str)
443 (setq p (1+ (string-match "/[^/]*\\'" pat)))
444 (while (setq p (string-match PC-delim-regex pat p))
445 (setq pat (concat (substring pat 0 p)
449 (setq files (PC-expand-many-files (concat pat "*")))
451 (let ((dir (file-name-directory (car files)))
453 (while (and (setq p (cdr p))
454 (equal dir (file-name-directory (car p)))))
456 (setq filename nil table nil pred nil
458 (delete-region beg end)
459 (setq str (concat dir (file-name-nondirectory str)))
461 (setq end (+ beg (length str)))))
462 (setq filename nil table nil pred nil))))
464 ;; Strip directory name if appropriate
467 (setq basestr (substring str incname)
468 dirname (substring str 0 incname))
469 (setq basestr (file-name-nondirectory str)
470 dirname (file-name-directory str)))
473 ;; Convert search pattern to a standard regular expression
474 (setq regex (regexp-quote basestr)
475 offset (if (and (> (length regex) 0)
476 (not (eq (aref basestr 0) ?\*))
477 (or (eq PC-first-char t)
478 (and PC-first-char filename))) 1 0)
480 (while (setq p (string-match PC-delim-regex regex p))
481 (if (eq (aref regex p) ? )
482 (setq regex (concat (substring regex 0 p)
485 (substring regex (1+ p)))
486 p (+ p (length PC-ndelims-regex) (length PC-delim-regex)))
487 (let ((bump (if (memq (aref regex p)
488 '(?$ ?^ ?\. ?* ?+ ?? ?[ ?] ?\\))
490 (setq regex (concat (substring regex 0 (+ p bump))
492 (substring regex (+ p bump)))
493 p (+ p (length PC-ndelims-regex) 1)))))
496 (while (setq p (string-match "\\\\\\*" regex p))
497 (setq regex (concat (substring regex 0 p)
499 (substring regex (+ p 2))))))
500 ;;(setq the-regex regex)
501 (setq regex (concat "\\`" regex))
503 ;; Find an initial list of possible completions
504 (if (not (setq p (string-match (concat PC-delim-regex
505 (if filename "\\|\\*" ""))
507 (+ (length dirname) offset))))
509 ;; Minibuffer contains no hyphens -- simple case!
510 (setq poss (all-completions str
514 ;; Use all-completions to do an initial cull. This is a big win,
515 ;; since all-completions is written in C!
516 (let ((compl (all-completions (substring str 0 p)
521 (and (string-match regex (car p))
523 (set-text-properties 0 (length (car p)) '() (car p))
524 (setq poss (cons (car p) poss))))
527 ;; Now we have a list of possible completions
530 ;; No valid completions found
532 (if (and (eq mode 'word)
533 (not PC-word-failed-flag))
534 (let ((PC-word-failed-flag t))
535 (delete-backward-char 1)
536 (PC-do-completion 'word))
538 (PC-temp-minibuffer-message (if ambig
539 " [Ambiguous dir name]"
545 ;; More than one valid completion found
546 ((or (cdr (setq helpposs poss))
547 (memq mode '(help word)))
549 ;; Handle completion-ignored-extensions
551 (not (eq mode 'help))
554 ;; Build a regular expression representing the extensions list
555 (or (equal completion-ignored-extensions PC-ignored-extensions)
556 (setq PC-ignored-regexp
560 (setq PC-ignored-extensions
561 completion-ignored-extensions)
565 ;; Check if there are any without an ignored extension.
566 ;; Also ignore `.' and `..'.
569 (or (string-match PC-ignored-regexp (car p2))
570 (string-match "\\(\\`\\|/\\)[.][.]?/?\\'" (car p2))
571 (setq p (cons (car p2) p)))
574 ;; If there are "good" names, use them
575 (and p (setq poss p))))
577 ;; Is the actual string one of the possible completions?
578 (setq p (and (not (eq mode 'help)) poss))
580 (not (string-equal (car p) basestr)))
583 (PC-temp-minibuffer-message " [Complete, but not unique]"))
585 (not (and (null mode)
586 (eq this-command last-command))))
589 ;; If ambiguous, try for a partial completion
595 ;; Check if next few letters are the same in all cases
596 (if (and (not (eq mode 'help))
597 (setq prefix (try-completion "" (mapcar 'list poss))))
600 (setq prefix (PC-chop-word prefix basestr)))
601 (goto-char (+ beg (length dirname)))
604 (while (< i (length prefix))
605 (if (and (< (point) end)
609 (if (and (< (point) end)
610 (or (and (looking-at " ")
611 (memq (aref prefix i)
613 (eq (downcase (aref prefix i))
619 (and filename (looking-at "\\*")
622 (setq end (1- end))))
624 (insert (substring prefix i (1+ i)))
627 (or pt (equal (point) beg)
629 (looking-at PC-delim-regex))
630 (setq skip (concat skip
631 (regexp-quote prefix)
633 prefix (try-completion
639 (and (string-match skip x)
644 (or (> i 0) (> (length prefix) 0))
645 (or (not (eq mode 'word))
646 (and first (> (length prefix) 0)
648 prefix (substring prefix 0 1))))))
649 (goto-char (if (eq mode 'word) end
652 (if (and (eq mode 'word)
653 (not PC-word-failed-flag))
657 ;; We changed it... would it be complete without the space?
658 (if (PC-is-complete-p (buffer-substring 1 (1- end))
660 (delete-region (1- end) end)))
664 ;; We changed it... enough to be complete?
666 (PC-is-complete-p (buffer-string) table pred))
668 ;; If totally ambiguous, display a list of completions
669 (if (or completion-auto-help
671 (with-output-to-temp-buffer "*Completions*"
672 (display-completion-list (sort helpposs 'string-lessp))
674 (set-buffer standard-output)
675 ;; Record which part of the buffer we are completing
676 ;; so that choosing a completion from the list
677 ;; knows how much old text to replace.
678 (setq completion-base-size dirlength)))
679 (PC-temp-minibuffer-message " [Next char not unique]"))
682 ;; Only one possible completion
684 (if (equal basestr (car poss))
686 (PC-temp-minibuffer-message " [Sole completion]"))
687 (delete-region beg end)
690 (substitute-in-file-name (concat dirname (car poss)))
695 (defun PC-is-complete-p (str table pred)
696 (let ((res (if (listp table)
699 (or (equal str "nil") ; heh, heh, heh
700 (intern-soft str table))
701 (funcall table str pred 'lambda)))))
704 (and (not (listp table)) (not (vectorp table)))
708 (defun PC-chop-word (new old)
711 (while (and (setq i (string-match PC-delim-regex old (1+ i)))
712 (setq j (string-match PC-delim-regex new (1+ j)))))
714 (or (not PC-word-failed-flag)
715 (setq j (string-match PC-delim-regex new (1+ j)))))
716 (substring new 0 (1+ j))
719 (defvar PC-not-minibuffer nil)
721 (defun PC-temp-minibuffer-message (message)
722 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
723 (cond (PC-not-minibuffer
727 ((fboundp 'temp-minibuffer-message)
728 (temp-minibuffer-message message))
730 (let ((point-max (point-max)))
732 (goto-char point-max)
734 (let ((inhibit-quit t))
736 (delete-region point-max (point-max))
739 unread-command-events '(7))))))))
742 (defun PC-lisp-complete-symbol ()
743 "Perform completion on Lisp symbol preceding point.
744 That symbol is compared against the symbols that exist
745 and any additional characters determined by what is there
747 If the symbol starts just after an open-parenthesis,
748 only symbols with function definitions are considered.
749 Otherwise, all symbols with function definitions, values
750 or properties are considered."
753 (buffer-syntax (syntax-table))
756 (if lisp-mode-syntax-table
757 (set-syntax-table lisp-mode-syntax-table))
759 (while (= (char-syntax (following-char)) ?\')
762 (set-syntax-table buffer-syntax)))
763 (minibuffer-completion-table obarray)
764 (minibuffer-completion-predicate
765 (if (eq (char-after (1- beg)) ?\()
767 (function (lambda (sym)
768 (or (boundp sym) (fboundp sym)
769 (symbol-plist sym))))))
770 (PC-not-minibuffer t))
771 (PC-do-completion nil beg end)))
774 ;;; Wildcards in `C-x C-f' command. This is independent from the main
775 ;;; completion code, except for `PC-expand-many-files' which is called
776 ;;; when "*"'s are found in the path during filename completion. (The
777 ;;; above completion code always understands "*"'s, except in file paths,
778 ;;; without relying on the following code.)
780 (defvar PC-many-files-list nil)
782 (defun PC-try-load-many-files ()
783 (if (string-match "\\*" buffer-file-name)
784 (let* ((pat buffer-file-name)
785 (files (PC-expand-many-files pat))
787 (next (reverse (cdr files))))
788 (kill-buffer (current-buffer))
790 (error "No matching files"))
791 ;; Bring the other files (not the first) into buffers.
792 (save-window-excursion
794 (let ((buf (find-file-noselect (car next))))
795 ;; Put this buffer at the front of the buffer list.
796 (switch-to-buffer buf))
797 (setq next (cdr next))))
798 ;; This modifies the `buf' variable inside find-file-noselect.
799 (setq buf (get-file-buffer first))
801 nil ; should do verify-visited-file-modtime stuff.
802 (setq filename first)
803 (setq buf (create-file-buffer filename))
804 ;; This modified `truename' inside find-file-noselect.
805 (setq truename (abbreviate-file-name (file-truename filename)))
808 (insert-file-contents filename t))
810 (setq PC-many-files-list (mapconcat
811 (if (string-match "\\*.*/" pat)
813 'file-name-nondirectory)
815 find-file-hooks (cons 'PC-after-load-many-files
817 ;; This modifies the "error" variable inside find-file-noselect.
822 (defun PC-after-load-many-files ()
823 (setq find-file-hooks (delq 'PC-after-load-many-files find-file-hooks))
824 (message "Also loaded %s." PC-many-files-list))
826 (defun PC-expand-many-files (name)
828 (set-buffer (generate-new-buffer " *Glob Output*"))
830 (shell-command (concat "echo " name) t)
831 (goto-char (point-min))
832 (if (looking-at ".*No match")
835 (while (search-forward " " nil t)
836 (delete-backward-char 1)
838 (goto-char (point-max))
839 (delete-backward-char 1)
841 (goto-char (point-min))
842 (let ((files (read (current-buffer))) (p nil))
843 (kill-buffer (current-buffer))
844 (or (equal completion-ignored-extensions PC-ignored-extensions)
845 (setq PC-ignored-regexp
849 (setq PC-ignored-extensions
850 completion-ignored-extensions)
855 (or (string-match PC-ignored-regexp (car files))
856 (setq p (cons (car files) p)))
857 (setq files (cdr files)))
860 ;;; Facilities for loading C header files. This is independent from the
861 ;;; main completion code. See also the variable `PC-include-file-path'
862 ;;; at top of this file.
864 (defun PC-look-for-include-file ()
865 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
866 (let ((name (substring (buffer-file-name)
867 (match-beginning 1) (match-end 1)))
868 (punc (aref (buffer-file-name) (match-beginning 0)))
871 (kill-buffer (current-buffer))
874 (set-buffer (car (buffer-list)))
878 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
879 (setq name (buffer-substring (match-beginning 1)
881 punc (char-after (1- (match-beginning 1))))
882 ;; Suggested by Frank Siebenlist:
884 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
886 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
888 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
890 (setq name (buffer-substring (match-beginning 1)
894 (if (string-match "\\.elc$" name)
895 (setq name (substring name 0 -1))
896 (or (string-match "\\.el$" name)
897 (setq name (concat name ".el")))))
898 (error "Not on an #include line"))))))
899 (or (string-match "\\.[a-zA-Z0-9]+$" name)
900 (setq name (concat name ".h")))
902 (let ((path (or path (PC-include-file-path))))
905 (concat (file-name-as-directory (car path))
907 (setq path (cdr path)))
909 (setq name (concat (file-name-as-directory (car path)) name))
910 (error "No such include file: <%s>" name)))
911 (let ((dir (save-excursion
912 (set-buffer (car (buffer-list)))
914 (if (file-exists-p (concat dir name))
915 (setq name (concat dir name))
916 (error "No such include file: \"%s\"" name))))
917 (setq new-buf (get-file-buffer name))
919 ;; no need to verify last-modified time for this!
921 (setq new-buf (create-file-buffer name))
924 (insert-file-contents name t))
931 (defun PC-include-file-path ()
932 (or PC-include-file-path
933 (let ((env (getenv "INCPATH"))
936 (or env (error "No include file path specified"))
937 (while (setq pos (string-match ":[^:]+$" env))
938 (setq path (cons (substring env (1+ pos)) path)
939 env (substring env 0 pos)))
942 ;;; This is adapted from lib-complete.el, by Mike Williams.
943 (defun PC-include-file-all-completions (file search-path &optional full)
944 "Return all completions for FILE in any directory on SEARCH-PATH.
945 If optional third argument FULL is non-nil, returned pathnames should be
946 absolute rather than relative to some directory on the SEARCH-PATH."
948 (mapcar '(lambda (dir)
949 (if dir (file-name-as-directory dir) default-directory))
951 (if (file-name-absolute-p file)
952 ;; It's an absolute file name, so don't need search-path
954 (setq file (expand-file-name file))
955 (file-name-all-completions
956 (file-name-nondirectory file) (file-name-directory file)))
957 (let ((subdir (file-name-directory file))
958 (ndfile (file-name-nondirectory file))
960 ;; Append subdirectory part to each element of search-path
963 (mapcar '(lambda (dir) (concat dir subdir))
966 ;; Make list of completions in each directory on search-path
968 (let* ((dir (car search-path))
969 (subdir (if full dir subdir)))
970 (if (file-directory-p dir)
974 (mapcar '(lambda (file) (concat subdir file))
975 (file-name-all-completions ndfile
978 (setq search-path (cdr search-path))))
979 ;; Compress out duplicates while building complete list (slloooow!)
980 (let ((sorted (sort (apply 'nconc file-lists)
981 '(lambda (x y) (not (string-lessp x y)))))
984 (if (equal (car sorted) (car compressed)) nil
985 (setq compressed (cons (car sorted) compressed)))
986 (setq sorted (cdr sorted)))
989 (defun PC-read-include-file-name-internal (string dir action)
990 (if (string-match "<\\([^\"<>]*\\)>?$" string)
991 (let* ((name (substring string (match-beginning 1) (match-end 1)))
992 (str2 (substring string (match-beginning 0)))
994 (mapcar (function (lambda (x) (list (format "<%s>" x))))
995 (PC-include-file-all-completions
996 name (PC-include-file-path)))))
998 ((not completion-table) nil)
999 ((eq action nil) (try-completion str2 completion-table nil))
1000 ((eq action t) (all-completions str2 completion-table nil))
1001 ((eq action 'lambda)
1002 (eq (try-completion str2 completion-table nil) t))))
1003 (funcall PC-old-read-file-name-internal string dir action)))