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