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