(dired-insert-directory): Don't assume Unix-style dir names.
[bpt/emacs.git] / lisp / ffap.el
index 314d48e..8c2bf71 100644 (file)
@@ -1,7 +1,7 @@
 ;;; ffap.el --- find file (or url) at point
 
 ;; Copyright (C) 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007 Free Software Foundation, Inc.
+;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Michelangelo Grigni <mic@mathcs.emory.edu>
 ;; Maintainer: FSF
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -22,9 +22,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 \f
 ;;; Commentary:
@@ -426,7 +424,7 @@ Returned values:
   ;; (ffap-machine-p "mathcs" 5678 nil 'ping)
   ;; (ffap-machine-p "foo.bonk" nil nil 'ping)
   ;; (ffap-machine-p "foo.bonk.com" nil nil 'ping)
-  (if (or (string-match "[^-[:alnum:].]" host) ; Illegal chars (?)
+  (if (or (string-match "[^-[:alnum:].]" host) ; Invalid chars (?)
          (not (string-match "[^0-9]" host))) ; 1: a number? 2: quick reject
       nil
     (let* ((domain
@@ -482,7 +480,7 @@ Returned values:
   "In remote FULLNAME, replace path with NAME.  May return nil."
   ;; Use ange-ftp or efs if loaded, but do not load them otherwise.
   (let (found)
-    (mapcar
+    (mapc
      (function (lambda (sym) (and (fboundp sym) (setq found sym))))
      '(
        efs-replace-path-component
@@ -797,7 +795,10 @@ This uses ffap-file-exists-string, which may try adding suffixes from
     ("\\.bib\\'" . ffap-bib)           ; search ffap-bib-path
     ("\\`\\." . ffap-home)             ; .emacs, .bashrc, .profile
     ("\\`~/" . ffap-lcd)               ; |~/misc/ffap.el.Z|
-    ("^[Rr][Ff][Cc][- #]?\\([0-9]+\\)" ; no $
+    ;; This uses to have a blank, but ffap-string-at-point doesn't
+    ;; handle blanks.
+    ;; http://lists.gnu.org/archive/html/emacs-devel/2008-01/msg01058.html
+    ("^[Rr][Ff][Cc][-#]?\\([0-9]+\\)"  ; no $
      . ffap-rfc)                       ; "100% RFC2100 compliant"
     (dired-mode . ffap-dired)          ; maybe in a subdirectory
     )
@@ -919,7 +920,7 @@ If t, `ffap-tex-init' will initialize this when needed.")
   (ffap-locate-file name t ffap-bib-path))
 
 (defun ffap-dired (name)
-  (let ((pt (point)) dir try)
+  (let ((pt (point)) try)
     (save-excursion
       (and (progn
             (beginning-of-line)
@@ -953,12 +954,20 @@ If t, `ffap-tex-init' will initialize this when needed.")
                      "/pub/gnu/emacs/elisp-archive/"))
     (substring name 2))))
 
+(defcustom ffap-rfc-directories nil
+  "A list of directories to look for RFC files.
+If a given RFC isn't in these then `ffap-rfc-path' is offered."
+  :type '(repeat directory)
+  :version "23.1"
+  :group 'ffap)
+
 (defvar ffap-rfc-path
   (concat (ffap-host-to-filename "ftp.rfc-editor.org") "/in-notes/rfc%s.txt"))
 
 (defun ffap-rfc (name)
-  (format ffap-rfc-path
-         (substring name (match-beginning 1) (match-end 1))))
+  (let ((num (match-string 1 name)))
+    (or (ffap-locate-file (format "rfc%s.txt" num) t ffap-rfc-directories)
+        (format ffap-rfc-path num))))
 
 \f
 ;;; At-Point Functions:
@@ -969,7 +978,7 @@ If t, `ffap-tex-init' will initialize this when needed.")
     ;; Slightly controversial decisions:
     ;; * strip trailing "@" and ":"
     ;; * no commas (good for latex)
-    (file "--:$+<>@-Z_[:lower:]~*?" "<@" "@>;.,!:")
+    (file "--:\\\\$+<>@-Z_[:lower:]~*?" "<@" "@>;.,!:")
     ;; An url, or maybe a email/news message-id:
     (url "--:=&?$+@-Z_[:lower:]~#,%;*" "^[:alnum:]" ":;.,!?")
     ;; Find a string that does *not* contain a colon:
@@ -1045,6 +1054,9 @@ Assumes the buffer has not changed."
       ;; Older: (apply 'copy-region-as-kill ffap-string-at-point-region)
       (message "Copied to kill ring: %s"  str))))
 
+;; External.
+(declare-function w3-view-this-url "ext:w3" (&optional no-show))
+
 (defun ffap-url-at-point nil
   "Return url from around point if it exists, or nil."
   ;; Could use w3's url-get-url-at-point instead.  Both handle "URL:",
@@ -1259,43 +1271,52 @@ which may actually result in an url rather than a filename."
                    ))
          (setq dir (file-name-directory guess))))
     (let ((minibuffer-completing-file-name t)
-         (completion-ignore-case read-file-name-completion-ignore-case))
-      (setq guess
-           (completing-read
-            prompt
-            'ffap-read-file-or-url-internal
-            dir
-            nil
-            (if dir (cons guess (length dir)) guess)
-            (list 'file-name-history)
-            (and buffer-file-name
-                 (abbreviate-file-name buffer-file-name)))))
+         (completion-ignore-case read-file-name-completion-ignore-case)
+          (fnh-elem (cons ffap-url-regexp 'url-file-handler)))
+      ;; Explain to `rfn-eshadow' that we can use URLs here.
+      (push fnh-elem file-name-handler-alist)
+      (unwind-protect
+          (setq guess
+                (let ((default-directory (if dir (expand-file-name dir)
+                                           default-directory)))
+                  (completing-read
+                   prompt
+                   'ffap-read-file-or-url-internal
+                   nil
+                   nil
+                   (if dir (cons guess (length dir)) guess)
+                   (list 'file-name-history)
+                   (and buffer-file-name
+                        (abbreviate-file-name buffer-file-name)))))
+        ;; Remove the special handler manually.  We used to just let-bind
+        ;; file-name-handler-alist to preserve its value, but that caused
+        ;; other modifications to be lost (e.g. when Tramp gets loaded
+        ;; during the completing-read call).
+        (setq file-name-handler-alist (delq fnh-elem file-name-handler-alist))))
     ;; Do file substitution like (interactive "F"), suggested by MCOOK.
     (or (ffap-url-p guess) (setq guess (substitute-in-file-name guess)))
     ;; Should not do it on url's, where $ is a common (VMS?) character.
     ;; Note: upcoming url.el package ought to handle this automatically.
     guess))
 
-(defun ffap-read-url-internal (string dir action)
+(defun ffap-read-url-internal (string pred action)
   "Complete url's from history, treating given string as valid."
   (let ((hist (ffap-soft-value "url-global-history-hash-table")))
     (cond
      ((not action)
-      (or (try-completion string hist) string))
+      (or (try-completion string hist pred) string))
      ((eq action t)
-      (or (all-completions string hist) (list string)))
+      (or (all-completions string hist pred) (list string)))
      ;; action == lambda, documented where?  Tests whether string is a
      ;; valid "match".  Let us always say yes.
      (t t))))
 
-(defun ffap-read-file-or-url-internal (string dir action)
-  (unless dir
-    (setq dir default-directory))
-  (unless string
+(defun ffap-read-file-or-url-internal (string pred action)
+  (unless string                        ;Why would this ever happen?
     (setq string default-directory))
   (if (ffap-url-p string)
-      (ffap-read-url-internal string dir action)
-    (read-file-name-internal string dir action)))
+      (ffap-read-url-internal string pred action)
+    (read-file-name-internal string pred action)))
 
 ;; The rest of this page is just to work with package complete.el.
 ;; This code assumes that you load ffap.el after complete.el.
@@ -1377,7 +1398,11 @@ Uses the face `ffap' if it is defined, or else `highlight'."
        (ffap-read-file-or-url
         (if ffap-url-regexp "Find file or URL: " "Find file: ")
         (prog1
-            (setq guess (or guess (ffap-guesser))) ; using ffap-alist here
+             (let ((mark-active nil))
+               ;; Don't use the region here, since it can be something
+               ;; completely unwieldy.  If the user wants that, she could
+               ;; use M-w before and then C-y.  --Stef
+               (setq guess (or guess (ffap-guesser)))) ; using ffap-alist here
           (and guess (ffap-highlight))
           )))
     (ffap-highlight t)))
@@ -1500,7 +1525,7 @@ Function CONT is applied to the entry chosen by the user."
            (x-popup-menu
             t
             (list "" (cons title
-                           (mapcar (function (lambda (i) (cons (car i) i)))
+                           (mapcar (lambda (i) (cons (car i) i))
                                    alist))))))
      ;; minibuffer with completion buffer:
      (t
@@ -1514,8 +1539,7 @@ Function CONT is applied to the entry chosen by the user."
               nil)))
       (sit-for 0)                      ; redraw original screen
       ;; Convert string to its entry, or else the default:
