url-handlers.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ;;; url-handlers.el --- file-name-handler stuff for URL loading
  2. ;; Copyright (C) 1996-1999, 2004-2012 Free Software Foundation, Inc.
  3. ;; Keywords: comm, data, processes, hypermedia
  4. ;; This file is part of GNU Emacs.
  5. ;;
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; (require 'url)
  19. (require 'url-parse)
  20. ;; (require 'url-util)
  21. (eval-when-compile (require 'mm-decode))
  22. ;; (require 'mailcap)
  23. ;; The following functions in the byte compiler's warnings are known not
  24. ;; to cause any real problem for the following reasons:
  25. ;; - mm-save-part-to-file, mm-destroy-parts: always used
  26. ;; after mm-dissect-buffer and defined in the same file.
  27. ;; The following are autoloaded instead of `require'd to avoid eagerly
  28. ;; loading all of URL when turning on url-handler-mode in the .emacs.
  29. (autoload 'url-expand-file-name "url-expand" "Convert url to a fully specified url, and canonicalize it.")
  30. (autoload 'mm-dissect-buffer "mm-decode" "Dissect the current buffer and return a list of MIME handles.")
  31. (autoload 'url-scheme-get-property "url-methods" "Get property of a URL SCHEME.")
  32. ;; Implementation status
  33. ;; ---------------------
  34. ;; Function Status
  35. ;; ------------------------------------------------------------
  36. ;; add-name-to-file Needs DAV Bindings
  37. ;; copy-file Broken (assumes 1st item is URL)
  38. ;; delete-directory Finished (DAV)
  39. ;; delete-file Finished (DAV)
  40. ;; diff-latest-backup-file
  41. ;; directory-file-name unnecessary (what about VMS)?
  42. ;; directory-files Finished (DAV)
  43. ;; dired-call-process
  44. ;; dired-compress-file
  45. ;; dired-uncache
  46. ;; expand-file-name Finished
  47. ;; file-accessible-directory-p
  48. ;; file-attributes Finished, better with DAV
  49. ;; file-directory-p Needs DAV, finished
  50. ;; file-executable-p Finished
  51. ;; file-exists-p Finished
  52. ;; file-local-copy
  53. ;; file-modes
  54. ;; file-name-all-completions Finished (DAV)
  55. ;; file-name-as-directory
  56. ;; file-name-completion Finished (DAV)
  57. ;; file-name-directory
  58. ;; file-name-nondirectory
  59. ;; file-name-sans-versions why?
  60. ;; file-newer-than-file-p
  61. ;; file-ownership-preserved-p No way to know
  62. ;; file-readable-p Finished
  63. ;; file-regular-p !directory_p
  64. ;; file-remote-p Finished
  65. ;; file-symlink-p Needs DAV bindings
  66. ;; file-truename Needs DAV bindings
  67. ;; file-writable-p Check for LOCK?
  68. ;; find-backup-file-name why?
  69. ;; get-file-buffer why?
  70. ;; insert-directory Use DAV
  71. ;; insert-file-contents Finished
  72. ;; load
  73. ;; make-directory Finished (DAV)
  74. ;; make-symbolic-link Needs DAV bindings
  75. ;; rename-file Finished (DAV)
  76. ;; set-file-modes Use mod_dav specific executable flag?
  77. ;; set-visited-file-modtime Impossible?
  78. ;; shell-command Impossible?
  79. ;; unhandled-file-name-directory
  80. ;; vc-registered Finished (DAV)
  81. ;; verify-visited-file-modtime
  82. ;; write-region
  83. (defvar url-handler-regexp
  84. "\\`\\(https?\\|ftp\\|file\\|nfs\\)://"
  85. "*A regular expression for matching URLs handled by `file-name-handler-alist'.
  86. Some valid URL protocols just do not make sense to visit interactively
  87. \(about, data, info, irc, mailto, etc\). This regular expression
  88. avoids conflicts with local files that look like URLs \(Gnus is
  89. particularly bad at this\).")
  90. ;;;###autoload
  91. (define-minor-mode url-handler-mode
  92. "Toggle using `url' library for URL filenames (URL Handler mode).
  93. With a prefix argument ARG, enable URL Handler mode if ARG is
  94. positive, and disable it otherwise. If called from Lisp, enable
  95. the mode if ARG is omitted or nil."
  96. :global t :group 'url
  97. (if (not (boundp 'file-name-handler-alist))
  98. ;; Can't be turned ON anyway.
  99. (setq url-handler-mode nil)
  100. ;; Remove old entry, if any.
  101. (setq file-name-handler-alist
  102. (delq (rassq 'url-file-handler file-name-handler-alist)
  103. file-name-handler-alist))
  104. (if url-handler-mode
  105. (push (cons url-handler-regexp 'url-file-handler)
  106. file-name-handler-alist))))
  107. (defun url-run-real-handler (operation args)
  108. (let ((inhibit-file-name-handlers (cons 'url-file-handler
  109. (if (eq operation inhibit-file-name-operation)
  110. inhibit-file-name-handlers)))
  111. (inhibit-file-name-operation operation))
  112. (apply operation args)))
  113. ;;;###autoload
  114. (defun url-file-handler (operation &rest args)
  115. "Function called from the `file-name-handler-alist' routines.
  116. OPERATION is what needs to be done (`file-exists-p', etc). ARGS are
  117. the arguments that would have been passed to OPERATION."
  118. (let ((fn (or (get operation 'url-file-handlers)
  119. (intern-soft (format "url-%s" operation))))
  120. (val nil)
  121. (hooked nil))
  122. (if (and fn (fboundp fn))
  123. (setq hooked t
  124. val (save-match-data (apply fn args)))
  125. (setq hooked nil
  126. val (url-run-real-handler operation args)))
  127. (url-debug 'handlers "%s %S%S => %S" (if hooked "Hooked" "Real")
  128. operation args val)
  129. val))
  130. (defun url-file-handler-identity (&rest args)
  131. ;; Identity function
  132. (car args))
  133. ;; These are operations that we can fully support
  134. (put 'file-readable-p 'url-file-handlers 'url-file-exists-p)
  135. (put 'substitute-in-file-name 'url-file-handlers 'url-file-handler-identity)
  136. (put 'file-name-absolute-p 'url-file-handlers (lambda (&rest ignored) t))
  137. (put 'expand-file-name 'url-file-handlers 'url-handler-expand-file-name)
  138. (put 'directory-file-name 'url-file-handlers 'url-handler-directory-file-name)
  139. (put 'unhandled-file-name-directory 'url-file-handlers 'url-handler-unhandled-file-name-directory)
  140. (put 'file-remote-p 'url-file-handlers 'url-handler-file-remote-p)
  141. ;; (put 'file-name-as-directory 'url-file-handlers 'url-handler-file-name-as-directory)
  142. ;; These are operations that we do not support yet (DAV!!!)
  143. (put 'file-writable-p 'url-file-handlers 'ignore)
  144. (put 'file-symlink-p 'url-file-handlers 'ignore)
  145. ;; Just like for ange-ftp: let's not waste time trying to look for RCS/foo,v
  146. ;; files and such since we can't do anything clever with them anyway.
  147. (put 'vc-registered 'url-file-handlers 'ignore)
  148. (defun url-handler-expand-file-name (file &optional base)
  149. ;; When we see "/foo/bar" in a file whose working dir is "http://bla/bla",
  150. ;; there are two interpretations possible: either it's a local "/foo/bar"
  151. ;; or it's "http:/bla/foo/bar". When working with URLs, the second
  152. ;; interpretation is the right one, but when working with Emacs file
  153. ;; names, the first is preferred.
  154. (if (file-name-absolute-p file)
  155. (expand-file-name file "/")
  156. (url-expand-file-name file base)))
  157. ;; directory-file-name and file-name-as-directory are kind of hard to
  158. ;; implement really right for URLs since URLs can have repeated / chars.
  159. ;; We'd want the following behavior:
  160. ;; idempotence: (d-f-n (d-f-n X) == (d-f-n X)
  161. ;; idempotence: (f-n-a-d (f-n-a-d X) == (f-n-a-d X)
  162. ;; reversible: (d-f-n (f-n-a-d (d-f-n X))) == (d-f-n X)
  163. ;; reversible: (f-n-a-d (d-f-n (f-n-a-d X))) == (f-n-a-d X)
  164. (defun url-handler-directory-file-name (dir)
  165. ;; When there's more than a single /, just don't touch the slashes at all.
  166. (if (string-match "//\\'" dir) dir
  167. (url-run-real-handler 'directory-file-name (list dir))))
  168. (defun url-handler-unhandled-file-name-directory (filename)
  169. (let ((url (url-generic-parse-url filename)))
  170. (if (equal (url-type url) "file")
  171. ;; `file' URLs are actually local. The filename part may be ""
  172. ;; which really stands for "/".
  173. ;; FIXME: maybe we should check that the host part is "" or "localhost"
  174. ;; or some name that represents the local host?
  175. (or (file-name-directory (url-filename url)) "/")
  176. ;; All other URLs are not expected to be directly accessible from
  177. ;; a local process.
  178. nil)))
  179. (defun url-handler-file-remote-p (filename &optional identification connected)
  180. (let ((url (url-generic-parse-url filename)))
  181. (if (and (url-type url) (not (equal (url-type url) "file")))
  182. ;; Maybe we can find a suitable check for CONNECTED. For now,
  183. ;; we ignore it.
  184. (cond
  185. ((eq identification 'method) (url-type url))
  186. ((eq identification 'user) (url-user url))
  187. ((eq identification 'host) (url-host url))
  188. ((eq identification 'localname) (url-filename url))
  189. (t (url-recreate-url
  190. (url-parse-make-urlobj (url-type url) (url-user url) nil
  191. (url-host url) (url-port url)))))
  192. ;; If there is no URL type, or it is a "file://" URL, the
  193. ;; filename is expected to be non remote. A more subtle check
  194. ;; for "file://" URLs could be applied, as said in
  195. ;; `url-handler-unhandled-file-name-directory'.
  196. nil)))
  197. ;; The actual implementation
  198. ;;;###autoload
  199. (defun url-copy-file (url newname &optional ok-if-already-exists
  200. keep-time preserve-uid-gid)
  201. "Copy URL to NEWNAME. Both args must be strings.
  202. Signals a `file-already-exists' error if file NEWNAME already exists,
  203. unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
  204. A number as third arg means request confirmation if NEWNAME already exists.
  205. This is what happens in interactive use with M-x.
  206. Fourth arg KEEP-TIME non-nil means give the new file the same
  207. last-modified time as the old one. (This works on only some systems.)
  208. Fifth arg PRESERVE-UID-GID is ignored.
  209. A prefix arg makes KEEP-TIME non-nil."
  210. (if (and (file-exists-p newname)
  211. (not ok-if-already-exists))
  212. (error "Opening output file: File already exists, %s" newname))
  213. (let ((buffer (url-retrieve-synchronously url))
  214. (handle nil))
  215. (if (not buffer)
  216. (error "Opening input file: No such file or directory, %s" url))
  217. (with-current-buffer buffer
  218. (setq handle (mm-dissect-buffer t)))
  219. (mm-save-part-to-file handle newname)
  220. (kill-buffer buffer)
  221. (mm-destroy-parts handle)))
  222. ;;;###autoload
  223. (defun url-file-local-copy (url &rest ignored)
  224. "Copy URL into a temporary file on this machine.
  225. Returns the name of the local copy, or nil, if FILE is directly
  226. accessible."
  227. (let ((filename (make-temp-file "url")))
  228. (url-copy-file url filename 'ok-if-already-exists)
  229. filename))
  230. (defun url-insert (buffer &optional beg end)
  231. "Insert the body of a URL object.
  232. BUFFER should be a complete URL buffer as returned by `url-retrieve'.
  233. If the headers specify a coding-system, it is applied to the body before it is inserted.
  234. Returns a list of the form (SIZE CHARSET), where SIZE is the size in bytes
  235. of the inserted text and CHARSET is the charset that was specified in the header,
  236. or nil if none was found.
  237. BEG and END can be used to only insert a subpart of the body.
  238. They count bytes from the beginning of the body."
  239. (let* ((handle (with-current-buffer buffer (mm-dissect-buffer t)))
  240. (data (with-current-buffer (mm-handle-buffer handle)
  241. (if beg
  242. (buffer-substring (+ (point-min) beg)
  243. (if end (+ (point-min) end) (point-max)))
  244. (buffer-string))))
  245. (charset (mail-content-type-get (mm-handle-type handle)
  246. 'charset)))
  247. (mm-destroy-parts handle)
  248. (if charset
  249. (insert (mm-decode-string data (mm-charset-to-coding-system charset)))
  250. (insert data))
  251. (list (length data) charset)))
  252. ;;;###autoload
  253. (defun url-insert-file-contents (url &optional visit beg end replace)
  254. (let ((buffer (url-retrieve-synchronously url)))
  255. (if (not buffer)
  256. (error "Opening input file: No such file or directory, %s" url))
  257. (if visit (setq buffer-file-name url))
  258. (save-excursion
  259. (let* ((start (point))
  260. (size-and-charset (url-insert buffer beg end)))
  261. (kill-buffer buffer)
  262. (when replace
  263. (delete-region (point-min) start)
  264. (delete-region (point) (point-max)))
  265. (unless (cadr size-and-charset)
  266. ;; If the headers don't specify any particular charset, use the
  267. ;; usual heuristic/rules that we apply to files.
  268. (decode-coding-inserted-region start (point) url visit beg end replace))
  269. (list url (car size-and-charset))))))
  270. (defun url-file-name-completion (url directory &optional predicate)
  271. (error "Unimplemented"))
  272. (defun url-file-name-all-completions (file directory)
  273. (error "Unimplemented"))
  274. ;; All other handlers map onto their respective backends.
  275. (defmacro url-handlers-create-wrapper (method args)
  276. `(defun ,(intern (format "url-%s" method)) ,args
  277. ,(format "URL file-name-handler wrapper for `%s' call.\n---\n%s" method
  278. (or (documentation method t) "No original documentation."))
  279. (setq url (url-generic-parse-url url))
  280. (when (url-type url)
  281. (funcall (url-scheme-get-property (url-type url) (quote ,method))
  282. ,@(remove '&rest (remove '&optional args))))))
  283. (url-handlers-create-wrapper file-exists-p (url))
  284. (url-handlers-create-wrapper file-attributes (url &optional id-format))
  285. (url-handlers-create-wrapper file-symlink-p (url))
  286. (url-handlers-create-wrapper file-writable-p (url))
  287. (url-handlers-create-wrapper file-directory-p (url))
  288. (url-handlers-create-wrapper file-executable-p (url))
  289. (url-handlers-create-wrapper directory-files (url &optional full match nosort))
  290. (url-handlers-create-wrapper file-truename (url &optional counter prev-dirs))
  291. (add-hook 'find-file-hook 'url-handlers-set-buffer-mode)
  292. (defun url-handlers-set-buffer-mode ()
  293. "Set correct modes for the current buffer if visiting a remote file."
  294. (and (stringp buffer-file-name)
  295. (string-match url-handler-regexp buffer-file-name)
  296. (auto-save-mode 0)))
  297. (provide 'url-handlers)
  298. ;;; url-handlers.el ends here