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