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