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