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