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 | ||
3911966b SM |
27 | ;;; Todo: |
28 | ||
19c04f39 | 29 | ;; - Make read-file-name-predicate obsolete. |
d28cfdc2 | 30 | ;; - New command minibuffer-force-complete that chooses one of all-completions. |
3911966b SM |
31 | ;; - Add vc-file-name-completion-table to read-file-name-internal. |
32 | ;; - A feature like completing-help.el. | |
33 | ;; - Make the `hide-spaces' arg of all-completions obsolete? | |
32bae13c SM |
34 | |
35 | ;;; Code: | |
36 | ||
37 | (eval-when-compile (require 'cl)) | |
38 | ||
e2947429 SM |
39 | (defvar completion-all-completions-with-base-size nil |
40 | "If non-nil, `all-completions' may return the base-size in the last cdr. | |
41 | The base-size is the length of the prefix that is elided from each | |
42 | element in the returned list of completions. See `completion-base-size'.") | |
43 | ||
21622c6d SM |
44 | ;;; Completion table manipulation |
45 | ||
e2947429 SM |
46 | (defun completion--some (fun xs) |
47 | "Apply FUN to each element of XS in turn. | |
48 | Return the first non-nil returned value. | |
49 | Like CL's `some'." | |
50 | (let (res) | |
51 | (while (and (not res) xs) | |
52 | (setq res (funcall fun (pop xs)))) | |
53 | res)) | |
54 | ||
21622c6d | 55 | (defun apply-partially (fun &rest args) |
e2947429 SM |
56 | "Do a \"curried\" partial application of FUN to ARGS. |
57 | ARGS is a list of the first N arguments to pass to FUN. | |
58 | The result is a new function that takes the remaining arguments, | |
59 | and calls FUN." | |
21622c6d SM |
60 | (lexical-let ((fun fun) (args1 args)) |
61 | (lambda (&rest args2) (apply fun (append args1 args2))))) | |
62 | ||
63 | (defun complete-with-action (action table string pred) | |
64 | "Perform completion ACTION. | |
65 | STRING is the string to complete. | |
66 | TABLE is the completion table, which should not be a function. | |
67 | PRED is a completion predicate. | |
68 | ACTION can be one of nil, t or `lambda'." | |
69 | ;; (assert (not (functionp table))) | |
70 | (funcall | |
71 | (cond | |
72 | ((null action) 'try-completion) | |
73 | ((eq action t) 'all-completions) | |
74 | (t 'test-completion)) | |
75 | string table pred)) | |
76 | ||
77 | (defun completion-table-dynamic (fun) | |
78 | "Use function FUN as a dynamic completion table. | |
79 | FUN is called with one argument, the string for which completion is required, | |
b95c7600 JB |
80 | and it should return an alist containing all the intended possible completions. |
81 | This alist may be a full list of possible completions so that FUN can ignore | |
82 | the value of its argument. If completion is performed in the minibuffer, | |
83 | FUN will be called in the buffer from which the minibuffer was entered. | |
21622c6d SM |
84 | |
85 | The result of the `dynamic-completion-table' form is a function | |
86 | that can be used as the ALIST argument to `try-completion' and | |
b95c7600 | 87 | `all-completions'. See Info node `(elisp)Programmed Completion'." |
21622c6d SM |
88 | (lexical-let ((fun fun)) |
89 | (lambda (string pred action) | |
90 | (with-current-buffer (let ((win (minibuffer-selected-window))) | |
91 | (if (window-live-p win) (window-buffer win) | |
92 | (current-buffer))) | |
93 | (complete-with-action action (funcall fun string) string pred))))) | |
94 | ||
95 | (defmacro lazy-completion-table (var fun) | |
96 | "Initialize variable VAR as a lazy completion table. | |
97 | If the completion table VAR is used for the first time (e.g., by passing VAR | |
98 | as an argument to `try-completion'), the function FUN is called with no | |
99 | arguments. FUN must return the completion table that will be stored in VAR. | |
100 | If completion is requested in the minibuffer, FUN will be called in the buffer | |
101 | from which the minibuffer was entered. The return value of | |
102 | `lazy-completion-table' must be used to initialize the value of VAR. | |
103 | ||
104 | You should give VAR a non-nil `risky-local-variable' property." | |
69e018a7 | 105 | (declare (debug (symbolp lambda-expr))) |
21622c6d SM |
106 | (let ((str (make-symbol "string"))) |
107 | `(completion-table-dynamic | |
108 | (lambda (,str) | |
109 | (when (functionp ,var) | |
110 | (setq ,var (,fun))) | |
111 | ,var)))) | |
112 | ||
113 | (defun completion-table-with-context (prefix table string pred action) | |
25c0d999 | 114 | ;; TODO: add `suffix' maybe? |
e2947429 SM |
115 | ;; Notice that `pred' is not a predicate when called from read-file-name |
116 | ;; or Info-read-node-name-2. | |
34200787 SM |
117 | (when (functionp pred) |
118 | (setq pred | |
119 | (lexical-let ((pred pred)) | |
120 | ;; Predicates are called differently depending on the nature of | |
121 | ;; the completion table :-( | |
122 | (cond | |
123 | ((vectorp table) ;Obarray. | |
124 | (lambda (sym) (funcall pred (concat prefix (symbol-name sym))))) | |
125 | ((hash-table-p table) | |
126 | (lambda (s v) (funcall pred (concat prefix s)))) | |
127 | ((functionp table) | |
128 | (lambda (s) (funcall pred (concat prefix s)))) | |
129 | (t ;Lists and alists. | |
130 | (lambda (s) | |
131 | (funcall pred (concat prefix (if (consp s) (car s) s))))))))) | |
e2947429 SM |
132 | (let ((comp (complete-with-action action table string pred))) |
133 | (cond | |
134 | ;; In case of try-completion, add the prefix. | |
135 | ((stringp comp) (concat prefix comp)) | |
136 | ;; In case of non-empty all-completions, | |
137 | ;; add the prefix size to the base-size. | |
138 | ((consp comp) | |
139 | (let ((last (last comp))) | |
140 | (when completion-all-completions-with-base-size | |
141 | (setcdr last (+ (or (cdr last) 0) (length prefix)))) | |
142 | comp)) | |
143 | (t comp)))) | |
21622c6d SM |
144 | |
145 | (defun completion-table-with-terminator (terminator table string pred action) | |
25c0d999 SM |
146 | (cond |
147 | ((eq action nil) | |
148 | (let ((comp (try-completion string table pred))) | |
88893215 SM |
149 | (if (eq comp t) |
150 | (concat string terminator) | |
151 | (if (and (stringp comp) | |
25c0d999 | 152 | (eq (try-completion comp table pred) t)) |
88893215 | 153 | (concat comp terminator) |
25c0d999 SM |
154 | comp)))) |
155 | ((eq action t) (all-completions string table pred)) | |
156 | ;; completion-table-with-terminator is always used for | |
157 | ;; "sub-completions" so it's only called if the terminator is missing, | |
158 | ;; in which case `test-completion' should return nil. | |
159 | ((eq action 'lambda) nil))) | |
160 | ||
161 | (defun completion-table-with-predicate (table pred1 strict string pred2 action) | |
162 | "Make a completion table equivalent to TABLE but filtered through PRED1. | |
163 | PRED1 is a function of one argument which returns non-nil iff the | |
164 | argument is an element of TABLE which should be considered for completion. | |
165 | STRING, PRED2, and ACTION are the usual arguments to completion tables, | |
166 | as described in `try-completion', `all-completions', and `test-completion'. | |
3911966b SM |
167 | If STRICT is t, the predicate always applies; if nil it only applies if |
168 | it does not reduce the set of possible completions to nothing. | |
25c0d999 SM |
169 | Note: TABLE needs to be a proper completion table which obeys predicates." |
170 | (cond | |
171 | ((and (not strict) (eq action 'lambda)) | |
172 | ;; Ignore pred1 since it doesn't really have to apply anyway. | |
af48580e | 173 | (test-completion string table pred2)) |
25c0d999 SM |
174 | (t |
175 | (or (complete-with-action action table string | |
176 | (if (null pred2) pred1 | |
177 | (lexical-let ((pred1 pred2) (pred2 pred2)) | |
178 | (lambda (x) | |
179 | ;; Call `pred1' first, so that `pred2' | |
180 | ;; really can't tell that `x' is in table. | |
181 | (if (funcall pred1 x) (funcall pred2 x)))))) | |
182 | ;; If completion failed and we're not applying pred1 strictly, try | |
183 | ;; again without pred1. | |
184 | (and (not strict) | |
185 | (complete-with-action action table string pred2)))))) | |
21622c6d | 186 | |
e2947429 SM |
187 | (defun completion-table-in-turn (&rest tables) |
188 | "Create a completion table that tries each table in TABLES in turn." | |
189 | (lexical-let ((tables tables)) | |
21622c6d | 190 | (lambda (string pred action) |
e2947429 SM |
191 | (completion--some (lambda (table) |
192 | (complete-with-action action table string pred)) | |
193 | tables)))) | |
194 | ||
25c0d999 SM |
195 | ;; (defmacro complete-in-turn (a b) `(completion-table-in-turn ,a ,b)) |
196 | ;; (defmacro dynamic-completion-table (fun) `(completion-table-dynamic ,fun)) | |
e2947429 SM |
197 | (define-obsolete-function-alias |
198 | 'complete-in-turn 'completion-table-in-turn "23.1") | |
25c0d999 SM |
199 | (define-obsolete-function-alias |
200 | 'dynamic-completion-table 'completion-table-dynamic "23.1") | |
21622c6d SM |
201 | |
202 | ;;; Minibuffer completion | |
203 | ||
ba5ff07b SM |
204 | (defgroup minibuffer nil |
205 | "Controlling the behavior of the minibuffer." | |
206 | :link '(custom-manual "(emacs)Minibuffer") | |
207 | :group 'environment) | |
208 | ||
32bae13c SM |
209 | (defun minibuffer-message (message &rest args) |
210 | "Temporarily display MESSAGE at the end of the minibuffer. | |
211 | The text is displayed for `minibuffer-message-timeout' seconds, | |
212 | or until the next input event arrives, whichever comes first. | |
213 | Enclose MESSAGE in [...] if this is not yet the case. | |
214 | If ARGS are provided, then pass MESSAGE through `format'." | |
215 | ;; Clear out any old echo-area message to make way for our new thing. | |
216 | (message nil) | |
bd5c2732 SM |
217 | (setq message (if (and (null args) (string-match "\\[.+\\]" message)) |
218 | ;; Make sure we can put-text-property. | |
219 | (copy-sequence message) | |
220 | (concat " [" message "]"))) | |
32bae13c SM |
221 | (when args (setq message (apply 'format message args))) |
222 | (let ((ol (make-overlay (point-max) (point-max) nil t t))) | |
223 | (unwind-protect | |
224 | (progn | |
bf87d5fc SM |
225 | (unless (zerop (length message)) |
226 | ;; The current C cursor code doesn't know to use the overlay's | |
227 | ;; marker's stickiness to figure out whether to place the cursor | |
228 | ;; before or after the string, so let's spoon-feed it the pos. | |
229 | (put-text-property 0 1 'cursor t message)) | |
32bae13c SM |
230 | (overlay-put ol 'after-string message) |
231 | (sit-for (or minibuffer-message-timeout 1000000))) | |
232 | (delete-overlay ol)))) | |
233 | ||
234 | (defun minibuffer-completion-contents () | |
235 | "Return the user input in a minibuffer before point as a string. | |
236 | That is what completion commands operate on." | |
237 | (buffer-substring (field-beginning) (point))) | |
238 | ||
239 | (defun delete-minibuffer-contents () | |
240 | "Delete all user input in a minibuffer. | |
241 | If the current buffer is not a minibuffer, erase its entire contents." | |
242 | (delete-field)) | |
243 | ||
ba5ff07b SM |
244 | (defcustom completion-auto-help t |
245 | "Non-nil means automatically provide help for invalid completion input. | |
246 | If the value is t the *Completion* buffer is displayed whenever completion | |
247 | is requested but cannot be done. | |
248 | If the value is `lazy', the *Completions* buffer is only displayed after | |
249 | the second failed attempt to complete." | |
e1bb0fe5 | 250 | :type '(choice (const nil) (const t) (const lazy)) |
ba5ff07b SM |
251 | :group 'minibuffer) |
252 | ||
e2947429 | 253 | (defvar completion-styles-alist |
19c04f39 SM |
254 | '((basic completion-basic-try-completion completion-basic-all-completions) |
255 | (emacs22 completion-emacs22-try-completion completion-emacs22-all-completions) | |
256 | (emacs21 completion-emacs21-try-completion completion-emacs21-all-completions) | |
34200787 SM |
257 | (partial-completion |
258 | completion-pcm-try-completion completion-pcm-all-completions)) | |
e2947429 SM |
259 | "List of available completion styles. |
260 | Each element has the form (NAME TRY-COMPLETION ALL-COMPLETIONS) | |
26c548b0 | 261 | where NAME is the name that should be used in `completion-styles', |
e2947429 SM |
262 | TRY-COMPLETION is the function that does the completion, and |
263 | ALL-COMPLETIONS is the function that lists the completions.") | |
264 | ||
34200787 | 265 | (defcustom completion-styles '(basic partial-completion) |
e2947429 SM |
266 | "List of completion styles to use." |
267 | :type `(repeat (choice ,@(mapcar (lambda (x) (list 'const (car x))) | |
268 | completion-styles-alist))) | |
269 | :group 'minibuffer | |
270 | :version "23.1") | |
271 | ||
19c04f39 SM |
272 | (defun completion-try-completion (string table pred point) |
273 | "Try to complete STRING using completion table TABLE. | |
274 | Only the elements of table that satisfy predicate PRED are considered. | |
275 | POINT is the position of point within STRING. | |
276 | The return value can be either nil to indicate that there is no completion, | |
277 | t to indicate that STRING is the only possible completion, | |
278 | or a pair (STRING . NEWPOINT) of the completed result string together with | |
279 | a new position for point." | |
3911966b SM |
280 | ;; The property `completion-styles' indicates that this functional |
281 | ;; completion-table claims to take care of completion styles itself. | |
282 | ;; [I.e. It will most likely call us back at some point. ] | |
283 | (if (and (symbolp table) (get table 'completion-styles)) | |
19c04f39 SM |
284 | ;; Extended semantics for functional completion-tables: |
285 | ;; They accept a 4th argument `point' and when called with action=nil | |
286 | ;; and this 4th argument (a position inside `string'), they should | |
287 | ;; return instead of a string a pair (STRING . NEWPOINT). | |
288 | (funcall table string pred nil point) | |
e2947429 | 289 | (completion--some (lambda (style) |
2ed430f4 | 290 | (funcall (nth 1 (assq style completion-styles-alist)) |
19c04f39 | 291 | string table pred point)) |
e2947429 SM |
292 | completion-styles))) |
293 | ||
19c04f39 SM |
294 | (defun completion-all-completions (string table pred point) |
295 | "List the possible completions of STRING in completion table TABLE. | |
296 | Only the elements of table that satisfy predicate PRED are considered. | |
297 | POINT is the position of point within STRING. | |
26c548b0 | 298 | The return value is a list of completions and may contain the base-size |
19c04f39 | 299 | in the last `cdr'." |
3911966b SM |
300 | ;; The property `completion-styles' indicates that this functional |
301 | ;; completion-table claims to take care of completion styles itself. | |
302 | ;; [I.e. It will most likely call us back at some point. ] | |
e2947429 | 303 | (let ((completion-all-completions-with-base-size t)) |
19c04f39 SM |
304 | (if (and (symbolp table) (get table 'completion-styles)) |
305 | ;; Extended semantics for functional completion-tables: | |
306 | ;; They accept a 4th argument `point' and when called with action=t | |
307 | ;; and this 4th argument (a position inside `string'), they may | |
308 | ;; return BASE-SIZE in the last `cdr'. | |
309 | (funcall table string pred t point) | |
e2947429 | 310 | (completion--some (lambda (style) |
2ed430f4 | 311 | (funcall (nth 2 (assq style completion-styles-alist)) |
19c04f39 | 312 | string table pred point)) |
e2947429 SM |
313 | completion-styles)))) |
314 | ||
ba5ff07b SM |
315 | (defun minibuffer--bitset (modified completions exact) |
316 | (logior (if modified 4 0) | |
317 | (if completions 2 0) | |
318 | (if exact 1 0))) | |
319 | ||
3911966b | 320 | (defun completion--do-completion (&optional try-completion-function) |
32bae13c | 321 | "Do the completion and return a summary of what happened. |
ba5ff07b SM |
322 | M = completion was performed, the text was Modified. |
323 | C = there were available Completions. | |
324 | E = after completion we now have an Exact match. | |
325 | ||
326 | MCE | |
327 | 000 0 no possible completion | |
328 | 001 1 was already an exact and unique completion | |
329 | 010 2 no completion happened | |
330 | 011 3 was already an exact completion | |
331 | 100 4 ??? impossible | |
332 | 101 5 ??? impossible | |
333 | 110 6 some completion happened | |
334 | 111 7 completed to an exact completion" | |
335 | (let* ((beg (field-beginning)) | |
19c04f39 | 336 | (end (field-end)) |
3911966b | 337 | (string (buffer-substring beg end)) |
19c04f39 SM |
338 | (comp (funcall (or try-completion-function |
339 | 'completion-try-completion) | |
340 | string | |
341 | minibuffer-completion-table | |
342 | minibuffer-completion-predicate | |
343 | (- (point) beg)))) | |
32bae13c | 344 | (cond |
19c04f39 | 345 | ((null comp) |
ba5ff07b | 346 | (ding) (minibuffer-message "No match") (minibuffer--bitset nil nil nil)) |
19c04f39 | 347 | ((eq t comp) (minibuffer--bitset nil nil t)) ;Exact and unique match. |
32bae13c SM |
348 | (t |
349 | ;; `completed' should be t if some completion was done, which doesn't | |
350 | ;; include simply changing the case of the entered string. However, | |
351 | ;; for appearance, the string is rewritten if the case changes. | |
19c04f39 SM |
352 | (let* ((comp-pos (cdr comp)) |
353 | (completion (car comp)) | |
354 | (completed (not (eq t (compare-strings completion nil nil | |
355 | string nil nil t)))) | |
3911966b SM |
356 | (unchanged (eq t (compare-strings completion nil nil |
357 | string nil nil nil)))) | |
32bae13c | 358 | (unless unchanged |
ba5ff07b SM |
359 | |
360 | ;; Insert in minibuffer the chars we got. | |
3911966b SM |
361 | (goto-char end) |
362 | (insert completion) | |
19c04f39 SM |
363 | (delete-region beg end) |
364 | (goto-char (+ beg comp-pos))) | |
ba5ff07b | 365 | |
32bae13c SM |
366 | (if (not (or unchanged completed)) |
367 | ;; The case of the string changed, but that's all. We're not sure | |
368 | ;; whether this is a unique completion or not, so try again using | |
369 | ;; the real case (this shouldn't recurse again, because the next | |
370 | ;; time try-completion will return either t or the exact string). | |
3911966b | 371 | (completion--do-completion try-completion-function) |
32bae13c SM |
372 | |
373 | ;; It did find a match. Do we match some possibility exactly now? | |
19c04f39 | 374 | (let ((exact (test-completion completion |
32bae13c SM |
375 | minibuffer-completion-table |
376 | minibuffer-completion-predicate))) | |
ba5ff07b SM |
377 | (unless completed |
378 | ;; Show the completion table, if requested. | |
379 | (cond | |
380 | ((not exact) | |
381 | (if (case completion-auto-help | |
382 | (lazy (eq this-command last-command)) | |
383 | (t completion-auto-help)) | |
384 | (minibuffer-completion-help) | |
385 | (minibuffer-message "Next char not unique"))) | |
386 | ;; If the last exact completion and this one were the same, | |
387 | ;; it means we've already given a "Complete but not unique" | |
388 | ;; message and the user's hit TAB again, so now we give him help. | |
389 | ((eq this-command last-command) | |
390 | (if completion-auto-help (minibuffer-completion-help))))) | |
391 | ||
392 | (minibuffer--bitset completed t exact)))))))) | |
32bae13c SM |
393 | |
394 | (defun minibuffer-complete () | |
395 | "Complete the minibuffer contents as far as possible. | |
396 | Return nil if there is no valid completion, else t. | |
397 | If no characters can be completed, display a list of possible completions. | |
398 | If you repeat this command after it displayed such a list, | |
399 | scroll the window of possible completions." | |
400 | (interactive) | |
401 | ;; If the previous command was not this, | |
402 | ;; mark the completion buffer obsolete. | |
403 | (unless (eq this-command last-command) | |
404 | (setq minibuffer-scroll-window nil)) | |
405 | ||
406 | (let ((window minibuffer-scroll-window)) | |
407 | ;; If there's a fresh completion window with a live buffer, | |
408 | ;; and this command is repeated, scroll that window. | |
409 | (if (window-live-p window) | |
410 | (with-current-buffer (window-buffer window) | |
411 | (if (pos-visible-in-window-p (point-max) window) | |
412 | ;; If end is in view, scroll up to the beginning. | |
413 | (set-window-start window (point-min) nil) | |
414 | ;; Else scroll down one screen. | |
415 | (scroll-other-window)) | |
416 | nil) | |
417 | ||
3911966b | 418 | (case (completion--do-completion) |
ba5ff07b SM |
419 | (0 nil) |
420 | (1 (goto-char (field-end)) | |
421 | (minibuffer-message "Sole completion") | |
422 | t) | |
423 | (3 (goto-char (field-end)) | |
424 | (minibuffer-message "Complete, but not unique") | |
425 | t) | |
426 | (t t))))) | |
32bae13c SM |
427 | |
428 | (defun minibuffer-complete-and-exit () | |
429 | "If the minibuffer contents is a valid completion then exit. | |
430 | Otherwise try to complete it. If completion leads to a valid completion, | |
431 | a repetition of this command will exit." | |
432 | (interactive) | |
3911966b SM |
433 | (let ((beg (field-beginning)) |
434 | (end (field-end))) | |
435 | (cond | |
436 | ;; Allow user to specify null string | |
437 | ((= beg end) (exit-minibuffer)) | |
438 | ((test-completion (buffer-substring beg end) | |
439 | minibuffer-completion-table | |
440 | minibuffer-completion-predicate) | |
441 | (when completion-ignore-case | |
442 | ;; Fixup case of the field, if necessary. | |
b0a5a021 | 443 | (let* ((string (buffer-substring beg end)) |
3911966b SM |
444 | (compl (try-completion |
445 | string | |
446 | minibuffer-completion-table | |
447 | minibuffer-completion-predicate))) | |
448 | (when (and (stringp compl) | |
449 | ;; If it weren't for this piece of paranoia, I'd replace | |
450 | ;; the whole thing with a call to do-completion. | |
451 | (= (length string) (length compl))) | |
32bae13c SM |
452 | (goto-char end) |
453 | (insert compl) | |
3911966b SM |
454 | (delete-region beg end)))) |
455 | (exit-minibuffer)) | |
32bae13c | 456 | |
3911966b SM |
457 | ((eq minibuffer-completion-confirm 'confirm-only) |
458 | ;; The user is permitted to exit with an input that's rejected | |
459 | ;; by test-completion, but at the condition to confirm her choice. | |
460 | (if (eq last-command this-command) | |
461 | (exit-minibuffer) | |
462 | (minibuffer-message "Confirm") | |
463 | nil)) | |
32bae13c | 464 | |
3911966b SM |
465 | (t |
466 | ;; Call do-completion, but ignore errors. | |
467 | (case (condition-case nil | |
468 | (completion--do-completion) | |
469 | (error 1)) | |
470 | ((1 3) (exit-minibuffer)) | |
471 | (7 (if (not minibuffer-completion-confirm) | |
472 | (exit-minibuffer) | |
473 | (minibuffer-message "Confirm") | |
474 | nil)) | |
475 | (t nil)))))) | |
476 | ||
19c04f39 SM |
477 | (defun completion--try-word-completion (string table predicate point) |
478 | (let ((comp (completion-try-completion string table predicate point))) | |
479 | (if (not (consp comp)) | |
480 | comp | |
32bae13c | 481 | |
3911966b SM |
482 | ;; If completion finds next char not unique, |
483 | ;; consider adding a space or a hyphen. | |
19c04f39 | 484 | (when (= (length string) (length (car comp))) |
3911966b | 485 | (let ((exts '(" " "-")) |
19c04f39 SM |
486 | (before (substring string 0 point)) |
487 | (after (substring string point)) | |
488 | tem) | |
489 | (while (and exts (not (consp tem))) | |
3911966b | 490 | (setq tem (completion-try-completion |
19c04f39 SM |
491 | (concat before (pop exts) after) |
492 | table predicate (1+ point)))) | |
493 | (if (consp tem) (setq comp tem)))) | |
3911966b | 494 | |
32bae13c SM |
495 | ;; Completing a single word is actually more difficult than completing |
496 | ;; as much as possible, because we first have to find the "current | |
497 | ;; position" in `completion' in order to find the end of the word | |
498 | ;; we're completing. Normally, `string' is a prefix of `completion', | |
499 | ;; which makes it trivial to find the position, but with fancier | |
500 | ;; completion (plus env-var expansion, ...) `completion' might not | |
501 | ;; look anything like `string' at all. | |
19c04f39 SM |
502 | (let* ((comppoint (cdr comp)) |
503 | (completion (car comp)) | |
504 | (before (substring string 0 point)) | |
505 | (combined (concat before "\n" completion))) | |
506 | ;; Find in completion the longest text that was right before point. | |
507 | (when (string-match "\\(.+\\)\n.*?\\1" combined) | |
508 | (let* ((prefix (match-string 1 before)) | |
509 | ;; We used non-greedy match to make `rem' as long as possible. | |
510 | (rem (substring combined (match-end 0))) | |
511 | ;; Find in the remainder of completion the longest text | |
512 | ;; that was right after point. | |
513 | (after (substring string point)) | |
514 | (suffix (if (string-match "\\`\\(.+\\).*\n.*\\1" | |
515 | (concat after "\n" rem)) | |
516 | (match-string 1 after)))) | |
517 | ;; The general idea is to try and guess what text was inserted | |
518 | ;; at point by the completion. Problem is: if we guess wrong, | |
519 | ;; we may end up treating as "added by completion" text that was | |
520 | ;; actually painfully typed by the user. So if we then cut | |
521 | ;; after the first word, we may throw away things the | |
522 | ;; user wrote. So let's try to be as conservative as possible: | |
523 | ;; only cut after the first word, if we're reasonably sure that | |
524 | ;; our guess is correct. | |
525 | ;; Note: a quick survey on emacs-devel seemed to indicate that | |
526 | ;; nobody actually cares about the "word-at-a-time" feature of | |
527 | ;; minibuffer-complete-word, whose real raison-d'être is that it | |
528 | ;; tries to add "-" or " ". One more reason to only cut after | |
529 | ;; the first word, if we're really sure we're right. | |
530 | (when (and (or suffix (zerop (length after))) | |
531 | (string-match (concat | |
532 | ;; Make submatch 1 as small as possible | |
533 | ;; to reduce the risk of cutting | |
534 | ;; valuable text. | |
535 | ".*" (regexp-quote prefix) "\\(.*?\\)" | |
536 | (if suffix (regexp-quote suffix) "\\'")) | |
537 | completion) | |
538 | ;; The new point in `completion' should also be just | |
539 | ;; before the suffix, otherwise something more complex | |
540 | ;; is going on, and we're not sure where we are. | |
541 | (eq (match-end 1) comppoint) | |
542 | ;; (match-beginning 1)..comppoint is now the stretch | |
543 | ;; of text in `completion' that was completed at point. | |
544 | (string-match "\\W" completion (match-beginning 1)) | |
545 | ;; Is there really something to cut? | |
546 | (> comppoint (match-end 0))) | |
547 | ;; Cut after the first word. | |
548 | (let ((cutpos (match-end 0))) | |
549 | (setq completion (concat (substring completion 0 cutpos) | |
550 | (substring completion comppoint))) | |
551 | (setq comppoint cutpos))))) | |
552 | ||
553 | (cons completion comppoint))))) | |
ba5ff07b SM |
554 | |
555 | ||
556 | (defun minibuffer-complete-word () | |
557 | "Complete the minibuffer contents at most a single word. | |
558 | After one word is completed as much as possible, a space or hyphen | |
559 | is added, provided that matches some possible completion. | |
560 | Return nil if there is no valid completion, else t." | |
561 | (interactive) | |
3911966b | 562 | (case (completion--do-completion 'completion--try-word-completion) |
ba5ff07b SM |
563 | (0 nil) |
564 | (1 (goto-char (field-end)) | |
565 | (minibuffer-message "Sole completion") | |
566 | t) | |
567 | (3 (goto-char (field-end)) | |
568 | (minibuffer-message "Complete, but not unique") | |
569 | t) | |
570 | (t t))) | |
571 | ||
3911966b | 572 | (defun completion--insert-strings (strings) |
32bae13c SM |
573 | "Insert a list of STRINGS into the current buffer. |
574 | Uses columns to keep the listing readable but compact. | |
575 | It also eliminates runs of equal strings." | |
576 | (when (consp strings) | |
577 | (let* ((length (apply 'max | |
578 | (mapcar (lambda (s) | |
579 | (if (consp s) | |
580 | (+ (length (car s)) (length (cadr s))) | |
581 | (length s))) | |
582 | strings))) | |
583 | (window (get-buffer-window (current-buffer) 0)) | |
584 | (wwidth (if window (1- (window-width window)) 79)) | |
585 | (columns (min | |
586 | ;; At least 2 columns; at least 2 spaces between columns. | |
587 | (max 2 (/ wwidth (+ 2 length))) | |
588 | ;; Don't allocate more columns than we can fill. | |
589 | ;; Windows can't show less than 3 lines anyway. | |
590 | (max 1 (/ (length strings) 2)))) | |
591 | (colwidth (/ wwidth columns)) | |
592 | (column 0) | |
593 | (laststring nil)) | |
594 | ;; The insertion should be "sensible" no matter what choices were made | |
595 | ;; for the parameters above. | |
596 | (dolist (str strings) | |
597 | (unless (equal laststring str) ; Remove (consecutive) duplicates. | |
598 | (setq laststring str) | |
599 | (unless (bolp) | |
600 | (insert " \t") | |
601 | (setq column (+ column colwidth)) | |
602 | ;; Leave the space unpropertized so that in the case we're | |
603 | ;; already past the goal column, there is still | |
604 | ;; a space displayed. | |
605 | (set-text-properties (- (point) 1) (point) | |
606 | ;; We can't just set tab-width, because | |
607 | ;; completion-setup-function will kill all | |
608 | ;; local variables :-( | |
609 | `(display (space :align-to ,column)))) | |
610 | (when (< wwidth (+ (max colwidth | |
611 | (if (consp str) | |
612 | (+ (length (car str)) (length (cadr str))) | |
613 | (length str))) | |
614 | column)) | |
615 | (delete-char -2) (insert "\n") (setq column 0)) | |
616 | (if (not (consp str)) | |
617 | (put-text-property (point) (progn (insert str) (point)) | |
618 | 'mouse-face 'highlight) | |
619 | (put-text-property (point) (progn (insert (car str)) (point)) | |
620 | 'mouse-face 'highlight) | |
621 | (put-text-property (point) (progn (insert (cadr str)) (point)) | |
622 | 'mouse-face nil))))))) | |
623 | ||
6138158d SM |
624 | (defvar completion-common-substring nil) |
625 | (make-obsolete-variable 'completion-common-substring nil "23.1") | |
32bae13c | 626 | |
21622c6d SM |
627 | (defvar completion-setup-hook nil |
628 | "Normal hook run at the end of setting up a completion list buffer. | |
629 | When this hook is run, the current buffer is the one in which the | |
630 | command to display the completion list buffer was run. | |
631 | The completion list buffer is available as the value of `standard-output'. | |
6138158d SM |
632 | See also `display-completion-list'.") |
633 | ||
634 | (defface completions-first-difference | |
635 | '((t (:inherit bold))) | |
636 | "Face put on the first uncommon character in completions in *Completions* buffer." | |
637 | :group 'completion) | |
638 | ||
639 | (defface completions-common-part | |
640 | '((t (:inherit default))) | |
641 | "Face put on the common prefix substring in completions in *Completions* buffer. | |
642 | The idea of `completions-common-part' is that you can use it to | |
643 | make the common parts less visible than normal, so that the rest | |
644 | of the differing parts is, by contrast, slightly highlighted." | |
645 | :group 'completion) | |
646 | ||
647 | (defun completion-hilit-commonality (completions prefix-len) | |
648 | (when completions | |
649 | (let* ((last (last completions)) | |
650 | (base-size (cdr last)) | |
651 | (com-str-len (- prefix-len (or base-size 0)))) | |
652 | ;; Remove base-size during mapcar, and add it back later. | |
653 | (setcdr last nil) | |
654 | (nconc | |
655 | (mapcar | |
656 | (lambda (elem) | |
657 | (let ((str | |
658 | (if (consp elem) | |
659 | (car (setq elem (cons (copy-sequence (car elem)) | |
660 | (cdr elem)))) | |
661 | (setq elem (copy-sequence elem))))) | |
662 | (put-text-property 0 com-str-len | |
663 | 'font-lock-face 'completions-common-part | |
664 | str) | |
665 | (if (> (length str) com-str-len) | |
666 | (put-text-property com-str-len (1+ com-str-len) | |
667 | 'font-lock-face 'completions-first-difference | |
668 | str))) | |
669 | elem) | |
670 | completions) | |
671 | base-size)))) | |
21622c6d | 672 | |
32bae13c SM |
673 | (defun display-completion-list (completions &optional common-substring) |
674 | "Display the list of completions, COMPLETIONS, using `standard-output'. | |
675 | Each element may be just a symbol or string | |
676 | or may be a list of two strings to be printed as if concatenated. | |
677 | If it is a list of two strings, the first is the actual completion | |
678 | alternative, the second serves as annotation. | |
679 | `standard-output' must be a buffer. | |
680 | The actual completion alternatives, as inserted, are given `mouse-face' | |
681 | properties of `highlight'. | |
682 | At the end, this runs the normal hook `completion-setup-hook'. | |
683 | It can find the completion buffer in `standard-output'. | |
6138158d | 684 | The obsolete optional second arg COMMON-SUBSTRING is a string. |
32bae13c | 685 | It is used to put faces, `completions-first-difference' and |
b95c7600 | 686 | `completions-common-part' on the completion buffer. The |
32bae13c | 687 | `completions-common-part' face is put on the common substring |
6138158d SM |
688 | specified by COMMON-SUBSTRING." |
689 | (if common-substring | |
690 | (setq completions (completion-hilit-commonality | |
691 | completions (length common-substring)))) | |
32bae13c SM |
692 | (if (not (bufferp standard-output)) |
693 | ;; This *never* (ever) happens, so there's no point trying to be clever. | |
694 | (with-temp-buffer | |
695 | (let ((standard-output (current-buffer)) | |
696 | (completion-setup-hook nil)) | |
697 | (display-completion-list completions)) | |
698 | (princ (buffer-string))) | |
699 | ||
700 | (with-current-buffer standard-output | |
701 | (goto-char (point-max)) | |
702 | (if (null completions) | |
703 | (insert "There are no possible completions of what you have typed.") | |
e1bb0fe5 | 704 | |
32bae13c | 705 | (insert "Possible completions are:\n") |
e2947429 SM |
706 | (let ((last (last completions))) |
707 | ;; Get the base-size from the tail of the list. | |
708 | (set (make-local-variable 'completion-base-size) (or (cdr last) 0)) | |
709 | (setcdr last nil)) ;Make completions a properly nil-terminated list. | |
3911966b | 710 | (completion--insert-strings completions)))) |
e2947429 | 711 | |
6138158d SM |
712 | ;; The hilit used to be applied via completion-setup-hook, so there |
713 | ;; may still be some code that uses completion-common-substring. | |
32bae13c SM |
714 | (let ((completion-common-substring common-substring)) |
715 | (run-hooks 'completion-setup-hook)) | |
716 | nil) | |
717 | ||
718 | (defun minibuffer-completion-help () | |
719 | "Display a list of possible completions of the current minibuffer contents." | |
720 | (interactive) | |
721 | (message "Making completion list...") | |
722 | (let* ((string (field-string)) | |
3911966b | 723 | (completions (completion-all-completions |
32bae13c SM |
724 | string |
725 | minibuffer-completion-table | |
19c04f39 SM |
726 | minibuffer-completion-predicate |
727 | (- (point) (field-beginning))))) | |
32bae13c SM |
728 | (message nil) |
729 | (if (and completions | |
e2947429 SM |
730 | (or (consp (cdr completions)) |
731 | (not (equal (car completions) string)))) | |
32bae13c | 732 | (with-output-to-temp-buffer "*Completions*" |
e2947429 SM |
733 | (let* ((last (last completions)) |
734 | (base-size (cdr last))) | |
735 | ;; Remove the base-size tail because `sort' requires a properly | |
736 | ;; nil-terminated list. | |
737 | (when last (setcdr last nil)) | |
738 | (display-completion-list (nconc (sort completions 'string-lessp) | |
739 | base-size)))) | |
32bae13c SM |
740 | |
741 | ;; If there are no completions, or if the current input is already the | |
742 | ;; only possible completion, then hide (previous&stale) completions. | |
743 | (let ((window (and (get-buffer "*Completions*") | |
744 | (get-buffer-window "*Completions*" 0)))) | |
745 | (when (and (window-live-p window) (window-dedicated-p window)) | |
746 | (condition-case () | |
747 | (delete-window window) | |
748 | (error (iconify-frame (window-frame window)))))) | |
749 | (ding) | |
750 | (minibuffer-message | |
751 | (if completions "Sole completion" "No completions"))) | |
752 | nil)) | |
753 | ||
754 | (defun exit-minibuffer () | |
755 | "Terminate this minibuffer argument." | |
756 | (interactive) | |
757 | ;; If the command that uses this has made modifications in the minibuffer, | |
758 | ;; we don't want them to cause deactivation of the mark in the original | |
759 | ;; buffer. | |
760 | ;; A better solution would be to make deactivate-mark buffer-local | |
761 | ;; (or to turn it into a list of buffers, ...), but in the mean time, | |
762 | ;; this should do the trick in most cases. | |
ba5ff07b | 763 | (setq deactivate-mark nil) |
32bae13c SM |
764 | (throw 'exit nil)) |
765 | ||
766 | (defun self-insert-and-exit () | |
767 | "Terminate minibuffer input." | |
768 | (interactive) | |
769 | (if (characterp last-command-char) | |
770 | (call-interactively 'self-insert-command) | |
771 | (ding)) | |
772 | (exit-minibuffer)) | |
773 | ||
34b67b0f SM |
774 | (defun minibuffer--double-dollars (str) |
775 | (replace-regexp-in-string "\\$" "$$" str)) | |
776 | ||
21622c6d SM |
777 | (defun completion--make-envvar-table () |
778 | (mapcar (lambda (enventry) | |
779 | (substring enventry 0 (string-match "=" enventry))) | |
780 | process-environment)) | |
781 | ||
782 | (defun completion--embedded-envvar-table (string pred action) | |
783 | (when (string-match (concat "\\(?:^\\|[^$]\\(?:\\$\\$\\)*\\)" | |
784 | "$\\([[:alnum:]_]*\\|{\\([^}]*\\)\\)\\'") | |
785 | string) | |
786 | (let* ((beg (or (match-beginning 2) (match-beginning 1))) | |
017c22fe | 787 | (table (completion--make-envvar-table)) |
21622c6d SM |
788 | (prefix (substring string 0 beg))) |
789 | (if (eq (aref string (1- beg)) ?{) | |
790 | (setq table (apply-partially 'completion-table-with-terminator | |
791 | "}" table))) | |
792 | (completion-table-with-context prefix table | |
793 | (substring string beg) | |
794 | pred action)))) | |
017c22fe | 795 | |
f50e56f0 | 796 | (defun completion--file-name-table (string pred action) |
b95c7600 | 797 | "Internal subroutine for `read-file-name'. Do not call this." |
34b67b0f SM |
798 | (if (and (zerop (length string)) (eq 'lambda action)) |
799 | nil ; FIXME: why? | |
f50e56f0 SM |
800 | (let* ((dir (if (stringp pred) |
801 | ;; It used to be that `pred' was abused to pass `dir' | |
802 | ;; as an argument. | |
803 | (prog1 (expand-file-name pred) (setq pred nil)) | |
804 | default-directory)) | |
805 | (str (condition-case nil | |
21622c6d SM |
806 | (substitute-in-file-name string) |
807 | (error string))) | |
34b67b0f SM |
808 | (name (file-name-nondirectory str)) |
809 | (specdir (file-name-directory str)) | |
810 | (realdir (if specdir (expand-file-name specdir dir) | |
811 | (file-name-as-directory dir)))) | |
017c22fe | 812 | |
34b67b0f SM |
813 | (cond |
814 | ((null action) | |
815 | (let ((comp (file-name-completion name realdir | |
816 | read-file-name-predicate))) | |
817 | (if (stringp comp) | |
818 | ;; Requote the $s before returning the completion. | |
819 | (minibuffer--double-dollars (concat specdir comp)) | |
820 | ;; Requote the $s before checking for changes. | |
821 | (setq str (minibuffer--double-dollars str)) | |
822 | (if (string-equal string str) | |
823 | comp | |
824 | ;; If there's no real completion, but substitute-in-file-name | |
825 | ;; changed the string, then return the new string. | |
826 | str)))) | |
017c22fe | 827 | |
34b67b0f | 828 | ((eq action t) |
e2947429 SM |
829 | (let ((all (file-name-all-completions name realdir)) |
830 | ;; Actually, this is not always right in the presence of | |
831 | ;; envvars, but there's not much we can do, I think. | |
832 | (base-size (length (file-name-directory string)))) | |
833 | ||
834 | ;; Check the predicate, if necessary. | |
835 | (unless (memq read-file-name-predicate '(nil file-exists-p)) | |
34b67b0f SM |
836 | (let ((comp ()) |
837 | (pred | |
838 | (if (eq read-file-name-predicate 'file-directory-p) | |
839 | ;; Brute-force speed up for directory checking: | |
840 | ;; Discard strings which don't end in a slash. | |
841 | (lambda (s) | |
842 | (let ((len (length s))) | |
843 | (and (> len 0) (eq (aref s (1- len)) ?/)))) | |
844 | ;; Must do it the hard (and slow) way. | |
845 | read-file-name-predicate))) | |
846 | (let ((default-directory realdir)) | |
847 | (dolist (tem all) | |
848 | (if (funcall pred tem) (push tem comp)))) | |
e2947429 SM |
849 | (setq all (nreverse comp)))) |
850 | ||
88893215 SM |
851 | (if (and completion-all-completions-with-base-size (consp all)) |
852 | ;; Add base-size, but only if the list is non-empty. | |
853 | (nconc all base-size)) | |
854 | ||
855 | all)) | |
34b67b0f SM |
856 | |
857 | (t | |
858 | ;; Only other case actually used is ACTION = lambda. | |
859 | (let ((default-directory dir)) | |
860 | (funcall (or read-file-name-predicate 'file-exists-p) str))))))) | |
861 | ||
21622c6d | 862 | (defalias 'read-file-name-internal |
017c22fe | 863 | (completion-table-in-turn 'completion--embedded-envvar-table |
88893215 | 864 | 'completion--file-name-table) |
21622c6d | 865 | "Internal subroutine for `read-file-name'. Do not call this.") |
34b67b0f | 866 | |
dbd50d4b SM |
867 | (defvar read-file-name-function nil |
868 | "If this is non-nil, `read-file-name' does its work by calling this function.") | |
869 | ||
870 | (defvar read-file-name-predicate nil | |
871 | "Current predicate used by `read-file-name-internal'.") | |
872 | ||
873 | (defcustom read-file-name-completion-ignore-case | |
874 | (if (memq system-type '(ms-dos windows-nt darwin macos vax-vms axp-vms)) | |
875 | t nil) | |
876 | "Non-nil means when reading a file name completion ignores case." | |
877 | :group 'minibuffer | |
878 | :type 'boolean | |
879 | :version "22.1") | |
880 | ||
881 | (defcustom insert-default-directory t | |
882 | "Non-nil means when reading a filename start with default dir in minibuffer. | |
883 | ||
884 | When the initial minibuffer contents show a name of a file or a directory, | |
885 | typing RETURN without editing the initial contents is equivalent to typing | |
886 | the default file name. | |
887 | ||
888 | If this variable is non-nil, the minibuffer contents are always | |
889 | initially non-empty, and typing RETURN without editing will fetch the | |
890 | default name, if one is provided. Note however that this default name | |
891 | is not necessarily the same as initial contents inserted in the minibuffer, | |
892 | if the initial contents is just the default directory. | |
893 | ||
894 | If this variable is nil, the minibuffer often starts out empty. In | |
895 | that case you may have to explicitly fetch the next history element to | |
896 | request the default name; typing RETURN without editing will leave | |
897 | the minibuffer empty. | |
898 | ||
899 | For some commands, exiting with an empty minibuffer has a special meaning, | |
900 | such as making the current buffer visit no file in the case of | |
901 | `set-visited-file-name'." | |
902 | :group 'minibuffer | |
903 | :type 'boolean) | |
904 | ||
4e3870f5 GM |
905 | ;; Not always defined, but only called if next-read-file-uses-dialog-p says so. |
906 | (declare-function x-file-dialog "xfns.c" | |
907 | (prompt dir &optional default-filename mustmatch only-dir-p)) | |
908 | ||
dbd50d4b SM |
909 | (defun read-file-name (prompt &optional dir default-filename mustmatch initial predicate) |
910 | "Read file name, prompting with PROMPT and completing in directory DIR. | |
911 | Value is not expanded---you must call `expand-file-name' yourself. | |
912 | Default name to DEFAULT-FILENAME if user exits the minibuffer with | |
913 | the same non-empty string that was inserted by this function. | |
914 | (If DEFAULT-FILENAME is omitted, the visited file name is used, | |
915 | except that if INITIAL is specified, that combined with DIR is used.) | |
916 | If the user exits with an empty minibuffer, this function returns | |
917 | an empty string. (This can only happen if the user erased the | |
918 | pre-inserted contents or if `insert-default-directory' is nil.) | |
919 | Fourth arg MUSTMATCH non-nil means require existing file's name. | |
920 | Non-nil and non-t means also require confirmation after completion. | |
921 | Fifth arg INITIAL specifies text to start with. | |
922 | If optional sixth arg PREDICATE is non-nil, possible completions and | |
923 | the resulting file name must satisfy (funcall PREDICATE NAME). | |
924 | DIR should be an absolute directory name. It defaults to the value of | |
925 | `default-directory'. | |
926 | ||
927 | If this command was invoked with the mouse, use a file dialog box if | |
928 | `use-dialog-box' is non-nil, and the window system or X toolkit in use | |
929 | provides a file dialog box. | |
930 | ||
931 | See also `read-file-name-completion-ignore-case' | |
932 | and `read-file-name-function'." | |
933 | (unless dir (setq dir default-directory)) | |
934 | (unless (file-name-absolute-p dir) (setq dir (expand-file-name dir))) | |
935 | (unless default-filename | |
936 | (setq default-filename (if initial (expand-file-name initial dir) | |
937 | buffer-file-name))) | |
938 | ;; If dir starts with user's homedir, change that to ~. | |
939 | (setq dir (abbreviate-file-name dir)) | |
940 | ;; Likewise for default-filename. | |
e8a5fe3e SM |
941 | (if default-filename |
942 | (setq default-filename (abbreviate-file-name default-filename))) | |
dbd50d4b SM |
943 | (let ((insdef (cond |
944 | ((and insert-default-directory (stringp dir)) | |
945 | (if initial | |
946 | (cons (minibuffer--double-dollars (concat dir initial)) | |
947 | (length (minibuffer--double-dollars dir))) | |
948 | (minibuffer--double-dollars dir))) | |
949 | (initial (cons (minibuffer--double-dollars initial) 0))))) | |
950 | ||
951 | (if read-file-name-function | |
952 | (funcall read-file-name-function | |
953 | prompt dir default-filename mustmatch initial predicate) | |
e8a5fe3e | 954 | (let ((completion-ignore-case read-file-name-completion-ignore-case) |
dbd50d4b SM |
955 | (minibuffer-completing-file-name t) |
956 | (read-file-name-predicate (or predicate 'file-exists-p)) | |
957 | (add-to-history nil)) | |
958 | ||
959 | (let* ((val | |
960 | (if (not (next-read-file-uses-dialog-p)) | |
e8a5fe3e SM |
961 | ;; We used to pass `dir' to `read-file-name-internal' by |
962 | ;; abusing the `predicate' argument. It's better to | |
963 | ;; just use `default-directory', but in order to avoid | |
964 | ;; changing `default-directory' in the current buffer, | |
965 | ;; we don't let-bind it. | |
966 | (lexical-let ((dir (file-name-as-directory | |
967 | (expand-file-name dir)))) | |
968 | (minibuffer-with-setup-hook | |
969 | (lambda () (setq default-directory dir)) | |
970 | (completing-read prompt 'read-file-name-internal | |
971 | nil mustmatch insdef 'file-name-history | |
972 | default-filename))) | |
dbd50d4b SM |
973 | ;; If DIR contains a file name, split it. |
974 | (let ((file (file-name-nondirectory dir))) | |
975 | (when (and default-filename (not (zerop (length file)))) | |
976 | (setq default-filename file) | |
977 | (setq dir (file-name-directory dir))) | |
978 | (if default-filename | |
979 | (setq default-filename | |
980 | (expand-file-name default-filename dir))) | |
981 | (setq add-to-history t) | |
982 | (x-file-dialog prompt dir default-filename mustmatch | |
983 | (eq predicate 'file-directory-p))))) | |
984 | ||
985 | (replace-in-history (eq (car-safe file-name-history) val))) | |
986 | ;; If completing-read returned the inserted default string itself | |
987 | ;; (rather than a new string with the same contents), | |
988 | ;; it has to mean that the user typed RET with the minibuffer empty. | |
989 | ;; In that case, we really want to return "" | |
990 | ;; so that commands such as set-visited-file-name can distinguish. | |
991 | (when (eq val default-filename) | |
992 | ;; In this case, completing-read has not added an element | |
993 | ;; to the history. Maybe we should. | |
994 | (if (not replace-in-history) | |
995 | (setq add-to-history t)) | |
996 | (setq val "")) | |
997 | (unless val (error "No file name specified")) | |
998 | ||
999 | (if (and default-filename | |
1000 | (string-equal val (if (consp insdef) (car insdef) insdef))) | |
1001 | (setq val default-filename)) | |
1002 | (setq val (substitute-in-file-name val)) | |
1003 | ||
1004 | (if replace-in-history | |
1005 | ;; Replace what Fcompleting_read added to the history | |
1006 | ;; with what we will actually return. | |
1007 | (let ((val1 (minibuffer--double-dollars val))) | |
1008 | (if history-delete-duplicates | |
1009 | (setcdr file-name-history | |
1010 | (delete val1 (cdr file-name-history)))) | |
1011 | (setcar file-name-history val1)) | |
1012 | (if add-to-history | |
1013 | ;; Add the value to the history--but not if it matches | |
1014 | ;; the last value already there. | |
1015 | (let ((val1 (minibuffer--double-dollars val))) | |
1016 | (unless (and (consp file-name-history) | |
1017 | (equal (car file-name-history) val1)) | |
1018 | (setq file-name-history | |
1019 | (cons val1 | |
1020 | (if history-delete-duplicates | |
1021 | (delete val1 file-name-history) | |
1022 | file-name-history))))))) | |
1023 | val))))) | |
1024 | ||
8b04c0ae JL |
1025 | (defun internal-complete-buffer-except (&optional buffer) |
1026 | "Perform completion on all buffers excluding BUFFER. | |
1027 | Like `internal-complete-buffer', but removes BUFFER from the completion list." | |
1028 | (lexical-let ((except (if (stringp buffer) buffer (buffer-name buffer)))) | |
1029 | (apply-partially 'completion-table-with-predicate | |
1030 | 'internal-complete-buffer | |
1031 | (lambda (name) | |
1032 | (not (equal (if (consp name) (car name) name) except))) | |
1033 | nil))) | |
1034 | ||
19c04f39 SM |
1035 | ;;; Old-style completion, used in Emacs-21. |
1036 | ||
1037 | (defun completion-emacs21-try-completion (string table pred point) | |
1038 | (let ((completion (try-completion string table pred))) | |
1039 | (if (stringp completion) | |
1040 | (cons completion (length completion)) | |
1041 | completion))) | |
1042 | ||
1043 | (defun completion-emacs21-all-completions (string table pred point) | |
6138158d SM |
1044 | (completion-hilit-commonality |
1045 | (all-completions string table pred t) | |
1046 | (length string))) | |
19c04f39 SM |
1047 | |
1048 | ;;; Basic completion, used in Emacs-22. | |
1049 | ||
1050 | (defun completion-emacs22-try-completion (string table pred point) | |
1051 | (let ((suffix (substring string point)) | |
1052 | (completion (try-completion (substring string 0 point) table pred))) | |
1053 | (if (not (stringp completion)) | |
1054 | completion | |
1055 | ;; Merge a trailing / in completion with a / after point. | |
1056 | ;; We used to only do it for word completion, but it seems to make | |
1057 | ;; sense for all completions. | |
34200787 SM |
1058 | ;; Actually, claiming this feature was part of Emacs-22 completion |
1059 | ;; is pushing it a bit: it was only done in minibuffer-completion-word, | |
1060 | ;; which was (by default) not bound during file completion, where such | |
1061 | ;; slashes are most likely to occur. | |
1062 | (if (and (not (zerop (length completion))) | |
1063 | (eq ?/ (aref completion (1- (length completion)))) | |
19c04f39 SM |
1064 | (not (zerop (length suffix))) |
1065 | (eq ?/ (aref suffix 0))) | |
34200787 SM |
1066 | ;; This leaves point after the / . |
1067 | (setq suffix (substring suffix 1))) | |
19c04f39 SM |
1068 | (cons (concat completion suffix) (length completion))))) |
1069 | ||
1070 | (defun completion-emacs22-all-completions (string table pred point) | |
6138158d SM |
1071 | (completion-hilit-commonality |
1072 | (all-completions (substring string 0 point) table pred t) | |
1073 | point)) | |
19c04f39 | 1074 | |
34200787 SM |
1075 | (defun completion-basic-try-completion (string table pred point) |
1076 | (let ((suffix (substring string point)) | |
1077 | (completion (try-completion (substring string 0 point) table pred))) | |
1078 | (if (not (stringp completion)) | |
1079 | completion | |
1080 | ;; Merge end of completion with beginning of suffix. | |
1081 | ;; Simple generalization of the "merge trailing /" done in Emacs-22. | |
1082 | (when (and (not (zerop (length suffix))) | |
1083 | (string-match "\\(.+\\)\n\\1" (concat completion "\n" suffix) | |
1084 | ;; Make sure we don't compress things to less | |
1085 | ;; than we started with. | |
1086 | point) | |
1087 | ;; Just make sure we didn't match some other \n. | |
1088 | (eq (match-end 1) (length completion))) | |
1089 | (setq suffix (substring suffix (- (match-end 1) (match-beginning 1))))) | |
1090 | ||
1091 | (cons (concat completion suffix) (length completion))))) | |
1092 | ||
19c04f39 SM |
1093 | (defalias 'completion-basic-all-completions 'completion-emacs22-all-completions) |
1094 | ||
34200787 SM |
1095 | ;;; Partial-completion-mode style completion. |
1096 | ||
1097 | ;; BUGS: | |
1098 | ||
1099 | ;; - "minibuffer-s- TAB" with minibuffer-selected-window ends up with | |
1100 | ;; "minibuffer--s-" which matches other options. | |
1101 | ||
1102 | (defvar completion-pcm--delim-wild-regex nil) | |
1103 | ||
1104 | (defun completion-pcm--prepare-delim-re (delims) | |
1105 | (setq completion-pcm--delim-wild-regex (concat "[" delims "*]"))) | |
1106 | ||
1107 | (defcustom completion-pcm-word-delimiters "-_. " | |
1108 | "A string of characters treated as word delimiters for completion. | |
1109 | Some arcane rules: | |
1110 | If `]' is in this string, it must come first. | |
1111 | If `^' is in this string, it must not come first. | |
1112 | If `-' is in this string, it must come first or right after `]'. | |
1113 | In other words, if S is this string, then `[S]' must be a valid Emacs regular | |
1114 | expression (not containing character ranges like `a-z')." | |
1115 | :set (lambda (symbol value) | |
1116 | (set-default symbol value) | |
1117 | ;; Refresh other vars. | |
1118 | (completion-pcm--prepare-delim-re value)) | |
1119 | :initialize 'custom-initialize-reset | |
26c548b0 | 1120 | :group 'minibuffer |
34200787 SM |
1121 | :type 'string) |
1122 | ||
1123 | (defun completion-pcm--pattern-trivial-p (pattern) | |
1124 | (and (stringp (car pattern)) (null (cdr pattern)))) | |
1125 | ||
1126 | (defun completion-pcm--string->pattern (basestr &optional point) | |
1127 | "Split BASESTR into a pattern. | |
1128 | A pattern is a list where each element is either a string | |
1129 | or a symbol chosen among `any', `star', `point'." | |
1130 | (if (and point (< point (length basestr))) | |
1131 | (let ((prefix (substring basestr 0 point)) | |
1132 | (suffix (substring basestr point))) | |
1133 | (append (completion-pcm--string->pattern prefix) | |
1134 | '(point) | |
1135 | (completion-pcm--string->pattern suffix))) | |
1136 | (let ((pattern nil) | |
1137 | (p 0) | |
1138 | (p0 0)) | |
26c548b0 | 1139 | |
34200787 SM |
1140 | (while (setq p (string-match completion-pcm--delim-wild-regex basestr p)) |
1141 | (push (substring basestr p0 p) pattern) | |
1142 | (if (eq (aref basestr p) ?*) | |
1143 | (progn | |
1144 | (push 'star pattern) | |
1145 | (setq p0 (1+ p))) | |
1146 | (push 'any pattern) | |
1147 | (setq p0 p)) | |
1148 | (incf p)) | |
1149 | ||
1150 | ;; An empty string might be erroneously added at the beginning. | |
1151 | ;; It should be avoided properly, but it's so easy to remove it here. | |
1152 | (delete "" (nreverse (cons (substring basestr p0) pattern)))))) | |
1153 | ||
1154 | (defun completion-pcm--pattern->regex (pattern &optional group) | |
1155 | (concat "\\`" | |
1156 | (mapconcat | |
1157 | (lambda (x) | |
1158 | (case x | |
1159 | ((star any point) (if group "\\(.*?\\)" ".*?")) | |
1160 | (t (regexp-quote x)))) | |
1161 | pattern | |
1162 | ""))) | |
1163 | ||
1164 | (defun completion-pcm--all-completions (pattern table pred) | |
1165 | "Find all completions for PATTERN in TABLE obeying PRED. | |
26c548b0 | 1166 | PATTERN is as returned by `completion-pcm--string->pattern'." |
34200787 SM |
1167 | ;; Find an initial list of possible completions. |
1168 | (if (completion-pcm--pattern-trivial-p pattern) | |
1169 | ||
1170 | ;; Minibuffer contains no delimiters -- simple case! | |
1171 | (all-completions (car pattern) table pred) | |
26c548b0 | 1172 | |
34200787 SM |
1173 | ;; Use all-completions to do an initial cull. This is a big win, |
1174 | ;; since all-completions is written in C! | |
1175 | (let* (;; Convert search pattern to a standard regular expression. | |
1176 | (regex (completion-pcm--pattern->regex pattern)) | |
1177 | (completion-regexp-list (cons regex completion-regexp-list)) | |
1178 | (compl (all-completions | |
602f074a | 1179 | (if (stringp (car pattern)) (car pattern) "") |
34200787 SM |
1180 | table pred)) |
1181 | (last (last compl))) | |
1182 | ;; FIXME: If `base-size' is not 0, we have a problem :-( | |
1183 | (if last (setcdr last nil)) | |
1184 | (if (not (functionp table)) | |
1185 | ;; The internal functions already obeyed completion-regexp-list. | |
1186 | compl | |
1187 | (let ((case-fold-search completion-ignore-case) | |
1188 | (poss ())) | |
1189 | (dolist (c compl) | |
1190 | (when (string-match regex c) (push c poss))) | |
1191 | poss))))) | |
1192 | ||
1193 | (defun completion-pcm-all-completions (string table pred point) | |
1194 | (let ((pattern (completion-pcm--string->pattern string point))) | |
1195 | (completion-pcm--all-completions pattern table pred))) | |
1196 | ||
1197 | (defun completion-pcm--merge-completions (strs pattern) | |
1198 | "Extract the commonality in STRS, with the help of PATTERN." | |
1199 | (cond | |
1200 | ((null (cdr strs)) (list (car strs))) | |
1201 | (t | |
1202 | (let ((re (completion-pcm--pattern->regex pattern 'group)) | |
1203 | (ccs ())) ;Chopped completions. | |
1204 | ||
1205 | ;; First chop each string into the parts corresponding to each | |
1206 | ;; non-constant element of `pattern', using regexp-matching. | |
1207 | (let ((case-fold-search completion-ignore-case)) | |
1208 | (dolist (str strs) | |
1209 | (unless (string-match re str) | |
1210 | (error "Internal error: %s doesn't match %s" str re)) | |
1211 | (let ((chopped ()) | |
1212 | (i 1)) | |
1213 | (while (match-beginning i) | |
1214 | (push (match-string i str) chopped) | |
1215 | (setq i (1+ i))) | |
1216 | ;; Add the text corresponding to the implicit trailing `any'. | |
1217 | (push (substring str (match-end 0)) chopped) | |
1218 | (push (nreverse chopped) ccs)))) | |
1219 | ||
1220 | ;; Then for each of those non-constant elements, extract the | |
1221 | ;; commonality between them. | |
1222 | (let ((res ())) | |
1223 | ;; Make the implicit `any' explicit. We could make it explicit | |
1224 | ;; everywhere, but it would slow down regexp-matching a little bit. | |
1225 | (dolist (elem (append pattern '(any))) | |
1226 | (if (stringp elem) | |
1227 | (push elem res) | |
1228 | (let ((comps ())) | |
1229 | (dolist (cc (prog1 ccs (setq ccs nil))) | |
1230 | (push (car cc) comps) | |
1231 | (push (cdr cc) ccs)) | |
1232 | (let* ((prefix (try-completion "" comps)) | |
1233 | (unique (or (and (eq prefix t) (setq prefix "")) | |
1234 | (eq t (try-completion prefix comps))))) | |
1235 | (unless (equal prefix "") (push prefix res)) | |
1236 | ;; If there's only one completion, `elem' is not useful | |
1237 | ;; any more: it can only match the empty string. | |
1238 | ;; FIXME: in some cases, it may be necessary to turn an | |
1239 | ;; `any' into a `star' because the surrounding context has | |
1240 | ;; changed such that string->pattern wouldn't add an `any' | |
1241 | ;; here any more. | |
1242 | (unless unique (push elem res)))))) | |
1243 | ;; We return it in reverse order. | |
1244 | res))))) | |
1245 | ||
1246 | (defun completion-pcm--pattern->string (pattern) | |
1247 | (mapconcat (lambda (x) (cond | |
1248 | ((stringp x) x) | |
1249 | ((eq x 'star) "*") | |
1250 | ((eq x 'any) "") | |
1251 | ((eq x 'point) ""))) | |
1252 | pattern | |
1253 | "")) | |
1254 | ||
1255 | (defun completion-pcm-try-completion (string table pred point) | |
1256 | (let* ((pattern (completion-pcm--string->pattern string point)) | |
1257 | (all (completion-pcm--all-completions pattern table pred))) | |
1258 | (when all | |
1259 | (let* ((mergedpat (completion-pcm--merge-completions all pattern)) | |
1260 | ;; `mergedpat' is in reverse order. | |
1261 | (pointpat (or (memq 'point mergedpat) (memq 'any mergedpat))) | |
1262 | ;; New pos from the end. | |
1263 | (newpos (length (completion-pcm--pattern->string pointpat))) | |
1264 | ;; Do it afterwards because it changes `pointpat' by sideeffect. | |
1265 | (merged (completion-pcm--pattern->string (nreverse mergedpat)))) | |
1266 | (cons merged (- (length merged) newpos)))))) | |
34200787 SM |
1267 | |
1268 | ||
32bae13c | 1269 | (provide 'minibuffer) |
dc6ee347 MB |
1270 | |
1271 | ;; arch-tag: ef8a0a15-1080-4790-a754-04017c02f08f | |
32bae13c | 1272 | ;;; minibuffer.el ends here |