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