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