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