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