Commit d3d6ea50 authored by Lars Ingebrigtsen's avatar Lars Ingebrigtsen
Browse files

Make `ffap-read-file-or-url' work again (to read URLs)

* lisp/ffap.el (ffap--url-file-handler): New function (bug#44822).
(ffap-read-file-or-url): Use it to allow switching between URLs
and files.
parent 2dcb0f8f
Pipeline #11671 failed with stages
in 26 seconds
...@@ -1525,24 +1525,40 @@ which may actually result in an URL rather than a filename." ...@@ -1525,24 +1525,40 @@ which may actually result in an URL rather than a filename."
;; The solution here is to forcefully activate url-handler-mode, which ;; The solution here is to forcefully activate url-handler-mode, which
;; takes care of it for us. ;; takes care of it for us.
(defun ffap--url-file-handler (operation &rest args)
(let ((inhibit-file-name-handlers
(cons 'ffap--url-file-handler inhibit-file-name-handlers))
(inhibit-file-name-operation operation))
(cl-case operation
;; We mainly just want to disable these bits:
(substitute-in-file-name (car args))
(expand-file-name
(if (equal (car args) "http://<remove>")
""
(car args)))
(otherwise
(apply operation args)))))
(defun ffap-read-file-or-url (prompt guess) (defun ffap-read-file-or-url (prompt guess)
"Read file or URL from minibuffer, with PROMPT and initial GUESS." "Read file or URL from minibuffer, with PROMPT and initial GUESS."
(or guess (setq guess default-directory)) (let ((elem (cons ffap-url-regexp #'ffap--url-file-handler)))
;; Tricky: guess may have or be a local directory, like "w3/w3.elc" (unwind-protect
;; or "w3/" or "../el/ffap.el" or "../../../" (progn
(if (ffap-url-p guess) (push elem file-name-handler-alist)
;; FIXME: We earlier tried to make use of `url-file-handler' so (if (ffap-url-p guess)
;; `read-file-name' could also be used for URLs, but it (read-file-name prompt "http://<remove>" nil nil guess)
;; introduced all kinds of subtle breakage such as: (unless guess
;; - (file-name-directory "http://a") returning "http://a/" (setq guess default-directory))
;; - Trying to contact remote hosts with no justification (unless (ffap-file-remote-p guess)
;; These should be fixed in url-handler-mode before we can try (setq guess (abbreviate-file-name (expand-file-name guess))))
;; using it here again. (read-file-name prompt
(read-string prompt guess nil nil t) (file-name-directory guess) nil nil
(unless (ffap-file-remote-p guess) (file-name-nondirectory guess))))
(setq guess (abbreviate-file-name (expand-file-name guess)))) ;; Remove the special handler manually. We used to just let-bind
(read-file-name prompt (file-name-directory guess) nil nil ;; file-name-handler-alist to preserve its value, but that caused
(file-name-nondirectory guess)))) ;; other modifications to be lost (e.g. when Tramp gets loaded
;; during the completing-read call).
(setq file-name-handler-alist (delq elem file-name-handler-alist)))))
;; The rest of this page is just to work with package complete.el. ;; The rest of this page is just to work with package complete.el.
;; This code assumes that you load ffap.el after complete.el. ;; This code assumes that you load ffap.el after complete.el.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment