Commit | Line | Data |
---|---|---|
32bae13c SM |
1 | ;;; minibuffer.el --- Minibuffer completion functions |
2 | ||
3 | ;; Copyright (C) 2008 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
9 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
11 | ;; the Free Software Foundation, either version 3 of the License, or | |
12 | ;; (at your option) any later version. | |
13 | ||
14 | ;; This program is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
ba5ff07b SM |
24 | ;; Names starting with "minibuffer--" are for functions and variables that |
25 | ;; are meant to be for internal use only. | |
26 | ||
e2947429 | 27 | ;; TODO: |
d28cfdc2 SM |
28 | ;; - New command minibuffer-force-complete that chooses one of all-completions. |
29 | ;; - make the `hide-spaces' arg of all-completions obsolete? | |
32bae13c SM |
30 | |
31 | ;;; Code: | |
32 | ||
33 | (eval-when-compile (require 'cl)) | |
34 | ||
e2947429 SM |
35 | (defvar completion-all-completions-with-base-size nil |
36 | "If non-nil, `all-completions' may return the base-size in the last cdr. | |
37 | The base-size is the length of the prefix that is elided from each | |
38 | element in the returned list of completions. See `completion-base-size'.") | |
39 | ||
21622c6d SM |
40 | ;;; Completion table manipulation |
41 | ||
e2947429 SM |
42 | (defun completion--some (fun xs) |
43 | "Apply FUN to each element of XS in turn. | |
44 | Return the first non-nil returned value. | |
45 | Like CL's `some'." | |
46 | (let (res) | |
47 | (while (and (not res) xs) | |
48 | (setq res (funcall fun (pop xs)))) | |
49 | res)) | |
50 | ||
21622c6d | 51 | (defun apply-partially (fun &rest args) |
e2947429 SM |
52 | "Do a \"curried\" partial application of FUN to ARGS. |
53 | ARGS is a list of the first N arguments to pass to FUN. | |
54 | The result is a new function that takes the remaining arguments, | |
55 | and calls FUN." | |
21622c6d SM |
56 | (lexical-let ((fun fun) (args1 args)) |
57 | (lambda (&rest args2) (apply fun (append args1 args2))))) | |
58 | ||
59 | (defun complete-with-action (action table string pred) | |
60 | "Perform completion ACTION. | |
61 | STRING is the string to complete. | |
62 | TABLE is the completion table, which should not be a function. | |
63 | PRED is a completion predicate. | |
64 | ACTION can be one of nil, t or `lambda'." | |
65 | ;; (assert (not (functionp table))) | |
66 | (funcall | |
67 | (cond | |
68 | ((null action) 'try-completion) | |
69 | ((eq action t) 'all-completions) | |
70 | (t 'test-completion)) | |
71 | string table pred)) | |
72 | ||
73 | (defun completion-table-dynamic (fun) | |
74 | "Use function FUN as a dynamic completion table. | |
75 | FUN is called with one argument, the string for which completion is required, | |
76 | and it should return an alist containing all the intended possible | |
77 | completions. This alist may be a full list of possible completions so that FUN | |
78 | can ignore the value of its argument. If completion is performed in the | |
79 | minibuffer, FUN will be called in the buffer from which the minibuffer was | |
80 | entered. | |
81 | ||
82 | The result of the `dynamic-completion-table' form is a function | |
83 | that can be used as the ALIST argument to `try-completion' and | |
84 | `all-completion'. See Info node `(elisp)Programmed Completion'." | |
85 | (lexical-let ((fun fun)) | |
86 | (lambda (string pred action) | |
87 | (with-current-buffer (let ((win (minibuffer-selected-window))) | |
88 | (if (window-live-p win) (window-buffer win) | |
89 | (current-buffer))) | |
90 | (complete-with-action action (funcall fun string) string pred))))) | |
91 | ||
92 | (defmacro lazy-completion-table (var fun) | |
93 | "Initialize variable VAR as a lazy completion table. | |
94 | If the completion table VAR is used for the first time (e.g., by passing VAR | |
95 | as an argument to `try-completion'), the function FUN is called with no | |
96 | arguments. FUN must return the completion table that will be stored in VAR. | |
97 | If completion is requested in the minibuffer, FUN will be called in the buffer | |
98 | from which the minibuffer was entered. The return value of | |
99 | `lazy-completion-table' must be used to initialize the value of VAR. | |
100 | ||
101 | You should give VAR a non-nil `risky-local-variable' property." | |
69e018a7 | 102 | (declare (debug (symbolp lambda-expr))) |
21622c6d SM |
103 | (let ((str (make-symbol "string"))) |
104 | `(completion-table-dynamic | |
105 | (lambda (,str) | |
106 | (when (functionp ,var) | |
107 | (setq ,var (,fun))) | |
108 | ,var)))) | |
109 | ||
110 | (defun completion-table-with-context (prefix table string pred action) | |
25c0d999 | 111 | ;; TODO: add `suffix' maybe? |
e2947429 SM |
112 | ;; Notice that `pred' is not a predicate when called from read-file-name |
113 | ;; or Info-read-node-name-2. | |
25c0d999 SM |
114 | (if (functionp pred) |
115 | (setq pred (lexical-let ((pred pred)) | |
116 | ;; FIXME: this doesn't work if `table' is an obarray. | |
117 | (lambda (s) (funcall pred (concat prefix s)))))) | |
e2947429 SM |
118 | (let ((comp (complete-with-action action table string pred))) |
119 | (cond | |
120 | ;; In case of try-completion, add the prefix. | |
121 | ((stringp comp) (concat prefix comp)) | |
122 | ;; In case of non-empty all-completions, | |
123 | ;; add the prefix size to the base-size. | |
124 | ((consp comp) | |
125 | (let ((last (last comp))) | |
126 | (when completion-all-completions-with-base-size | |
127 | (setcdr last (+ (or (cdr last) 0) (length prefix)))) | |
128 | comp)) | |
129 | (t comp)))) | |
21622c6d SM |
130 | |
131 | (defun completion-table-with-terminator (terminator table string pred action) | |
25c0d999 SM |
132 | (cond |
133 | ((eq action nil) | |
134 | (let ((comp (try-completion string table pred))) | |
88893215 SM |
135 | (if (eq comp t) |
136 | (concat string terminator) | |
137 | (if (and (stringp comp) | |
25c0d999 | 138 | (eq (try-completion comp table pred) t)) |
88893215 | 139 | (concat comp terminator) |
25c0d999 SM |
140 | comp)))) |
141 | ((eq action t) (all-completions string table pred)) | |
142 | ;; completion-table-with-terminator is always used for | |
143 | ;; "sub-completions" so it's only called if the terminator is missing, | |
144 | ;; in which case `test-completion' should return nil. | |
145 | ((eq action 'lambda) nil))) | |
146 | ||
147 | (defun completion-table-with-predicate (table pred1 strict string pred2 action) | |
148 | "Make a completion table equivalent to TABLE but filtered through PRED1. | |
149 | PRED1 is a function of one argument which returns non-nil iff the | |
150 | argument is an element of TABLE which should be considered for completion. | |
151 | STRING, PRED2, and ACTION are the usual arguments to completion tables, | |
152 | as described in `try-completion', `all-completions', and `test-completion'. | |
153 | If STRICT is t, the predicate always applies, if nil it only applies if | |
154 | it doesn't reduce the set of possible completions to nothing. | |
155 | Note: TABLE needs to be a proper completion table which obeys predicates." | |
156 | (cond | |
157 | ((and (not strict) (eq action 'lambda)) | |
158 | ;; Ignore pred1 since it doesn't really have to apply anyway. | |
159 | (test-completion string tabel pred2)) | |
160 | (t | |
161 | (or (complete-with-action action table string | |
162 | (if (null pred2) pred1 | |
163 | (lexical-let ((pred1 pred2) (pred2 pred2)) | |
164 | (lambda (x) | |
165 | ;; Call `pred1' first, so that `pred2' | |
166 | ;; really can't tell that `x' is in table. | |
167 | (if (funcall pred1 x) (funcall pred2 x)))))) | |
168 | ;; If completion failed and we're not applying pred1 strictly, try | |
169 | ;; again without pred1. | |
170 | (and (not strict) | |
171 | (complete-with-action action table string pred2)))))) | |
21622c6d | 172 | |
e2947429 SM |
173 | (defun completion-table-in-turn (&rest tables) |
174 | "Create a completion table that tries each table in TABLES in turn." | |
175 | (lexical-let ((tables tables)) | |
21622c6d | 176 | (lambda (string pred action) |
e2947429 SM |
177 | (completion--some (lambda (table) |
178 | (complete-with-action action table string pred)) | |
179 | tables)))) | |
180 | ||
25c0d999 SM |
181 | ;; (defmacro complete-in-turn (a b) `(completion-table-in-turn ,a ,b)) |
182 | ;; (defmacro dynamic-completion-table (fun) `(completion-table-dynamic ,fun)) | |
e2947429 SM |
183 | (define-obsolete-function-alias |
184 | 'complete-in-turn 'completion-table-in-turn "23.1") | |
25c0d999 SM |
185 | (define-obsolete-function-alias |
186 | 'dynamic-completion-table 'completion-table-dynamic "23.1") | |
21622c6d SM |
187 | |
188 | ;;; Minibuffer completion | |
189 | ||
ba5ff07b SM |
190 | (defgroup minibuffer nil |
191 | "Controlling the behavior of the minibuffer." | |
192 | :link '(custom-manual "(emacs)Minibuffer") | |
193 | :group 'environment) | |
194 | ||
32bae13c SM |
195 | (defun minibuffer-message (message &rest args) |
196 | "Temporarily display MESSAGE at the end of the minibuffer. | |
197 | The text is displayed for `minibuffer-message-timeout' seconds, | |
198 | or until the next input event arrives, whichever comes first. | |
199 | Enclose MESSAGE in [...] if this is not yet the case. | |
200 | If ARGS are provided, then pass MESSAGE through `format'." | |
201 | ;; Clear out any old echo-area message to make way for our new thing. | |
202 | (message nil) | |
bd5c2732 SM |
203 | (setq message (if (and (null args) (string-match "\\[.+\\]" message)) |
204 | ;; Make sure we can put-text-property. | |
205 | (copy-sequence message) | |
206 | (concat " [" message "]"))) | |
32bae13c SM |
207 | (when args (setq message (apply 'format message args))) |
208 | (let ((ol (make-overlay (point-max) (point-max) nil t t))) | |
209 | (unwind-protect | |
210 | (progn | |
bf87d5fc SM |
211 | (unless (zerop (length message)) |
212 | ;; The current C cursor code doesn't know to use the overlay's | |
213 | ;; marker's stickiness to figure out whether to place the cursor | |
214 | ;; before or after the string, so let's spoon-feed it the pos. | |
215 | (put-text-property 0 1 'cursor t message)) | |
32bae13c SM |
216 | (overlay-put ol 'after-string message) |
217 | (sit-for (or minibuffer-message-timeout 1000000))) | |
218 | (delete-overlay ol)))) | |
219 | ||
220 | (defun minibuffer-completion-contents () | |
221 | "Return the user input in a minibuffer before point as a string. | |
222 | That is what completion commands operate on." | |
223 | (buffer-substring (field-beginning) (point))) | |
224 | ||
225 | (defun delete-minibuffer-contents () | |
226 | "Delete all user input in a minibuffer. | |
227 | If the current buffer is not a minibuffer, erase its entire contents." | |
228 | (delete-field)) | |
229 | ||
ba5ff07b SM |
230 | (defcustom completion-auto-help t |
231 | "Non-nil means automatically provide help for invalid completion input. | |
232 | If the value is t the *Completion* buffer is displayed whenever completion | |
233 | is requested but cannot be done. | |
234 | If the value is `lazy', the *Completions* buffer is only displayed after | |
235 | the second failed attempt to complete." | |
e1bb0fe5 | 236 | :type '(choice (const nil) (const t) (const lazy)) |
ba5ff07b SM |
237 | :group 'minibuffer) |
238 | ||
e2947429 SM |
239 | (defvar completion-styles-alist |
240 | '((basic try-completion all-completions) | |
241 | ;; (partial-completion | |
242 | ;; completion-pcm--try-completion completion-pcm--all-completions) | |
243 | ) | |
244 | "List of available completion styles. | |
245 | Each element has the form (NAME TRY-COMPLETION ALL-COMPLETIONS) | |
246 | where NAME is the name that should be used in `completion-styles' | |
247 | TRY-COMPLETION is the function that does the completion, and | |
248 | ALL-COMPLETIONS is the function that lists the completions.") | |
249 | ||
250 | (defcustom completion-styles '(basic) | |
251 | "List of completion styles to use." | |
252 | :type `(repeat (choice ,@(mapcar (lambda (x) (list 'const (car x))) | |
253 | completion-styles-alist))) | |
254 | :group 'minibuffer | |
255 | :version "23.1") | |
256 | ||
257 | (defun minibuffer-try-completion (string table pred) | |
258 | (if (and (symbolp table) (get table 'no-completion-styles)) | |
259 | (try-completion string table pred) | |
260 | (completion--some (lambda (style) | |
2ed430f4 | 261 | (funcall (nth 1 (assq style completion-styles-alist)) |
e2947429 SM |
262 | string table pred)) |
263 | completion-styles))) | |
264 | ||
265 | (defun minibuffer-all-completions (string table pred &optional hide-spaces) | |
266 | (let ((completion-all-completions-with-base-size t)) | |
267 | (if (and (symbolp table) (get table 'no-completion-styles)) | |
268 | (all-completions string table pred hide-spaces) | |
269 | (completion--some (lambda (style) | |
2ed430f4 | 270 | (funcall (nth 2 (assq style completion-styles-alist)) |
e2947429 SM |
271 | string table pred hide-spaces)) |
272 | completion-styles)))) | |
273 | ||
ba5ff07b SM |
274 | (defun minibuffer--bitset (modified completions exact) |
275 | (logior (if modified 4 0) | |
276 | (if completions 2 0) | |
277 | (if exact 1 0))) | |
278 | ||
279 | (defun minibuffer--do-completion (&optional try-completion-function) | |
32bae13c | 280 | "Do the completion and return a summary of what happened. |
ba5ff07b SM |
281 | M = completion was performed, the text was Modified. |
282 | C = there were available Completions. | |
283 | E = after completion we now have an Exact match. | |
284 | ||
285 | MCE | |
286 | 000 0 no possible completion | |
287 | 001 1 was already an exact and unique completion | |
288 | 010 2 no completion happened | |
289 | 011 3 was already an exact completion | |
290 | 100 4 ??? impossible | |
291 | 101 5 ??? impossible | |
292 | 110 6 some completion happened | |
293 | 111 7 completed to an exact completion" | |
294 | (let* ((beg (field-beginning)) | |
295 | (string (buffer-substring beg (point))) | |
e2947429 SM |
296 | (completion (funcall (or try-completion-function |
297 | 'minibuffer-try-completion) | |
ba5ff07b SM |
298 | string |
299 | minibuffer-completion-table | |
300 | minibuffer-completion-predicate))) | |
32bae13c SM |
301 | (cond |
302 | ((null completion) | |
ba5ff07b SM |
303 | (ding) (minibuffer-message "No match") (minibuffer--bitset nil nil nil)) |
304 | ((eq t completion) (minibuffer--bitset nil nil t)) ;Exact and unique match. | |
32bae13c SM |
305 | (t |
306 | ;; `completed' should be t if some completion was done, which doesn't | |
307 | ;; include simply changing the case of the entered string. However, | |
308 | ;; for appearance, the string is rewritten if the case changes. | |
309 | (let ((completed (not (eq t (compare-strings completion nil nil | |
310 | string nil nil t)))) | |
311 | (unchanged (eq t (compare-strings completion nil nil | |
312 | string nil nil nil)))) | |
313 | (unless unchanged | |
ba5ff07b SM |
314 | ;; Merge a trailing / in completion with a / after point. |
315 | ;; We used to only do it for word completion, but it seems to make | |
316 | ;; sense for all completions. | |
317 | (if (and (eq ?/ (aref completion (1- (length completion)))) | |
318 | (< (point) (field-end)) | |
319 | (eq ?/ (char-after))) | |
320 | (setq completion (substring completion 0 -1))) | |
321 | ||
322 | ;; Insert in minibuffer the chars we got. | |
323 | (let ((end (point))) | |
32bae13c SM |
324 | (insert completion) |
325 | (delete-region beg end))) | |
ba5ff07b | 326 | |
32bae13c SM |
327 | (if (not (or unchanged completed)) |
328 | ;; The case of the string changed, but that's all. We're not sure | |
329 | ;; whether this is a unique completion or not, so try again using | |
330 | ;; the real case (this shouldn't recurse again, because the next | |
331 | ;; time try-completion will return either t or the exact string). | |
d2925a49 | 332 | (minibuffer--do-completion try-completion-function) |
32bae13c SM |
333 | |
334 | ;; It did find a match. Do we match some possibility exactly now? | |
335 | (let ((exact (test-completion (field-string) | |
336 | minibuffer-completion-table | |
337 | minibuffer-completion-predicate))) | |
ba5ff07b SM |
338 | (unless completed |
339 | ;; Show the completion table, if requested. | |
340 | (cond | |
341 | ((not exact) | |
342 | (if (case completion-auto-help | |
343 | (lazy (eq this-command last-command)) | |
344 | (t completion-auto-help)) | |
345 | (minibuffer-completion-help) | |
346 | (minibuffer-message "Next char not unique"))) | |
347 | ;; If the last exact completion and this one were the same, | |
348 | ;; it means we've already given a "Complete but not unique" | |
349 | ;; message and the user's hit TAB again, so now we give him help. | |
350 | ((eq this-command last-command) | |
351 | (if completion-auto-help (minibuffer-completion-help))))) | |
352 | ||
353 | (minibuffer--bitset completed t exact)))))))) | |
32bae13c SM |
354 | |
355 | (defun minibuffer-complete () | |
356 | "Complete the minibuffer contents as far as possible. | |
357 | Return nil if there is no valid completion, else t. | |
358 | If no characters can be completed, display a list of possible completions. | |
359 | If you repeat this command after it displayed such a list, | |
360 | scroll the window of possible completions." | |
361 | (interactive) | |
362 | ;; If the previous command was not this, | |
363 | ;; mark the completion buffer obsolete. | |
364 | (unless (eq this-command last-command) | |
365 | (setq minibuffer-scroll-window nil)) | |
366 | ||
367 | (let ((window minibuffer-scroll-window)) | |
368 | ;; If there's a fresh completion window with a live buffer, | |
369 | ;; and this command is repeated, scroll that window. | |
370 | (if (window-live-p window) | |
371 | (with-current-buffer (window-buffer window) | |
372 | (if (pos-visible-in-window-p (point-max) window) | |
373 | ;; If end is in view, scroll up to the beginning. | |
374 | (set-window-start window (point-min) nil) | |
375 | ;; Else scroll down one screen. | |
376 | (scroll-other-window)) | |
377 | nil) | |
378 | ||
ba5ff07b SM |
379 | (case (minibuffer--do-completion) |
380 | (0 nil) | |
381 | (1 (goto-char (field-end)) | |
382 | (minibuffer-message "Sole completion") | |
383 | t) | |
384 | (3 (goto-char (field-end)) | |
385 | (minibuffer-message "Complete, but not unique") | |
386 | t) | |
387 | (t t))))) | |
32bae13c SM |
388 | |
389 | (defun minibuffer-complete-and-exit () | |
390 | "If the minibuffer contents is a valid completion then exit. | |
391 | Otherwise try to complete it. If completion leads to a valid completion, | |
392 | a repetition of this command will exit." | |
393 | (interactive) | |
394 | (cond | |
395 | ;; Allow user to specify null string | |
396 | ((= (field-beginning) (field-end)) (exit-minibuffer)) | |
397 | ((test-completion (field-string) | |
398 | minibuffer-completion-table | |
399 | minibuffer-completion-predicate) | |
400 | (when completion-ignore-case | |
401 | ;; Fixup case of the field, if necessary. | |
402 | (let* ((string (field-string)) | |
e2947429 SM |
403 | (compl (minibuffer-try-completion |
404 | string | |
405 | minibuffer-completion-table | |
406 | minibuffer-completion-predicate))) | |
32bae13c SM |
407 | (when (and (stringp compl) |
408 | ;; If it weren't for this piece of paranoia, I'd replace | |
409 | ;; the whole thing with a call to complete-do-completion. | |
410 | (= (length string) (length compl))) | |
411 | (let ((beg (field-beginning)) | |
412 | (end (field-end))) | |
413 | (goto-char end) | |
414 | (insert compl) | |
415 | (delete-region beg end))))) | |
416 | (exit-minibuffer)) | |
417 | ||
418 | ((eq minibuffer-completion-confirm 'confirm-only) | |
419 | ;; The user is permitted to exit with an input that's rejected | |
420 | ;; by test-completion, but at the condition to confirm her choice. | |
421 | (if (eq last-command this-command) | |
422 | (exit-minibuffer) | |
423 | (minibuffer-message "Confirm") | |
424 | nil)) | |
425 | ||
426 | (t | |
427 | ;; Call do-completion, but ignore errors. | |
ba5ff07b SM |
428 | (case (condition-case nil |
429 | (minibuffer--do-completion) | |
430 | (error 1)) | |
431 | ((1 3) (exit-minibuffer)) | |
432 | (7 (if (not minibuffer-completion-confirm) | |
433 | (exit-minibuffer) | |
434 | (minibuffer-message "Confirm") | |
435 | nil)) | |
436 | (t nil))))) | |
437 | ||
438 | (defun minibuffer-try-word-completion (string table predicate) | |
e2947429 | 439 | (let ((completion (minibuffer-try-completion string table predicate))) |
ba5ff07b SM |
440 | (if (not (stringp completion)) |
441 | completion | |
32bae13c | 442 | |
32bae13c SM |
443 | ;; Completing a single word is actually more difficult than completing |
444 | ;; as much as possible, because we first have to find the "current | |
445 | ;; position" in `completion' in order to find the end of the word | |
446 | ;; we're completing. Normally, `string' is a prefix of `completion', | |
447 | ;; which makes it trivial to find the position, but with fancier | |
448 | ;; completion (plus env-var expansion, ...) `completion' might not | |
449 | ;; look anything like `string' at all. | |
e1bb0fe5 | 450 | |
32bae13c SM |
451 | (when minibuffer-completing-file-name |
452 | ;; In order to minimize the problem mentioned above, let's try to | |
453 | ;; reduce the different between `string' and `completion' by | |
454 | ;; mirroring some of the work done in read-file-name-internal. | |
455 | (let ((substituted (condition-case nil | |
456 | ;; Might fail when completing an env-var. | |
457 | (substitute-in-file-name string) | |
458 | (error string)))) | |
459 | (unless (eq string substituted) | |
ba5ff07b | 460 | (setq string substituted)))) |
32bae13c SM |
461 | |
462 | ;; Make buffer (before point) contain the longest match | |
463 | ;; of `string's tail and `completion's head. | |
464 | (let* ((startpos (max 0 (- (length string) (length completion)))) | |
465 | (length (- (length string) startpos))) | |
466 | (while (and (> length 0) | |
467 | (not (eq t (compare-strings string startpos nil | |
468 | completion 0 length | |
469 | completion-ignore-case)))) | |
470 | (setq startpos (1+ startpos)) | |
471 | (setq length (1- length))) | |
472 | ||
ba5ff07b | 473 | (setq string (substring string startpos))) |
32bae13c SM |
474 | |
475 | ;; Now `string' is a prefix of `completion'. | |
476 | ||
477 | ;; If completion finds next char not unique, | |
478 | ;; consider adding a space or a hyphen. | |
479 | (when (= (length string) (length completion)) | |
480 | (let ((exts '(" " "-")) | |
481 | tem) | |
482 | (while (and exts (not (stringp tem))) | |
e2947429 SM |
483 | (setq tem (minibuffer-try-completion (concat string (pop exts)) |
484 | table predicate))) | |
32bae13c SM |
485 | (if (stringp tem) (setq completion tem)))) |
486 | ||
ba5ff07b SM |
487 | ;; Otherwise cut after the first word. |
488 | (if (string-match "\\W" completion (length string)) | |
489 | ;; First find first word-break in the stuff found by completion. | |
490 | ;; i gets index in string of where to stop completing. | |
491 | (substring completion 0 (match-end 0)) | |
492 | completion)))) | |
493 | ||
494 | ||
495 | (defun minibuffer-complete-word () | |
496 | "Complete the minibuffer contents at most a single word. | |
497 | After one word is completed as much as possible, a space or hyphen | |
498 | is added, provided that matches some possible completion. | |
499 | Return nil if there is no valid completion, else t." | |
500 | (interactive) | |
501 | (case (minibuffer--do-completion 'minibuffer-try-word-completion) | |
502 | (0 nil) | |
503 | (1 (goto-char (field-end)) | |
504 | (minibuffer-message "Sole completion") | |
505 | t) | |
506 | (3 (goto-char (field-end)) | |
507 | (minibuffer-message "Complete, but not unique") | |
508 | t) | |
509 | (t t))) | |
510 | ||
511 | (defun minibuffer--insert-strings (strings) | |
32bae13c SM |
512 | "Insert a list of STRINGS into the current buffer. |
513 | Uses columns to keep the listing readable but compact. | |
514 | It also eliminates runs of equal strings." | |
515 | (when (consp strings) | |
516 | (let* ((length (apply 'max | |
517 | (mapcar (lambda (s) | |
518 | (if (consp s) | |
519 | (+ (length (car s)) (length (cadr s))) | |
520 | (length s))) | |
521 | strings))) | |
522 | (window (get-buffer-window (current-buffer) 0)) | |
523 | (wwidth (if window (1- (window-width window)) 79)) | |
524 | (columns (min | |
525 | ;; At least 2 columns; at least 2 spaces between columns. | |
526 | (max 2 (/ wwidth (+ 2 length))) | |
527 | ;; Don't allocate more columns than we can fill. | |
528 | ;; Windows can't show less than 3 lines anyway. | |
529 | (max 1 (/ (length strings) 2)))) | |
530 | (colwidth (/ wwidth columns)) | |
531 | (column 0) | |
532 | (laststring nil)) | |
533 | ;; The insertion should be "sensible" no matter what choices were made | |
534 | ;; for the parameters above. | |
535 | (dolist (str strings) | |
536 | (unless (equal laststring str) ; Remove (consecutive) duplicates. | |
537 | (setq laststring str) | |
538 | (unless (bolp) | |
539 | (insert " \t") | |
540 | (setq column (+ column colwidth)) | |
541 | ;; Leave the space unpropertized so that in the case we're | |
542 | ;; already past the goal column, there is still | |
543 | ;; a space displayed. | |
544 | (set-text-properties (- (point) 1) (point) | |
545 | ;; We can't just set tab-width, because | |
546 | ;; completion-setup-function will kill all | |
547 | ;; local variables :-( | |
548 | `(display (space :align-to ,column)))) | |
549 | (when (< wwidth (+ (max colwidth | |
550 | (if (consp str) | |
551 | (+ (length (car str)) (length (cadr str))) | |
552 | (length str))) | |
553 | column)) | |
554 | (delete-char -2) (insert "\n") (setq column 0)) | |
555 | (if (not (consp str)) | |
556 | (put-text-property (point) (progn (insert str) (point)) | |
557 | 'mouse-face 'highlight) | |
558 | (put-text-property (point) (progn (insert (car str)) (point)) | |
559 | 'mouse-face 'highlight) | |
560 | (put-text-property (point) (progn (insert (cadr str)) (point)) | |
561 | 'mouse-face nil))))))) | |
562 | ||
563 | (defvar completion-common-substring) | |
564 | ||
21622c6d SM |
565 | (defvar completion-setup-hook nil |
566 | "Normal hook run at the end of setting up a completion list buffer. | |
567 | When this hook is run, the current buffer is the one in which the | |
568 | command to display the completion list buffer was run. | |
569 | The completion list buffer is available as the value of `standard-output'. | |
570 | The common prefix substring for completion may be available as the | |
571 | value of `completion-common-substring'. See also `display-completion-list'.") | |
572 | ||
32bae13c SM |
573 | (defun display-completion-list (completions &optional common-substring) |
574 | "Display the list of completions, COMPLETIONS, using `standard-output'. | |
575 | Each element may be just a symbol or string | |
576 | or may be a list of two strings to be printed as if concatenated. | |
577 | If it is a list of two strings, the first is the actual completion | |
578 | alternative, the second serves as annotation. | |
579 | `standard-output' must be a buffer. | |
580 | The actual completion alternatives, as inserted, are given `mouse-face' | |
581 | properties of `highlight'. | |
582 | At the end, this runs the normal hook `completion-setup-hook'. | |
583 | It can find the completion buffer in `standard-output'. | |
584 | The optional second arg COMMON-SUBSTRING is a string. | |
585 | It is used to put faces, `completions-first-difference' and | |
586 | `completions-common-part' on the completion buffer. The | |
587 | `completions-common-part' face is put on the common substring | |
588 | specified by COMMON-SUBSTRING. If COMMON-SUBSTRING is nil | |
589 | and the current buffer is not the minibuffer, the faces are not put. | |
590 | Internally, COMMON-SUBSTRING is bound to `completion-common-substring' | |
591 | during running `completion-setup-hook'." | |
592 | (if (not (bufferp standard-output)) | |
593 | ;; This *never* (ever) happens, so there's no point trying to be clever. | |
594 | (with-temp-buffer | |
595 | (let ((standard-output (current-buffer)) | |
596 | (completion-setup-hook nil)) | |
597 | (display-completion-list completions)) | |
598 | (princ (buffer-string))) | |
599 | ||
600 | (with-current-buffer standard-output | |
601 | (goto-char (point-max)) | |
602 | (if (null completions) | |
603 | (insert "There are no possible completions of what you have typed.") | |
e1bb0fe5 | 604 | |
32bae13c | 605 | (insert "Possible completions are:\n") |
e2947429 SM |
606 | (let ((last (last completions))) |
607 | ;; Get the base-size from the tail of the list. | |
608 | (set (make-local-variable 'completion-base-size) (or (cdr last) 0)) | |
609 | (setcdr last nil)) ;Make completions a properly nil-terminated list. | |
ba5ff07b | 610 | (minibuffer--insert-strings completions)))) |
e2947429 | 611 | |
32bae13c SM |
612 | (let ((completion-common-substring common-substring)) |
613 | (run-hooks 'completion-setup-hook)) | |
614 | nil) | |
615 | ||
616 | (defun minibuffer-completion-help () | |
617 | "Display a list of possible completions of the current minibuffer contents." | |
618 | (interactive) | |
619 | (message "Making completion list...") | |
620 | (let* ((string (field-string)) | |
e2947429 | 621 | (completions (minibuffer-all-completions |
32bae13c SM |
622 | string |
623 | minibuffer-completion-table | |
624 | minibuffer-completion-predicate | |
625 | t))) | |
626 | (message nil) | |
627 | (if (and completions | |
e2947429 SM |
628 | (or (consp (cdr completions)) |
629 | (not (equal (car completions) string)))) | |
32bae13c | 630 | (with-output-to-temp-buffer "*Completions*" |
e2947429 SM |
631 | (let* ((last (last completions)) |
632 | (base-size (cdr last))) | |
633 | ;; Remove the base-size tail because `sort' requires a properly | |
634 | ;; nil-terminated list. | |
635 | (when last (setcdr last nil)) | |
636 | (display-completion-list (nconc (sort completions 'string-lessp) | |
637 | base-size)))) | |
32bae13c SM |
638 | |
639 | ;; If there are no completions, or if the current input is already the | |
640 | ;; only possible completion, then hide (previous&stale) completions. | |
641 | (let ((window (and (get-buffer "*Completions*") | |
642 | (get-buffer-window "*Completions*" 0)))) | |
643 | (when (and (window-live-p window) (window-dedicated-p window)) | |
644 | (condition-case () | |
645 | (delete-window window) | |
646 | (error (iconify-frame (window-frame window)))))) | |
647 | (ding) | |
648 | (minibuffer-message | |
649 | (if completions "Sole completion" "No completions"))) | |
650 | nil)) | |
651 | ||
652 | (defun exit-minibuffer () | |
653 | "Terminate this minibuffer argument." | |
654 | (interactive) | |
655 | ;; If the command that uses this has made modifications in the minibuffer, | |
656 | ;; we don't want them to cause deactivation of the mark in the original | |
657 | ;; buffer. | |
658 | ;; A better solution would be to make deactivate-mark buffer-local | |
659 | ;; (or to turn it into a list of buffers, ...), but in the mean time, | |
660 | ;; this should do the trick in most cases. | |
ba5ff07b | 661 | (setq deactivate-mark nil) |
32bae13c SM |
662 | (throw 'exit nil)) |
663 | ||
664 | (defun self-insert-and-exit () | |
665 | "Terminate minibuffer input." | |
666 | (interactive) | |
667 | (if (characterp last-command-char) | |
668 | (call-interactively 'self-insert-command) | |
669 | (ding)) | |
670 | (exit-minibuffer)) | |
671 | ||
34b67b0f SM |
672 | (defun minibuffer--double-dollars (str) |
673 | (replace-regexp-in-string "\\$" "$$" str)) | |
674 | ||
21622c6d SM |
675 | (defun completion--make-envvar-table () |
676 | (mapcar (lambda (enventry) | |
677 | (substring enventry 0 (string-match "=" enventry))) | |
678 | process-environment)) | |
679 | ||
680 | (defun completion--embedded-envvar-table (string pred action) | |
681 | (when (string-match (concat "\\(?:^\\|[^$]\\(?:\\$\\$\\)*\\)" | |
682 | "$\\([[:alnum:]_]*\\|{\\([^}]*\\)\\)\\'") | |
683 | string) | |
684 | (let* ((beg (or (match-beginning 2) (match-beginning 1))) | |
017c22fe | 685 | (table (completion--make-envvar-table)) |
21622c6d SM |
686 | (prefix (substring string 0 beg))) |
687 | (if (eq (aref string (1- beg)) ?{) | |
688 | (setq table (apply-partially 'completion-table-with-terminator | |
689 | "}" table))) | |
690 | (completion-table-with-context prefix table | |
691 | (substring string beg) | |
692 | pred action)))) | |
017c22fe | 693 | |
21622c6d | 694 | (defun completion--file-name-table (string dir action) |
34b67b0f SM |
695 | "Internal subroutine for read-file-name. Do not call this." |
696 | (setq dir (expand-file-name dir)) | |
697 | (if (and (zerop (length string)) (eq 'lambda action)) | |
698 | nil ; FIXME: why? | |
21622c6d SM |
699 | (let* ((str (condition-case nil |
700 | (substitute-in-file-name string) | |
701 | (error string))) | |
34b67b0f SM |
702 | (name (file-name-nondirectory str)) |
703 | (specdir (file-name-directory str)) | |
704 | (realdir (if specdir (expand-file-name specdir dir) | |
705 | (file-name-as-directory dir)))) | |
017c22fe | 706 | |
34b67b0f SM |
707 | (cond |
708 | ((null action) | |
709 | (let ((comp (file-name-completion name realdir | |
710 | read-file-name-predicate))) | |
711 | (if (stringp comp) | |
712 | ;; Requote the $s before returning the completion. | |
713 | (minibuffer--double-dollars (concat specdir comp)) | |
714 | ;; Requote the $s before checking for changes. | |
715 | (setq str (minibuffer--double-dollars str)) | |
716 | (if (string-equal string str) | |
717 | comp | |
718 | ;; If there's no real completion, but substitute-in-file-name | |
719 | ;; changed the string, then return the new string. | |
720 | str)))) | |
017c22fe | 721 | |
34b67b0f | 722 | ((eq action t) |
e2947429 SM |
723 | (let ((all (file-name-all-completions name realdir)) |
724 | ;; Actually, this is not always right in the presence of | |
725 | ;; envvars, but there's not much we can do, I think. | |
726 | (base-size (length (file-name-directory string)))) | |
727 | ||
728 | ;; Check the predicate, if necessary. | |
729 | (unless (memq read-file-name-predicate '(nil file-exists-p)) | |
34b67b0f SM |
730 | (let ((comp ()) |
731 | (pred | |
732 | (if (eq read-file-name-predicate 'file-directory-p) | |
733 | ;; Brute-force speed up for directory checking: | |
734 | ;; Discard strings which don't end in a slash. | |
735 | (lambda (s) | |
736 | (let ((len (length s))) | |
737 | (and (> len 0) (eq (aref s (1- len)) ?/)))) | |
738 | ;; Must do it the hard (and slow) way. | |
739 | read-file-name-predicate))) | |
740 | (let ((default-directory realdir)) | |
741 | (dolist (tem all) | |
742 | (if (funcall pred tem) (push tem comp)))) | |
e2947429 SM |
743 | (setq all (nreverse comp)))) |
744 | ||
88893215 SM |
745 | (if (and completion-all-completions-with-base-size (consp all)) |
746 | ;; Add base-size, but only if the list is non-empty. | |
747 | (nconc all base-size)) | |
748 | ||
749 | all)) | |
34b67b0f SM |
750 | |
751 | (t | |
752 | ;; Only other case actually used is ACTION = lambda. | |
753 | (let ((default-directory dir)) | |
754 | (funcall (or read-file-name-predicate 'file-exists-p) str))))))) | |
755 | ||
21622c6d | 756 | (defalias 'read-file-name-internal |
017c22fe | 757 | (completion-table-in-turn 'completion--embedded-envvar-table |
88893215 | 758 | 'completion--file-name-table) |
21622c6d | 759 | "Internal subroutine for `read-file-name'. Do not call this.") |
34b67b0f | 760 | |
32bae13c | 761 | (provide 'minibuffer) |
dc6ee347 MB |
762 | |
763 | ;; arch-tag: ef8a0a15-1080-4790-a754-04017c02f08f | |
32bae13c | 764 | ;;; minibuffer.el ends here |