]> code.delx.au - gnu-emacs/blob - lisp/complete.el
(describe-char-after): Use
[gnu-emacs] / lisp / complete.el
1 ;;; complete.el --- partial completion mechanism plus other goodies
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 1999 Free Software Foundation, Inc.
4
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Keywords: abbrev convenience
7 ;; Special thanks to Hallvard Furuseth for his many ideas and contributions.
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Extended completion for the Emacs minibuffer.
29 ;;
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.
36 ;;
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.
41 ;;
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.
47
48
49 ;; Usage: M-x partial-completion-mode. During completable minibuffer entry,
50 ;;
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.
55 ;;
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,
59 ;;
60 ;; (setq PC-meta-flag nil)
61 ;;
62 ;; in your .emacs file. To load partial completion automatically, put
63 ;;
64 ;; (partial-completion-mode t)
65 ;;
66 ;; in your .emacs file, too. Things will be faster if you byte-compile
67 ;; this file when you install it.
68 ;;
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.)
76 ;;
77 ;; The regular M-TAB (lisp-complete-symbol) command also supports
78 ;; partial completion in this package.
79
80 ;; File name completion does not do partial completion of directories
81 ;; on the path, e.g., "/u/b/f" will not complete to "/usr/bin/foo",
82 ;; but you can put *'s in the path to accomplish this: "/u*/b*/f".
83 ;; Stars are required for performance reasons.
84
85 ;; In addition, this package includes a feature for accessing include
86 ;; files. For example, `C-x C-f <sys/time.h> RET' reads the file
87 ;; /usr/include/sys/time.h. The variable PC-include-file-path is a
88 ;; list of directories in which to search for include files. Completion
89 ;; is supported in include file names.
90
91
92 ;;; Code:
93
94 (defgroup partial-completion nil
95 "Partial Completion of items."
96 :prefix "pc-"
97 :group 'minibuffer
98 :group 'convenience)
99
100 ;;;###autoload
101 (defcustom partial-completion-mode nil
102 "Toggle Partial Completion mode.
103 When Partial Completion mode is enabled, TAB (or M-TAB if `PC-meta-flag' is
104 nil) is enhanced so that if some string is divided into words and each word is
105 delimited by a character in `PC-word-delimiters', partial words are completed
106 as much as possible and `*' characters are treated likewise in file names.
107 This variable should be set only with \\[customize], which is equivalent
108 to using the function `partial-completion-mode'."
109 :set (lambda (symbol value)
110 (partial-completion-mode (or value 0)))
111 :initialize 'custom-initialize-default
112 :type 'boolean
113 :group 'partial-completion
114 :require 'complete)
115
116 (defcustom PC-first-char 'find-file
117 "*Control how the first character of a string is to be interpreted.
118 If nil, the first character of a string is not taken literally if it is a word
119 delimiter, so that \".e\" matches \"*.e*\".
120 If t, the first character of a string is always taken literally even if it is a
121 word delimiter, so that \".e\" matches \".e*\".
122 If non-nil and non-t, the first character is taken literally only for file name
123 completion."
124 :type '(choice (const :tag "delimiter" nil)
125 (const :tag "literal" t)
126 (other :tag "find-file" find-file))
127 :group 'partial-completion)
128
129 (defcustom PC-meta-flag t
130 "*If non-nil, TAB means PC completion and M-TAB means normal completion.
131 Otherwise, TAB means normal completion and M-TAB means Partial Completion."
132 :type 'boolean
133 :group 'partial-completion)
134
135 (defcustom PC-word-delimiters "-_. "
136 "*A string of characters treated as word delimiters for completion.
137 Some arcane rules:
138 If `]' is in this string, it must come first.
139 If `^' is in this string, it must not come first.
140 If `-' is in this string, it must come first or right after `]'.
141 In other words, if S is this string, then `[S]' must be a legal Emacs regular
142 expression (not containing character ranges like `a-z')."
143 :type 'string
144 :group 'partial-completion)
145
146 (defcustom PC-include-file-path '("/usr/include" "/usr/local/include")
147 "*A list of directories in which to look for include files.
148 If nil, means use the colon-separated path in the variable $INCPATH instead."
149 :type '(repeat directory)
150 :group 'partial-completion)
151
152 (defcustom PC-disable-includes nil
153 "*If non-nil, include-file support in \\[find-file] is disabled."
154 :type 'boolean
155 :group 'partial-completion)
156
157 (defvar PC-default-bindings t
158 "If non-nil, default partial completion key bindings are suppressed.")
159 \f
160 (defvar PC-old-read-file-name-internal nil)
161
162 ;;;###autoload
163 (defun partial-completion-mode (&optional arg)
164 "Toggle Partial Completion mode.
165 With prefix ARG, turn Partial Completion mode on if ARG is positive.
166
167 When Partial Completion mode is enabled, TAB (or M-TAB if `PC-meta-flag' is
168 nil) is enhanced so that if some string is divided into words and each word is
169 delimited by a character in `PC-word-delimiters', partial words are completed
170 as much as possible.
171
172 For example, M-x p-c-m expands to M-x partial-completion-mode since no other
173 command begins with that sequence of characters, and
174 \\[find-file] f_b.c TAB might complete to foo_bar.c if that file existed and no
175 other file in that directory begin with that sequence of characters.
176
177 Unless `PC-disable-includes' is non-nil, the \"<...>\" sequence is interpreted
178 specially in \\[find-file]. For example,
179 \\[find-file] <sys/time.h> RET finds the file /usr/include/sys/time.h.
180 See also the variable `PC-include-file-path'."
181 (interactive "P")
182 (let ((on-p (if arg
183 (> (prefix-numeric-value arg) 0)
184 (not partial-completion-mode))))
185 ;; Deal with key bindings...
186 (PC-bindings on-p)
187 ;; Deal with include file feature...
188 (cond ((not on-p)
189 (remove-hook 'find-file-not-found-hooks 'PC-look-for-include-file))
190 ((not PC-disable-includes)
191 (add-hook 'find-file-not-found-hooks 'PC-look-for-include-file)))
192 ;; ... with some underhand redefining.
193 (cond ((and (not on-p) (functionp PC-old-read-file-name-internal))
194 (fset 'read-file-name-internal PC-old-read-file-name-internal))
195 ((and (not PC-disable-includes) (not PC-old-read-file-name-internal))
196 (setq PC-old-read-file-name-internal
197 (symbol-function 'read-file-name-internal))
198 (fset 'read-file-name-internal
199 'PC-read-include-file-name-internal)))
200 ;; Finally set the mode variable.
201 (setq partial-completion-mode on-p)))
202
203 (defun PC-bindings (bind)
204 (let ((completion-map minibuffer-local-completion-map)
205 (must-match-map minibuffer-local-must-match-map))
206 (cond ((not bind)
207 ;; These bindings are the default bindings. It would be better to
208 ;; restore the previous bindings.
209 (define-key completion-map "\t" 'minibuffer-complete)
210 (define-key completion-map " " 'minibuffer-complete-word)
211 (define-key completion-map "?" 'minibuffer-completion-help)
212
213 (define-key must-match-map "\t" 'minibuffer-complete)
214 (define-key must-match-map " " 'minibuffer-complete-word)
215 (define-key must-match-map "\r" 'minibuffer-complete-and-exit)
216 (define-key must-match-map "\n" 'minibuffer-complete-and-exit)
217 (define-key must-match-map "?" 'minibuffer-completion-help)
218
219 (define-key global-map "\e\t" 'complete-symbol))
220 (PC-default-bindings
221 (define-key completion-map "\t" 'PC-complete)
222 (define-key completion-map " " 'PC-complete-word)
223 (define-key completion-map "?" 'PC-completion-help)
224
225 (define-key completion-map "\e\t" 'PC-complete)
226 (define-key completion-map "\e " 'PC-complete-word)
227 (define-key completion-map "\e\r" 'PC-force-complete-and-exit)
228 (define-key completion-map "\e\n" 'PC-force-complete-and-exit)
229 (define-key completion-map "\e?" 'PC-completion-help)
230
231 (define-key must-match-map "\t" 'PC-complete)
232 (define-key must-match-map " " 'PC-complete-word)
233 (define-key must-match-map "\r" 'PC-complete-and-exit)
234 (define-key must-match-map "\n" 'PC-complete-and-exit)
235 (define-key must-match-map "?" 'PC-completion-help)
236
237 (define-key must-match-map "\e\t" 'PC-complete)
238 (define-key must-match-map "\e " 'PC-complete-word)
239 (define-key must-match-map "\e\r" 'PC-complete-and-exit)
240 (define-key must-match-map "\e\n" 'PC-complete-and-exit)
241 (define-key must-match-map "\e?" 'PC-completion-help)
242
243 (define-key global-map "\e\t" 'PC-lisp-complete-symbol)))))
244
245 ;; Because the `partial-completion-mode' option is defined before the
246 ;; `partial-completion-mode' command and its callee, we give the former a
247 ;; default `:initialize' keyword value. Otherwise, the `:set' keyword value
248 ;; would be called to initialise the variable value, and that would call the
249 ;; as-yet undefined `partial-completion-mode' function.
250 ;; Since the default `:initialize' keyword value (obviously) does not turn on
251 ;; Partial Completion Mode, we do that here, once the `partial-completion-mode'
252 ;; function and its callee are defined.
253 (when partial-completion-mode
254 (partial-completion-mode t))
255 \f
256 (defun PC-complete ()
257 "Like minibuffer-complete, but allows \"b--di\"-style abbreviations.
258 For example, \"M-x b--di\" would match `byte-recompile-directory', or any
259 name which consists of three or more words, the first beginning with \"b\"
260 and the third beginning with \"di\".
261
262 The pattern \"b--d\" is ambiguous for `byte-recompile-directory' and
263 `beginning-of-defun', so this would produce a list of completions
264 just like when normal Emacs completions are ambiguous.
265
266 Word-delimiters for the purposes of Partial Completion are \"-\", \"_\",
267 \".\", and SPC."
268 (interactive)
269 (if (PC-was-meta-key)
270 (minibuffer-complete)
271 ;; If the previous command was not this one,
272 ;; never scroll, always retry completion.
273 (or (eq last-command this-command)
274 (setq minibuffer-scroll-window nil))
275 (let ((window minibuffer-scroll-window))
276 ;; If there's a fresh completion window with a live buffer,
277 ;; and this command is repeated, scroll that window.
278 (if (and window (window-buffer window)
279 (buffer-name (window-buffer window)))
280 (save-excursion
281 (set-buffer (window-buffer window))
282 (if (pos-visible-in-window-p (point-max) window)
283 (set-window-start window (point-min) nil)
284 (scroll-other-window)))
285 (PC-do-completion nil)))))
286
287
288 (defun PC-complete-word ()
289 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
290 See `PC-complete' for details.
291 This can be bound to other keys, like `-' and `.', if you wish."
292 (interactive)
293 (if (eq (PC-was-meta-key) PC-meta-flag)
294 (if (eq last-command-char ? )
295 (minibuffer-complete-word)
296 (self-insert-command 1))
297 (self-insert-command 1)
298 (if (eobp)
299 (PC-do-completion 'word))))
300
301
302 (defun PC-complete-space ()
303 "Like `minibuffer-complete-word', but allows \"b--di\"-style abbreviations.
304 See `PC-complete' for details.
305 This is suitable for binding to other keys which should act just like SPC."
306 (interactive)
307 (if (eq (PC-was-meta-key) PC-meta-flag)
308 (minibuffer-complete-word)
309 (insert " ")
310 (if (eobp)
311 (PC-do-completion 'word))))
312
313
314 (defun PC-complete-and-exit ()
315 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
316 See `PC-complete' for details."
317 (interactive)
318 (if (eq (PC-was-meta-key) PC-meta-flag)
319 (minibuffer-complete-and-exit)
320 (PC-do-complete-and-exit)))
321
322 (defun PC-force-complete-and-exit ()
323 "Like `minibuffer-complete-and-exit', but allows \"b--di\"-style abbreviations.
324 See `PC-complete' for details."
325 (interactive)
326 (let ((minibuffer-completion-confirm nil))
327 (PC-do-complete-and-exit)))
328
329 (defun PC-do-complete-and-exit ()
330 (if (= (point-max) (minibuffer-prompt-end)) ; Duplicate the "bug" that Info-menu relies on...
331 (exit-minibuffer)
332 (let ((flag (PC-do-completion 'exit)))
333 (and flag
334 (if (or (eq flag 'complete)
335 (not minibuffer-completion-confirm))
336 (exit-minibuffer)
337 (PC-temp-minibuffer-message " [Confirm]"))))))
338
339
340 (defun PC-completion-help ()
341 "Like `minibuffer-completion-help', but allows \"b--di\"-style abbreviations.
342 See `PC-complete' for details."
343 (interactive)
344 (if (eq (PC-was-meta-key) PC-meta-flag)
345 (minibuffer-completion-help)
346 (PC-do-completion 'help)))
347
348 (defun PC-was-meta-key ()
349 (or (/= (length (this-command-keys)) 1)
350 (let ((key (aref (this-command-keys) 0)))
351 (if (integerp key)
352 (>= key 128)
353 (not (null (memq 'meta (event-modifiers key))))))))
354
355
356 (defvar PC-ignored-extensions 'empty-cache)
357 (defvar PC-delims 'empty-cache)
358 (defvar PC-ignored-regexp nil)
359 (defvar PC-word-failed-flag nil)
360 (defvar PC-delim-regex nil)
361 (defvar PC-ndelims-regex nil)
362 (defvar PC-delims-list nil)
363
364 (defvar PC-completion-as-file-name-predicate
365 (function
366 (lambda ()
367 (memq minibuffer-completion-table
368 '(read-file-name-internal read-directory-name-internal))))
369 "A function testing whether a minibuffer completion now will work filename-style.
370 The function takes no arguments, and typically looks at the value
371 of `minibuffer-completion-table' and the minibuffer contents.")
372
373 (defun PC-do-completion (&optional mode beg end)
374 (or beg (setq beg (minibuffer-prompt-end)))
375 (or end (setq end (point-max)))
376 (let* ((table minibuffer-completion-table)
377 (pred minibuffer-completion-predicate)
378 (filename (funcall PC-completion-as-file-name-predicate))
379 (dirname nil)
380 dirlength
381 (str (buffer-substring beg end))
382 (incname (and filename (string-match "<\\([^\"<>]*\\)>?$" str)))
383 (ambig nil)
384 basestr
385 regex
386 p offset
387 (poss nil)
388 helpposs
389 (case-fold-search completion-ignore-case))
390
391 ;; Check if buffer contents can already be considered complete
392 (if (and (eq mode 'exit)
393 (PC-is-complete-p str table pred))
394 'complete
395
396 ;; Record how many characters at the beginning are not included
397 ;; in completion.
398 (setq dirlength
399 (if filename
400 (length (file-name-directory str))
401 0))
402
403 ;; Do substitutions in directory names
404 (and filename
405 (not (equal str (setq p (substitute-in-file-name str))))
406 (progn
407 (delete-region beg end)
408 (insert p)
409 (setq str p end (+ beg (length str)))))
410
411 ;; Prepare various delimiter strings
412 (or (equal PC-word-delimiters PC-delims)
413 (setq PC-delims PC-word-delimiters
414 PC-delim-regex (concat "[" PC-delims "]")
415 PC-ndelims-regex (concat "[^" PC-delims "]*")
416 PC-delims-list (append PC-delims nil)))
417
418 ;; Look for wildcard expansions in directory name
419 (and filename
420 (string-match "\\*.*/" str)
421 (let ((pat str)
422 files)
423 (setq p (1+ (string-match "/[^/]*\\'" pat)))
424 (while (setq p (string-match PC-delim-regex pat p))
425 (setq pat (concat (substring pat 0 p)
426 "*"
427 (substring pat p))
428 p (+ p 2)))
429 (setq files (PC-expand-many-files (concat pat "*")))
430 (if files
431 (let ((dir (file-name-directory (car files)))
432 (p files))
433 (while (and (setq p (cdr p))
434 (equal dir (file-name-directory (car p)))))
435 (if p
436 (setq filename nil table nil pred nil
437 ambig t)
438 (delete-region beg end)
439 (setq str (concat dir (file-name-nondirectory str)))
440 (insert str)
441 (setq end (+ beg (length str)))))
442 (setq filename nil table nil pred nil))))
443
444 ;; Strip directory name if appropriate
445 (if filename
446 (if incname
447 (setq basestr (substring str incname)
448 dirname (substring str 0 incname))
449 (setq basestr (file-name-nondirectory str)
450 dirname (file-name-directory str))
451 ;; Make sure str is consistent with its directory and basename
452 ;; parts. This is important on DOZe'NT systems when str only
453 ;; includes a drive letter, like in "d:".
454 (setq str (concat dirname basestr)))
455 (setq basestr str))
456
457 ;; Convert search pattern to a standard regular expression
458 (setq regex (regexp-quote basestr)
459 offset (if (and (> (length regex) 0)
460 (not (eq (aref basestr 0) ?\*))
461 (or (eq PC-first-char t)
462 (and PC-first-char filename))) 1 0)
463 p offset)
464 (while (setq p (string-match PC-delim-regex regex p))
465 (if (eq (aref regex p) ? )
466 (setq regex (concat (substring regex 0 p)
467 PC-ndelims-regex
468 PC-delim-regex
469 (substring regex (1+ p)))
470 p (+ p (length PC-ndelims-regex) (length PC-delim-regex)))
471 (let ((bump (if (memq (aref regex p)
472 '(?$ ?^ ?\. ?* ?+ ?? ?[ ?] ?\\))
473 -1 0)))
474 (setq regex (concat (substring regex 0 (+ p bump))
475 PC-ndelims-regex
476 (substring regex (+ p bump)))
477 p (+ p (length PC-ndelims-regex) 1)))))
478 (setq p 0)
479 (if filename
480 (while (setq p (string-match "\\\\\\*" regex p))
481 (setq regex (concat (substring regex 0 p)
482 "[^/]*"
483 (substring regex (+ p 2))))))
484 ;;(setq the-regex regex)
485 (setq regex (concat "\\`" regex))
486
487 ;; Find an initial list of possible completions
488 (if (not (setq p (string-match (concat PC-delim-regex
489 (if filename "\\|\\*" ""))
490 str
491 (+ (length dirname) offset))))
492
493 ;; Minibuffer contains no hyphens -- simple case!
494 (setq poss (all-completions str
495 table
496 pred))
497
498 ;; Use all-completions to do an initial cull. This is a big win,
499 ;; since all-completions is written in C!
500 (let ((compl (all-completions (substring str 0 p)
501 table
502 pred)))
503 (setq p compl)
504 (while p
505 (and (string-match regex (car p))
506 (progn
507 (set-text-properties 0 (length (car p)) '() (car p))
508 (setq poss (cons (car p) poss))))
509 (setq p (cdr p)))))
510
511 ;; Now we have a list of possible completions
512 (cond
513
514 ;; No valid completions found
515 ((null poss)
516 (if (and (eq mode 'word)
517 (not PC-word-failed-flag))
518 (let ((PC-word-failed-flag t))
519 (delete-backward-char 1)
520 (PC-do-completion 'word))
521 (beep)
522 (PC-temp-minibuffer-message (if ambig
523 " [Ambiguous dir name]"
524 (if (eq mode 'help)
525 " [No completions]"
526 " [No match]")))
527 nil))
528
529 ;; More than one valid completion found
530 ((or (cdr (setq helpposs poss))
531 (memq mode '(help word)))
532
533 ;; Handle completion-ignored-extensions
534 (and filename
535 (not (eq mode 'help))
536 (let ((p2 poss))
537
538 ;; Build a regular expression representing the extensions list
539 (or (equal completion-ignored-extensions PC-ignored-extensions)
540 (setq PC-ignored-regexp
541 (concat "\\("
542 (mapconcat
543 'regexp-quote
544 (setq PC-ignored-extensions
545 completion-ignored-extensions)
546 "\\|")
547 "\\)\\'")))
548
549 ;; Check if there are any without an ignored extension.
550 ;; Also ignore `.' and `..'.
551 (setq p nil)
552 (while p2
553 (or (string-match PC-ignored-regexp (car p2))
554 (string-match "\\(\\`\\|/\\)[.][.]?/?\\'" (car p2))
555 (setq p (cons (car p2) p)))
556 (setq p2 (cdr p2)))
557
558 ;; If there are "good" names, use them
559 (and p (setq poss p))))
560
561 ;; Is the actual string one of the possible completions?
562 (setq p (and (not (eq mode 'help)) poss))
563 (while (and p
564 (not (string-equal (car p) basestr)))
565 (setq p (cdr p)))
566 (and p (null mode)
567 (PC-temp-minibuffer-message " [Complete, but not unique]"))
568 (if (and p
569 (not (and (null mode)
570 (eq this-command last-command))))
571 t
572
573 ;; If ambiguous, try for a partial completion
574 (let ((improved nil)
575 prefix
576 (pt nil)
577 (skip "\\`"))
578
579 ;; Check if next few letters are the same in all cases
580 (if (and (not (eq mode 'help))
581 (setq prefix (try-completion "" (mapcar 'list poss))))
582 (let ((first t) i)
583 (if (eq mode 'word)
584 (setq prefix (PC-chop-word prefix basestr)))
585 (goto-char (+ beg (length dirname)))
586 (while (and (progn
587 (setq i 0)
588 (while (< i (length prefix))
589 (if (and (< (point) end)
590 (eq (aref prefix i)
591 (following-char)))
592 (forward-char 1)
593 (if (and (< (point) end)
594 (or (and (looking-at " ")
595 (memq (aref prefix i)
596 PC-delims-list))
597 (eq (downcase (aref prefix i))
598 (downcase
599 (following-char)))))
600 (progn
601 (delete-char 1)
602 (setq end (1- end)))
603 (and filename (looking-at "\\*")
604 (progn
605 (delete-char 1)
606 (setq end (1- end))))
607 (setq improved t))
608 (insert (substring prefix i (1+ i)))
609 (setq end (1+ end)))
610 (setq i (1+ i)))
611 (or pt (equal (point) beg)
612 (setq pt (point)))
613 (looking-at PC-delim-regex))
614 (setq skip (concat skip
615 (regexp-quote prefix)
616 PC-ndelims-regex)
617 prefix (try-completion
618 ""
619 (mapcar
620 (function
621 (lambda (x)
622 (list
623 (and (string-match skip x)
624 (substring
625 x
626 (match-end 0))))))
627 poss)))
628 (or (> i 0) (> (length prefix) 0))
629 (or (not (eq mode 'word))
630 (and first (> (length prefix) 0)
631 (setq first nil
632 prefix (substring prefix 0 1))))))
633 (goto-char (if (eq mode 'word) end
634 (or pt beg)))))
635
636 (if (and (eq mode 'word)
637 (not PC-word-failed-flag))
638
639 (if improved
640
641 ;; We changed it... would it be complete without the space?
642 (if (PC-is-complete-p (buffer-substring 1 (1- end))
643 table pred)
644 (delete-region (1- end) end)))
645
646 (if improved
647
648 ;; We changed it... enough to be complete?
649 (and (eq mode 'exit)
650 (PC-is-complete-p (field-string) table pred))
651
652 ;; If totally ambiguous, display a list of completions
653 (if (or completion-auto-help
654 (eq mode 'help))
655 (with-output-to-temp-buffer "*Completions*"
656 (display-completion-list (sort helpposs 'string-lessp))
657 (save-excursion
658 (set-buffer standard-output)
659 ;; Record which part of the buffer we are completing
660 ;; so that choosing a completion from the list
661 ;; knows how much old text to replace.
662 (setq completion-base-size dirlength)))
663 (PC-temp-minibuffer-message " [Next char not unique]"))
664 nil)))))
665
666 ;; Only one possible completion
667 (t
668 (if (equal basestr (car poss))
669 (if (null mode)
670 (PC-temp-minibuffer-message " [Sole completion]"))
671 (delete-region beg end)
672 (insert (format "%s"
673 (if filename
674 (substitute-in-file-name (concat dirname (car poss)))
675 (car poss)))))
676 t)))))
677
678
679 (defun PC-is-complete-p (str table pred)
680 (let ((res (if (listp table)
681 (assoc str table)
682 (if (vectorp table)
683 (or (equal str "nil") ; heh, heh, heh
684 (intern-soft str table))
685 (funcall table str pred 'lambda)))))
686 (and res
687 (or (not pred)
688 (and (not (listp table)) (not (vectorp table)))
689 (funcall pred res))
690 res)))
691
692 (defun PC-chop-word (new old)
693 (let ((i -1)
694 (j -1))
695 (while (and (setq i (string-match PC-delim-regex old (1+ i)))
696 (setq j (string-match PC-delim-regex new (1+ j)))))
697 (if (and j
698 (or (not PC-word-failed-flag)
699 (setq j (string-match PC-delim-regex new (1+ j)))))
700 (substring new 0 (1+ j))
701 new)))
702
703 (defvar PC-not-minibuffer nil)
704
705 (defun PC-temp-minibuffer-message (message)
706 "A Lisp version of `temp_minibuffer_message' from minibuf.c."
707 (cond (PC-not-minibuffer
708 (message message)
709 (sit-for 2)
710 (message ""))
711 ((fboundp 'temp-minibuffer-message)
712 (temp-minibuffer-message message))
713 (t
714 (let ((point-max (point-max)))
715 (save-excursion
716 (goto-char point-max)
717 (insert message))
718 (let ((inhibit-quit t))
719 (sit-for 2)
720 (delete-region point-max (point-max))
721 (when quit-flag
722 (setq quit-flag nil
723 unread-command-events '(7))))))))
724
725
726 (defun PC-lisp-complete-symbol ()
727 "Perform completion on Lisp symbol preceding point.
728 That symbol is compared against the symbols that exist
729 and any additional characters determined by what is there
730 are inserted.
731 If the symbol starts just after an open-parenthesis,
732 only symbols with function definitions are considered.
733 Otherwise, all symbols with function definitions, values
734 or properties are considered."
735 (interactive)
736 (let* ((end (point))
737 (buffer-syntax (syntax-table))
738 (beg (unwind-protect
739 (save-excursion
740 (if lisp-mode-syntax-table
741 (set-syntax-table lisp-mode-syntax-table))
742 (backward-sexp 1)
743 (while (= (char-syntax (following-char)) ?\')
744 (forward-char 1))
745 (point))
746 (set-syntax-table buffer-syntax)))
747 (minibuffer-completion-table obarray)
748 (minibuffer-completion-predicate
749 (if (eq (char-after (1- beg)) ?\()
750 'fboundp
751 (function (lambda (sym)
752 (or (boundp sym) (fboundp sym)
753 (symbol-plist sym))))))
754 (PC-not-minibuffer t))
755 (PC-do-completion nil beg end)))
756
757 ;;; Use the shell to do globbing.
758 ;;; This could now use file-expand-wildcards instead.
759
760 (defun PC-expand-many-files (name)
761 (save-excursion
762 (set-buffer (generate-new-buffer " *Glob Output*"))
763 (erase-buffer)
764 (shell-command (concat "echo " name) t)
765 (goto-char (point-min))
766 (if (looking-at ".*No match")
767 nil
768 (insert "(\"")
769 (while (search-forward " " nil t)
770 (delete-backward-char 1)
771 (insert "\" \""))
772 (goto-char (point-max))
773 (delete-backward-char 1)
774 (insert "\")")
775 (goto-char (point-min))
776 (let ((files (read (current-buffer))) (p nil))
777 (kill-buffer (current-buffer))
778 (or (equal completion-ignored-extensions PC-ignored-extensions)
779 (setq PC-ignored-regexp
780 (concat "\\("
781 (mapconcat
782 'regexp-quote
783 (setq PC-ignored-extensions
784 completion-ignored-extensions)
785 "\\|")
786 "\\)\\'")))
787 (setq p nil)
788 (while files
789 (or (string-match PC-ignored-regexp (car files))
790 (setq p (cons (car files) p)))
791 (setq files (cdr files)))
792 p))))
793
794 ;;; Facilities for loading C header files. This is independent from the
795 ;;; main completion code. See also the variable `PC-include-file-path'
796 ;;; at top of this file.
797
798 (defun PC-look-for-include-file ()
799 (if (string-match "[\"<]\\([^\"<>]*\\)[\">]?$" (buffer-file-name))
800 (let ((name (substring (buffer-file-name)
801 (match-beginning 1) (match-end 1)))
802 (punc (aref (buffer-file-name) (match-beginning 0)))
803 (path nil)
804 new-buf)
805 (kill-buffer (current-buffer))
806 (if (equal name "")
807 (save-excursion
808 (set-buffer (car (buffer-list)))
809 (save-excursion
810 (beginning-of-line)
811 (if (looking-at
812 "[ \t]*#[ \t]*include[ \t]+[<\"]\\(.+\\)[>\"][ \t]*[\n/]")
813 (setq name (buffer-substring (match-beginning 1)
814 (match-end 1))
815 punc (char-after (1- (match-beginning 1))))
816 ;; Suggested by Frank Siebenlist:
817 (if (or (looking-at
818 "[ \t]*([ \t]*load[ \t]+\"\\([^\"]+\\)\"")
819 (looking-at
820 "[ \t]*([ \t]*load-library[ \t]+\"\\([^\"]+\\)\"")
821 (looking-at
822 "[ \t]*([ \t]*require[ \t]+'\\([^\t )]+\\)[\t )]"))
823 (progn
824 (setq name (buffer-substring (match-beginning 1)
825 (match-end 1))
826 punc ?\<
827 path load-path)
828 (if (string-match "\\.elc$" name)
829 (setq name (substring name 0 -1))
830 (or (string-match "\\.el$" name)
831 (setq name (concat name ".el")))))
832 (error "Not on an #include line"))))))
833 (or (string-match "\\.[[:alnum:]]+$" name)
834 (setq name (concat name ".h")))
835 (if (eq punc ?\<)
836 (let ((path (or path (PC-include-file-path))))
837 (while (and path
838 (not (file-exists-p
839 (concat (file-name-as-directory (car path))
840 name))))
841 (setq path (cdr path)))
842 (if path
843 (setq name (concat (file-name-as-directory (car path)) name))
844 (error "No such include file: <%s>" name)))
845 (let ((dir (save-excursion
846 (set-buffer (car (buffer-list)))
847 default-directory)))
848 (if (file-exists-p (concat dir name))
849 (setq name (concat dir name))
850 (error "No such include file: `%s'" name))))
851 (setq new-buf (get-file-buffer name))
852 (if new-buf
853 ;; no need to verify last-modified time for this!
854 (set-buffer new-buf)
855 (setq new-buf (create-file-buffer name))
856 (set-buffer new-buf)
857 (erase-buffer)
858 (insert-file-contents name t))
859 ;; Returning non-nil with the new buffer current
860 ;; is sufficient to tell find-file to use it.
861 t)
862 nil))
863
864 (defun PC-include-file-path ()
865 (or PC-include-file-path
866 (let ((env (getenv "INCPATH"))
867 (path nil)
868 pos)
869 (or env (error "No include file path specified"))
870 (while (setq pos (string-match ":[^:]+$" env))
871 (setq path (cons (substring env (1+ pos)) path)
872 env (substring env 0 pos)))
873 path)))
874
875 ;;; This is adapted from lib-complete.el, by Mike Williams.
876 (defun PC-include-file-all-completions (file search-path &optional full)
877 "Return all completions for FILE in any directory on SEARCH-PATH.
878 If optional third argument FULL is non-nil, returned pathnames should be
879 absolute rather than relative to some directory on the SEARCH-PATH."
880 (setq search-path
881 (mapcar '(lambda (dir)
882 (if dir (file-name-as-directory dir) default-directory))
883 search-path))
884 (if (file-name-absolute-p file)
885 ;; It's an absolute file name, so don't need search-path
886 (progn
887 (setq file (expand-file-name file))
888 (file-name-all-completions
889 (file-name-nondirectory file) (file-name-directory file)))
890 (let ((subdir (file-name-directory file))
891 (ndfile (file-name-nondirectory file))
892 file-lists)
893 ;; Append subdirectory part to each element of search-path
894 (if subdir
895 (setq search-path
896 (mapcar '(lambda (dir) (concat dir subdir))
897 search-path)
898 file ))
899 ;; Make list of completions in each directory on search-path
900 (while search-path
901 (let* ((dir (car search-path))
902 (subdir (if full dir subdir)))
903 (if (file-directory-p dir)
904 (progn
905 (setq file-lists
906 (cons
907 (mapcar '(lambda (file) (concat subdir file))
908 (file-name-all-completions ndfile
909 (car search-path)))
910 file-lists))))
911 (setq search-path (cdr search-path))))
912 ;; Compress out duplicates while building complete list (slloooow!)
913 (let ((sorted (sort (apply 'nconc file-lists)
914 '(lambda (x y) (not (string-lessp x y)))))
915 compressed)
916 (while sorted
917 (if (equal (car sorted) (car compressed)) nil
918 (setq compressed (cons (car sorted) compressed)))
919 (setq sorted (cdr sorted)))
920 compressed))))
921
922 (defun PC-read-include-file-name-internal (string dir action)
923 (if (string-match "<\\([^\"<>]*\\)>?$" string)
924 (let* ((name (substring string (match-beginning 1) (match-end 1)))
925 (str2 (substring string (match-beginning 0)))
926 (completion-table
927 (mapcar (function (lambda (x) (list (format "<%s>" x))))
928 (PC-include-file-all-completions
929 name (PC-include-file-path)))))
930 (cond
931 ((not completion-table) nil)
932 ((eq action nil) (try-completion str2 completion-table nil))
933 ((eq action t) (all-completions str2 completion-table nil))
934 ((eq action 'lambda)
935 (eq (try-completion str2 completion-table nil) t))))
936 (funcall PC-old-read-file-name-internal string dir action)))
937 \f
938
939 (provide 'complete)
940
941 ;;; End.