1 ;;; complete.el --- partial completion mechanism plus other goodies
3 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
8 ;; Special thanks to Hallvard Furuseth for his many ideas and contributions.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Extended completion for the Emacs minibuffer.
31 ;; The basic idea is that the command name or other completable text is
32 ;; divided into words and each word is completed separately, so that
33 ;; "M-x p-b" expands to "M-x print-buffer". If the entry is ambiguous
34 ;; each word is completed as much as possible and then the cursor is
35 ;; left at the first position where typing another letter will resolve
38 ;; Word separators for this purpose are hyphen, space, and period.
39 ;; These would most likely occur in command names, Info menu items,
40 ;; and file names, respectively. But all word separators are treated
41 ;; alike at all times.
43 ;; This completion package replaces the old-style completer's key
44 ;; bindings for TAB, SPC, RET, and `?'. The old completer is still
45 ;; available on the Meta versions of those keys. If you set
46 ;; PC-meta-flag to nil, the old completion keys will be left alone
47 ;; and the partial completer will use the Meta versions of the keys.
50 ;; Usage: M-x PC-mode. Now, during completable minibuffer entry,
52 ;; TAB means to do a partial completion;
53 ;; SPC means to do a partial complete-word;
54 ;; RET means to do a partial complete-and-exit;
55 ;; ? means to do a partial completion-help.
57 ;; If you set PC-meta-flag to nil, then TAB, SPC, RET, and ? perform
58 ;; original Emacs completions, and M-TAB etc. do partial completion.
59 ;; To do this, put the command,
61 ;; (setq PC-meta-flag nil)
63 ;; in your .emacs file. To load partial completion automatically, put
67 ;; in your .emacs file, too. Things will be faster if you byte-compile
68 ;; this file when you install it.
70 ;; As an extra feature, in cases where RET would not normally
71 ;; complete (such as `C-x b'), the M-RET key will always do a partial
72 ;; complete-and-exit. Thus `C-x b f.c RET' will select or create a
73 ;; buffer called "f.c", but `C-x b f.c M-RET' will select the existing
74 ;; buffer whose name matches that pattern (perhaps "filing.c").
75 ;; (PC-meta-flag does not affect this behavior; M-RET used to be
76 ;; undefined in this situation.)
78 ;; The regular M-TAB (lisp-complete-symbol) command also supports
79 ;; partial completion in this package.
81 ;; This package also contains a wildcard feature for C-x C-f (find-file).
82 ;; For example, `C-x C-f *.c RET' loads all .c files at once, exactly
83 ;; as if you had typed C-x C-f separately for each file. Completion
84 ;; is supported in connection with wildcards. Currently only the `*'
85 ;; wildcard character works.
87 ;; File name completion does not do partial completion of directories
88 ;; on the path, e.g., "/u/b/f" will not complete to "/usr/bin/foo",
89 ;; but you can put *'s in the path to accomplish this: "/u*/b*/f".
90 ;; Stars are required for performance reasons.
92 ;; In addition, this package includes a feature for accessing include
93 ;; files. For example, `C-x C-f <sys/time.h> RET' reads the file
94 ;; /usr/include/sys/time.h. The variable PC-include-file-path is a
95 ;; list of directories in which to search for include files. Completion
96 ;; is supported in include file names.
101 (defgroup partial-completion nil
102 "Partial Completion of items."
106 (defcustom partial-completion-mode nil
107 "Toggle Partial Completion mode.
108 When Partial Completion mode is enabled, TAB (or M-TAB if `PC-meta-flag' is
109 nil) is enhanced so that if some string is divided into words and each word is
110 delimited by a character in `PC-word-delimiters', partial words are completed
111 as much as possible and `*' characters are treated likewise in file names.
112 You must modify via \\[customize] for this variable to have an effect."
113 :set
(lambda (symbol value
)
114 (partial-completion-mode (or value
0)))
115 :initialize
'custom-initialize-default
117 :group
'partial-completion
120 (defcustom PC-first-char
'find-file
121 "*Control how the first character of a string is to be interpreted.
122 If nil, the first character of a string is not taken literally if it is a word
123 delimiter, so that \".e\" matches \"*.e*\".
124 If t, the first character of a string is always taken literally even if it is a
125 word delimiter, so that \".e\" matches \".e*\".
126 If non-nil and non-t, the first character is taken literally only for file name
128 :type
'(choice (const :tag
"delimiter" nil
)
129 (const :tag
"literal" t
)
130 (sexp :tag
"find-file" :format
"%t\n" find-file
))
131 :group
'partial-completion
)
133 (defcustom PC-meta-flag t
134 "*If non-nil, TAB means PC completion and M-TAB means normal completion.
135 Otherwise, TAB means normal completion and M-TAB means Partial Completion."
137 :group
'partial-completion
)
139 (defcustom PC-word-delimiters
"-_. "
140 "*A string of characters treated as word delimiters for completion.
142 If `]' is in this string, it must come first.
143 If `^' is in this string, it must not come first.
144 If `-' is in this string, it must come first or right after `]'.
145 In other words, if S is this string, then `[S]' must be a legal Emacs regular
146 expression (not containing character ranges like `a-z')."
148 :group
'partial-completion
)
150 (defcustom PC-include-file-path
'("/usr/include" "/usr/local/include")
151 "*A list of directories in which to look for include files.
152 If nil, means use the colon-separated path in the variable $INCPATH instead."
153 :type
'(repeat directory
)
154 :group
'partial-completion
)
156 (defcustom PC-disable-wildcards nil
157 "*If non-nil, wildcard support in \\[find-file] is disabled."
159 :group
'partial-completion
)
161 (defcustom PC-disable-includes nil
162 "*If non-nil, include-file support in \\[find-file] is disabled."
164 :group
'partial-completion
)
166 (defvar PC-default-bindings t
167 "If non-nil, default partial completion key bindings are suppressed.")
169 (defvar PC-old-read-file-name-internal nil
)
172 (defun partial-completion-mode (&optional arg
)
173 "Toggle Partial Completion mode.
174 With prefix ARG, turn Partial Completion mode on if ARG is positive.
176 When Partial Completion mode is enabled, TAB (or M-TAB if `PC-meta-flag' is
177 nil) is enhanced so that if some string is divided into words and each word is
178 delimited by a character in `PC-word-delimiters', partial words are completed
181 For example, M-x p-c-b expands to M-x partial-completion-mode since no other
182 command begins with that sequence of characters, and
183 \\[find-file] f_b.c TAB might complete to foo_bar.c if that file existed and no
184 other file in that directory begin with that sequence of characters.
186 Unless `PC-disable-wildcards' is non-nil, the \"*\" wildcard is interpreted
187 specially when entering file or directory names. For example,
188 \\[find-file] *.c RET finds each C file in the currenty directory, and
189 \\[find-file] */foo_bar.c TAB completes the directory name as far as possible.
191 Unless `PC-disable-includes' is non-nil, the \"<...>\" sequence is interpreted
192 specially in \\[find-file]. For example,
193 \\[find-file] <sys/time.h> RET finds the file /usr/include/sys/time.h.
194 See also the variable `PC-include-file-path'."
197 (> (prefix-numeric-value arg
) 0)
198 (not partial-completion-mode
))))
199 ;; Deal with key bindings...
201 ;; Deal with wildcard file feature...
203 (remove-hook 'find-file-not-found-hooks
'PC-try-load-many-files
))
204 ((not PC-disable-wildcards
)
205 (add-hook 'find-file-not-found-hooks
'PC-try-load-many-files
)))
206 ;; Deal with include file feature...
208 (remove-hook 'find-file-not-found-hooks
'PC-look-for-include-file
))
209 ((not PC-disable-includes
)
210 (add-hook 'find-file-not-found-hooks
'PC-look-for-include-file
)))
211 ;; ... with some underhand redefining.
212 (cond ((and (not on-p
) (functionp PC-old-read-file-name-internal
))
213 (fset 'read-file-name-internal PC-old-read-file-name-internal
))
214 ((and (not PC-disable-includes
) (not PC-old-read-file-name-internal
))
215 (setq PC-old-read-file-name-internal
216 (symbol-function 'read-file-name-internal
))
217 (fset 'read-file-name-internal
218 'PC-read-include-file-name-internal
)))
219 ;; Finally set the mode variable.
220 (setq partial-completion-mode on-p
)))
222 (defun PC-bindings (bind)
223 (let ((completion-map minibuffer-local-completion-map
)
224 (must-match-map minibuffer-local-must-match-map
))
226 ;; These bindings are the default bindings. It would be better to
227 ;; restore the previous bindings.
228 (define-key completion-map
"\t" 'minibuffer-complete
)
229 (define-key completion-map
" " 'minibuffer-complete-word
)
230 (define-key completion-map
"?" 'minibuffer-completion-help
)
232 (define-key must-match-map
"\t" 'minibuffer-complete
)
233 (define-key must-match-map
" " 'minibuffer-complete-word
)
234 (define-key must-match-map
"\r" 'minibuffer-complete-and-exit
)
235 (define-key must-match-map
"\n" 'minibuffer-complete-and-exit
)
236 (define-key must-match-map
"?" 'minibuffer-completion-help
)
238 (define-key global-map
"\e\t" 'complete-symbol
))
240 (define-key completion-map
"\t" 'PC-complete
)
241 (define-key completion-map
" " 'PC-complete-word
)
242 (define-key completion-map
"?" 'PC-completion-help
)
244 (define-key completion-map
"\e\t" 'PC-complete
)
245 (define-key completion-map
"\e " 'PC-complete-word
)
246 (define-key completion-map
"\e\r" 'PC-force-complete-and-exit
)
247 (define-key completion-map
"\e\n" 'PC-force-complete-and-exit
)
248 (define-key completion-map
"\e?" 'PC-completion-help
)
250 (define-key must-match-map
"\t" 'PC-complete
)
251 (define-key must-match-map
" " 'PC-complete-word
)
252 (define-key must-match-map
"\r" 'PC-complete-and-exit
)
253 (define-key must-match-map
"\n" 'PC-complete-and-exit
)
254 (define-key must-match-map
"?" 'PC-completion-help
)
256 (define-key must-match-map
"\e\t" 'PC-complete
)
257 (define-key must-match-map
"\e " 'PC-complete-word
)
258 (define-key must-match-map
"\e\r" 'PC-complete-and-exit
)
259 (define-key must-match-map
"\e\n" 'PC-complete-and-exit
)
260 (define-key must-match-map
"\e?" 'PC-completion-help
)
262 (define-key global-map
"\e\t" 'PC-lisp-complete-symbol
)))))
264 ;; Because the `partial-completion-mode' option is defined before the
265 ;; `partial-completion-mode' command and its callee, we give the former a
266 ;; default `:initialize' keyword value. Otherwise, the `:set' keyword value
267 ;; would be called to initialise the variable value, and that would call the
268 ;; as-yet undefined `partial-completion-mode' function.
269 ;; Since the default `:initialize' keyword value (obviously) does not turn on
270 ;; Partial Completion Mode, we do that here, once the `partial-completion-mode'
271 ;; function and its callee are defined.
272 (when partial-completion-mode
273 (partial-completion-mode t
))
275 (defun PC-complete ()
276 "Like minibuffer-complete, but allows \"b--di\"-style abbreviations.
277 For example, \"M-x b--di\" would match `byte-recompile-directory', or any
278 name which consists of three or more words, the first beginning with \"b\"
279 and the third beginning with \"di\".
281 The pattern \"b--d\" is ambiguous for `byte-recompile-directory' and
282 `beginning-of-defun', so this would produce a list of completions
283 just like when normal Emacs completions are ambiguous.
285 Word-delimiters for the purposes of Partial Completion are \"-\", \"_\",
288 (if (PC-was-meta-key)
289 (minibuffer-complete)
290 ;; If the previous command was not this one,
291 ;; never scroll, always retry completion.
292 (or (eq last-command this-command
)
293 (setq minibuffer-scroll-window nil
))
294 (let ((window minibuffer-scroll-window
))
295 ;; If there's a fresh completion window with a live buffer,
296 ;; and this command is repeated, scroll that window.
297 (if (and window
(window-buffer window
)
298 (buffer-name (window-buffer window
)))
300 (set-buffer (window-buffer window
))
301 (if (pos-visible-in-window-p (point-max) window
)
302 (set-window-start window
(point-min) nil
)
303 (scroll-other-window)))
304 (PC-do-completion nil
)))))
307 (defun PC-complete-word ()
308 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
309 See `PC-complete' for details.
310 This can be bound to other keys, like `-' and `.', if you wish."
312 (if (eq (PC-was-meta-key) PC-meta-flag
)
313 (if (eq last-command-char ?
)
314 (minibuffer-complete-word)
315 (self-insert-command 1))
316 (self-insert-command 1)
318 (PC-do-completion 'word
))))
321 (defun PC-complete-space ()
322 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
323 See `PC-complete' for details.
324 This is suitable for binding to other keys which should act just like SPC."
326 (if (eq (PC-was-meta-key) PC-meta-flag
)
327 (minibuffer-complete-word)
330 (PC-do-completion 'word
))))
333 (defun PC-complete-and-exit ()
334 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
335 See `PC-complete' for details."
337 (if (eq (PC-was-meta-key) PC-meta-flag
)
338 (minibuffer-complete-and-exit)
339 (PC-do-complete-and-exit)))
341 (defun PC-force-complete-and-exit ()
342 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
343 See `PC-complete' for details."
345 (let ((minibuffer-completion-confirm nil
))
346 (PC-do-complete-and-exit)))
348 (defun PC-do-complete-and-exit ()
349 (if (= (buffer-size) 0) ; Duplicate the "bug" that Info-menu relies on...
351 (let ((flag (PC-do-completion 'exit
)))
353 (if (or (eq flag
'complete
)
354 (not minibuffer-completion-confirm
))
356 (PC-temp-minibuffer-message " [Confirm]"))))))
359 (defun PC-completion-help ()
360 "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
361 See `PC-complete' for details."
363 (if (eq (PC-was-meta-key) PC-meta-flag
)
364 (minibuffer-completion-help)
365 (PC-do-completion 'help
)))
367 (defun PC-was-meta-key ()
368 (or (/= (length (this-command-keys)) 1)
369 (let ((key (aref (this-command-keys) 0)))
372 (not (null (memq 'meta
(event-modifiers key
))))))))
375 (defvar PC-ignored-extensions
'empty-cache
)
376 (defvar PC-delims
'empty-cache
)
377 (defvar PC-ignored-regexp nil
)
378 (defvar PC-word-failed-flag nil
)
379 (defvar PC-delim-regex nil
)
380 (defvar PC-ndelims-regex nil
)
381 (defvar PC-delims-list nil
)
383 (defvar PC-completion-as-file-name-predicate
386 (memq minibuffer-completion-table
387 '(read-file-name-internal read-directory-name-internal
))))
388 "A function testing whether a minibuffer completion now will work filename-style.
389 The function takes no arguments, and typically looks at the value
390 of `minibuffer-completion-table' and the minibuffer contents.")
392 (defun PC-do-completion (&optional mode beg end
)
393 (or beg
(setq beg
(point-min)))
394 (or end
(setq end
(point-max)))
395 (let* ((table minibuffer-completion-table
)
396 (pred minibuffer-completion-predicate
)
397 (filename (funcall PC-completion-as-file-name-predicate
))
400 (str (buffer-substring beg end
))
401 (incname (and filename
(string-match "<\\([^\"<>]*\\)>?$" str
)))
408 (case-fold-search completion-ignore-case
))
410 ;; Check if buffer contents can already be considered complete
411 (if (and (eq mode
'exit
)
412 (PC-is-complete-p str table pred
))
415 ;; Record how many characters at the beginning are not included
419 (length (file-name-directory str
))
422 ;; Do substitutions in directory names
424 (not (equal str
(setq p
(substitute-in-file-name str
))))
426 (delete-region beg end
)
428 (setq str p end
(+ beg
(length str
)))))
430 ;; Prepare various delimiter strings
431 (or (equal PC-word-delimiters PC-delims
)
432 (setq PC-delims PC-word-delimiters
433 PC-delim-regex
(concat "[" PC-delims
"]")
434 PC-ndelims-regex
(concat "[^" PC-delims
"]*")
435 PC-delims-list
(append PC-delims nil
)))
437 ;; Look for wildcard expansions in directory name
439 (string-match "\\*.*/" str
)
442 (setq p
(1+ (string-match "/[^/]*\\'" pat
)))
443 (while (setq p
(string-match PC-delim-regex pat p
))
444 (setq pat
(concat (substring pat
0 p
)
448 (setq files
(PC-expand-many-files (concat pat
"*")))
450 (let ((dir (file-name-directory (car files
)))
452 (while (and (setq p
(cdr p
))
453 (equal dir
(file-name-directory (car p
)))))
455 (setq filename nil table nil pred nil
457 (delete-region beg end
)
458 (setq str
(concat dir
(file-name-nondirectory str
)))
460 (setq end
(+ beg
(length str
)))))
461 (setq filename nil table nil pred nil
))))
463 ;; Strip directory name if appropriate
466 (setq basestr
(substring str incname
)
467 dirname
(substring str
0 incname
))
468 (setq basestr
(file-name-nondirectory str
)
469 dirname
(file-name-directory str
)))
472 ;; Convert search pattern to a standard regular expression
473 (setq regex
(regexp-quote basestr
)
474 offset
(if (and (> (length regex
) 0)
475 (not (eq (aref basestr
0) ?\
*))
476 (or (eq PC-first-char t
)
477 (and PC-first-char filename
))) 1 0)
479 (while (setq p
(string-match PC-delim-regex regex p
))
480 (if (eq (aref regex p
) ?
)
481 (setq regex
(concat (substring regex
0 p
)
484 (substring regex
(1+ p
)))
485 p
(+ p
(length PC-ndelims-regex
) (length PC-delim-regex
)))
486 (let ((bump (if (memq (aref regex p
)
487 '(?$ ?^ ?\. ?
* ?
+ ?? ?
[ ?
] ?
\\))
489 (setq regex
(concat (substring regex
0 (+ p bump
))
491 (substring regex
(+ p bump
)))
492 p
(+ p
(length PC-ndelims-regex
) 1)))))
495 (while (setq p
(string-match "\\\\\\*" regex p
))
496 (setq regex
(concat (substring regex
0 p
)
498 (substring regex
(+ p
2))))))
499 ;;(setq the-regex regex)
500 (setq regex
(concat "\\`" regex
))
502 ;; Find an initial list of possible completions
503 (if (not (setq p
(string-match (concat PC-delim-regex
504 (if filename
"\\|\\*" ""))
506 (+ (length dirname
) offset
))))
508 ;; Minibuffer contains no hyphens -- simple case!
509 (setq poss
(all-completions str
513 ;; Use all-completions to do an initial cull. This is a big win,
514 ;; since all-completions is written in C!
515 (let ((compl (all-completions (substring str
0 p
)
520 (and (string-match regex
(car p
))
522 (set-text-properties 0 (length (car p
)) '() (car p
))
523 (setq poss
(cons (car p
) poss
))))
526 ;; Now we have a list of possible completions
529 ;; No valid completions found
531 (if (and (eq mode
'word
)
532 (not PC-word-failed-flag
))
533 (let ((PC-word-failed-flag t
))
534 (delete-backward-char 1)
535 (PC-do-completion 'word
))
537 (PC-temp-minibuffer-message (if ambig
538 " [Ambiguous dir name]"
544 ;; More than one valid completion found
545 ((or (cdr (setq helpposs poss
))
546 (memq mode
'(help word
)))
548 ;; Handle completion-ignored-extensions
550 (not (eq mode
'help
))
553 ;; Build a regular expression representing the extensions list
554 (or (equal completion-ignored-extensions PC-ignored-extensions
)
555 (setq PC-ignored-regexp
559 (setq PC-ignored-extensions
560 completion-ignored-extensions
)
564 ;; Check if there are any without an ignored extension
567 (or (string-match PC-ignored-regexp
(car p2
))
568 (setq p
(cons (car p2
) p
)))
571 ;; If there are "good" names, use them
572 (and p
(setq poss p
))))
574 ;; Is the actual string one of the possible completions?
575 (setq p
(and (not (eq mode
'help
)) poss
))
577 (not (string-equal (car p
) basestr
)))
580 (PC-temp-minibuffer-message " [Complete, but not unique]"))
582 (not (and (null mode
)
583 (eq this-command last-command
))))
586 ;; If ambiguous, try for a partial completion
592 ;; Check if next few letters are the same in all cases
593 (if (and (not (eq mode
'help
))
594 (setq prefix
(try-completion "" (mapcar 'list poss
))))
597 (setq prefix
(PC-chop-word prefix basestr
)))
598 (goto-char (+ beg
(length dirname
)))
601 (while (< i
(length prefix
))
602 (if (and (< (point) end
)
606 (if (and (< (point) end
)
607 (or (and (looking-at " ")
608 (memq (aref prefix i
)
610 (eq (downcase (aref prefix i
))
616 (and filename
(looking-at "\\*")
619 (setq end
(1- end
))))
621 (insert (substring prefix i
(1+ i
)))
624 (or pt
(equal (point) beg
)
626 (looking-at PC-delim-regex
))
627 (setq skip
(concat skip
628 (regexp-quote prefix
)
630 prefix
(try-completion
636 (and (string-match skip x
)
641 (or (> i
0) (> (length prefix
) 0))
642 (or (not (eq mode
'word
))
643 (and first
(> (length prefix
) 0)
645 prefix
(substring prefix
0 1))))))
646 (goto-char (if (eq mode
'word
) end
649 (if (and (eq mode
'word
)
650 (not PC-word-failed-flag
))
654 ;; We changed it... would it be complete without the space?
655 (if (PC-is-complete-p (buffer-substring 1 (1- end
))
657 (delete-region (1- end
) end
)))
661 ;; We changed it... enough to be complete?
663 (PC-is-complete-p (buffer-string) table pred
))
665 ;; If totally ambiguous, display a list of completions
666 (if (or completion-auto-help
668 (with-output-to-temp-buffer "*Completions*"
669 (display-completion-list (sort helpposs
'string-lessp
))
671 (set-buffer standard-output
)
672 ;; Record which part of the buffer we are completing
673 ;; so that choosing a completion from the list
674 ;; knows how much old text to replace.
675 (setq completion-base-size dirlength
)))
676 (PC-temp-minibuffer-message " [Next char not unique]"))
679 ;; Only one possible completion
681 (if (equal basestr
(car poss
))
683 (PC-temp-minibuffer-message " [Sole completion]"))
684 (delete-region beg end
)
687 (substitute-in-file-name (concat dirname
(car poss
)))
692 (defun PC-is-complete-p (str table pred
)
693 (let ((res (if (listp table
)
696 (or (equal str
"nil") ; heh, heh, heh
697 (intern-soft str table
))
698 (funcall table str pred
'lambda
)))))
701 (and (not (listp table
)) (not (vectorp table
)))
705 (defun PC-chop-word (new old
)
708 (while (and (setq i
(string-match PC-delim-regex old
(1+ i
)))
709 (setq j
(string-match PC-delim-regex new
(1+ j
)))))
711 (or (not PC-word-failed-flag
)
712 (setq j
(string-match PC-delim-regex new
(1+ j
)))))
713 (substring new
0 (1+ j
))
716 (defvar PC-not-minibuffer nil
)
718 (defun PC-temp-minibuffer-message (message)
719 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
720 (cond (PC-not-minibuffer
724 ((fboundp 'temp-minibuffer-message
)
725 (temp-minibuffer-message message
))
727 (let ((point-max (point-max)))
729 (goto-char point-max
)
731 (let ((inhibit-quit t
))
733 (delete-region point-max
(point-max))
736 unread-command-events
'(7))))))))
739 (defun PC-lisp-complete-symbol ()
740 "Perform completion on Lisp symbol preceding point.
741 That symbol is compared against the symbols that exist
742 and any additional characters determined by what is there
744 If the symbol starts just after an open-parenthesis,
745 only symbols with function definitions are considered.
746 Otherwise, all symbols with function definitions, values
747 or properties are considered."
750 (buffer-syntax (syntax-table))
753 (if lisp-mode-syntax-table
754 (set-syntax-table lisp-mode-syntax-table
))
756 (while (= (char-syntax (following-char)) ?
\')
759 (set-syntax-table buffer-syntax
)))
760 (minibuffer-completion-table obarray
)
761 (minibuffer-completion-predicate
762 (if (eq (char-after (1- beg
)) ?\
()
764 (function (lambda (sym)
765 (or (boundp sym
) (fboundp sym
)
766 (symbol-plist sym
))))))
767 (PC-not-minibuffer t
))
768 (PC-do-completion nil beg end
)))
771 ;;; Wildcards in `C-x C-f' command. This is independent from the main
772 ;;; completion code, except for `PC-expand-many-files' which is called
773 ;;; when "*"'s are found in the path during filename completion. (The
774 ;;; above completion code always understands "*"'s, except in file paths,
775 ;;; without relying on the following code.)
777 (defvar PC-many-files-list nil
)
779 (defun PC-try-load-many-files ()
780 (if (string-match "\\*" buffer-file-name
)
781 (let* ((pat buffer-file-name
)
782 (files (PC-expand-many-files pat
))
785 (kill-buffer (current-buffer))
787 (error "No matching files"))
788 ;; Bring the other files (not the first) into buffers.
789 (save-window-excursion
790 (while (setq next
(cdr next
))
791 (let ((buf (find-file-noselect (car next
))))
792 ;; Put this buffer at the front of the buffer list.
793 (switch-to-buffer buf
))))
794 ;; This modifies the `buf' variable inside find-file-noselect.
795 (setq buf
(get-file-buffer first
))
797 nil
; should do verify-visited-file-modtime stuff.
798 (setq filename first
)
799 (setq buf
(create-file-buffer filename
))
800 ;; This modified `truename' inside find-file-noselect.
801 (setq truename
(abbreviate-file-name (file-truename filename
)))
804 (insert-file-contents filename t
))
806 (setq PC-many-files-list
(mapconcat
807 (if (string-match "\\*.*/" pat
)
809 'file-name-nondirectory
)
811 find-file-hooks
(cons 'PC-after-load-many-files
813 ;; This modifies the "error" variable inside find-file-noselect.
818 (defun PC-after-load-many-files ()
819 (setq find-file-hooks
(delq 'PC-after-load-many-files find-file-hooks
))
820 (message "Also loaded %s." PC-many-files-list
))
822 (defun PC-expand-many-files (name)
824 (set-buffer (generate-new-buffer " *Glob Output*"))
826 (shell-command (concat "echo " name
) t
)
827 (goto-char (point-min))
828 (if (looking-at ".*No match")
831 (while (search-forward " " nil t
)
832 (delete-backward-char 1)
834 (goto-char (point-max))
835 (delete-backward-char 1)
837 (goto-char (point-min))
838 (let ((files (read (current-buffer))))
839 (kill-buffer (current-buffer))
842 ;;; Facilities for loading C header files. This is independent from the
843 ;;; main completion code. See also the variable `PC-include-file-path'
844 ;;; at top of this file.
846 (defun PC-look-for-include-file ()
847 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
848 (let ((name (substring (buffer-file-name)
849 (match-beginning 1) (match-end 1)))
850 (punc (aref (buffer-file-name) (match-beginning 0)))
853 (kill-buffer (current-buffer))
856 (set-buffer (car (buffer-list)))
860 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
861 (setq name
(buffer-substring (match-beginning 1)
863 punc
(char-after (1- (match-beginning 1))))
864 ;; Suggested by Frank Siebenlist:
866 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
868 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
870 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
872 (setq name
(buffer-substring (match-beginning 1)
876 (if (string-match "\\.elc$" name
)
877 (setq name
(substring name
0 -
1))
878 (or (string-match "\\.el$" name
)
879 (setq name
(concat name
".el")))))
880 (error "Not on an #include line"))))))
881 (or (string-match "\\.[a-zA-Z0-9]+$" name
)
882 (setq name
(concat name
".h")))
884 (let ((path (or path
(PC-include-file-path))))
887 (concat (file-name-as-directory (car path
))
889 (setq path
(cdr path
)))
891 (setq name
(concat (file-name-as-directory (car path
)) name
))
892 (error "No such include file: <%s>" name
)))
893 (let ((dir (save-excursion
894 (set-buffer (car (buffer-list)))
896 (if (file-exists-p (concat dir name
))
897 (setq name
(concat dir name
))
898 (error "No such include file: \"%s\"" name
))))
899 (setq new-buf
(get-file-buffer name
))
901 ;; no need to verify last-modified time for this!
903 (setq new-buf
(create-file-buffer name
))
906 (insert-file-contents name t
))
913 (defun PC-include-file-path ()
914 (or PC-include-file-path
915 (let ((env (getenv "INCPATH"))
918 (or env
(error "No include file path specified"))
919 (while (setq pos
(string-match ":[^:]+$" env
))
920 (setq path
(cons (substring env
(1+ pos
)) path
)
921 env
(substring env
0 pos
)))
924 ;;; This is adapted from lib-complete.el, by Mike Williams.
925 (defun PC-include-file-all-completions (file search-path
&optional full
)
926 "Return all completions for FILE in any directory on SEARCH-PATH.
927 If optional third argument FULL is non-nil, returned pathnames should be
928 absolute rather than relative to some directory on the SEARCH-PATH."
930 (mapcar '(lambda (dir)
931 (if dir
(file-name-as-directory dir
) default-directory
))
933 (if (file-name-absolute-p file
)
934 ;; It's an absolute file name, so don't need search-path
936 (setq file
(expand-file-name file
))
937 (file-name-all-completions
938 (file-name-nondirectory file
) (file-name-directory file
)))
939 (let ((subdir (file-name-directory file
))
940 (ndfile (file-name-nondirectory file
))
942 ;; Append subdirectory part to each element of search-path
945 (mapcar '(lambda (dir) (concat dir subdir
))
948 ;; Make list of completions in each directory on search-path
950 (let* ((dir (car search-path
))
951 (subdir (if full dir subdir
)))
952 (if (file-directory-p dir
)
956 (mapcar '(lambda (file) (concat subdir file
))
957 (file-name-all-completions ndfile
960 (setq search-path
(cdr search-path
))))
961 ;; Compress out duplicates while building complete list (slloooow!)
962 (let ((sorted (sort (apply 'nconc file-lists
)
963 '(lambda (x y
) (not (string-lessp x y
)))))
966 (if (equal (car sorted
) (car compressed
)) nil
967 (setq compressed
(cons (car sorted
) compressed
)))
968 (setq sorted
(cdr sorted
)))
971 (defun PC-read-include-file-name-internal (string dir action
)
972 (if (string-match "<\\([^\"<>]*\\)>?$" string
)
973 (let* ((name (substring string
(match-beginning 1) (match-end 1)))
974 (str2 (substring string
(match-beginning 0)))
976 (mapcar (function (lambda (x) (list (format "<%s>" x
))))
977 (PC-include-file-all-completions
978 name
(PC-include-file-path)))))
980 ((not completion-table
) nil
)
981 ((eq action nil
) (try-completion str2 completion-table nil
))
982 ((eq action t
) (all-completions str2 completion-table nil
))
984 (eq (try-completion str2 completion-table nil
) t
))))
985 (funcall PC-old-read-file-name-internal string dir action
)))