mm-util.el (mm-handle-filename): Move to mm-decode.el (bug#8330).
[emacs.git] / lisp / ido.el
blob177f933887015d861a316c4a3bfba368e2adc486
1 ;;; ido.el --- interactively do things with buffers and files.
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Based on: iswitchb by Stephen Eglen <stephen@cns.ed.ac.uk>
7 ;; Keywords: extensions convenience
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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.
31 ;; Interactive substring matching
32 ;; ------------------------------
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.
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.
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.
49 ;; Example:
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
55 ;; Buffer: {123456 | 123}
57 ;; If I then press 2:
58 ;; Buffer: 2[3]{123456 | 123}
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].
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.
70 ;; So, I press TAB:
71 ;; Buffer: 23{123456 | 123}
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.
78 ;; However, if I type 4, I only have one match left:
79 ;; Buffer: 234[123456]
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.
85 ;; If I want to create a new buffer named "234", I press C-j instead of
86 ;; TAB or RET.
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".
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).
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.
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.
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.
114 ;; To go up to the parent directory, delete any partial file name
115 ;; already specified (e.g. using [backspace]) and hit [backspace].
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.
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.
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)
132 ;; Hidden buffers and files
133 ;; ------------------------
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.
142 ;; You can toggle display of the hidden buffers and files with C-a.
144 ;; Additional functionality
145 ;; ------------------------
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.
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.
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.
160 ;; Prefix matching
161 ;; ---------------
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.
170 ;; You can toggle prefix matching with C-p.
172 ;; Example:
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.
177 ;; Flexible matching
178 ;; -----------------
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.
185 ;; The flexible matching implies that any item which simply contains all
186 ;; of the entered characters in the specified sequence will match.
188 ;; Example:
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.
195 ;; Regexp matching
196 ;; ---------------
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'.
203 ;; Note: ido-style completion is inhibited when you enable regexp matching.
206 ;; Customization
207 ;; -------------
209 ;; Customize the `ido' group to change the `ido' functionality.
211 ;; To modify the keybindings, use the ido-setup-hook. For example:
212 ;;(add-hook 'ido-setup-hook 'ido-my-keys)
214 ;;(defun ido-my-keys ()
215 ;; "Add my keybindings for ido."
216 ;; (define-key ido-completion-map " " 'ido-next-match)
217 ;; )
219 ;; Seeing all the matching buffers or files
220 ;; ----------------------------------------
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'.
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.
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.
238 ;; Changing the list of files
239 ;; --------------------------
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:
246 ;; (add-hook 'ido-make-buffer-list-hook 'ido-summary-buffers-to-end)
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.)
255 ;; Highlighting
256 ;; ------------
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).
264 ;; Replacement for read-buffer and read-file-name
265 ;; ----------------------------------------------
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'.
271 ;; To use ido for all buffer and file selections in Emacs, customize the
272 ;; variable `ido-everywhere'.
274 ;; Using ido-like behavior in other lisp packages
275 ;; -----------------------------------------------
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.
282 ;;; Acknowledgements
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.
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.
293 ;;; History
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.
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.
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.
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.
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.
322 ;;; Code:
324 (defvar recentf-list)
326 ;;; User Variables
328 ;; These are some things you might want to change.
330 (defun ido-fractionp (n)
331 (and (numberp n) (> n 0.0) (<= n 1.0)))
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"))
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.
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)
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)
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)
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)
391 (defcustom ido-ignore-extensions t
392 "Non-nil means ignore files in `completion-ignored-extensions' list."
393 :type 'boolean
394 :group 'ido)
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)
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)
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)
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)
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)))
430 ;;(setq ido-ignore-buffers '("^ " ido-ignore-c-mode))
432 ;; Examples for setting the value of ido-ignore-files
433 ;;(setq ido-ignore-files '("^ " "\\.c\\'" "\\.h\\'"))
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
626 (defvar ido-unc-hosts-cache t
627 "Cached value from `ido-unc-hosts' function.")
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)
642 (defcustom ido-downcase-unc-hosts t
643 "Non-nil if UNC host names should be downcased."
644 :type 'boolean
645 :group 'ido)
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)
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)
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)
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)
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)
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)
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)
694 (defcustom ido-merged-indicator "^"
695 "The string appended to first choice if it has multiple directory choices."
696 :type 'string
697 :group 'ido)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
800 (defface ido-first-match '((t (:bold t)))
801 "Face used by ido for highlighting first match."
802 :group 'ido)
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)
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)
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)
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)
835 (defface ido-incomplete-regexp
836 '((t
837 (:inherit font-lock-warning-face)))
838 "Ido face for indicating incomplete regexps."
839 :group 'ido)
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)
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)
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)
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
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)
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'.")
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)
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)
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.")
906 (defcustom ido-minibuffer-setup-hook nil
907 "Ido-specific customization of minibuffer setup.
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:
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))))
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)
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)
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)
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)
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)
952 ;;; Internal Variables
954 ;; Persistent variables
956 (defvar ido-completion-map nil
957 "Currently active keymap for ido commands.")
959 (defvar ido-common-completion-map nil
960 "Keymap for all ido commands.")
962 (defvar ido-file-completion-map nil
963 "Keymap for ido file commands.")
965 (defvar ido-file-dir-completion-map nil
966 "Keymap for ido file and directory commands.")
968 (defvar ido-buffer-completion-map nil
969 "Keymap for ido buffer commands.")
971 (defvar ido-file-history nil
972 "History of files selected using `ido-find-file'.")
974 (defvar ido-buffer-history nil
975 "History of buffers selected using `ido-switch-buffer'.")
977 (defvar ido-last-directory-list nil
978 "List of last selected directory names.
979 See `ido-enable-last-directory-history' for details.")
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.")
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.")
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...).")
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.")
1001 ;; Temporary storage
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)
1008 (defvar ido-common-match-string nil
1009 "Stores the string that is common to all matching files.")
1011 (defvar ido-rescan nil
1012 "Non-nil means we need to regenerate the list of matching items.")
1014 (defvar ido-rotate nil
1015 "Non-nil means we are rotating list of matches.")
1017 (defvar ido-text nil
1018 "Stores the users string as it is typed in.")
1020 (defvar ido-text-init nil
1021 "The initial string for the users string it is typed in.")
1023 (defvar ido-input-stack nil
1024 "Stores the users strings when user hits M-b/M-f.")
1026 (defvar ido-matches nil
1027 "List of files currently matching `ido-text'.")
1029 (defvar ido-report-no-match t
1030 "Report [No Match] when no completions matches `ido-text'.")
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.")
1037 (defvar ido-current-directory nil
1038 "Current directory for `ido-find-file'.")
1040 (defvar ido-auto-merge-timer nil
1041 "Delay timer for auto merge.")
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.")
1048 (defvar ido-incomplete-regexp nil
1049 "Non-nil if an incomplete regexp is entered.")
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.")
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.")
1060 ;;; Variables with dynamic bindings.
1061 ;;; Declared here to keep the byte compiler quiet.
1063 ;; Stores the current ido item type ('file, 'dir, 'buffer, or 'list).
1064 (defvar ido-cur-item)
1066 ;;; Stores the current default item
1067 (defvar ido-default-item)
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)
1075 ;; Stores the choice list for ido-completing-read
1076 (defvar ido-choice-list)
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)
1082 ;; Remember if current directory is non-readable (so we cannot do completion).
1083 (defvar ido-directory-nonreadable)
1085 ;; Remember if current directory is 'huge' (so we don't want to do completion).
1086 (defvar ido-directory-too-big)
1088 ;; Keep current item list if non-nil.
1089 (defvar ido-keep-item-list)
1091 ;; Process ido-ignore-* lists.
1092 (defvar ido-process-ignore-lists)
1094 ;; Don't process ido-ignore- lists once.
1095 (defvar ido-process-ignore-lists-inhibit)
1097 ;; Buffer from which ido was entered.
1098 (defvar ido-entry-buffer)
1100 ;; Non-nil if matching file must be selected.
1101 (defvar ido-require-match)
1103 ;; Non-nil if we should add [confirm] to prompt
1104 (defvar ido-show-confirm-message)
1106 ;; Stores a temporary version of the file list being created.
1107 (defvar ido-temp-list)
1109 ;; Non-nil if default list element should be rotated into place.
1110 (defvar ido-rotate-temp)
1112 ;; Stores current index in ido-work-directory-list.
1113 (defvar ido-work-directory-index)
1115 ;; Stores current index in ido-work-file-list.
1116 (defvar ido-work-file-index)
1118 ;; Set when merged work directory list is in use.
1119 (defvar ido-use-merged-list)
1121 ;; Set when merged work directory list not yet built.
1122 (defvar ido-try-merged-list)
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)
1128 ;; Original value of vc-handled-backends for use in ido-toggle-vc.
1129 (defvar ido-saved-vc-hb)
1131 ;; Stores temporary state of literal find file.
1132 (defvar ido-find-literal)
1134 ;; Set to 'ignore to inhibit switching between find-file/switch-buffer.
1135 (defvar ido-context-switch-command)
1137 ;;; FUNCTIONS
1139 (defun ido-active (&optional merge)
1140 (if merge
1141 ido-use-merged-list
1142 (and (boundp 'ido-completing-read)
1143 (or (featurep 'xemacs)
1144 (= ido-use-mycompletion-depth (minibuffer-depth))))))
1146 (defvar ido-trace-enable nil)
1148 (defun ido-trace (p &optional s retval)
1149 (if ido-trace-enable
1150 (let ((b (get-buffer-create " *IDO Trace*"))
1151 (deactivate-mark deactivate-mark))
1152 (save-excursion
1153 (save-restriction
1154 (set-buffer b)
1155 (insert p ": " (if (stringp s) s (format "%S" s)) "\n")))))
1156 retval)
1158 (defun ido-toggle-trace (arg)
1159 (interactive "P")
1160 (setq ido-trace-enable (or arg (not ido-trace-enable)))
1161 (if ido-trace-enable
1162 (message "IDO trace on"))
1163 (let ((b (get-buffer " *IDO Trace*")))
1164 (if b
1165 (if ido-trace-enable
1166 (kill-buffer b)
1167 (pop-to-buffer b t t)
1168 (setq truncate-lines t)))))
1170 (defun ido-local-file-exists-p (file)
1171 "Tell if FILE exists locally."
1172 (let (file-name-handler-alist)
1173 (file-exists-p file)))
1175 (defun ido-unc-hosts (&optional query)
1176 "Return list of UNC host names."
1177 (let ((hosts
1178 (cond
1179 ((listp ido-unc-hosts)
1180 ido-unc-hosts) ;; static list or nil
1181 ((listp ido-unc-hosts-cache)
1182 ido-unc-hosts-cache) ;; result of net search
1183 ((and query (fboundp ido-unc-hosts))
1184 (message (propertize "Searching for UNC hosts..." 'face 'highlight))
1185 (setq ido-unc-hosts-cache (funcall ido-unc-hosts))
1186 (message nil)
1187 ido-unc-hosts-cache)
1188 (query
1189 (setq ido-unc-hosts-cache nil))
1190 (t (fboundp ido-unc-hosts)))))
1191 (when query
1192 (let ((case-fold-search ido-downcase-unc-hosts)
1193 res host re-list re)
1194 (while hosts
1195 (setq host (car hosts)
1196 hosts (cdr hosts)
1197 re-list (and ido-process-ignore-lists
1198 ido-ignore-unc-host-regexps))
1199 (while re-list
1200 (setq re (car re-list)
1201 re-list (cdr re-list))
1202 (if (string-match re host)
1203 (setq re-list nil
1204 host nil)))
1205 (when host
1206 (when ido-downcase-unc-hosts
1207 (setq host (downcase host)))
1208 (setq res (cons host res))))
1209 (setq hosts (sort res #'string<))))
1210 hosts))
1212 (defun ido-unc-hosts-net-view ()
1213 "Query network for list of UNC host names using `NET VIEW'."
1214 (let (hosts)
1215 (with-temp-buffer
1216 (shell-command "net view" t)
1217 (goto-char (point-min))
1218 (while (re-search-forward "^\\\\\\\\\\([[:graph:]]+\\)" nil t)
1219 (setq hosts (cons (match-string 1) hosts))))
1220 hosts))
1222 (defun ido-is-tramp-root (&optional dir)
1223 (and ido-enable-tramp-completion
1224 (string-match "\\`/[^/]+[@:]\\'"
1225 (or dir ido-current-directory))))
1227 (defun ido-is-unc-root (&optional dir)
1228 (and (ido-unc-hosts)
1229 (string-equal "//"
1230 (or dir ido-current-directory))))
1232 (defun ido-is-unc-host (&optional dir)
1233 (and (ido-unc-hosts)
1234 (string-match "\\`//[^/]+/\\'"
1235 (or dir ido-current-directory))))
1237 (defun ido-is-root-directory (&optional dir)
1238 (setq dir (or dir ido-current-directory))
1240 (string-equal "/" dir)
1241 (and (memq system-type '(windows-nt ms-dos))
1242 (string-match "\\`[a-zA-Z]:[/\\]\\'" dir))
1243 (if ido-enable-tramp-completion
1244 (ido-is-tramp-root dir)
1245 (string-match "\\`/[^:/][^:/]+:\\'" dir))))
1247 (defun ido-is-ftp-directory (&optional dir)
1248 (string-match
1249 (if ido-enable-tramp-completion
1250 "\\`/[^/:][^/:]+:" ;; like tramp-file-name-regexp-unified, but doesn't match single drive letters
1251 "\\`/[^/:][^/:]+:/")
1252 (or dir ido-current-directory)))
1254 (defun ido-is-slow-ftp-host (&optional dir)
1255 (and (or ido-slow-ftp-hosts ido-slow-ftp-host-regexps)
1256 (setq dir (or dir ido-current-directory))
1257 ;; (featurep 'ange-ftp)
1258 ;; (ange-ftp-ftp-name dir)
1259 (string-match
1260 (if ido-enable-tramp-completion
1261 "\\`/\\([^/]+[@:]\\)*\\([^@/:][^@/:]+\\):"
1262 "\\`/\\([^/:]*@\\)?\\([^@/:][^@/:]+\\):/")
1263 dir)
1264 (let ((host (substring dir (match-beginning 2) (match-end 2))))
1265 (or (member host ido-slow-ftp-hosts)
1266 (let ((re ido-slow-ftp-host-regexps))
1267 (while (and re (not (string-match (car re) host)))
1268 (setq re (cdr re)))
1269 re)))))
1271 (defun ido-time-stamp (&optional time)
1272 ;; Time is a floating point number (fractions of 1 hour)
1273 (setq time (or time (current-time)))
1274 (/ (+ (* (car time) 65536.0) (car (cdr time))) 3600.0))
1276 (defun ido-cache-ftp-valid (&optional time)
1277 (and (numberp ido-cache-ftp-work-directory-time)
1278 (> ido-cache-ftp-work-directory-time 0)
1279 (or (not time)
1280 (< (- (ido-time-stamp) time) ido-cache-ftp-work-directory-time))))
1282 (defun ido-cache-unc-valid (&optional time)
1283 (and (numberp ido-cache-unc-host-shares-time)
1284 (> ido-cache-unc-host-shares-time 0)
1285 (or (not time)
1286 (< (- (ido-time-stamp) time) ido-cache-unc-host-shares-time))))
1288 (defun ido-may-cache-directory (&optional dir)
1289 (setq dir (or dir ido-current-directory))
1290 (cond
1291 ((and (ido-is-root-directory dir)
1292 (or ido-enable-tramp-completion
1293 (memq system-type '(windows-nt ms-dos))))
1294 nil)
1295 ((ido-is-unc-host dir)
1296 (ido-cache-unc-valid))
1297 ((ido-is-ftp-directory dir)
1298 (ido-cache-ftp-valid))
1299 ((ido-directory-too-big-p dir)
1300 nil)
1301 (t t)))
1303 (defun ido-pp (list &optional sep)
1304 (let ((print-level nil) (eval-expression-print-level nil)
1305 (print-length nil) (eval-expression-print-length nil))
1306 (insert "\n;; ----- " (symbol-name list) " -----\n(\n ")
1307 (setq list (symbol-value list))
1308 (while list
1309 (let* ((elt (car list))
1310 (s (if (consp elt) (car elt) elt)))
1311 (if (and (stringp s) (= (length s) 0))
1312 (setq s nil))
1313 (if s
1314 (prin1 elt (current-buffer)))
1315 (if (and (setq list (cdr list)) s)
1316 (insert (or sep "\n ")))))
1317 (insert "\n)\n")))
1319 (defun ido-save-history ()
1320 "Save ido history and cache information between sessions."
1321 (interactive)
1322 (when (and ido-last-directory-list ido-save-directory-list-file)
1323 (let ((buf (get-buffer-create " *ido session*"))
1324 (version-control 'never))
1325 (unwind-protect
1326 (with-current-buffer buf
1327 (erase-buffer)
1328 (insert ";;; -*- coding: utf-8 -*-\n")
1329 (setq buffer-file-coding-system 'utf-8)
1330 (ido-pp 'ido-last-directory-list)
1331 (ido-pp 'ido-work-directory-list)
1332 (ido-pp 'ido-work-file-list)
1333 (ido-pp 'ido-dir-file-cache "\n\n ")
1334 (if (listp ido-unc-hosts-cache)
1335 (ido-pp 'ido-unc-hosts-cache)
1336 (insert "\n;; ----- ido-unc-hosts-cache -----\nt\n"))
1337 (write-file ido-save-directory-list-file nil))
1338 (kill-buffer buf)))))
1340 (defun ido-load-history (&optional arg)
1341 "Load ido history and cache information from previous session.
1342 With prefix argument, reload history unconditionally."
1343 (interactive "P")
1344 (if (or arg (and ido-save-directory-list-file (not ido-last-directory-list)))
1345 (let ((file (expand-file-name ido-save-directory-list-file))
1346 buf)
1347 (when (file-readable-p file)
1348 (setq buf (get-buffer-create " *ido session*"))
1349 (unwind-protect
1350 (with-current-buffer buf
1351 (erase-buffer)
1352 (insert-file-contents file)
1353 (condition-case nil
1354 (setq ido-last-directory-list (read (current-buffer))
1355 ido-work-directory-list (read (current-buffer))
1356 ido-work-file-list (read (current-buffer))
1357 ido-dir-file-cache (read (current-buffer))
1358 ido-unc-hosts-cache (read (current-buffer)))
1359 (error nil)))
1360 (kill-buffer buf)))))
1361 (ido-wash-history))
1363 (defun ido-wash-history ()
1364 "Clean-up ido history and cache information.
1365 Removes badly formatted data and ignored directories."
1366 (interactive)
1367 ;; Check format of each of our lists, discard bogus elements
1368 (setq ido-last-directory-list
1369 (and (listp ido-last-directory-list)
1370 (let ((l ido-last-directory-list) r)
1371 (while l
1372 (if (and (consp (car l))
1373 (stringp (car (car l)))
1374 (stringp (cdr (car l))))
1375 (setq r (cons (car l) r)))
1376 (setq l (cdr l)))
1377 (nreverse r))))
1378 (setq ido-work-directory-list
1379 (and (listp ido-work-directory-list)
1380 (let ((l ido-work-directory-list) r)
1381 (while l
1382 (if (and (stringp (car l))
1383 (or ido-record-ftp-work-directories
1384 (not (ido-is-ftp-directory (car l)))))
1385 (setq r (cons (car l) r)))
1386 (setq l (cdr l)))
1387 (nreverse r))))
1388 (setq ido-work-file-list
1389 (and (listp ido-work-file-list)
1390 (let ((l ido-work-file-list) r)
1391 (while l
1392 (if (stringp (car l))
1393 (setq r (cons (car l) r)))
1394 (setq l (cdr l)))
1395 (nreverse r))))
1396 (setq ido-dir-file-cache
1397 (and (listp ido-dir-file-cache)
1398 (let ((l ido-dir-file-cache) r)
1399 (while l
1400 (if (and (listp (car l))
1401 (> (length (car l)) 2)
1402 (let ((dir (car (car l)))
1403 (time (car (cdr (car l))))
1404 (files (cdr (cdr (car l)))))
1405 (and
1406 (stringp dir)
1407 (consp time)
1408 (cond
1409 ((integerp (car time))
1410 (and (/= (car time) 0)
1411 (integerp (car (cdr time)))
1412 (/= (car (cdr time)) 0)
1413 (ido-may-cache-directory dir)))
1414 ((eq (car time) 'ftp)
1415 (and (numberp (cdr time))
1416 (ido-is-ftp-directory dir)
1417 (ido-cache-ftp-valid (cdr time))))
1418 ((eq (car time) 'unc)
1419 (and (numberp (cdr time))
1420 (ido-is-unc-host dir)
1421 (ido-cache-unc-valid (cdr time))))
1422 (t nil))
1423 (let ((s files) (ok t))
1424 (while s
1425 (if (stringp (car s))
1426 (setq s (cdr s))
1427 (setq s nil ok nil)))
1428 ok))))
1429 (setq r (cons (car l) r)))
1430 (setq l (cdr l)))
1431 (nreverse r))))
1433 ;; Remove ignored directories from work directory list
1434 ;; according to ido-work-directory-list-ignore-regexps
1435 (if ido-work-directory-list
1436 (let ((dirs (reverse ido-work-directory-list)))
1437 (setq ido-work-directory-list nil)
1438 (while dirs
1439 (ido-record-work-directory (car dirs))
1440 (setq dirs (cdr dirs)))))
1441 ;; Get rid of text properties
1442 (let ((l ido-last-directory-list) e)
1443 (while l
1444 (setq e (car l) l (cdr l))
1445 (set-text-properties 0 (length (car e)) nil (car e))
1446 (set-text-properties 0 (length (cdr e)) nil (cdr e))))
1447 (let ((l ido-work-directory-list) e)
1448 (while l
1449 (setq e (car l) l (cdr l))
1450 (set-text-properties 0 (length e) nil e)))
1451 (let ((l ido-work-file-list) e)
1452 (while l
1453 (setq e (car l) l (cdr l))
1454 (set-text-properties 0 (length e) nil e)))
1455 (let ((l ido-dir-file-cache) e d)
1456 (while l
1457 (setq e (car l) l (cdr l))
1458 (if (listp e)
1459 (while e
1460 (setq d (car e) e (cdr e))
1461 (if (not (consp d))
1462 (set-text-properties 0 (length d) nil d)))))))
1465 (defun ido-kill-emacs-hook ()
1466 ;; ido kill emacs hook
1467 (ido-save-history))
1469 (defun ido-common-initialization ()
1470 (ido-init-completion-maps)
1471 (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
1472 (add-hook 'choose-completion-string-functions 'ido-choose-completion-string))
1474 (define-minor-mode ido-everywhere
1475 "Toggle using ido-mode everywhere file and directory names are read.
1476 With ARG, turn ido-mode on if arg is positive, off otherwise."
1477 :global t
1478 :group 'ido
1479 (when (get 'ido-everywhere 'file)
1480 (setq read-file-name-function (car (get 'ido-everywhere 'file)))
1481 (put 'ido-everywhere 'file nil))
1482 (when (get 'ido-everywhere 'buffer)
1483 (setq read-buffer-function (car (get 'ido-everywhere 'buffer)))
1484 (put 'ido-everywhere 'buffer nil))
1485 (when ido-everywhere
1486 (when (memq ido-mode '(both file))
1487 (put 'ido-everywhere 'file (cons read-file-name-function nil))
1488 (setq read-file-name-function 'ido-read-file-name))
1489 (when (memq ido-mode '(both buffer))
1490 (put 'ido-everywhere 'buffer (cons read-buffer-function nil))
1491 (setq read-buffer-function 'ido-read-buffer))))
1493 (defvar ido-minor-mode-map-entry nil)
1495 ;;;###autoload
1496 (defun ido-mode (&optional arg)
1497 "Toggle ido mode on or off.
1498 With ARG, turn ido-mode on if arg is positive, off otherwise.
1499 Turning on ido-mode will remap (via a minor-mode keymap) the default
1500 keybindings for the `find-file' and `switch-to-buffer' families of
1501 commands to the ido versions of these functions.
1502 However, if ARG arg equals 'files, remap only commands for files, or
1503 if it equals 'buffers, remap only commands for buffer switching.
1504 This function also adds a hook to the minibuffer."
1505 (interactive "P")
1506 (setq ido-mode
1507 (cond
1508 ((null arg) (if ido-mode nil 'both))
1509 ((eq arg t) 'both)
1510 ((eq arg 'files) 'file)
1511 ((eq arg 'buffers) 'buffer)
1512 ((memq arg '(file buffer both)) arg)
1513 ((> (prefix-numeric-value arg) 0) 'both)
1514 (t nil)))
1516 (ido-everywhere (if ido-everywhere 1 -1))
1518 (when ido-mode
1519 (ido-common-initialization)
1520 (ido-load-history)
1522 (add-hook 'kill-emacs-hook 'ido-kill-emacs-hook)
1524 (let ((map (make-sparse-keymap)))
1525 (when (memq ido-mode '(file both))
1526 (define-key map [remap find-file] 'ido-find-file)
1527 (define-key map [remap find-file-read-only] 'ido-find-file-read-only)
1528 (define-key map [remap find-alternate-file] 'ido-find-alternate-file)
1529 (define-key map [remap write-file] 'ido-write-file)
1530 (define-key map [remap insert-file] 'ido-insert-file)
1531 (define-key map [remap list-directory] 'ido-list-directory)
1532 (define-key map [remap dired] 'ido-dired)
1533 (define-key map [remap find-file-other-window]
1534 'ido-find-file-other-window)
1535 (define-key map [remap find-file-read-only-other-window]
1536 'ido-find-file-read-only-other-window)
1537 (define-key map [remap find-file-other-frame]
1538 'ido-find-file-other-frame)
1539 (define-key map [remap find-file-read-only-other-frame]
1540 'ido-find-file-read-only-other-frame))
1542 (when (memq ido-mode '(buffer both))
1543 (define-key map [remap switch-to-buffer] 'ido-switch-buffer)
1544 (define-key map [remap switch-to-buffer-other-window]
1545 'ido-switch-buffer-other-window)
1546 (define-key map [remap switch-to-buffer-other-frame]
1547 'ido-switch-buffer-other-frame)
1548 (define-key map [remap insert-buffer] 'ido-insert-buffer)
1549 (define-key map [remap kill-buffer] 'ido-kill-buffer)
1550 (define-key map [remap display-buffer] 'ido-display-buffer))
1552 (if ido-minor-mode-map-entry
1553 (setcdr ido-minor-mode-map-entry map)
1554 (setq ido-minor-mode-map-entry (cons 'ido-mode map))
1555 (add-to-list 'minor-mode-map-alist ido-minor-mode-map-entry))))
1557 (message "Ido mode %s" (if ido-mode "enabled" "disabled")))
1560 ;;; IDO KEYMAP
1561 (defun ido-init-completion-maps ()
1562 "Set up the completion keymaps used by `ido'."
1564 ;; Common map
1565 (let ((map (make-sparse-keymap)))
1566 (define-key map "\C-a" 'ido-toggle-ignore)
1567 (define-key map "\C-c" 'ido-toggle-case)
1568 (define-key map "\C-e" 'ido-edit-input)
1569 (define-key map "\t" 'ido-complete)
1570 (define-key map " " 'ido-complete-space)
1571 (define-key map "\C-j" 'ido-select-text)
1572 (define-key map "\C-m" 'ido-exit-minibuffer)
1573 (define-key map "\C-p" 'ido-toggle-prefix)
1574 (define-key map "\C-r" 'ido-prev-match)
1575 (define-key map "\C-s" 'ido-next-match)
1576 (define-key map "\C-t" 'ido-toggle-regexp)
1577 (define-key map "\C-z" 'ido-undo-merge-work-directory)
1578 (define-key map [(control ?\s)] 'ido-restrict-to-matches)
1579 (define-key map [(meta ?\s)] 'ido-take-first-match)
1580 (define-key map [(control ?@)] 'ido-restrict-to-matches)
1581 (define-key map [right] 'ido-next-match)
1582 (define-key map [left] 'ido-prev-match)
1583 (define-key map "?" 'ido-completion-help)
1584 ;; Magic commands.
1585 (define-key map "\C-b" 'ido-magic-backward-char)
1586 (define-key map "\C-f" 'ido-magic-forward-char)
1587 (define-key map "\C-d" 'ido-magic-delete-char)
1588 (set-keymap-parent map minibuffer-local-map)
1589 (setq ido-common-completion-map map))
1591 ;; File and directory map
1592 (let ((map (make-sparse-keymap)))
1593 (define-key map "\C-x\C-b" 'ido-enter-switch-buffer)
1594 (define-key map "\C-x\C-f" 'ido-fallback-command)
1595 (define-key map "\C-x\C-d" 'ido-enter-dired)
1596 (define-key map [down] 'ido-next-match-dir)
1597 (define-key map [up] 'ido-prev-match-dir)
1598 (define-key map [(meta up)] 'ido-prev-work-directory)
1599 (define-key map [(meta down)] 'ido-next-work-directory)
1600 (define-key map [backspace] 'ido-delete-backward-updir)
1601 (define-key map "\d" 'ido-delete-backward-updir)
1602 (define-key map [remap delete-backward-char] 'ido-delete-backward-updir) ; BS
1603 (define-key map [remap backward-kill-word] 'ido-delete-backward-word-updir) ; M-DEL
1605 (define-key map [(control backspace)] 'ido-up-directory)
1606 (define-key map "\C-l" 'ido-reread-directory)
1607 (define-key map [(meta ?d)] 'ido-wide-find-dir-or-delete-dir)
1608 (define-key map [(meta ?b)] 'ido-push-dir)
1609 (define-key map [(meta ?v)] 'ido-push-dir-first)
1610 (define-key map [(meta ?f)] 'ido-wide-find-file-or-pop-dir)
1611 (define-key map [(meta ?k)] 'ido-forget-work-directory)
1612 (define-key map [(meta ?m)] 'ido-make-directory)
1613 (define-key map [(meta ?n)] 'ido-next-work-directory)
1614 (define-key map [(meta ?o)] 'ido-prev-work-file)
1615 (define-key map [(meta control ?o)] 'ido-next-work-file)
1616 (define-key map [(meta ?p)] 'ido-prev-work-directory)
1617 (define-key map [(meta ?s)] 'ido-merge-work-directories)
1618 (set-keymap-parent map ido-common-completion-map)
1619 (setq ido-file-dir-completion-map map))
1621 ;; File only map
1622 (let ((map (make-sparse-keymap)))
1623 (define-key map "\C-k" 'ido-delete-file-at-head)
1624 (define-key map "\C-o" 'ido-copy-current-word)
1625 (define-key map "\C-w" 'ido-copy-current-file-name)
1626 (define-key map [(meta ?l)] 'ido-toggle-literal)
1627 (set-keymap-parent map ido-file-dir-completion-map)
1628 (setq ido-file-completion-map map))
1630 ;; Buffer map
1631 (let ((map (make-sparse-keymap)))
1632 (define-key map "\C-x\C-f" 'ido-enter-find-file)
1633 (define-key map "\C-x\C-b" 'ido-fallback-command)
1634 (define-key map "\C-k" 'ido-kill-buffer-at-head)
1635 (define-key map "\C-o" 'ido-toggle-virtual-buffers)
1636 (set-keymap-parent map ido-common-completion-map)
1637 (setq ido-buffer-completion-map map)))
1640 (defun ido-setup-completion-map ()
1641 "Set up the keymap for `ido'."
1643 ;; generated every time so that it can inherit new functions.
1644 (let ((map (make-sparse-keymap))
1645 (viper-p (if (boundp 'viper-mode) viper-mode)))
1647 (when viper-p
1648 (define-key map [remap viper-intercept-ESC-key] 'ignore))
1650 (cond
1651 ((memq ido-cur-item '(file dir))
1652 (when ido-context-switch-command
1653 (define-key map "\C-x\C-b" ido-context-switch-command)
1654 (define-key map "\C-x\C-d" 'ignore))
1655 (when viper-p
1656 (define-key map [remap viper-backward-char] 'ido-delete-backward-updir)
1657 (define-key map [remap viper-del-backward-char-in-insert] 'ido-delete-backward-updir)
1658 (define-key map [remap viper-delete-backward-word] 'ido-delete-backward-word-updir))
1659 (set-keymap-parent map
1660 (if (eq ido-cur-item 'file)
1661 ido-file-completion-map
1662 ido-file-dir-completion-map)))
1664 ((eq ido-cur-item 'buffer)
1665 (when ido-context-switch-command
1666 (define-key map "\C-x\C-f" ido-context-switch-command))
1667 (set-keymap-parent map ido-buffer-completion-map))
1670 (set-keymap-parent map ido-common-completion-map)))
1672 (setq ido-completion-map map)))
1674 (defun ido-final-slash (dir &optional fix-it)
1675 ;; return DIR if DIR has final slash.
1676 ;; else if FIX-IT is non-nil, return DIR/
1677 ;; else return nil.
1678 (setq dir (ido-name dir))
1679 (cond
1680 ((string-match "/\\'" dir) dir)
1681 ((ido-is-tramp-root dir) dir)
1682 (fix-it (concat dir "/"))
1683 (t nil)))
1685 (defun ido-no-final-slash (s)
1686 ;; Remove optional final slash from string S
1687 (let ((l (1- (length s))))
1688 (if (and (> l 0) (eq (aref s l) ?/))
1689 (substring s 0 l)
1690 s)))
1692 (defun ido-nonreadable-directory-p (dir)
1693 ;; Return t if dir is a directory, but not readable
1694 ;; Do not check for non-readable directories via tramp, as this causes a premature
1695 ;; connect on incomplete tramp paths (after entring just method:).
1696 (let ((ido-enable-tramp-completion nil))
1697 (and (ido-final-slash dir)
1698 (not (ido-is-unc-host dir))
1699 (file-directory-p dir)
1700 (not (file-readable-p dir)))))
1702 (defun ido-directory-too-big-p (dir)
1703 ;; Return t if dir is a directory, but too big to show
1704 ;; Do not check for non-readable directories via tramp, as this causes a premature
1705 ;; connect on incomplete tramp paths (after entring just method:).
1706 (let ((ido-enable-tramp-completion nil))
1707 (and (numberp ido-max-directory-size)
1708 (ido-final-slash dir)
1709 (not (ido-is-unc-host dir))
1710 (file-directory-p dir)
1711 (> (nth 7 (file-attributes dir)) ido-max-directory-size))))
1713 (defun ido-set-current-directory (dir &optional subdir no-merge)
1714 ;; Set ido's current directory to DIR or DIR/SUBDIR
1715 (unless (and ido-enable-tramp-completion
1716 (string-match "\\`/[^/]*@\\'" dir))
1717 (setq dir (ido-final-slash dir t)))
1718 (setq ido-use-merged-list nil
1719 ido-try-merged-list (not no-merge))
1720 (when subdir
1721 (setq dir (concat dir subdir))
1722 (unless (and ido-enable-tramp-completion
1723 (string-match "\\`/[^/]*@\\'" dir))
1724 (setq dir (ido-final-slash dir t))))
1725 (if (get-buffer ido-completion-buffer)
1726 (kill-buffer ido-completion-buffer))
1727 (cond
1728 ((equal dir ido-current-directory)
1729 nil)
1730 ((ido-is-unc-root dir)
1731 (ido-trace "unc" dir)
1732 (setq ido-current-directory dir)
1733 (setq ido-directory-nonreadable nil)
1734 (setq ido-directory-too-big nil)
1737 (ido-trace "cd" dir)
1738 (setq ido-current-directory dir)
1739 (if (get-buffer ido-completion-buffer)
1740 (kill-buffer ido-completion-buffer))
1741 (setq ido-directory-nonreadable (ido-nonreadable-directory-p dir))
1742 (setq ido-directory-too-big (and (not ido-directory-nonreadable)
1743 (ido-directory-too-big-p dir)))
1744 t)))
1746 (defun ido-set-current-home (&optional dir)
1747 ;; Set ido's current directory to user's home directory
1748 (ido-set-current-directory (expand-file-name (or dir "~/"))))
1750 (defun ido-record-command (command arg)
1751 ;; Add (command arg) to command-history if ido-record-commands is t
1752 (if ido-record-commands
1753 (let ((cmd (list command arg)))
1754 (if (or (not command-history)
1755 (not (equal cmd (car command-history))))
1756 (setq command-history (cons cmd command-history))))))
1758 (defun ido-make-prompt (item prompt)
1759 ;; Make the prompt for ido-read-internal
1760 (cond
1761 ((and (memq item '(file dir)) ido-current-directory)
1762 (let ((dirname (abbreviate-file-name ido-current-directory))
1763 (max-width (if (and ido-max-file-prompt-width (floatp ido-max-file-prompt-width))
1764 (floor (* (frame-width) ido-max-file-prompt-width))
1765 ido-max-file-prompt-width))
1766 (literal (and (boundp 'ido-find-literal) ido-find-literal "(literal) "))
1767 (vc-off (and ido-saved-vc-hb (not vc-handled-backends) "[-VC] "))
1768 (prefix nil)
1769 (rule ido-rewrite-file-prompt-rules))
1770 (let ((case-fold-search nil))
1771 (while rule
1772 (if (and (consp (car rule))
1773 (string-match (car (car rule)) dirname))
1774 (setq dirname
1775 (if (stringp (cdr (car rule)))
1776 (replace-match (cdr (car rule)) t nil dirname)
1777 (funcall (cdr (car rule)) dirname))))
1778 (setq rule (cdr rule))))
1779 (run-hooks 'ido-rewrite-file-prompt-functions)
1780 (concat prompt
1781 ; (if ido-process-ignore-lists "" "&")
1782 (or literal "")
1783 (or vc-off "")
1784 (or prefix "")
1785 (let ((l (length dirname)))
1786 (if (and max-width (> max-width 0) (> l max-width))
1787 (let* ((s (substring dirname (- max-width)))
1788 (i (string-match "/" s)))
1789 (concat "..." (if i (substring s i) s)))
1790 dirname)))))
1791 (t prompt)))
1793 ;; Here is very briefly how ido-find-file works:
1795 ;; (ido-find-file)
1796 ;; (ido-file-internal method)
1797 ;; set ido-current-directory
1798 ;; (ido-read-internal 'file ...)
1799 ;; (while ...
1800 ;; (ido-make-item-list ...)
1801 ;; (ido-set-matches)
1802 ;; (completing-read ... ido-text-init ...)
1804 ;; ... here user is allowed to type characters and commands
1805 ;; a command may set ido-exit and call (exit-minibuffer)
1806 ;; to make ido-read-internal do advanced tasks (or return)
1808 ;; ... ido-tidy and ido-exhibit are pre- and post-hooks
1809 ;; which are run before and after each user command.
1811 ;; return value from completing-read is stored in ido-final-text
1812 ;; - ido-exit may cause further actions to be taken:
1813 ;; 'refresh - repeat loop (make-item-list, set-matches)
1814 ;; 'edit - edit the prompt string, then repeat loop
1815 ;; 'keep - repeat loop but don't (re)make-item-list
1816 ;; 'updir - go up one directory, repeat loop
1817 ;; else set ido-selected based on ido-final-text,
1818 ;; optionally update ido-current-directory and repeat loop, or
1819 ;; exit with the return value of ido-selected (file name)
1820 ;; selected file name is returned from ido-read-internal,
1821 ;; ido-exit and method determines what action is taken
1822 ;; e.g. the file name may be ignored or joined with ido-current-directory, and
1823 ;; the relevant function is called (find-file, write-file, etc).
1825 (defun ido-read-internal (item prompt history &optional default require-match initial)
1826 "Perform the `ido-read-buffer' and `ido-read-file-name' functions.
1827 Return the name of a buffer or file selected.
1828 PROMPT is the prompt to give to the user.
1829 DEFAULT if given is the default item to start with.
1830 If REQUIRE-MATCH is non-nil, an existing file must be selected.
1831 If INITIAL is non-nil, it specifies the initial input string."
1832 (let
1833 ((ido-cur-item item)
1834 (ido-entry-buffer (current-buffer))
1835 (ido-process-ignore-lists t)
1836 (ido-process-ignore-lists-inhibit nil)
1837 (ido-set-default-item t)
1838 ido-default-item
1839 ido-selected
1840 ido-final-text
1841 (done nil)
1842 (icomplete-mode nil) ;; prevent icomplete starting up
1843 ;; Exported dynamic variables:
1844 ido-cur-list
1845 ido-ignored-list
1846 (ido-rotate-temp nil)
1847 (ido-keep-item-list nil)
1848 (ido-use-merged-list nil)
1849 (ido-try-merged-list t)
1850 (ido-pre-merge-state nil)
1851 (ido-case-fold ido-case-fold)
1852 (ido-enable-prefix ido-enable-prefix)
1853 (ido-enable-regexp ido-enable-regexp)
1854 (ido-show-confirm-message nil)
1857 (ido-setup-completion-map)
1858 (setq ido-text-init initial)
1859 (setq ido-input-stack nil)
1861 (run-hooks 'ido-setup-hook)
1863 (while (not done)
1864 (ido-trace "\n_LOOP_" ido-text-init)
1865 (setq ido-exit nil)
1866 (setq ido-rescan t)
1867 (setq ido-rotate nil)
1868 (setq ido-text "")
1869 (when ido-set-default-item
1870 (setq ido-default-item
1871 (cond
1872 ((eq item 'buffer)
1873 (if (bufferp default) (buffer-name default) default))
1874 ((stringp default)
1875 (if (memq item '(file dir))
1876 (file-name-nondirectory default)
1877 default))
1878 ((eq item 'file)
1879 (and ido-enable-last-directory-history
1880 (let ((d (assoc ido-current-directory ido-last-directory-list)))
1881 (and d (cdr d)))))))
1882 (if (member ido-default-item ido-ignore-item-temp-list)
1883 (setq ido-default-item nil))
1884 (ido-trace "new default" ido-default-item)
1885 (if ido-default-item
1886 (setq ido-initial-position 0))
1887 (setq ido-set-default-item nil))
1889 (if ido-process-ignore-lists-inhibit
1890 (setq ido-process-ignore-lists nil))
1892 (if (and ido-use-merged-list (memq ido-try-merged-list '(t wide)) (not ido-keep-item-list))
1893 (let ((olist ido-cur-list)
1894 (oign ido-ignored-list)
1895 (omat ido-matches)
1896 (l (ido-make-merged-file-list ido-text-init
1897 (eq ido-use-merged-list 'auto)
1898 (eq ido-try-merged-list 'wide))))
1899 (ido-trace "merged" l)
1900 (cond
1901 ((not l)
1902 (if (eq ido-try-merged-list 'wide)
1903 (setq ido-pre-merge-state
1904 (list "" ido-current-directory olist oign omat)
1905 ido-cur-list nil
1906 ido-ignored-list nil
1907 ido-matches nil
1908 ido-keep-item-list t
1909 ido-try-merged-list (if (eq ido-use-merged-list 'auto) 'auto nil)
1910 ido-use-merged-list nil)
1911 (setq ido-cur-list olist
1912 ido-ignored-list oign
1913 ido-matches omat
1914 ido-keep-item-list t
1915 ido-try-merged-list (if (eq ido-use-merged-list 'auto) 'auto nil)
1916 ido-use-merged-list nil)))
1917 ((eq l t)
1918 (setq ido-use-merged-list nil))
1919 ((eq l 'input-pending-p)
1920 (setq ido-try-merged-list t
1921 ido-use-merged-list nil))
1923 (setq ido-pre-merge-state
1924 (list ido-text-init ido-current-directory olist oign omat))
1925 (ido-set-current-directory (car (cdr (car l))))
1926 (if (ido-final-slash ido-text-init)
1927 (setq ido-text-init ""))
1928 (setq ido-cur-list l
1929 ido-ignored-list nil
1930 ido-matches l
1931 ido-rescan nil
1932 ido-keep-item-list t
1933 ido-use-merged-list t)
1934 (ido-trace "Merged" t)
1935 ))))
1937 (cond
1938 (ido-keep-item-list
1939 (setq ido-keep-item-list nil
1940 ido-rescan nil))
1941 ((eq ido-cur-item 'file)
1942 (setq ido-ignored-list nil
1943 ido-cur-list (and (not ido-directory-nonreadable)
1944 (not ido-directory-too-big)
1945 (ido-make-file-list ido-default-item))))
1946 ((eq ido-cur-item 'dir)
1947 (setq ido-ignored-list nil
1948 ido-cur-list (and (not ido-directory-nonreadable)
1949 (not ido-directory-too-big)
1950 (ido-make-dir-list ido-default-item))))
1951 ((eq ido-cur-item 'buffer)
1952 (setq ido-ignored-list nil
1953 ido-cur-list (ido-make-buffer-list ido-default-item)))
1954 ((eq ido-cur-item 'list)
1955 (setq ido-ignored-list nil
1956 ido-cur-list (ido-make-choice-list ido-default-item)))
1957 (t nil))
1958 (setq ido-rotate-temp nil)
1960 (if ido-process-ignore-lists-inhibit
1961 (setq ido-process-ignore-lists t
1962 ido-process-ignore-lists-inhibit nil))
1964 (ido-set-matches)
1965 (if (and ido-matches (eq ido-try-merged-list 'auto))
1966 (setq ido-try-merged-list t))
1967 (let ((max-mini-window-height (or ido-max-window-height
1968 (and (boundp 'max-mini-window-height)
1969 max-mini-window-height)))
1970 (ido-completing-read t)
1971 (ido-require-match require-match)
1972 (ido-use-mycompletion-depth (1+ (minibuffer-depth)))
1973 (show-paren-mode nil)
1974 ;; Postpone history adding till later
1975 (history-add-new-input nil))
1976 ;; prompt the user for the file name
1977 (setq ido-exit nil)
1978 (setq ido-final-text
1979 (catch 'ido
1980 (read-from-minibuffer (ido-make-prompt item prompt)
1981 (prog1 ido-text-init
1982 (setq ido-text-init nil))
1983 ido-completion-map nil history))))
1984 (ido-trace "read-from-minibuffer" ido-final-text)
1985 (if (get-buffer ido-completion-buffer)
1986 (kill-buffer ido-completion-buffer))
1988 (ido-trace "\n_EXIT_" ido-exit)
1990 (cond
1991 ((eq ido-exit 'refresh)
1992 (if (and (eq ido-use-merged-list 'auto)
1993 (or (input-pending-p)))
1994 (setq ido-use-merged-list nil
1995 ido-keep-item-list t))
1996 nil)
1998 ((eq ido-exit 'done)
1999 (setq done t
2000 ido-selected ido-text
2001 ido-exit nil))
2003 ((memq ido-exit '(edit chdir))
2004 (cond
2005 ((memq ido-cur-item '(file dir))
2006 (let* ((read-file-name-function nil)
2007 (edit (eq ido-exit 'edit))
2008 (d ido-current-directory)
2009 (f ido-text-init)
2010 (new t))
2011 (setq ido-text-init "")
2012 (while new
2013 (setq new (if edit
2014 (condition-case nil
2015 (read-file-name (concat prompt "[EDIT] ")
2016 (expand-file-name d)
2017 (concat d f) nil f)
2018 (quit (concat d f)))
2020 d (or (file-name-directory new) "/")
2021 f (file-name-nondirectory new)
2022 edit t)
2023 (if (or
2024 (file-directory-p d)
2025 (and (yes-or-no-p (format "Create directory %s? " d))
2026 (condition-case nil
2027 (progn (make-directory d t) t)
2028 (error
2029 (message "Could not create directory")
2030 (sit-for 1)
2031 nil))))
2032 (progn
2033 (ido-set-current-directory d nil (eq ido-exit 'chdir))
2034 (setq ido-text-init f
2035 new nil))))))
2037 (setq ido-text-init
2038 (condition-case nil
2039 (read-string (concat prompt "[EDIT] ") ido-final-text)
2040 (quit ido-final-text)))))
2042 nil)
2044 ((eq ido-exit 'keep)
2045 (setq ido-keep-item-list t))
2047 ((memq ido-exit '(dired fallback find-file switch-to-buffer insert-buffer insert-file))
2048 (setq done t))
2050 ((memq ido-exit '(updir push))
2051 ;; cannot go up if already at the root-dir (Unix) or at the
2052 ;; root-dir of a certain drive (Windows or MS-DOS).
2053 (if (ido-is-tramp-root)
2054 (when (string-match "\\`\\(/\\([^/]+[:@]\\)*\\)\\([^/]+\\)[:@]\\'" ido-current-directory)
2055 (setq ido-text-init (match-string 3 ido-current-directory))
2056 (ido-set-current-directory (match-string 1 ido-current-directory))
2057 (setq ido-set-default-item t))
2058 (unless (ido-is-root-directory)
2059 (when (eq ido-exit 'push)
2060 (setq ido-input-stack (cons (cons ido-cur-item ido-text) ido-input-stack))
2061 (setq ido-cur-item 'dir)
2062 (setq ido-text-init (file-name-nondirectory (substring ido-current-directory 0 -1)))
2063 (ido-trace "push" ido-input-stack))
2064 (ido-set-current-directory (file-name-directory (substring ido-current-directory 0 -1)))
2065 (setq ido-set-default-item t))))
2067 ((eq ido-exit 'pop)
2068 (ido-trace "pop" ido-input-stack)
2069 (let ((elt (car ido-input-stack)))
2070 (setq ido-input-stack (cdr ido-input-stack))
2071 (ido-set-current-directory (concat ido-current-directory ido-text))
2072 (setq ido-cur-item (car elt))
2073 (setq ido-text-init (cdr elt))))
2075 ((eq ido-exit 'pop-all)
2076 (ido-trace "pop-all" ido-input-stack)
2077 (while ido-input-stack
2078 (let ((elt (car ido-input-stack)))
2079 (setq ido-input-stack (cdr ido-input-stack))
2080 (ido-set-current-directory (concat ido-current-directory ido-text))
2081 (setq ido-cur-item (car elt))
2082 (setq ido-text-init (cdr elt)))))
2084 ;; Handling the require-match must be done in a better way.
2085 ((and require-match
2086 (not (memq require-match '(confirm confirm-after-completion)))
2087 (not (if ido-directory-too-big
2088 (file-exists-p (concat ido-current-directory ido-final-text))
2089 (ido-existing-item-p))))
2090 (error "Must specify valid item"))
2093 (setq ido-selected
2094 (if (or (eq ido-exit 'takeprompt)
2095 (null ido-matches))
2096 ido-final-text
2097 ;; else take head of list
2098 (ido-name (car ido-matches))))
2100 (cond
2101 ((memq item '(buffer list))
2102 (setq done t))
2104 ((string-equal "./" ido-selected)
2105 nil)
2107 ((string-equal "../" ido-selected)
2108 ;; cannot go up if already at the root-dir (Unix) or at the
2109 ;; root-dir of a certain drive (Windows or MS-DOS).
2110 (or (ido-is-root-directory)
2111 (ido-set-current-directory (file-name-directory (substring ido-current-directory 0 -1))))
2112 (setq ido-set-default-item t))
2114 ((and (string-match (if ido-enable-tramp-completion ".[:@]\\'" ".:\\'") ido-selected)
2115 (ido-is-root-directory) ;; Ange-ftp or Tramp
2116 (not (ido-local-file-exists-p ido-selected)))
2117 (ido-set-current-directory ido-current-directory ido-selected)
2118 (ido-trace "tramp prefix" ido-selected)
2119 (if (ido-is-slow-ftp-host)
2120 (setq ido-exit 'fallback
2121 done t)
2122 (setq ido-set-default-item t)))
2124 ((or (string-match "[/\\][^/\\]" ido-selected)
2125 (and (memq system-type '(windows-nt ms-dos))
2126 (string-match "\\`[a-zA-Z]:" ido-selected)))
2127 (ido-set-current-directory (file-name-directory ido-selected))
2128 (setq ido-set-default-item t))
2130 ((string-match "\\`~" ido-selected)
2131 (ido-set-current-home ido-selected))
2133 ((ido-final-slash ido-selected)
2134 (if ido-enable-last-directory-history
2135 (let ((x (assoc ido-current-directory ido-last-directory-list)))
2136 (if x
2137 (setcdr x ido-selected)
2138 (setq ido-last-directory-list
2139 (cons (cons ido-current-directory ido-selected) ido-last-directory-list)))))
2140 (ido-set-current-directory ido-current-directory ido-selected)
2141 (if ido-input-stack
2142 ; automatically pop stack elements which match existing files or directories
2143 (let (elt)
2144 (while (and (setq elt (car ido-input-stack))
2145 (file-exists-p (concat ido-current-directory (cdr elt))))
2146 (if (setq ido-input-stack (cdr ido-input-stack))
2147 (ido-set-current-directory ido-current-directory (cdr elt))
2148 (setq ido-text-init (cdr elt)))
2149 (setq ido-cur-item (car elt))))
2150 (setq ido-set-default-item t)))
2153 (setq done t))))))
2154 (and history (add-to-history history ido-selected))
2155 ido-selected))
2157 (defun ido-edit-input ()
2158 "Edit absolute file name entered so far with ido; terminate by RET.
2159 If cursor is not at the end of the user input, move to end of input."
2160 (interactive)
2161 (if (not (eobp))
2162 (end-of-line)
2163 (setq ido-text-init (if ido-matches (ido-name (car ido-matches)) ido-text))
2164 (setq ido-exit 'edit)
2165 (exit-minibuffer)))
2167 ;;; MAIN FUNCTIONS
2168 (defun ido-buffer-internal (method &optional fallback prompt default initial switch-cmd)
2169 ;; Internal function for ido-switch-buffer and friends
2170 (if (not ido-mode)
2171 (progn
2172 (run-hook-with-args 'ido-before-fallback-functions
2173 (or fallback 'switch-to-buffer))
2174 (call-interactively (or fallback 'switch-to-buffer)))
2175 (let* ((ido-context-switch-command switch-cmd)
2176 (ido-current-directory nil)
2177 (ido-directory-nonreadable nil)
2178 (ido-directory-too-big nil)
2179 (ido-use-virtual-buffers (if (eq method 'kill)
2180 nil ;; Don't consider virtual buffers for killing
2181 ido-use-virtual-buffers))
2182 (require-match (confirm-nonexistent-file-or-buffer))
2183 (buf (ido-read-internal 'buffer (or prompt "Buffer: ") 'ido-buffer-history default
2184 require-match initial))
2185 filename)
2187 ;; Choose the buffer name: either the text typed in, or the head
2188 ;; of the list of matches
2190 (cond
2191 ((eq ido-exit 'find-file)
2192 (ido-file-internal
2193 (if (memq method '(other-window other-frame)) method ido-default-file-method)
2194 nil nil nil nil ido-text))
2196 ((eq ido-exit 'insert-file)
2197 (ido-file-internal 'insert 'insert-file nil "Insert file: " nil ido-text 'ido-enter-insert-buffer))
2199 ((eq ido-exit 'fallback)
2200 (let ((read-buffer-function nil))
2201 (setq this-command (or fallback 'switch-to-buffer))
2202 (run-hook-with-args 'ido-before-fallback-functions this-command)
2203 (call-interactively this-command)))
2205 ;; Check buf is non-nil.
2206 ((not buf) nil)
2207 ((= (length buf) 0) nil)
2209 ;; View buffer if it exists
2210 ((get-buffer buf)
2211 (add-to-history 'buffer-name-history buf)
2212 (if (eq method 'insert)
2213 (progn
2214 (ido-record-command 'insert-buffer buf)
2215 (push-mark
2216 (save-excursion
2217 (insert-buffer-substring (get-buffer buf))
2218 (point))))
2219 (ido-visit-buffer buf method t)))
2221 ;; check for a virtual buffer reference
2222 ((and ido-use-virtual-buffers ido-virtual-buffers
2223 (setq filename (assoc buf ido-virtual-buffers)))
2224 (ido-visit-buffer (find-file-noselect (cdr filename)) method t))
2226 ((and (eq ido-create-new-buffer 'prompt)
2227 (null require-match)
2228 (not (y-or-n-p (format "No buffer matching `%s', create one? " buf))))
2229 nil)
2231 ;; buffer doesn't exist
2232 ((and (eq ido-create-new-buffer 'never)
2233 (null require-match))
2234 (message "No buffer matching `%s'" buf))
2236 ((and (eq ido-create-new-buffer 'prompt)
2237 (null require-match)
2238 (not (y-or-n-p (format "No buffer matching `%s', create one? " buf))))
2239 nil)
2241 ;; create a new buffer
2243 (add-to-history 'buffer-name-history buf)
2244 (setq buf (get-buffer-create buf))
2245 (if (fboundp 'set-buffer-major-mode)
2246 (set-buffer-major-mode buf))
2247 (ido-visit-buffer buf method t))))))
2249 (defun ido-record-work-directory (&optional dir)
2250 (when (and (numberp ido-max-work-directory-list) (> ido-max-work-directory-list 0))
2251 (if (and (setq dir (or dir ido-current-directory)) (> (length dir) 0))
2252 (let ((items ido-work-directory-list-ignore-regexps)
2253 (case-fold-search nil))
2254 (while (and items dir)
2255 (if (string-match (car items) dir)
2256 (setq dir nil))
2257 (setq items (cdr items)))
2258 (if dir
2259 (setq ido-work-directory-list (cons dir (delete dir ido-work-directory-list))))))
2260 (if (> (length ido-work-directory-list) ido-max-work-directory-list)
2261 (setcdr (nthcdr (1- ido-max-work-directory-list) ido-work-directory-list) nil))))
2263 (defun ido-forget-work-directory ()
2264 (interactive)
2265 (when (and ido-current-directory ido-work-directory-list)
2266 (setq ido-work-directory-list (delete ido-current-directory ido-work-directory-list))
2267 (when ido-use-merged-list
2268 (ido-undo-merge-work-directory)
2269 (setq ido-exit 'refresh
2270 ido-try-merged-list t
2271 ido-use-merged-list t
2272 ido-text-init ido-text
2273 ido-rotate-temp t)
2274 (exit-minibuffer))))
2276 (defun ido-record-work-file (name)
2277 ;; Save NAME in ido-work-file-list
2278 (when (and (numberp ido-max-work-file-list) (> ido-max-work-file-list 0))
2280 (and ido-work-file-list (equal (car ido-work-file-list) name))
2281 (setq ido-work-file-list (cons name (delete name ido-work-file-list))))
2282 (if (> (length ido-work-file-list) ido-max-work-file-list)
2283 (setcdr (nthcdr (1- ido-max-work-file-list) ido-work-file-list) nil))))
2285 (defun ido-expand-directory (dir)
2286 ;; Expand DIR or use DEFAULT-DIRECTORY if nil.
2287 ;; Add final slash to result in case it was missing from DEFAULT-DIRECTORY.
2288 (ido-final-slash (expand-file-name (or dir default-directory)) t))
2290 (defun ido-file-internal (method &optional fallback default prompt item initial switch-cmd)
2291 ;; Internal function for ido-find-file and friends
2292 (unless item
2293 (setq item 'file))
2294 (let ((ido-current-directory (ido-expand-directory default))
2295 (ido-context-switch-command switch-cmd)
2296 ido-directory-nonreadable ido-directory-too-big
2297 filename)
2299 (if (or (not ido-mode) (ido-is-slow-ftp-host))
2300 (setq filename t
2301 ido-exit 'fallback)
2302 (setq ido-directory-nonreadable
2303 (ido-nonreadable-directory-p ido-current-directory)
2304 ido-directory-too-big
2305 (and (not ido-directory-nonreadable)
2306 (ido-directory-too-big-p ido-current-directory))))
2308 (when (and (eq item 'file)
2309 (or ido-use-url-at-point ido-use-filename-at-point))
2310 (let (fn d)
2311 (require 'ffap)
2312 ;; Duplicate code from ffap-guesser as we want different
2313 ;; behavior for files and URLs.
2314 (cond
2315 ((with-no-warnings
2316 (and ido-use-url-at-point
2317 ffap-url-regexp
2318 (ffap-fixup-url (or (ffap-url-at-point)
2319 (ffap-gopher-at-point)))))
2320 (setq ido-exit 'ffap
2321 filename t))
2323 ((and ido-use-filename-at-point
2324 (setq fn (with-no-warnings
2325 (if (eq ido-use-filename-at-point 'guess)
2326 (ffap-guesser)
2327 (ffap-string-at-point))))
2328 (not (string-match "^http:/" fn))
2329 (let ((absolute-fn (expand-file-name fn)))
2330 (setq d (if (file-directory-p absolute-fn)
2331 (file-name-as-directory absolute-fn)
2332 (file-name-directory absolute-fn))))
2333 (file-directory-p d))
2334 (setq ido-current-directory d)
2335 (setq initial (file-name-nondirectory fn))))))
2337 (let (ido-saved-vc-hb
2338 (vc-handled-backends (and (boundp 'vc-handled-backends) vc-handled-backends))
2339 (ido-work-directory-index -1)
2340 (ido-work-file-index -1)
2341 (ido-find-literal nil))
2343 (unless filename
2344 (setq ido-saved-vc-hb vc-handled-backends)
2345 (let ((minibuffer-completing-file-name t))
2346 (setq filename (ido-read-internal item
2347 (or prompt "Find file: ")
2348 'ido-file-history
2349 (and (eq method 'alt-file) buffer-file-name)
2350 (confirm-nonexistent-file-or-buffer) initial))))
2352 ;; Choose the file name: either the text typed in, or the head
2353 ;; of the list of matches
2355 (cond
2356 ((eq ido-exit 'fallback)
2357 ;; Need to guard setting of default-directory here, since
2358 ;; we don't want to change directory of current buffer.
2359 (let ((default-directory ido-current-directory)
2360 (read-file-name-function nil))
2361 (setq this-command (or fallback 'find-file))
2362 (run-hook-with-args 'ido-before-fallback-functions this-command)
2363 (call-interactively this-command)))
2365 ((eq ido-exit 'switch-to-buffer)
2366 (ido-buffer-internal
2367 (if (memq method '(other-window other-frame)) method ido-default-buffer-method)
2368 nil nil nil ido-text))
2370 ((eq ido-exit 'insert-buffer)
2371 (ido-buffer-internal 'insert 'insert-buffer "Insert buffer: " nil ido-text 'ido-enter-insert-file))
2373 ((eq ido-exit 'dired)
2374 (dired (concat ido-current-directory (or ido-text ""))))
2376 ((eq ido-exit 'ffap)
2377 (find-file-at-point))
2379 ((eq method 'alt-file)
2380 (ido-record-work-file filename)
2381 (setq default-directory ido-current-directory)
2382 (ido-record-work-directory)
2383 (find-alternate-file filename))
2385 ((memq method '(dired list-directory))
2386 (if (equal filename ".")
2387 (setq filename ""))
2388 (let* ((dirname (ido-final-slash (concat ido-current-directory filename) t))
2389 (file (substring dirname 0 -1)))
2390 (cond
2391 ((file-directory-p dirname)
2392 (ido-record-command method dirname)
2393 (ido-record-work-directory dirname)
2394 (funcall method dirname))
2395 ((file-directory-p ido-current-directory)
2396 (cond
2397 ((file-exists-p file)
2398 (ido-record-command method ido-current-directory)
2399 (ido-record-work-directory)
2400 (funcall method ido-current-directory)
2401 (if (eq method 'dired)
2402 (with-no-warnings
2403 (dired-goto-file (expand-file-name file)))))
2404 ((string-match "[[*?]" filename)
2405 (setq dirname (concat ido-current-directory filename))
2406 (ido-record-command method dirname)
2407 (ido-record-work-directory)
2408 (funcall method dirname))
2409 ((y-or-n-p (format "Directory %s does not exist. Create it? " filename))
2410 (ido-record-command method dirname)
2411 (ido-record-work-directory dirname)
2412 (make-directory-internal dirname)
2413 (funcall method dirname))
2415 ;; put make-directory command on history
2416 (ido-record-command 'make-directory dirname))))
2417 (t (error "No such directory")))))
2419 ((eq method 'write)
2420 (ido-record-work-file filename)
2421 (setq default-directory ido-current-directory)
2422 (setq filename (concat ido-current-directory filename))
2423 (ido-record-command 'write-file filename)
2424 (add-to-history 'file-name-history filename)
2425 (ido-record-work-directory)
2426 (write-file filename t))
2428 ((eq method 'read-only)
2429 (ido-record-work-file filename)
2430 (setq filename (concat ido-current-directory filename))
2431 (ido-record-command fallback filename)
2432 (ido-record-work-directory)
2433 (run-hook-with-args 'ido-before-fallback-functions fallback)
2434 (funcall fallback filename))
2436 ((eq method 'insert)
2437 (ido-record-work-file filename)
2438 (setq filename (concat ido-current-directory filename))
2439 (ido-record-command
2440 (if ido-find-literal 'insert-file-literally 'insert-file)
2441 filename)
2442 (add-to-history 'file-name-history filename)
2443 (ido-record-work-directory)
2444 (insert-file-1 filename
2445 (if ido-find-literal
2446 #'insert-file-contents-literally
2447 #'insert-file-contents)))
2449 (filename
2450 (ido-record-work-file filename)
2451 (setq filename (concat ido-current-directory filename))
2452 (ido-record-command 'find-file filename)
2453 (add-to-history 'file-name-history filename)
2454 (ido-record-work-directory)
2455 (ido-visit-buffer (find-file-noselect filename nil ido-find-literal) method))))))
2457 (defun ido-existing-item-p ()
2458 ;; Return non-nil if there is a matching item
2459 (not (null ido-matches)))
2461 ;;; COMPLETION CODE
2463 (defun ido-set-common-completion ()
2464 ;; Find common completion of `ido-text' in `ido-matches'
2465 ;; The result is stored in `ido-common-match-string'
2466 (let (val)
2467 (setq ido-common-match-string nil)
2468 (if (and ido-matches
2469 (not ido-enable-regexp) ;; testing
2470 (stringp ido-text)
2471 (> (length ido-text) 0))
2472 (if (setq val (ido-find-common-substring ido-matches ido-text))
2473 (setq ido-common-match-string val)))
2474 val))
2476 (defun ido-complete ()
2477 "Try and complete the current pattern amongst the file names."
2478 (interactive)
2479 (let (res)
2480 (cond
2481 (ido-incomplete-regexp
2482 ;; Do nothing
2484 ((and (memq ido-cur-item '(file dir))
2485 (string-match "[$]" ido-text))
2486 (let ((evar (substitute-in-file-name (concat ido-current-directory ido-text))))
2487 (if (not (file-exists-p (file-name-directory evar)))
2488 (message "Expansion generates non-existing directory name")
2489 (if (file-directory-p evar)
2490 (ido-set-current-directory evar)
2491 (let ((d (or (file-name-directory evar) "/"))
2492 (f (file-name-nondirectory evar)))
2493 (when (file-directory-p d)
2494 (ido-set-current-directory d)
2495 (setq ido-text-init f))))
2496 (setq ido-exit 'refresh)
2497 (exit-minibuffer))))
2499 (ido-directory-too-big
2500 (setq ido-directory-too-big nil)
2501 (setq ido-text-init ido-text)
2502 (setq ido-exit 'refresh)
2503 (exit-minibuffer))
2505 ((not ido-matches)
2506 (when ido-completion-buffer
2507 (call-interactively (setq this-command ido-cannot-complete-command))))
2509 ((and (= 1 (length ido-matches))
2510 (not (and ido-enable-tramp-completion
2511 (string-equal ido-current-directory "/")
2512 (string-match ".[@:]\\'" (ido-name (car ido-matches)))))
2513 (not (ido-local-file-exists-p (ido-name (car ido-matches)))))
2514 ;; only one choice, so select it.
2515 (if (not ido-confirm-unique-completion)
2516 (exit-minibuffer)
2517 (setq ido-rescan (not ido-enable-prefix))
2518 (delete-region (minibuffer-prompt-end) (point))
2519 (insert (ido-name (car ido-matches)))))
2521 (t ;; else there could be some completions
2522 (setq res ido-common-match-string)
2523 (if (and (not (memq res '(t nil)))
2524 (not (equal res ido-text)))
2525 ;; found something to complete, so put it in the minibuffer.
2526 (progn
2527 ;; move exact match to front if not in prefix mode
2528 (setq ido-rescan (not ido-enable-prefix))
2529 (delete-region (minibuffer-prompt-end) (point))
2530 (insert res))
2531 ;; else nothing to complete
2532 (call-interactively (setq this-command ido-cannot-complete-command))
2533 )))))
2535 (defun ido-complete-space ()
2536 "Try completion unless inserting the space makes sense."
2537 (interactive)
2538 (if (and (stringp ido-common-match-string)
2539 (stringp ido-text)
2540 (cond
2541 ((> (length ido-common-match-string) (length ido-text))
2542 (= (aref ido-common-match-string (length ido-text)) ? ))
2543 (ido-matches
2544 (let (insert-space
2545 (re (concat (regexp-quote ido-text) " "))
2546 (comp ido-matches))
2547 (while comp
2548 (if (string-match re (ido-name (car comp)))
2549 (setq comp nil insert-space t)
2550 (setq comp (cdr comp))))
2551 insert-space))
2552 (t nil)))
2553 (insert " ")
2554 (ido-complete)))
2556 (defun ido-undo-merge-work-directory (&optional text try refresh)
2557 "Undo or redo last ido directory merge operation.
2558 If no merge has yet taken place, toggle automatic merging option."
2559 (interactive)
2560 (cond
2561 (ido-pre-merge-state
2562 (ido-set-current-directory (nth 1 ido-pre-merge-state))
2563 (setq ido-text-init (or text (car ido-pre-merge-state))
2564 ido-cur-list (nth 2 ido-pre-merge-state)
2565 ido-ignored-list (nth 3 ido-pre-merge-state)
2566 ido-matches (nth 4 ido-pre-merge-state)
2567 ido-use-merged-list nil
2568 ido-try-merged-list try
2569 ido-keep-item-list (not refresh)
2570 ido-rescan nil
2571 ido-exit 'refresh
2572 ido-pre-merge-state nil)
2573 (exit-minibuffer))
2574 (text
2575 nil)
2576 (ido-try-merged-list
2577 (setq ido-try-merged-list nil))
2578 (ido-matches
2579 (setq ido-try-merged-list t))
2580 ((not ido-use-merged-list)
2581 (ido-merge-work-directories))))
2583 ;;; Magic C-f
2585 (defun ido-magic-forward-char (arg)
2586 "Move forward in user input or perform magic action.
2587 If no user input is present, or at end of input, perform magic actions:
2588 C-x C-b ... C-f switch to `ido-find-file'.
2589 C-x C-f ... C-f fallback to non-ido `find-file'.
2590 C-x C-d ... C-f fallback to non-ido brief `dired'.
2591 C-x d ... C-f fallback to non-ido `dired'."
2592 (interactive "P")
2593 (cond
2594 ((or arg (not (eobp)))
2595 (forward-char (min (prefix-numeric-value arg)
2596 (- (point-max) (point)))))
2597 ((memq ido-cur-item '(file dir))
2598 (ido-fallback-command))
2599 (ido-context-switch-command
2600 (call-interactively ido-context-switch-command))
2601 ((eq ido-cur-item 'buffer)
2602 (ido-enter-find-file))))
2604 ;;; Magic C-b
2606 (defun ido-magic-backward-char (arg)
2607 "Move backward in user input or perform magic action.
2608 If no user input is present, or at start of input, perform magic actions:
2609 C-x C-f C-b switch to `ido-switch-buffer'.
2610 C-x C-d C-b switch to `ido-switch-buffer'.
2611 C-x d C-b switch to `ido-switch-buffer'.
2612 C-x C-b C-b fallback to non-ido `switch-to-buffer'."
2613 (interactive "P")
2614 (cond
2615 ((or arg (> (point) (minibuffer-prompt-end)))
2616 (forward-char
2617 (- (min (prefix-numeric-value arg)
2618 (- (point) (minibuffer-prompt-end))))))
2619 ((eq last-command this-command)
2620 (when (and (memq ido-cur-item '(file dir))
2621 (not (bobp)))
2622 (ido-push-dir))) ; else do nothing
2623 ((eq ido-cur-item 'buffer)
2624 (ido-fallback-command))
2625 (ido-context-switch-command
2626 (call-interactively ido-context-switch-command))
2628 (ido-enter-switch-buffer))))
2630 ;;; Magic C-d
2632 (defun ido-magic-delete-char (arg)
2633 "Delete following char in user input or perform magic action.
2634 If at end of user input, perform magic actions:
2635 C-x C-f ... C-d enter `dired' on current directory."
2636 (interactive "P")
2637 (cond
2638 ((or arg (not (eobp)))
2639 (delete-char (min (prefix-numeric-value arg)
2640 (- (point-max) (point)))))
2641 (ido-context-switch-command
2642 nil)
2643 ((memq ido-cur-item '(file dir))
2644 (ido-enter-dired))))
2647 ;;; TOGGLE FUNCTIONS
2649 (defun ido-toggle-case ()
2650 "Toggle the value of `ido-case-fold'."
2651 (interactive)
2652 (setq ido-case-fold (not ido-case-fold))
2653 ;; ask for list to be regenerated.
2654 (setq ido-rescan t))
2656 (defun ido-toggle-regexp ()
2657 "Toggle the value of `ido-enable-regexp'."
2658 (interactive)
2659 (setq ido-enable-regexp (not ido-enable-regexp))
2660 ;; ask for list to be regenerated.
2661 (setq ido-rescan t))
2663 (defun ido-toggle-prefix ()
2664 "Toggle the value of `ido-enable-prefix'."
2665 (interactive)
2666 (setq ido-enable-prefix (not ido-enable-prefix))
2667 ;; ask for list to be regenerated.
2668 (setq ido-rescan t))
2670 (defun ido-toggle-ignore ()
2671 "Toggle ignoring files specified with `ido-ignore-files'."
2672 (interactive)
2673 (if (and (not (eobp)) (> (point) (minibuffer-prompt-end)))
2674 (goto-char (minibuffer-prompt-end))
2675 (if ido-directory-too-big
2676 (progn
2677 (message "Reading directory...")
2678 (setq ido-directory-too-big nil))
2679 (setq ido-process-ignore-lists (not ido-process-ignore-lists)))
2680 (setq ido-text-init ido-text)
2681 (setq ido-exit 'refresh)
2682 (exit-minibuffer)))
2684 (defun ido-toggle-vc ()
2685 "Disable version control for this file."
2686 (interactive)
2687 (if (and ido-mode (eq ido-cur-item 'file))
2688 (progn
2689 (setq vc-handled-backends
2690 (if vc-handled-backends nil ido-saved-vc-hb))
2691 (setq ido-text-init ido-text)
2692 (setq ido-exit 'keep)
2693 (exit-minibuffer))))
2695 (defun ido-toggle-literal ()
2696 "Toggle literal reading of this file."
2697 (interactive)
2698 (if (and ido-mode (eq ido-cur-item 'file))
2699 (progn
2700 (setq ido-find-literal (not ido-find-literal))
2701 (setq ido-text-init ido-text)
2702 (setq ido-exit 'keep)
2703 (exit-minibuffer))))
2705 (defun ido-toggle-virtual-buffers ()
2706 "Toggle the use of virtual buffers.
2707 See `ido-use-virtual-buffers' for explanation of virtual buffer."
2708 (interactive)
2709 (when (and ido-mode (eq ido-cur-item 'buffer))
2710 (setq ido-use-virtual-buffers (not ido-use-virtual-buffers))
2711 (setq ido-text-init ido-text)
2712 (setq ido-exit 'refresh)
2713 (exit-minibuffer)))
2715 (defun ido-reread-directory ()
2716 "Read current directory again.
2717 May be useful if cached version is no longer valid, but directory
2718 timestamp has not changed (e.g. with ftp or on Windows)."
2719 (interactive)
2720 (if (and ido-mode (memq ido-cur-item '(file dir)))
2721 (progn
2722 (if (ido-is-unc-root)
2723 (setq ido-unc-hosts-cache t)
2724 (ido-remove-cached-dir ido-current-directory))
2725 (setq ido-text-init ido-text)
2726 (setq ido-rotate-temp t)
2727 (setq ido-exit 'refresh)
2728 (exit-minibuffer))))
2730 (defun ido-exit-minibuffer ()
2731 "Exit minibuffer, but make sure we have a match if one is needed."
2732 (interactive)
2733 (if (and (or (not ido-require-match)
2734 (if (memq ido-require-match '(confirm confirm-after-completion))
2735 (if (or (eq ido-cur-item 'dir)
2736 (eq last-command this-command))
2738 (setq ido-show-confirm-message t)
2739 nil))
2740 (ido-existing-item-p))
2741 (not ido-incomplete-regexp))
2742 (exit-minibuffer)))
2744 (defun ido-select-text ()
2745 "Select the buffer or file named by the prompt.
2746 If no buffer or file exactly matching the prompt exists, maybe create a new one."
2747 (interactive)
2748 (setq ido-exit 'takeprompt)
2749 (exit-minibuffer))
2751 (defun ido-fallback-command ()
2752 "Fallback to non-ido version of current command."
2753 (interactive)
2754 (let ((i (length ido-text)))
2755 (while (> i 0)
2756 (push (aref ido-text (setq i (1- i))) unread-command-events)))
2757 (setq ido-exit 'fallback)
2758 (exit-minibuffer))
2760 (defun ido-enter-find-file ()
2761 "Drop into `find-file' from buffer switching."
2762 (interactive)
2763 (setq ido-exit 'find-file)
2764 (exit-minibuffer))
2766 (defun ido-enter-switch-buffer ()
2767 "Drop into `ido-switch-buffer' from file switching."
2768 (interactive)
2769 (setq ido-exit 'switch-to-buffer)
2770 (exit-minibuffer))
2772 (defun ido-enter-dired ()
2773 "Drop into `dired' from file switching."
2774 (interactive)
2775 (setq ido-exit 'dired)
2776 (exit-minibuffer))
2778 (defun ido-enter-insert-buffer ()
2779 "Drop into `insert-buffer' from insert file."
2780 (interactive)
2781 (setq ido-exit 'insert-buffer)
2782 (exit-minibuffer))
2784 (defun ido-enter-insert-file ()
2785 "Drop into `insert-file' from insert buffer."
2786 (interactive)
2787 (setq ido-exit 'insert-file)
2788 (exit-minibuffer))
2791 (defun ido-up-directory (&optional clear)
2792 "Go up one directory level."
2793 (interactive "P")
2794 (setq ido-text-init (if clear nil ido-text))
2795 (setq ido-exit 'updir)
2796 (setq ido-rotate-temp t)
2797 (exit-minibuffer))
2799 (defun ido-delete-backward-updir (count)
2800 "Delete char backwards, or at beginning of buffer, go up one level."
2801 (interactive "P")
2802 (cond
2803 ((= (minibuffer-prompt-end) (point))
2804 (if (not count)
2805 (ido-up-directory t)))
2806 ((and ido-pre-merge-state (string-equal (car ido-pre-merge-state) ido-text))
2807 (ido-undo-merge-work-directory (substring ido-text 0 -1) t t))
2808 ((eq this-original-command 'viper-backward-char)
2809 (funcall this-original-command (prefix-numeric-value count)))
2810 ((eq this-original-command 'viper-del-backward-char-in-insert)
2811 (funcall this-original-command))
2813 (delete-char (- (prefix-numeric-value count))))))
2815 (defun ido-delete-backward-word-updir (count)
2816 "Delete all chars backwards, or at beginning of buffer, go up one level."
2817 (interactive "P")
2818 (if (= (minibuffer-prompt-end) (point))
2819 (if (not count)
2820 (ido-up-directory t))
2821 (if (eq this-original-command 'viper-delete-backward-word)
2822 (funcall this-original-command (prefix-numeric-value count))
2823 (backward-kill-word (prefix-numeric-value count)))))
2825 (defun ido-get-work-directory (&optional incr must-match)
2826 (let ((n (length ido-work-directory-list))
2827 (i ido-work-directory-index)
2828 (j 0)
2829 dir)
2830 (if (or (not ido-text) (= (length ido-text) 0))
2831 (setq must-match nil))
2832 (while (< j n)
2833 (setq i (+ i incr)
2834 j (1+ j))
2835 (if (> incr 0)
2836 (if (>= i n) (setq i 0))
2837 (if (< i 0) (setq i (1- n))))
2838 (setq dir (nth i ido-work-directory-list))
2839 (if (and dir
2840 (not (equal dir ido-current-directory))
2841 (file-directory-p dir)
2842 (or (not must-match)
2843 ;; TODO. check for nonreadable and too-big.
2844 (ido-set-matches-1
2845 (if (eq ido-cur-item 'file)
2846 (ido-make-file-list-1 dir)
2847 (ido-make-dir-list-1 dir)))))
2848 (setq j n)
2849 (setq dir nil)))
2850 (if dir
2851 (setq ido-work-directory-index i))
2852 dir))
2854 (defun ido-prev-work-directory ()
2855 "Change to next working directory in list."
2856 (interactive)
2857 (let ((dir (ido-get-work-directory 1 ido-work-directory-match-only)))
2858 (when dir
2859 (ido-set-current-directory dir)
2860 (setq ido-exit 'refresh)
2861 (setq ido-text-init ido-text)
2862 (setq ido-rotate-temp t)
2863 (exit-minibuffer))))
2865 (defun ido-next-work-directory ()
2866 "Change to previous working directory in list."
2867 (interactive)
2868 (let ((dir (ido-get-work-directory -1 ido-work-directory-match-only)))
2869 (when dir
2870 (ido-set-current-directory dir)
2871 (setq ido-exit 'refresh)
2872 (setq ido-text-init ido-text)
2873 (setq ido-rotate-temp t)
2874 (exit-minibuffer))))
2876 (defun ido-merge-work-directories ()
2877 "Search (and merge) work directories for files matching the current input string."
2878 (interactive)
2879 (setq ido-use-merged-list t ido-try-merged-list t)
2880 (setq ido-exit 'refresh)
2881 (setq ido-text-init ido-text)
2882 (setq ido-rotate-temp t)
2883 (exit-minibuffer))
2885 (defun ido-wide-find-file (&optional file)
2886 "Prompt for FILE to search for using find, starting from current directory."
2887 (interactive)
2888 (unless file
2889 (let ((enable-recursive-minibuffers t))
2890 (setq file
2891 (condition-case nil
2892 (read-string (concat "Wide find file: " ido-current-directory) ido-text)
2893 (quit "")))))
2894 (when (> (length file) 0)
2895 (setq ido-use-merged-list t ido-try-merged-list 'wide)
2896 (setq ido-exit 'refresh)
2897 (setq ido-text-init file)
2898 (setq ido-rotate-temp t)
2899 (exit-minibuffer)))
2901 (defun ido-wide-find-dir (&optional dir)
2902 "Prompt for DIR to search for using find, starting from current directory."
2903 (interactive)
2904 (unless dir
2905 (let ((enable-recursive-minibuffers t))
2906 (setq dir
2907 (condition-case nil
2908 (read-string (concat "Wide find directory: " ido-current-directory) ido-text)
2909 (quit "")))))
2910 (when (> (length dir) 0)
2911 (setq ido-use-merged-list t ido-try-merged-list 'wide)
2912 (setq ido-exit 'refresh)
2913 (setq ido-text-init (ido-final-slash dir t))
2914 (setq ido-rotate-temp t)
2915 (exit-minibuffer)))
2917 (defun ido-wide-find-dir-or-delete-dir (&optional dir)
2918 "Prompt for DIR to search for using find, starting from current directory.
2919 If input stack is non-empty, delete current directory component."
2920 (interactive)
2921 (if ido-input-stack
2922 (ido-delete-backward-word-updir 1)
2923 (ido-wide-find-dir)))
2925 (defun ido-take-first-match ()
2926 "Use first matching item as input text."
2927 (interactive)
2928 (when ido-matches
2929 (setq ido-text-init (ido-name (car ido-matches)))
2930 (setq ido-exit 'refresh)
2931 (exit-minibuffer)))
2933 (defun ido-push-dir ()
2934 "Move to previous directory in file name, push current input on stack."
2935 (interactive)
2936 (setq ido-exit 'push)
2937 (exit-minibuffer))
2939 (defun ido-push-dir-first ()
2940 "Move to previous directory in file name, push first match on stack."
2941 (interactive)
2942 (if ido-matches
2943 (setq ido-text (ido-name (car ido-matches))))
2944 (setq ido-exit 'push)
2945 (exit-minibuffer))
2947 (defun ido-pop-dir (arg)
2948 "Pop directory from input stack back to input.
2949 With \\[universal-argument], pop all elements."
2950 (interactive "P")
2951 (when ido-input-stack
2952 (setq ido-exit (if arg 'pop-all 'pop))
2953 (exit-minibuffer)))
2955 (defun ido-wide-find-file-or-pop-dir (arg)
2956 (interactive "P")
2957 (if ido-input-stack
2958 (ido-pop-dir arg)
2959 (ido-wide-find-file)))
2961 (defun ido-make-directory (&optional dir)
2962 "Prompt for DIR to create in current directory."
2963 (interactive)
2964 (unless dir
2965 (let ((enable-recursive-minibuffers t))
2966 (setq dir
2967 (read-string (concat "Make directory: " ido-current-directory) ido-text))))
2968 (when (> (length dir) 0)
2969 (setq dir (concat ido-current-directory dir))
2970 (unless (file-exists-p dir)
2971 (make-directory dir t)
2972 (ido-set-current-directory dir)
2973 (setq ido-exit 'refresh)
2974 (setq ido-text-init nil)
2975 (setq ido-rotate-temp t)
2976 (exit-minibuffer))))
2978 (defun ido-get-work-file (incr)
2979 (let ((n (length ido-work-file-list))
2980 (i (+ ido-work-file-index incr))
2981 name)
2982 (if (> incr 0)
2983 (if (>= i n) (setq i 0))
2984 (if (< i 0) (setq i (1- n))))
2985 (setq name (nth i ido-work-file-list))
2986 (setq ido-work-file-index i)
2987 name))
2989 (defun ido-prev-work-file ()
2990 "Change to next working file name in list."
2991 (interactive)
2992 (let ((name (ido-get-work-file 1)))
2993 (when name
2994 (setq ido-text-init name)
2995 (setq ido-exit 'refresh)
2996 (exit-minibuffer))))
2998 (defun ido-next-work-file ()
2999 "Change to previous working file name in list."
3000 (interactive)
3001 (let ((name (ido-get-work-file -1)))
3002 (when name
3003 (setq ido-text-init name)
3004 (setq ido-exit 'refresh)
3005 (exit-minibuffer))))
3007 (defun ido-copy-current-file-name (all)
3008 "Insert file name of current buffer.
3009 If repeated, insert text from buffer instead."
3010 (interactive "P")
3011 (let* ((bfname (or (buffer-file-name ido-entry-buffer)
3012 (buffer-name ido-entry-buffer)))
3013 (name (and bfname (file-name-nondirectory bfname))))
3014 (when name
3015 (setq ido-text-init
3016 (if (or all
3017 (eq last-command this-command)
3018 (not (equal (file-name-directory bfname) ido-current-directory))
3019 (not (string-match "\\.[^.]*\\'" name)))
3020 name
3021 (substring name 0 (1+ (match-beginning 0)))))
3022 (setq ido-exit 'refresh
3023 ido-try-merged-list nil)
3024 (exit-minibuffer))))
3026 (defun ido-copy-current-word (all)
3027 "Insert current word (file or directory name) from current buffer."
3028 (interactive "P")
3029 (let ((word (with-current-buffer ido-entry-buffer
3030 (let ((p (point)) start-line end-line start-name name)
3031 (if (and mark-active (/= p (mark)))
3032 (setq start-name (mark))
3033 (beginning-of-line)
3034 (setq start-line (point))
3035 (end-of-line)
3036 (setq end-line (point))
3037 (goto-char p)
3038 (if (re-search-backward "[^-_a-zA-Z0-9:./\\~@]" start-line 1)
3039 (forward-char 1))
3040 (setq start-name (point))
3041 (re-search-forward "[-_a-zA-Z0-9:./\\~@]*" end-line 1)
3042 (if (= start-name (point))
3043 (setq start-name nil)))
3044 (and start-name
3045 (buffer-substring-no-properties start-name (point)))))))
3046 (if (cond
3047 ((not word) nil)
3048 ((string-match "\\`[~/]" word)
3049 (setq ido-text-init word
3050 ido-try-merged-list nil
3051 ido-exit 'chdir))
3052 ((string-match "/" word)
3053 (setq ido-text-init (concat ido-current-directory word)
3054 ido-try-merged-list nil
3055 ido-exit 'chdir))
3057 (setq ido-text-init word
3058 ido-try-merged-list nil
3059 ido-exit 'refresh)))
3060 (exit-minibuffer))))
3062 (defun ido-next-match ()
3063 "Put first element of `ido-matches' at the end of the list."
3064 (interactive)
3065 (if ido-matches
3066 (let ((next (cadr ido-matches)))
3067 (setq ido-cur-list (ido-chop ido-cur-list next))
3068 (setq ido-matches (ido-chop ido-matches next))
3069 (setq ido-rescan nil))))
3071 (defun ido-prev-match ()
3072 "Put last element of `ido-matches' at the front of the list."
3073 (interactive)
3074 (if ido-matches
3075 (let ((prev (car (last ido-matches))))
3076 (setq ido-cur-list (ido-chop ido-cur-list prev))
3077 (setq ido-matches (ido-chop ido-matches prev))
3078 (setq ido-rescan nil))))
3080 (defun ido-next-match-dir ()
3081 "Find next directory in match list.
3082 If work directories have been merged, cycle through directories for
3083 first matching file."
3084 (interactive)
3085 (if ido-use-merged-list
3086 (if ido-matches
3087 (let* ((elt (car ido-matches))
3088 (dirs (cdr elt)))
3089 (when (> (length dirs) 1)
3090 (setcdr elt (ido-chop dirs (cadr dirs))))
3091 (setq ido-rescan nil)))
3092 (let ((cnt (length ido-matches))
3093 (i 1))
3094 (while (and (< i cnt) (not (ido-final-slash (nth i ido-matches))))
3095 (setq i (1+ i)))
3096 (if (< i cnt)
3097 (setq ido-cur-list (ido-chop ido-cur-list (nth i ido-matches)))))))
3099 (defun ido-prev-match-dir ()
3100 "Find previous directory in match list.
3101 If work directories have been merged, cycle through directories
3102 for first matching file."
3103 (interactive)
3104 (if ido-use-merged-list
3105 (if ido-matches
3106 (let* ((elt (car ido-matches))
3107 (dirs (cdr elt)))
3108 (when (> (length dirs) 1)
3109 (setcdr elt (ido-chop dirs (car (last dirs)))))
3110 (setq ido-rescan nil)))
3111 (let* ((cnt (length ido-matches))
3112 (i (1- cnt)))
3113 (while (and (> i 0) (not (ido-final-slash (nth i ido-matches))))
3114 (setq i (1- i)))
3115 (if (> i 0)
3116 (setq ido-cur-list (ido-chop ido-cur-list (nth i ido-matches)))))))
3118 (defun ido-restrict-to-matches ()
3119 "Set current item list to the currently matched items."
3120 (interactive)
3121 (when ido-matches
3122 (setq ido-cur-list ido-matches
3123 ido-text-init ""
3124 ido-rescan nil
3125 ido-exit 'keep)
3126 (exit-minibuffer)))
3128 (defun ido-chop (items elem)
3129 "Remove all elements before ELEM and put them at the end of ITEMS."
3130 (let ((ret nil)
3131 (next nil)
3132 (sofar nil))
3133 (while (not ret)
3134 (setq next (car items))
3135 (if (equal next elem)
3136 (setq ret (append items (nreverse sofar)))
3137 ;; else
3138 (progn
3139 (setq items (cdr items))
3140 (setq sofar (cons next sofar)))))
3141 ret))
3143 (defun ido-name (item)
3144 ;; Return file name for current item, whether in a normal list
3145 ;; or a merged work directory list.
3146 (if (consp item) (car item) item))
3149 ;;; CREATE LIST OF ALL CURRENT FILES
3151 (defun ido-all-completions ()
3152 ;; Return unsorted list of all competions.
3153 (let ((ido-process-ignore-lists nil)
3154 (ido-directory-too-big nil))
3155 (cond
3156 ((eq ido-cur-item 'file)
3157 (ido-make-file-list-1 ido-current-directory))
3158 ((eq ido-cur-item 'dir)
3159 (ido-make-dir-list-1 ido-current-directory))
3160 ((eq ido-cur-item 'buffer)
3161 (ido-make-buffer-list-1))
3162 ((eq ido-cur-item 'list)
3163 ido-choice-list)
3164 (t nil))))
3167 ;; File list sorting
3169 (defun ido-file-lessp (a b)
3170 ;; Simple compare two file names.
3171 (string-lessp (ido-no-final-slash a) (ido-no-final-slash b)))
3174 (defun ido-file-extension-lessp (a b)
3175 ;; Compare file names according to ido-file-extensions-order list.
3176 (let ((n (compare-strings a 0 nil b 0 nil nil))
3177 lessp p)
3178 (if (eq n t)
3180 (if (< n 0)
3181 (setq n (1- (- n))
3182 p a a b b p
3183 lessp t)
3184 (setq n (1- n)))
3185 (cond
3186 ((= n 0)
3187 lessp)
3188 ((= (aref a n) ?.)
3189 (ido-file-extension-aux a b n lessp))
3191 (while (and (> n 2) (/= (aref a n) ?.))
3192 (setq n (1- n)))
3193 (if (> n 1)
3194 (ido-file-extension-aux a b n lessp)
3195 lessp))))))
3197 (defun ido-file-extension-aux (a b n lessp)
3198 (let ((oa (ido-file-extension-order a n))
3199 (ob (ido-file-extension-order b n)))
3200 (cond
3201 ((and oa ob)
3202 (cond
3203 ((= oa ob)
3204 lessp)
3205 (lessp
3206 (> oa ob))
3208 (< oa ob))))
3210 (not lessp))
3212 lessp)
3214 lessp))))
3216 (defun ido-file-extension-order (s n)
3217 (let ((l ido-file-extensions-order)
3218 (i 0) o do)
3219 (while l
3220 (cond
3221 ((eq (car l) t)
3222 (setq do i
3223 l (cdr l)))
3224 ((eq (compare-strings s n nil (car l) 0 nil nil) t)
3225 (setq o i
3226 l nil))
3228 (setq l (cdr l))))
3229 (setq i (1+ i)))
3230 (or o do)))
3233 (defun ido-sort-merged-list (items promote)
3234 ;; Input is list of ("file" . "dir") cons cells.
3235 ;; Output is sorted list of ("file "dir" ...) lists
3236 (let ((l (sort items (lambda (a b) (string-lessp (car b) (car a)))))
3237 res a cur dirs)
3238 (while l
3239 (setq a (car l)
3240 l (cdr l))
3241 (if (and res (string-equal (car (car res)) (car a)))
3242 (progn
3243 (setcdr (car (if cur (cdr res) res)) (cons (cdr a) (cdr (car res))))
3244 (if (and promote (string-equal ido-current-directory (cdr a)))
3245 (setq cur t)))
3246 (setq res (cons (list (car a) (cdr a)) res)
3247 cur nil)))
3248 res))
3250 (defun ido-wide-find-dirs-or-files (dir file &optional prefix finddir)
3251 ;; As ido-run-find-command, but returns a list of cons pairs ("file" . "dir")
3252 (let ((filenames
3253 (split-string
3254 (shell-command-to-string
3255 (concat "find "
3256 (shell-quote-argument dir)
3257 " -name "
3258 (shell-quote-argument
3259 (concat (if prefix "" "*") file "*"))
3260 " -type " (if finddir "d" "f") " -print"))))
3261 filename d f
3262 res)
3263 (while filenames
3264 (setq filename (car filenames)
3265 filenames (cdr filenames))
3266 (if (and (string-match "^/" filename)
3267 (file-exists-p filename))
3268 (setq d (file-name-directory filename)
3269 f (file-name-nondirectory filename)
3270 res (cons (cons (if finddir (ido-final-slash f t) f) d) res))))
3271 res))
3273 (defun ido-flatten-merged-list (items)
3274 ;; Create a list of directory names based on a merged directory list.
3275 (let (res)
3276 (while items
3277 (let* ((item (car items))
3278 (file (car item))
3279 (dirs (cdr item)))
3280 (while dirs
3281 (setq res (cons (concat (car dirs) file) res)
3282 dirs (cdr dirs))))
3283 (setq items (cdr items)))
3284 res))
3287 (defun ido-make-merged-file-list-1 (text auto wide)
3288 (let (res)
3289 (if (and (ido-final-slash text) ido-dir-file-cache)
3290 (if wide
3291 (setq res (ido-wide-find-dirs-or-files
3292 ido-current-directory (substring text 0 -1) ido-enable-prefix t))
3293 ;; Use list of cached directories
3294 (let ((re (concat (regexp-quote (substring text 0 -1)) "[^/:]*/\\'"))
3295 (dirs ido-dir-file-cache)
3296 dir b d f)
3297 (if nil ;; simple
3298 (while dirs
3299 (setq dir (car (car dirs))
3300 dirs (cdr dirs))
3301 (when (and (string-match re dir)
3302 (not (ido-ignore-item-p dir ido-ignore-directories-merge))
3303 (file-directory-p dir))
3304 (setq b (substring dir 0 -1)
3305 f (concat (file-name-nondirectory b) "/")
3306 d (file-name-directory b)
3307 res (cons (cons f d) res))))
3308 (while dirs
3309 (setq dir (car dirs)
3310 d (car dir)
3311 dirs (cdr dirs))
3312 (when (not (ido-ignore-item-p d ido-ignore-directories-merge))
3313 (setq dir (cdr (cdr dir)))
3314 (while dir
3315 (setq f (car dir)
3316 dir (cdr dir))
3317 (if (and (string-match re f)
3318 (not (ido-ignore-item-p f ido-ignore-directories)))
3319 (setq res (cons (cons f d) res)))))
3320 (if (and auto (input-pending-p))
3321 (setq dirs nil
3322 res t))))))
3323 (if wide
3324 (setq res (ido-wide-find-dirs-or-files
3325 ido-current-directory text ido-enable-prefix nil))
3326 (let ((ido-text text)
3327 (dirs ido-work-directory-list)
3328 (must-match (and text (> (length text) 0)))
3329 dir fl)
3330 (if (and auto (not (member ido-current-directory dirs)))
3331 (setq dirs (cons ido-current-directory dirs)))
3332 (while dirs
3333 (setq dir (car dirs)
3334 dirs (cdr dirs))
3335 (when (and dir (stringp dir)
3336 (or ido-merge-ftp-work-directories
3337 (not (ido-is-ftp-directory dir)))
3338 (file-directory-p dir)
3339 ;; TODO. check for nonreadable and too-big.
3340 (setq fl (if (eq ido-cur-item 'file)
3341 (ido-make-file-list-1 dir t)
3342 (ido-make-dir-list-1 dir t))))
3343 (if must-match
3344 (setq fl (ido-set-matches-1 fl)))
3345 (if fl
3346 (setq res (nconc fl res))))
3347 (if (and auto (input-pending-p))
3348 (setq dirs nil
3349 res t))))))
3350 res))
3352 (defun ido-make-merged-file-list (text auto wide)
3353 (let (res)
3354 (message "Searching for `%s'...." text)
3355 (condition-case nil
3356 (if (eq t (setq res
3357 (while-no-input
3358 (ido-make-merged-file-list-1 text auto wide))))
3359 (setq res 'input-pending-p))
3360 (quit
3361 (setq res t
3362 ido-try-merged-list nil
3363 ido-use-merged-list nil)))
3364 (when (and res (listp res))
3365 (setq res (ido-sort-merged-list res auto)))
3366 (when (and (or ido-rotate-temp ido-rotate-file-list-default)
3367 (listp res)
3368 (> (length text) 0))
3369 (let ((elt (assoc text res)))
3370 (when (and elt (not (eq elt (car res))))
3371 (setq res (delq elt res))
3372 (setq res (cons elt res)))))
3373 (message nil)
3374 res))
3376 (defun ido-make-buffer-list-1 (&optional frame visible)
3377 ;; Return list of non-ignored buffer names
3378 (delq nil
3379 (mapcar
3380 (lambda (x)
3381 (let ((name (buffer-name x)))
3382 (if (not (or (ido-ignore-item-p name ido-ignore-buffers) (member name visible)))
3383 name)))
3384 (buffer-list frame))))
3386 (defun ido-make-buffer-list (default)
3387 ;; Return the current list of buffers.
3388 ;; Currently visible buffers are put at the end of the list.
3389 ;; The hook `ido-make-buffer-list-hook' is run after the list has been
3390 ;; created to allow the user to further modify the order of the buffer names
3391 ;; in this list. If DEFAULT is non-nil, and corresponds to an existing buffer,
3392 ;; it is put to the start of the list.
3393 (let* ((ido-current-buffers (ido-get-buffers-in-frames 'current))
3394 (ido-temp-list (ido-make-buffer-list-1 (selected-frame) ido-current-buffers)))
3395 (if ido-temp-list
3396 (nconc ido-temp-list ido-current-buffers)
3397 (setq ido-temp-list ido-current-buffers))
3398 (if default
3399 (setq ido-temp-list
3400 (cons default (delete default ido-temp-list))))
3401 (if ido-use-virtual-buffers
3402 (ido-add-virtual-buffers-to-list))
3403 (run-hooks 'ido-make-buffer-list-hook)
3404 ido-temp-list))
3406 (defun ido-add-virtual-buffers-to-list ()
3407 "Add recently visited files, and bookmark files, to the buffer list.
3408 This is to make them appear as if they were \"virtual buffers\"."
3409 ;; If no buffers matched, and virtual buffers are being used, then
3410 ;; consult the list of past visited files, to see if we can find
3411 ;; the file which the user might thought was still open.
3412 (unless recentf-mode (recentf-mode 1))
3413 (setq ido-virtual-buffers nil)
3414 (let (name)
3415 (dolist (head recentf-list)
3416 (and (setq name (file-name-nondirectory head))
3417 (null (get-file-buffer head))
3418 (not (assoc name ido-virtual-buffers))
3419 (not (member name ido-temp-list))
3420 (not (ido-ignore-item-p name ido-ignore-buffers))
3421 ;;(file-exists-p head)
3422 (push (cons name head) ido-virtual-buffers))))
3423 (when ido-virtual-buffers
3424 (if ido-use-faces
3425 (dolist (comp ido-virtual-buffers)
3426 (put-text-property 0 (length (car comp))
3427 'face 'ido-virtual
3428 (car comp))))
3429 (setq ido-temp-list
3430 (nconc ido-temp-list
3431 (nreverse (mapcar #'car ido-virtual-buffers))))))
3433 (defun ido-make-choice-list (default)
3434 ;; Return the current list of choices.
3435 ;; If DEFAULT is non-nil, and corresponds to an element of choices,
3436 ;; it is put to the start of the list.
3437 (let ((ido-temp-list ido-choice-list))
3438 (if default
3439 (progn
3440 (setq ido-temp-list
3441 (delete default ido-temp-list))
3442 (setq ido-temp-list
3443 (cons default ido-temp-list))))
3444 ; (run-hooks 'ido-make-choice-list-hook)
3445 ido-temp-list))
3447 (defun ido-to-end (items)
3448 ;; Move the elements from ITEMS to the end of `ido-temp-list'
3449 (mapc
3450 (lambda (elem)
3451 (setq ido-temp-list (delq elem ido-temp-list)))
3452 items)
3453 (if ido-temp-list
3454 (nconc ido-temp-list items)
3455 (setq ido-temp-list items)))
3457 (declare-function tramp-tramp-file-p "tramp" (name))
3459 (defun ido-file-name-all-completions-1 (dir)
3460 (cond
3461 ((ido-nonreadable-directory-p dir) '())
3462 ;; do not check (ido-directory-too-big-p dir) here.
3463 ;; Caller must have done that if necessary.
3465 ((and ido-enable-tramp-completion
3466 (or (fboundp 'tramp-completion-mode-p)
3467 (require 'tramp nil t))
3468 (string-match "\\`/[^/]+[:@]\\'" dir))
3469 ;; Strip method:user@host: part of tramp completions.
3470 ;; Tramp completions do not include leading slash.
3471 (let* ((len (1- (length dir)))
3472 (non-essential t)
3473 (compl
3474 (or (file-name-all-completions "" dir)
3475 ;; work around bug in ange-ftp.
3476 ;; /ftp:user@host: => nil
3477 ;; /ftp:user@host:./ => ok
3478 (and
3479 (not (string= "/ftp:" dir))
3480 (tramp-tramp-file-p dir)
3481 (fboundp 'tramp-ftp-file-name-p)
3482 (funcall 'tramp-ftp-file-name-p dir)
3483 (string-match ":\\'" dir)
3484 (file-name-all-completions "" (concat dir "./"))))))
3485 (if (and compl
3486 (> (length (car compl)) len)
3487 (string= (substring (car compl) 0 len) (substring dir 1)))
3488 (mapcar (lambda (c) (substring c len)) compl)
3489 compl)))
3491 (file-name-all-completions "" dir))))
3493 (defun ido-file-name-all-completions (dir)
3494 ;; Return name of all files in DIR
3495 ;; Uses and updates ido-dir-file-cache
3496 (cond
3497 ((ido-is-unc-root dir)
3498 (mapcar
3499 (lambda (host)
3500 (if (string-match "/\\'" host) host (concat host "/")))
3501 (ido-unc-hosts t)))
3502 ((and (numberp ido-max-dir-file-cache) (> ido-max-dir-file-cache 0)
3503 (stringp dir) (> (length dir) 0)
3504 (ido-may-cache-directory dir))
3505 (let* ((cached (assoc dir ido-dir-file-cache))
3506 (ctime (nth 1 cached))
3507 (ftp (ido-is-ftp-directory dir))
3508 (unc (ido-is-unc-host dir))
3509 (attr (if (or ftp unc) nil (file-attributes dir)))
3510 (mtime (nth 5 attr))
3511 valid)
3512 (when cached ; should we use the cached entry ?
3513 (cond
3514 (ftp
3515 (setq valid (and (eq (car ctime) 'ftp)
3516 (ido-cache-ftp-valid (cdr ctime)))))
3517 (unc
3518 (setq valid (and (eq (car ctime) 'unc)
3519 (ido-cache-unc-valid (cdr ctime)))))
3521 (if attr
3522 (setq valid (and (= (car ctime) (car mtime))
3523 (= (car (cdr ctime)) (car (cdr mtime))))))))
3524 (unless valid
3525 (setq ido-dir-file-cache (delq cached ido-dir-file-cache)
3526 cached nil)))
3527 (unless cached
3528 (cond
3529 (unc
3530 (setq mtime (cons 'unc (ido-time-stamp))))
3531 ((and ftp (file-readable-p dir))
3532 (setq mtime (cons 'ftp (ido-time-stamp)))))
3533 (if mtime
3534 (setq cached (cons dir (cons mtime (ido-file-name-all-completions-1 dir)))
3535 ido-dir-file-cache (cons cached ido-dir-file-cache)))
3536 (if (> (length ido-dir-file-cache) ido-max-dir-file-cache)
3537 (setcdr (nthcdr (1- ido-max-dir-file-cache) ido-dir-file-cache) nil)))
3538 (and cached
3539 (cdr (cdr cached)))))
3541 (ido-file-name-all-completions-1 dir))))
3543 (defun ido-remove-cached-dir (dir)
3544 ;; Remove dir from ido-dir-file-cache
3545 (if (and ido-dir-file-cache
3546 (stringp dir) (> (length dir) 0))
3547 (let ((cached (assoc dir ido-dir-file-cache)))
3548 (if cached
3549 (setq ido-dir-file-cache (delq cached ido-dir-file-cache))))))
3552 (defun ido-make-file-list-1 (dir &optional merged)
3553 ;; Return list of non-ignored files in DIR
3554 ;; If MERGED is non-nil, each file is cons'ed with DIR
3555 (and (or (ido-is-tramp-root dir) (ido-is-unc-root dir)
3556 (file-directory-p dir))
3557 (delq nil
3558 (mapcar
3559 (lambda (name)
3560 (if (not (ido-ignore-item-p name ido-ignore-files t))
3561 (if merged (cons name dir) name)))
3562 (ido-file-name-all-completions dir)))))
3564 (defun ido-make-file-list (default)
3565 ;; Return the current list of files.
3566 ;; Currently visible files are put at the end of the list.
3567 ;; The hook `ido-make-file-list-hook' is run after the list has been
3568 ;; created to allow the user to further modify the order of the file names
3569 ;; in this list.
3570 (let ((ido-temp-list (ido-make-file-list-1 ido-current-directory)))
3571 (setq ido-temp-list (sort ido-temp-list
3572 (if ido-file-extensions-order
3573 #'ido-file-extension-lessp
3574 #'ido-file-lessp)))
3575 (unless (ido-is-tramp-root ido-current-directory)
3576 (let ((default-directory ido-current-directory))
3577 (ido-to-end ;; move ftp hosts and visited files to end
3578 (delq nil (mapcar
3579 (lambda (x) (if (or (and (string-match ".:\\'" x)
3580 (not (ido-local-file-exists-p x)))
3581 (and (not (ido-final-slash x))
3582 (let (file-name-handler-alist)
3583 (get-file-buffer x)))) x))
3584 ido-temp-list)))))
3585 (ido-to-end ;; move . files to end
3586 (delq nil (mapcar
3587 (lambda (x) (if (string-equal (substring x 0 1) ".") x))
3588 ido-temp-list)))
3589 (if (and default (member default ido-temp-list))
3590 (if (or ido-rotate-temp ido-rotate-file-list-default)
3591 (unless (equal default (car ido-temp-list))
3592 (let ((l ido-temp-list) k)
3593 (while (and l (cdr l) (not (equal default (car (cdr l)))))
3594 (setq l (cdr l)))
3595 (setq k (cdr l))
3596 (setcdr l nil)
3597 (nconc k ido-temp-list)
3598 (setq ido-temp-list k)))
3599 (setq ido-temp-list
3600 (delete default ido-temp-list))
3601 (setq ido-temp-list
3602 (cons default ido-temp-list))))
3603 (when ido-show-dot-for-dired
3604 (setq ido-temp-list (delete "." ido-temp-list))
3605 (setq ido-temp-list (cons "." ido-temp-list)))
3606 (run-hooks 'ido-make-file-list-hook)
3607 ido-temp-list))
3609 (defun ido-make-dir-list-1 (dir &optional merged)
3610 ;; Return list of non-ignored subdirs in DIR
3611 ;; If MERGED is non-nil, each subdir is cons'ed with DIR
3612 (and (or (ido-is-tramp-root dir) (file-directory-p dir))
3613 (delq nil
3614 (mapcar
3615 (lambda (name)
3616 (and (ido-final-slash name) (not (ido-ignore-item-p name ido-ignore-directories))
3617 (if merged (cons name dir) name)))
3618 (ido-file-name-all-completions dir)))))
3620 (defun ido-make-dir-list (default)
3621 ;; Return the current list of directories.
3622 ;; The hook `ido-make-dir-list-hook' is run after the list has been
3623 ;; created to allow the user to further modify the order of the
3624 ;; directory names in this list.
3625 (let ((ido-temp-list (ido-make-dir-list-1 ido-current-directory)))
3626 (setq ido-temp-list (sort ido-temp-list #'ido-file-lessp))
3627 (ido-to-end ;; move . files to end
3628 (delq nil (mapcar
3629 (lambda (x) (if (string-equal (substring x 0 1) ".") x))
3630 ido-temp-list)))
3631 (if (and default (member default ido-temp-list))
3632 (if (or ido-rotate-temp ido-rotate-file-list-default)
3633 (unless (equal default (car ido-temp-list))
3634 (let ((l ido-temp-list) k)
3635 (while (and l (cdr l) (not (equal default (car (cdr l)))))
3636 (setq l (cdr l)))
3637 (setq k (cdr l))
3638 (setcdr l nil)
3639 (nconc k ido-temp-list)
3640 (setq ido-temp-list k)))
3641 (setq ido-temp-list
3642 (delete default ido-temp-list))
3643 (setq ido-temp-list
3644 (cons default ido-temp-list))))
3645 (setq ido-temp-list (delete "." ido-temp-list))
3646 (unless ido-input-stack
3647 (setq ido-temp-list (cons "." ido-temp-list)))
3648 (run-hooks 'ido-make-dir-list-hook)
3649 ido-temp-list))
3651 ;; List of the files visible in the current frame.
3652 (defvar ido-bufs-in-frame)
3654 (defun ido-get-buffers-in-frames (&optional current)
3655 ;; Return the list of buffers that are visible in the current frame.
3656 ;; If optional argument `current' is given, restrict searching to the
3657 ;; current frame, rather than all frames, regardless of value of
3658 ;; `ido-all-frames'.
3659 (let ((ido-bufs-in-frame nil))
3660 (walk-windows 'ido-get-bufname nil
3661 (if current
3663 ido-all-frames))
3664 ido-bufs-in-frame))
3666 (defun ido-get-bufname (win)
3667 ;; Used by `ido-get-buffers-in-frames' to walk through all windows
3668 (let ((buf (buffer-name (window-buffer win))))
3669 (unless (or (member buf ido-bufs-in-frame)
3670 (member buf ido-ignore-item-temp-list))
3671 ;; Only add buf if it is not already in list.
3672 ;; This prevents same buf in two different windows being
3673 ;; put into the list twice.
3674 (setq ido-bufs-in-frame
3675 (cons buf ido-bufs-in-frame)))))
3677 ;;; FIND MATCHING ITEMS
3679 (defun ido-set-matches-1 (items &optional do-full)
3680 ;; Return list of matches in items
3681 (let* ((case-fold-search ido-case-fold)
3682 (slash (and (not ido-enable-prefix) (ido-final-slash ido-text)))
3683 (text (if slash (substring ido-text 0 -1) ido-text))
3684 (rex0 (if ido-enable-regexp text (regexp-quote text)))
3685 (rexq (concat rex0 (if slash ".*/" "")))
3686 (re (if ido-enable-prefix (concat "\\`" rexq) rexq))
3687 (full-re (and do-full (not ido-enable-regexp) (not (string-match "\$\\'" rex0))
3688 (concat "\\`" rex0 (if slash "/" "") "\\'")))
3689 (suffix-re (and do-full slash
3690 (not ido-enable-regexp) (not (string-match "\$\\'" rex0))
3691 (concat rex0 "/\\'")))
3692 (prefix-re (and full-re (not ido-enable-prefix)
3693 (concat "\\`" rexq)))
3694 (non-prefix-dot (or (not ido-enable-dot-prefix)
3695 (not ido-process-ignore-lists)
3696 ido-enable-prefix
3697 (= (length ido-text) 0)))
3698 full-matches suffix-matches prefix-matches matches)
3699 (setq ido-incomplete-regexp nil)
3700 (condition-case error
3701 (mapc
3702 (lambda (item)
3703 (let ((name (ido-name item)))
3704 (if (and (or non-prefix-dot
3705 (if (= (aref ido-text 0) ?.)
3706 (= (aref name 0) ?.)
3707 (/= (aref name 0) ?.)))
3708 (string-match re name))
3709 (cond
3710 ((and (eq ido-cur-item 'buffer)
3711 (or (not (stringp ido-default-item))
3712 (not (string= name ido-default-item)))
3713 (string= name (buffer-name ido-entry-buffer)))
3714 (setq matches (cons item matches)))
3715 ((and full-re (string-match full-re name))
3716 (setq full-matches (cons item full-matches)))
3717 ((and suffix-re (string-match suffix-re name))
3718 (setq suffix-matches (cons item suffix-matches)))
3719 ((and prefix-re (string-match prefix-re name))
3720 (setq prefix-matches (cons item prefix-matches)))
3721 (t (setq matches (cons item matches))))))
3723 items)
3724 (invalid-regexp
3725 (setq ido-incomplete-regexp t
3726 ;; Consider the invalid regexp message internally as a
3727 ;; special-case single match, and handle appropriately
3728 ;; elsewhere.
3729 matches (cdr error))))
3730 (when prefix-matches
3731 (ido-trace "prefix match" prefix-matches)
3732 ;; Bug#2042.
3733 (setq matches (nconc prefix-matches matches)))
3734 (when suffix-matches
3735 (ido-trace "suffix match" (list text suffix-re suffix-matches))
3736 (setq matches (nconc suffix-matches matches)))
3737 (when full-matches
3738 (ido-trace "full match" (list text full-re full-matches))
3739 (setq matches (nconc full-matches matches)))
3740 (when (and (null matches)
3741 ido-enable-flex-matching
3742 (> (length ido-text) 1)
3743 (not ido-enable-regexp))
3744 (setq re (mapconcat #'regexp-quote (split-string ido-text "") ".*"))
3745 (if ido-enable-prefix
3746 (setq re (concat "\\`" re)))
3747 (mapc
3748 (lambda (item)
3749 (let ((name (ido-name item)))
3750 (if (string-match re name)
3751 (setq matches (cons item matches)))))
3752 items))
3753 matches))
3756 (defun ido-set-matches ()
3757 ;; Set `ido-matches' to the list of items matching prompt
3758 (when ido-rescan
3759 (setq ido-matches (ido-set-matches-1 (reverse ido-cur-list) (not ido-rotate))
3760 ido-rotate nil)))
3762 (defun ido-ignore-item-p (name re-list &optional ignore-ext)
3763 ;; Return t if the buffer or file NAME should be ignored.
3764 (or (member name ido-ignore-item-temp-list)
3765 (and
3766 ido-process-ignore-lists re-list
3767 (save-match-data
3768 (let ((ext-list (and ignore-ext ido-ignore-extensions
3769 completion-ignored-extensions))
3770 (case-fold-search ido-case-fold)
3771 ignorep nextstr
3772 (flen (length name)) slen)
3773 (while ext-list
3774 (setq nextstr (car ext-list))
3775 (if (cond
3776 ((stringp nextstr)
3777 (and (>= flen (setq slen (length nextstr)))
3778 (string-equal (substring name (- flen slen)) nextstr)))
3779 ((functionp nextstr) (funcall nextstr name))
3780 (t nil))
3781 (setq ignorep t
3782 ext-list nil
3783 re-list nil)
3784 (setq ext-list (cdr ext-list))))
3785 (while re-list
3786 (setq nextstr (car re-list))
3787 (if (cond
3788 ((stringp nextstr) (string-match nextstr name))
3789 ((functionp nextstr) (funcall nextstr name))
3790 (t nil))
3791 (setq ignorep t
3792 re-list nil)
3793 (setq re-list (cdr re-list))))
3794 ;; return the result
3795 (if ignorep
3796 (setq ido-ignored-list (cons name ido-ignored-list)))
3797 ignorep)))))
3799 ;; Private variable used by `ido-word-matching-substring'.
3800 (defvar ido-change-word-sub)
3802 (defun ido-find-common-substring (items subs)
3803 ;; Return common string following SUBS in each element of ITEMS.
3804 (let (res
3805 alist
3806 ido-change-word-sub)
3807 (setq ido-change-word-sub
3808 (if ido-enable-regexp
3809 subs
3810 (regexp-quote subs)))
3811 (setq res (mapcar #'ido-word-matching-substring items))
3812 (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen)
3813 (setq alist (mapcar #'ido-makealist res)) ;; could use an OBARRAY
3815 ;; try-completion returns t if there is an exact match.
3816 (let* ((completion-ignore-case ido-case-fold)
3817 (comp (try-completion subs alist)))
3818 (if (eq comp t)
3819 subs
3820 comp))))
3822 (defun ido-word-matching-substring (word)
3823 ;; Return part of WORD before 1st match to `ido-change-word-sub'.
3824 ;; If `ido-change-word-sub' cannot be found in WORD, return nil.
3825 (let ((case-fold-search ido-case-fold))
3826 (let ((m (string-match ido-change-word-sub (ido-name word))))
3827 (if m
3828 (substring (ido-name word) m)
3829 ;; else no match
3830 nil))))
3832 (defun ido-makealist (res)
3833 ;; Return dotted pair (RES . 1).
3834 (cons res 1))
3836 (defun ido-choose-completion-string (choice &rest ignored)
3837 (when (ido-active)
3838 ;; Insert the completion into the buffer where completion was requested.
3839 (if (get-buffer ido-completion-buffer)
3840 (kill-buffer ido-completion-buffer))
3841 (cond
3842 ((ido-active t) ;; ido-use-merged-list
3843 (setq ido-current-directory ""
3844 ido-text choice
3845 ido-exit 'done))
3846 ((not (ido-final-slash choice))
3847 (setq ido-text choice
3848 ido-exit 'done))
3850 (ido-set-current-directory ido-current-directory choice)
3851 (setq ido-exit 'refresh)))
3852 (exit-minibuffer)
3855 (defun ido-completion-help ()
3856 "Show possible completions in a *File Completions* buffer."
3857 (interactive)
3858 (setq ido-rescan nil)
3859 (let ((temp-buf (get-buffer ido-completion-buffer))
3860 display-it full-list)
3861 (if (and (eq last-command this-command) temp-buf)
3862 ;; scroll buffer
3863 (let (win (buf (current-buffer)))
3864 (display-buffer temp-buf nil nil)
3865 (set-buffer temp-buf)
3866 (setq win (get-buffer-window temp-buf))
3867 (if (pos-visible-in-window-p (point-max) win)
3868 (if (or ido-completion-buffer-all-completions
3869 (boundp 'ido-completion-buffer-full))
3870 (set-window-start win (point-min))
3871 (with-no-warnings
3872 (set (make-local-variable 'ido-completion-buffer-full) t))
3873 (setq full-list t
3874 display-it t))
3875 (scroll-other-window))
3876 (set-buffer buf))
3877 (setq display-it t))
3878 (if display-it
3879 (with-output-to-temp-buffer ido-completion-buffer
3880 (let ((completion-list (sort
3881 (cond
3882 (ido-directory-too-big
3883 (message "Reading directory...")
3884 (setq ido-directory-too-big nil
3885 ido-ignored-list nil
3886 ido-cur-list (ido-all-completions)
3887 ido-rescan t)
3888 (ido-set-matches)
3889 (or ido-matches ido-cur-list))
3890 (ido-use-merged-list
3891 (ido-flatten-merged-list (or ido-matches ido-cur-list)))
3892 ((or full-list ido-completion-buffer-all-completions)
3893 (ido-all-completions))
3895 (copy-sequence (or ido-matches ido-cur-list))))
3896 #'ido-file-lessp)))
3897 (if (featurep 'xemacs)
3898 ;; XEmacs extents are put on by default, doesn't seem to be
3899 ;; any way of switching them off.
3900 ;; This obscure code avoids a byte compiler warning in Emacs.
3901 (let ((f 'display-completion-list))
3902 (funcall f completion-list
3903 :help-string "ido "
3904 :activate-callback
3905 '(lambda (x y z) (message "Doesn't work yet, sorry!"))))
3906 ;; else running Emacs
3907 ;;(add-hook 'completion-setup-hook 'completion-setup-function)
3908 (display-completion-list completion-list)))))))
3910 ;;; KILL CURRENT BUFFER
3911 (defun ido-kill-buffer-at-head ()
3912 "Kill the buffer at the head of `ido-matches'.
3913 If cursor is not at the end of the user input, delete to end of input."
3914 (interactive)
3915 (if (not (eobp))
3916 (delete-region (point) (line-end-position))
3917 (let ((enable-recursive-minibuffers t)
3918 (buf (ido-name (car ido-matches)))
3919 (nextbuf (cadr ido-matches)))
3920 (when (get-buffer buf)
3921 ;; If next match names a buffer use the buffer object; buffer
3922 ;; name may be changed by packages such as uniquify; mindful
3923 ;; of virtual buffers.
3924 (when (and nextbuf (get-buffer nextbuf))
3925 (setq nextbuf (get-buffer nextbuf)))
3926 (if (null (kill-buffer buf))
3927 ;; Buffer couldn't be killed.
3928 (setq ido-rescan t)
3929 ;; Else `kill-buffer' succeeds so re-make the buffer list
3930 ;; taking into account packages like uniquify may rename
3931 ;; buffers.
3932 (if (bufferp nextbuf)
3933 (setq nextbuf (buffer-name nextbuf)))
3934 (setq ido-default-item nextbuf
3935 ido-text-init ido-text
3936 ido-exit 'refresh)
3937 (exit-minibuffer))))))
3939 ;;; DELETE CURRENT FILE
3940 (defun ido-delete-file-at-head ()
3941 "Delete the file at the head of `ido-matches'.
3942 If cursor is not at the end of the user input, delete to end of input."
3943 (interactive)
3944 (if (not (eobp))
3945 (delete-region (point) (line-end-position))
3946 (let ((enable-recursive-minibuffers t)
3947 (file (ido-name (car ido-matches))))
3948 (if file
3949 (setq file (concat ido-current-directory file)))
3950 (when (and file
3951 (file-exists-p file)
3952 (not (file-directory-p file))
3953 (file-writable-p ido-current-directory)
3954 (yes-or-no-p (concat "Delete " file "? ")))
3955 (delete-file file)
3956 ;; Check if file still exists.
3957 (if (file-exists-p file)
3958 ;; file could not be deleted
3959 (setq ido-rescan t)
3960 ;; else file was killed so remove name from list.
3961 (setq ido-cur-list (delq (car ido-matches) ido-cur-list)))))))
3964 ;;; VISIT CHOSEN BUFFER
3965 (defun ido-visit-buffer (buffer method &optional record)
3966 "Switch to BUFFER according to METHOD.
3967 Record command in `command-history' if optional RECORD is non-nil."
3968 (if (bufferp buffer)
3969 (setq buffer (buffer-name buffer)))
3970 (let (win newframe)
3971 (cond
3972 ((eq method 'kill)
3973 (if record
3974 (ido-record-command 'kill-buffer buffer))
3975 (kill-buffer buffer))
3977 ((eq method 'other-window)
3978 (if record
3979 (ido-record-command 'switch-to-buffer buffer))
3980 (switch-to-buffer-other-window buffer))
3982 ((eq method 'display)
3983 (display-buffer buffer))
3985 ((eq method 'other-frame)
3986 (switch-to-buffer-other-frame buffer)
3987 (select-frame-set-input-focus (selected-frame)))
3989 ((and (memq method '(raise-frame maybe-frame))
3990 window-system
3991 (setq win (ido-buffer-window-other-frame buffer))
3992 (or (eq method 'raise-frame)
3993 (y-or-n-p "Jump to frame? ")))
3994 (setq newframe (window-frame win))
3995 (select-frame-set-input-focus newframe)
3996 (select-window win))
3998 ;; (eq method 'selected-window)
4000 ;; No buffer in other frames...
4001 (if record
4002 (ido-record-command 'switch-to-buffer buffer))
4003 (switch-to-buffer buffer)
4004 ))))
4007 (defun ido-buffer-window-other-frame (buffer)
4008 ;; Return window pointer if BUFFER is visible in another frame.
4009 ;; If BUFFER is visible in the current frame, return nil.
4010 (let ((blist (ido-get-buffers-in-frames 'current)))
4011 ;;If the buffer is visible in current frame, return nil
4012 (if (member buffer blist)
4014 ;; maybe in other frame or icon
4015 (get-buffer-window buffer 0) ; better than 'visible
4019 ;;; ----------- IDONIZED FUNCTIONS ------------
4021 ;;;###autoload
4022 (defun ido-switch-buffer ()
4023 "Switch to another buffer.
4024 The buffer is displayed according to `ido-default-buffer-method' -- the
4025 default is to show it in the same window, unless it is already visible
4026 in another frame.
4028 As you type in a string, all of the buffers matching the string are
4029 displayed if substring-matching is used \(default). Look at
4030 `ido-enable-prefix' and `ido-toggle-prefix'. When you have found the
4031 buffer you want, it can then be selected. As you type, most keys have
4032 their normal keybindings, except for the following: \\<ido-buffer-completion-map>
4034 RET Select the buffer at the front of the list of matches. If the
4035 list is empty, possibly prompt to create new buffer.
4037 \\[ido-select-text] Select the current prompt as the buffer.
4038 If no buffer is found, prompt for a new one.
4040 \\[ido-next-match] Put the first element at the end of the list.
4041 \\[ido-prev-match] Put the last element at the start of the list.
4042 \\[ido-complete] Complete a common suffix to the current string that
4043 matches all buffers. If there is only one match, select that buffer.
4044 If there is no common suffix, show a list of all matching buffers
4045 in a separate window.
4046 \\[ido-edit-input] Edit input string.
4047 \\[ido-fallback-command] Fallback to non-ido version of current command.
4048 \\[ido-toggle-regexp] Toggle regexp searching.
4049 \\[ido-toggle-prefix] Toggle between substring and prefix matching.
4050 \\[ido-toggle-case] Toggle case-sensitive searching of buffer names.
4051 \\[ido-completion-help] Show list of matching buffers in separate window.
4052 \\[ido-enter-find-file] Drop into `ido-find-file'.
4053 \\[ido-kill-buffer-at-head] Kill buffer at head of buffer list.
4054 \\[ido-toggle-ignore] Toggle ignoring buffers listed in `ido-ignore-buffers'."
4055 (interactive)
4056 (ido-buffer-internal ido-default-buffer-method))
4058 ;;;###autoload
4059 (defun ido-switch-buffer-other-window ()
4060 "Switch to another buffer and show it in another window.
4061 The buffer name is selected interactively by typing a substring.
4062 For details of keybindings, see `ido-switch-buffer'."
4063 (interactive)
4064 (ido-buffer-internal 'other-window 'switch-to-buffer-other-window))
4066 ;;;###autoload
4067 (defun ido-display-buffer ()
4068 "Display a buffer in another window but don't select it.
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 'display 'display-buffer nil nil nil 'ignore))
4074 ;;;###autoload
4075 (defun ido-kill-buffer ()
4076 "Kill a buffer.
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 'kill 'kill-buffer "Kill buffer: " (buffer-name (current-buffer)) nil 'ignore))
4082 ;;;###autoload
4083 (defun ido-insert-buffer ()
4084 "Insert contents of a buffer in current buffer after point.
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 'insert 'insert-buffer "Insert buffer: " nil nil 'ido-enter-insert-file))
4090 ;;;###autoload
4091 (defun ido-switch-buffer-other-frame ()
4092 "Switch to another buffer and show it in another frame.
4093 The buffer name is selected interactively by typing a substring.
4094 For details of keybindings, see `ido-switch-buffer'."
4095 (interactive)
4096 (if ido-mode
4097 (ido-buffer-internal 'other-frame)
4098 (call-interactively 'switch-to-buffer-other-frame)))
4100 ;;;###autoload
4101 (defun ido-find-file-in-dir (dir)
4102 "Switch to another file starting from DIR."
4103 (interactive "DDir: ")
4104 (setq dir (file-name-as-directory dir))
4105 (ido-file-internal ido-default-file-method nil dir nil nil nil 'ignore))
4107 ;;;###autoload
4108 (defun ido-find-file ()
4109 "Edit file with name obtained via minibuffer.
4110 The file is displayed according to `ido-default-file-method' -- the
4111 default is to show it in the same window, unless it is already
4112 visible in another frame.
4114 The file name is selected interactively by typing a substring. As you
4115 type in a string, all of the filenames matching the string are displayed
4116 if substring-matching is used \(default). Look at `ido-enable-prefix' and
4117 `ido-toggle-prefix'. When you have found the filename you want, it can
4118 then be selected. As you type, most keys have their normal keybindings,
4119 except for the following: \\<ido-file-completion-map>
4121 RET Select the file at the front of the list of matches. If the
4122 list is empty, possibly prompt to create new file.
4124 \\[ido-select-text] Select the current prompt as the buffer or file.
4125 If no buffer or file is found, prompt for a new one.
4127 \\[ido-next-match] Put the first element at the end of the list.
4128 \\[ido-prev-match] Put the last element at the start of the list.
4129 \\[ido-complete] Complete a common suffix to the current string that
4130 matches all files. If there is only one match, select that file.
4131 If there is no common suffix, show a list of all matching files
4132 in a separate window.
4133 \\[ido-edit-input] Edit input string (including directory).
4134 \\[ido-prev-work-directory] or \\[ido-next-work-directory] go to previous/next directory in work directory history.
4135 \\[ido-merge-work-directories] search for file in the work directory history.
4136 \\[ido-forget-work-directory] removes current directory from the work directory history.
4137 \\[ido-prev-work-file] or \\[ido-next-work-file] cycle through the work file history.
4138 \\[ido-wide-find-file-or-pop-dir] and \\[ido-wide-find-dir-or-delete-dir] prompts and uses find to locate files or directories.
4139 \\[ido-make-directory] prompts for a directory to create in current directory.
4140 \\[ido-fallback-command] Fallback to non-ido version of current command.
4141 \\[ido-toggle-regexp] Toggle regexp searching.
4142 \\[ido-toggle-prefix] Toggle between substring and prefix matching.
4143 \\[ido-toggle-case] Toggle case-sensitive searching of file names.
4144 \\[ido-toggle-vc] Toggle version control for this file.
4145 \\[ido-toggle-literal] Toggle literal reading of this file.
4146 \\[ido-completion-help] Show list of matching files in separate window.
4147 \\[ido-toggle-ignore] Toggle ignoring files listed in `ido-ignore-files'."
4149 (interactive)
4150 (ido-file-internal ido-default-file-method))
4152 ;;;###autoload
4153 (defun ido-find-file-other-window ()
4154 "Switch to another file and show it in another window.
4155 The file name is selected interactively by typing a substring.
4156 For details of keybindings, see `ido-find-file'."
4157 (interactive)
4158 (ido-file-internal 'other-window 'find-file-other-window))
4160 ;;;###autoload
4161 (defun ido-find-alternate-file ()
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 'alt-file 'find-alternate-file nil "Find alternate file: "))
4168 ;;;###autoload
4169 (defun ido-find-file-read-only ()
4170 "Edit file read-only with name obtained via minibuffer.
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 'read-only 'find-file-read-only nil "Find file read-only: "))
4176 ;;;###autoload
4177 (defun ido-find-file-read-only-other-window ()
4178 "Edit file read-only in other window 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-other-window nil "Find file read-only other window: "))
4184 ;;;###autoload
4185 (defun ido-find-file-read-only-other-frame ()
4186 "Edit file read-only in other frame 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-frame nil "Find file read-only other frame: "))
4192 ;;;###autoload
4193 (defun ido-display-file ()
4194 "Display a file in another window but don't select it.
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 'display nil nil nil nil nil 'ignore))
4200 ;;;###autoload
4201 (defun ido-find-file-other-frame ()
4202 "Switch to another file and show it in another frame.
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 'other-frame 'find-file-other-frame))
4208 ;;;###autoload
4209 (defun ido-write-file ()
4210 "Write current buffer to a file.
4211 The file name is selected interactively by typing a substring.
4212 For details of keybindings, see `ido-find-file'."
4213 (interactive)
4214 (let ((ido-process-ignore-lists t)
4215 (ido-work-directory-match-only nil)
4216 (ido-ignore-files (cons "[^/]\\'" ido-ignore-files))
4217 (ido-report-no-match nil)
4218 (ido-confirm-unique-completion t)
4219 (ido-auto-merge-work-directories-length -1))
4220 (ido-file-internal 'write 'write-file nil "Write file: " nil nil 'ignore)))
4222 ;;;###autoload
4223 (defun ido-insert-file ()
4224 "Insert contents of file in current buffer.
4225 The file name is selected interactively by typing a substring.
4226 For details of keybindings, see `ido-find-file'."
4227 (interactive)
4228 (ido-file-internal 'insert 'insert-file nil "Insert file: " nil nil 'ido-enter-insert-buffer))
4230 ;;;###autoload
4231 (defun ido-dired ()
4232 "Call `dired' the ido way.
4233 The directory is selected interactively by typing a substring.
4234 For details of keybindings, see `ido-find-file'."
4235 (interactive)
4236 (let ((ido-report-no-match nil)
4237 (ido-auto-merge-work-directories-length -1))
4238 (ido-file-internal 'dired 'dired nil "Dired: " 'dir)))
4240 (defun ido-list-directory ()
4241 "Call `list-directory' the ido way.
4242 The directory is selected interactively by typing a substring.
4243 For details of keybindings, see `ido-find-file'."
4244 (interactive)
4245 (let ((ido-report-no-match nil)
4246 (ido-auto-merge-work-directories-length -1))
4247 (ido-file-internal 'list-directory 'list-directory nil "List directory: " 'dir)))
4249 ;;; XEmacs hack for showing default buffer
4251 ;; The first time we enter the minibuffer, Emacs puts up the default
4252 ;; buffer to switch to, but XEmacs doesn't -- presumably there is a
4253 ;; subtle difference in the two versions of post-command-hook. The
4254 ;; default is shown for both whenever we delete all of our text
4255 ;; though, indicating its just a problem the first time we enter the
4256 ;; function. To solve this, we use another entry hook for emacs to
4257 ;; show the default the first time we enter the minibuffer.
4260 ;;; ICOMPLETE TYPE CODE
4262 (defun ido-initiate-auto-merge (buffer)
4263 (ido-trace "\n*merge timeout*" buffer)
4264 (setq ido-auto-merge-timer nil)
4265 (when (and (buffer-live-p buffer)
4266 (ido-active)
4267 (boundp 'ido-eoinput) ido-eoinput)
4268 (let ((contents (buffer-substring-no-properties (minibuffer-prompt-end) ido-eoinput)))
4269 (ido-trace "request merge")
4270 (setq ido-use-merged-list 'auto
4271 ido-text-init contents
4272 ido-rotate-temp t
4273 ido-exit 'refresh)
4274 (with-current-buffer buffer
4275 (ido-tidy))
4276 (throw 'ido contents))))
4278 (defun ido-exhibit ()
4279 "Post command hook for `ido'."
4280 ;; Find matching files and display a list in the minibuffer.
4281 ;; Copied from `icomplete-exhibit' with two changes:
4282 ;; 1. It prints a default file name when there is no text yet entered.
4283 ;; 2. It calls my completion routine rather than the standard completion.
4285 (when (ido-active)
4286 (let ((contents (buffer-substring-no-properties (minibuffer-prompt-end) (point-max)))
4287 (buffer-undo-list t)
4288 try-single-dir-match
4289 refresh)
4291 (when ido-trace-enable
4292 (ido-trace "\nexhibit" this-command)
4293 (ido-trace "dir" ido-current-directory)
4294 (ido-trace "contents" contents)
4295 (ido-trace "list" ido-cur-list)
4296 (ido-trace "matches" ido-matches)
4297 (ido-trace "rescan" ido-rescan))
4299 (save-excursion
4300 (goto-char (point-max))
4301 ;; Register the end of input, so we know where the extra stuff (match-status info) begins:
4302 (unless (boundp 'ido-eoinput)
4303 ;; In case it got wiped out by major mode business:
4304 (make-local-variable 'ido-eoinput))
4305 (setq ido-eoinput (point))
4307 ;; Handle explicit directory changes
4308 (cond
4309 ((memq ido-cur-item '(buffer list))
4312 ((= (length contents) 0)
4315 ((= (length contents) 1)
4316 (cond
4317 ((and (ido-is-tramp-root) (string-equal contents "/"))
4318 (ido-set-current-directory ido-current-directory contents)
4319 (setq refresh t))
4320 ((and (ido-unc-hosts) (string-equal contents "/")
4321 (let ((ido-enable-tramp-completion nil))
4322 (ido-is-root-directory)))
4323 (ido-set-current-directory "//")
4324 (setq refresh t))
4327 ((and (string-match (if ido-enable-tramp-completion ".[:@]\\'" ".:\\'") contents)
4328 (ido-is-root-directory) ;; Ange-ftp or tramp
4329 (not (ido-local-file-exists-p contents)))
4330 (ido-set-current-directory ido-current-directory contents)
4331 (when (ido-is-slow-ftp-host)
4332 (setq ido-exit 'fallback)
4333 (exit-minibuffer))
4334 (setq refresh t))
4336 ((ido-final-slash contents) ;; xxx/
4337 (ido-trace "final slash" contents)
4338 (cond
4339 ((string-equal contents "~/")
4340 (ido-set-current-home)
4341 (setq refresh t))
4342 ((string-equal contents "../")
4343 (ido-up-directory t)
4344 (setq refresh t))
4345 ((string-equal contents "./")
4346 (setq refresh t))
4347 ((string-match "\\`~[-_a-zA-Z0-9]+[$]?/\\'" contents)
4348 (ido-trace "new home" contents)
4349 (ido-set-current-home contents)
4350 (setq refresh t))
4351 ((string-match "[$][A-Za-z0-9_]+/\\'" contents)
4352 (let ((exp (condition-case ()
4353 (expand-file-name
4354 (substitute-in-file-name (substring contents 0 -1))
4355 ido-current-directory)
4356 (error nil))))
4357 (ido-trace contents exp)
4358 (when (and exp (file-directory-p exp))
4359 (ido-set-current-directory (file-name-directory exp))
4360 (setq ido-text-init (file-name-nondirectory exp))
4361 (setq refresh t))))
4362 ((and (memq system-type '(windows-nt ms-dos))
4363 (string-equal (substring contents 1) ":/"))
4364 (ido-set-current-directory (file-name-directory contents))
4365 (setq refresh t))
4366 ((string-equal (substring contents -2 -1) "/")
4367 (ido-set-current-directory
4368 (if (memq system-type '(windows-nt ms-dos))
4369 (expand-file-name "/" ido-current-directory)
4370 "/"))
4371 (setq refresh t))
4372 ((and (or ido-directory-nonreadable ido-directory-too-big)
4373 (file-directory-p (concat ido-current-directory (file-name-directory contents))))
4374 (ido-set-current-directory
4375 (concat ido-current-directory (file-name-directory contents)))
4376 (setq refresh t))
4378 (ido-trace "try single dir")
4379 (setq try-single-dir-match t))))
4381 ((and (string-equal (substring contents -2 -1) "/")
4382 (not (string-match "[$]" contents)))
4383 (ido-set-current-directory
4384 (cond
4385 ((= (length contents) 2)
4386 "/")
4387 (ido-matches
4388 (concat ido-current-directory (ido-name (car ido-matches))))
4390 (concat ido-current-directory (substring contents 0 -1)))))
4391 (setq ido-text-init (substring contents -1))
4392 (setq refresh t))
4394 ((and (not ido-use-merged-list)
4395 (not (ido-final-slash contents))
4396 (eq ido-try-merged-list t)
4397 (numberp ido-auto-merge-work-directories-length)
4398 (> ido-auto-merge-work-directories-length 0)
4399 (= (length contents) ido-auto-merge-work-directories-length)
4400 (not (and ido-auto-merge-inhibit-characters-regexp
4401 (string-match ido-auto-merge-inhibit-characters-regexp contents)))
4402 (not (input-pending-p)))
4403 (setq ido-use-merged-list 'auto
4404 ido-text-init contents
4405 ido-rotate-temp t)
4406 (setq refresh t))
4408 (t nil))
4410 (when refresh
4411 (ido-trace "refresh on /" ido-text-init)
4412 (setq ido-exit 'refresh)
4413 (exit-minibuffer))
4415 ;; Update the list of matches
4416 (setq ido-text contents)
4417 (ido-set-matches)
4418 (ido-trace "new " ido-matches)
4420 (when (and ido-enter-matching-directory
4421 ido-matches
4422 (or (eq ido-enter-matching-directory 'first)
4423 (null (cdr ido-matches)))
4424 (ido-final-slash (ido-name (car ido-matches)))
4425 (or try-single-dir-match
4426 (eq ido-enter-matching-directory t)))
4427 (ido-trace "single match" (car ido-matches))
4428 (ido-set-current-directory
4429 (concat ido-current-directory (ido-name (car ido-matches))))
4430 (setq ido-exit 'refresh)
4431 (exit-minibuffer))
4433 (when (and (not ido-matches)
4434 (not ido-directory-nonreadable)
4435 (not ido-directory-too-big)
4436 ;; ido-rescan ?
4437 ido-process-ignore-lists
4438 ido-ignored-list)
4439 (let ((ido-process-ignore-lists nil)
4440 (ido-rotate ido-rotate)
4441 (ido-cur-list ido-ignored-list))
4442 (ido-trace "try all" ido-ignored-list)
4443 (ido-set-matches))
4444 (when ido-matches
4445 (ido-trace "found " ido-matches)
4446 (setq ido-rescan t)
4447 (setq ido-process-ignore-lists-inhibit t)
4448 (setq ido-text-init ido-text)
4449 (setq ido-exit 'refresh)
4450 (exit-minibuffer)))
4452 (when (and
4453 ido-rescan
4454 (not ido-matches)
4455 (memq ido-cur-item '(file dir))
4456 (not (ido-is-root-directory))
4457 (> (length contents) 1)
4458 (not (string-match "[$]" contents))
4459 (not ido-directory-nonreadable)
4460 (not ido-directory-too-big))
4461 (ido-trace "merge?")
4462 (if ido-use-merged-list
4463 (ido-undo-merge-work-directory contents nil)
4464 (when (and (eq ido-try-merged-list t)
4465 (numberp ido-auto-merge-work-directories-length)
4466 (= ido-auto-merge-work-directories-length 0)
4467 (not (and ido-auto-merge-inhibit-characters-regexp
4468 (string-match ido-auto-merge-inhibit-characters-regexp contents)))
4469 (not (input-pending-p)))
4470 (ido-trace "\n*start timer*")
4471 (setq ido-auto-merge-timer
4472 (run-with-timer ido-auto-merge-delay-time nil 'ido-initiate-auto-merge (current-buffer))))))
4474 (setq ido-rescan t)
4476 (if (and ido-use-merged-list
4477 ido-matches
4478 (not (string-equal (car (cdr (car ido-matches))) ido-current-directory)))
4479 (progn
4480 (ido-set-current-directory (car (cdr (car ido-matches))))
4481 (setq ido-use-merged-list t
4482 ido-exit 'keep
4483 ido-text-init ido-text)
4484 (exit-minibuffer)))
4486 ;; Insert the match-status information:
4487 (ido-set-common-completion)
4488 (let ((inf (ido-completions contents)))
4489 (setq ido-show-confirm-message nil)
4490 (ido-trace "inf" inf)
4491 (insert inf))
4492 ))))
4494 (defun ido-completions (name)
4495 ;; Return the string that is displayed after the user's text.
4496 ;; Modified from `icomplete-completions'.
4498 (let* ((comps ido-matches)
4499 (ind (and (consp (car comps)) (> (length (cdr (car comps))) 1)
4500 ido-merged-indicator))
4501 first)
4503 (if (and ind ido-use-faces)
4504 (put-text-property 0 1 'face 'ido-indicator ind))
4506 (if (and ido-use-faces comps)
4507 (let* ((fn (ido-name (car comps)))
4508 (ln (length fn)))
4509 (setq first (format "%s" fn))
4510 (put-text-property 0 ln 'face
4511 (if (= (length comps) 1)
4512 (if ido-incomplete-regexp
4513 'ido-incomplete-regexp
4514 'ido-only-match)
4515 'ido-first-match)
4516 first)
4517 (if ind (setq first (concat first ind)))
4518 (setq comps (cons first (cdr comps)))))
4520 (cond ((null comps)
4521 (cond
4522 (ido-show-confirm-message
4523 (or (nth 10 ido-decorations) " [Confirm]"))
4524 (ido-directory-nonreadable
4525 (or (nth 8 ido-decorations) " [Not readable]"))
4526 (ido-directory-too-big
4527 (or (nth 9 ido-decorations) " [Too big]"))
4528 (ido-report-no-match
4529 (nth 6 ido-decorations)) ;; [No match]
4530 (t "")))
4531 (ido-incomplete-regexp
4532 (concat " " (car comps)))
4533 ((null (cdr comps)) ;one match
4534 (concat (if (if (not ido-enable-regexp)
4535 (= (length (ido-name (car comps))) (length name))
4536 ;; We can't rely on the length of the input
4537 ;; for regexps, so explicitly check for a
4538 ;; complete match
4539 (string-match name (ido-name (car comps)))
4540 (string-equal (match-string 0 (ido-name (car comps)))
4541 (ido-name (car comps))))
4543 ;; when there is one match, show the matching file name in full
4544 (concat (nth 4 ido-decorations) ;; [ ... ]
4545 (ido-name (car comps))
4546 (nth 5 ido-decorations)))
4547 (if (not ido-use-faces) (nth 7 ido-decorations)))) ;; [Matched]
4548 (t ;multiple matches
4549 (let* ((items (if (> ido-max-prospects 0) (1+ ido-max-prospects) 999))
4550 (alternatives
4551 (apply
4552 #'concat
4553 (cdr (apply
4554 #'nconc
4555 (mapcar
4556 (lambda (com)
4557 (setq com (ido-name com))
4558 (setq items (1- items))
4559 (cond
4560 ((< items 0) ())
4561 ((= items 0) (list (nth 3 ido-decorations))) ; " | ..."
4563 (list (or ido-separator (nth 2 ido-decorations)) ; " | "
4564 (let ((str (substring com 0)))
4565 (if (and ido-use-faces
4566 (not (string= str first))
4567 (ido-final-slash str))
4568 (put-text-property 0 (length str) 'face 'ido-subdir str))
4569 str)))))
4570 comps))))))
4572 (concat
4573 ;; put in common completion item -- what you get by pressing tab
4574 (if (and (stringp ido-common-match-string)
4575 (> (length ido-common-match-string) (length name)))
4576 (concat (nth 4 ido-decorations) ;; [ ... ]
4577 (substring ido-common-match-string (length name))
4578 (nth 5 ido-decorations)))
4579 ;; list all alternatives
4580 (nth 0 ido-decorations) ;; { ... }
4581 alternatives
4582 (nth 1 ido-decorations)))))))
4584 (defun ido-minibuffer-setup ()
4585 "Minibuffer setup hook for `ido'."
4586 ;; Copied from `icomplete-minibuffer-setup-hook'.
4587 (when (ido-active)
4588 (add-hook 'pre-command-hook 'ido-tidy nil t)
4589 (add-hook 'post-command-hook 'ido-exhibit nil t)
4590 (when (featurep 'xemacs)
4591 (ido-exhibit)
4592 (goto-char (point-min)))
4593 (run-hooks 'ido-minibuffer-setup-hook)
4594 (when ido-initial-position
4595 (goto-char (+ (minibuffer-prompt-end) ido-initial-position))
4596 (setq ido-initial-position nil))))
4598 (defun ido-tidy ()
4599 "Pre command hook for `ido'."
4600 ;; Remove completions display, if any, prior to new user input.
4601 ;; Copied from `icomplete-tidy'."
4603 (when ido-auto-merge-timer
4604 (ido-trace "\n*cancel timer*" this-command)
4605 (cancel-timer ido-auto-merge-timer)
4606 (setq ido-auto-merge-timer nil))
4608 (if (ido-active)
4609 (if (and (boundp 'ido-eoinput)
4610 ido-eoinput)
4612 (if (> ido-eoinput (point-max))
4613 ;; Oops, got rug pulled out from under us - reinit:
4614 (setq ido-eoinput (point-max))
4615 (let ((buffer-undo-list t))
4616 (delete-region ido-eoinput (point-max))))
4618 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
4619 (make-local-variable 'ido-eoinput)
4620 (setq ido-eoinput 1))))
4622 (defun ido-summary-buffers-to-end ()
4623 ;; Move the summaries to the end of the buffer list.
4624 ;; This is an example function which can be hooked on to
4625 ;; `ido-make-buffer-list-hook'. Any buffer matching the regexps
4626 ;; `Summary' or `output\*$'are put to the end of the list.
4627 (let ((summaries (delq nil (mapcar
4628 (lambda (x)
4629 (if (or
4630 (string-match "Summary" x)
4631 (string-match "output\\*\\'" x))
4633 ido-temp-list))))
4634 (ido-to-end summaries)))
4636 ;;; Helper functions for other programs
4638 (put 'dired-do-rename 'ido 'ignore)
4639 (put 'ibuffer-find-file 'ido 'find-file)
4640 (put 'dired-other-window 'ido 'dir)
4642 ;;;###autoload
4643 (defun ido-read-buffer (prompt &optional default require-match)
4644 "Ido replacement for the built-in `read-buffer'.
4645 Return the name of a buffer selected.
4646 PROMPT is the prompt to give to the user. DEFAULT if given is the default
4647 buffer to be selected, which will go to the front of the list.
4648 If REQUIRE-MATCH is non-nil, an existing buffer must be selected."
4649 (let* ((ido-current-directory nil)
4650 (ido-directory-nonreadable nil)
4651 (ido-directory-too-big nil)
4652 (ido-context-switch-command 'ignore)
4653 (buf (ido-read-internal 'buffer prompt 'ido-buffer-history default require-match)))
4654 (if (eq ido-exit 'fallback)
4655 (let ((read-buffer-function nil))
4656 (run-hook-with-args 'ido-before-fallback-functions 'read-buffer)
4657 (read-buffer prompt default require-match))
4658 buf)))
4660 ;;;###autoload
4661 (defun ido-read-file-name (prompt &optional dir default-filename mustmatch initial predicate)
4662 "Ido replacement for the built-in `read-file-name'.
4663 Read file name, prompting with PROMPT and completing in directory DIR.
4664 See `read-file-name' for additional parameters."
4665 (let (filename)
4666 (cond
4667 ((or (eq predicate 'file-directory-p)
4668 (eq (get this-command 'ido) 'dir)
4669 (memq this-command ido-read-file-name-as-directory-commands))
4670 (setq filename
4671 (ido-read-directory-name prompt dir default-filename mustmatch initial))
4672 (if (eq ido-exit 'fallback)
4673 (setq filename 'fallback)))
4674 ((and (not (eq (get this-command 'ido) 'ignore))
4675 (not (memq this-command ido-read-file-name-non-ido))
4676 (or (null predicate) (eq predicate 'file-exists-p)))
4677 (let* (ido-saved-vc-hb
4678 (ido-context-switch-command
4679 (if (eq (get this-command 'ido) 'find-file) nil 'ignore))
4680 (vc-handled-backends (and (boundp 'vc-handled-backends) vc-handled-backends))
4681 (minibuffer-completing-file-name t)
4682 (ido-current-directory (ido-expand-directory dir))
4683 (ido-directory-nonreadable (not (file-readable-p ido-current-directory)))
4684 (ido-directory-too-big (and (not ido-directory-nonreadable)
4685 (ido-directory-too-big-p ido-current-directory)))
4686 (ido-work-directory-index -1)
4687 (ido-show-dot-for-dired (and ido-show-dot-for-dired
4688 (not default-filename)))
4689 (ido-work-file-index -1)
4690 (ido-find-literal nil))
4691 (setq ido-exit nil)
4692 (setq filename
4693 (ido-read-internal 'file prompt 'ido-file-history default-filename mustmatch initial))
4694 (cond
4695 ((eq ido-exit 'fallback)
4696 (setq filename 'fallback))
4697 ((eq ido-exit 'dired)
4698 (setq filename ido-current-directory))
4699 (filename
4700 (setq filename
4701 (concat ido-current-directory filename))))))
4703 (setq filename 'fallback)))
4704 (if (eq filename 'fallback)
4705 (let ((read-file-name-function nil))
4706 (run-hook-with-args 'ido-before-fallback-functions 'read-file-name)
4707 (read-file-name prompt dir default-filename mustmatch initial predicate))
4708 filename)))
4710 ;;;###autoload
4711 (defun ido-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
4712 "Ido replacement for the built-in `read-directory-name'.
4713 Read directory name, prompting with PROMPT and completing in directory DIR.
4714 See `read-directory-name' for additional parameters."
4715 (let* (filename
4716 (minibuffer-completing-file-name t)
4717 (ido-context-switch-command 'ignore)
4718 ido-saved-vc-hb
4719 (ido-current-directory (ido-expand-directory dir))
4720 (ido-directory-nonreadable (not (file-readable-p ido-current-directory)))
4721 (ido-directory-too-big (and (not ido-directory-nonreadable)
4722 (ido-directory-too-big-p ido-current-directory)))
4723 (ido-work-directory-index -1)
4724 (ido-work-file-index -1))
4725 (setq filename
4726 (ido-read-internal 'dir prompt 'ido-file-history default-dirname mustmatch initial))
4727 (if filename
4728 (if (and (stringp filename) (string-equal filename "."))
4729 ido-current-directory
4730 (concat ido-current-directory filename)))))
4732 ;;;###autoload
4733 (defun ido-completing-read (prompt choices &optional predicate require-match initial-input hist def inherit-input-method)
4734 "Ido replacement for the built-in `completing-read'.
4735 Read a string in the minibuffer with ido-style completion.
4736 PROMPT is a string to prompt with; normally it ends in a colon and a space.
4737 CHOICES is a list of strings which are the possible completions.
4738 PREDICATE and INHERIT-INPUT-METHOD is currently ignored; it is included
4739 to be compatible with `completing-read'.
4740 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
4741 the input is (or completes to) an element of CHOICES or is null.
4742 If the input is null, `ido-completing-read' returns DEF, or an empty
4743 string if DEF is nil, regardless of the value of REQUIRE-MATCH.
4744 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
4745 with point positioned at the end.
4746 HIST, if non-nil, specifies a history list.
4747 DEF, if non-nil, is the default value."
4748 (let ((ido-current-directory nil)
4749 (ido-directory-nonreadable nil)
4750 (ido-directory-too-big nil)
4751 (ido-context-switch-command 'ignore)
4752 (ido-choice-list choices))
4753 ;; Initialize ido before invoking ido-read-internal
4754 (ido-common-initialization)
4755 (ido-read-internal 'list prompt hist def require-match initial-input)))
4757 (defun ido-unload-function ()
4758 "Unload the Ido library."
4759 (ido-mode -1)
4760 (setq minor-mode-map-alist (assq-delete-all 'ido-mode minor-mode-map-alist))
4761 ;; continue standard unloading
4762 nil)
4764 (provide 'ido)
4766 ;;; ido.el ends here