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