url-tramp.el 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; url-tramp.el --- file-name-handler magic invoking Tramp for some protocols
  2. ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
  3. ;; Author: Michael Albinus <michael.albinus@gmx.de>
  4. ;; Keywords: comm, data, processes, hypermedia
  5. ;; This file is part of GNU Emacs.
  6. ;;
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (require 'url-parse)
  20. (require 'tramp)
  21. (require 'password-cache)
  22. ;;;###autoload
  23. (defcustom url-tramp-protocols '("ftp" "ssh" "scp" "rsync" "telnet")
  24. "List of URL protocols the work is handled by Tramp.
  25. They must also be covered by `url-handler-regexp'."
  26. :group 'url
  27. :version "25.1"
  28. :type '(list string))
  29. (defun url-tramp-convert-url-to-tramp (url)
  30. "Convert URL to a Tramp file name."
  31. (let ((obj (url-generic-parse-url (and (stringp url) url))))
  32. (if (member (url-type obj) url-tramp-protocols)
  33. (progn
  34. (if (url-password obj)
  35. (password-cache-add
  36. (tramp-make-tramp-file-name
  37. (url-type obj) (url-user obj) (url-host obj) "")
  38. (url-password obj))
  39. (tramp-make-tramp-file-name
  40. (url-type obj) (url-user obj) (url-host obj) (url-filename obj))))
  41. url)))
  42. (defun url-tramp-convert-tramp-to-url (file)
  43. "Convert FILE, a Tramp file name, to a URL."
  44. (let ((obj (ignore-errors (tramp-dissect-file-name file))))
  45. (if (member (tramp-file-name-method obj) url-tramp-protocols)
  46. (url-recreate-url
  47. (url-parse-make-urlobj
  48. (tramp-file-name-method obj)
  49. (tramp-file-name-user obj)
  50. nil ; password.
  51. (tramp-file-name-host obj)
  52. nil ; port.
  53. (tramp-file-name-localname obj)
  54. nil nil t)) ; target attributes fullness.
  55. file)))
  56. ;;;###autoload
  57. (defun url-tramp-file-handler (operation &rest args)
  58. "Function called from the `file-name-handler-alist' routines.
  59. OPERATION is what needs to be done. ARGS are the arguments that
  60. would have been passed to OPERATION."
  61. (let ((default-directory (url-tramp-convert-url-to-tramp default-directory))
  62. (args (mapcar 'url-tramp-convert-url-to-tramp args)))
  63. (url-tramp-convert-tramp-to-url (apply operation args))))
  64. (provide 'url-tramp)
  65. ;;; url-tramp.el ends here