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