]> code.delx.au - gnu-emacs/blobdiff - lisp/ido.el
(compilation-start): In the no-async-subprocesses branch, fontify
[gnu-emacs] / lisp / ido.el
index eda6827eb2cc9f6b9ca6949c809c37944147a318..6a66ce0388dd8156d94b16dc6bfed3a50750532c 100644 (file)
@@ -489,6 +489,13 @@ Value can be toggled within `ido' using `ido-toggle-prefix'."
   :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
@@ -553,6 +560,20 @@ the `ido-work-directory-list' list."
   :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."
@@ -671,16 +692,17 @@ Obsolete.  Set 3rd element of `ido-decorations' instead."
   :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)
 
@@ -917,6 +939,9 @@ it doesn't interfere with other minibuffer usage.")
 ;; `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)
 
@@ -954,8 +979,8 @@ it doesn't interfere with other minibuffer usage.")
 ;; 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)
@@ -1219,15 +1244,14 @@ Removes badly formatted data and ignored directories."
 (defvar ido-minor-mode-map-entry nil)
 
 ;;;###autoload
-(defun ido-mode (&optional arg nobind)
+(defun ido-mode (&optional arg)
   "Toggle ido speed-ups on or off.
 With ARG, turn ido speed-up on if arg is positive, off otherwise.
-If second argument NOBIND is non-nil, no keys are rebound; otherwise,
-turning on ido-mode will modify the default keybindings for the
-find-file and switch-to-buffer families of commands to the ido
-versions of these functions.
-However, if second arg equals 'files, bind only for files, or if it
-equals 'buffers, bind only for buffers.
+Turning on ido-mode will remap (via a minor-mode keymap) the default
+keybindings for the `find-file' and `switch-to-buffer' families of
+commands to the ido versions of these functions.
+However, if ARG arg equals 'files, remap only commands for files, or
+if it equals 'buffers, remap only commands for buffer switching.
 This function also adds a hook to the minibuffer."
   (interactive "P")
   (setq ido-mode
@@ -1326,6 +1350,7 @@ This function also adds a hook to the minibuffer."
       (define-key map "\d"        'ido-delete-backward-updir)
       (define-key map [(meta backspace)] 'ido-delete-backward-word-updir)
       (define-key map [(control backspace)] 'ido-up-directory)
+      (define-key map "\C-l" 'ido-reread-directory)
       (define-key map [(meta ?b)] 'ido-next-work-file)
       (define-key map [(meta ?d)] 'ido-wide-find-dir)
       (define-key map [(meta ?f)] 'ido-wide-find-file)
@@ -1340,7 +1365,6 @@ This function also adds a hook to the minibuffer."
     (when (eq ido-cur-item 'file)
       (define-key map "\C-k" 'ido-delete-file-at-head)
       (define-key map "\C-o" 'ido-copy-current-word)
-      (define-key map "\C-l" 'ido-reread-directory)
       (define-key map "\C-w" 'ido-copy-current-file-name)
       (define-key map [(meta ?l)] 'ido-toggle-literal)
       (define-key map "\C-v" 'ido-toggle-vc)
@@ -1353,10 +1377,11 @@ This function also adds a hook to the minibuffer."
       )
 
     (when (if (boundp 'viper-mode) viper-mode)
-      (define-key map [remap viper-backward-char] 'ido-delete-backward-updir)
-      (define-key map [remap viper-del-backward-char-in-insert] 'ido-delete-backward-updir)
-      (define-key map [remap viper-delete-backward-word] 'ido-delete-backward-word-updir)
-      (define-key map [remap viper-intercept-ESC-key] 'ignore))
+      (define-key map [remap viper-intercept-ESC-key] 'ignore)
+      (when (memq ido-cur-item '(file dir))
+       (define-key map [remap viper-backward-char] 'ido-delete-backward-updir)
+       (define-key map [remap viper-del-backward-char-in-insert] 'ido-delete-backward-updir)
+       (define-key map [remap viper-delete-backward-word] 'ido-delete-backward-word-updir)))
 
     (setq ido-mode-map map)
     (run-hooks 'ido-define-mode-map-hook)))
@@ -1372,6 +1397,22 @@ This function also adds a hook to the minibuffer."
    (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))
@@ -1385,6 +1426,7 @@ This function also adds a hook to the minibuffer."
     (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)
@@ -1408,7 +1450,7 @@ This function also adds a hook to the minibuffer."
                         (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))
@@ -1791,7 +1833,8 @@ PROMPT is the prompt to give to the user.  DEFAULT if given is the default
 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)
@@ -1830,24 +1873,54 @@ If INITIAL is non-nil, it specifies the initial input string."
     (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)))
 
@@ -1868,6 +1941,9 @@ If INITIAL is non-nil, it specifies the initial input string."
        ((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)
@@ -1987,7 +2063,11 @@ If INITIAL is non-nil, it specifies the initial input string."
                     (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)
@@ -2087,8 +2167,8 @@ If no merge has yet taken place, toggle automatic merging option."
   (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))))
@@ -2252,7 +2332,9 @@ If no buffer or file exactly matching the prompt exists, maybe create a new one.
   "Prompt for FILE to search for using find, starting from current directory."
   (interactive)
   (unless file
-    (setq file (read-string (concat "Wide find file: " ido-current-directory) ido-text)))
+    (let ((enable-recursive-minibuffers t))
+      (setq file
+           (read-string (concat "Wide find file: " ido-current-directory) ido-text))))
   (when (> (length file) 0)
     (setq ido-use-merged-list t ido-try-merged-list 'wide)
     (setq ido-exit 'refresh)
@@ -2264,7 +2346,9 @@ If no buffer or file exactly matching the prompt exists, maybe create a new one.
   "Prompt for DIR to search for using find, starting from current directory."
   (interactive)
   (unless dir
-    (setq dir (read-string (concat "Wide find directory: " ido-current-directory) ido-text)))
+    (let ((enable-recursive-minibuffers t))
+      (setq dir
+           (read-string (concat "Wide find directory: " ido-current-directory) ido-text))))
   (when (> (length dir) 0)
     (setq ido-use-merged-list t ido-try-merged-list 'wide)
     (setq ido-exit 'refresh)
@@ -2276,7 +2360,9 @@ If no buffer or file exactly matching the prompt exists, maybe create a new one.
   "Prompt for DIR to create in current directory."
   (interactive)
   (unless dir
-    (setq dir (read-string (concat "Make directory: " ido-current-directory) ido-text)))
+    (let ((enable-recursive-minibuffers t))
+      (setq dir
+           (read-string (concat "Make directory: " ido-current-directory) ido-text))))
   (when (> (length dir) 0)
     (setq dir (concat ido-current-directory dir))
     (unless (file-exists-p dir)
@@ -2471,7 +2557,8 @@ for first matching file."
 
 (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.
@@ -2638,30 +2725,33 @@ for first matching file."
     (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
@@ -3317,6 +3407,7 @@ For details of keybindings, do `\\[describe-function] ido-find-file'."
        (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: ")))
 
@@ -3463,6 +3554,11 @@ For details of keybindings, do `\\[describe-function] ido-find-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))))
@@ -3519,6 +3615,7 @@ For details of keybindings, do `\\[describe-function] ido-find-file'."
          (exit-minibuffer))
 
        (when (and (not ido-matches)
+                  (not ido-directory-nonreadable)
                   ;; ido-rescan ?
                   ido-process-ignore-lists
                   ido-ignored-list)
@@ -3541,7 +3638,8 @@ For details of keybindings, do `\\[describe-function] ido-find-file'."
               (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)
@@ -3603,9 +3701,12 @@ For details of keybindings, do `\\[describe-function] ido-find-file'."
          (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))
@@ -3716,13 +3817,14 @@ See `read-file-name' for additional parameters."
    (ido-read-directory-name prompt dir default-filename mustmatch initial))
   ((and (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
@@ -3735,11 +3837,12 @@ See `read-file-name' for additional parameters."
 (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
@@ -3747,4 +3850,5 @@ See `read-file-name' for additional parameters."
            ido-current-directory
          (concat ido-current-directory filename)))))
 
+;;; arch-tag: b63a3500-1735-41bd-8a01-05373f0864da
 ;;; ido.el ends here