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