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