url-tramp.el 2.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
;;; url-tramp.el --- file-name-handler magic invoking Tramp for some protocols

;; Copyright (C) 2014 Free Software Foundation, Inc.

;; Author: Michael Albinus <michael.albinus@gmx.de>
;; Keywords: comm, data, processes, hypermedia

;; This file is part of GNU Emacs.
;;
;; 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 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
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'url-parse)
(require 'tramp)
(require 'password-cache)

;;;###autoload
(defcustom url-tramp-protocols '("ftp" "ssh" "scp" "rsync" "telnet")
  "List of URL protocols the work is handled by Tramp.
They must also be covered by `url-handler-regexp'."
  :group 'url
Stefan Monnier's avatar
Stefan Monnier committed
36
  :version "25.1"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  :type '(list string))

(defun url-tramp-convert-url-to-tramp (url)
  "Convert URL to a Tramp file name."
  (let ((obj (url-generic-parse-url (and (stringp url) url))))
    (if (member (url-type obj) url-tramp-protocols)
	(progn
	  (if (url-password obj)
	      (password-cache-add
	       (tramp-make-tramp-file-name
		(url-type obj) (url-user obj) (url-host obj) "")
	       (url-password obj))
	    (tramp-make-tramp-file-name
	     (url-type obj) (url-user obj) (url-host obj) (url-filename obj))))
      url)))

(defun url-tramp-convert-tramp-to-url (file)
  "Convert FILE, a Tramp file name, to a URL."
  (let ((obj (ignore-errors (tramp-dissect-file-name file))))
    (if (member (tramp-file-name-method obj) url-tramp-protocols)
	(url-recreate-url
	 (url-parse-make-urlobj
	  (tramp-file-name-method obj)
	  (tramp-file-name-user obj)
	  nil ; password.
	  (tramp-file-name-host obj)
	  nil ; port.
	  (tramp-file-name-localname obj)
	  nil nil t)) ; target attributes fullness.
      file)))

;;;###autoload
(defun url-tramp-file-handler (operation &rest args)
  "Function called from the `file-name-handler-alist' routines.
OPERATION is what needs to be done.  ARGS are the arguments that
would have been passed to OPERATION."
  (let ((default-directory (url-tramp-convert-url-to-tramp default-directory))
	(args (mapcar 'url-tramp-convert-url-to-tramp args)))
    (url-tramp-convert-tramp-to-url (apply operation args))))

(provide 'url-tramp)

;;; url-tramp.el ends here