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