Doc, comment, etc updates for increased use of locate-user-emacs-file
[bpt/emacs.git] / lisp / ido.el
1 ;;; ido.el --- interactively do things with buffers and files
2
3 ;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
4
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Based on: iswitchb by Stephen Eglen <stephen@cns.ed.ac.uk>
7 ;; Keywords: extensions convenience
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24
25 ;;; Commentary:
26
27 ;; Ido - interactive do - switches between buffers and opens files and
28 ;; directories with a minimum of keystrokes. It is a superset of
29 ;; iswitchb, the interactive buffer switching package by Stephen Eglen.
30
31 ;; Interactive substring matching
32 ;; ------------------------------
33 ;;
34 ;; As you type in a substring, the list of buffers or files currently
35 ;; matching the substring are displayed as you type. The list is
36 ;; ordered so that the most recent buffers or files visited come at
37 ;; the start of the list.
38 ;;
39 ;; The buffer or file at the start of the list will be the one visited
40 ;; when you press RETURN. By typing more of the substring, the list is
41 ;; narrowed down so that gradually the buffer or file you want will be
42 ;; at the top of the list. Alternatively, you can use C-s and C-r (or
43 ;; the right and left arrow keys) to rotate buffer or file names in the
44 ;; list until the one you want is at the top of the list.
45 ;;
46 ;; Completion is also available so that you can see what is common to
47 ;; all of the matching buffers or files as you type.
48 ;;
49 ;; Example:
50 ;;
51 ;; If I have two buffers called "123456" and "123", with "123456" the
52 ;; most recent, when I use ido-switch-buffer, I first of all get
53 ;; presented with the list of all the buffers
54 ;;
55 ;; Buffer: {123456 | 123}
56 ;;
57 ;; If I then press 2:
58 ;; Buffer: 2[3]{123456 | 123}
59 ;;
60 ;; The list in {...} are the matching buffers, most recent first
61 ;; (buffers visible in the current frame are put at the end of the
62 ;; list by default). At any time I can select the item at the head of
63 ;; the list by pressing RET. I can also put the first element at the
64 ;; end of the list by pressing C-s or [right], or bring the last
65 ;; element to the head of the list by pressing C-r or [left].
66 ;;
67 ;; The item in [...] indicates what can be added to my input by
68 ;; pressing TAB. In this case, I will get "3" added to my input.
69
70 ;; So, I press TAB:
71 ;; Buffer: 23{123456 | 123}
72 ;;
73 ;; At this point, I still have two matching buffers.
74 ;; If I want the first buffer in the list, I simply press RET. If I
75 ;; wanted the second in the list, I could press C-s to move it to the
76 ;; top of the list and then RET to select it.
77 ;;
78 ;; However, if I type 4, I only have one match left:
79 ;; Buffer: 234[123456]
80 ;;
81 ;; Since there is only one matching buffer left, it is given in [] and
82 ;; it is shown in the `ido-only-match' face (ForestGreen). I can now
83 ;; press TAB or RET to go to that buffer.
84 ;;
85 ;; If I want to create a new buffer named "234", I press C-j instead of
86 ;; TAB or RET.
87 ;;
88 ;; If instead, I type "a":
89 ;; Buffer: 234a [No match]
90 ;; There are no matching buffers. If I press RET or TAB, I can be
91 ;; prompted to create a new buffer called "234a".
92 ;;
93 ;; Of course, where this function comes in really useful is when you
94 ;; can specify the buffer using only a few keystrokes. In the above
95 ;; example, the quickest way to get to the "123456" file would be
96 ;; just to type 4 and then RET (assuming there isn't any newer buffer
97 ;; with 4 in its name).
98
99 ;; Likewise, if you use C-x C-f (ido-find-file), the list of files and
100 ;; directories in the current directory is provided in the same
101 ;; fashion as the buffers above. The files and directories are
102 ;; normally sorted in alphabetical order, but the most recently
103 ;; visited directory is placed first to speed up navigating to
104 ;; directories that you have visited recently.
105 ;;
106 ;; In addition to scrolling through the list using [right] and [left],
107 ;; you can use [up] and [down] to quickly scroll the list to the next
108 ;; or previous subdirectory.
109 ;;
110 ;; To go down into a subdirectory, and continue the file selection on
111 ;; the files in that directory, simply move the directory to the head
112 ;; of the list and hit RET.
113 ;;
114 ;; To go up to the parent directory, delete any partial file name
115 ;; already specified (e.g. using [backspace]) and hit [backspace].
116 ;;
117 ;; To go to the root directory (on the current drive), enter two
118 ;; slashes. On MS-DOS or Windows, to select the root of another
119 ;; drive, enter X:/ where X is the drive letter. You can also visit
120 ;; files on other hosts using the ange-ftp notations `/host:' and
121 ;; `/user@host:'. See the variable `ido-slow-ftp-hosts' if you want
122 ;; to inhibit the ido substring matching for ftp access.
123 ;;
124 ;; If for some reason you cannot specify the proper file using
125 ;; ido-find-file, you can press C-f to enter the normal find-file.
126 ;; You can also press C-b to drop into ido-switch-buffer.
127
128 ;; See the doc string of ido-switch-buffer and ido-find-file for full
129 ;; keybindings and features.
130 ;; (describe-function 'ido-find-file)
131
132 ;; Hidden buffers and files
133 ;; ------------------------
134 ;;
135 ;; Normally, ido does not include hidden buffers (whose name starts
136 ;; with a space) and hidden files and directories (whose name starts
137 ;; with `.') in the list of possible completions. However, if the
138 ;; substring you enter does not match any of the visible buffers or
139 ;; files, ido will automatically look for completions among the hidden
140 ;; buffers or files.
141 ;;
142 ;; You can toggle display of the hidden buffers and files with C-a.
143
144 ;; Additional functionality
145 ;; ------------------------
146 ;;
147 ;; After C-x b, the buffer at the head of the list can be killed by
148 ;; pressing C-k. If the buffer needs saving, you will be queried
149 ;; before the buffer is killed.
150 ;;
151 ;; Likewise, after C-x C-f, you can delete (i.e. physically remove)
152 ;; the file at the head of the list with C-k. You will always be
153 ;; asked for confirmation before the file is deleted.
154 ;;
155 ;; If you enter C-x b to switch to a buffer visiting a given file, and
156 ;; you find that the file you are after is not in any buffer, you can
157 ;; press C-f to immediately drop into ido-find-file. And you can
158 ;; switch back to buffer selection with C-b.
159
160 ;; Prefix matching
161 ;; ---------------
162 ;;
163 ;; The standard way of completion with Unix-shells and Emacs is to insert a
164 ;; PREFIX and then hitting TAB (or another completion key). Cause of this
165 ;; behavior has become second nature to a lot of Emacs users `ido' offers in
166 ;; addition to the default substring-matching-method (look above) also the
167 ;; prefix-matching-method. The kind of matching is the only difference to
168 ;; the description of the substring-matching above.
169 ;;
170 ;; You can toggle prefix matching with C-p.
171 ;;
172 ;; Example:
173 ;;
174 ;; If you have again two Buffers "123456" and "123" then hitting "2" does
175 ;; not match because "2" is not a PREFIX in any of the buffer-names.
176
177 ;; Flexible matching
178 ;; -----------------
179 ;;
180 ;; If you set ido-enable-flex-matching, ido will do a more flexible
181 ;; matching (unless regexp matching is active) to find possible matches
182 ;; among the available buffer or file names if no matches are found using
183 ;; the normal prefix or substring matching.
184 ;;
185 ;; The flexible matching implies that any item which simply contains all
186 ;; of the entered characters in the specified sequence will match.
187 ;;
188 ;; Example:
189 ;;
190 ;; If you have four files "alpha", "beta", "gamma", and "delta",
191 ;; entering "aa" will match "alpha" and "gamma", while "ea" matches
192 ;; "beta" and "delta". If prefix matching is also active, "aa" only
193 ;; matches "alpha", while "ea" does not match any files.
194
195 ;; Regexp matching
196 ;; ---------------
197 ;;
198 ;; There is limited provision for regexp matching within ido,
199 ;; enabled through `ido-enable-regexp' (toggle with C-t).
200 ;; This allows you to type `[ch]$' for example and see all file names
201 ;; ending in `c' or `h'.
202 ;;
203 ;; Note: ido-style completion is inhibited when you enable regexp matching.
204
205
206 ;; Customization
207 ;; -------------
208 ;;
209 ;; Customize the `ido' group to change the `ido' functionality.
210 ;;
211 ;; To modify the keybindings, use the ido-setup-hook. For example:
212 ;;(add-hook 'ido-setup-hook 'ido-my-keys)
213 ;;
214 ;;(defun ido-my-keys ()
215 ;; "Add my keybindings for ido."
216 ;; (define-key ido-completion-map " " 'ido-next-match)
217 ;; )
218
219 ;; Seeing all the matching buffers or files
220 ;; ----------------------------------------
221 ;;
222 ;; If you have many matching files, they may not all fit onto one
223 ;; line of the minibuffer. Normally, the minibuffer window will grow
224 ;; to show you more of the matching files (depending on the setting
225 ;; of the variables `resize-mini-windows' and `max-mini-window-height').
226 ;; If you want ido to behave differently from the default minibuffer
227 ;; resizing behavior, set the variable `ido-max-window-height'.
228 ;;
229 ;; Also, to improve the responsiveness of ido, the maximum number of
230 ;; matching items is limited to 12, but you can increase or removed
231 ;; this limit via the `ido-max-prospects' variable.
232
233 ;; To see a full list of all matching buffers in a separate buffer,
234 ;; hit ? or press TAB when there are no further completions to the
235 ;; substring. Repeated TAB presses will scroll you through this
236 ;; separate buffer.
237
238 ;; Changing the list of files
239 ;; --------------------------
240
241 ;; By default, the list of current files is most recent first,
242 ;; oldest last, with the exception that the files visible in the
243 ;; current frame are put at the end of the list. A hook exists to
244 ;; allow other functions to order the list. For example, if you add:
245 ;;
246 ;; (add-hook 'ido-make-buffer-list-hook 'ido-summary-buffers-to-end)
247 ;;
248 ;; then all files matching "Summary" are moved to the end of the
249 ;; list. (I find this handy for keeping the INBOX Summary and so on
250 ;; out of the way.) It also moves files matching "output\*$" to the
251 ;; end of the list (these are created by AUCTeX when compiling.)
252 ;; Other functions could be made available which alter the list of
253 ;; matching files (either deleting or rearranging elements.)
254
255 ;; Highlighting
256 ;; ------------
257
258 ;; The highlighting of matching items is controlled via ido-use-faces.
259 ;; The faces used are ido-first-match, ido-only-match and
260 ;; ido-subdir.
261 ;; Coloring of the matching item was suggested by
262 ;; Carsten Dominik (dominik@strw.leidenuniv.nl).
263
264 ;; Replacement for read-buffer and read-file-name
265 ;; ----------------------------------------------
266
267 ;; ido-read-buffer and ido-read-file-name have been written to be drop
268 ;; in replacements for the normal buffer and file name reading
269 ;; functions `read-buffer' and `read-file-name'.
270
271 ;; To use ido for all buffer and file selections in Emacs, customize the
272 ;; variable `ido-everywhere'.
273
274 ;; Using ido-like behavior in other Lisp packages
275 ;; -----------------------------------------------
276
277 ;; If you don't want to rely on the `ido-everywhere' functionality,
278 ;; ido-read-buffer, ido-read-file-name, and ido-read-directory-name
279 ;; can be used by other packages to read a buffer name, a file name,
280 ;; or a directory name in the `ido' way.
281
282 ;;; Acknowledgments
283
284 ;; Infinite amounts of gratitude goes to Stephen Eglen <stephen@cns.ed.ac.uk>
285 ;; who wrote iswitch-buffer mode - from which I ripped off 99% of the code
286 ;; for ido-switch-buffer and found the inspiration for ido-find-file.
287 ;; The ido package would never have existed without his work.
288
289 ;; Also thanks to Klaus Berndl, Rohit Namjoshi, Robert Fenk, Alex
290 ;; Schroeder, Bill Benedetto, Stephen Eglen, and many others for bug
291 ;; fixes and improvements.
292
293 ;;; History
294
295 ;; Since I discovered Stephen Eglen's excellent iswitchb package, I just
296 ;; couldn't live without it, but once being addicted to switching buffers
297 ;; with a minimum of keystrokes, I soon found that opening files in the
298 ;; old-fashioned way was just too slow - so I decided to write a package
299 ;; which could open files with the same speed and ease as iswitchb could
300 ;; switch buffers.
301
302 ;; I originally wrote a separate ifindf.el package based on a copy of
303 ;; iswitchb.el, which did for opening files what iswitchb did for
304 ;; switching buffers. Along the way, I corrected a few errors in
305 ;; ifindf which could have found its way back into iswitchb, but since
306 ;; most of the functionality of the two package was practically
307 ;; identical, I decided that the proper thing to do was to merge my
308 ;; ifindf package back into iswitchb.
309 ;;
310 ;; This is basically what ido (interactively do) is all about; but I
311 ;; found it awkward to merge my changes into the "iswitchb-" namespace,
312 ;; so I invented a common "ido-" namespace for the merged packages.
313 ;;
314 ;; This version is based on ido.el version 1.57 released on
315 ;; gnu.emacs.sources adapted for Emacs 22.1 to use command remapping
316 ;; and optionally hooking the read-buffer and read-file-name functions.
317 ;;
318 ;; Prefix matching was added by Klaus Berndl <klaus.berndl@sdm.de> based on
319 ;; an idea of Yuji Minejima <ggb01164@nifty.ne.jp> and his mcomplete-package.
320
321
322 ;;; Code:
323
324 (defvar recentf-list)
325
326 ;;; User Variables
327 ;;
328 ;; These are some things you might want to change.
329
330 (defun ido-fractionp (n)
331 "Return t if N is a fraction."
332 (and (numberp n) (> n 0.0) (<= n 1.0)))
333
334 (defgroup ido nil
335 "Switch between files using substrings."
336 :group 'extensions
337 :group 'convenience
338 :version "22.1"
339 :link '(emacs-commentary-link :tag "Commentary" "ido.el")
340 :link '(emacs-library-link :tag "Lisp File" "ido.el")
341 :link '(custom-manual "(ido) Top")
342 :link '(info-link "(ido) Customization"))
343
344 ;;;###autoload
345 (defcustom ido-mode nil
346 "Determines for which buffer/file Ido should be enabled.
347 The following values are possible:
348 - `buffer': Turn only on ido buffer behavior (switching, killing,
349 displaying...)
350 - `file': Turn only on ido file behavior (finding, writing, inserting...)
351 - `both': Turn on ido buffer and file behavior.
352 - nil: Turn off any ido switching.
353
354 Setting this variable directly does not take effect;
355 use either \\[customize] or the function `ido-mode'."
356 :set #'(lambda (_symbol value)
357 (ido-mode value))
358 :initialize 'custom-initialize-default
359 :require 'ido
360 :link '(emacs-commentary-link "ido.el")
361 :set-after '(ido-save-directory-list-file
362 ;; This will clear ido-unc-hosts-cache, so set it
363 ;; before loading history file.
364 ido-unc-hosts)
365 :type '(choice (const :tag "Turn on only buffer" buffer)
366 (const :tag "Turn on only file" file)
367 (const :tag "Turn on both buffer and file" both)
368 (const :tag "Switch off all" nil))
369 :group 'ido)
370
371 (defcustom ido-case-fold case-fold-search
372 "Non-nil if searching of buffer and file names should ignore case."
373 :type 'boolean
374 :group 'ido)
375
376 (defcustom ido-ignore-buffers
377 '("\\` ")
378 "List of regexps or functions matching buffer names to ignore.
379 For example, traditional behavior is not to list buffers whose names begin
380 with a space, for which the regexp is `\\` '. See the source file for
381 example functions that filter buffer names."
382 :type '(repeat (choice regexp function))
383 :group 'ido)
384
385 (defcustom ido-ignore-files
386 '("\\`CVS/" "\\`#" "\\`.#" "\\`\\.\\./" "\\`\\./")
387 "List of regexps or functions matching file names to ignore.
388 For example, traditional behavior is not to list files whose names begin
389 with a #, for which the regexp is `\\`#'. See the source file for
390 example functions that filter filenames."
391 :type '(repeat (choice regexp function))
392 :group 'ido)
393
394 (defcustom ido-ignore-extensions t
395 "Non-nil means ignore files in `completion-ignored-extensions' list."
396 :type 'boolean
397 :group 'ido)
398
399 (defcustom ido-show-dot-for-dired nil
400 "Non-nil means to always put . as the first item in file name lists.
401 This allows the current directory to be opened immediately with `dired'."
402 :type 'boolean
403 :group 'ido)
404
405 (defcustom ido-file-extensions-order nil
406 "List of file extensions specifying preferred order of file selections.
407 Each element is either a string with `.' as the first char, an empty
408 string matching files without extension, or t which is the default order
409 for files with an unlisted file extension."
410 :type '(repeat (choice string
411 (const :tag "Default order" t)))
412 :group 'ido)
413
414 (defcustom ido-ignore-directories
415 '("\\`CVS/" "\\`\\.\\./" "\\`\\./")
416 "List of regexps or functions matching sub-directory names to ignore."
417 :type '(repeat (choice regexp function))
418 :group 'ido)
419
420 (defcustom ido-ignore-directories-merge nil
421 "List of regexps or functions matching directory names to ignore during merge.
422 Directory names matched by one of the regexps in this list are not inserted
423 in merged file and directory lists."
424 :type '(repeat (choice regexp function))
425 :group 'ido)
426
427 ;; Examples for setting the value of ido-ignore-buffers
428 ;;(defun ido-ignore-c-mode (name)
429 ;; "Ignore all c mode buffers -- example function for ido."
430 ;; (with-current-buffer name
431 ;; (derived-mode-p 'c-mode)))
432 ;;
433 ;;(setq ido-ignore-buffers '("^ " ido-ignore-c-mode))
434
435 ;; Examples for setting the value of ido-ignore-files
436 ;;(setq ido-ignore-files '("^ " "\\.c\\'" "\\.h\\'"))
437
438 (defcustom ido-default-file-method 'raise-frame
439 "How to visit a new file when using `ido-find-file'.
440 Possible values:
441 `selected-window' Show new file in selected window
442 `other-window' Show new file in another window (same frame)
443 `display' Display file in another window without selecting to it
444 `other-frame' Show new file in another frame
445 `maybe-frame' If a file is visible in another frame, prompt to ask if you
446 you want to see the file in the same window of the current
447 frame or in the other frame
448 `raise-frame' If a file is visible in another frame, raise that
449 frame; otherwise, visit the file in the same window"
450 :type '(choice (const :tag "Visit in selected window" selected-window)
451 (const :tag "Visit in other window" other-window)
452 (const :tag "Display (no select) in other window" display)
453 (const :tag "Visit in other frame" other-frame)
454 (const :tag "Ask to visit in other frame" maybe-frame)
455 (const :tag "Raise frame if already visited" raise-frame))
456 :group 'ido)
457
458 (defcustom ido-default-buffer-method 'raise-frame
459 "How to switch to new buffer when using `ido-switch-buffer'.
460 See `ido-default-file-method' for details."
461 :type '(choice (const :tag "Show in selected window" selected-window)
462 (const :tag "Show in other window" other-window)
463 (const :tag "Display (no select) in other window" display)
464 (const :tag "Show in other frame" other-frame)
465 (const :tag "Ask to show in other frame" maybe-frame)
466 (const :tag "Raise frame if already shown" raise-frame))
467 :group 'ido)
468
469 (defcustom ido-enable-flex-matching nil
470 "Non-nil means that `ido' will do flexible string matching.
471 Flexible matching means that if the entered string does not
472 match any item, any item containing the entered characters
473 in the given sequence will match."
474 :type 'boolean
475 :group 'ido)
476
477
478 (defcustom ido-enable-regexp nil
479 "Non-nil means that `ido' will do regexp matching.
480 Value can be toggled within `ido' using `ido-toggle-regexp'."
481 :type 'boolean
482 :group 'ido)
483
484 (defcustom ido-enable-prefix nil
485 "Non-nil means only match if the entered text is a prefix of file name.
486 This behavior is like the standard Emacs completion.
487 If nil, match if the entered text is an arbitrary substring.
488 Value can be toggled within `ido' using `ido-toggle-prefix'."
489 :type 'boolean
490 :group 'ido)
491
492 (defcustom ido-enable-dot-prefix nil
493 "Non-nil means to match leading dot as prefix.
494 I.e. hidden files and buffers will match only if you type a dot
495 as first char even if `ido-enable-prefix' is nil."
496 :type 'boolean
497 :group 'ido)
498
499 ;; See http://debbugs.gnu.org/2042 for more info.
500 (defcustom ido-buffer-disable-smart-matches t
501 "Non-nil means not to re-order matches for buffer switching.
502 By default, ido arranges matches in the following order:
503
504 full-matches > suffix matches > prefix matches > remaining matches
505
506 which can get in the way for buffer switching."
507 :version "24.3"
508 :type 'boolean
509 :group 'ido)
510
511 (defcustom ido-confirm-unique-completion nil
512 "Non-nil means that even a unique completion must be confirmed.
513 This means that \\[ido-complete] must always be followed by \\[ido-exit-minibuffer]
514 even when there is only one unique completion."
515 :type 'boolean
516 :group 'ido)
517
518 (defcustom ido-cannot-complete-command 'ido-completion-help
519 "Command run when `ido-complete' can't complete any more.
520 The most useful values are `ido-completion-help', which pops up a
521 window with completion alternatives, or `ido-next-match' or
522 `ido-prev-match', which cycle the buffer list."
523 :type 'function
524 :group 'ido)
525
526
527 (defcustom ido-record-commands t
528 "Non-nil means that Ido will record commands in command history.
529 Note that the non-Ido equivalent command is recorded."
530 :type 'boolean
531 :group 'ido)
532
533 (defcustom ido-max-prospects 12
534 "Upper limit of the prospect list if non-zero.
535 Zero means no limit for the prospect list.
536 For a long list of prospects, building the full list for the
537 minibuffer can take a non-negligible amount of time; setting this
538 variable reduces that time."
539 :type 'integer
540 :group 'ido)
541
542 (defcustom ido-max-file-prompt-width 0.35
543 "Upper limit of the prompt string.
544 If value is an integer, it specifies the number of characters of
545 the string.
546 If value is a floating point number, it specifies a fraction of
547 the frame width."
548 :type '(choice
549 (integer :tag "Characters" :value 20)
550 (restricted-sexp :tag "Fraction of frame width"
551 :value 0.35
552 :match-alternatives (ido-fractionp)))
553 :group 'ido)
554
555 (defcustom ido-max-window-height nil
556 "Non-nil specifies a value to override `max-mini-window-height'."
557 :type '(choice
558 (const :tag "Don't override" nil)
559 (integer :tag "Number of lines" :value 1)
560 (restricted-sexp
561 :tag "Fraction of window height"
562 :value 0.25
563 :match-alternatives (ido-fractionp)))
564 :group 'ido)
565
566 (defcustom ido-enable-last-directory-history t
567 "Non-nil means that `ido' will remember latest selected directory names.
568 See `ido-last-directory-list' and `ido-save-directory-list-file'."
569 :type 'boolean
570 :group 'ido)
571
572 (defcustom ido-max-work-directory-list 50
573 "Maximum number of working directories to record.
574 This is the list of directories where files have most recently been opened.
575 See `ido-work-directory-list' and `ido-save-directory-list-file'."
576 :type 'integer
577 :group 'ido)
578
579 (defcustom ido-work-directory-list-ignore-regexps nil
580 "List of regexps matching directories which should not be recorded.
581 Directory names matched by one of the regexps in this list are not inserted in
582 the `ido-work-directory-list' list."
583 :type '(repeat regexp)
584 :group 'ido)
585
586
587 (defcustom ido-use-filename-at-point nil
588 "Non-nil means that ido shall look for a filename at point.
589 May use `ffap-guesser' to guess whether text at point is a filename.
590 If found, use that as the starting point for filename selection."
591 :type '(choice
592 (const :tag "Disabled" nil)
593 (const :tag "Guess filename" guess)
594 (other :tag "Use literal filename" t))
595 :group 'ido)
596
597
598 (defcustom ido-use-url-at-point nil
599 "Non-nil means that ido shall look for a URL at point.
600 If found, call `find-file-at-point' to visit it."
601 :type 'boolean
602 :group 'ido)
603
604
605 (defcustom ido-enable-tramp-completion t
606 "Non-nil means that ido shall perform tramp method and server name completion.
607 A tramp file name uses the following syntax: /method:user@host:filename."
608 :type 'boolean
609 :group 'ido)
610
611 (defcustom ido-record-ftp-work-directories t
612 "Non-nil means record ftp file names in the work directory list."
613 :type 'boolean
614 :group 'ido)
615
616 (defcustom ido-merge-ftp-work-directories nil
617 "If nil, merging ignores ftp file names in the work directory list."
618 :type 'boolean
619 :group 'ido)
620
621 (defcustom ido-cache-ftp-work-directory-time 1.0
622 "Maximum time to cache contents of an ftp directory (in hours).
623 \\<ido-file-completion-map>
624 Use \\[ido-reread-directory] in prompt to refresh list.
625 If zero, ftp directories are not cached."
626 :type 'number
627 :group 'ido)
628
629 (defcustom ido-slow-ftp-hosts nil
630 "List of slow ftp hosts where ido prompting should not be used.
631 If an ftp host is on this list, ido automatically switches to the non-ido
632 equivalent function, e.g. `find-file' rather than `ido-find-file'."
633 :type '(repeat string)
634 :group 'ido)
635
636 (defcustom ido-slow-ftp-host-regexps nil
637 "List of regexps matching slow ftp hosts (see `ido-slow-ftp-hosts')."
638 :type '(repeat regexp)
639 :group 'ido)
640
641 (defvar ido-unc-hosts-cache t
642 "Cached value from the function `ido-unc-hosts'.")
643
644 (defcustom ido-unc-hosts nil
645 "List of known UNC host names to complete after initial //.
646 If value is a function, that function is called to search network for
647 hosts on first use of UNC path."
648 :type '(choice (repeat :tag "List of UNC host names" string)
649 (function-item :tag "Use `NET VIEW'"
650 :value ido-unc-hosts-net-view)
651 (function :tag "Your own function"))
652 :set #'(lambda (symbol value)
653 (set symbol value)
654 (setq ido-unc-hosts-cache t))
655 :group 'ido)
656
657 (defcustom ido-downcase-unc-hosts t
658 "Non-nil if UNC host names should be downcased."
659 :type 'boolean
660 :group 'ido)
661
662 (defcustom ido-ignore-unc-host-regexps nil
663 "List of regexps matching UNC hosts to ignore.
664 Case is ignored if `ido-downcase-unc-hosts' is set."
665 :type '(repeat regexp)
666 :group 'ido)
667
668 (defcustom ido-cache-unc-host-shares-time 8.0
669 "Maximum time to cache shares of an UNC host (in hours).
670 \\<ido-file-completion-map>
671 Use \\[ido-reread-directory] in prompt to refresh list.
672 If zero, UNC host shares are not cached."
673 :type 'number
674 :group 'ido)
675
676 (defcustom ido-max-work-file-list 10
677 "Maximum number of names of recently opened files to record.
678 This is the list of the file names (sans directory) which have most recently
679 been opened. See `ido-work-file-list' and `ido-save-directory-list-file'."
680 :type 'integer
681 :group 'ido)
682
683 (defcustom ido-work-directory-match-only t
684 "Non-nil means to skip non-matching directories in the directory history.
685 When some text is already entered at the `ido-find-file' prompt, using
686 \\[ido-prev-work-directory] or \\[ido-next-work-directory] will skip directories
687 without any matching entries."
688 :type 'boolean
689 :group 'ido)
690
691 (defcustom ido-auto-merge-work-directories-length 0
692 "Automatically switch to merged work directories during file name input.
693 The value is number of characters to type before switching to merged mode.
694 If zero, the switch happens when no matches are found in the current directory.
695 Automatic merging is disabled if the value is negative."
696 :type 'integer
697 :group 'ido)
698
699 (defcustom ido-auto-merge-delay-time 0.70
700 "Delay in seconds to wait for more input before doing auto merge."
701 :type 'number
702 :group 'ido)
703
704 (defcustom ido-auto-merge-inhibit-characters-regexp "[][*?~]"
705 "Regexp matching characters which should inhibit automatic merging.
706 When a (partial) file name matches this regexp, merging is inhibited."
707 :type 'regexp
708 :group 'ido)
709
710 (defcustom ido-merged-indicator "^"
711 "The string appended to first choice if it has multiple directory choices."
712 :type 'string
713 :group 'ido)
714
715 (defcustom ido-max-dir-file-cache 100
716 "Maximum number of working directories to be cached.
717 \\<ido-file-completion-map>
718 This is the size of the cache of `file-name-all-completions' results.
719 Each cache entry is time stamped with the modification time of the
720 directory. Some systems, like Windows, have unreliable directory
721 modification times, so you may choose to disable caching on such
722 systems, or explicitly refresh the cache contents using the command
723 `ido-reread-directory' command (\\[ido-reread-directory]) in the minibuffer.
724 See also `ido-dir-file-cache' and `ido-save-directory-list-file'."
725 :type 'integer
726 :group 'ido)
727
728 (defcustom ido-max-directory-size nil
729 "Maximum size (in bytes) for directories to use ido completion.
730 \\<ido-completion-map>
731 If you enter a directory with a size larger than this size, ido will
732 not provide the normal completion. To show the completions, use \\[ido-toggle-ignore]."
733 :type '(choice (const :tag "No limit" nil)
734 (integer :tag "Size in bytes" 30000))
735 :group 'ido)
736
737 (defcustom ido-rotate-file-list-default nil
738 "Non-nil means that `ido' will always rotate file list to get default in front."
739 :type 'boolean
740 :group 'ido)
741
742 (defcustom ido-enter-matching-directory 'only
743 "Additional methods to enter sub-directory of first/only matching item.
744 If value is 'first, enter first matching sub-directory when typing a slash.
745 If value is 'only, typing a slash only enters the sub-directory if it is
746 the only matching item.
747 If value is t, automatically enter a sub-directory when it is the only
748 matching item, even without typing a slash."
749 :type '(choice (const :tag "Never" nil)
750 (const :tag "Slash enters first directory" first)
751 (const :tag "Slash enters first and only directory" only)
752 (other :tag "Always enter unique directory" t))
753 :group 'ido)
754
755 (defcustom ido-create-new-buffer 'prompt
756 "Specify whether a new buffer is created if no buffer matches substring.
757 Choices are 'always to create new buffers unconditionally, 'prompt to
758 ask user whether to create buffer, or 'never to never create new buffer."
759 :type '(choice (const always)
760 (const prompt)
761 (const never))
762 :group 'ido)
763
764 (defcustom ido-setup-hook nil
765 "Hook run after the ido variables and keymap have been setup.
766 The dynamic variable `ido-cur-item' contains the current type of item that
767 is read by ido; possible values are file, dir, buffer, and list.
768 Additional keys can be defined in `ido-completion-map'."
769 :type 'hook
770 :group 'ido)
771
772 (defcustom ido-separator nil
773 "String used by ido to separate the alternatives in the minibuffer.
774 Obsolete. Set 3rd element of `ido-decorations' instead."
775 :type '(choice string (const nil))
776 :group 'ido)
777
778 (defcustom ido-decorations '( "{" "}" " | " " | ..." "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]" " [Confirm]")
779 "List of strings used by ido to display the alternatives in the minibuffer.
780 There are between 11 and 13 elements in this list:
781 1st and 2nd elements are used as brackets around the prospect list,
782 3rd element is the separator between prospects (ignored if
783 `ido-separator' is set),
784 4th element is the string inserted at the end of a truncated list of prospects,
785 5th and 6th elements are used as brackets around the common match string which
786 can be completed using TAB,
787 7th element is the string displayed when there are no matches, and
788 8th element is displayed if there is a single match (and faces are not used),
789 9th element is displayed when the current directory is non-readable,
790 10th element is displayed when directory exceeds `ido-max-directory-size',
791 11th element is displayed to confirm creating new file or buffer.
792 12th and 13th elements (if present) are used as brackets around the sole
793 remaining completion. If absent, elements 5 and 6 are used instead."
794 :type '(repeat string)
795 :group 'ido)
796
797 (defcustom ido-use-virtual-buffers nil
798 "Specify how virtual buffers should be used.
799 The value can be one of the following:
800
801 nil: No virtual buffers are used.
802 auto: Use virtual buffers when the current input matches no
803 existing buffers.
804 t: Always use virtual buffers.
805
806 Essentially it works as follows: Say you are visiting a file and
807 the buffer gets cleaned up by midnight.el. Later, you want to
808 switch to that buffer, but find it's no longer open. With virtual
809 buffers enabled, the buffer name stays in the buffer list (using
810 the `ido-virtual' face, and always at the end), and if you select
811 it, it opens the file back up again. This allows you to think
812 less about whether recently opened files are still open or not.
813 Most of the time you can quit Emacs, restart, and then switch to
814 a file buffer that was previously open as if it still were. This
815 feature relies upon the `recentf' package, which will be enabled
816 if this variable is configured to a non-nil value."
817 :version "24.4"
818 :type '(choice (const :tag "Always" t)
819 (const :tag "Automatic" auto)
820 (const :tag "Never" nil))
821 :group 'ido)
822
823 (defcustom ido-use-faces t
824 "Non-nil means use ido faces to highlighting first match, only match and
825 subdirs in the alternatives."
826 :type 'boolean
827 :group 'ido)
828
829 (defface ido-first-match '((t :weight bold))
830 "Face used by ido for highlighting first match."
831 :group 'ido)
832
833 (defface ido-only-match '((((class color))
834 :foreground "ForestGreen")
835 (t :slant italic))
836 "Face used by ido for highlighting only match."
837 :group 'ido)
838
839 (defface ido-subdir '((((min-colors 88) (class color))
840 :foreground "red1")
841 (((class color))
842 :foreground "red")
843 (t :underline t))
844 "Face used by ido for highlighting subdirs in the alternatives."
845 :group 'ido)
846
847 (defface ido-virtual '((t :inherit font-lock-builtin-face))
848 "Face used by ido for matching virtual buffer names."
849 :version "24.1"
850 :group 'ido)
851
852 (defface ido-indicator '((((min-colors 88) (class color))
853 :foreground "yellow1" :background "red1" :width condensed)
854 (((class color))
855 :foreground "yellow" :background "red" :width condensed)
856 (t :inverse-video t))
857 "Face used by ido for highlighting its indicators."
858 :group 'ido)
859
860 (defface ido-incomplete-regexp
861 '((t :inherit font-lock-warning-face))
862 "Ido face for indicating incomplete regexps."
863 :group 'ido)
864
865 (defcustom ido-make-file-list-hook nil
866 "List of functions to run when the list of matching files is created.
867 Each function on the list may modify the dynamically bound variable
868 `ido-temp-list' which contains the current list of matching files."
869 :type 'hook
870 :group 'ido)
871
872 (defcustom ido-make-dir-list-hook nil
873 "List of functions to run when the list of matching directories is created.
874 Each function on the list may modify the dynamically bound variable
875 `ido-temp-list' which contains the current list of matching directories."
876 :type 'hook
877 :group 'ido)
878
879 (defcustom ido-make-buffer-list-hook nil
880 "List of functions to run when the list of matching buffers is created.
881 Each function on the list may modify the dynamically bound variable
882 `ido-temp-list' which contains the current list of matching buffer names."
883 :type 'hook
884 :group 'ido)
885
886 (defcustom ido-rewrite-file-prompt-functions nil
887 "List of functions to run when the find-file prompt is created.
888 Each function on the list may modify the following dynamically bound
889 variables:
890 dirname - the (abbreviated) directory name
891 to be modified by the hook functions
892 max-width - the max width of the resulting dirname; nil means no limit
893 prompt - the basic prompt (e.g. \"Find File: \")
894 literal - the string shown if doing \"literal\" find; set to nil to omit
895 vc-off - the string shown if version control is inhibited; set to nil to omit
896 prefix - either nil or a fixed prefix for the dirname
897
898 The following variables are available, but should not be changed:
899 `ido-current-directory' - the unabbreviated directory name
900 item - equals `file' or `dir' depending on the current mode."
901 :type 'hook
902 :group 'ido)
903
904 (defvar ido-rewrite-file-prompt-rules nil
905 "Alist of rewriting rules for directory names in ido prompts.
906 A list of elements of the form (FROM . TO) or (FROM . FUNC), each
907 meaning to rewrite the directory name if matched by FROM by either
908 substituting the matched string by TO or calling the function FUNC
909 with the current directory name as its only argument and using the
910 return value as the new directory name. In addition, each FUNC may
911 also modify the dynamic variables described for the variable
912 `ido-rewrite-file-prompt-functions'.")
913
914 (defcustom ido-completion-buffer "*Ido Completions*"
915 "Name of completion buffer used by ido.
916 Set to nil to disable completion buffers popping up."
917 :type 'string
918 :group 'ido)
919
920 (defcustom ido-completion-buffer-all-completions nil
921 "Non-nil means to show all completions in completion buffer.
922 Otherwise, only the current list of matches is shown."
923 :type 'boolean
924 :group 'ido)
925
926 (defcustom ido-all-frames 'visible
927 "Argument to pass to `walk-windows' when Ido is finding buffers.
928 See documentation of `walk-windows' for useful values."
929 :type '(choice (const :tag "Selected frame only" nil)
930 (const :tag "All existing frames" t)
931 (const :tag "All visible frames" visible)
932 (const :tag "All frames on this terminal" 0))
933 :group 'ido)
934
935 (defcustom ido-minibuffer-setup-hook nil
936 "Ido-specific customization of minibuffer setup.
937
938 This hook is run during minibuffer setup if `ido' is active.
939 It is intended for use in customizing ido for interoperation
940 with other packages. For instance:
941
942 \(add-hook 'ido-minibuffer-setup-hook
943 \(function
944 \(lambda ()
945 \(make-local-variable 'max-mini-window-height)
946 \(setq max-mini-window-height 3))))
947
948 will constrain Emacs to a maximum minibuffer height of 3 lines when
949 ido is running. Copied from `icomplete-minibuffer-setup-hook'."
950 :type 'hook
951 :group 'ido)
952
953 (defcustom ido-save-directory-list-file
954 (locate-user-emacs-file "ido.last" ".ido.last")
955 "File in which the ido state is saved between invocations.
956 Variables stored are: `ido-last-directory-list', `ido-work-directory-list',
957 `ido-work-file-list', and `ido-dir-file-cache'.
958 Must be set before enabling ido mode."
959 :version "24.4" ; added locate-user-emacs-file
960 :type 'string
961 :group 'ido)
962
963 (defcustom ido-read-file-name-as-directory-commands '()
964 "List of commands which uses `read-file-name' to read a directory name.
965 When `ido-everywhere' is non-nil, the commands in this list will read
966 the directory using `ido-read-directory-name'."
967 :type '(repeat symbol)
968 :group 'ido)
969
970 (defcustom ido-read-file-name-non-ido '()
971 "List of commands which shall not read file names the ido way.
972 When `ido-everywhere' is non-nil, the commands in this list will read
973 the file name using normal `read-file-name' style."
974 :type '(repeat symbol)
975 :group 'ido)
976
977 (defcustom ido-before-fallback-functions '()
978 "List of functions to call before calling a fallback command.
979 The fallback command is passed as an argument to the functions."
980 :type 'hook
981 :group 'ido)
982
983 ;;; Internal Variables
984
985 ;; Persistent variables
986
987 (defvar ido-completion-map nil
988 "Currently active keymap for ido commands.")
989
990 (defvar ido-common-completion-map nil
991 "Keymap for all ido commands.")
992
993 (defvar ido-file-completion-map nil
994 "Keymap for ido file commands.")
995
996 (defvar ido-file-dir-completion-map nil
997 "Keymap for ido file and directory commands.")
998
999 (defvar ido-buffer-completion-map nil
1000 "Keymap for ido buffer commands.")
1001
1002 (defvar ido-file-history nil
1003 "History of files selected using `ido-find-file'.")
1004
1005 (defvar ido-buffer-history nil
1006 "History of buffers selected using `ido-switch-buffer'.")
1007
1008 (defvar ido-last-directory-list nil
1009 "List of last selected directory names.
1010 See `ido-enable-last-directory-history' for details.")
1011
1012 (defvar ido-work-directory-list nil
1013 "List of actual working directory names.
1014 The current directory is inserted at the front of this list whenever a
1015 file is opened with `ido-find-file' and family.")
1016
1017 (defvar ido-work-file-list nil
1018 "List of actual work file names.
1019 Opening a file with `ido-find-file' and similar functions
1020 inserts the current file name (relative to its containing directory)
1021 at the front of this list.")
1022
1023 (defvar ido-dir-file-cache nil
1024 "List of `file-name-all-completions' results.
1025 Each element in the list is of the form (DIR (MTIME) FILE...).")
1026
1027 (defvar ido-ignore-item-temp-list nil
1028 "List of items to ignore in current ido invocation.
1029 Intended to be let-bound by functions which call ido repeatedly.
1030 Should never be set permanently.")
1031
1032 ;; Temporary storage
1033
1034 (defvar ido-eoinput 1
1035 "Point where minibuffer input ends and completion info begins.
1036 Copied from `icomplete-eoinput'.")
1037 (make-variable-buffer-local 'ido-eoinput)
1038
1039 (defvar ido-common-match-string nil
1040 "Stores the string that is common to all matching files.")
1041
1042 (defvar ido-rescan nil
1043 "Non-nil means we need to regenerate the list of matching items.")
1044
1045 (defvar ido-rotate nil
1046 "Non-nil means we are rotating list of matches.")
1047
1048 (defvar ido-text nil
1049 "Stores the users string as it is typed in.")
1050
1051 (defvar ido-text-init nil
1052 "The initial string for the users string it is typed in.")
1053
1054 (defvar ido-input-stack nil
1055 "Stores the users strings when user hits M-b/M-f.")
1056
1057 (defvar ido-matches nil
1058 "List of files currently matching `ido-text'.")
1059
1060 (defvar ido-report-no-match t
1061 "Report [No Match] when no completions matches `ido-text'.")
1062
1063 (defvar ido-exit nil
1064 "Flag to monitor how `ido-find-file' exits.
1065 If equal to `takeprompt', we use the prompt as the file name to be
1066 selected.")
1067
1068 (defvar ido-current-directory nil
1069 "Current directory for `ido-find-file'.")
1070
1071 (defvar ido-auto-merge-timer nil
1072 "Delay timer for auto merge.")
1073
1074 (defvar ido-use-mycompletion-depth 0
1075 "Non-nil means use `ido' completion feedback.
1076 Is set by ido functions to the current `minibuffer-depth',
1077 so that it doesn't interfere with other minibuffer usage.")
1078
1079 (defvar ido-incomplete-regexp nil
1080 "Non-nil if an incomplete regexp is entered.")
1081
1082 (defvar ido-initial-position nil
1083 "Non-nil means to explicitly cursor on entry to minibuffer.
1084 Value is an integer which is number of chars to right of prompt.")
1085
1086 (defvar ido-virtual-buffers nil
1087 "List of virtual buffers, that is, past visited files.
1088 This is a copy of `recentf-list', pared down and with faces applied.
1089 Only used if `ido-use-virtual-buffers' is non-nil.")
1090
1091 ;;; Variables with dynamic bindings.
1092 ;;; Declared here to keep the byte compiler quiet.
1093
1094 ;; Stores the current ido item type ('file, 'dir, 'buffer, or 'list).
1095 (defvar ido-cur-item)
1096
1097 ;;; Stores the current default item
1098 (defvar ido-default-item)
1099
1100 ;; Stores the current list of items that will be searched through.
1101 ;; The list is ordered, so that the most interesting item comes first,
1102 ;; although by default, the files visible in the current frame are put
1103 ;; at the end of the list. Created by `ido-make-item-list'.
1104 (defvar ido-cur-list)
1105
1106 ;; Stores the choice list for ido-completing-read
1107 (defvar ido-choice-list)
1108
1109 ;; Stores the list of items which are ignored when building
1110 ;; `ido-cur-list'. It is in no specific order.
1111 (defvar ido-ignored-list)
1112
1113 ;; Remember if current directory is non-readable (so we cannot do completion).
1114 (defvar ido-directory-nonreadable)
1115
1116 ;; Remember if current directory is 'huge' (so we don't want to do completion).
1117 (defvar ido-directory-too-big)
1118
1119 ;; Keep current item list if non-nil.
1120 (defvar ido-keep-item-list)
1121
1122 ;; Process ido-ignore-* lists.
1123 (defvar ido-process-ignore-lists)
1124
1125 ;; Don't process ido-ignore- lists once.
1126 (defvar ido-process-ignore-lists-inhibit)
1127
1128 ;; Is ido using virtual buffers?
1129 (defvar ido-enable-virtual-buffers)
1130
1131 ;; Buffer from which ido was entered.
1132 (defvar ido-entry-buffer)
1133
1134 ;; Non-nil if matching file must be selected.
1135 (defvar ido-require-match)
1136
1137 ;; Non-nil if we should add [confirm] to prompt
1138 (defvar ido-show-confirm-message)
1139
1140 ;; Stores a temporary version of the file list being created.
1141 (defvar ido-temp-list)
1142
1143 ;; Non-nil if default list element should be rotated into place.
1144 (defvar ido-rotate-temp)
1145
1146 ;; Stores current index in ido-work-directory-list.
1147 (defvar ido-work-directory-index)
1148
1149 ;; Stores current index in ido-work-file-list.
1150 (defvar ido-work-file-index)
1151
1152 ;; Set when merged work directory list is in use.
1153 (defvar ido-use-merged-list)
1154
1155 ;; Set when merged work directory list not yet built.
1156 (defvar ido-try-merged-list)
1157
1158 ;; Saved state prior to last work directory merge.
1159 ;; Value is a list (ido-text dir cur-list ignored-list matches).
1160 (defvar ido-pre-merge-state)
1161
1162 ;; Original value of vc-handled-backends for use in ido-toggle-vc.
1163 (defvar ido-saved-vc-hb)
1164
1165 ;; Stores temporary state of literal find file.
1166 (defvar ido-find-literal)
1167
1168 ;; Set to 'ignore to inhibit switching between find-file/switch-buffer.
1169 (defvar ido-context-switch-command)
1170
1171 ;; Dynamically bound in ido-read-internal.
1172 (defvar ido-completing-read)
1173
1174 ;;; FUNCTIONS
1175
1176 (defun ido-active (&optional merge)
1177 (if merge
1178 ido-use-merged-list
1179 (and (boundp 'ido-completing-read)
1180 (or (featurep 'xemacs)
1181 (= ido-use-mycompletion-depth (minibuffer-depth))))))
1182
1183 (defvar ido-trace-enable nil)
1184
1185 (defun ido-trace (p &optional s retval)
1186 (if ido-trace-enable
1187 (let ((b (get-buffer-create " *IDO Trace*"))
1188 (deactivate-mark deactivate-mark))
1189 (save-excursion
1190 (save-restriction
1191 (set-buffer b)
1192 (insert p ": " (if (stringp s) s (format "%S" s)) "\n")))))
1193 retval)
1194
1195 (defun ido-toggle-trace (arg)
1196 (interactive "P")
1197 (setq ido-trace-enable (or arg (not ido-trace-enable)))
1198 (if ido-trace-enable
1199 (message "IDO trace on"))
1200 (let ((b (get-buffer " *IDO Trace*")))
1201 (if b
1202 (if ido-trace-enable
1203 (kill-buffer b)
1204 (pop-to-buffer b t t)
1205 (setq truncate-lines t)))))
1206
1207 (defun ido-local-file-exists-p (file)
1208 "Tell if FILE exists locally."
1209 (let (file-name-handler-alist)
1210 (file-exists-p file)))
1211
1212 (defun ido-unc-hosts (&optional query)
1213 "Return list of UNC host names."
1214 (let ((hosts
1215 (cond
1216 ((listp ido-unc-hosts)
1217 ido-unc-hosts) ;; static list or nil
1218 ((listp ido-unc-hosts-cache)
1219 ido-unc-hosts-cache) ;; result of net search
1220 ((and query (fboundp ido-unc-hosts))
1221 (message (propertize "Searching for UNC hosts..." 'face 'highlight))
1222 (setq ido-unc-hosts-cache (funcall ido-unc-hosts))
1223 (message nil)
1224 ido-unc-hosts-cache)
1225 (query
1226 (setq ido-unc-hosts-cache nil))
1227 (t (fboundp ido-unc-hosts)))))
1228 (when query
1229 (let ((case-fold-search ido-downcase-unc-hosts)
1230 res host re-list re)
1231 (while hosts
1232 (setq host (car hosts)
1233 hosts (cdr hosts)
1234 re-list (and ido-process-ignore-lists
1235 ido-ignore-unc-host-regexps))
1236 (while re-list
1237 (setq re (car re-list)
1238 re-list (cdr re-list))
1239 (if (string-match re host)
1240 (setq re-list nil
1241 host nil)))
1242 (when host
1243 (when ido-downcase-unc-hosts
1244 (setq host (downcase host)))
1245 (setq res (cons host res))))
1246 (setq hosts (sort res #'string<))))
1247 hosts))
1248
1249 (defun ido-unc-hosts-net-view ()
1250 "Query network for list of UNC host names using `NET VIEW'."
1251 (let (hosts)
1252 (with-temp-buffer
1253 (shell-command "net view" t)
1254 (goto-char (point-min))
1255 (while (re-search-forward "^\\\\\\\\\\([[:graph:]]+\\)" nil t)
1256 (setq hosts (cons (match-string 1) hosts))))
1257 hosts))
1258
1259 (defun ido-is-tramp-root (&optional dir)
1260 (and ido-enable-tramp-completion
1261 (string-match "\\`/[^/]+[@:]\\'"
1262 (or dir ido-current-directory))))
1263
1264 (defun ido-is-unc-root (&optional dir)
1265 (and (ido-unc-hosts)
1266 (string-equal "//"
1267 (or dir ido-current-directory))))
1268
1269 (defun ido-is-unc-host (&optional dir)
1270 (and (ido-unc-hosts)
1271 (string-match "\\`//[^/]+/\\'"
1272 (or dir ido-current-directory))))
1273
1274 (defun ido-is-root-directory (&optional dir)
1275 (setq dir (or dir ido-current-directory))
1276 (or
1277 (string-equal "/" dir)
1278 (and (memq system-type '(windows-nt ms-dos))
1279 (string-match "\\`[a-zA-Z]:[/\\]\\'" dir))
1280 (if ido-enable-tramp-completion
1281 (ido-is-tramp-root dir)
1282 (string-match "\\`/[^:/][^:/]+:\\'" dir))))
1283
1284 (defun ido-is-ftp-directory (&optional dir)
1285 (string-match
1286 (if ido-enable-tramp-completion
1287 "\\`/[^/:][^/:]+:" ;; like tramp-file-name-regexp-unified, but doesn't match single drive letters
1288 "\\`/[^/:][^/:]+:/")
1289 (or dir ido-current-directory)))
1290
1291 (defun ido-is-slow-ftp-host (&optional dir)
1292 (and (or ido-slow-ftp-hosts ido-slow-ftp-host-regexps)
1293 (setq dir (or dir ido-current-directory))
1294 ;; (featurep 'ange-ftp)
1295 ;; (ange-ftp-ftp-name dir)
1296 (string-match
1297 (if ido-enable-tramp-completion
1298 "\\`/\\([^/]+[@:]\\)*\\([^@/:][^@/:]+\\):"
1299 "\\`/\\([^/:]*@\\)?\\([^@/:][^@/:]+\\):/")
1300 dir)
1301 (let ((host (substring dir (match-beginning 2) (match-end 2))))
1302 (or (member host ido-slow-ftp-hosts)
1303 (let ((re ido-slow-ftp-host-regexps))
1304 (while (and re (not (string-match (car re) host)))
1305 (setq re (cdr re)))
1306 re)))))
1307
1308 (defun ido-time-stamp (&optional time)
1309 ;; Time is a floating point number (fractions of 1 hour)
1310 (setq time (or time (current-time)))
1311 (/ (+ (* (car time) 65536.0) (car (cdr time))) 3600.0))
1312
1313 (defun ido-cache-ftp-valid (&optional time)
1314 (and (numberp ido-cache-ftp-work-directory-time)
1315 (> ido-cache-ftp-work-directory-time 0)
1316 (or (not time)
1317 (< (- (ido-time-stamp) time) ido-cache-ftp-work-directory-time))))
1318
1319 (defun ido-cache-unc-valid (&optional time)
1320 (and (numberp ido-cache-unc-host-shares-time)
1321 (> ido-cache-unc-host-shares-time 0)
1322 (or (not time)
1323 (< (- (ido-time-stamp) time) ido-cache-unc-host-shares-time))))
1324
1325 (defun ido-may-cache-directory (&optional dir)
1326 (setq dir (or dir ido-current-directory))
1327 (cond
1328 ((and (ido-is-root-directory dir)
1329 (or ido-enable-tramp-completion
1330 (memq system-type '(windows-nt ms-dos))))
1331 nil)
1332 ((ido-is-unc-host dir)
1333 (ido-cache-unc-valid))
1334 ((ido-is-ftp-directory dir)
1335 (ido-cache-ftp-valid))
1336 ((ido-directory-too-big-p dir)
1337 nil)
1338 (t t)))
1339
1340 (defun ido-pp (list &optional sep)
1341 (let ((print-level nil) (eval-expression-print-level nil)
1342 (print-length nil) (eval-expression-print-length nil))
1343 (insert "\n;; ----- " (symbol-name list) " -----\n(\n ")
1344 (setq list (symbol-value list))
1345 (while list
1346 (let* ((elt (car list))
1347 (s (if (consp elt) (car elt) elt)))
1348 (if (and (stringp s) (= (length s) 0))
1349 (setq s nil))
1350 (if s
1351 (prin1 elt (current-buffer)))
1352 (if (and (setq list (cdr list)) s)
1353 (insert (or sep "\n ")))))
1354 (insert "\n)\n")))
1355
1356 (defun ido-save-history ()
1357 "Save ido history and cache information between sessions."
1358 (interactive)
1359 (when (and ido-last-directory-list ido-save-directory-list-file)
1360 (let ((buf (get-buffer-create " *ido session*"))
1361 (version-control 'never))
1362 (unwind-protect
1363 (with-current-buffer buf
1364 (erase-buffer)
1365 (insert ";;; -*- coding: utf-8 -*-\n")
1366 (setq buffer-file-coding-system 'utf-8)
1367 (ido-pp 'ido-last-directory-list)
1368 (ido-pp 'ido-work-directory-list)
1369 (ido-pp 'ido-work-file-list)
1370 (ido-pp 'ido-dir-file-cache "\n\n ")
1371 (if (listp ido-unc-hosts-cache)
1372 (ido-pp 'ido-unc-hosts-cache)
1373 (insert "\n;; ----- ido-unc-hosts-cache -----\nt\n"))
1374 (write-file ido-save-directory-list-file nil))
1375 (kill-buffer buf)))))
1376
1377 (defun ido-load-history (&optional arg)
1378 "Load ido history and cache information from previous session.
1379 With prefix argument, reload history unconditionally."
1380 (interactive "P")
1381 (if (or arg (and ido-save-directory-list-file (not ido-last-directory-list)))
1382 (let ((file (expand-file-name ido-save-directory-list-file))
1383 buf)
1384 (when (file-readable-p file)
1385 (setq buf (get-buffer-create " *ido session*"))
1386 (unwind-protect
1387 (with-current-buffer buf
1388 (erase-buffer)
1389 (insert-file-contents file)
1390 (condition-case nil
1391 (setq ido-last-directory-list (read (current-buffer))
1392 ido-work-directory-list (read (current-buffer))
1393 ido-work-file-list (read (current-buffer))
1394 ido-dir-file-cache (read (current-buffer))
1395 ido-unc-hosts-cache (read (current-buffer)))
1396 (error nil)))
1397 (kill-buffer buf)))))
1398 (ido-wash-history))
1399
1400 (defun ido-wash-history ()
1401 "Clean-up ido history and cache information.
1402 Removes badly formatted data and ignored directories."
1403 (interactive)
1404 ;; Check format of each of our lists, discard bogus elements
1405 (setq ido-last-directory-list
1406 (and (listp ido-last-directory-list)
1407 (let ((l ido-last-directory-list) r)
1408 (while l
1409 (if (and (consp (car l))
1410 (stringp (car (car l)))
1411 (stringp (cdr (car l))))
1412 (setq r (cons (car l) r)))
1413 (setq l (cdr l)))
1414 (nreverse r))))
1415 (setq ido-work-directory-list
1416 (and (listp ido-work-directory-list)
1417 (let ((l ido-work-directory-list) r)
1418 (while l
1419 (if (and (stringp (car l))
1420 (or ido-record-ftp-work-directories
1421 (not (ido-is-ftp-directory (car l)))))
1422 (setq r (cons (car l) r)))
1423 (setq l (cdr l)))
1424 (nreverse r))))
1425 (setq ido-work-file-list
1426 (and (listp ido-work-file-list)
1427 (let ((l ido-work-file-list) r)
1428 (while l
1429 (if (stringp (car l))
1430 (setq r (cons (car l) r)))
1431 (setq l (cdr l)))
1432 (nreverse r))))
1433 (setq ido-dir-file-cache
1434 (and (listp ido-dir-file-cache)
1435 (let ((l ido-dir-file-cache) r)
1436 (while l
1437 (if (and (listp (car l))
1438 (> (length (car l)) 2)
1439 (let ((dir (car (car l)))
1440 (time (car (cdr (car l))))
1441 (files (cdr (cdr (car l)))))
1442 (and
1443 (stringp dir)
1444 (consp time)
1445 (cond
1446 ((integerp (car time))
1447 (and (/= (car time) 0)
1448 (integerp (car (cdr time)))
1449 (/= (car (cdr time)) 0)
1450 (ido-may-cache-directory dir)))
1451 ((eq (car time) 'ftp)
1452 (and (numberp (cdr time))
1453 (ido-is-ftp-directory dir)
1454 (ido-cache-ftp-valid (cdr time))))
1455 ((eq (car time) 'unc)
1456 (and (numberp (cdr time))
1457 (ido-is-unc-host dir)
1458 (ido-cache-unc-valid (cdr time))))
1459 (t nil))
1460 (let ((s files) (ok t))
1461 (while s
1462 (if (stringp (car s))
1463 (setq s (cdr s))
1464 (setq s nil ok nil)))
1465 ok))))
1466 (setq r (cons (car l) r)))
1467 (setq l (cdr l)))
1468 (nreverse r))))
1469
1470 ;; Remove ignored directories from work directory list
1471 ;; according to ido-work-directory-list-ignore-regexps
1472 (if ido-work-directory-list
1473 (let ((dirs (reverse ido-work-directory-list)))
1474 (setq ido-work-directory-list nil)
1475 (while dirs
1476 (ido-record-work-directory (car dirs))
1477 (setq dirs (cdr dirs)))))
1478 ;; Get rid of text properties
1479 (let ((l ido-last-directory-list) e)
1480 (while l
1481 (setq e (car l) l (cdr l))
1482 (set-text-properties 0 (length (car e)) nil (car e))
1483 (set-text-properties 0 (length (cdr e)) nil (cdr e))))
1484 (let ((l ido-work-directory-list) e)
1485 (while l
1486 (setq e (car l) l (cdr l))
1487 (set-text-properties 0 (length e) nil e)))
1488 (let ((l ido-work-file-list) e)
1489 (while l
1490 (setq e (car l) l (cdr l))
1491 (set-text-properties 0 (length e) nil e)))
1492 (let ((l ido-dir-file-cache) e d)
1493 (while l
1494 (setq e (car l) l (cdr l))
1495 (if (listp e)
1496 (while e
1497 (setq d (car e) e (cdr e))
1498 (if (not (consp d))
1499 (set-text-properties 0 (length d) nil d)))))))
1500
1501
1502 (defun ido-kill-emacs-hook ()
1503 ;; ido kill emacs hook
1504 (ido-save-history))
1505
1506 (defun ido-common-initialization ()
1507 (ido-init-completion-maps)
1508 (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
1509 (add-hook 'choose-completion-string-functions 'ido-choose-completion-string))
1510
1511 (define-minor-mode ido-everywhere
1512 "Toggle use of Ido for all buffer/file reading.
1513 With a prefix argument ARG, enable this feature if ARG is
1514 positive, and disable it otherwise. If called from Lisp, enable
1515 the mode if ARG is omitted or nil."
1516 :global t
1517 :group 'ido
1518 (when (get 'ido-everywhere 'file)
1519 (setq read-file-name-function (car (get 'ido-everywhere 'file)))
1520 (put 'ido-everywhere 'file nil))
1521 (when (get 'ido-everywhere 'buffer)
1522 (setq read-buffer-function (car (get 'ido-everywhere 'buffer)))
1523 (put 'ido-everywhere 'buffer nil))
1524 (when ido-everywhere
1525 (when (memq ido-mode '(both file))
1526 (put 'ido-everywhere 'file (cons read-file-name-function nil))
1527 (setq read-file-name-function 'ido-read-file-name))
1528 (when (memq ido-mode '(both buffer))
1529 (put 'ido-everywhere 'buffer (cons read-buffer-function nil))
1530 (setq read-buffer-function 'ido-read-buffer))))
1531
1532 (defvar ido-minor-mode-map-entry nil)
1533
1534 ;;;###autoload
1535 (defun ido-mode (&optional arg)
1536 "Toggle ido mode on or off.
1537 With ARG, turn ido-mode on if arg is positive, off otherwise.
1538 Turning on ido-mode will remap (via a minor-mode keymap) the default
1539 keybindings for the `find-file' and `switch-to-buffer' families of
1540 commands to the ido versions of these functions.
1541 However, if ARG arg equals 'files, remap only commands for files, or
1542 if it equals 'buffers, remap only commands for buffer switching.
1543 This function also adds a hook to the minibuffer."
1544 (interactive "P")
1545 (setq ido-mode
1546 (cond
1547 ((null arg) (if ido-mode nil 'both))
1548 ((eq arg t) 'both)
1549 ((eq arg 'files) 'file)
1550 ((eq arg 'buffers) 'buffer)
1551 ((memq arg '(file buffer both)) arg)
1552 ((> (prefix-numeric-value arg) 0) 'both)
1553 (t nil)))
1554
1555 (ido-everywhere (if ido-everywhere 1 -1))
1556
1557 (when ido-mode
1558 (ido-common-initialization)
1559 (ido-load-history)
1560
1561 (add-hook 'kill-emacs-hook 'ido-kill-emacs-hook)
1562
1563 (let ((map (make-sparse-keymap)))
1564 (when (memq ido-mode '(file both))
1565 (define-key map [remap find-file] 'ido-find-file)
1566 (define-key map [remap find-file-read-only] 'ido-find-file-read-only)
1567 (define-key map [remap find-alternate-file] 'ido-find-alternate-file)
1568 (define-key map [remap write-file] 'ido-write-file)
1569 (define-key map [remap insert-file] 'ido-insert-file)
1570 (define-key map [remap list-directory] 'ido-list-directory)
1571 (define-key map [remap dired] 'ido-dired)
1572 (define-key map [remap find-file-other-window]
1573 'ido-find-file-other-window)
1574 (define-key map [remap find-file-read-only-other-window]
1575 'ido-find-file-read-only-other-window)
1576 (define-key map [remap find-file-other-frame]
1577 'ido-find-file-other-frame)
1578 (define-key map [remap find-file-read-only-other-frame]
1579 'ido-find-file-read-only-other-frame))
1580
1581 (when (memq ido-mode '(buffer both))
1582 (define-key map [remap switch-to-buffer] 'ido-switch-buffer)
1583 (define-key map [remap switch-to-buffer-other-window]
1584 'ido-switch-buffer-other-window)
1585 (define-key map [remap switch-to-buffer-other-frame]
1586 'ido-switch-buffer-other-frame)
1587 (define-key map [remap insert-buffer] 'ido-insert-buffer)
1588 (define-key map [remap kill-buffer] 'ido-kill-buffer)
1589 (define-key map [remap display-buffer] 'ido-display-buffer))
1590
1591 (if ido-minor-mode-map-entry
1592 (setcdr ido-minor-mode-map-entry map)
1593 (setq ido-minor-mode-map-entry (cons 'ido-mode map))
1594 (add-to-list 'minor-mode-map-alist ido-minor-mode-map-entry))))
1595
1596 (when (called-interactively-p 'any)
1597 (message "Ido mode %s" (if ido-mode "enabled" "disabled"))))
1598
1599
1600 ;;; IDO KEYMAP
1601 (defun ido-init-completion-maps ()
1602 "Set up the completion keymaps used by `ido'."
1603
1604 ;; Common map
1605 (let ((map (make-sparse-keymap)))
1606 (define-key map "\C-a" 'ido-toggle-ignore)
1607 (define-key map "\C-c" 'ido-toggle-case)
1608 (define-key map "\C-e" 'ido-edit-input)
1609 (define-key map "\t" 'ido-complete)
1610 (define-key map " " 'ido-complete-space)
1611 (define-key map "\C-j" 'ido-select-text)
1612 (define-key map "\C-m" 'ido-exit-minibuffer)
1613 (define-key map "\C-p" 'ido-toggle-prefix)
1614 (define-key map "\C-r" 'ido-prev-match)
1615 (define-key map "\C-s" 'ido-next-match)
1616 (define-key map [?\C-.] 'ido-next-match)
1617 (define-key map [?\C-,] 'ido-prev-match)
1618 (define-key map "\C-t" 'ido-toggle-regexp)
1619 (define-key map "\C-z" 'ido-undo-merge-work-directory)
1620 (define-key map [(control ?\s)] 'ido-restrict-to-matches)
1621 (define-key map [(meta ?\s)] 'ido-take-first-match)
1622 (define-key map [(control ?@)] 'ido-restrict-to-matches)
1623 (define-key map [right] 'ido-next-match)
1624 (define-key map [left] 'ido-prev-match)
1625 (define-key map "?" 'ido-completion-help)
1626 ;; Magic commands.
1627 (define-key map "\C-b" 'ido-magic-backward-char)
1628 (define-key map "\C-f" 'ido-magic-forward-char)
1629 (define-key map "\C-d" 'ido-magic-delete-char)
1630 (set-keymap-parent map minibuffer-local-map)
1631 (setq ido-common-completion-map map))
1632
1633 ;; File and directory map
1634 (let ((map (make-sparse-keymap)))
1635 (define-key map "\C-x\C-b" 'ido-enter-switch-buffer)
1636 (define-key map "\C-x\C-f" 'ido-fallback-command)
1637 (define-key map "\C-x\C-d" 'ido-enter-dired)
1638 (define-key map [down] 'ido-next-match-dir)
1639 (define-key map [up] 'ido-prev-match-dir)
1640 (define-key map [(meta up)] 'ido-prev-work-directory)
1641 (define-key map [(meta down)] 'ido-next-work-directory)
1642 (define-key map [backspace] 'ido-delete-backward-updir)
1643 (define-key map "\d" 'ido-delete-backward-updir)
1644 (define-key map [remap delete-backward-char] 'ido-delete-backward-updir) ; BS
1645 (define-key map [remap backward-kill-word] 'ido-delete-backward-word-updir) ; M-DEL
1646
1647 (define-key map [(control backspace)] 'ido-up-directory)
1648 (define-key map "\C-l" 'ido-reread-directory)
1649 (define-key map [(meta ?d)] 'ido-wide-find-dir-or-delete-dir)
1650 (define-key map [(meta ?b)] 'ido-push-dir)
1651 (define-key map [(meta ?v)] 'ido-push-dir-first)
1652 (define-key map [(meta ?f)] 'ido-wide-find-file-or-pop-dir)
1653 (define-key map [(meta ?k)] 'ido-forget-work-directory)
1654 (define-key map [(meta ?m)] 'ido-make-directory)
1655 (define-key map [(meta ?n)] 'ido-next-work-directory)
1656 (define-key map [(meta ?o)] 'ido-prev-work-file)
1657 (define-key map [(meta control ?o)] 'ido-next-work-file)
1658 (define-key map [(meta ?p)] 'ido-prev-work-directory)
1659 (define-key map [(meta ?s)] 'ido-merge-work-directories)
1660 (set-keymap-parent map ido-common-completion-map)
1661 (setq ido-file-dir-completion-map map))
1662
1663 ;; File only map
1664 (let ((map (make-sparse-keymap)))
1665 (define-key map "\C-k" 'ido-delete-file-at-head)
1666 (define-key map "\C-o" 'ido-copy-current-word)
1667 (define-key map "\C-w" 'ido-copy-current-file-name)
1668 (define-key map [(meta ?l)] 'ido-toggle-literal)
1669 (set-keymap-parent map ido-file-dir-completion-map)
1670 (setq ido-file-completion-map map))
1671
1672 ;; Buffer map
1673 (let ((map (make-sparse-keymap)))
1674 (define-key map "\C-x\C-f" 'ido-enter-find-file)
1675 (define-key map "\C-x\C-b" 'ido-fallback-command)
1676 (define-key map "\C-k" 'ido-kill-buffer-at-head)
1677 (define-key map "\C-o" 'ido-toggle-virtual-buffers)
1678 (set-keymap-parent map ido-common-completion-map)
1679 (setq ido-buffer-completion-map map)))
1680
1681
1682 (defun ido-setup-completion-map ()
1683 "Set up the keymap for `ido'."
1684
1685 ;; generated every time so that it can inherit new functions.
1686 (let ((map (make-sparse-keymap))
1687 (viper-p (if (boundp 'viper-mode) viper-mode)))
1688
1689 (when viper-p
1690 (define-key map [remap viper-intercept-ESC-key] 'ignore))
1691
1692 (cond
1693 ((memq ido-cur-item '(file dir))
1694 (when ido-context-switch-command
1695 (define-key map "\C-x\C-b" ido-context-switch-command)
1696 (define-key map "\C-x\C-d" 'ignore))
1697 (when viper-p
1698 (define-key map [remap viper-backward-char] 'ido-delete-backward-updir)
1699 (define-key map [remap viper-del-backward-char-in-insert] 'ido-delete-backward-updir)
1700 (define-key map [remap viper-delete-backward-word] 'ido-delete-backward-word-updir))
1701 (set-keymap-parent map
1702 (if (eq ido-cur-item 'file)
1703 ido-file-completion-map
1704 ido-file-dir-completion-map)))
1705
1706 ((eq ido-cur-item 'buffer)
1707 (when ido-context-switch-command
1708 (define-key map "\C-x\C-f" ido-context-switch-command))
1709 (set-keymap-parent map ido-buffer-completion-map))
1710
1711 (t
1712 (set-keymap-parent map ido-common-completion-map)))
1713
1714 (setq ido-completion-map map)))
1715
1716 (defun ido-final-slash (dir &optional fix-it)
1717 ;; return DIR if DIR has final slash.
1718 ;; else if FIX-IT is non-nil, return DIR/
1719 ;; else return nil.
1720 (setq dir (ido-name dir))
1721 (cond
1722 ((string-match "/\\'" dir) dir)
1723 ((ido-is-tramp-root dir) dir)
1724 (fix-it (concat dir "/"))
1725 (t nil)))
1726
1727 (defun ido-no-final-slash (s)
1728 ;; Remove optional final slash from string S
1729 (let ((l (1- (length s))))
1730 (if (and (> l 0) (eq (aref s l) ?/))
1731 (substring s 0 l)
1732 s)))
1733
1734 (defun ido-nonreadable-directory-p (dir)
1735 ;; Return t if dir is a directory, but not readable
1736 ;; Do not check for non-readable directories via tramp, as this causes a premature
1737 ;; connect on incomplete tramp paths (after entering just method:).
1738 (let ((ido-enable-tramp-completion nil))
1739 (and (ido-final-slash dir)
1740 (not (ido-is-unc-host dir))
1741 (file-directory-p dir)
1742 (not (file-readable-p dir)))))
1743
1744 (defun ido-directory-too-big-p (dir)
1745 ;; Return t if dir is a directory, but too big to show
1746 ;; Do not check for non-readable directories via tramp, as this causes a premature
1747 ;; connect on incomplete tramp paths (after entering just method:).
1748 (let ((ido-enable-tramp-completion nil))
1749 (and (numberp ido-max-directory-size)
1750 (ido-final-slash dir)
1751 (not (ido-is-unc-host dir))
1752 (file-directory-p dir)
1753 (> (nth 7 (file-attributes (file-truename dir))) ido-max-directory-size))))
1754
1755 (defun ido-set-current-directory (dir &optional subdir no-merge)
1756 ;; Set ido's current directory to DIR or DIR/SUBDIR
1757 (unless (and ido-enable-tramp-completion
1758 (string-match "\\`/[^/]*@\\'" dir))
1759 (setq dir (ido-final-slash dir t)))
1760 (setq ido-use-merged-list nil
1761 ido-try-merged-list (not no-merge))
1762 (when subdir
1763 (setq dir (concat dir subdir))
1764 (unless (and ido-enable-tramp-completion
1765 (string-match "\\`/[^/]*@\\'" dir))
1766 (setq dir (ido-final-slash dir t))))
1767 (and ido-completion-buffer
1768 (get-buffer ido-completion-buffer)
1769 (kill-buffer ido-completion-buffer))
1770 (cond
1771 ((equal dir ido-current-directory)
1772 nil)
1773 ((ido-is-unc-root dir)
1774 (ido-trace "unc" dir)
1775 (setq ido-current-directory dir)
1776 (setq ido-directory-nonreadable nil)
1777 (setq ido-directory-too-big nil)
1778 t)
1779 (t
1780 (ido-trace "cd" dir)
1781 (setq ido-current-directory dir)
1782 (and ido-completion-buffer
1783 (get-buffer ido-completion-buffer)
1784 (kill-buffer ido-completion-buffer))
1785 (setq ido-directory-nonreadable (ido-nonreadable-directory-p dir))
1786 (setq ido-directory-too-big (and (not ido-directory-nonreadable)
1787 (ido-directory-too-big-p dir)))
1788 t)))
1789
1790 (defun ido-set-current-home (&optional dir)
1791 ;; Set ido's current directory to user's home directory
1792 (ido-set-current-directory (expand-file-name (or dir "~/"))))
1793
1794 (defun ido-record-command (command arg)
1795 "Add (COMMAND ARG) to `command-history' if `ido-record-commands' is non-nil."
1796 (if ido-record-commands ; FIXME: use `when' instead of `if'?
1797 (let ((cmd (list command arg)))
1798 (if (or (not command-history) ; FIXME: ditto
1799 (not (equal cmd (car command-history))))
1800 (setq command-history (cons cmd command-history))))))
1801
1802 (defun ido-make-prompt (item prompt)
1803 ;; Make the prompt for ido-read-internal
1804 (cond
1805 ((and (memq item '(file dir)) ido-current-directory)
1806 (let ((dirname (abbreviate-file-name ido-current-directory))
1807 (max-width (if (and ido-max-file-prompt-width (floatp ido-max-file-prompt-width))
1808 (floor (* (frame-width) ido-max-file-prompt-width))
1809 ido-max-file-prompt-width))
1810 (literal (and (boundp 'ido-find-literal) ido-find-literal "(literal) "))
1811 (vc-off (and ido-saved-vc-hb (not vc-handled-backends) "[-VC] "))
1812 (prefix nil)
1813 (rule ido-rewrite-file-prompt-rules))
1814 (let ((case-fold-search nil))
1815 (while rule
1816 (if (and (consp (car rule))
1817 (string-match (car (car rule)) dirname))
1818 (setq dirname
1819 (if (stringp (cdr (car rule)))
1820 (replace-match (cdr (car rule)) t nil dirname)
1821 (funcall (cdr (car rule)) dirname))))
1822 (setq rule (cdr rule))))
1823 (run-hooks 'ido-rewrite-file-prompt-functions)
1824 (concat prompt
1825 ; (if ido-process-ignore-lists "" "&")
1826 (or literal "")
1827 (or vc-off "")
1828 (or prefix "")
1829 (let ((l (length dirname)))
1830 (if (and max-width (> max-width 0) (> l max-width))
1831 (let* ((s (substring dirname (- max-width)))
1832 (i (string-match "/" s)))
1833 (concat "..." (if i (substring s i) s)))
1834 dirname)))))
1835 (t prompt)))
1836
1837 ;; Here is very briefly how ido-find-file works:
1838 ;;
1839 ;; (ido-find-file)
1840 ;; (ido-file-internal method)
1841 ;; set ido-current-directory
1842 ;; (ido-read-internal 'file ...)
1843 ;; (while ...
1844 ;; (ido-make-item-list ...)
1845 ;; (ido-set-matches)
1846 ;; (completing-read ... ido-text-init ...)
1847 ;;
1848 ;; ... here user is allowed to type characters and commands
1849 ;; a command may set ido-exit and call (exit-minibuffer)
1850 ;; to make ido-read-internal do advanced tasks (or return)
1851 ;;
1852 ;; ... ido-tidy and ido-exhibit are pre- and post-hooks
1853 ;; which are run before and after each user command.
1854 ;;
1855 ;; return value from completing-read is stored in ido-final-text
1856 ;; - ido-exit may cause further actions to be taken:
1857 ;; 'refresh - repeat loop (make-item-list, set-matches)
1858 ;; 'edit - edit the prompt string, then repeat loop
1859 ;; 'keep - repeat loop but don't (re)make-item-list
1860 ;; 'updir - go up one directory, repeat loop
1861 ;; else set ido-selected based on ido-final-text,
1862 ;; optionally update ido-current-directory and repeat loop, or
1863 ;; exit with the return value of ido-selected (file name)
1864 ;; selected file name is returned from ido-read-internal,
1865 ;; ido-exit and method determines what action is taken
1866 ;; e.g. the file name may be ignored or joined with ido-current-directory, and
1867 ;; the relevant function is called (find-file, write-file, etc).
1868
1869 (defun ido-read-internal (item prompt hist &optional default require-match initial)
1870 "Perform the `ido-read-buffer' and `ido-read-file-name' functions.
1871 Return the name of a buffer or file selected.
1872 PROMPT is the prompt to give to the user.
1873 DEFAULT if given is the default item to start with.
1874 If REQUIRE-MATCH is non-nil, an existing file must be selected.
1875 If INITIAL is non-nil, it specifies the initial input string."
1876 (let
1877 ((ido-cur-item item)
1878 (ido-entry-buffer (current-buffer))
1879 (ido-process-ignore-lists t)
1880 (ido-process-ignore-lists-inhibit nil)
1881 (ido-set-default-item t)
1882 ido-default-item
1883 ido-selected
1884 ido-final-text
1885 (done nil)
1886 (icomplete-mode nil) ;; prevent icomplete starting up
1887 ;; Exported dynamic variables:
1888 ido-cur-list
1889 ido-ignored-list
1890 (ido-rotate-temp nil)
1891 (ido-keep-item-list nil)
1892 (ido-use-merged-list nil)
1893 (ido-try-merged-list t)
1894 (ido-pre-merge-state nil)
1895 (ido-case-fold ido-case-fold)
1896 (ido-enable-prefix ido-enable-prefix)
1897 (ido-enable-regexp ido-enable-regexp)
1898 (ido-show-confirm-message nil)
1899 )
1900
1901 (ido-setup-completion-map)
1902 (setq ido-text-init initial)
1903 (setq ido-input-stack nil)
1904
1905 (run-hooks 'ido-setup-hook)
1906
1907 (while (not done)
1908 (ido-trace "\n_LOOP_" ido-text-init)
1909 (setq ido-exit nil)
1910 (setq ido-rescan t)
1911 (setq ido-rotate nil)
1912 (setq ido-text "")
1913 (when ido-set-default-item
1914 (setq ido-default-item
1915 (cond
1916 ((eq item 'buffer)
1917 (if (bufferp default) (buffer-name default) default))
1918 ((stringp default)
1919 (if (memq item '(file dir))
1920 (file-name-nondirectory default)
1921 default))
1922 ((eq item 'file)
1923 (and ido-enable-last-directory-history
1924 (let ((d (assoc ido-current-directory ido-last-directory-list)))
1925 (and d (cdr d)))))))
1926 (if (member ido-default-item ido-ignore-item-temp-list)
1927 (setq ido-default-item nil))
1928 (ido-trace "new default" ido-default-item)
1929 (if ido-default-item
1930 (setq ido-initial-position 0))
1931 (setq ido-set-default-item nil))
1932
1933 (if ido-process-ignore-lists-inhibit
1934 (setq ido-process-ignore-lists nil))
1935
1936 (if (and ido-use-merged-list (memq ido-try-merged-list '(t wide)) (not ido-keep-item-list))
1937 (let ((olist ido-cur-list)
1938 (oign ido-ignored-list)
1939 (omat ido-matches)
1940 (l (ido-make-merged-file-list ido-text-init
1941 (eq ido-use-merged-list 'auto)
1942 (eq ido-try-merged-list 'wide))))
1943 (ido-trace "merged" l)
1944 (cond
1945 ((not l)
1946 (if (eq ido-try-merged-list 'wide)
1947 (setq ido-pre-merge-state
1948 (list "" ido-current-directory olist oign omat)
1949 ido-cur-list nil
1950 ido-ignored-list nil
1951 ido-matches nil
1952 ido-keep-item-list t
1953 ido-try-merged-list (if (eq ido-use-merged-list 'auto) 'auto nil)
1954 ido-use-merged-list nil)
1955 (setq ido-cur-list olist
1956 ido-ignored-list oign
1957 ido-matches omat
1958 ido-keep-item-list t
1959 ido-try-merged-list (if (eq ido-use-merged-list 'auto) 'auto nil)
1960 ido-use-merged-list nil)))
1961 ((eq l t)
1962 (setq ido-use-merged-list nil))
1963 ((eq l 'input-pending-p)
1964 (setq ido-try-merged-list t
1965 ido-use-merged-list nil))
1966 (t
1967 (setq ido-pre-merge-state
1968 (list ido-text-init ido-current-directory olist oign omat))
1969 (ido-set-current-directory (car (cdr (car l))))
1970 (if (ido-final-slash ido-text-init)
1971 (setq ido-text-init ""))
1972 (setq ido-cur-list l
1973 ido-ignored-list nil
1974 ido-matches l
1975 ido-rescan nil
1976 ido-keep-item-list t
1977 ido-use-merged-list t)
1978 (ido-trace "Merged" t)
1979 ))))
1980
1981 (cond
1982 (ido-keep-item-list
1983 (setq ido-keep-item-list nil
1984 ido-rescan nil))
1985 ((eq ido-cur-item 'file)
1986 (setq ido-ignored-list nil
1987 ido-cur-list (and (not ido-directory-nonreadable)
1988 (not ido-directory-too-big)
1989 (ido-make-file-list ido-default-item))))
1990 ((eq ido-cur-item 'dir)
1991 (setq ido-ignored-list nil
1992 ido-cur-list (and (not ido-directory-nonreadable)
1993 (not ido-directory-too-big)
1994 (ido-make-dir-list ido-default-item))))
1995 ((eq ido-cur-item 'buffer)
1996 (setq ido-ignored-list nil
1997 ido-cur-list (ido-make-buffer-list ido-default-item)))
1998 ((eq ido-cur-item 'list)
1999 (setq ido-ignored-list nil
2000 ido-cur-list (ido-make-choice-list ido-default-item)))
2001 (t nil))
2002 (setq ido-rotate-temp nil)
2003
2004 (if ido-process-ignore-lists-inhibit
2005 (setq ido-process-ignore-lists t
2006 ido-process-ignore-lists-inhibit nil))
2007
2008 (ido-set-matches)
2009 (if (and ido-matches (eq ido-try-merged-list 'auto))
2010 (setq ido-try-merged-list t))
2011 (let ((max-mini-window-height (or ido-max-window-height
2012 (and (boundp 'max-mini-window-height)
2013 max-mini-window-height)))
2014 (ido-completing-read t)
2015 (ido-require-match require-match)
2016 (ido-use-mycompletion-depth (1+ (minibuffer-depth)))
2017 (show-paren-mode nil)
2018 ;; Postpone history adding till later
2019 (history-add-new-input nil))
2020 ;; prompt the user for the file name
2021 (setq ido-exit nil)
2022 (setq ido-final-text
2023 (catch 'ido
2024 (read-from-minibuffer (ido-make-prompt item prompt)
2025 (prog1 ido-text-init
2026 (setq ido-text-init nil))
2027 ido-completion-map nil hist))))
2028 (ido-trace "read-from-minibuffer" ido-final-text)
2029 (and ido-completion-buffer
2030 (get-buffer ido-completion-buffer)
2031 (kill-buffer ido-completion-buffer))
2032
2033 (ido-trace "\n_EXIT_" ido-exit)
2034
2035 (cond
2036 ((eq ido-exit 'refresh)
2037 (if (and (eq ido-use-merged-list 'auto)
2038 (or (input-pending-p)))
2039 (setq ido-use-merged-list nil
2040 ido-keep-item-list t))
2041 nil)
2042
2043 ((eq ido-exit 'done)
2044 (setq done t
2045 ido-selected ido-text
2046 ido-exit nil))
2047
2048 ((memq ido-exit '(edit chdir))
2049 (cond
2050 ((memq ido-cur-item '(file dir))
2051 (let* ((read-file-name-function nil)
2052 (edit (eq ido-exit 'edit))
2053 (d ido-current-directory)
2054 (f ido-text-init)
2055 (new t))
2056 (setq ido-text-init "")
2057 (while new
2058 (setq new (if edit
2059 (condition-case nil
2060 (read-file-name (concat prompt "[EDIT] ")
2061 (expand-file-name d)
2062 (concat d f) nil f)
2063 (quit (concat d f)))
2064 f)
2065 d (or (file-name-directory new) "/")
2066 f (file-name-nondirectory new)
2067 edit t)
2068 (if (or
2069 (file-directory-p d)
2070 (and (yes-or-no-p (format "Create directory %s? " d))
2071 (condition-case nil
2072 (progn (make-directory d t) t)
2073 (error
2074 (message "Could not create directory")
2075 (sit-for 1)
2076 nil))))
2077 (progn
2078 (ido-set-current-directory d nil (eq ido-exit 'chdir))
2079 (setq ido-text-init f
2080 new nil))))))
2081 (t
2082 (setq ido-text-init
2083 (condition-case nil
2084 (read-string (concat prompt "[EDIT] ") ido-final-text)
2085 (quit ido-final-text)))))
2086
2087 nil)
2088
2089 ((eq ido-exit 'keep)
2090 (setq ido-keep-item-list t))
2091
2092 ((memq ido-exit '(dired fallback find-file switch-to-buffer insert-buffer insert-file))
2093 (setq done t))
2094
2095 ((memq ido-exit '(updir push))
2096 ;; cannot go up if already at the root-dir (Unix) or at the
2097 ;; root-dir of a certain drive (Windows or MS-DOS).
2098 (if (ido-is-tramp-root)
2099 (when (string-match "\\`\\(/\\([^/]+[:@]\\)*\\)\\([^/]+\\)[:@]\\'" ido-current-directory)
2100 (setq ido-text-init (match-string 3 ido-current-directory))
2101 (ido-set-current-directory (match-string 1 ido-current-directory))
2102 (setq ido-set-default-item t))
2103 (unless (ido-is-root-directory)
2104 (when (eq ido-exit 'push)
2105 (setq ido-input-stack (cons (cons ido-cur-item ido-text) ido-input-stack))
2106 (setq ido-cur-item 'dir)
2107 (setq ido-text-init (file-name-nondirectory (substring ido-current-directory 0 -1)))
2108 (ido-trace "push" ido-input-stack))
2109 (ido-set-current-directory (file-name-directory (substring ido-current-directory 0 -1)))
2110 (setq ido-set-default-item t))))
2111
2112 ((eq ido-exit 'pop)
2113 (ido-trace "pop" ido-input-stack)
2114 (let ((elt (car ido-input-stack)))
2115 (setq ido-input-stack (cdr ido-input-stack))
2116 (ido-set-current-directory (concat ido-current-directory ido-text))
2117 (setq ido-cur-item (car elt))
2118 (setq ido-text-init (cdr elt))))
2119
2120 ((eq ido-exit 'pop-all)
2121 (ido-trace "pop-all" ido-input-stack)
2122 (while ido-input-stack
2123 (let ((elt (car ido-input-stack)))
2124 (setq ido-input-stack (cdr ido-input-stack))
2125 (ido-set-current-directory (concat ido-current-directory ido-text))
2126 (setq ido-cur-item (car elt))
2127 (setq ido-text-init (cdr elt)))))
2128
2129 ;; Handling the require-match must be done in a better way.
2130 ((and require-match
2131 (not (memq require-match '(confirm confirm-after-completion)))
2132 (not (if ido-directory-too-big
2133 (file-exists-p (concat ido-current-directory ido-final-text))
2134 (ido-existing-item-p))))
2135 (error "Must specify valid item"))
2136
2137 (t
2138 (setq ido-selected
2139 (if (or (eq ido-exit 'takeprompt)
2140 (null ido-matches))
2141 ido-final-text
2142 ;; else take head of list
2143 (ido-name (car ido-matches))))
2144
2145 (cond
2146 ((memq item '(buffer list))
2147 (setq done t))
2148
2149 ((string-equal "./" ido-selected)
2150 nil)
2151
2152 ((string-equal "../" ido-selected)
2153 ;; cannot go up if already at the root-dir (Unix) or at the
2154 ;; root-dir of a certain drive (Windows or MS-DOS).
2155 (or (ido-is-root-directory)
2156 (ido-set-current-directory (file-name-directory (substring ido-current-directory 0 -1))))
2157 (setq ido-set-default-item t))
2158
2159 ((and (string-match (if ido-enable-tramp-completion ".[:@]\\'" ".:\\'") ido-selected)
2160 (ido-is-root-directory) ;; Ange-ftp or Tramp
2161 (not (ido-local-file-exists-p ido-selected)))
2162 (ido-set-current-directory ido-current-directory ido-selected)
2163 (ido-trace "tramp prefix" ido-selected)
2164 (if (ido-is-slow-ftp-host)
2165 (setq ido-exit 'fallback
2166 done t)
2167 (setq ido-set-default-item t)))
2168
2169 ((string-match (if (memq system-type '(windows-nt ms-dos))
2170 "\\`[a-zA-Z]:\\|[/\\][^/\\]"
2171 "/[^/]")
2172 ido-selected)
2173 (ido-set-current-directory (file-name-directory ido-selected))
2174 (setq ido-set-default-item t))
2175
2176 ((string-match "\\`~" ido-selected)
2177 (ido-set-current-home ido-selected))
2178
2179 ((ido-final-slash ido-selected)
2180 (if ido-enable-last-directory-history
2181 (let ((x (assoc ido-current-directory ido-last-directory-list)))
2182 (if x
2183 (setcdr x ido-selected)
2184 (setq ido-last-directory-list
2185 (cons (cons ido-current-directory ido-selected) ido-last-directory-list)))))
2186 (ido-set-current-directory ido-current-directory ido-selected)
2187 (if ido-input-stack
2188 ; automatically pop stack elements which match existing files or directories
2189 (let (elt)
2190 (while (and (setq elt (car ido-input-stack))
2191 (file-exists-p (concat ido-current-directory (cdr elt))))
2192 (if (setq ido-input-stack (cdr ido-input-stack))
2193 (ido-set-current-directory ido-current-directory (cdr elt))
2194 (setq ido-text-init (cdr elt)))
2195 (setq ido-cur-item (car elt))))
2196 (setq ido-set-default-item t)))
2197
2198 (t
2199 (setq done t))))))
2200 (add-to-history (cond
2201 ((consp hist)
2202 (or (car hist) 'minibuffer-history))
2203 (hist hist)
2204 (t 'minibuffer-history))
2205 ido-selected)
2206 ido-selected))
2207
2208 (defun ido-edit-input ()
2209 "Edit absolute file name entered so far with ido; terminate by RET.
2210 If cursor is not at the end of the user input, move to end of input."
2211 (interactive)
2212 (if (not (eobp))
2213 (end-of-line)
2214 (setq ido-text-init (if ido-matches (ido-name (car ido-matches)) ido-text))
2215 (setq ido-exit 'edit)
2216 (exit-minibuffer)))
2217
2218 ;;; MAIN FUNCTIONS
2219 (defun ido-buffer-internal (method &optional fallback prompt default initial switch-cmd)
2220 ;; Internal function for ido-switch-buffer and friends
2221 (if (not ido-mode)
2222 (progn
2223 (run-hook-with-args 'ido-before-fallback-functions
2224 (or fallback 'switch-to-buffer))
2225 (call-interactively (or fallback 'switch-to-buffer)))
2226 (let* ((ido-context-switch-command switch-cmd)
2227 (ido-current-directory nil)
2228 (ido-directory-nonreadable nil)
2229 (ido-directory-too-big nil)
2230 (ido-enable-virtual-buffers (and ido-use-virtual-buffers
2231 (not (eq ido-use-virtual-buffers 'auto))))
2232 (require-match (confirm-nonexistent-file-or-buffer))
2233 (buf (ido-read-internal 'buffer (or prompt "Buffer: ") 'ido-buffer-history default
2234 require-match initial))
2235 filename)
2236
2237 ;; Choose the buffer name: either the text typed in, or the head
2238 ;; of the list of matches
2239
2240 (cond
2241 ((eq ido-exit 'find-file)
2242 (ido-file-internal
2243 (if (memq method '(other-window other-frame)) method ido-default-file-method)
2244 nil nil nil nil ido-text))
2245
2246 ((eq ido-exit 'insert-file)
2247 (ido-file-internal 'insert 'insert-file nil "Insert file: " nil ido-text 'ido-enter-insert-buffer))
2248
2249 ((eq ido-exit 'fallback)
2250 (let ((read-buffer-function nil))
2251 (setq this-command (or fallback 'switch-to-buffer))
2252 (run-hook-with-args 'ido-before-fallback-functions this-command)
2253 (call-interactively this-command)))
2254
2255 ;; Check buf is non-nil.
2256 ((not buf) nil)
2257 ((= (length buf) 0) nil)
2258
2259 ;; View buffer if it exists
2260 ((get-buffer buf)
2261 (add-to-history 'buffer-name-history buf)
2262 (if (eq method 'insert)
2263 (progn
2264 (ido-record-command 'insert-buffer buf)
2265 (push-mark
2266 (save-excursion
2267 (insert-buffer-substring (get-buffer buf))
2268 (point))))
2269 (ido-visit-buffer buf method t)))
2270
2271 ;; check for a virtual buffer reference
2272 ((and ido-enable-virtual-buffers
2273 ido-virtual-buffers
2274 (setq filename (assoc buf ido-virtual-buffers)))
2275 (ido-visit-buffer (find-file-noselect (cdr filename)) method t))
2276
2277 ((and (eq ido-create-new-buffer 'prompt)
2278 (null require-match)
2279 (not (y-or-n-p (format "No buffer matching `%s', create one? " buf))))
2280 nil)
2281
2282 ;; buffer doesn't exist
2283 ((and (eq ido-create-new-buffer 'never)
2284 (null require-match))
2285 (message "No buffer matching `%s'" buf))
2286
2287 ((and (eq ido-create-new-buffer 'prompt)
2288 (null require-match)
2289 (not (y-or-n-p (format "No buffer matching `%s', create one? " buf))))
2290 nil)
2291
2292 ;; create a new buffer
2293 (t
2294 (add-to-history 'buffer-name-history buf)
2295 (setq buf (get-buffer-create buf))
2296 (if (fboundp 'set-buffer-major-mode)
2297 (set-buffer-major-mode buf))
2298 (ido-visit-buffer buf method t))))))
2299
2300 (defun ido-record-work-directory (&optional dir)
2301 (when (and (numberp ido-max-work-directory-list) (> ido-max-work-directory-list 0))
2302 (if (and (setq dir (or dir ido-current-directory)) (> (length dir) 0))
2303 (let ((items ido-work-directory-list-ignore-regexps)
2304 (case-fold-search nil))
2305 (while (and items dir)
2306 (if (string-match (car items) dir)
2307 (setq dir nil))
2308 (setq items (cdr items)))
2309 (if dir
2310 (setq ido-work-directory-list (cons dir (delete dir ido-work-directory-list))))))
2311 (if (> (length ido-work-directory-list) ido-max-work-directory-list)
2312 (setcdr (nthcdr (1- ido-max-work-directory-list) ido-work-directory-list) nil))))
2313
2314 (defun ido-forget-work-directory ()
2315 (interactive)
2316 (when (and ido-current-directory ido-work-directory-list)
2317 (setq ido-work-directory-list (delete ido-current-directory ido-work-directory-list))
2318 (when ido-use-merged-list
2319 (ido-undo-merge-work-directory)
2320 (setq ido-exit 'refresh
2321 ido-try-merged-list t
2322 ido-use-merged-list t
2323 ido-text-init ido-text
2324 ido-rotate-temp t)
2325 (exit-minibuffer))))
2326
2327 (defun ido-record-work-file (name)
2328 ;; Save NAME in ido-work-file-list
2329 (when (and (numberp ido-max-work-file-list) (> ido-max-work-file-list 0))
2330 (or
2331 (and ido-work-file-list (equal (car ido-work-file-list) name))
2332 (setq ido-work-file-list (cons name (delete name ido-work-file-list))))
2333 (if (> (length ido-work-file-list) ido-max-work-file-list)
2334 (setcdr (nthcdr (1- ido-max-work-file-list) ido-work-file-list) nil))))
2335
2336 (defun ido-expand-directory (dir)
2337 ;; Expand DIR or use DEFAULT-DIRECTORY if nil.
2338 ;; Add final slash to result in case it was missing from DEFAULT-DIRECTORY.
2339 (ido-final-slash (expand-file-name (or dir default-directory)) t))
2340
2341 (defun ido-file-internal (method &optional fallback default prompt item initial switch-cmd)
2342 ;; Internal function for ido-find-file and friends
2343 (unless item
2344 (setq item 'file))
2345 (let ((ido-current-directory (ido-expand-directory default))
2346 (ido-context-switch-command switch-cmd)
2347 ido-directory-nonreadable ido-directory-too-big
2348 filename)
2349
2350 (if (or (not ido-mode) (ido-is-slow-ftp-host))
2351 (setq filename t
2352 ido-exit 'fallback)
2353 (setq ido-directory-nonreadable
2354 (ido-nonreadable-directory-p ido-current-directory)
2355 ido-directory-too-big
2356 (and (not ido-directory-nonreadable)
2357 (ido-directory-too-big-p ido-current-directory))))
2358
2359 (when (and (eq item 'file)
2360 (or ido-use-url-at-point ido-use-filename-at-point))
2361 (let (fn d)
2362 (require 'ffap)
2363 ;; Duplicate code from ffap-guesser as we want different
2364 ;; behavior for files and URLs.
2365 (cond
2366 ((with-no-warnings
2367 (and ido-use-url-at-point
2368 ffap-url-regexp
2369 (ffap-fixup-url (or (ffap-url-at-point)
2370 (ffap-gopher-at-point)))))
2371 (setq ido-exit 'ffap
2372 filename t))
2373
2374 ((and ido-use-filename-at-point
2375 (setq fn (with-no-warnings
2376 (if (eq ido-use-filename-at-point 'guess)
2377 (ffap-guesser)
2378 (ffap-string-at-point))))
2379 (not (string-match "^http:/" fn))
2380 (let ((absolute-fn (expand-file-name fn)))
2381 (setq d (if (file-directory-p absolute-fn)
2382 (file-name-as-directory absolute-fn)
2383 (file-name-directory absolute-fn))))
2384 (file-directory-p d))
2385 (setq ido-current-directory d)
2386 (setq initial (file-name-nondirectory fn))))))
2387
2388 (let (ido-saved-vc-hb
2389 (vc-handled-backends (and (boundp 'vc-handled-backends) vc-handled-backends))
2390 (ido-work-directory-index -1)
2391 (ido-work-file-index -1)
2392 (ido-find-literal nil))
2393
2394 (unless filename
2395 (setq ido-saved-vc-hb vc-handled-backends)
2396 (let ((minibuffer-completing-file-name t))
2397 (setq filename (ido-read-internal item
2398 (or prompt "Find file: ")
2399 'ido-file-history
2400 (and (eq method 'alt-file) buffer-file-name)
2401 (confirm-nonexistent-file-or-buffer) initial))))
2402
2403 ;; Choose the file name: either the text typed in, or the head
2404 ;; of the list of matches
2405
2406 (cond
2407 ((eq ido-exit 'fallback)
2408 ;; Need to guard setting of default-directory here, since
2409 ;; we don't want to change directory of current buffer.
2410 (let ((default-directory ido-current-directory)
2411 (read-file-name-function nil))
2412 (setq this-command (or fallback 'find-file))
2413 (run-hook-with-args 'ido-before-fallback-functions this-command)
2414 (call-interactively this-command)))
2415
2416 ((eq ido-exit 'switch-to-buffer)
2417 (ido-buffer-internal
2418 (if (memq method '(other-window other-frame)) method ido-default-buffer-method)
2419 nil nil nil ido-text))
2420
2421 ((eq ido-exit 'insert-buffer)
2422 (ido-buffer-internal 'insert 'insert-buffer "Insert buffer: " nil ido-text 'ido-enter-insert-file))
2423
2424 ((eq ido-exit 'dired)
2425 (funcall (cond ((eq method 'other-window) 'dired-other-window)
2426 ((eq method 'other-frame) 'dired-other-frame)
2427 (t 'dired))
2428 (concat ido-current-directory (or ido-text ""))))
2429
2430 ((eq ido-exit 'ffap)
2431 (find-file-at-point))
2432
2433 ((eq method 'alt-file)
2434 (ido-record-work-file filename)
2435 (setq default-directory ido-current-directory)
2436 (ido-record-work-directory)
2437 (find-alternate-file filename))
2438
2439 ((memq method '(dired list-directory))
2440 (if (equal filename ".")
2441 (setq filename ""))
2442 (let* ((dirname (ido-final-slash (concat ido-current-directory filename) t))
2443 (file (substring dirname 0 -1)))
2444 (cond
2445 ((file-directory-p dirname)
2446 (ido-record-command method dirname)
2447 (ido-record-work-directory dirname)
2448 (funcall method dirname))
2449 ((file-directory-p ido-current-directory)
2450 (cond
2451 ((file-exists-p file)
2452 (ido-record-command method ido-current-directory)
2453 (ido-record-work-directory)
2454 (funcall method ido-current-directory)
2455 (if (eq method 'dired)
2456 (with-no-warnings
2457 (dired-goto-file (expand-file-name file)))))
2458 ((string-match "[[*?]" filename)
2459 (setq dirname (concat ido-current-directory filename))
2460 (ido-record-command method dirname)
2461 (ido-record-work-directory)
2462 (funcall method dirname))
2463 ((y-or-n-p (format "Directory %s does not exist. Create it? " filename))
2464 (ido-record-command method dirname)
2465 (ido-record-work-directory dirname)
2466 (make-directory-internal dirname)
2467 (funcall method dirname))
2468 (t
2469 ;; put make-directory command on history
2470 (ido-record-command 'make-directory dirname))))
2471 (t (error "No such directory")))))
2472
2473 ((eq method 'write)
2474 (ido-record-work-file filename)
2475 (setq default-directory ido-current-directory)
2476 (setq filename (concat ido-current-directory filename))
2477 (ido-record-command 'write-file filename)
2478 (add-to-history 'file-name-history filename)
2479 (ido-record-work-directory)
2480 (write-file filename t))
2481
2482 ((eq method 'read-only)
2483 (ido-record-work-file filename)
2484 (setq filename (concat ido-current-directory filename))
2485 (ido-record-command fallback filename)
2486 (ido-record-work-directory)
2487 (run-hook-with-args 'ido-before-fallback-functions fallback)
2488 (funcall fallback filename))
2489
2490 ((eq method 'insert)
2491 (ido-record-work-file filename)
2492 (setq filename (concat ido-current-directory filename))
2493 (ido-record-command
2494 (if ido-find-literal 'insert-file-literally 'insert-file)
2495 filename)
2496 (add-to-history 'file-name-history filename)
2497 (ido-record-work-directory)
2498 (insert-file-1 filename
2499 (if ido-find-literal
2500 #'insert-file-contents-literally
2501 #'insert-file-contents)))
2502
2503 (filename
2504 (ido-record-work-file filename)
2505 (setq filename (concat ido-current-directory filename))
2506 (ido-record-command 'find-file filename)
2507 (add-to-history 'file-name-history filename)
2508 (ido-record-work-directory)
2509 (ido-visit-buffer (find-file-noselect filename nil ido-find-literal) method))))))
2510
2511 (defun ido-existing-item-p ()
2512 ;; Return non-nil if there is a matching item
2513 (not (null ido-matches)))
2514
2515 ;;; COMPLETION CODE
2516
2517 (defun ido-set-common-completion ()
2518 ;; Find common completion of `ido-text' in `ido-matches'
2519 ;; The result is stored in `ido-common-match-string'
2520 (let (val)
2521 (setq ido-common-match-string nil)
2522 (if (and ido-matches
2523 (not ido-enable-regexp) ;; testing
2524 (stringp ido-text)
2525 (> (length ido-text) 0))
2526 (if (setq val (ido-find-common-substring ido-matches ido-text))
2527 (setq ido-common-match-string val)))
2528 val))
2529
2530 (defun ido-complete ()
2531 "Try and complete the current pattern amongst the file names."
2532 (interactive)
2533 (let (res)
2534 (cond
2535 (ido-incomplete-regexp
2536 ;; Do nothing
2537 )
2538 ((and (memq ido-cur-item '(file dir))
2539 (string-match "[$]" ido-text))
2540 (let ((evar (substitute-in-file-name (concat ido-current-directory ido-text))))
2541 (if (not (file-exists-p (file-name-directory evar)))
2542 (message "Expansion generates non-existing directory name")
2543 (if (file-directory-p evar)
2544 (ido-set-current-directory evar)
2545 (let ((d (or (file-name-directory evar) "/"))
2546 (f (file-name-nondirectory evar)))
2547 (when (file-directory-p d)
2548 (ido-set-current-directory d)
2549 (setq ido-text-init f))))
2550 (setq ido-exit 'refresh)
2551 (exit-minibuffer))))
2552
2553 (ido-directory-too-big
2554 (setq ido-directory-too-big nil)
2555 (setq ido-text-init ido-text)
2556 (setq ido-exit 'refresh)
2557 (exit-minibuffer))
2558
2559 ((not ido-matches)
2560 (when ido-completion-buffer
2561 (call-interactively (setq this-command ido-cannot-complete-command))))
2562
2563 ((and (= 1 (length ido-matches))
2564 (not (and ido-enable-tramp-completion
2565 (string-equal ido-current-directory "/")
2566 (string-match ".[@:]\\'" (ido-name (car ido-matches)))))
2567 (not (ido-local-file-exists-p (ido-name (car ido-matches)))))
2568 ;; only one choice, so select it.
2569 (if (not ido-confirm-unique-completion)
2570 (exit-minibuffer)
2571 (setq ido-rescan (not ido-enable-prefix))
2572 (delete-region (minibuffer-prompt-end) (point))
2573 (insert (ido-name (car ido-matches)))))
2574
2575 (t ;; else there could be some completions
2576 (setq res ido-common-match-string)
2577 (if (and (not (memq res '(t nil)))
2578 (not (equal res ido-text)))
2579 ;; found something to complete, so put it in the minibuffer.
2580 (progn
2581 ;; move exact match to front if not in prefix mode
2582 (setq ido-rescan (not ido-enable-prefix))
2583 (delete-region (minibuffer-prompt-end) (point))
2584 (insert res))
2585 ;; else nothing to complete
2586 (call-interactively (setq this-command ido-cannot-complete-command))
2587 )))))
2588
2589 (defun ido-complete-space ()
2590 "Try completion unless inserting the space makes sense."
2591 (interactive)
2592 (if (and (stringp ido-common-match-string)
2593 (stringp ido-text)
2594 (cond
2595 ((> (length ido-common-match-string) (length ido-text))
2596 (= (aref ido-common-match-string (length ido-text)) ? ))
2597 (ido-matches
2598 (let (insert-space
2599 (re (concat (regexp-quote ido-text) " "))
2600 (comp ido-matches))
2601 (while comp
2602 (if (string-match re (ido-name (car comp)))
2603 (setq comp nil insert-space t)
2604 (setq comp (cdr comp))))
2605 insert-space))
2606 (t nil)))
2607 (insert " ")
2608 (ido-complete)))
2609
2610 (defun ido-undo-merge-work-directory (&optional text try refresh)
2611 "Undo or redo last ido directory merge operation.
2612 If no merge has yet taken place, toggle automatic merging option."
2613 (interactive)
2614 (cond
2615 (ido-pre-merge-state
2616 (ido-set-current-directory (nth 1 ido-pre-merge-state))
2617 (setq ido-text-init (or text (car ido-pre-merge-state))
2618 ido-cur-list (nth 2 ido-pre-merge-state)
2619 ido-ignored-list (nth 3 ido-pre-merge-state)
2620 ido-matches (nth 4 ido-pre-merge-state)
2621 ido-use-merged-list nil
2622 ido-try-merged-list try
2623 ido-keep-item-list (not refresh)
2624 ido-rescan nil
2625 ido-exit 'refresh
2626 ido-pre-merge-state nil)
2627 (exit-minibuffer))
2628 (text
2629 nil)
2630 (ido-try-merged-list
2631 (setq ido-try-merged-list nil))
2632 (ido-matches
2633 (setq ido-try-merged-list t))
2634 ((not ido-use-merged-list)
2635 (ido-merge-work-directories))))
2636
2637 ;;; Magic C-f
2638
2639 (defun ido-magic-forward-char (arg)
2640 "Move forward in user input or perform magic action.
2641 If no user input is present, or at end of input, perform magic actions:
2642 C-x C-b ... C-f switch to `ido-find-file'.
2643 C-x C-f ... C-f fallback to non-ido `find-file'.
2644 C-x C-d ... C-f fallback to non-ido brief `dired'.
2645 C-x d ... C-f fallback to non-ido `dired'."
2646 (interactive "P")
2647 (cond
2648 ((or arg (not (eobp)))
2649 (forward-char (min (prefix-numeric-value arg)
2650 (- (point-max) (point)))))
2651 ((memq ido-cur-item '(file dir))
2652 (ido-fallback-command))
2653 (ido-context-switch-command
2654 (call-interactively ido-context-switch-command))
2655 ((eq ido-cur-item 'buffer)
2656 (ido-enter-find-file))))
2657
2658 ;;; Magic C-b
2659
2660 (defun ido-magic-backward-char (arg)
2661 "Move backward in user input or perform magic action.
2662 If no user input is present, or at start of input, perform magic actions:
2663 C-x C-f C-b switch to `ido-switch-buffer'.
2664 C-x C-d C-b switch to `ido-switch-buffer'.
2665 C-x d C-b switch to `ido-switch-buffer'.
2666 C-x C-b C-b fallback to non-ido `switch-to-buffer'."
2667 (interactive "P")
2668 (cond
2669 ((or arg (> (point) (minibuffer-prompt-end)))
2670 (forward-char
2671 (- (min (prefix-numeric-value arg)
2672 (- (point) (minibuffer-prompt-end))))))
2673 ((eq last-command this-command)
2674 (when (and (memq ido-cur-item '(file dir))
2675 (not (bobp)))
2676 (ido-push-dir))) ; else do nothing
2677 ((eq ido-cur-item 'buffer)
2678 (ido-fallback-command))
2679 (ido-context-switch-command
2680 (call-interactively ido-context-switch-command))
2681 (t
2682 (ido-enter-switch-buffer))))
2683
2684 ;;; Magic C-d
2685
2686 (defun ido-magic-delete-char (arg)
2687 "Delete following char in user input or perform magic action.
2688 If at end of user input, perform magic actions:
2689 C-x C-f ... C-d enter `dired' on current directory."
2690 (interactive "P")
2691 (cond
2692 ((or arg (not (eobp)))
2693 (delete-char (min (prefix-numeric-value arg)
2694 (- (point-max) (point)))))
2695 (ido-context-switch-command
2696 nil)
2697 ((memq ido-cur-item '(file dir))
2698 (ido-enter-dired))))
2699
2700
2701 ;;; TOGGLE FUNCTIONS
2702
2703 (defun ido-toggle-case ()
2704 "Toggle the value of `ido-case-fold'."
2705 (interactive)
2706 (setq ido-case-fold (not ido-case-fold))
2707 ;; ask for list to be regenerated.
2708 (setq ido-rescan t))
2709
2710 (defun ido-toggle-regexp ()
2711 "Toggle the value of `ido-enable-regexp'."
2712 (interactive)
2713 (setq ido-enable-regexp (not ido-enable-regexp))
2714 ;; ask for list to be regenerated.
2715 (setq ido-rescan t))
2716
2717 (defun ido-toggle-prefix ()
2718 "Toggle the value of `ido-enable-prefix'."
2719 (interactive)
2720 (setq ido-enable-prefix (not ido-enable-prefix))
2721 ;; ask for list to be regenerated.
2722 (setq ido-rescan t))
2723
2724 (defun ido-toggle-ignore ()
2725 "Toggle ignoring files specified with `ido-ignore-files'."
2726 (interactive)
2727 (if (and (not (eobp)) (> (point) (minibuffer-prompt-end)))
2728 (goto-char (minibuffer-prompt-end))
2729 (if ido-directory-too-big
2730 (progn
2731 (message "Reading directory...")
2732 (setq ido-directory-too-big nil))
2733 (setq ido-process-ignore-lists (not ido-process-ignore-lists)))
2734 (setq ido-text-init ido-text)
2735 (setq ido-exit 'refresh)
2736 (exit-minibuffer)))
2737
2738 (defun ido-toggle-vc ()
2739 "Disable version control for this file."
2740 (interactive)
2741 (if (and ido-mode (eq ido-cur-item 'file))
2742 (progn
2743 (setq vc-handled-backends
2744 (if vc-handled-backends nil ido-saved-vc-hb))
2745 (setq ido-text-init ido-text)
2746 (setq ido-exit 'keep)
2747 (exit-minibuffer))))
2748
2749 (defun ido-toggle-literal ()
2750 "Toggle literal reading of this file."
2751 (interactive)
2752 (if (and ido-mode (eq ido-cur-item 'file))
2753 (progn
2754 (setq ido-find-literal (not ido-find-literal))
2755 (setq ido-text-init ido-text)
2756 (setq ido-exit 'keep)
2757 (exit-minibuffer))))
2758
2759 (defun ido-toggle-virtual-buffers ()
2760 "Toggle the use of virtual buffers.
2761 See `ido-use-virtual-buffers' for explanation of virtual buffer."
2762 (interactive)
2763 (when (and ido-mode (eq ido-cur-item 'buffer))
2764 (setq ido-enable-virtual-buffers
2765 (if ido-enable-virtual-buffers
2766 nil
2767 ;; Use `always' instead of t for `ido-exhibit'.
2768 'always))
2769 (setq ido-text-init ido-text)
2770 (setq ido-exit 'refresh)
2771 (exit-minibuffer)))
2772
2773 (defun ido-reread-directory ()
2774 "Read current directory again.
2775 May be useful if cached version is no longer valid, but directory
2776 timestamp has not changed (e.g. with ftp or on Windows)."
2777 (interactive)
2778 (if (and ido-mode (memq ido-cur-item '(file dir)))
2779 (progn
2780 (if (ido-is-unc-root)
2781 (setq ido-unc-hosts-cache t)
2782 (ido-remove-cached-dir ido-current-directory))
2783 (setq ido-text-init ido-text)
2784 (setq ido-rotate-temp t)
2785 (setq ido-exit 'refresh)
2786 (exit-minibuffer))))
2787
2788 (defun ido-exit-minibuffer ()
2789 "Exit minibuffer, but make sure we have a match if one is needed."
2790 (interactive)
2791 (if (and (or (not ido-require-match)
2792 (if (memq ido-require-match '(confirm confirm-after-completion))
2793 (if (or (eq ido-cur-item 'dir)
2794 (eq last-command this-command))
2795 t
2796 (setq ido-show-confirm-message t)
2797 nil))
2798 (ido-existing-item-p))
2799 (not ido-incomplete-regexp))
2800 (exit-minibuffer)))
2801
2802 (defun ido-select-text ()
2803 "Select the buffer or file named by the prompt.
2804 If no buffer or file exactly matching the prompt exists, maybe create a new one."
2805 (interactive)
2806 (setq ido-exit 'takeprompt)
2807 (exit-minibuffer))
2808
2809 (defun ido-fallback-command ()
2810 "Fallback to non-ido version of current command."
2811 (interactive)
2812 (let ((i (length ido-text)))
2813 (while (> i 0)
2814 (push (aref ido-text (setq i (1- i))) unread-command-events)))
2815 (setq ido-exit 'fallback)
2816 (exit-minibuffer))
2817
2818 (defun ido-enter-find-file ()
2819 "Drop into `find-file' from buffer switching."
2820 (interactive)
2821 (setq ido-exit 'find-file)
2822 (exit-minibuffer))
2823
2824 (defun ido-enter-switch-buffer ()
2825 "Drop into `ido-switch-buffer' from file switching."
2826 (interactive)
2827 (setq ido-exit 'switch-to-buffer)
2828 (exit-minibuffer))
2829
2830 (defun ido-enter-dired ()
2831 "Drop into `dired' from file switching."
2832 (interactive)
2833 (setq ido-exit 'dired)
2834 (exit-minibuffer))
2835
2836 (defun ido-enter-insert-buffer ()
2837 "Drop into `insert-buffer' from insert file."
2838 (interactive)
2839 (setq ido-exit 'insert-buffer)
2840 (exit-minibuffer))
2841
2842 (defun ido-enter-insert-file ()
2843 "Drop into `insert-file' from insert buffer."
2844 (interactive)
2845 (setq ido-exit 'insert-file)
2846 (exit-minibuffer))
2847
2848
2849 (defun ido-up-directory (&optional clear)
2850 "Go up one directory level."
2851 (interactive "P")
2852 (setq ido-text-init (if clear nil ido-text))
2853 (setq ido-exit 'updir)
2854 (setq ido-rotate-temp t)
2855 (exit-minibuffer))
2856
2857 (defun ido-delete-backward-updir (count)
2858 "Delete char backwards, or at beginning of buffer, go up one level."
2859 (interactive "P")
2860 (cond
2861 ((= (minibuffer-prompt-end) (point))
2862 (if (not count)
2863 (ido-up-directory t)))
2864 ((and ido-pre-merge-state (string-equal (car ido-pre-merge-state) ido-text))
2865 (ido-undo-merge-work-directory (substring ido-text 0 -1) t t))
2866 ((eq this-original-command 'viper-backward-char)
2867 (funcall this-original-command (prefix-numeric-value count)))
2868 ((eq this-original-command 'viper-del-backward-char-in-insert)
2869 (funcall this-original-command))
2870 (t
2871 (delete-char (- (prefix-numeric-value count))))))
2872
2873 (defun ido-delete-backward-word-updir (count)
2874 "Delete all chars backwards, or at beginning of buffer, go up one level."
2875 (interactive "P")
2876 (if (= (minibuffer-prompt-end) (point))
2877 (if (not count)
2878 (ido-up-directory t))
2879 (if (eq this-original-command 'viper-delete-backward-word)
2880 (funcall this-original-command (prefix-numeric-value count))
2881 (backward-kill-word (prefix-numeric-value count)))))
2882
2883 (defun ido-get-work-directory (&optional incr must-match)
2884 (let ((n (length ido-work-directory-list))
2885 (i ido-work-directory-index)
2886 (j 0)
2887 dir)
2888 (if (or (not ido-text) (= (length ido-text) 0))
2889 (setq must-match nil))
2890 (while (< j n)
2891 (setq i (+ i incr)
2892 j (1+ j))
2893 (if (> incr 0)
2894 (if (>= i n) (setq i 0))
2895 (if (< i 0) (setq i (1- n))))
2896 (setq dir (nth i ido-work-directory-list))
2897 (if (and dir
2898 (not (equal dir ido-current-directory))
2899 (file-directory-p dir)
2900 (or (not must-match)
2901 ;; TODO. check for nonreadable and too-big.
2902 (ido-set-matches-1
2903 (if (eq ido-cur-item 'file)
2904 (ido-make-file-list-1 dir)
2905 (ido-make-dir-list-1 dir)))))
2906 (setq j n)
2907 (setq dir nil)))
2908 (if dir
2909 (setq ido-work-directory-index i))
2910 dir))
2911
2912 (defun ido-prev-work-directory ()
2913 "Change to next working directory in list."
2914 (interactive)
2915 (let ((dir (ido-get-work-directory 1 ido-work-directory-match-only)))
2916 (when dir
2917 (ido-set-current-directory dir)
2918 (setq ido-exit 'refresh)
2919 (setq ido-text-init ido-text)
2920 (setq ido-rotate-temp t)
2921 (exit-minibuffer))))
2922
2923 (defun ido-next-work-directory ()
2924 "Change to previous working directory in list."
2925 (interactive)
2926 (let ((dir (ido-get-work-directory -1 ido-work-directory-match-only)))
2927 (when dir
2928 (ido-set-current-directory dir)
2929 (setq ido-exit 'refresh)
2930 (setq ido-text-init ido-text)
2931 (setq ido-rotate-temp t)
2932 (exit-minibuffer))))
2933
2934 (defun ido-merge-work-directories ()
2935 "Search (and merge) work directories for files matching the current input string."
2936 (interactive)
2937 (setq ido-use-merged-list t ido-try-merged-list t)
2938 (setq ido-exit 'refresh)
2939 (setq ido-text-init ido-text)
2940 (setq ido-rotate-temp t)
2941 (exit-minibuffer))
2942
2943 (defun ido-wide-find-file (&optional file)
2944 "Prompt for FILE to search for using find, starting from current directory."
2945 (interactive)
2946 (unless file
2947 (let ((enable-recursive-minibuffers t))
2948 (setq file
2949 (condition-case nil
2950 (read-string (concat "Wide find file: " ido-current-directory) ido-text)
2951 (quit "")))))
2952 (when (> (length file) 0)
2953 (setq ido-use-merged-list t ido-try-merged-list 'wide)
2954 (setq ido-exit 'refresh)
2955 (setq ido-text-init file)
2956 (setq ido-rotate-temp t)
2957 (exit-minibuffer)))
2958
2959 (defun ido-wide-find-dir (&optional dir)
2960 "Prompt for DIR to search for using find, starting from current directory."
2961 (interactive)
2962 (unless dir
2963 (let ((enable-recursive-minibuffers t))
2964 (setq dir
2965 (condition-case nil
2966 (read-string (concat "Wide find directory: " ido-current-directory) ido-text)
2967 (quit "")))))
2968 (when (> (length dir) 0)
2969 (setq ido-use-merged-list t ido-try-merged-list 'wide)
2970 (setq ido-exit 'refresh)
2971 (setq ido-text-init (ido-final-slash dir t))
2972 (setq ido-rotate-temp t)
2973 (exit-minibuffer)))
2974
2975 (defun ido-wide-find-dir-or-delete-dir (&optional _dir)
2976 "Prompt for DIR to search for using find, starting from current directory.
2977 If input stack is non-empty, delete current directory component."
2978 (interactive)
2979 (if ido-input-stack
2980 (ido-delete-backward-word-updir 1)
2981 (ido-wide-find-dir)))
2982
2983 (defun ido-take-first-match ()
2984 "Use first matching item as input text."
2985 (interactive)
2986 (when ido-matches
2987 (setq ido-text-init (ido-name (car ido-matches)))
2988 (setq ido-exit 'refresh)
2989 (exit-minibuffer)))
2990
2991 (defun ido-push-dir ()
2992 "Move to previous directory in file name, push current input on stack."
2993 (interactive)
2994 (setq ido-exit 'push)
2995 (exit-minibuffer))
2996
2997 (defun ido-push-dir-first ()
2998 "Move to previous directory in file name, push first match on stack."
2999 (interactive)
3000 (if ido-matches
3001 (setq ido-text (ido-name (car ido-matches))))
3002 (setq ido-exit 'push)
3003 (exit-minibuffer))
3004
3005 (defun ido-pop-dir (arg)
3006 "Pop directory from input stack back to input.
3007 With \\[universal-argument], pop all elements."
3008 (interactive "P")
3009 (when ido-input-stack
3010 (setq ido-exit (if arg 'pop-all 'pop))
3011 (exit-minibuffer)))
3012
3013 (defun ido-wide-find-file-or-pop-dir (arg)
3014 (interactive "P")
3015 (if ido-input-stack
3016 (ido-pop-dir arg)
3017 (ido-wide-find-file)))
3018
3019 (defun ido-make-directory (&optional dir)
3020 "Prompt for DIR to create in current directory."
3021 (interactive)
3022 (unless dir
3023 (let ((enable-recursive-minibuffers t))
3024 (setq dir
3025 (read-string (concat "Make directory: " ido-current-directory) ido-text))))
3026 (when (> (length dir) 0)
3027 (setq dir (concat ido-current-directory dir))
3028 (unless (file-exists-p dir)
3029 (make-directory dir t)
3030 (ido-set-current-directory dir)
3031 (setq ido-exit 'refresh)
3032 (setq ido-text-init nil)
3033 (setq ido-rotate-temp t)
3034 (exit-minibuffer))))
3035
3036 (defun ido-get-work-file (incr)
3037 (let ((n (length ido-work-file-list))
3038 (i (+ ido-work-file-index incr))
3039 name)
3040 (if (> incr 0)
3041 (if (>= i n) (setq i 0))
3042 (if (< i 0) (setq i (1- n))))
3043 (setq name (nth i ido-work-file-list))
3044 (setq ido-work-file-index i)
3045 name))
3046
3047 (defun ido-prev-work-file ()
3048 "Change to next working file name in list."
3049 (interactive)
3050 (let ((name (ido-get-work-file 1)))
3051 (when name
3052 (setq ido-text-init name)
3053 (setq ido-exit 'refresh)
3054 (exit-minibuffer))))
3055
3056 (defun ido-next-work-file ()
3057 "Change to previous working file name in list."
3058 (interactive)
3059 (let ((name (ido-get-work-file -1)))
3060 (when name
3061 (setq ido-text-init name)
3062 (setq ido-exit 'refresh)
3063 (exit-minibuffer))))
3064
3065 (defun ido-copy-current-file-name (all)
3066 "Insert file name of current buffer.
3067 If repeated, insert text from buffer instead."
3068 (interactive "P")
3069 (let* ((bfname (or (buffer-file-name ido-entry-buffer)
3070 (buffer-name ido-entry-buffer)))
3071 (name (and bfname (file-name-nondirectory bfname))))
3072 (when name
3073 (setq ido-text-init
3074 (if (or all
3075 (eq last-command this-command)
3076 (not (equal (file-name-directory bfname) ido-current-directory))
3077 (not (string-match "\\.[^.]*\\'" name)))
3078 name
3079 (substring name 0 (1+ (match-beginning 0)))))
3080 (setq ido-exit 'refresh
3081 ido-try-merged-list nil)
3082 (exit-minibuffer))))
3083
3084 (defun ido-copy-current-word (_all)
3085 "Insert current word (file or directory name) from current buffer."
3086 (interactive "P")
3087 (let ((word (with-current-buffer ido-entry-buffer
3088 (let ((p (point)) start-line end-line start-name)
3089 (if (and mark-active (/= p (mark)))
3090 (setq start-name (mark))
3091 (beginning-of-line)
3092 (setq start-line (point))
3093 (end-of-line)
3094 (setq end-line (point))
3095 (goto-char p)
3096 (if (re-search-backward "[^-_a-zA-Z0-9:./\\~@]" start-line 1)
3097 (forward-char 1))
3098 (setq start-name (point))
3099 (re-search-forward "[-_a-zA-Z0-9:./\\~@]*" end-line 1)
3100 (if (= start-name (point))
3101 (setq start-name nil)))
3102 (and start-name
3103 (buffer-substring-no-properties start-name (point)))))))
3104 (if (cond
3105 ((not word) nil)
3106 ((string-match "\\`[~/]" word)
3107 (setq ido-text-init word
3108 ido-try-merged-list nil
3109 ido-exit 'chdir))
3110 ((string-match "/" word)
3111 (setq ido-text-init (concat ido-current-directory word)
3112 ido-try-merged-list nil
3113 ido-exit 'chdir))
3114 (t
3115 (setq ido-text-init word
3116 ido-try-merged-list nil
3117 ido-exit 'refresh)))
3118 (exit-minibuffer))))
3119
3120 (defun ido-next-match ()
3121 "Put first element of `ido-matches' at the end of the list."
3122 (interactive)
3123 (if ido-matches
3124 (let ((next (cadr ido-matches)))
3125 (setq ido-cur-list (ido-chop ido-cur-list next))
3126 (setq ido-matches (ido-chop ido-matches next))
3127 (setq ido-rescan nil))))
3128
3129 (defun ido-prev-match ()
3130 "Put last element of `ido-matches' at the front of the list."
3131 (interactive)
3132 (if ido-matches
3133 (let ((prev (car (last ido-matches))))
3134 (setq ido-cur-list (ido-chop ido-cur-list prev))
3135 (setq ido-matches (ido-chop ido-matches prev))
3136 (setq ido-rescan nil))))
3137
3138 (defun ido-next-match-dir ()
3139 "Find next directory in match list.
3140 If work directories have been merged, cycle through directories for
3141 first matching file."
3142 (interactive)
3143 (if ido-use-merged-list
3144 (if ido-matches
3145 (let* ((elt (car ido-matches))
3146 (dirs (cdr elt)))
3147 (when (> (length dirs) 1)
3148 (setcdr elt (ido-chop dirs (cadr dirs))))
3149 (setq ido-rescan nil)))
3150 (let ((cnt (length ido-matches))
3151 (i 1))
3152 (while (and (< i cnt) (not (ido-final-slash (nth i ido-matches))))
3153 (setq i (1+ i)))
3154 (if (< i cnt)
3155 (setq ido-cur-list (ido-chop ido-cur-list (nth i ido-matches)))))))
3156
3157 (defun ido-prev-match-dir ()
3158 "Find previous directory in match list.
3159 If work directories have been merged, cycle through directories
3160 for first matching file."
3161 (interactive)
3162 (if ido-use-merged-list
3163 (if ido-matches
3164 (let* ((elt (car ido-matches))
3165 (dirs (cdr elt)))
3166 (when (> (length dirs) 1)
3167 (setcdr elt (ido-chop dirs (car (last dirs)))))
3168 (setq ido-rescan nil)))
3169 (let* ((cnt (length ido-matches))
3170 (i (1- cnt)))
3171 (while (and (> i 0) (not (ido-final-slash (nth i ido-matches))))
3172 (setq i (1- i)))
3173 (if (> i 0)
3174 (setq ido-cur-list (ido-chop ido-cur-list (nth i ido-matches)))))))
3175
3176 (defun ido-restrict-to-matches ()
3177 "Set current item list to the currently matched items."
3178 (interactive)
3179 (when ido-matches
3180 (setq ido-cur-list ido-matches
3181 ido-text-init ""
3182 ido-rescan nil
3183 ido-exit 'keep)
3184 (exit-minibuffer)))
3185
3186 (defun ido-chop (items elem)
3187 "Remove all elements before ELEM and put them at the end of ITEMS."
3188 (let ((ret nil)
3189 (next nil)
3190 (sofar nil))
3191 (while (not ret)
3192 (setq next (car items))
3193 (if (equal next elem)
3194 (setq ret (append items (nreverse sofar)))
3195 ;; else
3196 (progn
3197 (setq items (cdr items))
3198 (setq sofar (cons next sofar)))))
3199 ret))
3200
3201 (defun ido-name (item)
3202 ;; Return file name for current item, whether in a normal list
3203 ;; or a merged work directory list.
3204 (if (consp item) (car item) item))
3205
3206
3207 ;;; CREATE LIST OF ALL CURRENT FILES
3208
3209 (defun ido-all-completions ()
3210 ;; Return unsorted list of all completions.
3211 (let ((ido-process-ignore-lists nil)
3212 (ido-directory-too-big nil))
3213 (cond
3214 ((eq ido-cur-item 'file)
3215 (ido-make-file-list-1 ido-current-directory))
3216 ((eq ido-cur-item 'dir)
3217 (ido-make-dir-list-1 ido-current-directory))
3218 ((eq ido-cur-item 'buffer)
3219 (ido-make-buffer-list-1))
3220 ((eq ido-cur-item 'list)
3221 ido-choice-list)
3222 (t nil))))
3223
3224
3225 ;; File list sorting
3226
3227 (defun ido-file-lessp (a b)
3228 ;; Simple compare two file names.
3229 (string-lessp (ido-no-final-slash a) (ido-no-final-slash b)))
3230
3231
3232 (defun ido-file-extension-lessp (a b)
3233 ;; Compare file names according to ido-file-extensions-order list.
3234 (let ((n (compare-strings a 0 nil b 0 nil nil))
3235 lessp p)
3236 (if (eq n t)
3237 nil
3238 (if (< n 0)
3239 (setq n (1- (- n))
3240 p a a b b p
3241 lessp t)
3242 (setq n (1- n)))
3243 (cond
3244 ((= n 0)
3245 lessp)
3246 ((= (aref a n) ?.)
3247 (ido-file-extension-aux a b n lessp))
3248 (t
3249 (while (and (> n 2) (/= (aref a n) ?.))
3250 (setq n (1- n)))
3251 (if (> n 1)
3252 (ido-file-extension-aux a b n lessp)
3253 lessp))))))
3254
3255 (defun ido-file-extension-aux (a b n lessp)
3256 (let ((oa (ido-file-extension-order a n))
3257 (ob (ido-file-extension-order b n)))
3258 (cond
3259 ((and oa ob)
3260 (cond
3261 ((= oa ob)
3262 lessp)
3263 (lessp
3264 (> oa ob))
3265 (t
3266 (< oa ob))))
3267 (oa
3268 (not lessp))
3269 (ob
3270 lessp)
3271 (t
3272 lessp))))
3273
3274 (defun ido-file-extension-order (s n)
3275 (let ((l ido-file-extensions-order)
3276 (i 0) o do)
3277 (while l
3278 (cond
3279 ((eq (car l) t)
3280 (setq do i
3281 l (cdr l)))
3282 ((eq (compare-strings s n nil (car l) 0 nil nil) t)
3283 (setq o i
3284 l nil))
3285 (t
3286 (setq l (cdr l))))
3287 (setq i (1+ i)))
3288 (or o do)))
3289
3290
3291 (defun ido-sort-merged-list (items promote)
3292 ;; Input is list of ("file" . "dir") cons cells.
3293 ;; Output is sorted list of ("file "dir" ...) lists
3294 (let ((l (sort items (lambda (a b) (string-lessp (car b) (car a)))))
3295 res a cur)
3296 (while l
3297 (setq a (car l)
3298 l (cdr l))
3299 (if (and res (string-equal (car (car res)) (car a)))
3300 (progn
3301 (setcdr (car (if cur (cdr res) res)) (cons (cdr a) (cdr (car res))))
3302 (if (and promote (string-equal ido-current-directory (cdr a)))
3303 (setq cur t)))
3304 (setq res (cons (list (car a) (cdr a)) res)
3305 cur nil)))
3306 res))
3307
3308 (defun ido-wide-find-dirs-or-files (dir file &optional prefix finddir)
3309 ;; As ido-run-find-command, but returns a list of cons pairs ("file" . "dir")
3310 (let ((filenames
3311 (delq nil
3312 (mapcar (lambda (name)
3313 (unless (ido-ignore-item-p name ido-ignore-files t)
3314 name))
3315 (split-string
3316 (shell-command-to-string
3317 (concat "find "
3318 (shell-quote-argument dir)
3319 (if ido-case-fold " -iname " " -name ")
3320 (shell-quote-argument
3321 (concat (if prefix "" "*") file "*"))
3322 " -type " (if finddir "d" "f") " -print"))))))
3323 filename d f
3324 res)
3325 (while filenames
3326 (setq filename (car filenames)
3327 filenames (cdr filenames))
3328 (if (and (file-name-absolute-p filename)
3329 (file-exists-p filename))
3330 (setq d (file-name-directory filename)
3331 f (file-name-nondirectory filename)
3332 res (cons (cons (if finddir (ido-final-slash f t) f) d) res))))
3333 res))
3334
3335 (defun ido-flatten-merged-list (items)
3336 "Create a list of directory names based on a merged directory list."
3337 (let (res)
3338 (while items
3339 (let* ((item (car items))
3340 (file (car item))
3341 (dirs (cdr item)))
3342 (while dirs
3343 (setq res (cons (concat (car dirs) file) res)
3344 dirs (cdr dirs))))
3345 (setq items (cdr items)))
3346 res))
3347
3348
3349 (defun ido-make-merged-file-list-1 (text auto wide)
3350 (let (res)
3351 (if (and (ido-final-slash text) ido-dir-file-cache)
3352 (if wide
3353 (setq res (ido-wide-find-dirs-or-files
3354 ido-current-directory (substring text 0 -1) ido-enable-prefix t))
3355 ;; Use list of cached directories
3356 (let ((re (concat (regexp-quote (substring text 0 -1)) "[^/:]*/\\'"))
3357 (dirs ido-dir-file-cache)
3358 dir b d f)
3359 (if nil ;; simple
3360 (while dirs
3361 (setq dir (car (car dirs))
3362 dirs (cdr dirs))
3363 (when (and (string-match re dir)
3364 (not (ido-ignore-item-p dir ido-ignore-directories-merge))
3365 (file-directory-p dir))
3366 (setq b (substring dir 0 -1)
3367 f (concat (file-name-nondirectory b) "/")
3368 d (file-name-directory b)
3369 res (cons (cons f d) res))))
3370 (while dirs
3371 (setq dir (car dirs)
3372 d (car dir)
3373 dirs (cdr dirs))
3374 (when (not (ido-ignore-item-p d ido-ignore-directories-merge))
3375 (setq dir (cdr (cdr dir)))
3376 (while dir
3377 (setq f (car dir)
3378 dir (cdr dir))
3379 (if (and (string-match re f)
3380 (not (ido-ignore-item-p f ido-ignore-directories)))
3381 (setq res (cons (cons f d) res)))))
3382 (if (and auto (input-pending-p))
3383 (setq dirs nil
3384 res t))))))
3385 (if wide
3386 (setq res (ido-wide-find-dirs-or-files
3387 ido-current-directory text ido-enable-prefix nil))
3388 (let ((ido-text text)
3389 (dirs ido-work-directory-list)
3390 (must-match (and text (> (length text) 0)))
3391 dir fl)
3392 (if (and auto (not (member ido-current-directory dirs)))
3393 (setq dirs (cons ido-current-directory dirs)))
3394 (while dirs
3395 (setq dir (car dirs)
3396 dirs (cdr dirs))
3397 (when (and dir (stringp dir)
3398 (or ido-merge-ftp-work-directories
3399 (not (ido-is-ftp-directory dir)))
3400 (file-directory-p dir)
3401 ;; TODO. check for nonreadable and too-big.
3402 (setq fl (if (eq ido-cur-item 'file)
3403 (ido-make-file-list-1 dir t)
3404 (ido-make-dir-list-1 dir t))))
3405 (if must-match
3406 (setq fl (ido-set-matches-1 fl)))
3407 (if fl
3408 (setq res (nconc fl res))))
3409 (if (and auto (input-pending-p))
3410 (setq dirs nil
3411 res t))))))
3412 res))
3413
3414 (defun ido-make-merged-file-list (text auto wide)
3415 (let (res)
3416 (message "Searching for `%s'...." text)
3417 (condition-case nil
3418 (if (eq t (setq res
3419 (while-no-input
3420 (ido-make-merged-file-list-1 text auto wide))))
3421 (setq res 'input-pending-p))
3422 (quit
3423 (setq res t
3424 ido-try-merged-list nil
3425 ido-use-merged-list nil)))
3426 (when (and res (listp res))
3427 (setq res (ido-sort-merged-list res auto)))
3428 (when (and (or ido-rotate-temp ido-rotate-file-list-default)
3429 (listp res)
3430 (> (length text) 0))
3431 (let ((elt (assoc text res)))
3432 (when (and elt (not (eq elt (car res))))
3433 (setq res (delq elt res))
3434 (setq res (cons elt res)))))
3435 (message nil)
3436 res))
3437
3438 (defun ido-make-buffer-list-1 (&optional frame visible)
3439 "Return list of non-ignored buffer names."
3440 (delq nil
3441 (mapcar
3442 (lambda (x)
3443 (let ((name (buffer-name x)))
3444 (if (not (or (ido-ignore-item-p name ido-ignore-buffers) (member name visible)))
3445 name)))
3446 (buffer-list frame))))
3447
3448 (defun ido-make-buffer-list (default)
3449 "Return the current list of buffers.
3450 Currently visible buffers are put at the end of the list.
3451 The hook `ido-make-buffer-list-hook' is run after the list has been
3452 created to allow the user to further modify the order of the buffer names
3453 in this list. If DEFAULT is non-nil, and corresponds to an existing buffer,
3454 it is put to the start of the list."
3455 (let* ((ido-current-buffers (ido-get-buffers-in-frames 'current))
3456 (ido-temp-list (ido-make-buffer-list-1 (selected-frame) ido-current-buffers)))
3457 (if ido-temp-list
3458 (nconc ido-temp-list ido-current-buffers)
3459 (setq ido-temp-list ido-current-buffers))
3460 (if default
3461 (setq ido-temp-list
3462 (cons default (delete default ido-temp-list))))
3463 (if (bound-and-true-p ido-enable-virtual-buffers)
3464 (ido-add-virtual-buffers-to-list))
3465 (run-hooks 'ido-make-buffer-list-hook)
3466 ido-temp-list))
3467
3468 (defun ido-add-virtual-buffers-to-list ()
3469 "Add recently visited files, and bookmark files, to the buffer list.
3470 This is to make them appear as if they were \"virtual buffers\"."
3471 ;; If no buffers matched, and virtual buffers are being used, then
3472 ;; consult the list of past visited files, to see if we can find
3473 ;; the file which the user might thought was still open.
3474 (unless recentf-mode (recentf-mode 1))
3475 (setq ido-virtual-buffers nil)
3476 (let (name)
3477 (dolist (head recentf-list)
3478 (setq name (file-name-nondirectory head))
3479 ;; In case HEAD is a directory with trailing /. See bug#14552.
3480 (when (equal name "")
3481 (setq name (file-name-nondirectory (directory-file-name head))))
3482 (when (equal name "")
3483 (setq name head))
3484 (and (not (equal name ""))
3485 (null (get-file-buffer head))
3486 (not (assoc name ido-virtual-buffers))
3487 (not (member name ido-temp-list))
3488 (not (ido-ignore-item-p name ido-ignore-buffers))
3489 ;;(file-exists-p head)
3490 (push (cons name head) ido-virtual-buffers))))
3491 (when ido-virtual-buffers
3492 (if ido-use-faces
3493 (dolist (comp ido-virtual-buffers)
3494 (put-text-property 0 (length (car comp))
3495 'face 'ido-virtual
3496 (car comp))))
3497 (setq ido-temp-list
3498 (nconc ido-temp-list
3499 (nreverse (mapcar #'car ido-virtual-buffers))))))
3500
3501 (defun ido-make-choice-list (default)
3502 "Return the current list of choices.
3503 If DEFAULT is non-nil, and corresponds to an element of choices,
3504 it is put to the start of the list."
3505 (let ((ido-temp-list ido-choice-list))
3506 (if default
3507 (progn
3508 (setq ido-temp-list
3509 (delete default ido-temp-list))
3510 (setq ido-temp-list
3511 (cons default ido-temp-list))))
3512 ; (run-hooks 'ido-make-choice-list-hook)
3513 ido-temp-list))
3514
3515 (defun ido-to-end (items)
3516 "Move the elements from ITEMS to the end of `ido-temp-list'."
3517 (mapc
3518 (lambda (elem)
3519 (setq ido-temp-list (delq elem ido-temp-list)))
3520 items)
3521 (if ido-temp-list
3522 (nconc ido-temp-list items)
3523 (setq ido-temp-list items)))
3524
3525 (defun ido-file-name-all-completions-1 (dir)
3526 (cond
3527 ((ido-nonreadable-directory-p dir) '())
3528 ;; do not check (ido-directory-too-big-p dir) here.
3529 ;; Caller must have done that if necessary.
3530
3531 ((and ido-enable-tramp-completion
3532 (string-match "\\`/[^/]+[:@]\\'" dir))
3533 ;; Strip method:user@host: part of tramp completions.
3534 ;; Tramp completions do not include leading slash.
3535 (let* ((len (1- (length dir)))
3536 (non-essential t)
3537 (compl
3538 (or (file-name-all-completions "" dir)
3539 ;; work around bug in ange-ftp.
3540 ;; /ftp:user@host: => nil
3541 ;; /ftp:user@host:./ => ok
3542 (and
3543 (not (string= "/ftp:" dir))
3544 (file-remote-p dir)
3545 ;; tramp-ftp-file-name-p is available only when tramp
3546 ;; has been loaded.
3547 (fboundp 'tramp-ftp-file-name-p)
3548 (funcall 'tramp-ftp-file-name-p dir)
3549 (string-match ":\\'" dir)
3550 (file-name-all-completions "" (concat dir "./"))))))
3551 (if (and compl
3552 (> (length (car compl)) len)
3553 (string= (substring (car compl) 0 len) (substring dir 1)))
3554 (mapcar (lambda (c) (substring c len)) compl)
3555 compl)))
3556 (t
3557 (file-name-all-completions "" dir))))
3558
3559 (defun ido-file-name-all-completions (dir)
3560 "Return name of all files in DIR.
3561 Uses and updates `ido-dir-file-cache'."
3562 (cond
3563 ((ido-is-unc-root dir)
3564 (mapcar
3565 (lambda (host)
3566 (if (string-match "/\\'" host) host (concat host "/")))
3567 (ido-unc-hosts t)))
3568 ((and (numberp ido-max-dir-file-cache) (> ido-max-dir-file-cache 0)
3569 (stringp dir) (> (length dir) 0)
3570 (ido-may-cache-directory dir))
3571 (let* ((cached (assoc dir ido-dir-file-cache))
3572 (ctime (nth 1 cached))
3573 (ftp (ido-is-ftp-directory dir))
3574 (unc (ido-is-unc-host dir))
3575 (attr (if (or ftp unc) nil (file-attributes dir)))
3576 (mtime (nth 5 attr))
3577 valid)
3578 (when cached ; should we use the cached entry ?
3579 (cond
3580 (ftp
3581 (setq valid (and (eq (car ctime) 'ftp)
3582 (ido-cache-ftp-valid (cdr ctime)))))
3583 (unc
3584 (setq valid (and (eq (car ctime) 'unc)
3585 (ido-cache-unc-valid (cdr ctime)))))
3586 (t
3587 (if attr
3588 (setq valid (and (= (car ctime) (car mtime))
3589 (= (car (cdr ctime)) (car (cdr mtime))))))))
3590 (unless valid
3591 (setq ido-dir-file-cache (delq cached ido-dir-file-cache)
3592 cached nil)))
3593 (unless cached
3594 (cond
3595 (unc
3596 (setq mtime (cons 'unc (ido-time-stamp))))
3597 ((and ftp (file-readable-p dir))
3598 (setq mtime (cons 'ftp (ido-time-stamp)))))
3599 (if mtime
3600 (setq cached (cons dir (cons mtime (ido-file-name-all-completions-1 dir)))
3601 ido-dir-file-cache (cons cached ido-dir-file-cache)))
3602 (if (> (length ido-dir-file-cache) ido-max-dir-file-cache)
3603 (setcdr (nthcdr (1- ido-max-dir-file-cache) ido-dir-file-cache) nil)))
3604 (and cached
3605 (cdr (cdr cached)))))
3606 (t
3607 (ido-file-name-all-completions-1 dir))))
3608
3609 (defun ido-remove-cached-dir (dir)
3610 "Remove DIR from `ido-dir-file-cache'."
3611 (if (and ido-dir-file-cache
3612 (stringp dir) (> (length dir) 0))
3613 (let ((cached (assoc dir ido-dir-file-cache)))
3614 (if cached
3615 (setq ido-dir-file-cache (delq cached ido-dir-file-cache))))))
3616
3617
3618 (defun ido-make-file-list-1 (dir &optional merged)
3619 "Return list of non-ignored files in DIR
3620 If MERGED is non-nil, each file is cons'ed with DIR."
3621 (and (or (ido-is-tramp-root dir) (ido-is-unc-root dir)
3622 (file-directory-p dir))
3623 (delq nil
3624 (mapcar
3625 (lambda (name)
3626 (if (not (ido-ignore-item-p name ido-ignore-files t))
3627 (if merged (cons name dir) name)))
3628 (ido-file-name-all-completions dir)))))
3629
3630 (defun ido-make-file-list (default)
3631 "Return the current list of files.
3632 Currently visible files are put at the end of the list.
3633 The hook `ido-make-file-list-hook' is run after the list has been
3634 created to allow the user to further modify the order of the file names
3635 in this list."
3636 (let ((ido-temp-list (ido-make-file-list-1 ido-current-directory)))
3637 (setq ido-temp-list (sort ido-temp-list
3638 (if ido-file-extensions-order
3639 #'ido-file-extension-lessp
3640 #'ido-file-lessp)))
3641 (unless (ido-is-tramp-root ido-current-directory)
3642 (let ((default-directory ido-current-directory))
3643 (ido-to-end ;; move ftp hosts and visited files to end
3644 (delq nil (mapcar
3645 (lambda (x) (if (or (and (string-match ".:\\'" x)
3646 (not (ido-local-file-exists-p x)))
3647 (and (not (ido-final-slash x))
3648 (let (file-name-handler-alist)
3649 (get-file-buffer x)))) x))
3650 ido-temp-list)))))
3651 (ido-to-end ;; move . files to end
3652 (delq nil (mapcar
3653 (lambda (x) (if (string-equal (substring x 0 1) ".") x))
3654 ido-temp-list)))
3655 (if (and default (member default ido-temp-list))
3656 (if (or ido-rotate-temp ido-rotate-file-list-default)
3657 (unless (equal default (car ido-temp-list))
3658 (let ((l ido-temp-list) k)
3659 (while (and l (cdr l) (not (equal default (car (cdr l)))))
3660 (setq l (cdr l)))
3661 (setq k (cdr l))
3662 (setcdr l nil)
3663 (nconc k ido-temp-list)
3664 (setq ido-temp-list k)))
3665 (setq ido-temp-list
3666 (delete default ido-temp-list))
3667 (setq ido-temp-list
3668 (cons default ido-temp-list))))
3669 (when ido-show-dot-for-dired
3670 (setq ido-temp-list (delete "." ido-temp-list))
3671 (setq ido-temp-list (cons "." ido-temp-list)))
3672 (run-hooks 'ido-make-file-list-hook)
3673 ido-temp-list))
3674
3675 (defun ido-make-dir-list-1 (dir &optional merged)
3676 "Return list of non-ignored subdirs in DIR.
3677 If MERGED is non-nil, each subdir is cons'ed with DIR."
3678 (and (or (ido-is-tramp-root dir) (file-directory-p dir))
3679 (delq nil
3680 (mapcar
3681 (lambda (name)
3682 (and (ido-final-slash name) (not (ido-ignore-item-p name ido-ignore-directories))
3683 (if merged (cons name dir) name)))
3684 (ido-file-name-all-completions dir)))))
3685
3686 (defun ido-make-dir-list (default)
3687 "Return the current list of directories.
3688 The hook `ido-make-dir-list-hook' is run after the list has been
3689 created to allow the user to further modify the order of the
3690 directory names in this list."
3691 (let ((ido-temp-list (ido-make-dir-list-1 ido-current-directory)))
3692 (setq ido-temp-list (sort ido-temp-list #'ido-file-lessp))
3693 (ido-to-end ;; move . files to end
3694 (delq nil (mapcar
3695 (lambda (x) (if (string-equal (substring x 0 1) ".") x))
3696 ido-temp-list)))
3697 (if (and default (member default ido-temp-list))
3698 (if (or ido-rotate-temp ido-rotate-file-list-default)
3699 (unless (equal default (car ido-temp-list))
3700 (let ((l ido-temp-list) k)
3701 (while (and l (cdr l) (not (equal default (car (cdr l)))))
3702 (setq l (cdr l)))
3703 (setq k (cdr l))
3704 (setcdr l nil)
3705 (nconc k ido-temp-list)
3706 (setq ido-temp-list k)))
3707 (setq ido-temp-list
3708 (delete default ido-temp-list))
3709 (setq ido-temp-list
3710 (cons default ido-temp-list))))
3711 (setq ido-temp-list (delete "." ido-temp-list))
3712 (unless ido-input-stack
3713 (setq ido-temp-list (cons "." ido-temp-list)))
3714 (run-hooks 'ido-make-dir-list-hook)
3715 ido-temp-list))
3716
3717 ;; List of the files visible in the current frame.
3718 (defvar ido-bufs-in-frame)
3719
3720 (defun ido-get-buffers-in-frames (&optional current)
3721 "Return the list of buffers that are visible in the current frame.
3722 If optional argument CURRENT is given, restrict searching to the current
3723 frame, rather than all frames, regardless of value of `ido-all-frames'."
3724 (let ((ido-bufs-in-frame nil))
3725 (walk-windows 'ido-get-bufname nil
3726 (if current
3727 nil
3728 ido-all-frames))
3729 ido-bufs-in-frame))
3730
3731 (defun ido-get-bufname (win)
3732 "Used by `ido-get-buffers-in-frames' to walk through all windows."
3733 (let ((buf (buffer-name (window-buffer win))))
3734 (unless (or (member buf ido-bufs-in-frame)
3735 (member buf ido-ignore-item-temp-list))
3736 ;; Only add buf if it is not already in list.
3737 ;; This prevents same buf in two different windows being
3738 ;; put into the list twice.
3739 (setq ido-bufs-in-frame
3740 (cons buf ido-bufs-in-frame)))))
3741
3742 ;;; FIND MATCHING ITEMS
3743
3744 (defun ido-set-matches-1 (items &optional do-full)
3745 "Return list of matches in ITEMS."
3746 (let* ((case-fold-search ido-case-fold)
3747 (slash (and (not ido-enable-prefix) (ido-final-slash ido-text)))
3748 (text (if slash (substring ido-text 0 -1) ido-text))
3749 (rex0 (if ido-enable-regexp text (regexp-quote text)))
3750 (rexq (concat rex0 (if slash ".*/" "")))
3751 (re (if ido-enable-prefix (concat "\\`" rexq) rexq))
3752 (full-re (and do-full
3753 (not (and (eq ido-cur-item 'buffer)
3754 ido-buffer-disable-smart-matches))
3755 (not ido-enable-regexp)
3756 (not (string-match "\$\\'" rex0))
3757 (concat "\\`" rex0 (if slash "/" "") "\\'")))
3758 (suffix-re (and do-full slash
3759 (not (and (eq ido-cur-item 'buffer)
3760 ido-buffer-disable-smart-matches))
3761 (not ido-enable-regexp)
3762 (not (string-match "\$\\'" rex0))
3763 (concat rex0 "/\\'")))
3764 (prefix-re (and full-re (not ido-enable-prefix)
3765 (concat "\\`" rexq)))
3766 (non-prefix-dot (or (not ido-enable-dot-prefix)
3767 (not ido-process-ignore-lists)
3768 ido-enable-prefix
3769 (= (length ido-text) 0)))
3770 full-matches suffix-matches prefix-matches matches)
3771 (setq ido-incomplete-regexp nil)
3772 (condition-case error
3773 (mapc
3774 (lambda (item)
3775 (let ((name (ido-name item)))
3776 (if (and (or non-prefix-dot
3777 (if (= (aref ido-text 0) ?.)
3778 (= (aref name 0) ?.)
3779 (/= (aref name 0) ?.)))
3780 (string-match re name))
3781 (cond
3782 ((and (eq ido-cur-item 'buffer)
3783 (or (not (stringp ido-default-item))
3784 (not (string= name ido-default-item)))
3785 (string= name (buffer-name ido-entry-buffer)))
3786 (setq matches (cons item matches)))
3787 ((and full-re (string-match full-re name))
3788 (setq full-matches (cons item full-matches)))
3789 ((and suffix-re (string-match suffix-re name))
3790 (setq suffix-matches (cons item suffix-matches)))
3791 ((and prefix-re (string-match prefix-re name))
3792 (setq prefix-matches (cons item prefix-matches)))
3793 (t (setq matches (cons item matches))))))
3794 t)
3795 items)
3796 (invalid-regexp
3797 (setq ido-incomplete-regexp t
3798 ;; Consider the invalid regexp message internally as a
3799 ;; special-case single match, and handle appropriately
3800 ;; elsewhere.
3801 matches (cdr error))))
3802 (when prefix-matches
3803 (ido-trace "prefix match" prefix-matches)
3804 ;; Bug#2042.
3805 (setq matches (nconc prefix-matches matches)))
3806 (when suffix-matches
3807 (ido-trace "suffix match" (list text suffix-re suffix-matches))
3808 (setq matches (nconc suffix-matches matches)))
3809 (when full-matches
3810 (ido-trace "full match" (list text full-re full-matches))
3811 (setq matches (nconc full-matches matches)))
3812 (when (and (null matches)
3813 ido-enable-flex-matching
3814 (> (length ido-text) 1)
3815 (not ido-enable-regexp))
3816 (setq re (concat (regexp-quote (string (aref ido-text 0)))
3817 (mapconcat (lambda (c)
3818 (concat "[^" (string c) "]*"
3819 (regexp-quote (string c))))
3820 (substring ido-text 1) "")))
3821 (if ido-enable-prefix
3822 (setq re (concat "\\`" re)))
3823 (mapc
3824 (lambda (item)
3825 (let ((name (ido-name item)))
3826 (if (string-match re name)
3827 (setq matches (cons item matches)))))
3828 items))
3829 (delete-consecutive-dups matches t)))
3830
3831
3832 (defun ido-set-matches ()
3833 "Set `ido-matches' to the list of items matching prompt."
3834 (when ido-rescan
3835 (setq ido-matches (ido-set-matches-1 (reverse ido-cur-list) (not ido-rotate))
3836 ido-rotate nil)))
3837
3838 (defun ido-ignore-item-p (name re-list &optional ignore-ext)
3839 "Return t if the buffer or file NAME should be ignored."
3840 (or (member name ido-ignore-item-temp-list)
3841 (and
3842 ido-process-ignore-lists re-list
3843 (save-match-data
3844 (let ((ext-list (and ignore-ext ido-ignore-extensions
3845 completion-ignored-extensions))
3846 (case-fold-search ido-case-fold)
3847 ignorep nextstr
3848 (flen (length name)) slen)
3849 (while ext-list
3850 (setq nextstr (car ext-list))
3851 (if (cond
3852 ((stringp nextstr)
3853 (and (>= flen (setq slen (length nextstr)))
3854 (string-equal (substring name (- flen slen)) nextstr)))
3855 ((functionp nextstr) (funcall nextstr name))
3856 (t nil))
3857 (setq ignorep t
3858 ext-list nil
3859 re-list nil)
3860 (setq ext-list (cdr ext-list))))
3861 (while re-list
3862 (setq nextstr (car re-list))
3863 (if (cond
3864 ((stringp nextstr) (string-match nextstr name))
3865 ((functionp nextstr) (funcall nextstr name))
3866 (t nil))
3867 (setq ignorep t
3868 re-list nil)
3869 (setq re-list (cdr re-list))))
3870 ;; return the result
3871 (if ignorep
3872 (setq ido-ignored-list (cons name ido-ignored-list)))
3873 ignorep)))))
3874
3875 ;; Private variable used by `ido-word-matching-substring'.
3876 (defvar ido-change-word-sub)
3877
3878 (defun ido-find-common-substring (items subs)
3879 "Return common string following SUBS in each element of ITEMS."
3880 (let (res
3881 alist
3882 ido-change-word-sub)
3883 (setq ido-change-word-sub
3884 (if ido-enable-regexp
3885 subs
3886 (regexp-quote subs)))
3887 (setq res (mapcar #'ido-word-matching-substring items))
3888 (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen)
3889 (setq alist (mapcar #'ido-makealist res)) ;; could use an OBARRAY
3890
3891 ;; try-completion returns t if there is an exact match.
3892 (let* ((completion-ignore-case ido-case-fold)
3893 (comp (try-completion subs alist)))
3894 (if (eq comp t)
3895 subs
3896 comp))))
3897
3898 (defun ido-word-matching-substring (word)
3899 "Return part of WORD before first match to `ido-change-word-sub'.
3900 If `ido-change-word-sub' cannot be found in WORD, return nil."
3901 (let ((case-fold-search ido-case-fold))
3902 (let ((m (string-match ido-change-word-sub (ido-name word))))
3903 (if m
3904 (substring (ido-name word) m)
3905 ;; else no match
3906 nil))))
3907
3908 (defun ido-makealist (res)
3909 "Return dotted pair (RES . 1)."
3910 (cons res 1))
3911
3912 (defun ido-choose-completion-string (choice &rest ignored)
3913 (when (ido-active)
3914 ;; Insert the completion into the buffer where completion was requested.
3915 (and ido-completion-buffer
3916 (get-buffer ido-completion-buffer)
3917 (kill-buffer ido-completion-buffer))
3918 (cond
3919 ((ido-active t) ;; ido-use-merged-list
3920 (setq ido-current-directory ""
3921 ido-text choice
3922 ido-exit 'done))
3923 ((not (ido-final-slash choice))
3924 (setq ido-text choice
3925 ido-exit 'done))
3926 (t
3927 (ido-set-current-directory ido-current-directory choice)
3928 (setq ido-exit 'refresh)))
3929 (exit-minibuffer)
3930 t))
3931
3932 (defun ido-completion-help ()
3933 "Show possible completions in a *File Completions* buffer."
3934 (interactive)
3935 (setq ido-rescan nil)
3936 (let ((temp-buf (and ido-completion-buffer
3937 (get-buffer ido-completion-buffer)))
3938 display-it full-list)
3939 (if (and (eq last-command this-command) temp-buf)
3940 ;; scroll buffer
3941 (let (win (buf (current-buffer)))
3942 (display-buffer temp-buf nil nil)
3943 (set-buffer temp-buf)
3944 (setq win (get-buffer-window temp-buf))
3945 (if (pos-visible-in-window-p (point-max) win)
3946 (if (or ido-completion-buffer-all-completions
3947 (boundp 'ido-completion-buffer-full))
3948 (set-window-start win (point-min))
3949 (with-no-warnings
3950 (set (make-local-variable 'ido-completion-buffer-full) t))
3951 (setq full-list t
3952 display-it t))
3953 (scroll-other-window))
3954 (set-buffer buf))
3955 (setq display-it t))
3956 (if (and ido-completion-buffer display-it)
3957 (with-output-to-temp-buffer ido-completion-buffer
3958 (let ((completion-list (sort
3959 (cond
3960 (ido-directory-too-big
3961 (message "Reading directory...")
3962 (setq ido-directory-too-big nil
3963 ido-ignored-list nil
3964 ido-cur-list (ido-all-completions)
3965 ido-rescan t)
3966 (ido-set-matches)
3967 (or ido-matches ido-cur-list))
3968 (ido-use-merged-list
3969 (ido-flatten-merged-list (or ido-matches ido-cur-list)))
3970 ((or full-list ido-completion-buffer-all-completions)
3971 (ido-all-completions))
3972 (t
3973 (copy-sequence (or ido-matches ido-cur-list))))
3974 #'ido-file-lessp)))
3975 (if (featurep 'xemacs)
3976 ;; XEmacs extents are put on by default, doesn't seem to be
3977 ;; any way of switching them off.
3978 (display-completion-list
3979 completion-list
3980 :help-string "ido "
3981 :activate-callback
3982 (lambda (&rest _) (message "Doesn't work yet, sorry!")))
3983 ;; else running Emacs
3984 ;;(add-hook 'completion-setup-hook 'completion-setup-function)
3985 (display-completion-list completion-list)))))))
3986
3987 ;;; KILL CURRENT BUFFER
3988 (defun ido-kill-buffer-at-head ()
3989 "Kill the buffer at the head of `ido-matches'.
3990 If cursor is not at the end of the user input, delete to end of input."
3991 (interactive)
3992 (if (not (eobp))
3993 (delete-region (point) (line-end-position))
3994 (let ((enable-recursive-minibuffers t)
3995 (buf (ido-name (car ido-matches)))
3996 (nextbuf (cadr ido-matches)))
3997 (cond
3998 ((get-buffer buf)
3999 ;; If next match names a buffer use the buffer object; buffer
4000 ;; name may be changed by packages such as uniquify.
4001 (when (and nextbuf (get-buffer nextbuf))
4002 (setq nextbuf (get-buffer nextbuf)))
4003 (if (null (kill-buffer buf))
4004 ;; Buffer couldn't be killed.
4005 (setq ido-rescan t)
4006 ;; Else `kill-buffer' succeeds so re-make the buffer list
4007 ;; taking into account packages like uniquify may rename
4008 ;; buffers.
4009 (if (bufferp nextbuf)
4010 (setq nextbuf (buffer-name nextbuf)))
4011 (setq ido-default-item nextbuf
4012 ido-text-init ido-text
4013 ido-exit 'refresh)
4014 (exit-minibuffer)))
4015 ;; Handle virtual buffers
4016 ((assoc buf ido-virtual-buffers)
4017 (setq recentf-list
4018 (delete (cdr (assoc buf ido-virtual-buffers)) recentf-list))
4019 (setq ido-cur-list (delete buf ido-cur-list))
4020 (setq ido-rescan t))))))
4021
4022 ;;; DELETE CURRENT FILE
4023 (defun ido-delete-file-at-head ()
4024 "Delete the file at the head of `ido-matches'.
4025 Trash the file if `delete-by-moving-to-trash' is non-nil.
4026 If cursor is not at the end of the user input, delete to end of input."
4027 (interactive)
4028 (if (not (eobp))
4029 (delete-region (point) (line-end-position))
4030 (let ((enable-recursive-minibuffers t)
4031 (file (ido-name (car ido-matches))))
4032 (if file
4033 (setq file (concat ido-current-directory file)))
4034 (when (and file
4035 (file-exists-p file)
4036 (not (file-directory-p file))
4037 (file-writable-p ido-current-directory)
4038 (or delete-by-moving-to-trash
4039 (yes-or-no-p (concat "Delete " file "? "))))
4040 (delete-file file 'trash)
4041 ;; Check if file still exists.
4042 (if (file-exists-p file)
4043 ;; file could not be deleted
4044 (setq ido-rescan t)
4045 ;; else file was killed so remove name from list.
4046 (setq ido-cur-list (delq (car ido-matches) ido-cur-list)))))))
4047
4048
4049 ;;; VISIT CHOSEN BUFFER
4050 (defun ido-visit-buffer (buffer method &optional record)
4051 "Switch to BUFFER according to METHOD.
4052 Record command in `command-history' if optional RECORD is non-nil."
4053 (if (bufferp buffer)
4054 (setq buffer (buffer-name buffer)))
4055 (let (win newframe)
4056 (cond
4057 ((eq method 'kill)
4058 (if record
4059 (ido-record-command 'kill-buffer buffer))
4060 (kill-buffer buffer))
4061
4062 ((eq method 'other-window)
4063 (if record
4064 (ido-record-command 'switch-to-buffer buffer))
4065 (switch-to-buffer-other-window buffer))
4066
4067 ((eq method 'display)
4068 (display-buffer buffer))
4069
4070 ((eq method 'other-frame)
4071 (switch-to-buffer-other-frame buffer)
4072 (select-frame-set-input-focus (selected-frame)))
4073
4074 ((and (memq method '(raise-frame maybe-frame))
4075 window-system
4076 (setq win (ido-buffer-window-other-frame buffer))
4077 (or (eq method 'raise-frame)
4078 (y-or-n-p "Jump to frame? ")))
4079 (setq newframe (window-frame win))
4080 (select-frame-set-input-focus newframe)
4081 (select-window win))
4082
4083 ;; (eq method 'selected-window)
4084 (t
4085 ;; No buffer in other frames...
4086 (if record
4087 (ido-record-command 'switch-to-buffer buffer))
4088 (switch-to-buffer buffer)
4089 ))))
4090
4091
4092 (defun ido-buffer-window-other-frame (buffer)
4093 "Return window pointer if BUFFER is visible in another frame.
4094 If BUFFER is visible in the current frame, return nil."
4095 (let ((blist (ido-get-buffers-in-frames 'current)))
4096 ;;If the buffer is visible in current frame, return nil
4097 (if (member buffer blist)
4098 nil
4099 ;; maybe in other frame or icon
4100 (get-buffer-window buffer 0) ; better than 'visible
4101 )))
4102
4103
4104 ;;; ----------- IDONIZED FUNCTIONS ------------
4105
4106 ;;;###autoload
4107 (defun ido-switch-buffer ()
4108 "Switch to another buffer.
4109 The buffer is displayed according to `ido-default-buffer-method' -- the
4110 default is to show it in the same window, unless it is already visible
4111 in another frame.
4112
4113 As you type in a string, all of the buffers matching the string are
4114 displayed if substring-matching is used \(default). Look at
4115 `ido-enable-prefix' and `ido-toggle-prefix'. When you have found the
4116 buffer you want, it can then be selected. As you type, most keys have
4117 their normal keybindings, except for the following: \\<ido-buffer-completion-map>
4118
4119 RET Select the buffer at the front of the list of matches. If the
4120 list is empty, possibly prompt to create new buffer.
4121
4122 \\[ido-select-text] Use the current input string verbatim.
4123
4124 \\[ido-next-match] Put the first element at the end of the list.
4125 \\[ido-prev-match] Put the last element at the start of the list.
4126 \\[ido-complete] Complete a common suffix to the current string that
4127 matches all buffers. If there is only one match, select that buffer.
4128 If there is no common suffix, show a list of all matching buffers
4129 in a separate window.
4130 \\[ido-edit-input] Edit input string.
4131 \\[ido-fallback-command] Fallback to non-ido version of current command.
4132 \\[ido-toggle-regexp] Toggle regexp searching.
4133 \\[ido-toggle-prefix] Toggle between substring and prefix matching.
4134 \\[ido-toggle-case] Toggle case-sensitive searching of buffer names.
4135 \\[ido-completion-help] Show list of matching buffers in separate window.
4136 \\[ido-enter-find-file] Drop into `ido-find-file'.
4137 \\[ido-kill-buffer-at-head] Kill buffer at head of buffer list.
4138 \\[ido-toggle-ignore] Toggle ignoring buffers listed in `ido-ignore-buffers'."
4139 (interactive)
4140 (ido-buffer-internal ido-default-buffer-method))
4141
4142 ;;;###autoload
4143 (defun ido-switch-buffer-other-window ()
4144 "Switch to another buffer and show it in another window.
4145 The buffer name is selected interactively by typing a substring.
4146 For details of keybindings, see `ido-switch-buffer'."
4147 (interactive)
4148 (ido-buffer-internal 'other-window 'switch-to-buffer-other-window))
4149
4150 ;;;###autoload
4151 (defun ido-display-buffer ()
4152 "Display a buffer in another window but don't select it.
4153 The buffer name is selected interactively by typing a substring.
4154 For details of keybindings, see `ido-switch-buffer'."
4155 (interactive)
4156 (ido-buffer-internal 'display 'display-buffer nil nil nil 'ignore))
4157
4158 ;;;###autoload
4159 (defun ido-kill-buffer ()
4160 "Kill a buffer.
4161 The buffer name is selected interactively by typing a substring.
4162 For details of keybindings, see `ido-switch-buffer'."
4163 (interactive)
4164 (ido-buffer-internal 'kill 'kill-buffer "Kill buffer: " (buffer-name (current-buffer)) nil 'ignore))
4165
4166 ;;;###autoload
4167 (defun ido-insert-buffer ()
4168 "Insert contents of a buffer in current buffer after point.
4169 The buffer name is selected interactively by typing a substring.
4170 For details of keybindings, see `ido-switch-buffer'."
4171 (interactive)
4172 (ido-buffer-internal 'insert 'insert-buffer "Insert buffer: " nil nil 'ido-enter-insert-file))
4173
4174 ;;;###autoload
4175 (defun ido-switch-buffer-other-frame ()
4176 "Switch to another buffer and show it in another frame.
4177 The buffer name is selected interactively by typing a substring.
4178 For details of keybindings, see `ido-switch-buffer'."
4179 (interactive)
4180 (if ido-mode
4181 (ido-buffer-internal 'other-frame)
4182 (call-interactively 'switch-to-buffer-other-frame)))
4183
4184 ;;;###autoload
4185 (defun ido-find-file-in-dir (dir)
4186 "Switch to another file starting from DIR."
4187 (interactive "DDir: ")
4188 (setq dir (file-name-as-directory dir))
4189 (ido-file-internal ido-default-file-method nil dir nil nil nil 'ignore))
4190
4191 ;;;###autoload
4192 (defun ido-find-file ()
4193 "Edit file with name obtained via minibuffer.
4194 The file is displayed according to `ido-default-file-method' -- the
4195 default is to show it in the same window, unless it is already
4196 visible in another frame.
4197
4198 The file name is selected interactively by typing a substring. As you
4199 type in a string, all of the filenames matching the string are displayed
4200 if substring-matching is used \(default). Look at `ido-enable-prefix' and
4201 `ido-toggle-prefix'. When you have found the filename you want, it can
4202 then be selected. As you type, most keys have their normal keybindings,
4203 except for the following: \\<ido-file-completion-map>
4204
4205 RET Select the file at the front of the list of matches. If the
4206 list is empty, possibly prompt to create new file.
4207
4208 \\[ido-select-text] Use the current input string verbatim.
4209
4210 \\[ido-next-match] Put the first element at the end of the list.
4211 \\[ido-prev-match] Put the last element at the start of the list.
4212 \\[ido-complete] Complete a common suffix to the current string that
4213 matches all files. If there is only one match, select that file.
4214 If there is no common suffix, show a list of all matching files
4215 in a separate window.
4216 \\[ido-magic-delete-char] Open the specified directory in Dired mode.
4217 \\[ido-edit-input] Edit input string (including directory).
4218 \\[ido-prev-work-directory] or \\[ido-next-work-directory] go to previous/next directory in work directory history.
4219 \\[ido-merge-work-directories] search for file in the work directory history.
4220 \\[ido-forget-work-directory] removes current directory from the work directory history.
4221 \\[ido-prev-work-file] or \\[ido-next-work-file] cycle through the work file history.
4222 \\[ido-wide-find-file-or-pop-dir] and \\[ido-wide-find-dir-or-delete-dir] prompts and uses find to locate files or directories.
4223 \\[ido-make-directory] prompts for a directory to create in current directory.
4224 \\[ido-fallback-command] Fallback to non-ido version of current command.
4225 \\[ido-toggle-regexp] Toggle regexp searching.
4226 \\[ido-toggle-prefix] Toggle between substring and prefix matching.
4227 \\[ido-toggle-case] Toggle case-sensitive searching of file names.
4228 \\[ido-toggle-literal] Toggle literal reading of this file.
4229 \\[ido-completion-help] Show list of matching files in separate window.
4230 \\[ido-toggle-ignore] Toggle ignoring files listed in `ido-ignore-files'."
4231
4232 (interactive)
4233 (ido-file-internal ido-default-file-method))
4234
4235 ;;;###autoload
4236 (defun ido-find-file-other-window ()
4237 "Switch to another file and show it in another window.
4238 The file name is selected interactively by typing a substring.
4239 For details of keybindings, see `ido-find-file'."
4240 (interactive)
4241 (ido-file-internal 'other-window 'find-file-other-window))
4242
4243 ;;;###autoload
4244 (defun ido-find-alternate-file ()
4245 "Switch to another file and show it in another window.
4246 The file name is selected interactively by typing a substring.
4247 For details of keybindings, see `ido-find-file'."
4248 (interactive)
4249 (ido-file-internal 'alt-file 'find-alternate-file nil "Find alternate file: "))
4250
4251 ;;;###autoload
4252 (defun ido-find-file-read-only ()
4253 "Edit file read-only with name obtained via minibuffer.
4254 The file name is selected interactively by typing a substring.
4255 For details of keybindings, see `ido-find-file'."
4256 (interactive)
4257 (ido-file-internal 'read-only 'find-file-read-only nil "Find file read-only: "))
4258
4259 ;;;###autoload
4260 (defun ido-find-file-read-only-other-window ()
4261 "Edit file read-only in other window with name obtained via minibuffer.
4262 The file name is selected interactively by typing a substring.
4263 For details of keybindings, see `ido-find-file'."
4264 (interactive)
4265 (ido-file-internal 'read-only 'find-file-read-only-other-window nil "Find file read-only other window: "))
4266
4267 ;;;###autoload
4268 (defun ido-find-file-read-only-other-frame ()
4269 "Edit file read-only in other frame with name obtained via minibuffer.
4270 The file name is selected interactively by typing a substring.
4271 For details of keybindings, see `ido-find-file'."
4272 (interactive)
4273 (ido-file-internal 'read-only 'find-file-read-only-other-frame nil "Find file read-only other frame: "))
4274
4275 ;;;###autoload
4276 (defun ido-display-file ()
4277 "Display a file in another window but don't select it.
4278 The file name is selected interactively by typing a substring.
4279 For details of keybindings, see `ido-find-file'."
4280 (interactive)
4281 (ido-file-internal 'display nil nil nil nil nil 'ignore))
4282
4283 ;;;###autoload
4284 (defun ido-find-file-other-frame ()
4285 "Switch to another file and show it in another frame.
4286 The file name is selected interactively by typing a substring.
4287 For details of keybindings, see `ido-find-file'."
4288 (interactive)
4289 (ido-file-internal 'other-frame 'find-file-other-frame))
4290
4291 ;;;###autoload
4292 (defun ido-write-file ()
4293 "Write current buffer to a file.
4294 The file name is selected interactively by typing a substring.
4295 For details of keybindings, see `ido-find-file'."
4296 (interactive)
4297 (let ((ido-process-ignore-lists t)
4298 (ido-work-directory-match-only nil)
4299 (ido-ignore-files (cons "[^/]\\'" ido-ignore-files))
4300 (ido-report-no-match nil)
4301 (ido-confirm-unique-completion t)
4302 (ido-auto-merge-work-directories-length -1))
4303 (ido-file-internal 'write 'write-file nil "Write file: " nil nil 'ignore)))
4304
4305 ;;;###autoload
4306 (defun ido-insert-file ()
4307 "Insert contents of file in current buffer.
4308 The file name is selected interactively by typing a substring.
4309 For details of keybindings, see `ido-find-file'."
4310 (interactive)
4311 (ido-file-internal 'insert 'insert-file nil "Insert file: " nil nil 'ido-enter-insert-buffer))
4312
4313 ;;;###autoload
4314 (defun ido-dired ()
4315 "Call `dired' the ido way.
4316 The directory is selected interactively by typing a substring.
4317 For details of keybindings, see `ido-find-file'."
4318 (interactive)
4319 (let ((ido-report-no-match nil)
4320 (ido-auto-merge-work-directories-length -1))
4321 (ido-file-internal 'dired 'dired nil "Dired: " 'dir)))
4322
4323 (defun ido-list-directory ()
4324 "Call `list-directory' the ido way.
4325 The directory is selected interactively by typing a substring.
4326 For details of keybindings, see `ido-find-file'."
4327 (interactive)
4328 (let ((ido-report-no-match nil)
4329 (ido-auto-merge-work-directories-length -1))
4330 (ido-file-internal 'list-directory 'list-directory nil "List directory: " 'dir)))
4331
4332 ;;; XEmacs hack for showing default buffer
4333
4334 ;; The first time we enter the minibuffer, Emacs puts up the default
4335 ;; buffer to switch to, but XEmacs doesn't -- presumably there is a
4336 ;; subtle difference in the two versions of post-command-hook. The
4337 ;; default is shown for both whenever we delete all of our text
4338 ;; though, indicating its just a problem the first time we enter the
4339 ;; function. To solve this, we use another entry hook for emacs to
4340 ;; show the default the first time we enter the minibuffer.
4341
4342
4343 ;;; ICOMPLETE TYPE CODE
4344
4345 (defun ido-initiate-auto-merge (buffer)
4346 (ido-trace "\n*merge timeout*" buffer)
4347 (setq ido-auto-merge-timer nil)
4348 (when (and (buffer-live-p buffer)
4349 (ido-active)
4350 (boundp 'ido-eoinput) ido-eoinput)
4351 (let ((contents (buffer-substring-no-properties (minibuffer-prompt-end) ido-eoinput)))
4352 (ido-trace "request merge")
4353 (setq ido-use-merged-list 'auto
4354 ido-text-init contents
4355 ido-rotate-temp t
4356 ido-exit 'refresh)
4357 (with-current-buffer buffer
4358 (ido-tidy))
4359 (throw 'ido contents))))
4360
4361 (defun ido-exhibit ()
4362 "Post command hook for `ido'."
4363 ;; Find matching files and display a list in the minibuffer.
4364 ;; Copied from `icomplete-exhibit' with two changes:
4365 ;; 1. It prints a default file name when there is no text yet entered.
4366 ;; 2. It calls my completion routine rather than the standard completion.
4367
4368 (when (ido-active)
4369 (let ((contents (buffer-substring-no-properties (minibuffer-prompt-end) (point-max)))
4370 (buffer-undo-list t)
4371 try-single-dir-match
4372 refresh)
4373
4374 (when ido-trace-enable
4375 (ido-trace "\nexhibit" this-command)
4376 (ido-trace "dir" ido-current-directory)
4377 (ido-trace "contents" contents)
4378 (ido-trace "list" ido-cur-list)
4379 (ido-trace "matches" ido-matches)
4380 (ido-trace "rescan" ido-rescan))
4381
4382 (save-excursion
4383 (goto-char (point-max))
4384 ;; Register the end of input, so we know where the extra stuff (match-status info) begins:
4385 (unless (boundp 'ido-eoinput)
4386 ;; In case it got wiped out by major mode business:
4387 (make-local-variable 'ido-eoinput))
4388 (setq ido-eoinput (point))
4389
4390 ;; Handle explicit directory changes
4391 (cond
4392 ((memq ido-cur-item '(buffer list))
4393 )
4394
4395 ((= (length contents) 0)
4396 )
4397
4398 ((= (length contents) 1)
4399 (cond
4400 ((and (ido-is-tramp-root) (string-equal contents "/"))
4401 (ido-set-current-directory ido-current-directory contents)
4402 (setq refresh t))
4403 ((and (ido-unc-hosts) (string-equal contents "/")
4404 (let ((ido-enable-tramp-completion nil))
4405 (ido-is-root-directory)))
4406 (ido-set-current-directory "//")
4407 (setq refresh t))
4408 ))
4409
4410 ((and (string-match (if ido-enable-tramp-completion ".[:@]\\'" ".:\\'") contents)
4411 (ido-is-root-directory) ;; Ange-ftp or tramp
4412 (not (ido-local-file-exists-p contents)))
4413 (ido-set-current-directory ido-current-directory contents)
4414 (when (ido-is-slow-ftp-host)
4415 (setq ido-exit 'fallback)
4416 (exit-minibuffer))
4417 (setq refresh t))
4418
4419 ((ido-final-slash contents) ;; xxx/
4420 (ido-trace "final slash" contents)
4421 (cond
4422 ((string-equal contents "~/")
4423 (ido-set-current-home)
4424 (setq refresh t))
4425 ((string-equal contents "../")
4426 (ido-up-directory t)
4427 (setq refresh t))
4428 ((string-equal contents "./")
4429 (setq refresh t))
4430 ((string-match "\\`~[-_a-zA-Z0-9]+[$]?/\\'" contents)
4431 (ido-trace "new home" contents)
4432 (ido-set-current-home contents)
4433 (setq refresh t))
4434 ((string-match "[$][A-Za-z0-9_]+/\\'" contents)
4435 (let ((exp (condition-case ()
4436 (expand-file-name
4437 (substitute-in-file-name (substring contents 0 -1))
4438 ido-current-directory)
4439 (error nil))))
4440 (ido-trace contents exp)
4441 (when (and exp (file-directory-p exp))
4442 (ido-set-current-directory (file-name-directory exp))
4443 (setq ido-text-init (file-name-nondirectory exp))
4444 (setq refresh t))))
4445 ((and (memq system-type '(windows-nt ms-dos))
4446 (string-equal (substring contents 1) ":/"))
4447 (ido-set-current-directory (file-name-directory contents))
4448 (setq refresh t))
4449 ((string-equal (substring contents -2 -1) "/")
4450 (ido-set-current-directory
4451 (if (memq system-type '(windows-nt ms-dos))
4452 (expand-file-name "/" ido-current-directory)
4453 "/"))
4454 (setq refresh t))
4455 ((and (or ido-directory-nonreadable ido-directory-too-big)
4456 (file-directory-p (concat ido-current-directory (file-name-directory contents))))
4457 (ido-set-current-directory
4458 (concat ido-current-directory (file-name-directory contents)))
4459 (setq refresh t))
4460 (t
4461 (ido-trace "try single dir")
4462 (setq try-single-dir-match t))))
4463
4464 ((and (string-equal (substring contents -2 -1) "/")
4465 (not (string-match "[$]" contents)))
4466 (ido-set-current-directory
4467 (cond
4468 ((= (length contents) 2)
4469 "/")
4470 (ido-matches
4471 (concat ido-current-directory (ido-name (car ido-matches))))
4472 (t
4473 (concat ido-current-directory (substring contents 0 -1)))))
4474 (setq ido-text-init (substring contents -1))
4475 (setq refresh t))
4476
4477 ((and (not ido-use-merged-list)
4478 (not (ido-final-slash contents))
4479 (eq ido-try-merged-list t)
4480 (numberp ido-auto-merge-work-directories-length)
4481 (> ido-auto-merge-work-directories-length 0)
4482 (= (length contents) ido-auto-merge-work-directories-length)
4483 (not (and ido-auto-merge-inhibit-characters-regexp
4484 (string-match ido-auto-merge-inhibit-characters-regexp contents)))
4485 (not (input-pending-p)))
4486 (setq ido-use-merged-list 'auto
4487 ido-text-init contents
4488 ido-rotate-temp t)
4489 (setq refresh t))
4490
4491 (t nil))
4492
4493 (when refresh
4494 (ido-trace "refresh on /" ido-text-init)
4495 (setq ido-exit 'refresh)
4496 (exit-minibuffer))
4497
4498 (when (and ido-enter-matching-directory
4499 ido-matches
4500 (or (eq ido-enter-matching-directory 'first)
4501 (null (cdr ido-matches)))
4502 (ido-final-slash (ido-name (car ido-matches)))
4503 (or try-single-dir-match
4504 (eq ido-enter-matching-directory t)))
4505 (ido-trace "single match" (car ido-matches))
4506 (ido-set-current-directory
4507 (concat ido-current-directory (ido-name (car ido-matches))))
4508 (setq ido-exit 'refresh)
4509 (exit-minibuffer))
4510
4511 ;; Update the list of matches
4512 (setq ido-text contents)
4513 (ido-set-matches)
4514 (ido-trace "new " ido-matches)
4515
4516 (when (and (boundp 'ido-enable-virtual-buffers)
4517 (not (eq ido-enable-virtual-buffers 'always))
4518 (eq ido-cur-item 'buffer)
4519 (eq ido-use-virtual-buffers 'auto))
4520
4521 (when (and (not ido-enable-virtual-buffers)
4522 (not ido-matches))
4523 (setq ido-text-init ido-text)
4524 (setq ido-enable-virtual-buffers t)
4525 (setq ido-exit 'refresh)
4526 (exit-minibuffer))
4527
4528 ;; If input matches real buffers turn off virtual buffers.
4529 (when (and ido-enable-virtual-buffers
4530 ido-matches
4531 (ido-set-matches-1 (ido-make-buffer-list-1)))
4532 (setq ido-enable-virtual-buffers nil)
4533 (setq ido-text-init ido-text)
4534 (setq ido-exit 'refresh)
4535 (exit-minibuffer)))
4536
4537 (when (and (not ido-matches)
4538 (not ido-directory-nonreadable)
4539 (not ido-directory-too-big)
4540 ;; ido-rescan ?
4541 ido-process-ignore-lists
4542 ido-ignored-list)
4543 (let ((ido-process-ignore-lists nil)
4544 (ido-rotate ido-rotate)
4545 (ido-cur-list ido-ignored-list))
4546 (ido-trace "try all" ido-ignored-list)
4547 (ido-set-matches))
4548 (when ido-matches
4549 (ido-trace "found " ido-matches)
4550 (setq ido-rescan t)
4551 (setq ido-process-ignore-lists-inhibit t)
4552 (setq ido-text-init ido-text)
4553 (setq ido-exit 'refresh)
4554 (exit-minibuffer)))
4555
4556 (when (and
4557 ido-rescan
4558 (not ido-matches)
4559 (memq ido-cur-item '(file dir))
4560 (not (ido-is-root-directory))
4561 (> (length contents) 1)
4562 (not (string-match "[$]" contents))
4563 (not ido-directory-nonreadable)
4564 (not ido-directory-too-big))
4565 (ido-trace "merge?")
4566 (if ido-use-merged-list
4567 (ido-undo-merge-work-directory contents nil)
4568 (when (and (eq ido-try-merged-list t)
4569 (numberp ido-auto-merge-work-directories-length)
4570 (= ido-auto-merge-work-directories-length 0)
4571 (not (and ido-auto-merge-inhibit-characters-regexp
4572 (string-match ido-auto-merge-inhibit-characters-regexp contents)))
4573 (not (input-pending-p)))
4574 (ido-trace "\n*start timer*")
4575 (setq ido-auto-merge-timer
4576 (run-with-timer ido-auto-merge-delay-time nil 'ido-initiate-auto-merge (current-buffer))))))
4577
4578 (setq ido-rescan t)
4579
4580 (if (and ido-use-merged-list
4581 ido-matches
4582 (not (string-equal (car (cdr (car ido-matches))) ido-current-directory)))
4583 (progn
4584 (ido-set-current-directory (car (cdr (car ido-matches))))
4585 (setq ido-use-merged-list t
4586 ido-exit 'keep
4587 ido-text-init ido-text)
4588 (exit-minibuffer)))
4589
4590 ;; Insert the match-status information:
4591 (ido-set-common-completion)
4592 (let ((inf (ido-completions contents)))
4593 (setq ido-show-confirm-message nil)
4594 (ido-trace "inf" inf)
4595 (insert inf))
4596 ))))
4597
4598 (defun ido-completions (name)
4599 "Return the string that is displayed after the user's text.
4600 Modified from `icomplete-completions'."
4601 (let* ((comps ido-matches)
4602 (ind (and (consp (car comps)) (> (length (cdr (car comps))) 1)
4603 ido-merged-indicator))
4604 first)
4605
4606 (if (and ind ido-use-faces)
4607 (put-text-property 0 1 'face 'ido-indicator ind))
4608
4609 (if (and ido-use-faces comps)
4610 (let* ((fn (ido-name (car comps)))
4611 (ln (length fn)))
4612 (setq first (format "%s" fn))
4613 (put-text-property 0 ln 'face
4614 (if (= (length comps) 1)
4615 (if ido-incomplete-regexp
4616 'ido-incomplete-regexp
4617 'ido-only-match)
4618 'ido-first-match)
4619 first)
4620 (if ind (setq first (concat first ind)))
4621 (setq comps (cons first (cdr comps)))))
4622
4623 (cond ((null comps)
4624 (cond
4625 (ido-show-confirm-message
4626 (or (nth 10 ido-decorations) " [Confirm]"))
4627 (ido-directory-nonreadable
4628 (or (nth 8 ido-decorations) " [Not readable]"))
4629 (ido-directory-too-big
4630 (or (nth 9 ido-decorations) " [Too big]"))
4631 (ido-report-no-match
4632 (nth 6 ido-decorations)) ;; [No match]
4633 (t "")))
4634 (ido-incomplete-regexp
4635 (concat " " (car comps)))
4636 ((null (cdr comps)) ;one match
4637 (concat (if (if (not ido-enable-regexp)
4638 (= (length (ido-name (car comps))) (length name))
4639 ;; We can't rely on the length of the input
4640 ;; for regexps, so explicitly check for a
4641 ;; complete match
4642 (string-match name (ido-name (car comps)))
4643 (string-equal (match-string 0 (ido-name (car comps)))
4644 (ido-name (car comps))))
4645 ""
4646 ;; When there is only one match, show the matching file
4647 ;; name in full, wrapped in [ ... ].
4648 (concat
4649 (or (nth 11 ido-decorations) (nth 4 ido-decorations))
4650 (ido-name (car comps))
4651 (or (nth 12 ido-decorations) (nth 5 ido-decorations))))
4652 (if (not ido-use-faces) (nth 7 ido-decorations)))) ;; [Matched]
4653 (t ;multiple matches
4654 (let* ((items (if (> ido-max-prospects 0) (1+ ido-max-prospects) 999))
4655 (alternatives
4656 (apply
4657 #'concat
4658 (cdr (apply
4659 #'nconc
4660 (mapcar
4661 (lambda (com)
4662 (setq com (ido-name com))
4663 (setq items (1- items))
4664 (cond
4665 ((< items 0) ())
4666 ((= items 0) (list (nth 3 ido-decorations))) ; " | ..."
4667 (t
4668 (list (or ido-separator (nth 2 ido-decorations)) ; " | "
4669 (let ((str (substring com 0)))
4670 (if (and ido-use-faces
4671 (not (string= str first))
4672 (ido-final-slash str))
4673 (put-text-property 0 (length str) 'face 'ido-subdir str))
4674 str)))))
4675 comps))))))
4676
4677 (concat
4678 ;; put in common completion item -- what you get by pressing tab
4679 (if (and (stringp ido-common-match-string)
4680 (> (length ido-common-match-string) (length name)))
4681 (concat (nth 4 ido-decorations) ;; [ ... ]
4682 (substring ido-common-match-string (length name))
4683 (nth 5 ido-decorations)))
4684 ;; list all alternatives
4685 (nth 0 ido-decorations) ;; { ... }
4686 alternatives
4687 (nth 1 ido-decorations)))))))
4688
4689 (defun ido-minibuffer-setup ()
4690 "Minibuffer setup hook for `ido'."
4691 ;; Copied from `icomplete-minibuffer-setup-hook'.
4692 (when (ido-active)
4693 (add-hook 'pre-command-hook 'ido-tidy nil t)
4694 (add-hook 'post-command-hook 'ido-exhibit nil t)
4695 (when (featurep 'xemacs)
4696 (ido-exhibit)
4697 (goto-char (point-min)))
4698 (run-hooks 'ido-minibuffer-setup-hook)
4699 (when ido-initial-position
4700 (goto-char (+ (minibuffer-prompt-end) ido-initial-position))
4701 (setq ido-initial-position nil))))
4702
4703 (defun ido-tidy ()
4704 "Pre command hook for `ido'."
4705 ;; Remove completions display, if any, prior to new user input.
4706 ;; Copied from `icomplete-tidy'."
4707
4708 (when ido-auto-merge-timer
4709 (ido-trace "\n*cancel timer*" this-command)
4710 (cancel-timer ido-auto-merge-timer)
4711 (setq ido-auto-merge-timer nil))
4712
4713 (when (ido-active)
4714 (if (bound-and-true-p ido-eoinput)
4715 (if (> ido-eoinput (point-max))
4716 ;; Oops, got rug pulled out from under us - reinit:
4717 (setq ido-eoinput (point-max))
4718 (let ((inhibit-read-only t)
4719 (buffer-undo-list t))
4720 (delete-region ido-eoinput (point-max))))
4721
4722 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
4723 (make-local-variable 'ido-eoinput)
4724 (setq ido-eoinput 1))))
4725
4726 (defun ido-summary-buffers-to-end ()
4727 ;; Move the summaries to the end of the buffer list.
4728 ;; This is an example function which can be hooked on to
4729 ;; `ido-make-buffer-list-hook'. Any buffer matching the regexps
4730 ;; `Summary' or `output\*$'are put to the end of the list.
4731 (let ((summaries (delq nil (mapcar
4732 (lambda (x)
4733 (if (or
4734 (string-match "Summary" x)
4735 (string-match "output\\*\\'" x))
4736 x))
4737 ido-temp-list))))
4738 (ido-to-end summaries)))
4739
4740 ;;; Helper functions for other programs
4741
4742 (put 'ibuffer-find-file 'ido 'find-file)
4743 (put 'dired 'ido 'dir)
4744 (put 'dired-other-window 'ido 'dir)
4745 (put 'dired-other-frame 'ido 'dir)
4746 ;; See http://debbugs.gnu.org/11954 for reasons.
4747 (put 'dired-do-copy 'ido 'ignore)
4748 (put 'dired-do-rename 'ido 'ignore)
4749
4750 ;;;###autoload
4751 (defun ido-read-buffer (prompt &optional default require-match)
4752 "Ido replacement for the built-in `read-buffer'.
4753 Return the name of a buffer selected.
4754 PROMPT is the prompt to give to the user. DEFAULT if given is the default
4755 buffer to be selected, which will go to the front of the list.
4756 If REQUIRE-MATCH is non-nil, an existing buffer must be selected."
4757 (let* ((ido-current-directory nil)
4758 (ido-directory-nonreadable nil)
4759 (ido-directory-too-big nil)
4760 (ido-context-switch-command 'ignore)
4761 (buf (ido-read-internal 'buffer prompt 'ido-buffer-history default require-match)))
4762 (if (eq ido-exit 'fallback)
4763 (let ((read-buffer-function nil))
4764 (run-hook-with-args 'ido-before-fallback-functions 'read-buffer)
4765 (read-buffer prompt default require-match))
4766 buf)))
4767
4768 ;;;###autoload
4769 (defun ido-read-file-name (prompt &optional dir default-filename mustmatch initial predicate)
4770 "Ido replacement for the built-in `read-file-name'.
4771 Read file name, prompting with PROMPT and completing in directory DIR.
4772 See `read-file-name' for additional parameters."
4773 (let (filename)
4774 (cond
4775 ((or (eq predicate 'file-directory-p)
4776 (eq (and (symbolp this-command)
4777 (get this-command 'ido)) 'dir)
4778 (memq this-command ido-read-file-name-as-directory-commands))
4779 (setq filename
4780 (ido-read-directory-name prompt dir default-filename mustmatch initial)))
4781 ((and (not (eq (and (symbolp this-command)
4782 (get this-command 'ido)) 'ignore))
4783 (not (memq this-command ido-read-file-name-non-ido))
4784 (or (null predicate) (eq predicate 'file-exists-p)))
4785 (let* (ido-saved-vc-hb
4786 (ido-context-switch-command
4787 (if (eq (and (symbolp this-command)
4788 (get this-command 'ido)) 'find-file)
4789 nil 'ignore))
4790 (vc-handled-backends (and (boundp 'vc-handled-backends) vc-handled-backends))
4791 (minibuffer-completing-file-name t)
4792 (ido-current-directory (ido-expand-directory dir))
4793 (ido-directory-nonreadable (not (file-readable-p ido-current-directory)))
4794 (ido-directory-too-big (and (not ido-directory-nonreadable)
4795 (ido-directory-too-big-p ido-current-directory)))
4796 (ido-work-directory-index -1)
4797 (ido-show-dot-for-dired (and ido-show-dot-for-dired
4798 (not default-filename)))
4799 (ido-work-file-index -1)
4800 (ido-find-literal nil))
4801 (setq ido-exit nil)
4802 (setq filename
4803 (ido-read-internal 'file prompt 'ido-file-history
4804 (cond ; Bug#11861.
4805 ((stringp default-filename) default-filename)
4806 ((consp default-filename) (car default-filename))
4807 ((and (not default-filename) initial)
4808 (expand-file-name initial dir))
4809 (buffer-file-name buffer-file-name))
4810 mustmatch initial))
4811 (setq dir ido-current-directory) ; See bug#1516.
4812 (cond
4813 ((eq ido-exit 'fallback)
4814 (setq filename 'fallback))
4815 ((eq ido-exit 'dired)
4816 (setq filename ido-current-directory))
4817 (filename
4818 (setq filename
4819 (concat ido-current-directory filename))))))
4820 (t
4821 (setq filename 'fallback)))
4822 (if (eq filename 'fallback)
4823 (let ((read-file-name-function nil))
4824 (run-hook-with-args 'ido-before-fallback-functions 'read-file-name)
4825 (read-file-name prompt dir default-filename mustmatch initial predicate))
4826 filename)))
4827
4828 ;;;###autoload
4829 (defun ido-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
4830 "Ido replacement for the built-in `read-directory-name'.
4831 Read directory name, prompting with PROMPT and completing in directory DIR.
4832 See `read-directory-name' for additional parameters."
4833 (let* (filename
4834 (minibuffer-completing-file-name t)
4835 (ido-context-switch-command 'ignore)
4836 ido-saved-vc-hb
4837 (ido-current-directory (ido-expand-directory dir))
4838 (ido-directory-nonreadable (not (file-readable-p ido-current-directory)))
4839 (ido-directory-too-big (and (not ido-directory-nonreadable)
4840 (ido-directory-too-big-p ido-current-directory)))
4841 (ido-work-directory-index -1)
4842 (ido-work-file-index -1))
4843 (setq filename (ido-read-internal
4844 'dir prompt 'ido-file-history
4845 (or default-dirname ; Bug#11861.
4846 (if initial
4847 (expand-file-name initial ido-current-directory)
4848 ido-current-directory))
4849 mustmatch initial))
4850 (cond
4851 ((eq ido-exit 'fallback)
4852 (let ((read-file-name-function nil))
4853 (run-hook-with-args 'ido-before-fallback-functions 'read-directory-name)
4854 (read-directory-name prompt ido-current-directory
4855 default-dirname mustmatch initial)))
4856 ((equal filename ".") ido-current-directory)
4857 (t (concat ido-current-directory filename)))))
4858
4859 ;;;###autoload
4860 (defun ido-completing-read (prompt choices &optional _predicate require-match
4861 initial-input hist def _inherit-input-method)
4862 "Ido replacement for the built-in `completing-read'.
4863 Read a string in the minibuffer with ido-style completion.
4864 PROMPT is a string to prompt with; normally it ends in a colon and a space.
4865 CHOICES is a list of strings which are the possible completions.
4866 PREDICATE and INHERIT-INPUT-METHOD is currently ignored; it is included
4867 to be compatible with `completing-read'.
4868 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
4869 the input is (or completes to) an element of CHOICES or is null.
4870 If the input is null, `ido-completing-read' returns DEF, or an empty
4871 string if DEF is nil, regardless of the value of REQUIRE-MATCH.
4872 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
4873 with point positioned at the end.
4874 HIST, if non-nil, specifies a history list.
4875 DEF, if non-nil, is the default value."
4876 (let ((ido-current-directory nil)
4877 (ido-directory-nonreadable nil)
4878 (ido-directory-too-big nil)
4879 (ido-context-switch-command 'ignore)
4880 (ido-choice-list choices))
4881 ;; Initialize ido before invoking ido-read-internal
4882 (ido-common-initialization)
4883 (ido-read-internal 'list prompt hist def require-match initial-input)))
4884
4885 (defun ido-unload-function ()
4886 "Unload the Ido library."
4887 (ido-mode -1)
4888 (setq minor-mode-map-alist (assq-delete-all 'ido-mode minor-mode-map-alist))
4889 ;; continue standard unloading
4890 nil)
4891
4892 (provide 'ido)
4893
4894 ;;; ido.el ends here