1 ;;; minibuffer.el --- Minibuffer completion functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Names with "--" are for functions and variables that are meant to be for
28 ;; Functional completion tables have an extended calling conventions:
29 ;; - The `action' can be (additionally to nil, t, and lambda) of the form
30 ;; (boundaries . SUFFIX) in which case it should return
31 ;; (boundaries START . END). See `completion-boundaries'.
32 ;; Any other return value should be ignored (so we ignore values returned
33 ;; from completion tables that don't know about this new `action' form).
37 ;; - completion-all-sorted-completions list all the completions, whereas
38 ;; it should only lists the ones that `try-completion' would consider.
39 ;; E.g. it should honor completion-ignored-extensions.
40 ;; - choose-completion can't automatically figure out the boundaries
41 ;; corresponding to the displayed completions because we only
42 ;; provide the start info but not the end info in
43 ;; completion-base-position.
44 ;; - quoting is problematic. E.g. the double-dollar quoting used in
45 ;; substitute-in-file-name (and hence read-file-name-internal) bumps
47 ;; - choose-completion doesn't know how to quote the text it inserts.
48 ;; E.g. it fails to double the dollars in file-name completion, or
49 ;; to backslash-escape spaces and other chars in comint completion.
50 ;; - when completing ~/tmp/fo$$o, the highligting in *Completions*
51 ;; is off by one position.
52 ;; - all code like PCM which relies on all-completions to match
53 ;; its argument gets confused because all-completions returns unquoted
54 ;; texts (as desired for *Completions* output).
55 ;; - C-x C-f ~/*/sr ? should not list "~/./src".
56 ;; - minibuffer-force-complete completes ~/src/emacs/t<!>/lisp/minibuffer.el
57 ;; to ~/src/emacs/trunk/ and throws away lisp/minibuffer.el.
61 ;; - Make things like icomplete-mode or lightning-completion work with
62 ;; completion-in-region-mode.
63 ;; - completion-insert-complete-hook (called after inserting a complete
64 ;; completion), typically used for "complete-abbrev" where it would expand
65 ;; the abbrev. Tho we'd probably want to provide it from the
67 ;; - extend `boundaries' to provide various other meta-data about the
68 ;; output of `all-completions':
69 ;; - preferred sorting order when displayed in *Completions*.
70 ;; - annotations/text-properties to add when displayed in *Completions*.
71 ;; - quoting/unquoting (so we can complete files names with envvars
72 ;; and backslashes, and all-completion can list names without
73 ;; quoting backslashes and dollars).
74 ;; - indicate how to turn all-completion's output into
75 ;; try-completion's output: e.g. completion-ignored-extensions.
76 ;; maybe that could be merged with the "quote" operation above.
77 ;; - completion hook to run when the completion is
78 ;; selected/inserted (maybe this should be provided some other
79 ;; way, e.g. as text-property, so `try-completion can also return it?)
80 ;; both for when it's inserted via TAB or via choose-completion.
81 ;; - indicate that `all-completions' doesn't do prefix-completion
82 ;; but just returns some list that relates in some other way to
83 ;; the provided string (as is the case in filecache.el), in which
84 ;; case partial-completion (for example) doesn't make any sense
85 ;; and neither does the completions-first-difference highlight.
86 ;; - indicate how to display the completions in *Completions* (turn
87 ;; \n into something else, add special boundaries between
88 ;; completions). E.g. when completing from the kill-ring.
90 ;; - make partial-completion-mode obsolete:
91 ;; - (?) <foo.h> style completion for file names.
92 ;; This can't be done identically just by tweaking completion,
93 ;; because partial-completion-mode's behavior is to expand <string.h>
94 ;; to /usr/include/string.h only when exiting the minibuffer, at which
95 ;; point the completion code is actually not involved normally.
96 ;; Partial-completion-mode does it via a find-file-not-found-function.
97 ;; - special code for C-x C-f <> to visit the file ref'd at point
98 ;; via (require 'foo) or #include "foo". ffap seems like a better
99 ;; place for this feature (supplemented with major-mode-provided
100 ;; functions to find the file ref'd at point).
102 ;; - case-sensitivity currently confuses two issues:
103 ;; - whether or not a particular completion table should be case-sensitive
104 ;; (i.e. whether strings that differ only by case are semantically
106 ;; - whether the user wants completion to pay attention to case.
107 ;; e.g. we may want to make it possible for the user to say "first try
108 ;; completion case-sensitively, and if that fails, try to ignore case".
110 ;; - add support for ** to pcm.
111 ;; - Add vc-file-name-completion-table to read-file-name-internal.
112 ;; - A feature like completing-help.el.
116 (eval-when-compile (require 'cl
))
118 ;;; Completion table manipulation
120 ;; New completion-table operation.
121 (defun completion-boundaries (string table pred suffix
)
122 "Return the boundaries of the completions returned by TABLE for STRING.
123 STRING is the string on which completion will be performed.
124 SUFFIX is the string after point.
125 The result is of the form (START . END) where START is the position
126 in STRING of the beginning of the completion field and END is the position
127 in SUFFIX of the end of the completion field.
128 E.g. for simple completion tables, the result is always (0 . (length SUFFIX))
129 and for file names the result is the positions delimited by
130 the closest directory separators."
131 (let ((boundaries (if (functionp table
)
132 (funcall table string pred
(cons 'boundaries suffix
)))))
133 (if (not (eq (car-safe boundaries
) 'boundaries
))
134 (setq boundaries nil
))
135 (cons (or (cadr boundaries
) 0)
136 (or (cddr boundaries
) (length suffix
)))))
138 (defun completion--some (fun xs
)
139 "Apply FUN to each element of XS in turn.
140 Return the first non-nil returned value.
142 (let ((firsterror nil
)
144 (while (and (not res
) xs
)
146 (setq res
(funcall fun
(pop xs
)))
147 (error (unless firsterror
(setq firsterror err
)) nil
)))
149 (if firsterror
(signal (car firsterror
) (cdr firsterror
))))))
151 (defun complete-with-action (action table string pred
)
152 "Perform completion ACTION.
153 STRING is the string to complete.
154 TABLE is the completion table, which should not be a function.
155 PRED is a completion predicate.
156 ACTION can be one of nil, t or `lambda'."
158 ((functionp table
) (funcall table string pred action
))
159 ((eq (car-safe action
) 'boundaries
)
160 (cons 'boundaries
(completion-boundaries string table pred
(cdr action
))))
164 ((null action
) 'try-completion
)
165 ((eq action t
) 'all-completions
)
166 (t 'test-completion
))
167 string table pred
))))
169 (defun completion-table-dynamic (fun)
170 "Use function FUN as a dynamic completion table.
171 FUN is called with one argument, the string for which completion is required,
172 and it should return an alist containing all the intended possible completions.
173 This alist may be a full list of possible completions so that FUN can ignore
174 the value of its argument. If completion is performed in the minibuffer,
175 FUN will be called in the buffer from which the minibuffer was entered.
177 The result of the `completion-table-dynamic' form is a function
178 that can be used as the COLLECTION argument to `try-completion' and
179 `all-completions'. See Info node `(elisp)Programmed Completion'."
180 (lambda (string pred action
)
181 (if (eq (car-safe action
) 'boundaries
)
182 ;; `fun' is not supposed to return another function but a plain old
183 ;; completion table, whose boundaries are always trivial.
185 (with-current-buffer (let ((win (minibuffer-selected-window)))
186 (if (window-live-p win
) (window-buffer win
)
188 (complete-with-action action
(funcall fun string
) string pred
)))))
190 (defmacro lazy-completion-table
(var fun
)
191 "Initialize variable VAR as a lazy completion table.
192 If the completion table VAR is used for the first time (e.g., by passing VAR
193 as an argument to `try-completion'), the function FUN is called with no
194 arguments. FUN must return the completion table that will be stored in VAR.
195 If completion is requested in the minibuffer, FUN will be called in the buffer
196 from which the minibuffer was entered. The return value of
197 `lazy-completion-table' must be used to initialize the value of VAR.
199 You should give VAR a non-nil `risky-local-variable' property."
200 (declare (debug (symbolp lambda-expr
)))
201 (let ((str (make-symbol "string")))
202 `(completion-table-dynamic
204 (when (functionp ,var
)
208 (defun completion-table-case-fold (table string pred action
)
209 (let ((completion-ignore-case t
))
210 (complete-with-action action table string pred
)))
212 (defun completion-table-with-context (prefix table string pred action
)
213 ;; TODO: add `suffix' maybe?
214 ;; Notice that `pred' may not be a function in some abusive cases.
215 (when (functionp pred
)
217 ;; Predicates are called differently depending on the nature of
218 ;; the completion table :-(
220 ((vectorp table
) ;Obarray.
221 (lambda (sym) (funcall pred
(concat prefix
(symbol-name sym
)))))
222 ((hash-table-p table
)
223 (lambda (s _v
) (funcall pred
(concat prefix s
))))
225 (lambda (s) (funcall pred
(concat prefix s
))))
226 (t ;Lists and alists.
228 (funcall pred
(concat prefix
(if (consp s
) (car s
) s
))))))))
229 (if (eq (car-safe action
) 'boundaries
)
230 (let* ((len (length prefix
))
231 (bound (completion-boundaries string table pred
(cdr action
))))
232 (list* 'boundaries
(+ (car bound
) len
) (cdr bound
)))
233 (let ((comp (complete-with-action action table string pred
)))
235 ;; In case of try-completion, add the prefix.
236 ((stringp comp
) (concat prefix comp
))
239 (defun completion-table-with-terminator (terminator table string pred action
)
240 "Construct a completion table like TABLE but with an extra TERMINATOR.
241 This is meant to be called in a curried way by first passing TERMINATOR
242 and TABLE only (via `apply-partially').
243 TABLE is a completion table, and TERMINATOR is a string appended to TABLE's
244 completion if it is complete. TERMINATOR is also used to determine the
245 completion suffix's boundary.
246 TERMINATOR can also be a cons cell (TERMINATOR . TERMINATOR-REGEXP)
247 in which case TERMINATOR-REGEXP is a regular expression whose submatch
248 number 1 should match TERMINATOR. This is used when there is a need to
249 distinguish occurrences of the TERMINATOR strings which are really terminators
250 from others (e.g. escaped). In this form, the car of TERMINATOR can also be,
251 instead of a string, a function that takes the completion and returns the
252 \"terminated\" string."
253 ;; FIXME: This implementation is not right since it only adds the terminator
254 ;; in try-completion, so any completion-style that builds the completion via
255 ;; all-completions won't get the terminator, and selecting an entry in
256 ;; *Completions* won't get the terminator added either.
258 ((eq (car-safe action
) 'boundaries
)
259 (let* ((suffix (cdr action
))
260 (bounds (completion-boundaries string table pred suffix
))
261 (terminator-regexp (if (consp terminator
)
262 (cdr terminator
) (regexp-quote terminator
)))
263 (max (and terminator-regexp
264 (string-match terminator-regexp suffix
))))
265 (list* 'boundaries
(car bounds
)
266 (min (cdr bounds
) (or max
(length suffix
))))))
268 (let ((comp (try-completion string table pred
)))
269 (if (consp terminator
) (setq terminator
(car terminator
)))
271 (if (functionp terminator
)
272 (funcall terminator string
)
273 (concat string terminator
))
274 (if (and (stringp comp
) (not (zerop (length comp
)))
275 ;; Try to avoid the second call to try-completion, since
276 ;; it may be very inefficient (because `comp' made us
277 ;; jump to a new boundary, so we complete in that
278 ;; boundary with an empty start string).
279 (let ((newbounds (completion-boundaries comp table pred
"")))
280 (< (car newbounds
) (length comp
)))
281 (eq (try-completion comp table pred
) t
))
282 (if (functionp terminator
)
283 (funcall terminator comp
)
284 (concat comp terminator
))
287 ;; FIXME: We generally want the `try' and `all' behaviors to be
288 ;; consistent so pcm can merge the `all' output to get the `try' output,
289 ;; but that sometimes clashes with the need for `all' output to look
290 ;; good in *Completions*.
291 ;; (mapcar (lambda (s) (concat s terminator))
292 ;; (all-completions string table pred))))
293 (all-completions string table pred
))
294 ;; completion-table-with-terminator is always used for
295 ;; "sub-completions" so it's only called if the terminator is missing,
296 ;; in which case `test-completion' should return nil.
297 ((eq action
'lambda
) nil
)))
299 (defun completion-table-with-predicate (table pred1 strict string pred2 action
)
300 "Make a completion table equivalent to TABLE but filtered through PRED1.
301 PRED1 is a function of one argument which returns non-nil if and only if the
302 argument is an element of TABLE which should be considered for completion.
303 STRING, PRED2, and ACTION are the usual arguments to completion tables,
304 as described in `try-completion', `all-completions', and `test-completion'.
305 If STRICT is t, the predicate always applies; if nil it only applies if
306 it does not reduce the set of possible completions to nothing.
307 Note: TABLE needs to be a proper completion table which obeys predicates."
309 ((and (not strict
) (eq action
'lambda
))
310 ;; Ignore pred1 since it doesn't really have to apply anyway.
311 (test-completion string table pred2
))
313 (or (complete-with-action action table string
314 (if (null pred2
) pred1
316 ;; Call `pred1' first, so that `pred2'
317 ;; really can't tell that `x' is in table.
318 (if (funcall pred1 x
) (funcall pred2 x
)))))
319 ;; If completion failed and we're not applying pred1 strictly, try
320 ;; again without pred1.
322 (complete-with-action action table string pred2
))))))
324 (defun completion-table-in-turn (&rest tables
)
325 "Create a completion table that tries each table in TABLES in turn."
326 ;; FIXME: the boundaries may come from TABLE1 even when the completion list
327 ;; is returned by TABLE2 (because TABLE1 returned an empty list).
328 (lambda (string pred action
)
329 (completion--some (lambda (table)
330 (complete-with-action action table string pred
))
333 ;; (defmacro complete-in-turn (a b) `(completion-table-in-turn ,a ,b))
334 ;; (defmacro dynamic-completion-table (fun) `(completion-table-dynamic ,fun))
335 (define-obsolete-function-alias
336 'complete-in-turn
'completion-table-in-turn
"23.1")
337 (define-obsolete-function-alias
338 'dynamic-completion-table
'completion-table-dynamic
"23.1")
340 ;;; Minibuffer completion
342 (defgroup minibuffer nil
343 "Controlling the behavior of the minibuffer."
344 :link
'(custom-manual "(emacs)Minibuffer")
347 (defun minibuffer-message (message &rest args
)
348 "Temporarily display MESSAGE at the end of the minibuffer.
349 The text is displayed for `minibuffer-message-timeout' seconds,
350 or until the next input event arrives, whichever comes first.
351 Enclose MESSAGE in [...] if this is not yet the case.
352 If ARGS are provided, then pass MESSAGE through `format'."
353 (if (not (minibufferp (current-buffer)))
356 (apply 'message message args
)
357 (message "%s" message
))
358 (prog1 (sit-for (or minibuffer-message-timeout
1000000))
360 ;; Clear out any old echo-area message to make way for our new thing.
362 (setq message
(if (and (null args
) (string-match-p "\\` *\\[.+\\]\\'" message
))
363 ;; Make sure we can put-text-property.
364 (copy-sequence message
)
365 (concat " [" message
"]")))
366 (when args
(setq message
(apply 'format message args
)))
367 (let ((ol (make-overlay (point-max) (point-max) nil t t
))
368 ;; A quit during sit-for normally only interrupts the sit-for,
369 ;; but since minibuffer-message is used at the end of a command,
370 ;; at a time when the command has virtually finished already, a C-g
371 ;; should really cause an abort-recursive-edit instead (i.e. as if
372 ;; the C-g had been typed at top-level). Binding inhibit-quit here
373 ;; is an attempt to get that behavior.
377 (unless (zerop (length message
))
378 ;; The current C cursor code doesn't know to use the overlay's
379 ;; marker's stickiness to figure out whether to place the cursor
380 ;; before or after the string, so let's spoon-feed it the pos.
381 (put-text-property 0 1 'cursor t message
))
382 (overlay-put ol
'after-string message
)
383 (sit-for (or minibuffer-message-timeout
1000000)))
384 (delete-overlay ol
)))))
386 (defun minibuffer-completion-contents ()
387 "Return the user input in a minibuffer before point as a string.
388 That is what completion commands operate on."
389 (buffer-substring (field-beginning) (point)))
391 (defun delete-minibuffer-contents ()
392 "Delete all user input in a minibuffer.
393 If the current buffer is not a minibuffer, erase its entire contents."
394 ;; We used to do `delete-field' here, but when file name shadowing
395 ;; is on, the field doesn't cover the entire minibuffer contents.
396 (delete-region (minibuffer-prompt-end) (point-max)))
398 (defvar completion-show-inline-help t
399 "If non-nil, print helpful inline messages during completion.")
401 (defcustom completion-auto-help t
402 "Non-nil means automatically provide help for invalid completion input.
403 If the value is t the *Completion* buffer is displayed whenever completion
404 is requested but cannot be done.
405 If the value is `lazy', the *Completions* buffer is only displayed after
406 the second failed attempt to complete."
407 :type
'(choice (const nil
) (const t
) (const lazy
))
410 (defconst completion-styles-alist
412 completion-emacs21-try-completion completion-emacs21-all-completions
413 "Simple prefix-based completion.
414 I.e. when completing \"foo_bar\" (where _ is the position of point),
415 it will consider all completions candidates matching the glob
416 pattern \"foobar*\".")
418 completion-emacs22-try-completion completion-emacs22-all-completions
419 "Prefix completion that only operates on the text before point.
420 I.e. when completing \"foo_bar\" (where _ is the position of point),
421 it will consider all completions candidates matching the glob
422 pattern \"foo*\" and will add back \"bar\" to the end of it.")
424 completion-basic-try-completion completion-basic-all-completions
425 "Completion of the prefix before point and the suffix after point.
426 I.e. when completing \"foo_bar\" (where _ is the position of point),
427 it will consider all completions candidates matching the glob
428 pattern \"foo*bar*\".")
430 completion-pcm-try-completion completion-pcm-all-completions
431 "Completion of multiple words, each one taken as a prefix.
432 I.e. when completing \"l-co_h\" (where _ is the position of point),
433 it will consider all completions candidates matching the glob
434 pattern \"l*-co*h*\".
435 Furthermore, for completions that are done step by step in subfields,
436 the method is applied to all the preceding fields that do not yet match.
437 E.g. C-x C-f /u/mo/s TAB could complete to /usr/monnier/src.
438 Additionally the user can use the char \"*\" as a glob pattern.")
440 completion-substring-try-completion completion-substring-all-completions
441 "Completion of the string taken as a substring.
442 I.e. when completing \"foo_bar\" (where _ is the position of point),
443 it will consider all completions candidates matching the glob
444 pattern \"*foo*bar*\".")
446 completion-initials-try-completion completion-initials-all-completions
447 "Completion of acronyms and initialisms.
448 E.g. can complete M-x lch to list-command-history
449 and C-x C-f ~/sew to ~/src/emacs/work."))
450 "List of available completion styles.
451 Each element has the form (NAME TRY-COMPLETION ALL-COMPLETIONS DOC):
452 where NAME is the name that should be used in `completion-styles',
453 TRY-COMPLETION is the function that does the completion (it should
454 follow the same calling convention as `completion-try-completion'),
455 ALL-COMPLETIONS is the function that lists the completions (it should
456 follow the calling convention of `completion-all-completions'),
457 and DOC describes the way this style of completion works.")
459 (defcustom completion-styles
460 ;; First, use `basic' because prefix completion has been the standard
461 ;; for "ever" and works well in most cases, so using it first
462 ;; ensures that we obey previous behavior in most cases.
464 ;; Then use `partial-completion' because it has proven to
465 ;; be a very convenient extension.
467 ;; Finally use `emacs22' so as to maintain (in many/most cases)
468 ;; the previous behavior that when completing "foobar" with point
469 ;; between "foo" and "bar" the completion try to complete "foo"
470 ;; and simply add "bar" to the end of the result.
472 "List of completion styles to use.
473 The available styles are listed in `completion-styles-alist'."
474 :type
`(repeat (choice ,@(mapcar (lambda (x) (list 'const
(car x
)))
475 completion-styles-alist
)))
479 (defun completion-try-completion (string table pred point
)
480 "Try to complete STRING using completion table TABLE.
481 Only the elements of table that satisfy predicate PRED are considered.
482 POINT is the position of point within STRING.
483 The return value can be either nil to indicate that there is no completion,
484 t to indicate that STRING is the only possible completion,
485 or a pair (STRING . NEWPOINT) of the completed result string together with
486 a new position for point."
487 (completion--some (lambda (style)
488 (funcall (nth 1 (assq style completion-styles-alist
))
489 string table pred point
))
492 (defun completion-all-completions (string table pred point
)
493 "List the possible completions of STRING in completion table TABLE.
494 Only the elements of table that satisfy predicate PRED are considered.
495 POINT is the position of point within STRING.
496 The return value is a list of completions and may contain the base-size
498 ;; FIXME: We need to additionally return the info needed for the
499 ;; second part of completion-base-position.
500 (completion--some (lambda (style)
501 (funcall (nth 2 (assq style completion-styles-alist
))
502 string table pred point
))
505 (defun minibuffer--bitset (modified completions exact
)
506 (logior (if modified
4 0)
510 (defun completion--replace (beg end newtext
)
511 "Replace the buffer text between BEG and END with NEWTEXT.
512 Moves point to the end of the new text."
513 ;; Maybe this should be in subr.el.
514 ;; You'd think this is trivial to do, but details matter if you want
515 ;; to keep markers "at the right place" and be robust in the face of
516 ;; after-change-functions that may themselves modify the buffer.
517 (let ((prefix-len 0))
518 ;; Don't touch markers in the shared prefix (if any).
519 (while (and (< prefix-len
(length newtext
))
520 (< (+ beg prefix-len
) end
)
521 (eq (char-after (+ beg prefix-len
))
522 (aref newtext prefix-len
)))
523 (setq prefix-len
(1+ prefix-len
)))
524 (unless (zerop prefix-len
)
525 (setq beg
(+ beg prefix-len
))
526 (setq newtext
(substring newtext prefix-len
))))
527 (let ((suffix-len 0))
528 ;; Don't touch markers in the shared suffix (if any).
529 (while (and (< suffix-len
(length newtext
))
530 (< beg
(- end suffix-len
))
531 (eq (char-before (- end suffix-len
))
532 (aref newtext
(- (length newtext
) suffix-len
1))))
533 (setq suffix-len
(1+ suffix-len
)))
534 (unless (zerop suffix-len
)
535 (setq end
(- end suffix-len
))
536 (setq newtext
(substring newtext
0 (- suffix-len
))))
539 (delete-region (point) (+ (point) (- end beg
)))
540 (forward-char suffix-len
)))
542 (defcustom completion-cycle-threshold nil
543 "Number of completion candidates below which cycling is used.
544 Depending on this setting `minibuffer-complete' may use cycling,
545 like `minibuffer-force-complete'.
546 If nil, cycling is never used.
547 If t, cycling is always used.
548 If an integer, cycling is used as soon as there are fewer completion
549 candidates than this number."
550 :type
'(choice (const :tag
"No cycling" nil
)
551 (const :tag
"Always cycle" t
)
552 (integer :tag
"Threshold")))
554 (defvar completion-all-sorted-completions nil
)
555 (make-variable-buffer-local 'completion-all-sorted-completions
)
556 (defvar completion-cycling nil
)
558 (defvar completion-fail-discreetly nil
559 "If non-nil, stay quiet when there is no match.")
561 (defun completion--message (msg)
562 (if completion-show-inline-help
563 (minibuffer-message msg
)))
565 (defun completion--do-completion (&optional try-completion-function
)
566 "Do the completion and return a summary of what happened.
567 M = completion was performed, the text was Modified.
568 C = there were available Completions.
569 E = after completion we now have an Exact match.
572 000 0 no possible completion
573 001 1 was already an exact and unique completion
574 010 2 no completion happened
575 011 3 was already an exact completion
578 110 6 some completion happened
579 111 7 completed to an exact completion"
580 (let* ((beg (field-beginning))
582 (string (buffer-substring beg end
))
583 (comp (funcall (or try-completion-function
584 'completion-try-completion
)
586 minibuffer-completion-table
587 minibuffer-completion-predicate
591 (minibuffer-hide-completions)
592 (unless completion-fail-discreetly
594 (completion--message "No match"))
595 (minibuffer--bitset nil nil nil
))
597 (minibuffer-hide-completions)
598 (goto-char (field-end))
599 (minibuffer--bitset nil nil t
)) ;Exact and unique match.
601 ;; `completed' should be t if some completion was done, which doesn't
602 ;; include simply changing the case of the entered string. However,
603 ;; for appearance, the string is rewritten if the case changes.
604 (let* ((comp-pos (cdr comp
))
605 (completion (car comp
))
606 (completed (not (eq t
(compare-strings completion nil nil
608 (unchanged (eq t
(compare-strings completion nil nil
609 string nil nil nil
))))
612 ;; Insert in minibuffer the chars we got.
613 (completion--replace beg end completion
))
614 ;; Move point to its completion-mandated destination.
615 (forward-char (- comp-pos
(length completion
)))
617 (if (not (or unchanged completed
))
618 ;; The case of the string changed, but that's all. We're not sure
619 ;; whether this is a unique completion or not, so try again using
620 ;; the real case (this shouldn't recurse again, because the next
621 ;; time try-completion will return either t or the exact string).
622 (completion--do-completion try-completion-function
)
624 ;; It did find a match. Do we match some possibility exactly now?
625 (let ((exact (test-completion completion
626 minibuffer-completion-table
627 minibuffer-completion-predicate
))
629 ;; Check to see if we want to do cycling. We do it
630 ;; here, after having performed the normal completion,
631 ;; so as to take advantage of the difference between
632 ;; try-completion and all-completions, for things
633 ;; like completion-ignored-extensions.
634 (when (and completion-cycle-threshold
635 ;; Check that the completion didn't make
636 ;; us jump to a different boundary.
638 (< (car (completion-boundaries
639 (substring completion
0 comp-pos
)
640 minibuffer-completion-table
641 minibuffer-completion-predicate
644 (completion-all-sorted-completions))))
645 (completion--flush-all-sorted-completions)
647 ((and (consp (cdr comps
)) ;; There's something to cycle.
649 ;; This signal an (intended) error if comps is too
650 ;; short or if completion-cycle-threshold is t.
651 (consp (nthcdr completion-cycle-threshold comps
)))))
652 ;; Fewer than completion-cycle-threshold remaining
653 ;; completions: let's cycle.
654 (setq completed t exact t
)
655 (setq completion-all-sorted-completions comps
)
656 (minibuffer-force-complete))
658 ;; We could also decide to refresh the completions,
659 ;; if they're displayed (and assuming there are
660 ;; completions left).
661 (minibuffer-hide-completions))
662 ;; Show the completion table, if requested.
664 (if (case completion-auto-help
665 (lazy (eq this-command last-command
))
666 (t completion-auto-help
))
667 (minibuffer-completion-help)
668 (completion--message "Next char not unique")))
669 ;; If the last exact completion and this one were the same, it
670 ;; means we've already given a "Complete, but not unique" message
671 ;; and the user's hit TAB again, so now we give him help.
672 ((eq this-command last-command
)
673 (if completion-auto-help
(minibuffer-completion-help))))
675 (minibuffer--bitset completed t exact
))))))))
677 (defun minibuffer-complete ()
678 "Complete the minibuffer contents as far as possible.
679 Return nil if there is no valid completion, else t.
680 If no characters can be completed, display a list of possible completions.
681 If you repeat this command after it displayed such a list,
682 scroll the window of possible completions."
684 ;; If the previous command was not this,
685 ;; mark the completion buffer obsolete.
686 (unless (eq this-command last-command
)
687 (completion--flush-all-sorted-completions)
688 (setq minibuffer-scroll-window nil
))
691 ;; If there's a fresh completion window with a live buffer,
692 ;; and this command is repeated, scroll that window.
693 ((window-live-p minibuffer-scroll-window
)
694 (let ((window minibuffer-scroll-window
))
695 (with-current-buffer (window-buffer window
)
696 (if (pos-visible-in-window-p (point-max) window
)
697 ;; If end is in view, scroll up to the beginning.
698 (set-window-start window
(point-min) nil
)
699 ;; Else scroll down one screen.
700 (scroll-other-window))
702 ;; If we're cycling, keep on cycling.
703 ((and completion-cycling completion-all-sorted-completions
)
704 (minibuffer-force-complete)
706 (t (case (completion--do-completion)
708 (#b001
(completion--message "Sole completion")
710 (#b011
(completion--message "Complete, but not unique")
714 (defun completion--flush-all-sorted-completions (&rest _ignore
)
715 (remove-hook 'after-change-functions
716 'completion--flush-all-sorted-completions t
)
717 (setq completion-cycling nil
)
718 (setq completion-all-sorted-completions nil
))
720 (defun completion-all-sorted-completions ()
721 (or completion-all-sorted-completions
722 (let* ((start (field-beginning))
724 (all (completion-all-completions (buffer-substring start end
)
725 minibuffer-completion-table
726 minibuffer-completion-predicate
729 (base-size (or (cdr last
) 0)))
732 ;; Prefer shorter completions.
733 (setq all
(sort all
(lambda (c1 c2
)
734 (let ((s1 (get-text-property
735 0 :completion-cycle-penalty c1
))
736 (s2 (get-text-property
737 0 :completion-cycle-penalty c2
)))
739 (< (length c1
) (length c2
))
740 (< (or s1
(length c1
))
741 (or s2
(length c2
))))))))
742 ;; Prefer recently used completions.
743 ;; FIXME: Additional sorting ideas:
744 ;; - for M-x, prefer commands that have no key binding.
745 (let ((hist (symbol-value minibuffer-history-variable
)))
746 (setq all
(sort all
(lambda (c1 c2
)
747 (> (length (member c1 hist
))
748 (length (member c2 hist
)))))))
749 ;; Cache the result. This is not just for speed, but also so that
750 ;; repeated calls to minibuffer-force-complete can cycle through
751 ;; all possibilities.
752 (add-hook 'after-change-functions
753 'completion--flush-all-sorted-completions nil t
)
754 (setq completion-all-sorted-completions
755 (nconc all base-size
))))))
757 (defun minibuffer-force-complete ()
758 "Complete the minibuffer to an exact match.
759 Repeated uses step through the possible completions."
761 ;; FIXME: Need to deal with the extra-size issue here as well.
762 ;; FIXME: ~/src/emacs/t<M-TAB>/lisp/minibuffer.el completes to
763 ;; ~/src/emacs/trunk/ and throws away lisp/minibuffer.el.
764 (let* ((start (field-beginning))
766 (all (completion-all-sorted-completions)))
767 (if (not (consp all
))
769 (if all
"No more completions" "No completions"))
770 (setq completion-cycling t
)
773 (delete-region (+ start
(cdr (last all
))) end
)
774 ;; If completing file names, (car all) may be a directory, so we'd now
775 ;; have a new set of possible completions and might want to reset
776 ;; completion-all-sorted-completions to nil, but we prefer not to,
777 ;; so that repeated calls minibuffer-force-complete still cycle
778 ;; through the previous possible completions.
779 (let ((last (last all
)))
780 (setcdr last
(cons (car all
) (cdr last
)))
781 (setq completion-all-sorted-completions
(cdr all
))))))
783 (defvar minibuffer-confirm-exit-commands
784 '(minibuffer-complete minibuffer-complete-word PC-complete PC-complete-word
)
785 "A list of commands which cause an immediately following
786 `minibuffer-complete-and-exit' to ask for extra confirmation.")
788 (defun minibuffer-complete-and-exit ()
789 "Exit if the minibuffer contains a valid completion.
790 Otherwise, try to complete the minibuffer contents. If
791 completion leads to a valid completion, a repetition of this
794 If `minibuffer-completion-confirm' is `confirm', do not try to
795 complete; instead, ask for confirmation and accept any input if
797 If `minibuffer-completion-confirm' is `confirm-after-completion',
798 do not try to complete; instead, ask for confirmation if the
799 preceding minibuffer command was a member of
800 `minibuffer-confirm-exit-commands', and accept the input
803 (let ((beg (field-beginning))
806 ;; Allow user to specify null string
807 ((= beg end
) (exit-minibuffer))
808 ((test-completion (buffer-substring beg end
)
809 minibuffer-completion-table
810 minibuffer-completion-predicate
)
811 ;; FIXME: completion-ignore-case has various slightly
812 ;; incompatible meanings. E.g. it can reflect whether the user
813 ;; wants completion to pay attention to case, or whether the
814 ;; string will be used in a context where case is significant.
815 ;; E.g. usually try-completion should obey the first, whereas
816 ;; test-completion should obey the second.
817 (when completion-ignore-case
818 ;; Fixup case of the field, if necessary.
819 (let* ((string (buffer-substring beg end
))
820 (compl (try-completion
822 minibuffer-completion-table
823 minibuffer-completion-predicate
)))
824 (when (and (stringp compl
) (not (equal string compl
))
825 ;; If it weren't for this piece of paranoia, I'd replace
826 ;; the whole thing with a call to do-completion.
827 ;; This is important, e.g. when the current minibuffer's
828 ;; content is a directory which only contains a single
829 ;; file, so `try-completion' actually completes to
831 (= (length string
) (length compl
)))
834 (delete-region beg end
))))
837 ((memq minibuffer-completion-confirm
'(confirm confirm-after-completion
))
838 ;; The user is permitted to exit with an input that's rejected
839 ;; by test-completion, after confirming her choice.
840 (if (or (eq last-command this-command
)
841 ;; For `confirm-after-completion' we only ask for confirmation
842 ;; if trying to exit immediately after typing TAB (this
843 ;; catches most minibuffer typos).
844 (and (eq minibuffer-completion-confirm
'confirm-after-completion
)
845 (not (memq last-command minibuffer-confirm-exit-commands
))))
847 (minibuffer-message "Confirm")
851 ;; Call do-completion, but ignore errors.
852 (case (condition-case nil
853 (completion--do-completion)
855 ((#b001
#b011
) (exit-minibuffer))
856 (#b111
(if (not minibuffer-completion-confirm
)
858 (minibuffer-message "Confirm")
862 (defun completion--try-word-completion (string table predicate point
)
863 (let ((comp (completion-try-completion string table predicate point
)))
864 (if (not (consp comp
))
867 ;; If completion finds next char not unique,
868 ;; consider adding a space or a hyphen.
869 (when (= (length string
) (length (car comp
)))
870 ;; Mark the added char with the `completion-word' property, so it
871 ;; can be handled specially by completion styles such as
872 ;; partial-completion.
873 ;; We used to remove `partial-completion' from completion-styles
874 ;; instead, but it was too blunt, leading to situations where SPC
875 ;; was the only insertable char at point but minibuffer-complete-word
876 ;; refused inserting it.
877 (let ((exts (mapcar (lambda (str) (propertize str
'completion-try-word t
))
879 (before (substring string
0 point
))
880 (after (substring string point
))
882 (while (and exts
(not (consp tem
)))
883 (setq tem
(completion-try-completion
884 (concat before
(pop exts
) after
)
885 table predicate
(1+ point
))))
886 (if (consp tem
) (setq comp tem
))))
888 ;; Completing a single word is actually more difficult than completing
889 ;; as much as possible, because we first have to find the "current
890 ;; position" in `completion' in order to find the end of the word
891 ;; we're completing. Normally, `string' is a prefix of `completion',
892 ;; which makes it trivial to find the position, but with fancier
893 ;; completion (plus env-var expansion, ...) `completion' might not
894 ;; look anything like `string' at all.
895 (let* ((comppoint (cdr comp
))
896 (completion (car comp
))
897 (before (substring string
0 point
))
898 (combined (concat before
"\n" completion
)))
899 ;; Find in completion the longest text that was right before point.
900 (when (string-match "\\(.+\\)\n.*?\\1" combined
)
901 (let* ((prefix (match-string 1 before
))
902 ;; We used non-greedy match to make `rem' as long as possible.
903 (rem (substring combined
(match-end 0)))
904 ;; Find in the remainder of completion the longest text
905 ;; that was right after point.
906 (after (substring string point
))
907 (suffix (if (string-match "\\`\\(.+\\).*\n.*\\1"
908 (concat after
"\n" rem
))
909 (match-string 1 after
))))
910 ;; The general idea is to try and guess what text was inserted
911 ;; at point by the completion. Problem is: if we guess wrong,
912 ;; we may end up treating as "added by completion" text that was
913 ;; actually painfully typed by the user. So if we then cut
914 ;; after the first word, we may throw away things the
915 ;; user wrote. So let's try to be as conservative as possible:
916 ;; only cut after the first word, if we're reasonably sure that
917 ;; our guess is correct.
918 ;; Note: a quick survey on emacs-devel seemed to indicate that
919 ;; nobody actually cares about the "word-at-a-time" feature of
920 ;; minibuffer-complete-word, whose real raison-d'être is that it
921 ;; tries to add "-" or " ". One more reason to only cut after
922 ;; the first word, if we're really sure we're right.
923 (when (and (or suffix
(zerop (length after
)))
924 (string-match (concat
925 ;; Make submatch 1 as small as possible
926 ;; to reduce the risk of cutting
928 ".*" (regexp-quote prefix
) "\\(.*?\\)"
929 (if suffix
(regexp-quote suffix
) "\\'"))
931 ;; The new point in `completion' should also be just
932 ;; before the suffix, otherwise something more complex
933 ;; is going on, and we're not sure where we are.
934 (eq (match-end 1) comppoint
)
935 ;; (match-beginning 1)..comppoint is now the stretch
936 ;; of text in `completion' that was completed at point.
937 (string-match "\\W" completion
(match-beginning 1))
938 ;; Is there really something to cut?
939 (> comppoint
(match-end 0)))
940 ;; Cut after the first word.
941 (let ((cutpos (match-end 0)))
942 (setq completion
(concat (substring completion
0 cutpos
)
943 (substring completion comppoint
)))
944 (setq comppoint cutpos
)))))
946 (cons completion comppoint
)))))
949 (defun minibuffer-complete-word ()
950 "Complete the minibuffer contents at most a single word.
951 After one word is completed as much as possible, a space or hyphen
952 is added, provided that matches some possible completion.
953 Return nil if there is no valid completion, else t."
955 (case (completion--do-completion 'completion--try-word-completion
)
957 (#b001
(completion--message "Sole completion")
959 (#b011
(completion--message "Complete, but not unique")
963 (defface completions-annotations
'((t :inherit italic
))
964 "Face to use for annotations in the *Completions* buffer.")
966 (defcustom completions-format
'horizontal
967 "Define the appearance and sorting of completions.
968 If the value is `vertical', display completions sorted vertically
969 in columns in the *Completions* buffer.
970 If the value is `horizontal', display completions sorted
971 horizontally in alphabetical order, rather than down the screen."
972 :type
'(choice (const horizontal
) (const vertical
))
976 (defun completion--insert-strings (strings)
977 "Insert a list of STRINGS into the current buffer.
978 Uses columns to keep the listing readable but compact.
979 It also eliminates runs of equal strings."
980 (when (consp strings
)
981 (let* ((length (apply 'max
984 (+ (string-width (car s
))
985 (string-width (cadr s
)))
988 (window (get-buffer-window (current-buffer) 0))
989 (wwidth (if window
(1- (window-width window
)) 79))
991 ;; At least 2 columns; at least 2 spaces between columns.
992 (max 2 (/ wwidth
(+ 2 length
)))
993 ;; Don't allocate more columns than we can fill.
994 ;; Windows can't show less than 3 lines anyway.
995 (max 1 (/ (length strings
) 2))))
996 (colwidth (/ wwidth columns
))
998 (rows (/ (length strings
) columns
))
1001 ;; The insertion should be "sensible" no matter what choices were made
1002 ;; for the parameters above.
1003 (dolist (str strings
)
1004 (unless (equal laststring str
) ; Remove (consecutive) duplicates.
1005 (setq laststring str
)
1006 (let ((length (if (consp str
)
1007 (+ (string-width (car str
))
1008 (string-width (cadr str
)))
1009 (string-width str
))))
1011 ((eq completions-format
'vertical
)
1014 (forward-line (- -
1 rows
))
1015 (setq row
0 column
(+ column colwidth
)))
1018 (while (> (current-column) column
)
1024 (set-text-properties (- (point) 1) (point)
1025 `(display (space :align-to
,column
)))))
1027 ;; Horizontal format
1029 (if (< wwidth
(+ (max colwidth length
) column
))
1030 ;; No space for `str' at point, move to next line.
1031 (progn (insert "\n") (setq column
0))
1033 ;; Leave the space unpropertized so that in the case we're
1034 ;; already past the goal column, there is still
1035 ;; a space displayed.
1036 (set-text-properties (- (point) 1) (point)
1037 ;; We can't just set tab-width, because
1038 ;; completion-setup-function will kill
1039 ;; all local variables :-(
1040 `(display (space :align-to
,column
)))
1042 (if (not (consp str
))
1043 (put-text-property (point) (progn (insert str
) (point))
1044 'mouse-face
'highlight
)
1045 (put-text-property (point) (progn (insert (car str
)) (point))
1046 'mouse-face
'highlight
)
1047 (add-text-properties (point) (progn (insert (cadr str
)) (point))
1049 face completions-annotations
)))
1051 ((eq completions-format
'vertical
)
1056 (setq row
(1+ row
)))
1058 ;; Horizontal format
1059 ;; Next column to align to.
1060 (setq column
(+ column
1061 ;; Round up to a whole number of columns.
1062 (* colwidth
(ceiling length colwidth
))))))))))))
1064 (defvar completion-common-substring nil
)
1065 (make-obsolete-variable 'completion-common-substring nil
"23.1")
1067 (defvar completion-setup-hook nil
1068 "Normal hook run at the end of setting up a completion list buffer.
1069 When this hook is run, the current buffer is the one in which the
1070 command to display the completion list buffer was run.
1071 The completion list buffer is available as the value of `standard-output'.
1072 See also `display-completion-list'.")
1074 (defface completions-first-difference
1075 '((t (:inherit bold
)))
1076 "Face put on the first uncommon character in completions in *Completions* buffer."
1079 (defface completions-common-part
1080 '((t (:inherit default
)))
1081 "Face put on the common prefix substring in completions in *Completions* buffer.
1082 The idea of `completions-common-part' is that you can use it to
1083 make the common parts less visible than normal, so that the rest
1084 of the differing parts is, by contrast, slightly highlighted."
1087 (defun completion-hilit-commonality (completions prefix-len base-size
)
1089 (let ((com-str-len (- prefix-len
(or base-size
0))))
1094 ;; Don't modify the string itself, but a copy, since the
1095 ;; the string may be read-only or used for other purposes.
1096 ;; Furthermore, since `completions' may come from
1097 ;; display-completion-list, `elem' may be a list.
1099 (car (setq elem
(cons (copy-sequence (car elem
))
1101 (setq elem
(copy-sequence elem
)))))
1102 (put-text-property 0
1103 ;; If completion-boundaries returns incorrect
1104 ;; values, all-completions may return strings
1105 ;; that don't contain the prefix.
1106 (min com-str-len
(length str
))
1107 'font-lock-face
'completions-common-part
1109 (if (> (length str
) com-str-len
)
1110 (put-text-property com-str-len
(1+ com-str-len
)
1111 'font-lock-face
'completions-first-difference
1117 (defun display-completion-list (completions &optional common-substring
)
1118 "Display the list of completions, COMPLETIONS, using `standard-output'.
1119 Each element may be just a symbol or string
1120 or may be a list of two strings to be printed as if concatenated.
1121 If it is a list of two strings, the first is the actual completion
1122 alternative, the second serves as annotation.
1123 `standard-output' must be a buffer.
1124 The actual completion alternatives, as inserted, are given `mouse-face'
1125 properties of `highlight'.
1126 At the end, this runs the normal hook `completion-setup-hook'.
1127 It can find the completion buffer in `standard-output'.
1129 The obsolete optional arg COMMON-SUBSTRING, if non-nil, should be a string
1130 specifying a common substring for adding the faces
1131 `completions-first-difference' and `completions-common-part' to
1132 the completions buffer."
1133 (if common-substring
1134 (setq completions
(completion-hilit-commonality
1135 completions
(length common-substring
)
1136 ;; We don't know the base-size.
1138 (if (not (bufferp standard-output
))
1139 ;; This *never* (ever) happens, so there's no point trying to be clever.
1141 (let ((standard-output (current-buffer))
1142 (completion-setup-hook nil
))
1143 (display-completion-list completions common-substring
))
1144 (princ (buffer-string)))
1146 (with-current-buffer standard-output
1147 (goto-char (point-max))
1148 (if (null completions
)
1149 (insert "There are no possible completions of what you have typed.")
1150 (insert "Possible completions are:\n")
1151 (completion--insert-strings completions
))))
1153 ;; The hilit used to be applied via completion-setup-hook, so there
1154 ;; may still be some code that uses completion-common-substring.
1156 (let ((completion-common-substring common-substring
))
1157 (run-hooks 'completion-setup-hook
)))
1160 (defvar completion-annotate-function
1162 ;; Note: there's a lot of scope as for when to add annotations and
1163 ;; what annotations to add. E.g. completing-help.el allowed adding
1164 ;; the first line of docstrings to M-x completion. But there's
1165 ;; a tension, since such annotations, while useful at times, can
1166 ;; actually drown the useful information.
1167 ;; So completion-annotate-function should be used parsimoniously, or
1168 ;; else only used upon a user's request (e.g. we could add a command
1169 ;; to completion-list-mode to add annotations to the current
1171 "Function to add annotations in the *Completions* buffer.
1172 The function takes a completion and should either return nil, or a string that
1173 will be displayed next to the completion. The function can access the
1174 completion table and predicates via `minibuffer-completion-table' and related
1177 (defun minibuffer-completion-help ()
1178 "Display a list of possible completions of the current minibuffer contents."
1180 (message "Making completion list...")
1181 (let* ((start (field-beginning))
1183 (string (field-string))
1184 (completions (completion-all-completions
1186 minibuffer-completion-table
1187 minibuffer-completion-predicate
1188 (- (point) (field-beginning)))))
1190 (if (and completions
1191 (or (consp (cdr completions
))
1192 (not (equal (car completions
) string
))))
1193 (let* ((last (last completions
))
1194 (base-size (cdr last
))
1195 ;; If the *Completions* buffer is shown in a new
1196 ;; window, mark it as softly-dedicated, so bury-buffer in
1197 ;; minibuffer-hide-completions will know whether to
1198 ;; delete the window or not.
1199 (display-buffer-mark-dedicated 'soft
))
1200 (with-output-to-temp-buffer "*Completions*"
1201 ;; Remove the base-size tail because `sort' requires a properly
1202 ;; nil-terminated list.
1203 (when last
(setcdr last nil
))
1204 (setq completions
(sort completions
'string-lessp
))
1205 (when completion-annotate-function
1209 (funcall completion-annotate-function s
)))
1210 (if ann
(list s ann
) s
)))
1212 (with-current-buffer standard-output
1213 (set (make-local-variable 'completion-base-position
)
1214 (list (+ start base-size
)
1215 ;; FIXME: We should pay attention to completion
1216 ;; boundaries here, but currently
1217 ;; completion-all-completions does not give us the
1218 ;; necessary information.
1220 (display-completion-list completions
)))
1222 ;; If there are no completions, or if the current input is already the
1223 ;; only possible completion, then hide (previous&stale) completions.
1224 (minibuffer-hide-completions)
1227 (if completions
"Sole completion" "No completions")))
1230 (defun minibuffer-hide-completions ()
1231 "Get rid of an out-of-date *Completions* buffer."
1232 ;; FIXME: We could/should use minibuffer-scroll-window here, but it
1233 ;; can also point to the minibuffer-parent-window, so it's a bit tricky.
1234 (let ((win (get-buffer-window "*Completions*" 0)))
1235 (if win
(with-selected-window win
(bury-buffer)))))
1237 (defun exit-minibuffer ()
1238 "Terminate this minibuffer argument."
1240 ;; If the command that uses this has made modifications in the minibuffer,
1241 ;; we don't want them to cause deactivation of the mark in the original
1243 ;; A better solution would be to make deactivate-mark buffer-local
1244 ;; (or to turn it into a list of buffers, ...), but in the mean time,
1245 ;; this should do the trick in most cases.
1246 (setq deactivate-mark nil
)
1249 (defun self-insert-and-exit ()
1250 "Terminate minibuffer input."
1252 (if (characterp last-command-event
)
1253 (call-interactively 'self-insert-command
)
1257 (defvar completion-in-region-functions nil
1258 "Wrapper hook around `completion-in-region'.
1259 The functions on this special hook are called with 5 arguments:
1260 NEXT-FUN START END COLLECTION PREDICATE.
1261 NEXT-FUN is a function of four arguments (START END COLLECTION PREDICATE)
1262 that performs the default operation. The other four arguments are like
1263 the ones passed to `completion-in-region'. The functions on this hook
1264 are expected to perform completion on START..END using COLLECTION
1265 and PREDICATE, either by calling NEXT-FUN or by doing it themselves.")
1267 (defvar completion-in-region--data nil
)
1269 (defvar completion-in-region-mode-predicate nil
1270 "Predicate to tell `completion-in-region-mode' when to exit.
1271 It is called with no argument and should return nil when
1272 `completion-in-region-mode' should exit (and hence pop down
1273 the *Completions* buffer).")
1275 (defvar completion-in-region-mode--predicate nil
1276 "Copy of the value of `completion-in-region-mode-predicate'.
1277 This holds the value `completion-in-region-mode-predicate' had when
1278 we entered `completion-in-region-mode'.")
1280 (defun completion-in-region (start end collection
&optional predicate
)
1281 "Complete the text between START and END using COLLECTION.
1282 Return nil if there is no valid completion, else t.
1283 Point needs to be somewhere between START and END."
1284 (assert (<= start
(point)) (<= (point) end
))
1286 ;; FIXME: Maybe we should use this hook to provide a "display
1287 ;; completions" operation as well.
1288 completion-in-region-functions
(start end collection predicate
)
1289 (let ((minibuffer-completion-table collection
)
1290 (minibuffer-completion-predicate predicate
)
1291 (ol (make-overlay start end nil nil t
)))
1292 (overlay-put ol
'field
'completion
)
1293 (when completion-in-region-mode-predicate
1294 (completion-in-region-mode 1)
1295 (setq completion-in-region--data
1296 (list (current-buffer) start end collection
)))
1298 (call-interactively 'minibuffer-complete
)
1299 (delete-overlay ol
)))))
1301 (defvar completion-in-region-mode-map
1302 (let ((map (make-sparse-keymap)))
1303 ;; FIXME: Only works if completion-in-region-mode was activated via
1304 ;; completion-at-point called directly.
1305 (define-key map
"?" 'completion-help-at-point
)
1306 (define-key map
"\t" 'completion-at-point
)
1308 "Keymap activated during `completion-in-region'.")
1310 ;; It is difficult to know when to exit completion-in-region-mode (i.e. hide
1311 ;; the *Completions*).
1312 ;; - lisp-mode: never.
1313 ;; - comint: only do it if you hit SPC at the right time.
1314 ;; - pcomplete: pop it down on SPC or after some time-delay.
1315 ;; - semantic: use a post-command-hook check similar to this one.
1316 (defun completion-in-region--postch ()
1317 (or unread-command-events
;Don't pop down the completions in the middle of
1318 ;mouse-drag-region/mouse-set-point.
1319 (and completion-in-region--data
1320 (and (eq (car completion-in-region--data
)
1322 (>= (point) (nth 1 completion-in-region--data
))
1325 (goto-char (nth 2 completion-in-region--data
))
1326 (line-end-position)))
1327 (funcall completion-in-region-mode--predicate
)))
1328 (completion-in-region-mode -
1)))
1330 ;; (defalias 'completion-in-region--prech 'completion-in-region--postch)
1332 (define-minor-mode completion-in-region-mode
1333 "Transient minor mode used during `completion-in-region'."
1335 (setq completion-in-region--data nil
)
1336 ;; (remove-hook 'pre-command-hook #'completion-in-region--prech)
1337 (remove-hook 'post-command-hook
#'completion-in-region--postch
)
1338 (setq minor-mode-overriding-map-alist
1339 (delq (assq 'completion-in-region-mode minor-mode-overriding-map-alist
)
1340 minor-mode-overriding-map-alist
))
1341 (if (null completion-in-region-mode
)
1342 (unless (equal "*Completions*" (buffer-name (window-buffer)))
1343 (minibuffer-hide-completions))
1344 ;; (add-hook 'pre-command-hook #'completion-in-region--prech)
1345 (assert completion-in-region-mode-predicate
)
1346 (setq completion-in-region-mode--predicate
1347 completion-in-region-mode-predicate
)
1348 (add-hook 'post-command-hook
#'completion-in-region--postch
)
1349 (push `(completion-in-region-mode .
,completion-in-region-mode-map
)
1350 minor-mode-overriding-map-alist
)))
1352 ;; Define-minor-mode added our keymap to minor-mode-map-alist, but we want it
1353 ;; on minor-mode-overriding-map-alist instead.
1354 (setq minor-mode-map-alist
1355 (delq (assq 'completion-in-region-mode minor-mode-map-alist
)
1356 minor-mode-map-alist
))
1358 (defvar completion-at-point-functions
'(tags-completion-at-point-function)
1359 "Special hook to find the completion table for the thing at point.
1360 Each function on this hook is called in turns without any argument and should
1361 return either nil to mean that it is not applicable at point,
1362 or a function of no argument to perform completion (discouraged),
1363 or a list of the form (START END COLLECTION &rest PROPS) where
1364 START and END delimit the entity to complete and should include point,
1365 COLLECTION is the completion table to use to complete it, and
1366 PROPS is a property list for additional information.
1367 Currently supported properties are:
1368 `:predicate' a predicate that completion candidates need to satisfy.
1369 `:annotation-function' the value to use for `completion-annotate-function'.")
1371 (defvar completion--capf-misbehave-funs nil
1372 "List of functions found on `completion-at-point-functions' that misbehave.")
1373 (defvar completion--capf-safe-funs nil
1374 "List of well-behaved functions found on `completion-at-point-functions'.")
1376 (defun completion--capf-wrapper (fun which
)
1377 ;; FIXME: The safe/misbehave handling assumes that a given function will
1378 ;; always return the same kind of data, but this breaks down with functions
1379 ;; like comint-completion-at-point or mh-letter-completion-at-point, which
1380 ;; could be sometimes safe and sometimes misbehaving (and sometimes neither).
1383 (safe (member fun completion--capf-safe-funs
))
1384 (optimist (not (member fun completion--capf-misbehave-funs
))))
1385 (let ((res (funcall fun
)))
1388 (unless (member fun completion--capf-safe-funs
)
1389 (push fun completion--capf-safe-funs
)))
1390 ((not (or (listp res
) (functionp res
)))
1391 (unless (member fun completion--capf-misbehave-funs
)
1393 "Completion function %S uses a deprecated calling convention" fun
)
1394 (push fun completion--capf-misbehave-funs
))))
1395 (if res
(cons fun res
)))))
1397 (defun completion-at-point ()
1398 "Perform completion on the text around point.
1399 The completion method is determined by `completion-at-point-functions'."
1401 (let ((res (run-hook-wrapped 'completion-at-point-functions
1402 #'completion--capf-wrapper
'all
)))
1404 (`(,_ .
,(and (pred functionp
) f
)) (funcall f
))
1405 (`(,hookfun .
(,start
,end
,collection .
,plist
))
1406 (let* ((completion-annotate-function
1407 (or (plist-get plist
:annotation-function
)
1408 completion-annotate-function
))
1409 (completion-in-region-mode-predicate
1411 ;; We're still in the same completion field.
1412 (eq (car-safe (funcall hookfun
)) start
))))
1413 (completion-in-region start end collection
1414 (plist-get plist
:predicate
))))
1415 ;; Maybe completion already happened and the function returned t.
1418 (defun completion-help-at-point ()
1419 "Display the completions on the text around point.
1420 The completion method is determined by `completion-at-point-functions'."
1422 (let ((res (run-hook-wrapped 'completion-at-point-functions
1423 ;; Ignore misbehaving functions.
1424 #'completion--capf-wrapper
'optimist
)))
1426 (`(,_ .
,(and (pred functionp
) f
))
1427 (message "Don't know how to show completions for %S" f
))
1428 (`(,hookfun .
(,start
,end
,collection .
,plist
))
1429 (let* ((minibuffer-completion-table collection
)
1430 (minibuffer-completion-predicate (plist-get plist
:predicate
))
1431 (completion-annotate-function
1432 (or (plist-get plist
:annotation-function
)
1433 completion-annotate-function
))
1434 (completion-in-region-mode-predicate
1436 ;; We're still in the same completion field.
1437 (eq (car-safe (funcall hookfun
)) start
)))
1438 (ol (make-overlay start end nil nil t
)))
1439 ;; FIXME: We should somehow (ab)use completion-in-region-function or
1440 ;; introduce a corresponding hook (plus another for word-completion,
1441 ;; and another for force-completion, maybe?).
1442 (overlay-put ol
'field
'completion
)
1443 (completion-in-region-mode 1)
1444 (setq completion-in-region--data
1445 (list (current-buffer) start end collection
))
1447 (call-interactively 'minibuffer-completion-help
)
1448 (delete-overlay ol
))))
1450 ;; The hook function already performed completion :-(
1451 ;; Not much we can do at this point.
1452 (message "%s already performed completion!" hookfun
)
1454 (_ (message "Nothing to complete at point")))))
1458 (define-obsolete-variable-alias 'minibuffer-local-must-match-filename-map
1459 'minibuffer-local-filename-must-match-map
"23.1")
1461 (let ((map minibuffer-local-map
))
1462 (define-key map
"\C-g" 'abort-recursive-edit
)
1463 (define-key map
"\r" 'exit-minibuffer
)
1464 (define-key map
"\n" 'exit-minibuffer
))
1466 (let ((map minibuffer-local-completion-map
))
1467 (define-key map
"\t" 'minibuffer-complete
)
1468 ;; M-TAB is already abused for many other purposes, so we should find
1469 ;; another binding for it.
1470 ;; (define-key map "\e\t" 'minibuffer-force-complete)
1471 (define-key map
" " 'minibuffer-complete-word
)
1472 (define-key map
"?" 'minibuffer-completion-help
))
1474 (let ((map minibuffer-local-must-match-map
))
1475 (define-key map
"\r" 'minibuffer-complete-and-exit
)
1476 (define-key map
"\n" 'minibuffer-complete-and-exit
))
1478 (let ((map minibuffer-local-filename-completion-map
))
1479 (define-key map
" " nil
))
1480 (let ((map minibuffer-local-filename-must-match-map
))
1481 (define-key map
" " nil
))
1483 (let ((map minibuffer-local-ns-map
))
1484 (define-key map
" " 'exit-minibuffer
)
1485 (define-key map
"\t" 'exit-minibuffer
)
1486 (define-key map
"?" 'self-insert-and-exit
))
1488 ;;; Completion tables.
1490 (defun minibuffer--double-dollars (str)
1491 (replace-regexp-in-string "\\$" "$$" str
))
1493 (defun completion--make-envvar-table ()
1494 (mapcar (lambda (enventry)
1495 (substring enventry
0 (string-match-p "=" enventry
)))
1496 process-environment
))
1498 (defconst completion--embedded-envvar-re
1499 (concat "\\(?:^\\|[^$]\\(?:\\$\\$\\)*\\)"
1500 "$\\([[:alnum:]_]*\\|{\\([^}]*\\)\\)\\'"))
1502 (defun completion--embedded-envvar-table (string _pred action
)
1503 "Completion table for envvars embedded in a string.
1504 The envvar syntax (and escaping) rules followed by this table are the
1505 same as `substitute-in-file-name'."
1506 ;; We ignore `pred', because the predicates passed to us via
1507 ;; read-file-name-internal are not 100% correct and fail here:
1508 ;; e.g. we get predicates like file-directory-p there, whereas the filename
1509 ;; completed needs to be passed through substitute-in-file-name before it
1510 ;; can be passed to file-directory-p.
1511 (when (string-match completion--embedded-envvar-re string
)
1512 (let* ((beg (or (match-beginning 2) (match-beginning 1)))
1513 (table (completion--make-envvar-table))
1514 (prefix (substring string
0 beg
)))
1516 ((eq action
'lambda
)
1517 ;; This table is expected to be used in conjunction with some
1518 ;; other table that provides the "main" completion. Let the
1519 ;; other table handle the test-completion case.
1521 ((eq (car-safe action
) 'boundaries
)
1522 ;; Only return boundaries if there's something to complete,
1523 ;; since otherwise when we're used in
1524 ;; completion-table-in-turn, we could return boundaries and
1525 ;; let some subsequent table return a list of completions.
1526 ;; FIXME: Maybe it should rather be fixed in
1527 ;; completion-table-in-turn instead, but it's difficult to
1528 ;; do it efficiently there.
1529 (when (try-completion (substring string beg
) table nil
)
1530 ;; Compute the boundaries of the subfield to which this
1531 ;; completion applies.
1532 (let ((suffix (cdr action
)))
1534 (or (match-beginning 2) (match-beginning 1))
1535 (when (string-match "[^[:alnum:]_]" suffix
)
1536 (match-beginning 0))))))
1538 (if (eq (aref string
(1- beg
)) ?
{)
1539 (setq table
(apply-partially 'completion-table-with-terminator
1541 ;; Even if file-name completion is case-insensitive, we want
1542 ;; envvar completion to be case-sensitive.
1543 (let ((completion-ignore-case nil
))
1544 (completion-table-with-context
1545 prefix table
(substring string beg
) nil action
)))))))
1547 (defun completion-file-name-table (string pred action
)
1548 "Completion table for file names."
1551 ((eq (car-safe action
) 'boundaries
)
1552 (let ((start (length (file-name-directory string
)))
1553 (end (string-match-p "/" (cdr action
))))
1555 ;; if `string' is "C:" in w32, (file-name-directory string)
1556 ;; returns "C:/", so `start' is 3 rather than 2.
1557 ;; Not quite sure what is The Right Fix, but clipping it
1558 ;; back to 2 will work for this particular case. We'll
1559 ;; see if we can come up with a better fix when we bump
1560 ;; into more such problematic cases.
1561 (min start
(length string
)) end
)))
1563 ((eq action
'lambda
)
1564 (if (zerop (length string
))
1565 nil
;Not sure why it's here, but it probably doesn't harm.
1566 (funcall (or pred
'file-exists-p
) string
)))
1569 (let* ((name (file-name-nondirectory string
))
1570 (specdir (file-name-directory string
))
1571 (realdir (or specdir default-directory
)))
1575 (let ((comp (file-name-completion name realdir pred
)))
1577 (concat specdir comp
)
1581 (let ((all (file-name-all-completions name realdir
)))
1583 ;; Check the predicate, if necessary.
1584 (unless (memq pred
'(nil file-exists-p
))
1587 (if (eq pred
'file-directory-p
)
1588 ;; Brute-force speed up for directory checking:
1589 ;; Discard strings which don't end in a slash.
1591 (let ((len (length s
)))
1592 (and (> len
0) (eq (aref s
(1- len
)) ?
/))))
1593 ;; Must do it the hard (and slow) way.
1595 (let ((default-directory (expand-file-name realdir
)))
1597 (if (funcall pred tem
) (push tem comp
))))
1598 (setq all
(nreverse comp
))))
1602 (defvar read-file-name-predicate nil
1603 "Current predicate used by `read-file-name-internal'.")
1604 (make-obsolete-variable 'read-file-name-predicate
1605 "use the regular PRED argument" "23.2")
1607 (defun completion--file-name-table (string pred action
)
1608 "Internal subroutine for `read-file-name'. Do not call this.
1609 This is a completion table for file names, like `completion-file-name-table'
1610 except that it passes the file name through `substitute-in-file-name'."
1612 ((eq (car-safe action
) 'boundaries
)
1613 ;; For the boundaries, we can't really delegate to
1614 ;; substitute-in-file-name+completion-file-name-table and then fix
1615 ;; them up (as we do for the other actions), because it would
1616 ;; require us to track the relationship between `str' and
1617 ;; `string', which is difficult. And in any case, if
1618 ;; substitute-in-file-name turns "fo-$TO-ba" into "fo-o/b-ba",
1619 ;; there's no way for us to return proper boundaries info, because
1620 ;; the boundary is not (yet) in `string'.
1622 ;; FIXME: Actually there is a way to return correct boundaries
1623 ;; info, at the condition of modifying the all-completions
1624 ;; return accordingly. But for now, let's not bother.
1625 (completion-file-name-table string pred action
))
1628 (let* ((default-directory
1630 ;; It used to be that `pred' was abused to pass `dir'
1632 (prog1 (file-name-as-directory (expand-file-name pred
))
1635 (str (condition-case nil
1636 (substitute-in-file-name string
)
1638 (comp (completion-file-name-table
1640 (with-no-warnings (or pred read-file-name-predicate
))
1645 ;; Requote the $s before returning the completion.
1646 (minibuffer--double-dollars comp
))
1647 ((and (null action
) comp
1648 ;; Requote the $s before checking for changes.
1649 (setq str
(minibuffer--double-dollars str
))
1650 (not (string-equal string str
)))
1651 ;; If there's no real completion, but substitute-in-file-name
1652 ;; changed the string, then return the new string.
1656 (defalias 'read-file-name-internal
1657 (completion-table-in-turn 'completion--embedded-envvar-table
1658 'completion--file-name-table
)
1659 "Internal subroutine for `read-file-name'. Do not call this.")
1661 (defvar read-file-name-function
'read-file-name-default
1662 "The function called by `read-file-name' to do its work.
1663 It should accept the same arguments as `read-file-name'.")
1665 (defcustom read-file-name-completion-ignore-case
1666 (if (memq system-type
'(ms-dos windows-nt darwin cygwin
))
1668 "Non-nil means when reading a file name completion ignores case."
1673 (defcustom insert-default-directory t
1674 "Non-nil means when reading a filename start with default dir in minibuffer.
1676 When the initial minibuffer contents show a name of a file or a directory,
1677 typing RETURN without editing the initial contents is equivalent to typing
1678 the default file name.
1680 If this variable is non-nil, the minibuffer contents are always
1681 initially non-empty, and typing RETURN without editing will fetch the
1682 default name, if one is provided. Note however that this default name
1683 is not necessarily the same as initial contents inserted in the minibuffer,
1684 if the initial contents is just the default directory.
1686 If this variable is nil, the minibuffer often starts out empty. In
1687 that case you may have to explicitly fetch the next history element to
1688 request the default name; typing RETURN without editing will leave
1689 the minibuffer empty.
1691 For some commands, exiting with an empty minibuffer has a special meaning,
1692 such as making the current buffer visit no file in the case of
1693 `set-visited-file-name'."
1697 ;; Not always defined, but only called if next-read-file-uses-dialog-p says so.
1698 (declare-function x-file-dialog
"xfns.c"
1699 (prompt dir
&optional default-filename mustmatch only-dir-p
))
1701 (defun read-file-name--defaults (&optional dir initial
)
1704 ;; With non-nil `initial', use `dir' as the first default.
1705 ;; Essentially, this mean reversing the normal order of the
1706 ;; current directory name and the current file name, i.e.
1707 ;; 1. with normal file reading:
1708 ;; 1.1. initial input is the current directory
1709 ;; 1.2. the first default is the current file name
1710 ;; 2. with non-nil `initial' (e.g. for `find-alternate-file'):
1711 ;; 2.2. initial input is the current file name
1712 ;; 2.1. the first default is the current directory
1713 (initial (abbreviate-file-name dir
))
1714 ;; In file buffers, try to get the current file name
1716 (abbreviate-file-name buffer-file-name
))))
1718 (run-hook-with-args-until-success 'file-name-at-point-functions
)))
1719 (when file-name-at-point
1720 (setq default
(delete-dups
1721 (delete "" (delq nil
(list file-name-at-point default
))))))
1722 ;; Append new defaults to the end of existing `minibuffer-default'.
1724 (if (listp minibuffer-default
) minibuffer-default
(list minibuffer-default
))
1725 (if (listp default
) default
(list default
)))))
1727 (defun read-file-name (prompt &optional dir default-filename mustmatch initial predicate
)
1728 "Read file name, prompting with PROMPT and completing in directory DIR.
1729 Value is not expanded---you must call `expand-file-name' yourself.
1730 Default name to DEFAULT-FILENAME if user exits the minibuffer with
1731 the same non-empty string that was inserted by this function.
1732 (If DEFAULT-FILENAME is omitted, the visited file name is used,
1733 except that if INITIAL is specified, that combined with DIR is used.
1734 If DEFAULT-FILENAME is a list of file names, the first file name is used.)
1735 If the user exits with an empty minibuffer, this function returns
1736 an empty string. (This can only happen if the user erased the
1737 pre-inserted contents or if `insert-default-directory' is nil.)
1739 Fourth arg MUSTMATCH can take the following values:
1740 - nil means that the user can exit with any input.
1741 - t means that the user is not allowed to exit unless
1742 the input is (or completes to) an existing file.
1743 - `confirm' means that the user can exit with any input, but she needs
1744 to confirm her choice if the input is not an existing file.
1745 - `confirm-after-completion' means that the user can exit with any
1746 input, but she needs to confirm her choice if she called
1747 `minibuffer-complete' right before `minibuffer-complete-and-exit'
1748 and the input is not an existing file.
1749 - anything else behaves like t except that typing RET does not exit if it
1750 does non-null completion.
1752 Fifth arg INITIAL specifies text to start with.
1754 If optional sixth arg PREDICATE is non-nil, possible completions and
1755 the resulting file name must satisfy (funcall PREDICATE NAME).
1756 DIR should be an absolute directory name. It defaults to the value of
1757 `default-directory'.
1759 If this command was invoked with the mouse, use a graphical file
1760 dialog if `use-dialog-box' is non-nil, and the window system or X
1761 toolkit in use provides a file dialog box, and DIR is not a
1762 remote file. For graphical file dialogs, any the special values
1763 of MUSTMATCH; `confirm' and `confirm-after-completion' are
1764 treated as equivalent to nil.
1766 See also `read-file-name-completion-ignore-case'
1767 and `read-file-name-function'."
1768 (funcall (or read-file-name-function
#'read-file-name-default
)
1769 prompt dir default-filename mustmatch initial predicate
))
1771 (defun read-file-name-default (prompt &optional dir default-filename mustmatch initial predicate
)
1772 "Default method for reading file names.
1773 See `read-file-name' for the meaning of the arguments."
1774 (unless dir
(setq dir default-directory
))
1775 (unless (file-name-absolute-p dir
) (setq dir
(expand-file-name dir
)))
1776 (unless default-filename
1777 (setq default-filename
(if initial
(expand-file-name initial dir
)
1779 ;; If dir starts with user's homedir, change that to ~.
1780 (setq dir
(abbreviate-file-name dir
))
1781 ;; Likewise for default-filename.
1782 (if default-filename
1783 (setq default-filename
1784 (if (consp default-filename
)
1785 (mapcar 'abbreviate-file-name default-filename
)
1786 (abbreviate-file-name default-filename
))))
1788 ((and insert-default-directory
(stringp dir
))
1790 (cons (minibuffer--double-dollars (concat dir initial
))
1791 (length (minibuffer--double-dollars dir
)))
1792 (minibuffer--double-dollars dir
)))
1793 (initial (cons (minibuffer--double-dollars initial
) 0)))))
1795 (let ((completion-ignore-case read-file-name-completion-ignore-case
)
1796 (minibuffer-completing-file-name t
)
1797 (pred (or predicate
'file-exists-p
))
1798 (add-to-history nil
))
1801 (if (or (not (next-read-file-uses-dialog-p))
1802 ;; Graphical file dialogs can't handle remote
1804 (file-remote-p dir
))
1805 ;; We used to pass `dir' to `read-file-name-internal' by
1806 ;; abusing the `predicate' argument. It's better to
1807 ;; just use `default-directory', but in order to avoid
1808 ;; changing `default-directory' in the current buffer,
1809 ;; we don't let-bind it.
1810 (let ((dir (file-name-as-directory
1811 (expand-file-name dir
))))
1812 (minibuffer-with-setup-hook
1814 (setq default-directory dir
)
1815 ;; When the first default in `minibuffer-default'
1816 ;; duplicates initial input `insdef',
1817 ;; reset `minibuffer-default' to nil.
1818 (when (equal (or (car-safe insdef
) insdef
)
1819 (or (car-safe minibuffer-default
)
1820 minibuffer-default
))
1821 (setq minibuffer-default
1822 (cdr-safe minibuffer-default
)))
1823 ;; On the first request on `M-n' fill
1824 ;; `minibuffer-default' with a list of defaults
1825 ;; relevant for file-name reading.
1826 (set (make-local-variable 'minibuffer-default-add-function
)
1828 (with-current-buffer
1829 (window-buffer (minibuffer-selected-window))
1830 (read-file-name--defaults dir initial
)))))
1831 (completing-read prompt
'read-file-name-internal
1832 pred mustmatch insdef
1833 'file-name-history default-filename
)))
1834 ;; If DEFAULT-FILENAME not supplied and DIR contains
1835 ;; a file name, split it.
1836 (let ((file (file-name-nondirectory dir
))
1837 ;; When using a dialog, revert to nil and non-nil
1838 ;; interpretation of mustmatch. confirm options
1839 ;; need to be interpreted as nil, otherwise
1840 ;; it is impossible to create new files using
1841 ;; dialogs with the default settings.
1843 (not (memq mustmatch
1844 '(nil confirm confirm-after-completion
)))))
1845 (when (and (not default-filename
)
1846 (not (zerop (length file
))))
1847 (setq default-filename file
)
1848 (setq dir
(file-name-directory dir
)))
1849 (when default-filename
1850 (setq default-filename
1851 (expand-file-name (if (consp default-filename
)
1852 (car default-filename
)
1855 (setq add-to-history t
)
1856 (x-file-dialog prompt dir default-filename
1858 (eq predicate
'file-directory-p
)))))
1860 (replace-in-history (eq (car-safe file-name-history
) val
)))
1861 ;; If completing-read returned the inserted default string itself
1862 ;; (rather than a new string with the same contents),
1863 ;; it has to mean that the user typed RET with the minibuffer empty.
1864 ;; In that case, we really want to return ""
1865 ;; so that commands such as set-visited-file-name can distinguish.
1866 (when (consp default-filename
)
1867 (setq default-filename
(car default-filename
)))
1868 (when (eq val default-filename
)
1869 ;; In this case, completing-read has not added an element
1870 ;; to the history. Maybe we should.
1871 (if (not replace-in-history
)
1872 (setq add-to-history t
))
1874 (unless val
(error "No file name specified"))
1876 (if (and default-filename
1877 (string-equal val
(if (consp insdef
) (car insdef
) insdef
)))
1878 (setq val default-filename
))
1879 (setq val
(substitute-in-file-name val
))
1881 (if replace-in-history
1882 ;; Replace what Fcompleting_read added to the history
1883 ;; with what we will actually return. As an exception,
1884 ;; if that's the same as the second item in
1885 ;; file-name-history, it's really a repeat (Bug#4657).
1886 (let ((val1 (minibuffer--double-dollars val
)))
1887 (if history-delete-duplicates
1888 (setcdr file-name-history
1889 (delete val1
(cdr file-name-history
))))
1890 (if (string= val1
(cadr file-name-history
))
1891 (pop file-name-history
)
1892 (setcar file-name-history val1
)))
1894 ;; Add the value to the history--but not if it matches
1895 ;; the last value already there.
1896 (let ((val1 (minibuffer--double-dollars val
)))
1897 (unless (and (consp file-name-history
)
1898 (equal (car file-name-history
) val1
))
1899 (setq file-name-history
1901 (if history-delete-duplicates
1902 (delete val1 file-name-history
)
1903 file-name-history
)))))))
1906 (defun internal-complete-buffer-except (&optional buffer
)
1907 "Perform completion on all buffers excluding BUFFER.
1908 BUFFER nil or omitted means use the current buffer.
1909 Like `internal-complete-buffer', but removes BUFFER from the completion list."
1910 (let ((except (if (stringp buffer
) buffer
(buffer-name buffer
))))
1911 (apply-partially 'completion-table-with-predicate
1912 'internal-complete-buffer
1914 (not (equal (if (consp name
) (car name
) name
) except
)))
1917 ;;; Old-style completion, used in Emacs-21 and Emacs-22.
1919 (defun completion-emacs21-try-completion (string table pred _point
)
1920 (let ((completion (try-completion string table pred
)))
1921 (if (stringp completion
)
1922 (cons completion
(length completion
))
1925 (defun completion-emacs21-all-completions (string table pred _point
)
1926 (completion-hilit-commonality
1927 (all-completions string table pred
)
1929 (car (completion-boundaries string table pred
""))))
1931 (defun completion-emacs22-try-completion (string table pred point
)
1932 (let ((suffix (substring string point
))
1933 (completion (try-completion (substring string
0 point
) table pred
)))
1934 (if (not (stringp completion
))
1936 ;; Merge a trailing / in completion with a / after point.
1937 ;; We used to only do it for word completion, but it seems to make
1938 ;; sense for all completions.
1939 ;; Actually, claiming this feature was part of Emacs-22 completion
1940 ;; is pushing it a bit: it was only done in minibuffer-completion-word,
1941 ;; which was (by default) not bound during file completion, where such
1942 ;; slashes are most likely to occur.
1943 (if (and (not (zerop (length completion
)))
1944 (eq ?
/ (aref completion
(1- (length completion
))))
1945 (not (zerop (length suffix
)))
1946 (eq ?
/ (aref suffix
0)))
1947 ;; This leaves point after the / .
1948 (setq suffix
(substring suffix
1)))
1949 (cons (concat completion suffix
) (length completion
)))))
1951 (defun completion-emacs22-all-completions (string table pred point
)
1952 (let ((beforepoint (substring string
0 point
)))
1953 (completion-hilit-commonality
1954 (all-completions beforepoint table pred
)
1956 (car (completion-boundaries beforepoint table pred
"")))))
1958 ;;; Basic completion.
1960 (defun completion--merge-suffix (completion point suffix
)
1961 "Merge end of COMPLETION with beginning of SUFFIX.
1962 Simple generalization of the \"merge trailing /\" done in Emacs-22.
1963 Return the new suffix."
1964 (if (and (not (zerop (length suffix
)))
1965 (string-match "\\(.+\\)\n\\1" (concat completion
"\n" suffix
)
1966 ;; Make sure we don't compress things to less
1967 ;; than we started with.
1969 ;; Just make sure we didn't match some other \n.
1970 (eq (match-end 1) (length completion
)))
1971 (substring suffix
(- (match-end 1) (match-beginning 1)))
1972 ;; Nothing to merge.
1975 (defun completion-basic--pattern (beforepoint afterpoint bounds
)
1977 "" (list (substring beforepoint
(car bounds
))
1979 (substring afterpoint
0 (cdr bounds
)))))
1981 (defun completion-basic-try-completion (string table pred point
)
1982 (let* ((beforepoint (substring string
0 point
))
1983 (afterpoint (substring string point
))
1984 (bounds (completion-boundaries beforepoint table pred afterpoint
)))
1985 (if (zerop (cdr bounds
))
1986 ;; `try-completion' may return a subtly different result
1987 ;; than `all+merge', so try to use it whenever possible.
1988 (let ((completion (try-completion beforepoint table pred
)))
1989 (if (not (stringp completion
))
1993 (completion--merge-suffix completion point afterpoint
))
1994 (length completion
))))
1995 (let* ((suffix (substring afterpoint
(cdr bounds
)))
1996 (prefix (substring beforepoint
0 (car bounds
)))
1998 "" (list (substring beforepoint
(car bounds
))
2000 (substring afterpoint
0 (cdr bounds
)))))
2001 (all (completion-pcm--all-completions prefix pattern table pred
)))
2002 (if minibuffer-completing-file-name
2003 (setq all
(completion-pcm--filename-try-filter all
)))
2004 (completion-pcm--merge-try pattern all prefix suffix
)))))
2006 (defun completion-basic-all-completions (string table pred point
)
2007 (let* ((beforepoint (substring string
0 point
))
2008 (afterpoint (substring string point
))
2009 (bounds (completion-boundaries beforepoint table pred afterpoint
))
2010 ;; (suffix (substring afterpoint (cdr bounds)))
2011 (prefix (substring beforepoint
0 (car bounds
)))
2013 "" (list (substring beforepoint
(car bounds
))
2015 (substring afterpoint
0 (cdr bounds
)))))
2016 (all (completion-pcm--all-completions prefix pattern table pred
)))
2017 (completion-hilit-commonality all point
(car bounds
))))
2019 ;;; Partial-completion-mode style completion.
2021 (defvar completion-pcm--delim-wild-regex nil
2022 "Regular expression matching delimiters controlling the partial-completion.
2023 Typically, this regular expression simply matches a delimiter, meaning
2024 that completion can add something at (match-beginning 0), but if it has
2025 a submatch 1, then completion can add something at (match-end 1).
2026 This is used when the delimiter needs to be of size zero (e.g. the transition
2027 from lowercase to uppercase characters).")
2029 (defun completion-pcm--prepare-delim-re (delims)
2030 (setq completion-pcm--delim-wild-regex
(concat "[" delims
"*]")))
2032 (defcustom completion-pcm-word-delimiters
"-_./: "
2033 "A string of characters treated as word delimiters for completion.
2035 If `]' is in this string, it must come first.
2036 If `^' is in this string, it must not come first.
2037 If `-' is in this string, it must come first or right after `]'.
2038 In other words, if S is this string, then `[S]' must be a valid Emacs regular
2039 expression (not containing character ranges like `a-z')."
2040 :set
(lambda (symbol value
)
2041 (set-default symbol value
)
2042 ;; Refresh other vars.
2043 (completion-pcm--prepare-delim-re value
))
2044 :initialize
'custom-initialize-reset
2048 (defcustom completion-pcm-complete-word-inserts-delimiters nil
2049 "Treat the SPC or - inserted by `minibuffer-complete-word' as delimiters.
2050 Those chars are treated as delimiters iff this variable is non-nil.
2051 I.e. if non-nil, M-x SPC will just insert a \"-\" in the minibuffer, whereas
2052 if nil, it will list all possible commands in *Completions* because none of
2053 the commands start with a \"-\" or a SPC."
2056 (defun completion-pcm--pattern-trivial-p (pattern)
2057 (and (stringp (car pattern
))
2058 ;; It can be followed by `point' and "" and still be trivial.
2060 (dolist (elem (cdr pattern
))
2061 (unless (member elem
'(point ""))
2062 (setq trivial nil
)))
2065 (defun completion-pcm--string->pattern
(string &optional point
)
2066 "Split STRING into a pattern.
2067 A pattern is a list where each element is either a string
2068 or a symbol chosen among `any', `star', `point', `prefix'."
2069 (if (and point
(< point
(length string
)))
2070 (let ((prefix (substring string
0 point
))
2071 (suffix (substring string point
)))
2072 (append (completion-pcm--string->pattern prefix
)
2074 (completion-pcm--string->pattern suffix
)))
2075 (let* ((pattern nil
)
2079 (while (and (setq p
(string-match completion-pcm--delim-wild-regex
2081 (or completion-pcm-complete-word-inserts-delimiters
2082 ;; If the char was added by minibuffer-complete-word,
2083 ;; then don't treat it as a delimiter, otherwise
2084 ;; "M-x SPC" ends up inserting a "-" rather than listing
2086 (not (get-text-property p
'completion-try-word string
))))
2087 ;; Usually, completion-pcm--delim-wild-regex matches a delimiter,
2088 ;; meaning that something can be added *before* it, but it can also
2089 ;; match a prefix and postfix, in which case something can be added
2090 ;; in-between (e.g. match [[:lower:]][[:upper:]]).
2091 ;; This is determined by the presence of a submatch-1 which delimits
2093 (if (match-end 1) (setq p
(match-end 1)))
2094 (push (substring string p0 p
) pattern
)
2095 (if (eq (aref string p
) ?
*)
2097 (push 'star pattern
)
2103 ;; An empty string might be erroneously added at the beginning.
2104 ;; It should be avoided properly, but it's so easy to remove it here.
2105 (delete "" (nreverse (cons (substring string p0
) pattern
))))))
2107 (defun completion-pcm--pattern->regex
(pattern &optional group
)
2113 ((stringp x
) (regexp-quote x
))
2114 ((if (consp group
) (memq x group
) group
) "\\(.*?\\)")
2118 ;; Avoid pathological backtracking.
2119 (while (string-match "\\.\\*\\?\\(?:\\\\[()]\\)*\\(\\.\\*\\?\\)" re
)
2120 (setq re
(replace-match "" t t re
1)))
2123 (defun completion-pcm--all-completions (prefix pattern table pred
)
2124 "Find all completions for PATTERN in TABLE obeying PRED.
2125 PATTERN is as returned by `completion-pcm--string->pattern'."
2126 ;; (assert (= (car (completion-boundaries prefix table pred ""))
2127 ;; (length prefix)))
2128 ;; Find an initial list of possible completions.
2129 (if (completion-pcm--pattern-trivial-p pattern
)
2131 ;; Minibuffer contains no delimiters -- simple case!
2132 (all-completions (concat prefix
(car pattern
)) table pred
)
2134 ;; Use all-completions to do an initial cull. This is a big win,
2135 ;; since all-completions is written in C!
2136 (let* (;; Convert search pattern to a standard regular expression.
2137 (regex (completion-pcm--pattern->regex pattern
))
2138 (case-fold-search completion-ignore-case
)
2139 (completion-regexp-list (cons regex completion-regexp-list
))
2140 (compl (all-completions
2141 (concat prefix
(if (stringp (car pattern
)) (car pattern
) ""))
2143 (if (not (functionp table
))
2144 ;; The internal functions already obeyed completion-regexp-list.
2148 (when (string-match-p regex c
) (push c poss
)))
2151 (defun completion-pcm--hilit-commonality (pattern completions
)
2153 (let* ((re (completion-pcm--pattern->regex pattern
'(point)))
2154 (case-fold-search completion-ignore-case
))
2157 ;; Don't modify the string itself.
2158 (setq str
(copy-sequence str
))
2159 (unless (string-match re str
)
2160 (error "Internal error: %s does not match %s" re str
))
2161 (let ((pos (or (match-beginning 1) (match-end 0))))
2162 (put-text-property 0 pos
2163 'font-lock-face
'completions-common-part
2165 (if (> (length str
) pos
)
2166 (put-text-property pos
(1+ pos
)
2167 'font-lock-face
'completions-first-difference
2172 (defun completion-pcm--find-all-completions (string table pred point
2174 "Find all completions for STRING at POINT in TABLE, satisfying PRED.
2175 POINT is a position inside STRING.
2176 FILTER is a function applied to the return value, that can be used, e.g. to
2177 filter out additional entries (because TABLE migth not obey PRED)."
2178 (unless filter
(setq filter
'identity
))
2179 (let* ((beforepoint (substring string
0 point
))
2180 (afterpoint (substring string point
))
2181 (bounds (completion-boundaries beforepoint table pred afterpoint
))
2182 (prefix (substring beforepoint
0 (car bounds
)))
2183 (suffix (substring afterpoint
(cdr bounds
)))
2185 (setq string
(substring string
(car bounds
) (+ point
(cdr bounds
))))
2186 (let* ((relpoint (- point
(car bounds
)))
2187 (pattern (completion-pcm--string->pattern string relpoint
))
2188 (all (condition-case err
2190 (completion-pcm--all-completions
2191 prefix pattern table pred
))
2192 (error (unless firsterror
(setq firsterror err
)) nil
))))
2193 (when (and (null all
)
2195 (null (ignore-errors (try-completion prefix table pred
))))
2196 ;; The prefix has no completions at all, so we should try and fix
2198 (let ((substring (substring prefix
0 -
1)))
2199 (destructuring-bind (subpat suball subprefix _subsuffix
)
2200 (completion-pcm--find-all-completions
2201 substring table pred
(length substring
) filter
)
2202 (let ((sep (aref prefix
(1- (length prefix
))))
2203 ;; Text that goes between the new submatches and the
2204 ;; completion substring.
2206 ;; Eliminate submatches that don't end with the separator.
2207 (dolist (submatch (prog1 suball
(setq suball
())))
2208 (when (eq sep
(aref submatch
(1- (length submatch
))))
2209 (push submatch suball
)))
2211 ;; Update the boundaries and corresponding pattern.
2212 ;; We assume that all submatches result in the same boundaries
2213 ;; since we wouldn't know how to merge them otherwise anyway.
2214 ;; FIXME: COMPLETE REWRITE!!!
2215 (let* ((newbeforepoint
2216 (concat subprefix
(car suball
)
2217 (substring string
0 relpoint
)))
2218 (leftbound (+ (length subprefix
) (length (car suball
))))
2219 (newbounds (completion-boundaries
2220 newbeforepoint table pred afterpoint
)))
2221 (unless (or (and (eq (cdr bounds
) (cdr newbounds
))
2222 (eq (car newbounds
) leftbound
))
2223 ;; Refuse new boundaries if they step over
2225 (< (car newbounds
) leftbound
))
2226 ;; The new completed prefix does change the boundaries
2227 ;; of the completed substring.
2228 (setq suffix
(substring afterpoint
(cdr newbounds
)))
2230 (concat (substring newbeforepoint
(car newbounds
))
2231 (substring afterpoint
0 (cdr newbounds
))))
2232 (setq between
(substring newbeforepoint leftbound
2234 (setq pattern
(completion-pcm--string->pattern
2236 (- (length newbeforepoint
)
2238 (dolist (submatch suball
)
2239 (setq all
(nconc (mapcar
2240 (lambda (s) (concat submatch between s
))
2242 (completion-pcm--all-completions
2243 (concat subprefix submatch between
)
2244 pattern table pred
)))
2246 ;; FIXME: This can come in handy for try-completion,
2247 ;; but isn't right for all-completions, since it lists
2248 ;; invalid completions.
2250 ;; ;; Even though we found expansions in the prefix, none
2251 ;; ;; leads to a valid completion.
2252 ;; ;; Let's keep the expansions, tho.
2253 ;; (dolist (submatch suball)
2254 ;; (push (concat submatch between newsubstring) all)))
2256 (setq pattern
(append subpat
(list 'any
(string sep
))
2257 (if between
(list between
)) pattern
))
2258 (setq prefix subprefix
)))))
2259 (if (and (null all
) firsterror
)
2260 (signal (car firsterror
) (cdr firsterror
))
2261 (list pattern all prefix suffix
)))))
2263 (defun completion-pcm-all-completions (string table pred point
)
2264 (destructuring-bind (pattern all
&optional prefix _suffix
)
2265 (completion-pcm--find-all-completions string table pred point
)
2267 (nconc (completion-pcm--hilit-commonality pattern all
)
2270 (defun completion--sreverse (str)
2271 "Like `reverse' but for a string STR rather than a list."
2272 (apply 'string
(nreverse (mapcar 'identity str
))))
2274 (defun completion--common-suffix (strs)
2275 "Return the common suffix of the strings STRS."
2276 (completion--sreverse
2279 (mapcar 'completion--sreverse strs
))))
2281 (defun completion-pcm--merge-completions (strs pattern
)
2282 "Extract the commonality in STRS, with the help of PATTERN."
2283 ;; When completing while ignoring case, we want to try and avoid
2284 ;; completing "fo" to "foO" when completing against "FOO" (bug#4219).
2285 ;; So we try and make sure that the string we return is all made up
2286 ;; of text from the completions rather than part from the
2287 ;; completions and part from the input.
2288 ;; FIXME: This reduces the problems of inconsistent capitalization
2289 ;; but it doesn't fully fix it: we may still end up completing
2290 ;; "fo-ba" to "foo-BAR" or "FOO-bar" when completing against
2291 ;; '("foo-barr" "FOO-BARD").
2293 ((null (cdr strs
)) (list (car strs
)))
2295 (let ((re (completion-pcm--pattern->regex pattern
'group
))
2296 (ccs ())) ;Chopped completions.
2298 ;; First chop each string into the parts corresponding to each
2299 ;; non-constant element of `pattern', using regexp-matching.
2300 (let ((case-fold-search completion-ignore-case
))
2302 (unless (string-match re str
)
2303 (error "Internal error: %s doesn't match %s" str re
))
2308 (while (setq next
(match-end i
))
2309 (push (substring str last next
) chopped
)
2312 ;; Add the text corresponding to the implicit trailing `any'.
2313 (push (substring str last
) chopped
)
2314 (push (nreverse chopped
) ccs
))))
2316 ;; Then for each of those non-constant elements, extract the
2317 ;; commonality between them.
2320 ;; Make the implicit trailing `any' explicit.
2321 (dolist (elem (append pattern
'(any)))
2323 (setq fixed
(concat fixed elem
))
2325 (dolist (cc (prog1 ccs
(setq ccs nil
)))
2326 (push (car cc
) comps
)
2327 (push (cdr cc
) ccs
))
2328 ;; Might improve the likelihood to avoid choosing
2329 ;; different capitalizations in different parts.
2330 ;; In practice, it doesn't seem to make any difference.
2331 (setq ccs
(nreverse ccs
))
2332 (let* ((prefix (try-completion fixed comps
))
2333 (unique (or (and (eq prefix t
) (setq prefix fixed
))
2334 (eq t
(try-completion prefix comps
)))))
2335 (unless (equal prefix
"") (push prefix res
))
2336 ;; If there's only one completion, `elem' is not useful
2337 ;; any more: it can only match the empty string.
2338 ;; FIXME: in some cases, it may be necessary to turn an
2339 ;; `any' into a `star' because the surrounding context has
2340 ;; changed such that string->pattern wouldn't add an `any'
2344 (when (memq elem
'(star point prefix
))
2345 ;; Extract common suffix additionally to common prefix.
2346 ;; Only do it for `point', `star', and `prefix' since for
2347 ;; `any' it could lead to a merged completion that
2348 ;; doesn't itself match the candidates.
2349 (let ((suffix (completion--common-suffix comps
)))
2350 (assert (stringp suffix
))
2351 (unless (equal suffix
"")
2352 (push suffix res
)))))
2354 ;; We return it in reverse order.
2357 (defun completion-pcm--pattern->string
(pattern)
2358 (mapconcat (lambda (x) (cond
2361 (t ""))) ;any, point, prefix.
2365 ;; We want to provide the functionality of `try', but we use `all'
2366 ;; and then merge it. In most cases, this works perfectly, but
2367 ;; if the completion table doesn't consider the same completions in
2368 ;; `try' as in `all', then we have a problem. The most common such
2369 ;; case is for filename completion where completion-ignored-extensions
2370 ;; is only obeyed by the `try' code. We paper over the difference
2371 ;; here. Note that it is not quite right either: if the completion
2372 ;; table uses completion-table-in-turn, this filtering may take place
2373 ;; too late to correctly fallback from the first to the
2374 ;; second alternative.
2375 (defun completion-pcm--filename-try-filter (all)
2376 "Filter to adjust `all' file completion to the behavior of `try'."
2379 (re (concat "\\(?:\\`\\.\\.?/\\|"
2380 (regexp-opt completion-ignored-extensions
)
2383 (unless (string-match-p re f
) (push f try
)))
2387 (defun completion-pcm--merge-try (pattern all prefix suffix
)
2389 ((not (consp all
)) all
)
2390 ((and (not (consp (cdr all
))) ;Only one completion.
2391 ;; Ignore completion-ignore-case here.
2392 (equal (completion-pcm--pattern->string pattern
) (car all
)))
2395 (let* ((mergedpat (completion-pcm--merge-completions all pattern
))
2396 ;; `mergedpat' is in reverse order. Place new point (by
2397 ;; order of preference) either at the old point, or at
2398 ;; the last place where there's something to choose, or
2400 (pointpat (or (memq 'point mergedpat
)
2401 (memq 'any mergedpat
)
2402 (memq 'star mergedpat
)
2405 ;; New pos from the start.
2406 (newpos (length (completion-pcm--pattern->string pointpat
)))
2407 ;; Do it afterwards because it changes `pointpat' by sideeffect.
2408 (merged (completion-pcm--pattern->string
(nreverse mergedpat
))))
2410 (setq suffix
(completion--merge-suffix merged newpos suffix
))
2411 (cons (concat prefix merged suffix
) (+ newpos
(length prefix
)))))))
2413 (defun completion-pcm-try-completion (string table pred point
)
2414 (destructuring-bind (pattern all prefix suffix
)
2415 (completion-pcm--find-all-completions
2416 string table pred point
2417 (if minibuffer-completing-file-name
2418 'completion-pcm--filename-try-filter
))
2419 (completion-pcm--merge-try pattern all prefix suffix
)))
2421 ;;; Substring completion
2422 ;; Mostly derived from the code of `basic' completion.
2424 (defun completion-substring--all-completions (string table pred point
)
2425 (let* ((beforepoint (substring string
0 point
))
2426 (afterpoint (substring string point
))
2427 (bounds (completion-boundaries beforepoint table pred afterpoint
))
2428 (suffix (substring afterpoint
(cdr bounds
)))
2429 (prefix (substring beforepoint
0 (car bounds
)))
2430 (basic-pattern (completion-basic--pattern
2431 beforepoint afterpoint bounds
))
2432 (pattern (if (not (stringp (car basic-pattern
)))
2434 (cons 'prefix basic-pattern
)))
2435 (all (completion-pcm--all-completions prefix pattern table pred
)))
2436 (list all pattern prefix suffix
(car bounds
))))
2438 (defun completion-substring-try-completion (string table pred point
)
2439 (destructuring-bind (all pattern prefix suffix _carbounds
)
2440 (completion-substring--all-completions string table pred point
)
2441 (if minibuffer-completing-file-name
2442 (setq all
(completion-pcm--filename-try-filter all
)))
2443 (completion-pcm--merge-try pattern all prefix suffix
)))
2445 (defun completion-substring-all-completions (string table pred point
)
2446 (destructuring-bind (all pattern prefix _suffix _carbounds
)
2447 (completion-substring--all-completions string table pred point
)
2449 (nconc (completion-pcm--hilit-commonality pattern all
)
2452 ;; Initials completion
2453 ;; Complete /ums to /usr/monnier/src or lch to list-command-history.
2455 (defun completion-initials-expand (str table pred
)
2456 (let ((bounds (completion-boundaries str table pred
"")))
2457 (unless (or (zerop (length str
))
2458 ;; Only check within the boundaries, since the
2459 ;; boundary char (e.g. /) might be in delim-regexp.
2460 (string-match completion-pcm--delim-wild-regex str
2462 (if (zerop (car bounds
))
2463 (mapconcat 'string str
"-")
2464 ;; If there's a boundary, it's trickier. The main use-case
2465 ;; we consider here is file-name completion. We'd like
2466 ;; to expand ~/eee to ~/e/e/e and /eee to /e/e/e.
2467 ;; But at the same time, we don't want /usr/share/ae to expand
2468 ;; to /usr/share/a/e just because we mistyped "ae" for "ar",
2469 ;; so we probably don't want initials to touch anything that
2470 ;; looks like /usr/share/foo. As a heuristic, we just check that
2471 ;; the text before the boundary char is at most 1 char.
2472 ;; This allows both ~/eee and /eee and not much more.
2473 ;; FIXME: It sadly also disallows the use of ~/eee when that's
2474 ;; embedded within something else (e.g. "(~/eee" in Info node
2475 ;; completion or "ancestor:/eee" in bzr-revision completion).
2476 (when (< (car bounds
) 3)
2477 (let ((sep (substring str
(1- (car bounds
)) (car bounds
))))
2478 ;; FIXME: the above string-match checks the whole string, whereas
2479 ;; we end up only caring about the after-boundary part.
2480 (concat (substring str
0 (car bounds
))
2481 (mapconcat 'string
(substring str
(car bounds
)) sep
))))))))
2483 (defun completion-initials-all-completions (string table pred _point
)
2484 (let ((newstr (completion-initials-expand string table pred
)))
2486 (completion-pcm-all-completions newstr table pred
(length newstr
)))))
2488 (defun completion-initials-try-completion (string table pred _point
)
2489 (let ((newstr (completion-initials-expand string table pred
)))
2491 (completion-pcm-try-completion newstr table pred
(length newstr
)))))
2496 (defun minibuffer-insert-file-name-at-point ()
2497 "Get a file name at point in original buffer and insert it to minibuffer."
2499 (let ((file-name-at-point
2500 (with-current-buffer (window-buffer (minibuffer-selected-window))
2501 (run-hook-with-args-until-success 'file-name-at-point-functions
))))
2502 (when file-name-at-point
2503 (insert file-name-at-point
))))
2505 (provide 'minibuffer
)
2507 ;;; minibuffer.el ends here