1 ;;; minibuf.el --- Minibuffer functions for SXEmacs
3 ;; Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems.
5 ;; Copyright (C) 1995, 1996, 2000 Ben Wing.
7 ;; Author: Richard Mlynarik
9 ;; Maintainer: SXEmacs Development Team
10 ;; Keywords: internal, dumped
12 ;; This file is part of SXEmacs.
14 ;; SXEmacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; SXEmacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Synched up with: all the minibuffer history stuff is synched with
28 ;;; 19.30. Not sure about the rest.
32 ;; This file is dumped with SXEmacs.
34 ;; Written by Richard Mlynarik 2-Oct-92
36 ;; 06/11/1997 - Use char-(after|before) instead of
37 ;; (following|preceding)-char. -slb
41 (defgroup minibuffer nil
42 "Controling the behavior of the minibuffer."
46 (defcustom insert-default-directory t
47 "*Non-nil means when reading a filename start with default dir in minibuffer."
51 (defcustom minibuffer-history-uniquify t
52 "*Non-nil means when adding an item to a minibuffer history, remove
53 previous occurrences of the same item from the history list first,
54 rather than just consing the new element onto the front of the list."
58 (defvar minibuffer-completion-table nil
59 "Alist or obarray used for completion in the minibuffer.
60 This becomes the ALIST argument to `try-completion' and `all-completions'.
62 The value may alternatively be a function, which is given three arguments:
63 STRING, the current buffer contents;
64 PREDICATE, the predicate for filtering possible matches;
65 CODE, which says what kind of things to do.
66 CODE can be nil, t or `lambda'.
67 nil means to return the best completion of STRING, nil if there is none,
68 or t if it is already a unique completion.
69 t means to return a list of all possible completions of STRING.
70 `lambda' means to return t if STRING is a valid completion as it stands.")
72 (defvar minibuffer-completion-predicate nil
73 "Within call to `completing-read', this holds the PREDICATE argument.")
75 (defvar minibuffer-completion-confirm nil
76 "Non-nil => demand confirmation of completion before exiting minibuffer.")
78 (defcustom minibuffer-confirm-incomplete nil
79 "If true, then in contexts where completing-read allows answers which
80 are not valid completions, an extra RET must be typed to confirm the
81 response. This is helpful for catching typos, etc."
85 (defcustom completion-auto-help t
86 "*Non-nil means automatically provide help for invalid completion input."
90 (defcustom enable-recursive-minibuffers nil
91 "*Non-nil means to allow minibuffer commands while in the minibuffer.
92 More precisely, this variable makes a difference when the minibuffer window
93 is the selected window. If you are in some other window, minibuffer commands
94 are allowed even if a minibuffer is active."
98 (defcustom minibuffer-max-depth 1
99 ;; See comment in #'minibuffer-max-depth-exceeded
100 "*Global maximum number of minibuffers allowed;
101 compare to enable-recursive-minibuffers, which is only consulted when the
102 minibuffer is reinvoked while it is the selected window."
103 :type '(choice integer
104 (const :tag "Indefinite" nil))
107 ;; Moved to C. The minibuffer prompt must be setup before this is run
108 ;; and that can only be done from the C side.
109 ;(defvar minibuffer-setup-hook nil
110 ; "Normal hook run just after entry to minibuffer.")
112 ;; see comment at list-mode-hook.
113 (put 'minibuffer-setup-hook 'permanent-local t)
115 (defvar minibuffer-exit-hook nil
116 "Normal hook run just after exit from minibuffer.")
117 (put 'minibuffer-exit-hook 'permanent-local t)
119 (defvar minibuffer-help-form nil
120 "Value that `help-form' takes on inside the minibuffer.")
122 (defvar minibuffer-default nil
123 "Default value for minibuffer input.")
125 (defvar minibuffer-local-map
126 (let ((map (make-sparse-keymap 'minibuffer-local-map)))
128 "Default keymap to use when reading from the minibuffer.")
130 (defvar minibuffer-local-completion-map
131 (let ((map (make-sparse-keymap 'minibuffer-local-completion-map)))
132 (set-keymap-parents map (list minibuffer-local-map))
134 "Local keymap for minibuffer input with completion.")
136 (defvar minibuffer-local-must-match-map
137 (let ((map (make-sparse-keymap 'minibuffer-must-match-map)))
138 (set-keymap-parents map (list minibuffer-local-completion-map))
140 "Local keymap for minibuffer input with completion, for exact match.")
142 ;; (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
143 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit) ;; moved here from pending-del.el
144 (define-key minibuffer-local-map "\r" 'exit-minibuffer)
145 (define-key minibuffer-local-map "\n" 'exit-minibuffer)
147 ;; Historical crock. Unused by anything but user code, if even that
148 ;(defvar minibuffer-local-ns-map
149 ; (let ((map (make-sparse-keymap 'minibuffer-local-ns-map)))
150 ; (set-keymap-parents map (list minibuffer-local-map))
152 ; "Local keymap for the minibuffer when spaces are not allowed.")
153 ;(define-key minibuffer-local-ns-map [space] 'exit-minibuffer)
154 ;(define-key minibuffer-local-ns-map [tab] 'exit-minibuffer)
155 ;(define-key minibuffer-local-ns-map [?\?] 'self-insert-and-exit)
157 (define-key minibuffer-local-completion-map "\t" 'minibuffer-complete)
158 (define-key minibuffer-local-completion-map " " 'minibuffer-complete-word)
159 (define-key minibuffer-local-completion-map "?" 'minibuffer-completion-help)
160 (define-key minibuffer-local-must-match-map "\r" 'minibuffer-complete-and-exit)
161 (define-key minibuffer-local-must-match-map "\n" 'minibuffer-complete-and-exit)
163 (define-key minibuffer-local-map "\M-n" 'next-history-element)
164 (define-key minibuffer-local-map "\M-p" 'previous-history-element)
165 (define-key minibuffer-local-map '[next] "\M-n")
166 (define-key minibuffer-local-map '[prior] "\M-p")
167 (define-key minibuffer-local-map "\M-r" 'previous-matching-history-element)
168 (define-key minibuffer-local-map "\M-s" 'next-matching-history-element)
169 (define-key minibuffer-local-must-match-map [next]
170 'next-complete-history-element)
171 (define-key minibuffer-local-must-match-map [prior]
172 'previous-complete-history-element)
174 ;; This is an experiment--make up and down arrows do history.
175 (define-key minibuffer-local-map [up] 'previous-history-element)
176 (define-key minibuffer-local-map [down] 'next-history-element)
177 (define-key minibuffer-local-completion-map [up] 'previous-history-element)
178 (define-key minibuffer-local-completion-map [down] 'next-history-element)
179 (define-key minibuffer-local-must-match-map [up] 'previous-history-element)
180 (define-key minibuffer-local-must-match-map [down] 'next-history-element)
182 (defvar read-expression-map (let ((map (make-sparse-keymap
183 'read-expression-map)))
184 (set-keymap-parents map
185 (list minibuffer-local-map))
186 (define-key map "\M-\t" 'lisp-complete-symbol)
188 "Minibuffer keymap used for reading Lisp expressions.")
190 (defvar read-shell-command-map
191 (let ((map (make-sparse-keymap 'read-shell-command-map)))
192 (set-keymap-parents map (list minibuffer-local-map))
193 (define-key map "\t" 'comint-dynamic-complete)
194 (define-key map "\M-\t" 'comint-dynamic-complete)
195 (define-key map "\M-?" 'comint-dynamic-list-completions)
197 "Minibuffer keymap used by `shell-command' and related commands.")
199 (defcustom use-dialog-box t
200 "*Variable controlling usage of the dialog box.
201 If nil, the dialog box will never be used, even in response to mouse events."
205 (defcustom minibuffer-electric-file-name-behavior t
206 "*If non-nil, slash and tilde in certain places cause immediate deletion.
207 These are the same places where this behavior would occur later on anyway,
208 in `substitute-in-file-name'."
212 ;; originally by Stig@hackvan.com
213 (defun minibuffer-electric-separator ()
215 (let ((c last-command-char))
216 (and minibuffer-electric-file-name-behavior
217 (eq c directory-sep-char)
218 (eq c (char-before (point)))
220 (goto-char (point-min))
221 (and (looking-at "/.+:~?[^/]*/.+")
222 (re-search-forward "^/.+:~?[^/]*" nil t)
224 (delete-region (point) (point-max))
227 (goto-char (point-min))
228 (and (looking-at ".+://[^/]*/.+")
229 (re-search-forward "^.+:/" nil t)
231 (delete-region (point) (point-max))
233 ;; permit `//hostname/path/to/file'
234 (not (eq (point) (1+ (point-min))))
235 ;; permit `http://url/goes/here'
236 (or (not (eq ?: (char-after (- (point) 2))))
237 (eq ?/ (char-after (point-min))))
238 (delete-region (point-min) (point)))
241 (defun minibuffer-electric-tilde ()
243 (and minibuffer-electric-file-name-behavior
244 (eq directory-sep-char (char-before (point)))
245 ;; permit URL's with //, for e.g. http://hostname/~user
246 (not (save-excursion (search-backward "//" nil t)))
247 (delete-region (point-min) (point)))
251 (defvar read-file-name-map
252 (let ((map (make-sparse-keymap 'read-file-name-map)))
253 (set-keymap-parents map (list minibuffer-local-completion-map))
254 (define-key map (vector directory-sep-char) 'minibuffer-electric-separator)
255 (define-key map "~" 'minibuffer-electric-tilde)
259 (defvar read-file-name-must-match-map
260 (let ((map (make-sparse-keymap 'read-file-name-map)))
261 (set-keymap-parents map (list minibuffer-local-must-match-map))
262 (define-key map (vector directory-sep-char) 'minibuffer-electric-separator)
263 (define-key map "~" 'minibuffer-electric-tilde)
267 (defun minibuffer-keyboard-quit ()
268 "Abort recursive edit.
269 If `zmacs-regions' is true, and the zmacs region is active in this buffer,
270 then this key deactivates the region without beeping."
272 (if (region-active-p)
273 ;; pseudo-zmacs compatibility: don't beep if this ^G is simply
274 ;; deactivating the region. If it is inactive, beep.
276 (abort-recursive-edit)))
278 ;;;; Guts of minibuffer invocation
280 ;;#### The only things remaining in C are
281 ;; "Vminibuf_prompt" and the display junk
282 ;; "minibuf_prompt_width" and "minibuf_prompt_pix_width"
283 ;; Also "active_frame", though I suspect I could already
284 ;; hack that in Lisp if I could make any sense of the
285 ;; complete mess of frame/frame code in XEmacs.
286 ;; Vminibuf_prompt could easily be made Lisp-bindable.
287 ;; I suspect that minibuf_prompt*_width are actually recomputed
288 ;; by redisplay as needed -- or could be arranged to be so --
289 ;; and that there could be need for read-minibuffer-internal to
290 ;; save and restore them.
291 ;;#### The only other thing which read-from-minibuffer-internal does
292 ;; which we can't presently do in Lisp is move the frame cursor
293 ;; to the start of the minibuffer line as it returns. This is
294 ;; a rather nice touch and should be preserved -- probably by
295 ;; providing some Lisp-level mechanism (extension to cursor-in-echo-area ?)
299 ;; Like reset_buffer in FSF's buffer.c
300 ;; (Except that kill-all-local-variables doesn't nuke 'permanent-local
301 ;; variables -- we preserve them, reset_buffer doesn't.)
302 (defun reset-buffer (buffer)
303 (with-current-buffer buffer
304 ;(if (fboundp 'unlock-buffer) (unlock-buffer))
305 (kill-all-local-variables)
306 (setq buffer-read-only nil)
307 ;; don't let read only text yanked into the minibuffer
308 ;; permanently wedge it.
309 (make-local-variable 'inhibit-read-only)
310 (setq inhibit-read-only t)
312 ;(setq default-directory nil)
313 (setq buffer-file-name nil)
314 (setq buffer-file-truename nil)
315 (set-buffer-modified-p nil)
316 (setq buffer-backed-up nil)
317 (setq buffer-auto-save-file-name nil)
318 (set-buffer-dedicated-frame buffer nil)
321 (defvar minibuffer-history-variable 'minibuffer-history
322 "History list symbol to add minibuffer values to.
323 Each minibuffer output is added with
324 (set minibuffer-history-variable
325 (cons STRING (symbol-value minibuffer-history-variable)))")
326 (defvar minibuffer-history-position)
329 (defvar initial-minibuffer-history-position)
330 (defvar current-minibuffer-contents)
331 (defvar current-minibuffer-point)
333 (defcustom minibuffer-history-minimum-string-length nil
334 "*If this variable is non-nil, a string will not be added to the
335 minibuffer history if its length is less than that value."
336 :type '(choice (const :tag "Any" nil)
340 (define-error 'input-error "Keyboard input error")
342 (put 'input-error 'display-error
343 #'(lambda (error-object stream)
344 (princ (cadr error-object) stream)))
346 (defun read-from-minibuffer (prompt &optional initial-contents
352 "Read a string from the minibuffer, prompting with string PROMPT.
353 If optional second arg INITIAL-CONTENTS is non-nil, it is a string
354 to be inserted into the minibuffer before reading input.
355 If INITIAL-CONTENTS is (STRING . POSITION), the initial input
356 is STRING, but point is placed POSITION characters into the string.
357 Third arg KEYMAP is a keymap to use while reading;
358 if omitted or nil, the default is `minibuffer-local-map'.
359 If fourth arg READ is non-nil, then interpret the result as a lisp object
360 and return that object:
361 in other words, do `(car (read-from-string INPUT-STRING))'
362 Fifth arg HISTORY, if non-nil, specifies a history list
363 and optionally the initial position in the list.
364 It can be a symbol, which is the history list variable to use,
365 or it can be a cons cell (HISTVAR . HISTPOS).
366 In that case, HISTVAR is the history list variable to use,
367 and HISTPOS is the initial position (the position in the list
368 which INITIAL-CONTENTS corresponds to).
369 If HISTORY is `t', no history will be recorded.
370 Positions are counted starting from 1 at the beginning of the list.
371 Sixth arg ABBREV-TABLE, if non-nil, becomes the value of `local-abbrev-table'
373 Seventh arg DEFAULT, if non-nil, will be returned when user enters
376 See also the variable `completion-highlight-first-word-only' for
377 control over completion display."
378 (if (and (not enable-recursive-minibuffers)
379 (> (minibuffer-depth) 0)
380 (eq (selected-window) (minibuffer-window)))
381 (error "Command attempted to use minibuffer while in minibuffer"))
383 (if (and minibuffer-max-depth
384 (> minibuffer-max-depth 0)
385 (>= (minibuffer-depth) minibuffer-max-depth))
386 (minibuffer-max-depth-exceeded))
388 ;; catch this error before the poor user has typed something...
390 (if (symbolp history)
392 (error "History list %S is unbound" history))
393 (or (boundp (car history))
394 (error "History list %S is unbound" (car history)))))
398 ;; XEmacs in -batch mode calls minibuffer: print the prompt.
399 (message "%s" (gettext prompt))
402 ;;#### Should this even be falling though to the code below?
403 ;;#### How does this stuff work now, anyway?
405 (let* ((dir default-directory)
406 (owindow (selected-window))
407 (oframe (selected-frame))
408 (window (minibuffer-window))
409 (buffer (if (eq (minibuffer-depth) 0)
410 (window-buffer window)
411 (get-buffer-create (format " *Minibuf-%d"
412 (minibuffer-depth)))))
413 (frame (window-frame window))
414 (mconfig (if (eq frame (selected-frame))
415 nil (current-window-configuration frame)))
416 (oconfig (current-window-configuration))
417 ;; dynamic scope sucks sucks sucks sucks sucks sucks.
418 ;; `M-x doctor' makes history a local variable, and thus
419 ;; our binding above is buffer-local and doesn't apply
420 ;; once we switch buffers!!!! We demand better scope!
422 (minibuffer-default default))
425 (set-buffer (reset-buffer buffer))
426 (setq default-directory dir)
427 (make-local-variable 'print-escape-newlines)
428 (setq print-escape-newlines t)
429 (make-local-variable 'current-minibuffer-contents)
430 (make-local-variable 'current-minibuffer-point)
431 (make-local-variable 'initial-minibuffer-history-position)
432 (setq current-minibuffer-contents ""
433 current-minibuffer-point 1)
434 (if (not minibuffer-smart-completion-tracking-behavior)
436 (make-local-variable 'mode-motion-hook)
439 (setq mode-motion-hook 'minibuffer-smart-mouse-tracker))
440 (make-local-variable 'mouse-track-click-hook)
441 (add-hook 'mouse-track-click-hook
442 'minibuffer-smart-maybe-select-highlighted-completion))
443 (set-window-buffer window buffer)
444 (select-window window)
445 (set-window-hscroll window 0)
446 (buffer-enable-undo buffer)
449 (if (consp initial-contents)
451 (insert (car initial-contents))
452 (goto-char (1+ (cdr initial-contents)))
453 (setq current-minibuffer-contents (car initial-contents)
454 current-minibuffer-point (cdr initial-contents)))
455 (insert initial-contents)
456 (setq current-minibuffer-contents initial-contents
457 current-minibuffer-point (point))))
458 (use-local-map (help-keymap-with-help-key
459 (or keymap minibuffer-local-map)
460 minibuffer-help-form))
461 (let ((mouse-grabbed-buffer
462 (and minibuffer-smart-completion-tracking-behavior
464 (current-prefix-arg current-prefix-arg)
465 ;; (help-form minibuffer-help-form)
466 (minibuffer-history-variable (cond ((not _history_)
472 (minibuffer-history-position (cond ((consp _history_)
476 (minibuffer-scroll-window owindow))
477 (setq initial-minibuffer-history-position
478 minibuffer-history-position)
480 (setq local-abbrev-table abbrev-table
482 ;; This is now run from read-minibuffer-internal
483 ;(if minibuffer-setup-hook
484 ; (run-hooks 'minibuffer-setup-hook))
488 (if (> (recursion-depth) (minibuffer-depth))
489 (let ((standard-output t)
491 (read-minibuffer-internal prompt))
492 (read-minibuffer-internal prompt))))
493 ;; Translate an "abort" (throw 'exit 't)
497 (let* ((val (progn (set-buffer buffer)
498 (if minibuffer-exit-hook
499 (run-hooks 'minibuffer-exit-hook))
500 (if (and (eq (char-after (point-min)) nil)
504 (histval (if (and default (string= val ""))
510 (let ((v (read-from-string val)))
511 (if (< (cdr v) (length val))
513 (or (string-match "[ \t\n]*\\'" val (cdr v))
514 (error "Trailing garbage following expression"))))
516 ;; total total kludge
517 (if (stringp v) (setq v (list 'quote v)))
521 '(input-error "End of input before end of expression")))
522 (error (setq err e))))
523 ;; Add the value to the appropriate history list unless
524 ;; it's already the most recent element, or it's only
525 ;; two characters long.
526 (if (and (symbolp minibuffer-history-variable)
527 (boundp minibuffer-history-variable))
528 (let ((list (symbol-value minibuffer-history-variable)))
531 (and list (equal histval (car list)))
533 minibuffer-history-minimum-string-length
535 minibuffer-history-minimum-string-length))
536 (set minibuffer-history-variable
537 (if minibuffer-history-uniquify
538 (cons histval (remove histval list))
539 (cons histval list))))))
540 (if err (signal (car err) (cdr err)))
542 ;; stupid display code requires this for some reason
544 (buffer-disable-undo buffer)
545 (setq buffer-read-only nil)
548 ;; restore frame configurations
549 (if (and mconfig (frame-live-p oframe)
550 (eq frame (selected-frame)))
551 ;; if we changed frames (due to surrogate minibuffer),
552 ;; and we're still on the new frame, go back to the old one.
553 (select-frame oframe))
554 (if mconfig (set-window-configuration mconfig))
555 (set-window-configuration oconfig))))
558 (defun minibuffer-max-depth-exceeded ()
560 ;; This signals an error if an Nth minibuffer is invoked while N-1 are
561 ;; already active, whether the minibuffer window is selected or not.
562 ;; Since, under X, it's easy to jump out of the minibuffer (by doing M-x,
563 ;; getting distracted, and clicking elsewhere) many many novice users have
564 ;; had the problem of having multiple minibuffers build up, even to the
565 ;; point of exceeding max-lisp-eval-depth. Since the variable
566 ;; enable-recursive-minibuffers historically/crockishly is only consulted
567 ;; when the minibuffer is currently active (like typing M-x M-x) it doesn't
568 ;; help in this situation.
570 ;; This routine also offers to edit .emacs for you to get rid of this
571 ;; complaint, like `disabled' commands do, since it's likely that non-novice
572 ;; users will be annoyed by this change, so we give them an easy way to get
573 ;; rid of it forever.
575 (beep t 'minibuffer-limit-exceeded)
577 "Minibuffer already active: abort it with `^]', enable new one with `n': ")
578 (let ((char (let ((cursor-in-echo-area t)) ; #### doesn't always work??
583 ((y-or-n-p "Enable recursive minibuffers for other sessions too? ")
584 ;; This is completely disgusting, but it's basically what novice.el
585 ;; does. This kind of thing should be generalized.
586 (setq minibuffer-max-depth nil)
590 (substitute-in-file-name custom-file)))
591 (goto-char (point-min))
592 (if (re-search-forward
593 (concat "^(setq minibuffer-max-depth "
594 #r"\([0-9]+\|'?nil\|'?()\))"
597 (delete-region (match-beginning 0 ) (match-end 0))
598 ;; Must have been disabled by default.
599 (goto-char (point-max)))
600 (insert"\n(setq minibuffer-max-depth nil)\n")
602 (message "Multiple minibuffers enabled")
605 (abort-recursive-edit))
607 (error "Minibuffer already active")))))
610 ;;;; Guts of minibuffer completion
613 ;; Used by minibuffer-do-completion
614 (defvar last-exact-completion nil)
616 (defun temp-minibuffer-message (m)
617 (let ((savemax (point-max)))
619 (goto-char (point-max))
622 (let ((inhibit-quit t))
624 (delete-region savemax (point-max))
625 ;; If the user types a ^G while we're in sit-for, then quit-flag
626 ;; gets set. In this case, we want that ^G to be interpreted
627 ;; as a normal character, and act just like typeahead.
628 (if (and quit-flag (not unread-command-event))
629 (setq unread-command-event (character-to-event (quit-char))
633 ;; Determines whether buffer-string is an exact completion
634 (defun exact-minibuffer-completion-p (buffer-string)
635 (cond ((not minibuffer-completion-table)
638 ((vectorp minibuffer-completion-table)
639 (let ((tem (intern-soft buffer-string
640 minibuffer-completion-table)))
642 (and (string-equal buffer-string "nil")
643 ;; intern-soft loses for 'nil
645 (mapatoms #'(lambda (s)
650 minibuffer-completion-table)
652 (if minibuffer-completion-predicate
653 (funcall minibuffer-completion-predicate
657 ((and (consp minibuffer-completion-table)
658 ;;#### Emacs-Lisp truly sucks!
659 ;; lambda, autoload, etc
660 (not (symbolp (car minibuffer-completion-table))))
661 (if (not completion-ignore-case)
662 (assoc buffer-string minibuffer-completion-table)
663 (let ((s (upcase buffer-string))
664 (tail minibuffer-completion-table)
667 (setq tem (car (car tail)))
668 (if (or (equal tem buffer-string)
670 (if tem (equal (upcase tem) s)))
673 (setq tail (cdr tail))))
676 (funcall minibuffer-completion-table
678 minibuffer-completion-predicate
682 ;; 0 'none no possible completion
683 ;; 1 'unique was already an exact and unique completion
684 ;; 3 'exact was already an exact (but nonunique) completion
685 ;; NOT USED 'completed-exact-unique completed to an exact and completion
686 ;; 4 'completed-exact completed to an exact (but nonunique) completion
687 ;; 5 'completed some completion happened
688 ;; 6 'uncompleted no completion happened
689 (defun minibuffer-do-completion-1 (buffer-string completion)
690 (cond ((not completion)
693 ;; exact and unique match
696 ;; It did find a match. Do we match some possibility exactly now?
697 (let ((completedp (not (string-equal completion buffer-string))))
700 ;; Some completion happened
703 (setq buffer-string completion)))
704 (if (exact-minibuffer-completion-p buffer-string)
705 ;; An exact completion was possible
707 ;; Since no callers need to know the difference, don't bother
708 ;; with this (potentially expensive) discrimination.
709 ;; (if (eq (try-completion completion
710 ;; minibuffer-completion-table
711 ;; minibuffer-completion-predicate)
713 ;; 'completed-exact-unique
717 ;; Not an exact match
723 (defun minibuffer-do-completion (buffer-string)
724 (let* ((completion (try-completion buffer-string
725 minibuffer-completion-table
726 minibuffer-completion-predicate))
727 (status (minibuffer-do-completion-1 buffer-string completion))
728 (last last-exact-completion))
729 (setq last-exact-completion nil)
730 (cond ((eq status 'none)
732 (ding nil 'no-completion)
733 (temp-minibuffer-message " [No match]"))
737 ;; It did find a match. Do we match some possibility exactly now?
738 (if (not (string-equal completion buffer-string))
740 ;; Some completion happened
743 (setq buffer-string completion)))
744 (cond ((eq status 'exact)
745 ;; If the last exact completion and this one were
746 ;; the same, it means we've already given a
747 ;; "Complete but not unique" message and that the
748 ;; user's hit TAB again, so now we give help.
749 (setq last-exact-completion completion)
750 (if (equal buffer-string last)
751 (minibuffer-completion-help)))
752 ((eq status 'uncompleted)
753 (if completion-auto-help
754 (minibuffer-completion-help)
755 (temp-minibuffer-message " [Next char not unique]")))
763 (defun completing-read (prompt table
764 &optional predicate require-match
765 initial-contents history default)
766 "Read a string in the minibuffer, with completion.
768 PROMPT is a string to prompt with; normally it ends in a colon and a space.
769 TABLE is an alist whose elements' cars are strings, or an obarray.
770 TABLE can also be a function which does the completion itself.
771 PREDICATE limits completion to a subset of TABLE.
772 See `try-completion' and `all-completions' for more details
773 on completion, TABLE, and PREDICATE.
775 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
776 the input is (or completes to) an element of TABLE or is null.
777 If it is also not t, Return does not exit if it does non-null completion.
778 If INITIAL-CONTENTS is non-nil, insert it in the minibuffer initially.
779 If it is (STRING . POSITION), the initial input
780 is STRING, but point is placed POSITION characters into the string.
782 HISTORY, if non-nil, specifies a history list
783 and optionally the initial position in the list.
784 It can be a symbol, which is the history list variable to use,
785 or it can be a cons cell (HISTVAR . HISTPOS).
786 In that case, HISTVAR is the history list variable to use,
787 and HISTPOS is the initial position (the position in the list
788 which INITIAL-CONTENTS corresponds to).
789 If HISTORY is `t', no history will be recorded.
790 Positions are counted starting from 1 at the beginning of the list.
791 DEFAULT, if non-nil, will be returned when the user enters an empty
794 Completion ignores case if the ambient value of
795 `completion-ignore-case' is non-nil."
796 (let ((minibuffer-completion-table table)
797 (minibuffer-completion-predicate predicate)
798 (minibuffer-completion-confirm (if (eq require-match 't) nil t))
799 (last-exact-completion nil)
801 (setq ret (read-from-minibuffer prompt
803 (if (not require-match)
804 minibuffer-local-completion-map
805 minibuffer-local-must-match-map)
810 (if (and (string= ret "")
816 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
817 ;;;; Minibuffer completion commands ;;;;
818 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
821 (defun minibuffer-complete ()
822 "Complete the minibuffer contents as far as possible.
823 Return nil if there is no valid completion, else t.
824 If no characters can be completed, display a list of possible completions.
825 If you repeat this command after it displayed such a list,
826 scroll the window of possible completions."
828 ;; If the previous command was not this, then mark the completion
830 (or (eq last-command this-command)
831 (setq minibuffer-scroll-window nil))
832 (let ((window minibuffer-scroll-window))
833 (if (and window (windowp window) (window-buffer window)
834 (buffer-name (window-buffer window)))
835 ;; If there's a fresh completion window with a live buffer
836 ;; and this command is repeated, scroll that window.
837 (let ((obuf (current-buffer)))
840 (set-buffer (window-buffer window))
841 (if (pos-visible-in-window-p (point-max) window)
842 ;; If end is in view, scroll up to the beginning.
843 (set-window-start window (point-min))
844 ;; Else scroll down one frame.
845 (scroll-other-window)))
848 (let ((status (minibuffer-do-completion (buffer-string))))
849 (if (eq status 'none)
852 (cond ((eq status 'unique)
853 (temp-minibuffer-message
854 " [Sole completion]"))
856 (temp-minibuffer-message
857 " [Complete, but not unique]")))
861 (defun minibuffer-complete-and-exit ()
862 "Complete the minibuffer contents, and maybe exit.
863 Exit if the name is valid with no completion needed.
864 If name was completed to a valid match,
865 a repetition of this command will exit."
867 (if (= (point-min) (point-max))
868 ;; Crockishly allow user to specify null string
870 (let ((buffer-string (buffer-string)))
871 ;; Short-cut -- don't call minibuffer-do-completion if we already
872 ;; have an (possibly nonunique) exact completion.
873 (if (exact-minibuffer-completion-p buffer-string)
875 (let ((status (minibuffer-do-completion buffer-string)))
876 (if (or (eq status 'unique)
878 (if (or (eq status 'completed-exact)
879 (eq status 'completed-exact-unique))
880 (if minibuffer-completion-confirm
881 (progn (temp-minibuffer-message " [Confirm]")
884 (throw 'exit nil)))))
887 (defun self-insert-and-exit ()
888 "Terminate minibuffer input."
890 (self-insert-command 1)
893 (defun exit-minibuffer ()
894 "Terminate this minibuffer argument.
895 If minibuffer-confirm-incomplete is true, and we are in a completing-read
896 of some kind, and the contents of the minibuffer is not an existing
897 completion, requires an additional RET before the minibuffer will be exited
898 \(assuming that RET was the character that invoked this command:
899 the character in question must be typed again)."
901 (if (not minibuffer-confirm-incomplete)
903 (let ((buffer-string (buffer-string)))
904 (if (exact-minibuffer-completion-p buffer-string)
906 (let ((completion (if (not minibuffer-completion-table)
908 (try-completion buffer-string
909 minibuffer-completion-table
910 minibuffer-completion-predicate))))
911 (if (or (eq completion 't)
912 ;; Crockishly allow user to specify null string
913 (string-equal buffer-string ""))
915 (if completion ;; rewritten for I18N3 snarfing
916 (temp-minibuffer-message " [incomplete; confirm]")
917 (temp-minibuffer-message " [no completions; confirm]"))
918 (let ((event (let ((inhibit-quit t))
921 (setq quit-flag nil)))))
922 (cond ((equal event last-command-event)
924 ((equal (quit-char) (event-to-character event))
927 (dispatch-event event)))))
929 ;;;; minibuffer-complete-word
932 ;;;#### I think I have done this correctly; it certainly is simpler
933 ;;;#### than what the C code seemed to be trying to do.
934 (defun minibuffer-complete-word ()
935 "Complete the minibuffer contents at most a single word.
936 After one word is completed as much as possible, a space or hyphen
937 is added, provided that matches some possible completion.
938 Return nil if there is no valid completion, else t."
940 (let* ((buffer-string (buffer-string))
941 (completion (try-completion buffer-string
942 minibuffer-completion-table
943 minibuffer-completion-predicate))
944 (status (minibuffer-do-completion-1 buffer-string completion)))
945 (cond ((eq status 'none)
946 (ding nil 'no-completion)
947 (temp-minibuffer-message " [No match]")
950 ;; New message, only in this new Lisp code
951 (temp-minibuffer-message " [Sole completion]")
954 (cond ((or (eq status 'uncompleted)
956 (let ((foo #'(lambda (s)
959 (concat buffer-string s)
960 minibuffer-completion-table
961 minibuffer-completion-predicate)
963 (goto-char (point-max))
968 (char last-command-char))
969 ;; Try to complete by adding a word-delimiter
970 (or (and (characterp char) (> char 0)
971 (funcall foo (char-to-string char)))
972 (and (not (eq char ?\ ))
974 (and (not (eq char ?\-))
977 (if completion-auto-help
978 (minibuffer-completion-help)
979 ;; New message, only in this new Lisp code
980 ;; rewritten for I18N3 snarfing
981 (if (eq status 'exact)
982 (temp-minibuffer-message
983 " [Complete, but not unique]")
984 (temp-minibuffer-message " [Ambiguous]")))
989 ;; First word-break in stuff found by completion
990 (goto-char (point-min))
991 (let ((len (length buffer-string))
993 (if (and (< len (length completion))
998 (upcase (aref buffer-string n))
999 (upcase (aref completion n)))
1001 (throw 'match nil)))
1004 (goto-char (point-min))
1006 (re-search-forward "\\W" nil t)))
1007 (delete-region (point) (point-max))
1008 (goto-char (point-max))))
1012 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1013 ;;;; "Smart minibuffer" hackery ;;;;
1014 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1016 ;;; ("Kludgy minibuffer hackery" is perhaps a better name)
1018 ;; This works by setting `mouse-grabbed-buffer' to the minibuffer,
1019 ;; defining button2 in the minibuffer keymap to
1020 ;; `minibuffer-smart-select-highlighted-completion', and setting the
1021 ;; mode-motion-hook of the minibuffer to `minibuffer-mouse-tracker'.
1022 ;; By setting `mouse-grabbed-buffer', the minibuffer's keymap and
1023 ;; mode-motion-hook apply (for mouse motion and presses) no matter
1024 ;; what buffer the mouse is over. Then, `minibuffer-mouse-tracker'
1025 ;; examines the text under the mouse looking for something that looks
1026 ;; like a completion, and causes it to be highlighted, and
1027 ;; `minibuffer-smart-select-highlighted-completion' looks for a
1028 ;; flagged completion under the mouse and inserts it. This has the
1029 ;; following advantages:
1031 ;; -- filenames and such in any buffer can be inserted by clicking,
1032 ;; not just completions
1034 ;; but the following disadvantages:
1036 ;; -- unless you're aware of the "filename in any buffer" feature,
1037 ;; the fact that strings in arbitrary buffers get highlighted appears
1039 ;; -- mouse motion can cause ange-ftp actions -- bad bad bad.
1041 ;; There's some hackery in minibuffer-mouse-tracker to try to avoid the
1042 ;; ange-ftp stuff, but it doesn't work.
1045 (defcustom minibuffer-smart-completion-tracking-behavior nil
1046 "*If non-nil, look for completions under mouse in all buffers.
1047 This allows you to click on something that looks like a completion
1048 and have it selected, regardless of what buffer it is in.
1050 This is not enabled by default because
1052 -- The \"mysterious\" highlighting in normal buffers is confusing to
1053 people not expecting it, and looks like a bug
1054 -- If ange-ftp is enabled, this tracking sometimes causes ange-ftp
1055 action as a result of mouse motion, which is *bad bad bad*.
1056 Hopefully this bug will be fixed at some point."
1060 (defun minibuffer-smart-mouse-tracker (event)
1061 ;; Used as the mode-motion-hook of the minibuffer window, which is the
1062 ;; value of `mouse-grabbed-buffer' while the minibuffer is active. If
1063 ;; the word under the mouse is a valid minibuffer completion, then it
1066 ;; We do some special voodoo when we're reading a pathname, because
1067 ;; the way filename completion works is funny. Possibly there's some
1068 ;; more general way this could be dealt with...
1070 ;; We do some further voodoo when reading a pathname that is an
1071 ;; ange-ftp or efs path, because causing FTP activity as a result of
1072 ;; mouse motion is a really bad time.
1074 (and minibuffer-smart-completion-tracking-behavior
1076 ;; avoid conflict with display-completion-list extents
1077 (not (extent-at (event-point event)
1078 (event-buffer event)
1080 (let ((filename-kludge-p (eq minibuffer-completion-table
1081 'read-file-name-internal)))
1082 (mode-motion-highlight-internal
1084 #'(lambda () (default-mouse-track-beginning-of-word
1085 (if filename-kludge-p 'nonwhite t)))
1089 (default-mouse-track-end-of-word
1090 (if filename-kludge-p 'nonwhite t))
1091 (if (and (/= p (point)) minibuffer-completion-table)
1092 (setq string (buffer-substring p (point))))
1093 (if (string-match "\\`[ \t\n]*\\'" string)
1095 (if filename-kludge-p
1096 (setq string (minibuffer-smart-select-kludge-filename
1098 ;; try-completion bogusly returns a string even when
1099 ;; that string is complete if that string is also a
1100 ;; prefix for other completions. This means that we
1101 ;; can't just do the obvious thing, (eq t
1102 ;; (try-completion ...)).
1104 (if (and filename-kludge-p
1105 ;; #### evil evil evil evil
1106 (or (and (fboundp 'ange-ftp-ftp-path)
1107 (declare-fboundp (ange-ftp-ftp-path string)))
1108 (and (fboundp 'efs-ftp-path)
1109 (declare-fboundp (efs-ftp-path string)))))
1112 (try-completion string
1113 minibuffer-completion-table
1114 minibuffer-completion-predicate)))
1116 (and (equal comp string)
1117 (or (null minibuffer-completion-predicate)
1119 minibuffer-completion-predicate) ; ???
1120 (funcall minibuffer-completion-predicate
1122 minibuffer-completion-table)
1125 minibuffer-completion-table)
1127 (goto-char p))))))))))
1129 (defun minibuffer-smart-select-kludge-filename (string)
1131 (set-buffer mouse-grabbed-buffer) ; the minibuf
1132 (let ((kludge-string (concat (buffer-string) string)))
1133 (if (or (and (fboundp 'ange-ftp-ftp-path)
1134 (declare-fboundp (ange-ftp-ftp-path kludge-string)))
1135 (and (fboundp 'efs-ftp-path)
1136 (declare-fboundp (efs-ftp-path kludge-string))))
1137 ;; #### evil evil evil, but more so.
1139 (append-expand-filename (buffer-string) string)))))
1141 (defun minibuffer-smart-select-highlighted-completion (event)
1142 "Select the highlighted text under the mouse as a minibuffer response.
1143 When the minibuffer is being used to prompt the user for a completion,
1144 any valid completions which are visible on the frame will highlight
1145 when the mouse moves over them. Clicking \\<minibuffer-local-map>\
1146 \\[minibuffer-smart-select-highlighted-completion] will select the
1147 highlighted completion under the mouse.
1149 If the mouse is clicked while not over a highlighted completion,
1150 then the global binding of \\[minibuffer-smart-select-highlighted-completion] \
1151 will be executed instead. In this\nway you can get at the normal global \
1152 behavior of \\[minibuffer-smart-select-highlighted-completion] as well as
1153 the special minibuffer behavior."
1155 (if minibuffer-smart-completion-tracking-behavior
1156 (minibuffer-smart-select-highlighted-completion-1 event t)
1157 (let ((command (lookup-key global-map
1158 (vector current-mouse-event))))
1159 (if command (call-interactively command)))))
1161 (defun minibuffer-smart-select-highlighted-completion-1 (event global-p)
1162 (let* ((filename-kludge-p (eq minibuffer-completion-table
1163 'read-file-name-internal))
1166 (evpoint (event-point event))
1167 (evextent (and evpoint (extent-at evpoint (event-buffer event)
1170 ;; avoid conflict with display-completion-list extents.
1171 ;; if we find one, do that behavior instead.
1172 (list-mode-item-selected-1 evextent event)
1174 (let* ((buffer (window-buffer (event-window event)))
1175 (p (event-point event))
1176 (extent (and p (extent-at p buffer 'mouse-face))))
1178 (if (not (and (extent-live-p extent)
1179 (eq (extent-object extent) (current-buffer))
1180 (not (extent-detached-p extent))))
1182 ;; ...else user has selected a highlighted completion.
1184 (buffer-substring (extent-start-position extent)
1185 (extent-end-position extent)))
1186 (if filename-kludge-p
1187 (setq completion (minibuffer-smart-select-kludge-filename
1189 ;; remove the extent so that it's not hanging around in
1191 (detach-extent extent)
1192 (set-buffer mouse-grabbed-buffer)
1194 (insert completion))))
1195 ;; we need to execute the command or do the throw outside of the
1197 (cond ((and command-p global-p)
1198 (let ((command (lookup-key global-map
1199 (vector current-mouse-event))))
1201 (call-interactively command)
1202 (if minibuffer-completion-table
1204 "Highlighted words are valid completions. You may select one.")
1205 (error "no completions")))))
1207 ;; things get confused if the minibuffer is terminated while
1209 (select-window (minibuffer-window))
1210 (if (and filename-kludge-p (file-directory-p completion))
1211 ;; if the user clicked middle on a directory name, display the
1212 ;; files in that directory.
1214 (goto-char (point-max))
1215 (minibuffer-completion-help))
1216 ;; otherwise, terminate input
1217 (throw 'exit nil)))))))
1219 (defun minibuffer-smart-maybe-select-highlighted-completion
1220 (event &optional click-count)
1221 "Like `minibuffer-smart-select-highlighted-completion' but does nothing if
1222 there is no completion (as opposed to executing the global binding). Useful
1223 as the value of `mouse-track-click-hook'."
1225 (minibuffer-smart-select-highlighted-completion-1 event nil))
1227 (define-key minibuffer-local-map 'button2
1228 'minibuffer-smart-select-highlighted-completion)
1231 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1232 ;;;; Minibuffer History ;;;;
1233 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1235 (defvar minibuffer-history '()
1236 "Default minibuffer history list.
1237 This is used for all minibuffer input except when an alternate history
1238 list is specified.")
1240 ;; Some other history lists:
1242 (defvar minibuffer-history-search-history '())
1243 (defvar function-history '())
1244 (defvar variable-history '())
1245 (defvar buffer-history '())
1246 (defvar shell-command-history '())
1247 (defvar file-name-history '())
1249 (defvar read-expression-history nil)
1251 (defvar minibuffer-history-sexp-flag nil ;weird FSF Emacs kludge
1252 "Non-nil when doing history operations on `command-history'.
1253 More generally, indicates that the history list being acted on
1254 contains expressions rather than strings.")
1256 (defun previous-matching-history-element (regexp n)
1257 "Find the previous history element that matches REGEXP.
1258 \(Previous history elements refer to earlier actions.)
1259 With prefix argument N, search for Nth previous match.
1260 If N is negative, find the next or Nth next match."
1262 (let ((enable-recursive-minibuffers t)
1263 (minibuffer-history-sexp-flag nil)
1264 (minibuffer-max-depth (and minibuffer-max-depth
1265 (1+ minibuffer-max-depth))))
1266 (if (eq 't (symbol-value minibuffer-history-variable))
1267 (error "History is not being recorded in this context"))
1268 (list (read-from-minibuffer "Previous element matching (regexp): "
1269 (car minibuffer-history-search-history)
1270 minibuffer-local-map
1272 'minibuffer-history-search-history)
1273 (prefix-numeric-value current-prefix-arg))))
1274 (let ((history (symbol-value minibuffer-history-variable))
1276 (pos minibuffer-history-position))
1278 (error "History is not being recorded in this context"))
1281 (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
1283 (if (= pos 1) ;; rewritten for I18N3 snarfing
1284 (error "No later matching history item")
1285 (error "No earlier matching history item")))
1286 (if (string-match regexp
1287 (if minibuffer-history-sexp-flag
1288 (let ((print-level nil))
1289 (prin1-to-string (nth (1- pos) history)))
1290 (nth (1- pos) history)))
1291 (setq n (+ n (if (< n 0) 1 -1)))))
1292 (setq minibuffer-history-position pos)
1293 (setq current-minibuffer-contents (buffer-string)
1294 current-minibuffer-point (point))
1296 (let ((elt (nth (1- pos) history)))
1297 (insert (if minibuffer-history-sexp-flag
1298 (let ((print-level nil))
1299 (prin1-to-string elt))
1301 (goto-char (point-min)))
1302 (if (or (eq (car (car command-history)) 'previous-matching-history-element)
1303 (eq (car (car command-history)) 'next-matching-history-element))
1304 (setq command-history (cdr command-history))))
1306 (defun next-matching-history-element (regexp n)
1307 "Find the next history element that matches REGEXP.
1308 \(The next history element refers to a more recent action.)
1309 With prefix argument N, search for Nth next match.
1310 If N is negative, find the previous or Nth previous match."
1312 (let ((enable-recursive-minibuffers t)
1313 (minibuffer-history-sexp-flag nil)
1314 (minibuffer-max-depth (and minibuffer-max-depth
1315 (1+ minibuffer-max-depth))))
1316 (if (eq t (symbol-value minibuffer-history-variable))
1317 (error "History is not being recorded in this context"))
1318 (list (read-from-minibuffer "Next element matching (regexp): "
1319 (car minibuffer-history-search-history)
1320 minibuffer-local-map
1322 'minibuffer-history-search-history)
1323 (prefix-numeric-value current-prefix-arg))))
1324 (previous-matching-history-element regexp (- n)))
1326 (defun next-history-element (n)
1327 "Insert the next element of the minibuffer history into the minibuffer."
1329 (if (eq 't (symbol-value minibuffer-history-variable))
1330 (error "History is not being recorded in this context"))
1332 (when (eq minibuffer-history-position
1333 initial-minibuffer-history-position)
1334 (setq current-minibuffer-contents (buffer-string)
1335 current-minibuffer-point (point)))
1336 (let ((narg (- minibuffer-history-position n))
1337 (minimum (if minibuffer-default -1 0)))
1338 ;; a weird special case here; when in repeat-complex-command, we're
1339 ;; trying to edit the top command, and minibuffer-history-position
1340 ;; points to 1, the next-to-top command. in this case, the top
1341 ;; command in the history is suppressed in favor of the one being
1342 ;; edited, and there is no more command below it, except maybe the
1344 (if (and (zerop narg) (eq minibuffer-history-position
1345 initial-minibuffer-history-position))
1346 (setq minimum (1+ minimum)))
1347 (cond ((< narg minimum)
1348 (error (if minibuffer-default
1349 "No following item in %s"
1350 "No following item in %s; no default available")
1351 minibuffer-history-variable))
1352 ((> narg (length (symbol-value minibuffer-history-variable)))
1353 (error "No preceding item in %s" minibuffer-history-variable)))
1355 (setq minibuffer-history-position narg)
1356 (if (eq narg initial-minibuffer-history-position)
1358 (insert current-minibuffer-contents)
1359 (goto-char current-minibuffer-point))
1360 (let ((elt (if (> narg 0)
1361 (nth (1- minibuffer-history-position)
1362 (symbol-value minibuffer-history-variable))
1363 minibuffer-default)))
1365 (if (not (stringp elt))
1366 (let ((print-level nil))
1368 (let ((print-readably t)
1369 (print-escape-newlines t))
1370 (prin1-to-string elt))
1371 (error (prin1-to-string elt))))
1373 ;; FSF has point-min here.
1374 (goto-char (point-max))))))
1376 (defun previous-history-element (n)
1377 "Insert the previous element of the minibuffer history into the minibuffer."
1379 (next-history-element (- n)))
1381 (defun next-complete-history-element (n)
1382 "Get next element of history which is a completion of minibuffer contents."
1384 (let ((point-at-start (point)))
1385 (next-matching-history-element
1386 (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)
1387 ;; next-matching-history-element always puts us at (point-min).
1388 ;; Move to the position we were at before changing the buffer contents.
1389 ;; This is still sensical, because the text before point has not changed.
1390 (goto-char point-at-start)))
1392 (defun previous-complete-history-element (n)
1393 "Get previous element of history which is a completion of minibuffer contents."
1395 (next-complete-history-element (- n)))
1398 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1399 ;;;; reading various things from a minibuffer ;;;;
1400 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1402 (defun read-expression (prompt &optional initial-contents history default-value)
1403 "Return a Lisp object read using the minibuffer, prompting with PROMPT.
1404 If non-nil, optional second arg INITIAL-CONTENTS is a string to insert
1405 in the minibuffer before reading.
1406 Third arg HISTORY, if non-nil, specifies a history list.
1407 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
1408 for history command, and as the value to return if the user enters the
1410 (let ((minibuffer-history-sexp-flag t)
1411 ;; Semi-kludge to get around M-x C-x o M-ESC trying to do completion.
1412 (minibuffer-completion-table nil))
1413 (read-from-minibuffer prompt
1417 (or history 'read-expression-history)
1418 lisp-mode-abbrev-table
1421 (defun read-string (prompt &optional initial-contents history default-value)
1422 "Return a string from the minibuffer, prompting with string PROMPT.
1423 If non-nil, optional second arg INITIAL-CONTENTS is a string to insert
1424 in the minibuffer before reading.
1425 Third arg HISTORY, if non-nil, specifies a history list.
1426 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
1427 for history command, and as the value to return if the user enters the
1429 (let ((minibuffer-completion-table nil))
1430 (read-from-minibuffer prompt
1432 minibuffer-local-map
1433 nil history nil default-value)))
1435 (defun eval-minibuffer (prompt &optional initial-contents history default-value)
1436 "Return value of Lisp expression read using the minibuffer.
1437 Prompt with PROMPT. If non-nil, optional second arg INITIAL-CONTENTS
1438 is a string to insert in the minibuffer before reading.
1439 Third arg HISTORY, if non-nil, specifies a history list.
1440 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
1441 for history command, and as the value to return if the user enters the
1443 (eval (read-expression prompt initial-contents history default-value)))
1445 ;; The name `command-history' is already taken
1446 (defvar read-command-history '())
1448 (defun read-command (prompt &optional default-value)
1449 "Read the name of a command and return as a symbol.
1450 Prompts with PROMPT. By default, return DEFAULT-VALUE."
1451 (intern (completing-read prompt obarray 'commandp t nil
1452 ;; 'command-history is not right here: that's a
1453 ;; list of evalable forms, not a history list.
1454 'read-command-history
1457 (defun read-function (prompt &optional default-value)
1458 "Read the name of a function and return as a symbol.
1459 Prompts with PROMPT. By default, return DEFAULT-VALUE."
1460 (intern (completing-read prompt obarray 'fboundp t nil
1461 'function-history default-value)))
1463 (defun read-variable (prompt &optional default-value)
1464 "Read the name of a user variable and return it as a symbol.
1465 Prompts with PROMPT. By default, return DEFAULT-VALUE.
1466 A user variable is one whose documentation starts with a `*' character."
1467 (intern (completing-read prompt obarray 'user-variable-p t nil
1469 (if (symbolp default-value)
1470 (symbol-name default-value)
1473 (defun read-buffer (prompt &optional default require-match)
1474 "Read the name of a buffer and return as a string.
1475 Prompts with PROMPT. Optional second arg DEFAULT is value to return if user
1476 enters an empty line. If optional third arg REQUIRE-MATCH is non-nil,
1477 only existing buffer names are allowed."
1478 (let ((prompt (if default
1479 (format "%s(default %s) "
1480 (gettext prompt) (if (bufferp default)
1481 (buffer-name default)
1484 (alist (mapcar #'(lambda (b) (cons (buffer-name b) b))
1488 (setq result (completing-read prompt alist nil require-match
1490 (if (bufferp default)
1491 (buffer-name default)
1493 (cond ((not (equal result ""))
1495 ((not require-match)
1496 (setq result default)
1500 ((not (get-buffer default))
1503 (setq result default)
1505 (if (bufferp result)
1506 (buffer-name result)
1509 (defun read-number (prompt &optional integers-only default-value)
1510 "Read a number from the minibuffer, prompting with PROMPT.
1511 If optional second argument INTEGERS-ONLY is non-nil, accept
1513 If DEFAULT-VALUE is non-nil, return that if user enters an empty
1515 (let ((pred (if integers-only 'integerp 'numberp))
1517 (while (not (funcall pred num))
1518 (setq num (condition-case ()
1519 (let ((minibuffer-completion-table nil))
1520 (read-from-minibuffer
1521 prompt (if num (prin1-to-string num)) nil t
1522 nil nil default-value))
1524 (invalid-read-syntax nil)
1526 (or (funcall pred num) (beep)))
1529 (defun read-shell-command (prompt &optional initial-input history default-value)
1530 "Just like read-string, but uses read-shell-command-map:
1531 \\{read-shell-command-map}"
1532 (let ((minibuffer-completion-table nil))
1533 (read-from-minibuffer prompt initial-input read-shell-command-map
1534 nil (or history 'shell-command-history)
1535 nil default-value)))
1538 ;;; This read-file-name stuff probably belongs in files.el
1540 ;; Quote "$" as "$$" to get it past substitute-in-file-name
1541 (defun un-substitute-in-file-name (string)
1542 (let ((regexp "\\$")
1543 (olen (length string))
1546 (if (not (string-match regexp string))
1549 (while (string-match regexp string (match-end 0))
1551 (setq new (make-string (+ olen n) ?$))
1554 (setq ch (aref string o))
1556 (setq o (1+ o) n (1+ n))
1558 ;; already aset by make-string initial-value
1563 ;; Wrapper for `directory-files' for use in generating completion lists.
1564 ;; Generates output in the same format as `file-name-all-completions'.
1566 ;; The EFS replacement for `directory-files' doesn't support the FILES-ONLY
1567 ;; option, so it has to be faked. The listing cache will hopefully
1568 ;; improve the performance of this operation.
1569 (defun minibuf-directory-files (dir &optional match-regexp files-only)
1570 (let ((want-file (or (eq files-only nil) (eq files-only t)))
1571 (want-dirs (or (eq files-only nil) (not (eq files-only t)))))
1573 (mapcar (function (lambda (f)
1574 (if (file-directory-p (expand-file-name f dir))
1575 (and want-dirs (file-name-as-directory f))
1576 (and want-file f))))
1577 (delete "." (directory-files dir nil match-regexp))))))
1580 (defun read-file-name-2 (history prompt dir default
1581 must-match initial-contents
1584 (setq dir default-directory))
1585 (setq dir (abbreviate-file-name dir t))
1586 (let* ((insert (cond ((and (not insert-default-directory)
1587 (not initial-contents))
1590 (cons (un-substitute-in-file-name
1591 (concat dir initial-contents))
1594 (un-substitute-in-file-name dir))))
1596 ;; Hateful, broken, case-sensitive un*x
1597 ;;; (completing-read prompt
1603 ;; #### - this is essentially the guts of completing read.
1604 ;; There should be an elegant way to pass a pair of keymaps to
1605 ;; completing read, but this will do for now. All sins are
1607 (let ((minibuffer-completion-table completer)
1608 (minibuffer-completion-predicate dir)
1609 (minibuffer-completion-confirm (if (eq must-match 't)
1611 (last-exact-completion nil))
1612 (read-from-minibuffer prompt
1614 (if (not must-match)
1616 read-file-name-must-match-map)
1621 ;;; ;; Kludge! Put "/foo/bar" on history rather than "/default//foo/bar"
1622 ;;; (let ((hist (cond ((not history) 'minibuffer-history)
1623 ;;; ((consp history) (car history))
1627 ;;; (not (eq hist 't))
1629 ;;; (equal (car-safe (symbol-value hist)) val))
1630 ;;; (let ((e (condition-case nil
1631 ;;; (expand-file-name val)
1633 ;;; (if (and e (not (equal e val)))
1634 ;;; (set hist (cons e (cdr (symbol-value hist))))))))
1637 (error "No file name specified"))
1639 (equal val (if (consp insert) (car insert) insert)))
1642 (substitute-in-file-name val)))))
1644 ;; #### this function should use minibuffer-completion-table
1645 ;; or something. But that is sloooooow.
1646 ;; #### all this shit needs better documentation!!!!!!!!
1647 (defun read-file-name-activate-callback (event extent dir-p)
1648 ;; used as the activate-callback of the filename list items
1649 ;; in the completion buffer, in place of default-choose-completion.
1650 ;; if a regular file was selected, we call default-choose-completion
1651 ;; (which just inserts the string in the minibuffer and calls
1652 ;; exit-minibuffer). If a directory was selected, we display
1653 ;; the contents of the directory.
1654 (let* ((file (extent-string extent))
1655 (completion-buf (extent-object extent))
1656 (minibuf (symbol-value-in-buffer 'completion-reference-buffer
1658 (in-dir (file-name-directory (buffer-substring nil nil minibuf)))
1659 (full (expand-file-name file in-dir)))
1660 (if (not (file-directory-p full))
1661 (default-choose-completion event extent minibuf)
1662 (erase-buffer minibuf)
1663 (insert-string (file-name-as-directory
1664 (abbreviate-file-name full t)) minibuf)
1665 (reset-buffer completion-buf)
1666 (let ((standard-output completion-buf))
1667 (display-completion-list
1668 (minibuf-directory-files full nil (if dir-p 'directory))
1670 :reference-buffer minibuf
1671 :activate-callback 'read-file-name-activate-callback)
1672 (goto-char (point-min) completion-buf)))))
1674 (defun read-file-name-1 (type history prompt dir default
1675 must-match initial-contents
1677 (if (should-use-dialog-box-p)
1680 (apply #'make-dialog-box
1681 type `(:title ,(capitalize-string-as-title
1682 ;; Kludge: Delete ": " off the end.
1683 (replace-in-string prompt ": $" ""))
1684 ,@(and dir (list :initial-directory
1686 :file-must-exist ,must-match
1687 ,@(and initial-contents
1688 (list :initial-filename
1689 initial-contents))))))
1690 ;; hack -- until we implement reading a directory properly,
1691 ;; allow a file as indicating the directory it's in
1692 (if (and (eq completer 'read-directory-name-internal)
1693 (not (file-directory-p file)))
1694 (file-name-directory file)
1697 ;; this calls read-file-name-2
1698 (mouse-read-file-name-1 history prompt dir default must-match
1699 initial-contents completer)
1702 'minibuffer-setup-hook
1704 ;; #### SCREAM! Create a `file-system-ignore-case'
1705 ;; function, so this kind of stuff is generalized!
1706 (and (eq system-type 'windows-nt)
1707 (set (make-local-variable 'completion-ignore-case) t))
1709 (make-local-variable
1710 'completion-display-completion-list-function)
1711 #'(lambda (completions)
1712 (display-completion-list
1714 :user-data (not (eq completer 'read-file-name-internal))
1716 'read-file-name-activate-callback)))))
1717 (read-file-name-2 history prompt dir default must-match
1718 initial-contents completer)))
1720 (defun read-file-name (prompt
1721 &optional dir default must-match initial-contents
1723 "Read file name, prompting with PROMPT and completing in directory DIR.
1724 This will prompt with a dialog box if appropriate, according to
1725 `should-use-dialog-box-p'.
1726 Value is not expanded---you must call `expand-file-name' yourself.
1727 Value is subject to interpretation by `substitute-in-file-name' however.
1728 Default name to DEFAULT if user enters a null string.
1729 (If DEFAULT is omitted, the visited file name is used,
1730 except that if INITIAL-CONTENTS is specified, that combined with DIR is
1732 Fourth arg MUST-MATCH non-nil means require existing file's name.
1733 Non-nil and non-t means also require confirmation after completion.
1734 Fifth arg INITIAL-CONTENTS specifies text to start with. If this is not
1735 specified, and `insert-default-directory' is non-nil, DIR or the current
1736 directory will be used.
1737 Sixth arg HISTORY specifies the history list to use. Default is
1738 `file-name-history'.
1739 DIR defaults to current buffer's directory default."
1741 'file (or history 'file-name-history)
1742 prompt dir (or default
1743 (and initial-contents
1744 (abbreviate-file-name (expand-file-name
1745 initial-contents dir) t))
1746 (and buffer-file-truename
1747 (abbreviate-file-name buffer-file-name t)))
1748 must-match initial-contents
1749 ;; A separate function (not an anonymous lambda-expression)
1750 ;; and passed as a symbol because of disgusting kludges in various
1751 ;; places which do stuff like (let ((filename-kludge-p (eq minibuffer-completion-table 'read-file-name-internal))) ...)
1752 'read-file-name-internal))
1754 (defun read-directory-name (prompt
1755 &optional dir default must-match initial-contents
1757 "Read directory name, prompting with PROMPT and completing in directory DIR.
1758 This will prompt with a dialog box if appropriate, according to
1759 `should-use-dialog-box-p'.
1760 Value is not expanded---you must call `expand-file-name' yourself.
1761 Value is subject to interpreted by substitute-in-file-name however.
1762 Default name to DEFAULT if user enters a null string.
1763 (If DEFAULT is omitted, the current buffer's default directory is used.)
1764 Fourth arg MUST-MATCH non-nil means require existing directory's name.
1765 Non-nil and non-t means also require confirmation after completion.
1766 Fifth arg INITIAL-CONTENTS specifies text to start with.
1767 Sixth arg HISTORY specifies the history list to use. Default is
1768 `file-name-history'.
1769 DIR defaults to current buffer's directory default."
1771 'directory (or history 'file-name-history)
1772 prompt dir (or default default-directory) must-match initial-contents
1773 'read-directory-name-internal))
1776 ;; Environment-variable and ~username completion hack
1777 (defun read-file-name-internal-1 (string dir action completer)
1778 (if (not (string-match
1779 #r"\([^$]\|\`\)\(\$\$\)*\$\([A-Za-z0-9_]*\|{[^}]*\)\'"
1781 ;; Not doing environment-variable completion hack
1782 (let* ((orig (if (equal string "") nil string))
1783 (sstring (if orig (substitute-in-file-name string) string))
1784 (specdir (if orig (file-name-directory sstring) nil))
1785 (name (if orig (file-name-nondirectory sstring) string))
1786 (direct (if specdir (expand-file-name specdir dir) dir)))
1787 ;; ~username completion
1788 (if (and (fboundp 'user-name-completion-1)
1789 (string-match "^[~]" name))
1790 (let ((user (substring name 1)))
1791 (cond ((eq action 'lambda)
1792 (file-directory-p name))
1795 (mapcar #'(lambda (p) (concat "~" p))
1796 (user-name-all-completions user)))
1799 (let* ((val+uniq (user-name-completion-1 user))
1800 (val (car val+uniq))
1801 (uniq (cdr val+uniq)))
1802 (cond ((stringp val)
1804 (file-name-as-directory (concat "~" val))
1807 (file-name-as-directory name))
1816 ;; An odd number of trailing $'s
1817 (let* ((start (match-beginning 3))
1818 (env (substring string
1819 (cond ((= start (length string))
1822 ((= (aref string start) ?{)
1827 (head (substring string 0 (1- start)))
1829 (mapcar #'(lambda (x)
1830 (cons (substring x 0 (string-match "=" x))
1832 process-environment))))
1834 (cond ((eq action 'lambda)
1838 (mapcar #'(lambda (p)
1839 (if (and (> (length p) 0)
1840 ;;#### Unix-specific
1841 ;;#### -- need absolute-pathname-p
1844 (concat head "$" p)))
1845 (all-completions env (funcall alist))))
1848 (let* ((e (funcall alist))
1849 (val (try-completion env e)))
1850 (cond ((stringp val)
1851 (if (string-match "[^A-Za-z0-9_]" val)
1854 ;; completed uniquely?
1855 (if (eq (try-completion val e) 't)
1857 (concat head "$" val)))
1860 (un-substitute-in-file-name (getenv env))))
1864 (defun read-file-name-internal (string dir action)
1865 (read-file-name-internal-1
1867 #'(lambda (action orig string specdir dir name)
1868 (cond ((eq action 'lambda)
1871 (let ((sstring (condition-case nil
1872 (expand-file-name string)
1875 ;; Some pathname syntax error in string
1877 (file-exists-p sstring)))))
1880 (mapcar #'un-substitute-in-file-name
1881 (if (string= name "")
1882 (delete "./" (file-name-all-completions "" dir))
1883 (file-name-all-completions name dir))))
1886 (let* ((d (or dir default-directory))
1887 (val (file-name-completion name d)))
1888 (if (and (eq val 't)
1889 (not (null completion-ignored-extensions)))
1890 ;;#### (file-name-completion "foo") returns 't
1891 ;; when both "foo" and "foo~" exist and the latter
1892 ;; is "pruned" by completion-ignored-extensions.
1893 ;; I think this is a bug in file-name-completion.
1894 (setq val (let ((completion-ignored-extensions '()))
1895 (file-name-completion name d))))
1897 (un-substitute-in-file-name (if specdir
1898 (concat specdir val)
1900 (let ((tem (un-substitute-in-file-name string)))
1901 (if (not (equal tem orig))
1902 ;; substitute-in-file-name did something
1906 (defun read-directory-name-internal (string dir action)
1907 (read-file-name-internal-1
1909 #'(lambda (action orig string specdir dir name)
1910 (let* ((dirs #'(lambda (fn)
1911 (let ((l (if (equal name "")
1912 (minibuf-directory-files
1916 (minibuf-directory-files
1918 (concat "\\`" (regexp-quote name))
1923 (cond ((eq action 'lambda)
1927 (file-directory-p string)))
1930 (funcall dirs #'(lambda (n)
1931 (un-substitute-in-file-name
1932 (file-name-as-directory n)))))
1935 (let ((val (try-completion
1939 (list (file-name-as-directory
1942 (un-substitute-in-file-name (if specdir
1943 (concat specdir val)
1945 (let ((tem (un-substitute-in-file-name string)))
1946 (if (not (equal tem orig))
1947 ;; substitute-in-file-name did something
1951 (defun append-expand-filename (file-string string)
1952 "Append STRING to FILE-STRING differently depending on whether STRING
1953 is a username (~string), an environment variable ($string),
1954 or a filename (/string). The resultant string is returned with the
1955 environment variable or username expanded and resolved to indicate
1956 whether it is a file(/result) or a directory (/result/)."
1958 (cond ((string-match #r"\([~$]\)\([^~$/]*\)$" file-string)
1959 (cond ((string= (substring file-string
1962 (concat (substring file-string 0 (match-end 1))
1964 (t (substitute-in-file-name
1965 (concat (substring file-string 0 (match-end 1))
1967 (t (concat (file-name-directory
1968 (substitute-in-file-name file-string)) string))))
1971 (cond ((stringp (setq result (and (file-exists-p (expand-file-name file))
1972 (read-file-name-internal
1974 (expand-file-name file)
1980 (defun mouse-rfn-setup-vars (prompt)
1981 ;; a specifier would be nice.
1982 (set (make-local-variable 'frame-title-format)
1983 (capitalize-string-as-title
1984 ;; Kludge: Delete ": " off the end.
1985 (replace-in-string prompt ": $" "")))
1986 ;; ensure that killing the frame works right,
1987 ;; instead of leaving us in the minibuffer.
1988 (add-local-hook 'delete-frame-hook
1990 (abort-recursive-edit))))
1992 (defun mouse-file-display-completion-list (window dir minibuf user-data)
1993 (let ((standard-output (window-buffer window)))
1995 (display-completion-list
1996 (minibuf-directory-files dir nil t)
1997 :window-width (window-width window)
1998 :window-height (window-text-area-height window)
1999 :completion-string ""
2001 'mouse-read-file-name-activate-callback
2002 :user-data user-data
2003 :reference-buffer minibuf
2008 (defun mouse-directory-display-completion-list (window dir minibuf user-data)
2009 (let ((standard-output (window-buffer window)))
2011 (display-completion-list
2012 (minibuf-directory-files dir nil 1)
2013 :window-width (window-width window)
2014 :window-height (window-text-area-height window)
2015 :completion-string ""
2017 'mouse-read-file-name-activate-callback
2018 :user-data user-data
2019 :reference-buffer minibuf
2024 (defun mouse-read-file-name-activate-callback (event extent user-data)
2025 (let* ((file (extent-string extent))
2026 (minibuf (symbol-value-in-buffer 'completion-reference-buffer
2027 (extent-object extent)))
2028 (ministring (buffer-substring nil nil minibuf))
2029 (in-dir (file-name-directory ministring))
2030 (full (expand-file-name file in-dir))
2031 (filebuf (nth 0 user-data))
2032 (dirbuf (nth 1 user-data))
2033 (filewin (nth 2 user-data))
2034 (dirwin (nth 3 user-data)))
2035 (if (file-regular-p full)
2036 (default-choose-completion event extent minibuf)
2037 (erase-buffer minibuf)
2038 (insert-string (file-name-as-directory
2039 (abbreviate-file-name full t)) minibuf)
2040 (reset-buffer filebuf)
2042 (mouse-directory-display-completion-list filewin full minibuf
2044 (mouse-file-display-completion-list filewin full minibuf user-data)
2045 (reset-buffer dirbuf)
2046 (mouse-directory-display-completion-list dirwin full minibuf
2049 ;; our cheesy but god-awful time consuming file dialog box implementation.
2050 ;; this will be replaced with use of the native file dialog box (when
2052 (defun mouse-read-file-name-1 (history prompt dir default
2053 must-match initial-contents
2055 ;; file-p is t if we're reading files, nil if directories.
2056 (let* ((file-p (eq 'read-file-name-internal completer))
2057 (filebuf (get-buffer-create "*Completions*"))
2058 (dirbuf (and file-p (generate-new-buffer " *mouse-read-file*")))
2059 (butbuf (generate-new-buffer " *mouse-read-file*"))
2060 (frame (make-dialog-frame))
2065 (reset-buffer filebuf)
2067 ;; set up the frame.
2069 (let ((window-min-height 1))
2070 ;; #### should be 2 not 3, but that causes
2071 ;; "window too small to split" errors for some
2072 ;; people (but not for me ...) There's a more
2073 ;; fundamental bug somewhere.
2074 (split-window nil (- (frame-height frame) 3)))
2077 (split-window-horizontally 16)
2078 (setq filewin (frame-rightmost-window frame)
2079 dirwin (frame-leftmost-window frame))
2080 (set-window-buffer filewin filebuf)
2081 (set-window-buffer dirwin dirbuf))
2082 (setq filewin (frame-highest-window frame))
2083 (set-window-buffer filewin filebuf))
2084 (setq user-data (list filebuf dirbuf filewin dirwin))
2085 (set-window-buffer (frame-lowest-window frame) butbuf)
2087 ;; set up completion buffers.
2090 ;; #### I really need to flesh out the object
2091 ;; hierarchy better to avoid these kludges.
2092 ;; (?? I wrote this comment above some time ago,
2093 ;; and I don't understand what I'm referring to
2096 (mouse-rfn-setup-vars prompt)
2097 (when (featurep 'scrollbar)
2098 (set-specifier scrollbar-width 0 (current-buffer)))
2099 (setq truncate-lines t))))
2101 (set-buffer filebuf)
2102 (add-local-hook 'completion-setup-hook rfcshookfun)
2105 (add-local-hook 'completion-setup-hook rfcshookfun)))
2107 ;; set up minibuffer.
2109 'minibuffer-setup-hook
2112 (mouse-directory-display-completion-list
2113 filewin dir (current-buffer) user-data)
2114 (mouse-file-display-completion-list
2115 filewin dir (current-buffer) user-data)
2116 (mouse-directory-display-completion-list
2117 dirwin dir (current-buffer) user-data))
2119 (make-local-variable
2120 'completion-display-completion-list-function)
2121 (lambda (completions)
2122 (display-completion-list
2125 :window-width (window-width filewin)
2126 :window-height (window-text-area-height filewin)
2127 :completion-string ""
2129 'mouse-read-file-name-activate-callback
2130 :user-data user-data)))
2131 (mouse-rfn-setup-vars prompt)
2132 (save-selected-window
2133 ;; kludge to ensure the frame title is correct.
2134 ;; the minibuffer leaves the frame title the way
2135 ;; it was before (i.e. of the selected window before
2136 ;; the dialog box was opened), so to get it correct
2137 ;; we have to be tricky.
2138 (select-window filewin)
2139 (redisplay-frame nil t)
2140 ;; #### another kludge. sometimes the focus ends up
2141 ;; back in the main window, not the dialog box. it
2142 ;; occurs randomly and it's not possible to reliably
2143 ;; reproduce. We try to fix it by draining non-user
2144 ;; events and then setting the focus back on the frame.
2146 (focus-frame frame))))
2148 ;; set up button buffer.
2150 (mouse-rfn-setup-vars prompt)
2152 (setq default-directory dir))
2153 (when (featurep 'scrollbar)
2154 (set-specifier scrollbar-width 0 butbuf))
2156 (insert-gui-button (make-gui-button "OK"
2158 (exit-minibuffer))))
2160 (insert-gui-button (make-gui-button "Cancel"
2162 (abort-recursive-edit))))
2164 ;; now start reading filename.
2165 (read-file-name-2 history prompt dir default
2166 must-match initial-contents
2170 ;; get rid of our hook that calls abort-recursive-edit -- not a good
2172 (kill-local-variable 'delete-frame-hook)
2173 (delete-frame frame)
2174 (kill-buffer filebuf)
2175 (kill-buffer butbuf)
2176 (and dirbuf (kill-buffer dirbuf)))))
2178 (defun read-face (prompt &optional must-match)
2179 "Read the name of a face from the minibuffer and return it as a symbol."
2180 (intern (completing-read prompt obarray 'find-face must-match)))
2183 (defun read-color-completion-table ()
2185 ;; #### Evil device-type dependency
2187 (if-fboundp #'x-read-color-completion-table
2188 (x-read-color-completion-table)
2189 (let ((rgb-file (locate-file "rgb.txt" x-library-search-path))
2192 ;; prevents multiple searches for rgb.txt if we can't find it
2193 (setq x-read-color-completion-table nil)
2194 (with-current-buffer (get-buffer-create " *colors*")
2195 (reset-buffer (current-buffer))
2196 (insert-file-contents rgb-file)
2198 ;; skip over comments
2199 (while (looking-at "^!")
2202 (skip-chars-forward "0-9 \t")
2205 (setq color (buffer-substring p (point))
2206 clist (cons (list color) clist))
2207 ;; Ugh. If we want to be able to complete the lowercase form
2208 ;; of the color name, we need to add it twice! Yuck.
2209 (let ((dcase (downcase color)))
2210 (or (string= dcase color)
2211 (push (list dcase) clist)))
2213 (kill-buffer (current-buffer))))
2214 (setq x-read-color-completion-table clist)
2215 x-read-color-completion-table)))
2217 (mapcar #'list (tty-color-list)))))
2221 (defun read-color (prompt &optional must-match initial-contents)
2222 "Read the name of a color from the minibuffer."
2223 (let ((table (x-read-color-completion-table)))
2224 (completing-read prompt table nil (and table must-match)
2228 ;; #### The doc string for read-non-nil-coding system gets lost if we
2229 ;; only include these if the mule feature is present. Strangely,
2230 ;; read-coding-system doesn't.
2232 ;;(if (featurep 'mule)
2234 (defun read-coding-system (prompt &optional default-coding-system)
2235 "Read a coding-system (or nil) from the minibuffer.
2236 Prompting with string PROMPT.
2237 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
2238 DEFAULT-CODING-SYSTEM can be a string, symbol, or coding-system object."
2239 (intern (completing-read prompt obarray 'find-coding-system t nil nil
2240 (cond ((symbolp default-coding-system)
2241 (symbol-name default-coding-system))
2242 ((coding-system-p default-coding-system)
2243 (symbol-name (coding-system-name default-coding-system)))
2245 default-coding-system)))))
2247 (defun read-non-nil-coding-system (prompt)
2248 "Read a non-nil coding-system from the minibuffer.
2249 Prompt with string PROMPT."
2250 (let ((retval (intern "")))
2251 (while (= 0 (length (symbol-name retval)))
2252 (setq retval (intern (completing-read prompt obarray
2257 ;;) ;; end of (featurep 'mule)
2261 (defcustom force-dialog-box-use nil
2262 "*If non-nil, always use a dialog box for asking questions, if possible.
2263 You should *bind* this, not set it. This is useful if you're doing
2264 something mousy but which wasn't actually invoked using the mouse."
2268 ;; We include this here rather than dialog.el so it is defined
2269 ;; even when dialog boxes are not present.
2270 (defun should-use-dialog-box-p ()
2271 "If non-nil, questions should be asked with a dialog box instead of the
2272 minibuffer. This looks at `last-command-event' to see if it was a mouse
2273 event, and checks whether dialog-support exists and the current device
2274 supports dialog boxes.
2276 The dialog box is totally disabled if the variable `use-dialog-box'
2278 (and (featurep 'dialog)
2279 (device-on-window-system-p)
2281 (or force-dialog-box-use
2282 (button-press-event-p last-command-event)
2283 (button-release-event-p last-command-event)
2284 (misc-user-event-p last-command-event))))
2286 ;;; minibuf.el ends here