XEmacs 21.2-b1
[chise/xemacs-chise.git.1] / lisp / minibuf.el
1 ;;; minibuf.el --- Minibuffer functions for XEmacs
2
3 ;; Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems
5 ;; Copyright (C) 1995, 1996 Ben Wing
6
7 ;; Author: Richard Mlynarik
8 ;; Created: 2-Oct-92
9 ;; Maintainer: XEmacs Development Team
10 ;; Keywords: internal, dumped
11
12 ;; This file is part of XEmacs.
13
14 ;; XEmacs is free software; you can redistribute it and/or modify it
15 ;; under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; XEmacs is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with XEmacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Synched up with: all the minibuffer history stuff is synched with
30 ;;; 19.30.  Not sure about the rest.
31
32 ;;; Commentary:
33
34 ;; This file is dumped with XEmacs.
35
36 ;; Written by Richard Mlynarik 2-Oct-92
37
38 ;; 06/11/1997 -  Use char-(after|before) instead of
39 ;;  (following|preceding)-char. -slb
40
41 ;;; Code:
42
43 (defgroup minibuffer nil
44   "Controling the behaviour of the minibuffer."
45   :group 'environment)
46
47
48 (defcustom insert-default-directory t
49  "*Non-nil means when reading a filename start with default dir in minibuffer."
50  :type 'boolean
51  :group 'minibuffer)
52
53 (defcustom minibuffer-history-uniquify t
54   "*Non-nil means when adding an item to a minibuffer history, remove
55 previous occurances of the same item from the history list first,
56 rather than just consing the new element onto the front of the list."
57   :type 'boolean
58   :group 'minibuffer)
59
60 (defvar minibuffer-completion-table nil
61   "Alist or obarray used for completion in the minibuffer.
62 This becomes the ALIST argument to `try-completion' and `all-completions'.
63
64 The value may alternatively be a function, which is given three arguments:
65   STRING, the current buffer contents;
66   PREDICATE, the predicate for filtering possible matches;
67   CODE, which says what kind of things to do.
68 CODE can be nil, t or `lambda'.
69 nil means to return the best completion of STRING, nil if there is none,
70   or t if it is was already a unique completion.
71 t means to return a list of all possible completions of STRING.
72 `lambda' means to return t if STRING is a valid completion as it stands.")
73
74 (defvar minibuffer-completion-predicate nil
75   "Within call to `completing-read', this holds the PREDICATE argument.")
76
77 (defvar minibuffer-completion-confirm nil
78   "Non-nil => demand confirmation of completion before exiting minibuffer.")
79
80 (defvar minibuffer-confirm-incomplete nil
81   "If true, then in contexts where completing-read allows answers which
82 are not valid completions, an extra RET must be typed to confirm the
83 response.  This is helpful for catching typos, etc.")
84
85 (defcustom completion-auto-help t
86   "*Non-nil means automatically provide help for invalid completion input."
87   :type 'boolean
88   :group 'minibuffer)
89
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."
95   :type 'boolean
96   :group 'minibuffer)
97
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))
105   :group 'minibuffer)
106
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.")
111
112 (defvar minibuffer-exit-hook nil
113   "Normal hook run just after exit from minibuffer.")
114
115 (defvar minibuffer-help-form nil
116   "Value that `help-form' takes on inside the minibuffer.")
117
118 (defvar minibuffer-default nil
119   "Default value for minibuffer input.")
120   
121 (defvar minibuffer-local-map
122   (let ((map (make-sparse-keymap 'minibuffer-local-map)))
123     map)
124   "Default keymap to use when reading from the minibuffer.")
125
126 (defvar minibuffer-local-completion-map
127   (let ((map (make-sparse-keymap 'minibuffer-local-completion-map)))
128     (set-keymap-parents map (list minibuffer-local-map))
129     map)
130   "Local keymap for minibuffer input with completion.")
131
132 (defvar minibuffer-local-must-match-map
133   (let ((map (make-sparse-keymap 'minibuffer-must-match-map)))
134     (set-keymap-parents map (list minibuffer-local-completion-map))
135     map)
136   "Local keymap for minibuffer input with completion, for exact match.")
137
138 ;; (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
139 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit) ;; moved here from pending-del.el
140 (define-key minibuffer-local-map "\r" 'exit-minibuffer)
141 (define-key minibuffer-local-map "\n" 'exit-minibuffer)
142
143 ;; Historical crock.  Unused by anything but user code, if even that
144 ;(defvar minibuffer-local-ns-map
145 ;  (let ((map (make-sparse-keymap 'minibuffer-local-ns-map)))
146 ;    (set-keymap-parents map (list minibuffer-local-map))
147 ;    map)
148 ;  "Local keymap for the minibuffer when spaces are not allowed.")
149 ;(define-key minibuffer-local-ns-map [space] 'exit-minibuffer)
150 ;(define-key minibuffer-local-ns-map [tab] 'exit-minibuffer)
151 ;(define-key minibuffer-local-ns-map [?\?] 'self-insert-and-exit)
152
153 (define-key minibuffer-local-completion-map "\t" 'minibuffer-complete)
154 (define-key minibuffer-local-completion-map " " 'minibuffer-complete-word)
155 (define-key minibuffer-local-completion-map "?" 'minibuffer-completion-help)
156 (define-key minibuffer-local-must-match-map "\r" 'minibuffer-complete-and-exit)
157 (define-key minibuffer-local-must-match-map "\n" 'minibuffer-complete-and-exit)
158
159 (define-key minibuffer-local-map "\M-n" 'next-history-element)
160 (define-key minibuffer-local-map "\M-p" 'previous-history-element)
161 (define-key minibuffer-local-map '[next]  "\M-n")
162 (define-key minibuffer-local-map '[prior] "\M-p")
163 (define-key minibuffer-local-map "\M-r" 'previous-matching-history-element)
164 (define-key minibuffer-local-map "\M-s" 'next-matching-history-element)
165 (define-key minibuffer-local-must-match-map [next]
166   'next-complete-history-element)
167 (define-key minibuffer-local-must-match-map [prior]
168   'previous-complete-history-element)
169
170 ;; This is an experiment--make up and down arrows do history.
171 (define-key minibuffer-local-map [up] 'previous-history-element)
172 (define-key minibuffer-local-map [down] 'next-history-element)
173 (define-key minibuffer-local-completion-map [up] 'previous-history-element)
174 (define-key minibuffer-local-completion-map [down] 'next-history-element)
175 (define-key minibuffer-local-must-match-map [up] 'previous-history-element)
176 (define-key minibuffer-local-must-match-map [down] 'next-history-element)
177
178 (defvar read-expression-map (let ((map (make-sparse-keymap
179                                         'read-expression-map)))
180                               (set-keymap-parents map
181                                                   (list minibuffer-local-map))
182                               (define-key map "\M-\t" 'lisp-complete-symbol)
183                               map)
184   "Minibuffer keymap used for reading Lisp expressions.")
185
186 (defvar read-shell-command-map
187   (let ((map (make-sparse-keymap 'read-shell-command-map)))
188     (set-keymap-parents map (list minibuffer-local-map))
189     (define-key map "\t" 'comint-dynamic-complete)
190     (define-key map "\M-\t" 'comint-dynamic-complete)
191     (define-key map "\M-?" 'comint-dynamic-list-completions)
192     map)
193   "Minibuffer keymap used by shell-command and related commands.")
194
195 (defcustom use-dialog-box t
196   "*Variable controlling usage of the dialog box.
197 If nil, the dialog box will never be used, even in response to mouse events."
198   :type 'boolean
199   :group 'minibuffer)
200 \f
201 (defcustom minibuffer-electric-file-name-behavior t
202   "*If non-nil, slash and tilde in certain places cause immediate deletion.
203 These are the same places where this behavior would occur later on anyway,
204 in `substitute-in-file-name'."
205   :type 'boolean
206   :group 'minibuffer)
207
208 ;; originally by Stig@hackvan.com
209 (defun minibuffer-electric-separator ()
210   (interactive)
211   (let ((c last-command-char))
212     (and minibuffer-electric-file-name-behavior
213          (eq c directory-sep-char)
214          (eq c (char-before (point)))
215          (not (save-excursion
216               (goto-char (point-min))
217               (and (looking-at "/.+:~?[^/]*/.+")
218                    (re-search-forward "^/.+:~?[^/]*" nil t)
219                    (progn
220                      (delete-region (point) (point-max))
221                      t))))
222          (not (save-excursion
223                 (goto-char (point-min))
224                 (and (looking-at ".+://[^/]*/.+")
225                      (re-search-forward "^.+:/" nil t)
226                      (progn
227                        (delete-region (point) (point-max))
228                        t))))
229          ;; permit `//hostname/path/to/file'
230          (not (eq (point) (1+ (point-min))))
231          ;; permit `http://url/goes/here'
232          (or (not (eq ?: (char-after (- (point) 2))))
233              (eq ?/ (char-after (point-min))))
234        (delete-region (point-min) (point)))
235     (insert c)))
236
237 (defun minibuffer-electric-tilde ()
238   (interactive)
239   (and minibuffer-electric-file-name-behavior
240        (eq directory-sep-char (char-before (point)))
241        ;; permit URL's with //, for e.g. http://hostname/~user
242        (not (save-excursion (search-backward "//" nil t)))
243        (delete-region (point-min) (point)))
244   (insert ?~))
245
246 (defvar read-file-name-map
247   (let ((map (make-sparse-keymap 'read-file-name-map)))
248     (set-keymap-parents map (list minibuffer-local-completion-map))
249     (define-key map (vector directory-sep-char) 'minibuffer-electric-separator)
250     (define-key map "~" 'minibuffer-electric-tilde)
251     map
252     ))
253
254 (defvar read-file-name-must-match-map
255   (let ((map (make-sparse-keymap 'read-file-name-map)))
256     (set-keymap-parents map (list minibuffer-local-must-match-map))
257     (define-key map (vector directory-sep-char) 'minibuffer-electric-separator)
258     (define-key map "~" 'minibuffer-electric-tilde)
259     map
260     ))
261 \f
262 (defun minibuffer-keyboard-quit ()
263   "Abort recursive edit.
264 If `zmacs-regions' is true, and the zmacs region is active in this buffer,
265 then this key deactivates the region without beeping."
266   (interactive)
267   (if (and (region-active-p)
268            (eq (current-buffer) (zmacs-region-buffer)))
269       ;; pseudo-zmacs compatibility: don't beep if this ^G is simply
270       ;; deactivating the region.  If it is inactive, beep.
271       nil
272     (abort-recursive-edit)))
273 \f
274 ;;;; Guts of minibuffer invocation
275
276 ;;#### The only things remaining in C are
277 ;; "Vminibuf_prompt" and the display junk
278 ;;  "minibuf_prompt_width" and "minibuf_prompt_pix_width"
279 ;; Also "active_frame", though I suspect I could already
280 ;;   hack that in Lisp if I could make any sense of the
281 ;;   complete mess of frame/frame code in XEmacs.
282 ;; Vminibuf_prompt could easily be made Lisp-bindable.
283 ;;  I suspect that minibuf_prompt*_width are actually recomputed
284 ;;  by redisplay as needed -- or could be arranged to be so --
285 ;;  and that there could be need for read-minibuffer-internal to
286 ;;  save and restore them.
287 ;;#### The only other thing which read-from-minibuffer-internal does
288 ;;  which we can't presently do in Lisp is move the frame cursor
289 ;;  to the start of the minibuffer line as it returns.  This is
290 ;;  a rather nice touch and should be preserved -- probably by
291 ;;  providing some Lisp-level mechanism (extension to cursor-in-echo-area ?)
292 ;;  to effect it.
293
294
295 ;; Like reset_buffer in FSF's buffer.c
296 ;;  (Except that kill-all-local-variables doesn't nuke 'permanent-local
297 ;;   variables -- we preserve them, reset_buffer doesn't.)
298 (defun reset-buffer (buffer)
299   (with-current-buffer buffer
300     ;(if (fboundp 'unlock-buffer) (unlock-buffer))
301     (kill-all-local-variables)
302     (setq buffer-read-only nil)
303     ;; don't let read only text yanked into the minibuffer
304     ;; permanently wedge it.
305     (make-local-variable 'inhibit-read-only)
306     (setq inhibit-read-only t)
307     (erase-buffer)
308     ;(setq default-directory nil)
309     (setq buffer-file-name nil)
310     (setq buffer-file-truename nil)
311     (set-buffer-modified-p nil)
312     (setq buffer-backed-up nil)
313     (setq buffer-auto-save-file-name nil)
314     (set-buffer-dedicated-frame buffer nil)
315     buffer))
316
317 (defvar minibuffer-history-variable 'minibuffer-history
318   "History list symbol to add minibuffer values to.
319 Each minibuffer output is added with
320   (set minibuffer-history-variable
321        (cons STRING (symbol-value minibuffer-history-variable)))")
322 (defvar minibuffer-history-position)
323
324 ;; Added by hniksic:
325 (defvar initial-minibuffer-history-position)
326 (defvar current-minibuffer-contents)
327 (defvar current-minibuffer-point)
328
329 (defcustom minibuffer-history-minimum-string-length nil
330   "*If this variable is non-nil, a string will not be added to the
331 minibuffer history if its length is less than that value."
332   :type '(choice (const :tag "Any" nil)
333                  integer)
334   :group 'minibuffer)
335
336 (define-error 'input-error "Keyboard input error")
337
338 (put 'input-error 'display-error
339      #'(lambda (error-object stream)
340          (princ (cadr error-object) stream)))
341
342 (defun read-from-minibuffer (prompt &optional initial-contents
343                                     keymap
344                                     readp
345                                     history
346                                     abbrev-table)
347   "Read a string from the minibuffer, prompting with string PROMPT.
348 If optional second arg INITIAL-CONTENTS is non-nil, it is a string
349   to be inserted into the minibuffer before reading input.
350   If INITIAL-CONTENTS is (STRING . POSITION), the initial input
351   is STRING, but point is placed POSITION characters into the string.
352 Third arg KEYMAP is a keymap to use whilst reading;
353   if omitted or nil, the default is `minibuffer-local-map'.
354 If fourth arg READ is non-nil, then interpret the result as a lisp object
355   and return that object:
356   in other words, do `(car (read-from-string INPUT-STRING))'
357 Fifth arg HISTORY, if non-nil, specifies a history list
358   and optionally the initial position in the list.
359   It can be a symbol, which is the history list variable to use,
360   or it can be a cons cell (HISTVAR . HISTPOS).
361   In that case, HISTVAR is the history list variable to use,
362   and HISTPOS is the initial position (the position in the list
363   which INITIAL-CONTENTS corresponds to).
364   If HISTORY is `t', no history will be recorded.
365   Positions are counted starting from 1 at the beginning of the list.
366 Sixth arg ABBREV-TABLE, if non-nil, becomes the value of `local-abbrev-table'
367   in the minibuffer.
368
369 See also the variable completion-highlight-first-word-only for control over
370   completion display."
371   (if (and (not enable-recursive-minibuffers)
372            (> (minibuffer-depth) 0)
373            (eq (selected-window) (minibuffer-window)))
374       (error "Command attempted to use minibuffer while in minibuffer"))
375
376   (if (and minibuffer-max-depth
377            (> minibuffer-max-depth 0)
378            (>= (minibuffer-depth) minibuffer-max-depth))
379       (minibuffer-max-depth-exceeded))
380
381   ;; catch this error before the poor user has typed something...
382   (if history
383       (if (symbolp history)
384           (or (boundp history)
385               (error "History list %S is unbound" history))
386         (or (boundp (car history))
387             (error "History list %S is unbound" (car history)))))
388
389   (if (noninteractive)
390       (progn
391         ;; XEmacs in -batch mode calls minibuffer: print the prompt.
392         (message "%s" (gettext prompt))
393         ;;#### force-output
394
395         ;;#### Should this even be falling though to the code below?
396         ;;#### How does this stuff work now, anyway?
397         ))
398   (let* ((dir default-directory)
399          (owindow (selected-window))
400          (oframe (selected-frame))
401          (window (minibuffer-window))
402          (buffer (if (eq (minibuffer-depth) 0)
403                      (window-buffer window)
404                    (get-buffer-create (format " *Minibuf-%d"
405                                               (minibuffer-depth)))))
406          (frame (window-frame window))
407          (mconfig (if (eq frame (selected-frame))
408                       nil (current-window-configuration frame)))
409          (oconfig (current-window-configuration))
410          ;; dynamic scope sucks sucks sucks sucks sucks sucks.
411          ;; `M-x doctor' makes history a local variable, and thus
412          ;; our binding above is buffer-local and doesn't apply
413          ;; once we switch buffers!!!!  We demand better scope!
414          (_history_ history))
415     (unwind-protect
416          (progn
417            (set-buffer (reset-buffer buffer))
418            (setq default-directory dir)
419            (make-local-variable 'print-escape-newlines)
420            (setq print-escape-newlines t)
421            (make-local-variable 'current-minibuffer-contents)
422            (make-local-variable 'current-minibuffer-point)
423            (make-local-variable 'initial-minibuffer-history-position)
424            (setq current-minibuffer-contents ""
425                  current-minibuffer-point 1)
426            (if (not minibuffer-smart-completion-tracking-behavior)
427                nil
428              (make-local-variable 'mode-motion-hook)
429              (or mode-motion-hook
430                  ;;####disgusting
431                  (setq mode-motion-hook 'minibuffer-smart-mouse-tracker))
432              (make-local-variable 'mouse-track-click-hook)
433              (add-hook 'mouse-track-click-hook
434                        'minibuffer-smart-maybe-select-highlighted-completion))
435            (set-window-buffer window buffer)
436            (select-window window)
437            (set-window-hscroll window 0)
438            (buffer-enable-undo buffer)
439            (message nil)
440            (if initial-contents
441                (if (consp initial-contents)
442                    (progn
443                      (insert (car initial-contents))
444                      (goto-char (1+ (cdr initial-contents)))
445                      (setq current-minibuffer-contents (car initial-contents)
446                            current-minibuffer-point (cdr initial-contents)))
447                  (insert initial-contents)
448                  (setq current-minibuffer-contents initial-contents
449                        current-minibuffer-point (point))))
450            (use-local-map (or keymap minibuffer-local-map))
451            (let ((mouse-grabbed-buffer
452                   (and minibuffer-smart-completion-tracking-behavior
453                        (current-buffer)))
454                  (current-prefix-arg current-prefix-arg)
455                  (help-form minibuffer-help-form)
456                  (minibuffer-history-variable (cond ((not _history_)
457                                                      'minibuffer-history)
458                                                     ((consp _history_)
459                                                      (car _history_))
460                                                     (t
461                                                      _history_)))
462                  (minibuffer-history-position (cond ((consp _history_)
463                                                      (cdr _history_))
464                                                     (t
465                                                      0)))
466                  (minibuffer-scroll-window owindow))
467              (setq initial-minibuffer-history-position
468                    minibuffer-history-position)
469              (if abbrev-table
470                  (setq local-abbrev-table abbrev-table
471                        abbrev-mode t))
472              ;; This is now run from read-minibuffer-internal
473              ;(if minibuffer-setup-hook
474              ;    (run-hooks 'minibuffer-setup-hook))
475              ;(message nil)
476              (if (eq 't
477                      (catch 'exit
478                        (if (> (recursion-depth) (minibuffer-depth))
479                            (let ((standard-output t)
480                                  (standard-input t))
481                              (read-minibuffer-internal prompt))
482                            (read-minibuffer-internal prompt))))
483                  ;; Translate an "abort" (throw 'exit 't)
484                  ;;  into a real quit
485                  (signal 'quit '())
486                ;; return value
487                (let* ((val (progn (set-buffer buffer)
488                                   (if minibuffer-exit-hook
489                                       (run-hooks 'minibuffer-exit-hook))
490                                   (buffer-string)))
491                     (histval val)
492                       (err nil))
493                  (if readp
494                      (condition-case e
495                          (let ((v (read-from-string val)))
496                            (if (< (cdr v) (length val))
497                                (save-match-data
498                                  (or (string-match "[ \t\n]*\\'" val (cdr v))
499                                      (error "Trailing garbage following expression"))))
500                            (setq v (car v))
501                            ;; total total kludge
502                            (if (stringp v) (setq v (list 'quote v)))
503                            (setq val v))
504                        (end-of-file
505                         (setq err
506                               '(input-error "End of input before end of expression")))
507                        (error (setq err e))))
508                  ;; Add the value to the appropriate history list unless
509                  ;; it's already the most recent element, or it's only
510                  ;; two characters long.
511                  (if (and (symbolp minibuffer-history-variable)
512                           (boundp minibuffer-history-variable))
513                      (let ((list (symbol-value minibuffer-history-variable)))
514                        (or (eq list t)
515                            (null val)
516                            (and list (equal histval (car list)))
517                            (and (stringp val)
518                                 minibuffer-history-minimum-string-length
519                                 (< (length val)
520                                    minibuffer-history-minimum-string-length))
521                            (set minibuffer-history-variable
522                                 (if minibuffer-history-uniquify
523                                     (cons histval (remove histval list))
524                                   (cons histval list))))))
525                  (if err (signal (car err) (cdr err)))
526                  val))))
527       ;; stupid display code requires this for some reason
528       (set-buffer buffer)
529       (buffer-disable-undo buffer)
530       (setq buffer-read-only nil)
531       (erase-buffer)
532
533       ;; restore frame configurations
534       (if (and mconfig (frame-live-p oframe)
535                (eq frame (selected-frame)))
536           ;; if we changed frames (due to surrogate minibuffer),
537           ;; and we're still on the new frame, go back to the old one.
538           (select-frame oframe))
539       (if mconfig (set-window-configuration mconfig))
540       (set-window-configuration oconfig))))
541
542
543 (defun minibuffer-max-depth-exceeded ()
544   ;;
545   ;; This signals an error if an Nth minibuffer is invoked while N-1 are
546   ;; already active, whether the minibuffer window is selected or not.
547   ;; Since, under X, it's easy to jump out of the minibuffer (by doing M-x,
548   ;; getting distracted, and clicking elsewhere) many many novice users have
549   ;; had the problem of having multiple minibuffers build up, even to the
550   ;; point of exceeding max-lisp-eval-depth.  Since the variable
551   ;; enable-recursive-minibuffers historically/crockishly is only consulted
552   ;; when the minibuffer is currently active (like typing M-x M-x) it doesn't
553   ;; help in this situation.
554   ;;
555   ;; This routine also offers to edit .emacs for you to get rid of this
556   ;; complaint, like `disabled' commands do, since it's likely that non-novice
557   ;; users will be annoyed by this change, so we give them an easy way to get
558   ;; rid of it forever.
559   ;;
560   (beep t 'minibuffer-limit-exceeded)
561   (message
562    "Minibuffer already active: abort it with `^]', enable new one with `n': ")
563   (let ((char (let ((cursor-in-echo-area t)) ; #### doesn't always work??
564                 (read-char))))
565     (cond
566      ((eq char ?n)
567       (cond
568        ((y-or-n-p "Enable recursive minibuffers for other sessions too? ")
569         ;; This is completely disgusting, but it's basically what novice.el
570         ;; does.  This kind of thing should be generalized.
571         (setq minibuffer-max-depth nil)
572         (save-excursion
573           (set-buffer
574            (find-file-noselect
575             (substitute-in-file-name custom-file)))
576           (goto-char (point-min))
577           (if (re-search-forward
578                "^(setq minibuffer-max-depth \\([0-9]+\\|'?nil\\|'?()\\))\n"
579                nil t)
580               (delete-region (match-beginning 0 ) (match-end 0))
581             ;; Must have been disabled by default.
582             (goto-char (point-max)))
583           (insert"\n(setq minibuffer-max-depth nil)\n")
584           (save-buffer))
585         (message "Multiple minibuffers enabled")
586         (sit-for 1))))
587      ((eq char ?\1d)
588       (abort-recursive-edit))
589      (t
590       (error "Minibuffer already active")))))
591
592 \f
593 ;;;; Guts of minibuffer completion
594
595
596 ;; Used by minibuffer-do-completion
597 (defvar last-exact-completion)
598
599 (defun temp-minibuffer-message (m)
600   (let ((savemax (point-max)))
601     (save-excursion
602       (goto-char (point-max))
603       (message nil)
604       (insert m))
605     (let ((inhibit-quit t))
606       (sit-for 2)
607       (delete-region savemax (point-max))
608       ;;  If the user types a ^G while we're in sit-for, then quit-flag
609       ;;  gets set. In this case, we want that ^G to be interpreted
610       ;;  as a normal character, and act just like typeahead.
611       (if (and quit-flag (not unread-command-event))
612           (setq unread-command-event (character-to-event (quit-char))
613                 quit-flag nil)))))
614
615
616 ;; Determines whether buffer-string is an exact completion
617 (defun exact-minibuffer-completion-p (buffer-string)
618   (cond ((not minibuffer-completion-table)
619          ;; Empty alist
620          nil)
621         ((vectorp minibuffer-completion-table)
622          (let ((tem (intern-soft buffer-string
623                                  minibuffer-completion-table)))
624            (if (or tem
625                    (and (string-equal buffer-string "nil")
626                         ;; intern-soft loses for 'nil
627                         (catch 'found
628                           (mapatoms #'(lambda (s)
629                                         (if (string-equal
630                                              (symbol-name s)
631                                              buffer-string)
632                                             (throw 'found t)))
633                                     minibuffer-completion-table)
634                           nil)))
635                (if minibuffer-completion-predicate
636                    (funcall minibuffer-completion-predicate
637                             tem)
638                    t)
639                nil)))
640         ((and (consp minibuffer-completion-table)
641               ;;#### Emacs-Lisp truly sucks!
642               ;; lambda, autoload, etc
643               (not (symbolp (car minibuffer-completion-table))))
644          (if (not completion-ignore-case)
645              (assoc buffer-string minibuffer-completion-table)
646              (let ((s (upcase buffer-string))
647                    (tail minibuffer-completion-table)
648                    tem)
649                (while tail
650                  (setq tem (car (car tail)))
651                  (if (or (equal tem buffer-string)
652                          (equal tem s)
653                         (if tem (equal (upcase tem) s)))
654                      (setq s 'win
655                            tail nil)    ;exit
656                      (setq tail (cdr tail))))
657                (eq s 'win))))
658         (t
659          (funcall minibuffer-completion-table
660                   buffer-string
661                   minibuffer-completion-predicate
662                   'lambda)))
663   )
664
665 ;; 0 'none                 no possible completion
666 ;; 1 'unique               was already an exact and unique completion
667 ;; 3 'exact                was already an exact (but nonunique) completion
668 ;; NOT USED 'completed-exact-unique completed to an exact and completion
669 ;; 4 'completed-exact      completed to an exact (but nonunique) completion
670 ;; 5 'completed            some completion happened
671 ;; 6 'uncompleted          no completion happened
672 (defun minibuffer-do-completion-1 (buffer-string completion)
673   (cond ((not completion)
674          'none)
675         ((eq completion t)
676          ;; exact and unique match
677          'unique)
678         (t
679          ;; It did find a match.  Do we match some possibility exactly now?
680          (let ((completedp (not (string-equal completion buffer-string))))
681            (if completedp
682                (progn
683                  ;; Some completion happened
684                  (erase-buffer)
685                  (insert completion)
686                  (setq buffer-string completion)))
687            (if (exact-minibuffer-completion-p buffer-string)
688                ;; An exact completion was possible
689                (if completedp
690 ;; Since no callers need to know the difference, don't bother
691 ;;  with this (potentially expensive) discrimination.
692 ;;                 (if (eq (try-completion completion
693 ;;                                         minibuffer-completion-table
694 ;;                                         minibuffer-completion-predicate)
695 ;;                         't)
696 ;;                     'completed-exact-unique
697                        'completed-exact
698 ;;                     )
699                    'exact)
700                ;; Not an exact match
701                (if completedp
702                    'completed
703                    'uncompleted))))))
704
705
706 (defun minibuffer-do-completion (buffer-string)
707   (let* ((completion (try-completion buffer-string
708                                      minibuffer-completion-table
709                                      minibuffer-completion-predicate))
710          (status (minibuffer-do-completion-1 buffer-string completion))
711          (last last-exact-completion))
712     (setq last-exact-completion nil)
713     (cond ((eq status 'none)
714            ;; No completions
715            (ding nil 'no-completion)
716            (temp-minibuffer-message " [No match]"))
717           ((eq status 'unique)
718            )
719           (t
720            ;; It did find a match.  Do we match some possibility exactly now?
721            (if (not (string-equal completion buffer-string))
722                (progn
723                  ;; Some completion happened
724                  (erase-buffer)
725                  (insert completion)
726                  (setq buffer-string completion)))
727            (cond ((eq status 'exact)
728                   ;; If the last exact completion and this one were
729                   ;;  the same, it means we've already given a
730                   ;;  "Complete but not unique" message and that the
731                   ;;  user's hit TAB again, so now we give help.
732                   (setq last-exact-completion completion)
733                   (if (equal buffer-string last)
734                       (minibuffer-completion-help)))
735                  ((eq status 'uncompleted)
736                   (if completion-auto-help
737                       (minibuffer-completion-help)
738                       (temp-minibuffer-message " [Next char not unique]")))
739                  (t
740                   nil))))
741     status))
742
743 \f
744 ;;;; completing-read
745
746 (defun completing-read (prompt table
747                         &optional predicate require-match
748                                   initial-contents history)
749   "Read a string in the minibuffer, with completion.
750 Args: PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-CONTENTS, HISTORY.
751 PROMPT is a string to prompt with; normally it ends in a colon and a space.
752 TABLE is an alist whose elements' cars are strings, or an obarray.
753 PREDICATE limits completion to a subset of TABLE.
754 See `try-completion' for more details on completion, TABLE, and PREDICATE.
755 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
756  the input is (or completes to) an element of TABLE or is null.
757  If it is also not t, Return does not exit if it does non-null completion.
758 If INITIAL-CONTENTS is non-nil, insert it in the minibuffer initially.
759   If it is (STRING . POSITION), the initial input
760   is STRING, but point is placed POSITION characters into the string.
761 HISTORY, if non-nil, specifies a history list
762   and optionally the initial position in the list.
763   It can be a symbol, which is the history list variable to use,
764   or it can be a cons cell (HISTVAR . HISTPOS).
765   In that case, HISTVAR is the history list variable to use,
766   and HISTPOS is the initial position (the position in the list
767   which INITIAL-CONTENTS corresponds to).
768   If HISTORY is `t', no history will be recorded.
769   Positions are counted starting from 1 at the beginning of the list.
770 Completion ignores case if the ambient value of
771   `completion-ignore-case' is non-nil."
772   (let ((minibuffer-completion-table table)
773         (minibuffer-completion-predicate predicate)
774         (minibuffer-completion-confirm (if (eq require-match 't) nil t))
775         (last-exact-completion nil))
776     (read-from-minibuffer prompt
777                           initial-contents
778                           (if (not require-match)
779                               minibuffer-local-completion-map
780                               minibuffer-local-must-match-map)
781                           nil
782                           history)))
783
784 \f
785 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
786 ;;;;                   Minibuffer completion commands                   ;;;;
787 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
788
789
790 (defun minibuffer-complete ()
791   "Complete the minibuffer contents as far as possible.
792 Return nil if there is no valid completion, else t.
793 If no characters can be completed, display a list of possible completions.
794 If you repeat this command after it displayed such a list,
795 scroll the window of possible completions."
796   (interactive)
797   ;; If the previous command was not this, then mark the completion
798   ;;  buffer obsolete.
799   (or (eq last-command this-command)
800       (setq minibuffer-scroll-window nil))
801   (let ((window minibuffer-scroll-window))
802     (if (and window (windowp window) (window-buffer window)
803              (buffer-name (window-buffer window)))
804         ;; If there's a fresh completion window with a live buffer
805         ;;  and this command is repeated, scroll that window.
806         (let ((obuf (current-buffer)))
807           (unwind-protect
808               (progn
809                 (set-buffer (window-buffer window))
810                 (if (pos-visible-in-window-p (point-max) window)
811                     ;; If end is in view, scroll up to the beginning.
812                     (set-window-start window (point-min))
813                   ;; Else scroll down one frame.
814                   (scroll-other-window)))
815             (set-buffer obuf))
816           nil)
817       (let ((status (minibuffer-do-completion (buffer-string))))
818         (if (eq status 'none)
819             nil
820           (progn
821             (cond ((eq status 'unique)
822                    (temp-minibuffer-message
823                     " [Sole completion]"))
824                   ((eq status 'exact)
825                    (temp-minibuffer-message
826                     " [Complete, but not unique]")))
827             t))))))
828
829
830 (defun minibuffer-complete-and-exit ()
831   "Complete the minibuffer contents, and maybe exit.
832 Exit if the name is valid with no completion needed.
833 If name was completed to a valid match,
834 a repetition of this command will exit."
835   (interactive)
836   (if (= (point-min) (point-max))
837       ;; Crockishly allow user to specify null string
838       (throw 'exit nil))
839   (let ((buffer-string (buffer-string)))
840     ;; Short-cut -- don't call minibuffer-do-completion if we already
841     ;;  have an (possibly nonunique) exact completion.
842     (if (exact-minibuffer-completion-p buffer-string)
843         (throw 'exit nil))
844     (let ((status (minibuffer-do-completion buffer-string)))
845       (if (or (eq status 'unique)
846               (eq status 'exact)
847               (if (or (eq status 'completed-exact)
848                       (eq status 'completed-exact-unique))
849                   (if minibuffer-completion-confirm
850                       (progn (temp-minibuffer-message " [Confirm]")
851                              nil)
852                       t)))
853           (throw 'exit nil)))))
854
855
856 (defun self-insert-and-exit ()
857   "Terminate minibuffer input."
858   (interactive)
859   (self-insert-command 1)
860   (throw 'exit nil))
861
862 (defun exit-minibuffer ()
863   "Terminate this minibuffer argument.
864 If minibuffer-confirm-incomplete is true, and we are in a completing-read
865 of some kind, and the contents of the minibuffer is not an existing
866 completion, requires an additional RET before the minibuffer will be exited
867 \(assuming that RET was the character that invoked this command:
868 the character in question must be typed again)."
869   (interactive)
870   (if (not minibuffer-confirm-incomplete)
871       (throw 'exit nil))
872   (let ((buffer-string (buffer-string)))
873     (if (exact-minibuffer-completion-p buffer-string)
874         (throw 'exit nil))
875     (let ((completion (if (not minibuffer-completion-table)
876                           t
877                           (try-completion buffer-string
878                                           minibuffer-completion-table
879                                           minibuffer-completion-predicate))))
880       (if (or (eq completion 't)
881               ;; Crockishly allow user to specify null string
882               (string-equal buffer-string ""))
883           (throw 'exit nil))
884       (if completion ;; rewritten for I18N3 snarfing
885           (temp-minibuffer-message " [incomplete; confirm]")
886         (temp-minibuffer-message " [no completions; confirm]"))
887       (let ((event (let ((inhibit-quit t))
888                      (prog1
889                          (next-command-event)
890                        (setq quit-flag nil)))))
891         (cond ((equal event last-command-event)
892                (throw 'exit nil))
893               ((equal (quit-char) (event-to-character event))
894                ;; Minibuffer abort.
895                (throw 'exit t)))
896         (dispatch-event event)))))
897 \f
898 ;;;; minibuffer-complete-word
899
900
901 ;;;#### I think I have done this correctly; it certainly is simpler
902 ;;;#### than what the C code seemed to be trying to do.
903 (defun minibuffer-complete-word ()
904   "Complete the minibuffer contents at most a single word.
905 After one word is completed as much as possible, a space or hyphen
906 is added, provided that matches some possible completion.
907 Return nil if there is no valid completion, else t."
908   (interactive)
909   (let* ((buffer-string (buffer-string))
910          (completion (try-completion buffer-string
911                                      minibuffer-completion-table
912                                      minibuffer-completion-predicate))
913          (status (minibuffer-do-completion-1 buffer-string completion)))
914     (cond ((eq status 'none)
915            (ding nil 'no-completion)
916            (temp-minibuffer-message " [No match]")
917            nil)
918           ((eq status 'unique)
919            ;; New message, only in this new Lisp code
920            (temp-minibuffer-message " [Sole completion]")
921            t)
922           (t
923            (cond ((or (eq status 'uncompleted)
924                       (eq status 'exact))
925                   (let ((foo #'(lambda (s)
926                                  (condition-case nil
927                                      (if (try-completion
928                                           (concat buffer-string s)
929                                           minibuffer-completion-table
930                                           minibuffer-completion-predicate)
931                                          (progn
932                                            (goto-char (point-max))
933                                            (insert s)
934                                            t)
935                                        nil)
936                                    (error nil))))
937                         (char last-command-char))
938                     ;; Try to complete by adding a word-delimiter
939                     (or (and (characterp char) (> char 0)
940                              (funcall foo (char-to-string char)))
941                         (and (not (eq char ?\ ))
942                              (funcall foo " "))
943                         (and (not (eq char ?\-))
944                              (funcall foo "-"))
945                         (progn
946                           (if completion-auto-help
947                               (minibuffer-completion-help)
948                               ;; New message, only in this new Lisp code
949                             ;; rewritten for I18N3 snarfing
950                             (if (eq status 'exact)
951                                 (temp-minibuffer-message
952                                  " [Complete, but not unique]")
953                               (temp-minibuffer-message " [Ambiguous]")))
954                           nil))))
955                  (t
956                   (erase-buffer)
957                   (insert completion)
958                   ;; First word-break in stuff found by completion
959                   (goto-char (point-min))
960                   (let ((len (length buffer-string))
961                         n)
962                     (if (and (< len (length completion))
963                              (catch 'match
964                                (setq n 0)
965                                (while (< n len)
966                                  (if (char-equal
967                                        (upcase (aref buffer-string n))
968                                        (upcase (aref completion n)))
969                                      (setq n (1+ n))
970                                      (throw 'match nil)))
971                                t)
972                              (progn
973                                (goto-char (point-min))
974                                (forward-char len)
975                                (re-search-forward "\\W" nil t)))
976                         (delete-region (point) (point-max))
977                         (goto-char (point-max))))
978                   t))))))
979 \f
980 \f
981 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
982 ;;;;                      "Smart minibuffer" hackery                    ;;;;
983 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
984
985 ;;; ("Kludgy minibuffer hackery" is perhaps a better name)
986
987 ;; This works by setting `mouse-grabbed-buffer' to the minibuffer,
988 ;; defining button2 in the minibuffer keymap to
989 ;; `minibuffer-smart-select-highlighted-completion', and setting the
990 ;; mode-motion-hook of the minibuffer to `minibuffer-mouse-tracker'.
991 ;; By setting `mouse-grabbed-buffer', the minibuffer's keymap and
992 ;; mode-motion-hook apply (for mouse motion and presses) no matter
993 ;; what buffer the mouse is over.  Then, `minibuffer-mouse-tracker'
994 ;; examines the text under the mouse looking for something that looks
995 ;; like a completion, and causes it to be highlighted, and
996 ;; `minibuffer-smart-select-highlighted-completion' looks for a
997 ;; flagged completion under the mouse and inserts it.  This has the
998 ;; following advantages:
999 ;;
1000 ;; -- filenames and such in any buffer can be inserted by clicking,
1001 ;;    not just completions
1002 ;;
1003 ;; but the following disadvantages:
1004 ;;
1005 ;; -- unless you're aware of the "filename in any buffer" feature,
1006 ;;    the fact that strings in arbitrary buffers get highlighted appears
1007 ;;    as a bug
1008 ;; -- mouse motion can cause ange-ftp actions -- bad bad bad.
1009 ;;
1010 ;; There's some hackery in minibuffer-mouse-tracker to try to avoid the
1011 ;; ange-ftp stuff, but it doesn't work.
1012 ;;
1013
1014 (defcustom minibuffer-smart-completion-tracking-behavior nil
1015   "*If non-nil, look for completions under mouse in all buffers.
1016 This allows you to click on something that looks like a completion
1017 and have it selected, regardless of what buffer it is in.
1018
1019 This is not enabled by default because
1020
1021 -- The \"mysterious\" highlighting in normal buffers is confusing to
1022    people not expecting it, and looks like a bug
1023 -- If ange-ftp is enabled, this tracking sometimes causes ange-ftp
1024    action as a result of mouse motion, which is *bad bad bad*.
1025    Hopefully this bug will be fixed at some point."
1026   :type 'boolean
1027   :group 'minibuffer)
1028
1029 (defun minibuffer-smart-mouse-tracker (event)
1030   ;; Used as the mode-motion-hook of the minibuffer window, which is the
1031   ;; value of `mouse-grabbed-buffer' while the minibuffer is active.  If
1032   ;; the word under the mouse is a valid minibuffer completion, then it
1033   ;; is highlighted.
1034   ;;
1035   ;; We do some special voodoo when we're reading a pathname, because
1036   ;; the way filename completion works is funny.  Possibly there's some
1037   ;; more general way this could be dealt with...
1038   ;;
1039   ;; We do some further voodoo when reading a pathname that is an
1040   ;; ange-ftp or efs path, because causing FTP activity as a result of
1041   ;; mouse motion is a really bad time.
1042   ;;
1043   (and minibuffer-smart-completion-tracking-behavior
1044        (event-point event)
1045        ;; avoid conflict with display-completion-list extents
1046        (not (extent-at (event-point event)
1047                        (event-buffer event)
1048                        'list-mode-item))
1049        (let ((filename-kludge-p (eq minibuffer-completion-table
1050                                     'read-file-name-internal)))
1051          (mode-motion-highlight-internal
1052           event
1053           #'(lambda () (default-mouse-track-beginning-of-word
1054                          (if filename-kludge-p 'nonwhite t)))
1055           #'(lambda ()
1056               (let ((p (point))
1057                     (string ""))
1058                 (default-mouse-track-end-of-word
1059                   (if filename-kludge-p 'nonwhite t))
1060                 (if (and (/= p (point)) minibuffer-completion-table)
1061                     (setq string (buffer-substring p (point))))
1062                 (if (string-match "\\`[ \t\n]*\\'" string)
1063                     (goto-char p)
1064                   (if filename-kludge-p
1065                       (setq string (minibuffer-smart-select-kludge-filename
1066                                     string)))
1067                   ;; try-completion bogusly returns a string even when
1068                   ;; that string is complete if that string is also a
1069                   ;; prefix for other completions.  This means that we
1070                   ;; can't just do the obvious thing, (eq t
1071                   ;; (try-completion ...)).
1072                   (let (comp)
1073                     (if (and filename-kludge-p
1074                              ;; #### evil evil evil evil
1075                              (or (and (fboundp 'ange-ftp-ftp-path)
1076                                       (ange-ftp-ftp-path string))
1077                                  (and (fboundp 'efs-ftp-path)
1078                                       (efs-ftp-path string))))
1079                         (setq comp t)
1080                       (setq comp
1081                             (try-completion string
1082                                             minibuffer-completion-table
1083                                             minibuffer-completion-predicate)))
1084                     (or (eq comp t)
1085                         (and (equal comp string)
1086                              (or (null minibuffer-completion-predicate)
1087                                  (stringp
1088                                   minibuffer-completion-predicate) ; ???
1089                                  (funcall minibuffer-completion-predicate
1090                                           (if (vectorp
1091                                                minibuffer-completion-table)
1092                                               (intern-soft
1093                                                string
1094                                                minibuffer-completion-table)
1095                                             string))))
1096                         (goto-char p))))))))))
1097
1098 (defun minibuffer-smart-select-kludge-filename (string)
1099   (save-excursion
1100     (set-buffer mouse-grabbed-buffer) ; the minibuf
1101     (let ((kludge-string (concat (buffer-string) string)))
1102       (if (or (and (fboundp 'ange-ftp-ftp-path)
1103                    (ange-ftp-ftp-path kludge-string))
1104                (and (fboundp 'efs-ftp-path) (efs-ftp-path kludge-string)))
1105            ;; #### evil evil evil, but more so.
1106            string
1107          (append-expand-filename (buffer-string) string)))))
1108
1109 (defun minibuffer-smart-select-highlighted-completion (event)
1110   "Select the highlighted text under the mouse as a minibuffer response.
1111 When the minibuffer is being used to prompt the user for a completion,
1112 any valid completions which are visible on the frame will highlight
1113 when the mouse moves over them.  Clicking \\<minibuffer-local-map>\
1114 \\[minibuffer-smart-select-highlighted-completion] will select the
1115 highlighted completion under the mouse.
1116
1117 If the mouse is clicked while not over a highlighted completion,
1118 then the global binding of \\[minibuffer-smart-select-highlighted-completion] \
1119 will be executed instead.  In this\nway you can get at the normal global \
1120 behavior of \\[minibuffer-smart-select-highlighted-completion] as well as
1121 the special minibuffer behavior."
1122   (interactive "e")
1123   (if minibuffer-smart-completion-tracking-behavior
1124       (minibuffer-smart-select-highlighted-completion-1 event t)
1125     (let ((command (lookup-key global-map
1126                                (vector current-mouse-event))))
1127       (if command (call-interactively command)))))
1128
1129 (defun minibuffer-smart-select-highlighted-completion-1 (event global-p)
1130   (let* ((filename-kludge-p (eq minibuffer-completion-table
1131                                 'read-file-name-internal))
1132          completion
1133          command-p
1134          (evpoint (event-point event))
1135          (evextent (and evpoint (extent-at evpoint (event-buffer event)
1136                                            'list-mode-item))))
1137     (if evextent
1138         ;; avoid conflict with display-completion-list extents.
1139         ;; if we find one, do that behavior instead.
1140         (list-mode-item-selected-1 evextent event)
1141       (save-excursion
1142         (let* ((buffer (window-buffer (event-window event)))
1143                (p (event-point event))
1144                (extent (and p (extent-at p buffer 'mouse-face))))
1145           (set-buffer buffer)
1146           (if (not (and (extent-live-p extent)
1147                         (eq (extent-object extent) (current-buffer))
1148                         (not (extent-detached-p extent))))
1149               (setq command-p t)
1150             ;; ...else user has selected a highlighted completion.
1151             (setq completion
1152                   (buffer-substring (extent-start-position extent)
1153                                     (extent-end-position extent)))
1154             (if filename-kludge-p
1155                 (setq completion (minibuffer-smart-select-kludge-filename
1156                                   completion)))
1157             ;; remove the extent so that it's not hanging around in
1158             ;; *Completions*
1159             (detach-extent extent)
1160             (set-buffer mouse-grabbed-buffer)
1161             (erase-buffer)
1162             (insert completion))))
1163       ;; we need to execute the command or do the throw outside of the
1164       ;; save-excursion.
1165       (cond ((and command-p global-p)
1166              (let ((command (lookup-key global-map
1167                                         (vector current-mouse-event))))
1168                (if command
1169                    (call-interactively command)
1170                  (if minibuffer-completion-table
1171                      (error
1172                       "Highlighted words are valid completions.  You may select one.")
1173                    (error "no completions")))))
1174             ((not command-p)
1175              ;; things get confused if the minibuffer is terminated while
1176              ;; not selected.
1177              (select-window (minibuffer-window))
1178              (if (and filename-kludge-p (file-directory-p completion))
1179                  ;; if the user clicked middle on a directory name, display the
1180                  ;; files in that directory.
1181                  (progn
1182                    (goto-char (point-max))
1183                    (minibuffer-completion-help))
1184                ;; otherwise, terminate input
1185                (throw 'exit nil)))))))
1186
1187 (defun minibuffer-smart-maybe-select-highlighted-completion
1188   (event &optional click-count)
1189   "Like minibuffer-smart-select-highlighted-completion but does nothing if
1190 there is no completion (as opposed to executing the global binding).  Useful
1191 as the value of `mouse-track-click-hook'."
1192   (interactive "e")
1193   (minibuffer-smart-select-highlighted-completion-1 event nil))
1194
1195 (define-key minibuffer-local-map 'button2
1196   'minibuffer-smart-select-highlighted-completion)
1197
1198 \f
1199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1200 ;;;;                         Minibuffer History                         ;;;;
1201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1202
1203 (defvar minibuffer-history '()
1204   "Default minibuffer history list.
1205 This is used for all minibuffer input except when an alternate history
1206 list is specified.")
1207
1208 ;; Some other history lists:
1209 ;;
1210 (defvar minibuffer-history-search-history '())
1211 (defvar function-history '())
1212 (defvar variable-history '())
1213 (defvar buffer-history '())
1214 (defvar shell-command-history '())
1215 (defvar file-name-history '())
1216
1217 (defvar read-expression-history nil)
1218
1219 (defvar minibuffer-history-sexp-flag nil ;weird FSF Emacs kludge
1220   "Non-nil when doing history operations on `command-history'.
1221 More generally, indicates that the history list being acted on
1222 contains expressions rather than strings.")
1223
1224 (defun previous-matching-history-element (regexp n)
1225   "Find the previous history element that matches REGEXP.
1226 \(Previous history elements refer to earlier actions.)
1227 With prefix argument N, search for Nth previous match.
1228 If N is negative, find the next or Nth next match."
1229   (interactive
1230    (let ((enable-recursive-minibuffers t)
1231          (minibuffer-history-sexp-flag nil))
1232      (if (eq 't (symbol-value minibuffer-history-variable))
1233          (error "History is not being recorded in this context"))
1234      (list (read-from-minibuffer "Previous element matching (regexp): "
1235                                  (car minibuffer-history-search-history)
1236                                  minibuffer-local-map
1237                                  nil
1238                                  'minibuffer-history-search-history)
1239            (prefix-numeric-value current-prefix-arg))))
1240   (let ((history (symbol-value minibuffer-history-variable))
1241         prevpos
1242         (pos minibuffer-history-position))
1243     (if (eq history t)
1244         (error "History is not being recorded in this context"))
1245     (while (/= n 0)
1246       (setq prevpos pos)
1247       (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
1248       (if (= pos prevpos)
1249           (if (= pos 1) ;; rewritten for I18N3 snarfing
1250               (error "No later matching history item")
1251             (error "No earlier matching history item")))
1252       (if (string-match regexp
1253                         (if minibuffer-history-sexp-flag
1254                             (let ((print-level nil))
1255                               (prin1-to-string (nth (1- pos) history)))
1256                             (nth (1- pos) history)))
1257           (setq n (+ n (if (< n 0) 1 -1)))))
1258     (setq minibuffer-history-position pos)
1259     (setq current-minibuffer-contents (buffer-string)
1260           current-minibuffer-point (point))
1261     (erase-buffer)
1262     (let ((elt (nth (1- pos) history)))
1263       (insert (if minibuffer-history-sexp-flag
1264                   (let ((print-level nil))
1265                     (prin1-to-string elt))
1266                   elt)))
1267       (goto-char (point-min)))
1268   (if (or (eq (car (car command-history)) 'previous-matching-history-element)
1269           (eq (car (car command-history)) 'next-matching-history-element))
1270       (setq command-history (cdr command-history))))
1271
1272 (defun next-matching-history-element (regexp n)
1273   "Find the next history element that matches REGEXP.
1274 \(The next history element refers to a more recent action.)
1275 With prefix argument N, search for Nth next match.
1276 If N is negative, find the previous or Nth previous match."
1277   (interactive
1278    (let ((enable-recursive-minibuffers t)
1279          (minibuffer-history-sexp-flag nil))
1280      (if (eq t (symbol-value minibuffer-history-variable))
1281          (error "History is not being recorded in this context"))
1282      (list (read-from-minibuffer "Next element matching (regexp): "
1283                                  (car minibuffer-history-search-history)
1284                                  minibuffer-local-map
1285                                  nil
1286                                  'minibuffer-history-search-history)
1287            (prefix-numeric-value current-prefix-arg))))
1288   (previous-matching-history-element regexp (- n)))
1289
1290 (defun next-history-element (n)
1291   "Insert the next element of the minibuffer history into the minibuffer."
1292   (interactive "p")
1293   (if (eq 't (symbol-value minibuffer-history-variable))
1294       (error "History is not being recorded in this context"))
1295   (unless (zerop n)
1296     (when (eq minibuffer-history-position
1297               initial-minibuffer-history-position)
1298       (setq current-minibuffer-contents (buffer-string)
1299             current-minibuffer-point (point)))
1300     (let ((narg (- minibuffer-history-position n))
1301           (minimum (if minibuffer-default -1 0)))
1302       (cond ((< narg minimum)
1303              (error "No following item in %s" minibuffer-history-variable))
1304             ((> narg (length (symbol-value minibuffer-history-variable)))
1305              (error "No preceding item in %s" minibuffer-history-variable)))
1306       (erase-buffer)
1307       (setq minibuffer-history-position narg)
1308       (if (eq narg initial-minibuffer-history-position)
1309           (progn
1310             (insert current-minibuffer-contents)
1311             (goto-char current-minibuffer-point))
1312         (let ((elt (if (>= narg 0)
1313                        (nth (1- minibuffer-history-position)
1314                             (symbol-value minibuffer-history-variable))
1315                      minibuffer-default)))
1316           (insert
1317            (if (not (stringp elt))
1318                (let ((print-level nil))
1319                  (condition-case nil
1320                      (let ((print-readably t)
1321                            (print-escape-newlines t))
1322                        (prin1-to-string elt))
1323                    (error (prin1-to-string elt))))
1324              elt)))
1325         ;; FSF has point-min here.
1326         (goto-char (point-max))))))
1327
1328 (defun previous-history-element (n)
1329   "Insert the previous element of the minibuffer history into the minibuffer."
1330   (interactive "p")
1331   (next-history-element (- n)))
1332
1333 (defun next-complete-history-element (n)
1334   "Get next element of history which is a completion of minibuffer contents."
1335   (interactive "p")
1336   (let ((point-at-start (point)))
1337     (next-matching-history-element
1338      (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)
1339     ;; next-matching-history-element always puts us at (point-min).
1340     ;; Move to the position we were at before changing the buffer contents.
1341     ;; This is still sensical, because the text before point has not changed.
1342     (goto-char point-at-start)))
1343
1344 (defun previous-complete-history-element (n)
1345   "Get previous element of history which is a completion of minibuffer contents."
1346   (interactive "p")
1347   (next-complete-history-element (- n)))
1348
1349 \f
1350 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1351 ;;;;                reading various things from a minibuffer            ;;;;
1352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1353
1354 (defun read-expression (prompt &optional initial-contents history)
1355   "Return a Lisp object read using the minibuffer.
1356 Prompt with PROMPT.  If non-nil, optional second arg INITIAL-CONTENTS
1357 is a string to insert in the minibuffer before reading.
1358 Third arg HISTORY, if non-nil, specifies a history list."
1359   (let ((minibuffer-history-sexp-flag t)
1360         ;; Semi-kludge to get around M-x C-x o M-ESC trying to do completion.
1361         (minibuffer-completion-table nil))
1362     (read-from-minibuffer prompt
1363                           initial-contents
1364                           read-expression-map
1365                           t
1366                           (or history 'read-expression-history)
1367                           lisp-mode-abbrev-table)))
1368
1369 (defun read-string (prompt &optional initial-contents history)
1370   "Return a string from the minibuffer, prompting with string PROMPT.
1371 If non-nil, optional second arg INITIAL-CONTENTS is a string to insert
1372 in the minibuffer before reading.
1373 Third arg HISTORY, if non-nil, specifies a history list."
1374   (let ((minibuffer-completion-table nil))
1375     (read-from-minibuffer prompt
1376                           initial-contents
1377                           minibuffer-local-map
1378                           nil history)))
1379
1380 (defun eval-minibuffer (prompt &optional initial-contents history)
1381   "Return value of Lisp expression read using the minibuffer.
1382 Prompt with PROMPT.  If non-nil, optional second arg INITIAL-CONTENTS
1383 is a string to insert in the minibuffer before reading.
1384 Third arg HISTORY, if non-nil, specifies a history list."
1385   (eval (read-expression prompt initial-contents history)))
1386
1387 ;; The name `command-history' is already taken
1388 (defvar read-command-history '())
1389
1390 (defun read-command (prompt)
1391   "Read the name of a command and return as a symbol.
1392 Prompts with PROMPT."
1393   (intern (completing-read prompt obarray 'commandp t nil
1394                            ;; 'command-history is not right here: that's a
1395                            ;; list of evalable forms, not a history list.
1396                            'read-command-history
1397                            )))
1398
1399 (defun read-function (prompt)
1400   "Read the name of a function and return as a symbol.
1401 Prompts with PROMPT."
1402   (intern (completing-read prompt obarray 'fboundp t nil
1403                            'function-history)))
1404
1405 (defun read-variable (prompt)
1406   "Read the name of a user variable and return it as a symbol.
1407 Prompts with PROMPT.
1408 A user variable is one whose documentation starts with a `*' character."
1409   (intern (completing-read prompt obarray 'user-variable-p t nil
1410                            'variable-history)))
1411
1412 (defun read-buffer (prompt &optional default require-match)
1413   "Read the name of a buffer and return as a string.
1414 Prompts with PROMPT.  Optional second arg DEFAULT is value to return if user
1415 enters an empty line.  If optional third arg REQUIRE-MATCH is non-nil,
1416 only existing buffer names are allowed."
1417   (let ((prompt (if default
1418                     (format "%s(default %s) "
1419                             (gettext prompt) (if (bufferp default)
1420                                                  (buffer-name default)
1421                                                default))
1422                     prompt))
1423         (alist (mapcar #'(lambda (b) (cons (buffer-name b) b))
1424                        (buffer-list)))
1425         result)
1426     (while (progn
1427              (setq result (completing-read prompt alist nil require-match
1428                                            nil 'buffer-history))
1429              (cond ((not (equal result ""))
1430                     nil)
1431                    ((not require-match)
1432                     (setq result default)
1433                     nil)
1434                    ((not default)
1435                     t)
1436                    ((not (get-buffer default))
1437                     t)
1438                    (t
1439                     (setq result default)
1440                     nil))))
1441     (if (bufferp result)
1442         (buffer-name result)
1443       result)))
1444
1445 (defun read-number (prompt &optional integers-only)
1446   "Read a number from the minibuffer."
1447   (let ((pred (if integers-only 'integerp 'numberp))
1448         num)
1449     (while (not (funcall pred num))
1450       (setq num (condition-case ()
1451                     (let ((minibuffer-completion-table nil))
1452                       (read-from-minibuffer
1453                        prompt (if num (prin1-to-string num)) nil t
1454                        t)) ;no history
1455                   (invalid-read-syntax nil)
1456                   (end-of-file nil)))
1457       (or (funcall pred num) (beep)))
1458     num))
1459
1460 (defun read-shell-command (prompt &optional initial-input history)
1461   "Just like read-string, but uses read-shell-command-map:
1462 \\{read-shell-command-map}"
1463   (let ((minibuffer-completion-table nil))
1464     (read-from-minibuffer prompt initial-input read-shell-command-map
1465                           nil (or history 'shell-command-history))))
1466
1467 \f
1468 ;;; This read-file-name stuff probably belongs in files.el
1469
1470 ;; Quote "$" as "$$" to get it past substitute-in-file-name
1471 (defun un-substitute-in-file-name (string)
1472   (let ((regexp "\\$")
1473         (olen (length string))
1474         new
1475         n o ch)
1476     (cond ((eq system-type 'vax-vms)
1477            string)
1478           ((not (string-match regexp string))
1479            string)
1480           (t
1481            (setq n 1)
1482            (while (string-match regexp string (match-end 0))
1483              (setq n (1+ n)))
1484            (setq new (make-string (+ olen n) ?$))
1485            (setq n 0 o 0)
1486            (while (< o olen)
1487              (setq ch (aref string o))
1488              (aset new n ch)
1489              (setq o (1+ o) n (1+ n))
1490              (if (eq ch ?$)
1491                  ;; already aset by make-string initial-value
1492                  (setq n (1+ n))))
1493            new))))
1494
1495 (defun read-file-name-2 (history prompt dir default
1496                                  must-match initial-contents
1497                                  completer)
1498   (if (not dir)
1499       (setq dir default-directory))
1500   (setq dir (abbreviate-file-name dir t))
1501   (let* ((insert (cond ((and (not insert-default-directory)
1502                              (not initial-contents))
1503                         "")
1504                        (initial-contents
1505                         (cons (un-substitute-in-file-name
1506                                (concat dir initial-contents))
1507                               (length dir)))
1508                        (t
1509                         (un-substitute-in-file-name dir))))
1510          (val (let ((completion-ignore-case (or completion-ignore-case
1511                                                 (eq system-type 'vax-vms))))
1512                 ;;  Hateful, broken, case-sensitive un*x
1513 ;;;                 (completing-read prompt
1514 ;;;                                  completer
1515 ;;;                                  dir
1516 ;;;                                  must-match
1517 ;;;                                  insert
1518 ;;;                                  history)
1519                 ;; #### - this is essentially the guts of completing read.
1520                 ;; There should be an elegant way to pass a pair of keymaps to
1521                 ;; completing read, but this will do for now.  All sins are
1522                 ;; relative.  --Stig
1523                 (let ((minibuffer-completion-table completer)
1524                       (minibuffer-completion-predicate dir)
1525                       (minibuffer-completion-confirm (if (eq must-match 't)
1526                                                          nil t))
1527                       (last-exact-completion nil))
1528                   (read-from-minibuffer prompt
1529                                         insert
1530                                         (if (not must-match)
1531                                             read-file-name-map
1532                                           read-file-name-must-match-map)
1533                                         nil
1534                                         history)))
1535               ))
1536 ;;;     ;; Kludge!  Put "/foo/bar" on history rather than "/default//foo/bar"
1537 ;;;     (let ((hist (cond ((not history) 'minibuffer-history)
1538 ;;;                       ((consp history) (car history))
1539 ;;;                       (t history))))
1540 ;;;       (if (and val
1541 ;;;                hist
1542 ;;;                (not (eq hist 't))
1543 ;;;                (boundp hist)
1544 ;;;                (equal (car-safe (symbol-value hist)) val))
1545 ;;;           (let ((e (condition-case nil
1546 ;;;                        (expand-file-name val)
1547 ;;;                      (error nil))))
1548 ;;;             (if (and e (not (equal e val)))
1549 ;;;                 (set hist (cons e (cdr (symbol-value hist))))))))
1550
1551     (cond ((not val)
1552            (error "No file name specified"))
1553           ((and default
1554                 (equal val (if (consp insert) (car insert) insert)))
1555            default)
1556           (t
1557            (substitute-in-file-name val)))))
1558
1559 ;; #### this function should use minibuffer-completion-table
1560 ;; or something.  But that is sloooooow.
1561 ;; #### all this shit needs better documentation!!!!!!!!
1562 (defun read-file-name-activate-callback (event extent dir-p)
1563   ;; used as the activate-callback of the filename list items
1564   ;; in the completion buffer, in place of default-choose-completion.
1565   ;; if a regular file was selected, we call default-choose-completion
1566   ;; (which just inserts the string in the minibuffer and calls
1567   ;; exit-minibuffer).  If a directory was selected, we display
1568   ;; the contents of the directory.
1569   (let* ((file (extent-string extent))
1570          (completion-buf (extent-object extent))
1571          (minibuf (symbol-value-in-buffer 'completion-reference-buffer
1572                                           completion-buf))
1573          (in-dir (file-name-directory (buffer-substring nil nil minibuf)))
1574          (full (expand-file-name file in-dir)))
1575     (if (not (file-directory-p full))
1576         (default-choose-completion event extent minibuf)
1577       (erase-buffer minibuf)
1578       (insert-string (file-name-as-directory
1579                       (abbreviate-file-name full t)) minibuf)
1580       (reset-buffer completion-buf)
1581       (let ((standard-output completion-buf))
1582         (display-completion-list
1583          (delete "." (directory-files full nil nil nil (if dir-p 'directory)))
1584          :user-data dir-p
1585          :reference-buffer minibuf
1586          :activate-callback 'read-file-name-activate-callback)
1587         (goto-char (point-min) completion-buf)))))
1588
1589 (defun read-file-name-1 (history prompt dir default
1590                                  must-match initial-contents
1591                                  completer)
1592   (if (should-use-dialog-box-p)
1593       ;; this calls read-file-name-2
1594       (mouse-read-file-name-1 history prompt dir default must-match
1595                               initial-contents completer)
1596     (let ((rfhookfun
1597            (lambda ()
1598              ;; #### SCREAM!  Create a `file-system-ignore-case'
1599              ;; function, so this kind of stuff is generalized!
1600              (and (eq system-type 'windows-nt)
1601                   (set (make-local-variable 'completion-ignore-case) t))
1602              (set
1603               (make-local-variable
1604                'completion-display-completion-list-function)
1605               #'(lambda (completions)
1606                   (display-completion-list
1607                    completions
1608                    :user-data (not (eq completer 'read-file-name-internal))
1609                    :activate-callback
1610                    'read-file-name-activate-callback)))
1611              ;; kludge!
1612              (remove-hook 'minibuffer-setup-hook rfhookfun)
1613              )))
1614       (unwind-protect
1615           (progn
1616             (add-hook 'minibuffer-setup-hook rfhookfun)
1617             (read-file-name-2 history prompt dir default must-match
1618                               initial-contents completer))
1619         (remove-hook 'minibuffer-setup-hook rfhookfun)))))
1620
1621 (defun read-file-name (prompt
1622                        &optional dir default must-match initial-contents
1623                        history)
1624   "Read file name, prompting with PROMPT and completing in directory DIR.
1625 This will prompt with a dialog box if appropriate, according to
1626  `should-use-dialog-box-p'.
1627 Value is not expanded---you must call `expand-file-name' yourself.
1628 Value is subject to interpreted by substitute-in-file-name however.
1629 Default name to DEFAULT if user enters a null string.
1630  (If DEFAULT is omitted, the visited file name is used,
1631   except that if INITIAL-CONTENTS is specified, that combined with DIR is
1632   used.)
1633 Fourth arg MUST-MATCH non-nil means require existing file's name.
1634  Non-nil and non-t means also require confirmation after completion.
1635 Fifth arg INITIAL-CONTENTS specifies text to start with.
1636 Sixth arg HISTORY specifies the history list to use.  Default is
1637  `file-name-history'.
1638 DIR defaults to current buffer's directory default."
1639   (read-file-name-1
1640    (or history 'file-name-history)
1641    prompt dir (or default
1642                   (if initial-contents (expand-file-name initial-contents dir)
1643                     buffer-file-name))
1644    must-match initial-contents
1645    ;; A separate function (not an anonymous lambda-expression)
1646    ;; and passed as a symbol because of disgusting kludges in various
1647    ;; places which do stuff like (let ((filename-kludge-p (eq minibuffer-completion-table 'read-file-name-internal))) ...)
1648    'read-file-name-internal))
1649
1650 (defun read-directory-name (prompt
1651                             &optional dir default must-match initial-contents
1652                             history)
1653   "Read directory name, prompting with PROMPT and completing in directory DIR.
1654 This will prompt with a dialog box if appropriate, according to
1655  `should-use-dialog-box-p'.
1656 Value is not expanded---you must call `expand-file-name' yourself.
1657 Value is subject to interpreted by substitute-in-file-name however.
1658 Default name to DEFAULT if user enters a null string.
1659  (If DEFAULT is omitted, the current buffer's default directory is used.)
1660 Fourth arg MUST-MATCH non-nil means require existing directory's name.
1661  Non-nil and non-t means also require confirmation after completion.
1662 Fifth arg INITIAL-CONTENTS specifies text to start with.
1663 Sixth arg HISTORY specifies the history list to use.  Default is
1664  `file-name-history'.
1665 DIR defaults to current buffer's directory default."
1666   (read-file-name-1
1667     (or history 'file-name-history)
1668     prompt dir (or default default-directory) must-match initial-contents
1669     'read-directory-name-internal))
1670
1671
1672 ;; Environment-variable completion hack
1673 (defun read-file-name-internal-1 (string dir action completer)
1674   (if (not (string-match
1675             "\\([^$]\\|\\`\\)\\(\\$\\$\\)*\\$\\([A-Za-z0-9_]*\\|{[^}]*\\)\\'"
1676             string))
1677       ;; Not doing environment-variable completion hack
1678       (let* ((orig (if (equal string "") nil string))
1679              (sstring (if orig (substitute-in-file-name string) string))
1680              (specdir (if orig (file-name-directory sstring) nil)))
1681         (funcall completer
1682                  action
1683                  orig
1684                  sstring
1685                  specdir
1686                  (if specdir (expand-file-name specdir dir) dir)
1687                  (if orig (file-name-nondirectory sstring) string)))
1688       ;; An odd number of trailing $'s
1689       (let* ((start (match-beginning 3))
1690              (env (substring string
1691                              (cond ((= start (length string))
1692                                     ;; "...$"
1693                                     start)
1694                                    ((= (aref string start) ?{)
1695                                     ;; "...${..."
1696                                     (1+ start))
1697                                    (t
1698                                     start))))
1699              (head (substring string 0 (1- start)))
1700              (alist #'(lambda ()
1701                         (mapcar #'(lambda (x)
1702                                     (cons (substring x 0 (string-match "=" x))
1703                                           'nil))
1704                                 process-environment))))
1705
1706         (cond ((eq action 'lambda)
1707                nil)
1708               ((eq action 't)
1709                ;; all completions
1710                (mapcar #'(lambda (p)
1711                            (if (and (> (length p) 0)
1712                                     ;;#### Unix-specific
1713                                     ;;####  -- need absolute-pathname-p
1714                                     (/= (aref p 0) ?/))
1715                                (concat "$" p)
1716                              (concat head "$" p)))
1717                        (all-completions env (funcall alist))))
1718               (t ;; 'nil
1719                ;; complete
1720                (let* ((e (funcall alist))
1721                       (val (try-completion env e)))
1722                  (cond ((stringp val)
1723                         (if (string-match "[^A-Za-z0-9_]" val)
1724                             (concat head
1725                                     "${" val
1726                                     ;; completed uniquely?
1727                                     (if (eq (try-completion val e) 't)
1728                                         "}" ""))
1729                             (concat head "$" val)))
1730                        ((eql val 't)
1731                         (concat head
1732                                 (un-substitute-in-file-name (getenv env))))
1733                        (t nil))))))))
1734
1735
1736 (defun read-file-name-internal (string dir action)
1737   (read-file-name-internal-1
1738    string dir action
1739    #'(lambda (action orig string specdir dir name)
1740       (cond ((eq action 'lambda)
1741              (if (not orig)
1742                  nil
1743                (let ((sstring (condition-case nil
1744                                   (expand-file-name string)
1745                                 (error nil))))
1746                  (if (not sstring)
1747                      ;; Some pathname syntax error in string
1748                      nil
1749                      (file-exists-p sstring)))))
1750             ((eq action 't)
1751              ;; all completions
1752              (mapcar #'un-substitute-in-file-name
1753                      (file-name-all-completions name dir)))
1754             (t;; 'nil
1755              ;; complete
1756              (let* ((d (or dir default-directory))
1757                     (val (file-name-completion name d)))
1758                (if (and (eq val 't)
1759                         (not (null completion-ignored-extensions)))
1760                    ;;#### (file-name-completion "foo") returns 't
1761                    ;;   when both "foo" and "foo~" exist and the latter
1762                    ;;   is "pruned" by completion-ignored-extensions.
1763                    ;; I think this is a bug in file-name-completion.
1764                    (setq val (let ((completion-ignored-extensions '()))
1765                                (file-name-completion name d))))
1766                (if (stringp val)
1767                    (un-substitute-in-file-name (if specdir
1768                                                    (concat specdir val)
1769                                                    val))
1770                    (let ((tem (un-substitute-in-file-name string)))
1771                      (if (not (equal tem orig))
1772                          ;; substitute-in-file-name did something
1773                          tem
1774                          val)))))))))
1775
1776 (defun read-directory-name-internal (string dir action)
1777   (read-file-name-internal-1
1778    string dir action
1779    #'(lambda (action orig string specdir dir name)
1780       (let* ((dirs #'(lambda (fn)
1781                        (let ((l (if (equal name "")
1782                                     (directory-files
1783                                      dir
1784                                      nil
1785                                      ""
1786                                      nil
1787                                      'directories)
1788                                   (directory-files
1789                                    dir
1790                                    nil
1791                                    (concat "\\`" (regexp-quote name))
1792                                    nil
1793                                    'directories))))
1794                          (mapcar fn
1795                                  (cond ((eq system-type 'vax-vms)
1796                                         l)
1797                                        (t
1798                                         ;; Wretched unix
1799                                         (delete "." l))))))))
1800         (cond ((eq action 'lambda)
1801                ;; complete?
1802                (if (not orig)
1803                    nil
1804                  (file-directory-p string)))
1805               ((eq action 't)
1806                ;; all completions
1807                (funcall dirs #'(lambda (n)
1808                                  (un-substitute-in-file-name
1809                                   (file-name-as-directory n)))))
1810               (t
1811                ;; complete
1812                (let ((val (try-completion
1813                            name
1814                            (funcall dirs
1815                                     #'(lambda (n)
1816                                         (list (file-name-as-directory
1817                                                n)))))))
1818                  (if (stringp val)
1819                      (un-substitute-in-file-name (if specdir
1820                                                      (concat specdir val)
1821                                                    val))
1822                    (let ((tem (un-substitute-in-file-name string)))
1823                      (if (not (equal tem orig))
1824                          ;; substitute-in-file-name did something
1825                          tem
1826                        val))))))))))
1827
1828 (defun append-expand-filename (file-string string)
1829   "Append STRING to FILE-STRING differently depending on whether STRING
1830 is a username (~string), an environment variable ($string),
1831 or a filename (/string).  The resultant string is returned with the
1832 environment variable or username expanded and resolved to indicate
1833 whether it is a file(/result) or a directory (/result/)."
1834   (let ((file
1835          (cond ((string-match "\\([~$]\\)\\([^~$/]*\\)$" file-string)
1836                 (cond ((string= (substring file-string
1837                                            (match-beginning 1)
1838                                            (match-end 1)) "~")
1839                        (concat (substring file-string 0 (match-end 1))
1840                                string))
1841                       (t (substitute-in-file-name
1842                           (concat (substring file-string 0 (match-end 1))
1843                                   string)))))
1844                (t (concat (file-name-directory
1845                            (substitute-in-file-name file-string)) string))))
1846         result)
1847
1848     (cond ((stringp (setq result (and (file-exists-p (expand-file-name file))
1849                                       (read-file-name-internal
1850                                        (condition-case nil
1851                                            (expand-file-name file)
1852                                          (error file))
1853                                        "" nil))))
1854            result)
1855           (t file))))
1856
1857 (defun mouse-file-display-completion-list (window dir minibuf user-data)
1858   (let ((standard-output (window-buffer window)))
1859     (condition-case nil
1860         (display-completion-list
1861          (directory-files dir nil nil nil t)
1862          :window-width (* 2 (window-width window))
1863          :activate-callback
1864          'mouse-read-file-name-activate-callback
1865          :user-data user-data
1866          :reference-buffer minibuf
1867          :help-string "")
1868       (t nil))))
1869
1870 (defun mouse-directory-display-completion-list (window dir minibuf user-data)
1871   (let ((standard-output (window-buffer window)))
1872     (condition-case nil
1873         (display-completion-list
1874          (delete "." (directory-files dir nil nil nil 1))
1875          :window-width (window-width window)
1876          :activate-callback
1877          'mouse-read-file-name-activate-callback
1878          :user-data user-data
1879          :reference-buffer minibuf
1880          :help-string "")
1881       (t nil))))
1882
1883 (defun mouse-read-file-name-activate-callback (event extent user-data)
1884   (let* ((file (extent-string extent))
1885          (minibuf (symbol-value-in-buffer 'completion-reference-buffer
1886                                           (extent-object extent)))
1887          (in-dir (buffer-substring nil nil minibuf))
1888          (full (expand-file-name file in-dir))
1889          (filebuf (nth 0 user-data))
1890          (dirbuff (nth 1 user-data))
1891          (filewin (nth 2 user-data))
1892          (dirwin (nth 3 user-data)))
1893     (if (file-regular-p full)
1894         (default-choose-completion event extent minibuf)
1895       (erase-buffer minibuf)
1896       (insert-string (file-name-as-directory
1897                       (abbreviate-file-name full t)) minibuf)
1898       (reset-buffer filebuf)
1899       (if (not dirbuff)
1900           (mouse-directory-display-completion-list filewin full minibuf
1901                                                    user-data)
1902         (mouse-file-display-completion-list filewin full minibuf user-data)
1903         (reset-buffer dirbuff)
1904         (mouse-directory-display-completion-list dirwin full minibuf
1905                                                  user-data)))))
1906
1907 ;; this is rather cheesified but gets the job done.
1908 (defun mouse-read-file-name-1 (history prompt dir default
1909                                  must-match initial-contents
1910                                  completer)
1911   (let* ((file-p (eq 'read-file-name-internal completer))
1912          (filebuf (get-buffer-create "*Completions*"))
1913          (dirbuff (and file-p (generate-new-buffer " *mouse-read-file*")))
1914          (butbuff (generate-new-buffer " *mouse-read-file*"))
1915          (frame (make-dialog-frame))
1916          filewin dirwin
1917          user-data)
1918     (unwind-protect
1919         (progn
1920           (reset-buffer filebuf)
1921           (select-frame frame)
1922           (let ((window-min-height 1))
1923             ;; #### should be 2 not 3, but that causes
1924             ;; "window too small to split" errors for some
1925             ;; people (but not for me ...) There's a more
1926             ;; fundamental bug somewhere.
1927             (split-window nil (- (frame-height frame) 3)))
1928           (if file-p
1929               (progn
1930                 (split-window-horizontally 16)
1931                 (setq filewin (frame-rightmost-window frame)
1932                       dirwin (frame-leftmost-window frame))
1933                 (set-window-buffer filewin filebuf)
1934                 (set-window-buffer dirwin dirbuff))
1935             (setq filewin (frame-highest-window frame))
1936             (set-window-buffer filewin filebuf))
1937           (setq user-data (list filebuf dirbuff filewin dirwin))
1938           (set-window-buffer (frame-lowest-window frame) butbuff)
1939           (set-buffer butbuff)
1940           (when dir
1941             (setq default-directory dir))
1942           (when (featurep 'scrollbar)
1943             (set-specifier scrollbar-width 0 butbuff))
1944           (insert "                 ")
1945           (insert-gui-button (make-gui-button "OK"
1946                                               (lambda (foo)
1947                                                 (exit-minibuffer))))
1948           (insert "                 ")
1949           (insert-gui-button (make-gui-button "Cancel"
1950                                               (lambda (foo)
1951                                                 (abort-recursive-edit))))
1952           (let ((rfhookfun
1953                  (lambda ()
1954                    (if (not file-p)
1955                        (mouse-directory-display-completion-list
1956                         filewin dir (current-buffer) user-data)
1957                      (mouse-file-display-completion-list filewin dir
1958                                                          (current-buffer)
1959                                                          user-data)
1960                      (mouse-directory-display-completion-list dirwin dir
1961                                                               (current-buffer)
1962                                                               user-data))
1963                    (set
1964                     (make-local-variable
1965                      'completion-display-completion-list-function)
1966                     #'(lambda (completions)
1967                         (display-completion-list
1968                          completions
1969                          :help-string ""
1970                          :activate-callback
1971                          'mouse-read-file-name-activate-callback
1972                          :user-data user-data)))
1973                    ;; kludge!
1974                    (remove-hook 'minibuffer-setup-hook rfhookfun)
1975                    ))
1976                 (rfcshookfun
1977                  ;; kludge!
1978                  ;; #### I really need to flesh out the object
1979                  ;; hierarchy better to avoid these kludges.
1980                  (lambda ()
1981                    (save-excursion
1982                      (set-buffer standard-output)
1983                      (setq truncate-lines t)))))
1984             (unwind-protect
1985                 (progn
1986                   (add-hook 'minibuffer-setup-hook rfhookfun)
1987                   (add-hook 'completion-setup-hook rfcshookfun)
1988                   (read-file-name-2 history prompt dir default
1989                                     must-match initial-contents
1990                                     completer))
1991               (remove-hook 'minibuffer-setup-hook rfhookfun)
1992               (remove-hook 'completion-setup-hook rfcshookfun))))
1993       (delete-frame frame)
1994       (kill-buffer filebuf)
1995       (kill-buffer butbuff)
1996       (and dirbuff (kill-buffer dirbuff)))))
1997
1998 (defun read-face (prompt &optional must-match)
1999   "Read the name of a face from the minibuffer and return it as a symbol."
2000   (intern (completing-read prompt obarray 'find-face must-match)))
2001
2002 ;; #### - wrong place for this variable?  Exactly.  We probably want
2003 ;; `color-list' to be a console method, so `tty-color-list' becomes
2004 ;; obsolete, and `read-color-completion-table' conses (mapcar #'list
2005 ;; (color-list)), optionally caching the results.
2006
2007 ;; Ben wanted all of the possibilities from the `configure' script used
2008 ;; here, but I think this is way too many.  I already trimmed the R4 variants
2009 ;; and a few obvious losers from the list.  --Stig
2010 (defvar x-library-search-path '("/usr/X11R6/lib/X11/"
2011                                 "/usr/X11R5/lib/X11/"
2012                                 "/usr/lib/X11R6/X11/"
2013                                 "/usr/lib/X11R5/X11/"
2014                                 "/usr/local/X11R6/lib/X11/"
2015                                 "/usr/local/X11R5/lib/X11/"
2016                                 "/usr/local/lib/X11R6/X11/"
2017                                 "/usr/local/lib/X11R5/X11/"
2018                                 "/usr/X11/lib/X11/"
2019                                 "/usr/lib/X11/"
2020                                 "/usr/local/lib/X11/"
2021                                 "/usr/X386/lib/X11/"
2022                                 "/usr/x386/lib/X11/"
2023                                 "/usr/XFree86/lib/X11/"
2024                                 "/usr/unsupported/lib/X11/"
2025                                 "/usr/athena/lib/X11/"
2026                                 "/usr/local/x11r5/lib/X11/"
2027                                 "/usr/lpp/Xamples/lib/X11/"
2028                                 "/usr/openwin/lib/X11/"
2029                                 "/usr/openwin/share/lib/X11/")
2030   "Search path used by `read-color' to find rgb.txt.")
2031
2032 (defvar x-read-color-completion-table)
2033
2034 (defun read-color-completion-table ()
2035   (case (device-type)
2036     ;; #### Evil device-type dependency
2037     (x
2038      (if (boundp 'x-read-color-completion-table)
2039          x-read-color-completion-table
2040        (let ((rgb-file (locate-file "rgb.txt" x-library-search-path))
2041              clist color p)
2042          (if (not rgb-file)
2043              ;; prevents multiple searches for rgb.txt if we can't find it
2044              (setq x-read-color-completion-table nil)
2045            (with-current-buffer (get-buffer-create " *colors*")
2046              (reset-buffer (current-buffer))
2047              (insert-file-contents rgb-file)
2048              (while (not (eobp))
2049                ;; skip over comments
2050                (while (looking-at "^!")
2051                  (end-of-line)
2052                  (forward-char 1))
2053                (skip-chars-forward "0-9 \t")
2054                (setq p (point))
2055                (end-of-line)
2056                (setq color (buffer-substring p (point))
2057                      clist (cons (list color) clist))
2058                ;; Ugh.  If we want to be able to complete the lowercase form
2059                ;; of the color name, we need to add it twice!  Yuck.
2060                (let ((dcase (downcase color)))
2061                  (or (string= dcase color)
2062                      (push (list dcase) clist)))
2063                (forward-char 1))
2064              (kill-buffer (current-buffer))))
2065          (setq x-read-color-completion-table clist)
2066          x-read-color-completion-table)))
2067     (mswindows
2068      (mapcar #'list (mswindows-color-list)))
2069     (tty
2070      (mapcar #'list (tty-color-list)))))
2071
2072 (defun read-color (prompt &optional must-match initial-contents)
2073   "Read the name of a color from the minibuffer.
2074 On X devices, this uses `x-library-search-path' to find rgb.txt in order
2075  to build a completion table.
2076 On TTY devices, this uses `tty-color-list'.
2077 On mswindows devices, this uses `mswindows-color-list'."
2078   (let ((table (read-color-completion-table)))
2079     (completing-read prompt table nil (and table must-match)
2080                      initial-contents)))
2081
2082 \f
2083 ;; #### The doc string for read-non-nil-coding system gets lost if we
2084 ;; only include these if the mule feature is present.  Strangely,
2085 ;; read-coding-system doesn't.
2086
2087 ;;(if (featurep 'mule)
2088
2089 (defun read-coding-system (prompt)
2090   "Read a coding-system (or nil) from the minibuffer.
2091 Prompting with string PROMPT."
2092   (intern (completing-read prompt obarray 'find-coding-system t)))
2093
2094 (defun read-non-nil-coding-system (prompt)
2095   "Read a non-nil coding-system from the minibuffer.
2096 Prompt with string PROMPT."
2097   (let ((retval (intern "")))
2098     (while (= 0 (length (symbol-name retval)))
2099       (setq retval (intern (completing-read prompt obarray
2100                                             'find-coding-system
2101                                             t))))
2102     retval))
2103
2104 ;;) ;; end of (featurep 'mule)
2105
2106 \f
2107
2108 (defcustom force-dialog-box-use nil
2109   "*If non-nil, always use a dialog box for asking questions, if possible.
2110 You should *bind* this, not set it.  This is useful if you're doing
2111 something mousy but which wasn't actually invoked using the mouse."
2112   :type 'boolean
2113   :group 'minibuffer)
2114
2115 ;; We include this here rather than dialog.el so it is defined
2116 ;; even when dialog boxes are not present.
2117 (defun should-use-dialog-box-p ()
2118   "If non-nil, questions should be asked with a dialog box instead of the
2119 minibuffer.  This looks at `last-command-event' to see if it was a mouse
2120 event, and checks whether dialog-support exists and the current device
2121 supports dialog boxes.
2122
2123 The dialog box is totally disabled if the variable `use-dialog-box'
2124 is set to nil."
2125   (and (featurep 'dialog)
2126        (device-on-window-system-p)
2127        use-dialog-box
2128        (or force-dialog-box-use
2129            (button-press-event-p last-command-event)
2130            (button-release-event-p last-command-event)
2131            (misc-user-event-p last-command-event))))
2132
2133 ;;; minibuf.el ends here