|
14
|
1 ;;; url-file.el --- File retrieval code
|
|
|
2 ;; Author: wmperry
|
|
165
|
3 ;; Created: 1997/06/24 22:38:39
|
|
|
4 ;; Version: 1.21
|
|
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
|
|
165
|
128 (kill-buffer (current-buffer))
|
|
|
129 (find-file dir)
|
|
|
130 (url-dired-minor-mode t))
|
|
14
|
131
|
|
|
132 (defun url-host-is-local-p (host)
|
|
|
133 "Return t iff HOST references our local machine."
|
|
|
134 (let ((case-fold-search t))
|
|
|
135 (or
|
|
|
136 (null host)
|
|
|
137 (string= "" host)
|
|
|
138 (equal (downcase host) (downcase (system-name)))
|
|
|
139 (and (string-match "^localhost$" host) t)
|
|
|
140 (and (not (string-match (regexp-quote ".") host))
|
|
|
141 (equal (downcase host) (if (string-match (regexp-quote ".")
|
|
|
142 (system-name))
|
|
|
143 (substring (system-name) 0
|
|
|
144 (match-beginning 0))
|
|
|
145 (system-name)))))))
|
|
144
|
146
|
|
|
147 (defun url-file-build-continuation (name)
|
|
|
148 (list 'url-file-asynch-callback
|
|
|
149 name (current-buffer)
|
|
|
150 url-current-callback-func url-current-callback-data))
|
|
|
151
|
|
|
152 (defun url-file-asynch-callback (x y name buff func args &optional efs)
|
|
|
153 (if (featurep 'efs)
|
|
|
154 ;; EFS passes us an extra argument
|
|
|
155 (setq name buff
|
|
|
156 buff func
|
|
|
157 func args
|
|
|
158 args efs))
|
|
|
159 (cond
|
|
|
160 ((not name) nil)
|
|
|
161 ((not (file-exists-p name)) nil)
|
|
|
162 (t
|
|
|
163 (if (not buff)
|
|
|
164 (setq buff (generate-new-buffer " *url-asynch-file*")))
|
|
|
165 (set-buffer buff)
|
|
|
166 (insert-file-contents-literally name)
|
|
|
167 (condition-case ()
|
|
|
168 (delete-file name)
|
|
|
169 (error nil))))
|
|
|
170 (if func
|
|
|
171 (apply func args)
|
|
|
172 (url-sentinel (current-buffer) nil)))
|
|
|
173
|
|
14
|
174 (defun url-file (url)
|
|
|
175 ;; Find a file
|
|
|
176 (let* ((urlobj (url-generic-parse-url url))
|
|
|
177 (user (url-user urlobj))
|
|
86
|
178 (pass (url-password urlobj))
|
|
14
|
179 (site (url-host urlobj))
|
|
|
180 (file (url-unhex-string (url-filename urlobj)))
|
|
|
181 (dest (url-target urlobj))
|
|
|
182 (filename (if (or user (not (url-host-is-local-p site)))
|
|
|
183 (concat "/" (or user "anonymous") "@" site ":" file)
|
|
165
|
184 file))
|
|
|
185 (viewer (mm-mime-info
|
|
|
186 (mm-extension-to-mime (url-file-extension file))))
|
|
|
187 (pos-index (if url-directory-index-file
|
|
|
188 (expand-file-name url-directory-index-file filename))))
|
|
14
|
189 (url-clear-tmp-buffer)
|
|
86
|
190 (and user pass
|
|
|
191 (cond
|
|
|
192 ((featurep 'ange-ftp)
|
|
|
193 (ange-ftp-set-passwd site user pass))
|
|
|
194 ((or (featurep 'efs) (featurep 'efs-auto))
|
|
|
195 (efs-set-passwd site user pass))
|
|
|
196 (t
|
|
|
197 nil)))
|
|
165
|
198 (if (and pos-index
|
|
|
199 (file-exists-p pos-index)
|
|
|
200 (file-readable-p pos-index))
|
|
|
201 (setq filename pos-index))
|
|
|
202 (setq url-current-mime-type (mm-extension-to-mime
|
|
|
203 (url-file-extension filename)))
|
|
14
|
204 (cond
|
|
|
205 ((file-directory-p filename)
|
|
144
|
206 (if (not (string-match "/$" filename))
|
|
|
207 (setq filename (concat filename "/")))
|
|
|
208 (if (not (string-match "/$" file))
|
|
|
209 (setq file (concat file "/")))
|
|
98
|
210 (url-set-filename urlobj file)
|
|
|
211 (url-format-directory filename))
|
|
144
|
212 (url-be-asynchronous
|
|
14
|
213 (cond
|
|
|
214 ((file-exists-p filename) nil)
|
|
|
215 ((file-exists-p (concat filename ".Z"))
|
|
|
216 (setq filename (concat filename ".Z")))
|
|
|
217 ((file-exists-p (concat filename ".gz"))
|
|
|
218 (setq filename (concat filename ".gz")))
|
|
|
219 ((file-exists-p (concat filename ".z"))
|
|
|
220 (setq filename (concat filename ".z")))
|
|
144
|
221 (t nil))
|
|
|
222 (let ((new (mm-generate-unique-filename)))
|
|
|
223 (cond
|
|
|
224 ((url-host-is-local-p site)
|
|
|
225 (insert-file-contents-literally filename)
|
|
|
226 (if (featurep 'efs)
|
|
|
227 (url-file-asynch-callback nil nil nil nil nil
|
|
|
228 url-current-callback-func
|
|
|
229 url-current-callback-data)
|
|
|
230 (url-file-asynch-callback nil nil nil nil
|
|
|
231 url-current-callback-func
|
|
|
232 url-current-callback-data)))
|
|
|
233 ((featurep 'ange-ftp)
|
|
|
234 (ange-ftp-copy-file-internal filename (expand-file-name new) t
|
|
|
235 nil t
|
|
|
236 (url-file-build-continuation new)
|
|
|
237 t))
|
|
|
238 ((or (featurep 'efs) (featurep 'efs-auto))
|
|
14
|
239 (efs-copy-file-internal filename (efs-ftp-path filename)
|
|
|
240 new (efs-ftp-path new)
|
|
144
|
241 t nil 0
|
|
|
242 (url-file-build-continuation new)
|
|
|
243 0 nil)))))
|
|
14
|
244 (t
|
|
165
|
245 (let ((errobj nil))
|
|
14
|
246 (if (or url-source ; Need it in a buffer
|
|
|
247 (and (symbolp viewer)
|
|
|
248 (not (eq viewer 'w3-default-local-file)))
|
|
|
249 (stringp viewer))
|
|
|
250 (condition-case errobj
|
|
|
251 (url-insert-possibly-compressed-file filename t)
|
|
|
252 (error
|
|
|
253 (url-save-error errobj)
|
|
165
|
254 (url-retrieve (concat "www://error/nofile/" file))))))))))
|
|
14
|
255
|
|
|
256 (fset 'url-ftp 'url-file)
|
|
|
257
|
|
|
258 (provide 'url-file)
|