(minibuffer-message): Put cursor at the right place.
[bpt/emacs.git] / lisp / minibuffer.el
1 ;;; minibuffer.el --- Minibuffer completion functions
2
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; Names starting with "minibuffer--" are for functions and variables that
25 ;; are meant to be for internal use only.
26
27 ;; TODO:
28 ;; - make the `hide-spaces' arg of all-completions obsolete.
29
30 ;; BUGS:
31 ;; - envvar completion for file names breaks completion-base-size.
32
33 ;;; Code:
34
35 (eval-when-compile (require 'cl))
36
37 (defvar completion-all-completions-with-base-size nil
38 "If non-nil, `all-completions' may return the base-size in the last cdr.
39 The base-size is the length of the prefix that is elided from each
40 element in the returned list of completions. See `completion-base-size'.")
41
42 ;;; Completion table manipulation
43
44 (defun completion--some (fun xs)
45 "Apply FUN to each element of XS in turn.
46 Return the first non-nil returned value.
47 Like CL's `some'."
48 (let (res)
49 (while (and (not res) xs)
50 (setq res (funcall fun (pop xs))))
51 res))
52
53 (defun apply-partially (fun &rest args)
54 "Do a \"curried\" partial application of FUN to ARGS.
55 ARGS is a list of the first N arguments to pass to FUN.
56 The result is a new function that takes the remaining arguments,
57 and calls FUN."
58 (lexical-let ((fun fun) (args1 args))
59 (lambda (&rest args2) (apply fun (append args1 args2)))))
60
61 (defun complete-with-action (action table string pred)
62 "Perform completion ACTION.
63 STRING is the string to complete.
64 TABLE is the completion table, which should not be a function.
65 PRED is a completion predicate.
66 ACTION can be one of nil, t or `lambda'."
67 ;; (assert (not (functionp table)))
68 (funcall
69 (cond
70 ((null action) 'try-completion)
71 ((eq action t) 'all-completions)
72 (t 'test-completion))
73 string table pred))
74
75 (defun completion-table-dynamic (fun)
76 "Use function FUN as a dynamic completion table.
77 FUN is called with one argument, the string for which completion is required,
78 and it should return an alist containing all the intended possible
79 completions. This alist may be a full list of possible completions so that FUN
80 can ignore the value of its argument. If completion is performed in the
81 minibuffer, FUN will be called in the buffer from which the minibuffer was
82 entered.
83
84 The result of the `dynamic-completion-table' form is a function
85 that can be used as the ALIST argument to `try-completion' and
86 `all-completion'. See Info node `(elisp)Programmed Completion'."
87 (lexical-let ((fun fun))
88 (lambda (string pred action)
89 (with-current-buffer (let ((win (minibuffer-selected-window)))
90 (if (window-live-p win) (window-buffer win)
91 (current-buffer)))
92 (complete-with-action action (funcall fun string) string pred)))))
93
94 (defmacro lazy-completion-table (var fun)
95 "Initialize variable VAR as a lazy completion table.
96 If the completion table VAR is used for the first time (e.g., by passing VAR
97 as an argument to `try-completion'), the function FUN is called with no
98 arguments. FUN must return the completion table that will be stored in VAR.
99 If completion is requested in the minibuffer, FUN will be called in the buffer
100 from which the minibuffer was entered. The return value of
101 `lazy-completion-table' must be used to initialize the value of VAR.
102
103 You should give VAR a non-nil `risky-local-variable' property."
104 (declare (debug (symbolp lambda-expr)))
105 (let ((str (make-symbol "string")))
106 `(completion-table-dynamic
107 (lambda (,str)
108 (when (functionp ,var)
109 (setq ,var (,fun)))
110 ,var))))
111
112 (defun completion-table-with-context (prefix table string pred action)
113 ;; TODO: add `suffix', and think about how we should support `pred'.
114 ;; Notice that `pred' is not a predicate when called from read-file-name
115 ;; or Info-read-node-name-2.
116 ;; (if pred (setq pred (lexical-let ((pred pred))
117 ;; ;; FIXME: this doesn't work if `table' is an obarray.
118 ;; (lambda (s) (funcall pred (concat prefix s))))))
119 (let ((comp (complete-with-action action table string pred)))
120 (cond
121 ;; In case of try-completion, add the prefix.
122 ((stringp comp) (concat prefix comp))
123 ;; In case of non-empty all-completions,
124 ;; add the prefix size to the base-size.
125 ((consp comp)
126 (let ((last (last comp)))
127 (when completion-all-completions-with-base-size
128 (setcdr last (+ (or (cdr last) 0) (length prefix))))
129 comp))
130 (t comp))))
131
132 (defun completion-table-with-terminator (terminator table string pred action)
133 (let ((comp (complete-with-action action table string pred)))
134 (if (eq action nil)
135 (if (eq comp t)
136 (concat string terminator)
137 (if (and (stringp comp)
138 (eq (complete-with-action action table comp pred) t))
139 (concat comp terminator)
140 comp))
141 comp)))
142
143 (defun completion-table-in-turn (&rest tables)
144 "Create a completion table that tries each table in TABLES in turn."
145 (lexical-let ((tables tables))
146 (lambda (string pred action)
147 (completion--some (lambda (table)
148 (complete-with-action action table string pred))
149 tables))))
150
151 (defmacro complete-in-turn (a b) `(completion-table-in-turn ,a ,b))
152 (define-obsolete-function-alias
153 'complete-in-turn 'completion-table-in-turn "23.1")
154
155 ;;; Minibuffer completion
156
157 (defgroup minibuffer nil
158 "Controlling the behavior of the minibuffer."
159 :link '(custom-manual "(emacs)Minibuffer")
160 :group 'environment)
161
162 (defun minibuffer-message (message &rest args)
163 "Temporarily display MESSAGE at the end of the minibuffer.
164 The text is displayed for `minibuffer-message-timeout' seconds,
165 or until the next input event arrives, whichever comes first.
166 Enclose MESSAGE in [...] if this is not yet the case.
167 If ARGS are provided, then pass MESSAGE through `format'."
168 ;; Clear out any old echo-area message to make way for our new thing.
169 (message nil)
170 (unless (and (null args) (string-match "\\[.+\\]" message))
171 (setq message (concat " [" message "]")))
172 (when args (setq message (apply 'format message args)))
173 (let ((ol (make-overlay (point-max) (point-max) nil t t)))
174 (unwind-protect
175 (progn
176 (unless (zerop (length message))
177 ;; The current C cursor code doesn't know to use the overlay's
178 ;; marker's stickiness to figure out whether to place the cursor
179 ;; before or after the string, so let's spoon-feed it the pos.
180 (put-text-property 0 1 'cursor t message))
181 (overlay-put ol 'after-string message)
182 (sit-for (or minibuffer-message-timeout 1000000)))
183 (delete-overlay ol))))
184
185 (defun minibuffer-completion-contents ()
186 "Return the user input in a minibuffer before point as a string.
187 That is what completion commands operate on."
188 (buffer-substring (field-beginning) (point)))
189
190 (defun delete-minibuffer-contents ()
191 "Delete all user input in a minibuffer.
192 If the current buffer is not a minibuffer, erase its entire contents."
193 (delete-field))
194
195 (defcustom completion-auto-help t
196 "Non-nil means automatically provide help for invalid completion input.
197 If the value is t the *Completion* buffer is displayed whenever completion
198 is requested but cannot be done.
199 If the value is `lazy', the *Completions* buffer is only displayed after
200 the second failed attempt to complete."
201 :type '(choice (const nil) (const t) (const lazy))
202 :group 'minibuffer)
203
204 (defvar completion-styles-alist
205 '((basic try-completion all-completions)
206 ;; (partial-completion
207 ;; completion-pcm--try-completion completion-pcm--all-completions)
208 )
209 "List of available completion styles.
210 Each element has the form (NAME TRY-COMPLETION ALL-COMPLETIONS)
211 where NAME is the name that should be used in `completion-styles'
212 TRY-COMPLETION is the function that does the completion, and
213 ALL-COMPLETIONS is the function that lists the completions.")
214
215 (defcustom completion-styles '(basic)
216 "List of completion styles to use."
217 :type `(repeat (choice ,@(mapcar (lambda (x) (list 'const (car x)))
218 completion-styles-alist)))
219 :group 'minibuffer
220 :version "23.1")
221
222 (defun minibuffer-try-completion (string table pred)
223 (if (and (symbolp table) (get table 'no-completion-styles))
224 (try-completion string table pred)
225 (completion--some (lambda (style)
226 (funcall (nth 1 (assq style completion-styles-alist))
227 string table pred))
228 completion-styles)))
229
230 (defun minibuffer-all-completions (string table pred &optional hide-spaces)
231 (let ((completion-all-completions-with-base-size t))
232 (if (and (symbolp table) (get table 'no-completion-styles))
233 (all-completions string table pred hide-spaces)
234 (completion--some (lambda (style)
235 (funcall (nth 2 (assq style completion-styles-alist))
236 string table pred hide-spaces))
237 completion-styles))))
238
239 (defun minibuffer--bitset (modified completions exact)
240 (logior (if modified 4 0)
241 (if completions 2 0)
242 (if exact 1 0)))
243
244 (defun minibuffer--do-completion (&optional try-completion-function)
245 "Do the completion and return a summary of what happened.
246 M = completion was performed, the text was Modified.
247 C = there were available Completions.
248 E = after completion we now have an Exact match.
249
250 MCE
251 000 0 no possible completion
252 001 1 was already an exact and unique completion
253 010 2 no completion happened
254 011 3 was already an exact completion
255 100 4 ??? impossible
256 101 5 ??? impossible
257 110 6 some completion happened
258 111 7 completed to an exact completion"
259 (let* ((beg (field-beginning))
260 (string (buffer-substring beg (point)))
261 (completion (funcall (or try-completion-function
262 'minibuffer-try-completion)
263 string
264 minibuffer-completion-table
265 minibuffer-completion-predicate)))
266 (cond
267 ((null completion)
268 (ding) (minibuffer-message "No match") (minibuffer--bitset nil nil nil))
269 ((eq t completion) (minibuffer--bitset nil nil t)) ;Exact and unique match.
270 (t
271 ;; `completed' should be t if some completion was done, which doesn't
272 ;; include simply changing the case of the entered string. However,
273 ;; for appearance, the string is rewritten if the case changes.
274 (let ((completed (not (eq t (compare-strings completion nil nil
275 string nil nil t))))
276 (unchanged (eq t (compare-strings completion nil nil
277 string nil nil nil))))
278 (unless unchanged
279 ;; Merge a trailing / in completion with a / after point.
280 ;; We used to only do it for word completion, but it seems to make
281 ;; sense for all completions.
282 (if (and (eq ?/ (aref completion (1- (length completion))))
283 (< (point) (field-end))
284 (eq ?/ (char-after)))
285 (setq completion (substring completion 0 -1)))
286
287 ;; Insert in minibuffer the chars we got.
288 (let ((end (point)))
289 (insert completion)
290 (delete-region beg end)))
291
292 (if (not (or unchanged completed))
293 ;; The case of the string changed, but that's all. We're not sure
294 ;; whether this is a unique completion or not, so try again using
295 ;; the real case (this shouldn't recurse again, because the next
296 ;; time try-completion will return either t or the exact string).
297 (minibuffer--do-completion try-completion-function)
298
299 ;; It did find a match. Do we match some possibility exactly now?
300 (let ((exact (test-completion (field-string)
301 minibuffer-completion-table
302 minibuffer-completion-predicate)))
303 (unless completed
304 ;; Show the completion table, if requested.
305 (cond
306 ((not exact)
307 (if (case completion-auto-help
308 (lazy (eq this-command last-command))
309 (t completion-auto-help))
310 (minibuffer-completion-help)
311 (minibuffer-message "Next char not unique")))
312 ;; If the last exact completion and this one were the same,
313 ;; it means we've already given a "Complete but not unique"
314 ;; message and the user's hit TAB again, so now we give him help.
315 ((eq this-command last-command)
316 (if completion-auto-help (minibuffer-completion-help)))))
317
318 (minibuffer--bitset completed t exact))))))))
319
320 (defun minibuffer-complete ()
321 "Complete the minibuffer contents as far as possible.
322 Return nil if there is no valid completion, else t.
323 If no characters can be completed, display a list of possible completions.
324 If you repeat this command after it displayed such a list,
325 scroll the window of possible completions."
326 (interactive)
327 ;; If the previous command was not this,
328 ;; mark the completion buffer obsolete.
329 (unless (eq this-command last-command)
330 (setq minibuffer-scroll-window nil))
331
332 (let ((window minibuffer-scroll-window))
333 ;; If there's a fresh completion window with a live buffer,
334 ;; and this command is repeated, scroll that window.
335 (if (window-live-p window)
336 (with-current-buffer (window-buffer window)
337 (if (pos-visible-in-window-p (point-max) window)
338 ;; If end is in view, scroll up to the beginning.
339 (set-window-start window (point-min) nil)
340 ;; Else scroll down one screen.
341 (scroll-other-window))
342 nil)
343
344 (case (minibuffer--do-completion)
345 (0 nil)
346 (1 (goto-char (field-end))
347 (minibuffer-message "Sole completion")
348 t)
349 (3 (goto-char (field-end))
350 (minibuffer-message "Complete, but not unique")
351 t)
352 (t t)))))
353
354 (defun minibuffer-complete-and-exit ()
355 "If the minibuffer contents is a valid completion then exit.
356 Otherwise try to complete it. If completion leads to a valid completion,
357 a repetition of this command will exit."
358 (interactive)
359 (cond
360 ;; Allow user to specify null string
361 ((= (field-beginning) (field-end)) (exit-minibuffer))
362 ((test-completion (field-string)
363 minibuffer-completion-table
364 minibuffer-completion-predicate)
365 (when completion-ignore-case
366 ;; Fixup case of the field, if necessary.
367 (let* ((string (field-string))
368 (compl (minibuffer-try-completion
369 string
370 minibuffer-completion-table
371 minibuffer-completion-predicate)))
372 (when (and (stringp compl)
373 ;; If it weren't for this piece of paranoia, I'd replace
374 ;; the whole thing with a call to complete-do-completion.
375 (= (length string) (length compl)))
376 (let ((beg (field-beginning))
377 (end (field-end)))
378 (goto-char end)
379 (insert compl)
380 (delete-region beg end)))))
381 (exit-minibuffer))
382
383 ((eq minibuffer-completion-confirm 'confirm-only)
384 ;; The user is permitted to exit with an input that's rejected
385 ;; by test-completion, but at the condition to confirm her choice.
386 (if (eq last-command this-command)
387 (exit-minibuffer)
388 (minibuffer-message "Confirm")
389 nil))
390
391 (t
392 ;; Call do-completion, but ignore errors.
393 (case (condition-case nil
394 (minibuffer--do-completion)
395 (error 1))
396 ((1 3) (exit-minibuffer))
397 (7 (if (not minibuffer-completion-confirm)
398 (exit-minibuffer)
399 (minibuffer-message "Confirm")
400 nil))
401 (t nil)))))
402
403 (defun minibuffer-try-word-completion (string table predicate)
404 (let ((completion (minibuffer-try-completion string table predicate)))
405 (if (not (stringp completion))
406 completion
407
408 ;; Completing a single word is actually more difficult than completing
409 ;; as much as possible, because we first have to find the "current
410 ;; position" in `completion' in order to find the end of the word
411 ;; we're completing. Normally, `string' is a prefix of `completion',
412 ;; which makes it trivial to find the position, but with fancier
413 ;; completion (plus env-var expansion, ...) `completion' might not
414 ;; look anything like `string' at all.
415
416 (when minibuffer-completing-file-name
417 ;; In order to minimize the problem mentioned above, let's try to
418 ;; reduce the different between `string' and `completion' by
419 ;; mirroring some of the work done in read-file-name-internal.
420 (let ((substituted (condition-case nil
421 ;; Might fail when completing an env-var.
422 (substitute-in-file-name string)
423 (error string))))
424 (unless (eq string substituted)
425 (setq string substituted))))
426
427 ;; Make buffer (before point) contain the longest match
428 ;; of `string's tail and `completion's head.
429 (let* ((startpos (max 0 (- (length string) (length completion))))
430 (length (- (length string) startpos)))
431 (while (and (> length 0)
432 (not (eq t (compare-strings string startpos nil
433 completion 0 length
434 completion-ignore-case))))
435 (setq startpos (1+ startpos))
436 (setq length (1- length)))
437
438 (setq string (substring string startpos)))
439
440 ;; Now `string' is a prefix of `completion'.
441
442 ;; If completion finds next char not unique,
443 ;; consider adding a space or a hyphen.
444 (when (= (length string) (length completion))
445 (let ((exts '(" " "-"))
446 tem)
447 (while (and exts (not (stringp tem)))
448 (setq tem (minibuffer-try-completion (concat string (pop exts))
449 table predicate)))
450 (if (stringp tem) (setq completion tem))))
451
452 ;; Otherwise cut after the first word.
453 (if (string-match "\\W" completion (length string))
454 ;; First find first word-break in the stuff found by completion.
455 ;; i gets index in string of where to stop completing.
456 (substring completion 0 (match-end 0))
457 completion))))
458
459
460 (defun minibuffer-complete-word ()
461 "Complete the minibuffer contents at most a single word.
462 After one word is completed as much as possible, a space or hyphen
463 is added, provided that matches some possible completion.
464 Return nil if there is no valid completion, else t."
465 (interactive)
466 (case (minibuffer--do-completion 'minibuffer-try-word-completion)
467 (0 nil)
468 (1 (goto-char (field-end))
469 (minibuffer-message "Sole completion")
470 t)
471 (3 (goto-char (field-end))
472 (minibuffer-message "Complete, but not unique")
473 t)
474 (t t)))
475
476 (defun minibuffer--insert-strings (strings)
477 "Insert a list of STRINGS into the current buffer.
478 Uses columns to keep the listing readable but compact.
479 It also eliminates runs of equal strings."
480 (when (consp strings)
481 (let* ((length (apply 'max
482 (mapcar (lambda (s)
483 (if (consp s)
484 (+ (length (car s)) (length (cadr s)))
485 (length s)))
486 strings)))
487 (window (get-buffer-window (current-buffer) 0))
488 (wwidth (if window (1- (window-width window)) 79))
489 (columns (min
490 ;; At least 2 columns; at least 2 spaces between columns.
491 (max 2 (/ wwidth (+ 2 length)))
492 ;; Don't allocate more columns than we can fill.
493 ;; Windows can't show less than 3 lines anyway.
494 (max 1 (/ (length strings) 2))))
495 (colwidth (/ wwidth columns))
496 (column 0)
497 (laststring nil))
498 ;; The insertion should be "sensible" no matter what choices were made
499 ;; for the parameters above.
500 (dolist (str strings)
501 (unless (equal laststring str) ; Remove (consecutive) duplicates.
502 (setq laststring str)
503 (unless (bolp)
504 (insert " \t")
505 (setq column (+ column colwidth))
506 ;; Leave the space unpropertized so that in the case we're
507 ;; already past the goal column, there is still
508 ;; a space displayed.
509 (set-text-properties (- (point) 1) (point)
510 ;; We can't just set tab-width, because
511 ;; completion-setup-function will kill all
512 ;; local variables :-(
513 `(display (space :align-to ,column))))
514 (when (< wwidth (+ (max colwidth
515 (if (consp str)
516 (+ (length (car str)) (length (cadr str)))
517 (length str)))
518 column))
519 (delete-char -2) (insert "\n") (setq column 0))
520 (if (not (consp str))
521 (put-text-property (point) (progn (insert str) (point))
522 'mouse-face 'highlight)
523 (put-text-property (point) (progn (insert (car str)) (point))
524 'mouse-face 'highlight)
525 (put-text-property (point) (progn (insert (cadr str)) (point))
526 'mouse-face nil)))))))
527
528 (defvar completion-common-substring)
529
530 (defvar completion-setup-hook nil
531 "Normal hook run at the end of setting up a completion list buffer.
532 When this hook is run, the current buffer is the one in which the
533 command to display the completion list buffer was run.
534 The completion list buffer is available as the value of `standard-output'.
535 The common prefix substring for completion may be available as the
536 value of `completion-common-substring'. See also `display-completion-list'.")
537
538 (defun display-completion-list (completions &optional common-substring)
539 "Display the list of completions, COMPLETIONS, using `standard-output'.
540 Each element may be just a symbol or string
541 or may be a list of two strings to be printed as if concatenated.
542 If it is a list of two strings, the first is the actual completion
543 alternative, the second serves as annotation.
544 `standard-output' must be a buffer.
545 The actual completion alternatives, as inserted, are given `mouse-face'
546 properties of `highlight'.
547 At the end, this runs the normal hook `completion-setup-hook'.
548 It can find the completion buffer in `standard-output'.
549 The optional second arg COMMON-SUBSTRING is a string.
550 It is used to put faces, `completions-first-difference' and
551 `completions-common-part' on the completion buffer. The
552 `completions-common-part' face is put on the common substring
553 specified by COMMON-SUBSTRING. If COMMON-SUBSTRING is nil
554 and the current buffer is not the minibuffer, the faces are not put.
555 Internally, COMMON-SUBSTRING is bound to `completion-common-substring'
556 during running `completion-setup-hook'."
557 (if (not (bufferp standard-output))
558 ;; This *never* (ever) happens, so there's no point trying to be clever.
559 (with-temp-buffer
560 (let ((standard-output (current-buffer))
561 (completion-setup-hook nil))
562 (display-completion-list completions))
563 (princ (buffer-string)))
564
565 (with-current-buffer standard-output
566 (goto-char (point-max))
567 (if (null completions)
568 (insert "There are no possible completions of what you have typed.")
569
570 (insert "Possible completions are:\n")
571 (let ((last (last completions)))
572 ;; Get the base-size from the tail of the list.
573 (set (make-local-variable 'completion-base-size) (or (cdr last) 0))
574 (setcdr last nil)) ;Make completions a properly nil-terminated list.
575 (minibuffer--insert-strings completions))))
576
577 (let ((completion-common-substring common-substring))
578 (run-hooks 'completion-setup-hook))
579 nil)
580
581 (defun minibuffer-completion-help ()
582 "Display a list of possible completions of the current minibuffer contents."
583 (interactive)
584 (message "Making completion list...")
585 (let* ((string (field-string))
586 (completions (minibuffer-all-completions
587 string
588 minibuffer-completion-table
589 minibuffer-completion-predicate
590 t)))
591 (message nil)
592 (if (and completions
593 (or (consp (cdr completions))
594 (not (equal (car completions) string))))
595 (with-output-to-temp-buffer "*Completions*"
596 (let* ((last (last completions))
597 (base-size (cdr last)))
598 ;; Remove the base-size tail because `sort' requires a properly
599 ;; nil-terminated list.
600 (when last (setcdr last nil))
601 (display-completion-list (nconc (sort completions 'string-lessp)
602 base-size))))
603
604 ;; If there are no completions, or if the current input is already the
605 ;; only possible completion, then hide (previous&stale) completions.
606 (let ((window (and (get-buffer "*Completions*")
607 (get-buffer-window "*Completions*" 0))))
608 (when (and (window-live-p window) (window-dedicated-p window))
609 (condition-case ()
610 (delete-window window)
611 (error (iconify-frame (window-frame window))))))
612 (ding)
613 (minibuffer-message
614 (if completions "Sole completion" "No completions")))
615 nil))
616
617 (defun exit-minibuffer ()
618 "Terminate this minibuffer argument."
619 (interactive)
620 ;; If the command that uses this has made modifications in the minibuffer,
621 ;; we don't want them to cause deactivation of the mark in the original
622 ;; buffer.
623 ;; A better solution would be to make deactivate-mark buffer-local
624 ;; (or to turn it into a list of buffers, ...), but in the mean time,
625 ;; this should do the trick in most cases.
626 (setq deactivate-mark nil)
627 (throw 'exit nil))
628
629 (defun self-insert-and-exit ()
630 "Terminate minibuffer input."
631 (interactive)
632 (if (characterp last-command-char)
633 (call-interactively 'self-insert-command)
634 (ding))
635 (exit-minibuffer))
636
637 (defun minibuffer--double-dollars (str)
638 (replace-regexp-in-string "\\$" "$$" str))
639
640 (defun completion--make-envvar-table ()
641 (mapcar (lambda (enventry)
642 (substring enventry 0 (string-match "=" enventry)))
643 process-environment))
644
645 (defun completion--embedded-envvar-table (string pred action)
646 (when (string-match (concat "\\(?:^\\|[^$]\\(?:\\$\\$\\)*\\)"
647 "$\\([[:alnum:]_]*\\|{\\([^}]*\\)\\)\\'")
648 string)
649 (let* ((beg (or (match-beginning 2) (match-beginning 1)))
650 (table (completion--make-envvar-table))
651 (prefix (substring string 0 beg)))
652 (if (eq (aref string (1- beg)) ?{)
653 (setq table (apply-partially 'completion-table-with-terminator
654 "}" table)))
655 (completion-table-with-context prefix table
656 (substring string beg)
657 pred action))))
658
659 (defun completion--file-name-table (string dir action)
660 "Internal subroutine for read-file-name. Do not call this."
661 (setq dir (expand-file-name dir))
662 (if (and (zerop (length string)) (eq 'lambda action))
663 nil ; FIXME: why?
664 (let* ((str (condition-case nil
665 (substitute-in-file-name string)
666 (error string)))
667 (name (file-name-nondirectory str))
668 (specdir (file-name-directory str))
669 (realdir (if specdir (expand-file-name specdir dir)
670 (file-name-as-directory dir))))
671
672 (cond
673 ((null action)
674 (let ((comp (file-name-completion name realdir
675 read-file-name-predicate)))
676 (if (stringp comp)
677 ;; Requote the $s before returning the completion.
678 (minibuffer--double-dollars (concat specdir comp))
679 ;; Requote the $s before checking for changes.
680 (setq str (minibuffer--double-dollars str))
681 (if (string-equal string str)
682 comp
683 ;; If there's no real completion, but substitute-in-file-name
684 ;; changed the string, then return the new string.
685 str))))
686
687 ((eq action t)
688 (let ((all (file-name-all-completions name realdir))
689 ;; Actually, this is not always right in the presence of
690 ;; envvars, but there's not much we can do, I think.
691 (base-size (length (file-name-directory string))))
692
693 ;; Check the predicate, if necessary.
694 (unless (memq read-file-name-predicate '(nil file-exists-p))
695 (let ((comp ())
696 (pred
697 (if (eq read-file-name-predicate 'file-directory-p)
698 ;; Brute-force speed up for directory checking:
699 ;; Discard strings which don't end in a slash.
700 (lambda (s)
701 (let ((len (length s)))
702 (and (> len 0) (eq (aref s (1- len)) ?/))))
703 ;; Must do it the hard (and slow) way.
704 read-file-name-predicate)))
705 (let ((default-directory realdir))
706 (dolist (tem all)
707 (if (funcall pred tem) (push tem comp))))
708 (setq all (nreverse comp))))
709
710 ;; Add base-size, but only if the list is non-empty.
711 (if (consp all) (nconc all base-size))))
712
713 (t
714 ;; Only other case actually used is ACTION = lambda.
715 (let ((default-directory dir))
716 (funcall (or read-file-name-predicate 'file-exists-p) str)))))))
717
718 (defalias 'read-file-name-internal
719 (completion-table-in-turn 'completion--embedded-envvar-table
720 'completion--file-name-table)
721 "Internal subroutine for `read-file-name'. Do not call this.")
722
723 (provide 'minibuffer)
724 ;;; minibuffer.el ends here