-      (setq choice (or (assoc choice alist) (car alist))))
-     )
+      (setq choice (or (assoc choice alist) (car alist)))))
     (if choice
        (funcall cont choice)
       (message "No choice made!")      ; possible with menus
@@ -1546,7 +1570,7 @@ Applies `ffap-menu-text-plist' text properties at all matches."
                                 ffap-menu-text-plist)
            (message "Scanning...%2d%% <%s>"
                     (/ (* 100 (- (point) (point-min))) range) item)))
-      (or mod (set-buffer-modified-p nil))))
+      (or mod (restore-buffer-modified-p nil))))
   (message "Scanning...done")
   ;; Remove duplicates.
   (setq ffap-menu-alist                        ; sort by item
@@ -1687,20 +1711,8 @@ Only intended for interactive use."
 \f
 ;;; Bug Reporter:
 
-(defun ffap-bug nil
-  "Submit a bug report for the ffap package."
-  ;; Important: keep the version string here in synch with that at top
-  ;; of file!  Could use lisp-mnt from Emacs 19, but that would depend
-  ;; on being able to find the ffap.el source file.
-  (interactive)
-  (require 'reporter)
-  (let ((reporter-prompt-for-summary-p t))
-    (reporter-submit-bug-report
-     "Michelangelo Grigni <mic@mathcs.emory.edu>"
-     "ffap"
-     (mapcar 'intern (all-completions "ffap-" obarray 'boundp)))))
-
-(fset 'ffap-submit-bug 'ffap-bug)      ; another likely name
+(define-obsolete-function-alias 'ffap-bug 'report-emacs-bug "23.1")
+(define-obsolete-function-alias 'ffap-submit-bug 'report-emacs-bug "23.1")
 
 \f
 ;;; Hooks for Gnus, VM, Rmail:
@@ -1724,6 +1736,13 @@ Only intended for interactive use."
 (defvar gnus-summary-buffer)
 (defvar gnus-article-buffer)
 
+;; This code is called from gnus.
+(declare-function gnus-summary-select-article "gnus-sum"
+                  (&optional all-headers force pseudo article))
+
+(declare-function gnus-configure-windows "gnus-win"
+                  (setting &optional force))
+
 (defun ffap-gnus-wrapper (form)                ; used by both commands below
   (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
        (gnus-summary-select-article))  ; get article of current line
@@ -1898,5 +1917,5 @@ Of course if you do not like these bindings, just roll your own!")
 
 \f
 
-;;; arch-tag: 9dd3e88a-5dec-4607-bd57-60ae9ede8ebc
+;; arch-tag: 9dd3e88a-5dec-4607-bd57-60ae9ede8ebc
 ;;; ffap.el ends here