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