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