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