Commit | Line | Data |
---|---|---|
32bae13c SM |
1 | ;;; minibuffer.el --- Minibuffer completion functions |
2 | ||
ae940284 | 3 | ;; Copyright (C) 2008, 2009 Free Software Foundation, Inc. |
32bae13c SM |
4 | |
5 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
eb3fa2cf | 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
32bae13c SM |
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 | ||
eb3fa2cf | 14 | ;; GNU Emacs is distributed in the hope that it will be useful, |
32bae13c SM |
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 | |
eb3fa2cf | 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
32bae13c SM |
21 | |
22 | ;;; Commentary: | |
23 | ||
a38313e1 SM |
24 | ;; Names with "--" are for functions and variables that are meant to be for |
25 | ;; internal use only. | |
26 | ||
27 | ;; Functional completion tables have an extended calling conventions: | |
a38313e1 | 28 | ;; - The `action' can be (additionally to nil, t, and lambda) of the form |
f8381803 SM |
29 | ;; (boundaries . SUFFIX) in which case it should return |
30 | ;; (boundaries START . END). See `completion-boundaries'. | |
a38313e1 SM |
31 | ;; Any other return value should be ignored (so we ignore values returned |
32 | ;; from completion tables that don't know about this new `action' form). | |
a38313e1 SM |
33 | |
34 | ;;; Bugs: | |
35 | ||
eee6de73 SM |
36 | ;; - completion-all-sorted-completions list all the completions, whereas |
37 | ;; it should only lists the ones that `try-completion' would consider. | |
38 | ;; E.g. it should honor completion-ignored-extensions. | |
a38313e1 | 39 | ;; - choose-completion can't automatically figure out the boundaries |
528c56e2 SM |
40 | ;; corresponding to the displayed completions because we only |
41 | ;; provide the start info but not the end info in | |
42 | ;; completion-base-position. | |
4fcc3d32 SM |
43 | ;; - quoting is problematic. E.g. the double-dollar quoting used in |
44 | ;; substitie-in-file-name (and hence read-file-name-internal) bumps | |
45 | ;; into various bugs: | |
528c56e2 SM |
46 | ;; - choose-completion doesn't know how to quote the text it inserts. |
47 | ;; E.g. it fails to double the dollars in file-name completion, or | |
48 | ;; to backslash-escape spaces and other chars in comint completion. | |
4fcc3d32 SM |
49 | ;; - when completing ~/tmp/fo$$o, the highligting in *Completions* |
50 | ;; is off by one position. | |
51 | ;; - all code like PCM which relies on all-completions to match | |
52 | ;; its argument gets confused because all-completions returns unquoted | |
53 | ;; texts (as desired for *Completions* output). | |
528c56e2 SM |
54 | ;; - C-x C-f ~/*/sr ? should not list "~/./src". |
55 | ;; - minibuffer-force-complete completes ~/src/emacs/t<!>/lisp/minibuffer.el | |
56 | ;; to ~/src/emacs/trunk/ and throws away lisp/minibuffer.el. | |
ba5ff07b | 57 | |
3911966b SM |
58 | ;;; Todo: |
59 | ||
ab22be48 | 60 | ;; - make partial-complete-mode obsolete: |
ab22be48 | 61 | ;; - (?) <foo.h> style completion for file names. |
528c56e2 SM |
62 | ;; This can't be done identically just by tweaking completion, |
63 | ;; because partial-completion-mode's behavior is to expand <string.h> | |
64 | ;; to /usr/include/string.h only when exiting the minibuffer, at which | |
65 | ;; point the completion code is actually not involved normally. | |
66 | ;; Partial-completion-mode does it via a find-file-not-found-function. | |
67 | ;; - special code for C-x C-f <> to visit the file ref'd at point | |
68 | ;; via (require 'foo) or #include "foo". ffap seems like a better | |
69 | ;; place for this feature (supplemented with major-mode-provided | |
70 | ;; functions to find the file ref'd at point). | |
71 | ||
72 | ;; - case-sensitivity currently confuses two issues: | |
ab22be48 | 73 | ;; - whether or not a particular completion table should be case-sensitive |
528c56e2 | 74 | ;; (i.e. whether strings that differ only by case are semantically |
ab22be48 SM |
75 | ;; equivalent) |
76 | ;; - whether the user wants completion to pay attention to case. | |
77 | ;; e.g. we may want to make it possible for the user to say "first try | |
78 | ;; completion case-sensitively, and if that fails, try to ignore case". | |
79 | ||
a38313e1 | 80 | ;; - add support for ** to pcm. |
3911966b SM |
81 | ;; - Add vc-file-name-completion-table to read-file-name-internal. |
82 | ;; - A feature like completing-help.el. | |
32bae13c SM |
83 | |
84 | ;;; Code: | |
85 | ||
86 | (eval-when-compile (require 'cl)) | |
87 | ||
21622c6d SM |
88 | ;;; Completion table manipulation |
89 | ||
a38313e1 | 90 | ;; New completion-table operation. |
f8381803 SM |
91 | (defun completion-boundaries (string table pred suffix) |
92 | "Return the boundaries of the completions returned by TABLE for STRING. | |
a38313e1 | 93 | STRING is the string on which completion will be performed. |
f8381803 SM |
94 | SUFFIX is the string after point. |
95 | The result is of the form (START . END) where START is the position | |
96 | in STRING of the beginning of the completion field and END is the position | |
97 | in SUFFIX of the end of the completion field. | |
f8381803 SM |
98 | E.g. for simple completion tables, the result is always (0 . (length SUFFIX)) |
99 | and for file names the result is the positions delimited by | |
a38313e1 SM |
100 | the closest directory separators." |
101 | (let ((boundaries (if (functionp table) | |
f8381803 | 102 | (funcall table string pred (cons 'boundaries suffix))))) |
a38313e1 SM |
103 | (if (not (eq (car-safe boundaries) 'boundaries)) |
104 | (setq boundaries nil)) | |
105 | (cons (or (cadr boundaries) 0) | |
f8381803 | 106 | (or (cddr boundaries) (length suffix))))) |
a38313e1 | 107 | |
e2947429 SM |
108 | (defun completion--some (fun xs) |
109 | "Apply FUN to each element of XS in turn. | |
110 | Return the first non-nil returned value. | |
111 | Like CL's `some'." | |
a38313e1 SM |
112 | (let ((firsterror nil) |
113 | res) | |
e2947429 | 114 | (while (and (not res) xs) |
a38313e1 SM |
115 | (condition-case err |
116 | (setq res (funcall fun (pop xs))) | |
117 | (error (unless firsterror (setq firsterror err)) nil))) | |
118 | (or res | |
119 | (if firsterror (signal (car firsterror) (cdr firsterror)))))) | |
e2947429 | 120 | |
21622c6d SM |
121 | (defun complete-with-action (action table string pred) |
122 | "Perform completion ACTION. | |
123 | STRING is the string to complete. | |
124 | TABLE is the completion table, which should not be a function. | |
125 | PRED is a completion predicate. | |
126 | ACTION can be one of nil, t or `lambda'." | |
a38313e1 SM |
127 | (cond |
128 | ((functionp table) (funcall table string pred action)) | |
129 | ((eq (car-safe action) 'boundaries) | |
130 | (cons 'boundaries (completion-boundaries string table pred (cdr action)))) | |
131 | (t | |
132 | (funcall | |
133 | (cond | |
134 | ((null action) 'try-completion) | |
135 | ((eq action t) 'all-completions) | |
136 | (t 'test-completion)) | |
137 | string table pred)))) | |
21622c6d SM |
138 | |
139 | (defun completion-table-dynamic (fun) | |
140 | "Use function FUN as a dynamic completion table. | |
141 | FUN is called with one argument, the string for which completion is required, | |
b95c7600 JB |
142 | and it should return an alist containing all the intended possible completions. |
143 | This alist may be a full list of possible completions so that FUN can ignore | |
144 | the value of its argument. If completion is performed in the minibuffer, | |
145 | FUN will be called in the buffer from which the minibuffer was entered. | |
21622c6d | 146 | |
e8061cd9 | 147 | The result of the `completion-table-dynamic' form is a function |
d9aa6b33 | 148 | that can be used as the COLLECTION argument to `try-completion' and |
b95c7600 | 149 | `all-completions'. See Info node `(elisp)Programmed Completion'." |
21622c6d SM |
150 | (lexical-let ((fun fun)) |
151 | (lambda (string pred action) | |
152 | (with-current-buffer (let ((win (minibuffer-selected-window))) | |
153 | (if (window-live-p win) (window-buffer win) | |
154 | (current-buffer))) | |
155 | (complete-with-action action (funcall fun string) string pred))))) | |
156 | ||
157 | (defmacro lazy-completion-table (var fun) | |
158 | "Initialize variable VAR as a lazy completion table. | |
159 | If the completion table VAR is used for the first time (e.g., by passing VAR | |
160 | as an argument to `try-completion'), the function FUN is called with no | |
161 | arguments. FUN must return the completion table that will be stored in VAR. | |
162 | If completion is requested in the minibuffer, FUN will be called in the buffer | |
163 | from which the minibuffer was entered. The return value of | |
164 | `lazy-completion-table' must be used to initialize the value of VAR. | |
165 | ||
166 | You should give VAR a non-nil `risky-local-variable' property." | |
69e018a7 | 167 | (declare (debug (symbolp lambda-expr))) |
21622c6d SM |
168 | (let ((str (make-symbol "string"))) |
169 | `(completion-table-dynamic | |
170 | (lambda (,str) | |
171 | (when (functionp ,var) | |
172 | (setq ,var (,fun))) | |
173 | ,var)))) | |
174 | ||
175 | (defun completion-table-with-context (prefix table string pred action) | |
25c0d999 | 176 | ;; TODO: add `suffix' maybe? |
a38313e1 | 177 | ;; Notice that `pred' may not be a function in some abusive cases. |
34200787 SM |
178 | (when (functionp pred) |
179 | (setq pred | |
180 | (lexical-let ((pred pred)) | |
181 | ;; Predicates are called differently depending on the nature of | |
182 | ;; the completion table :-( | |
183 | (cond | |
184 | ((vectorp table) ;Obarray. | |
185 | (lambda (sym) (funcall pred (concat prefix (symbol-name sym))))) | |
186 | ((hash-table-p table) | |
187 | (lambda (s v) (funcall pred (concat prefix s)))) | |
188 | ((functionp table) | |
189 | (lambda (s) (funcall pred (concat prefix s)))) | |
190 | (t ;Lists and alists. | |
191 | (lambda (s) | |
192 | (funcall pred (concat prefix (if (consp s) (car s) s))))))))) | |
a38313e1 SM |
193 | (if (eq (car-safe action) 'boundaries) |
194 | (let* ((len (length prefix)) | |
f8381803 SM |
195 | (bound (completion-boundaries string table pred (cdr action)))) |
196 | (list* 'boundaries (+ (car bound) len) (cdr bound))) | |
a38313e1 SM |
197 | (let ((comp (complete-with-action action table string pred))) |
198 | (cond | |
199 | ;; In case of try-completion, add the prefix. | |
200 | ((stringp comp) (concat prefix comp)) | |
a38313e1 | 201 | (t comp))))) |
21622c6d SM |
202 | |
203 | (defun completion-table-with-terminator (terminator table string pred action) | |
528c56e2 SM |
204 | "Construct a completion table like TABLE but with an extra TERMINATOR. |
205 | This is meant to be called in a curried way by first passing TERMINATOR | |
206 | and TABLE only (via `apply-partially'). | |
207 | TABLE is a completion table, and TERMINATOR is a string appended to TABLE's | |
208 | completion if it is complete. TERMINATOR is also used to determine the | |
a452eee8 SM |
209 | completion suffix's boundary. |
210 | TERMINATOR can also be a cons cell (TERMINATOR . TERMINATOR-REGEXP) | |
211 | in which case TERMINATOR-REGEXP is a regular expression whose submatch | |
212 | number 1 should match TERMINATOR. This is used when there is a need to | |
213 | distinguish occurrences of the TERMINATOR strings which are really terminators | |
214 | from others (e.g. escaped)." | |
25c0d999 | 215 | (cond |
528c56e2 SM |
216 | ((eq (car-safe action) 'boundaries) |
217 | (let* ((suffix (cdr action)) | |
218 | (bounds (completion-boundaries string table pred suffix)) | |
a452eee8 SM |
219 | (terminator-regexp (if (consp terminator) |
220 | (cdr terminator) (regexp-quote terminator))) | |
221 | (max (string-match terminator-regexp suffix))) | |
528c56e2 SM |
222 | (list* 'boundaries (car bounds) |
223 | (min (cdr bounds) (or max (length suffix)))))) | |
25c0d999 SM |
224 | ((eq action nil) |
225 | (let ((comp (try-completion string table pred))) | |
a452eee8 | 226 | (if (consp terminator) (setq terminator (car terminator))) |
88893215 SM |
227 | (if (eq comp t) |
228 | (concat string terminator) | |
229 | (if (and (stringp comp) | |
528c56e2 SM |
230 | ;; FIXME: Try to avoid this second call, especially since |
231 | ;; it may be very inefficient (because `comp' made us | |
232 | ;; jump to a new boundary, so we complete in that | |
233 | ;; boundary with an empty start string). | |
234 | ;; completion-boundaries might help. | |
25c0d999 | 235 | (eq (try-completion comp table pred) t)) |
88893215 | 236 | (concat comp terminator) |
25c0d999 | 237 | comp)))) |
a38313e1 SM |
238 | ((eq action t) |
239 | ;; FIXME: We generally want the `try' and `all' behaviors to be | |
240 | ;; consistent so pcm can merge the `all' output to get the `try' output, | |
241 | ;; but that sometimes clashes with the need for `all' output to look | |
242 | ;; good in *Completions*. | |
125f7951 SM |
243 | ;; (mapcar (lambda (s) (concat s terminator)) |
244 | ;; (all-completions string table pred)))) | |
a38313e1 | 245 | (all-completions string table pred)) |
25c0d999 SM |
246 | ;; completion-table-with-terminator is always used for |
247 | ;; "sub-completions" so it's only called if the terminator is missing, | |
248 | ;; in which case `test-completion' should return nil. | |
249 | ((eq action 'lambda) nil))) | |
250 | ||
251 | (defun completion-table-with-predicate (table pred1 strict string pred2 action) | |
252 | "Make a completion table equivalent to TABLE but filtered through PRED1. | |
cf43708e | 253 | PRED1 is a function of one argument which returns non-nil if and only if the |
25c0d999 SM |
254 | argument is an element of TABLE which should be considered for completion. |
255 | STRING, PRED2, and ACTION are the usual arguments to completion tables, | |
256 | as described in `try-completion', `all-completions', and `test-completion'. | |
3911966b SM |
257 | If STRICT is t, the predicate always applies; if nil it only applies if |
258 | it does not reduce the set of possible completions to nothing. | |
25c0d999 SM |
259 | Note: TABLE needs to be a proper completion table which obeys predicates." |
260 | (cond | |
261 | ((and (not strict) (eq action 'lambda)) | |
262 | ;; Ignore pred1 since it doesn't really have to apply anyway. | |
af48580e | 263 | (test-completion string table pred2)) |
25c0d999 SM |
264 | (t |
265 | (or (complete-with-action action table string | |
266 | (if (null pred2) pred1 | |
267 | (lexical-let ((pred1 pred2) (pred2 pred2)) | |
268 | (lambda (x) | |
269 | ;; Call `pred1' first, so that `pred2' | |
270 | ;; really can't tell that `x' is in table. | |
271 | (if (funcall pred1 x) (funcall pred2 x)))))) | |
272 | ;; If completion failed and we're not applying pred1 strictly, try | |
273 | ;; again without pred1. | |
274 | (and (not strict) | |
275 | (complete-with-action action table string pred2)))))) | |
21622c6d | 276 | |
e2947429 SM |
277 | (defun completion-table-in-turn (&rest tables) |
278 | "Create a completion table that tries each table in TABLES in turn." | |
528c56e2 SM |
279 | ;; FIXME: the boundaries may come from TABLE1 even when the completion list |
280 | ;; is returned by TABLE2 (because TABLE1 returned an empty list). | |
e2947429 | 281 | (lexical-let ((tables tables)) |
21622c6d | 282 | (lambda (string pred action) |
e2947429 SM |
283 | (completion--some (lambda (table) |
284 | (complete-with-action action table string pred)) | |
285 | tables)))) | |
286 | ||
25c0d999 SM |
287 | ;; (defmacro complete-in-turn (a b) `(completion-table-in-turn ,a ,b)) |
288 | ;; (defmacro dynamic-completion-table (fun) `(completion-table-dynamic ,fun)) | |
e2947429 SM |
289 | (define-obsolete-function-alias |
290 | 'complete-in-turn 'completion-table-in-turn "23.1") | |
25c0d999 SM |
291 | (define-obsolete-function-alias |
292 | 'dynamic-completion-table 'completion-table-dynamic "23.1") | |
21622c6d SM |
293 | |
294 | ;;; Minibuffer completion | |
295 | ||
ba5ff07b SM |
296 | (defgroup minibuffer nil |
297 | "Controlling the behavior of the minibuffer." | |
298 | :link '(custom-manual "(emacs)Minibuffer") | |
299 | :group 'environment) | |
300 | ||
32bae13c SM |
301 | (defun minibuffer-message (message &rest args) |
302 | "Temporarily display MESSAGE at the end of the minibuffer. | |
303 | The text is displayed for `minibuffer-message-timeout' seconds, | |
304 | or until the next input event arrives, whichever comes first. | |
305 | Enclose MESSAGE in [...] if this is not yet the case. | |
306 | If ARGS are provided, then pass MESSAGE through `format'." | |
ab22be48 SM |
307 | (if (not (minibufferp (current-buffer))) |
308 | (progn | |
309 | (if args | |
310 | (apply 'message message args) | |
311 | (message "%s" message)) | |
312 | (prog1 (sit-for (or minibuffer-message-timeout 1000000)) | |
313 | (message nil))) | |
314 | ;; Clear out any old echo-area message to make way for our new thing. | |
315 | (message nil) | |
316 | (setq message (if (and (null args) (string-match-p "\\` *\\[.+\\]\\'" message)) | |
317 | ;; Make sure we can put-text-property. | |
318 | (copy-sequence message) | |
319 | (concat " [" message "]"))) | |
320 | (when args (setq message (apply 'format message args))) | |
321 | (let ((ol (make-overlay (point-max) (point-max) nil t t)) | |
322 | ;; A quit during sit-for normally only interrupts the sit-for, | |
323 | ;; but since minibuffer-message is used at the end of a command, | |
324 | ;; at a time when the command has virtually finished already, a C-g | |
325 | ;; should really cause an abort-recursive-edit instead (i.e. as if | |
326 | ;; the C-g had been typed at top-level). Binding inhibit-quit here | |
327 | ;; is an attempt to get that behavior. | |
328 | (inhibit-quit t)) | |
329 | (unwind-protect | |
330 | (progn | |
331 | (unless (zerop (length message)) | |
332 | ;; The current C cursor code doesn't know to use the overlay's | |
333 | ;; marker's stickiness to figure out whether to place the cursor | |
334 | ;; before or after the string, so let's spoon-feed it the pos. | |
335 | (put-text-property 0 1 'cursor t message)) | |
336 | (overlay-put ol 'after-string message) | |
337 | (sit-for (or minibuffer-message-timeout 1000000))) | |
338 | (delete-overlay ol))))) | |
32bae13c SM |
339 | |
340 | (defun minibuffer-completion-contents () | |
341 | "Return the user input in a minibuffer before point as a string. | |
342 | That is what completion commands operate on." | |
343 | (buffer-substring (field-beginning) (point))) | |
344 | ||
345 | (defun delete-minibuffer-contents () | |
346 | "Delete all user input in a minibuffer. | |
347 | If the current buffer is not a minibuffer, erase its entire contents." | |
8c9f211f CY |
348 | ;; We used to do `delete-field' here, but when file name shadowing |
349 | ;; is on, the field doesn't cover the entire minibuffer contents. | |
350 | (delete-region (minibuffer-prompt-end) (point-max))) | |
32bae13c | 351 | |
ba5ff07b SM |
352 | (defcustom completion-auto-help t |
353 | "Non-nil means automatically provide help for invalid completion input. | |
354 | If the value is t the *Completion* buffer is displayed whenever completion | |
355 | is requested but cannot be done. | |
356 | If the value is `lazy', the *Completions* buffer is only displayed after | |
357 | the second failed attempt to complete." | |
e1bb0fe5 | 358 | :type '(choice (const nil) (const t) (const lazy)) |
ba5ff07b SM |
359 | :group 'minibuffer) |
360 | ||
2f7f4bee | 361 | (defconst completion-styles-alist |
fcb68f70 SM |
362 | '((emacs21 |
363 | completion-emacs21-try-completion completion-emacs21-all-completions | |
364 | "Simple prefix-based completion.") | |
365 | (emacs22 | |
366 | completion-emacs22-try-completion completion-emacs22-all-completions | |
367 | "Prefix completion that only operates on the text before point.") | |
368 | (basic | |
369 | completion-basic-try-completion completion-basic-all-completions | |
370 | "Completion of the prefix before point and the suffix after point.") | |
34200787 | 371 | (partial-completion |
fcb68f70 SM |
372 | completion-pcm-try-completion completion-pcm-all-completions |
373 | "Completion of multiple words, each one taken as a prefix. | |
374 | E.g. M-x l-c-h can complete to list-command-history | |
375 | and C-x C-f /u/m/s to /usr/monnier/src.") | |
376 | (initials | |
377 | completion-initials-try-completion completion-initials-all-completions | |
378 | "Completion of acronyms and initialisms. | |
379 | E.g. can complete M-x lch to list-command-history | |
380 | and C-x C-f ~/sew to ~/src/emacs/work.")) | |
e2947429 | 381 | "List of available completion styles. |
fcb68f70 | 382 | Each element has the form (NAME TRY-COMPLETION ALL-COMPLETIONS DOC): |
26c548b0 | 383 | where NAME is the name that should be used in `completion-styles', |
fcb68f70 SM |
384 | TRY-COMPLETION is the function that does the completion (it should |
385 | follow the same calling convention as `completion-try-completion'), | |
386 | ALL-COMPLETIONS is the function that lists the completions (it should | |
387 | follow the calling convention of `completion-all-completions'), | |
388 | and DOC describes the way this style of completion works.") | |
e2947429 | 389 | |
68b113f6 | 390 | (defcustom completion-styles '(basic partial-completion emacs22) |
265d4549 SM |
391 | "List of completion styles to use. |
392 | The available styles are listed in `completion-styles-alist'." | |
e2947429 SM |
393 | :type `(repeat (choice ,@(mapcar (lambda (x) (list 'const (car x))) |
394 | completion-styles-alist))) | |
395 | :group 'minibuffer | |
396 | :version "23.1") | |
397 | ||
19c04f39 SM |
398 | (defun completion-try-completion (string table pred point) |
399 | "Try to complete STRING using completion table TABLE. | |
400 | Only the elements of table that satisfy predicate PRED are considered. | |
401 | POINT is the position of point within STRING. | |
402 | The return value can be either nil to indicate that there is no completion, | |
403 | t to indicate that STRING is the only possible completion, | |
404 | or a pair (STRING . NEWPOINT) of the completed result string together with | |
405 | a new position for point." | |
fcb68f70 SM |
406 | (completion--some (lambda (style) |
407 | (funcall (nth 1 (assq style completion-styles-alist)) | |
408 | string table pred point)) | |
409 | completion-styles)) | |
e2947429 | 410 | |
19c04f39 SM |
411 | (defun completion-all-completions (string table pred point) |
412 | "List the possible completions of STRING in completion table TABLE. | |
413 | Only the elements of table that satisfy predicate PRED are considered. | |
414 | POINT is the position of point within STRING. | |
26c548b0 | 415 | The return value is a list of completions and may contain the base-size |
19c04f39 | 416 | in the last `cdr'." |
ab22be48 SM |
417 | ;; FIXME: We need to additionally return completion-extra-size (similar |
418 | ;; to completion-base-size but for the text after point). | |
fcb68f70 SM |
419 | (completion--some (lambda (style) |
420 | (funcall (nth 2 (assq style completion-styles-alist)) | |
421 | string table pred point)) | |
422 | completion-styles)) | |
e2947429 | 423 | |
ba5ff07b SM |
424 | (defun minibuffer--bitset (modified completions exact) |
425 | (logior (if modified 4 0) | |
426 | (if completions 2 0) | |
427 | (if exact 1 0))) | |
428 | ||
3911966b | 429 | (defun completion--do-completion (&optional try-completion-function) |
32bae13c | 430 | "Do the completion and return a summary of what happened. |
ba5ff07b SM |
431 | M = completion was performed, the text was Modified. |
432 | C = there were available Completions. | |
433 | E = after completion we now have an Exact match. | |
434 | ||
435 | MCE | |
436 | 000 0 no possible completion | |
437 | 001 1 was already an exact and unique completion | |
438 | 010 2 no completion happened | |
439 | 011 3 was already an exact completion | |
440 | 100 4 ??? impossible | |
441 | 101 5 ??? impossible | |
442 | 110 6 some completion happened | |
443 | 111 7 completed to an exact completion" | |
444 | (let* ((beg (field-beginning)) | |
19c04f39 | 445 | (end (field-end)) |
3911966b | 446 | (string (buffer-substring beg end)) |
19c04f39 SM |
447 | (comp (funcall (or try-completion-function |
448 | 'completion-try-completion) | |
449 | string | |
450 | minibuffer-completion-table | |
451 | minibuffer-completion-predicate | |
452 | (- (point) beg)))) | |
32bae13c | 453 | (cond |
19c04f39 | 454 | ((null comp) |
890429cc | 455 | (minibuffer-hide-completions) |
ba5ff07b | 456 | (ding) (minibuffer-message "No match") (minibuffer--bitset nil nil nil)) |
265d4549 | 457 | ((eq t comp) |
890429cc | 458 | (minibuffer-hide-completions) |
265d4549 SM |
459 | (goto-char (field-end)) |
460 | (minibuffer--bitset nil nil t)) ;Exact and unique match. | |
32bae13c SM |
461 | (t |
462 | ;; `completed' should be t if some completion was done, which doesn't | |
463 | ;; include simply changing the case of the entered string. However, | |
464 | ;; for appearance, the string is rewritten if the case changes. | |
19c04f39 SM |
465 | (let* ((comp-pos (cdr comp)) |
466 | (completion (car comp)) | |
467 | (completed (not (eq t (compare-strings completion nil nil | |
468 | string nil nil t)))) | |
3911966b SM |
469 | (unchanged (eq t (compare-strings completion nil nil |
470 | string nil nil nil)))) | |
32bae13c | 471 | (unless unchanged |
ba5ff07b SM |
472 | |
473 | ;; Insert in minibuffer the chars we got. | |
3911966b SM |
474 | (goto-char end) |
475 | (insert completion) | |
81ff9458 SM |
476 | (delete-region beg end)) |
477 | ;; Move point. | |
478 | (goto-char (+ beg comp-pos)) | |
ba5ff07b | 479 | |
32bae13c SM |
480 | (if (not (or unchanged completed)) |
481 | ;; The case of the string changed, but that's all. We're not sure | |
482 | ;; whether this is a unique completion or not, so try again using | |
483 | ;; the real case (this shouldn't recurse again, because the next | |
484 | ;; time try-completion will return either t or the exact string). | |
3911966b | 485 | (completion--do-completion try-completion-function) |
32bae13c SM |
486 | |
487 | ;; It did find a match. Do we match some possibility exactly now? | |
19c04f39 | 488 | (let ((exact (test-completion completion |
32bae13c SM |
489 | minibuffer-completion-table |
490 | minibuffer-completion-predicate))) | |
890429cc SM |
491 | (if completed |
492 | ;; We could also decide to refresh the completions, | |
493 | ;; if they're displayed (and assuming there are | |
494 | ;; completions left). | |
495 | (minibuffer-hide-completions) | |
ba5ff07b SM |
496 | ;; Show the completion table, if requested. |
497 | (cond | |
498 | ((not exact) | |
499 | (if (case completion-auto-help | |
500 | (lazy (eq this-command last-command)) | |
501 | (t completion-auto-help)) | |
502 | (minibuffer-completion-help) | |
503 | (minibuffer-message "Next char not unique"))) | |
890429cc SM |
504 | ;; If the last exact completion and this one were the same, it |
505 | ;; means we've already given a "Next char not unique" message | |
506 | ;; and the user's hit TAB again, so now we give him help. | |
ba5ff07b SM |
507 | ((eq this-command last-command) |
508 | (if completion-auto-help (minibuffer-completion-help))))) | |
509 | ||
510 | (minibuffer--bitset completed t exact)))))))) | |
32bae13c SM |
511 | |
512 | (defun minibuffer-complete () | |
513 | "Complete the minibuffer contents as far as possible. | |
514 | Return nil if there is no valid completion, else t. | |
515 | If no characters can be completed, display a list of possible completions. | |
516 | If you repeat this command after it displayed such a list, | |
517 | scroll the window of possible completions." | |
518 | (interactive) | |
519 | ;; If the previous command was not this, | |
520 | ;; mark the completion buffer obsolete. | |
521 | (unless (eq this-command last-command) | |
522 | (setq minibuffer-scroll-window nil)) | |
523 | ||
524 | (let ((window minibuffer-scroll-window)) | |
525 | ;; If there's a fresh completion window with a live buffer, | |
526 | ;; and this command is repeated, scroll that window. | |
527 | (if (window-live-p window) | |
528 | (with-current-buffer (window-buffer window) | |
529 | (if (pos-visible-in-window-p (point-max) window) | |
530 | ;; If end is in view, scroll up to the beginning. | |
531 | (set-window-start window (point-min) nil) | |
532 | ;; Else scroll down one screen. | |
533 | (scroll-other-window)) | |
534 | nil) | |
535 | ||
3911966b | 536 | (case (completion--do-completion) |
a38313e1 | 537 | (#b000 nil) |
265d4549 | 538 | (#b001 (minibuffer-message "Sole completion") |
a38313e1 | 539 | t) |
265d4549 | 540 | (#b011 (minibuffer-message "Complete, but not unique") |
a38313e1 SM |
541 | t) |
542 | (t t))))) | |
32bae13c | 543 | |
14c24780 SM |
544 | (defvar completion-all-sorted-completions nil) |
545 | (make-variable-buffer-local 'completion-all-sorted-completions) | |
546 | ||
547 | (defun completion--flush-all-sorted-completions (&rest ignore) | |
548 | (setq completion-all-sorted-completions nil)) | |
549 | ||
550 | (defun completion-all-sorted-completions () | |
551 | (or completion-all-sorted-completions | |
552 | (let* ((start (field-beginning)) | |
553 | (end (field-end)) | |
554 | (all (completion-all-completions (buffer-substring start end) | |
555 | minibuffer-completion-table | |
556 | minibuffer-completion-predicate | |
557 | (- (point) start))) | |
558 | (last (last all)) | |
559 | (base-size (or (cdr last) 0))) | |
560 | (when last | |
561 | (setcdr last nil) | |
562 | ;; Prefer shorter completions. | |
563 | (setq all (sort all (lambda (c1 c2) (< (length c1) (length c2))))) | |
564 | ;; Prefer recently used completions. | |
565 | (let ((hist (symbol-value minibuffer-history-variable))) | |
566 | (setq all (sort all (lambda (c1 c2) | |
567 | (> (length (member c1 hist)) | |
568 | (length (member c2 hist))))))) | |
569 | ;; Cache the result. This is not just for speed, but also so that | |
570 | ;; repeated calls to minibuffer-force-complete can cycle through | |
571 | ;; all possibilities. | |
572 | (add-hook 'after-change-functions | |
573 | 'completion--flush-all-sorted-completions nil t) | |
574 | (setq completion-all-sorted-completions | |
575 | (nconc all base-size)))))) | |
576 | ||
577 | (defun minibuffer-force-complete () | |
578 | "Complete the minibuffer to an exact match. | |
579 | Repeated uses step through the possible completions." | |
580 | (interactive) | |
581 | ;; FIXME: Need to deal with the extra-size issue here as well. | |
528c56e2 SM |
582 | ;; FIXME: ~/src/emacs/t<M-TAB>/lisp/minibuffer.el completes to |
583 | ;; ~/src/emacs/trunk/ and throws away lisp/minibuffer.el. | |
14c24780 SM |
584 | (let* ((start (field-beginning)) |
585 | (end (field-end)) | |
586 | (all (completion-all-sorted-completions))) | |
587 | (if (not (consp all)) | |
588 | (minibuffer-message (if all "No more completions" "No completions")) | |
589 | (goto-char end) | |
590 | (insert (car all)) | |
591 | (delete-region (+ start (cdr (last all))) end) | |
592 | ;; If completing file names, (car all) may be a directory, so we'd now | |
593 | ;; have a new set of possible completions and might want to reset | |
594 | ;; completion-all-sorted-completions to nil, but we prefer not to, | |
595 | ;; so that repeated calls minibuffer-force-complete still cycle | |
596 | ;; through the previous possible completions. | |
075518b5 SM |
597 | (let ((last (last all))) |
598 | (setcdr last (cons (car all) (cdr last))) | |
599 | (setq completion-all-sorted-completions (cdr all)))))) | |
14c24780 | 600 | |
d1826585 | 601 | (defvar minibuffer-confirm-exit-commands |
a25c543a | 602 | '(minibuffer-complete minibuffer-complete-word PC-complete PC-complete-word) |
d1826585 MB |
603 | "A list of commands which cause an immediately following |
604 | `minibuffer-complete-and-exit' to ask for extra confirmation.") | |
605 | ||
32bae13c | 606 | (defun minibuffer-complete-and-exit () |
bec1e8a5 CY |
607 | "Exit if the minibuffer contains a valid completion. |
608 | Otherwise, try to complete the minibuffer contents. If | |
609 | completion leads to a valid completion, a repetition of this | |
610 | command will exit. | |
611 | ||
612 | If `minibuffer-completion-confirm' is `confirm', do not try to | |
613 | complete; instead, ask for confirmation and accept any input if | |
614 | confirmed. | |
615 | If `minibuffer-completion-confirm' is `confirm-after-completion', | |
616 | do not try to complete; instead, ask for confirmation if the | |
90810a8e CY |
617 | preceding minibuffer command was a member of |
618 | `minibuffer-confirm-exit-commands', and accept the input | |
619 | otherwise." | |
32bae13c | 620 | (interactive) |
3911966b SM |
621 | (let ((beg (field-beginning)) |
622 | (end (field-end))) | |
623 | (cond | |
624 | ;; Allow user to specify null string | |
625 | ((= beg end) (exit-minibuffer)) | |
626 | ((test-completion (buffer-substring beg end) | |
627 | minibuffer-completion-table | |
628 | minibuffer-completion-predicate) | |
629 | (when completion-ignore-case | |
630 | ;; Fixup case of the field, if necessary. | |
b0a5a021 | 631 | (let* ((string (buffer-substring beg end)) |
3911966b SM |
632 | (compl (try-completion |
633 | string | |
634 | minibuffer-completion-table | |
635 | minibuffer-completion-predicate))) | |
636 | (when (and (stringp compl) | |
637 | ;; If it weren't for this piece of paranoia, I'd replace | |
638 | ;; the whole thing with a call to do-completion. | |
eee6de73 SM |
639 | ;; This is important, e.g. when the current minibuffer's |
640 | ;; content is a directory which only contains a single | |
641 | ;; file, so `try-completion' actually completes to | |
642 | ;; that file. | |
3911966b | 643 | (= (length string) (length compl))) |
32bae13c SM |
644 | (goto-char end) |
645 | (insert compl) | |
3911966b SM |
646 | (delete-region beg end)))) |
647 | (exit-minibuffer)) | |
32bae13c | 648 | |
bec1e8a5 | 649 | ((eq minibuffer-completion-confirm 'confirm) |
3911966b | 650 | ;; The user is permitted to exit with an input that's rejected |
bec1e8a5 | 651 | ;; by test-completion, after confirming her choice. |
3911966b SM |
652 | (if (eq last-command this-command) |
653 | (exit-minibuffer) | |
654 | (minibuffer-message "Confirm") | |
655 | nil)) | |
32bae13c | 656 | |
bec1e8a5 CY |
657 | ((eq minibuffer-completion-confirm 'confirm-after-completion) |
658 | ;; Similar to the above, but only if trying to exit immediately | |
659 | ;; after typing TAB (this catches most minibuffer typos). | |
d1826585 | 660 | (if (memq last-command minibuffer-confirm-exit-commands) |
bec1e8a5 CY |
661 | (progn (minibuffer-message "Confirm") |
662 | nil) | |
663 | (exit-minibuffer))) | |
664 | ||
3911966b SM |
665 | (t |
666 | ;; Call do-completion, but ignore errors. | |
667 | (case (condition-case nil | |
668 | (completion--do-completion) | |
669 | (error 1)) | |
a38313e1 SM |
670 | ((#b001 #b011) (exit-minibuffer)) |
671 | (#b111 (if (not minibuffer-completion-confirm) | |
672 | (exit-minibuffer) | |
673 | (minibuffer-message "Confirm") | |
674 | nil)) | |
3911966b SM |
675 | (t nil)))))) |
676 | ||
19c04f39 SM |
677 | (defun completion--try-word-completion (string table predicate point) |
678 | (let ((comp (completion-try-completion string table predicate point))) | |
679 | (if (not (consp comp)) | |
680 | comp | |
32bae13c | 681 | |
3911966b SM |
682 | ;; If completion finds next char not unique, |
683 | ;; consider adding a space or a hyphen. | |
19c04f39 | 684 | (when (= (length string) (length (car comp))) |
1afbbf85 SM |
685 | ;; Mark the added char with the `completion-word' property, so it |
686 | ;; can be handled specially by completion styles such as | |
687 | ;; partial-completion. | |
688 | ;; We used to remove `partial-completion' from completion-styles | |
689 | ;; instead, but it was too blunt, leading to situations where SPC | |
690 | ;; was the only insertable char at point but minibuffer-complete-word | |
691 | ;; refused inserting it. | |
692 | (let ((exts (mapcar (lambda (str) (propertize str 'completion-try-word t)) | |
693 | '(" " "-"))) | |
19c04f39 SM |
694 | (before (substring string 0 point)) |
695 | (after (substring string point)) | |
696 | tem) | |
697 | (while (and exts (not (consp tem))) | |
3911966b | 698 | (setq tem (completion-try-completion |
19c04f39 SM |
699 | (concat before (pop exts) after) |
700 | table predicate (1+ point)))) | |
701 | (if (consp tem) (setq comp tem)))) | |
3911966b | 702 | |
32bae13c SM |
703 | ;; Completing a single word is actually more difficult than completing |
704 | ;; as much as possible, because we first have to find the "current | |
705 | ;; position" in `completion' in order to find the end of the word | |
706 | ;; we're completing. Normally, `string' is a prefix of `completion', | |
707 | ;; which makes it trivial to find the position, but with fancier | |
708 | ;; completion (plus env-var expansion, ...) `completion' might not | |
709 | ;; look anything like `string' at all. | |
19c04f39 SM |
710 | (let* ((comppoint (cdr comp)) |
711 | (completion (car comp)) | |
712 | (before (substring string 0 point)) | |
713 | (combined (concat before "\n" completion))) | |
714 | ;; Find in completion the longest text that was right before point. | |
715 | (when (string-match "\\(.+\\)\n.*?\\1" combined) | |
716 | (let* ((prefix (match-string 1 before)) | |
717 | ;; We used non-greedy match to make `rem' as long as possible. | |
718 | (rem (substring combined (match-end 0))) | |
719 | ;; Find in the remainder of completion the longest text | |
720 | ;; that was right after point. | |
721 | (after (substring string point)) | |
722 | (suffix (if (string-match "\\`\\(.+\\).*\n.*\\1" | |
723 | (concat after "\n" rem)) | |
724 | (match-string 1 after)))) | |
725 | ;; The general idea is to try and guess what text was inserted | |
726 | ;; at point by the completion. Problem is: if we guess wrong, | |
727 | ;; we may end up treating as "added by completion" text that was | |
728 | ;; actually painfully typed by the user. So if we then cut | |
729 | ;; after the first word, we may throw away things the | |
730 | ;; user wrote. So let's try to be as conservative as possible: | |
731 | ;; only cut after the first word, if we're reasonably sure that | |
732 | ;; our guess is correct. | |
733 | ;; Note: a quick survey on emacs-devel seemed to indicate that | |
734 | ;; nobody actually cares about the "word-at-a-time" feature of | |
735 | ;; minibuffer-complete-word, whose real raison-d'être is that it | |
736 | ;; tries to add "-" or " ". One more reason to only cut after | |
737 | ;; the first word, if we're really sure we're right. | |
738 | (when (and (or suffix (zerop (length after))) | |
739 | (string-match (concat | |
740 | ;; Make submatch 1 as small as possible | |
741 | ;; to reduce the risk of cutting | |
742 | ;; valuable text. | |
743 | ".*" (regexp-quote prefix) "\\(.*?\\)" | |
744 | (if suffix (regexp-quote suffix) "\\'")) | |
745 | completion) | |
746 | ;; The new point in `completion' should also be just | |
747 | ;; before the suffix, otherwise something more complex | |
748 | ;; is going on, and we're not sure where we are. | |
749 | (eq (match-end 1) comppoint) | |
750 | ;; (match-beginning 1)..comppoint is now the stretch | |
751 | ;; of text in `completion' that was completed at point. | |
752 | (string-match "\\W" completion (match-beginning 1)) | |
753 | ;; Is there really something to cut? | |
754 | (> comppoint (match-end 0))) | |
755 | ;; Cut after the first word. | |
756 | (let ((cutpos (match-end 0))) | |
757 | (setq completion (concat (substring completion 0 cutpos) | |
758 | (substring completion comppoint))) | |
759 | (setq comppoint cutpos))))) | |
760 | ||
761 | (cons completion comppoint))))) | |
ba5ff07b SM |
762 | |
763 | ||
764 | (defun minibuffer-complete-word () | |
765 | "Complete the minibuffer contents at most a single word. | |
766 | After one word is completed as much as possible, a space or hyphen | |
767 | is added, provided that matches some possible completion. | |
768 | Return nil if there is no valid completion, else t." | |
769 | (interactive) | |
3911966b | 770 | (case (completion--do-completion 'completion--try-word-completion) |
a38313e1 | 771 | (#b000 nil) |
265d4549 | 772 | (#b001 (minibuffer-message "Sole completion") |
a38313e1 | 773 | t) |
265d4549 | 774 | (#b011 (minibuffer-message "Complete, but not unique") |
a38313e1 SM |
775 | t) |
776 | (t t))) | |
ba5ff07b | 777 | |
890429cc SM |
778 | (defface completions-annotations '((t :inherit italic)) |
779 | "Face to use for annotations in the *Completions* buffer.") | |
780 | ||
3a9f97fa JL |
781 | (defcustom completions-format nil |
782 | "Define the appearance and sorting of completions. | |
783 | If the value is `vertical', display completions sorted vertically | |
784 | in columns in the *Completions* buffer. | |
785 | If the value is `horizontal' or nil, display completions sorted | |
786 | horizontally in alphabetical order, rather than down the screen." | |
787 | :type '(choice (const nil) (const horizontal) (const vertical)) | |
788 | :group 'minibuffer | |
789 | :version "23.2") | |
790 | ||
3911966b | 791 | (defun completion--insert-strings (strings) |
32bae13c SM |
792 | "Insert a list of STRINGS into the current buffer. |
793 | Uses columns to keep the listing readable but compact. | |
794 | It also eliminates runs of equal strings." | |
795 | (when (consp strings) | |
796 | (let* ((length (apply 'max | |
797 | (mapcar (lambda (s) | |
798 | (if (consp s) | |
e5b5b82d SM |
799 | (+ (string-width (car s)) |
800 | (string-width (cadr s))) | |
801 | (string-width s))) | |
32bae13c SM |
802 | strings))) |
803 | (window (get-buffer-window (current-buffer) 0)) | |
804 | (wwidth (if window (1- (window-width window)) 79)) | |
805 | (columns (min | |
806 | ;; At least 2 columns; at least 2 spaces between columns. | |
807 | (max 2 (/ wwidth (+ 2 length))) | |
808 | ;; Don't allocate more columns than we can fill. | |
809 | ;; Windows can't show less than 3 lines anyway. | |
810 | (max 1 (/ (length strings) 2)))) | |
811 | (colwidth (/ wwidth columns)) | |
812 | (column 0) | |
3a9f97fa JL |
813 | (rows (/ (length strings) columns)) |
814 | (row 0) | |
32bae13c SM |
815 | (laststring nil)) |
816 | ;; The insertion should be "sensible" no matter what choices were made | |
817 | ;; for the parameters above. | |
818 | (dolist (str strings) | |
f87ff539 | 819 | (unless (equal laststring str) ; Remove (consecutive) duplicates. |
32bae13c | 820 | (setq laststring str) |
f87ff539 SM |
821 | (let ((length (if (consp str) |
822 | (+ (string-width (car str)) | |
823 | (string-width (cadr str))) | |
824 | (string-width str)))) | |
3a9f97fa JL |
825 | (cond |
826 | ((eq completions-format 'vertical) | |
827 | ;; Vertical format | |
828 | (when (> row rows) | |
829 | (forward-line (- -1 rows)) | |
830 | (setq row 0 column (+ column colwidth))) | |
831 | (when (> column 0) | |
832 | (end-of-line) | |
833 | (while (> (current-column) column) | |
834 | (if (eobp) | |
835 | (insert "\n") | |
836 | (forward-line 1) | |
837 | (end-of-line))) | |
838 | (insert " \t") | |
839 | (set-text-properties (- (point) 1) (point) | |
840 | `(display (space :align-to ,column))))) | |
841 | (t | |
842 | ;; Horizontal format | |
843 | (unless (bolp) | |
844 | (if (< wwidth (+ (max colwidth length) column)) | |
845 | ;; No space for `str' at point, move to next line. | |
846 | (progn (insert "\n") (setq column 0)) | |
847 | (insert " \t") | |
848 | ;; Leave the space unpropertized so that in the case we're | |
849 | ;; already past the goal column, there is still | |
850 | ;; a space displayed. | |
851 | (set-text-properties (- (point) 1) (point) | |
852 | ;; We can't just set tab-width, because | |
853 | ;; completion-setup-function will kill all | |
854 | ;; local variables :-( | |
855 | `(display (space :align-to ,column))) | |
856 | nil)))) | |
f87ff539 SM |
857 | (if (not (consp str)) |
858 | (put-text-property (point) (progn (insert str) (point)) | |
859 | 'mouse-face 'highlight) | |
860 | (put-text-property (point) (progn (insert (car str)) (point)) | |
861 | 'mouse-face 'highlight) | |
890429cc SM |
862 | (add-text-properties (point) (progn (insert (cadr str)) (point)) |
863 | '(mouse-face nil | |
3a9f97fa JL |
864 | face completions-annotations))) |
865 | (cond | |
866 | ((eq completions-format 'vertical) | |
867 | ;; Vertical format | |
868 | (if (> column 0) | |
869 | (forward-line) | |
870 | (insert "\n")) | |
871 | (setq row (1+ row))) | |
872 | (t | |
873 | ;; Horizontal format | |
874 | ;; Next column to align to. | |
875 | (setq column (+ column | |
876 | ;; Round up to a whole number of columns. | |
877 | (* colwidth (ceiling length colwidth)))))))))))) | |
32bae13c | 878 | |
6138158d SM |
879 | (defvar completion-common-substring nil) |
880 | (make-obsolete-variable 'completion-common-substring nil "23.1") | |
32bae13c | 881 | |
21622c6d SM |
882 | (defvar completion-setup-hook nil |
883 | "Normal hook run at the end of setting up a completion list buffer. | |
884 | When this hook is run, the current buffer is the one in which the | |
885 | command to display the completion list buffer was run. | |
886 | The completion list buffer is available as the value of `standard-output'. | |
6138158d SM |
887 | See also `display-completion-list'.") |
888 | ||
889 | (defface completions-first-difference | |
890 | '((t (:inherit bold))) | |
891 | "Face put on the first uncommon character in completions in *Completions* buffer." | |
892 | :group 'completion) | |
893 | ||
894 | (defface completions-common-part | |
895 | '((t (:inherit default))) | |
896 | "Face put on the common prefix substring in completions in *Completions* buffer. | |
897 | The idea of `completions-common-part' is that you can use it to | |
898 | make the common parts less visible than normal, so that the rest | |
899 | of the differing parts is, by contrast, slightly highlighted." | |
900 | :group 'completion) | |
901 | ||
125f7951 | 902 | (defun completion-hilit-commonality (completions prefix-len base-size) |
6138158d | 903 | (when completions |
125f7951 | 904 | (let ((com-str-len (- prefix-len (or base-size 0)))) |
6138158d SM |
905 | (nconc |
906 | (mapcar | |
457d37ba SM |
907 | (lambda (elem) |
908 | (let ((str | |
909 | ;; Don't modify the string itself, but a copy, since the | |
910 | ;; the string may be read-only or used for other purposes. | |
911 | ;; Furthermore, since `completions' may come from | |
912 | ;; display-completion-list, `elem' may be a list. | |
913 | (if (consp elem) | |
914 | (car (setq elem (cons (copy-sequence (car elem)) | |
915 | (cdr elem)))) | |
916 | (setq elem (copy-sequence elem))))) | |
1bba1cfc SM |
917 | (put-text-property 0 |
918 | ;; If completion-boundaries returns incorrect | |
919 | ;; values, all-completions may return strings | |
920 | ;; that don't contain the prefix. | |
921 | (min com-str-len (length str)) | |
457d37ba SM |
922 | 'font-lock-face 'completions-common-part |
923 | str) | |
924 | (if (> (length str) com-str-len) | |
925 | (put-text-property com-str-len (1+ com-str-len) | |
926 | 'font-lock-face 'completions-first-difference | |
927 | str))) | |
928 | elem) | |
6138158d SM |
929 | completions) |
930 | base-size)))) | |
21622c6d | 931 | |
7bc7f64d | 932 | (defun display-completion-list (completions &optional common-substring) |
32bae13c SM |
933 | "Display the list of completions, COMPLETIONS, using `standard-output'. |
934 | Each element may be just a symbol or string | |
935 | or may be a list of two strings to be printed as if concatenated. | |
936 | If it is a list of two strings, the first is the actual completion | |
937 | alternative, the second serves as annotation. | |
938 | `standard-output' must be a buffer. | |
939 | The actual completion alternatives, as inserted, are given `mouse-face' | |
940 | properties of `highlight'. | |
941 | At the end, this runs the normal hook `completion-setup-hook'. | |
942 | It can find the completion buffer in `standard-output'. | |
7ce8dff2 | 943 | |
72444d02 | 944 | The obsolete optional arg COMMON-SUBSTRING, if non-nil, should be a string |
7ce8dff2 CY |
945 | specifying a common substring for adding the faces |
946 | `completions-first-difference' and `completions-common-part' to | |
7bc7f64d | 947 | the completions buffer." |
6138158d SM |
948 | (if common-substring |
949 | (setq completions (completion-hilit-commonality | |
125f7951 SM |
950 | completions (length common-substring) |
951 | ;; We don't know the base-size. | |
952 | nil))) | |
32bae13c SM |
953 | (if (not (bufferp standard-output)) |
954 | ;; This *never* (ever) happens, so there's no point trying to be clever. | |
955 | (with-temp-buffer | |
956 | (let ((standard-output (current-buffer)) | |
957 | (completion-setup-hook nil)) | |
7bc7f64d | 958 | (display-completion-list completions common-substring)) |
32bae13c SM |
959 | (princ (buffer-string))) |
960 | ||
d5e63715 SM |
961 | (with-current-buffer standard-output |
962 | (goto-char (point-max)) | |
963 | (if (null completions) | |
964 | (insert "There are no possible completions of what you have typed.") | |
965 | (insert "Possible completions are:\n") | |
966 | (completion--insert-strings completions)))) | |
e2947429 | 967 | |
6138158d SM |
968 | ;; The hilit used to be applied via completion-setup-hook, so there |
969 | ;; may still be some code that uses completion-common-substring. | |
7ce8dff2 CY |
970 | (with-no-warnings |
971 | (let ((completion-common-substring common-substring)) | |
972 | (run-hooks 'completion-setup-hook))) | |
32bae13c SM |
973 | nil) |
974 | ||
ab22be48 SM |
975 | (defvar completion-annotate-function |
976 | nil | |
977 | ;; Note: there's a lot of scope as for when to add annotations and | |
978 | ;; what annotations to add. E.g. completing-help.el allowed adding | |
979 | ;; the first line of docstrings to M-x completion. But there's | |
980 | ;; a tension, since such annotations, while useful at times, can | |
981 | ;; actually drown the useful information. | |
982 | ;; So completion-annotate-function should be used parsimoniously, or | |
983 | ;; else only used upon a user's request (e.g. we could add a command | |
984 | ;; to completion-list-mode to add annotations to the current | |
985 | ;; completions). | |
986 | "Function to add annotations in the *Completions* buffer. | |
987 | The function takes a completion and should either return nil, or a string that | |
988 | will be displayed next to the completion. The function can access the | |
989 | completion table and predicates via `minibuffer-completion-table' and related | |
990 | variables.") | |
991 | ||
32bae13c SM |
992 | (defun minibuffer-completion-help () |
993 | "Display a list of possible completions of the current minibuffer contents." | |
994 | (interactive) | |
995 | (message "Making completion list...") | |
d5e63715 SM |
996 | (let* ((start (field-beginning)) |
997 | (string (field-string)) | |
3911966b | 998 | (completions (completion-all-completions |
32bae13c SM |
999 | string |
1000 | minibuffer-completion-table | |
19c04f39 SM |
1001 | minibuffer-completion-predicate |
1002 | (- (point) (field-beginning))))) | |
32bae13c SM |
1003 | (message nil) |
1004 | (if (and completions | |
e2947429 SM |
1005 | (or (consp (cdr completions)) |
1006 | (not (equal (car completions) string)))) | |
d2c9fc42 SM |
1007 | (let* ((last (last completions)) |
1008 | (base-size (cdr last)) | |
1009 | ;; If the *Completions* buffer is shown in a new | |
1010 | ;; window, mark it as softly-dedicated, so bury-buffer in | |
1011 | ;; minibuffer-hide-completions will know whether to | |
1012 | ;; delete the window or not. | |
1013 | (display-buffer-mark-dedicated 'soft)) | |
1014 | (with-output-to-temp-buffer "*Completions*" | |
e2947429 SM |
1015 | ;; Remove the base-size tail because `sort' requires a properly |
1016 | ;; nil-terminated list. | |
1017 | (when last (setcdr last nil)) | |
ab22be48 SM |
1018 | (setq completions (sort completions 'string-lessp)) |
1019 | (when completion-annotate-function | |
1020 | (setq completions | |
1021 | (mapcar (lambda (s) | |
1022 | (let ((ann | |
1023 | (funcall completion-annotate-function s))) | |
1024 | (if ann (list s ann) s))) | |
1025 | completions))) | |
d5e63715 | 1026 | (with-current-buffer standard-output |
d2c9fc42 SM |
1027 | (set (make-local-variable 'completion-base-position) |
1028 | ;; FIXME: We should provide the END part as well, but | |
1029 | ;; currently completion-all-completions does not give | |
1030 | ;; us the necessary information. | |
1031 | (list (+ start base-size) nil))) | |
d5e63715 | 1032 | (display-completion-list completions))) |
32bae13c SM |
1033 | |
1034 | ;; If there are no completions, or if the current input is already the | |
1035 | ;; only possible completion, then hide (previous&stale) completions. | |
4d93a9e0 | 1036 | (minibuffer-hide-completions) |
32bae13c SM |
1037 | (ding) |
1038 | (minibuffer-message | |
1039 | (if completions "Sole completion" "No completions"))) | |
1040 | nil)) | |
1041 | ||
890429cc SM |
1042 | (defun minibuffer-hide-completions () |
1043 | "Get rid of an out-of-date *Completions* buffer." | |
1044 | ;; FIXME: We could/should use minibuffer-scroll-window here, but it | |
1045 | ;; can also point to the minibuffer-parent-window, so it's a bit tricky. | |
1046 | (let ((win (get-buffer-window "*Completions*" 0))) | |
1047 | (if win (with-selected-window win (bury-buffer))))) | |
1048 | ||
32bae13c SM |
1049 | (defun exit-minibuffer () |
1050 | "Terminate this minibuffer argument." | |
1051 | (interactive) | |
1052 | ;; If the command that uses this has made modifications in the minibuffer, | |
1053 | ;; we don't want them to cause deactivation of the mark in the original | |
1054 | ;; buffer. | |
1055 | ;; A better solution would be to make deactivate-mark buffer-local | |
1056 | ;; (or to turn it into a list of buffers, ...), but in the mean time, | |
1057 | ;; this should do the trick in most cases. | |
ba5ff07b | 1058 | (setq deactivate-mark nil) |
32bae13c SM |
1059 | (throw 'exit nil)) |
1060 | ||
1061 | (defun self-insert-and-exit () | |
1062 | "Terminate minibuffer input." | |
1063 | (interactive) | |
8989a920 | 1064 | (if (characterp last-command-event) |
32bae13c SM |
1065 | (call-interactively 'self-insert-command) |
1066 | (ding)) | |
1067 | (exit-minibuffer)) | |
1068 | ||
a185548b SM |
1069 | (defvar completion-in-region-functions nil |
1070 | "Wrapper hook around `complete-in-region'. | |
1071 | The functions on this special hook are called with 5 arguments: | |
1072 | NEXT-FUN START END COLLECTION PREDICATE. | |
1073 | NEXT-FUN is a function of four arguments (START END COLLECTION PREDICATE) | |
1074 | that performs the default operation. The other four argument are like | |
1075 | the ones passed to `complete-in-region'. The functions on this hook | |
1076 | are expected to perform completion on START..END using COLLECTION | |
1077 | and PREDICATE, either by calling NEXT-FUN or by doing it themselves.") | |
1078 | ||
1079 | (defun completion-in-region (start end collection &optional predicate) | |
1080 | "Complete the text between START and END using COLLECTION. | |
3e38b2bd | 1081 | Return nil if there is no valid completion, else t. |
a185548b SM |
1082 | Point needs to be somewhere between START and END." |
1083 | ;; FIXME: some callers need to setup completion-ignore-case, | |
1084 | ;; completion-ignored-extensions. The latter can be embedded in the | |
1085 | ;; completion tables, but the first cannot (actually, maybe it should). | |
1086 | (assert (<= start (point)) (<= (point) end)) | |
1087 | ;; FIXME: undisplay the *Completions* buffer once the completion is done. | |
1088 | (with-wrapper-hook | |
1089 | completion-in-region-functions (start end collection predicate) | |
1090 | (let ((minibuffer-completion-table collection) | |
1091 | (minibuffer-completion-predicate predicate) | |
1092 | (ol (make-overlay start end nil nil t))) | |
1093 | (overlay-put ol 'field 'completion) | |
1094 | (unwind-protect | |
1095 | (call-interactively 'minibuffer-complete) | |
1096 | (delete-overlay ol))))) | |
8ba31f36 | 1097 | |
a38313e1 SM |
1098 | (let ((map minibuffer-local-map)) |
1099 | (define-key map "\C-g" 'abort-recursive-edit) | |
1100 | (define-key map "\r" 'exit-minibuffer) | |
1101 | (define-key map "\n" 'exit-minibuffer)) | |
1102 | ||
1103 | (let ((map minibuffer-local-completion-map)) | |
1104 | (define-key map "\t" 'minibuffer-complete) | |
14c24780 SM |
1105 | ;; M-TAB is already abused for many other purposes, so we should find |
1106 | ;; another binding for it. | |
1107 | ;; (define-key map "\e\t" 'minibuffer-force-complete) | |
a38313e1 SM |
1108 | (define-key map " " 'minibuffer-complete-word) |
1109 | (define-key map "?" 'minibuffer-completion-help)) | |
1110 | ||
1111 | (let ((map minibuffer-local-must-match-map)) | |
1112 | (define-key map "\r" 'minibuffer-complete-and-exit) | |
1113 | (define-key map "\n" 'minibuffer-complete-and-exit)) | |
1114 | ||
1115 | (let ((map minibuffer-local-filename-completion-map)) | |
1116 | (define-key map " " nil)) | |
8ba31f36 | 1117 | (let ((map minibuffer-local-filename-must-match-map)) |
a38313e1 SM |
1118 | (define-key map " " nil)) |
1119 | ||
1120 | (let ((map minibuffer-local-ns-map)) | |
1121 | (define-key map " " 'exit-minibuffer) | |
1122 | (define-key map "\t" 'exit-minibuffer) | |
1123 | (define-key map "?" 'self-insert-and-exit)) | |
1124 | ||
1125 | ;;; Completion tables. | |
1126 | ||
34b67b0f SM |
1127 | (defun minibuffer--double-dollars (str) |
1128 | (replace-regexp-in-string "\\$" "$$" str)) | |
1129 | ||
21622c6d SM |
1130 | (defun completion--make-envvar-table () |
1131 | (mapcar (lambda (enventry) | |
9f3618b5 | 1132 | (substring enventry 0 (string-match-p "=" enventry))) |
21622c6d SM |
1133 | process-environment)) |
1134 | ||
a38313e1 SM |
1135 | (defconst completion--embedded-envvar-re |
1136 | (concat "\\(?:^\\|[^$]\\(?:\\$\\$\\)*\\)" | |
1137 | "$\\([[:alnum:]_]*\\|{\\([^}]*\\)\\)\\'")) | |
1138 | ||
21622c6d | 1139 | (defun completion--embedded-envvar-table (string pred action) |
c6432f1e SM |
1140 | "Completion table for envvars embedded in a string. |
1141 | The envvar syntax (and escaping) rules followed by this table are the | |
1142 | same as `substitute-in-file-name'." | |
1143 | ;; We ignore `pred', because the predicates passed to us via | |
1144 | ;; read-file-name-internal are not 100% correct and fail here: | |
1145 | ;; e.g. we get predicates like file-directory-p there, whereas the filename | |
1146 | ;; completed needs to be passed through substitute-in-file-name before it | |
1147 | ;; can be passed to file-directory-p. | |
528c56e2 SM |
1148 | (when (string-match completion--embedded-envvar-re string) |
1149 | (let* ((beg (or (match-beginning 2) (match-beginning 1))) | |
1150 | (table (completion--make-envvar-table)) | |
1151 | (prefix (substring string 0 beg))) | |
c6432f1e SM |
1152 | (cond |
1153 | ((eq action 'lambda) | |
1154 | ;; This table is expected to be used in conjunction with some | |
1155 | ;; other table that provides the "main" completion. Let the | |
1156 | ;; other table handle the test-completion case. | |
1157 | nil) | |
1158 | ((eq (car-safe action) 'boundaries) | |
528c56e2 SM |
1159 | ;; Only return boundaries if there's something to complete, |
1160 | ;; since otherwise when we're used in | |
1161 | ;; completion-table-in-turn, we could return boundaries and | |
1162 | ;; let some subsequent table return a list of completions. | |
1163 | ;; FIXME: Maybe it should rather be fixed in | |
1164 | ;; completion-table-in-turn instead, but it's difficult to | |
1165 | ;; do it efficiently there. | |
c6432f1e | 1166 | (when (try-completion (substring string beg) table nil) |
528c56e2 SM |
1167 | ;; Compute the boundaries of the subfield to which this |
1168 | ;; completion applies. | |
1169 | (let ((suffix (cdr action))) | |
1170 | (list* 'boundaries | |
1171 | (or (match-beginning 2) (match-beginning 1)) | |
1172 | (when (string-match "[^[:alnum:]_]" suffix) | |
c6432f1e SM |
1173 | (match-beginning 0)))))) |
1174 | (t | |
a38313e1 SM |
1175 | (if (eq (aref string (1- beg)) ?{) |
1176 | (setq table (apply-partially 'completion-table-with-terminator | |
1177 | "}" table))) | |
ab22be48 SM |
1178 | ;; Even if file-name completion is case-insensitive, we want |
1179 | ;; envvar completion to be case-sensitive. | |
1180 | (let ((completion-ignore-case nil)) | |
1181 | (completion-table-with-context | |
c6432f1e | 1182 | prefix table (substring string beg) nil action))))))) |
017c22fe | 1183 | |
528c56e2 SM |
1184 | (defun completion-file-name-table (string pred action) |
1185 | "Completion table for file names." | |
1186 | (ignore-errors | |
a38313e1 | 1187 | (cond |
a38313e1 | 1188 | ((eq (car-safe action) 'boundaries) |
f8381803 | 1189 | (let ((start (length (file-name-directory string))) |
9f3618b5 | 1190 | (end (string-match-p "/" (cdr action)))) |
a38313e1 | 1191 | (list* 'boundaries start end))) |
d9aa6b33 | 1192 | |
528c56e2 SM |
1193 | ((eq action 'lambda) |
1194 | (if (zerop (length string)) | |
1195 | nil ;Not sure why it's here, but it probably doesn't harm. | |
1196 | (funcall (or pred 'file-exists-p) string))) | |
1197 | ||
a38313e1 | 1198 | (t |
528c56e2 SM |
1199 | (let* ((name (file-name-nondirectory string)) |
1200 | (specdir (file-name-directory string)) | |
1201 | (realdir (or specdir default-directory))) | |
017c22fe | 1202 | |
34b67b0f SM |
1203 | (cond |
1204 | ((null action) | |
528c56e2 SM |
1205 | (let ((comp (file-name-completion name realdir pred))) |
1206 | (if (stringp comp) | |
1207 | (concat specdir comp) | |
1208 | comp))) | |
017c22fe | 1209 | |
34b67b0f | 1210 | ((eq action t) |
125f7951 | 1211 | (let ((all (file-name-all-completions name realdir))) |
e2947429 SM |
1212 | |
1213 | ;; Check the predicate, if necessary. | |
528c56e2 | 1214 | (unless (memq pred '(nil file-exists-p)) |
34b67b0f SM |
1215 | (let ((comp ()) |
1216 | (pred | |
528c56e2 | 1217 | (if (eq pred 'file-directory-p) |
34b67b0f SM |
1218 | ;; Brute-force speed up for directory checking: |
1219 | ;; Discard strings which don't end in a slash. | |
1220 | (lambda (s) | |
1221 | (let ((len (length s))) | |
1222 | (and (> len 0) (eq (aref s (1- len)) ?/)))) | |
1223 | ;; Must do it the hard (and slow) way. | |
528c56e2 SM |
1224 | pred))) |
1225 | (let ((default-directory (expand-file-name realdir))) | |
34b67b0f SM |
1226 | (dolist (tem all) |
1227 | (if (funcall pred tem) (push tem comp)))) | |
e2947429 SM |
1228 | (setq all (nreverse comp)))) |
1229 | ||
528c56e2 SM |
1230 | all)))))))) |
1231 | ||
1232 | (defvar read-file-name-predicate nil | |
1233 | "Current predicate used by `read-file-name-internal'.") | |
1234 | (make-obsolete-variable 'read-file-name-predicate | |
1235 | "use the regular PRED argument" "23.2") | |
1236 | ||
1237 | (defun completion--file-name-table (string pred action) | |
1238 | "Internal subroutine for `read-file-name'. Do not call this. | |
1239 | This is a completion table for file names, like `completion-file-name-table' | |
1240 | except that it passes the file name through `substitute-in-file-name'." | |
1241 | (cond | |
1242 | ((eq (car-safe action) 'boundaries) | |
1243 | ;; For the boundaries, we can't really delegate to | |
1244 | ;; completion-file-name-table and then fix them up, because it | |
1245 | ;; would require us to track the relationship between `str' and | |
1246 | ;; `string', which is difficult. And in any case, if | |
1247 | ;; substitute-in-file-name turns "fo-$TO-ba" into "fo-o/b-ba", there's | |
1248 | ;; no way for us to return proper boundaries info, because the | |
1249 | ;; boundary is not (yet) in `string'. | |
1250 | (let ((start (length (file-name-directory string))) | |
1251 | (end (string-match-p "/" (cdr action)))) | |
1252 | (list* 'boundaries start end))) | |
34b67b0f SM |
1253 | |
1254 | (t | |
528c56e2 SM |
1255 | (let* ((default-directory |
1256 | (if (stringp pred) | |
1257 | ;; It used to be that `pred' was abused to pass `dir' | |
1258 | ;; as an argument. | |
1259 | (prog1 (file-name-as-directory (expand-file-name pred)) | |
1260 | (setq pred nil)) | |
1261 | default-directory)) | |
1262 | (str (condition-case nil | |
1263 | (substitute-in-file-name string) | |
1264 | (error string))) | |
1265 | (comp (completion-file-name-table | |
1266 | str (or pred read-file-name-predicate) action))) | |
1267 | ||
1268 | (cond | |
1269 | ((stringp comp) | |
1270 | ;; Requote the $s before returning the completion. | |
1271 | (minibuffer--double-dollars comp)) | |
1272 | ((and (null action) comp | |
1273 | ;; Requote the $s before checking for changes. | |
1274 | (setq str (minibuffer--double-dollars str)) | |
1275 | (not (string-equal string str))) | |
1276 | ;; If there's no real completion, but substitute-in-file-name | |
1277 | ;; changed the string, then return the new string. | |
1278 | str) | |
1279 | (t comp)))))) | |
34b67b0f | 1280 | |
21622c6d | 1281 | (defalias 'read-file-name-internal |
017c22fe | 1282 | (completion-table-in-turn 'completion--embedded-envvar-table |
88893215 | 1283 | 'completion--file-name-table) |
21622c6d | 1284 | "Internal subroutine for `read-file-name'. Do not call this.") |
34b67b0f | 1285 | |
dbd50d4b SM |
1286 | (defvar read-file-name-function nil |
1287 | "If this is non-nil, `read-file-name' does its work by calling this function.") | |
1288 | ||
dbd50d4b | 1289 | (defcustom read-file-name-completion-ignore-case |
9f6336e8 | 1290 | (if (memq system-type '(ms-dos windows-nt darwin cygwin)) |
dbd50d4b SM |
1291 | t nil) |
1292 | "Non-nil means when reading a file name completion ignores case." | |
1293 | :group 'minibuffer | |
1294 | :type 'boolean | |
1295 | :version "22.1") | |
1296 | ||
1297 | (defcustom insert-default-directory t | |
1298 | "Non-nil means when reading a filename start with default dir in minibuffer. | |
1299 | ||
1300 | When the initial minibuffer contents show a name of a file or a directory, | |
1301 | typing RETURN without editing the initial contents is equivalent to typing | |
1302 | the default file name. | |
1303 | ||
1304 | If this variable is non-nil, the minibuffer contents are always | |
1305 | initially non-empty, and typing RETURN without editing will fetch the | |
1306 | default name, if one is provided. Note however that this default name | |
1307 | is not necessarily the same as initial contents inserted in the minibuffer, | |
1308 | if the initial contents is just the default directory. | |
1309 | ||
1310 | If this variable is nil, the minibuffer often starts out empty. In | |
1311 | that case you may have to explicitly fetch the next history element to | |
1312 | request the default name; typing RETURN without editing will leave | |
1313 | the minibuffer empty. | |
1314 | ||
1315 | For some commands, exiting with an empty minibuffer has a special meaning, | |
1316 | such as making the current buffer visit no file in the case of | |
1317 | `set-visited-file-name'." | |
1318 | :group 'minibuffer | |
1319 | :type 'boolean) | |
1320 | ||
4e3870f5 GM |
1321 | ;; Not always defined, but only called if next-read-file-uses-dialog-p says so. |
1322 | (declare-function x-file-dialog "xfns.c" | |
1323 | (prompt dir &optional default-filename mustmatch only-dir-p)) | |
1324 | ||
7d371eac JL |
1325 | (defun read-file-name-defaults (&optional dir initial) |
1326 | (let ((default | |
1327 | (cond | |
1328 | ;; With non-nil `initial', use `dir' as the first default. | |
1329 | ;; Essentially, this mean reversing the normal order of the | |
1330 | ;; current directory name and the current file name, i.e. | |
1331 | ;; 1. with normal file reading: | |
1332 | ;; 1.1. initial input is the current directory | |
1333 | ;; 1.2. the first default is the current file name | |
1334 | ;; 2. with non-nil `initial' (e.g. for `find-alternate-file'): | |
1335 | ;; 2.2. initial input is the current file name | |
1336 | ;; 2.1. the first default is the current directory | |
1337 | (initial (abbreviate-file-name dir)) | |
1338 | ;; In file buffers, try to get the current file name | |
1339 | (buffer-file-name | |
1340 | (abbreviate-file-name buffer-file-name)))) | |
1341 | (file-name-at-point | |
1342 | (run-hook-with-args-until-success 'file-name-at-point-functions))) | |
1343 | (when file-name-at-point | |
1344 | (setq default (delete-dups | |
1345 | (delete "" (delq nil (list file-name-at-point default)))))) | |
1346 | ;; Append new defaults to the end of existing `minibuffer-default'. | |
1347 | (append | |
1348 | (if (listp minibuffer-default) minibuffer-default (list minibuffer-default)) | |
1349 | (if (listp default) default (list default))))) | |
1350 | ||
dbd50d4b SM |
1351 | (defun read-file-name (prompt &optional dir default-filename mustmatch initial predicate) |
1352 | "Read file name, prompting with PROMPT and completing in directory DIR. | |
1353 | Value is not expanded---you must call `expand-file-name' yourself. | |
1354 | Default name to DEFAULT-FILENAME if user exits the minibuffer with | |
1355 | the same non-empty string that was inserted by this function. | |
1356 | (If DEFAULT-FILENAME is omitted, the visited file name is used, | |
032c3399 JL |
1357 | except that if INITIAL is specified, that combined with DIR is used. |
1358 | If DEFAULT-FILENAME is a list of file names, the first file name is used.) | |
dbd50d4b SM |
1359 | If the user exits with an empty minibuffer, this function returns |
1360 | an empty string. (This can only happen if the user erased the | |
1361 | pre-inserted contents or if `insert-default-directory' is nil.) | |
846b6eba CY |
1362 | |
1363 | Fourth arg MUSTMATCH can take the following values: | |
1364 | - nil means that the user can exit with any input. | |
1365 | - t means that the user is not allowed to exit unless | |
1366 | the input is (or completes to) an existing file. | |
1367 | - `confirm' means that the user can exit with any input, but she needs | |
1368 | to confirm her choice if the input is not an existing file. | |
1369 | - `confirm-after-completion' means that the user can exit with any | |
1370 | input, but she needs to confirm her choice if she called | |
1371 | `minibuffer-complete' right before `minibuffer-complete-and-exit' | |
1372 | and the input is not an existing file. | |
1373 | - anything else behaves like t except that typing RET does not exit if it | |
1374 | does non-null completion. | |
1375 | ||
dbd50d4b | 1376 | Fifth arg INITIAL specifies text to start with. |
846b6eba | 1377 | |
dbd50d4b SM |
1378 | If optional sixth arg PREDICATE is non-nil, possible completions and |
1379 | the resulting file name must satisfy (funcall PREDICATE NAME). | |
1380 | DIR should be an absolute directory name. It defaults to the value of | |
1381 | `default-directory'. | |
1382 | ||
846b6eba CY |
1383 | If this command was invoked with the mouse, use a graphical file |
1384 | dialog if `use-dialog-box' is non-nil, and the window system or X | |
8368c14e CY |
1385 | toolkit in use provides a file dialog box, and DIR is not a |
1386 | remote file. For graphical file dialogs, any the special values | |
1387 | of MUSTMATCH; `confirm' and `confirm-after-completion' are | |
1388 | treated as equivalent to nil. | |
dbd50d4b SM |
1389 | |
1390 | See also `read-file-name-completion-ignore-case' | |
1391 | and `read-file-name-function'." | |
1392 | (unless dir (setq dir default-directory)) | |
1393 | (unless (file-name-absolute-p dir) (setq dir (expand-file-name dir))) | |
1394 | (unless default-filename | |
1395 | (setq default-filename (if initial (expand-file-name initial dir) | |
1396 | buffer-file-name))) | |
1397 | ;; If dir starts with user's homedir, change that to ~. | |
1398 | (setq dir (abbreviate-file-name dir)) | |
1399 | ;; Likewise for default-filename. | |
e8a5fe3e | 1400 | (if default-filename |
032c3399 JL |
1401 | (setq default-filename |
1402 | (if (consp default-filename) | |
1403 | (mapcar 'abbreviate-file-name default-filename) | |
1404 | (abbreviate-file-name default-filename)))) | |
dbd50d4b SM |
1405 | (let ((insdef (cond |
1406 | ((and insert-default-directory (stringp dir)) | |
1407 | (if initial | |
1408 | (cons (minibuffer--double-dollars (concat dir initial)) | |
1409 | (length (minibuffer--double-dollars dir))) | |
1410 | (minibuffer--double-dollars dir))) | |
1411 | (initial (cons (minibuffer--double-dollars initial) 0))))) | |
1412 | ||
1413 | (if read-file-name-function | |
1414 | (funcall read-file-name-function | |
1415 | prompt dir default-filename mustmatch initial predicate) | |
e8a5fe3e | 1416 | (let ((completion-ignore-case read-file-name-completion-ignore-case) |
dbd50d4b | 1417 | (minibuffer-completing-file-name t) |
528c56e2 | 1418 | (pred (or predicate 'file-exists-p)) |
dbd50d4b SM |
1419 | (add-to-history nil)) |
1420 | ||
1421 | (let* ((val | |
8368c14e CY |
1422 | (if (or (not (next-read-file-uses-dialog-p)) |
1423 | ;; Graphical file dialogs can't handle remote | |
1424 | ;; files (Bug#99). | |
1425 | (file-remote-p dir)) | |
e8a5fe3e SM |
1426 | ;; We used to pass `dir' to `read-file-name-internal' by |
1427 | ;; abusing the `predicate' argument. It's better to | |
1428 | ;; just use `default-directory', but in order to avoid | |
1429 | ;; changing `default-directory' in the current buffer, | |
1430 | ;; we don't let-bind it. | |
1431 | (lexical-let ((dir (file-name-as-directory | |
1432 | (expand-file-name dir)))) | |
1433 | (minibuffer-with-setup-hook | |
7d371eac JL |
1434 | (lambda () |
1435 | (setq default-directory dir) | |
1436 | ;; When the first default in `minibuffer-default' | |
1437 | ;; duplicates initial input `insdef', | |
1438 | ;; reset `minibuffer-default' to nil. | |
1439 | (when (equal (or (car-safe insdef) insdef) | |
1440 | (or (car-safe minibuffer-default) | |
1441 | minibuffer-default)) | |
1442 | (setq minibuffer-default | |
1443 | (cdr-safe minibuffer-default))) | |
1444 | ;; On the first request on `M-n' fill | |
1445 | ;; `minibuffer-default' with a list of defaults | |
1446 | ;; relevant for file-name reading. | |
1447 | (set (make-local-variable 'minibuffer-default-add-function) | |
1448 | (lambda () | |
1449 | (with-current-buffer | |
1450 | (window-buffer (minibuffer-selected-window)) | |
1451 | (read-file-name-defaults dir initial))))) | |
e8a5fe3e | 1452 | (completing-read prompt 'read-file-name-internal |
528c56e2 SM |
1453 | pred mustmatch insdef |
1454 | 'file-name-history default-filename))) | |
6462af0d JR |
1455 | ;; If DEFAULT-FILENAME not supplied and DIR contains |
1456 | ;; a file name, split it. | |
2aafe808 JR |
1457 | (let ((file (file-name-nondirectory dir)) |
1458 | ;; When using a dialog, revert to nil and non-nil | |
1459 | ;; interpretation of mustmatch. confirm options | |
1460 | ;; need to be interpreted as nil, otherwise | |
1461 | ;; it is impossible to create new files using | |
1462 | ;; dialogs with the default settings. | |
1463 | (dialog-mustmatch | |
528c56e2 SM |
1464 | (not (memq mustmatch |
1465 | '(nil confirm confirm-after-completion))))) | |
6462af0d JR |
1466 | (when (and (not default-filename) |
1467 | (not (zerop (length file)))) | |
dbd50d4b SM |
1468 | (setq default-filename file) |
1469 | (setq dir (file-name-directory dir))) | |
032c3399 JL |
1470 | (when default-filename |
1471 | (setq default-filename | |
1472 | (expand-file-name (if (consp default-filename) | |
1473 | (car default-filename) | |
1474 | default-filename) | |
1475 | dir))) | |
dbd50d4b | 1476 | (setq add-to-history t) |
2aafe808 JR |
1477 | (x-file-dialog prompt dir default-filename |
1478 | dialog-mustmatch | |
dbd50d4b SM |
1479 | (eq predicate 'file-directory-p))))) |
1480 | ||
1481 | (replace-in-history (eq (car-safe file-name-history) val))) | |
1482 | ;; If completing-read returned the inserted default string itself | |
1483 | ;; (rather than a new string with the same contents), | |
1484 | ;; it has to mean that the user typed RET with the minibuffer empty. | |
1485 | ;; In that case, we really want to return "" | |
1486 | ;; so that commands such as set-visited-file-name can distinguish. | |
032c3399 JL |
1487 | (when (consp default-filename) |
1488 | (setq default-filename (car default-filename))) | |
dbd50d4b SM |
1489 | (when (eq val default-filename) |
1490 | ;; In this case, completing-read has not added an element | |
1491 | ;; to the history. Maybe we should. | |
1492 | (if (not replace-in-history) | |
1493 | (setq add-to-history t)) | |
1494 | (setq val "")) | |
1495 | (unless val (error "No file name specified")) | |
1496 | ||
1497 | (if (and default-filename | |
1498 | (string-equal val (if (consp insdef) (car insdef) insdef))) | |
1499 | (setq val default-filename)) | |
1500 | (setq val (substitute-in-file-name val)) | |
1501 | ||
1502 | (if replace-in-history | |
1503 | ;; Replace what Fcompleting_read added to the history | |
7346a407 CY |
1504 | ;; with what we will actually return. As an exception, |
1505 | ;; if that's the same as the second item in | |
1506 | ;; file-name-history, it's really a repeat (Bug#4657). | |
dbd50d4b SM |
1507 | (let ((val1 (minibuffer--double-dollars val))) |
1508 | (if history-delete-duplicates | |
1509 | (setcdr file-name-history | |
1510 | (delete val1 (cdr file-name-history)))) | |
7346a407 CY |
1511 | (if (string= val1 (cadr file-name-history)) |
1512 | (pop file-name-history) | |
1513 | (setcar file-name-history val1))) | |
dbd50d4b SM |
1514 | (if add-to-history |
1515 | ;; Add the value to the history--but not if it matches | |
1516 | ;; the last value already there. | |
1517 | (let ((val1 (minibuffer--double-dollars val))) | |
1518 | (unless (and (consp file-name-history) | |
1519 | (equal (car file-name-history) val1)) | |
1520 | (setq file-name-history | |
1521 | (cons val1 | |
1522 | (if history-delete-duplicates | |
1523 | (delete val1 file-name-history) | |
1524 | file-name-history))))))) | |
1525 | val))))) | |
1526 | ||
8b04c0ae JL |
1527 | (defun internal-complete-buffer-except (&optional buffer) |
1528 | "Perform completion on all buffers excluding BUFFER. | |
e35b3063 | 1529 | BUFFER nil or omitted means use the current buffer. |
8b04c0ae JL |
1530 | Like `internal-complete-buffer', but removes BUFFER from the completion list." |
1531 | (lexical-let ((except (if (stringp buffer) buffer (buffer-name buffer)))) | |
1532 | (apply-partially 'completion-table-with-predicate | |
1533 | 'internal-complete-buffer | |
1534 | (lambda (name) | |
1535 | (not (equal (if (consp name) (car name) name) except))) | |
1536 | nil))) | |
1537 | ||
eee6de73 | 1538 | ;;; Old-style completion, used in Emacs-21 and Emacs-22. |
19c04f39 SM |
1539 | |
1540 | (defun completion-emacs21-try-completion (string table pred point) | |
1541 | (let ((completion (try-completion string table pred))) | |
1542 | (if (stringp completion) | |
1543 | (cons completion (length completion)) | |
1544 | completion))) | |
1545 | ||
1546 | (defun completion-emacs21-all-completions (string table pred point) | |
6138158d | 1547 | (completion-hilit-commonality |
eee6de73 | 1548 | (all-completions string table pred) |
125f7951 SM |
1549 | (length string) |
1550 | (car (completion-boundaries string table pred "")))) | |
19c04f39 | 1551 | |
19c04f39 SM |
1552 | (defun completion-emacs22-try-completion (string table pred point) |
1553 | (let ((suffix (substring string point)) | |
1554 | (completion (try-completion (substring string 0 point) table pred))) | |
1555 | (if (not (stringp completion)) | |
1556 | completion | |
1557 | ;; Merge a trailing / in completion with a / after point. | |
1558 | ;; We used to only do it for word completion, but it seems to make | |
1559 | ;; sense for all completions. | |
34200787 SM |
1560 | ;; Actually, claiming this feature was part of Emacs-22 completion |
1561 | ;; is pushing it a bit: it was only done in minibuffer-completion-word, | |
1562 | ;; which was (by default) not bound during file completion, where such | |
1563 | ;; slashes are most likely to occur. | |
1564 | (if (and (not (zerop (length completion))) | |
1565 | (eq ?/ (aref completion (1- (length completion)))) | |
19c04f39 SM |
1566 | (not (zerop (length suffix))) |
1567 | (eq ?/ (aref suffix 0))) | |
34200787 SM |
1568 | ;; This leaves point after the / . |
1569 | (setq suffix (substring suffix 1))) | |
19c04f39 SM |
1570 | (cons (concat completion suffix) (length completion))))) |
1571 | ||
1572 | (defun completion-emacs22-all-completions (string table pred point) | |
125f7951 SM |
1573 | (let ((beforepoint (substring string 0 point))) |
1574 | (completion-hilit-commonality | |
1575 | (all-completions beforepoint table pred) | |
1576 | point | |
1577 | (car (completion-boundaries beforepoint table pred ""))))) | |
19c04f39 | 1578 | |
eee6de73 SM |
1579 | ;;; Basic completion. |
1580 | ||
1581 | (defun completion--merge-suffix (completion point suffix) | |
1582 | "Merge end of COMPLETION with beginning of SUFFIX. | |
1583 | Simple generalization of the \"merge trailing /\" done in Emacs-22. | |
1584 | Return the new suffix." | |
1585 | (if (and (not (zerop (length suffix))) | |
1586 | (string-match "\\(.+\\)\n\\1" (concat completion "\n" suffix) | |
1587 | ;; Make sure we don't compress things to less | |
1588 | ;; than we started with. | |
1589 | point) | |
1590 | ;; Just make sure we didn't match some other \n. | |
1591 | (eq (match-end 1) (length completion))) | |
1592 | (substring suffix (- (match-end 1) (match-beginning 1))) | |
1593 | ;; Nothing to merge. | |
1594 | suffix)) | |
1595 | ||
34200787 | 1596 | (defun completion-basic-try-completion (string table pred point) |
eee6de73 SM |
1597 | (let* ((beforepoint (substring string 0 point)) |
1598 | (afterpoint (substring string point)) | |
86011bf2 SM |
1599 | (bounds (completion-boundaries beforepoint table pred afterpoint))) |
1600 | (if (zerop (cdr bounds)) | |
1601 | ;; `try-completion' may return a subtly different result | |
1602 | ;; than `all+merge', so try to use it whenever possible. | |
1603 | (let ((completion (try-completion beforepoint table pred))) | |
1604 | (if (not (stringp completion)) | |
1605 | completion | |
1606 | (cons | |
1607 | (concat completion | |
1608 | (completion--merge-suffix completion point afterpoint)) | |
1609 | (length completion)))) | |
1610 | (let* ((suffix (substring afterpoint (cdr bounds))) | |
1611 | (prefix (substring beforepoint 0 (car bounds))) | |
1612 | (pattern (delete | |
1613 | "" (list (substring beforepoint (car bounds)) | |
1614 | 'point | |
1615 | (substring afterpoint 0 (cdr bounds))))) | |
1616 | (all (completion-pcm--all-completions prefix pattern table pred))) | |
1617 | (if minibuffer-completing-file-name | |
1618 | (setq all (completion-pcm--filename-try-filter all))) | |
1619 | (completion-pcm--merge-try pattern all prefix suffix))))) | |
1620 | ||
1621 | (defun completion-basic-all-completions (string table pred point) | |
1622 | (let* ((beforepoint (substring string 0 point)) | |
1623 | (afterpoint (substring string point)) | |
1624 | (bounds (completion-boundaries beforepoint table pred afterpoint)) | |
1625 | (suffix (substring afterpoint (cdr bounds))) | |
1626 | (prefix (substring beforepoint 0 (car bounds))) | |
1627 | (pattern (delete | |
1628 | "" (list (substring beforepoint (car bounds)) | |
1629 | 'point | |
1630 | (substring afterpoint 0 (cdr bounds))))) | |
1631 | (all (completion-pcm--all-completions prefix pattern table pred))) | |
125f7951 | 1632 | (completion-hilit-commonality all point (car bounds)))) |
19c04f39 | 1633 | |
34200787 SM |
1634 | ;;; Partial-completion-mode style completion. |
1635 | ||
890429cc SM |
1636 | (defvar completion-pcm--delim-wild-regex nil |
1637 | "Regular expression matching delimiters controlling the partial-completion. | |
1638 | Typically, this regular expression simply matches a delimiter, meaning | |
1639 | that completion can add something at (match-beginning 0), but if it has | |
1640 | a submatch 1, then completion can add something at (match-end 1). | |
1641 | This is used when the delimiter needs to be of size zero (e.g. the transition | |
1642 | from lowercase to uppercase characters).") | |
34200787 SM |
1643 | |
1644 | (defun completion-pcm--prepare-delim-re (delims) | |
1645 | (setq completion-pcm--delim-wild-regex (concat "[" delims "*]"))) | |
1646 | ||
1647 | (defcustom completion-pcm-word-delimiters "-_. " | |
1648 | "A string of characters treated as word delimiters for completion. | |
1649 | Some arcane rules: | |
1650 | If `]' is in this string, it must come first. | |
1651 | If `^' is in this string, it must not come first. | |
1652 | If `-' is in this string, it must come first or right after `]'. | |
1653 | In other words, if S is this string, then `[S]' must be a valid Emacs regular | |
1654 | expression (not containing character ranges like `a-z')." | |
1655 | :set (lambda (symbol value) | |
1656 | (set-default symbol value) | |
1657 | ;; Refresh other vars. | |
1658 | (completion-pcm--prepare-delim-re value)) | |
1659 | :initialize 'custom-initialize-reset | |
26c548b0 | 1660 | :group 'minibuffer |
34200787 SM |
1661 | :type 'string) |
1662 | ||
1663 | (defun completion-pcm--pattern-trivial-p (pattern) | |
1bba1cfc SM |
1664 | (and (stringp (car pattern)) |
1665 | ;; It can be followed by `point' and "" and still be trivial. | |
1666 | (let ((trivial t)) | |
1667 | (dolist (elem (cdr pattern)) | |
1668 | (unless (member elem '(point "")) | |
1669 | (setq trivial nil))) | |
1670 | trivial))) | |
34200787 | 1671 | |
a38313e1 SM |
1672 | (defun completion-pcm--string->pattern (string &optional point) |
1673 | "Split STRING into a pattern. | |
34200787 SM |
1674 | A pattern is a list where each element is either a string |
1675 | or a symbol chosen among `any', `star', `point'." | |
a38313e1 SM |
1676 | (if (and point (< point (length string))) |
1677 | (let ((prefix (substring string 0 point)) | |
1678 | (suffix (substring string point))) | |
34200787 SM |
1679 | (append (completion-pcm--string->pattern prefix) |
1680 | '(point) | |
1681 | (completion-pcm--string->pattern suffix))) | |
1682 | (let ((pattern nil) | |
1683 | (p 0) | |
1684 | (p0 0)) | |
26c548b0 | 1685 | |
890429cc SM |
1686 | (while (and (setq p (string-match completion-pcm--delim-wild-regex |
1687 | string p)) | |
1afbbf85 SM |
1688 | ;; If the char was added by minibuffer-complete-word, then |
1689 | ;; don't treat it as a delimiter, otherwise "M-x SPC" | |
1690 | ;; ends up inserting a "-" rather than listing | |
1691 | ;; all completions. | |
1692 | (not (get-text-property p 'completion-try-word string))) | |
890429cc SM |
1693 | ;; Usually, completion-pcm--delim-wild-regex matches a delimiter, |
1694 | ;; meaning that something can be added *before* it, but it can also | |
1695 | ;; match a prefix and postfix, in which case something can be added | |
1696 | ;; in-between (e.g. match [[:lower:]][[:upper:]]). | |
1697 | ;; This is determined by the presence of a submatch-1 which delimits | |
1698 | ;; the prefix. | |
1699 | (if (match-end 1) (setq p (match-end 1))) | |
a38313e1 SM |
1700 | (push (substring string p0 p) pattern) |
1701 | (if (eq (aref string p) ?*) | |
34200787 SM |
1702 | (progn |
1703 | (push 'star pattern) | |
1704 | (setq p0 (1+ p))) | |
1705 | (push 'any pattern) | |
1706 | (setq p0 p)) | |
1707 | (incf p)) | |
1708 | ||
1709 | ;; An empty string might be erroneously added at the beginning. | |
1710 | ;; It should be avoided properly, but it's so easy to remove it here. | |
a38313e1 | 1711 | (delete "" (nreverse (cons (substring string p0) pattern)))))) |
34200787 SM |
1712 | |
1713 | (defun completion-pcm--pattern->regex (pattern &optional group) | |
a38313e1 | 1714 | (let ((re |
ab22be48 SM |
1715 | (concat "\\`" |
1716 | (mapconcat | |
1717 | (lambda (x) | |
1718 | (case x | |
15c72e1d SM |
1719 | ((star any point) |
1720 | (if (if (consp group) (memq x group) group) | |
ab22be48 SM |
1721 | "\\(.*?\\)" ".*?")) |
1722 | (t (regexp-quote x)))) | |
1723 | pattern | |
15c72e1d | 1724 | "")))) |
a38313e1 SM |
1725 | ;; Avoid pathological backtracking. |
1726 | (while (string-match "\\.\\*\\?\\(?:\\\\[()]\\)*\\(\\.\\*\\?\\)" re) | |
1727 | (setq re (replace-match "" t t re 1))) | |
1728 | re)) | |
34200787 | 1729 | |
a38313e1 | 1730 | (defun completion-pcm--all-completions (prefix pattern table pred) |
34200787 | 1731 | "Find all completions for PATTERN in TABLE obeying PRED. |
26c548b0 | 1732 | PATTERN is as returned by `completion-pcm--string->pattern'." |
125f7951 SM |
1733 | ;; (assert (= (car (completion-boundaries prefix table pred "")) |
1734 | ;; (length prefix))) | |
34200787 SM |
1735 | ;; Find an initial list of possible completions. |
1736 | (if (completion-pcm--pattern-trivial-p pattern) | |
1737 | ||
1738 | ;; Minibuffer contains no delimiters -- simple case! | |
125f7951 | 1739 | (all-completions (concat prefix (car pattern)) table pred) |
26c548b0 | 1740 | |
34200787 SM |
1741 | ;; Use all-completions to do an initial cull. This is a big win, |
1742 | ;; since all-completions is written in C! | |
1743 | (let* (;; Convert search pattern to a standard regular expression. | |
1744 | (regex (completion-pcm--pattern->regex pattern)) | |
15c72e1d SM |
1745 | (case-fold-search completion-ignore-case) |
1746 | (completion-regexp-list (cons regex completion-regexp-list)) | |
34200787 | 1747 | (compl (all-completions |
a38313e1 | 1748 | (concat prefix (if (stringp (car pattern)) (car pattern) "")) |
125f7951 | 1749 | table pred))) |
34200787 SM |
1750 | (if (not (functionp table)) |
1751 | ;; The internal functions already obeyed completion-regexp-list. | |
1752 | compl | |
15c72e1d | 1753 | (let ((poss ())) |
34200787 | 1754 | (dolist (c compl) |
9f3618b5 | 1755 | (when (string-match-p regex c) (push c poss))) |
34200787 SM |
1756 | poss))))) |
1757 | ||
7372b09c SM |
1758 | (defun completion-pcm--hilit-commonality (pattern completions) |
1759 | (when completions | |
1760 | (let* ((re (completion-pcm--pattern->regex pattern '(point))) | |
1bba1cfc | 1761 | (case-fold-search completion-ignore-case)) |
7372b09c | 1762 | ;; Remove base-size during mapcar, and add it back later. |
1bba1cfc SM |
1763 | (mapcar |
1764 | (lambda (str) | |
1765 | ;; Don't modify the string itself. | |
1766 | (setq str (copy-sequence str)) | |
1767 | (unless (string-match re str) | |
1768 | (error "Internal error: %s does not match %s" re str)) | |
1769 | (let ((pos (or (match-beginning 1) (match-end 0)))) | |
1770 | (put-text-property 0 pos | |
1771 | 'font-lock-face 'completions-common-part | |
1772 | str) | |
1773 | (if (> (length str) pos) | |
1774 | (put-text-property pos (1+ pos) | |
1775 | 'font-lock-face 'completions-first-difference | |
1776 | str))) | |
1777 | str) | |
1778 | completions)))) | |
7372b09c | 1779 | |
eee6de73 SM |
1780 | (defun completion-pcm--find-all-completions (string table pred point |
1781 | &optional filter) | |
1782 | "Find all completions for STRING at POINT in TABLE, satisfying PRED. | |
1783 | POINT is a position inside STRING. | |
1784 | FILTER is a function applied to the return value, that can be used, e.g. to | |
1785 | filter out additional entries (because TABLE migth not obey PRED)." | |
1786 | (unless filter (setq filter 'identity)) | |
f8381803 SM |
1787 | (let* ((beforepoint (substring string 0 point)) |
1788 | (afterpoint (substring string point)) | |
1789 | (bounds (completion-boundaries beforepoint table pred afterpoint)) | |
1790 | (prefix (substring beforepoint 0 (car bounds))) | |
1791 | (suffix (substring afterpoint (cdr bounds))) | |
a38313e1 | 1792 | firsterror) |
f8381803 SM |
1793 | (setq string (substring string (car bounds) (+ point (cdr bounds)))) |
1794 | (let* ((relpoint (- point (car bounds))) | |
1795 | (pattern (completion-pcm--string->pattern string relpoint)) | |
a38313e1 | 1796 | (all (condition-case err |
eee6de73 SM |
1797 | (funcall filter |
1798 | (completion-pcm--all-completions | |
1799 | prefix pattern table pred)) | |
a38313e1 SM |
1800 | (error (unless firsterror (setq firsterror err)) nil)))) |
1801 | (when (and (null all) | |
1802 | (> (car bounds) 0) | |
1803 | (null (ignore-errors (try-completion prefix table pred)))) | |
1804 | ;; The prefix has no completions at all, so we should try and fix | |
1805 | ;; that first. | |
1806 | (let ((substring (substring prefix 0 -1))) | |
1807 | (destructuring-bind (subpat suball subprefix subsuffix) | |
1808 | (completion-pcm--find-all-completions | |
eee6de73 | 1809 | substring table pred (length substring) filter) |
a38313e1 SM |
1810 | (let ((sep (aref prefix (1- (length prefix)))) |
1811 | ;; Text that goes between the new submatches and the | |
1812 | ;; completion substring. | |
1813 | (between nil)) | |
1814 | ;; Eliminate submatches that don't end with the separator. | |
1815 | (dolist (submatch (prog1 suball (setq suball ()))) | |
1816 | (when (eq sep (aref submatch (1- (length submatch)))) | |
1817 | (push submatch suball))) | |
1818 | (when suball | |
1819 | ;; Update the boundaries and corresponding pattern. | |
1820 | ;; We assume that all submatches result in the same boundaries | |
1821 | ;; since we wouldn't know how to merge them otherwise anyway. | |
f8381803 SM |
1822 | ;; FIXME: COMPLETE REWRITE!!! |
1823 | (let* ((newbeforepoint | |
1824 | (concat subprefix (car suball) | |
1825 | (substring string 0 relpoint))) | |
1826 | (leftbound (+ (length subprefix) (length (car suball)))) | |
a38313e1 | 1827 | (newbounds (completion-boundaries |
f8381803 SM |
1828 | newbeforepoint table pred afterpoint))) |
1829 | (unless (or (and (eq (cdr bounds) (cdr newbounds)) | |
1830 | (eq (car newbounds) leftbound)) | |
a38313e1 SM |
1831 | ;; Refuse new boundaries if they step over |
1832 | ;; the submatch. | |
f8381803 | 1833 | (< (car newbounds) leftbound)) |
a38313e1 SM |
1834 | ;; The new completed prefix does change the boundaries |
1835 | ;; of the completed substring. | |
f8381803 SM |
1836 | (setq suffix (substring afterpoint (cdr newbounds))) |
1837 | (setq string | |
1838 | (concat (substring newbeforepoint (car newbounds)) | |
1839 | (substring afterpoint 0 (cdr newbounds)))) | |
1840 | (setq between (substring newbeforepoint leftbound | |
a38313e1 SM |
1841 | (car newbounds))) |
1842 | (setq pattern (completion-pcm--string->pattern | |
f8381803 SM |
1843 | string |
1844 | (- (length newbeforepoint) | |
1845 | (car newbounds))))) | |
a38313e1 SM |
1846 | (dolist (submatch suball) |
1847 | (setq all (nconc (mapcar | |
1848 | (lambda (s) (concat submatch between s)) | |
eee6de73 SM |
1849 | (funcall filter |
1850 | (completion-pcm--all-completions | |
1851 | (concat subprefix submatch between) | |
1852 | pattern table pred))) | |
a38313e1 | 1853 | all))) |
c63028e1 SM |
1854 | ;; FIXME: This can come in handy for try-completion, |
1855 | ;; but isn't right for all-completions, since it lists | |
1856 | ;; invalid completions. | |
1857 | ;; (unless all | |
1858 | ;; ;; Even though we found expansions in the prefix, none | |
1859 | ;; ;; leads to a valid completion. | |
1860 | ;; ;; Let's keep the expansions, tho. | |
1861 | ;; (dolist (submatch suball) | |
1862 | ;; (push (concat submatch between newsubstring) all))) | |
1863 | )) | |
a38313e1 SM |
1864 | (setq pattern (append subpat (list 'any (string sep)) |
1865 | (if between (list between)) pattern)) | |
1866 | (setq prefix subprefix))))) | |
1867 | (if (and (null all) firsterror) | |
1868 | (signal (car firsterror) (cdr firsterror)) | |
1869 | (list pattern all prefix suffix))))) | |
1870 | ||
34200787 | 1871 | (defun completion-pcm-all-completions (string table pred point) |
a38313e1 SM |
1872 | (destructuring-bind (pattern all &optional prefix suffix) |
1873 | (completion-pcm--find-all-completions string table pred point) | |
d4e88786 SM |
1874 | (when all |
1875 | (nconc (completion-pcm--hilit-commonality pattern all) | |
1876 | (length prefix))))) | |
34200787 SM |
1877 | |
1878 | (defun completion-pcm--merge-completions (strs pattern) | |
1879 | "Extract the commonality in STRS, with the help of PATTERN." | |
681e0e7c SM |
1880 | ;; When completing while ignoring case, we want to try and avoid |
1881 | ;; completing "fo" to "foO" when completing against "FOO" (bug#4219). | |
1882 | ;; So we try and make sure that the string we return is all made up | |
1883 | ;; of text from the completions rather than part from the | |
1884 | ;; completions and part from the input. | |
1885 | ;; FIXME: This reduces the problems of inconsistent capitalization | |
1886 | ;; but it doesn't fully fix it: we may still end up completing | |
1887 | ;; "fo-ba" to "foo-BAR" or "FOO-bar" when completing against | |
1888 | ;; '("foo-barr" "FOO-BARD"). | |
34200787 SM |
1889 | (cond |
1890 | ((null (cdr strs)) (list (car strs))) | |
1891 | (t | |
1892 | (let ((re (completion-pcm--pattern->regex pattern 'group)) | |
1893 | (ccs ())) ;Chopped completions. | |
1894 | ||
1895 | ;; First chop each string into the parts corresponding to each | |
1896 | ;; non-constant element of `pattern', using regexp-matching. | |
1897 | (let ((case-fold-search completion-ignore-case)) | |
1898 | (dolist (str strs) | |
1899 | (unless (string-match re str) | |
1900 | (error "Internal error: %s doesn't match %s" str re)) | |
1901 | (let ((chopped ()) | |
681e0e7c SM |
1902 | (last 0) |
1903 | (i 1) | |
1904 | next) | |
1905 | (while (setq next (match-end i)) | |
1906 | (push (substring str last next) chopped) | |
1907 | (setq last next) | |
34200787 SM |
1908 | (setq i (1+ i))) |
1909 | ;; Add the text corresponding to the implicit trailing `any'. | |
681e0e7c | 1910 | (push (substring str last) chopped) |
34200787 SM |
1911 | (push (nreverse chopped) ccs)))) |
1912 | ||
1913 | ;; Then for each of those non-constant elements, extract the | |
1914 | ;; commonality between them. | |
681e0e7c SM |
1915 | (let ((res ()) |
1916 | (fixed "")) | |
1917 | ;; Make the implicit trailing `any' explicit. | |
34200787 SM |
1918 | (dolist (elem (append pattern '(any))) |
1919 | (if (stringp elem) | |
681e0e7c | 1920 | (setq fixed (concat fixed elem)) |
34200787 SM |
1921 | (let ((comps ())) |
1922 | (dolist (cc (prog1 ccs (setq ccs nil))) | |
1923 | (push (car cc) comps) | |
1924 | (push (cdr cc) ccs)) | |
681e0e7c SM |
1925 | ;; Might improve the likelihood to avoid choosing |
1926 | ;; different capitalizations in different parts. | |
1927 | ;; In practice, it doesn't seem to make any difference. | |
1928 | (setq ccs (nreverse ccs)) | |
1929 | (let* ((prefix (try-completion fixed comps)) | |
1930 | (unique (or (and (eq prefix t) (setq prefix fixed)) | |
34200787 SM |
1931 | (eq t (try-completion prefix comps))))) |
1932 | (unless (equal prefix "") (push prefix res)) | |
1933 | ;; If there's only one completion, `elem' is not useful | |
1934 | ;; any more: it can only match the empty string. | |
1935 | ;; FIXME: in some cases, it may be necessary to turn an | |
1936 | ;; `any' into a `star' because the surrounding context has | |
1937 | ;; changed such that string->pattern wouldn't add an `any' | |
1938 | ;; here any more. | |
681e0e7c SM |
1939 | (unless unique (push elem res)) |
1940 | (setq fixed ""))))) | |
34200787 SM |
1941 | ;; We return it in reverse order. |
1942 | res))))) | |
1943 | ||
1944 | (defun completion-pcm--pattern->string (pattern) | |
1945 | (mapconcat (lambda (x) (cond | |
1946 | ((stringp x) x) | |
1947 | ((eq x 'star) "*") | |
1948 | ((eq x 'any) "") | |
1949 | ((eq x 'point) ""))) | |
1950 | pattern | |
1951 | "")) | |
1952 | ||
eee6de73 SM |
1953 | ;; We want to provide the functionality of `try', but we use `all' |
1954 | ;; and then merge it. In most cases, this works perfectly, but | |
1955 | ;; if the completion table doesn't consider the same completions in | |
1956 | ;; `try' as in `all', then we have a problem. The most common such | |
1957 | ;; case is for filename completion where completion-ignored-extensions | |
1958 | ;; is only obeyed by the `try' code. We paper over the difference | |
1959 | ;; here. Note that it is not quite right either: if the completion | |
1960 | ;; table uses completion-table-in-turn, this filtering may take place | |
1961 | ;; too late to correctly fallback from the first to the | |
1962 | ;; second alternative. | |
1963 | (defun completion-pcm--filename-try-filter (all) | |
1964 | "Filter to adjust `all' file completion to the behavior of `try'." | |
34200787 | 1965 | (when all |
eee6de73 SM |
1966 | (let ((try ()) |
1967 | (re (concat "\\(?:\\`\\.\\.?/\\|" | |
1968 | (regexp-opt completion-ignored-extensions) | |
1969 | "\\)\\'"))) | |
1970 | (dolist (f all) | |
9f3618b5 | 1971 | (unless (string-match-p re f) (push f try))) |
eee6de73 | 1972 | (or try all)))) |
9f3618b5 | 1973 | |
eee6de73 SM |
1974 | |
1975 | (defun completion-pcm--merge-try (pattern all prefix suffix) | |
1976 | (cond | |
1977 | ((not (consp all)) all) | |
1978 | ((and (not (consp (cdr all))) ;Only one completion. | |
1979 | ;; Ignore completion-ignore-case here. | |
1980 | (equal (completion-pcm--pattern->string pattern) (car all))) | |
1981 | t) | |
1982 | (t | |
34200787 | 1983 | (let* ((mergedpat (completion-pcm--merge-completions all pattern)) |
81ff9458 SM |
1984 | ;; `mergedpat' is in reverse order. Place new point (by |
1985 | ;; order of preference) either at the old point, or at | |
1986 | ;; the last place where there's something to choose, or | |
1987 | ;; at the very end. | |
25b54627 SM |
1988 | (pointpat (or (memq 'point mergedpat) |
1989 | (memq 'any mergedpat) | |
1990 | (memq 'star mergedpat) | |
b00942d0 | 1991 | mergedpat)) |
81ff9458 | 1992 | ;; New pos from the start. |
34200787 | 1993 | (newpos (length (completion-pcm--pattern->string pointpat))) |
81ff9458 | 1994 | ;; Do it afterwards because it changes `pointpat' by sideeffect. |
34200787 | 1995 | (merged (completion-pcm--pattern->string (nreverse mergedpat)))) |
eee6de73 SM |
1996 | |
1997 | (setq suffix (completion--merge-suffix merged newpos suffix)) | |
a38313e1 | 1998 | (cons (concat prefix merged suffix) (+ newpos (length prefix))))))) |
34200787 | 1999 | |
eee6de73 SM |
2000 | (defun completion-pcm-try-completion (string table pred point) |
2001 | (destructuring-bind (pattern all prefix suffix) | |
2002 | (completion-pcm--find-all-completions | |
2003 | string table pred point | |
2004 | (if minibuffer-completing-file-name | |
2005 | 'completion-pcm--filename-try-filter)) | |
2006 | (completion-pcm--merge-try pattern all prefix suffix))) | |
2007 | ||
fcb68f70 SM |
2008 | ;;; Initials completion |
2009 | ;; Complete /ums to /usr/monnier/src or lch to list-command-history. | |
2010 | ||
2011 | (defun completion-initials-expand (str table pred) | |
2012 | (unless (or (zerop (length str)) | |
6e2ca895 | 2013 | (string-match completion-pcm--delim-wild-regex str)) |
fcb68f70 SM |
2014 | (let ((bounds (completion-boundaries str table pred ""))) |
2015 | (if (zerop (car bounds)) | |
2016 | (mapconcat 'string str "-") | |
2017 | ;; If there's a boundary, it's trickier. The main use-case | |
2018 | ;; we consider here is file-name completion. We'd like | |
2019 | ;; to expand ~/eee to ~/e/e/e and /eee to /e/e/e. | |
2020 | ;; But at the same time, we don't want /usr/share/ae to expand | |
2021 | ;; to /usr/share/a/e just because we mistyped "ae" for "ar", | |
2022 | ;; so we probably don't want initials to touch anything that | |
2023 | ;; looks like /usr/share/foo. As a heuristic, we just check that | |
2024 | ;; the text before the boundary char is at most 1 char. | |
2025 | ;; This allows both ~/eee and /eee and not much more. | |
2026 | ;; FIXME: It sadly also disallows the use of ~/eee when that's | |
2027 | ;; embedded within something else (e.g. "(~/eee" in Info node | |
2028 | ;; completion or "ancestor:/eee" in bzr-revision completion). | |
2029 | (when (< (car bounds) 3) | |
2030 | (let ((sep (substring str (1- (car bounds)) (car bounds)))) | |
2031 | ;; FIXME: the above string-match checks the whole string, whereas | |
2032 | ;; we end up only caring about the after-boundary part. | |
2033 | (concat (substring str 0 (car bounds)) | |
2034 | (mapconcat 'string (substring str (car bounds)) sep)))))))) | |
2035 | ||
2036 | (defun completion-initials-all-completions (string table pred point) | |
2037 | (let ((newstr (completion-initials-expand string table pred))) | |
2038 | (when newstr | |
2039 | (completion-pcm-all-completions newstr table pred (length newstr))))) | |
2040 | ||
2041 | (defun completion-initials-try-completion (string table pred point) | |
2042 | (let ((newstr (completion-initials-expand string table pred))) | |
2043 | (when newstr | |
2044 | (completion-pcm-try-completion newstr table pred (length newstr))))) | |
2045 | ||
7d371eac JL |
2046 | \f |
2047 | ;; Miscellaneous | |
2048 | ||
2049 | (defun minibuffer-insert-file-name-at-point () | |
2050 | "Get a file name at point in original buffer and insert it to minibuffer." | |
2051 | (interactive) | |
2052 | (let ((file-name-at-point | |
2053 | (with-current-buffer (window-buffer (minibuffer-selected-window)) | |
2054 | (run-hook-with-args-until-success 'file-name-at-point-functions)))) | |
2055 | (when file-name-at-point | |
2056 | (insert file-name-at-point)))) | |
34200787 | 2057 | |
32bae13c | 2058 | (provide 'minibuffer) |
dc6ee347 MB |
2059 | |
2060 | ;; arch-tag: ef8a0a15-1080-4790-a754-04017c02f08f | |
32bae13c | 2061 | ;;; minibuffer.el ends here |