;;; ido.el --- interactively do things with buffers and files.
-;; Copyright (C) 1996-2003 Free Software Foundation, Inc.
+;; Copyright (C) 1996-2004 Free Software Foundation, Inc.
;; Author: Kim F. Storm <storm@cua.dk>
;; Based on: iswitchb by Stephen Eglen <stephen@cns.ed.ac.uk>
;; for ido-switch-buffer and found the inspiration for ido-find-file.
;; The ido package would never have existed without his work.
-;; Also thanks to Klaus Berndl, Rohit Namjoshi, Robert Fenk, Alex Schroeder,
-;; Bill Benedetto, and Stephen Eglen for bug fixes and improvements.
+;; Also thanks to Klaus Berndl, Rohit Namjoshi, Robert Fenk, Alex
+;; Schroeder, Bill Benedetto, Stephen Eglen, and many others for bug
+;; fixes and improvements.
;;; History
;; so I invented a common "ido-" namespace for the merged packages.
;;
;; This version is based on ido.el version 1.57 released on
-;; gnu.emacs.sources adapted for emacs 21.4 to use command remapping
+;; gnu.emacs.sources adapted for emacs 21.5 to use command remapping
;; and optionally hooking the read-buffer and read-file-name functions.
;;
;; Prefix matching was added by Klaus Berndl <klaus.berndl@sdm.de> based on
;; then all files matching "Summary" are moved to the end of the
;; list. (I find this handy for keeping the INBOX Summary and so on
;; out of the way.) It also moves files matching "output\*$" to the
-;; end of the list (these are created by AUC TeX when compiling.)
+;; end of the list (these are created by AUCTeX when compiling.)
;; Other functions could be made available which alter the list of
;; matching files (either deleting or rearranging elements.)
:type 'boolean
:group 'ido)
+(defcustom ido-confirm-unique-completion nil
+ "*Non-nil means that even a unique completion must be confirmed.
+This means that \\[ido-complete] must always be followed by \\[ido-exit-minibuffer]
+even when there is only one unique completion."
+ :type 'boolean
+ :group 'ido)
+
(defcustom ido-cannot-complete-command 'ido-completion-help
"*Command run when `ido-complete' can't complete any more.
The most useful values are `ido-completion-help', which pops up a
:group 'ido)
+(defcustom ido-use-filename-at-point nil
+ "*Non-nil means that ido shall look for a filename at point.
+If found, use that as the starting point for filename selection."
+ :type 'boolean
+ :group 'ido)
+
+
+(defcustom ido-use-url-at-point nil
+ "*Non-nil means that ido shall look for a URL at point.
+If found, call `find-file-at-point' to visit it."
+ :type 'boolean
+ :group 'ido)
+
+
(defcustom ido-enable-tramp-completion t
"*Non-nil means that ido shall perform tramp method and server name completion.
A tramp file name uses the following syntax: /method:user@host:filename."
:type '(choice string (const nil))
:group 'ido)
-(defcustom ido-decorations '( "{" "}" " | " " | ..." "[" "]" " [No match]" " [Matched]")
+(defcustom ido-decorations '( "{" "}" " | " " | ..." "[" "]" " [No match]" " [Matched]" " [Not readable]")
"*List of strings used by ido to display the alternatives in the minibuffer.
-There are 8 elements in this list, each is a pair of strings:
+There are 9 elements in this list:
1st and 2nd elements are used as brackets around the prospect list,
3rd element is the separator between prospects (ignored if ido-separator is set),
4th element is the string inserted at the end of a truncated list of prospects,
5th and 6th elements are used as brackets around the common match string which
can be completed using TAB,
7th element is the string displayed when there are a no matches, and
-8th element displayed if there is a single match (and faces are not used)."
+8th element is displayed if there is a single match (and faces are not used).
+9th element is displayed when the current directory is non-readable."
:type '(repeat string)
:group 'ido)
;; `ido-cur-list'. It is in no specific order.
(defvar ido-ignored-list)
+;; Remember if current directory is non-readable (so we cannot do completion).
+(defvar ido-directory-nonreadable)
+
;; Keep current item list if non-nil.
(defvar ido-keep-item-list)
;; Value is a list (ido-text dir cur-list ignored-list matches).
(defvar ido-pre-merge-state)
-;; Original value of vc-master-templates for use in ido-toggle-vc.
-(defvar ido-saved-vc-mt)
+;; Original value of vc-handled-backends for use in ido-toggle-vc.
+(defvar ido-saved-vc-hb)
;; Stores temporary state of literal find file.
(defvar ido-find-literal)
(fix-it (concat dir "/"))
(t nil)))
+(defun ido-no-final-slash (s)
+ ;; Remove optional final slash from string S
+ (let ((l (1- (length s))))
+ (if (and (> l 0) (eq (aref s l) ?/))
+ (substring s 0 l)
+ s)))
+
+(defun ido-nonreadable-directory-p (dir)
+ ;; Return t if dir is a directory, but not readable
+ ;; Do not check for non-readable directories via tramp, as this causes a premature
+ ;; connect on incomplete tramp paths (after entring just method:).
+ (let ((ido-enable-tramp-completion nil))
+ (and (ido-final-slash dir)
+ (file-directory-p dir)
+ (not (file-readable-p dir)))))
+
(defun ido-set-current-directory (dir &optional subdir no-merge)
;; Set ido's current directory to DIR or DIR/SUBDIR
(setq dir (ido-final-slash dir t))
(setq ido-current-directory dir)
(if (get-buffer ido-completion-buffer)
(kill-buffer ido-completion-buffer))
+ (setq ido-directory-nonreadable (ido-nonreadable-directory-p dir))
t))
(defun ido-set-current-home (&optional dir)
(floor (* (frame-width) ido-max-file-prompt-width))
ido-max-file-prompt-width))
(literal (and (boundp 'ido-find-literal) ido-find-literal "(literal) "))
- (vc-off (and ido-saved-vc-mt (not vc-master-templates) "[-VC] "))
+ (vc-off (and ido-saved-vc-hb (not vc-handled-backends) "[-VC] "))
(prefix nil)
(rule ido-rewrite-file-prompt-rules))
(let ((case-fold-search nil))
((memq ido-exit '(edit chdir))
(cond
((memq ido-cur-item '(file dir))
- (let* ((process-environment (cons "HOME=/" process-environment)) ;; cheat read-file-name
- (read-file-name-function nil)
+ (let* ((read-file-name-function nil)
(edit (eq ido-exit 'edit))
(d ido-current-directory)
(f ido-text-init)
(setq ido-text-init "")
(while new
(setq new (if edit
- (read-file-name (concat prompt "[EDIT] ") d (concat d f) nil f)
+ (read-file-name (concat prompt "[EDIT] ")
+ (expand-file-name d)
+ (concat d f) nil f)
f)
d (or (file-name-directory new) "/")
f (file-name-nondirectory new)
buffer to be selected, which will go to the front of the list.
If REQUIRE-MATCH is non-nil, an existing-buffer must be selected.
If INITIAL is non-nil, it specifies the initial input string."
- (let ((ido-current-directory nil))
+ (let ((ido-current-directory nil)
+ (ido-directory-nonreadable nil))
(ido-read-internal 'buffer prompt 'ido-buffer-history default require-match initial)))
(defun ido-record-work-directory (&optional dir)
(if (> (length ido-work-file-list) ido-max-work-file-list)
(setcdr (nthcdr (1- ido-max-work-file-list) ido-work-file-list) nil))))
+(defun ido-expand-directory (dir)
+ ;; Expand DIR or use DEFAULT-DIRECTORY if nil.
+ ;; Add final slash to result in case it was missing from DEFAULT-DIRECTORY.
+ (ido-final-slash (expand-file-name (or dir default-directory)) t))
+
(defun ido-file-internal (method &optional fallback default prompt item initial)
;; Internal function for ido-find-file and friends
- (let ((ido-current-directory (expand-file-name (or default default-directory)))
- filename)
-
- (if (or (not ido-mode) (ido-is-slow-ftp-host))
- (setq filename t
- ido-exit 'fallback))
+ (unless item
+ (setq item 'file))
+ (let* ((ido-current-directory (ido-expand-directory default))
+ (ido-directory-nonreadable (ido-nonreadable-directory-p ido-current-directory))
+ filename)
- (let (ido-saved-vc-mt
- (vc-master-templates (and (boundp 'vc-master-templates) vc-master-templates))
+ (cond
+ ((or (not ido-mode) (ido-is-slow-ftp-host))
+ (setq filename t
+ ido-exit 'fallback))
+
+ ((and (eq item 'file)
+ (or ido-use-url-at-point ido-use-filename-at-point))
+ (let (fn d)
+ (require 'ffap)
+ ;; Duplicate code from ffap-guesser as we want different behaviour for files and URLs.
+ (cond
+ ((and ido-use-url-at-point
+ ffap-url-regexp
+ (ffap-fixup-url (or (ffap-url-at-point)
+ (ffap-gopher-at-point))))
+ (setq ido-exit 'ffap
+ filename t))
+
+ ((and ido-use-filename-at-point
+ (setq fn (ffap-string-at-point))
+ (not (string-match "^http:/" fn))
+ (setq d (file-name-directory fn))
+ (file-directory-p d))
+ (setq ido-current-directory d)
+ (setq initial (file-name-nondirectory fn)))))))
+
+ (let (ido-saved-vc-hb
+ (vc-handled-backends (and (boundp 'vc-handled-backends) vc-handled-backends))
(ido-work-directory-index -1)
(ido-work-file-index -1)
(ido-find-literal nil))
(unless filename
- (setq ido-saved-vc-mt vc-master-templates)
- (setq filename (ido-read-internal (or item 'file)
+ (setq ido-saved-vc-hb vc-handled-backends)
+ (setq filename (ido-read-internal item
(or prompt "Find file: ")
'ido-file-history nil nil initial)))
((eq ido-exit 'dired)
(dired (concat ido-current-directory (or ido-text ""))))
+ ((eq ido-exit 'ffap)
+ (find-file-at-point))
+
((eq method 'alt-file)
(ido-record-work-file filename)
(setq default-directory ido-current-directory)
(string-equal ido-current-directory "/")
(string-match "..[@:]\\'" (car ido-matches)))))
;; only one choice, so select it.
- (exit-minibuffer))
+ (if (not ido-confirm-unique-completion)
+ (exit-minibuffer)
+ (setq ido-rescan (not ido-enable-prefix))
+ (delete-region (minibuffer-prompt-end) (point))
+ (insert (car ido-matches))))
(t ;; else there could be some completions
(setq res ido-common-match-string)
(interactive)
(if (and ido-mode (eq ido-cur-item 'file))
(progn
- (setq vc-master-templates
- (if vc-master-templates nil ido-saved-vc-mt))
+ (setq vc-handled-backends
+ (if vc-handled-backends nil ido-saved-vc-hb))
(setq ido-text-init ido-text)
(setq ido-exit 'keep)
(exit-minibuffer))))
(defun ido-sort-list (items)
;; Simple list of file or buffer names
- (sort items (lambda (a b) (string-lessp a b))))
+ (sort items (lambda (a b) (string-lessp (ido-no-final-slash a)
+ (ido-no-final-slash b)))))
(defun ido-sort-merged-list (items promote)
;; Input is list of ("file" . "dir") cons cells.
(setq ido-temp-list items)))
(defun ido-file-name-all-completions1 (dir)
- (if (and ido-enable-tramp-completion
- (string-match "\\`/\\([^/:]+:\\([^/:@]+@\\)?\\)\\'" dir))
-
- ;; Trick tramp's file-name-all-completions handler to DTRT, as it
- ;; has some pretty obscure requirements. This seems to work...
- ;; /ftp: => (f-n-a-c "/ftp:" "")
- ;; /ftp:kfs: => (f-n-a-c "" "/ftp:kfs:")
- ;; /ftp:kfs@ => (f-n-a-c "ftp:kfs@" "/")
- ;; /ftp:kfs@kfs: => (f-n-a-c "" "/ftp:kfs@kfs:")
- ;; Currently no attempt is made to handle multi: stuff.
-
- (let* ((prefix (match-string 1 dir))
- (user-flag (match-beginning 2))
- (len (and prefix (length prefix)))
- compl)
- (if user-flag
- (setq dir (substring dir 1)))
- (require 'tramp nil t)
- (ido-trace "tramp complete" dir)
- (setq compl (file-name-all-completions dir (if user-flag "/" "")))
- (if (> len 0)
- (mapcar (lambda (c) (substring c len)) compl)
- compl))
- (file-name-all-completions "" dir)))
+ (cond
+ ((ido-nonreadable-directory-p dir) '())
+ ((and ido-enable-tramp-completion
+ (string-match "\\`/\\([^/:]+:\\([^/:@]+@\\)?\\)\\'" dir))
+
+ ;; Trick tramp's file-name-all-completions handler to DTRT, as it
+ ;; has some pretty obscure requirements. This seems to work...
+ ;; /ftp: => (f-n-a-c "/ftp:" "")
+ ;; /ftp:kfs: => (f-n-a-c "" "/ftp:kfs:")
+ ;; /ftp:kfs@ => (f-n-a-c "ftp:kfs@" "/")
+ ;; /ftp:kfs@kfs: => (f-n-a-c "" "/ftp:kfs@kfs:")
+ ;; Currently no attempt is made to handle multi: stuff.
+
+ (let* ((prefix (match-string 1 dir))
+ (user-flag (match-beginning 2))
+ (len (and prefix (length prefix)))
+ compl)
+ (if user-flag
+ (setq dir (substring dir 1)))
+ (require 'tramp nil t)
+ (ido-trace "tramp complete" dir)
+ (setq compl (file-name-all-completions dir (if user-flag "/" "")))
+ (if (> len 0)
+ (mapcar (lambda (c) (substring c len)) compl)
+ compl)))
+ (t
+ (file-name-all-completions "" dir))))
(defun ido-file-name-all-completions (dir)
;; Return name of all files in DIR
(ido-work-directory-match-only nil)
(ido-ignore-files (cons "[^/]\\'" ido-ignore-files))
(ido-report-no-match nil)
+ (ido-confirm-unique-completion t)
(ido-auto-merge-work-directories-length -1))
(ido-file-internal 'write 'write-file nil "Write file: ")))
(expand-file-name "/" ido-current-directory)
"/"))
(setq refresh t))
+ ((and ido-directory-nonreadable
+ (file-directory-p (concat ido-current-directory (file-name-directory contents))))
+ (ido-set-current-directory
+ (concat ido-current-directory (file-name-directory contents)))
+ (setq refresh t))
(t
(ido-trace "try single dir")
(setq try-single-dir-match t))))
(exit-minibuffer))
(when (and (not ido-matches)
+ (not ido-directory-nonreadable)
;; ido-rescan ?
ido-process-ignore-lists
ido-ignored-list)
(memq ido-cur-item '(file dir))
(not (ido-is-root-directory))
(> (length contents) 1)
- (not (string-match "[$]" contents)))
+ (not (string-match "[$]" contents))
+ (not ido-directory-nonreadable))
(ido-trace "merge?")
(if ido-use-merged-list
(ido-undo-merge-work-directory contents nil)
(setq comps (cons first (cdr comps)))))
(cond ((null comps)
- (if ido-report-no-match
- (nth 6 ido-decorations) ;; [No Match]
- ""))
+ (cond
+ (ido-directory-nonreadable
+ (or (nth 8 ido-decorations) " [Not readable]"))
+ (ido-report-no-match
+ (nth 6 ido-decorations)) ;; [No match]
+ (t "")))
((null (cdr comps)) ;one match
(concat (if (> (length (ido-name (car comps))) (length name))
;;; Helper functions for other programs
+(put 'dired-do-rename 'ido 'ignore)
+
;;;###autoload
(defun ido-read-file-name (prompt &optional dir default-filename mustmatch initial predicate)
"Read file name, prompting with PROMPT and completing in directory DIR.
See `read-file-name' for additional parameters."
(cond
((or (eq predicate 'file-directory-p)
+ (eq (get this-command 'ido) 'dir)
(memq this-command ido-read-file-name-as-directory-commands))
(ido-read-directory-name prompt dir default-filename mustmatch initial))
- ((and (not (memq this-command ido-read-file-name-non-ido))
+ ((and (not (eq (get this-command 'ido) 'ignore))
+ (not (memq this-command ido-read-file-name-non-ido))
(or (null predicate) (eq predicate 'file-exists-p)))
- (let (filename
- ido-saved-vc-mt
- (vc-master-templates (and (boundp 'vc-master-templates) vc-master-templates))
- (ido-current-directory (expand-file-name (or dir default-directory)))
- (ido-work-directory-index -1)
- (ido-work-file-index -1)
- (ido-find-literal nil))
+ (let* (filename
+ ido-saved-vc-hb
+ (vc-handled-backends (and (boundp 'vc-handled-backends) vc-handled-backends))
+ (ido-current-directory (ido-expand-directory dir))
+ (ido-directory-nonreadable (not (file-readable-p ido-current-directory)))
+ (ido-work-directory-index -1)
+ (ido-work-file-index -1)
+ (ido-find-literal nil))
(setq filename
(ido-read-internal 'file prompt 'ido-file-history default-filename mustmatch initial))
(if filename
(defun ido-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
"Read directory name, prompting with PROMPT and completing in directory DIR.
See `read-file-name' for additional parameters."
- (let (filename
- ido-saved-vc-mt
- (ido-current-directory (expand-file-name (or dir default-directory)))
- (ido-work-directory-index -1)
- (ido-work-file-index -1))
+ (let* (filename
+ ido-saved-vc-hb
+ (ido-current-directory (ido-expand-directory dir))
+ (ido-directory-nonreadable (not (file-readable-p ido-current-directory)))
+ (ido-work-directory-index -1)
+ (ido-work-file-index -1))
(setq filename
(ido-read-internal 'dir prompt 'ido-file-history default-dirname mustmatch initial))
(if filename