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