14
|
1 ;;; url-file.el --- File retrieval code
|
|
2 ;; Author: wmperry
|
98
|
3 ;; Created: 1997/02/10 16:16:46
|
|
4 ;; Version: 1.13
|
14
|
5 ;; Keywords: comm, data, processes
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
8 ;;; Copyright (c) 1993-1996 by William M. Perry (wmperry@cs.indiana.edu)
|
82
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
14
|
10 ;;;
|
|
11 ;;; This file is not part of GNU Emacs, but the same permissions apply.
|
|
12 ;;;
|
|
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
14 ;;; it under the terms of the GNU General Public License as published by
|
|
15 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;;; any later version.
|
|
17 ;;;
|
|
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;;; GNU General Public License for more details.
|
|
22 ;;;
|
|
23 ;;; You should have received a copy of the GNU General Public License
|
|
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;;; Boston, MA 02111-1307, USA.
|
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28
|
|
29 (require 'url-vars)
|
|
30 (require 'mule-sysdp)
|
|
31 (require 'url-parse)
|
|
32
|
|
33 (defun url-insert-possibly-compressed-file (fname &rest args)
|
|
34 ;; Insert a file into a buffer, checking for compressed versions.
|
|
35 (let ((compressed nil)
|
|
36 ;;
|
|
37 ;; F*** *U** **C* ***K!!!
|
|
38 ;; We cannot just use insert-file-contents-literally here, because
|
|
39 ;; then we would lose big time with ange-ftp. *sigh*
|
|
40 (crypt-encoding-alist nil)
|
|
41 (jka-compr-compression-info-list nil)
|
|
42 (jam-zcat-filename-list nil)
|
|
43 (file-coding-system-for-read mule-no-coding-system)
|
|
44 (coding-system-for-read mule-no-coding-system))
|
|
45 (setq compressed
|
|
46 (cond
|
98
|
47 ((file-exists-p fname)
|
|
48 (if (string-match "\\.\\(z\\|gz\\|Z\\)$" fname)
|
|
49 (case (intern (match-string 1 fname))
|
|
50 ((z gz)
|
|
51 (setq url-current-mime-headers (cons
|
|
52 (cons
|
|
53 "content-transfer-encoding"
|
|
54 "gzip")
|
|
55 url-current-mime-headers)))
|
|
56 (Z
|
|
57 (setq url-current-mime-headers (cons
|
|
58 (cons
|
|
59 "content-transfer-encoding"
|
|
60 "compress")
|
|
61 url-current-mime-headers))))
|
|
62 nil))
|
14
|
63 ((file-exists-p (concat fname ".Z"))
|
98
|
64 (setq fname (concat fname ".Z")
|
|
65 url-current-mime-headers (cons (cons
|
|
66 "content-transfer-encoding"
|
|
67 "compress")
|
|
68 url-current-mime-headers)))
|
14
|
69 ((file-exists-p (concat fname ".gz"))
|
98
|
70 (setq fname (concat fname ".gz")
|
|
71 url-current-mime-headers (cons (cons
|
|
72 "content-transfer-encoding"
|
|
73 "gzip")
|
|
74 url-current-mime-headers)))
|
14
|
75 ((file-exists-p (concat fname ".z"))
|
98
|
76 (setq fname (concat fname ".z")
|
|
77 url-current-mime-headers (cons (cons
|
|
78 "content-transfer-encoding"
|
|
79 "gzip")
|
|
80 url-current-mime-headers)))
|
14
|
81 (t
|
|
82 (error "File not found %s" fname))))
|
98
|
83 (apply 'insert-file-contents fname args)
|
|
84 (set-buffer-modified-p nil)))
|
|
85
|
|
86 (defvar url-dired-minor-mode-map
|
|
87 (let ((map (make-sparse-keymap)))
|
|
88 (define-key map "\C-m" 'url-dired-find-file)
|
|
89 (if url-running-xemacs
|
|
90 (define-key map [button2] 'url-dired-find-file-mouse)
|
|
91 (define-key map [mouse-2] 'url-dired-find-file-mouse))
|
|
92 map)
|
|
93 "Keymap used when browsing directories.")
|
|
94
|
|
95 (defvar url-dired-minor-mode nil
|
|
96 "Whether we are in url-dired-minor-mode")
|
|
97
|
|
98 (make-variable-buffer-local 'url-dired-minor-mode)
|
|
99
|
|
100 (defun url-dired-find-file ()
|
|
101 "In dired, visit the file or directory named on this line, using Emacs-W3."
|
|
102 (interactive)
|
|
103 (w3-open-local (dired-get-filename)))
|
|
104
|
|
105 (defun url-dired-find-file-mouse (event)
|
|
106 "In dired, visit the file or directory name you click on, using Emacs-W3."
|
|
107 (interactive "@e")
|
|
108 (if (event-point event)
|
|
109 (progn
|
|
110 (goto-char (event-point event))
|
|
111 (url-dired-find-file))))
|
|
112
|
|
113 (defun url-dired-minor-mode (&optional arg)
|
|
114 "Minor mode for directory browsing with Emacs-W3."
|
|
115 (interactive "P")
|
|
116 (cond
|
|
117 ((null arg)
|
|
118 (setq url-dired-minor-mode (not url-dired-minor-mode)))
|
|
119 ((equal 0 arg)
|
|
120 (setq url-dired-minor-mode nil))
|
|
121 (t
|
|
122 (setq url-dired-minor-mode t))))
|
|
123
|
|
124 (add-minor-mode 'url-dired-minor-mode " URL" url-dired-minor-mode-map)
|
14
|
125
|
|
126 (defun url-format-directory (dir)
|
|
127 ;; Format the files in DIR into hypertext
|
98
|
128 (if (and url-directory-index-file
|
|
129 (file-exists-p (expand-file-name url-directory-index-file dir))
|
|
130 (file-readable-p (expand-file-name url-directory-index-file dir)))
|
14
|
131 (save-excursion
|
|
132 (set-buffer url-working-buffer)
|
|
133 (erase-buffer)
|
98
|
134 (insert-file-contents-literally
|
|
135 (expand-file-name url-directory-index-file dir)))
|
|
136 (kill-buffer (current-buffer))
|
|
137 (find-file dir)
|
|
138 (url-dired-minor-mode t)))
|
|
139 ; (let ((files (directory-files dir nil)) file
|
|
140 ; div attr mod-time size typ title desc)
|
|
141 ; (save-excursion
|
|
142 ; (if (string-match "/\\([^/]+\\)/$" dir)
|
|
143 ; (setq title (concat ".../" (url-match dir 1) "/"))
|
|
144 ; (setq title "/"))
|
|
145 ; (setq div (1- (length files)))
|
|
146 ; (set-buffer url-working-buffer)
|
|
147 ; (erase-buffer)
|
|
148 ; (insert "<html>\n"
|
|
149 ; " <head>\n"
|
|
150 ; " <title>" title "</title>\n"
|
|
151 ; " </head>\n"
|
|
152 ; " <body>\n"
|
|
153 ; " <h1 align=center> Index of " title "</h1>\n"
|
|
154 ; " <table border=0>\n"
|
|
155 ; " <tr><th>Name<th>Last Modified<th>Size</tr>\n"
|
|
156 ; " <tr><td colspan=3><hr></tr>\n")
|
|
157 ; (while files
|
|
158 ; (url-lazy-message "Building directory list... (%d%%)"
|
|
159 ; (/ (* 100 (- div (length files))) div))
|
|
160 ; (setq file (expand-file-name (car files) dir)
|
|
161 ; attr (file-attributes file)
|
|
162 ; file (car files)
|
|
163 ; mod-time (nth 5 attr)
|
|
164 ; size (nth 7 attr)
|
|
165 ; typ (or (mm-extension-to-mime (url-file-extension file)) ""))
|
|
166 ; (setq file (url-hexify-string file))
|
|
167 ; (if (equal '(0 0) mod-time) ; Set to null if unknown or
|
|
168 ; (setq mod-time "Unknown")
|
|
169 ; (setq mod-time (current-time-string mod-time)))
|
|
170 ; (if (or (equal size 0) (equal size -1) (null size))
|
|
171 ; (setq size "-")
|
|
172 ; (setq size
|
|
173 ; (cond
|
|
174 ; ((< size 1024) "1K")
|
|
175 ; ((< size 1048576) (concat (int-to-string
|
|
176 ; (max 1 (/ size 1024))) "K"))
|
|
177 ; (t
|
|
178 ; (let* ((megs (max 1 (/ size 1048576)))
|
|
179 ; (kilo (/ (- size (* megs 1048576)) 1024)))
|
|
180 ; (concat (int-to-string megs)
|
|
181 ; (if (> kilo 0)
|
|
182 ; (concat "." (int-to-string kilo))
|
|
183 ; "") "M"))))))
|
|
184 ; (cond
|
|
185 ; ((or (equal "." (car files))
|
|
186 ; (equal "/.." (car files)))
|
|
187 ; (setq desc nil))
|
|
188 ; ((equal ".." (car files))
|
|
189 ; (if (not (= ?/ (aref file (1- (length file)))))
|
|
190 ; (setq file (concat file "/"))))
|
|
191 ; ((stringp (nth 0 attr)) ; Symbolic link handling
|
|
192 ; (setq desc "[LNK]"))
|
|
193 ; ((nth 0 attr) ; Directory handling
|
|
194 ; (setq desc "[DIR]"))
|
|
195 ; ((string-match "image" typ)
|
|
196 ; (setq desc "[IMG]"))
|
|
197 ; ((string-match "application" typ)
|
|
198 ; (setq desc "[APP]"))
|
|
199 ; ((string-match "text" typ)
|
|
200 ; (setq desc "[TXT]"))
|
|
201 ; ((auto-save-file-name-p (car files))
|
|
202 ; (setq desc "[BAK]"))
|
|
203 ; (t
|
|
204 ; (setq desc "[UNK]")))
|
|
205 ; (if desc
|
|
206 ; (insert "<tr><td>" desc " <a href=\"./" file "\">" (car files)
|
|
207 ; "</a><td>" mod-time "<td><p align=right>" size
|
|
208 ; "</tr>\n"))
|
|
209 ; (setq files (cdr files)))
|
|
210 ; (insert " </table>\n"
|
|
211 ; " </body>\n"
|
|
212 ; "</html>\n"
|
|
213 ; "<!-- Automatically generated by URL v" url-version
|
|
214 ; " -->\n")))
|
14
|
215
|
|
216 (defun url-host-is-local-p (host)
|
|
217 "Return t iff HOST references our local machine."
|
|
218 (let ((case-fold-search t))
|
|
219 (or
|
|
220 (null host)
|
|
221 (string= "" host)
|
|
222 (equal (downcase host) (downcase (system-name)))
|
|
223 (and (string-match "^localhost$" host) t)
|
|
224 (and (not (string-match (regexp-quote ".") host))
|
|
225 (equal (downcase host) (if (string-match (regexp-quote ".")
|
|
226 (system-name))
|
|
227 (substring (system-name) 0
|
|
228 (match-beginning 0))
|
|
229 (system-name)))))))
|
|
230
|
|
231 (defun url-file (url)
|
|
232 ;; Find a file
|
|
233 (let* ((urlobj (url-generic-parse-url url))
|
|
234 (user (url-user urlobj))
|
86
|
235 (pass (url-password urlobj))
|
14
|
236 (site (url-host urlobj))
|
|
237 (file (url-unhex-string (url-filename urlobj)))
|
|
238 (dest (url-target urlobj))
|
|
239 (filename (if (or user (not (url-host-is-local-p site)))
|
|
240 (concat "/" (or user "anonymous") "@" site ":" file)
|
|
241 file)))
|
|
242
|
|
243 (if (and file (url-host-is-local-p site)
|
|
244 (memq system-type '(ms-windows ms-dos windows-nt os2)))
|
|
245 (let ((x (1- (length file)))
|
|
246 (y 0))
|
|
247 (while (<= y x)
|
|
248 (if (= (aref file y) ?\\ )
|
|
249 (aset file y ?/))
|
|
250 (setq y (1+ y)))))
|
|
251
|
|
252 (url-clear-tmp-buffer)
|
86
|
253 (and user pass
|
|
254 (cond
|
|
255 ((featurep 'ange-ftp)
|
|
256 (ange-ftp-set-passwd site user pass))
|
|
257 ((or (featurep 'efs) (featurep 'efs-auto))
|
|
258 (efs-set-passwd site user pass))
|
|
259 (t
|
|
260 nil)))
|
14
|
261 (cond
|
|
262 ((file-directory-p filename)
|
98
|
263 (if (string-match "/$" filename)
|
|
264 nil
|
|
265 (setq filename (concat filename "/")))
|
|
266 (if (string-match "/$" file)
|
|
267 nil
|
|
268 (setq file (concat file "/")))
|
|
269 (url-set-filename urlobj file)
|
|
270 (url-format-directory filename))
|
14
|
271 ((and (boundp 'w3-dump-to-disk) (symbol-value 'w3-dump-to-disk))
|
|
272 (cond
|
|
273 ((file-exists-p filename) nil)
|
|
274 ((file-exists-p (concat filename ".Z"))
|
|
275 (setq filename (concat filename ".Z")))
|
|
276 ((file-exists-p (concat filename ".gz"))
|
|
277 (setq filename (concat filename ".gz")))
|
|
278 ((file-exists-p (concat filename ".z"))
|
|
279 (setq filename (concat filename ".z")))
|
|
280 (t
|
|
281 (error "File not found %s" filename)))
|
|
282 (cond
|
|
283 ((url-host-is-local-p site)
|
|
284 (copy-file
|
|
285 filename
|
|
286 (read-file-name "Save to: " nil (url-basepath filename t)) t))
|
|
287 ((featurep 'ange-ftp)
|
|
288 (ange-ftp-copy-file-internal
|
|
289 filename
|
|
290 (expand-file-name
|
|
291 (read-file-name "Save to: " nil (url-basepath filename t))) t
|
|
292 nil t nil t))
|
|
293 ((or (featurep 'efs) (featurep 'efs-auto))
|
|
294 (let ((new (expand-file-name
|
|
295 (read-file-name "Save to: " nil
|
|
296 (url-basepath filename t)))))
|
|
297 (efs-copy-file-internal filename (efs-ftp-path filename)
|
|
298 new (efs-ftp-path new)
|
|
299 t nil 0 nil 0 nil)))
|
|
300 (t (copy-file
|
|
301 filename
|
|
302 (read-file-name "Save to: " nil (url-basepath filename t)) t)))
|
|
303 (if (get-buffer url-working-buffer)
|
|
304 (kill-buffer url-working-buffer)))
|
|
305 (t
|
|
306 (let ((viewer (mm-mime-info
|
|
307 (mm-extension-to-mime (url-file-extension file))))
|
|
308 (errobj nil))
|
|
309 (if (or url-source ; Need it in a buffer
|
|
310 (and (symbolp viewer)
|
|
311 (not (eq viewer 'w3-default-local-file)))
|
|
312 (stringp viewer))
|
|
313 (condition-case errobj
|
|
314 (url-insert-possibly-compressed-file filename t)
|
|
315 (error
|
|
316 (url-save-error errobj)
|
|
317 (url-retrieve (concat "www://error/nofile/" file))))))))
|
|
318 (setq url-current-type (if site "ftp" "file")
|
|
319 url-current-object urlobj
|
|
320 url-find-this-link dest
|
|
321 url-current-user user
|
|
322 url-current-server site
|
|
323 url-current-mime-type (mm-extension-to-mime
|
|
324 (url-file-extension file))
|
|
325 url-current-file file)))
|
|
326
|
|
327 (fset 'url-ftp 'url-file)
|
|
328
|
|
329 (provide 'url-file)
|