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
36
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
;;; url-handlers.el --- file-name-handler stuff for URL loading
;; Copyright (c) 1996, 1997, 1998, 1999, 2004, 2005 Free Software Foundation, Inc.
;; 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 2, 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;; Code:
;; (require 'url)
(eval-when-compile (require 'url-parse))
;; (require 'url-util)
(eval-when-compile (require 'mm-decode))
;; (require 'mailcap)
;; The following functions in the byte compiler's warnings are known not
;; to cause any real problem for the following reasons:
;; - mm-save-part-to-file, mm-destroy-parts: always used
;; after mm-dissect-buffer and defined in the same file.
;; The following are autoloaded instead of `require'd to avoid eagerly
;; loading all of URL when turning on url-handler-mode in the .emacs.
(autoload 'url-retrieve-synchronously "url" "Retrieve url synchronously.")
(autoload 'url-expand-file-name "url-expand" "Convert url to a fully specified url, and canonicalize it.")
(autoload 'mm-dissect-buffer "mm-decode" "Dissect the current buffer and return a list of MIME handles.")
(autoload 'url-scheme-get-property "url-methods" "Get property of a URL SCHEME.")
(eval-when-compile
(require 'cl))
;; Implementation status
;; ---------------------
;; Function Status
;; ------------------------------------------------------------
;; add-name-to-file Needs DAV Bindings
;; copy-file Broken (assumes 1st item is URL)
;; delete-directory Finished (DAV)
;; delete-file Finished (DAV)
;; diff-latest-backup-file
;; directory-file-name unnecessary (what about VMS)?
;; directory-files Finished (DAV)
;; dired-call-process
;; dired-compress-file
;; dired-uncache
;; expand-file-name Finished
;; file-accessible-directory-p
;; file-attributes Finished, better with DAV
;; file-directory-p Needs DAV, finished
;; file-executable-p Finished
;; file-exists-p Finished
;; file-local-copy
;; file-modes
;; file-name-all-completions Finished (DAV)
;; file-name-as-directory
;; file-name-completion Finished (DAV)
;; file-name-directory
;; file-name-nondirectory
;; file-name-sans-versions why?
;; file-newer-than-file-p
;; file-ownership-preserved-p No way to know
;; file-readable-p Finished
;; file-regular-p !directory_p
;; file-symlink-p Needs DAV bindings
;; file-truename Needs DAV bindings
;; file-writable-p Check for LOCK?
;; find-backup-file-name why?
;; get-file-buffer why?
;; insert-directory Use DAV
;; insert-file-contents Finished
;; load
;; make-directory Finished (DAV)
;; make-symbolic-link Needs DAV bindings
;; rename-file Finished (DAV)
;; set-file-modes Use mod_dav specific executable flag?
;; set-visited-file-modtime Impossible?
;; shell-command Impossible?
;; unhandled-file-name-directory
;; vc-registered Finished (DAV)
;; verify-visited-file-modtime
;; write-region
(defvar url-handler-regexp
"\\`\\(https?\\|ftp\\|file\\|nfs\\)://"
"*A regular expression for matching URLs handled by file-name-handler-alist.
Some valid URL protocols just do not make sense to visit interactively
\(about, data, info, irc, mailto, etc\). This regular expression
avoids conflicts with local files that look like URLs \(Gnus is
particularly bad at this\).")
;;;###autoload
(define-minor-mode url-handler-mode
"Use URL to handle URL-like file names."
:global t :group 'url
(if (not (boundp 'file-name-handler-alist))
;; Can't be turned ON anyway.
(setq url-handler-mode nil)
;; Remove old entry, if any.
(setq file-name-handler-alist
(delq (rassq 'url-file-handler file-name-handler-alist)
file-name-handler-alist))
(if url-handler-mode
(push (cons url-handler-regexp 'url-file-handler)
file-name-handler-alist))))
(defun url-run-real-handler (operation args)
(let ((inhibit-file-name-handlers (cons 'url-file-handler
(if (eq operation inhibit-file-name-operation)
inhibit-file-name-handlers)))
(inhibit-file-name-operation operation))
(apply operation args)))
(defun url-file-handler (operation &rest args)
"Function called from the `file-name-handler-alist' routines.
OPERATION is what needs to be done (`file-exists-p', etc). ARGS are
the arguments that would have been passed to OPERATION."
(let ((fn (or (get operation 'url-file-handlers)
(intern-soft (format "url-%s" operation))))
(val nil)
(hooked nil))
(if (and fn (fboundp fn))
(setq hooked t
val (apply fn args))
(setq hooked nil
val (url-run-real-handler operation args)))
(url-debug 'handlers "%s %S%S => %S" (if hooked "Hooked" "Real")
operation args val)
val))
(defun url-file-handler-identity (&rest args)
;; Identity function
(car args))
;; These are operations that we can fully support
(put 'file-readable-p 'url-file-handlers 'url-file-exists-p)
(put 'substitute-in-file-name 'url-file-handlers 'url-file-handler-identity)
(put 'file-name-absolute-p 'url-file-handlers (lambda (&rest ignored) t))
(put 'expand-file-name 'url-file-handlers 'url-handler-expand-file-name)
;; These are operations that we do not support yet (DAV!!!)
(put 'file-writable-p 'url-file-handlers 'ignore)
(put 'file-symlink-p 'url-file-handlers 'ignore)
;; Just like for ange-ftp: let's not waste time trying to look for RCS/foo,v
;; files and such since we can't do anything clever with them anyway.
(put 'vc-registered 'url-file-handlers 'ignore)
(defun url-handler-expand-file-name (file &optional base)
(if (file-name-absolute-p file)
(expand-file-name file "/")
(url-expand-file-name file base)))
;; The actual implementation
;;;###autoload
(defun url-copy-file (url newname &optional ok-if-already-exists keep-time)
"Copy URL to NEWNAME. Both args must be strings.
Signals a `file-already-exists' error if file NEWNAME already exists,
unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
A number as third arg means request confirmation if NEWNAME already exists.
This is what happens in interactive use with M-x.
Fourth arg KEEP-TIME non-nil means give the new file the same
last-modified time as the old one. (This works on only some systems.)
A prefix arg makes KEEP-TIME non-nil."
(if (and (file-exists-p newname)
(not ok-if-already-exists))
(error "Opening output file: File already exists, %s" newname))
(let ((buffer (url-retrieve-synchronously url))
(handle nil))
(if (not buffer)
(error "Opening input file: No such file or directory, %s" url))
(with-current-buffer buffer
(setq handle (mm-dissect-buffer t)))
(mm-save-part-to-file handle newname)
(kill-buffer buffer)
(mm-destroy-parts handle)))
;;;###autoload
(defun url-file-local-copy (url &rest ignored)
"Copy URL into a temporary file on this machine.
Returns the name of the local copy, or nil, if FILE is directly
accessible."
(let ((filename (make-temp-name "url")))
(url-copy-file url filename)
filename))
;;;###autoload
(defun url-insert-file-contents (url &optional visit beg end replace)
(let ((buffer (url-retrieve-synchronously url))
(handle nil)
(data nil))
(if (not buffer)
(error "Opening input file: No such file or directory, %s" url))
(if visit (setq buffer-file-name url))
(with-current-buffer buffer
(setq handle (mm-dissect-buffer t))
(set-buffer (mm-handle-buffer handle))
(setq data (if beg (buffer-substring beg end)
(buffer-string))))
(kill-buffer buffer)
(mm-destroy-parts handle)
(if replace (delete-region (point-min) (point-max)))
(save-excursion
(let ((start (point)))
(insert data)
;; FIXME: for text/plain data, we sometimes receive a `charset'
;; annotation which we could use as a hint of the locale in use
;; at the remote site. Not sure how/if that should be done. --Stef
(decode-coding-inserted-region
start (point) url visit beg end replace)))
(list url (length data))))
(defun url-file-name-completion (url directory)
(error "Unimplemented"))
(defun url-file-name-all-completions (file directory)
(error "Unimplemented"))
;; All other handlers map onto their respective backends.
(defmacro url-handlers-create-wrapper (method args)
`(defun ,(intern (format "url-%s" method)) ,args
,(format "URL file-name-handler wrapper for `%s' call.\n---\n%s" method
(or (documentation method t) "No original documentation."))
(setq url (url-generic-parse-url url))
(when (url-type url)
(funcall (url-scheme-get-property (url-type url) (quote ,method))
,@(remove '&rest (remove '&optional args))))))
(url-handlers-create-wrapper file-exists-p (url))
(url-handlers-create-wrapper file-attributes (url &optional id-format))
(url-handlers-create-wrapper file-symlink-p (url))
(url-handlers-create-wrapper file-writable-p (url))
(url-handlers-create-wrapper file-directory-p (url))
(url-handlers-create-wrapper file-executable-p (url))
(if (featurep 'xemacs)
(progn
;; XEmacs specific prototypes
(url-handlers-create-wrapper
directory-files (url &optional full match nosort files-only))
(url-handlers-create-wrapper
file-truename (url &optional default)))
;; Emacs specific prototypes
(url-handlers-create-wrapper
directory-files (url &optional full match nosort))
(url-handlers-create-wrapper
file-truename (url &optional counter prev-dirs)))
(add-hook 'find-file-hook 'url-handlers-set-buffer-mode)
(defun url-handlers-set-buffer-mode ()
"Set correct modes for the current buffer if visiting a remote file."
(and (stringp buffer-file-name)
(string-match url-handler-regexp buffer-file-name)
(auto-save-mode 0)))
(provide 'url-handlers)
;; arch-tag: 7300b99c-cc83-42ff-9147-79b2723c62ac
;;; url-handlers.el ends here
|